Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
field.h
Go to the documentation of this file.
1 
15 #ifndef FIELD_INCLUDED
16 #define FIELD_INCLUDED
17 
18 #include <iostream>
19 #include <valarray>
20 #include <string>
21 #include <assert.h>
22 
25 
26 #include "bridge_complex.h"
27 #include "bridge_defs.h"
28 
30 
45 class Field
46 {
47  public:
48  // enum element_type { REAL = 1, COMPLEX = 2 };
50  typedef double real_t; // [23 Apr 2018 H.M. added]
51 
52  protected:
53  int m_Nin; // internal d.o.f.
54  int m_Nvol; // lattice volume
55  int m_Nex; // external d.o.f.
56  // Element_type::type m_element_type;
58  // total degree of freedom is Nin * Nsite * Nex.
59 
60  std::valarray<double> field;
61 
62  // as valarray takes size_t, we do not use long_t here
63  inline
64  size_t myindex(const int jin, const int site, const int jex) const
65  {
66  return jin + m_Nin * ((size_t)site + m_Nvol * jex);
67  }
68 
70 
71  public:
72 
73  Field() :
74  m_Nin(0), m_Nvol(0), m_Nex(0), m_element_type(Element_type::COMPLEX),
75  field(0),
76  m_vl(CommonParameters::Vlevel())
77  {
78  check();
79  }
80 
81  Field(const int Nin, const int Nvol, const int Nex,
82  const element_type cmpl = Element_type::COMPLEX) :
83  m_Nin(Nin), m_Nvol(Nvol), m_Nex(Nex), m_element_type(cmpl),
84  field(ntot()),
85  m_vl(CommonParameters::Vlevel())
86  {
87  check();
88  }
89 
90  Field clone() const
91  {
93  }
94 
95  void reset(const int Nin, const int Nvol, const int Nex,
97  {
98  if ((m_Nin == Nin) &&
99  (m_Nvol == Nvol) &&
100  (m_Nex == Nex) &&
101  (m_element_type == cmpl)) return;
102 
103  m_Nin = Nin;
104  m_Nvol = Nvol;
105  m_Nex = Nex;
106  m_element_type = cmpl;
107 
108  field.resize(ntot());
109  }
110 
111  // assignment
112  //Field& operator=(const double a) { field = a; return *this; }
113  Field& operator=(const Field& v)
114  {
115  assert(m_Nin == v.nin());
116  assert(m_Nvol == v.nvol());
117  assert(m_Nex == v.nex());
118  copy(*this, v);
119  return *this;
120  }
121 
122  private:
123  void check();
124 
125  public:
126  int nin() const { return m_Nin; }
127  int nvol() const { return m_Nvol; }
128  int nex() const { return m_Nex; }
130 
131  int ntot() const { return m_Nin * m_Nvol * m_Nex; }
132  int size() const { return ntot(); }
133 
135  bool check_size(const int nin, const int nvol, const int nex) const
136  {
137  bool chk = true;
138 
139  if ((m_Nin != nin) || (m_Nvol != nvol) || (m_Nex != nex)) chk = false;
140  return chk;
141  }
142 
143  double cmp(const int jin, const int site, const int jex) const
144  {
145  return field[myindex(jin, site, jex)];
146  }
147 
148  double cmp(const int i) const
149  {
150  return field[i];
151  }
152 
153  const double *ptr(const int jin, const int site, const int jex) const
154  {
155  // when using c++03, const_cast is necessary.
156  return &(const_cast<std::valarray<double>&>(field)[myindex(jin, site, jex)]);
157  }
158 
159  double *ptr(const int jin, const int site, const int jex)
160  {
161  return &field[myindex(jin, site, jex)];
162  }
163 
164  const double *ptr(const int i) const
165  {
166  // when using c++03, const_cast is necessary.
167  return &(const_cast<std::valarray<double>&>(field)[i]);
168  }
169 
170  double *ptr(const int i)
171  {
172  return &field[i];
173  }
174 
175  void set(const int jin, const int site, const int jex, double v)
176  {
177  field[myindex(jin, site, jex)] = v;
178  }
179 
180  void set(const int i, double v)
181  {
182  field[i] = v;
183  }
184 
185  void set(double a);
186 
187  void add(const int jin, const int site, const int jex, double v)
188  {
189  field[myindex(jin, site, jex)] += v;
190  }
191 
192  void add(const int i, double v)
193  {
194  field[i] += v;
195  }
196 
197  void setpart_ex(int ex, const Field& w, int exw)
198  {
199  assert(ex < nex());
200  assert(exw < w.nex());
201  return copy(*this, ex, w, exw);
202  }
203 
204  void addpart_ex(int ex, const Field& w, int exw)
205  {
206  assert(ex < nex());
207  assert(exw < w.nex());
208  return axpy(*this, ex, 1.0, w, exw);
209  }
210 
211  void addpart_ex(int ex, const Field& w, int exw, double prf)
212  {
213  assert(ex < nex());
214  assert(exw < w.nex());
215  return axpy(*this, ex, prf, w, exw);
216  }
217 
218  // BLAS-like routine (friend declaration)
219 
220  double norm2() const;
221 
222  double norm() const { return sqrt(norm2()); }
223 
224  friend
225  double dot(const Field& y, const Field& x);
226 
227  friend
228  double dot(const Field& y, const int exy, const Field& x, const int exx);
229 
231  friend
232  void dot_and_norm2(double &yx, double &y2, double &x2, const Field& y, const Field& x);
233 
234  friend
235  void dot_and_norm2(double &yx, double &y2, double &x2, const Field& y, const int exy, const Field& x, const int exx);
236 
237  friend
238  dcomplex dotc(const Field& y, const Field& x);
239 
240  friend
241  dcomplex dotc(const Field& y, const int exy, const Field& x, const int exx);
242 
244  friend
245  void dotc_and_norm2(double &yx, double &y2, double &x2, const Field& y, const Field& x);
246 
247  friend
248  void dotc_and_norm2(double &yx, double &y2, double &x2, const Field& y, const int exy, const Field& x, const int exx);
249 
250  friend
251  void axpy(Field& y, const double a, const Field& x);
252 
253  friend
254  void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
255 
256  friend
257  void axpy(Field& y, const dcomplex a, const Field& x);
258 
259  friend
260  void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
261 
262  friend
263  void scal(Field& x, const double a);
264 
265  friend
266  void scal(Field& x, const int exx, const double a);
267 
268  friend
269  void scal(Field& x, const dcomplex a);
270 
271  friend
272  void scal(Field& x, const int exx, const dcomplex a);
273 
274  friend
275  void copy(Field& y, const Field& x);
276 
277  friend
278  void copy(Field& y, const int exy, const Field& x, const int exx);
279 
280  friend
281  void aypx(const double a, Field& y, const Field& x);
282 
283  friend
284  void aypx(const dcomplex a, Field& y, const Field& x);
285 
286 
292  void stat(double& Fave, double& Fmax, double& Fdev) const;
293 };
294 
295 //----------------------------------------------------------------
296 // BLAS-like routines defined outside the Field class.
297 
300 double dot(const Field& y, const Field& x);
301 
303 double dot(const Field& y, const int exy, const Field& x, const int exx);
304 
307 dcomplex dotc(const Field& y, const Field& x);
308 
310 dcomplex dotc(const Field& y, const int exy, const Field& x, const int nexx);
311 
313 void axpy(Field& y, const double a, const Field& x);
314 
316 void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
317 
320 void axpy(Field& y, const dcomplex a, const Field& x);
321 
323 void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
324 
326 void scal(Field& x, const double a);
327 
329 void scal(Field& x, const int exx, const double a);
330 
333 void scal(Field& x, const dcomplex a);
334 
336 void scal(Field& x, const int exx, const dcomplex a);
337 
339 void copy(Field& y, const Field& x);
340 
342 void copy(Field& y, const int exy, const Field& x, const int exx);
343 
345 void aypx(const double a, Field& y, const Field& x);
346 
349 void aypx(const dcomplex a, Field& y, const Field& x);
350 
351 //----------------------------------------------------------------
352 
353 inline int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
354 {
355  return Communicator::exchange(count, recv_buf->ptr(0), send_buf->ptr(0), idir, ipm, tag);
356 }
357 
358 
359 inline int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
360 {
361  return Communicator::send_1to1(count, recv_buf->ptr(0), send_buf->ptr(0), p_to, p_from, tag);
362 }
363 
364 
365 //----------------------------------------------------------------
366 // utility: report statistics.
367 void report_field_stat(const Bridge::VerboseLevel vl, const std::string& msg, const Field& f);
368 
369 //----------------------------------------------------------------
370 #endif
int m_Nin
Definition: field.h:53
void report_field_stat(const Bridge::VerboseLevel vl, const std::string &msg, const Field &f)
Definition: field.cpp:703
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:433
friend void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:433
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:319
int m_Nex
Definition: field.h:55
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:153
double norm2() const
Definition: field.cpp:592
size_t myindex(const int jin, const int site, const int jex) const
Definition: field.h:64
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
const double * ptr(const int i) const
Definition: field.h:164
int m_Nvol
Definition: field.h:54
double * ptr(const int i)
Definition: field.h:170
double * ptr(const int jin, const int site, const int jex)
Definition: field.h:159
Container of Field-type object.
Definition: field.h:45
Bridge::VerboseLevel m_vl
Definition: field.h:69
friend void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:532
int nvol() const
Definition: field.h:127
friend void dot_and_norm2(double &yx, double &y2, double &x2, const Field &y, const Field &x)
calculate <y|x>, <y|y> and <x|x> simultaneously
Definition: field.cpp:123
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
friend void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:319
Field & operator=(const Field &v)
Definition: field.h:113
bool check_size(const int nin, const int nvol, const int nex) const
checking size parameters. [23 May 2016 H.Matsufuru]
Definition: field.h:135
Field()
Definition: field.h:73
double real_t
Definition: field.h:50
void addpart_ex(int ex, const Field &w, int exw)
Definition: field.h:204
static int send_1to1(int count, double *recv_buf, double *send_buf, int p_to, int p_from, int tag)
send array of double from rank p_from to rank p_to. communication distinguished by tag...
Element_type::type element_type
Definition: field.h:49
friend double dot(const Field &y, const Field &x)
Definition: field.cpp:46
void addpart_ex(int ex, const Field &w, int exw, double prf)
Definition: field.h:211
element_type m_element_type
Definition: field.h:57
int nin() const
Definition: field.h:126
void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:612
void set(const int i, double v)
Definition: field.h:180
friend dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:155
Field(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:81
double norm() const
Definition: field.h:222
Field clone() const
Definition: field.h:90
dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:155
static int exchange(int count, double *recv_buf, double *send_buf, int idir, int ipm, int tag)
receive array of double from upstream specified by idir and ipm, and send array to downstream...
void check()
Definition: field.cpp:39
int nex() const
Definition: field.h:128
Common parameter class: provides parameters as singleton.
double dot(const Field &y, const Field &x)
Definition: field.cpp:46
std::valarray< double > field
Definition: field.h:60
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
void add(const int i, double v)
Definition: field.h:192
int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
Definition: field.h:359
friend void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:612
Bridge::VerboseLevel vl
int ntot() const
Definition: field.h:131
VerboseLevel
Definition: bridgeIO.h:42
element_type field_element_type() const
Definition: field.h:129
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:187
void stat(double &Fave, double &Fmax, double &Fdev) const
determines the statistics of the field. average, maximum value, and deviation is determined over glob...
Definition: field.cpp:667
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:532
void setpart_ex(int ex, const Field &w, int exw)
Definition: field.h:197
friend void dotc_and_norm2(double &yx, double &y2, double &x2, const Field &y, const Field &x)
calculate <y|x>, <y|y> and <x|x> simultaneously
int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
Definition: field.h:353
double cmp(const int i) const
Definition: field.h:148
int size() const
Definition: field.h:132