Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4INCLRandom.cc
이 파일의 문서화 페이지로 가기
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 // INCL++ intra-nuclear cascade model
27 // Alain Boudard, CEA-Saclay, France
28 // Joseph Cugnon, University of Liege, Belgium
29 // Jean-Christophe David, CEA-Saclay, France
30 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
31 // Sylvie Leray, CEA-Saclay, France
32 // Davide Mancusi, CEA-Saclay, France
33 //
34 #define INCLXX_IN_GEANT4_MODE 1
35 
36 #include "globals.hh"
37 
38 /*
39  * G4INCLRandom.cc
40  *
41  * \date 7 June 2009
42  * \author Pekka Kaitaniemi
43  */
44 
45 #include "G4INCLRandom.hh"
46 #include "G4INCLGlobals.hh"
47 // #include <cassert>
48 
49 #include "G4INCLRanecu.hh"
50 #include "G4INCLRanecu3.hh"
51 #include "G4INCLGeant4Random.hh"
52 #include "G4INCLLogger.hh"
53 
54 namespace G4INCL {
55 
56  namespace Random {
57 
58  namespace {
59 
60  G4ThreadLocal IRandomGenerator* theGenerator = NULL;
61 
62 #ifdef INCL_COUNT_RND_CALLS
63  G4ThreadLocal unsigned long long nCalls;
64 #endif
65 
66  G4ThreadLocal SeedVector *savedSeeds = NULL;
67 
68  G4ThreadLocal Adapter *theAdapter = NULL;
69 
70  }
71 
73  if(isInitialized()) {
74  INCL_ERROR("INCL random number generator already initialized." << '\n');
75  } else {
76 #ifdef INCL_COUNT_RND_CALLS
77  nCalls = 0;
78 #endif
79  theGenerator = aGenerator;
80  }
81  if(!theAdapter)
82  theAdapter = new Adapter();
83  }
84 
85  void setSeeds(const SeedVector &sv) {
86  theGenerator->setSeeds(sv);
87  }
88 
90  return theGenerator->getSeeds();
91  }
92 
94 #ifdef INCL_COUNT_RND_CALLS
95  nCalls++;
96 #endif
97  return theGenerator->flat();
98  }
99 
101  G4double r;
102  while( (r=shoot()) <= 0. ) /* Loop checking, 10.07.2015, D.Mancusi */
103  ;
104  return r;
105  }
106 
108  G4double r;
109  while( (r=shoot()) >= 1. ) /* Loop checking, 10.07.2015, D.Mancusi */
110  ;
111  return r;
112  }
113 
115  return G4RandGauss::shoot(0.,sigma);
116  }
117 
119  // generate a Gaussian random number with standard deviation sigma
120  // uses the flat() and flat0() methods
121  static G4ThreadLocal G4bool generated = false;
122  static G4ThreadLocal G4double u, v;
123 
124  if( !generated )
125  {
126  u = shoot0();
127  v = Math::twoPi*shoot();
128  generated = true;
129  return sigma*std::sqrt(-2*std::log(u))*std::cos(v);
130  }
131  else
132  {
133  generated = false;
134  return sigma*std::sqrt(-2*std::log(u))*std::sin(v);
135  }
136  }
137 
139 
140  const G4double ctheta = (1.-2.*shoot());
141  const G4double stheta = std::sqrt(1.-ctheta*ctheta);
142  const G4double phi = Math::twoPi*shoot();
143  return ThreeVector(
144  norm * stheta * std::cos(phi),
145  norm * stheta * std::sin(phi),
146  norm * ctheta);
147 
148  }
149 
151  return normVector( rmax*Math::pow13(shoot0()) );
152  }
153 
155  const G4double sigmax = sigma * Math::oneOverSqrtThree;
156  return ThreeVector(gauss(sigmax), gauss(sigmax), gauss(sigmax));
157  }
158 
159  std::pair<G4double,G4double> correlatedGaussian(const G4double corrCoeff, const G4double x0, const G4double sigma) {
160 // assert(corrCoeff<=1. && corrCoeff>=-1.);
161  G4double factor = 1.-corrCoeff*corrCoeff;
162  if(factor<=0.)
163  factor=0.;
164  const G4double x = gaussWithMemory(sigma) + x0;
165  const G4double y = corrCoeff * x + gaussWithMemory(sigma*std::sqrt(factor)) + x0;
166  return std::make_pair(x, y);
167  }
168 
169  std::pair<G4double,G4double> correlatedUniform(const G4double corrCoeff) {
170  std::pair<G4double,G4double> gaussians = correlatedGaussian(corrCoeff);
171  return std::make_pair(Math::gaussianCDF(gaussians.first), Math::gaussianCDF(gaussians.second));
172  }
173 
175  delete theGenerator;
176  theGenerator = NULL;
177  delete savedSeeds;
178  savedSeeds = NULL;
179  delete theAdapter;
180  theAdapter = NULL;
181  }
182 
184  if(theGenerator == 0) return false;
185  return true;
186  }
187 
188 #ifdef INCL_COUNT_RND_CALLS
189  unsigned long long getNumberOfCalls() {
191  return nCalls;
192  }
193 #endif
194 
195  void saveSeeds() {
196  if(!savedSeeds)
197  savedSeeds = new SeedVector;
198 
199  (*savedSeeds) = theGenerator->getSeeds();
200  }
201 
203  if(!savedSeeds)
204  savedSeeds = new SeedVector;
205 
206  return *savedSeeds;
207  }
208 
209  void initialize(Config const * const
210 #ifndef INCLXX_IN_GEANT4_MODE
211  theConfig
212 #endif
213  ) {
214 #ifdef INCLXX_IN_GEANT4_MODE
216 #else // INCLXX_IN_GEANT4_MODE
217  RNGType rng = theConfig->getRNGType();
218  if(rng == RanecuType)
219  setGenerator(new Ranecu(theConfig->getRandomSeeds()));
220  else if(rng == Ranecu3Type)
221  setGenerator(new Ranecu3(theConfig->getRandomSeeds()));
222  else
223  setGenerator(NULL);
224 #endif // INCLXX_IN_GEANT4_MODE
225  }
226 
228  return shootInteger(n);
229  }
230 
231  Adapter const &getAdapter() {
232  return *theAdapter;
233  }
234 
235  }
236 
237 }
Float_t x
Definition: compare.C:6
std::shared_ptr< HepRandom > theGenerator
Definition: Random.cc:72
T shootInteger(T n)
Definition: G4INCLRandom.hh:96
void saveSeeds()
Save the status of the random-number generator.
ThreeVector gaussVector(G4double sigma=1.)
Generate Gaussianly-distributed ThreeVectors.
G4double shoot0()
ThreeVector normVector(G4double norm=1.)
void initialize(Config const *const)
Initialize generator according to a Config object.
Float_t y
Definition: compare.C:6
SeedVector getSavedSeeds()
Get the saved status of the random-number generator.
#define G4ThreadLocal
Definition: tls.hh:69
void deleteGenerator()
void setSeeds(const SeedVector &sv)
Definition: G4INCLRandom.cc:85
Adapter const & getAdapter()
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
Extended Ranecu-type RNG class.
G4double shoot()
Definition: G4INCLRandom.cc:93
std::pair< G4double, G4double > correlatedGaussian(const G4double corrCoeff, const G4double x0=0., const G4double sigma=1.)
Generate pairs of correlated Gaussian random numbers.
SeedVector getSeeds()
Definition: G4INCLRandom.cc:89
void setGenerator(G4INCL::IRandomGenerator *aGenerator)
Definition: G4INCLRandom.cc:72
#define INCL_ERROR(x)
G4double pow13(G4double x)
G4int operator()(const G4int n) const
ThreeVector sphereVector(G4double rmax=1.)
ThreeVector shoot(const G4int Ap, const G4int Af)
const G4double twoPi
int G4int
Definition: G4Types.hh:78
Float_t norm
std::pair< G4double, G4double > correlatedUniform(const G4double corrCoeff)
Generate pairs of correlated uniform random numbers.
G4double shoot1()
G4double gaussianCDF(const G4double x)
Cumulative distribution function for Gaussian.
const G4double oneOverSqrtThree
Char_t n[5]
G4double gaussWithMemory(G4double sigma=1.)
G4bool isInitialized()
G4double gauss(G4double sigma=1.)
#define INCLXX_IN_GEANT4_MODE
Definition: G4INCLRandom.cc:34