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