1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
27 #include "addToRunTimeSelectionTable.H"
28 #include "mathematicalConstants.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace distributionModels
36 defineTypeNameAndDebug(normal, 0);
37 addToRunTimeSelectionTable(distributionModel, normal, dictionary);
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 Foam::distributionModels::normal::normal
45 const dictionary& dict,
49 distributionModel(typeName, dict, rndGen),
50 minValue_(readScalar(distributionModelDict_.lookup("minValue"))),
51 maxValue_(readScalar(distributionModelDict_.lookup("maxValue"))),
52 expectation_(readScalar(distributionModelDict_.lookup("expectation"))),
53 variance_(readScalar(distributionModelDict_.lookup("variance"))),
58 FatalErrorIn("normal::normal(const dictionary&, Random&)")
59 << "Minimum value must be greater than zero. "
60 << "Supplied minValue = " << minValue_
64 if (maxValue_ < minValue_)
66 FatalErrorIn("normal::normal(const dictionary&, Random&)")
67 << "Maximum value is smaller than the minimum value:"
68 << " maxValue = " << maxValue_ << ", minValue = " << minValue_
74 Foam::distributionModels::normal::normal(const normal& p)
77 minValue_(p.minValue_),
78 maxValue_(p.maxValue_),
79 expectation_(p.expectation_),
80 variance_(p.variance_),
85 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
87 Foam::distributionModels::normal::~normal()
91 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 Foam::scalar Foam::distributionModels::normal::sample() const
96 scalar a = erf((minValue_ - expectation_)/variance_);
97 scalar b = erf((maxValue_ - expectation_)/variance_);
99 scalar y = rndGen_.sample01<scalar>();
100 scalar x = erfInv(y*(b - a) + a)*variance_ + expectation_;
102 // Note: numerical approximation of the inverse function yields slight
105 x = min(max(x, minValue_), maxValue_);
111 Foam::scalar Foam::distributionModels::normal::minValue() const
117 Foam::scalar Foam::distributionModels::normal::maxValue() const
123 Foam::scalar Foam::distributionModels::normal::erfInv(const scalar y) const
125 scalar k = 2.0/(constant::mathematical::pi*a_) + 0.5*log(1.0 - y*y);
126 scalar h = log(1.0 - y*y)/a_;
127 scalar x = sqrt(-k + sqrt(k*k - h));
136 // ************************************************************************* //