GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSKissEngine.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_RANDOMS_GGEMSKISSENGINE_HH
2 #define GUARD_GGEMS_RANDOMS_GGEMSKISSENGINE_HH
3 
4 // ************************************************************************
5 // * This file is part of GGEMS. *
6 // * *
7 // * GGEMS is free software: you can redistribute it and/or modify *
8 // * it under the terms of the GNU General Public License as published by *
9 // * the Free Software Foundation, either version 3 of the License, or *
10 // * (at your option) any later version. *
11 // * *
12 // * GGEMS is distributed in the hope that it will be useful, *
13 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 // * GNU General Public License for more details. *
16 // * *
17 // * You should have received a copy of the GNU General Public License *
18 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
19 // * *
20 // ************************************************************************
21 
34 #ifdef __OPENCL_C_VERSION__
35 
38 
42 
50 inline GGfloat KissUniform(global GGEMSRandom* random, GGint const index)
51 {
52  // y ^= (y<<5);
53  // y ^= (y>>7);
54  // y ^= (y<<22);
55  // t = z+w+c;
56  // z = w;
57  // c = t < 0;
58  // w = t & 2147483647;
59  // x += 1411392427;
60 
61  random->prng_state_2_[index] ^= (random->prng_state_2_[index] << 5);
62  random->prng_state_2_[index] ^= (random->prng_state_2_[index] >> 7);
63  random->prng_state_2_[index] ^= (random->prng_state_2_[index] << 22);
64 
65  GGint t = random->prng_state_3_[index] + random->prng_state_4_[index] + random->prng_state_5_[index];
66 
67  random->prng_state_3_[index] = random->prng_state_4_[index];
68  random->prng_state_5_[index] = t < 0;
69  random->prng_state_4_[index] = t & 2147483647;
70  random->prng_state_1_[index] += 1411392427;
71 
72  return ((GGfloat)(random->prng_state_1_[index] + random->prng_state_2_[index] + random->prng_state_4_[index])
73  // UINT_MAX 1.0 - float32_precision
74  / 4294967295.0) * (1.0f - 1.0f/(1<<23));
75 }
76 
80 
89 inline GGint KissPoisson(global GGEMSRandom* random, GGint const index, GGfloat const mean)
90 {
91  // Initialization of parameters
92  GGint number = 0;
93  GGfloat position = 0.0, poisson_value = 0.0, poisson_sum = 0.0;
94  GGfloat value = 0.0, y = 0.0, t = 0.0;
95 
96  if (mean <= 16.) {// border == 16, gaussian after 16
97  // to avoid 1 due to f32 approximation
98  do {
99  position = KissUniform(random, index);
100  }
101  while ((1.f-position) < 2.e-7f);
102 
103  poisson_value = exp(-mean);
104  poisson_sum = poisson_value;
105  // v---- Why ? It's not in G4Poisson - JB
106  while ((poisson_sum <= position) && (number < 40000.)) {
107  number++;
108  poisson_value *= mean/number;
109  // Not in G4, is it to manage f32 ? - JB
110  if ((poisson_sum + poisson_value) == poisson_sum) break;
111  poisson_sum += poisson_value;
112  }
113  return number;
114  }
115 
116  t = sqrt(-2.f*log(KissUniform(random, index)));
117  y = 2.f * PI * KissUniform(random, index);
118  t *= cos(y);
119  value = mean + t*sqrt(mean) + 0.5f;
120 
121  if (value <= 0.) return (GGuint)0;
122 
123  return (value >= 2.e9f) ? (GGint)2.e9f : (GGint)value;
124 }
125 
129 
138 inline GGfloat KissGauss(global GGEMSRandom* random, GGint const index, GGfloat const sigma)
139 {
140  // Box-Muller transformation
141  GGfloat u1 = KissUniform(random, index);
142  GGfloat u2 = KissUniform(random, index);
143  GGfloat r1 = sqrt(-2.0f * log(u1));
144  GGfloat r2 = 2.0f * PI * u2;
145 
146  return sigma * r1 * cos(r2);
147 }
148 
149 #endif
150 
151 #endif // End of GUARD_GGEMS_RANDOMS_GGEMSKISSENGINE_HH
GGEMSConstants.hh
Different namespaces storing constants useful for GGEMS.
PI
__constant GGfloat PI
Definition: GGEMSConstants.hh:38
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSRandom.hh
Structure storing the random buffers for both OpenCL and GGEMS.
GGEMSRandom_t
Structure storing informations about random.
Definition: GGEMSRandom.hh:42
GGuint
#define GGuint
Definition: GGEMSTypes.hh:231
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273