Geant4-11
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes
CLHEP::RanluxppEngine Class Referencefinal

#include <RanluxppEngine.h>

Inheritance diagram for CLHEP::RanluxppEngine:
CLHEP::HepRandomEngine

Public Member Functions

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

Static Public Member Functions

static std::string beginTag ()
 
static std::string engineName ()
 
static HepRandomEnginenewEngine (const std::vector< unsigned long > &v)
 
static HepRandomEnginenewEngine (std::istream &is)
 

Static Public Attributes

static const unsigned int VECTOR_STATE_SIZE = 21
 

Static Protected Member Functions

static bool checkFile (std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
 
static double exponent_bit_32 ()
 
static double mantissa_bit_12 ()
 
static double mantissa_bit_24 ()
 
static double mantissa_bit_32 ()
 
static double nearlyTwoToMinus_54 ()
 
static double twoToMinus_32 ()
 
static double twoToMinus_48 ()
 
static double twoToMinus_49 ()
 
static double twoToMinus_53 ()
 

Protected Attributes

long theSeed
 
const long * theSeeds
 

Private Member Functions

void advance ()
 
uint64_t nextRandomBits ()
 

Private Attributes

unsigned fCarry
 Carry bit of the RANLUX state. More...
 
int fPosition = 0
 Current position in bits. More...
 
uint64_t fState [9]
 RANLUX state of the generator. More...
 

Detailed Description

Author
Jonas Hahnfeld

Definition at line 33 of file RanluxppEngine.h.

Constructor & Destructor Documentation

◆ RanluxppEngine() [1/3]

CLHEP::RanluxppEngine::RanluxppEngine ( )

Definition at line 63 of file RanluxppEngine.cc.

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

References CLHEP::anonymous_namespace{RanluxppEngine.cc}::numberOfEngines(), and setSeed().

◆ RanluxppEngine() [2/3]

CLHEP::RanluxppEngine::RanluxppEngine ( long  seed)

Definition at line 68 of file RanluxppEngine.cc.

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

References setSeed(), and CLHEP::HepRandomEngine::theSeed.

◆ RanluxppEngine() [3/3]

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

Definition at line 73 of file RanluxppEngine.cc.

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

References get().

◆ ~RanluxppEngine()

CLHEP::RanluxppEngine::~RanluxppEngine ( )
virtualdefault

Member Function Documentation

◆ advance()

void CLHEP::RanluxppEngine::advance ( )
private

Definition at line 82 of file RanluxppEngine.cc.

82 {
83 uint64_t lcg[9];
84 to_lcg(fState, fCarry, lcg);
85 mulmod(kA_2048, lcg);
86 to_ranlux(lcg, fState, fCarry);
87 fPosition = 0;
88}
unsigned fCarry
Carry bit of the RANLUX state.
uint64_t fState[9]
RANLUX state of the generator.
int fPosition
Current position in bits.
static void mulmod(const uint64_t *in1, uint64_t *inout)
Definition: mulmod.h:219
static void to_lcg(const uint64_t *ranlux, unsigned c, uint64_t *lcg)
Definition: ranlux_lcg.h:24
static void to_ranlux(const uint64_t *lcg, uint64_t *ranlux, unsigned &c_out)
Definition: ranlux_lcg.h:55

References fCarry, fPosition, fState, CLHEP::anonymous_namespace{RanluxppEngine.cc}::kA_2048, mulmod(), to_lcg(), and to_ranlux().

Referenced by nextRandomBits().

◆ beginTag()

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

Definition at line 223 of file RanluxppEngine.cc.

223{ return "RanluxppEngine-begin"; }

Referenced by get(), and put().

◆ checkFile()

bool CLHEP::HepRandomEngine::checkFile ( std::istream &  file,
const std::string &  filename,
const std::string &  classname,
const std::string &  methodname 
)
staticprotectedinherited

Definition at line 47 of file RandomEngine.cc.

50 {
51 if (!file) {
52 std::cerr << "Failure to find or open file " << filename <<
53 " in " << classname << "::" << methodname << "()\n";
54 return false;
55 }
56 return true;
57}

References geant4_check_module_cycles::file.

Referenced by CLHEP::DualRand::restoreStatus(), CLHEP::HepJamesRandom::restoreStatus(), CLHEP::MTwistEngine::restoreStatus(), CLHEP::RanecuEngine::restoreStatus(), CLHEP::RanluxEngine::restoreStatus(), CLHEP::Ranlux64Engine::restoreStatus(), and CLHEP::RanshiEngine::restoreStatus().

◆ engineName()

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

Definition at line 221 of file RanluxppEngine.cc.

221{ return "RanluxppEngine"; }

Referenced by name().

◆ exponent_bit_32()

static double CLHEP::HepRandomEngine::exponent_bit_32 ( )
inlinestaticprotectedinherited

◆ flat()

double CLHEP::RanluxppEngine::flat ( )
overridevirtual

Implements CLHEP::HepRandomEngine.

Definition at line 111 of file RanluxppEngine.cc.

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

References CLHEP::kBits, and nextRandomBits().

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 123 of file RanluxppEngine.cc.

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

References flat().

◆ get() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 277 of file RanluxppEngine.cc.

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

References getState().

◆ get() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 234 of file RanluxppEngine.cc.

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

References beginTag(), and getState().

Referenced by RanluxppEngine(), and restoreStatus().

◆ getSeed()

long CLHEP::HepRandomEngine::getSeed ( ) const
inlineinherited

Definition at line 112 of file RandomEngine.h.

112{ return theSeed; }

References CLHEP::HepRandomEngine::theSeed.

◆ getSeeds()

const long * CLHEP::HepRandomEngine::getSeeds ( ) const
inlineinherited

Definition at line 115 of file RandomEngine.h.

115{ return theSeeds; }

References CLHEP::HepRandomEngine::theSeeds.

◆ getState() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 286 of file RanluxppEngine.cc.

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

References fCarry, fPosition, fState, and VECTOR_STATE_SIZE.

◆ getState() [2/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 245 of file RanluxppEngine.cc.

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

References getState(), and VECTOR_STATE_SIZE.

Referenced by get(), and getState().

◆ mantissa_bit_12()

static double CLHEP::HepRandomEngine::mantissa_bit_12 ( )
inlinestaticprotectedinherited

◆ mantissa_bit_24()

static double CLHEP::HepRandomEngine::mantissa_bit_24 ( )
inlinestaticprotectedinherited

◆ mantissa_bit_32()

static double CLHEP::HepRandomEngine::mantissa_bit_32 ( )
inlinestaticprotectedinherited

◆ name()

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

◆ nearlyTwoToMinus_54()

static double CLHEP::HepRandomEngine::nearlyTwoToMinus_54 ( )
inlinestaticprotectedinherited

◆ newEngine() [1/2]

HepRandomEngine * CLHEP::HepRandomEngine::newEngine ( const std::vector< unsigned long > &  v)
staticinherited

Definition at line 96 of file RandomEngine.cc.

96 {
98}
static HepRandomEngine * newEngine(std::istream &is)

References CLHEP::EngineFactory::newEngine().

◆ newEngine() [2/2]

HepRandomEngine * CLHEP::HepRandomEngine::newEngine ( std::istream &  is)
staticinherited

Definition at line 91 of file RandomEngine.cc.

91 {
92 return EngineFactory::newEngine(is);
93}

References CLHEP::EngineFactory::newEngine().

Referenced by CLHEP::StaticRandomStates::restore().

◆ nextRandomBits()

uint64_t CLHEP::RanluxppEngine::nextRandomBits ( )
private

Definition at line 90 of file RanluxppEngine.cc.

90 {
91 if (fPosition + kBits > kMaxPos) {
92 advance();
93 }
94
95 int idx = fPosition / 64;
96 int offset = fPosition % 64;
97 int numBits = 64 - offset;
98
99 uint64_t bits = fState[idx] >> offset;
100 if (numBits < kBits) {
101 bits |= fState[idx + 1] << numBits;
102 }
103 bits &= ((uint64_t(1) << kBits) - 1);
104
105 fPosition += kBits;
106 assert(fPosition <= kMaxPos && "position out of range!");
107
108 return bits;
109}
static constexpr int kMaxPos

References advance(), fPosition, fState, CLHEP::kBits, and CLHEP::kMaxPos.

Referenced by flat(), and operator unsigned int().

◆ operator double()

CLHEP::RanluxppEngine::operator double ( )
inlineoverridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 84 of file RanluxppEngine.h.

84{ return flat(); }

References flat().

◆ operator float()

CLHEP::RanluxppEngine::operator float ( )
inlineoverridevirtual

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 85 of file RanluxppEngine.h.

85{ return float(flat()); }

References flat().

◆ operator unsigned int()

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 86 of file RanluxppEngine.h.

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

References nextRandomBits().

◆ operator!=()

bool CLHEP::HepRandomEngine::operator!= ( const HepRandomEngine engine)
inlineinherited

◆ operator==()

bool CLHEP::HepRandomEngine::operator== ( const HepRandomEngine engine)
inlineinherited

◆ put() [1/2]

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

Reimplemented from CLHEP::HepRandomEngine.

Definition at line 258 of file RanluxppEngine.cc.

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

References fCarry, fPosition, fState, and VECTOR_STATE_SIZE.

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 225 of file RanluxppEngine.cc.

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

References beginTag(), and put().

◆ restoreStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 190 of file RanluxppEngine.cc.

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

References get().

◆ saveStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 184 of file RanluxppEngine.cc.

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

References put().

◆ setSeed()

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

Implements CLHEP::HepRandomEngine.

Definition at line 129 of file RanluxppEngine.cc.

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

References fCarry, fPosition, fState, CLHEP::anonymous_namespace{RanluxppEngine.cc}::kA_2048, mulmod(), powermod(), CLHEP::HepRandomEngine::theSeed, and to_ranlux().

Referenced by RanluxppEngine(), and setSeeds().

◆ setSeeds()

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

Implements CLHEP::HepRandomEngine.

Definition at line 150 of file RanluxppEngine.cc.

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

References setSeed(), and CLHEP::HepRandomEngine::theSeeds.

◆ showStatus()

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

Implements CLHEP::HepRandomEngine.

Definition at line 196 of file RanluxppEngine.cc.

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

References fCarry, fPosition, and fState.

◆ skip()

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

Definition at line 155 of file RanluxppEngine.cc.

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

References fCarry, fPosition, fState, CLHEP::anonymous_namespace{RanluxppEngine.cc}::kA_2048, CLHEP::kBits, CLHEP::kMaxPos, mulmod(), CLHEP::detail::n, powermod(), skip(), to_lcg(), and to_ranlux().

Referenced by skip().

◆ twoToMinus_32()

static double CLHEP::HepRandomEngine::twoToMinus_32 ( )
inlinestaticprotectedinherited

◆ twoToMinus_48()

static double CLHEP::HepRandomEngine::twoToMinus_48 ( )
inlinestaticprotectedinherited

◆ twoToMinus_49()

static double CLHEP::HepRandomEngine::twoToMinus_49 ( )
inlinestaticprotectedinherited

◆ twoToMinus_53()

static double CLHEP::HepRandomEngine::twoToMinus_53 ( )
inlinestaticprotectedinherited

Field Documentation

◆ fCarry

unsigned CLHEP::RanluxppEngine::fCarry
private

Carry bit of the RANLUX state.

Definition at line 97 of file RanluxppEngine.h.

Referenced by advance(), getState(), put(), setSeed(), showStatus(), and skip().

◆ fPosition

int CLHEP::RanluxppEngine::fPosition = 0
private

Current position in bits.

Definition at line 98 of file RanluxppEngine.h.

Referenced by advance(), getState(), nextRandomBits(), put(), setSeed(), showStatus(), and skip().

◆ fState

uint64_t CLHEP::RanluxppEngine::fState[9]
private

RANLUX state of the generator.

Definition at line 96 of file RanluxppEngine.h.

Referenced by advance(), getState(), nextRandomBits(), put(), setSeed(), showStatus(), and skip().

◆ theSeed

long CLHEP::HepRandomEngine::theSeed
protectedinherited

Definition at line 142 of file RandomEngine.h.

Referenced by CLHEP::DualRand::DualRand(), CLHEP::HepRandomEngine::getSeed(), CLHEP::DualRand::getState(), CLHEP::HepJamesRandom::getState(), CLHEP::MixMaxRng::getState(), CLHEP::MTwistEngine::getState(), CLHEP::RanecuEngine::getState(), CLHEP::Ranlux64Engine::getState(), CLHEP::RanluxEngine::getState(), CLHEP::RanshiEngine::getState(), CLHEP::HepJamesRandom::HepJamesRandom(), CLHEP::MixMaxRng::MixMaxRng(), CLHEP::RanecuEngine::put(), CLHEP::MixMaxRng::put(), CLHEP::MTwistEngine::put(), CLHEP::RanecuEngine::RanecuEngine(), CLHEP::RanluxEngine::RanluxEngine(), RanluxppEngine(), CLHEP::RanshiEngine::RanshiEngine(), CLHEP::DualRand::restoreStatus(), CLHEP::HepJamesRandom::restoreStatus(), CLHEP::MTwistEngine::restoreStatus(), CLHEP::RanecuEngine::restoreStatus(), CLHEP::RanluxEngine::restoreStatus(), CLHEP::Ranlux64Engine::restoreStatus(), CLHEP::RanshiEngine::restoreStatus(), CLHEP::MTwistEngine::saveStatus(), CLHEP::RanecuEngine::setIndex(), CLHEP::RanecuEngine::setSeed(), CLHEP::HepJamesRandom::setSeed(), CLHEP::MixMaxRng::setSeed(), setSeed(), CLHEP::Ranlux64Engine::setSeed(), CLHEP::RanluxEngine::setSeed(), CLHEP::DualRand::setSeed(), CLHEP::MTwistEngine::setSeed(), CLHEP::RanecuEngine::setSeeds(), CLHEP::Ranlux64Engine::setSeeds(), CLHEP::RanluxEngine::setSeeds(), CLHEP::MixMaxRng::setSeeds(), CLHEP::RanshiEngine::setSeeds(), CLHEP::DualRand::showStatus(), CLHEP::HepJamesRandom::showStatus(), CLHEP::MTwistEngine::showStatus(), CLHEP::RanecuEngine::showStatus(), CLHEP::Ranlux64Engine::showStatus(), CLHEP::RanluxEngine::showStatus(), and CLHEP::RanshiEngine::showStatus().

◆ theSeeds

const long* CLHEP::HepRandomEngine::theSeeds
protectedinherited

◆ VECTOR_STATE_SIZE

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

Definition at line 90 of file RanluxppEngine.h.

Referenced by getState(), and put().


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