randomGenerator.C
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 \*---------------------------------------------------------------------------*/
25 
26 #include "randomGenerator.H"
27 #include "uint64.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
32 :
33  global_(global),
34  x_(pTraits<uint64_t>(is))
35 {
36  checkSync();
37 }
38 
39 
41 (
42  const word& name,
43  const dictionary& dict,
44  randomGenerator&& defaultRndGen
45 )
46 :
47  global_(defaultRndGen.global_),
48  x_
49  (
50  dict.found(name)
51  ? dict.lookup<uint64_t>(name)
52  : dict.found(name + "Seed")
53  ? seed(dict.lookup<label>(name + "Seed")).x(global_)
54  : defaultRndGen.x_
55  )
56 {}
57 
58 
60 (
61  const word& name,
62  const dictionary& dict,
63  const seed defaultS,
64  const bool global
65 )
66 :
67  randomGenerator(name, dict, randomGenerator(defaultS, global))
68 {}
69 
70 
71 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
72 
74 {
75  if (global_ != rndGen.global_)
76  {
78  << "Attempted assignment of a " << (global_ ? "" : "non-")
79  << "global random generator to a " << (rndGen.global_ ? "" : "non-")
80  << "global random generator"
81  << exit(FatalError);
82  }
83  x_ = rndGen.x_;
84  checkSync();
85 }
86 
87 
89 {
90  *this = static_cast<const randomGenerator&>(rndGen);
91 }
92 
93 
94 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
95 
97 {
98  is >> rndGen.x_;
99  rndGen.checkSync();
100  is.check("operator>>(Istream& is, randomGenerator& rndGen)");
101  return is;
102 }
103 
104 
106 {
107  rndGen.checkSync();
108  os << rndGen.x_;
109  os.check("operator<<(Ostream& os, const randomGenerator& rndGen)");
110  return os;
111 }
112 
113 
114 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
115 
117 {
118  os << rndGen;
119 }
120 
121 
122 // ************************************************************************* //
bool found
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
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
Traits class for primitives.
Definition: pTraits.H:53
Random number generator.
randomGenerator(const seed s, const bool global=false)
Construct from a seed.
void operator=(const randomGenerator &)
Copy-assignment.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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)
error FatalError
dictionary dict
randomGenerator rndGen(653213)
64bit uinteger