mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: cachedRandom - added 'shuffle' function to shuffle list inplace
BUG: cachedRandom - updated how generator is (re)initialied
This commit is contained in:
@ -66,19 +66,20 @@ Foam::cachedRandom::cachedRandom(const label seed, const label count)
|
|||||||
seed_ = seed;
|
seed_ = seed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialise samples
|
||||||
|
osRandomSeed(seed_);
|
||||||
|
|
||||||
// Samples will be cached if count > 0
|
// Samples will be cached if count > 0
|
||||||
if (count > 0)
|
if (count > 0)
|
||||||
{
|
{
|
||||||
samples_.setSize(count);
|
samples_.setSize(count);
|
||||||
sampleI_ = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialise samples
|
|
||||||
osRandomSeed(seed_);
|
|
||||||
forAll(samples_, i)
|
forAll(samples_, i)
|
||||||
{
|
{
|
||||||
samples_[i] = osRandomDouble();
|
samples_[i] = osRandomDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sampleI_ = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -90,24 +91,29 @@ Foam::cachedRandom::cachedRandom(const cachedRandom& cr, const bool reset)
|
|||||||
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 (sampleI_ == -1)
|
|
||||||
{
|
|
||||||
WarningInFunction
|
|
||||||
<< "Copy constructor called, but samples not being cached. "
|
|
||||||
<< "This may lead to non-repeatable behaviour" << endl;
|
|
||||||
|
|
||||||
osRandomSeed(seed_);
|
if (samples_.size())
|
||||||
}
|
|
||||||
|
|
||||||
if (reset && samples_.size())
|
|
||||||
{
|
{
|
||||||
sampleI_ = 0;
|
sampleI_ = 0;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Re-initialise the samples
|
||||||
|
osRandomSeed(seed_);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,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-2016 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
@ -143,6 +143,10 @@ public:
|
|||||||
template<class Type>
|
template<class Type>
|
||||||
void randomise01(Type& value);
|
void randomise01(Type& value);
|
||||||
|
|
||||||
|
//- Shuffle the values in the list
|
||||||
|
template<class Type>
|
||||||
|
void shuffle(UList<Type>& values);
|
||||||
|
|
||||||
|
|
||||||
// Global random numbers - consistent across all processors
|
// Global random numbers - consistent across all processors
|
||||||
|
|
||||||
@ -224,7 +228,7 @@ label cachedRandom::globalPosition<label>(const label& start, const label& end);
|
|||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
#ifdef NoRepository
|
#ifdef NoRepository
|
||||||
#include "cachedRandomTemplates.C"
|
# include "cachedRandomTemplates.C"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|||||||
@ -75,6 +75,23 @@ void Foam::cachedRandom::randomise01(Type& value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class Type>
|
||||||
|
void Foam::cachedRandom::shuffle(UList<Type>& values)
|
||||||
|
{
|
||||||
|
const label nSample = values.size();
|
||||||
|
label posI = nSample - 1;
|
||||||
|
|
||||||
|
for (label i = 1; i < nSample; i++)
|
||||||
|
{
|
||||||
|
label j = position<label>(0, posI);
|
||||||
|
Type t = values[j];
|
||||||
|
values[j] = values[posI];
|
||||||
|
values[posI] = t;
|
||||||
|
posI--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Type>
|
template<class Type>
|
||||||
Type Foam::cachedRandom::globalSample01()
|
Type Foam::cachedRandom::globalSample01()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user