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