Bridge++  Ver. 1.2.x
 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 "commonParameters.h"
15 #include "communicator.h"
16 #include "bridgeIO.h"
17 using Bridge::vout;
18 
19 #include "field.h"
20 #include "fopr_CRS.h"
21 #include "solver_CG.h"
22 
23 #include "test.h"
24 
25 // // clover set
26 // string solution = "solution4x8_clover.crs";
27 // string matrix = "matrix4x8_clover.crs";
28 // string source = "source4x8_clover.crs";
29 
30 // // 5d-overlap set
31 // string solution = "solution4x4_ov5d.crs";
32 // string matrix = "matrix4x4_ov5d.crs";
33 // string source = "source4x4_ov5d.crs";
34 
35 // // domain-wall set
36 // string solution = "solution4x4_dw.crs";
37 // string matrix = "matrix4x4_dw.crs";
38 // string source = "source4x4_dw.crs";
39 
40 
41 namespace Test_Spectrum_CRSMatrix
42 {
43  void write_text(const Field& f, const string& filename);
44  void read_text(Field& f, const string& filename);
45 
46  int CRSsolver(
47  const string& solution,
48  const string& matrix,
49  const string& source,
50  double& result /* return value */
51  )
52  {
54 
55  // ##### Following part is common #####
56 
57  // read source and solution vectors
58  Field b, xq;
59 
60 // b.read_text(source);
61 // xq.read_text(solution);
62  read_text(b, source);
63  read_text(xq, solution);
64 
65  // read CRS matrix
66  Fopr_CRS *fopr = new Fopr_CRS(matrix);
67 
68 
69  // setup of CG solver
70  int Niter = 2000;
71  double Stop_cond = 1.0e-28;
72  Solver *solver = new Solver_CG((Fopr *)fopr);
73  solver->set_parameters(Niter, Stop_cond);
74 
75  Field b2(b);
76  Field x(b);
77 
78  // setup of CGNE source
79  fopr->Ddag(b2, b);
80 
81  // CGNE solver
82  int Nconv;
83  double diff;
84  fopr->set_mode("DdagD");
85  solver->solve(x, b2, Nconv, diff);
86  vout.general(vl, "solver(CG): Nconv = %4d diff = %12.6e\n", Nconv, diff);
87 
88  // check
89  x -= xq;
90  double xx = x.norm2();
91  vout.general(vl, "standard norm2 = %.8e\n", xx);
92 
93  result = xx;
94 
95  delete solver;
96  delete fopr;
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("Failed to open the text file. %s\n", filename.c_str());
111  abort();
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("Failed to open the text file. %s\n", filename.c_str());
142  abort();
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
BridgeIO vout
Definition: bridgeIO.cpp:207
double norm2() const
Definition: field.cpp:469
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:128
void general(const char *format,...)
Definition: bridgeIO.cpp:38
static Bridge::VerboseLevel Vlevel()
Container of Field-type object.
Definition: field.h:37
int nvol() const
Definition: field.h:101
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:108
void Ddag(Field &, const Field &)
Definition: fopr_CRS.cpp:76
void set_mode(std::string mode)
setting the mode of multiplication if necessary. Default implementation here is just to avoid irrelev...
Definition: fopr_CRS.h:80
Standard Conjugate Gradient solver algorithm.
Definition: solver_CG.h:41
virtual void set_parameters(const Parameters &params)=0
int nin() const
Definition: field.h:100
int CRSsolver(const string &solution, const string &matrix, const string &source, double &result)
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:82
int nex() const
Definition: field.h:102
void write_text(const Field &f, const string &filename)
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
void read_text(Field &f, const string &filename)
Base class for linear solver class family.
Definition: solver.h:37
Bridge::VerboseLevel vl
Definition: checker.cpp:18
int ntot() const
Definition: field.h:105
VerboseLevel
Definition: bridgeIO.h:25
Base class of fermion operator family.
Definition: fopr.h:39
Fermion operator with CRS matrix format.
Definition: fopr_CRS.h:38
virtual void solve(Field &solution, const Field &source, int &Nconv, double &diff)=0
static int NPE()
int size() const
Definition: field.h:106