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