initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / thermophysicalModels / pdfs / exponential / exponential.C
blob973161537fe4346ee36d18006e9ec052db817a58
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "exponential.H"
29 #include "addToRunTimeSelectionTable.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 namespace Foam
35     defineTypeNameAndDebug(exponential, 0);
36     addToRunTimeSelectionTable(pdf, exponential, dictionary);
39 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
41 Foam::exponential::exponential(const dictionary& dict, Random& rndGen)
43     pdf(dict, rndGen),
44     pdfDict_(dict.subDict(typeName + "PDF")),
45     minValue_(readScalar(pdfDict_.lookup("minValue"))),
46     maxValue_(readScalar(pdfDict_.lookup("maxValue"))),
47     lambda_(pdfDict_.lookup("lambda")),
48     ls_(lambda_),
49     range_(maxValue_-minValue_)
51     if (minValue_<0)
52     {
53         FatalErrorIn
54         (
55             "exponential::exponential(const dictionary& dict)"
56         ) << " minValue = " << minValue_ << ", it must be >0." << abort(FatalError);
57     }
59     scalar sMax = 0;
60     label n = lambda_.size();
61     for (label i=0; i<n; i++)
62     {
63         scalar s = lambda_[i]*exp(-lambda_[i]*minValue_);
64         for (label j=0; j<n; j++)
65         {
66             if (i!=j)
67             {
68                 scalar y = lambda_[j]*exp(-lambda_[j]*minValue_);
69                 s += y;
70             }
71         }
73         sMax = max(sMax, s);
74     }
76     for(label i=0; i<n; i++)
77     {
78         ls_[i] /= sMax;
79     }
83 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
85 Foam::exponential::~exponential()
89 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
91 Foam::scalar Foam::exponential::sample() const
93     scalar y = 0;
94     scalar x = 0;
95     label n = lambda_.size();
96     bool success = false;
98     while (!success)
99     {
100         x = minValue_ + range_*rndGen_.scalar01();
101         y = rndGen_.scalar01();
102         scalar p = 0.0;
104         for(label i=0; i<n; i++)
105         {
106             p += ls_[i]*exp(-lambda_[i]*x);
107         }
109         if (y<p)
110         {
111             success = true;
112         }
113     }
115     return x;
119 Foam::scalar Foam::exponential::minValue() const
121     return minValue_;
125 Foam::scalar Foam::exponential::maxValue() const
127     return maxValue_;
131 // ************************************************************************* //