Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_Spectrum_2ptFunction_eo_withFileIO.cpp
Go to the documentation of this file.
1 
14 #include "Tests/test.h"
15 
16 #include <sstream>
17 
18 #include "Fopr/fopr_Smeared.h"
19 
20 #include "IO/fieldIO_Binary.h"
21 #include "IO/gaugeConfig.h"
22 
27 
28 #include "Smear/projection.h"
29 
30 #include "Tools/filename.h"
31 #include "Tools/file_utils.h"
32 #include "Tools/gammaMatrixSet.h"
34 
35 //====================================================================
37 
54 namespace Test_Spectrum {
55  const std::string test_name = "Spectrum.Hadron2ptFunction_eo_withFileIO";
56 
57  //- test-private parameters
58  namespace {
59  // const std::string filename_input = "test_Spectrum_Clover_Hadron2ptFunction.yaml";
60  }
61 
62  //- prototype declaration
63  int hadron_2ptFunction_eo_withFileIO(const std::string&);
64 
65  int calculate_quark_propagator_eo_withFileIO(const std::string&);
66  int calculate_hadron_correlator_eo_withFileIO(const std::string&);
67 
68  //- hadron_2ptFunction for various fermions
70  {
71  return hadron_2ptFunction_eo_withFileIO("test_Spectrum_Clover_Hadron2ptFunction.yaml");
72  }
73 
74 
76  {
77  return hadron_2ptFunction_eo_withFileIO("test_Spectrum_Wilson_Hadron2ptFunction.yaml");
78  }
79 
80 
81 #ifdef USE_TESTMANAGER_AUTOREGISTER
82  namespace {
83 #if defined(USE_GROUP_SU2)
84  // Nc=2 is not available.
85 #else
86  static const bool is_registered_Clover = TestManager::RegisterTest(
87  "Spectrum.Clover.Hadron2ptFunction_eo_withFileIO",
89  );
90 
91  //- NB. test_Spectrum_Wilson is implemented separately for beginners
92  // static const bool is_registered_Wilson = TestManager::RegisterTest(
93  // "Spectrum.Wilson.Hadron2ptFunction_eo_withFileIO",
94  // hadron_2ptFunction_eo_withFileIO_Wilson
95  // );
96 #endif
97  }
98 #endif
99 
100  //====================================================================
101  int hadron_2ptFunction_eo_withFileIO(const std::string& filename_input)
102  {
103  int result = 0;
104 
105  unique_ptr<Timer> timer(new Timer(test_name));
106 
107 
108  // #### Execution main part ####
109  timer->start();
110 
111  result += calculate_quark_propagator_eo_withFileIO(filename_input);
112  result += calculate_hadron_correlator_eo_withFileIO(filename_input);
113 
114  timer->report();
115 
116 
117  return result;
118  }
119 
120 
121  //====================================================================
122  int calculate_quark_propagator_eo_withFileIO(const std::string& filename_input)
123  {
124  // #### parameter setup ####
125  int Nc = CommonParameters::Nc();
126  int Nd = CommonParameters::Nd();
127  int Ndim = CommonParameters::Ndim();
128  int Nvol = CommonParameters::Nvol();
129 
130  Parameters params_all = ParameterManager::read(filename_input);
131 
132  Parameters params_test = params_all.lookup("Test_Spectrum");
133 
134  const int N_quark = params_test.get_int("number_of_valence_quarks");
135 
136  const string str_gconf_status = params_test.get_string("gauge_config_status");
137  const string str_gconf_read = params_test.get_string("gauge_config_type_input");
138  const string readfile = params_test.get_string("config_filename_input");
139  const string str_rand_type = params_test.get_string("random_number_type");
140  const unsigned long seed = params_test.get_unsigned_long("seed_for_random_number");
141  const string str_vlevel = params_test.get_string("verbose_level");
142 
143  const string str_gfix_type = params_all.lookup("GaugeFixing").get_string("gauge_fixing_type");
144  const string str_proj_type = params_all.lookup("Projection").get_string("projection_type");
145  const string str_smear_type = params_all.lookup("Smear").get_string("smear_type");
146  const string str_solver_type = params_all.lookup("Solver").get_string("solver_type");
147 
149 
150 
151  //- print input parameters
152  vout.general(vl, " gconf_status = %s\n", str_gconf_status.c_str());
153  vout.general(vl, " gconf_read = %s\n", str_gconf_read.c_str());
154  vout.general(vl, " readfile = %s\n", readfile.c_str());
155  vout.general(vl, " rand_type = %s\n", str_rand_type.c_str());
156  vout.general(vl, " seed = %lu\n", seed);
157  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
158  vout.general(vl, " gfix_type = %s\n", str_gfix_type.c_str());
159  vout.general(vl, " proj_type = %s\n", str_proj_type.c_str());
160  vout.general(vl, " smear_type = %s\n", str_smear_type.c_str());
161  vout.general(vl, " solver_type = %s\n", str_solver_type.c_str());
162 
163  vector<Parameters> params_quark;
164 
165  for (int iq = 0; iq < N_quark; ++iq) {
166  string qlabel = Filename("Quark_{id}").format(iq + 1);
167  params_quark.push_back(params_all.lookup(qlabel));
168  }
169 
170  // NB. all str_gmset_type are supposed to be the same.
171  const string str_gmset_type = params_quark[0].lookup("Fopr").get_string("gamma_matrix_type");
172  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
173 
174  std::vector<std::string> savefile_base(N_quark);
175  for (int iq = 0; iq < N_quark; ++iq) {
176  savefile_base[iq] = params_quark[iq].get_string("temporary_filename_base");
177  }
178 
179  for (int iq = 0; iq < N_quark; ++iq) {
180  string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
181  vector<int> source_position = params_quark[iq].lookup("Source").get_int_vector("source_position");
182 
183  vout.general(vl, " Quark_%d:\n", iq + 1);
184  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
185  vout.general(vl, " source_type = %s\n", str_source_type.c_str());
186  for (int mu = 0; mu < Ndim; ++mu) {
187  vout.general(vl, " source_position[%d] = %d\n", mu, source_position[mu]);
188  }
189  }
190  vout.general(vl, "\n");
191 
192 
193  //- input parameter check
194  int err = 0;
195  err += ParameterCheck::non_NULL(str_gconf_status);
196 
197  if (err) {
198  vout.crucial(vl, "Error at %s: input parameters have not been set\n", test_name.c_str());
199  exit(EXIT_FAILURE);
200  }
201 
202  if (str_solver_type == "CG") {
203  vout.crucial(vl, "Error at %s: CG can not be adopted. Use CGNE,CGNR, instead.\n", test_name.c_str());
204  exit(EXIT_FAILURE);
205  }
206 
207  RandomNumberManager::initialize(str_rand_type, seed);
208 
209 
210  // #### Set up a gauge configuration ####
211  unique_ptr<Field_G> U(new Field_G(Nvol, Ndim));
212 
213  if (str_gconf_status == "Continue") {
214  GaugeConfig(str_gconf_read).read(U, readfile);
215  } else if (str_gconf_status == "Cold_start") {
216  GaugeConfig("Unit").read(U);
217  } else if (str_gconf_status == "Hot_start") {
218  GaugeConfig("Random").read(U);
219  } else {
220  vout.crucial(vl, "Error at %s: unsupported gconf status \"%s\"\n", test_name.c_str(), str_gconf_status.c_str());
221  exit(EXIT_FAILURE);
222  }
223 
224  unique_ptr<Projection> proj(Projection::New(str_proj_type));
225  proj->set_parameters(params_all.lookup("Projection"));
226 
227  unique_ptr<Smear> smear(Smear::New(str_smear_type, proj));
228  smear->set_parameters(params_all.lookup("Smear"));
229 
230  unique_ptr<Director_Smear> dr_smear(new Director_Smear(smear));
231  dr_smear->set_parameters(params_all.lookup("Director_Smear"));
232  dr_smear->set_config(U);
233 
234  int Nsmear = dr_smear->get_Nsmear();
235  Field_G *Usmear = (Field_G *)dr_smear->getptr_smearedConfig(Nsmear);
236 
237 
238  // #### Gauge fixing ####
239  unique_ptr<Field_G> Ufix(new Field_G(Nvol, Ndim));
240  unique_ptr<GaugeFixing> gfix(GaugeFixing::New(str_gfix_type));
241  gfix->set_parameters(params_all.lookup("GaugeFixing"));
242 
243  gfix->fix(*Ufix, *Usmear);
244 
245  copy(*U, *Ufix);
246 
247 
248  // #### object setup #####
249  unique_ptr<GammaMatrixSet> gmset(GammaMatrixSet::New(str_gmset_type));
250 
251 #ifdef LIB_CPP11
252  std::vector<unique_ptr<Fopr> > fopr(N_quark);
253  std::vector<unique_ptr<Fopr> > fopr_eo(N_quark);
254  std::vector<unique_ptr<Solver> > solver(N_quark);
255  std::vector<unique_ptr<Fprop> > fprop_eo(N_quark);
256  std::vector<unique_ptr<Source> > source(N_quark);
257 
258  // NB. Fopr is used only for check of diff2 below.
259 
260  for (int iq = 0; iq < N_quark; ++iq) {
261  string str_fopr_type = params_quark[iq].lookup("Fopr").get_string("fermion_type");
262 
263  fopr[iq].reset(Fopr::New(str_fopr_type, str_gmset_type));
264  fopr[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
265  fopr[iq]->set_config(U);
266 
267  fopr_eo[iq].reset(Fopr::New(str_fopr_type + "_eo", str_gmset_type));
268  fopr_eo[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
269  fopr_eo[iq]->set_config(U);
270 
271  solver[iq].reset(Solver::New(str_solver_type, fopr_eo[iq]));
272  solver[iq]->set_parameters(params_all.lookup("Solver"));
273 
274  fprop_eo[iq].reset(new Fprop_Standard_eo(solver[iq]));
275 
276  string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
277  source[iq].reset(Source::New(str_source_type));
278  source[iq]->set_parameters(params_quark[iq].lookup("Source"));
279  }
280 #else
281  std::vector<Fopr *> fopr(N_quark);
282  std::vector<Fopr *> fopr_eo(N_quark);
283  std::vector<Solver *> solver(N_quark);
284  std::vector<Fprop *> fprop_eo(N_quark);
285  std::vector<Source *> source(N_quark);
286 
287  // NB. Fopr is used only for check of diff2 below.
288 
289  for (int iq = 0; iq < N_quark; ++iq) {
290  string str_fopr_type = params_quark[iq].lookup("Fopr").get_string("fermion_type");
291 
292  fopr[iq] = Fopr::New(str_fopr_type, str_gmset_type);
293  fopr[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
294  fopr[iq]->set_config(U);
295 
296  fopr_eo[iq] = Fopr::New(str_fopr_type + "_eo", str_gmset_type);
297  fopr_eo[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
298  fopr_eo[iq]->set_config(U);
299 
300  solver[iq] = Solver::New(str_solver_type, fopr_eo[iq]);
301  solver[iq]->set_parameters(params_all.lookup("Solver"));
302 
303  fprop_eo[iq] = new Fprop_Standard_eo(solver[iq]);
304 
305  string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
306  source[iq] = Source::New(str_source_type);
307  source[iq]->set_parameters(params_quark[iq].lookup("Source"));
308  }
309 #endif
310  vout.general(vl, "\n");
311 
313 
314 
315  // #### Execution main part ####
316  Field_F xq, b;
317  b.set(0.0);
318 
319  int Nconv;
320  double diff, diff2;
321 
322  for (int iq = 0; iq < N_quark; ++iq) {
323  vout.general(vl, "Solving quark propagator, flavor = %d:\n", iq + 1);
324  vout.general(vl, " color spin Nconv diff diff2\n");
325 
326  for (int ispin = 0; ispin < Nd; ++ispin) {
327  for (int icolor = 0; icolor < Nc; ++icolor) {
328  int idx = icolor + Nc * ispin;
329  source[iq]->set(b, idx);
330 
331  fprop_eo[iq]->invert_D(xq, b, Nconv, diff);
332 
333  Field_F y(b);
334  fopr[iq]->set_mode("D");
335  fopr[iq]->mult(y, xq); // y = fopr[iq]->mult(xq);
336  axpy(y, -1.0, b); // y -= b;
337  diff2 = y.norm2() / b.norm2();
338 
339  vout.general(vl, " %2d %2d %6d %12.4e %12.4e\n",
340  icolor, ispin, Nconv, diff, diff2);
341 
342  std::string filename = FileUtils::generate_filename("%s_c%d_s%d.dat", savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
343  field_io->write_file(&xq, filename);
344  }
345  }
346 
347  vout.general(vl, "\n");
348  }
349 
350 #ifdef LIB_CPP11
351  // do nothing
352 #else
353  // tidy-up
354  for (int iq = 0; iq < N_quark; ++iq) {
355  delete fopr_eo[iq];
356  delete solver[iq];
357  delete fprop_eo[iq];
358  delete source[iq];
359  delete fopr[iq];
360  }
361 #endif
362 
363  return EXIT_SUCCESS;
364  }
365 
366 
367  //====================================================================
368  int calculate_hadron_correlator_eo_withFileIO(const std::string& filename_input)
369  {
370  // #### parameter setup ####
371  int Nc = CommonParameters::Nc();
372  int Nd = CommonParameters::Nd();
373  int Ndim = CommonParameters::Ndim();
374  int Nvol = CommonParameters::Nvol();
375 
376  Parameters params_all = ParameterManager::read(filename_input);
377 
378  Parameters params_test = params_all.lookup("Test_Spectrum");
379 
380  const int N_quark = params_test.get_int("number_of_valence_quarks");
381 
382  const string str_vlevel = params_test.get_string("verbose_level");
383 
384  const bool do_check = params_test.is_set("expected_result");
385  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
386 
387  std::vector<std::string> savefile_base(N_quark);
388 
389  for (int iq = 0; iq < N_quark; ++iq) {
390  savefile_base[iq] = params_all.lookup(Filename("Quark_{id}").format(iq + 1)).get_string("temporary_filename_base");
391  }
392 
394 
395  //- print input parameters
396  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
397 
398  // NB. all str_gmset_type are supposed to be the same.
399  string str_gmset_type = params_all.lookup("Quark_1").lookup("Fopr").get_string("gamma_matrix_type");
400  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
401 
402  std::vector<std::string> str_source_type(N_quark);
403 
404  for (int iq = 0; iq < N_quark; ++iq) {
405  vout.general(vl, " Quark_%d:\n", iq + 1);
406 
407  str_source_type[iq] = params_all.lookup(Filename("Quark_{id}").format(iq + 1)).lookup("Source").get_string("source_type");
408  vout.general(vl, " source_type = %s\n", str_source_type[iq].c_str());
409 
410  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
411  }
412  vout.general(vl, "\n");
413 
414 
415  // #### Set up objects #####
416  unique_ptr<GammaMatrixSet> gmset(GammaMatrixSet::New(str_gmset_type));
417 
419 
420  Corr2pt_4spinor corr(gmset);
421  corr.set_parameters(params_all.lookup("Corr2pt_4spinor"));
422 
423 
424  // #### Execution main part ####
425  typedef std::vector<Field_F> PropagatorSet;
426 
427  std::vector<PropagatorSet> sq(N_quark);
428  for (int iq = 0; iq < N_quark; ++iq) {
429  sq[iq].resize(Nc * Nd);
430 
431  for (int i = 0; i < Nc * Nd; ++i) {
432  sq[iq][i].set(0.0);
433  }
434  }
435 
436 
437  for (int iq = 0; iq < N_quark; ++iq) {
438  for (int ispin = 0; ispin < Nd; ++ispin) {
439  for (int icolor = 0; icolor < Nc; ++icolor) {
440  int idx = icolor + Nc * ispin;
441 
442  std::string filename = FileUtils::generate_filename("%s_c%d_s%d.dat", savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
443  field_io->read_file(&sq[iq][idx], filename);
444  }
445  }
446  }
447 
448 
449  //- meson correlators
450  std::vector<double> result(N_quark);
451 
452  vout.general(vl, "2-point correlator:\n");
453 
454  //- case(iq_1 == iq_2)
455  for (int iq = 0; iq < N_quark; ++iq) {
456  vout.general(vl, "Flavor combination = %d, %d\n", iq + 1, iq + 1);
457  result[iq] = corr.meson_all(sq[iq], sq[iq]);
458  vout.general(vl, "\n");
459  }
460 
461 
462  //- case(iq_1 < iq_2)
463  for (int iq = 0; iq < N_quark; ++iq) {
464  for (int jq = iq + 1; jq < N_quark; ++jq) {
465  vout.general(vl, "Flavor combination = %d, %d\n", iq + 1, jq + 1);
466  double result_2 = corr.meson_all(sq[iq], sq[jq]);
467  vout.general(vl, "\n");
468  }
469  }
470 
471 
473 
474 #ifdef LIB_CPP11
475  // do nothing
476 #else
477  // tidy-up
478 #endif
479 
480  if (do_check) {
481  return Test::verify(result[0], expected_result);
482  } else {
483  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
484  return EXIT_SKIP;
485  }
486  }
487 } // namespace Test_Spectrum
#define EXIT_SKIP
Definition: test.h:17
int calculate_hadron_correlator_eo_withFileIO(const std::string &)
int hadron_2ptFunction_eo_withFileIO(const std::string &)
BridgeIO vout
Definition: bridgeIO.cpp:495
void detailed(const char *format,...)
Definition: bridgeIO.cpp:212
std::string format()
Definition: filename.h:61
double meson_all(const std::vector< Field_F > &sq1, const std::vector< Field_F > &sq2)
double norm2() const
Definition: field.cpp:441
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:164
virtual void set_parameters(const Parameters &param)=0
void general(const char *format,...)
Definition: bridgeIO.cpp:195
Two-point correlator for Wilson-type fermions.
virtual void set_parameters(const Parameters &)=0
int get_int(const string &key) const
Definition: parameters.cpp:87
Class for parameters.
Definition: parameters.h:46
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:381
int calculate_quark_propagator_eo_withFileIO(const std::string &)
Wilson-type fermion field.
Definition: field_F.h:37
Parameters lookup(const string &key) const
Definition: parameters.h:78
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
Field * getptr_smearedConfig(int i_smear)
get pointer to i-th smeared config (0th is original thin link)
void read(Field_G *U, const string &filename=string())
unsigned long get_unsigned_long(const string &key) const
Definition: parameters.cpp:104
double get_double(const string &key) const
Definition: parameters.cpp:70
std::string generate_filename(const char *fmt,...)
Definition: file_utils.cpp:17
Get quark propagator for Fopr with even-odd site index.
Filename utility.
Definition: filename.h:43
int non_NULL(const std::string v)
Definition: checker.cpp:61
bool is_set(const string &key) const
Definition: parameters.cpp:396
void start()
Definition: timer.cpp:44
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:168
void set_config(Field *U)
set pointer to original thin link variable
int get_Nsmear()
get number of applied smearing operation
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
static void read(const std::string &params_file, Parameters &params)
const Format * Trivial
Definition: io_format.cpp:27
Manager of smeared configurations.
int verify(const double result, const double expected, double eps)
Definition: test.cpp:27
FieldIO_Binary class for file I/O of Field data in binary format.
const std::string test_name
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:42
virtual void read_file(Field *v, std::string)=0
read data from file.
virtual void write_file(Field *v, std::string)=0
write data to file.
void set_parameters(const Parameters &params)
set parameters, must be called before set_config
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:75
Definition: timer.h:31
virtual void fix(Field_G &Ufix, const Field_G &Uorg)=0
virtual void set_parameters(const Parameters &params)
string get_string(const string &key) const
Definition: parameters.cpp:116
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 set_parameters(const Parameters &params)=0