Bridge++  Ver. 1.2.x
 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 #ifdef USE_PARAMETERS_FACTORY
17 #include "parameters_factory.h"
18 #endif
19 
20 //- parameter entries
21 namespace {
22  void append_entry(Parameters& param)
23  {
24  param.Register_int("number_of_poles", 0);
25  param.Register_int("exponent_numerator", 0);
26  param.Register_int("exponent_denominator", 0);
27  param.Register_double("lower_bound", 0.0);
28  param.Register_double("upper_bound", 0.0);
29  param.Register_int("number_of_partitions", 0);
30 
31  param.Register_string("verbose_level", "NULL");
32  }
33 
34 
35 #ifdef USE_PARAMETERS_FACTORY
36  bool init_param = ParametersFactory::Register("Math_Rational", append_entry);
37 #endif
38 }
39 //- end
40 
41 //- parameters class
43 //- end
44 
45 const std::string Math_Rational::class_name = "Math_Rational";
46 
47 //====================================================================
49 {
50  const string str_vlevel = params.get_string("verbose_level");
51 
52  m_vl = vout.set_verbose_level(str_vlevel);
53 
54  //- fetch and check input parameters
55  int Np, n_exp, d_exp;
56  double x_min, x_max;
57 
58  int err = 0;
59  err += params.fetch_int("number_of_poles", Np);
60  err += params.fetch_int("exponent_numerator", n_exp);
61  err += params.fetch_int("exponent_denominator", d_exp);
62  err += params.fetch_double("lower_bound", x_min);
63  err += params.fetch_double("upper_bound", x_max);
64 
65  if (err) {
66  vout.crucial(m_vl, "%s: fetch error, input parameter not found.\n", class_name.c_str());
67  abort();
68  }
69 
70 
71  set_parameters(Np, n_exp, d_exp, x_min, x_max);
72 }
73 
74 
75 //====================================================================
76 void Math_Rational::set_parameters(const int Np, const int n_exp, const int d_exp,
77  const double x_min, const double x_max)
78 {
79  //- print input parameters
80  vout.general(m_vl, "Parameters of %s:\n", class_name.c_str());
81  vout.general(m_vl, " Np = %d\n", Np);
82  vout.general(m_vl, " n_exp = %d\n", n_exp);
83  vout.general(m_vl, " d_exp = %d\n", d_exp);
84  vout.general(m_vl, " x_min = %10.6f\n", x_min);
85  vout.general(m_vl, " x_max = %10.6f\n", x_max);
86 
87  //- range check
88  int err = 0;
89  err += ParameterCheck::non_zero(Np);
90  err += ParameterCheck::non_zero(n_exp);
91  err += ParameterCheck::non_zero(d_exp);
92  // NB. x_min,x_max == 0 is allowed.
93 
94  if (err) {
95  vout.crucial(m_vl, "%s: parameter range check failed.\n", class_name.c_str());
96  abort();
97  }
98 
99  //- store values
100  m_Np = Np;
101  m_n_exp = n_exp;
102  m_d_exp = d_exp;
103  m_x_min = x_min;
104  m_x_max = x_max;
105 
106  //- post-process
107  m_res.resize(m_Np);
108  m_pole.resize(m_Np);
109 
110  set_coeff();
111 }
112 
113 
114 //====================================================================
115 void Math_Rational::get_parameters(double& norm, std::valarray<double>& res,
116  std::valarray<double>& pole)
117 {
118  if (res.size() != m_Np) {
119  vout.crucial(m_vl, "%s: size of cl is not correct\n", class_name.c_str());
120  abort();
121  }
122 
123  if (pole.size() != m_Np) {
124  vout.crucial(m_vl, "%s: size of bl is not correct\n", class_name.c_str());
125  abort();
126  }
127 
128  norm = m_norm;
129  for (int i = 0; i < m_Np; ++i) {
130  res[i] = m_res[i];
131  pole[i] = m_pole[i];
132  }
133 }
134 
135 
136 //====================================================================
138 {
139  read_file();
140 }
141 
142 
143 //====================================================================
145 {
146  // setting input file
147  char filename[FILENAME_MAX];
148 
149  snprintf(filename, FILENAME_MAX,
150  "parameter_rational.%1d_%1d%s_%02d_%010.8f_%07.3f",
151  abs(m_n_exp), m_d_exp,
152  ((m_n_exp < 0) ? "inv" : ""),
153  m_Np,
154  m_x_min, m_x_max);
155 
156  vout.general(m_vl, "%s: expected filename: %s\n", class_name.c_str(), filename);
157 
158  int Np, n_exp, d_exp;
159  double x_min, x_max;
160 
161  // read parameters from file
162  std::fstream parameterfile;
163  parameterfile.open(filename, std::ios::in);
164  if (!parameterfile.is_open()) {
165  vout.crucial(m_vl, "Failed to open parameter file. %s(%d)\n",
166  __FILE__, __LINE__);
167  abort();
168  }
169 
170  parameterfile >> Np >> n_exp >> d_exp;
171  parameterfile >> x_min >> x_max;
172  parameterfile >> m_error;
173  parameterfile >> m_norm;
174  for (int i = 0; i < Np; i++) {
175  parameterfile >> m_res[i] >> m_pole[i];
176  }
177  parameterfile.close();
178 
179  vout.general(m_vl, "%s: read parameter file successful.\n", class_name.c_str());
180 
181  /*
182  vout.general(m_vl, " Rational approximation (read from file): %s\n",
183  filename.c_str());
184  vout.general(m_vl, " Np = %d\n", m_Np);
185  vout.general(m_vl, " n_exp = %d, d_exp = %d\n", m_n_exp,m_d_exp);
186  vout.general(m_vl, " x_min = %12.8f\n", m_x_min);
187  vout.general(m_vl, " x_max = %12.8f\n", m_x_max);
188  vout.general(m_vl, " error = %18.16e\n", m_error);
189  vout.general(m_vl, " RA_a0 = %18.16e\n", m_norm );
190  for(int i = 0; i < n; i++){
191  vout.general(m_vl, " RA_b[%d] = %18.16e, RA_c[%d] = %18.16e\n",
192  i, m_res[i], i, m_pole[i]);
193  }
194  */
195 
196  assert(m_Np == Np);
197  assert(m_n_exp == n_exp);
198  assert(m_d_exp == d_exp);
199  assert(fabs(m_x_min - x_min) < 1.e-12);
200  // assert(fabs(m_x_min - x_min) < 1.e-12);
201  // assert(fabs(m_x_max - x_max) < 1.e-12);
202  assert(fabs(m_x_min - x_min) < 1.e-8);
203  assert(fabs(m_x_max - x_max) < 1.e-8);
204 }
205 
206 
207 //====================================================================
208 double Math_Rational::func(double x)
209 {
210  double y = m_norm;
211 
212  for (int k = 0; k < m_Np; ++k) {
213  y += m_res[k] / (x + m_pole[k]);
214  }
215 
216  return y;
217 }
218 
219 
220 //====================================================================
221 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:207
void Register_string(const string &, const string &)
Definition: parameters.cpp:352
static const std::string class_name
Definition: math_Rational.h:51
void general(const char *format,...)
Definition: bridgeIO.cpp:38
void Register_int(const string &, const int)
Definition: parameters.cpp:331
void set_parameters(const Parameters &params)
void get_parameters(double &norm, std::valarray< double > &res, std::valarray< double > &pole)
std::valarray< double > m_res
Definition: math_Rational.h:60
Class for parameters.
Definition: parameters.h:40
double func(double x)
Bridge::VerboseLevel m_vl
Definition: math_Rational.h:54
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
static bool Register(const std::string &realm, const creator_callback &cb)
int non_zero(const double v)
Definition: checker.cpp:31
void Register_double(const string &, const double)
Definition: parameters.cpp:324
int fetch_double(const string &key, double &val) const
Definition: parameters.cpp:124
string get_string(const string &key) const
Definition: parameters.cpp:85
int fetch_int(const string &key, int &val) const
Definition: parameters.cpp:141
std::valarray< double > m_pole
Definition: math_Rational.h:61
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:191