Bridge++  Version 1.6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_Spectrum_2ptFunction_withFileIO_initial_guess.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 
56 namespace Test_Spectrum {
57  const std::string test_name = "Spectrum.Hadron2ptFunction_withFileIO_initial_guess";
58 
59  //- test-private parameters
60  namespace {
61  // const std::string filename_input = "test_Spectrum_Clover_Hadron2ptFunction.yaml";
62  }
63 
64  //- prototype declaration
65  int hadron_2ptFunction_withFileIO_initial_guess(const std::string&);
66 
69 
70  //- hadron_2ptFunction for various fermions
72  {
73  return hadron_2ptFunction_withFileIO_initial_guess("test_Spectrum_Clover_Hadron2ptFunction_initial_guess.yaml");
74  }
75 
76 #ifdef USE_TESTMANAGER_AUTOREGISTER
77  namespace {
78 #if defined(USE_GROUP_SU2)
79  // Nc=2 is not available.
80 #else
81  static const bool is_registered_Clover = TestManager::RegisterTest(
82  "Spectrum.Clover.Hadron2ptFunction_withFileIO_initial_guess",
84  );
85 #endif
86  }
87 #endif
88 
89  //====================================================================
90  int hadron_2ptFunction_withFileIO_initial_guess(const std::string& filename_input)
91  {
92  int result = 0;
93 
94  const unique_ptr<Timer> timer(new Timer(test_name));
95 
96 
97  // #### Execution main part ####
98  timer->start();
99 
100  result += calculate_quark_propagator_withFileIO_initial_guess(filename_input);
102 
103  timer->report();
104 
105  return result;
106  }
107 
108 
109  //====================================================================
110  int calculate_quark_propagator_withFileIO_initial_guess(const std::string& filename_input)
111  {
112  // #### parameter setup ####
113  const int Nc = CommonParameters::Nc();
114  const int Nd = CommonParameters::Nd();
115  const int Ndim = CommonParameters::Ndim();
116  const int Nvol = CommonParameters::Nvol();
117 
118  const Parameters params_all = ParameterManager::read(filename_input);
119 
120  const Parameters params_test = params_all.lookup("Test_Spectrum");
121 
122  const int N_quark = params_test.get_int("number_of_valence_quarks");
123 
124  const std::string str_gconf_status = params_test.get_string("gauge_config_status");
125  const std::string str_gconf_read = params_test.get_string("gauge_config_type_input");
126  const std::string readfile = params_test.get_string("config_filename_input");
127  const vector<int> Nshift_origin = params_test.get_int_vector("shift_origin");
128  const std::string str_vlevel = params_test.get_string("verbose_level");
129 
130  const std::string str_gfix_type = params_all.lookup("GaugeFixing").get_string("gauge_fixing_type");
131  const std::string str_proj_type = params_all.lookup("Projection").get_string("projection_type");
132  const std::string str_smear_type = params_all.lookup("Smear").get_string("smear_type");
133  const int Nsmear = params_all.lookup("Director_Smear").get_int("number_of_smearing");
134 
135  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
136 
137  //- print input parameters
138  vout.general(vl, " gconf_status = %s\n", str_gconf_status.c_str());
139  vout.general(vl, " gconf_read = %s\n", str_gconf_read.c_str());
140  vout.general(vl, " readfile = %s\n", readfile.c_str());
141  for (int mu = 0; mu < Ndim; ++mu) {
142  vout.general(vl, " shift_origin[%d] = %d\n", mu, Nshift_origin[mu]);
143  }
144  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
145  vout.general(vl, " gfix_type = %s\n", str_gfix_type.c_str());
146  vout.general(vl, " proj_type = %s\n", str_proj_type.c_str());
147  vout.general(vl, " smear_type = %s\n", str_smear_type.c_str());
148 
149  vector<Parameters> params_quark;
150  for (int iq = 0; iq < N_quark; ++iq) {
151  std::string qlabel = Filename("Quark_{id}").format(iq + 1);
152  params_quark.push_back(params_all.lookup(qlabel));
153  }
154 
155  // NB. all str_gmset_type are supposed to be the same.
156  std::string str_gmset_type = params_quark[0].lookup("Fopr").get_string("gamma_matrix_type");
157  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
158 
159  std::vector<std::string> savefile_base(N_quark);
160  for (int iq = 0; iq < N_quark; ++iq) {
161  savefile_base[iq] = params_quark[iq].get_string("temporary_filename_base");
162  }
163 
164  for (int iq = 0; iq < N_quark; ++iq) {
165  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
166  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
167  vector<int> source_position = params_quark[iq].lookup("Source").get_int_vector("source_position");
168 
169  vout.general(vl, " Quark_%d:\n", iq + 1);
170  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
171  vout.general(vl, " solver_type = %s\n", str_solver_type.c_str());
172  vout.general(vl, " source_type = %s\n", str_source_type.c_str());
173  for (int mu = 0; mu < Ndim; ++mu) {
174  vout.general(vl, " source_position[%d] = %d\n", mu, source_position[mu]);
175  }
176  }
177  vout.general(vl, "\n");
178 
179 
180  //- input parameter check
181  int err = 0;
182  err += ParameterCheck::non_NULL(str_gconf_status);
183 
184  if (err) {
185  vout.crucial(vl, "Error at %s: input parameters have not been set\n", test_name.c_str());
186  exit(EXIT_FAILURE);
187  }
188 
189  for (int iq = 0; iq < N_quark; ++iq) {
190  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
191 
192  if (str_solver_type == "CG") {
193  vout.crucial(vl, "Error at %s: CG can not be adopted. Use CGNE,CGNR, instead.\n", test_name.c_str());
194  exit(EXIT_FAILURE);
195  }
196  }
197 
198  if ( (Nsmear > 0) && (str_proj_type == "Stout_SU3") ) {
199  if (CommonParameters::Nc() != 3) {
200  vout.crucial(vl, "check skipped: Nc = 3 is needed, but Nc = %d.\n\n", CommonParameters::Nc());
201  //- NB. EXIT_SKIP is set in calculate_hadron_correlator_withFileIO()
202  // return EXIT_SKIP;
203  return EXIT_SUCCESS;
204  }
205  }
206 
207  if ( (str_gfix_type == "Coulomb") || (str_gfix_type == "Landau") ) {
208  if (CommonParameters::Nc() != 3) {
209  vout.crucial(vl, "check skipped: Nc = 3 is needed, but Nc = %d.\n\n", CommonParameters::Nc());
210  return EXIT_SKIP;
211  }
212  }
213 
214 
215  RandomNumberManager::initialize("Mseries", 1234567UL);
216 
217 
218  // #### Set up a gauge configuration ####
219  unique_ptr<Field_G> U(new Field_G(Nvol, Ndim));
220 
221  if (str_gconf_status == "Continue") {
222  GaugeConfig(str_gconf_read).read(U, readfile);
223  } else if (str_gconf_status == "Cold_start") {
224  GaugeConfig("Unit").read(U);
225  } else if (str_gconf_status == "Hot_start") {
226  GaugeConfig("Random").read(U);
227  } else {
228  vout.crucial(vl, "Error at %s: unsupported gconf status \"%s\"\n", test_name.c_str(), str_gconf_status.c_str());
229  exit(EXIT_FAILURE);
230  }
231 
232  {
234  for (int i_dir = 0; i_dir < Ndim; ++i_dir) {
235  for (int i_shift = 0; i_shift < Nshift_origin[i_dir]; ++i_shift) {
236  unique_ptr<Field_G> Ushift(new Field_G(Nvol, Ndim));
237  shift.backward(*Ushift, *U, i_dir);
238  copy(*U, *Ushift);
239  }
240  }
241  }
242 
243  if (Nsmear > 0) {
244  unique_ptr<Projection> proj(Projection::New(str_proj_type));
245  proj->set_parameters(params_all.lookup("Projection"));
246 
247  unique_ptr<Smear> smear(Smear::New(str_smear_type, proj));
248  smear->set_parameters(params_all.lookup("Smear"));
249 
250  const unique_ptr<Director_Smear> dr_smear(new Director_Smear(smear));
251  dr_smear->set_parameters(params_all.lookup("Director_Smear"));
252  dr_smear->set_config(U);
253 
254  const Field_G *Usmear = (Field_G *)dr_smear->getptr_smearedConfig(Nsmear);
255  copy(*U, *Usmear);
256  }
257 
258 
259  // #### Gauge fixing ####
260  {
261  unique_ptr<Field_G> Ufix(new Field_G(Nvol, Ndim));
262  const unique_ptr<GaugeFixing> gfix(GaugeFixing::New(str_gfix_type));
263  gfix->set_parameters(params_all.lookup("GaugeFixing"));
264 
265  gfix->fix(*Ufix, *U);
266 
267  copy(*U, *Ufix);
268  }
269 
270 
271  // #### object setup #####
272 #ifdef LIB_CPP11
273  std::vector<unique_ptr<Fopr> > fopr(N_quark);
274  std::vector<unique_ptr<Solver> > solver(N_quark);
275  std::vector<unique_ptr<Fprop> > fprop_lex(N_quark);
276  std::vector<unique_ptr<Source> > source(N_quark);
277 
278  for (int iq = 0; iq < N_quark; ++iq) {
279  std::string str_fopr_type = params_quark[iq].lookup("Fopr").get_string("fermion_type");
280  fopr[iq].reset(Fopr::New(str_fopr_type, str_gmset_type));
281  fopr[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
282  fopr[iq]->set_config(U);
283 
284  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
285  solver[iq].reset(Solver::New(str_solver_type, fopr[iq]));
286  solver[iq]->set_parameters(params_quark[iq].lookup("Solver"));
287 
288  fprop_lex[iq].reset(new Fprop_Standard_lex(solver[iq]));
289 
290  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
291  source[iq].reset(Source::New(str_source_type));
292  source[iq]->set_parameters(params_quark[iq].lookup("Source"));
293  }
294 #else
295  std::vector<Fopr *> fopr(N_quark);
296  std::vector<Solver *> solver(N_quark);
297  std::vector<Fprop *> fprop_lex(N_quark);
298  std::vector<Source *> source(N_quark);
299 
300  for (int iq = 0; iq < N_quark; ++iq) {
301  std::string str_fopr_type = params_quark[iq].lookup("Fopr").get_string("fermion_type");
302  fopr[iq] = Fopr::New(str_fopr_type, str_gmset_type);
303  fopr[iq]->set_parameters(params_quark[iq].lookup("Fopr"));
304  fopr[iq]->set_config(U);
305 
306  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
307  solver[iq] = Solver::New(str_solver_type, fopr[iq]);
308  solver[iq]->set_parameters(params_quark[iq].lookup("Solver"));
309 
310  fprop_lex[iq] = new Fprop_Standard_lex(solver[iq]);
311 
312  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
313  source[iq] = Source::New(str_source_type);
314  source[iq]->set_parameters(params_quark[iq].lookup("Source"));
315  }
316 #endif
317  vout.general(vl, "\n");
318 
320 
321 
322  // #### Execution main part ####
323  for (int iq = 0; iq < N_quark; ++iq) {
324  vout.general(vl, "Solving quark propagator, flavor = %d:\n", iq + 1);
325  vout.general(vl, " color spin Nconv diff diff2\n");
326 
327  for (int ispin = 0; ispin < Nd; ++ispin) {
328  for (int icolor = 0; icolor < Nc; ++icolor) {
329  int i_cd = icolor + Nc * ispin;
330 
331  Field_F b; // b.set(0.0);
332  source[iq]->set(b, i_cd);
333 
334  Field_F xq;
335  int Nconv;
336  double diff;
337 
338  std::string filename = FileUtils::generate_filename("%s_c%d_s%d.dat", savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
339 
340  bool use_init_guess = params_quark[iq].lookup("Solver").get_bool("use_initial_guess");
341  std::ifstream ifs(filename);
342  if ( use_init_guess && ifs.is_open() ) {
343  field_io->read_file(&xq, filename);
344  }
345 
346  fprop_lex[iq]->invert_D(xq, b, Nconv, diff);
347 
348  Field_F y(b);
349  fopr[iq]->set_mode("D");
350  fopr[iq]->mult(y, xq); // y = fopr[iq]->mult(xq);
351  axpy(y, -1.0, b); // y -= b;
352  double diff2 = y.norm2() / b.norm2();
353 
354  vout.general(vl, " %2d %2d %6d %12.4e %12.4e\n",
355  icolor, ispin, Nconv, diff, diff2);
356 
357  field_io->write_file(&xq, filename);
358  }
359  }
360 
361  vout.general(vl, "\n");
362  }
363 
364 #ifdef LIB_CPP11
365  // do nothing
366 #else
367  // tidy-up
368  for (int iq = 0; iq < N_quark; ++iq) {
369  delete fopr[iq];
370  delete solver[iq];
371  delete fprop_lex[iq];
372  delete source[iq];
373  }
374 #endif
375 
376 
377  return EXIT_SUCCESS;
378  }
379 
380 
381  //====================================================================
382  int calculate_hadron_correlator_withFileIO_initial_guess(const std::string& filename_input)
383  {
384  // #### parameter setup ####
385  const int Nc = CommonParameters::Nc();
386  const int Nd = CommonParameters::Nd();
387  const int Ndim = CommonParameters::Ndim();
388  const int Nvol = CommonParameters::Nvol();
389 
390  const Parameters params_all = ParameterManager::read(filename_input);
391 
392  const Parameters params_test = params_all.lookup("Test_Spectrum");
393 
394  const int N_quark = params_test.get_int("number_of_valence_quarks");
395 
396  const std::string str_vlevel = params_test.get_string("verbose_level");
397 
398  const bool do_check = params_test.is_set("expected_result");
399  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
400 
401  const std::string str_proj_type = params_all.lookup("Projection").get_string("projection_type");
402  const int Nsmear = params_all.lookup("Director_Smear").get_int("number_of_smearing");
403 
404  std::vector<std::string> savefile_base(N_quark);
405 
406  for (int iq = 0; iq < N_quark; ++iq) {
407  savefile_base[iq] = params_all
408  .lookup(Filename("Quark_{id}").format(iq + 1))
409  .get_string("temporary_filename_base");
410  }
411 
412  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
413 
414  //- print input parameters
415  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
416 
417  // NB. all str_gmset_type are supposed to be the same.
418  const std::string str_gmset_type = params_all.lookup("Quark_1").lookup("Fopr").get_string("gamma_matrix_type");
419  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
420 
421  for (int iq = 0; iq < N_quark; ++iq) {
422  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
423  }
424 
425  if ( (Nsmear > 0) && (str_proj_type == "Stout_SU3") ) {
426  if (CommonParameters::Nc() != 3) {
427  vout.crucial(vl, "check skipped: Nc = 3 is needed, but Nc = %d.\n\n", CommonParameters::Nc());
428  return EXIT_SKIP;
429  }
430  }
431 
432 
433  // #### Set up objects #####
434  unique_ptr<GammaMatrixSet> gmset(GammaMatrixSet::New(str_gmset_type));
435 
437 
438  Corr2pt_4spinor corr(gmset);
439  corr.set_parameters(params_all.lookup("Corr2pt_4spinor"));
440 
441 
442  // #### Execution main part ####
443  typedef std::vector<Field_F> PropagatorSet;
444  std::vector<PropagatorSet> sq(N_quark);
445  for (int iq = 0; iq < N_quark; ++iq) {
446  sq[iq].resize(Nc * Nd);
447 
448  for (int i_cd = 0; i_cd < Nc * Nd; ++i_cd) {
449  sq[iq][i_cd].set(0.0);
450  }
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 hadron_2ptFunction_withFileIO_initial_guess(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
int calculate_hadron_correlator_withFileIO_initial_guess(const std::string &)
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
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:320
Get quark propagator for Fopr with lexical site index.
int calculate_quark_propagator_withFileIO_initial_guess(const std::string &)
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