Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
CLHEP::RanshiEngine Class Reference

#include <RanshiEngine.h>

+ Inheritance diagram for CLHEP::RanshiEngine:

Public Member Functions

 RanshiEngine ()
 
 RanshiEngine (std::istream &is)
 
 RanshiEngine (long seed)
 
 RanshiEngine (int rowIndex, int colIndex)
 
virtual ~RanshiEngine ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
void setSeed (long seed, int)
 
void setSeeds (const long *seeds, int)
 
void saveStatus (const char filename[]="RanshiEngine.conf") const
 
void restoreStatus (const char filename[]="RanshiEngine.conf")
 
void showStatus () const
 
 operator double ()
 
 operator float ()
 
 operator unsigned int ()
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
std::string name () const
 
std::vector< unsigned long > put () const
 
bool get (const std::vector< unsigned long > &v)
 
bool getState (const std::vector< unsigned long > &v)
 
- Public Member Functions inherited from CLHEP::HepRandomEngine
 HepRandomEngine ()
 
virtual ~HepRandomEngine ()
 
bool operator== (const HepRandomEngine &engine)
 
bool operator!= (const HepRandomEngine &engine)
 
virtual double flat ()=0
 
virtual void flatArray (const int size, double *vect)=0
 
virtual void setSeed (long seed, int)=0
 
virtual void setSeeds (const long *seeds, int)=0
 
virtual void saveStatus (const char filename[]="Config.conf") const =0
 
virtual void restoreStatus (const char filename[]="Config.conf")=0
 
virtual void showStatus () const =0
 
virtual std::string name () const =0
 
virtual std::ostream & put (std::ostream &os) const
 
virtual std::istream & get (std::istream &is)
 
virtual std::istream & getState (std::istream &is)
 
virtual std::vector< unsigned long > put () const
 
virtual bool get (const std::vector< unsigned long > &v)
 
virtual bool getState (const std::vector< unsigned long > &v)
 
long getSeed () const
 
const long * getSeeds () const
 
virtual operator double ()
 
virtual operator float ()
 
virtual operator unsigned int ()
 

Static Public Member Functions

static std::string beginTag ()
 
static std::string engineName ()
 
- Static Public Member Functions inherited from CLHEP::HepRandomEngine
static std::string beginTag ()
 
static HepRandomEnginenewEngine (std::istream &is)
 
static HepRandomEnginenewEngine (const std::vector< unsigned long > &v)
 

Additional Inherited Members

- Static Protected Member Functions inherited from CLHEP::HepRandomEngine
static double exponent_bit_32 ()
 
static double mantissa_bit_12 ()
 
static double mantissa_bit_24 ()
 
static double mantissa_bit_32 ()
 
static double twoToMinus_32 ()
 
static double twoToMinus_48 ()
 
static double twoToMinus_49 ()
 
static double twoToMinus_53 ()
 
static double nearlyTwoToMinus_54 ()
 
static bool checkFile (std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
 
- Protected Attributes inherited from CLHEP::HepRandomEngine
long theSeed
 
const long * theSeeds
 

Detailed Description

Author

Definition at line 51 of file RanshiEngine.h.

Constructor & Destructor Documentation

◆ RanshiEngine() [1/4]

CLHEP::RanshiEngine::RanshiEngine ( )

Definition at line 65 of file RanshiEngine.cc.

67 halfBuff(0), numFlats(0)
68{
69 int numEngines = numberOfEngines++;
70 int i = 0;
71 while (i < numBuff) {
72 buffer[i] = (unsigned int)((numEngines+19780503L*(i+1))& 0xffffffff);
73 ++i;
74 }
75 theSeed = numEngines+19780503L*++i;
76 redSpin = (unsigned int)(theSeed & 0xffffffff);
77
78 for( i = 0; i < 10000; ++i) flat(); // Warm-up by running thorugh 10000 nums
79}

Referenced by setSeed().

◆ RanshiEngine() [2/4]

CLHEP::RanshiEngine::RanshiEngine ( std::istream &  is)

Definition at line 81 of file RanshiEngine.cc.

83 halfBuff(0), numFlats(0)
84{
85 is >> *this;
86}

◆ RanshiEngine() [3/4]

CLHEP::RanshiEngine::RanshiEngine ( long  seed)

Definition at line 88 of file RanshiEngine.cc.

90 halfBuff(0), numFlats(0)
91{
92 for (int i = 0; i < numBuff; ++i) {
93 buffer[i] = (unsigned int)seed&0xffffffff;
94 }
95 theSeed = seed;
96 redSpin = (unsigned int)(theSeed & 0xffffffff);
97 int j;
98 for (j = 0; j < numBuff*20; ++j) { // "warm-up" for engine to hit
99 flat(); // every ball on average 20X.
100 }
101}

◆ RanshiEngine() [4/4]

CLHEP::RanshiEngine::RanshiEngine ( int  rowIndex,
int  colIndex 
)

Definition at line 103 of file RanshiEngine.cc.

105 halfBuff(0), numFlats(0)
106{
107 int i = 0;
108 while( i < numBuff ) {
109 buffer[i] = (unsigned int)((rowIndex + (i+1)*(colIndex+8))&0xffffffff);
110 ++i;
111 }
112 theSeed = rowIndex;
113 redSpin = colIndex & 0xffffffff;
114 for( i = 0; i < 100; ++i) flat(); // Warm-up by running thorugh 100 nums
115}

◆ ~RanshiEngine()

CLHEP::RanshiEngine::~RanshiEngine ( )
virtual

Definition at line 117 of file RanshiEngine.cc.

117{ }

Member Function Documentation

◆ beginTag()

std::string CLHEP::RanshiEngine::beginTag ( )
static

Definition at line 289 of file RanshiEngine.cc.

289 {
290 return "RanshiEngine-begin";
291}

◆ engineName()

static std::string CLHEP::RanshiEngine::engineName ( )
inlinestatic

Definition at line 95 of file RanshiEngine.h.

95{return "RanshiEngine";}

Referenced by restoreStatus().

◆ flat()

double CLHEP::RanshiEngine::flat ( )
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 119 of file RanshiEngine.cc.

119 {
120 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
121 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
122 unsigned int boostResult = blkSpin ^ redSpin;
123
124 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
125
126 redSpin = (blkSpin + numFlats++) & 0xffffffff;
127 halfBuff = numBuff/2 - halfBuff;
128
129 return ( blkSpin * twoToMinus_32() + // most significant part
130 (boostResult>>11) * twoToMinus_53() + // fill in remaining bits
131 nearlyTwoToMinus_54()); // non-zero
132}
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()

Referenced by flatArray(), and RanshiEngine().

◆ flatArray()

void CLHEP::RanshiEngine::flatArray ( const int  size,
double *  vect 
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 134 of file RanshiEngine.cc.

134 {
135 for (int i = 0; i < size; ++i) {
136 vect[i] = flat();
137 }
138}

◆ get() [1/2]

bool CLHEP::RanshiEngine::get ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 331 of file RanshiEngine.cc.

331 {
332 if ((v[0] & 0xffffffffUL) != engineIDulong<RanshiEngine>()) {
333 std::cerr <<
334 "\nRanshiEngine get:state vector has wrong ID word - state unchanged\n";
335 return false;
336 }
337 return getState(v);
338}
virtual std::istream & getState(std::istream &is)

◆ get() [2/2]

std::istream & CLHEP::RanshiEngine::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 272 of file RanshiEngine.cc.

272 {
273 char beginMarker [MarkerLen];
274 is >> std::ws;
275 is.width(MarkerLen); // causes the next read to the char* to be <=
276 // that many bytes, INCLUDING A TERMINATION \0
277 // (Stroustrup, section 21.3.2)
278 is >> beginMarker;
279 if (strcmp(beginMarker,"RanshiEngine-begin")) {
280 is.clear(std::ios::badbit | is.rdstate());
281 std::cerr << "\nInput mispositioned or"
282 << "\nRanshiEngine state description missing or"
283 << "\nwrong engine type found." << std::endl;
284 return is;
285 }
286 return getState(is);
287}

◆ getState() [1/2]

bool CLHEP::RanshiEngine::getState ( const std::vector< unsigned long > &  v)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 340 of file RanshiEngine.cc.

340 {
341 if (v.size() != VECTOR_STATE_SIZE ) {
342 std::cerr <<
343 "\nRanshiEngine get:state vector has wrong length - state unchanged\n";
344 return false;
345 }
346 for (int i = 0; i < numBuff; ++i) {
347 buffer[i] = v[i+1];
348 }
349 redSpin = v[numBuff+1];
350 numFlats = v[numBuff+2];
351 halfBuff = v[numBuff+3];
352 return true;
353}

◆ getState() [2/2]

std::istream & CLHEP::RanshiEngine::getState ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 293 of file RanshiEngine.cc.

293 {
294 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
295 std::vector<unsigned long> v;
296 unsigned long uu;
297 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
298 is >> uu;
299 if (!is) {
300 is.clear(std::ios::badbit | is.rdstate());
301 std::cerr << "\nRanshiEngine state (vector) description improper."
302 << "\ngetState() has failed."
303 << "\nInput stream is probably mispositioned now." << std::endl;
304 return is;
305 }
306 v.push_back(uu);
307 }
308 getState(v);
309 return (is);
310 }
311
312// is >> theSeed; Removed, encompassed by possibleKeywordInput()
313
314 char endMarker [MarkerLen];
315 for (int i = 0; i < numBuff; ++i) {
316 is >> buffer[i];
317 }
318 is >> redSpin >> numFlats >> halfBuff;
319 is >> std::ws;
320 is.width(MarkerLen);
321 is >> endMarker;
322 if (strcmp(endMarker,"RanshiEngine-end")) {
323 is.clear(std::ios::badbit | is.rdstate());
324 std::cerr << "\nRanshiEngine state description incomplete."
325 << "\nInput stream is probably mispositioned now." << std::endl;
326 return is;
327 }
328 return is;
329}
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:166

Referenced by get(), getState(), and restoreStatus().

◆ name()

std::string CLHEP::RanshiEngine::name ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 63 of file RanshiEngine.cc.

63{return "RanshiEngine";}

◆ operator double()

CLHEP::RanshiEngine::operator double ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 222 of file RanshiEngine.cc.

222 {
223 return flat();
224}

◆ operator float()

CLHEP::RanshiEngine::operator float ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 226 of file RanshiEngine.cc.

226 {
227 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
228 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
229
230 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
231
232 redSpin = (blkSpin + numFlats++) & 0xffffffff;
233 halfBuff = numBuff/2 - halfBuff;
234
235 return float(blkSpin * twoToMinus_32());
236}

◆ operator unsigned int()

CLHEP::RanshiEngine::operator unsigned int ( )
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 238 of file RanshiEngine.cc.

238 {
239 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
240 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
241
242 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
243
244 redSpin = (blkSpin + numFlats++) & 0xffffffff;
245 halfBuff = numBuff/2 - halfBuff;
246
247 return blkSpin;
248}

◆ put() [1/2]

std::vector< unsigned long > CLHEP::RanshiEngine::put ( ) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 260 of file RanshiEngine.cc.

260 {
261 std::vector<unsigned long> v;
262 v.push_back (engineIDulong<RanshiEngine>());
263 for (int i = 0; i < numBuff; ++i) {
264 v.push_back(static_cast<unsigned long>(buffer[i]));
265 }
266 v.push_back(static_cast<unsigned long>(redSpin));
267 v.push_back(static_cast<unsigned long>(numFlats));
268 v.push_back(static_cast<unsigned long>(halfBuff));
269 return v;
270}

Referenced by put(), and saveStatus().

◆ put() [2/2]

std::ostream & CLHEP::RanshiEngine::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 250 of file RanshiEngine.cc.

250 {
251 char beginMarker[] = "RanshiEngine-begin";
252 os << beginMarker << "\nUvec\n";
253 std::vector<unsigned long> v = put();
254 for (unsigned int i=0; i<v.size(); ++i) {
255 os << v[i] << "\n";
256 }
257 return os;
258}
std::vector< unsigned long > put() const

◆ restoreStatus()

void CLHEP::RanshiEngine::restoreStatus ( const char  filename[] = "RanshiEngine.conf")
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 172 of file RanshiEngine.cc.

172 {
173 std::ifstream inFile(filename, std::ios::in);
174 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
175 std::cerr << " -- Engine state remains unchanged\n";
176 return;
177 }
178 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
179 std::vector<unsigned long> v;
180 unsigned long xin;
181 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
182 inFile >> xin;
183 if (!inFile) {
184 inFile.clear(std::ios::badbit | inFile.rdstate());
185 std::cerr << "\nRanshiEngine state (vector) description improper."
186 << "\nrestoreStatus has failed."
187 << "\nInput stream is probably mispositioned now." << std::endl;
188 return;
189 }
190 v.push_back(xin);
191 }
192 getState(v);
193 return;
194 }
195
196 if (!inFile.bad()) {
197// inFile >> theSeed; removed -- encompased by possibleKeywordInput
198 for (int i = 0; i < numBuff; ++i) {
199 inFile >> buffer[i];
200 }
201 inFile >> redSpin >> numFlats >> halfBuff;
202 }
203}
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:47
static std::string engineName()
Definition: RanshiEngine.h:95

◆ saveStatus()

void CLHEP::RanshiEngine::saveStatus ( const char  filename[] = "RanshiEngine.conf") const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 161 of file RanshiEngine.cc.

161 {
162 std::ofstream outFile(filename, std::ios::out);
163 if (!outFile.bad()) {
164 outFile << "Uvec\n";
165 std::vector<unsigned long> v = put();
166 for (unsigned int i=0; i<v.size(); ++i) {
167 outFile << v[i] << "\n";
168 }
169 }
170}

◆ setSeed()

void CLHEP::RanshiEngine::setSeed ( long  seed,
int   
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 140 of file RanshiEngine.cc.

140 {
141 *this = RanshiEngine(seed);
142}

◆ setSeeds()

void CLHEP::RanshiEngine::setSeeds ( const long *  seeds,
int   
)
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 144 of file RanshiEngine.cc.

144 {
145 if (*seeds) {
146 int i = 0;
147 while (seeds[i] && i < numBuff) {
148 buffer[i] = (unsigned int)seeds[i];
149 ++i;
150 }
151 while (i < numBuff) {
152 buffer[i] = buffer[i-1];
153 ++i;
154 }
155 theSeed = seeds[0];
156 redSpin = (unsigned int)theSeed;
157 }
158 theSeeds = seeds;
159}

◆ showStatus()

void CLHEP::RanshiEngine::showStatus ( ) const
virtual

Implements CLHEP::HepRandomEngine.

Definition at line 205 of file RanshiEngine.cc.

205 {
206 std::cout << std::setprecision(20) << std::endl;
207 std::cout << "----------- Ranshi engine status ----------" << std::endl;
208 std::cout << "Initial seed = " << theSeed << std::endl;
209 std::cout << "Current red spin = " << redSpin << std::endl;
210 std::cout << "Values produced = " << numFlats << std::endl;
211 std::cout << "Side of buffer = " << (halfBuff ? "upper" : "lower")
212 << std::endl;
213 std::cout << "Current buffer = " << std::endl;
214 for (int i = 0; i < numBuff; i+=4) {
215 std::cout << std::setw(10) << std::setiosflags(std::ios::right)
216 << buffer[i] << std::setw(11) << buffer[i+1] << std::setw(11)
217 << buffer[i+2] << std::setw(11) << buffer[i+3] << std::endl;
218 }
219 std::cout << "-------------------------------------------" << std::endl;
220}

The documentation for this class was generated from the following files: