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