SPH
Rng.cpp
Go to the documentation of this file.
1 #include "math/rng/Rng.h"
2 
4 
6  : seed(seed) {
7  idum = seed;
8  std::fill(iv.begin(), iv.end(), 0);
9 }
10 
12  : seed(other.seed)
13  , iv(std::move(other.iv))
14  , iy(other.iy)
15  , idum2(other.idum2)
16  , idum(other.idum) {}
17 
19  const int ndiv = 1 + imm1 / ntab;
20 
21  if (idum < 0) {
22  idum = max(-idum, 1);
23  idum2 = idum;
24  for (int j = ntab + 8; j >= 1; --j) {
25  const int k = idum / iq1;
26  idum = ia1 * (idum - k * iq1) - k * ir1;
27  if (idum < 0) {
28  idum = idum + im1;
29  }
30  if (j < ntab) {
31  iv[j - 1] = idum;
32  }
33  }
34  iy = iv[0];
35  }
36  int k = idum / iq1;
37  idum = ia1 * (idum - k * iq1) - k * ir1;
38  if (idum < 0) {
39  idum = idum + im1;
40  }
41  k = idum2 / iq2;
42  idum2 = ia2 * (idum2 - k * iq2) - k * ir2;
43  if (idum2 < 0) {
44  idum2 = idum2 + im2;
45  }
46  const int j = iy / ndiv;
47  iy = iv[j] - idum2;
48  iv[j] = idum;
49  if (iy < 1) {
50  iy = iy + imm1;
51  }
52  return min(am * iy, rnmx);
53 }
54 
55 
56 INLINE Float radicalInverse(const int base, int i) {
57  Float p = Float(base);
58  int j = i;
59  Float inverse = 0._f;
60  Float a;
61  while (j > 0) {
62  a = Float(j % base);
63  inverse += a / p;
64  j = int(j / base);
65  p *= base;
66  }
67  return Float(inverse);
68 }
69 
71  : primes{ 2, 3, 5, 7, 11, 13 } {
72  std::fill(c.begin(), c.end(), 0);
73 }
74 
76  : primes(std::move(other.primes))
77  , c(std::move(other.c)) {}
78 
80  SPH_ASSERT(s < 6);
81  return radicalInverse(primes[s], c[s]++);
82 }
83 
84 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE Float radicalInverse(const int base, int i)
Definition: Rng.cpp:56
Random number generators.
Random number generator used in code SPH5 of Benz & Asphaug (1994).
Definition: Rng.h:43
BenzAsphaugRng(const int seed)
Definition: Rng.cpp:5
Float operator()(const int s=0)
Definition: Rng.cpp:18
Quasi-random number generator.
Definition: Rng.h:74
Float operator()(const int s)
Definition: Rng.cpp:79
HaltonQrng()
Definition: Rng.cpp:70
INLINE Iterator< StorageType > end()
Definition: StaticArray.h:210
INLINE Iterator< StorageType > begin()
Definition: StaticArray.h:198
Overload of std::swap for Sph::Array.
Definition: Array.h:578