Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
randomNumbers.cpp
Go to the documentation of this file.
1 
15 #include "randomNumbers.h"
16 
17 #include <fstream>
18 #include <cassert>
19 
20 #include "Field/index_lex.h"
21 #include "Field/index_eo.h"
22 
23 namespace {
24  const double sq2r = 1.0 / sqrt(2.0);
25  // const double pi = 3.141592653589793;
26  const double pi = 4.0 * atan(1.0);
27  const double pi2 = 2.0 * pi;
28 }
29 
30 const std::string RandomNumbers::class_name = "RandomNumbers";
31 
32 //====================================================================
33 void RandomNumbers::gauss(double& rand1, double& rand2)
34 {
35  // Two Gaussian random number with deviation 1/\sqrt(2).
36  double r1 = get();
37  double r2 = get();
38 
39  double slg1 = sqrt(-log(1.0 - r1) * 2.0) * sq2r;
40  double ang1 = pi2 * r2;
41 
42  rand1 = slg1 * cos(ang1);
43  rand2 = slg1 * sin(ang1);
44 }
45 
46 
47 //====================================================================
49 {
50  if (f.nin() % 2 == 0) {
51  return generate_global<rand_gauss_even>(f);
52  } else {
53  return generate_global<rand_gauss_odd>(f);
54  }
55 }
56 
57 
58 //====================================================================
60 {
61  return generate_global<rand_uniform>(f);
62 }
63 
64 
65 //====================================================================
67 {
68  Field g = f.clone();
69 
71 
72  Index_eo idx;
73  idx.convertField(f, g);
74 }
75 
76 
77 //====================================================================
79 {
80  if (do_fill) {
81  for (size_t i = 0; i < m_block; i += 2) {
82  double r1 = m_rand_gauss->get();
83  double r2 = m_rand_gauss->get();
84 
85  double slg1 = sqrt(-log(1 - r1) * 2.0) * sq2r;
86  double ang1 = pi2 * r2;
87 
88  *m_ptr++ = slg1 * cos(ang1);
89  *m_ptr++ = slg1 * sin(ang1);
90  }
91  } else {
92  // just let rand_gauss state proceed
93  for (size_t i = 0; i < m_block; i += 2) {
94  m_rand_gauss->get();
95  m_rand_gauss->get();
96  }
97  }
98 }
99 
100 
102 {
103  return m_block;
104 }
105 
106 
107 //====================================================================
109 {
110  if (do_fill) {
111  size_t ngauss = m_block / 2;
112 
113  for (size_t i = 0; i < ngauss; ++i) {
114  double r1 = m_rand_gauss->get();
115  double r2 = m_rand_gauss->get();
116 
117  double slg1 = sqrt(-log(1 - r1) * 2.0) * sq2r;
118  double ang1 = pi2 * r2;
119 
120  *m_ptr++ = slg1 * cos(ang1);
121  *m_ptr++ = slg1 * sin(ang1);
122  }
123 
124  // remaining one
125  {
126  double r1 = m_rand_gauss->get();
127  double r2 = m_rand_gauss->get();
128 
129  double slg1 = sqrt(-log(1 - r1) * 2.0) * sq2r;
130  double ang1 = pi2 * r2;
131 
132  *m_ptr++ = slg1 * cos(ang1);
133  // the other one is dropped.
134  }
135  } else {
136  for (size_t i = 0; i < m_block + 1; ++i) {
137  m_rand_gauss->get();
138  }
139  }
140 }
141 
142 
144 {
145  return m_block + 1;
146 }
147 
148 
149 //====================================================================
151 {
152  if (do_fill) {
153  for (size_t i = 0; i < m_block; ++i) {
154  *m_ptr++ = m_rand_gauss->get();
155  }
156  } else {
157  for (size_t i = 0; i < m_block; ++i) {
158  m_rand_gauss->get();
159  }
160  }
161 }
162 
163 
165 {
166  return m_block;
167 }
168 
169 
170 //====================================================================
171 template<typename InnerGenerator>
173 {
174  InnerGenerator fill(f, this);
175 
176  int Nin = f.nin();
177  int Nvol = f.nvol();
178  int Nex = f.nex();
179 
180  if (Communicator::size() == 1) {
181  vout.detailed(m_vl, "%s: single node. no need to consider division.\n", class_name.c_str());
182 
183  for (int j = 0; j < Nex; ++j) {
184  for (int i = 0; i < Nvol; ++i) {
185  fill(true);
186  }
187  }
188  } else {
189  assert(Nvol == CommonParameters::Nvol());
190 
191  int Lx = CommonParameters::Lx();
192  int Ly = CommonParameters::Ly();
193  int Lz = CommonParameters::Lz();
194  int Lt = CommonParameters::Lt();
195 
196  int Nx = CommonParameters::Nx();
197  int Ny = CommonParameters::Ny();
198  int Nz = CommonParameters::Nz();
199  int Nt = CommonParameters::Nt();
200 
201  int gx = Communicator::ipe(0);
202  int gy = Communicator::ipe(1);
203  int gz = Communicator::ipe(2);
204  int gt = Communicator::ipe(3);
205 
206  for (int j = 0; j < Nex; ++j) {
207  bool in_j = true;
208 
209  for (int t = 0; t < Lt; ++t) {
210  bool in_t = in_j && (t >= gt * Nt) && (t < (gt + 1) * Nt);
211 
212  for (int z = 0; z < Lz; ++z) {
213  bool in_z = in_t && (z >= gz * Nz) && (z < (gz + 1) * Nz);
214 
215  for (int y = 0; y < Ly; ++y) {
216  bool in_y = in_z && (y >= gy * Ny) && (y < (gy + 1) * Ny);
217 
218  for (int x = 0; x < Lx; ++x) {
219  bool in_x = in_y && (x >= gx * Nx) && (x < (gx + 1) * Nx);
220 
221  fill(in_x);
222  }
223  }
224  }
225  }
226  }
227  } // end if communicator::size == 1
228 }
229 
230 
231 //====================================================================
232 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:495
void detailed(const char *format,...)
Definition: bridgeIO.cpp:212
virtual double get()=0
virtual void uniform_lex_global(Field &)
uniform random number defined on global lattice.
Container of Field-type object.
Definition: field.h:39
int nvol() const
Definition: field.h:116
static int ipe(const int dir)
logical coordinate of current proc.
Even-odd site index.
Definition: index_eo.h:39
static const std::string class_name
Definition: randomNumbers.h:39
void convertField(Field &eo, const Field &lex)
Definition: index_eo.cpp:20
virtual void gauss_lex_global(Field &)
gaussian random number defined on global lattice.
Bridge::VerboseLevel m_vl
Definition: randomNumbers.h:42
int nin() const
Definition: field.h:115
void operator()(const bool do_fill)
virtual void gauss_eo_global(Field &)
gaussian noise for even-odd perconditioned field (S.UEDA)
Field clone() const
Definition: field.h:79
int nex() const
Definition: field.h:117
void operator()(const bool do_fill)
void operator()(const bool do_fill)
static int size()
size of small world.
void generate_global(Field &f)
void gauss(double &rand1, double &rand2)