Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
JamesRandom.cc
이 파일의 문서화 페이지로 가기
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- HepJamesRandom ---
7 // class implementation file
8 // -----------------------------------------------------------------------
9 // This file is part of Geant4 (simulation toolkit for HEP).
10 //
11 // This algorithm implements the original universal random number generator
12 // as proposed by Marsaglia & Zaman in report FSU-SCRI-87-50 and coded
13 // in FORTRAN77 by Fred James as the RANMAR generator, part of the MATHLIB
14 // HEP library.
15 
16 // =======================================================================
17 // Gabriele Cosmo - Created: 5th September 1995
18 // - Fixed a bug in setSeed(): 26th February 1996
19 // - Minor corrections: 31st October 1996
20 // - Added methods for engine status: 19th November 1996
21 // - Fixed bug in setSeeds(): 15th September 1997
22 // J.Marraffino - Added stream operators and related constructor.
23 // Added automatic seed selection from seed table and
24 // engine counter: 16th Feb 1998
25 // Ken Smith - Added conversion operators: 6th Aug 1998
26 // J. Marraffino - Remove dependence on hepString class 13 May 1999
27 // V. Innocente - changed pointers to indices 3 may 2000
28 // M. Fischler - In restore, checkFile for file not found 03 Dec 2004
29 // M. Fischler - Methods for distrib. instacne save/restore 12/8/04
30 // M. Fischler - split get() into tag validation and
31 // getState() for anonymous restores 12/27/04
32 // M. Fischler - Enforcement that seeds be non-negative
33 // (lest the sequence be non-random) 2/14/05
34 // M. Fischler - put/get for vectors of ulongs 3/14/05
35 // M. Fischler - State-saving using only ints, for portability 4/12/05
36 //
37 // =======================================================================
38 
39 #include "CLHEP/Random/Random.h"
42 #include "CLHEP/Random/DoubConv.h"
44 
45 #include <string.h> // for strcmp
46 #include <cmath>
47 #include <cstdlib>
48 
49 namespace CLHEP {
50 
51 namespace {
52  // Number of instances with automatic seed selection
53  CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
54 
55  // Maximum index into the seed table
56  const int maxIndex = 215;
57 }
58 
59 static const int MarkerLen = 64; // Enough room to hold a begin or end marker.
60 
61 std::string HepJamesRandom::name() const {return "HepJamesRandom";}
62 
65 {
66  setSeed(seed,0);
67  setSeeds(&theSeed,0);
68 }
69 
70 HepJamesRandom::HepJamesRandom() // 15 Feb. 1998 JMM
72 {
73  long seeds[2];
74  long seed;
75 
76  int numEngines = numberOfEngines++;
77  int cycle = std::abs(int(numEngines/maxIndex));
78  int curIndex = std::abs(int(numEngines%maxIndex));
79 
80  long mask = ((cycle & 0x007fffff) << 8);
81  HepRandom::getTheTableSeeds( seeds, curIndex );
82  seed = seeds[0]^mask;
83  setSeed(seed,0);
84  setSeeds(&theSeed,0);
85 }
86 
87 HepJamesRandom::HepJamesRandom(int rowIndex, int colIndex) // 15 Feb. 1998 JMM
89 {
90  long seed;
91  long seeds[2];
92 
93  int cycle = std::abs(int(rowIndex/maxIndex));
94  int row = std::abs(int(rowIndex%maxIndex));
95  int col = std::abs(int(colIndex%2));
96  long mask = ((cycle & 0x000007ff) << 20);
97  HepRandom::getTheTableSeeds( seeds, row );
98  seed = (seeds[col])^mask;
99  setSeed(seed,0);
100  setSeeds(&theSeed,0);
101 }
102 
104 : HepRandomEngine()
105 {
106  is >> *this;
107 }
108 
110 
111 void HepJamesRandom::saveStatus( const char filename[] ) const
112 {
113  std::ofstream outFile( filename, std::ios::out ) ;
114 
115  if (!outFile.bad()) {
116  outFile << "Uvec\n";
117  std::vector<unsigned long> v = put();
118  for (unsigned int i=0; i<v.size(); ++i) {
119  outFile << v[i] << "\n";
120  }
121  }
122 }
123 
124 void HepJamesRandom::restoreStatus( const char filename[] )
125 {
126  int ipos, jpos;
127  std::ifstream inFile( filename, std::ios::in);
128  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
129  std::cerr << " -- Engine state remains unchanged\n";
130  return;
131  }
132  if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
133  std::vector<unsigned long> v;
134  unsigned long xin;
135  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
136  inFile >> xin;
137  if (!inFile) {
138  inFile.clear(std::ios::badbit | inFile.rdstate());
139  std::cerr << "\nJamesRandom state (vector) description improper."
140  << "\nrestoreStatus has failed."
141  << "\nInput stream is probably mispositioned now." << std::endl;
142  return;
143  }
144  v.push_back(xin);
145  }
146  getState(v);
147  return;
148  }
149 
150  if (!inFile.bad() && !inFile.eof()) {
151 // inFile >> theSeed; removed -- encompased by possibleKeywordInput
152  for (int i=0; i<97; ++i)
153  inFile >> u[i];
154  inFile >> c; inFile >> cd; inFile >> cm;
155  inFile >> jpos;
156  ipos = (64+jpos)%97;
157  i97 = ipos;
158  j97 = jpos;
159  }
160 }
161 
163 {
164  std::cout << std::endl;
165  std::cout << "----- HepJamesRandom engine status -----" << std::endl;
166  std::cout << " Initial seed = " << theSeed << std::endl;
167  std::cout << " u[] = ";
168  for (int i=0; i<97; ++i)
169  std::cout << u[i] << " ";
170  std::cout << std::endl;
171  std::cout << " c = " << c << ", cd = " << cd << ", cm = " << cm
172  << std::endl;
173  std::cout << " i97 = " << i97 << ", u[i97] = " << u[i97] << std::endl;
174  std::cout << " j97 = " << j97 << ", u[j97] = " << u[j97] << std::endl;
175  std::cout << "----------------------------------------" << std::endl;
176 }
177 
179 {
180  // The input value for "seed" should be within the range [0,900000000]
181  //
182  // Negative seeds result in serious flaws in the randomness;
183  // seeds above 900000000 are OK because of the %177 in the expression for i,
184  // but may have the same effect as other seeds below 900000000.
185 
186  int m, n;
187  float s, t;
188  long mm;
189 
190  if (seed < 0) {
191  std::cout << "Seed for HepJamesRandom must be non-negative\n"
192  << "Seed value supplied was " << seed
193  << "\nUsing its absolute value instead\n";
194  seed = -seed;
195  }
196 
197  long ij = seed/30082;
198  long kl = seed - 30082*ij;
199  long i = (ij/177) % 177 + 2;
200  long j = ij % 177 + 2;
201  long k = (kl/169) % 178 + 1;
202  long l = kl % 169;
203 
204  theSeed = seed;
205 
206  for ( n = 1 ; n < 98 ; n++ ) {
207  s = 0.0;
208  t = 0.5;
209  for ( m = 1 ; m < 25 ; m++) {
210  mm = ( ( (i*j) % 179 ) * k ) % 179;
211  i = j;
212  j = k;
213  k = mm;
214  l = ( 53 * l + 1 ) % 169;
215  if ( (l*mm % 64 ) >= 32 )
216  s += t;
217  t *= 0.5;
218  }
219  u[n-1] = s;
220  }
221  c = 362436.0 / 16777216.0;
222  cd = 7654321.0 / 16777216.0;
223  cm = 16777213.0 / 16777216.0;
224 
225  i97 = 96;
226  j97 = 32;
227 
228 }
229 
230 void HepJamesRandom::setSeeds(const long* seeds, int)
231 {
232  setSeed(seeds ? *seeds : 19780503L, 0);
233  theSeeds = seeds;
234 }
235 
237 {
238  double uni;
239 
240  do {
241  uni = u[i97] - u[j97];
242  if ( uni < 0.0 ) uni++;
243  u[i97] = uni;
244 
245  if (i97 == 0) i97 = 96;
246  else i97--;
247 
248  if (j97 == 0) j97 = 96;
249  else j97--;
250 
251  c -= cd;
252  if (c < 0.0) c += cm;
253 
254  uni -= c;
255  if (uni < 0.0) uni += 1.0;
256  } while ( uni <= 0.0 || uni >= 1.0 );
257 
258  return uni;
259 }
260 
261 void HepJamesRandom::flatArray(const int size, double* vect)
262 {
263 // double uni;
264  int i;
265 
266  for (i=0; i<size; ++i) {
267  vect[i] = flat();
268  }
269 }
270 
271 HepJamesRandom::operator double() {
272  return flat();
273 }
274 
275 HepJamesRandom::operator float() {
276  return float( flat() );
277 }
278 
279 HepJamesRandom::operator unsigned int() {
280  return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
281  (((unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
282 }
283 
284 std::ostream & HepJamesRandom::put ( std::ostream& os ) const {
285  char beginMarker[] = "JamesRandom-begin";
286  os << beginMarker << "\nUvec\n";
287  std::vector<unsigned long> v = put();
288  for (unsigned int i=0; i<v.size(); ++i) {
289  os << v[i] << "\n";
290  }
291  return os;
292 }
293 
294 std::vector<unsigned long> HepJamesRandom::put () const {
295  std::vector<unsigned long> v;
296  v.push_back (engineIDulong<HepJamesRandom>());
297  std::vector<unsigned long> t;
298  for (int i=0; i<97; ++i) {
299  t = DoubConv::dto2longs(u[i]);
300  v.push_back(t[0]); v.push_back(t[1]);
301  }
302  t = DoubConv::dto2longs(c);
303  v.push_back(t[0]); v.push_back(t[1]);
304  t = DoubConv::dto2longs(cd);
305  v.push_back(t[0]); v.push_back(t[1]);
306  t = DoubConv::dto2longs(cm);
307  v.push_back(t[0]); v.push_back(t[1]);
308  v.push_back(static_cast<unsigned long>(j97));
309  return v;
310 }
311 
312 
313 std::istream & HepJamesRandom::get ( std::istream& is) {
314  char beginMarker [MarkerLen];
315  is >> std::ws;
316  is.width(MarkerLen); // causes the next read to the char* to be <=
317  // that many bytes, INCLUDING A TERMINATION \0
318  // (Stroustrup, section 21.3.2)
319  is >> beginMarker;
320  if (strcmp(beginMarker,"JamesRandom-begin")) {
321  is.clear(std::ios::badbit | is.rdstate());
322  std::cerr << "\nInput stream mispositioned or"
323  << "\nJamesRandom state description missing or"
324  << "\nwrong engine type found." << std::endl;
325  return is;
326  }
327  return getState(is);
328 }
329 
330 std::string HepJamesRandom::beginTag ( ) {
331  return "JamesRandom-begin";
332 }
333 
334 std::istream & HepJamesRandom::getState ( std::istream& is) {
335  if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
336  std::vector<unsigned long> v;
337  unsigned long uu;
338  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
339  is >> uu;
340  if (!is) {
341  is.clear(std::ios::badbit | is.rdstate());
342  std::cerr << "\nJamesRandom state (vector) description improper."
343  << "\ngetState() has failed."
344  << "\nInput stream is probably mispositioned now." << std::endl;
345  return is;
346  }
347  v.push_back(uu);
348  }
349  getState(v);
350  return (is);
351  }
352 
353 // is >> theSeed; Removed, encompassed by possibleKeywordInput()
354 
355  int ipos, jpos;
356  char endMarker [MarkerLen];
357  for (int i=0; i<97; ++i) {
358  is >> u[i];
359  }
360  is >> c; is >> cd; is >> cm;
361  is >> jpos;
362  is >> std::ws;
363  is.width(MarkerLen);
364  is >> endMarker;
365  if(strcmp(endMarker,"JamesRandom-end")) {
366  is.clear(std::ios::badbit | is.rdstate());
367  std::cerr << "\nJamesRandom state description incomplete."
368  << "\nInput stream is probably mispositioned now." << std::endl;
369  return is;
370  }
371 
372  ipos = (64+jpos)%97;
373  i97 = ipos;
374  j97 = jpos;
375  return is;
376 }
377 
378 bool HepJamesRandom::get (const std::vector<unsigned long> & v) {
379  if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
380  std::cerr <<
381  "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
382  return false;
383  }
384  return getState(v);
385 }
386 
387 bool HepJamesRandom::getState (const std::vector<unsigned long> & v) {
388  if (v.size() != VECTOR_STATE_SIZE ) {
389  std::cerr <<
390  "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
391  return false;
392  }
393  std::vector<unsigned long> t(2);
394  for (int i=0; i<97; ++i) {
395  t[0] = v[2*i+1]; t[1] = v[2*i+2];
396  u[i] = DoubConv::longs2double(t);
397  }
398  t[0] = v[195]; t[1] = v[196]; c = DoubConv::longs2double(t);
399  t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
400  t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
401  j97 = v[201];
402  i97 = (64+j97)%97;
403  return true;
404 }
405 
406 } // namespace CLHEP
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
void setSeed(long seed, int dum=0)
Definition: JamesRandom.cc:178
void saveStatus(const char filename[]="JamesRand.conf") const
Definition: JamesRandom.cc:111
static std::string beginTag()
Definition: JamesRandom.cc:330
void flatArray(const int size, double *vect)
Definition: JamesRandom.cc:261
static ush mask[]
Definition: csz_inflate.cc:317
std::vector< unsigned long > put() const
Definition: JamesRandom.cc:294
static const unsigned int VECTOR_STATE_SIZE
Definition: JamesRandom.h:98
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
std::string name() const
Definition: JamesRandom.cc:61
static const int MarkerLen
Definition: DualRand.cc:67
static constexpr double L
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)
void setSeeds(const long *seeds, int dum=0)
Definition: JamesRandom.cc:230
static constexpr double s
virtual std::istream & getState(std::istream &is)
Definition: JamesRandom.cc:334
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
static constexpr double m
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:45
ifstream in
Definition: comparison.C:7
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:251
static constexpr double mm
Definition: SystemOfUnits.h:95
void showStatus() const
Definition: JamesRandom.cc:162
static std::string engineName()
Definition: JamesRandom.h:92
Char_t n[5]
void restoreStatus(const char filename[]="JamesRand.conf")
Definition: JamesRandom.cc:124
double flat()
Definition: G4AblaRandom.cc:48
virtual std::istream & get(std::istream &is)
Definition: JamesRandom.cc:313