chombo-discharge
CD_Random.H
Go to the documentation of this file.
1 /* chombo-discharge
2  * Copyright © 2021 SINTEF Energy Research.
3  * Please refer to Copyright.txt and LICENSE in the chombo-discharge root directory.
4  */
5 
12 #ifndef CD_Random_H
13 #define CD_Random_H
14 
15 // Std includes
16 #include <random>
17 #include <memory>
18 #include <mutex>
19 #include <type_traits>
20 #include <omp.h>
21 
22 // Chombo includes
23 #include <REAL.H>
24 #include <RealVect.H>
25 
26 // Our includes
27 #include <CD_NamespaceHeader.H>
28 
35 class Random
36 {
37 public:
41  Random() = delete;
42 
47  Random(const Random& a_other) = delete;
48 
53  Random(const Random&& a_other) = delete;
54 
59  Random&
60  operator=(const Random& a_other) = delete;
61 
66  Random&
67  operator=(const Random&& a_other) = delete;
68 
73  template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
74  inline static T
75  getPoisson(const Real a_mean);
76 
82  template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
83  inline static T
84  getBinomial(const T a_N, const Real a_p) noexcept;
85 
89  inline static Real
91 
95  inline static Real
97 
101  inline static Real
102  getNormal01();
103 
108  inline static RealVect
109  getDirection();
110 
115  template <typename T>
116  inline static Real
117  get(T& a_distribution);
118 
123  template <typename T>
124  inline static size_t
125  getDiscrete(T& a_distribution);
126 
130  inline static void
131  seed();
132 
138  inline static void
139  setSeed(const int a_seed);
140 
144  inline static void
145  setRandomSeed();
146 
152  inline static RealVect
153  randomPosition(const RealVect a_lo, const RealVect a_hi) noexcept;
154 
167  inline static RealVect
168  randomPosition(const RealVect a_cellPos,
169  const RealVect a_lo,
170  const RealVect a_hi,
171  const RealVect a_bndryCentroid,
172  const RealVect a_normal,
173  const Real a_dx,
174  const Real a_kappa) noexcept;
175 
184  inline static RealVect
185  randomPosition(const RealVect a_lo,
186  const RealVect a_hi,
187  const RealVect a_bndryCentroid,
188  const RealVect a_normal) noexcept;
189 
190 private:
194  static bool s_seeded;
195 
199  static thread_local std::mt19937_64 s_rng;
200 
204  static thread_local std::uniform_real_distribution<Real> s_uniform01;
205 
209  static thread_local std::uniform_real_distribution<Real> s_uniform11;
210 
214  static thread_local std::normal_distribution<Real> s_normal01;
215 };
216 
217 #include <CD_NamespaceFooter.H>
218 
219 #include <CD_RandomImplem.H>
220 
221 #endif
Implementation of CD_Random.H.
Class for encapsulating random number generation. This class is MPI and OpenMP safe.
Definition: CD_Random.H:36
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
Random & operator=(const Random &&a_other)=delete
Disallowed move assignment.
static RealVect getDirection()
Get a random direction in space.
Definition: CD_RandomImplem.H:171
Random(const Random &a_other)=delete
Deleted copy constructor. Must be impossible because this is a singleton.
static Real getUniformReal11()
Get a uniform real number on the interval [-1,1].
Definition: CD_RandomImplem.H:155
Random(const Random &&a_other)=delete
Deleted copy constructor. Must be impossible because this is a singleton.
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
Random()=delete
Disallowed constructor.
Random & operator=(const Random &a_other)=delete
Disallowed copy assignment.
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