randomGenerator.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2018-2024 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 Class
25  Foam::randomGenerator
26 
27 Description
28  Random number generator
29 
30  This is a clone of the drand48 algorithm. This is significantly quicker
31  than drand48, presumably due to the compiler inlining the sampling methods.
32  It is also significantly quicker than the standard library linear
33  congruential engine, as it does not use Schrage's algorithm to prevent
34  overflow.
35 
36  See <http://pubs.opengroup.org/onlinepubs/007908775/xsh/drand48.html> for
37  details of the seeding and iteration sequence.
38 
39 SourceFiles
40  randomGenerator.C
41  randomGeneratorI.H
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef randomGenerator_H
46 #define randomGenerator_H
47 
48 #include "scalarField.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward declaration of classes
56 class Istream;
57 class Ostream;
58 
59 // Forward declaration of friend functions and operators
60 class randomGenerator;
61 Istream& operator>>(Istream&, randomGenerator&);
62 Ostream& operator<<(Ostream&, const randomGenerator&);
63 
64 /*---------------------------------------------------------------------------*\
65  Class randomGenerator Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class randomGenerator
69 {
70 public:
71 
72  // Public Classes
73 
74  //- Seed class
75  class seed
76  {
77  // Private Data
78 
79  //- The seed value
80  const uint64_t s_;
81 
82 
83  // Private Member Functions
84 
85  //- Return the initial integer
86  inline uint64_t x(const bool global) const;
87 
88 
89  public:
90 
91  //- Allow randomGenerator to access the seed value
92  friend class randomGenerator;
93 
94 
95  // Constructors
96 
97  //- Construct from a label
98  inline seed(const label s);
99 
100  //- Construct from a word
101  inline seed(const word& s);
102  };
103 
104 
105 private:
106 
107  // Private Static Data
108 
109  //- The parameters of the linear congruential iteration
110  static const uint64_t A = 0x5DEECE66D, C = 0xB, M = uint64_t(1) << 48;
111 
112 
113  // Private Data
114 
115  //- Is this generator global?
116  const bool global_;
117 
118  //- The stored integer
119  uint64_t x_;
120 
121 
122  // Private Member Functions
123 
124  //- Check the state is synchronised
125  void checkSync() const;
126 
127  //- Advance the state and return an integer sample
128  inline uint64_t sample();
129 
130 
131  // Scalars
132 
133  //- Return a scalar uniformly distributed between zero and one.
134  // Don't check synchronisation.
135  inline scalar scalar01NoCheckSync();
136 
137  //- Return a scalar uniformly distributed between zero and one.
138  // Don't check synchronisation.
139  inline scalar scalarABNoCheckSync(const scalar a, const scalar b);
140 
141 
142  // Other Types
143 
144  //- Return a type with components uniformly distributed between
145  // zero and one. Don't check synchronisation.
146  template<class Type>
147  inline Type sample01NoCheckSync();
148 
149  //- Return a type with components uniformly distributed between two
150  // limits. Don't check synchronisation.
151  template<class Type>
152  inline Type sampleABNoCheckSync(const Type& a, const Type& b);
153 
154 
155 public:
156 
157  // Constructors
158 
159  //- Construct from a seed
160  inline randomGenerator(const seed s, const bool global = false);
161 
162  //- Copy construct
163  inline randomGenerator(const randomGenerator&);
164 
165  //- Move construct
167 
168  //- Construct from a stream
169  randomGenerator(Istream& is, const bool global = false);
170 
171  //- Construct from a dictionary
173  (
174  const word& name,
175  const dictionary& dict,
176  randomGenerator&& defaultRndGen
177  );
178 
179  //- Construct from a dictionary
181  (
182  const word& name,
183  const dictionary& dict,
184  const seed defaultS,
185  const bool global = false
186  );
187 
188 
189  //- Destructor
190  inline ~randomGenerator();
191 
192 
193  // Member Functions
194 
195  // Scalars
196 
197  //- Return a scalar uniformly distributed between zero and one
198  inline scalar scalar01();
199 
200  //- Return scalars uniformly distributed between zero and one
201  inline tmp<scalarField> scalar01(const label n);
202 
203  //- Return a scalar uniformly distributed between two limits
204  inline scalar scalarAB(const scalar a, const scalar b);
205 
206  //- Return scalars uniformly distributed between two limits
208  (
209  const label n,
210  const scalar a,
211  const scalar b
212  );
213 
214 
215  // Other Types
216 
217  //- Return a type with components uniformly distributed between
218  // zero and one
219  template<class Type>
220  inline Type sample01();
221 
222  //- Return types with components uniformly distributed between zero
223  // and one
224  template<class Type>
225  inline tmp<Field<Type>> sample01(const label n);
226 
227  //- Return a type with components uniformly distributed between two
228  // limits
229  template<class Type>
230  inline Type sampleAB(const Type& a, const Type& b);
231 
232  //- Return types with components uniformly distributed between two
233  // limits
234  template<class Type>
235  inline tmp<Field<Type>> sampleAB
236  (
237  const label n,
238  const Type& a,
239  const Type& b
240  );
241 
242 
243  //- Randomly permute a list
244  template<class Container>
245  inline void permute(Container& l);
246 
247  //- Create a randomly seeded generator
248  inline randomGenerator generator();
249 
250 
251  // Member Operators
252 
253  //- Copy-assignment
254  void operator=(const randomGenerator&);
255 
256  //- Move-assignment
257  void operator=(randomGenerator&&);
258 
259 
260  // IOstream Operators
261 
262  //- Read from stream
264 
265  //- Write to stream
266  friend Ostream& operator<<(Ostream&, const randomGenerator&);
267 };
268 
269 
270 template<>
271 inline scalar randomGenerator::sample01NoCheckSync();
272 
273 template<>
274 inline label randomGenerator::sample01NoCheckSync();
275 
276 template<>
277 inline scalar randomGenerator::sampleABNoCheckSync
278 (
279  const scalar& a,
280  const scalar& b
281 );
282 
283 template<>
284 inline label randomGenerator::sampleABNoCheckSync
285 (
286  const label& a,
287  const label& b
288 );
289 
290 
291 void writeEntry(Ostream& os, const randomGenerator& rndGen);
292 
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
296 } // End namespace Foam
297 
298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 
300 #include "randomGeneratorI.H"
301 
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303 
304 #endif
305 
306 // ************************************************************************* //
static const Foam::dimensionedScalar A("A", Foam::dimPressure, 611.21)
#define M(I)
label n
Graphite solid properties.
Definition: C.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
seed(const label s)
Construct from a label.
Random number generator.
scalar scalarAB(const scalar a, const scalar b)
Return a scalar uniformly distributed between two limits.
friend Ostream & operator<<(Ostream &, const randomGenerator &)
Write to stream.
randomGenerator(const seed s, const bool global=false)
Construct from a seed.
scalar scalar01()
Return a scalar uniformly distributed between zero and one.
void operator=(const randomGenerator &)
Copy-assignment.
randomGenerator generator()
Create a randomly seeded generator.
Type sample01()
Return a type with components uniformly distributed between.
friend Istream & operator>>(Istream &, randomGenerator &)
Read from stream.
void permute(Container &l)
Randomly permute a list.
Type sampleAB(const Type &a, const Type &b)
Return a type with components uniformly distributed between two.
~randomGenerator()
Destructor.
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
volScalarField & b
Definition: createFields.H:25
Namespace for OpenFOAM.
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
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
dictionary dict
randomGenerator rndGen(653213)