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

exponential.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 
00028 #include "exponential.H"
00029 #include "addToRunTimeSelectionTable.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035     defineTypeNameAndDebug(exponential, 0);
00036     addToRunTimeSelectionTable(pdf, exponential, dictionary);
00037 }
00038 
00039 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00040 
00041 Foam::exponential::exponential(const dictionary& dict, Random& rndGen)
00042 :
00043     pdf(dict, rndGen),
00044     pdfDict_(dict.subDict(typeName + "PDF")),
00045     minValue_(readScalar(pdfDict_.lookup("minValue"))),
00046     maxValue_(readScalar(pdfDict_.lookup("maxValue"))),
00047     lambda_(pdfDict_.lookup("lambda")),
00048     ls_(lambda_),
00049     range_(maxValue_-minValue_)
00050 {
00051     if (minValue_<0)
00052     {
00053         FatalErrorIn
00054         (
00055             "exponential::exponential(const dictionary& dict)"
00056         ) << " minValue = " << minValue_ << ", it must be >0." << abort(FatalError);
00057     }
00058 
00059     scalar sMax = 0;
00060     label n = lambda_.size();
00061     for (label i=0; i<n; i++)
00062     {
00063         scalar s = lambda_[i]*exp(-lambda_[i]*minValue_);
00064         for (label j=0; j<n; j++)
00065         {
00066             if (i!=j)
00067             {
00068                 scalar y = lambda_[j]*exp(-lambda_[j]*minValue_);
00069                 s += y;
00070             }
00071         }
00072 
00073         sMax = max(sMax, s);
00074     }
00075 
00076     for(label i=0; i<n; i++)
00077     {
00078         ls_[i] /= sMax;
00079     }
00080 }
00081 
00082 
00083 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00084 
00085 Foam::exponential::~exponential()
00086 {}
00087 
00088 
00089 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00090 
00091 Foam::scalar Foam::exponential::sample() const
00092 {
00093     scalar y = 0;
00094     scalar x = 0;
00095     label n = lambda_.size();
00096     bool success = false;
00097 
00098     while (!success)
00099     {
00100         x = minValue_ + range_*rndGen_.scalar01();
00101         y = rndGen_.scalar01();
00102         scalar p = 0.0;
00103 
00104         for(label i=0; i<n; i++)
00105         {
00106             p += ls_[i]*exp(-lambda_[i]*x);
00107         }
00108 
00109         if (y<p)
00110         {
00111             success = true;
00112         }
00113     }
00114 
00115     return x;
00116 }
00117 
00118 
00119 Foam::scalar Foam::exponential::minValue() const
00120 {
00121     return minValue_;
00122 }
00123 
00124 
00125 Foam::scalar Foam::exponential::maxValue() const
00126 {
00127     return maxValue_;
00128 }
00129 
00130 
00131 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd