Bridge++  Ver. 1.2.x
 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 <valarray>
19 #include <string>
20 #include <assert.h>
21 
22 #include "commonParameters.h"
23 #include "communicator.h"
24 #include "bridge_complex.h"
25 
27 
37 class Field
38 {
39  public:
40  enum element_type { REAL = 1, COMPLEX = 2 };
41 
42  protected:
43  int m_Nin; // internal d.o.f.
44  int m_Nvol; // lattice volume
45  int m_Nex; // external d.o.f.
47  // total degree of freedom is Nin * Nsite * Nex.
48 
49  std::valarray<double> field;
50 
51  inline
52  int myindex(const int jin, const int site, const int jex) const
53  {
54  return jin + m_Nin * (site + m_Nvol * jex);
55  }
56 
58 
59  public:
60 
61  Field() :
63  field(0),
64  m_vl(CommonParameters::Vlevel())
65  {
66  check();
67  }
68 
69  Field(const int Nin, const int Nvol, const int Nex, const element_type cmpl = COMPLEX) :
70  m_Nin(Nin), m_Nvol(Nvol), m_Nex(Nex), m_element_type(cmpl),
71  field(ntot()),
72  m_vl(CommonParameters::Vlevel())
73  {
74  check();
75  }
76 
77  Field clone() const
78  {
80  }
81 
82  void reset(const int Nin, const int Nvol, const int Nex,
83  const element_type cmpl = COMPLEX)
84  {
85  m_Nin = Nin;
86  m_Nvol = Nvol;
87  m_Nex = Nex;
88  m_element_type = cmpl;
89 
90  field.resize(ntot());
91  }
92 
93  // assignment
94  Field& operator=(const double a) { field = a; return *this; }
95 
96  private:
97  void check();
98 
99  public:
100  int nin() const { return m_Nin; }
101  int nvol() const { return m_Nvol; }
102  int nex() const { return m_Nex; }
104 
105  int ntot() const { return m_Nin * m_Nvol * m_Nex; }
106  int size() const { return ntot(); }
107 
108  double cmp(const int jin, const int site, const int jex) const
109  {
110  return field[myindex(jin, site, jex)];
111  }
112 
113  double cmp(const int i) const
114  {
115  return field[i];
116  }
117 
118  double *ptr(const int jin, const int site, const int jex)
119  {
120  return &field[myindex(jin, site, jex)];
121  }
122 
123  double *ptr(const int i)
124  {
125  return &field[i];
126  }
127 
128  void set(const int jin, const int site, const int jex, double v)
129  {
130  field[myindex(jin, site, jex)] = v;
131  }
132 
133  void set(const int i, double v)
134  {
135  field[i] = v;
136  }
137 
138  void set(double a);
139 
140  void add(const int jin, const int site, const int jex, double v)
141  {
142  field[myindex(jin, site, jex)] += v;
143  }
144 
145  void add(const int i, double v)
146  {
147  field[i] += v;
148  }
149 
150  void setpart_ex(int ex, const Field& w, int exw)
151  {
152  assert(ex < nex());
153  assert(exw < w.nex());
154 // for (int site = 0; site < m_Nvol; ++site) {
155 // for (int jin = 0; jin < m_Nin; ++jin) {
156 // field[myindex(jin, site, ex)] = w.field[myindex(jin, site, exw)];
157 // }
158 // }
159  return copy(*this, ex, w, exw);
160  }
161 
162  void addpart_ex(int ex, const Field& w, int exw)
163  {
164  assert(ex < nex());
165  assert(exw < w.nex());
166 // for (int site = 0; site < m_Nvol; ++site) {
167 // for (int jin = 0; jin < m_Nin; ++jin) {
168 // field[myindex(jin, site, ex)] += w.field[myindex(jin, site, exw)];
169 // }
170 // }
171  return axpy(*this, ex, 1.0, w, exw);
172  }
173 
174  void addpart_ex(int ex, const Field& w, int exw, double prf)
175  {
176  assert(ex < nex());
177  assert(exw < w.nex());
178 // for (int site = 0; site < m_Nvol; ++site) {
179 // for (int jin = 0; jin < m_Nin; ++jin) {
180 // field[myindex(jin, site, ex)]
181 // += prf * w.field[myindex(jin, site, exw)];
182 // }
183 // }
184  return axpy(*this, ex, prf, w, exw);
185  }
186 
187  // linear algebra operations
188  // Field& operator-() { field *= -1; return *this; }
189  Field& operator+=(const Field& v) { field += v.field; return *this; }
190  Field& operator-=(const Field& v) { field -= v.field; return *this; }
191  Field& operator*=(const double a) { field *= a; return *this; }
192  Field& operator/=(const double a) { field *= 1.0 / a; return *this; }
193  Field& operator*=(const dcomplex a)
194  {
195  if (imag(a) == 0.0) {
196  field *= real(a);
197  } else {
198  scal(*this, a);
199  }
200  return *this;
201  }
202 
203  // XXX
204  double operator*(const Field& rhs) { return dot(*this, rhs); }
205 
206  // BLAS-like routine
207 
208  double norm2() const;
209 
210  double norm() const { return sqrt(norm2()); }
211 
212  friend
213  double dot(const Field& y, const Field& x);
214 
215  friend
216  double dot(const Field& y, const int exy, const Field& x, const int exx);
217 
218  friend
219  dcomplex dotc(const Field& y, const Field& x);
220 
221  friend
222  dcomplex dotc(const Field& y, const int exy, const Field& x, const int exx);
223 
224  friend
225  void axpy(Field& y, const double a, const Field& x);
226 
227  friend
228  void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
229 
230  friend
231  void axpy(Field& y, const dcomplex a, const Field& x);
232 
233  friend
234  void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
235 
236  friend
237  void scal(Field& x, const double a);
238 
239  friend
240  void scal(Field& x, const int exx, const double a);
241 
242  friend
243  void scal(Field& x, const dcomplex a);
244 
245  friend
246  void scal(Field& x, const int exx, const dcomplex a);
247 
248  friend
249  void copy(Field& y, const Field& x);
250 
251  friend
252  void copy(Field& y, const int exy, const Field& x, const int exx);
253 
254  friend
255  void aypx(const double a, Field& y, const Field& x);
256 
257  friend
258  void aypx(const dcomplex a, Field& y, const Field& x);
259 
260 
266  void stat(double& Fave, double& Fmax, double& Fdev) const;
267 };
268 
269 //----------------------------------------------------------------
270 
271 inline Field operator*(const Field& v, const double s)
272 {
273  Field w(v);
274 
275  w *= s;
276  return w;
277 }
278 
279 
280 inline Field operator*(const double s, const Field& v)
281 {
282  return operator*(v, s);
283 }
284 
285 
286 inline Field operator+(const Field& lhs, const Field& rhs)
287 {
288  Field w(lhs);
289 
290  w += rhs;
291  return w;
292 }
293 
294 
295 inline Field operator-(const Field& lhs, const Field& rhs)
296 {
297  Field w(lhs);
298 
299  w -= rhs;
300  return w;
301 }
302 
303 
304 //----------------------------------------------------------------
305 // BLAS-like routines
306 
309 double dot(const Field& y, const Field& x);
310 
312 double dot(const Field& y, const int exy, const Field& x, const int exx);
313 
316 dcomplex dotc(const Field& y, const Field& x);
317 
319 dcomplex dotc(const Field& y, const int exy, const Field& x, const int nexx);
320 
322 void axpy(Field& y, const double a, const Field& x);
323 
325 void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
326 
329 void axpy(Field& y, const dcomplex a, const Field& x);
330 
332 void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
333 
335 void scal(Field& x, const double a);
336 
338 void scal(Field& x, const int exx, const double a);
339 
342 void scal(Field& x, const dcomplex a);
343 
345 void scal(Field& x, const int exx, const dcomplex a);
346 
348 void copy(Field& y, const Field& x);
349 
351 void copy(Field& y, const int exy, const Field& x, const int exx);
352 
354 void aypx(const double a, Field& y, const Field& x);
355 
358 void aypx(const dcomplex a, Field& y, const Field& x);
359 
360 //----------------------------------------------------------------
361 
362 inline int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
363 {
364  return Communicator::exchange(count, recv_buf->ptr(0), send_buf->ptr(0), idir, ipm, tag);
365 }
366 
367 
368 inline int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
369 {
370  return Communicator::send_1to1(count, recv_buf->ptr(0), send_buf->ptr(0), p_to, p_from, tag);
371 }
372 
373 
374 //----------------------------------------------------------------
375 // utility: report statistics.
376 void report_field_stat(const Bridge::VerboseLevel vl, const std::string& msg, const Field& f);
377 
378 //----------------------------------------------------------------
379 #endif
int m_Nin
Definition: field.h:43
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:310
void report_field_stat(const Bridge::VerboseLevel vl, const std::string &msg, const Field &f)
Definition: field.cpp:580
friend void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:310
int m_Nex
Definition: field.h:45
Field & operator+=(const Field &v)
Definition: field.h:189
double norm2() const
Definition: field.cpp:469
Field operator-(const Field &lhs, const Field &rhs)
Definition: field.h:295
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:128
double dot(const Field &y, const Field &x)
Definition: field.cpp:46
int m_Nvol
Definition: field.h:44
double * ptr(const int i)
Definition: field.h:123
Field & operator*=(const double a)
Definition: field.h:191
double * ptr(const int jin, const int site, const int jex)
Definition: field.h:118
Container of Field-type object.
Definition: field.h:37
Bridge::VerboseLevel m_vl
Definition: field.h:57
friend void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:409
int nvol() const
Definition: field.h:101
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:108
element_type
Definition: field.h:40
friend void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:193
Field & operator/=(const double a)
Definition: field.h:192
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:409
Field()
Definition: field.h:61
void addpart_ex(int ex, const Field &w, int exw)
Definition: field.h:162
Field operator+(const Field &lhs, const Field &rhs)
Definition: field.h:286
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...
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:174
element_type m_element_type
Definition: field.h:46
int nin() const
Definition: field.h:100
void set(const int i, double v)
Definition: field.h:133
friend dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:98
dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:98
Field & operator*=(const dcomplex a)
Definition: field.h:193
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:82
double norm() const
Definition: field.h:210
Field clone() const
Definition: field.h:77
void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:489
Field & operator-=(const Field &v)
Definition: field.h:190
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:102
Common parameter class: provides parameters as singleton.
std::valarray< double > field
Definition: field.h:49
Field operator*(const Field &v, const double s)
Definition: field.h:271
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:193
void add(const int i, double v)
Definition: field.h:145
double operator*(const Field &rhs)
Definition: field.h:204
int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
Definition: field.h:368
friend void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:489
Bridge::VerboseLevel vl
Definition: checker.cpp:18
int ntot() const
Definition: field.h:105
VerboseLevel
Definition: bridgeIO.h:25
Field(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:69
element_type field_element_type() const
Definition: field.h:103
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:140
Field & operator=(const double a)
Definition: field.h:94
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:544
void setpart_ex(int ex, const Field &w, int exw)
Definition: field.h:150
int myindex(const int jin, const int site, const int jex) const
Definition: field.h:52
int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
Definition: field.h:362
double cmp(const int i) const
Definition: field.h:113
int size() const
Definition: field.h:106