FreeFOAM The Cross-Platform CFD Toolkit
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 | Copyright (C) 1991-2010 OpenCFD Ltd.
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 "normal.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  namespace pdfs
35  {
36  defineTypeNameAndDebug(normal, 0);
37  addToRunTimeSelectionTable(pdf, normal, dictionary);
38  }
39 }
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
44 :
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"))),
50  a_(0.147)
51 {
52  if (minValue_ < 0)
53  {
54  FatalErrorIn("normal::normal(const dictionary&, Random&)")
55  << "Minimum value must be greater than zero. "
56  << "Supplied minValue = " << minValue_
57  << abort(FatalError);
58  }
59 
60  if (maxValue_ < minValue_)
61  {
62  FatalErrorIn("normal::normal(const dictionary&, Random&)")
63  << "Maximum value is smaller than the minimum value:"
64  << " maxValue = " << maxValue_ << ", minValue = " << minValue_
65  << abort(FatalError);
66  }
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
71 
73 {}
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
78 Foam::scalar Foam::pdfs::normal::sample() const
79 {
80 
81  scalar a = erf((minValue_ - expectation_)/variance_);
82  scalar b = erf((maxValue_ - expectation_)/variance_);
83 
84  scalar y = rndGen_.scalar01();
85  scalar x = erfInv(y*(b - a) + a)*variance_ + expectation_;
86 
87  // Note: numerical approximation of the inverse function yields slight
88  // inaccuracies
89 
90  x = min(max(x, minValue_), maxValue_);
91 
92  return x;
93 }
94 
95 
96 Foam::scalar Foam::pdfs::normal::minValue() const
97 {
98  return minValue_;
99 }
100 
101 
102 Foam::scalar Foam::pdfs::normal::maxValue() const
103 {
104  return maxValue_;
105 }
106 
107 
108 Foam::scalar Foam::pdfs::normal::erfInv(const scalar y) const
109 {
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));
113  if (y < 0.0)
114  {
115  x *= -1.0;
116  }
117  return x;
118 }
119 
120 
121 // ************************ vim: set sw=4 sts=4 et: ************************ //