normal.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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "normal.H"
31#include "MathFunctions.H"
33
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
37{
38namespace distributionModels
39{
42}
43}
44
45// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46
48(
49 const dictionary& dict,
51)
52:
53 distributionModel(typeName, dict, rndGen),
54 mu_
55 (
56 distributionModelDict_.getCompat<scalar>
57 (
58 "mu",
59 {{"expectation", 2106}}
60 )
61 ),
62 sigma_
63 (
64 distributionModelDict_.getCompat<scalar>
65 (
66 "sigma",
67 {{"variance", 2106}}
68 )
69 )
70{
71 if (mag(sigma_) == 0)
72 {
74 << "Standard deviation cannot be zero." << nl
75 << " sigma = " << sigma_ << nl
76 << exit(FatalError);
77 }
78
79 check();
80}
81
82
84:
86 mu_(p.mu_),
87 sigma_(p.sigma_)
88{}
89
90
91// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
92
94{
95 const scalar a = (minValue_ - mu_)/sigma_;
96 const scalar b = (maxValue_ - mu_)/sigma_;
97
98 const scalar aPhi = 0.5*(scalar(1) + erf(a/Foam::sqrt(scalar(2))));
99 const scalar bPhi = 0.5*(scalar(1) + erf(b/Foam::sqrt(scalar(2))));
100
101 const scalar u = rndGen_.sample01<scalar>();
102 const scalar p = u*(bPhi - aPhi) + aPhi;
103
104 // (B:p. 20-24)
105 const scalar x =
106 mu_ + sigma_*Foam::sqrt(scalar(2))*Math::erfInv(scalar(2)*p - scalar(1));
107
108 // Note: numerical approximation of the inverse function yields slight
109 // inaccuracies
110
111 return min(max(x, minValue_), maxValue_);
112}
113
114
116{
117 const scalar a = (minValue_ - mu_)/sigma_;
118 const scalar b = (maxValue_ - mu_)/sigma_;
119
120 // (B:p. 2)
121 const scalar aphi =
122 scalar(1)/Foam::sqrt(scalar(2)*constant::mathematical::pi)
123 *exp(-0.5*sqr(a));
124 const scalar bphi =
125 scalar(1)/Foam::sqrt(scalar(2)*constant::mathematical::pi)
126 *exp(-0.5*sqr(b));
127
128 // (B:p. 4)
129 const scalar aPhi = 0.5*(scalar(1) + erf(a/Foam::sqrt(scalar(2))));
130 const scalar bPhi = 0.5*(scalar(1) + erf(b/Foam::sqrt(scalar(2))));
131
132 // (B:p. 25)
133 return mu_ - sigma_*(bphi - aphi)/(bPhi - aPhi + VSMALL);
134}
135
136
137// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Random number generator.
Definition: Random.H:60
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A library of runtime-selectable doubly-truncated probability distribution models. Returns random samp...
Particle-size distribution model wherein random samples are drawn from the doubly-truncated univariat...
Definition: normal.H:251
virtual scalar meanValue() const
Return the theoretical mean of the distribution.
Definition: normal.C:115
virtual scalar sample() const
Sample the distribution.
Definition: normal.C:93
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
scalar erfInv(const scalar y)
Inverse error function of a real-number argument.
Definition: erfInv.C:36
constexpr scalar pi(M_PI)
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dimensionedScalar exp(const dimensionedScalar &ds)
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedScalar erf(const dimensionedScalar &ds)
static void check(const int retVal, const char *what)
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
volScalarField & b
Definition: createFields.H:27
Random rndGen
Definition: createFields.H:23