Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_Rational_Approx.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "math_Rational.h"
17 
18 //====================================================================
20 
27 namespace Test_Rational {
28  const std::string test_name = "Rational.Approx";
29 
30  //- test-private parameters
31  namespace {
32  const std::string filename_input = "test_Rational_Approx.yaml";
33  const std::string filename_output = "stdout";
34 
35  class Parameters_Test_Rational : public Parameters {
36  public:
37  Parameters_Test_Rational()
38  {
39  Register_string("verbose_level", "NULL");
40 
41  Register_double("expected_result", 0.0);
42  }
43  };
44  }
45 
46  //- prototype declaration
47  int approx(void);
48 
49 #ifdef USE_TESTMANAGER_AUTOREGISTER
50  namespace {
51 #if defined(USE_GROUP_SU2)
52  // Nc=2 is not available.
53 #else
54  static const bool is_registered = TestManager::RegisterTest(
55  test_name,
56  approx
57  );
58 #endif
59  }
60 #endif
61 
62  //====================================================================
63  int approx(void)
64  {
65  // #### parameter setup ####
66  Parameters *params_test = new Parameters_Test_Rational;
67  Parameters *params_rational = new Parameters_Math_Rational;
68  Parameters *params_all = new Parameters;
69 
70  params_all->Register_Parameters("Test_Rational", params_test);
71  params_all->Register_Parameters("Math_Rational", params_rational);
72 
73  ParameterManager_YAML params_manager;
74  params_manager.read_params(filename_input, params_all);
75 
76  const string str_vlevel = params_test->get_string("verbose_level");
77 
78  const bool do_check = params_test->is_set("expected_result");
79  const double expected_result = do_check ? params_test->get_double("expected_result") : 0.0;
80 
81  const int n_exp = params_rational->get_int("exponent_numerator");
82  const int d_exp = params_rational->get_int("exponent_denominator");
83  const double x_min = params_rational->get_double("lower_bound");
84  const double x_max = params_rational->get_double("upper_bound");
85  const int Npoint = params_rational->get_int("number_of_partitions");
86 
88 
89  //- print input parameters
90  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
91  vout.general(vl, " n_exp = %d\n", n_exp);
92  vout.general(vl, " d_exp = %d\n", d_exp);
93  vout.general(vl, " x_min = %12.8f\n", x_min);
94  vout.general(vl, " x_max = %12.8f\n", x_max);
95  vout.general(vl, " Npoint = %d\n", Npoint);
96  vout.general(vl, "\n");
97 
98 
99  // #### object setup ####
100  Math_Rational rational;
101  rational.set_parameters(*params_rational);
102 
103  Timer *timer = new Timer(test_name);
104 
105 
106  // #### Execution main part ####
107  timer->start();
108 
109  double x = x_min;
110  double Dx = (x_max - x_min) / Npoint;
111  double p_exp = ((double)n_exp) / ((double)d_exp);
112 
113  for (int j = 0; j < Npoint + 1; ++j) {
114  double y1 = rational.func(x);
115  double y2 = pow(x, p_exp);
116 
117  vout.general(vl, "x,rational,x^(n/d) = %12.8f %12.8f %12.8f \n", x, y1, y2);
118 
119  x += Dx;
120  }
121 
122  double result = x;
123 
124  timer->report();
125 
126 
127  // #### tidy up ####
128  delete params_test;
129  delete params_rational;
130  delete params_all;
131 
132  delete timer;
133 
134 
135  if (do_check) {
136  return Test::verify(expected_result, result);
137  } else {
138  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
139  return EXIT_SKIP;
140  }
141  }
142 } // namespace Test_Rational
#define EXIT_SKIP
Definition: test.h:17
Determionation of coefficients of rational approximation.
Definition: math_Rational.h:41
BridgeIO vout
Definition: bridgeIO.cpp:207
const std::string test_name
void read_params(const std::string &params_file, Parameters *params)
read parameters from file.
void detailed(const char *format,...)
Definition: bridgeIO.cpp:50
void general(const char *format,...)
Definition: bridgeIO.cpp:38
void set_parameters(const Parameters &params)
int get_int(const string &key) const
Definition: parameters.cpp:40
Class for parameters.
Definition: parameters.h:40
static bool RegisterTest(const std::string &key, const Test_function func)
Definition: testManager.h:79
bool is_set(const string &) const
Definition: parameters.cpp:366
double get_double(const string &key) const
Definition: parameters.cpp:25
double func(double x)
void start()
Definition: timer.cpp:44
void Register_Parameters(const string &, Parameters *const)
Definition: parameters.cpp:359
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:25
Parameter manager with YAML parser.
Definition: timer.h:31
int verify(const double expected, const double result)
Definition: test.cpp:27
string get_string(const string &key) const
Definition: parameters.cpp:85
void report(const Bridge::VerboseLevel vl=Bridge::GENERAL)
Definition: timer.cpp:128
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:191