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