mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: Random numbers - cachedRandom - updated to use the re-entrant random interface
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -31,88 +31,38 @@ License
|
|||||||
|
|
||||||
Foam::scalar Foam::cachedRandom::scalar01()
|
Foam::scalar Foam::cachedRandom::scalar01()
|
||||||
{
|
{
|
||||||
if (sampleI_ < 0)
|
return osRandomDouble(buffer_);
|
||||||
{
|
|
||||||
return osRandomDouble();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sampleI_ == samples_.size() - 1)
|
|
||||||
{
|
|
||||||
scalar s = samples_[sampleI_];
|
|
||||||
sampleI_ = 0;
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
scalar s = samples_[sampleI_];
|
|
||||||
sampleI_++;
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
Foam::cachedRandom::cachedRandom(const label seed, const label count)
|
Foam::cachedRandom::cachedRandom(const label seed)
|
||||||
:
|
:
|
||||||
seed_(1),
|
buffer_(osRandomBufferSize()),
|
||||||
samples_(0),
|
seed_(seed),
|
||||||
sampleI_(-1),
|
|
||||||
hasGaussSample_(false),
|
hasGaussSample_(false),
|
||||||
gaussSample_(0)
|
gaussSample_(0)
|
||||||
{
|
{
|
||||||
if (seed > 1)
|
// Initialise the random number generator
|
||||||
{
|
osRandomSeed(seed_, buffer_);
|
||||||
seed_ = seed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialise samples
|
|
||||||
osRandomSeed(seed_);
|
|
||||||
|
|
||||||
// Samples will be cached if count > 0
|
|
||||||
if (count > 0)
|
|
||||||
{
|
|
||||||
samples_.setSize(count);
|
|
||||||
forAll(samples_, i)
|
|
||||||
{
|
|
||||||
samples_[i] = osRandomDouble();
|
|
||||||
}
|
|
||||||
|
|
||||||
sampleI_ = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
|
Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
|
||||||
:
|
:
|
||||||
|
buffer_(cr.buffer_),
|
||||||
seed_(cr.seed_),
|
seed_(cr.seed_),
|
||||||
samples_(cr.samples_),
|
|
||||||
sampleI_(cr.sampleI_),
|
|
||||||
hasGaussSample_(cr.hasGaussSample_),
|
hasGaussSample_(cr.hasGaussSample_),
|
||||||
gaussSample_(cr.gaussSample_)
|
gaussSample_(cr.gaussSample_)
|
||||||
{
|
{
|
||||||
//if (sampleI_ == -1)
|
|
||||||
//{
|
|
||||||
// WarningInFunction
|
|
||||||
// << "Copy constructor called, but samples not being cached. "
|
|
||||||
// << "This may lead to non-repeatable behaviour" << endl;
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
|
||||||
if (reset)
|
if (reset)
|
||||||
{
|
{
|
||||||
hasGaussSample_ = false;
|
hasGaussSample_ = false;
|
||||||
gaussSample_ = 0;
|
gaussSample_ = 0;
|
||||||
|
|
||||||
if (samples_.size())
|
|
||||||
{
|
|
||||||
sampleI_ = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Re-initialise the samples
|
// Re-initialise the samples
|
||||||
osRandomSeed(seed_);
|
osRandomSeed(seed_, buffer_);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +75,13 @@ Foam::cachedRandom::~cachedRandom()
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
void Foam::cachedRandom::reset(const label seed)
|
||||||
|
{
|
||||||
|
seed_ = seed;
|
||||||
|
osRandomSeed(seed_, buffer_);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
Foam::scalar Foam::cachedRandom::sample01()
|
Foam::scalar Foam::cachedRandom::sample01()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -27,15 +27,6 @@ Class
|
|||||||
Description
|
Description
|
||||||
Random number generator.
|
Random number generator.
|
||||||
|
|
||||||
Pre-computes and caches samples on construction, so that when sample01()
|
|
||||||
is called, the function simply returns the next (pre-computed) sample. On
|
|
||||||
reaching the last sample, the sample sequence is repeated.
|
|
||||||
|
|
||||||
Constructed using a seed and sample count. If the supplied count is
|
|
||||||
negative, no caching is performed, and a new sample is generated on each
|
|
||||||
call to sample01().
|
|
||||||
|
|
||||||
Note: the copy constructor cannot be used if count = -1.
|
|
||||||
|
|
||||||
SourceFiles
|
SourceFiles
|
||||||
cachedRandomI.H
|
cachedRandomI.H
|
||||||
@ -65,15 +56,12 @@ class cachedRandom
|
|||||||
{
|
{
|
||||||
// Private data
|
// Private data
|
||||||
|
|
||||||
|
//- Buffer used by re-entrant random number generator
|
||||||
|
List<char> buffer_;
|
||||||
|
|
||||||
//- Initial random number seed
|
//- Initial random number seed
|
||||||
label seed_;
|
label seed_;
|
||||||
|
|
||||||
//- List of scalar samples
|
|
||||||
scalarList samples_;
|
|
||||||
|
|
||||||
//- Current sample marker
|
|
||||||
label sampleI_;
|
|
||||||
|
|
||||||
//- Indicator, which tells if there is a stored gaussian sample
|
//- Indicator, which tells if there is a stored gaussian sample
|
||||||
bool hasGaussSample_;
|
bool hasGaussSample_;
|
||||||
|
|
||||||
@ -92,7 +80,7 @@ public:
|
|||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
//- Construct given seed and sample count
|
//- Construct given seed and sample count
|
||||||
cachedRandom(const label seed, const label count);
|
cachedRandom(const label seed = 123456);
|
||||||
|
|
||||||
//- Copy constructor with optional reset of sampleI
|
//- Copy constructor with optional reset of sampleI
|
||||||
cachedRandom(const cachedRandom& cr, const bool reset = false);
|
cachedRandom(const cachedRandom& cr, const bool reset = false);
|
||||||
@ -109,18 +97,7 @@ public:
|
|||||||
//- Return const access to the initial random number seed
|
//- Return const access to the initial random number seed
|
||||||
inline label seed() const;
|
inline label seed() const;
|
||||||
|
|
||||||
//- Return const access to the list of samples
|
void reset(const label seed);
|
||||||
inline const scalarList& samples() const;
|
|
||||||
|
|
||||||
//- Return the current sample marker
|
|
||||||
inline label sampleI() const;
|
|
||||||
|
|
||||||
|
|
||||||
// Manipulation
|
|
||||||
|
|
||||||
//- Return non-const access to the sample marker
|
|
||||||
inline label& sampleI();
|
|
||||||
|
|
||||||
|
|
||||||
// Evaluation
|
// Evaluation
|
||||||
|
|
||||||
|
|||||||
@ -33,22 +33,4 @@ inline Foam::label Foam::cachedRandom::seed() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline const Foam::scalarList& Foam::cachedRandom::samples() const
|
|
||||||
{
|
|
||||||
return samples_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline Foam::label Foam::cachedRandom::sampleI() const
|
|
||||||
{
|
|
||||||
return sampleI_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline Foam::label& Foam::cachedRandom::sampleI()
|
|
||||||
{
|
|
||||||
return sampleI_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ************************************************************************* //
|
// ************************************************************************* //
|
||||||
|
|||||||
Reference in New Issue
Block a user