Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
RanluxEngine.cc
이 파일의 문서화 페이지로 가기
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- RanluxEngine ---
7 // class implementation file
8 // -----------------------------------------------------------------------
9 // This file is part of Geant4 (simulation toolkit for HEP).
10 //
11 // Ranlux random number generator originally implemented in FORTRAN77
12 // by Fred James as part of the MATHLIB HEP library.
13 // 'RanluxEngine' is designed to fit into the CLHEP random number
14 // class structure.
15 
16 // ===============================================================
17 // Adeyemi Adesanya - Created: 6th November 1995
18 // Gabriele Cosmo - Adapted & Revised: 22nd November 1995
19 // Adeyemi Adesanya - Added setSeeds() method: 2nd February 1996
20 // Gabriele Cosmo - Added flatArray() method: 8th February 1996
21 // - Minor corrections: 31st October 1996
22 // - Added methods for engine status: 19th November 1996
23 // - Fixed bug in setSeeds(): 15th September 1997
24 // J.Marraffino - Added stream operators and related constructor.
25 // Added automatic seed selection from seed table and
26 // engine counter: 14th Feb 1998
27 // - Fixed bug: setSeeds() requires a zero terminated
28 // array of seeds: 19th Feb 1998
29 // Ken Smith - Added conversion operators: 6th Aug 1998
30 // J. Marraffino - Remove dependence on hepString class 13 May 1999
31 // M. Fischler - In restore, checkFile for file not found 03 Dec 2004
32 // M. Fischler - Methods put, getfor instance save/restore 12/8/04
33 // M. Fischler - split get() into tag validation and
34 // getState() for anonymous restores 12/27/04
35 // M. Fischler - put/get for vectors of ulongs 3/14/05
36 // M. Fischler - State-saving using only ints, for portability 4/12/05
37 //
38 // ===============================================================
39 
40 #include "CLHEP/Random/Random.h"
44 
45 #include <string.h> // for strcmp
46 #include <cstdlib> // for std::abs(int)
47 
48 namespace CLHEP {
49 
50 namespace {
51  // Number of instances with automatic seed selection
52  CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
53 
54  // Maximum index into the seed table
55  const int maxIndex = 215;
56 }
57 
58 static const int MarkerLen = 64; // Enough room to hold a begin or end marker.
59 
60 std::string RanluxEngine::name() const {return "RanluxEngine";}
61 
64 {
65  long seedlist[2]={0,0};
66 
67  luxury = lux;
68  setSeed(seed, luxury);
69 
70  // setSeeds() wants a zero terminated array!
71  seedlist[0]=theSeed;
72  seedlist[1]=0;
73  setSeeds(seedlist, luxury);
74 }
75 
78 {
79  long seed;
80  long seedlist[2]={0,0};
81 
82  luxury = 3;
83  int numEngines = numberOfEngines++;
84  int cycle = std::abs(int(numEngines/maxIndex));
85  int curIndex = std::abs(int(numEngines%maxIndex));
86 
87  long mask = ((cycle & 0x007fffff) << 8);
88  HepRandom::getTheTableSeeds( seedlist, curIndex );
89  seed = seedlist[0]^mask;
90  setSeed(seed, luxury);
91 
92  // setSeeds() wants a zero terminated array!
93  seedlist[0]=theSeed;
94  seedlist[1]=0;
95  setSeeds(seedlist, luxury);
96 }
97 
98 RanluxEngine::RanluxEngine(int rowIndex, int colIndex, int lux)
100 {
101  long seed;
102  long seedlist[2]={0,0};
103 
104  luxury = lux;
105  int cycle = std::abs(int(rowIndex/maxIndex));
106  int row = std::abs(int(rowIndex%maxIndex));
107  int col = std::abs(int(colIndex%2));
108  long mask = (( cycle & 0x000007ff ) << 20 );
109  HepRandom::getTheTableSeeds( seedlist, row );
110  seed = ( seedlist[col] )^mask;
111  setSeed(seed, luxury);
112 
113  // setSeeds() wants a zero terminated array!
114  seedlist[0]=theSeed;
115  seedlist[1]=0;
116  setSeeds(seedlist, luxury);
117 }
118 
119 RanluxEngine::RanluxEngine( std::istream& is )
120 : HepRandomEngine()
121 {
122  is >> *this;
123 }
124 
126 
127 void RanluxEngine::setSeed(long seed, int lux) {
128 
129 // The initialisation is carried out using a Multiplicative
130 // Congruential generator using formula constants of L'Ecuyer
131 // as described in "A review of pseudorandom number generators"
132 // (Fred James) published in Computer Physics Communications 60 (1990)
133 // pages 329-344
134 
135  const int ecuyer_a = 53668;
136  const int ecuyer_b = 40014;
137  const int ecuyer_c = 12211;
138  const int ecuyer_d = 2147483563;
139 
140  const int lux_levels[5] = {0,24,73,199,365};
141 
142  long int_seed_table[24];
143  long next_seed = seed;
144  long k_multiple;
145  int i;
146 
147 // number of additional random numbers that need to be 'thrown away'
148 // every 24 numbers is set using luxury level variable.
149 
150  theSeed = seed;
151  if( (lux > 4)||(lux < 0) ){
152  if(lux >= 24){
153  nskip = lux - 24;
154  }else{
155  nskip = lux_levels[3]; // corresponds to default luxury level
156  }
157  }else{
158  luxury = lux;
159  nskip = lux_levels[luxury];
160  }
161 
162 
163  for(i = 0;i != 24;i++){
164  k_multiple = next_seed / ecuyer_a;
165  next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
166  - k_multiple * ecuyer_c ;
167  if(next_seed < 0)next_seed += ecuyer_d;
168  int_seed_table[i] = next_seed % int_modulus;
169  }
170 
171  for(i = 0;i != 24;i++)
172  float_seed_table[i] = int_seed_table[i] * mantissa_bit_24();
173 
174  i_lag = 23;
175  j_lag = 9;
176  carry = 0. ;
177 
178  if( float_seed_table[23] == 0. ) carry = mantissa_bit_24();
179 
180  count24 = 0;
181 }
182 
183 void RanluxEngine::setSeeds(const long *seeds, int lux) {
184 
185  const int ecuyer_a = 53668;
186  const int ecuyer_b = 40014;
187  const int ecuyer_c = 12211;
188  const int ecuyer_d = 2147483563;
189 
190  const int lux_levels[5] = {0,24,73,199,365};
191  int i;
192  long int_seed_table[24];
193  long k_multiple,next_seed;
194  const long *seedptr;
195 
196  theSeeds = seeds;
197  seedptr = seeds;
198 
199  if(seeds == 0){
200  setSeed(theSeed,lux);
201  theSeeds = &theSeed;
202  return;
203  }
204 
205  theSeed = *seeds;
206 
207 // number of additional random numbers that need to be 'thrown away'
208 // every 24 numbers is set using luxury level variable.
209 
210  if( (lux > 4)||(lux < 0) ){
211  if(lux >= 24){
212  nskip = lux - 24;
213  }else{
214  nskip = lux_levels[3]; // corresponds to default luxury level
215  }
216  }else{
217  luxury = lux;
218  nskip = lux_levels[luxury];
219  }
220 
221  for( i = 0;(i != 24)&&(*seedptr != 0);i++){
222  int_seed_table[i] = *seedptr % int_modulus;
223  seedptr++;
224  }
225 
226  if(i != 24){
227  next_seed = int_seed_table[i-1];
228  for(;i != 24;i++){
229  k_multiple = next_seed / ecuyer_a;
230  next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
231  - k_multiple * ecuyer_c ;
232  if(next_seed < 0)next_seed += ecuyer_d;
233  int_seed_table[i] = next_seed % int_modulus;
234  }
235  }
236 
237  for(i = 0;i != 24;i++)
238  float_seed_table[i] = int_seed_table[i] * mantissa_bit_24();
239 
240  i_lag = 23;
241  j_lag = 9;
242  carry = 0. ;
243 
244  if( float_seed_table[23] == 0. ) carry = mantissa_bit_24();
245 
246  count24 = 0;
247 }
248 
249 void RanluxEngine::saveStatus( const char filename[] ) const
250 {
251  std::ofstream outFile( filename, std::ios::out ) ;
252  if (!outFile.bad()) {
253  outFile << "Uvec\n";
254  std::vector<unsigned long> v = put();
255  for (unsigned int i=0; i<v.size(); ++i) {
256  outFile << v[i] << "\n";
257  }
258  }
259 }
260 
261 void RanluxEngine::restoreStatus( const char filename[] )
262 {
263  std::ifstream inFile( filename, std::ios::in);
264  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
265  std::cerr << " -- Engine state remains unchanged\n";
266  return;
267  }
268  if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
269  std::vector<unsigned long> v;
270  unsigned long xin;
271  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
272  inFile >> xin;
273  if (!inFile) {
274  inFile.clear(std::ios::badbit | inFile.rdstate());
275  std::cerr << "\nRanluxEngine state (vector) description improper."
276  << "\nrestoreStatus has failed."
277  << "\nInput stream is probably mispositioned now." << std::endl;
278  return;
279  }
280  v.push_back(xin);
281  }
282  getState(v);
283  return;
284  }
285 
286  if (!inFile.bad() && !inFile.eof()) {
287 // inFile >> theSeed; removed -- encompased by possibleKeywordInput
288  for (int i=0; i<24; ++i)
289  inFile >> float_seed_table[i];
290  inFile >> i_lag; inFile >> j_lag;
291  inFile >> carry; inFile >> count24;
292  inFile >> luxury; inFile >> nskip;
293  }
294 }
295 
297 {
298  std::cout << std::endl;
299  std::cout << "--------- Ranlux engine status ---------" << std::endl;
300  std::cout << " Initial seed = " << theSeed << std::endl;
301  std::cout << " float_seed_table[] = ";
302  for (int i=0; i<24; ++i)
303  std::cout << float_seed_table[i] << " ";
304  std::cout << std::endl;
305  std::cout << " i_lag = " << i_lag << ", j_lag = " << j_lag << std::endl;
306  std::cout << " carry = " << carry << ", count24 = " << count24 << std::endl;
307  std::cout << " luxury = " << luxury << " nskip = " << nskip << std::endl;
308  std::cout << "----------------------------------------" << std::endl;
309 }
310 
312 
313  float next_random;
314  float uni;
315  int i;
316 
318  if(uni < 0. ){
319  uni += 1.0;
320  carry = mantissa_bit_24();
321  }else{
322  carry = 0.;
323  }
324 
325  float_seed_table[i_lag] = uni;
326  i_lag --;
327  j_lag --;
328  if(i_lag < 0) i_lag = 23;
329  if(j_lag < 0) j_lag = 23;
330 
331  if( uni < mantissa_bit_12() ){
333  if( uni == 0) uni = mantissa_bit_24() * mantissa_bit_24();
334  }
335  next_random = uni;
336  count24 ++;
337 
338 // every 24th number generation, several random numbers are generated
339 // and wasted depending upon the luxury level.
340 
341  if(count24 == 24 ){
342  count24 = 0;
343  for( i = 0; i != nskip ; i++){
345  if(uni < 0. ){
346  uni += 1.0;
347  carry = mantissa_bit_24();
348  }else{
349  carry = 0.;
350  }
351  float_seed_table[i_lag] = uni;
352  i_lag --;
353  j_lag --;
354  if(i_lag < 0)i_lag = 23;
355  if(j_lag < 0) j_lag = 23;
356  }
357  }
358  return (double) next_random;
359 }
360 
361 void RanluxEngine::flatArray(const int size, double* vect)
362 {
363  float next_random;
364  float uni;
365  int i;
366  int index;
367 
368  for (index=0; index<size; ++index) {
370  if(uni < 0. ){
371  uni += 1.0;
372  carry = mantissa_bit_24();
373  }else{
374  carry = 0.;
375  }
376 
377  float_seed_table[i_lag] = uni;
378  i_lag --;
379  j_lag --;
380  if(i_lag < 0) i_lag = 23;
381  if(j_lag < 0) j_lag = 23;
382 
383  if( uni < mantissa_bit_12() ){
385  if( uni == 0) uni = mantissa_bit_24() * mantissa_bit_24();
386  }
387  next_random = uni;
388  vect[index] = (double)next_random;
389  count24 ++;
390 
391 // every 24th number generation, several random numbers are generated
392 // and wasted depending upon the luxury level.
393 
394  if(count24 == 24 ){
395  count24 = 0;
396  for( i = 0; i != nskip ; i++){
398  if(uni < 0. ){
399  uni += 1.0;
400  carry = mantissa_bit_24();
401  }else{
402  carry = 0.;
403  }
404  float_seed_table[i_lag] = uni;
405  i_lag --;
406  j_lag --;
407  if(i_lag < 0)i_lag = 23;
408  if(j_lag < 0) j_lag = 23;
409  }
410  }
411  }
412 }
413 
414 RanluxEngine::operator double() {
415  return flat();
416 }
417 
418 RanluxEngine::operator float() {
419  return float( flat() );
420 }
421 
422 RanluxEngine::operator unsigned int() {
423  return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff) |
424  (((unsigned int)(float_seed_table[i_lag]*exponent_bit_32())>>16) & 0xff);
425  // needed because Ranlux doesn't fill all bits of the double
426  // which therefore doesn't fill all bits of the integer.
427 }
428 
429 std::ostream & RanluxEngine::put ( std::ostream& os ) const
430 {
431  char beginMarker[] = "RanluxEngine-begin";
432  os << beginMarker << "\nUvec\n";
433  std::vector<unsigned long> v = put();
434  for (unsigned int i=0; i<v.size(); ++i) {
435  os << v[i] << "\n";
436  }
437  return os;
438 }
439 
440 std::vector<unsigned long> RanluxEngine::put () const {
441  std::vector<unsigned long> v;
442  v.push_back (engineIDulong<RanluxEngine>());
443  for (int i=0; i<24; ++i) {
444  v.push_back
445  (static_cast<unsigned long>(float_seed_table[i]/mantissa_bit_24()));
446  }
447  v.push_back(static_cast<unsigned long>(i_lag));
448  v.push_back(static_cast<unsigned long>(j_lag));
449  v.push_back(static_cast<unsigned long>(carry/mantissa_bit_24()));
450  v.push_back(static_cast<unsigned long>(count24));
451  v.push_back(static_cast<unsigned long>(luxury));
452  v.push_back(static_cast<unsigned long>(nskip));
453  return v;
454 }
455 
456 std::istream & RanluxEngine::get ( std::istream& is )
457 {
458  char beginMarker [MarkerLen];
459  is >> std::ws;
460  is.width(MarkerLen); // causes the next read to the char* to be <=
461  // that many bytes, INCLUDING A TERMINATION \0
462  // (Stroustrup, section 21.3.2)
463  is >> beginMarker;
464  if (strcmp(beginMarker,"RanluxEngine-begin")) {
465  is.clear(std::ios::badbit | is.rdstate());
466  std::cerr << "\nInput stream mispositioned or"
467  << "\nRanluxEngine state description missing or"
468  << "\nwrong engine type found." << std::endl;
469  return is;
470  }
471  return getState(is);
472 }
473 
474 std::string RanluxEngine::beginTag ( ) {
475  return "RanluxEngine-begin";
476 }
477 
478 std::istream & RanluxEngine::getState ( std::istream& is )
479 {
480  if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
481  std::vector<unsigned long> v;
482  unsigned long uu;
483  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
484  is >> uu;
485  if (!is) {
486  is.clear(std::ios::badbit | is.rdstate());
487  std::cerr << "\nRanluxEngine state (vector) description improper."
488  << "\ngetState() has failed."
489  << "\nInput stream is probably mispositioned now." << std::endl;
490  return is;
491  }
492  v.push_back(uu);
493  }
494  getState(v);
495  return (is);
496  }
497 
498 // is >> theSeed; Removed, encompassed by possibleKeywordInput()
499 
500  char endMarker [MarkerLen];
501  for (int i=0; i<24; ++i) {
502  is >> float_seed_table[i];
503  }
504  is >> i_lag; is >> j_lag;
505  is >> carry; is >> count24;
506  is >> luxury; is >> nskip;
507  is >> std::ws;
508  is.width(MarkerLen);
509  is >> endMarker;
510  if (strcmp(endMarker,"RanluxEngine-end")) {
511  is.clear(std::ios::badbit | is.rdstate());
512  std::cerr << "\nRanluxEngine state description incomplete."
513  << "\nInput stream is probably mispositioned now." << std::endl;
514  return is;
515  }
516  return is;
517 }
518 
519 bool RanluxEngine::get (const std::vector<unsigned long> & v) {
520  if ((v[0] & 0xffffffffUL) != engineIDulong<RanluxEngine>()) {
521  std::cerr <<
522  "\nRanluxEngine get:state vector has wrong ID word - state unchanged\n";
523  return false;
524  }
525  return getState(v);
526 }
527 
528 bool RanluxEngine::getState (const std::vector<unsigned long> & v) {
529  if (v.size() != VECTOR_STATE_SIZE ) {
530  std::cerr <<
531  "\nRanluxEngine get:state vector has wrong length - state unchanged\n";
532  return false;
533  }
534  for (int i=0; i<24; ++i) {
535  float_seed_table[i] = v[i+1]*mantissa_bit_24();
536  }
537  i_lag = v[25];
538  j_lag = v[26];
539  carry = v[27]*mantissa_bit_24();
540  count24 = v[28];
541  luxury = v[29];
542  nskip = v[30];
543  return true;
544 }
545 
546 } // namespace CLHEP
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
static std::string engineName()
Definition: RanluxEngine.h:104
void setSeed(long seed, int lux=3)
static const unsigned int VECTOR_STATE_SIZE
Definition: RanluxEngine.h:110
static ush mask[]
Definition: csz_inflate.cc:317
void showStatus() const
virtual std::istream & getState(std::istream &is)
void flatArray(const int size, double *vect)
float float_seed_table[24]
Definition: RanluxEngine.h:115
static double mantissa_bit_12()
static const int MarkerLen
Definition: DualRand.cc:67
static double mantissa_bit_24()
long seed
Definition: chem4.cc:68
Int_t col[ntarg]
Definition: Style.C:29
#define CLHEP_ATOMIC_INT_TYPE
Definition: atomic_int.h:20
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
static const int int_modulus
Definition: RanluxEngine.h:119
void setSeeds(const long *seeds, int lux=3)
virtual std::istream & get(std::istream &is)
std::string name() const
Definition: RanluxEngine.cc:60
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45
static constexpr double lux
ifstream in
Definition: comparison.C:7
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:251
std::vector< unsigned long > put() const
void saveStatus(const char filename[]="Ranlux.conf") const
static std::string beginTag()
void restoreStatus(const char filename[]="Ranlux.conf")
double flat()
Definition: G4AblaRandom.cc:48