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