Geant4  10.00.p01
RandGaussZiggurat.cc
Go to the documentation of this file.
1 #include "CLHEP/Random/RandGaussZiggurat.h"
2 #include "CLHEP/Units/PhysicalConstants.h"
3 #include <iostream>
4 #include <cmath> // for std::log()
5 
6 namespace CLHEP {
7 
8 bool RandGaussZiggurat::ziggurat_is_init=RandGaussZiggurat::ziggurat_init();
9 //bool RandGaussZiggurat::ziggurat_is_init=false;
10 unsigned long RandGaussZiggurat::kn[128], RandGaussZiggurat::ke[256];
11 float RandGaussZiggurat::wn[128],RandGaussZiggurat::fn[128],RandGaussZiggurat::we[256],RandGaussZiggurat::fe[256];
12 
13 HepRandomEngine & RandGaussZiggurat::engine() {return RandGauss::engine();}
14 
15 RandGaussZiggurat::~RandGaussZiggurat() {
16 }
17 
18 std::string RandGaussZiggurat::name() const
19 {
20  return "RandGaussZiggurat";
21 }
22 
23 bool RandGaussZiggurat::ziggurat_init()
24 {
25  const double rzm1 = 2147483648.0, rzm2 = 4294967296.;
26  double dn=3.442619855899,tn=dn,vn=9.91256303526217e-3, q;
27  double de=7.697117470131487, te=de, ve=3.949659822581572e-3;
28  int i;
29 
30 /* Set up tables for RNOR */
31  q=vn/std::exp(-.5*dn*dn);
32  kn[0]=(unsigned long)((dn/q)*rzm1);
33  kn[1]=0;
34 
35  wn[0]=q/rzm1;
36  wn[127]=dn/rzm1;
37 
38  fn[0]=1.;
39  fn[127]=std::exp(-.5*dn*dn);
40 
41  for(i=126;i>=1;i--) {
42  dn=std::sqrt(-2.*std::log(vn/dn+std::exp(-.5*dn*dn)));
43  kn[i+1]=(unsigned long)((dn/tn)*rzm1);
44  tn=dn;
45  fn[i]=std::exp(-.5*dn*dn);
46  wn[i]=dn/rzm1;
47  }
48 
49 /* Set up tables for REXP */
50  q = ve/std::exp(-de);
51  ke[0]=(unsigned long)((de/q)*rzm2);
52  ke[1]=0;
53 
54  we[0]=q/rzm2;
55  we[255]=de/rzm2;
56 
57  fe[0]=1.;
58  fe[255]=std::exp(-de);
59 
60  for(i=254;i>=1;i--) {
61  de=-std::log(ve/de+std::exp(-de));
62  ke[i+1]= (unsigned long)((de/te)*rzm2);
63  te=de;
64  fe[i]=std::exp(-de);
65  we[i]=de/rzm2;
66  }
67  ziggurat_is_init=true;
68 
69  //std::cout<<"Done RandGaussZiggurat::ziggurat_init()"<<std::endl;
70 
71  return true;
72 }
73 
74 float RandGaussZiggurat::ziggurat_nfix(long hz,HepRandomEngine* anEngine)
75 {
76  const float r = 3.442620f; /* The start of the right tail */
77  float x, y;
78  unsigned long iz=hz&127;
79  for(;;)
80  {
81  x=hz*wn[iz]; /* iz==0, handles the base strip */
82  if(iz==0) {
83  do {
84  /* change to (1.0 - UNI) as argument to std::log(), because CLHEP generates [0,1),
85  while the original UNI generates (0,1] */
86  x=-std::log(1.0 - ziggurat_UNI(anEngine))*0.2904764; /* .2904764 is 1/r */
87  y=-std::log(1.0 - ziggurat_UNI(anEngine));
88  } while(y+y<x*x);
89  return (hz>0)? r+x : -r-x;
90  }
91  /* iz>0, handle the wedges of other strips */
92  if( fn[iz]+(1.0 - ziggurat_UNI(anEngine))*(fn[iz-1]-fn[iz]) < std::exp(-.5*x*x) ) return x;
93 
94  /* initiate, try to exit for(;;) for loop*/
95  hz=(signed)ziggurat_SHR3(anEngine);
96  iz=hz&127;
97  if((unsigned long)abs(hz)<kn[iz]) return (hz*wn[iz]);
98  }
99 }
100 
101 double RandGaussZiggurat::operator()() {
102  return ziggurat_RNOR(localEngine.get()) * defaultStdDev + defaultMean;
103 }
104 
105 double RandGaussZiggurat::operator()( double mean, double stdDev ) {
106  return ziggurat_RNOR(localEngine.get()) * stdDev + mean;
107 }
108 
109 void RandGaussZiggurat::shootArray( const int size, float* vect, float mean, float stdDev )
110 {
111  for (int i=0; i<size; ++i) {
112  vect[i] = shoot(mean,stdDev);
113  }
114 }
115 
116 void RandGaussZiggurat::shootArray( const int size, double* vect, double mean, double stdDev )
117 {
118  for (int i=0; i<size; ++i) {
119  vect[i] = shoot(mean,stdDev);
120  }
121 }
122 
123 void RandGaussZiggurat::shootArray( HepRandomEngine* anEngine, const int size, float* vect, float mean, float stdDev )
124 {
125  for (int i=0; i<size; ++i) {
126  vect[i] = shoot(anEngine,mean,stdDev);
127  }
128 }
129 
130 void RandGaussZiggurat::shootArray( HepRandomEngine* anEngine, const int size, double* vect, double mean, double stdDev )
131 {
132  for (int i=0; i<size; ++i) {
133  vect[i] = shoot(anEngine,mean,stdDev);
134  }
135 }
136 
137 void RandGaussZiggurat::fireArray( const int size, float* vect)
138 {
139  for (int i=0; i<size; ++i) {
140  vect[i] = fire( defaultMean, defaultStdDev );
141  }
142 }
143 
144 void RandGaussZiggurat::fireArray( const int size, double* vect)
145 {
146  for (int i=0; i<size; ++i) {
147  vect[i] = fire( defaultMean, defaultStdDev );
148  }
149 }
150 
151 void RandGaussZiggurat::fireArray( const int size, float* vect, float mean, float stdDev )
152 {
153  for (int i=0; i<size; ++i) {
154  vect[i] = fire( mean, stdDev );
155  }
156 }
157 
158 void RandGaussZiggurat::fireArray( const int size, double* vect, double mean, double stdDev )
159 {
160  for (int i=0; i<size; ++i) {
161  vect[i] = fire( mean, stdDev );
162  }
163 }
164 
165 std::ostream & RandGaussZiggurat::put ( std::ostream & os ) const {
166  int pr=os.precision(20);
167  os << " " << name() << "\n";
168  RandGauss::put(os);
169  os.precision(pr);
170  return os;
171 }
172 
173 std::istream & RandGaussZiggurat::get ( std::istream & is ) {
174  std::string inName;
175  is >> inName;
176  if (inName != name()) {
177  is.clear(std::ios::badbit | is.rdstate());
178  std::cerr << "Mismatch when expecting to read state of a "
179  << name() << " distribution\n"
180  << "Name found was " << inName
181  << "\nistream is left in the badbit state\n";
182  return is;
183  }
184  RandGauss::get(is);
185  return is;
186 }
187 
188 } // namespace CLHEP
ThreeVector shoot(const G4int Ap, const G4int Af)
G4String name
Definition: TRTMaterials.hh:40
static int engine(pchar, pchar, double &, pchar &, const dic_type &)
Definition: Evaluator.cc:358
G4double iz
Definition: TRTMaterials.hh:39
G4fissionEvent * fe