Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_RandomNumbers_Mseries_Gaussian.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
17 
18 //====================================================================
20 
29 namespace Test_RandomNumbers_Mseries {
30  const std::string test_name = "RandomNumbers.Mseries.Gaussian";
31 
32  //- test-private parameters
33  namespace {
34  const std::string filename_input = "test_RandomNumbers_Mseries_Gaussian.yaml";
35  }
36 
37  //- prototype declaration
38  int gaussian(void);
39 
40 #ifdef USE_TESTMANAGER_AUTOREGISTER
41  namespace {
42  static const bool is_registered = TestManager::RegisterTest(
43  test_name,
44  gaussian
45  );
46  }
47 #endif
48 
49  //====================================================================
50  int gaussian(void)
51  {
52  // #### parameter setup ####
53 
54  const Parameters params_all = ParameterManager::read(filename_input);
55 
56  const Parameters params_test = params_all.lookup("Test_RandomNumbers");
57 
58  const int iseed = params_test.get_int("int_seed");
59  const int Nrand = params_test.get_int("number_of_samples");
60  const string str_vlevel = params_test.get_string("verbose_level");
61 
62  const bool do_check = params_test.is_set("expected_result");
63  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
64 
65  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
66 
67  //- print input parameters
68  vout.general(vl, " iseed = %d\n", iseed);
69  vout.general(vl, " Nrand = %d\n", Nrand);
70  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
71  vout.general(vl, "\n");
72 
73 
74  // #### object setup ####
75  const unique_ptr<RandomNumbers> rand(new RandomNumbers_Mseries(iseed));
76  const unique_ptr<Timer> timer(new Timer(test_name));
77 
78 
79  // #### Execution main part ####
80  timer->start();
81 
82  double av = 0.0;
83  double vr = 0.0;
84 
85  double rand1, rand2;
86  for (int i = 0; i < Nrand; ++i) {
87  rand->gauss(rand1, rand2);
88  av += rand1 + rand2;
89  vr += rand1 * rand1 + rand2 * rand2;
90  // vout.general(vl, " %10.8f %10.8f\n",rand1,rand2);
91  }
92  av = av / (2.0 * Nrand);
93  vr = vr / (2.0 * Nrand) - av * av;
94  vr = sqrt(vr);
95 
96  vout.general(vl, "\n");
97  vout.general(vl, "Gaussian distribution:\n");
98  vout.general(vl, " number of samples = %10d\n", Nrand);
99  vout.general(vl, " average = %10.8f\n", av);
100  vout.general(vl, " variance = %10.8f\n", vr);
101  vout.general(vl, " variance(expect) = %10.8f\n", 1.0 / sqrt(2.0));
102 
103  const double result = vr;
104 
105  timer->report();
106 
107 
108  if (do_check) {
109  return Test::verify(result, expected_result);
110  } else {
111  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
112  return EXIT_SKIP;
113  }
114  }
115 } // namespace Test_RandomNumbers
#define EXIT_SKIP
Definition: test.h:17
Random number generator base on M-series.
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
void general(const char *format,...)
Definition: bridgeIO.cpp:197
int get_int(const string &key) const
Definition: parameters.cpp:192
Class for parameters.
Definition: parameters.h:46
Parameters lookup(const string &key) const
Definition: parameters.h:79
static bool RegisterTest(const std::string &key, const Test_function func)
Definition: testManager.h:69
double get_double(const string &key) const
Definition: parameters.cpp:175
bool is_set(const string &key) const
Definition: parameters.cpp:528
void start()
Definition: timer.cpp:44
static void read(const std::string &params_file, Parameters &params)
int verify(const double result, const double expected, double eps)
Definition: test.cpp:27
Bridge::VerboseLevel vl
VerboseLevel
Definition: bridgeIO.h:42
Definition: timer.h:31
string get_string(const string &key) const
Definition: parameters.cpp:221
void report(const Bridge::VerboseLevel vl=Bridge::GENERAL)
Definition: timer.cpp:128
void gauss(double &rand1, double &rand2)
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:131