Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_RandomNumbers_Field.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "Tools/randomNumbers.h"
17 
18 //====================================================================
20 
31 namespace Test_RandomNumbers {
32  const std::string test_name = "RandomNumbers.Field";
33 
34  //- test-private parameters
35  namespace {
36  // const std::string filename_input = "test_RandomNumbers_MT19937_Field_Gaussian.yaml";
37  }
38 
39  //- prototype declaration
40  int rand_field(const std::string&);
41 
42  //- rand_field for various kinds of RandomNumbers
44  {
45  return rand_field("test_RandomNumbers_MT19937_Field_Gaussian.yaml");
46  }
47 
48 
50  {
51  return rand_field("test_RandomNumbers_MT19937_Field_U1.yaml");
52  }
53 
54 
56  {
57  return rand_field("test_RandomNumbers_MT19937_Field_Z2.yaml");
58  }
59 
60 
62  {
63  return rand_field("test_RandomNumbers_Mseries_Field_Gaussian.yaml");
64  }
65 
66 
68  {
69  return rand_field("test_RandomNumbers_Mseries_Field_U1.yaml");
70  }
71 
72 
74  {
75  return rand_field("test_RandomNumbers_Mseries_Field_Z2.yaml");
76  }
77 
78 
79 #ifdef USE_SFMTLIB
80  int rand_field_SFMT_Gaussian()
81  {
82  return rand_field("test_RandomNumbers_SFMT_Field_Gaussian.yaml");
83  }
84 
85 
86  int rand_field_SFMT_U1()
87  {
88  return rand_field("test_RandomNumbers_SFMT_Field_U1.yaml");
89  }
90 
91 
92  int rand_field_SFMT_Z2()
93  {
94  return rand_field("test_RandomNumbers_SFMT_Field_Z2.yaml");
95  }
96 #endif
97 
98 
99 #ifdef USE_TESTMANAGER_AUTOREGISTER
100  namespace {
101  static const bool is_registered_MT19937_Gaussian = TestManager::RegisterTest(
102  "RandomNumbers.MT19937.Field.Gaussian",
104  );
105 
106  static const bool is_registered_MT19937_U1 = TestManager::RegisterTest(
107  "RandomNumbers.MT19937.Field.U1",
109  );
110 
111  static const bool is_registered_MT19937_Z2 = TestManager::RegisterTest(
112  "RandomNumbers.MT19937.Field.Z2",
114  );
115 
116 
117  static const bool is_registered_Mseries_Gaussian = TestManager::RegisterTest(
118  "RandomNumbers.Mseries.Field.Gaussian",
120  );
121 
122  static const bool is_registered_Mseries_U1 = TestManager::RegisterTest(
123  "RandomNumbers.Mseries.Field.U1",
125  );
126 
127  static const bool is_registered_Mseries_Z2 = TestManager::RegisterTest(
128  "RandomNumbers.Mseries.Field.Z2",
130  );
131 
132 #ifdef USE_SFMTLIB
133  static const bool is_registered_SFMT_Gaussian = TestManager::RegisterTest(
134  "RandomNumbers.SFMT.Field.Gaussian",
135  rand_field_SFMT_Gaussian
136  );
137 
138  static const bool is_registered_SFMT_U1 = TestManager::RegisterTest(
139  "RandomNumbers.SFMT.Field.U1",
140  rand_field_SFMT_U1
141  );
142 
143  static const bool is_registered_SFMT_Z2 = TestManager::RegisterTest(
144  "RandomNumbers.SFMT.Field.Z2",
145  rand_field_SFMT_Z2
146  );
147 #endif
148  }
149 #endif
150 
151  //====================================================================
152  int rand_field(const std::string& filename_input)
153  {
154  // #### parameter setup ####
155  const Parameters params_all = ParameterManager::read(filename_input);
156  const Parameters params_test = params_all.lookup("Test_RandomNumbers");
157 
158  const string str_rand_type = params_test.get_string("random_number_type");
159  const string str_noise_type = params_test.get_string("noise_type");
160  const int iseed = params_test.get_int("int_seed");
161  const string str_vlevel = params_test.get_string("verbose_level");
162 
163  const bool do_check = params_test.is_set("expected_result");
164  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
165 
166  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
167 
168  //- print input parameters
169  vout.general(vl, " rand_type = %s\n", str_rand_type.c_str());
170  vout.general(vl, " noise_type = %s\n", str_noise_type.c_str());
171  vout.general(vl, " iseed = %d\n", iseed);
172  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
173  vout.general(vl, "\n");
174 
175 
176  // #### object setup ####
177  const unique_ptr<RandomNumbers> rand(RandomNumbers::New(str_rand_type, iseed));
178  const unique_ptr<Timer> timer(new Timer(test_name));
179 
180 
181  // #### Execution main part ####
182  timer->start();
183 
184  //- NB. Nin must be even, due to complex of U1,Z2 fields
185  const int Nin = 30;
186  const int Nvol = CommonParameters::Nvol();
187  const int NPE = CommonParameters::NPE();
188  const int Nex = 33;
189  Field v(Nin, Nvol, Nex);
190 
191  double av = 0.0;
192  double vr = 0.0;
193 
194  rand->lex_global(str_noise_type, v);
195 
196  const int size = v.size();
197  for (int i = 0; i < size; ++i) {
198  av += v.cmp(i);
199  vr += v.cmp(i) * v.cmp(i);
200  // vout.general(vl, " %10.8f\n",v.cmp(i));
201  }
202 
203  double av_all = Communicator::reduce_sum(av);
204  double vr_all = Communicator::reduce_sum(vr);
205 
206  av = av_all / Nvol / NPE / Nin / Nex;
207  vr = vr_all / Nvol / NPE / Nin / Nex - av * av;
208  vr = sqrt(vr);
209 
210  vout.general(vl, "\n");
211  vout.general(vl, "Distribution (Field):\n");
212  vout.general(vl, " number of samples = %10d\n", size);
213  vout.general(vl, " average = %10.8f\n", av);
214  vout.general(vl, " variance = %10.8f\n", vr);
215  vout.general(vl, " variance(expect) = %10.8f\n", 1.0 / sqrt(2.0));
216 
217  double result = vr;
218 
219  timer->report();
220 
221 
222  if (do_check) {
223  return Test::verify(result, expected_result);
224  } else {
225  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
226  return EXIT_SKIP;
227  }
228  }
229 } // 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
void general(const char *format,...)
Definition: bridgeIO.cpp:197
Container of Field-type object.
Definition: field.h:45
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
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
virtual void lex_global(const std::string &, Field &)
Bridge::VerboseLevel vl
VerboseLevel
Definition: bridgeIO.h:42
static int reduce_sum(int count, double *recv_buf, double *send_buf, int pattern=0)
make a global sum of an array of double over the communicator. pattern specifies the dimensions to be...
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
int rand_field(const std::string &)
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:131
int size() const
Definition: field.h:132