FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

multiNormal.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 2010-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "multiNormal.H"
00027 #include <OpenFOAM/addToRunTimeSelectionTable.H>
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033     namespace pdfs
00034     {
00035         defineTypeNameAndDebug(multiNormal, 0);
00036         addToRunTimeSelectionTable(pdf, multiNormal, dictionary);
00037     }
00038 }
00039 
00040 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00041 
00042 Foam::pdfs::multiNormal::multiNormal(const dictionary& dict, Random& rndGen)
00043 :
00044     pdf(typeName, dict, rndGen),
00045     minValue_(readScalar(pdfDict_.lookup("minValue"))),
00046     maxValue_(readScalar(pdfDict_.lookup("maxValue"))),
00047     range_(maxValue_ - minValue_),
00048     expectation_(pdfDict_.lookup("expectation")),
00049     variance_(pdfDict_.lookup("variance")),
00050     strength_(pdfDict_.lookup("strength"))
00051 {
00052     check();
00053 
00054     scalar sMax = 0;
00055     label n = strength_.size();
00056     for (label i=0; i<n; i++)
00057     {
00058         scalar x = expectation_[i];
00059         scalar s = strength_[i];
00060         for (label j=0; j<n; j++)
00061         {
00062             if (i!=j)
00063             {
00064                 scalar x2 = (x - expectation_[j])/variance_[j];
00065                 scalar y = exp(-0.5*x2*x2);
00066                 s += strength_[j]*y;
00067             }
00068         }
00069 
00070         sMax = max(sMax, s);
00071     }
00072 
00073     for (label i=0; i<n; i++)
00074     {
00075         strength_[i] /= sMax;
00076     }
00077 }
00078 
00079 
00080 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00081 
00082 Foam::pdfs::multiNormal::~multiNormal()
00083 {}
00084 
00085 
00086 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00087 
00088 Foam::scalar Foam::pdfs::multiNormal::sample() const
00089 {
00090     scalar y = 0;
00091     scalar x = 0;
00092     label n = expectation_.size();
00093     bool success = false;
00094 
00095     while (!success)
00096     {
00097         x = minValue_ + range_*rndGen_.scalar01();
00098         y = rndGen_.scalar01();
00099         scalar p = 0.0;
00100 
00101         for (label i=0; i<n; i++)
00102         {
00103             scalar nu = expectation_[i];
00104             scalar sigma = variance_[i];
00105             scalar s = strength_[i];
00106             scalar v = (x - nu)/sigma;
00107             p += s*exp(-0.5*v*v);
00108         }
00109 
00110         if (y<p)
00111         {
00112             success = true;
00113         }
00114     }
00115 
00116     return x;
00117 }
00118 
00119 
00120 Foam::scalar Foam::pdfs::multiNormal::minValue() const
00121 {
00122     return minValue_;
00123 }
00124 
00125 
00126 Foam::scalar Foam::pdfs::multiNormal::maxValue() const
00127 {
00128     return maxValue_;
00129 }
00130 
00131 
00132 // ************************************************************************* //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines