Bridge++  Ver. 2.0.2
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 
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  static const std::string class_name;
53 
54  protected:
55  int m_Nin;
56  int m_Nvol;
57  int m_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() :
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 Field& v)
113  {
114  assert(check_size(v.nin(), v.nvol(), v.nex()));
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 setc(double a);
188 
189  void setc(dcomplex a);
190 
191  void add(const int jin, const int site, const int jex, double v)
192  {
193  field[myindex(jin, site, jex)] += v;
194  }
195 
196  void add(const int i, double v)
197  {
198  field[i] += v;
199  }
200 
201  void setpart_ex(int ex, const Field& w, int exw)
202  {
203  assert(ex < nex());
204  assert(exw < w.nex());
205  return copy(*this, ex, w, exw);
206  }
207 
208  void addpart_ex(int ex, const Field& w, int exw)
209  {
210  assert(ex < nex());
211  assert(exw < w.nex());
212  return axpy(*this, ex, 1.0, w, exw);
213  }
214 
215  void addpart_ex(int ex, const Field& w, int exw, double prf)
216  {
217  assert(ex < nex());
218  assert(exw < w.nex());
219  return axpy(*this, ex, prf, w, exw);
220  }
221 
222  // BLAS-like routine (friend declaration)
223 
224  double norm2() const;
225 
226  double norm() const { return sqrt(norm2()); }
227 
228  void xI();
229 
230  friend
231  double dot(const Field& y, const Field& x);
232 
233  friend
234  double dot(const Field& y, const int exy, const Field& x, const int exx);
235 
237  friend
238  void dot_and_norm2(double& yx, double& y2, double& x2, const Field& y, const Field& x);
239 
240  friend
241  void dot_and_norm2(double& yx, double& y2, double& x2, const Field& y, const int exy, const Field& x, const int exx);
242 
243  friend
244  dcomplex dotc(const Field& y, const Field& x);
245 
246  friend
247  dcomplex dotc(const Field& y, const int exy, const Field& x, const int exx);
248 
250  friend
251  void dotc_and_norm2(double& yx, double& y2, double& x2, const Field& y, const Field& x);
252 
253  friend
254  void dotc_and_norm2(double& yx, double& y2, double& x2, const Field& y, const int exy, const Field& x, const int exx);
255 
256  friend
257  void axpy(Field& y, const double a, const Field& x);
258 
259  friend
260  void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
261 
262  friend
263  void axpy(Field& y, const dcomplex a, const Field& x);
264 
265  friend
266  void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
267 
268  friend
269  void scal(Field& x, const double a);
270 
271  friend
272  void scal(Field& x, const int exx, const double a);
273 
274  friend
275  void scal(Field& x, const dcomplex a);
276 
277  friend
278  void scal(Field& x, const int exx, const dcomplex a);
279 
280  friend
281  void copy(Field& y, const Field& x);
282 
283  friend
284  void copy(Field& y, const int exy, const Field& x, const int exx);
285 
286  friend
287  void aypx(const double a, Field& y, const Field& x);
288 
289  friend
290  void aypx(const dcomplex a, Field& y, const Field& x);
291 
292 
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 #endif
Element_type
Definition: bridge_defs.h:39
COMPLEX
#define COMPLEX
Definition: eigensolver_IRArnoldi.cpp:21
Field::myindex
size_t myindex(const int jin, const int site, const int jex) const
Definition: field.h:64
dotc
dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:712
Field::setpart_ex
void setpart_ex(int ex, const Field &w, int exw)
Definition: field.h:201
Field::cmp
double cmp(const int i) const
Definition: field.h:148
Field::m_vl
Bridge::VerboseLevel m_vl
Definition: field.h:69
copy
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:212
Field::dot_and_norm2
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:674
CommonParameters
Common parameter class: provides parameters as singleton.
Definition: commonParameters.h:42
Field::m_Nex
int m_Nex
external d.o.f.
Definition: field.h:57
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
Field::ptr
double * ptr(const int jin, const int site, const int jex)
Definition: field.h:159
Field::clone
Field clone() const
Definition: field.h:90
Field::ntot
int ntot() const
Definition: field.h:131
aypx
void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:509
Field::scal
friend void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:261
Field::nex
int nex() const
Definition: field.h:128
Field::operator=
Field & operator=(const Field &v)
Definition: field.h:112
Field::element_type
Element_type::type element_type
Definition: field.h:50
scal
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:261
Field::dot
friend double dot(const Field &y, const Field &x)
Definition: field.cpp:576
Field::check_size
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::copy
friend void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:212
Field::stat
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:169
Field::Field
Field()
Definition: field.h:73
Field::set
void set(const int i, double v)
Definition: field.h:180
Field::xI
void xI()
Definition: field.cpp:138
Field::nin
int nin() const
Definition: field.h:126
Field::m_element_type
element_type m_element_type
field complex type
Definition: field.h:58
axpy
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:380
Field::real_t
double real_t
Definition: field.h:51
Field::norm2
double norm2() const
Definition: field.cpp:113
Field::axpy
friend void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:380
Field::addpart_ex
void addpart_ex(int ex, const Field &w, int exw, double prf)
Definition: field.h:215
bridge_complex.h
Field::dotc
friend dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:712
Field::addpart_ex
void addpart_ex(int ex, const Field &w, int exw)
Definition: field.h:208
Field::check
void check()
Definition: field.cpp:28
Field::size
int size() const
Definition: field.h:132
Field::norm
double norm() const
Definition: field.h:226
Field::Field
Field(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:81
Field::class_name
static const std::string class_name
Definition: field.h:52
Field::add
void add(const int i, double v)
Definition: field.h:196
Field::aypx
friend void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:509
Field::nvol
int nvol() const
Definition: field.h:127
Field::reset
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
Field::field_element_type
element_type field_element_type() const
Definition: field.h:129
Field::add
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:191
Field::setc
void setc(double a)
Definition: field.cpp:55
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Field::ptr
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:153
Element_type::COMPLEX
@ COMPLEX
Definition: bridge_defs.h:43
Field::field
std::valarray< double > field
Definition: field.h:60
Field::dotc_and_norm2
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
Field::m_Nin
int m_Nin
internal d.o.f.
Definition: field.h:55
commonParameters.h
dot
double dot(const Field &y, const Field &x)
Definition: field.cpp:576
Field
Container of Field-type object.
Definition: field.h:46
communicator.h
Bridge::VerboseLevel
VerboseLevel
Definition: bridgeIO.h:42
Field::ptr
const double * ptr(const int i) const
Definition: field.h:164
bridge_defs.h
Element_type::type
type
Definition: bridge_defs.h:41
Field::m_Nvol
int m_Nvol
lattice volume
Definition: field.h:56
Field::ptr
double * ptr(const int i)
Definition: field.h:170