12 #ifndef CD_RandomImplem_H
13 #define CD_RandomImplem_H
21 #include <ParmParse.H>
25 #include <CD_NamespaceHeader.H>
31 ParmParse pp(
"Random");
33 if (pp.contains(
"seed")) {
58 const int seed = a_seed + procID() * omp_get_num_threads() + omp_get_thread_num();
60 s_rng = std::mt19937_64(
seed);
63 const int seed = a_seed + procID();
65 s_rng = std::mt19937_64(
seed);
71 const int seed = a_seed + omp_get_thread_num();
73 s_rng = std::mt19937_64(
seed);
76 const int seed = a_seed;
78 s_rng = std::mt19937_64(
seed);
88 int seed = std::chrono::system_clock::now().time_since_epoch().count();
92 MPI_Bcast(&
seed, 1, MPI_INT, 0, Chombo_MPI::comm);
98 template <
typename T,
typename>
106 if (a_mean < 250.0) {
107 std::poisson_distribution<T> poisson(a_mean);
109 ret = poisson(s_rng);
112 std::normal_distribution<Real> normal(a_mean, sqrt(a_mean));
114 ret = (T)
std::max(normal(s_rng), (Real)0.0);
120 template <
typename T,
typename>
128 const bool useNormalApprox = (a_N > (T)9.0 * ((1.0 - a_p) / a_p)) && (a_N > (T)9.0 * a_p / (1.0 - a_p));
130 if (useNormalApprox) {
131 const Real mean = a_N * a_p;
133 std::normal_distribution<Real> normalDist(mean, mean * (1.0 - a_p));
135 ret = (T)
std::max(normalDist(s_rng), (Real)0.0);
138 std::binomial_distribution<T> binomDist(a_N, a_p);
140 ret = binomDist(s_rng);
151 return s_uniform01(s_rng);
159 return s_uniform11(s_rng);
167 return s_normal01(s_rng);
175 constexpr Real safety = 1.E-12;
180 Real r = x1 * x1 + x2 * x2;
181 while (r >= 1.0 || r < safety) {
184 r = x1 * x1 + x2 * x2;
187 return RealVect(x1, x2) / sqrt(r);
188 #elif CH_SPACEDIM == 3
191 Real r = x1 * x1 + x2 * x2;
192 while (r >= 1.0 || r < safety) {
195 r = x1 * x1 + x2 * x2;
198 const Real x = 2 * x1 * sqrt(1 - r);
199 const Real y = 2 * x2 * sqrt(1 - r);
200 const Real z = 1 - 2 * r;
202 return RealVect(x, y, z);
206 template <
typename T>
212 return a_distribution(s_rng);
215 template <
typename T>
221 return a_distribution(s_rng);
228 const RealVect a_bndryCentroid,
229 const RealVect a_bndryNormal,
231 const Real a_kappa) noexcept
244 pos = a_cellPos + pos * a_dx;
252 const RealVect a_bndryCentroid,
253 const RealVect a_bndryNormal) noexcept
258 bool valid = (pos - a_bndryCentroid).dotProduct(a_bndryNormal) >= 0.0;
263 valid = (pos - a_bndryCentroid).dotProduct(a_bndryNormal) >= 0.0;
272 RealVect pos = RealVect::Zero;
274 for (
int dir = 0; dir < SpaceDim; dir++) {
281 #include <CD_NamespaceFooter.H>
File containing some useful static methods related to random number generation.
static Real get(T &a_distribution)
For getting a random number from a user-supplied distribution. T must be a distribution for which we ...
Definition: CD_RandomImplem.H:208
static void setRandomSeed()
Set a random RNG seed.
Definition: CD_RandomImplem.H:86
static RealVect getDirection()
Get a random direction in space.
Definition: CD_RandomImplem.H:171
static Real getUniformReal11()
Get a uniform real number on the interval [-1,1].
Definition: CD_RandomImplem.H:155
static size_t getDiscrete(T &a_distribution)
For getting a random number from a user-supplied distribution. T must be a distribution for which we ...
Definition: CD_RandomImplem.H:217
static Real getUniformReal01()
Get a uniform real number on the interval [0,1].
Definition: CD_RandomImplem.H:147
static Real getNormal01()
Get a number from a normal distribution centered on zero and variance 1.
Definition: CD_RandomImplem.H:163
static void seed()
Seed the RNG.
Definition: CD_RandomImplem.H:28
static void setSeed(const int a_seed)
Set the RNG seed.
Definition: CD_RandomImplem.H:52
static T getPoisson(const Real a_mean)
Get Poisson distributed number.
Definition: CD_RandomImplem.H:100
static T getBinomial(const T a_N, const Real a_p) noexcept
Get Poisson distributed number.
Definition: CD_RandomImplem.H:122
static RealVect randomPosition(const RealVect a_lo, const RealVect a_hi) noexcept
Return a random position in the cube (a_lo, a_hi);.
Definition: CD_RandomImplem.H:270
Real max(const Real &a_input) noexcept
Get the maximum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition: CD_ParallelOpsImplem.H:176