From bece113345480a42845b5a37fdac7f034bb22bd7 Mon Sep 17 00:00:00 2001 From: andy <a.heather@opencfd.co.uk> Date: Wed, 20 Oct 2010 09:58:31 +0100 Subject: [PATCH] ENH: Updated pdfs to employ cachedRandom class --- .../pdfs/RosinRammler/RosinRammler.C | 19 ++++++++-- .../pdfs/RosinRammler/RosinRammler.H | 15 +++++--- .../pdfs/exponential/exponential.C | 17 ++++++++- .../pdfs/exponential/exponential.H | 15 +++++--- .../pdfs/fixedValue/fixedValue.C | 9 ++++- .../pdfs/fixedValue/fixedValue.H | 15 +++++--- .../pdfs/general/general.C | 15 +++++++- .../pdfs/general/general.H | 17 ++++++--- .../pdfs/multiNormal/multiNormal.C | 22 +++++++++-- .../pdfs/multiNormal/multiNormal.H | 15 +++++--- src/thermophysicalModels/pdfs/normal/normal.C | 15 +++++++- src/thermophysicalModels/pdfs/normal/normal.H | 17 ++++++--- src/thermophysicalModels/pdfs/pdf/pdf.C | 37 ++++++++++++++++++- src/thermophysicalModels/pdfs/pdf/pdf.H | 25 +++++++++---- src/thermophysicalModels/pdfs/pdf/pdfNew.C | 4 +- .../pdfs/uniform/uniform.C | 12 +++++- .../pdfs/uniform/uniform.H | 15 +++++--- 17 files changed, 222 insertions(+), 62 deletions(-) diff --git a/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.C b/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.C index f634d252640..ee4207b0788 100644 --- a/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.C +++ b/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.C @@ -39,7 +39,11 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::RosinRammler::RosinRammler(const dictionary& dict, Random& rndGen) +Foam::pdfs::RosinRammler::RosinRammler +( + const dictionary& dict, + cachedRandom& rndGen +) : pdf(typeName, dict, rndGen), minValue_(readScalar(pdfDict_.lookup("minValue"))), @@ -51,6 +55,16 @@ Foam::pdfs::RosinRammler::RosinRammler(const dictionary& dict, Random& rndGen) } +Foam::pdfs::RosinRammler::RosinRammler(const RosinRammler& p) +: + pdf(p), + minValue_(p.minValue_), + maxValue_(p.maxValue_), + d_(p.d_), + n_(p.n_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::RosinRammler::~RosinRammler() @@ -61,9 +75,8 @@ Foam::pdfs::RosinRammler::~RosinRammler() Foam::scalar Foam::pdfs::RosinRammler::sample() const { - scalar K = 1.0 - exp(-pow((maxValue_ - minValue_)/d_, n_)); - scalar y = rndGen_.scalar01(); + scalar y = rndGen_.sample01<scalar>(); scalar x = minValue_ + d_*::pow(-log(1.0 - y*K), 1.0/n_); return x; } diff --git a/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.H b/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.H index 4a5a3d379f8..9a37742340e 100644 --- a/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.H +++ b/src/thermophysicalModels/pdfs/RosinRammler/RosinRammler.H @@ -82,11 +82,16 @@ public: // Constructors //- Construct from components - RosinRammler - ( - const dictionary& dict, - Random& rndGen - ); + RosinRammler(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + RosinRammler(const RosinRammler& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new RosinRammler(*this)); + } //- Destructor diff --git a/src/thermophysicalModels/pdfs/exponential/exponential.C b/src/thermophysicalModels/pdfs/exponential/exponential.C index 6255ca18276..b4162a27a32 100644 --- a/src/thermophysicalModels/pdfs/exponential/exponential.C +++ b/src/thermophysicalModels/pdfs/exponential/exponential.C @@ -39,7 +39,11 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::exponential::exponential(const dictionary& dict, Random& rndGen) +Foam::pdfs::exponential::exponential +( + const dictionary& dict, + cachedRandom& rndGen +) : pdf(typeName, dict, rndGen), minValue_(readScalar(pdfDict_.lookup("minValue"))), @@ -50,6 +54,15 @@ Foam::pdfs::exponential::exponential(const dictionary& dict, Random& rndGen) } +Foam::pdfs::exponential::exponential(const exponential& p) +: + pdf(p), + minValue_(p.minValue_), + maxValue_(p.maxValue_), + lambda_(p.lambda_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::exponential::~exponential() @@ -60,7 +73,7 @@ Foam::pdfs::exponential::~exponential() Foam::scalar Foam::pdfs::exponential::sample() const { - scalar y = rndGen_.scalar01(); + scalar y = rndGen_.sample01<scalar>(); scalar K = exp(-lambda_*maxValue_) - exp(-lambda_*minValue_); return -(1.0/lambda_)*log(exp(-lambda_*minValue_) + y*K); } diff --git a/src/thermophysicalModels/pdfs/exponential/exponential.H b/src/thermophysicalModels/pdfs/exponential/exponential.H index 9a28455480e..655db25c1e9 100644 --- a/src/thermophysicalModels/pdfs/exponential/exponential.H +++ b/src/thermophysicalModels/pdfs/exponential/exponential.H @@ -75,11 +75,16 @@ public: // Constructors //- Construct from components - exponential - ( - const dictionary& dict, - Random& rndGen - ); + exponential(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + exponential(const exponential& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new exponential(*this)); + } //- Destructor diff --git a/src/thermophysicalModels/pdfs/fixedValue/fixedValue.C b/src/thermophysicalModels/pdfs/fixedValue/fixedValue.C index b8afd673036..5808c5dbb52 100644 --- a/src/thermophysicalModels/pdfs/fixedValue/fixedValue.C +++ b/src/thermophysicalModels/pdfs/fixedValue/fixedValue.C @@ -39,13 +39,20 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::fixedValue::fixedValue(const dictionary& dict, Random& rndGen) +Foam::pdfs::fixedValue::fixedValue(const dictionary& dict, cachedRandom& rndGen) : pdf(typeName, dict, rndGen), value_(readScalar(pdfDict_.lookup("value"))) {} +Foam::pdfs::fixedValue::fixedValue(const fixedValue& p) +: + pdf(p), + value_(p.value_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::fixedValue::~fixedValue() diff --git a/src/thermophysicalModels/pdfs/fixedValue/fixedValue.H b/src/thermophysicalModels/pdfs/fixedValue/fixedValue.H index 97fd3c4a4e2..c2905114286 100644 --- a/src/thermophysicalModels/pdfs/fixedValue/fixedValue.H +++ b/src/thermophysicalModels/pdfs/fixedValue/fixedValue.H @@ -66,11 +66,16 @@ public: // Constructors //- Construct from components - fixedValue - ( - const dictionary& dict, - Random& rndGen - ); + fixedValue(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + fixedValue(const fixedValue& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new fixedValue(*this)); + } //- Destructor diff --git a/src/thermophysicalModels/pdfs/general/general.C b/src/thermophysicalModels/pdfs/general/general.C index da674ee2ad6..73618928505 100644 --- a/src/thermophysicalModels/pdfs/general/general.C +++ b/src/thermophysicalModels/pdfs/general/general.C @@ -39,7 +39,7 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::general::general(const dictionary& dict, Random& rndGen) +Foam::pdfs::general::general(const dictionary& dict, cachedRandom& rndGen) : pdf(typeName, dict, rndGen), xy_(pdfDict_.lookup("distribution")), @@ -74,6 +74,17 @@ Foam::pdfs::general::general(const dictionary& dict, Random& rndGen) } +Foam::pdfs::general::general(const general& p) +: + pdf(p), + xy_(p.xy_), + nEntries_(p.nEntries_), + minValue_(p.minValue_), + maxValue_(p.maxValue_), + integral_(p.integral_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::general::~general() @@ -84,7 +95,7 @@ Foam::pdfs::general::~general() Foam::scalar Foam::pdfs::general::sample() const { - scalar y = rndGen_.scalar01(); + scalar y = rndGen_.sample01<scalar>(); // find the interval where y is in the table label n=1; diff --git a/src/thermophysicalModels/pdfs/general/general.H b/src/thermophysicalModels/pdfs/general/general.H index 5c178443a62..dcc81459eb5 100644 --- a/src/thermophysicalModels/pdfs/general/general.H +++ b/src/thermophysicalModels/pdfs/general/general.H @@ -36,6 +36,8 @@ SourceFiles #define general_H #include "pdf.H" +#include "Vector.H" +#include "VectorSpace.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -76,11 +78,16 @@ public: // Constructors //- Construct from components - general - ( - const dictionary& dict, - Random& rndGen - ); + general(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + general(const general& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new general(*this)); + } //- Destructor diff --git a/src/thermophysicalModels/pdfs/multiNormal/multiNormal.C b/src/thermophysicalModels/pdfs/multiNormal/multiNormal.C index 21b7c8fb2a1..554063bdbf2 100644 --- a/src/thermophysicalModels/pdfs/multiNormal/multiNormal.C +++ b/src/thermophysicalModels/pdfs/multiNormal/multiNormal.C @@ -39,7 +39,11 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::multiNormal::multiNormal(const dictionary& dict, Random& rndGen) +Foam::pdfs::multiNormal::multiNormal +( + const dictionary& dict, + cachedRandom& rndGen +) : pdf(typeName, dict, rndGen), minValue_(readScalar(pdfDict_.lookup("minValue"))), @@ -77,6 +81,18 @@ Foam::pdfs::multiNormal::multiNormal(const dictionary& dict, Random& rndGen) } +Foam::pdfs::multiNormal::multiNormal(const multiNormal& p) +: + pdf(p), + minValue_(p.minValue_), + maxValue_(p.maxValue_), + range_(p.range_), + expectation_(p.expectation_), + variance_(p.variance_), + strength_(p.strength_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::multiNormal::~multiNormal() @@ -94,8 +110,8 @@ Foam::scalar Foam::pdfs::multiNormal::sample() const while (!success) { - x = minValue_ + range_*rndGen_.scalar01(); - y = rndGen_.scalar01(); + x = minValue_ + range_*rndGen_.sample01<scalar>(); + y = rndGen_.sample01<scalar>(); scalar p = 0.0; for (label i=0; i<n; i++) diff --git a/src/thermophysicalModels/pdfs/multiNormal/multiNormal.H b/src/thermophysicalModels/pdfs/multiNormal/multiNormal.H index 70947604f3b..4b8a9dc731b 100644 --- a/src/thermophysicalModels/pdfs/multiNormal/multiNormal.H +++ b/src/thermophysicalModels/pdfs/multiNormal/multiNormal.H @@ -85,11 +85,16 @@ public: // Constructors //- Construct from components - multiNormal - ( - const dictionary& dict, - Random& rndGen - ); + multiNormal(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + multiNormal(const multiNormal& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new multiNormal(*this)); + } //- Destructor diff --git a/src/thermophysicalModels/pdfs/normal/normal.C b/src/thermophysicalModels/pdfs/normal/normal.C index ed2308cb5b1..56c4a0460d2 100644 --- a/src/thermophysicalModels/pdfs/normal/normal.C +++ b/src/thermophysicalModels/pdfs/normal/normal.C @@ -40,7 +40,7 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::normal::normal(const dictionary& dict, Random& rndGen) +Foam::pdfs::normal::normal(const dictionary& dict, cachedRandom& rndGen) : pdf(typeName, dict, rndGen), minValue_(readScalar(pdfDict_.lookup("minValue"))), @@ -67,6 +67,17 @@ Foam::pdfs::normal::normal(const dictionary& dict, Random& rndGen) } +Foam::pdfs::normal::normal(const normal& p) +: + pdf(p), + minValue_(p.minValue_), + maxValue_(p.maxValue_), + expectation_(p.expectation_), + variance_(p.variance_), + a_(p.a_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::normal::~normal() @@ -81,7 +92,7 @@ Foam::scalar Foam::pdfs::normal::sample() const scalar a = erf((minValue_ - expectation_)/variance_); scalar b = erf((maxValue_ - expectation_)/variance_); - scalar y = rndGen_.scalar01(); + scalar y = rndGen_.sample01<scalar>(); scalar x = erfInv(y*(b - a) + a)*variance_ + expectation_; // Note: numerical approximation of the inverse function yields slight diff --git a/src/thermophysicalModels/pdfs/normal/normal.H b/src/thermophysicalModels/pdfs/normal/normal.H index 44aae0f7b0b..e7dde44aa20 100644 --- a/src/thermophysicalModels/pdfs/normal/normal.H +++ b/src/thermophysicalModels/pdfs/normal/normal.H @@ -85,11 +85,16 @@ public: // Constructors //- Construct from components - normal - ( - const dictionary& dict, - Random& rndGen - ); + normal(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + normal(const normal& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new normal(*this)); + } //- Destructor @@ -107,7 +112,7 @@ public: //- Return the maximum value virtual scalar maxValue() const; - scalar erfInv(const scalar y) const; + virtual scalar erfInv(const scalar y) const; }; diff --git a/src/thermophysicalModels/pdfs/pdf/pdf.C b/src/thermophysicalModels/pdfs/pdf/pdf.C index 5d6e5693b7b..822311de7fb 100644 --- a/src/thermophysicalModels/pdfs/pdf/pdf.C +++ b/src/thermophysicalModels/pdfs/pdf/pdf.C @@ -61,17 +61,52 @@ void Foam::pdfs::pdf::check() const // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::pdf::pdf(const word& name, const dictionary& dict, Random& rndGen) +Foam::pdfs::pdf::pdf +( + const word& name, + const dictionary& dict, + cachedRandom& rndGen +) : pdfDict_(dict.subDict(name + "PDF")), rndGen_(rndGen) {} +Foam::pdfs::pdf::pdf(const pdf& p) +: + pdfDict_(p.pdfDict_), + rndGen_(p.rndGen_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::pdf::~pdf() {} +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::scalar Foam::pdfs::pdf::sample() const +{ + notImplemented("Foam::scalar Foam::pdfs::pdf::sample() const"); + return 0.0; +} + + +Foam::scalar Foam::pdfs::pdf::minValue() const +{ + notImplemented("Foam::scalar Foam::pdfs::pdf::minValue() const"); + return 0.0; +} + + +Foam::scalar Foam::pdfs::pdf::maxValue() const +{ + notImplemented("Foam::scalar Foam::pdfs::pdf::maxValue() const"); + return 0.0; +} + + // ************************************************************************* // diff --git a/src/thermophysicalModels/pdfs/pdf/pdf.H b/src/thermophysicalModels/pdfs/pdf/pdf.H index d6dbf8fb90f..ddf006c0726 100644 --- a/src/thermophysicalModels/pdfs/pdf/pdf.H +++ b/src/thermophysicalModels/pdfs/pdf/pdf.H @@ -54,7 +54,7 @@ SourceFiles #include "IOdictionary.H" #include "autoPtr.H" -#include "Random.H" +#include "cachedRandom.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -78,7 +78,7 @@ protected: const dictionary pdfDict_; //- Reference to the random number generator - Random& rndGen_; + cachedRandom& rndGen_; // Protected Member Functions @@ -101,7 +101,7 @@ public: dictionary, ( const dictionary& dict, - Random& rndGen + cachedRandom& rndGen ), (dict, rndGen) ); @@ -110,11 +110,20 @@ public: // Constructors //- Construct from dictionary - pdf(const word& name, const dictionary& dict, Random& rndGen); + pdf(const word& name, const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + pdf(const pdf& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new pdf(*this)); + } //- Selector - static autoPtr<pdf> New(const dictionary& dict, Random& rndGen); + static autoPtr<pdf> New(const dictionary& dict, cachedRandom& rndGen); //- Destructor @@ -124,13 +133,13 @@ public: // Member Functions //- Sample the pdf - virtual scalar sample() const = 0; + virtual scalar sample() const; //- Return the minimum value - virtual scalar minValue() const = 0; + virtual scalar minValue() const; //- Return the maximum value - virtual scalar maxValue() const = 0; + virtual scalar maxValue() const; }; diff --git a/src/thermophysicalModels/pdfs/pdf/pdfNew.C b/src/thermophysicalModels/pdfs/pdf/pdfNew.C index c4144ee5630..d903726ac7a 100644 --- a/src/thermophysicalModels/pdfs/pdf/pdfNew.C +++ b/src/thermophysicalModels/pdfs/pdf/pdfNew.C @@ -30,7 +30,7 @@ License Foam::autoPtr<Foam::pdfs::pdf> Foam::pdfs::pdf::New ( const dictionary& dict, - Random& rndGen + cachedRandom& rndGen ) { const word modelType(dict.lookup("pdfType")); @@ -42,7 +42,7 @@ Foam::autoPtr<Foam::pdfs::pdf> Foam::pdfs::pdf::New if (cstrIter == dictionaryConstructorTablePtr_->end()) { - FatalErrorIn("pdfs::pdf::New(const dictionary&, Random&)") + FatalErrorIn("pdfs::pdf::New(const dictionary&, cachedRandom&)") << "Unknown pdf type " << modelType << nl << nl << "Valid pdf types are:" << nl << dictionaryConstructorTablePtr_->sortedToc() diff --git a/src/thermophysicalModels/pdfs/uniform/uniform.C b/src/thermophysicalModels/pdfs/uniform/uniform.C index 1aa0e3d1453..3a5fa5d564d 100644 --- a/src/thermophysicalModels/pdfs/uniform/uniform.C +++ b/src/thermophysicalModels/pdfs/uniform/uniform.C @@ -39,7 +39,7 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::pdfs::uniform::uniform(const dictionary& dict, Random& rndGen) +Foam::pdfs::uniform::uniform(const dictionary& dict, cachedRandom& rndGen) : pdf(typeName, dict, rndGen), minValue_(readScalar(pdfDict_.lookup("minValue"))), @@ -50,6 +50,14 @@ Foam::pdfs::uniform::uniform(const dictionary& dict, Random& rndGen) } +Foam::pdfs::uniform::uniform(const uniform& p) +: + pdf(p), + minValue_(p.minValue_), + maxValue_(p.maxValue_) +{} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::pdfs::uniform::~uniform() @@ -60,7 +68,7 @@ Foam::pdfs::uniform::~uniform() Foam::scalar Foam::pdfs::uniform::sample() const { - return (minValue_ + rndGen_.scalar01()*range_); + return rndGen_.position<scalar>(minValue_, maxValue_); } diff --git a/src/thermophysicalModels/pdfs/uniform/uniform.H b/src/thermophysicalModels/pdfs/uniform/uniform.H index a4cc6e32c68..a56f24933af 100644 --- a/src/thermophysicalModels/pdfs/uniform/uniform.H +++ b/src/thermophysicalModels/pdfs/uniform/uniform.H @@ -73,11 +73,16 @@ public: // Constructors //- Construct from components - uniform - ( - const dictionary& dict, - Random& rndGen - ); + uniform(const dictionary& dict, cachedRandom& rndGen); + + //- Construct copy + uniform(const uniform& p); + + //- Construct and return a clone + virtual autoPtr<pdf> clone() const + { + return autoPtr<pdf>(new uniform(*this)); + } //- Destructor -- GitLab