Bridge++  Ver. 2.0.2
source_Random.cpp
Go to the documentation of this file.
1 
14 #include "source_Random.h"
15 
16 #ifdef USE_FACTORY_AUTOREGISTER
17 namespace {
18  bool init = Source_Random::register_factory();
19 }
20 #endif
21 
22 const std::string Source_Random::class_name = "Source_Random";
23 
24 //====================================================================
26 {
27  std::string vlevel;
28  if (!params.fetch_string("verbose_level", vlevel)) {
29  m_vl = vout.set_verbose_level(vlevel);
30  }
31 
32  //- fetch and check input parameters
33  std::vector<int> source_position;
34  std::vector<int> source_momentum;
35  string str_noise_type;
36 
37  int err = 0;
38  err += params.fetch_int_vector("source_position", source_position);
39  err += params.fetch_int_vector("source_momentum", source_momentum);
40  err += params.fetch_string("noise_type", str_noise_type);
41 
42  if (err) {
43  vout.crucial(m_vl, "Error at %s: input parameter not found.\n", class_name.c_str());
44  exit(EXIT_FAILURE);
45  }
46 
47 
48  set_parameters(source_position, source_momentum, str_noise_type);
49 }
50 
51 
52 //====================================================================
54 {
55  params.set_int_vector("source_position", m_source_position);
56  params.set_int_vector("source_momentum", m_source_momentum);
57  params.set_string("noise_type", m_str_noise_type);
58 
59  params.set_string("verbose_level", vout.get_verbose_level(m_vl));
60 }
61 
62 
63 //====================================================================
64 void Source_Random::set_parameters(const std::vector<int>& source_position,
65  const std::vector<int>& source_momentum,
66  const string str_noise_type)
67 {
68  // #### parameter setup ####
69  const int Ndim = CommonParameters::Ndim();
70  const int t_dir = Ndim - 1;
71 
72  //- global lattice size
73  std::vector<int> Lsize(Ndim);
74 
75  Lsize[0] = CommonParameters::Lx();
76  Lsize[1] = CommonParameters::Ly();
77  Lsize[2] = CommonParameters::Lz();
78  Lsize[3] = CommonParameters::Lt();
79 
80  //- local size
81  const int Nt = CommonParameters::Nt();
82 
83  //- print input parameters
84  vout.general(m_vl, "Source for spinor field - Random:\n");
85  for (int mu = 0; mu < Ndim; ++mu) {
86  vout.general(m_vl, " source_position[%d] = %d\n",
87  mu, source_position[mu]);
88  }
89 
90  for (int mu = 0; mu < Ndim - 1; ++mu) {
91  vout.general(m_vl, " source_momentum[%d] = %d\n",
92  mu, source_momentum[mu]);
93  }
94 
95  vout.general(m_vl, " noise_type = %s\n", str_noise_type.c_str());
96 
97 
98  //- range check
99  int err = 0;
100  for (int mu = 0; mu < Ndim; ++mu) {
101  // NB. Lsize[mu] > abs(source_position[mu])
102  err += ParameterCheck::non_negative(Lsize[mu] - abs(source_position[mu]));
103  }
104 
105  if (err) {
106  vout.crucial(m_vl, "Error at %s: parameter range check failed.\n", class_name.c_str());
107  exit(EXIT_FAILURE);
108  }
109 
110 
111  //- store values
112  m_source_position.resize(Ndim);
113  for (int mu = 0; mu < Ndim; ++mu) {
114  m_source_position[mu] = (source_position[mu] + Lsize[mu]) % Lsize[mu];
115  }
116  m_source_momentum = source_momentum;
117  m_str_noise_type = str_noise_type;
118 
119 
120  //-- post-process
121  // m_rand->reset(static_cast<unsigned long>(i_seed_noise));
122 
123  //- PE location in t-direction.
124  const int tpe = m_source_position[t_dir] / Nt;
125 
126  m_in_node = false;
127 
128  if (tpe == Communicator::ipe(t_dir)) {
129  m_in_node = true;
130  }
131 }
132 
133 
134 //====================================================================
135 void Source_Random::set(Field& src, const int idx)
136 {
137  const int Ndim = CommonParameters::Ndim();
138 
139  //- local size
140  std::vector<int> Nsize(Ndim);
141 
142  Nsize[0] = CommonParameters::Nx();
143  Nsize[1] = CommonParameters::Ny();
144  Nsize[2] = CommonParameters::Nz();
145  Nsize[3] = CommonParameters::Nt();
146 
147  Field v(src);
148 
149  if (m_str_noise_type == "Gaussian") {
151  } else if (m_str_noise_type == "U1") {
152  m_rand->U1_lex_global(v);
153  } else if (m_str_noise_type == "Z2") {
154  m_rand->Z2_lex_global(v);
155  } else {
156  vout.crucial(m_vl, "Error at %s: Noise type of %s is not supported.\n", class_name.c_str(), m_str_noise_type.c_str());
157  exit(EXIT_FAILURE);
158  }
159 
160  //- clear field
161  src.set(0.0);
162 
163  const int idx_r = 2 * idx;
164  const int idx_i = 2 * idx + 1;
165 
166  if (m_in_node) {
167  int t = m_source_position[3] % Nsize[3];
168 
169  for (int z = 0; z < Nsize[2]; ++z) {
170  for (int y = 0; y < Nsize[1]; ++y) {
171  for (int x = 0; x < Nsize[0]; ++x) {
172  int site = m_index.site(x, y, z, t);
173 
174  double rn_r = v.cmp(idx_r, site, 0);
175  double rn_i = v.cmp(idx_i, site, 0);
176 
177  //XXX field layout: complex as two doubles
178  src.set(idx_r, site, 0, rn_r);
179  src.set(idx_i, site, 0, rn_i);
180 
181  //- for debug
182  vout.paranoiac(m_vl, "%s: src(x,y,z) = %e %e (%d %d %d)\n",
183  class_name.c_str(), rn_r, rn_i, x, y, z);
184  }
185  }
186  }
187  }
188 
189 #ifdef DEBUG
190  //- global lattice size
191  const int Lx = CommonParameters::Lx();
192  const int Ly = CommonParameters::Ly();
193  const int Lz = CommonParameters::Lz();
194  const int Lvol3 = Lx * Ly * Lz;
195 
196  const int Nvol = CommonParameters::Nvol();
197 
198  double src1_r = 0.0;
199  double src1_i = 0.0;
200  for (int site = 0; site < Nvol; ++site) {
201  src1_r += src.cmp(idx_r, site, 0);
202  src1_i += src.cmp(idx_i, site, 0);
203  }
204  const double src_r = Communicator::reduce_sum(src1_r) / Lvol3;
205  const double src_i = Communicator::reduce_sum(src1_i) / Lvol3;
206  vout.general(m_vl, "%s: sum_x src = %e %e\n", class_name.c_str(), src_r, src_i);
207 
208  const double src2 = src.norm2() / Lvol3;
209  vout.general(m_vl, "%s: src.norm2 = %e\n", class_name.c_str(), src2);
210 #endif
211 }
212 
213 
214 //====================================================================
215 void Source_Random::set(Field& src, const int i_color, const int i_spin)
216 {
217  const int Nc = CommonParameters::Nc();
218  const int idx = i_color + Nc * i_spin;
219 
220  set(src, idx);
221 }
222 
223 
224 //====================================================================
225 void Source_Random::set_all_color(Field& src, const int i_spin)
226 {
227  const int Nc = CommonParameters::Nc();
228  const int Ndim = CommonParameters::Ndim();
229 
230  //- local size
231  std::vector<int> Nsize(Ndim);
232 
233  Nsize[0] = CommonParameters::Nx();
234  Nsize[1] = CommonParameters::Ny();
235  Nsize[2] = CommonParameters::Nz();
236  Nsize[3] = CommonParameters::Nt();
237 
238  Field v(src);
239 
240  if (m_str_noise_type == "Gaussian") {
242  } else if (m_str_noise_type == "U1") {
243  m_rand->U1_lex_global(v);
244  } else if (m_str_noise_type == "Z2") {
245  m_rand->Z2_lex_global(v);
246  } else {
247  vout.crucial(m_vl, "Error at %s: Noise type of %s is not supported.\n", class_name.c_str(), m_str_noise_type.c_str());
248  exit(EXIT_FAILURE);
249  }
250 
251  //- clear field
252  src.set(0.0);
253 
254  if (m_in_node) {
255  int t = m_source_position[3] % Nsize[3];
256 
257  for (int z = 0; z < Nsize[2]; ++z) {
258  for (int y = 0; y < Nsize[1]; ++y) {
259  for (int x = 0; x < Nsize[0]; ++x) {
260  int site = m_index.site(x, y, z, t);
261 
262  for (int i_color = 0; i_color < Nc; ++i_color) {
263  int idx = i_color + Nc * i_spin;
264  int idx_r = 2 * idx;
265  int idx_i = 2 * idx + 1;
266 
267  double rn_r = v.cmp(idx_r, site, 0);
268  double rn_i = v.cmp(idx_i, site, 0);
269 
270  //XXX field layout: complex as two doubles
271  src.set(idx_r, site, 0, rn_r);
272  src.set(idx_i, site, 0, rn_i);
273 
274  //- for debug
275  vout.paranoiac(m_vl, "%s: src(idx, x,y,z) = %e %e (%d %d %d %d)\n",
276  class_name.c_str(), rn_r, rn_i, idx, x, y, z);
277  }
278  }
279  }
280  }
281  }
282 
283 #ifdef DEBUG
284  //- global lattice size
285  const int Lx = CommonParameters::Lx();
286  const int Ly = CommonParameters::Ly();
287  const int Lz = CommonParameters::Lz();
288  const int Lvol3 = Lx * Ly * Lz;
289 
290  const int Nvol = CommonParameters::Nvol();
291 
292  const int Nd = CommonParameters::Nd();
293 
294  double src1_r = 0.0;
295  double src1_i = 0.0;
296  for (int site = 0; site < Nvol; ++site) {
297  for (int idx = 0; idx < Nc * Nd; ++idx) {
298  int idx_r = 2 * idx;
299  int idx_i = 2 * idx + 1;
300 
301  src1_r += src.cmp(idx_r, site, 0);
302  src1_i += src.cmp(idx_i, site, 0);
303  }
304  }
305  const double src_r = Communicator::reduce_sum(src1_r) / (Lvol3 * Nc * Nd);
306  const double src_i = Communicator::reduce_sum(src1_i) / (Lvol3 * Nc * Nd);
307  vout.general(m_vl, "%s: sum_x src = %e %e\n", class_name.c_str(), src_r, src_i);
308 
309  const double src2 = src.norm2() / (Lvol3 * Nc * Nd);
310  vout.general(m_vl, "%s: src.norm2 = %e\n", class_name.c_str(), src2);
311 #endif
312 }
313 
314 
315 //====================================================================
317 {
318  const int Nc = CommonParameters::Nc();
319  const int Nd = CommonParameters::Nd();
320  const int Ndim = CommonParameters::Ndim();
321 
322  //- local size
323  std::vector<int> Nsize(Ndim);
324 
325  Nsize[0] = CommonParameters::Nx();
326  Nsize[1] = CommonParameters::Ny();
327  Nsize[2] = CommonParameters::Nz();
328  Nsize[3] = CommonParameters::Nt();
329 
330  Field v(src);
331 
332  if (m_str_noise_type == "Gaussian") {
334  } else if (m_str_noise_type == "U1") {
335  m_rand->U1_lex_global(v);
336  } else if (m_str_noise_type == "Z2") {
337  m_rand->Z2_lex_global(v);
338  } else {
339  vout.crucial(m_vl, "Error at %s: Noise type of %s is not supported.\n", class_name.c_str(), m_str_noise_type.c_str());
340  exit(EXIT_FAILURE);
341  }
342 
343  //- clear field
344  src.set(0.0);
345 
346  if (m_in_node) {
347  int t = m_source_position[3] % Nsize[3];
348 
349  for (int z = 0; z < Nsize[2]; ++z) {
350  for (int y = 0; y < Nsize[1]; ++y) {
351  for (int x = 0; x < Nsize[0]; ++x) {
352  int site = m_index.site(x, y, z, t);
353 
354  for (int idx = 0; idx < Nc * Nd; ++idx) {
355  int idx_r = 2 * idx;
356  int idx_i = 2 * idx + 1;
357 
358  double rn_r = v.cmp(idx_r, site, 0);
359  double rn_i = v.cmp(idx_i, site, 0);
360 
361  //XXX field layout: complex as two doubles
362  src.set(idx_r, site, 0, rn_r);
363  src.set(idx_i, site, 0, rn_i);
364 
365  //- for debug
366  vout.paranoiac(m_vl, "%s: src(idx, x,y,z) = %e %e (%d %d %d %d)\n",
367  class_name.c_str(), rn_r, rn_i, idx, x, y, z);
368  }
369  }
370  }
371  }
372  }
373 
374 #ifdef DEBUG
375  //- global lattice size
376  const int Lx = CommonParameters::Lx();
377  const int Ly = CommonParameters::Ly();
378  const int Lz = CommonParameters::Lz();
379  const int Lvol3 = Lx * Ly * Lz;
380 
381  const int Nvol = CommonParameters::Nvol();
382 
383  double src1_r = 0.0;
384  double src1_i = 0.0;
385  for (int site = 0; site < Nvol; ++site) {
386  for (int idx = 0; idx < Nc * Nd; ++idx) {
387  int idx_r = 2 * idx;
388  int idx_i = 2 * idx + 1;
389 
390  src1_r += src.cmp(idx_r, site, 0);
391  src1_i += src.cmp(idx_i, site, 0);
392  }
393  }
394  const double src_r = Communicator::reduce_sum(src1_r) / (Lvol3 * Nc * Nd);
395  const double src_i = Communicator::reduce_sum(src1_i) / (Lvol3 * Nc * Nd);
396  vout.general(m_vl, "%s: sum_x src = %e %e\n", class_name.c_str(), src_r, src_i);
397 
398  const double src2 = src.norm2() / (Lvol3 * Nc * Nd);
399  vout.general(m_vl, "%s: src.norm2 = %e\n", class_name.c_str(), src2);
400 #endif
401 }
402 
403 
404 //====================================================================
405 void Source_Random::set_all_space_time(Field& src, const int ic)
406 {
407  // This implementation assumes the given field src is complex field.
408  int nc = CommonParameters::Nc();
409  int Nex = src.nex();
410  int Nvol = src.nvol();
411  int Nin = src.nin();
412 
413  assert(ic < nc);
414  assert(Nin >= 2 * nc);
415  int nd = Nin / nc / 2;
416 // double rn, rn2, rn3;
417  double rn;
418 // double RF2 = 1.0 / sqrt(2.0);
419 
420  Field v(2, Nvol, Nex);
421  if (m_str_noise_type == "Gaussian") {
423  } else if (m_str_noise_type == "U1") {
424  m_rand->U1_lex_global(v);
425  } else if (m_str_noise_type == "Z2") {
426  m_rand->Z2_lex_global(v);
427  } else {
428  vout.crucial(m_vl, "Error at %s: Noise type of %s is not supported.\n", class_name.c_str(), m_str_noise_type.c_str());
429  exit(EXIT_FAILURE);
430  }
431  // m_rand->uniform_lex_global(v);
432  src.set(0.0);
433 
434  for (int ex = 0; ex < Nex; ++ex) {
435  for (int site = 0; site < Nvol; ++site) {
436  for (int ri = 0; ri < 2; ++ri) {
437  rn = v.cmp(ri, site, ex);
438  for (int id = 0; id < nd; ++id) {
439  int in = ri + 2 * (ic + nc * id);
440 // rn2 = floor(2.0 * rn);
441 // rn3 = (2.0 * rn2 - 1.0) * RF2;
442 // src.set(in, site, ex, rn3);
443  src.set(in, site, ex, rn);
444  }
445  }
446  }
447  }
448 }
449 
450 
451 //====================================================================
452 void Source_Random::set_all_space_time(Field& src, const int ic, const int is)
453 {
454  // This implementation assumes the given field src is complex field.
455  int nc = CommonParameters::Nc();
456  int nd = CommonParameters::Nd();
457  int Nex = src.nex();
458  int Nvol = src.nvol();
459  int Nin = src.nin();
460 
461  assert(ic < nc);
462  assert(is < nd);
463  assert(Nin == 2 * nc * nd);
464 
465  Field v(2, Nvol, Nex);
466  if (m_str_noise_type == "Gaussian") {
468  } else if (m_str_noise_type == "U1") {
469  m_rand->U1_lex_global(v);
470  } else if (m_str_noise_type == "Z2") {
471  m_rand->Z2_lex_global(v);
472  } else {
473  vout.crucial(m_vl, "Error at %s: Noise type of %s is not supported.\n", class_name.c_str(), m_str_noise_type.c_str());
474  exit(EXIT_FAILURE);
475  }
476  // m_rand->uniform_lex_global(v);
477  src.set(0.0);
478 
479 // double rn, rn2, rn3;
480  double rn;
481 // double RF2 = 1.0 / sqrt(2.0);
482 
483  for (int ex = 0; ex < Nex; ++ex) {
484  for (int site = 0; site < Nvol; ++site) {
485  for (int ri = 0; ri < 2; ++ri) {
486  int in = ri + 2 * (ic + nc * is);
487  rn = v.cmp(ri, site, ex);
488 // rn2 = floor(2.0 * rn);
489 // rn3 = (2.0 * rn2 - 1.0) * RF2;
490 // src.set(in, site, ex, rn3);
491  src.set(in, site, ex, rn);
492  }
493  }
494  }
495 }
496 
497 
498 //====================================================================
499 //============================================================END=====
Source_Random::set_all_space_time
void set_all_space_time(Field &src, const int ic)
Setting a noise vector. Filling all the sites and spin indices for color index "ic"....
Definition: source_Random.cpp:405
CommonParameters::Ny
static int Ny()
Definition: commonParameters.h:106
CommonParameters::Nz
static int Nz()
Definition: commonParameters.h:107
Source_Random::set
void set(Field &src, const int idx)
Definition: source_Random.cpp:135
Parameters::set_string
void set_string(const string &key, const string &value)
Definition: parameters.cpp:39
Source_Random::get_parameters
void get_parameters(Parameters &params) const
Definition: source_Random.cpp:53
CommonParameters::Ndim
static int Ndim()
Definition: commonParameters.h:117
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
Parameters
Class for parameters.
Definition: parameters.h:46
Source_Random::set_parameters
void set_parameters(const Parameters &params)
Definition: source_Random.cpp:25
Field::nex
int nex() const
Definition: field.h:128
RandomNumbers::Z2_lex_global
virtual void Z2_lex_global(Field &)
Z(2) random number defined on global lattice.
Definition: randomNumbers.cpp:142
CommonParameters::Ly
static int Ly()
Definition: commonParameters.h:92
CommonParameters::Nvol
static int Nvol()
Definition: commonParameters.h:109
RandomNumbers::U1_lex_global
virtual void U1_lex_global(Field &)
U(1) random number defined on global lattice.
Definition: randomNumbers.cpp:113
ParameterCheck::non_negative
int non_negative(const int v)
Definition: parameterCheck.cpp:21
Source_Random::m_source_position
std::vector< int > m_source_position
Definition: source_Random.h:45
Field::nin
int nin() const
Definition: field.h:126
source_Random.h
Bridge::BridgeIO::paranoiac
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:238
Field::norm2
double norm2() const
Definition: field.cpp:113
CommonParameters::Nx
static int Nx()
Definition: commonParameters.h:105
CommonParameters::Lt
static int Lt()
Definition: commonParameters.h:94
Communicator::reduce_sum
static int reduce_sum(int count, dcomplex *recv_buf, dcomplex *send_buf, int pattern=0)
make a global sum of an array of dcomplex over the communicator. pattern specifies the dimensions to ...
Definition: communicator.cpp:263
CommonParameters::Lx
static int Lx()
Definition: commonParameters.h:91
CommonParameters::Nc
static int Nc()
Definition: commonParameters.h:115
CommonParameters::Lz
static int Lz()
Definition: commonParameters.h:93
CommonParameters::Nt
static int Nt()
Definition: commonParameters.h:108
Parameters::fetch_int_vector
int fetch_int_vector(const string &key, vector< int > &value) const
Definition: parameters.cpp:429
AIndex_eo_qxs::idx
int idx(const int in, const int Nin, const int ist, const int Nx2, const int Ny, const int leo, const int Nvol2, const int ex)
Definition: aindex_eo.h:27
Source_Random::m_index
Index_lex m_index
Definition: source_Random.h:44
Source_Random::set_all_color_spin
void set_all_color_spin(Field &src)
Definition: source_Random.cpp:316
Field::nvol
int nvol() const
Definition: field.h:127
Source_Random::m_rand
RandomNumbers * m_rand
Definition: source_Random.h:43
Source_Random::m_in_node
bool m_in_node
Definition: source_Random.h:48
Index_lex::site
int site(const int &x, const int &y, const int &z, const int &t) const
Definition: index_lex.h:55
Parameters::set_int_vector
void set_int_vector(const string &key, const vector< int > &value)
Definition: parameters.cpp:45
Source_Random::m_vl
Bridge::VerboseLevel m_vl
Definition: source_Random.h:41
Source_Random::m_str_noise_type
std::string m_str_noise_type
Definition: source_Random.h:47
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
CommonParameters::Nd
static int Nd()
Definition: commonParameters.h:116
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
Source_Random::class_name
static const std::string class_name
Definition: source_Random.h:38
Communicator::ipe
static int ipe(const int dir)
logical coordinate of current proc.
Definition: communicator.cpp:105
Parameters::fetch_string
int fetch_string(const string &key, string &value) const
Definition: parameters.cpp:378
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Field
Container of Field-type object.
Definition: field.h:46
RandomNumbers::gauss_lex_global
virtual void gauss_lex_global(Field &)
gaussian random number defined on global lattice.
Definition: randomNumbers.cpp:95
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
Source_Random::m_source_momentum
std::vector< int > m_source_momentum
Definition: source_Random.h:46
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512
Source_Random::set_all_color
void set_all_color(Field &src, const int i_spin)
Definition: source_Random.cpp:225
Bridge::BridgeIO::get_verbose_level
static std::string get_verbose_level(const VerboseLevel vl)
Definition: bridgeIO.cpp:154