Geant4  10.01.p03
RanshiEngine.cc
Go to the documentation of this file.
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- RanshiEngine ---
7 // class implementation file
8 // -----------------------------------------------------------------------
9 //
10 // This algorithm implements the random number generator as proposed by
11 // "F. Gutbrod, Comp. Phys. Comm. 87 (1995) 291-306".
12 //
13 // =======================================================================
14 // Ken Smith - Created: 9th June 1998
15 // - Removed std::pow() from flat method: 21st Jul 1998
16 // - Added conversion operators: 6th Aug 1998
17 // J. Marraffino - Added some explicit casts to deal with
18 // machines where sizeof(int) != sizeof(long) 22 Aug 1998
19 // M. Fischler - Modified constructors taking seeds to not
20 // depend on numEngines (same seeds should
21 // produce same sequences). Default still
22 // depends on numEngines. 16 Sep 1998
23 // - Modified use of the various exponents of 2
24 // to avoid per-instance space overhead and
25 // correct the rounding procedure 16 Sep 1998
26 // J. Marraffino - Remove dependence on hepString class 13 May 1999
27 // M. Fischler - In restore, checkFile for file not found 03 Dec 2004
28 // M. Fischler - Methods for instance save/restore 12/8/04
29 // M. Fischler - split get() into tag validation and
30 // getState() for anonymous restores 12/27/04
31 // M. Fischler - State-saving using only ints, for portability 4/12/05
32 // L. Garren - use explicit 32bit mask to avoid compiler warnings 6/6/2014
33 // L. Garren - adding pragma for 32bit gcc 4.9 11/20/2014
34 //
35 // =======================================================================
36 
37 #include "CLHEP/Random/RanshiEngine.h"
38 #include "CLHEP/Random/engineIDulong.h"
39 #include "CLHEP/Utility/atomic_int.h"
40 
41 #include <string.h> // for strcmp
42 #include <iostream>
43 
44 // don't generate warnings about agressive loop optimization
45 #if defined __GNUC__
46  #if __GNUC__ > 3 && __GNUC_MINOR__ > 8
47  #pragma GCC diagnostic push
48  #pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
49  #endif
50 #endif
51 
52 namespace CLHEP {
53 
54 namespace {
55  // Number of instances with automatic seed selection
56  CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
57 }
58 
59 static const int MarkerLen = 64; // Enough room to hold a begin or end marker.
60 
61 std::string RanshiEngine::name() const {return "RanshiEngine";}
62 
63 RanshiEngine::RanshiEngine()
64 : HepRandomEngine(),
65  halfBuff(0), numFlats(0)
66 {
67  int numEngines = numberOfEngines++;
68  int i = 0;
69  while (i < numBuff) {
70  buffer[i] = (unsigned int)((numEngines+19780503L*(i+1))& 0xffffffff);
71  ++i;
72  }
73  theSeed = numEngines+19780503L*++i;
74  redSpin = (unsigned int)(theSeed & 0xffffffff);
75 
76  for( i = 0; i < 10000; ++i) flat(); // Warm-up by running thorugh 10000 nums
77 }
78 
79 RanshiEngine::RanshiEngine(std::istream& is)
80 : HepRandomEngine(),
81  halfBuff(0), numFlats(0)
82 {
83  is >> *this;
84 }
85 
86 RanshiEngine::RanshiEngine(long seed)
87 : HepRandomEngine(),
88  halfBuff(0), numFlats(0)
89 {
90  for (int i = 0; i < numBuff; ++i) {
91  buffer[i] = (unsigned int)seed&0xffffffff;
92  }
93  theSeed = seed;
94  redSpin = (unsigned int)(theSeed & 0xffffffff);
95  int j;
96  for (j = 0; j < numBuff*20; ++j) { // "warm-up" for engine to hit
97  flat(); // every ball on average 20X.
98  }
99 }
100 
101 RanshiEngine::RanshiEngine(int rowIndex, int colIndex)
102 : HepRandomEngine(),
103  halfBuff(0), numFlats(0)
104 {
105  int i = 0;
106  while( i < numBuff ) {
107  buffer[i] = (unsigned int)((rowIndex + (i+1)*(colIndex+8))&0xffffffff);
108  ++i;
109  }
110  theSeed = rowIndex;
111  redSpin = colIndex & 0xffffffff;
112  for( i = 0; i < 100; ++i) flat(); // Warm-up by running thorugh 100 nums
113 }
114 
115 RanshiEngine::~RanshiEngine() { }
116 
117 double RanshiEngine::flat() {
118  unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
119  unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
120  unsigned int boostResult = blkSpin ^ redSpin;
121 
122  buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
123 
124  redSpin = (blkSpin + numFlats++) & 0xffffffff;
125  halfBuff = numBuff/2 - halfBuff;
126 
127  return ( blkSpin * twoToMinus_32() + // most significant part
128  (boostResult>>11) * twoToMinus_53() + // fill in remaining bits
129  nearlyTwoToMinus_54()); // non-zero
130 }
131 
132 void RanshiEngine::flatArray(const int size, double* vect) {
133  for (int i = 0; i < size; ++i) {
134  vect[i] = flat();
135  }
136 }
137 
138 void RanshiEngine::setSeed(long seed, int) {
139  *this = RanshiEngine(seed);
140 }
141 
142 void RanshiEngine::setSeeds(const long* seeds, int) {
143  if (*seeds) {
144  int i = 0;
145  while (seeds[i] && i < numBuff) {
146  buffer[i] = (unsigned int)seeds[i];
147  ++i;
148  }
149  while (i < numBuff) {
150  buffer[i] = buffer[i-1];
151  ++i;
152  }
153  theSeed = seeds[0];
154  redSpin = (unsigned int)theSeed;
155  }
156  theSeeds = seeds;
157 }
158 
159 void RanshiEngine::saveStatus(const char filename[]) const {
160  std::ofstream outFile(filename, std::ios::out);
161  if (!outFile.bad()) {
162  outFile << "Uvec\n";
163  std::vector<unsigned long> v = put();
164  for (unsigned int i=0; i<v.size(); ++i) {
165  outFile << v[i] << "\n";
166  }
167  }
168 }
169 
170 void RanshiEngine::restoreStatus(const char filename[]) {
171  std::ifstream inFile(filename, std::ios::in);
172  if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
173  std::cerr << " -- Engine state remains unchanged\n";
174  return;
175  }
176  if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
177  std::vector<unsigned long> v;
178  unsigned long xin;
179  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
180  inFile >> xin;
181  if (!inFile) {
182  inFile.clear(std::ios::badbit | inFile.rdstate());
183  std::cerr << "\nRanshiEngine state (vector) description improper."
184  << "\nrestoreStatus has failed."
185  << "\nInput stream is probably mispositioned now." << std::endl;
186  return;
187  }
188  v.push_back(xin);
189  }
190  getState(v);
191  return;
192  }
193 
194  if (!inFile.bad()) {
195 // inFile >> theSeed; removed -- encompased by possibleKeywordInput
196  for (int i = 0; i < numBuff; ++i) {
197  inFile >> buffer[i];
198  }
199  inFile >> redSpin >> numFlats >> halfBuff;
200  }
201 }
202 
203 void RanshiEngine::showStatus() const {
204  std::cout << std::setprecision(20) << std::endl;
205  std::cout << "----------- Ranshi engine status ----------" << std::endl;
206  std::cout << "Initial seed = " << theSeed << std::endl;
207  std::cout << "Current red spin = " << redSpin << std::endl;
208  std::cout << "Values produced = " << numFlats << std::endl;
209  std::cout << "Side of buffer = " << (halfBuff ? "upper" : "lower")
210  << std::endl;
211  std::cout << "Current buffer = " << std::endl;
212  for (int i = 0; i < numBuff; i+=4) {
213  std::cout << std::setw(10) << std::setiosflags(std::ios::right)
214  << buffer[i] << std::setw(11) << buffer[i+1] << std::setw(11)
215  << buffer[i+2] << std::setw(11) << buffer[i+3] << std::endl;
216  }
217  std::cout << "-------------------------------------------" << std::endl;
218 }
219 
220 RanshiEngine::operator float() {
221  unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
222  unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
223 
224  buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
225 
226  redSpin = (blkSpin + numFlats++) & 0xffffffff;
227  halfBuff = numBuff/2 - halfBuff;
228 
229  return float(blkSpin * twoToMinus_32());
230 }
231 
232 RanshiEngine::operator unsigned int() {
233  unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
234  unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
235 
236  buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
237 
238  redSpin = (blkSpin + numFlats++) & 0xffffffff;
239  halfBuff = numBuff/2 - halfBuff;
240 
241  return blkSpin;
242 }
243 
244 std::ostream& RanshiEngine::put (std::ostream& os ) const {
245  char beginMarker[] = "RanshiEngine-begin";
246  os << beginMarker << "\nUvec\n";
247  std::vector<unsigned long> v = put();
248  for (unsigned int i=0; i<v.size(); ++i) {
249  os << v[i] << "\n";
250  }
251  return os;
252 }
253 
254 std::vector<unsigned long> RanshiEngine::put () const {
255  std::vector<unsigned long> v;
256  v.push_back (engineIDulong<RanshiEngine>());
257  for (int i = 0; i < numBuff; ++i) {
258  v.push_back(static_cast<unsigned long>(buffer[i]));
259  }
260  v.push_back(static_cast<unsigned long>(redSpin));
261  v.push_back(static_cast<unsigned long>(numFlats));
262  v.push_back(static_cast<unsigned long>(halfBuff));
263  return v;
264 }
265 
266 std::istream& RanshiEngine::get (std::istream& is) {
267  char beginMarker [MarkerLen];
268  is >> std::ws;
269  is.width(MarkerLen); // causes the next read to the char* to be <=
270  // that many bytes, INCLUDING A TERMINATION \0
271  // (Stroustrup, section 21.3.2)
272  is >> beginMarker;
273  if (strcmp(beginMarker,"RanshiEngine-begin")) {
274  is.clear(std::ios::badbit | is.rdstate());
275  std::cerr << "\nInput mispositioned or"
276  << "\nRanshiEngine state description missing or"
277  << "\nwrong engine type found." << std::endl;
278  return is;
279  }
280  return getState(is);
281 }
282 
283 std::string RanshiEngine::beginTag ( ) {
284  return "RanshiEngine-begin";
285 }
286 
287 std::istream& RanshiEngine::getState (std::istream& is) {
288  if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
289  std::vector<unsigned long> v;
290  unsigned long uu;
291  for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
292  is >> uu;
293  if (!is) {
294  is.clear(std::ios::badbit | is.rdstate());
295  std::cerr << "\nRanshiEngine state (vector) description improper."
296  << "\ngetState() has failed."
297  << "\nInput stream is probably mispositioned now." << std::endl;
298  return is;
299  }
300  v.push_back(uu);
301  }
302  getState(v);
303  return (is);
304  }
305 
306 // is >> theSeed; Removed, encompassed by possibleKeywordInput()
307 
308  char endMarker [MarkerLen];
309  for (int i = 0; i < numBuff; ++i) {
310  is >> buffer[i];
311  }
312  is >> redSpin >> numFlats >> halfBuff;
313  is >> std::ws;
314  is.width(MarkerLen);
315  is >> endMarker;
316  if (strcmp(endMarker,"RanshiEngine-end")) {
317  is.clear(std::ios::badbit | is.rdstate());
318  std::cerr << "\nRanshiEngine state description incomplete."
319  << "\nInput stream is probably mispositioned now." << std::endl;
320  return is;
321  }
322  return is;
323 }
324 
325 bool RanshiEngine::get (const std::vector<unsigned long> & v) {
326  if ((v[0] & 0xffffffffUL) != engineIDulong<RanshiEngine>()) {
327  std::cerr <<
328  "\nRanshiEngine get:state vector has wrong ID word - state unchanged\n";
329  return false;
330  }
331  return getState(v);
332 }
333 
334 bool RanshiEngine::getState (const std::vector<unsigned long> & v) {
335  if (v.size() != VECTOR_STATE_SIZE ) {
336  std::cerr <<
337  "\nRanshiEngine get:state vector has wrong length - state unchanged\n";
338  return false;
339  }
340  for (int i = 0; i < numBuff; ++i) {
341  buffer[i] = v[i+1];
342  }
343  redSpin = v[numBuff+1];
344  numFlats = v[numBuff+2];
345  halfBuff = v[numBuff+3];
346  return true;
347 }
348 
349 } // namespace CLHEP
350 
351 #if defined __GNUC__
352  #if __GNUC__ > 3 && __GNUC_MINOR__ > 8
353  #pragma GCC diagnostic pop
354  #endif
355 #endif
static const int MarkerLen
Definition: DualRand.cc:67
G4String name
Definition: TRTMaterials.hh:40
#define buffer
Definition: xmlparse.cc:611
static const G4int L[nN]
double flat()
Definition: G4AblaRandom.cc:47
void setSeeds(const SeedVector &sv)
Set the seeds of the current generator.
Definition: G4INCLRandom.cc:85