Bridge++  Version 1.4.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 "Tests/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  int Nc = CommonParameters::Nc();
92  int Nd = CommonParameters::Nd();
93  int Ndim = CommonParameters::Ndim();
94  int Nvol = CommonParameters::Nvol();
95 
96  Parameters params_all = ParameterManager::read(filename_input);
97 
98  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 
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  unique_ptr<GaugeConfig> gconf_read(new GaugeConfig(str_gconf_read));
138  gconf_read->read_file(U, readfile);
139 
140 
141  // #### object setup #####
142  double kappa = 0.12;
143  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 
155  unique_ptr<Fopr> fopr_c(Fopr::New("Clover", "Dirac"));
156  fopr_c->set_parameters(params_c);
157  fopr_c->set_config(U);
158  fopr_c->set_mode("D");
159 
160  unique_ptr<Fopr_CRS> fopr_crs(new Fopr_CRS(fopr_c.get()));
161  fopr_crs->write_matrix(matrix_file);
162 
163  int Niter = 100;
164  int Nrestart = 40;
165  double Stop_cond = 1.0e-28;
166  unique_ptr<Solver> solver(new Solver_CG(fopr_c));
167  solver->set_parameters(Niter, Nrestart, Stop_cond);
168 
169  std::vector<int> source_position(Ndim);
170  source_position[0] = 0;
171  source_position[1] = 0;
172  source_position[2] = 0;
173  source_position[3] = 0;
174 
175  unique_ptr<Source> source(Source::New("Local"));
176  Parameters params_source;
177  params_source.set_int_vector("source_position", source_position);
178  source->set_parameters(params_source);
179 
180 
181  // #### Execution main part ####
182  std::vector<Field_F> sq(Nc * Nd);
183  Field_F xq, b, b2;
184 
185  for (int ispin = 0; ispin < Nd; ++ispin) {
186  for (int icolor = 0; icolor < Nc; ++icolor) {
187  int idx = icolor + Nc * ispin;
188  source->set(b, idx);
189 
190  write_text(b, source_file.c_str());
191 
192  fopr_c->set_mode("D");
193  fopr_c->mult_dag(b2, b);
194 
195  int Nconv;
196  double diff_CG;
197  fopr_c->set_mode("DdagD");
198  solver->solve(xq, b2, Nconv, diff_CG);
199  vout.general(vl, " ispin = %2d icolor = %2d Nconv = %4d diff = %12.6e\n",
200  ispin, icolor, Nconv, diff_CG);
201 
202  write_text(xq, solution_file.c_str());
203 
204  Field y(b);
205  fopr_c->set_mode("D");
206  fopr_c->mult(y, xq);
207  axpy(y, -1.0, b); // y -= b;
208  scal(y, -1.0); //y *= -1.0;
209 
210  double yy = y.norm2();
211  vout.general(vl, " standard norm2 = %.8e\n", yy);
212 
213  sq[icolor + Nc * ispin] = xq;
214  }
215  }
216 
217 #if 0
218  // ### Solver for CRS version ###
219  unique_ptr<Solver> solver_crs(new Solver_CG(fopr_crs2));
220  solver_crs->set_parameters(Niter, Stop_cond);
221 
222  double yy = 0.0L;
223  {
224  int ispin = 0;
225  {
226  int icolor = 0;
227  //for(int ispin = 0; ispin < Nd; ++ispin){
228  // for(int icolor = 0; icolor < Nc; ++icolor){
229 
230  source->set(b, icolor, ispin);
231 
232  fopr_crs2->set_mode("Ddag");
233  b2 = (Field_F)fopr_crs2->mult((Field)b);
234 
235  int Nconv;
236  double diff_CG;
237  fopr_crs2->set_mode("DdagD");
238  solver_crs->solve(xq, b2, Nconv, diff_CG);
239  vout.general(vl, " ispin = %2d icolor = %2d Nconv = %4d diff = %12.6e\n",
240  ispin, icolor, Nconv, diff_CG);
241 
242  Field_F y(b);
243  fopr_crs2->set_mode("D");
244  y -= (Field_F)fopr_crs2->mult((Field)xq);
245  yy = y.norm2();
246  vout.general(vl, " standard norm2 = %.8e\n", yy);
247 
248  sq[icolor + Nc * ispin] = xq;
249  }
250  }
251 #endif
252 
253 
254  double result;
255  CRSsolver(solution_file, matrix_file, source_file, result);
256 
257 
258  if (do_check) {
259  return Test::verify(result, expected_result);
260  } else {
261  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
262  return EXIT_SKIP;
263  }
264  }
265 } // 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:495
void detailed(const char *format,...)
Definition: bridgeIO.cpp:212
double norm2() const
Definition: field.cpp:441
void set_int_vector(const string &key, const vector< int > &value)
Definition: parameters.cpp:40
void general(const char *format,...)
Definition: bridgeIO.cpp:195
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: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:78
static bool RegisterTest(const std::string &key, const Test_function func)
Definition: testManager.h:69
SU(N) gauge field.
Definition: field_G.h:38
double get_double(const string &key) const
Definition: parameters.cpp:70
int CRSsolver(const string &solution, const string &matrix, const string &source, double &result)
void write_matrix(std::string)
Definition: fopr_CRS.cpp:280
pointer get() const
virtual void set_parameters(const Parameters &)=0
int non_NULL(const std::string v)
Definition: checker.cpp:61
bool is_set(const string &key) const
Definition: parameters.cpp:396
virtual void set(Field &, int)=0
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:178
static void read(const std::string &params_file, Parameters &params)
void read_file(Field_G *U, const string &filename)
Definition: gaugeConfig.h:102
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:28
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:42
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:96
GaugeConfig class for file I/O of gauge configuration.
Definition: gaugeConfig.h:75
virtual void mult_dag(Field &, const Field &)
hermitian conjugate of mult(Field&, const Field&).
Definition: fopr.h:75
string get_string(const string &key) const
Definition: parameters.cpp:116
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
static int NPE()