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