standardNormal.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2024 OpenFOAM Foundation
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 "standardNormal.H"
27 #include "mathematicalConstants.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 namespace distributions
35 {
37 }
38 }
39 
40 
41 const Foam::scalar Foam::distributions::standardNormal::a_ = 0.147;
42 
43 
44 using namespace Foam::constant::mathematical;
45 
46 
47 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
48 
50 (
51  const scalarField& x
52 )
53 {
54  return sign(x)*sqrt(1 - exp(-sqr(x)*(4/pi + a_*sqr(x))/(1 + a_*sqr(x))));
55 }
56 
57 
59 {
60  const scalar l = log(1 - y*y), b = 2/(pi*a_) + l/2;
61  return sign(y)*sqrt(-b + sqrt(b*b - l/a_));
62 }
63 
64 
65 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
66 
68 :
70 {}
71 
72 
74 (
75  const randomGenerator::seed& s,
76  const bool global
77 )
78 :
80 {}
81 
82 
84 :
86 {}
87 
88 
89 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
90 
92 {}
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
98 {
99  static const scalar sqrt2 = sqrt(scalar(2));
100  const scalar s = rndGen_.sample01<scalar>();
101  return approxErfInv(2*s - 1)*sqrt2;
102 }
103 
104 
106 {
107  return -vGreat;
108 }
109 
110 
112 {
113  return vGreat;
114 }
115 
116 
118 {
119  return 0;
120 }
121 
122 
125 {
126  const scalar x0 = approxErfInv(1 - rootSmall);
127  const scalar x1 = approxErfInv(rootSmall - 1);
128 
129  tmp<scalarField> tResult(new scalarField(n));
130  scalarField& result = tResult.ref();
131 
132  forAll(result, i)
133  {
134  const scalar f = scalar(i)/(n - 1);
135  result[i] = (1 - f)*x0 + f*x1;
136  }
137 
138  return tResult;
139 }
140 
141 
143 (
144  const scalarField& x
145 ) const
146 {
147  static const scalar sqrt2Pi = sqrt(2*pi);
148  return exp(- sqr(x)/2)/sqrt2Pi;
149 }
150 
151 
152 // ************************************************************************* //
scalar y
label n
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
Base class for statistical distributions.
Definition: distribution.H:76
Standard normal distribution. Not selectable.
static tmp< scalarField > approxErf(const scalarField &x)
Approximate error function.
virtual scalar min() const
Return the minimum value.
virtual ~standardNormal()
Destructor.
virtual scalar sample() const
Sample the distribution.
virtual tmp< scalarField > PDF(const scalarField &x) const
Return the distribution probability density function.
virtual tmp< scalarField > x(const label n) const
Return coordinates to plot across the range of the distribution.
static scalar approxErfInv(const scalar y)
Approximate error function inverse.
standardNormal(randomGenerator &&rndGen)
Construct from a random generator.
virtual scalar max() const
Return the maximum value.
virtual scalar mean() const
Return the mean value.
Random number generator.
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
volScalarField & b
Definition: createFields.H:25
mathematical constants.
defineTypeNameAndDebug(exponential, 0)
Namespace for OpenFOAM.
dimensionedScalar exp(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
dimensionedScalar log(const dimensionedScalar &ds)
dimensionedScalar sqrt(const dimensionedScalar &ds)
labelList f(nPoints)
randomGenerator rndGen(653213)