Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_IO_Data_Text.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "IO/dataIO_Text.h"
17 #include "IO/gaugeConfig.h"
18 
20 
21 //====================================================================
23 
32 namespace Test_IO_Data {
33  const std::string test_name = "IO.Data.Text";
34 
35  //- test-private parameters
36  namespace {
37  const std::string filename_input = "test_IO_Data_Text.yaml";
38  }
39 
40  //- prototype declaration
41  int test_io_data_text(void);
42 
43 #ifdef USE_TESTMANAGER_AUTOREGISTER
44  namespace {
45 #if defined(USE_GROUP_SU2)
46  // Nc=2 is not available.
47 #else
48  static const bool is_registered = TestManager::RegisterTest(
49  test_name,
51  );
52 #endif
53  }
54 #endif
55 
56  //====================================================================
58  {
59  // #### parameter setup ####
60  const int Ndim = CommonParameters::Ndim();
61  const int Nvol = CommonParameters::Nvol();
62  const double tiny = CommonParameters::epsilon_criterion();
63 
64  const Parameters params_all = ParameterManager::read(filename_input);
65 
66  const Parameters params_test = params_all.lookup("Test_IO_Data");
67 
68  const string str_gconf_status = params_test.get_string("gauge_config_status");
69  const string str_gconf_read = params_test.get_string("gauge_config_type_input");
70  const string readfile = params_test.get_string("config_filename_input");
71  const string testfile = params_test.get_string("config_filename_output");
72  const string str_rand_type = params_test.get_string("random_number_type");
73  const unsigned long seed = params_test.get_unsigned_long("seed_for_random_number");
74  const int data_size = params_test.get_int("data_size");
75  const string str_vlevel = params_test.get_string("verbose_level");
76 
77  const bool do_check = params_test.is_set("expected_result");
78  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
79 
80  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
81 
82  //- print input parameters
83  vout.general(vl, " gconf_status = %s\n", str_gconf_status.c_str());
84  vout.general(vl, " gconf_read = %s\n", str_gconf_read.c_str());
85  vout.general(vl, " readfile = %s\n", readfile.c_str());
86  vout.general(vl, " testfile = %s\n", testfile.c_str());
87  vout.general(vl, " rand_type = %s\n", str_rand_type.c_str());
88  vout.general(vl, " seed = %lu\n", seed);
89  vout.general(vl, " data_size = %d\n", data_size);
90  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
91  vout.general(vl, "\n");
92 
93  //- input parameter check
94  int err = 0;
95  err += ParameterCheck::non_NULL(str_gconf_status);
96  err += ParameterCheck::non_NULL(testfile);
97 
98  if (err) {
99  vout.crucial(vl, "Error at %s: input parameters have not been set\n", test_name.c_str());
100  exit(EXIT_FAILURE);
101  }
102 
103 
104  RandomNumberManager::initialize(str_rand_type, seed);
105 
106 
107  // #### Set up a gauge configuration ####
108  unique_ptr<Field_G> U(new Field_G(Nvol, Ndim));
109 
110  if (str_gconf_status == "Continue") {
111  GaugeConfig(str_gconf_read).read(U, readfile);
112  } else if (str_gconf_status == "Cold_start") {
113  GaugeConfig("Unit").read(U);
114  } else if (str_gconf_status == "Hot_start") {
115  GaugeConfig("Random").read(U);
116  } else {
117  vout.crucial(vl, "Error at %s: unsupported gconf status \"%s\"\n", test_name.c_str(), str_gconf_status.c_str());
118  exit(EXIT_FAILURE);
119  }
120 
121 
122  // ##### object setup #####
123  const unique_ptr<DataIO> data_io(new DataIO_Text);
124  const unique_ptr<Timer> timer(new Timer(test_name));
125 
126 
127  // #### Execution main part ####
128  timer->start();
129 
130  err = 0;
131 
132  //- first write to file.
133  std::vector<double> array(data_size);
134  double *p = U->ptr(0);
135 
136  for (size_t i = 0; i < data_size; ++i) {
137  array[i] = *p++;
138  }
139 
140  data_io->write_file(array, testfile, false);
141 
142 
143  //- write again with complex data.
144  std::vector<dcomplex> arrayc(data_size);
145  p = U->ptr(0);
146 
147  for (size_t i = 0; i < data_size; ++i) {
148  double x = *p++;
149  double y = *p++;
150 
151  arrayc[i] = cmplx(x, y);
152  }
153 
154  data_io->write_file(arrayc, testfile, true);
155 
156 
157  //- then read back from file.
158  std::vector<double> array2(data_size);
159 
160  data_io->read_file(array2, testfile);
161 
162 
163  //- check with reference config.
164  for (size_t i = 0; i < data_size; ++i) {
165  if (abs(array[i] - array2[i]) > tiny) ++err;
166  }
167 
168  vout.general(vl, "%s: \t%s\n", test_name.c_str(), (err == 0) ? "ok" : "failed");
169 
170 
171  int result = err;
172 
173  timer->report();
174 
176 
177 
178  if (do_check) {
179  return Test::verify(result, expected_result);
180  } else {
181  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
182  return EXIT_SKIP;
183  }
184  }
185 } // namespace Test_IO_Data
#define EXIT_SKIP
Definition: test.h:17
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:153
static double epsilon_criterion()
void general(const char *format,...)
Definition: bridgeIO.cpp:197
int get_int(const string &key) const
Definition: parameters.cpp:192
Class for parameters.
Definition: parameters.h:46
DataIO_Text class for general file I/O in plain Text format.
Definition: dataIO_Text.h:44
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 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)
virtual void write_file(const double *v, const size_t n, const string &, const bool append=true)=0
int verify(const double result, const double expected, double eps)
Definition: test.cpp:27
Bridge::VerboseLevel vl
VerboseLevel
Definition: bridgeIO.h:42
int test_io_data_text(void)
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:78
const std::string test_name
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
virtual void read_file(double *v, const size_t n, const string &)=0