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