OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

normal.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) 1991-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "normal.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034     defineTypeNameAndDebug(normal, 0);
00035     addToRunTimeSelectionTable(pdf, normal, dictionary);
00036 }
00037 
00038 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00078 
00079 Foam::normal::~normal()
00080 {}
00081 
00082 
00083 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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