1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2010 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 * * * * * * * * * * * * * //
36 defineTypeNameAndDebug(normal, 0);
37 addToRunTimeSelectionTable(pdf, normal, dictionary);
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 Foam::pdfs::normal::normal(const dictionary& dict, Random& rndGen)
45 pdf(typeName, dict, rndGen),
46 minValue_(readScalar(pdfDict_.lookup("minValue"))),
47 maxValue_(readScalar(pdfDict_.lookup("maxValue"))),
48 expectation_(readScalar(pdfDict_.lookup("expectation"))),
49 variance_(readScalar(pdfDict_.lookup("variance"))),
54 FatalErrorIn("normal::normal(const dictionary&, Random&)")
55 << "Minimum value must be greater than zero. "
56 << "Supplied minValue = " << minValue_
60 if (maxValue_ < minValue_)
62 FatalErrorIn("normal::normal(const dictionary&, Random&)")
63 << "Maximum value is smaller than the minimum value:"
64 << " maxValue = " << maxValue_ << ", minValue = " << minValue_
70 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
72 Foam::pdfs::normal::~normal()
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
78 Foam::scalar Foam::pdfs::normal::sample() const
81 scalar a = erf((minValue_ - expectation_)/variance_);
82 scalar b = erf((maxValue_ - expectation_)/variance_);
84 scalar y = rndGen_.scalar01();
85 scalar x = erfInv(y*(b - a) + a)*variance_ + expectation_;
87 // Note: numerical approximation of the inverse function yields slight
90 x = min(max(x, minValue_), maxValue_);
96 Foam::scalar Foam::pdfs::normal::minValue() const
102 Foam::scalar Foam::pdfs::normal::maxValue() const
108 Foam::scalar Foam::pdfs::normal::erfInv(const scalar y) const
110 scalar k = 2.0/(mathematicalConstant::pi*a_) + 0.5*log(1.0 - y*y);
111 scalar h = log(1.0 - y*y)/a_;
112 scalar x = sqrt(-k + sqrt(k*k - h));
121 // ************************************************************************* //