ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / lagrangian / distributionModels / normal / normal.C
blob627ea6e9eaf903d386d442b227c3cfab0407e119
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
26 #include "normal.H"
27 #include "addToRunTimeSelectionTable.H"
28 #include "mathematicalConstants.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 namespace Foam
34     namespace distributionModels
35     {
36         defineTypeNameAndDebug(normal, 0);
37         addToRunTimeSelectionTable(distributionModel, normal, dictionary);
38     }
41 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
43 Foam::distributionModels::normal::normal
45     const dictionary& dict,
46     cachedRandom& rndGen
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"))),
54     a_(0.147)
56     if (minValue_ < 0)
57     {
58         FatalErrorIn("normal::normal(const dictionary&, Random&)")
59             << "Minimum value must be greater than zero. "
60             << "Supplied minValue = " << minValue_
61             << abort(FatalError);
62     }
64     if (maxValue_ < minValue_)
65     {
66         FatalErrorIn("normal::normal(const dictionary&, Random&)")
67             << "Maximum value is smaller than the minimum value:"
68             << "    maxValue = " << maxValue_ << ", minValue = " << minValue_
69             << abort(FatalError);
70     }
74 Foam::distributionModels::normal::normal(const normal& p)
76     distributionModel(p),
77     minValue_(p.minValue_),
78     maxValue_(p.maxValue_),
79     expectation_(p.expectation_),
80     variance_(p.variance_),
81     a_(p.a_)
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
103     //       inaccuracies
105     x = min(max(x, minValue_), maxValue_);
107     return x;
111 Foam::scalar Foam::distributionModels::normal::minValue() const
113     return minValue_;
117 Foam::scalar Foam::distributionModels::normal::maxValue() const
119     return maxValue_;
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));
128     if (y < 0.0)
129     {
130         x *= -1.0;
131     }
132     return x;
136 // ************************************************************************* //