normal.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "normal.H"
00028 #include "addToRunTimeSelectionTable.H"
00029
00030
00031
00032 namespace Foam
00033 {
00034 defineTypeNameAndDebug(normal, 0);
00035 addToRunTimeSelectionTable(pdf, normal, dictionary);
00036 }
00037
00038
00039
00040 Foam::normal::normal(const dictionary& dict, Random& rndGen)
00041 :
00042 pdf(dict, rndGen),
00043 pdfDict_(dict.subDict(typeName + "PDF")),
00044 minValue_(readScalar(pdfDict_.lookup("minValue"))),
00045 maxValue_(readScalar(pdfDict_.lookup("maxValue"))),
00046 expectation_(pdfDict_.lookup("expectation")),
00047 variance_(pdfDict_.lookup("variance")),
00048 strength_(pdfDict_.lookup("strength")),
00049 range_(maxValue_-minValue_)
00050 {
00051 scalar sMax = 0;
00052 label n = strength_.size();
00053 for (label i=0; i<n; i++)
00054 {
00055 scalar x = expectation_[i];
00056 scalar s = strength_[i];
00057 for (label j=0; j<n; j++)
00058 {
00059 if (i!=j)
00060 {
00061 scalar x2 = (x-expectation_[j])/variance_[j];
00062 scalar y = exp(-0.5*x2*x2);
00063 s += strength_[j]*y;
00064 }
00065 }
00066
00067 sMax = max(sMax, s);
00068 }
00069
00070 for (label i=0; i<n; i++)
00071 {
00072 strength_[i] /= sMax;
00073 }
00074 }
00075
00076
00077
00078
00079 Foam::normal::~normal()
00080 {}
00081
00082
00083
00084
00085 Foam::scalar Foam::normal::sample() const
00086 {
00087 scalar y = 0;
00088 scalar x = 0;
00089 label n = expectation_.size();
00090 bool success = false;
00091
00092 while (!success)
00093 {
00094 x = minValue_ + range_*rndGen_.scalar01();
00095 y = rndGen_.scalar01();
00096 scalar p = 0.0;
00097
00098 for (label i=0; i<n; i++)
00099 {
00100 scalar nu = expectation_[i];
00101 scalar sigma = variance_[i];
00102 scalar s = strength_[i];
00103 scalar v = (x-nu)/sigma;
00104 p += s*exp(-0.5*v*v);
00105 }
00106
00107 if (y<p)
00108 {
00109 success = true;
00110 }
00111 }
00112
00113 return x;
00114 }
00115
00116
00117 Foam::scalar Foam::normal::minValue() const
00118 {
00119 return minValue_;
00120 }
00121
00122
00123 Foam::scalar Foam::normal::maxValue() const
00124 {
00125 return maxValue_;
00126 }
00127
00128
00129
Copyright © 2000-2009 OpenCFD Ltd