Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_Spectrum_CRSMatrix_Clover_Lexical.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "Fopr/fopr_CRS.h"
17 #include "Fopr/fopr_Clover.h"
18 
20 
21 #include "Solver/solver_CG.h"
22 
23 #include "IO/gaugeConfig.h"
24 
25 //====================================================================
27 
44 namespace Test_Spectrum_CRSMatrix {
45  //- test-private parameters
46  namespace {
47  const std::string filename_input = "test_Spectrum_CRSMatrix_Clover_Lexical.yaml";
48  }
49 
50  //- prototype declaration
51  int clover_lex(void);
52 
53  int CRSsolver(
54  const string& solution,
55  const string& matrix,
56  const string& source,
57  double& result /* return value */
58  );
59 
60  void write_text(const Field& f, const string& filename);
61  void read_text(Field& f, const string& filename);
62 
63 
64 #ifdef USE_TESTMANAGER_AUTOREGISTER
65 #ifdef USE_MPI
66  // this test runs only in single-node environment.
67 #else
68  namespace {
69 #if defined(USE_GROUP_SU2)
70  // Nc=2 is not available.
71 #else
72  static const bool is_registered = TestManager::RegisterTest(
73  "Spectrum.CRSMatrix.Clover_Lexical",
74  clover_lex
75  );
76 #endif
77  }
78 #endif
79 #endif
80 
81  //====================================================================
82  int clover_lex(void)
83  {
84  if (CommonParameters::NPE() > 1) {
85  // run in parallel: unsupported.
86  vout.general("CRSMatrix test: node-parallel unsupported. skip.\n");
87  return EXIT_SKIP;
88  }
89 
90  // #### parameter setup ####
91  const int Nc = CommonParameters::Nc();
92  const int Nd = CommonParameters::Nd();
93  const int Ndim = CommonParameters::Ndim();
94  const int Nvol = CommonParameters::Nvol();
95 
96  const Parameters params_all = ParameterManager::read(filename_input);
97 
98  const Parameters params_test = params_all.lookup("Test_Spectrum_CRSMatrix");
99 
100  const string str_gconf_read = params_test.get_string("gauge_config_type_input");
101  const string readfile = params_test.get_string("config_filename_input");
102  const string matrix_file = params_test.get_string("matrix_output");
103  const string source_file = params_test.get_string("source_output");
104  const string solution_file = params_test.get_string("solution_output");
105  const string str_vlevel = params_test.get_string("verbose_level");
106 
107  const bool do_check = params_test.is_set("expected_result");
108  const double expected_result = do_check ? params_test.get_double("expected_result") : 0.0;
109 
110  const Bridge::VerboseLevel vl = vout.set_verbose_level(str_vlevel);
111 
112  //- print input parameters
113  vout.general(vl, " gconf_read = %s\n", str_gconf_read.c_str());
114  vout.general(vl, " readfile = %s\n", readfile.c_str());
115  vout.general(vl, " matrix_output = %s\n", matrix_file.c_str());
116  vout.general(vl, " source_output = %s\n", source_file.c_str());
117  vout.general(vl, " solution_output = %s\n", solution_file.c_str());
118  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
119  vout.general(vl, "\n");
120 
121  //- input parameter check
122  int err = 0;
123  err += ParameterCheck::non_NULL(str_gconf_read);
124  err += ParameterCheck::non_NULL(readfile);
125  err += ParameterCheck::non_NULL(matrix_file);
126  err += ParameterCheck::non_NULL(source_file);
127  err += ParameterCheck::non_NULL(solution_file);
128 
129  if (err) {
130  vout.crucial(vl, "Error: Test_Spectrum_CRSMatrix: input parameters have not been set\n");
131  exit(EXIT_FAILURE);
132  }
133 
134 
135  // #### Set up a gauge configuration ####
136  unique_ptr<Field_G> U(new Field_G(Nvol, Ndim));
137  const unique_ptr<GaugeConfig> gconf_read(new GaugeConfig(str_gconf_read));
138  gconf_read->read_file(U, readfile);
139 
140 
141  // #### object setup #####
142  const double kappa = 0.12;
143  const double cSW = 1.0;
144  std::vector<int> boundary(Ndim);
145  boundary[0] = -1;
146  boundary[1] = -1;
147  boundary[2] = -1;
148  boundary[3] = -1;
149 
150  Parameters params_c;
151  params_c.set_double("hopping_parameter", kappa);
152  params_c.set_double("clover_coefficient", cSW);
153  params_c.set_int_vector("boundary_condition", boundary);
154  params_c.set_string("verbose_level", str_vlevel.c_str());
155 
156  unique_ptr<Fopr> fopr_c(Fopr::New("Clover", "Dirac"));
157  fopr_c->set_parameters(params_c);
158  fopr_c->set_config(U);
159  fopr_c->set_mode("D");
160 
161  const unique_ptr<Fopr_CRS> fopr_crs(new Fopr_CRS(fopr_c.get()));
162  fopr_crs->write_matrix(matrix_file);
163 
164  const int Niter = 100;
165  const int Nrestart = 40;
166  const double Stop_cond = 1.0e-28;
167  const bool use_init_guess = false;
168 
169  Parameters params_solver;
170  params_solver.set_int("maximum_number_of_iteration", Niter);
171  params_solver.set_int("maximum_number_of_restart", Nrestart);
172  params_solver.set_double("convergence_criterion_squared", Stop_cond);
173  params_solver.set_bool("use_initial_guess", use_init_guess);
174  params_solver.set_string("verbose_level", str_vlevel.c_str());
175 
176  const unique_ptr<Solver> solver(new Solver_CG(fopr_c));
177  solver->set_parameters(params_solver);
178 
179  std::vector<int> source_position(Ndim);
180  source_position[0] = 0;
181  source_position[1] = 0;
182  source_position[2] = 0;
183  source_position[3] = 0;
184 
185  const unique_ptr<Source> source(Source::New("Local"));
186  Parameters params_source;
187  params_source.set_int_vector("source_position", source_position);
188  params_source.set_string("verbose_level", str_vlevel.c_str());
189  source->set_parameters(params_source);
190 
191 
192  // #### Execution main part ####
193  std::vector<Field_F> sq(Nc * Nd);
194  for (int idx = 0; idx < Nc * Nd; ++idx) {
195  sq[idx].set(0.0);
196  }
197 
198  for (int ispin = 0; ispin < Nd; ++ispin) {
199  for (int icolor = 0; icolor < Nc; ++icolor) {
200  int idx = icolor + Nc * ispin;
201 
202  Field_F b;
203  source->set(b, idx);
204 
205  write_text(b, source_file.c_str());
206 
207  Field_F b2;
208  fopr_c->set_mode("D");
209  fopr_c->mult_dag(b2, b);
210 
211  Field_F xq;
212  int Nconv;
213  double diff_CG;
214  fopr_c->set_mode("DdagD");
215  solver->solve(xq, b2, Nconv, diff_CG);
216  vout.general(vl, " ispin = %2d icolor = %2d Nconv = %4d diff = %12.6e\n",
217  ispin, icolor, Nconv, diff_CG);
218 
219  write_text(xq, solution_file.c_str());
220 
221  Field y(b);
222  fopr_c->set_mode("D");
223  fopr_c->mult(y, xq);
224  axpy(y, -1.0, b); // y -= b;
225  scal(y, -1.0); //y *= -1.0;
226 
227  double yy = y.norm2();
228  vout.general(vl, " standard norm2 = %.8e\n", yy);
229 
230  sq[icolor + Nc * ispin] = xq;
231  }
232  }
233 
234 #if 0
235  // ### Solver for CRS version ###
236  unique_ptr<Solver> solver_crs(new Solver_CG(fopr_crs2));
237  solver_crs->set_parameters(Niter, Stop_cond);
238 
239  double yy = 0.0L;
240  {
241  const int ispin = 0;
242  {
243  const int icolor = 0;
244  //for(int ispin = 0; ispin < Nd; ++ispin){
245  // for(int icolor = 0; icolor < Nc; ++icolor){
246 
247  source->set(b, icolor, ispin);
248 
249  fopr_crs2->set_mode("Ddag");
250  b2 = (Field_F)fopr_crs2->mult((Field)b);
251 
252  int Nconv;
253  double diff_CG;
254  fopr_crs2->set_mode("DdagD");
255  solver_crs->solve(xq, b2, Nconv, diff_CG);
256  vout.general(vl, " ispin = %2d icolor = %2d Nconv = %4d diff = %12.6e\n",
257  ispin, icolor, Nconv, diff_CG);
258 
259  Field_F y(b);
260  fopr_crs2->set_mode("D");
261  y -= (Field_F)fopr_crs2->mult((Field)xq);
262  yy = y.norm2();
263  vout.general(vl, " standard norm2 = %.8e\n", yy);
264 
265  sq[icolor + Nc * ispin] = xq;
266  }
267  }
268 #endif
269 
270 
271  double result;
272  CRSsolver(solution_file, matrix_file, source_file, result);
273 
274 
275  if (do_check) {
276  return Test::verify(result, expected_result);
277  } else {
278  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
279  return EXIT_SKIP;
280  }
281  }
282 } // namespace Test_Spectrum_CRSMatrix
#define EXIT_SKIP
Definition: test.h:17
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:433
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
double norm2() const
Definition: field.cpp:592
void set_int_vector(const string &key, const vector< int > &value)
Definition: parameters.cpp:45
void general(const char *format,...)
Definition: bridgeIO.cpp:197
virtual void set_mode(const std::string mode)
setting the mode of multiplication if necessary. Default implementation here is just to avoid irrelev...
Definition: fopr.h:94
void set_int(const string &key, const int value)
Definition: parameters.cpp:36
int solver(const std::string &)
virtual void set_config(Field *)=0
setting pointer to the gauge configuration.
Container of Field-type object.
Definition: field.h:45
Class for parameters.
Definition: parameters.h:46
Standard Conjugate Gradient solver algorithm.
Definition: solver_CG.h:38
Wilson-type fermion field.
Definition: field_F.h:37
virtual void set_parameters(const Parameters &params)=0
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
void set_string(const string &key, const string &value)
Definition: parameters.cpp:39
void set_bool(const string &key, const bool value)
Definition: parameters.cpp:30
SU(N) gauge field.
Definition: field_G.h:38
double get_double(const string &key) const
Definition: parameters.cpp:175
int CRSsolver(const string &solution, const string &matrix, const string &source, double &result)
pointer get() const
virtual void set_parameters(const Parameters &)=0
int non_NULL(const std::string v)
bool is_set(const string &key) const
Definition: parameters.cpp:528
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:319
void write_text(const Field &f, const string &filename)
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
static void read(const std::string &params_file, Parameters &params)
void read_file(Field_G *U, const string &filename)
Definition: gaugeConfig.h:104
void read_text(Field &f, const string &filename)
int verify(const double result, const double expected, double eps)
Definition: test.cpp:27
void set_double(const string &key, const double value)
Definition: parameters.cpp:33
Bridge::VerboseLevel vl
VerboseLevel
Definition: bridgeIO.h:42
virtual void mult(Field &, const Field &)=0
multiplies fermion operator to a given field (2nd argument)
void write_matrix(const std::string)
Definition: fopr_CRS.cpp:271
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:78
virtual void mult_dag(Field &, const Field &)
hermitian conjugate of mult(Field&, const Field&).
Definition: fopr.h:73
string get_string(const string &key) const
Definition: parameters.cpp:221
Fermion operator with CRS matrix format.
Definition: fopr_CRS.h:37
virtual void solve(Field &solution, const Field &source, int &Nconv, double &diff)=0
virtual void set_parameters(const Parameters &)=0
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:131
virtual void set(Field &, const int)=0