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