Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fopr_CRS.cpp
Go to the documentation of this file.
1 
14 #include "fopr_CRS.h"
15 
16 #include <fstream>
17 
18 #ifdef USE_FACTORY_AUTOREGISTER
19 namespace {
20  bool init = Fopr_CRS::register_factory();
21 }
22 #endif
23 
24 const std::string Fopr_CRS::class_name = "Fopr_CRS";
25 
26 //====================================================================
28 {
29  const std::string str_vlevel = params.get_string("verbose_level");
30 
31  m_vl = vout.set_verbose_level(str_vlevel);
32 }
33 
34 
35 //====================================================================
36 void Fopr_CRS::DdagD(Field& v, const Field& w)
37 {
38  Field w2(w.nin(), w.nvol(), w.nex());
39 
40  D(w2, w);
41  Ddag(v, w2);
42 }
43 
44 
45 //====================================================================
46 void Fopr_CRS::D(Field& v, const Field& w)
47 {
48  const int Nsize = w.size() / 2;
49 
50  assert(Nsize == m_Nsize);
51 
52  int Ncolumn = 0;
53 
54  // v = 0.0;
55  v.set(0.0);
56 
57  for (int j = 0; j < Nsize; ++j) {
58  if (j == Nsize - 1) {
59  Ncolumn = m_Nnz - m_rowidx_nz[j];
60  } else {
61  Ncolumn = m_rowidx_nz[j + 1] - m_rowidx_nz[j];
62  }
63 
64  double vr = 0.0;
65  double vi = 0.0;
66  for (int i = 0; i < Ncolumn; ++i) {
67  int i2 = i + m_rowidx_nz[j];
68  int k = m_column_nz[i2];
69  double wr = w.cmp(2 * k);
70  double wi = w.cmp(2 * k + 1);
71 
72  vr += m_elem_nz[2 * i2] * wr - m_elem_nz[2 * i2 + 1] * wi;
73  vi += m_elem_nz[2 * i2] * wi + m_elem_nz[2 * i2 + 1] * wr;
74 
75  v.set(2 * j, vr);
76  v.set(2 * j + 1, vi);
77  }
78  }
79 }
80 
81 
82 //====================================================================
83 void Fopr_CRS::Ddag(Field& v, const Field& w)
84 {
85  const int Nsize = w.size() / 2;
86 
87  assert(Nsize == m_Nsize);
88 
89  int Ncolumn = 0;
90 
91  // v = 0.0;
92  v.set(0.0);
93 
94  for (int j = 0; j < Nsize; ++j) {
95  if (j == Nsize - 1) {
96  Ncolumn = m_Nnz - m_rowidx_nz[j];
97  } else {
98  Ncolumn = m_rowidx_nz[j + 1] - m_rowidx_nz[j];
99  }
100 
101  double wr = w.cmp(2 * j);
102  double wi = w.cmp(2 * j + 1);
103 
104  for (int i = 0; i < Ncolumn; ++i) {
105  int i2 = i + m_rowidx_nz[j];
106  int k = m_column_nz[i2];
107  double vr = m_elem_nz[2 * i2] * wr + m_elem_nz[2 * i2 + 1] * wi;
108  double vi = m_elem_nz[2 * i2] * wi - m_elem_nz[2 * i2 + 1] * wr;
109 
110  v.add(2 * k, vr);
111  v.add(2 * k + 1, vi);
112  }
113  }
114 }
115 
116 
117 //====================================================================
119 {
120  // the implementation of this function assumes that the field
121  // is complex valued.
122 
123  assert(CommonParameters::NPE() == 1);
124 
125  m_Nin = m_fopr->field_nin();
126  m_Nvol = m_fopr->field_nvol();
127  m_Nex = m_fopr->field_nex();
128 
129  m_Nsize = (m_Nin / 2) * m_Nvol * m_Nex;
130 
131  Field w(m_Nin, m_Nvol, m_Nex), v(m_Nin, m_Nvol, m_Nex);
132 
133  int Nnz1;
134  std::vector<int> index_nz1(m_Nsize);
135  std::vector<double> elem_nz1(2 * m_Nsize);
136 
137  vout.general(m_vl, "Setting SRC matrix format.\n");
138  vout.general(m_vl, " Nex = %4d\n", m_Nex);
139 
140  //- estimate of matrix size
141  int Nnz_tot = 0;
142 
143  {
144  int in = 0;
145  {
146  int site = 0;
147  for (int ex = 0; ex < m_Nex; ++ex) {
148  w.set(0.0);
149  w.set(in, site, ex, 1.0);
150  m_fopr->mult_dag(v, w);
151  set_matrix_1row(Nnz1, index_nz1, elem_nz1, v);
152  Nnz_tot += Nnz1;
153  vout.general(m_vl, " ex = %4d Nnz1 = %d\n", ex, Nnz1);
154  }
155  }
156  }
157  Nnz_tot *= (m_Nin / 2) * m_Nvol;
158  vout.general(m_vl, " Nnz_tot = %d\n", Nnz_tot);
159 
160  //- resize data array
161  m_Nnz = Nnz_tot;
162  m_rowidx_nz.resize(m_Nsize);
163  m_column_nz.resize(m_Nnz);
164  m_elem_nz.resize(2 * m_Nnz);
165 
166  //- setting CRS matrix
167  Nnz_tot = 0;
168  for (int ex = 0; ex < m_Nex; ++ex) {
169  vout.general(m_vl, " ex = %4d starts.\n", ex);
170  for (int site = 0; site < m_Nvol; ++site) {
171  for (int in2 = 0; in2 < (m_Nin / 2); ++in2) {
172  int j = in2 + (m_Nin / 2) * (site + m_Nvol * ex);
173 
174  w.set(0.0);
175  w.set(2 * in2, site, ex, 1.0);
176  m_fopr->mult_dag(v, w);
177  set_matrix_1row(Nnz1, index_nz1, elem_nz1, v);
178  if (Nnz_tot + Nnz1 > m_Nnz) {
179  vout.crucial(m_vl, "Error at %s: unexpected data size ini set_matrix\n", class_name.c_str());
180  exit(EXIT_FAILURE);
181  }
182 
183  m_rowidx_nz[j] = Nnz_tot;
184  for (int i = 0; i < Nnz1; ++i) {
185  m_column_nz[Nnz_tot + i] = index_nz1[i];
186  m_elem_nz[2 * (Nnz_tot + i)] = elem_nz1[2 * i];
187  m_elem_nz[2 * (Nnz_tot + i) + 1] = elem_nz1[2 * i + 1];
188  }
189  Nnz_tot += Nnz1;
190  }
191  }
192  }
193 
194  m_Nnz = Nnz_tot;
195 
196  vout.general(m_vl, " Nnz(recalc) = %d\n", m_Nnz);
197 }
198 
199 
200 //====================================================================
201 void Fopr_CRS::set_matrix_1row(int& Nnz, std::vector<int>& index_nz,
202  std::vector<double>& elem_nz, const Field& v)
203 {
204  // the implementation of this function assumes that the field
205  // is complex valued.
206 
207  const int Nsize = v.size() / 2;
208 
209  int j = 0;
210 
211  for (int i = 0; i < Nsize; ++i) {
212  if ((v.cmp(2 * i) != 0.0) || (v.cmp(2 * i + 1) != 0.0)) {
213  index_nz[j] = i;
214  elem_nz[2 * j] = v.cmp(2 * i);
215  elem_nz[2 * j + 1] = -v.cmp(2 * i + 1);
216 
217  ++j;
218  }
219  }
220 
221  Nnz = j;
222 }
223 
224 
225 //====================================================================
226 void Fopr_CRS::set_matrix(const std::string fname)
227 {
228  std::fstream config;
229 
230  config.open(fname.c_str(), std::ios::in);
231  if (!config.is_open()) {
232  vout.crucial(m_vl, "Error at %s: Failed to open the next file %s in %s(%d)",
233  class_name.c_str(), fname.c_str(), __FILE__, __LINE__);
234  exit(EXIT_FAILURE);
235  }
236 
237  vout.general(m_vl, "Reading CRS matrix from %s\n", fname.c_str());
238 
239  config >> m_Nin;
240  config >> m_Nvol;
241  config >> m_Nex;
242  config >> m_Nsize;
243  config >> m_Nnz;
244 
245  m_rowidx_nz.resize(m_Nsize);
246  m_column_nz.resize(m_Nnz);
247  m_elem_nz.resize(2 * m_Nnz);
248 
249  for (int j = 0; j < m_Nsize; ++j) {
250  config >> m_rowidx_nz[j];
251  m_rowidx_nz[j] -= 1;
252  }
253 
254  for (int j = 0; j < m_Nnz; ++j) {
255  config >> m_column_nz[j];
256  m_column_nz[j] -= 1;
257  }
258 
259  for (int j = 0; j < m_Nnz; ++j) {
260  config >> m_elem_nz[2 * j];
261  config >> m_elem_nz[2 * j + 1];
262  }
263 
264  config.close();
265 
266  vout.general(m_vl, "Reading CRS matrix finished.\n");
267 }
268 
269 
270 //====================================================================
271 void Fopr_CRS::write_matrix(const std::string fname)
272 {
273  std::fstream config;
274 
275  config.open(fname.c_str(), std::ios::out);
276  if (!config.is_open()) {
277  vout.crucial(m_vl, "Error at %s: Failed to open the text file %s(%d)\n",
278  class_name.c_str(), __FILE__, __LINE__);
279  exit(EXIT_FAILURE);
280  }
281 
282  vout.general(m_vl, "Writing CRS matrix to %s\n", fname.c_str());
283 
284  config << m_Nin << std::endl;
285  config << m_Nvol << std::endl;
286  config << m_Nex << std::endl;
287  config << m_Nsize << std::endl;
288  config << m_Nnz << std::endl;
289 
290  for (int j = 0; j < m_Nsize; ++j) {
291  config << m_rowidx_nz[j] + 1 << std::endl;
292  }
293 
294  for (int j = 0; j < m_Nnz; ++j) {
295  config << m_column_nz[j] + 1 << std::endl;
296  }
297 
298  config.setf(std::ios_base::scientific, std::ios_base::floatfield);
299  config.precision(14);
300 
301  for (int j = 0; j < m_Nnz; ++j) {
302  config << m_elem_nz[2 * j] << std::endl;
303  config << m_elem_nz[2 * j + 1] << std::endl;
304  }
305 
306  config.close();
307 
308  vout.general(m_vl, "Writing CRS matrix finished.\n");
309 }
310 
311 
312 //====================================================================
313 //============================================================END=====
int m_Nin
Definition: fopr_CRS.h:43
int m_Nsize
Definition: fopr_CRS.h:44
BridgeIO vout
Definition: bridgeIO.cpp:503
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
int m_Nvol
Definition: fopr_CRS.h:43
void general(const char *format,...)
Definition: bridgeIO.cpp:197
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
void Ddag(Field &, const Field &)
Definition: fopr_CRS.cpp:83
void set_matrix_1row(int &, std::vector< int > &, std::vector< double > &, const Field &)
Definition: fopr_CRS.cpp:201
Class for parameters.
Definition: parameters.h:46
int m_Nex
Definition: fopr_CRS.h:43
virtual int field_nin()=0
returns the on-site d.o.f. for which the fermion operator is defined.
int nin() const
Definition: field.h:126
int m_Nnz
Definition: fopr_CRS.h:44
Bridge::VerboseLevel m_vl
Definition: fopr.h:127
virtual int field_nex()=0
returns the external d.o.f. for which the fermion operator is defined.
int nex() const
Definition: field.h:128
void set_matrix()
Definition: fopr_CRS.cpp:118
std::vector< int > m_rowidx_nz
Definition: fopr_CRS.h:45
void set_parameters(const Parameters &)
Definition: fopr_CRS.cpp:27
static const std::string class_name
Definition: fopr_CRS.h:40
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
std::vector< double > m_elem_nz
Definition: fopr_CRS.h:47
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:187
void DdagD(Field &, const Field &)
Definition: fopr_CRS.cpp:36
void write_matrix(const std::string)
Definition: fopr_CRS.cpp:271
std::vector< int > m_column_nz
Definition: fopr_CRS.h:46
void D(Field &, const Field &)
Definition: fopr_CRS.cpp:46
virtual void mult_dag(Field &, const Field &)
hermitian conjugate of mult(Field&, const Field&).
Definition: fopr.h:73
string get_string(const string &key) const
Definition: parameters.cpp:221
virtual int field_nvol()=0
returns the volume for which the fermion operator is defined.
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:131
Fopr * m_fopr
Definition: fopr_CRS.h:49
int size() const
Definition: field.h:132