chombo-discharge
Loading...
Searching...
No Matches
CD_RandomImplem.H
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2026 SINTEF Energy Research
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
13#ifndef CD_RANDOMIMPLEM_H
14#define CD_RANDOMIMPLEM_H
15
16// Std includes
17#include <chrono>
18#ifdef _OPENMP
19#include <omp.h>
20#endif
21
22// Chombo includes
23#include <SPMD.H>
24#include <CH_Timer.H>
25#include <ParmParse.H>
26
27// Our includes
28#include <CD_Random.H>
29#include <CD_NamespaceHeader.H>
30
31inline void
33{
34 if (!s_seeded) {
35 ParmParse pp("Random");
36
37 if (pp.contains("seed")) {
38 int seed;
39 pp.get("seed", seed);
40 if (seed < 0) {
42 }
43 else {
45 }
46 }
47 else {
49 }
50
51 s_seeded = true;
52 }
53}
54
55inline void
57{
58#ifdef CH_MPI
59#ifdef _OPENMP
60#pragma omp parallel
61 {
63
64 s_rng = std::mt19937_64(seed);
65 }
66#else
67 const int seed = a_seed + procID();
68
69 s_rng = std::mt19937_64(seed);
70#endif
71#else
72#ifdef _OPENMP
73#pragma omp parallel
74 {
75 const int seed = a_seed + omp_get_thread_num();
76
77 s_rng = std::mt19937_64(seed);
78 }
79#else
80 const int seed = a_seed;
81
82 s_rng = std::mt19937_64(seed);
83#endif
84#endif
85
86 s_seeded = true;
87}
88
89inline void
91{
92 auto seed = static_cast<int>(std::chrono::system_clock::now().time_since_epoch().count());
93
94 // Special hook for MPI -- master rank broadcasts the seed and everyone increments by their processor ID.
95#ifdef CH_MPI
97#endif
98
100}
101
102template <typename T, typename>
103inline T
105{
106 CH_assert(s_seeded);
107
108 T ret = (T)0;
109
110 if (a_mean <= 0.0) {
111 return ret;
112 }
113
114 if (a_mean < 250.0) {
116
117 ret = poisson(s_rng);
118 }
119 else {
121
122 ret = (T)std::max(normal(s_rng), (Real)0.0);
123 }
124
125 return ret;
126}
127
128template <typename T, typename>
129inline T
130Random::getBinomial(const T a_N, const Real a_p) noexcept
131{
132
133 CH_assert(s_seeded);
134
135 T ret = 0;
136
137 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));
138
139 if (useNormalApprox) {
140 const Real mean = a_N * a_p;
141
143
144 ret = (T)std::max(normalDist(s_rng), (Real)0.0);
145 }
146 else {
148
149 ret = binomDist(s_rng);
150 }
151
152 return ret;
153}
154
155inline Real
157{
158 CH_assert(s_seeded);
159
160 return s_uniform01(s_rng);
161}
162
163inline Real
165{
166 CH_assert(s_seeded);
167
168 return s_uniform11(s_rng);
169}
170
171inline Real
173{
174 CH_assert(s_seeded);
175
176 return s_normal01(s_rng);
177}
178
179inline RealVect
181{
182 CH_assert(s_seeded);
183
184 constexpr Real safety = 1.E-12;
185
186#if CH_SPACEDIM == 2
187 Real x1 = 2.0;
188 Real x2 = 2.0;
189 Real r = x1 * x1 + x2 * x2;
190 while (r >= 1.0 || r < safety) {
193 r = x1 * x1 + x2 * x2;
194 }
195
196 return RealVect(x1, x2) / sqrt(r);
197#elif CH_SPACEDIM == 3
198 Real x1 = 2.0;
199 Real x2 = 2.0;
200 Real r = x1 * x1 + x2 * x2;
201 while (r >= 1.0 || r < safety) {
204 r = x1 * x1 + x2 * x2;
205 }
206
207 const Real x = 2 * x1 * sqrt(1 - r);
208 const Real y = 2 * x2 * sqrt(1 - r);
209 const Real z = 1 - 2 * r;
210
211 return RealVect(x, y, z);
212#endif
213}
214
215template <typename T>
216inline Real
218{
219 CH_assert(s_seeded);
220
221 return a_distribution(s_rng);
222}
223
224template <typename T>
225inline size_t
227{
228 CH_assert(s_seeded);
229
230 return a_distribution(s_rng);
231}
232
233inline RealVect
235 const RealVect& a_lo,
236 const RealVect& a_hi,
238 const RealVect& a_bndryNormal,
239 const Real a_dx,
240 const Real a_kappa) noexcept
241{
242
244
245 if (a_kappa < 1.0) { // Rejection sampling.
247 }
248 else { // Regular cell. Get a position.
250 }
251
252 // Convert from unit cell coordinates to physical coordinates.
253 pos = a_cellPos + pos * a_dx;
254
255 return pos;
256}
257
258inline RealVect
260 const RealVect& a_hi,
262 const RealVect& a_bndryNormal) noexcept
263{
264 constexpr int maxIter = 100;
265
267 bool valid = (pos - a_bndryCentroid).dotProduct(a_bndryNormal) >= 0.0;
268
269 for (int iter = 0; !valid && iter < maxIter; ++iter) {
271 valid = (pos - a_bndryCentroid).dotProduct(a_bndryNormal) >= 0.0;
272 }
273
274 // Fall back to the boundary centroid for degenerate cut-cells where the
275 // valid half-space and the bounding box do not overlap.
276 if (!valid) {
278 }
279
280 return pos;
281}
282
283inline RealVect
285{
286
288
289 for (int dir = 0; dir < SpaceDim; dir++) {
291 }
292
293 return pos;
294}
295
296#include <CD_NamespaceFooter.H>
297
298#endif
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:217
static void setRandomSeed()
Set a random RNG seed.
Definition CD_RandomImplem.H:90
static RealVect getDirection()
Get a random direction in space.
Definition CD_RandomImplem.H:180
static Real getUniformReal11()
Get a uniform real number on the interval [-1,1].
Definition CD_RandomImplem.H:164
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:226
static Real getUniformReal01()
Get a uniform real number on the interval [0,1].
Definition CD_RandomImplem.H:156
static Real getNormal01()
Get a number from a normal distribution centered on zero and variance 1.
Definition CD_RandomImplem.H:172
static void seed()
Seed the RNG.
Definition CD_RandomImplem.H:32
static void setSeed(const int a_seed)
Set the RNG seed.
Definition CD_RandomImplem.H:56
static T getPoisson(const Real a_mean)
Get Poisson distributed number.
Definition CD_RandomImplem.H:104
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:284
static T getBinomial(const T a_N, const Real a_p) noexcept
Get Poisson distributed number.
Definition CD_RandomImplem.H:130
Base class for a tracer particle solver. This solver can advance particles in a pre-defined velocity ...
Definition CD_TracerParticleSolver.H:38
TracerParticleSolver()
Default constructor.
Definition CD_TracerParticleSolverImplem.H:26