cachedRandom.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "cachedRandom.H"
27 #include "OSspecific.H"
28 #include "PstreamReduceOps.H"
29 
30 // * * * * * * * * * * * * * private Member Functions * * * * * * * * * * * //
31 
32 Foam::scalar Foam::cachedRandom::scalar01()
33 {
34  if (sampleI_ < 0)
35  {
36  return osRandomDouble();
37  }
38 
39  if (sampleI_ == samples_.size() - 1)
40  {
41  scalar s = samples_[sampleI_];
42  sampleI_ = 0;
43  return s;
44  }
45  else
46  {
47  scalar s = samples_[sampleI_];
48  sampleI_++;
49  return s;
50  }
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
57 :
58  seed_(1),
59  samples_(0),
60  sampleI_(-1),
61  hasGaussSample_(false),
62  gaussSample_(0)
63 {
64  if (seed > 1)
65  {
66  seed_ = seed;
67  }
68 
69  // Samples will be cached if count > 0
70  if (count > 0)
71  {
72  samples_.setSize(count);
73  sampleI_ = 0;
74  }
75 
76  // Initialise samples
77  osRandomSeed(seed_);
78  forAll(samples_, i)
79  {
80  samples_[i] = osRandomDouble();
81  }
82 }
83 
84 
85 Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
86 :
87  seed_(cr.seed_),
88  samples_(cr.samples_),
89  sampleI_(cr.sampleI_),
90  hasGaussSample_(cr.hasGaussSample_),
91  gaussSample_(cr.gaussSample_)
92 {
93  if (reset)
94  {
95  hasGaussSample_ = false;
96  gaussSample_ = 0;
97  }
98  if (sampleI_ == -1)
99  {
101  << "Copy constructor called, but samples not being cached. "
102  << "This may lead to non-repeatable behaviour" << endl;
103 
104  osRandomSeed(seed_);
105  }
106 
107  if (reset && samples_.size())
108  {
109  sampleI_ = 0;
110  }
111 }
112 
113 
114 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
115 
117 {}
118 
119 
120 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
121 
122 template<>
124 {
125  return scalar01();
126 }
127 
128 
129 template<>
131 {
132  return round(scalar01());
133 }
134 
135 
136 template<>
138 {
139  if (hasGaussSample_)
140  {
141  hasGaussSample_ = false;
142  return gaussSample_;
143  }
144  else
145  {
146  scalar rsq, v1, v2;
147  do
148  {
149  v1 = 2*scalar01() - 1;
150  v2 = 2*scalar01() - 1;
151  rsq = sqr(v1) + sqr(v2);
152  } while (rsq >= 1 || rsq == 0);
153 
154  scalar fac = sqrt(-2*log(rsq)/rsq);
155 
156  gaussSample_ = v1*fac;
157  hasGaussSample_ = true;
158 
159  return v2*fac;
160  }
161 }
162 
163 
164 template<>
166 {
167  return round(GaussNormal<scalar>());
168 }
169 
170 
171 template<>
172 Foam::scalar Foam::cachedRandom::position
173 (
174  const scalar& start,
175  const scalar& end
176 )
177 {
178  return start + scalar01()*(end - start);
179 }
180 
181 
182 template<>
184 {
185  return start + round(scalar01()*(end - start));
186 }
187 
188 
189 template<>
191 {
192  scalar value = -GREAT;
193 
194  if (Pstream::master())
195  {
196  value = scalar01();
197  }
198 
199  Pstream::scatter(value);
200 
201  return value;
202 }
203 
204 
205 template<>
207 {
208  scalar value = -GREAT;
209 
210  if (Pstream::master())
211  {
212  value = scalar01();
213  }
214 
215  Pstream::scatter(value);
216 
217  return round(value);
218 }
219 
220 
221 template<>
223 {
224  scalar value = -GREAT;
225 
226  if (Pstream::master())
227  {
228  value = GaussNormal<scalar>();
229  }
230 
231  Pstream::scatter(value);
232 
233  return value;
234 }
235 
236 
237 template<>
239 {
240  scalar value = -GREAT;
241 
242  if (Pstream::master())
243  {
244  value = GaussNormal<scalar>();
245  }
246 
247  Pstream::scatter(value);
248 
249  return round(value);
250 }
251 
252 
253 template<>
255 (
256  const scalar& start,
257  const scalar& end
258 )
259 {
260  scalar value = -GREAT;
261 
262  if (Pstream::master())
263  {
264  value = scalar01()*(end - start);
265  }
266 
267  Pstream::scatter(value);
268 
269  return start + value;
270 }
271 
272 
273 template<>
275 (
276  const label& start,
277  const label& end
278 )
279 {
280  label value = labelMin;
281 
282  if (Pstream::master())
283  {
284  value = round(scalar01()*(end - start));
285  }
286 
287  Pstream::scatter(value);
288 
289  return start + value;
290 }
291 
292 
293 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Inter-processor communication reduction functions.
dimensionedScalar log(const dimensionedScalar &ds)
cachedRandom(const label seed, const label count)
Construct given seed and sample count.
Definition: cachedRandom.C:56
dimensionedSymmTensor sqr(const dimensionedVector &dv)
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
Definition: POSIX.C:1169
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:411
Type globalPosition(const Type &start, const Type &end)
Return a sample between start and end.
Random number generator.
Definition: cachedRandom.H:63
label seed() const
Return const access to the initial random number seed.
Definition: cachedRandomI.H:30
Type GaussNormal()
Return a sample whose components are normally distributed.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Type sample01()
Return a sample whose components lie in the range 0-1.
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
void setSize(const label)
Reset size of List.
Definition: List.C:295
Type globalGaussNormal()
Return a sample whose components are normally distributed.
#define WarningInFunction
Report a warning using Foam::Warning.
Type position(const Type &start, const Type &end)
Return a sample between start and end.
void osRandomSeed(const label seed)
Seed random number generator.
Definition: POSIX.C:1149
static const label labelMin
Definition: label.H:61
Type globalSample01()
Return a sample whose components lie in the range 0-1.