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