Bridge++  Ver. 2.0.2
test_Spectrum_CRSMatrix_CRSsolver.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "Fopr/fopr_CRS.h"
17 #include "Solver/solver_CG.h"
18 
19 //====================================================================
20 // // clover set
21 // string solution = "solution4x8_clover.crs";
22 // string matrix = "matrix4x8_clover.crs";
23 // string source = "source4x8_clover.crs";
24 
25 // // 5d-overlap set
26 // string solution = "solution4x4_ov5d.crs";
27 // string matrix = "matrix4x4_ov5d.crs";
28 // string source = "source4x4_ov5d.crs";
29 
30 // // domain-wall set
31 // string solution = "solution4x4_dw.crs";
32 // string matrix = "matrix4x4_dw.crs";
33 // string source = "source4x4_dw.crs";
34 
35 
37 {
38  void write_text(const Field& f, const string& filename);
39  void read_text(Field& f, const string& filename);
40 
41  int CRSsolver(
42  const string& solution,
43  const string& matrix,
44  const string& source,
45  double& result /* return value */
46  )
47  {
49 
50  // ##### Following part is common #####
51 
52  // read source and solution vectors
53  Field b;
54 
55  read_text(b, source);
56 
57  Field xq;
58  read_text(xq, solution);
59 
60  // read CRS matrix
61  unique_ptr<Fopr> fopr_crs(Fopr::New("CRS", matrix));
62 
63 
64  // setup of CG solver
65  int Niter = 100;
66  int Nrestart = 40;
67  double Stop_cond = 1.0e-28;
68  bool init_guess = false;
69 
70  Parameters params_solver;
71  params_solver.set_int("maximum_number_of_iteration", Niter);
72  params_solver.set_int("maximum_number_of_restart", Nrestart);
73  params_solver.set_double("convergence_criterion_squared", Stop_cond);
74  params_solver.set_bool("use_initial_guess", init_guess);
75 
76  unique_ptr<Solver> solver(new Solver_CG(fopr_crs.get(), params_solver));
77 
78  // setup of CGNE source
79  Field b2(b);
80  fopr_crs->set_mode("D");
81  fopr_crs->mult_dag(b2, b);
82 
83  // CGNE solver
84  Field x(b);
85  int Nconv;
86  double diff;
87  fopr_crs->set_mode("DdagD");
88  solver->solve(x, b2, Nconv, diff);
89  vout.general(vl, "solver(CG): Nconv = %4d diff = %12.6e\n", Nconv, diff);
90 
91  // check
92  axpy(x, -1.0, xq);
93  const double xx = x.norm2();
94  vout.general(vl, "standard norm2 = %.8e\n", xx);
95 
96  result = xx;
97 
98  return EXIT_SUCCESS;
99  }
100 
101 
102 //====================================================================
103  void write_text(const Field& f, const string& filename)
104  {
105  // This function works only on single node.
106  assert(CommonParameters::NPE() == 1);
107 
108  std::ofstream field_out(filename.c_str());
109  if (!field_out.is_open()) {
110  vout.crucial("Error: Failed to open the text file. %s\n", filename.c_str());
111  exit(EXIT_FAILURE);
112  }
113 
114  vout.general("Writing Field to %s\n", filename.c_str());
115 
116  field_out << f.nin() << std::endl;
117  field_out << f.nvol() << std::endl;
118  field_out << f.nex() << std::endl;
119 
120  field_out.setf(std::ios_base::scientific, std::ios_base::floatfield);
121  field_out.precision(14);
122 
123  for (int j = 0, n = f.size(); j < n; ++j) {
124  field_out << f.cmp(j) << std::endl;
125  }
126 
127  field_out.close();
128 
129  vout.general("Writing Field finished.\n");
130  }
131 
132 
133 //====================================================================
134  void read_text(Field& f, const string& filename)
135  {
136  // This function works only on single node.
137  assert(CommonParameters::NPE() == 1);
138 
139  std::ifstream field_in(filename.c_str());
140  if (!field_in.is_open()) {
141  vout.crucial("Error: Failed to open the text file. %s\n", filename.c_str());
142  exit(EXIT_FAILURE);
143  }
144 
145  vout.general("Reading Field from %s\n", filename.c_str());
146 
147  int Nin = 0, Nvol = 0, Nex = 0;
148  field_in >> Nin;
149  field_in >> Nvol;
150  field_in >> Nex;
151 
152  f.reset(Nin, Nvol, Nex);
153 
154  double v;
155  for (int j = 0, n = f.ntot(); j < n; ++j) {
156  field_in >> v;
157  f.set(j, v);
158  }
159 
160  field_in.close();
161 
162  vout.general("Reading Field finished.\n");
163  }
164 } // namespace Test_Spectrum_CRSMatrix
Parameters::set_bool
void set_bool(const string &key, const bool value)
Definition: parameters.cpp:30
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
Field::ntot
int ntot() const
Definition: field.h:131
Parameters::set_double
void set_double(const string &key, const double value)
Definition: parameters.cpp:33
Field::nex
int nex() const
Definition: field.h:128
axpy
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:380
Field::nin
int nin() const
Definition: field.h:126
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
ParameterCheck::vl
Bridge::VerboseLevel vl
Definition: parameterCheck.cpp:18
Field::size
int size() const
Definition: field.h:132
fopr_CRS.h
test.h
Field::nvol
int nvol() const
Definition: field.h:127
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
Test_Solver_Wilson::solver
int solver(const std::string &)
Definition: test_Solver_Wilson.cpp:134
Field::reset
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
Test_Spectrum_CRSMatrix::CRSsolver
int CRSsolver(const string &solution, const string &matrix, const string &source, double &result)
Definition: test_Spectrum_CRSMatrix_CRSsolver.cpp:41
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
CommonParameters::Vlevel
static Bridge::VerboseLevel Vlevel()
Definition: commonParameters.h:122
Test_Spectrum_CRSMatrix::read_text
void read_text(Field &f, const string &filename)
Definition: test_Spectrum_CRSMatrix_CRSsolver.cpp:134
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
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
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512