Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
CLHEP::RanluxppEngine Class Referencefinal

#include <RanluxppEngine.h>

+ Inheritance diagram for CLHEP::RanluxppEngine:

Public Member Functions

 RanluxppEngine ()
 
 RanluxppEngine (long seed)
 
 RanluxppEngine (std::istream &is)
 
virtual ~RanluxppEngine ()
 
double flat () override
 
void flatArray (const int size, double *vect) override
 
void setSeed (long seed, int dummy=0) override
 
void setSeeds (const long *seeds, int dummy=0) override
 
void skip (uint64_t n)
 
void saveStatus (const char filename[]="Ranluxpp.conf") const override
 
void restoreStatus (const char filename[]="Ranluxpp.conf") override
 
void showStatus () const override
 
std::string name () const override
 
std::ostream & put (std::ostream &os) const override
 
std::istream & get (std::istream &is) override
 
std::istream & getState (std::istream &is) override
 
std::vector< unsigned long > put () const override
 
bool get (const std::vector< unsigned long > &v) override
 
bool getState (const std::vector< unsigned long > &v) override
 
 operator double () override
 
 operator float () override
 
 operator unsigned int () override
 
- 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 engineName ()
 
static std::string beginTag ()
 
- 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)
 

Static Public Attributes

static const unsigned int VECTOR_STATE_SIZE = 21
 

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
Jonas Hahnfeld

Definition at line 35 of file RanluxppEngine.h.

Constructor & Destructor Documentation

◆ RanluxppEngine() [1/3]

CLHEP::RanluxppEngine::RanluxppEngine ( )

Definition at line 64 of file RanluxppEngine.cc.

64 : HepRandomEngine() {
65 int numEngines = ++numberOfEngines;
66 setSeed(numEngines);
67}
void setSeed(long seed, int dummy=0) override

◆ RanluxppEngine() [2/3]

CLHEP::RanluxppEngine::RanluxppEngine ( long  seed)

Definition at line 69 of file RanluxppEngine.cc.

69 : HepRandomEngine() {
70 theSeed = seed;
71 setSeed(seed);
72}

◆ RanluxppEngine() [3/3]

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

Definition at line 74 of file RanluxppEngine.cc.

74 : HepRandomEngine() {
75 get(is);
76}
std::istream & get(std::istream &is) override

◆ ~RanluxppEngine()

CLHEP::RanluxppEngine::~RanluxppEngine ( )
virtualdefault

Member Function Documentation

◆ beginTag()

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

Definition at line 224 of file RanluxppEngine.cc.

224{ return "RanluxppEngine-begin"; }

Referenced by get(), and put().

◆ engineName()

std::string CLHEP::RanluxppEngine::engineName ( )
static

Definition at line 222 of file RanluxppEngine.cc.

222{ return "RanluxppEngine"; }

Referenced by name().

◆ flat()

double CLHEP::RanluxppEngine::flat ( )
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 112 of file RanluxppEngine.cc.

112 {
113 // RandomEngine wants a "double random values ranging between ]0,1[", so
114 // exclude all zero bits.
115 uint64_t random;
116 do {
117 random = nextRandomBits();
118 } while (random == 0);
119
120 static constexpr double div = 1.0 / (uint64_t(1) << kBits);
121 return random * div;
122}

Referenced by flatArray(), operator double(), and operator float().

◆ flatArray()

void CLHEP::RanluxppEngine::flatArray ( const int  size,
double *  vect 
)
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 124 of file RanluxppEngine.cc.

124 {
125 for (int i = 0; i < size; i++) {
126 vect[i] = flat();
127 }
128}
double flat() override

◆ get() [1/2]

bool CLHEP::RanluxppEngine::get ( const std::vector< unsigned long > &  v)
overridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 278 of file RanluxppEngine.cc.

278 {
279 if (v[0] != engineIDulong<RanluxppEngine>()) {
280 std::cerr << "RanluxppEngine::get(): "
281 << "vector has wrong ID word - state unchanged" << std::endl;
282 return false;
283 }
284 return getState(v);
285}
std::istream & getState(std::istream &is) override

◆ get() [2/2]

std::istream & CLHEP::RanluxppEngine::get ( std::istream &  is)
overridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 235 of file RanluxppEngine.cc.

235 {
236 std::string tag;
237 is >> tag;
238 if (tag != beginTag()) {
239 is.clear(std::ios::badbit | is.rdstate());
240 std::cerr << "No RanluxppEngine found at current position\n";
241 return is;
242 }
243 return getState(is);
244}
static std::string beginTag()
Definition: xmlparse.c:284

Referenced by RanluxppEngine(), and restoreStatus().

◆ getState() [1/2]

bool CLHEP::RanluxppEngine::getState ( const std::vector< unsigned long > &  v)
overridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 287 of file RanluxppEngine.cc.

287 {
288 if (v.size() != VECTOR_STATE_SIZE) {
289 std::cerr << "RanluxppEngine::getState(): "
290 << "vector has wrong length - state unchanged" << std::endl;
291 return false;
292 }
293
294 // Assemble the state vector (see RanluxppEngine::put).
295 for (int i = 0; i < 9; i++) {
296 uint64_t lower = v[2 * i + 1];
297 uint64_t upper = v[2 * i + 2];
298 fState[i] = (upper << 32) + lower;
299 }
300 fCarry = v[19];
301 fPosition = v[20];
302
303 return true;
304}
static const unsigned int VECTOR_STATE_SIZE

◆ getState() [2/2]

std::istream & CLHEP::RanluxppEngine::getState ( std::istream &  is)
overridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 246 of file RanluxppEngine.cc.

246 {
247 std::vector<unsigned long> state;
248 state.reserve(VECTOR_STATE_SIZE);
249 for (unsigned int i = 0; i < VECTOR_STATE_SIZE; i++) {
250 unsigned long v;
251 is >> v;
252 state.push_back(v);
253 }
254
255 getState(state);
256 return is;
257}

Referenced by get(), and getState().

◆ name()

std::string CLHEP::RanluxppEngine::name ( ) const
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 220 of file RanluxppEngine.cc.

220{ return engineName(); }
static std::string engineName()

◆ operator double()

CLHEP::RanluxppEngine::operator double ( )
inlineoverridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 86 of file RanluxppEngine.h.

86{ return flat(); }

◆ operator float()

CLHEP::RanluxppEngine::operator float ( )
inlineoverridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 87 of file RanluxppEngine.h.

87{ return float(flat()); }

◆ operator unsigned int()

CLHEP::RanluxppEngine::operator unsigned int ( )
inlineoverridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 88 of file RanluxppEngine.h.

88{ return (unsigned int)nextRandomBits(); }

◆ put() [1/2]

std::vector< unsigned long > CLHEP::RanluxppEngine::put ( ) const
overridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 259 of file RanluxppEngine.cc.

259 {
260 std::vector<unsigned long> v;
261 v.reserve(VECTOR_STATE_SIZE);
262 v.push_back(engineIDulong<RanluxppEngine>());
263
264 // unsigned long is only guaranteed to be 32 bit wide, so chop up the 64 bit
265 // values in fState.
266 for (int i = 0; i < 9; i++) {
267 unsigned long lower = static_cast<uint32_t>(fState[i]);
268 v.push_back(lower);
269 unsigned long upper = static_cast<uint32_t>(fState[i] >> 32);
270 v.push_back(upper);
271 }
272
273 v.push_back(fCarry);
274 v.push_back(fPosition);
275 return v;
276}

Referenced by put(), and saveStatus().

◆ put() [2/2]

std::ostream & CLHEP::RanluxppEngine::put ( std::ostream &  os) const
overridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 226 of file RanluxppEngine.cc.

226 {
227 os << beginTag() << "\n";
228 const std::vector<unsigned long> state = put();
229 for (unsigned long v : state) {
230 os << v << "\n";
231 }
232 return os;
233}
std::vector< unsigned long > put() const override

◆ restoreStatus()

void CLHEP::RanluxppEngine::restoreStatus ( const char  filename[] = "Ranluxpp.conf")
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 191 of file RanluxppEngine.cc.

191 {
192 std::ifstream is(filename);
193 get(is);
194 is.close();
195}

◆ saveStatus()

void CLHEP::RanluxppEngine::saveStatus ( const char  filename[] = "Ranluxpp.conf") const
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 185 of file RanluxppEngine.cc.

185 {
186 std::ofstream os(filename);
187 put(os);
188 os.close();
189}

◆ setSeed()

void CLHEP::RanluxppEngine::setSeed ( long  seed,
int  dummy = 0 
)
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 130 of file RanluxppEngine.cc.

130 {
131 theSeed = seed;
132
133 uint64_t lcg[9];
134 lcg[0] = 1;
135 for (int i = 1; i < 9; i++) {
136 lcg[i] = 0;
137 }
138
139 uint64_t a_seed[9];
140 // Skip 2 ** 96 states.
141 powermod(kA_2048, a_seed, uint64_t(1) << 48);
142 powermod(a_seed, a_seed, uint64_t(1) << 48);
143 // Skip more states according to seed.
144 powermod(a_seed, a_seed, seed);
145 mulmod(a_seed, lcg);
146
147 to_ranlux(lcg, fState, fCarry);
148 fPosition = 0;
149}

Referenced by RanluxppEngine(), and setSeeds().

◆ setSeeds()

void CLHEP::RanluxppEngine::setSeeds ( const long *  seeds,
int  dummy = 0 
)
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 151 of file RanluxppEngine.cc.

151 {
152 theSeeds = seeds;
153 setSeed(*seeds, 0);
154}

◆ showStatus()

void CLHEP::RanluxppEngine::showStatus ( ) const
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 197 of file RanluxppEngine.cc.

197 {
198 std::cout
199 << "--------------------- RanluxppEngine status --------------------"
200 << std::endl;
201 std::cout << " fState[] = {";
202 std::cout << std::hex << std::setfill('0');
203 for (int i = 0; i < 9; i++) {
204 if (i % 3 == 0) {
205 std::cout << std::endl << " ";
206 } else {
207 std::cout << " ";
208 }
209 std::cout << "0x" << std::setw(16) << fState[i] << ",";
210 }
211 std::cout << std::endl << " }" << std::endl;
212 std::cout << std::dec;
213 std::cout << " fCarry = " << fCarry << ", fPosition = " << fPosition
214 << std::endl;
215 std::cout
216 << "----------------------------------------------------------------"
217 << std::endl;
218}

◆ skip()

void CLHEP::RanluxppEngine::skip ( uint64_t  n)

Definition at line 156 of file RanluxppEngine.cc.

156 {
157 int left = (kMaxPos - fPosition) / kBits;
158 assert(left >= 0 && "position was out of range!");
159 if (n < (uint64_t)left) {
160 // Just skip the next few entries in the currently available bits.
161 fPosition += n * kBits;
162 return;
163 }
164
165 n -= left;
166 // Need to advance and possibly skip over blocks.
167 int nPerState = kMaxPos / kBits;
168 int skip = (n / nPerState);
169
170 uint64_t a_skip[9];
171 powermod(kA_2048, a_skip, skip + 1);
172
173 uint64_t lcg[9];
174 to_lcg(fState, fCarry, lcg);
175 mulmod(a_skip, lcg);
176 to_ranlux(lcg, fState, fCarry);
177
178 // Potentially skip numbers in the freshly generated block.
179 int remaining = n - skip * nPerState;
180 assert(remaining >= 0 && "should not end up at a negative position!");
181 fPosition = remaining * kBits;
182 assert(fPosition <= kMaxPos && "position out of range!");
183}
void skip(uint64_t n)

Referenced by skip().

Member Data Documentation

◆ VECTOR_STATE_SIZE

const unsigned int CLHEP::RanluxppEngine::VECTOR_STATE_SIZE = 21
static

Definition at line 92 of file RanluxppEngine.h.

Referenced by getState(), and put().


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