Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
math_Rational.cpp
Go to the documentation of this file.
1 
14 #include "math_Rational.h"
15 
16 
17 
18 const std::string Math_Rational::class_name = "Math_Rational";
19 
20 //====================================================================
22 {
23  const string str_vlevel = params.get_string("verbose_level");
24 
25  m_vl = vout.set_verbose_level(str_vlevel);
26 
27  //- fetch and check input parameters
28  int Np, n_exp, d_exp;
29  double x_min, x_max;
30 
31  int err = 0;
32  err += params.fetch_int("number_of_poles", Np);
33  err += params.fetch_int("exponent_numerator", n_exp);
34  err += params.fetch_int("exponent_denominator", d_exp);
35  err += params.fetch_double("lower_bound", x_min);
36  err += params.fetch_double("upper_bound", x_max);
37 
38  if (err) {
39  vout.crucial(m_vl, "Error at %s: input parameter not found.\n", class_name.c_str());
40  exit(EXIT_FAILURE);
41  }
42 
43  set_parameters(Np, n_exp, d_exp, x_min, x_max);
44 }
45 
46 
47 //====================================================================
48 void Math_Rational::set_parameters(const int Np, const int n_exp, const int d_exp,
49  const double x_min, const double x_max)
50 {
51  //- print input parameters
52  vout.general(m_vl, "%s:\n", class_name.c_str());
53  vout.general(m_vl, " Np = %d\n", Np);
54  vout.general(m_vl, " n_exp = %d\n", n_exp);
55  vout.general(m_vl, " d_exp = %d\n", d_exp);
56  vout.general(m_vl, " x_min = %10.6f\n", x_min);
57  vout.general(m_vl, " x_max = %10.6f\n", x_max);
58 
59  //- range check
60  int err = 0;
61  err += ParameterCheck::non_zero(Np);
62  err += ParameterCheck::non_zero(n_exp);
63  err += ParameterCheck::non_zero(d_exp);
64  // NB. x_min,x_max == 0 is allowed.
65 
66  if (err) {
67  vout.crucial(m_vl, "Error at %s: parameter range check failed.\n", class_name.c_str());
68  exit(EXIT_FAILURE);
69  }
70 
71  //- store values
72  m_Np = Np;
73  m_n_exp = n_exp;
74  m_d_exp = d_exp;
75  m_x_min = x_min;
76  m_x_max = x_max;
77 
78  //- post-process
79  m_res.resize(m_Np);
80  m_pole.resize(m_Np);
81 
82  set_coeff();
83 }
84 
85 
86 //====================================================================
87 void Math_Rational::get_parameters(double& norm, std::vector<double>& res,
88  std::vector<double>& pole)
89 {
90  if (res.size() != m_Np) {
91  vout.crucial(m_vl, "Error at %s: size of cl is not correct\n", class_name.c_str());
92  exit(EXIT_FAILURE);
93  }
94 
95  if (pole.size() != m_Np) {
96  vout.crucial(m_vl, "Error at %s: size of bl is not correct\n", class_name.c_str());
97  exit(EXIT_FAILURE);
98  }
99 
100  norm = m_norm;
101  for (int i = 0; i < m_Np; ++i) {
102  res[i] = m_res[i];
103  pole[i] = m_pole[i];
104  }
105 }
106 
107 
108 //====================================================================
110 {
111  read_file();
112 }
113 
114 
115 //====================================================================
117 {
118  // setting input file
119  char filename[FILENAME_MAX];
120 
121  snprintf(filename, FILENAME_MAX,
122  "parameter_rational.%1d_%1d%s_%02d_%010.8f_%07.3f",
123  abs(m_n_exp), m_d_exp,
124  ((m_n_exp < 0) ? "inv" : ""),
125  m_Np,
126  m_x_min, m_x_max);
127 
128  vout.general(m_vl, "%s: expected filename: %s\n", class_name.c_str(), filename);
129 
130  int Np, n_exp, d_exp;
131  double x_min, x_max;
132 
133  // read parameters from file
134  std::fstream parameterfile;
135  parameterfile.open(filename, std::ios::in);
136  if (!parameterfile.is_open()) {
137  vout.crucial(m_vl, "Error at %s: failed to open parameter file. %s(%d)\n",
138  class_name.c_str(), __FILE__, __LINE__);
139  exit(EXIT_FAILURE);
140  }
141 
142  parameterfile >> Np >> n_exp >> d_exp;
143  parameterfile >> x_min >> x_max;
144  parameterfile >> m_error;
145  parameterfile >> m_norm;
146  for (int i = 0; i < Np; i++) {
147  parameterfile >> m_res[i] >> m_pole[i];
148  }
149  parameterfile.close();
150 
151  vout.general(m_vl, "%s: read parameter file successful.\n", class_name.c_str());
152 
153  /*
154  vout.general(m_vl, " Rational approximation (read from file): %s\n",
155  filename.c_str());
156  vout.general(m_vl, " Np = %d\n", m_Np);
157  vout.general(m_vl, " n_exp = %d, d_exp = %d\n", m_n_exp,m_d_exp);
158  vout.general(m_vl, " x_min = %12.8f\n", m_x_min);
159  vout.general(m_vl, " x_max = %12.8f\n", m_x_max);
160  vout.general(m_vl, " error = %18.16e\n", m_error);
161  vout.general(m_vl, " RA_a0 = %18.16e\n", m_norm );
162  for(int i = 0; i < n; i++){
163  vout.general(m_vl, " RA_b[%d] = %18.16e, RA_c[%d] = %18.16e\n",
164  i, m_res[i], i, m_pole[i]);
165  }
166  */
167 
168  assert(m_Np == Np);
169  assert(m_n_exp == n_exp);
170  assert(m_d_exp == d_exp);
171  assert(fabs(m_x_min - x_min) < 1.e-12);
172  // assert(fabs(m_x_min - x_min) < 1.e-12);
173  // assert(fabs(m_x_max - x_max) < 1.e-12);
174  assert(fabs(m_x_min - x_min) < 1.e-8);
175  assert(fabs(m_x_max - x_max) < 1.e-8);
176 }
177 
178 
179 //====================================================================
180 double Math_Rational::func(double x)
181 {
182  double y = m_norm;
183 
184  for (int k = 0; k < m_Np; ++k) {
185  y += m_res[k] / (x + m_pole[k]);
186  }
187 
188  return y;
189 }
190 
191 
192 //====================================================================
193 //============================================================END=====
void get_parameters(double &norm, std::vector< double > &res, std::vector< double > &pole)
BridgeIO vout
Definition: bridgeIO.cpp:495
static const std::string class_name
Definition: math_Rational.h:44
void general(const char *format,...)
Definition: bridgeIO.cpp:195
void set_parameters(const Parameters &params)
int fetch_double(const string &key, double &value) const
Definition: parameters.cpp:211
Class for parameters.
Definition: parameters.h:46
std::vector< double > m_pole
Definition: math_Rational.h:54
int fetch_int(const string &key, int &value) const
Definition: parameters.cpp:230
double func(double x)
Bridge::VerboseLevel m_vl
Definition: math_Rational.h:47
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
int non_zero(const double v)
Definition: checker.cpp:31
std::vector< double > m_res
Definition: math_Rational.h:53
string get_string(const string &key) const
Definition: parameters.cpp:116
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:131