Bridge++  Ver. 2.0.2
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 
77  // int hadron_2ptFunction_withFileIO_CloverGeneral()
78  // {
79  // return hadron_2ptFunction_withFileIO("test_Spectrum_CloverGeneral_Hadron2ptFunction.yaml");
80  // }
81 
82 
83  // int hadron_2ptFunction_withFileIO_Wilson_TwistedMass()
84  // {
85  // return hadron_2ptFunction_withFileIO("test_Spectrum_Wilson_TwistedMass_Hadron2ptFunction.yaml");
86  // }
87 
88 
89  // int hadron_2ptFunction_withFileIO_Wilson()
90  // {
91  // return hadron_2ptFunction_withFileIO("test_Spectrum_Wilson_Hadron2ptFunction.yaml");
92  // }
93 
94 
95  // int hadron_2ptFunction_withFileIO_WilsonGeneral()
96  // {
97  // return hadron_2ptFunction_withFileIO("test_Spectrum_WilsonGeneral_Hadron2ptFunction.yaml");
98  // }
99 
100 
101 #ifdef USE_TESTMANAGER_AUTOREGISTER
102  namespace {
103 #if defined(USE_GROUP_SU2)
104  // Nc=2 is not available.
105 #else
106  static const bool is_registered_Clover = TestManager::RegisterTest(
107  "Spectrum.Clover.Hadron2ptFunction_withFileIO_initial_guess",
109  );
110 #endif
111  }
112 #endif
113 
114  //====================================================================
115  int hadron_2ptFunction_withFileIO_initial_guess(const std::string& filename_input)
116  {
117  int result = 0;
118 
119  Timer timer(test_name);
120 
121 
122  // #### Execution main part ####
123  timer.start();
124 
125  result += calculate_quark_propagator_withFileIO_initial_guess(filename_input);
127 
128  timer.report();
129 
130  return result;
131  }
132 
133 
134  //====================================================================
135  int calculate_quark_propagator_withFileIO_initial_guess(const std::string& filename_input)
136  {
137  // #### parameter setup ####
138  const int Nc = CommonParameters::Nc();
139  const int Nd = CommonParameters::Nd();
140  const int Ndim = CommonParameters::Ndim();
141  const int Nvol = CommonParameters::Nvol();
142 
143  const Parameters params_all = ParameterManager::read(filename_input);
144 
145  const Parameters params_test = params_all.lookup("Test_Spectrum");
146 
147  const int N_quark = params_test.get_int("number_of_valence_quarks");
148 
149  const std::string str_gconf_status = params_test.get_string("gauge_config_status");
150  const std::string str_gconf_read = params_test.get_string("gauge_config_type_input");
151  const std::string readfile = params_test.get_string("config_filename_input");
152  const vector<int> Nshift_origin = params_test.get_int_vector("shift_origin");
153  const std::string str_vlevel = params_test.get_string("verbose_level");
154 
155  const std::string str_gfix_type = params_all.lookup("GaugeFixing").get_string("gauge_fixing_type");
156  const std::string str_proj_type = params_all.lookup("Projection").get_string("projection_type");
157  const std::string str_smear_type = params_all.lookup("Smear").get_string("smear_type");
158  const int Nsmear = params_all.lookup("Director_Smear").get_int("number_of_smearing");
159 
160  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
161 
162  //- print input parameters
163  vout.general(vl, " gconf_status = %s\n", str_gconf_status.c_str());
164  vout.general(vl, " gconf_read = %s\n", str_gconf_read.c_str());
165  vout.general(vl, " readfile = %s\n", readfile.c_str());
166  for (int mu = 0; mu < Ndim; ++mu) {
167  vout.general(vl, " shift_origin[%d] = %d\n", mu, Nshift_origin[mu]);
168  }
169  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
170  vout.general(vl, " gfix_type = %s\n", str_gfix_type.c_str());
171  vout.general(vl, " proj_type = %s\n", str_proj_type.c_str());
172  vout.general(vl, " smear_type = %s\n", str_smear_type.c_str());
173 
174  vector<Parameters> params_quark;
175  for (int iq = 0; iq < N_quark; ++iq) {
176  std::string qlabel = Filename("Quark_{id}").format(iq + 1);
177  params_quark.push_back(params_all.lookup(qlabel));
178  }
179 
180  // NB. all str_gmset_type are supposed to be the same.
181  std::string str_gmset_type = params_quark[0].lookup("Fopr").get_string("gamma_matrix_type");
182  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
183 
184  std::vector<std::string> savefile_base(N_quark);
185  for (int iq = 0; iq < N_quark; ++iq) {
186  savefile_base[iq] = params_quark[iq].get_string("temporary_filename_base");
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  std::string str_source_type = params_quark[iq].lookup("Source").get_string("source_type");
192  vector<int> source_position = params_quark[iq].lookup("Source").get_int_vector("source_position");
193 
194  vout.general(vl, " Quark_%d:\n", iq + 1);
195  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
196  vout.general(vl, " solver_type = %s\n", str_solver_type.c_str());
197  vout.general(vl, " source_type = %s\n", str_source_type.c_str());
198  for (int mu = 0; mu < Ndim; ++mu) {
199  vout.general(vl, " source_position[%d] = %d\n", mu, source_position[mu]);
200  }
201  }
202  vout.general(vl, "\n");
203 
204 
205  //- input parameter check
206  int err = 0;
207  err += ParameterCheck::non_NULL(str_gconf_status);
208 
209  if (err) {
210  vout.crucial(vl, "Error at %s: input parameters have not been set\n", test_name.c_str());
211  exit(EXIT_FAILURE);
212  }
213 
214  for (int iq = 0; iq < N_quark; ++iq) {
215  std::string str_solver_type = params_quark[iq].lookup("Solver").get_string("solver_type");
216 
217  if (str_solver_type == "CG") {
218  vout.crucial(vl, "Error at %s: CG can not be adopted. Use CGNE,CGNR, instead.\n", test_name.c_str());
219  exit(EXIT_FAILURE);
220  }
221  }
222 
223  if ((Nsmear > 0) && (str_proj_type == "Stout_SU3")) {
224  if (CommonParameters::Nc() != 3) {
225  vout.crucial(vl, "check skipped: Nc = 3 is needed, but Nc = %d.\n\n", CommonParameters::Nc());
226  //- NB. EXIT_SKIP is set in calculate_hadron_correlator_withFileIO()
227  // return EXIT_SKIP;
228  return EXIT_SUCCESS;
229  }
230  }
231 
232  if ((str_gfix_type == "Coulomb") || (str_gfix_type == "Landau")) {
233  if (CommonParameters::Nc() != 3) {
234  vout.crucial(vl, "check skipped: Nc = 3 is needed, but Nc = %d.\n\n", CommonParameters::Nc());
235  return EXIT_SKIP;
236  }
237  }
238 
239 
240  RandomNumberManager::initialize("Mseries", 1234567UL);
241 
242 
243  // #### Set up a gauge configuration ####
244  Field_G U(Nvol, Ndim);
245 
246  if (str_gconf_status == "Continue") {
247  GaugeConfig(str_gconf_read).read(U, readfile);
248  } else if (str_gconf_status == "Cold_start") {
249  GaugeConfig("Unit").read(U);
250  } else if (str_gconf_status == "Hot_start") {
251  GaugeConfig("Random").read(U);
252  } else {
253  vout.crucial(vl, "Error at %s: unsupported gconf status \"%s\"\n", test_name.c_str(), str_gconf_status.c_str());
254  exit(EXIT_FAILURE);
255  }
256 
257  {
259  for (int i_dir = 0; i_dir < Ndim; ++i_dir) {
260  for (int i_shift = 0; i_shift < Nshift_origin[i_dir]; ++i_shift) {
261  Field_G Ushift(Nvol, Ndim);
262  shift.backward(Ushift, U, i_dir);
263  copy(U, Ushift);
264  }
265  }
266  }
267 
268  if (Nsmear > 0) {
269  unique_ptr<Projection> proj(Projection::New(str_proj_type, params_all.lookup("Projection")));
270  unique_ptr<Smear> smear(Smear::New(str_smear_type, proj.get(), params_all.lookup("Smear")));
271  unique_ptr<Director_Smear> dr_smear(new Director_Smear(smear.get(), params_all.lookup("Director_Smear")));
272  dr_smear->set_config(&U);
273 
274  const Field_G *Usmear = (Field_G *)dr_smear->getptr_smearedConfig(Nsmear);
275  copy(U, *Usmear);
276  }
277 
278  // #### Gauge fixing ####
279  {
280  Field_G Ufix(Nvol, Ndim);
281  unique_ptr<GaugeFixing> gfix(GaugeFixing::New(str_gfix_type, params_all.lookup("GaugeFixing")));
282 
283  gfix->fix(Ufix, U);
284  copy(U, Ufix);
285  }
286 
287 
288  // #### object setup #####
289 
290  struct QuarkType
291  {
292  unique_ptr<Fopr> fopr;
293  unique_ptr<Solver> solver;
294  unique_ptr<Fprop> fprop;
295  unique_ptr<Source> source;
296  bool use_init_guess;
297  };
298 
299  std::vector<QuarkType> quark(N_quark);
300 
301  for (int iq = 0; iq < N_quark; ++iq) {
302  const Parameters& params_fopr = params_quark[iq].lookup("Fopr");
303  const Parameters& params_solver = params_quark[iq].lookup("Solver");
304  const Parameters& params_source = params_quark[iq].lookup("Source");
305 
306  quark[iq].fopr.reset(Fopr::New(params_fopr.get_string("fermion_type"),
307  params_fopr));
308  quark[iq].fopr->set_config(&U);
309 
310  quark[iq].solver.reset(Solver::New(params_solver.get_string("solver_type"),
311  quark[iq].fopr.get(),
312  params_solver));
313  quark[iq].use_init_guess = params_solver.get_bool("use_initial_guess");
314 
315  quark[iq].fprop.reset(new Fprop_Standard_lex(quark[iq].solver.get()));
316 
317  quark[iq].source.reset(Source::New(params_source.get_string("source_type"),
318  params_source));
319  }
320  vout.general(vl, "\n");
321 
322  unique_ptr<FieldIO> field_io(new FieldIO_Binary(IO_Format::Trivial));
323 
324 
325  // #### Execution main part ####
326  for (int iq = 0; iq < N_quark; ++iq) {
327  vout.general(vl, "Solving quark propagator, flavor = %d:\n", iq + 1);
328  vout.general(vl, " color spin Nconv diff diff2\n");
329 
330  for (int ispin = 0; ispin < Nd; ++ispin) {
331  for (int icolor = 0; icolor < Nc; ++icolor) {
332  int i_cd = icolor + Nc * ispin;
333 
334  Field_F b; // b.set(0.0);
335  quark[iq].source->set(b, i_cd);
336 
337  Field_F xq;
338  int Nconv;
339  double diff;
340 
341  std::string filename = FileUtils::generate_filename(
342  "%s_c%d_s%d.dat",
343  savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
344 
345  if (quark[iq].use_init_guess) {
346  // check if file exists, and read.
347  std::ifstream ifs(filename);
348  if (ifs.is_open()) {
349  field_io->read_file(xq, filename);
350  }
351  }
352 
353  quark[iq].fprop->invert_D(xq, b, Nconv, diff);
354 
355  Field_F y(b);
356  quark[iq].fopr->set_mode("D");
357  quark[iq].fopr->mult(y, xq); // y = fopr[iq]->mult(xq);
358  axpy(y, -1.0, b); // y -= b;
359  double diff2 = y.norm2() / b.norm2();
360 
361  vout.general(vl, " %2d %2d %6d %12.4e %12.4e\n",
362  icolor, ispin, Nconv, diff, diff2);
363 
364  field_io->write_file(xq, filename);
365  }
366  }
367 
368  vout.general(vl, "\n");
369  }
370 
371  return EXIT_SUCCESS;
372  }
373 
374 
375  //====================================================================
376  int calculate_hadron_correlator_withFileIO_initial_guess(const std::string& filename_input)
377  {
378  // #### parameter setup ####
379  const int Nc = CommonParameters::Nc();
380  const int Nd = CommonParameters::Nd();
381  const int Ndim = CommonParameters::Ndim();
382  const int Nvol = CommonParameters::Nvol();
383 
384  const Parameters params_all = ParameterManager::read(filename_input);
385 
386  const Parameters params_test = params_all.lookup("Test_Spectrum");
387 
388  const int N_quark = params_test.get_int("number_of_valence_quarks");
389 
390  const std::string str_vlevel = params_test.get_string("verbose_level");
391 
392  const bool do_check = params_test.is_set("expected_result");
393  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
394 
395  const std::string str_proj_type = params_all.lookup("Projection").get_string("projection_type");
396  const int Nsmear = params_all.lookup("Director_Smear").get_int("number_of_smearing");
397 
398  std::vector<std::string> savefile_base(N_quark);
399 
400  for (int iq = 0; iq < N_quark; ++iq) {
401  savefile_base[iq] = params_all
402  .lookup(Filename("Quark_{id}").format(iq + 1))
403  .get_string("temporary_filename_base");
404  }
405 
406  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
407 
408  //- print input parameters
409  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
410 
411  // NB. all str_gmset_type are supposed to be the same.
412  const std::string str_gmset_type = params_all.lookup("Quark_1").lookup("Fopr").get_string("gamma_matrix_type");
413  vout.general(vl, " gmset_type = %s\n", str_gmset_type.c_str());
414 
415  for (int iq = 0; iq < N_quark; ++iq) {
416  vout.general(vl, " savefile_base[iq] = %s[%d]\n", savefile_base[iq].c_str(), iq);
417  }
418 
419  if ((Nsmear > 0) && (str_proj_type == "Stout_SU3")) {
420  if (CommonParameters::Nc() != 3) {
421  vout.crucial(vl, "check skipped: Nc = 3 is needed, but Nc = %d.\n\n", CommonParameters::Nc());
422  return EXIT_SKIP;
423  }
424  }
425 
426 
427  // #### Set up objects #####
428  unique_ptr<GammaMatrixSet> gmset(GammaMatrixSet::New(str_gmset_type));
429 
430  unique_ptr<FieldIO> field_io(new FieldIO_Binary(IO_Format::Trivial));
431 
432  Corr2pt_4spinor corr(gmset.get(), params_all.lookup("Corr2pt_4spinor"));
433 
434 
435  // #### Execution main part ####
436  typedef std::vector<Field_F> PropagatorSet;
437  std::vector<PropagatorSet> sq(N_quark);
438  for (int iq = 0; iq < N_quark; ++iq) {
439  sq[iq].resize(Nc * Nd);
440 
441  for (int i_cd = 0; i_cd < Nc * Nd; ++i_cd) {
442  sq[iq][i_cd].set(0.0);
443  }
444  }
445 
446 
447  for (int iq = 0; iq < N_quark; ++iq) {
448  for (int ispin = 0; ispin < Nd; ++ispin) {
449  for (int icolor = 0; icolor < Nc; ++icolor) {
450  int i_cd = icolor + Nc * ispin;
451 
452  std::string filename = FileUtils::generate_filename("%s_c%d_s%d.dat", savefile_base[iq].c_str(), (icolor + 1), (ispin + 1));
453  field_io->read_file(sq[iq][i_cd], filename);
454  }
455  }
456  }
457 
458 
459  //- meson correlators
460  std::vector<double> result(N_quark);
461 
462  vout.general(vl, "2-point correlator:\n");
463 
464  //- case(iq_1 == iq_2)
465  for (int iq = 0; iq < N_quark; ++iq) {
466  vout.general(vl, "Flavor combination = %d, %d\n", iq + 1, iq + 1);
467  result[iq] = corr.meson_all(sq[iq], sq[iq]);
468  vout.general(vl, "\n");
469  }
470 
471 
472  //- case(iq_1 < iq_2)
473  for (int iq = 0; iq < N_quark; ++iq) {
474  for (int jq = iq + 1; jq < N_quark; ++jq) {
475  vout.general(vl, "Flavor combination = %d, %d\n", iq + 1, jq + 1);
476  double result_2 = corr.meson_all(sq[iq], sq[jq]);
477  vout.general(vl, "\n");
478  }
479  }
480 
482 
483 
484  if (do_check) {
485  return Test::verify(result[0], expected_result);
486  } else {
487  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
488  return EXIT_SKIP;
489  }
490  }
491 } // namespace Test_Spectrum
Test::verify
int verify(const double result, const double expected, double eps)
Definition: test.cpp:27
Test_Spectrum::calculate_hadron_correlator_withFileIO_initial_guess
int calculate_hadron_correlator_withFileIO_initial_guess(const std::string &)
Definition: test_Spectrum_2ptFunction_withFileIO_initial_guess.cpp:376
Test_Spectrum::hadron_2ptFunction_withFileIO_initial_guess
int hadron_2ptFunction_withFileIO_initial_guess(const std::string &)
Definition: test_Spectrum_2ptFunction_withFileIO_initial_guess.cpp:115
ParameterCheck::non_NULL
int non_NULL(const std::string v)
Definition: parameterCheck.cpp:65
Fprop_Standard_lex
Get quark propagator for Fopr with lexical site index.
Definition: fprop_Standard_lex.h:33
Test_Spectrum::test_name
const std::string test_name
Definition: test_Spectrum_2ptFunction.cpp:72
CommonParameters::Ndim
static int Ndim()
Definition: commonParameters.h:117
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
Parameters
Class for parameters.
Definition: parameters.h:46
Corr2pt_4spinor::meson_all
double meson_all(const std::vector< Field_F > &sq1, const std::vector< Field_F > &sq2)
Definition: corr2pt_4spinor.cpp:53
shiftField_lex.h
Parameters::get_int
int get_int(const string &key) const
Definition: parameters.cpp:192
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
GaugeConfig::read
void read(Field_G &U, const string &filename=string())
Definition: gaugeConfig.cpp:121
fprop_Standard_lex.h
gaugeConfig.h
CommonParameters::Nvol
static int Nvol()
Definition: commonParameters.h:109
axpy
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:380
filename.h
Parameters::get_int_vector
vector< int > get_int_vector(const string &key) const
Definition: parameters.cpp:267
source.h
Timer
Definition: timer.h:31
RandomNumberManager::finalize
static void finalize()
Definition: randomNumberManager.cpp:80
copy
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:212
IO_Format::Trivial
const Format * Trivial
Definition: io_format.cpp:27
RandomNumberManager::initialize
static bool initialize(const std::string &rng_type, unsigned long seed)
Definition: randomNumberManager.cpp:57
Field::norm2
double norm2() const
Definition: field.cpp:113
Timer::start
void start()
Definition: timer.cpp:44
CommonParameters::Nc
static int Nc()
Definition: commonParameters.h:115
ParameterCheck::vl
Bridge::VerboseLevel vl
Definition: parameterCheck.cpp:18
file_utils.h
test.h
ParameterManager::read
static void read(const std::string &params_file, Parameters &params)
Definition: parameterManager.cpp:33
FieldIO_Binary
FieldIO_Binary class for file I/O of Field data in binary format.
Definition: fieldIO_Binary.h:47
Parameters::get_bool
bool get_bool(const string &key) const
Definition: parameters.cpp:233
Parameters::get_double
double get_double(const string &key) const
Definition: parameters.cpp:175
Corr2pt_4spinor
Two-point correlator for Wilson-type fermions.
Definition: corr2pt_4spinor.h:42
Director_Smear
Manager of smeared configurations.
Definition: director_Smear.h:39
Test_Solver_Wilson::solver
int solver(const std::string &)
Definition: test_Solver_Wilson.cpp:134
ShiftField_lex
Methods to shift a field in the lexical site index.
Definition: shiftField_lex.h:39
EXIT_SKIP
#define EXIT_SKIP
Definition: test.h:17
FileUtils::generate_filename
std::string generate_filename(const char *fmt,...)
Definition: file_utils.cpp:17
CommonParameters::Nd
static int Nd()
Definition: commonParameters.h:116
randomNumberManager.h
Test_Spectrum
Test of spectroscopy.
Definition: test_Spectrum_2ptFunction.cpp:71
GaugeConfig
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:80
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
Parameters::is_set
bool is_set(const string &key) const
Definition: parameters.cpp:525
Test_Spectrum::calculate_quark_propagator_withFileIO_initial_guess
int calculate_quark_propagator_withFileIO_initial_guess(const std::string &)
Definition: test_Spectrum_2ptFunction_withFileIO_initial_guess.cpp:135
Field_F
Wilson-type fermion field.
Definition: field_F.h:37
Test_Spectrum::hadron_2ptFunction_withFileIO_Clover_initial_guess
int hadron_2ptFunction_withFileIO_Clover_initial_guess()
Definition: test_Spectrum_2ptFunction_withFileIO_initial_guess.cpp:71
fopr_Smeared.h
Parameters::get_string
string get_string(const string &key) const
Definition: parameters.cpp:221
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Bridge::VerboseLevel
VerboseLevel
Definition: bridgeIO.h:42
Field_G
SU(N) gauge field.
Definition: field_G.h:38
gaugeFixing.h
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
corr2pt_4spinor.h
Filename::format
std::string format()
Definition: filename.h:61
Filename
Filename utility.
Definition: filename.h:43
gammaMatrixSet.h
Parameters::lookup
Parameters lookup(const string &key) const
Definition: parameters.h:79
Timer::report
void report(const Bridge::VerboseLevel vl=Bridge::GENERAL)
Definition: timer.cpp:128
Test_Gauge::shift
int shift(void)
Definition: test_Gauge_Shift.cpp:58
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512
TestManager::RegisterTest
static bool RegisterTest(const std::string &key, const Test_function func)
Definition: testManager.h:69