multiNormal.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) 2011-2018 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 "multiNormal.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 namespace distributionModels
34 {
35  defineTypeNameAndDebug(multiNormal, 0);
36  addToRunTimeSelectionTable(distributionModel, multiNormal, dictionary);
37 }
38 }
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 (
44  const dictionary& dict,
45  Random& rndGen
46 )
47 :
48  distributionModel(typeName, dict, rndGen),
49  minValue_(readScalar(distributionModelDict_.lookup("minValue"))),
50  maxValue_(readScalar(distributionModelDict_.lookup("maxValue"))),
51  range_(maxValue_ - minValue_),
52  expectation_(distributionModelDict_.lookup("expectation")),
53  variance_(distributionModelDict_.lookup("variance")),
54  strength_(distributionModelDict_.lookup("strength"))
55 {
56  check();
57 
58  scalar sMax = 0;
59  label n = strength_.size();
60  for (label i=0; i<n; i++)
61  {
62  scalar x = expectation_[i];
63  scalar s = strength_[i];
64  for (label j=0; j<n; j++)
65  {
66  if (i!=j)
67  {
68  scalar x2 = (x - expectation_[j])/variance_[j];
69  scalar y = exp(-0.5*x2*x2);
70  s += strength_[j]*y;
71  }
72  }
73 
74  sMax = max(sMax, s);
75  }
76 
77  for (label i=0; i<n; i++)
78  {
79  strength_[i] /= sMax;
80  }
81 }
82 
83 
85 :
87  minValue_(p.minValue_),
88  maxValue_(p.maxValue_),
89  range_(p.range_),
90  expectation_(p.expectation_),
91  variance_(p.variance_),
92  strength_(p.strength_)
93 {}
94 
95 
96 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
97 
99 {}
100 
101 
102 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
103 
105 {
106  scalar y = 0;
107  scalar x = 0;
108  label n = expectation_.size();
109  bool success = false;
110 
111  while (!success)
112  {
113  x = minValue_ + range_*rndGen_.sample01<scalar>();
114  y = rndGen_.sample01<scalar>();
115  scalar p = 0.0;
116 
117  for (label i=0; i<n; i++)
118  {
119  scalar nu = expectation_[i];
120  scalar sigma = variance_[i];
121  scalar s = strength_[i];
122  scalar v = (x - nu)/sigma;
123  p += s*exp(-0.5*v*v);
124  }
125 
126  if (y<p)
127  {
128  success = true;
129  }
130  }
131 
132  return x;
133 }
134 
135 
137 {
138  return minValue_;
139 }
140 
141 
143 {
144  return maxValue_;
145 }
146 
147 
149 {
150  scalar mean = 0.0;
151  forAll(strength_, i)
152  {
153  mean += strength_[i]*expectation_[i];
154  }
155 
156  return mean;
157 }
158 
159 
160 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
A multiNormal distribution model.
Definition: multiNormal.H:55
Random & rndGen_
Reference to the random number generator.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
multiNormal(const dictionary &dict, Random &rndGen)
Construct from components.
Definition: multiNormal.C:43
const dimensionedScalar sigma
Stefan-Boltzmann constant: default SI units: [W/m^2/K^4].
virtual scalar minValue() const
Return the minimum value.
Definition: multiNormal.C:136
Macros for easy insertion into run-time selection tables.
scalar y
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Type sample01()
Advance the state and return a sample of a given type from a.
Definition: RandomI.H:70
dimensionedScalar exp(const dimensionedScalar &ds)
virtual scalar meanValue() const
Return the mean value.
Definition: multiNormal.C:148
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:68
Random number generator.
Definition: Random.H:57
defineTypeNameAndDebug(exponential, 0)
bool success
virtual scalar maxValue() const
Return the maximum value.
Definition: multiNormal.C:142
A library of runtime-selectable distribution models.
virtual scalar sample() const
Sample the distributionModel.
Definition: multiNormal.C:104
label n
addToRunTimeSelectionTable(distributionModel, exponential, dictionary)
volScalarField & p
virtual ~multiNormal()
Destructor.
Definition: multiNormal.C:98
volScalarField & nu
Namespace for OpenFOAM.