Bridge++  Ver. 1.2.x
 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 <iostream>
18 #include <fstream>
19 #include <cassert>
20 
21 #include "communicator.h"
22 #include "index_lex.h"
23 #include "index_eo.h"
24 
25 using Bridge::vout;
26 
27 namespace {
28  const double sq2r = 1.0 / sqrt(2.0);
29 // const double pi = 3.141592653589793;
30  const double pi = 4.0 * atan(1.0);
31  const double pi2 = 2.0 * pi;
32 }
33 
34 const std::string RandomNumbers::class_name = "RandomNumbers";
35 
36 //====================================================================
37 void RandomNumbers::gauss(double& rn1, double& rn2)
38 {
39  // Two Gaussian random number with deviation 1/\sqrt(2).
40  double r1 = get();
41  double r2 = get();
42 
43  double slg1 = sqrt(-log(1.0 - r1) * 2.0) * sq2r;
44  double ang1 = pi2 * r2;
45 
46  rn1 = slg1 * cos(ang1);
47  rn2 = slg1 * sin(ang1);
48 }
49 
50 
51 //====================================================================
53 {
54  if (f.nin() % 2 == 0) {
55  return generate_global<rand_gauss_even>(f);
56  } else {
57  return generate_global<rand_gauss_odd>(f);
58  }
59 }
60 
61 
62 //====================================================================
64 {
65  return generate_global<rand_uniform>(f);
66 }
67 
68 
69 //====================================================================
71 {
72  Field g = f.clone();
73 
75 
76  Index_eo idx;
77  idx.convertField(f, g);
78 }
79 
80 
81 //====================================================================
83 {
84  if (do_fill) {
85  for (size_t i = 0; i < m_block; i += 2) {
86  double r1 = m_rng->get();
87  double r2 = m_rng->get();
88 
89  double slg1 = sqrt(-log(1 - r1) * 2.0) * sq2r;
90  double ang1 = pi2 * r2;
91 
92  *m_ptr++ = slg1 * cos(ang1);
93  *m_ptr++ = slg1 * sin(ang1);
94  }
95  } else {
96  // just let rng state proceed
97  for (size_t i = 0; i < m_block; i += 2) {
98  m_rng->get();
99  m_rng->get();
100  }
101  }
102 }
103 
104 
106 {
107  return m_block;
108 }
109 
110 
111 //====================================================================
113 {
114  if (do_fill) {
115  size_t ngauss = m_block / 2;
116 
117  for (size_t i = 0; i < ngauss; ++i) {
118  double r1 = m_rng->get();
119  double r2 = m_rng->get();
120 
121  double slg1 = sqrt(-log(1 - r1) * 2.0) * sq2r;
122  double ang1 = pi2 * r2;
123 
124  *m_ptr++ = slg1 * cos(ang1);
125  *m_ptr++ = slg1 * sin(ang1);
126  }
127 
128  // remaining one
129  {
130  double r1 = m_rng->get();
131  double r2 = m_rng->get();
132 
133  double slg1 = sqrt(-log(1 - r1) * 2.0) * sq2r;
134  double ang1 = pi2 * r2;
135 
136  *m_ptr++ = slg1 * cos(ang1);
137  // the other one is dropped.
138  }
139  } else {
140  for (size_t i = 0; i < m_block + 1; ++i) {
141  m_rng->get();
142  }
143  }
144 }
145 
146 
148 {
149  return m_block + 1;
150 }
151 
152 
153 //====================================================================
155 {
156  if (do_fill) {
157  for (size_t i = 0; i < m_block; ++i) {
158  *m_ptr++ = m_rng->get();
159  }
160  } else {
161  for (size_t i = 0; i < m_block; ++i) {
162  m_rng->get();
163  }
164  }
165 }
166 
167 
169 {
170  return m_block;
171 }
172 
173 
174 //====================================================================
175 template<typename InnerGenerator>
177 {
178  InnerGenerator fill(f, this);
179 
180  int Nin = f.nin();
181  int Nvol = f.nvol();
182  int Nex = f.nex();
183 
184  if (Communicator::size() == 1) {
185  vout.detailed(m_vl, "%s: single node. no need to consider division.\n", class_name.c_str());
186 
187  for (int j = 0; j < Nex; ++j) {
188  for (int i = 0; i < Nvol; ++i) {
189  fill(true);
190  }
191  }
192  } else {
193  assert(Nvol == CommonParameters::Nvol());
194 
195  int Lx = CommonParameters::Lx();
196  int Ly = CommonParameters::Ly();
197  int Lz = CommonParameters::Lz();
198  int Lt = CommonParameters::Lt();
199 
200  int Nx = CommonParameters::Nx();
201  int Ny = CommonParameters::Ny();
202  int Nz = CommonParameters::Nz();
203  int Nt = CommonParameters::Nt();
204 
205  int gx = Communicator::ipe(0);
206  int gy = Communicator::ipe(1);
207  int gz = Communicator::ipe(2);
208  int gt = Communicator::ipe(3);
209 
210  for (int j = 0; j < Nex; ++j) {
211  bool in_j = true;
212 
213  for (int t = 0; t < Lt; ++t) {
214  bool in_t = in_j && (t >= gt * Nt) && (t < (gt + 1) * Nt);
215 
216  for (int z = 0; z < Lz; ++z) {
217  bool in_z = in_t && (z >= gz * Nz) && (z < (gz + 1) * Nz);
218 
219  for (int y = 0; y < Ly; ++y) {
220  bool in_y = in_z && (y >= gy * Ny) && (y < (gy + 1) * Ny);
221 
222  for (int x = 0; x < Lx; ++x) {
223  bool in_x = in_y && (x >= gx * Nx) && (x < (gx + 1) * Nx);
224 
225  fill(in_x);
226  }
227  }
228  }
229  }
230  }
231  } // end if communicator::size == 1
232 }
233 
234 
235 //====================================================================
236 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:207
void detailed(const char *format,...)
Definition: bridgeIO.cpp:50
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:37
int nvol() const
Definition: field.h:101
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:43
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:46
int nin() const
Definition: field.h:100
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:77
int nex() const
Definition: field.h:102
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 &rn1, double &rn2)