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