uniform.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) 2011-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 "uniform.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 namespace distributions
34 {
37 }
38 }
39 
40 
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 
43 Foam::scalar Foam::distributions::uniform::Phi(const scalar x) const
44 {
45  if (q() == -1)
46  {
47  return log(x);
48  }
49  else
50  {
51  return integerPow(x, 1 + q())/(1 + q());
52  }
53 }
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
59 (
60  const unitConversion& units,
61  const dictionary& dict,
62  const label sampleQ,
64 )
65 :
67  (
68  typeName,
69  units,
70  dict,
71  sampleQ,
72  std::move(rndGen)
73  ),
74  min_(dict.lookupBackwardsCompatible<scalar>({"min", "minValue"}, units)),
75  max_(dict.lookupBackwardsCompatible<scalar>({"max", "maxValue"}, units)),
76  Phi0_(Phi(min_)),
77  Phi1_(Phi(max_))
78 {
79  validateBounds(dict);
80  if (q() != 0) validatePositive(dict);
81 }
82 
83 
85 :
87  min_(d.min_),
88  max_(d.max_),
89  Phi0_(Phi(min_)),
90  Phi1_(Phi(max_))
91 {}
92 
93 
94 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
95 
97 {}
98 
99 
100 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101 
103 {
104  const scalar s = rndGen_.sample01<scalar>();
105 
106  if (q() == -1)
107  {
108  return min_*pow(max_/min_, s);
109  }
110  else
111  {
112  const scalar PhiS = (1 - s)*Phi0_ + s*Phi1_;
113 
114  return integerRoot((1 + q())*PhiS, 1 + q());
115  }
116 }
117 
118 
120 {
121  return min_;
122 }
123 
124 
126 {
127  return max_;
128 }
129 
130 
132 {
133  if (q() == -2)
134  {
135  return Foam::log(max_/min_)/(Phi1_ - Phi0_);
136  }
137  else
138  {
139  const scalar Mu0 = integerPow(min_, 2 + q())/(2 + q());
140  const scalar Mu1 = integerPow(max_, 2 + q())/(2 + q());
141 
142  return (Mu1 - Mu0)/(Phi1_ - Phi0_);
143  }
144 }
145 
146 
148 (
149  Ostream& os,
150  const unitConversion& units
151 ) const
152 {
154 
155  writeEntry(os, "min", units, min_);
156  writeEntry(os, "max", units, max_);
157 }
158 
159 
162 {
163  if (q() == -1)
164  {
165  return clipPDF(x, 1/x/Foam::log(max_/min_));
166  }
167  else
168  {
169  return clipPDF(x, integerPow(x, q())/(Phi1_ - Phi0_));
170  }
171 }
172 
173 
174 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
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
Base class for statistical distributions.
Definition: distribution.H:76
label q() const
Return the effective distribution size exponent.
Definition: distribution.H:107
virtual tmp< scalarField > x(const label n) const
Return coordinates to plot across the range of the distribution.
Definition: distribution.C:147
virtual void write(Ostream &os, const unitConversion &units) const
Write to a stream.
Definition: distribution.C:134
Distribution in which all values between a specified minimum and maximum have the same probability.
Definition: uniform.H:68
virtual scalar min() const
Return the minimum value.
Definition: uniform.C:119
virtual scalar sample() const
Sample the distribution.
Definition: uniform.C:102
virtual tmp< scalarField > PDF(const scalarField &x) const
Return the distribution probability density function.
Definition: uniform.C:161
uniform(const unitConversion &units, const dictionary &dict, const label sampleQ, randomGenerator &&rndGen)
Construct from a dictionary.
Definition: uniform.C:59
virtual void write(Ostream &os, const unitConversion &units) const
Write to a stream.
Definition: uniform.C:148
virtual ~uniform()
Destructor.
Definition: uniform.C:96
virtual scalar max() const
Return the maximum value.
Definition: uniform.C:125
virtual scalar mean() const
Return the mean value.
Definition: uniform.C:131
Random number generator.
A class for managing temporary objects.
Definition: tmp.H:55
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
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))
defineTypeNameAndDebug(exponential, 0)
addToRunTimeSelectionTable(distribution, exponential, dictionary)
Namespace for OpenFOAM.
scalar integerRoot(const scalar x, const label e)
Compute the power of the number x to the reciprocal integer 1/e.
Definition: scalarI.H:55
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
scalar integerPow(const scalar x, const label e)
Compute the power of the number x to the integer e.
Definition: scalarI.H:30
dimensionedScalar log(const dimensionedScalar &ds)
const HashTable< unitConversion > & units()
Get the table of unit conversions.
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
dictionary dict
randomGenerator rndGen(653213)