Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
36 namespace Test_Spectrum_CRSMatrix
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  const int Niter = 100;
66  const int Nrestart = 40;
67  const double Stop_cond = 1.0e-28;
68  const unique_ptr<Solver> solver(new Solver_CG(fopr_crs));
69  solver->set_parameters(Niter, Nrestart, Stop_cond);
70 
71  // setup of CGNE source
72  Field b2(b);
73  fopr_crs->set_mode("D");
74  fopr_crs->mult_dag(b2, b);
75 
76  // CGNE solver
77  Field x(b);
78  int Nconv;
79  double diff;
80  fopr_crs->set_mode("DdagD");
81  solver->solve(x, b2, Nconv, diff);
82  vout.general(vl, "solver(CG): Nconv = %4d diff = %12.6e\n", Nconv, diff);
83 
84  // check
85  axpy(x, -1.0, xq);
86  const double xx = x.norm2();
87  vout.general(vl, "standard norm2 = %.8e\n", xx);
88 
89  result = xx;
90 
91 
92  return EXIT_SUCCESS;
93  }
94 
95 
96 //====================================================================
97  void write_text(const Field& f, const string& filename)
98  {
99  // This function works only on single node.
100  assert(CommonParameters::NPE() == 1);
101 
102  std::ofstream field_out(filename.c_str());
103  if (!field_out.is_open()) {
104  vout.crucial("Error: Failed to open the text file. %s\n", filename.c_str());
105  exit(EXIT_FAILURE);
106  }
107 
108  vout.general("Writing Field to %s\n", filename.c_str());
109 
110  field_out << f.nin() << std::endl;
111  field_out << f.nvol() << std::endl;
112  field_out << f.nex() << std::endl;
113 
114  field_out.setf(std::ios_base::scientific, std::ios_base::floatfield);
115  field_out.precision(14);
116 
117  for (int j = 0, n = f.size(); j < n; ++j) {
118  field_out << f.cmp(j) << std::endl;
119  }
120 
121  field_out.close();
122 
123  vout.general("Writing Field finished.\n");
124  }
125 
126 
127 //====================================================================
128  void read_text(Field& f, const string& filename)
129  {
130  // This function works only on single node.
131  assert(CommonParameters::NPE() == 1);
132 
133  std::ifstream field_in(filename.c_str());
134  if (!field_in.is_open()) {
135  vout.crucial("Error: Failed to open the text file. %s\n", filename.c_str());
136  exit(EXIT_FAILURE);
137  }
138 
139  vout.general("Reading Field from %s\n", filename.c_str());
140 
141  int Nin = 0, Nvol = 0, Nex = 0;
142  field_in >> Nin;
143  field_in >> Nvol;
144  field_in >> Nex;
145 
146  f.reset(Nin, Nvol, Nex);
147 
148  double v;
149  for (int j = 0, n = f.ntot(); j < n; ++j) {
150  field_in >> v;
151  f.set(j, v);
152  }
153 
154  field_in.close();
155 
156  vout.general("Reading Field finished.\n");
157  }
158 } // namespace Test_Spectrum_CRSMatrix
BridgeIO vout
Definition: bridgeIO.cpp:503
double norm2() const
Definition: field.cpp:592
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
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
int solver(const std::string &)
static Bridge::VerboseLevel Vlevel()
Container of Field-type object.
Definition: field.h:45
int nvol() const
Definition: field.h:127
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Standard Conjugate Gradient solver algorithm.
Definition: solver_CG.h:38
virtual void set_parameters(const Parameters &params)=0
int nin() const
Definition: field.h:126
int CRSsolver(const string &solution, const string &matrix, const string &source, double &result)
int nex() const
Definition: field.h:128
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
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
void read_text(Field &f, const string &filename)
Bridge::VerboseLevel vl
int ntot() const
Definition: field.h:131
VerboseLevel
Definition: bridgeIO.h:42
virtual void mult_dag(Field &, const Field &)
hermitian conjugate of mult(Field&, const Field&).
Definition: fopr.h:73
virtual void solve(Field &solution, const Field &source, int &Nconv, double &diff)=0
int size() const
Definition: field.h:132