Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_IO_GaugeConfig.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "gaugeConfig.h"
17 
18 #include "randomNumbers_Mseries.h"
19 
20 //====================================================================
22 
29 namespace Test_IO_GaugeConfig {
30  const std::string test_name = "IO.GaugeConfig";
31 
32  //- test-private parameters
33  namespace {
34 // const std::string filename_input = "test_IO_GaugeConfig_Text.yaml";
35 // const std::string filename_output = "stdout";
36 
37  class Parameters_Test_IO_GaugeConfig : public Parameters {
38  public:
39  Parameters_Test_IO_GaugeConfig()
40  {
41  Register_string("gauge_config_status", "NULL");
42  Register_string("gauge_config_type_input", "NULL");
43  Register_string("gauge_config_type_output", "NULL");
44  Register_string("config_filename_input", "NULL");
45  Register_string("config_filename_output", "NULL");
46 
47  Register_string("verbose_level", "NULL");
48 
49  Register_double("expected_result", 0.0);
50  }
51  };
52  }
53 
54  //- prototype declaration
55  int test_io_gconf_main(const std::string&);
56  int check_conf(const Field_G *f, const Field_G *g);
57 
58  //- tests for various file formats
60  {
61  return test_io_gconf_main("test_IO_GaugeConfig_Text.yaml");
62  }
63 
64 
66  {
67  return test_io_gconf_main("test_IO_GaugeConfig_Binary.yaml");
68  }
69 
70 
72  {
73  return test_io_gconf_main("test_IO_GaugeConfig_BinaryParallel.yaml");
74  }
75 
76 
78  {
79  return test_io_gconf_main("test_IO_GaugeConfig_Fortran.yaml");
80  }
81 
82 
84  {
85  return test_io_gconf_main("test_IO_GaugeConfig_ILDG.yaml");
86  }
87 
88 
90  {
91  return test_io_gconf_main("test_IO_GaugeConfig_ILDG_Parallel.yaml");
92  }
93 
94 
95 #ifdef USE_TESTMANAGER_AUTOREGISTER
96  namespace {
97 #if defined(USE_GROUP_SU2)
98  // Nc=2 is not available.
99 #else
100  static const bool is_registered_text = TestManager::RegisterTest(
101  "IO.GaugeConfig.Text",
103  );
104  static const bool is_registered_binary = TestManager::RegisterTest(
105  "IO.GaugeConfig.Binary",
107  );
108 #ifdef USE_MPI
109  static const bool is_registered_binary_parallel = TestManager::RegisterTest(
110  "IO.GaugeConfig.BinaryParallel",
112  );
113 #endif
114  static const bool is_registered_fortran = TestManager::RegisterTest(
115  "IO.GaugeConfig.Fortran",
117  );
118 #ifdef USE_LIMELIB
119  static const bool is_registered_ILDG = TestManager::RegisterTest(
120  "IO.GaugeConfig.ILDG",
122  );
123 #endif
124 #ifdef USE_MPI
125  static const bool is_registered_ILDG_parallel = TestManager::RegisterTest(
126  "IO.GaugeConfig.ILDG_Parallel",
128  );
129 #endif
130 #endif
131  }
132 #endif
133 
134  //====================================================================
135  int test_io_gconf_main(const std::string& filename_input)
136  {
137  // #### parameter setup ####
138  int Ndim = CommonParameters::Ndim();
139  int Nvol = CommonParameters::Nvol();
140 
141  Parameters *params_test = new Parameters_Test_IO_GaugeConfig;
142  Parameters *params_all = new Parameters;
143 
144  params_all->Register_Parameters("Test_IO_GaugeConfig", params_test);
145 
146  ParameterManager_YAML params_manager;
147  params_manager.read_params(filename_input, params_all);
148 
149  const string str_gconf_status = params_test->get_string("gauge_config_status");
150  const string str_gconf_read = params_test->get_string("gauge_config_type_input");
151  const string str_gconf_write = params_test->get_string("gauge_config_type_output");
152  const string readfile = params_test->get_string("config_filename_input");
153  const string testfile = params_test->get_string("config_filename_output");
154  const string str_vlevel = params_test->get_string("verbose_level");
155 
156  const bool do_check = params_test->is_set("expected_result");
157  const double expected_result = do_check ? params_test->get_double("expected_result") : 0.0;
158 
160 
161  //- print input parameters
162  vout.general(vl, " gconf_status = %s\n", str_gconf_status.c_str());
163  vout.general(vl, " gconf_read = %s\n", str_gconf_read.c_str());
164  vout.general(vl, " gconf_write = %s\n", str_gconf_write.c_str());
165  vout.general(vl, " readfile = %s\n", readfile.c_str());
166  vout.general(vl, " testfile = %s\n", testfile.c_str());
167  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
168 
169  //- input parameter check
170  int err = 0;
171  err += ParameterCheck::non_NULL(str_gconf_status);
172  err += ParameterCheck::non_NULL(str_gconf_write);
173  err += ParameterCheck::non_NULL(testfile);
174 
175  if (err) {
176  vout.crucial(vl, "%s: Input parameters have not been set.\n", test_name.c_str());
177  abort();
178  }
179 
180 
181  // #### Set up a gauge configuration ####
182  Field_G *U = new Field_G(Nvol, Ndim);
183  GaugeConfig *gconf_read = new GaugeConfig(str_gconf_read);
184 
185  if (str_gconf_status == "Continue") {
186  gconf_read->read_file((Field *)U, readfile);
187  } else if (str_gconf_status == "Cold_start") {
188  U->set_unit();
189  } else if (str_gconf_status == "Hot_start") {
190  RandomNumbers_Mseries rand(1234567);
191  U->set_random(&rand);
192  } else {
193  vout.crucial(vl, "%s: unsupported gconf status \"%s\".\n", test_name.c_str(), str_gconf_status.c_str());
194  abort();
195  }
196 
197  Field_G *Utest = new Field_G(Nvol, Ndim);
198  GaugeConfig *gconf_test = new GaugeConfig(str_gconf_write);
199 
200 
201  // #### object setup #####
202  Timer *timer = new Timer(test_name);
203 
204 
205  // #### Execution main part ####
206  timer->start();
207 
208  err = 0;
209 
210  //- first write to file.
211  gconf_test->write_file(U, testfile);
212 
213  //- then read back from file.
214  gconf_test->read_file(Utest, testfile);
215 
216  //- check with reference config.
217  // bool is_equal = (*Utest == *U);
218  bool is_equal = (check_conf(Utest, U) == 0);
219  if (!is_equal) ++err;
220 
221  vout.general(vl, "Test_IO_GaugeConfig:\t%s\n", is_equal ? "ok" : "failed");
222 
223 
224  int result = err;
225 
226  timer->report();
227 
228 
229  // #### tydy up ####
230  delete params_test;
231  delete params_all;
232 
233  delete timer;
234 
235  delete U;
236  delete gconf_read;
237 
238  delete Utest;
239  delete gconf_test;
240 
241 
242  if (do_check) {
243  return Test::verify(expected_result, result);
244  } else {
245  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
246  return EXIT_SKIP;
247  }
248  }
249 
250 
251  //====================================================================
252  static inline bool is_equal(const double x, const double y)
253  {
254  const double eps = CommonParameters::epsilon_criterion();
255 
256  if ((x == 0) && (y == 0)) return true;
257 
258  if (x == 0) return fabs(y) < eps;
259 
260  if (y == 0) return fabs(x) < eps;
261 
262  return fabs((x - y) / y) < eps;
263  }
264 
265 
266  //====================================================================
267  int check_conf(const Field_G *f, const Field_G *g)
268  {
270 
271  int err = 0;
272 
273  int Ndim = CommonParameters::Ndim();
274  int Nvol = CommonParameters::Nvol();
275  int Nc = CommonParameters::Nc();
276 
277  for (int idir = 0; idir < Ndim; ++idir) {
278  for (int isite = 0; isite < Nvol; ++isite) {
279  for (int i = 0; i < Nc * Nc; ++i) {
280  double v1r = f->cmp_r(i, isite, idir);
281  double v1i = f->cmp_i(i, isite, idir);
282 
283  double v2r = g->cmp_r(i, isite, idir);
284  double v2i = g->cmp_i(i, isite, idir);
285 
286 // if (!((v1r == v2r) && (v1i == v2i))) ++err;
287  bool is_ok = (is_equal(v1r, v2r) && is_equal(v1i, v2i));
288  if (!is_ok) ++err;
289 
290  if (!is_ok) {
291  vout.general(vl, "%6d : %4d: %2d: %19.15f %19.15f\n %19.15f %19.15f : %s\n",
292  isite, i, idir,
293  v1r, v1i, v2r, v2i,
294  is_ok ? "ok" : "fail");
295  } else {
296  vout.paranoiac(vl, "%6d : %4d: %2d: %19.15f %19.15f\n %19.15f %19.15f : %s\n",
297  isite, i, idir,
298  v1r, v1i, v2r, v2i,
299  is_ok ? "ok" : "fail");
300  }
301  }
302  }
303  }
304 
305  vout.general(vl, "%s: error=%d\n", __func__, err);
306 
307  return err;
308  }
309 } // namespace Test_IO_GaugeConfig
#define EXIT_SKIP
Definition: test.h:17
Random number generator base on M-series.
static bool is_equal(const double x, const double y)
const std::string test_name
BridgeIO vout
Definition: bridgeIO.cpp:207
void read_params(const std::string &params_file, Parameters *params)
read parameters from file.
void detailed(const char *format,...)
Definition: bridgeIO.cpp:50
static double epsilon_criterion()
void general(const char *format,...)
Definition: bridgeIO.cpp:38
static Bridge::VerboseLevel Vlevel()
Container of Field-type object.
Definition: field.h:37
double cmp_i(const int cc, const int site, const int mn=0) const
Definition: field_G.h:88
Class for parameters.
Definition: parameters.h:40
void set_random(RandomNumbers *rand)
Definition: field_G_imp.cpp:62
void set_unit()
Definition: field_G_imp.cpp:39
static bool RegisterTest(const std::string &key, const Test_function func)
Definition: testManager.h:79
SU(N) gauge field.
Definition: field_G.h:36
bool is_set(const string &) const
Definition: parameters.cpp:366
int check_conf(const Field_G *f, const Field_G *g)
double get_double(const string &key) const
Definition: parameters.cpp:25
int test_io_gconf_main(const std::string &)
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:62
int non_NULL(const std::string v)
Definition: checker.cpp:61
void start()
Definition: timer.cpp:44
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
void Register_Parameters(const string &, Parameters *const)
Definition: parameters.cpp:359
void write_file(Field *u, const string &filename)
Definition: gaugeConfig.cpp:97
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:25
double cmp_r(const int cc, const int site, const int mn=0) const
Definition: field_G.h:83
Parameter manager with YAML parser.
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:56
Definition: timer.h:31
int verify(const double expected, const double result)
Definition: test.cpp:27
void read_file(Field *u, const string &filename)
Definition: gaugeConfig.cpp:85
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