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