OpenLB 1.7
Loading...
Searching...
No Matches
Public Member Functions | List of all members
olb::util::Randomizer< T, useStored > Class Template Reference

#include <random.h>

+ Collaboration diagram for olb::util::Randomizer< T, useStored >:

Public Member Functions

 Randomizer ()
 Constructor for (useStored=false)
 
 Randomizer (std::vector< T > sequence)
 Constructor with passed sequence.
 
 Randomizer (std::string filePathSequence, bool enforceStored=false)
 Constroctor with filePath to stored sequence.
 
template<typename O = T>
generate ()
 Generate scalar or vector filled with scalars.
 
generateScalarNormal (T avg, T stdDev)
 Generate scalar leading to normal distribution based on the Box-Muller approach.
 
generateScalarNormal (T avg, T stdDev, T cutoff)
 
void writeSequence (std::size_t numOfValues, std::string filePathSequence="./randomSequence.dat", int precision=5)
 Write sequence to file for later retrieval.
 

Detailed Description

template<typename T, bool useStored = false>
class olb::util::Randomizer< T, useStored >

Definition at line 40 of file random.h.

Constructor & Destructor Documentation

◆ Randomizer() [1/3]

template<typename T , bool useStored>
olb::util::Randomizer< T, useStored >::Randomizer ( )

Constructor for (useStored=false)

Definition at line 35 of file random.hh.

36{
37 static_assert(!useStored, "ERROR: no sequence provided");
38}

◆ Randomizer() [2/3]

template<typename T , bool useStored>
olb::util::Randomizer< T, useStored >::Randomizer ( std::vector< T > sequence)

Constructor with passed sequence.

Definition at line 41 of file random.hh.

42 : _storedSequence(sequence)
43{}

◆ Randomizer() [3/3]

template<typename T , bool useStored>
olb::util::Randomizer< T, useStored >::Randomizer ( std::string filePathSequence,
bool enforceStored = false )

Constroctor with filePath to stored sequence.

  • throws error, if file does not exist and enforceStored=true

Definition at line 47 of file random.hh.

49{
50 [[maybe_unused]] std::size_t count = 0;
51 int rank = singleton::mpi().getRank();
52 if (rank == 0){
53 //Check whether file exists and create, if not
54 std::ifstream filePre(filePathSequence.c_str(), std::ios::in);
55 if (!filePre.good()){
56 if (!enforceStored){
57 writeSequence(1000,filePathSequence.c_str());
58 } else {
59 throw std::runtime_error("ERROR: Sequence not provided for Randomizer!");
60 }
61 }
62 //Read actual file
63 std::ifstream file(filePathSequence.c_str(), std::ios::in);
64 std::string strVal;
65 while (std::getline(file, strVal))
66 {
67 _storedSequence.push_back(std::stod(strVal));
68 ++count;
69 }
70 }
71
72#ifdef PARALLEL_MODE_MPI
73 //Align count on all cores
74 singleton::mpi().bCast<std::size_t>( &count, 1 );
75
76 //Align vector size on all cores
77 if (rank != 0){
78 _storedSequence.resize(count);
79 }
80
81 //Align vector content on all cores
82 singleton::mpi().bCast( _storedSequence.data(), count );
83#endif
84}
void bCast(T *sendBuf, int sendCount, int root=0, MPI_Comm comm=MPI_COMM_WORLD)
Broadcast data from one processor to multiple processors.
int getRank() const
Returns the process ID.
void writeSequence(std::size_t numOfValues, std::string filePathSequence="./randomSequence.dat", int precision=5)
Write sequence to file for later retrieval.
Definition random.hh:125
MpiManager & mpi()

References olb::singleton::MpiManager::bCast(), olb::singleton::MpiManager::getRank(), and olb::singleton::mpi().

+ Here is the call graph for this function:

Member Function Documentation

◆ generate()

template<typename T , bool useStored>
template<typename O >
O olb::util::Randomizer< T, useStored >::generate ( )

Generate scalar or vector filled with scalars.

Definition at line 90 of file random.hh.

91{
92 if constexpr (std::is_arithmetic<O>::value) {
93 O output = generateScalar();
94 return output;
95 } else {
96 O output;
97 for(unsigned iDim=0; iDim<O::d; ++iDim){
98 output[iDim] = generateScalar();
99 }
100 return output;
101 }
102}
+ Here is the caller graph for this function:

◆ generateScalarNormal() [1/2]

template<typename T , bool useStored>
T olb::util::Randomizer< T, useStored >::generateScalarNormal ( T avg,
T stdDev )

Generate scalar leading to normal distribution based on the Box-Muller approach.

Definition at line 105 of file random.hh.

106{
107 T A = generateScalar();
108 T B = generateScalar();
109 T x = util::cos(2 * M_PI * A) * util::sqrt(-2 * util::log(B));
110 T output = avg + x * stdDev;
111 return output;
112}
#define M_PI
cpu::simd::Pack< T > sqrt(cpu::simd::Pack< T > value)
Definition pack.h:100
ADf< T, DIM > log(const ADf< T, DIM > &a)
Definition aDiff.h:475
ADf< T, DIM > cos(const ADf< T, DIM > &a)
Definition aDiff.h:578

References olb::util::cos(), olb::util::log(), M_PI, and olb::util::sqrt().

+ Here is the call graph for this function:

◆ generateScalarNormal() [2/2]

template<typename T , bool useStored>
T olb::util::Randomizer< T, useStored >::generateScalarNormal ( T avg,
T stdDev,
T cutoff )

Definition at line 115 of file random.hh.

116{
117 T scalar = generateScalarNormal( avg, stdDev );
118 if (scalar<(avg-cutoff) || scalar>(avg+cutoff)){
119 scalar = generateScalarNormal(avg, stdDev, cutoff );
120 }
121 return scalar;
122}
T generateScalarNormal(T avg, T stdDev)
Generate scalar leading to normal distribution based on the Box-Muller approach.
Definition random.hh:105

◆ writeSequence()

template<typename T , bool useStored>
void olb::util::Randomizer< T, useStored >::writeSequence ( std::size_t numOfValues,
std::string filePathSequence = "./randomSequence.dat",
int precision = 5 )

Write sequence to file for later retrieval.

Definition at line 125 of file random.hh.

128{
129 int rank = singleton::mpi().getRank();
130 if (rank == 0){
131 std::ofstream writer;
132 writer.open( filePathSequence );
133 writer << std::setprecision(precision);
134 for (std::size_t i=0; i<numOfValues; ++i){
135 writer << generateScalarRandom() << " " << std::endl;
136 }
137 writer.close();
138 }
139}

References olb::singleton::MpiManager::getRank(), and olb::singleton::mpi().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: