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-2019 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_(distributionModelDict_.template lookup<scalar>("minValue")),
50  maxValue_(distributionModelDict_.template lookup<scalar>("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  info();
83 }
84 
85 
87 :
89  minValue_(p.minValue_),
90  maxValue_(p.maxValue_),
91  range_(p.range_),
92  expectation_(p.expectation_),
93  variance_(p.variance_),
94  strength_(p.strength_)
95 {}
96 
97 
98 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
99 
101 {}
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
107 {
108  scalar y = 0;
109  scalar x = 0;
110  label n = expectation_.size();
111  bool success = false;
112 
113  while (!success)
114  {
115  x = minValue_ + range_*rndGen_.sample01<scalar>();
116  y = rndGen_.sample01<scalar>();
117  scalar p = 0.0;
118 
119  for (label i=0; i<n; i++)
120  {
121  scalar nu = expectation_[i];
122  scalar sigma = variance_[i];
123  scalar s = strength_[i];
124  scalar v = (x - nu)/sigma;
125  p += s*exp(-0.5*v*v);
126  }
127 
128  if (y<p)
129  {
130  success = true;
131  }
132  }
133 
134  return x;
135 }
136 
137 
139 {
140  return minValue_;
141 }
142 
143 
145 {
146  return maxValue_;
147 }
148 
149 
151 {
152  scalar mean = 0.0;
153  forAll(strength_, i)
154  {
155  mean += strength_[i]*expectation_[i];
156  }
157 
158  return mean;
159 }
160 
161 
162 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
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:156
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
multiNormal(const dictionary &dict, Random &rndGen)
Construct from components.
Definition: multiNormal.C:43
virtual scalar minValue() const
Return the minimum value.
Definition: multiNormal.C:138
Macros for easy insertion into run-time selection tables.
const dimensionedScalar sigma
Stefan-Boltzmann constant: default SI units: [W/m^2/K^4].
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:150
Random number generator.
Definition: Random.H:57
defineTypeNameAndDebug(exponential, 0)
bool success
virtual scalar maxValue() const
Return the maximum value.
Definition: multiNormal.C:144
A library of runtime-selectable distribution models.
virtual scalar sample() const
Sample the distributionModel.
Definition: multiNormal.C:106
label n
addToRunTimeSelectionTable(distributionModel, exponential, dictionary)
volScalarField & p
virtual ~multiNormal()
Destructor.
Definition: multiNormal.C:100
Namespace for OpenFOAM.