Skip to content
Snippets Groups Projects
Commit 690b7e4c authored by andy's avatar andy
Browse files

ENH: cachedRandom: added support for global random numbers

parent 9b1be279
Branches
Tags
No related merge requests found
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -25,6 +25,7 @@ License
#include "cachedRandom.H"
#include "OSspecific.H"
#include "PstreamReduceOps.H"
// * * * * * * * * * * * * * private Member Functions * * * * * * * * * * * //
......@@ -142,6 +143,78 @@ Foam::scalar Foam::cachedRandom::position
}
template<>
Foam::label Foam::cachedRandom::globalSample01()
{
scalar value = -GREAT;
if (Pstream::master())
{
value = scalar01();
}
reduce(value, maxOp<scalar>());
return round(value);
}
template<>
Foam::scalar Foam::cachedRandom::globalSample01()
{
scalar value = -GREAT;
if (Pstream::master())
{
value = scalar01();
}
reduce(value, maxOp<scalar>());
return value;
}
template<>
Foam::label Foam::cachedRandom::globalPosition
(
const label& start,
const label& end
)
{
label value = labelMin;
if (Pstream::master())
{
value = round(scalar01()*(end - start));
}
reduce(value, maxOp<label>());
return start + value;
}
template<>
Foam::scalar Foam::cachedRandom::globalPosition
(
const scalar& start,
const scalar& end
)
{
scalar value = -GREAT;
if (Pstream::master())
{
value = scalar01()*(end - start);
}
reduce(value, maxOp<scalar>());
return start + value;
}
void Foam::cachedRandom::operator=(const cachedRandom& cr)
{
seed_ = cr.seed_;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -121,17 +121,33 @@ public:
// Evaluation
//- Return a sample whose components lie in the range 0-1
template<class Type>
Type sample01();
// Random numbers
//- Return a sample whose components lie in the range 0-1
template<class Type>
Type sample01();
//- Return a sample between start and end
template<class Type>
Type position(const Type& start, const Type& end);
//- Return a sample between start and end
template<class Type>
Type position(const Type& start, const Type& end);
//- Randomise value in the range 0-1
template<class Type>
void randomise01(Type& value);
//- Randomise value in the range 0-1
template<class Type>
void randomise01(Type& value);
// Global random numbers - consistent across all processors
//- Return a sample whose components lie in the range 0-1
template<class Type>
Type globalSample01();
//- Return a sample between start and end
template<class Type>
Type globalPosition(const Type& start, const Type& end);
//- Randomise value in the range 0-1
template<class Type>
void globalRandomise01(Type& value);
// Operators
......@@ -160,6 +176,22 @@ scalar cachedRandom::position<scalar>
const scalar& end
);
template<>
label cachedRandom::globalSample01<label>();
template<>
scalar cachedRandom::globalSample01<scalar>();
template<>
label cachedRandom::globalPosition<label>(const label& start, const label& end);
template<>
scalar cachedRandom::globalPosition<scalar>
(
const scalar& start,
const scalar& end
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "cachedRandom.H"
#include "Pstream.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
......@@ -61,4 +62,50 @@ void Foam::cachedRandom::randomise01(Type& value)
}
template<class Type>
Type Foam::cachedRandom::globalSample01()
{
Type value = -GREAT*pTraits<Type>::one;
if (Pstream::master())
{
value = sample01<Type>();
}
reduce(value, maxOp<Type>());
return value;
}
template<class Type>
Type Foam::cachedRandom::globalPosition(const Type& start, const Type& end)
{
Type value = -GREAT*pTraits<Type>::one;
if (Pstream::master())
{
value = position<Type>(start, end);
}
reduce(value, maxOp<Type>());
return value;
}
template<class Type>
void Foam::cachedRandom::globalRandomise01(Type& value)
{
value = -GREAT*pTraits<Type>::one;
if (Pstream::master())
{
value = sample01<Type>();
}
reduce(value, maxOp<Type>());
}
// ************************************************************************* //
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment