Random.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 "Random.H"
27 #include "OSspecific.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 #if INT_MAX != 2147483647
32 # error "INT_MAX != 2147483647"
33 # error "The random number generator may not work!"
34 #endif
35 
36 
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 
40 {
41  if (seed > 1)
42  {
43  Seed = seed;
44  }
45  else
46  {
47  Seed = 1;
48  }
49 
51 }
52 
53 
55 {
56  if (osRandomInteger() > INT_MAX/2)
57  {
58  return 1;
59  }
60  else
61  {
62  return 0;
63  }
64 }
65 
66 
67 Foam::scalar Foam::Random::scalar01()
68 {
69  return osRandomDouble();
70 }
71 
72 
74 {
75  vector rndVec;
76  for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
77  {
78  rndVec.component(cmpt) = scalar01();
79  }
80 
81  return rndVec;
82 }
83 
84 
86 {
87  sphericalTensor rndTen;
88  rndTen.ii() = scalar01();
89 
90  return rndTen;
91 }
92 
93 
95 {
96  symmTensor rndTen;
97  for (direction cmpt=0; cmpt<symmTensor::nComponents; cmpt++)
98  {
99  rndTen.component(cmpt) = scalar01();
100  }
101 
102  return rndTen;
103 }
104 
105 
107 {
108  tensor rndTen;
109  for (direction cmpt=0; cmpt<tensor::nComponents; cmpt++)
110  {
111  rndTen.component(cmpt) = scalar01();
112  }
113 
114  return rndTen;
115 }
116 
117 
118 Foam::label Foam::Random::integer(const label lower, const label upper)
119 {
120  return lower + (osRandomInteger() % (upper+1-lower));
121 }
122 
123 
125 {
126  vector rndVec(start);
127 
128  for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
129  {
130  rndVec.component(cmpt) +=
131  scalar01()*(end.component(cmpt) - start.component(cmpt));
132  }
133 
134  return rndVec;
135 }
136 
137 
139 {
140  s = scalar01();
141 }
142 
143 
145 {
146  v = vector01();
147 }
148 
149 
151 {
152  st = sphericalTensor01();
153 }
154 
155 
157 {
158  st = symmTensor01();
159 }
160 
161 
163 {
164  t = tensor01();
165 }
166 
167 
169 {
170  static int iset = 0;
171  static scalar gset;
172  scalar fac, rsq, v1, v2;
173 
174  if (iset == 0)
175  {
176  do
177  {
178  v1 = 2.0*scalar01() - 1.0;
179  v2 = 2.0*scalar01() - 1.0;
180  rsq = sqr(v1) + sqr(v2);
181  } while (rsq >= 1.0 || rsq == 0.0);
182 
183  fac = sqrt(-2.0*log(rsq)/rsq);
184  gset = v1*fac;
185  iset = 1;
186 
187  return v2*fac;
188  }
189  else
190  {
191  iset = 0;
192 
193  return gset;
194  }
195 }
196 
197 
198 // ************************************************************************* //
Foam::osRandomInteger
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
Definition: POSIX.C:1331
Foam::Random::GaussNormal
scalar GaussNormal()
Return a normal Gaussian randon number.
Definition: Random.C:168
Foam::Tensor
Templated 3D tensor derived from VectorSpace adding construction from 9 components,...
Definition: complexI.H:224
Foam::SymmTensor< scalar >
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::SphericalTensor::ii
const Cmpt & ii() const
Definition: SphericalTensorI.H:68
Foam::Random::Seed
label Seed
Definition: Random.H:53
Foam::VectorSpace< Vector< scalar >, scalar, 3 >::nComponents
@ nComponents
Number of components in this vector space.
Definition: VectorSpace.H:88
Foam::Random::Random
Random(const label)
Construct given seed.
Definition: Random.C:39
Foam::VectorSpace::component
const Cmpt & component(const direction) const
Foam::osRandomSeed
void osRandomSeed(const label seed)
Seed random number generator.
Definition: POSIX.C:1321
Foam::Random::tensor01
tensor tensor01()
Tensor with every component scalar01.
Definition: Random.C:106
Foam::label
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
Foam::Random::scalar01
scalar scalar01()
Scalar [0..1] (so including 0,1)
Definition: Random.C:67
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:253
Foam::Random::vector01
vector vector01()
Vector with every component scalar01.
Definition: Random.C:73
s
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))
Foam::SphericalTensor
Templated 3D SphericalTensor derived from VectorSpace adding construction from 1 component,...
Definition: SphericalTensor.H:51
Random.H
Foam::Random::integer
label integer(const label lower, const label upper)
Label [lower..upper].
Definition: Random.C:118
Foam::Random::position
vector position(const vector &, const vector &)
Definition: Random.C:124
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:49
Foam::Random::randomise
void randomise(scalar &)
Definition: Random.C:138
Foam::Vector< scalar >
Foam::Random::sphericalTensor01
sphericalTensor sphericalTensor01()
sphericalTensor with every component scalar01
Definition: Random.C:85
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:142
Foam::Random::symmTensor01
symmTensor symmTensor01()
symmTensor with every component scalar01
Definition: Random.C:94
Foam::direction
unsigned char direction
Definition: direction.H:43
Foam::Random::bit
int bit()
Definition: Random.C:54
Foam::osRandomDouble
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
Definition: POSIX.C:1341