FreeFOAM The Cross-Platform CFD Toolkit
multiNormal.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) 2010-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 "multiNormal.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  namespace pdfs
34  {
35  defineTypeNameAndDebug(multiNormal, 0);
36  addToRunTimeSelectionTable(pdf, multiNormal, dictionary);
37  }
38 }
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 :
44  pdf(typeName, dict, rndGen),
45  minValue_(readScalar(pdfDict_.lookup("minValue"))),
46  maxValue_(readScalar(pdfDict_.lookup("maxValue"))),
47  range_(maxValue_ - minValue_),
48  expectation_(pdfDict_.lookup("expectation")),
49  variance_(pdfDict_.lookup("variance")),
50  strength_(pdfDict_.lookup("strength"))
51 {
52  check();
53 
54  scalar sMax = 0;
55  label n = strength_.size();
56  for (label i=0; i<n; i++)
57  {
58  scalar x = expectation_[i];
59  scalar s = strength_[i];
60  for (label j=0; j<n; j++)
61  {
62  if (i!=j)
63  {
64  scalar x2 = (x - expectation_[j])/variance_[j];
65  scalar y = exp(-0.5*x2*x2);
66  s += strength_[j]*y;
67  }
68  }
69 
70  sMax = max(sMax, s);
71  }
72 
73  for (label i=0; i<n; i++)
74  {
75  strength_[i] /= sMax;
76  }
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
81 
83 {}
84 
85 
86 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
87 
88 Foam::scalar Foam::pdfs::multiNormal::sample() const
89 {
90  scalar y = 0;
91  scalar x = 0;
92  label n = expectation_.size();
93  bool success = false;
94 
95  while (!success)
96  {
97  x = minValue_ + range_*rndGen_.scalar01();
98  y = rndGen_.scalar01();
99  scalar p = 0.0;
100 
101  for (label i=0; i<n; i++)
102  {
103  scalar nu = expectation_[i];
104  scalar sigma = variance_[i];
105  scalar s = strength_[i];
106  scalar v = (x - nu)/sigma;
107  p += s*exp(-0.5*v*v);
108  }
109 
110  if (y<p)
111  {
112  success = true;
113  }
114  }
115 
116  return x;
117 }
118 
119 
121 {
122  return minValue_;
123 }
124 
125 
127 {
128  return maxValue_;
129 }
130 
131 
132 // ************************************************************************* //