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

general.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-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 "general.H"
00027 #include <OpenFOAM/addToRunTimeSelectionTable.H>
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033     namespace pdfs
00034     {
00035         defineTypeNameAndDebug(general, 0);
00036         addToRunTimeSelectionTable(pdf, general, dictionary);
00037     }
00038 }
00039 
00040 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00041 
00042 Foam::pdfs::general::general(const dictionary& dict, Random& rndGen)
00043 :
00044     pdf(typeName, dict, rndGen),
00045     xy_(pdfDict_.lookup("distribution")),
00046     nEntries_(xy_.size()),
00047     minValue_(xy_[0][0]),
00048     maxValue_(xy_[nEntries_-1][0]),
00049     integral_(nEntries_)
00050 {
00051     check();
00052 
00053     // normalize the cumulative pdf
00054 
00055     integral_[0] = 0.0;
00056     for (label i=1; i<nEntries_; i++)
00057     {
00058 
00059         scalar k = (xy_[i][1] - xy_[i-1][1])/(xy_[i][0] - xy_[i-1][0]);
00060         scalar d = xy_[i-1][1] - k*xy_[i-1][0];
00061         scalar y1 = xy_[i][0]*(0.5*k*xy_[i][0] + d);
00062         scalar y0 = xy_[i-1][0]*(0.5*k*xy_[i-1][0] + d);
00063         scalar area = y1 - y0;
00064 
00065         integral_[i] = area + integral_[i-1];
00066     }
00067 
00068     for (label i=0; i<nEntries_; i++)
00069     {
00070         xy_[i][1] /= integral_[nEntries_-1];
00071         integral_[i] /= integral_[nEntries_-1];
00072     }
00073 
00074 }
00075 
00076 
00077 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00078 
00079 Foam::pdfs::general::~general()
00080 {}
00081 
00082 
00083 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00084 
00085 Foam::scalar Foam::pdfs::general::sample() const
00086 {
00087     scalar y = rndGen_.scalar01();
00088 
00089     // find the interval where y is in the table
00090     label n=1;
00091     while (integral_[n] <= y)
00092     {
00093         n++;
00094     }
00095 
00096     scalar k = (xy_[n][1] - xy_[n-1][1])/(xy_[n][0] - xy_[n-1][0]);
00097     scalar d = xy_[n-1][1] - k*xy_[n-1][0];
00098 
00099     scalar alpha = y + xy_[n-1][0]*(0.5*k*xy_[n-1][0] + d) - integral_[n-1];
00100     scalar x = 0.0;
00101 
00102     // if k is small it is a linear equation, otherwise it is of second order
00103     if (mag(k) > SMALL)
00104     {
00105         scalar p = 2.0*d/k;
00106         scalar q = -2.0*alpha/k;
00107         scalar sqrtEr = sqrt(0.25*p*p - q);
00108 
00109         scalar x1 = -0.5*p + sqrtEr;
00110         scalar x2 = -0.5*p - sqrtEr;
00111         if ((x1 >= xy_[n-1][0]) && (x1 <= xy_[n][0]))
00112         {
00113             x = x1;
00114         }
00115         else
00116         {
00117             x = x2;
00118         }
00119     }
00120     else
00121     {
00122         x = alpha/d;
00123     }
00124 
00125     return x;
00126 }
00127 
00128 
00129 Foam::scalar Foam::pdfs::general::minValue() const
00130 {
00131     return minValue_;
00132 }
00133 
00134 
00135 Foam::scalar Foam::pdfs::general::maxValue() const
00136 {
00137     return maxValue_;
00138 }
00139 
00140 
00141 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines