Geant4  10.00.p02
G4MTRandGamma.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id:$
28 //
29 #ifdef G4MULTITHREADED
30 
31 #include <cmath> // for log()
32 
33 #include "G4MTRandGamma.hh"
34 
36  if ( deleteEngine ) delete localEngine;
37 }
38 
39 G4double G4MTRandGamma::shoot( CLHEP::HepRandomEngine *anEngine, G4double k,
40  G4double lambda ) {
41  return genGamma( anEngine, k, lambda );
42 }
43 
45  CLHEP::HepRandomEngine *anEngine = G4MTHepRandom::getTheEngine();
46  return genGamma( anEngine, k, lambda );
47 }
48 
50  return genGamma( localEngine, k, lambda );
51 }
52 
53 void G4MTRandGamma::shootArray( const G4int size, G4double* vect,
55 {
56  G4int i;
57 
58  for (i=0; i<size; ++i)
59  vect[i] = shoot(k,lambda);
60 }
61 
62 void G4MTRandGamma::shootArray( CLHEP::HepRandomEngine* anEngine,
63  const G4int size, G4double* vect,
64  G4double k, G4double lambda )
65 {
66  G4int i;
67 
68  for (i=0; i<size; ++i)
69  vect[i] = shoot(anEngine,k,lambda);
70 }
71 
72 void G4MTRandGamma::fireArray( const G4int size, G4double* vect)
73 {
74  G4int i;
75 
76  for (i=0; i<size; ++i)
77  vect[i] = fire(defaultK,defaultLambda);
78 }
79 
80 void G4MTRandGamma::fireArray( const G4int size, G4double* vect,
81  G4double k, G4double lambda )
82 {
83  G4int i;
84 
85  for (i=0; i<size; ++i)
86  vect[i] = fire(k,lambda);
87 }
88 
89 G4double G4MTRandGamma::genGamma( CLHEP::HepRandomEngine *anEngine,
90  G4double a, G4double lambda ) {
91 /*************************************************************************
92  * Gamma Distribution - Rejection algorithm gs combined with *
93  * Acceptance complement method gd *
94  *************************************************************************/
95 
96 static G4ThreadLocal G4double aa = -1.0, aaa = -1.0, b, c, d, e, r, s, si, ss, q0,
97  q1 = 0.0416666664, q2 = 0.0208333723, q3 = 0.0079849875,
98  q4 = 0.0015746717, q5 = -0.0003349403, q6 = 0.0003340332,
99  q7 = 0.0006053049, q8 = -0.0004701849, q9 = 0.0001710320,
100  a1 = 0.333333333, a2 = -0.249999949, a3 = 0.199999867,
101  a4 =-0.166677482, a5 = 0.142873973, a6 =-0.124385581,
102  a7 = 0.110368310, a8 = -0.112750886, a9 = 0.104089866,
103  e1 = 1.000000000, e2 = 0.499999994, e3 = 0.166666848,
104  e4 = 0.041664508, e5 = 0.008345522, e6 = 0.001353826,
105  e7 = 0.000247453;
106 
107 G4double gds,p,q,t,sign_u,u,v,w,x;
108 G4double v1,v2,v12;
109 
110 // Check for invalid input values
111 
112  if( a <= 0.0 ) return (-1.0);
113  if( lambda <= 0.0 ) return (-1.0);
114 
115  if (a < 1.0)
116  { // CASE A: Acceptance rejection algorithm gs
117  b = 1.0 + 0.36788794412 * a; // Step 1
118  for(;;)
119  {
120  p = b * anEngine->flat();
121  if (p <= 1.0)
122  { // Step 2. Case gds <= 1
123  gds = std::exp(std::log(p) / a);
124  if (std::log(anEngine->flat()) <= -gds) return(gds/lambda);
125  }
126  else
127  { // Step 3. Case gds > 1
128  gds = - std::log ((b - p) / a);
129  if (std::log(anEngine->flat()) <= ((a - 1.0) * std::log(gds))) return(gds/lambda);
130  }
131  }
132  }
133  else
134  { // CASE B: Acceptance complement algorithm gd
135  if (a != aa)
136  { // Step 1. Preparations
137  aa = a;
138  ss = a - 0.5;
139  s = std::sqrt(ss);
140  d = 5.656854249 - 12.0 * s;
141  }
142  // Step 2. Normal deviate
143  do {
144  v1 = 2.0 * anEngine->flat() - 1.0;
145  v2 = 2.0 * anEngine->flat() - 1.0;
146  v12 = v1*v1 + v2*v2;
147  } while ( v12 > 1.0 );
148  t = v1*std::sqrt(-2.0*std::log(v12)/v12);
149  x = s + 0.5 * t;
150  gds = x * x;
151  if (t >= 0.0) return(gds/lambda); // Immediate acceptance
152 
153  u = anEngine->flat(); // Step 3. Uniform random number
154  if (d * u <= t * t * t) return(gds/lambda); // Squeeze acceptance
155 
156  if (a != aaa)
157  { // Step 4. Set-up for hat case
158  aaa = a;
159  r = 1.0 / a;
160  q0 = ((((((((q9 * r + q8) * r + q7) * r + q6) * r + q5) * r + q4) *
161  r + q3) * r + q2) * r + q1) * r;
162  if (a > 3.686)
163  {
164  if (a > 13.022)
165  {
166  b = 1.77;
167  si = 0.75;
168  c = 0.1515 / s;
169  }
170  else
171  {
172  b = 1.654 + 0.0076 * ss;
173  si = 1.68 / s + 0.275;
174  c = 0.062 / s + 0.024;
175  }
176  }
177  else
178  {
179  b = 0.463 + s - 0.178 * ss;
180  si = 1.235;
181  c = 0.195 / s - 0.079 + 0.016 * s;
182  }
183  }
184  if (x > 0.0) // Step 5. Calculation of q
185  {
186  v = t / (s + s); // Step 6.
187  if (std::fabs(v) > 0.25)
188  {
189  q = q0 - s * t + 0.25 * t * t + (ss + ss) * std::log(1.0 + v);
190  }
191  else
192  {
193  q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6) *
194  v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
195  } // Step 7. Quotient acceptance
196  if (std::log(1.0 - u) <= q) return(gds/lambda);
197  }
198 
199  for(;;)
200  { // Step 8. Double exponential deviate t
201  do
202  {
203  e = -std::log(anEngine->flat());
204  u = anEngine->flat();
205  u = u + u - 1.0;
206  sign_u = (u > 0)? 1.0 : -1.0;
207  t = b + (e * si) * sign_u;
208  }
209  while (t <= -0.71874483771719); // Step 9. Rejection of t
210  v = t / (s + s); // Step 10. New q(t)
211  if (std::fabs(v) > 0.25)
212  {
213  q = q0 - s * t + 0.25 * t * t + (ss + ss) * std::log(1.0 + v);
214  }
215  else
216  {
217  q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6) *
218  v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
219  }
220  if (q <= 0.0) continue; // Step 11.
221  if (q > 0.5)
222  {
223  w = std::exp(q) - 1.0;
224  }
225  else
226  {
227  w = ((((((e7 * q + e6) * q + e5) * q + e4) * q + e3) * q + e2) *
228  q + e1) * q;
229  } // Step 12. Hat acceptance
230  if ( c * u * sign_u <= w * std::exp(e - 0.5 * t * t))
231  {
232  x = s + 0.5 * t;
233  return(x*x/lambda);
234  }
235  }
236  }
237 }
238 
239 #endif
static CLHEP::HepRandomEngine * getTheEngine()
void fireArray(const G4int size, G4double *vect)
static const G4double a1
G4double fire()
static G4double shoot()
G4double defaultK
static const G4double e2
static const G4double a4
G4double a
Definition: TRTMaterials.hh:39
#define G4ThreadLocal
Definition: tls.hh:52
static const G4double e4
int G4int
Definition: G4Types.hh:78
CLHEP::HepRandomEngine * localEngine
static const double s
Definition: G4SIunits.hh:150
static const G4double a3
static const G4double e1
G4double defaultLambda
static void shootArray(const G4int size, G4double *vect, G4double k=1.0, G4double lambda=1.0)
virtual ~G4MTRandGamma()
static const G4double a5
double G4double
Definition: G4Types.hh:76
static const G4double e3
static const G4double a2
static G4double genGamma(CLHEP::HepRandomEngine *anEngine, G4double k, G4double lambda)