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

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