Bridge++  Version 1.5.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 "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 
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  const 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  const int Nc = CommonParameters::Nc();
126  const int Nd = CommonParameters::Nd();
127  const int Ndim = CommonParameters::Ndim();
128  const int Nvol = CommonParameters::Nvol();
129 
130  const Parameters params_all = ParameterManager::read(filename_input);
131 
132  const 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 std::string str_gconf_status = params_test.get_string("gauge_config_status");
137  const std::string str_gconf_read = params_test.get_string("gauge_config_type_input");
138  const std::string readfile = params_test.get_string("config_filename_input");
139  const vector<int> Nshift_origin = params_test.get_int_vector("shift_origin");
140  const std::string str_rand_type = params_test.get_string("random_number_type");
141  const unsigned long seed = params_test.get_unsigned_long("seed_for_random_number");
142  const std::string str_vlevel = params_test.get_string("verbose_level");
143 
144  const std::string str_gfix_type = params_all.lookup("GaugeFixing").get_string("gauge_fixing_type");
145  const std::string str_proj_type = params_all.lookup("Projection").get_string("projection_type");
146  const std::string str_smear_type = params_all.lookup("Smear").get_string("smear_type");
147 
148  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
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  for (int mu = 0; mu < Ndim; ++mu) {
156  vout.general(vl, " shift_origin[%d] = %d\n", mu, Nshift_origin[mu]);
157  }
158  vout.general(vl, " rand_type = %s\n", str_rand_type.c_str());
159  vout.general(vl, " seed = %lu\n", seed);
160  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
161  vout.general(vl, " gfix_type = %s\n", str_gfix_type.c_str());
162  vout.general(vl, " proj_type = %s\n", str_proj_type.c_str());
163  vout.general(vl, " smear_type = %s\n", str_smear_type.c_str());
164 
165  vector<Parameters> params_quark;
166  for (int iq = 0; iq < N_quark; ++iq) {
167  std::string qlabel = Filename("Quark_{id}").format(iq + 1);
168  params_quark.push_back(params_all.lookup(qlabel));
169  }
170 
171  // NB. all str_gmset_type are supposed to be the same.
172  const string str_gmset_type = params_quark[0].lookup("Fopr").get_string("gamma_matrix_type");
173  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
174 
175  std::vector<std::string> savefile_base(N_quark);
176  for (int iq = 0; iq < N_quark; ++iq) {
177  savefile_base[iq] = params_quark[iq].get_string("temporary_filename_base");
178  }
179 
180  for (int iq = 0; iq < N_quark; ++iq) {
181  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
182  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
183  vector<int> source_position = params_quark[iq].lookup("Source").get_int_vector("source_position");
184 
185  vout.general(vl, " Quark_%d:\n", iq + 1);
186  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
187  vout.general(vl, " solver_type = %s\n", str_solver_type.c_str());
188  vout.general(vl, " source_type = %s\n", str_source_type.c_str());
189  for (int mu = 0; mu < Ndim; ++mu) {
190  vout.general(vl, " source_position[%d] = %d\n", mu, source_position[mu]);
191  }
192  }
193  vout.general(vl, "\n");
194 
195 
196  //- input parameter check
197  int err = 0;
198  err += ParameterCheck::non_NULL(str_gconf_status);
199 
200  if (err) {
201  vout.crucial(vl, "Error at %s: input parameters have not been set\n", test_name.c_str());
202  exit(EXIT_FAILURE);
203  }
204 
205  for (int iq = 0; iq < N_quark; ++iq) {
206  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
207 
208  if (str_solver_type == "CG") {
209  vout.crucial(vl, "Error at %s: CG can not be adopted. Use CGNE,CGNR, instead.\n", test_name.c_str());
210  exit(EXIT_FAILURE);
211  }
212  }
213 
214  RandomNumberManager::initialize(str_rand_type, seed);
215 
216 
217  // #### Set up a gauge configuration ####
218  unique_ptr<Field_G> U(new Field_G(Nvol, Ndim));
219 
220  if (str_gconf_status == "Continue") {
221  GaugeConfig(str_gconf_read).read(U, readfile);
222  } else if (str_gconf_status == "Cold_start") {
223  GaugeConfig("Unit").read(U);
224  } else if (str_gconf_status == "Hot_start") {
225  GaugeConfig("Random").read(U);
226  } else {
227  vout.crucial(vl, "Error at %s: unsupported gconf status \"%s\"\n", test_name.c_str(), str_gconf_status.c_str());
228  exit(EXIT_FAILURE);
229  }
230 
231  {
233  for (int i_dir = 0; i_dir < Ndim; ++i_dir) {
234  for (int i_shift = 0; i_shift < Nshift_origin[i_dir]; ++i_shift) {
235  unique_ptr<Field_G> Ushift(new Field_G(Nvol, Ndim));
236  shift.backward(*Ushift, *U, i_dir);
237  copy(*U, *Ushift);
238  }
239  }
240  }
241 
242  {
243  unique_ptr<Projection> proj(Projection::New(str_proj_type));
244  proj->set_parameters(params_all.lookup("Projection"));
245 
246  unique_ptr<Smear> smear(Smear::New(str_smear_type, proj));
247  smear->set_parameters(params_all.lookup("Smear"));
248 
249  const unique_ptr<Director_Smear> dr_smear(new Director_Smear(smear));
250  dr_smear->set_parameters(params_all.lookup("Director_Smear"));
251  dr_smear->set_config(U);
252 
253  const int Nsmear = dr_smear->get_Nsmear();
254  const Field_G *Usmear = (Field_G *)dr_smear->getptr_smearedConfig(Nsmear);
255 
256  copy(*U, *Usmear);
257  }
258 
259 
260  // #### Gauge fixing ####
261  {
262  unique_ptr<Field_G> Ufix(new Field_G(Nvol, Ndim));
263  const unique_ptr<GaugeFixing> gfix(GaugeFixing::New(str_gfix_type));
264  gfix->set_parameters(params_all.lookup("GaugeFixing"));
265 
266  gfix->fix(*Ufix, *U);
267 
268  copy(*U, *Ufix);
269  }
270 
271 
272  // #### object setup #####
273  unique_ptr<GammaMatrixSet> gmset(GammaMatrixSet::New(str_gmset_type));
274 
275 #ifdef LIB_CPP11
276  std::vector<unique_ptr<Fopr> > fopr(N_quark);
277  std::vector<unique_ptr<Fopr> > fopr_eo(N_quark);
278  std::vector<unique_ptr<Solver> > solver(N_quark);
279  std::vector<unique_ptr<Fprop> > fprop_eo(N_quark);
280  std::vector<unique_ptr<Source> > source(N_quark);
281 
282  // NB. Fopr is used only for check of diff2 below.
283 
284  for (int iq = 0; iq < N_quark; ++iq) {
285  std::string str_fopr_type = params_quark[iq].lookup("Fopr").get_string("fermion_type");
286  fopr[iq].reset(Fopr::New(str_fopr_type, str_gmset_type));
287  fopr[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
288  fopr[iq]->set_config(U);
289 
290  fopr_eo[iq].reset(Fopr::New(str_fopr_type + "_eo", str_gmset_type));
291  fopr_eo[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
292  fopr_eo[iq]->set_config(U);
293 
294  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
295  solver[iq].reset(Solver::New(str_solver_type, fopr_eo[iq]));
296  solver[iq]->set_parameters(params_quark[iq].lookup("Solver"));
297 
298  fprop_eo[iq].reset(new Fprop_Standard_eo(solver[iq]));
299 
300  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
301  source[iq].reset(Source::New(str_source_type));
302  source[iq]->set_parameters(params_quark[iq].lookup("Source"));
303  }
304 #else
305  std::vector<Fopr *> fopr(N_quark);
306  std::vector<Fopr *> fopr_eo(N_quark);
307  std::vector<Solver *> solver(N_quark);
308  std::vector<Fprop *> fprop_eo(N_quark);
309  std::vector<Source *> source(N_quark);
310 
311  // NB. Fopr is used only for check of diff2 below.
312 
313  for (int iq = 0; iq < N_quark; ++iq) {
314  std::string str_fopr_type = params_quark[iq].lookup("Fopr").get_string("fermion_type");
315  fopr[iq] = Fopr::New(str_fopr_type, str_gmset_type);
316  fopr[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
317  fopr[iq]->set_config(U);
318 
319  fopr_eo[iq] = Fopr::New(str_fopr_type + "_eo", str_gmset_type);
320  fopr_eo[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
321  fopr_eo[iq]->set_config(U);
322 
323  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
324  solver[iq] = Solver::New(str_solver_type, fopr_eo[iq]);
325  solver[iq]->set_parameters(params_quark[iq].lookup("Solver"));
326 
327  fprop_eo[iq] = new Fprop_Standard_eo(solver[iq]);
328 
329  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
330  source[iq] = Source::New(str_source_type);
331  source[iq]->set_parameters(params_quark[iq].lookup("Source"));
332  }
333 #endif
334  vout.general(vl, "\n");
335 
337 
338 
339  // #### Execution main part ####
340  for (int iq = 0; iq < N_quark; ++iq) {
341  vout.general(vl, "Solving quark propagator, flavor = %d:\n", iq + 1);
342  vout.general(vl, " color spin Nconv diff diff2\n");
343 
344  for (int ispin = 0; ispin < Nd; ++ispin) {
345  for (int icolor = 0; icolor < Nc; ++icolor) {
346  int i_cd = icolor + Nc * ispin;
347 
348  Field_F b; // b.set(0.0);
349  source[iq]->set(b, i_cd);
350 
351  Field_F xq;
352  int Nconv;
353  double diff;
354  fprop_eo[iq]->invert_D(xq, b, Nconv, diff);
355 
356  Field_F y(b);
357  fopr[iq]->set_mode("D");
358  fopr[iq]->mult(y, xq); // y = fopr[iq]->mult(xq);
359  axpy(y, -1.0, b); // y -= b;
360  double diff2 = y.norm2() / b.norm2();
361 
362  vout.general(vl, " %2d %2d %6d %12.4e %12.4e\n",
363  icolor, ispin, Nconv, diff, diff2);
364 
365  std::string filename = FileUtils::generate_filename("%s_c%d_s%d.dat", savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
366  field_io->write_file(&xq, filename);
367  }
368  }
369 
370  vout.general(vl, "\n");
371  }
372 
373 #ifdef LIB_CPP11
374  // do nothing
375 #else
376  // tidy-up
377  for (int iq = 0; iq < N_quark; ++iq) {
378  delete fopr_eo[iq];
379  delete solver[iq];
380  delete fprop_eo[iq];
381  delete source[iq];
382  delete fopr[iq];
383  }
384 #endif
385 
386  return EXIT_SUCCESS;
387  }
388 
389 
390  //====================================================================
391  int calculate_hadron_correlator_eo_withFileIO(const std::string& filename_input)
392  {
393  // #### parameter setup ####
394  const int Nc = CommonParameters::Nc();
395  const int Nd = CommonParameters::Nd();
396  const int Ndim = CommonParameters::Ndim();
397  const int Nvol = CommonParameters::Nvol();
398 
399  const Parameters params_all = ParameterManager::read(filename_input);
400 
401  const Parameters params_test = params_all.lookup("Test_Spectrum");
402 
403  const int N_quark = params_test.get_int("number_of_valence_quarks");
404 
405  const std::string str_vlevel = params_test.get_string("verbose_level");
406 
407  const bool do_check = params_test.is_set("expected_result");
408  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
409 
410  std::vector<std::string> savefile_base(N_quark);
411 
412  for (int iq = 0; iq < N_quark; ++iq) {
413  savefile_base[iq] = params_all.lookup(Filename("Quark_{id}").format(iq + 1)).get_string("temporary_filename_base");
414  }
415 
416  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
417 
418  //- print input parameters
419  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
420 
421  // NB. all str_gmset_type are supposed to be the same.
422  const std::string str_gmset_type = params_all.lookup("Quark_1").lookup("Fopr").get_string("gamma_matrix_type");
423  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
424 
425  std::vector<std::string> str_source_type(N_quark);
426 
427  for (int iq = 0; iq < N_quark; ++iq) {
428  vout.general(vl, " Quark_%d:\n", iq + 1);
429  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
430  }
431  vout.general(vl, "\n");
432 
433 
434  // #### Set up objects #####
435  unique_ptr<GammaMatrixSet> gmset(GammaMatrixSet::New(str_gmset_type));
436 
438 
439  Corr2pt_4spinor corr(gmset);
440  corr.set_parameters(params_all.lookup("Corr2pt_4spinor"));
441 
442 
443  // #### Execution main part ####
444  typedef std::vector<Field_F> PropagatorSet;
445  std::vector<PropagatorSet> sq(N_quark);
446  for (int iq = 0; iq < N_quark; ++iq) {
447  sq[iq].resize(Nc * Nd);
448 
449  for (int i_cd = 0; i_cd < Nc * Nd; ++i_cd) {
450  sq[iq][i_cd].set(0.0);
451  }
452  }
453 
454  for (int iq = 0; iq < N_quark; ++iq) {
455  for (int ispin = 0; ispin < Nd; ++ispin) {
456  for (int icolor = 0; icolor < Nc; ++icolor) {
457  int i_cd = icolor + Nc * ispin;
458 
459  std::string filename = FileUtils::generate_filename("%s_c%d_s%d.dat", savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
460  field_io->read_file(&sq[iq][i_cd], filename);
461  }
462  }
463  }
464 
465 
466  //- meson correlators
467  std::vector<double> result(N_quark);
468 
469  vout.general(vl, "2-point correlator:\n");
470 
471  //- case(iq_1 == iq_2)
472  for (int iq = 0; iq < N_quark; ++iq) {
473  vout.general(vl, "Flavor combination = %d, %d\n", iq + 1, iq + 1);
474  result[iq] = corr.meson_all(sq[iq], sq[iq]);
475  vout.general(vl, "\n");
476  }
477 
478 
479  //- case(iq_1 < iq_2)
480  for (int iq = 0; iq < N_quark; ++iq) {
481  for (int jq = iq + 1; jq < N_quark; ++jq) {
482  vout.general(vl, "Flavor combination = %d, %d\n", iq + 1, jq + 1);
483  double result_2 = corr.meson_all(sq[iq], sq[jq]);
484  vout.general(vl, "\n");
485  }
486  }
487 
488 
490 
491 #ifdef LIB_CPP11
492  // do nothing
493 #else
494  // tidy-up
495 #endif
496 
497  if (do_check) {
498  return Test::verify(result[0], expected_result);
499  } else {
500  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
501  return EXIT_SKIP;
502  }
503  }
504 } // 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:592
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
virtual void set_parameters(const Parameters &param)=0
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:319
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
Field * getptr_smearedConfig(const int i_smear)
get pointer to i-th smeared config (0th is original thin link)
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
Methods to shift a field in the lexical site index.
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:78
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