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