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 template<class Type>
44 auto Foam::distributions::uniform::Phi(const Type& x, const label q)
45 {
46  if (q == -1)
47  {
48  return log(x);
49  }
50  else
51  {
52  return integerPow(x, 1 + q)/(1 + q);
53  }
54 }
55 
56 
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58 
60 (
61  const unitConversion& units,
62  const dictionary& dict,
63  const label sampleQ,
65 )
66 :
68  (
69  typeName,
70  units,
71  dict,
72  sampleQ,
73  std::move(rndGen)
74  ),
75  min_(dict.lookupBackwardsCompatible<scalar>({"min", "minValue"}, units)),
76  max_(dict.lookupBackwardsCompatible<scalar>({"max", "maxValue"}, units)),
77  Phi0_(Phi(min_, q())),
78  Phi1_(Phi(max_, q()))
79 {
80  validateBounds(dict);
81  if (q() != 0) validatePositive(dict);
82 }
83 
84 
86 :
88  min_(d.min_),
89  max_(d.max_),
90  Phi0_(Phi(min_, q())),
91  Phi1_(Phi(max_, q()))
92 {}
93 
94 
95 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
96 
98 {}
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
104 {
105  const scalar s = rndGen_.sample01<scalar>();
106 
107  if (q() == -1)
108  {
109  return min_*pow(max_/min_, s);
110  }
111  else
112  {
113  const scalar PhiS = (1 - s)*Phi0_ + s*Phi1_;
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  return (Phi(max_, q() + 1) - Phi(min_, q() + 1))/(Phi1_ - Phi0_);
134 }
135 
136 
139 (
140  const scalarField& x,
141  const label e,
142  const bool
143 ) const
144 {
145  const scalarField xClip(Foam::min(Foam::max(x, min()), max()));
146  return (Phi(xClip, q() + e) - Phi(min_, q() + e))/(Phi1_ - Phi0_);
147 }
148 
149 
151 (
152  Ostream& os,
153  const unitConversion& units
154 ) const
155 {
157 
158  writeEntry(os, "min", units, min_);
159  writeEntry(os, "max", units, max_);
160 }
161 
162 
165 {
166  if (q() == -1)
167  {
168  return clipPDF(x, 1/x/log(max_/min_));
169  }
170  else
171  {
172  return clipPDF(x, integerPow(x, q())/(Phi1_ - Phi0_));
173  }
174 }
175 
176 
177 // ************************************************************************* //
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
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 void write(Ostream &os, const unitConversion &units) const
Write to a stream.
Definition: distribution.C:153
Distribution in which all values between a specified minimum and maximum have the same probability.
Definition: uniform.H:68
virtual tmp< scalarField > integralPDFxPow(const scalarField &x, const label e, const bool consistent=false) const
Return the integral of the PDF multiplied by an integer power of x.
Definition: uniform.C:139
virtual scalar min() const
Return the minimum value.
Definition: uniform.C:119
virtual scalar sample() const
Sample the distribution.
Definition: uniform.C:103
uniform(const unitConversion &units, const dictionary &dict, const label sampleQ, randomGenerator &&rndGen)
Construct from a dictionary.
Definition: uniform.C:60
virtual tmp< scalarField > plotPDF(const scalarField &x) const
Return values to plot the probability density function.
Definition: uniform.C:164
virtual void write(Ostream &os, const unitConversion &units) const
Write to a stream.
Definition: uniform.C:151
virtual ~uniform()
Destructor.
Definition: uniform.C:97
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(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::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
const doubleScalar e
Definition: doubleScalar.H:106
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)
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
void pow(LagrangianPatchField< typename powProduct< Type, r >::type > &f, const LagrangianPatchField< Type > &f1)
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
dictionary dict
randomGenerator rndGen(653213)