Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_RandomNumbers_MT19937_Uniform.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
17 
18 //====================================================================
20 
29 namespace Test_RandomNumbers_MT19937 {
30  const std::string test_name = "RandomNumbers.MT19937.Uniform";
31 
32  //- test-private parameters
33  namespace {
34  const std::string filename_input = "test_RandomNumbers_MT19937_Uniform.yaml";
35  }
36 
37  //- prototype declaration
38  int uniform_calc_pi(void);
39 
40 #ifdef USE_TESTMANAGER_AUTOREGISTER
41  namespace {
42  static const bool is_registered = TestManager::RegisterTest(
43  test_name,
45  );
46  }
47 #endif
48 
49  //====================================================================
50  int uniform_calc_pi(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 Nseed = params_test.get_int("number_of_seeds");
59  const int iseed_base = params_test.get_int("int_seed_base");
60  const int Nrand = params_test.get_int("number_of_samples");
61  const string str_vlevel = params_test.get_string("verbose_level");
62 
63  const bool do_check = params_test.is_set("expected_result");
64  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
65 
66  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
67 
68  //- print input parameters
69  vout.general(vl, " Nseed = %d\n", Nseed);
70  vout.general(vl, " iseed_base = %d\n", iseed_base);
71  vout.general(vl, " Nrand = %d\n", Nrand);
72  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
73  vout.general(vl, "\n");
74 
75 
76  // #### object setup #####
77  const unique_ptr<Timer> timer(new Timer(test_name));
78 
79 
80  // #### Execution main part ####
81  timer->start();
82 
83  vout.general(vl, "\n");
84  vout.general(vl, "Monte Carlo estimate of pi:\n");
85  vout.general(vl, " number of samples = %10d\n", Nrand);
86  vout.general(vl, " seed estimate of pi\n");
87 
88  double t1 = 0.0;
89  double t2 = 0.0;
90 
91  for (int iseed = 0; iseed < Nseed; ++iseed) {
92  int iseed2 = iseed_base + iseed;
93 
95 
96  int Npi = 0;
97  for (int i = 0; i < Nrand; ++i) {
98  double rand1 = rand->get();
99  double rand2 = rand->get();
100  double r = rand1 * rand1 + rand2 * rand2;
101 
102  if (r < 1.0) { ++Npi; }
103  // vout.general(vl, " %10.8f %10.8f\n",rand1,rand2);
104  }
105 
106  double pi_exp = (4.0 * Npi) / Nrand;
107 
108  t1 += pi_exp;
109  t2 += pi_exp * pi_exp;
110 
111  //vout.general(vl, " estimate of pi = %10.8f\n",pi_exp);
112  vout.general(vl, " %10d %14.10f\n", iseed2, pi_exp);
113  }
114 
115  const double api = t1 / (double)Nseed;
116  const double vpi = t2 / (double)Nseed - api * api;
117  const double dpi = sqrt(vpi);
118  const double epi = dpi / sqrt((double)Nseed - 1);
119 
120  const double pi = 3.141592653589793;
121  vout.general(vl, " true value = %10.8f\n", pi);
122  vout.general(vl, " average = %10.8f\n", api);
123  vout.general(vl, " variance = %10.8f\n", vpi);
124  vout.general(vl, " deviation = %10.8f\n", dpi);
125  vout.general(vl, " error = %10.8f\n", epi);
126 
127  const double result = api;
128  // changed to check the obtained value of pi itseft. [25 May 2014 H.M.]
129 
130  timer->report();
131 
132 
133  if (do_check) {
134  return Test::verify(result, expected_result);
135  } else {
136  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
137  return EXIT_SKIP;
138  }
139  }
140 } // namespace Test_RandomNumbers
#define EXIT_SKIP
Definition: test.h:17
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
virtual double get()=0
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
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:131