Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
MixMaxRng.h
이 파일의 문서화 페이지로 가기
1 //
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- MixMaxRng ---
7 // class header file
8 // -----------------------------------------------------------------------
9 //
10 // This file interfaces the MixMax PseudoRandom Number Generator
11 // proposed by:
12 //
13 // G.K.Savvidy and N.G.Ter-Arutyunian,
14 // On the Monte Carlo simulation of physical systems,
15 // J.Comput.Phys. 97, 566 (1991);
16 // Preprint EPI-865-16-86, Yerevan, Jan. 1986
17 // http://dx.doi.org/10.1016/0021-9991(91)90015-D
18 //
19 // K.Savvidy
20 // "The MIXMAX random number generator"
21 // Comp. Phys. Commun. (2015)
22 // http://dx.doi.org/10.1016/j.cpc.2015.06.003
23 //
24 // K.Savvidy and G.Savvidy
25 // "Spectrum and Entropy of C-systems. MIXMAX random number generator"
26 // Chaos, Solitons & Fractals, Volume 91, (2016) pp. 33-38
27 // http://dx.doi.org/10.1016/j.chaos.2016.05.003
28 //
29 // =======================================================================
30 // Implementation by Konstantin Savvidy - Copyright 2004-2017
31 // =======================================================================
32 
33 #ifndef MixMaxRng_h
34 #define MixMaxRng_h 1
35 
36 #include <array>
38 
39 namespace CLHEP {
40 
46 typedef unsigned long int myID_t;
47 typedef unsigned long long int myuint_t;
48 
49 class MixMaxRng: public HepRandomEngine {
50 
51  static const int N = 17;
52 
53 public:
54 
55  MixMaxRng(std::istream& is);
56  MixMaxRng();
57  MixMaxRng(long seed);
58  MixMaxRng(int rowIndex, int colIndex);
59  ~MixMaxRng();
60  // Constructor and destructor.
61 
62  MixMaxRng(const MixMaxRng& rng);
63  MixMaxRng& operator=(const MixMaxRng& rng);
64  // Copy constructor and assignment operator.
65 
66  double flat() { return (S.counter<=(N-1)) ? generate(S.counter):iterate(); }
67  // Returns a pseudo random number between 0 and 1
68  // (excluding the zero: in (0,1] )
69  // smallest number which it will give is approximately 10^-19
70 
71  void flatArray (const int size, double* vect);
72  // Fills the array "vect" of specified size with flat random values.
73 
74  void setSeed(long seed, int dum=0);
75  // Sets the state of the algorithm according to seed.
76 
77  void setSeeds(const long * seeds, int seedNum=0);
78  // Sets the initial state of the engine according to the array of between one and four 32-bit seeds.
79  // If the size of long is greater on the platform, only the lower 32-bits are used.
80  // Streams created from seeds differing by at least one bit somewhere are guaranteed absolutely
81  // to be independent and non-colliding for at least the next 10^100 random numbers
82 
83  void saveStatus( const char filename[] = "MixMaxRngState.conf" ) const;
84  // Saves the the current engine state in the file given, by default MixMaxRngState.conf
85 
86  void restoreStatus( const char filename[] = "MixMaxRngState.conf" );
87  // Reads a valid engine state from a given file, by default MixMaxRngState.conf
88  // and restores it.
89 
90  void showStatus() const;
91  // Dumps the engine status on the screen.
92 
93  operator double();
94  // Returns same as flat()
95  operator float();
96  // less precise flat, faster if possible
97  operator unsigned int();
98  // 32-bit flat
99 
100  virtual std::ostream & put (std::ostream & os) const;
101  virtual std::istream & get (std::istream & is);
102  static std::string beginTag ( );
103  virtual std::istream & getState ( std::istream & is );
104 
105  std::string name() const { return "MixMaxRng"; }
106  static std::string engineName();
107 
108  std::vector<unsigned long> put () const;
109  bool get (const std::vector<unsigned long> & v);
110  bool getState (const std::vector<unsigned long> & v);
111 
112 private:
113 
114  static constexpr long long int SPECIAL = ((N==17)? 0 : ((N==240)? 487013230256099140ULL:0) ); // etc...
115  static constexpr long long int SPECIALMUL= ((N==17)? 36: ((N==240)? 51 :53) ); // etc...
116  // Note the potential for confusion...
117  static constexpr int BITS=61;
118  static constexpr myuint_t M61=2305843009213693951ULL;
119  static constexpr double INV_M61=0.43368086899420177360298E-18;
120  static constexpr unsigned int VECTOR_STATE_SIZE = 2*N+4; // 2N+4 for MIXMAX
121 
122  #define MIXMAX_MOD_MERSENNE(k) ((((k)) & M61) + (((k)) >> BITS) )
123 
124  static constexpr int rng_get_N();
125  static constexpr long long int rng_get_SPECIAL();
126  static constexpr int rng_get_SPECIALMUL();
127  void seed_uniquestream( myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
128  void seed_spbox(myuint_t);
129  void print_state() const;
130  myuint_t precalc();
131  myuint_t get_next() ;
132  inline double get_next_float() { return get_next_float_packbits(); }
133  // Returns a random double with all 52 bits random, in the range (0,1]
134 
135  MixMaxRng Branch();
136  void BranchInplace(int id);
137 
138  MixMaxRng(myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID ); // Constructor with four 32-bit seeds
139  inline void seed64(myuint_t seedval) { seed_uniquestream( 0, 0, (myID_t)(seedval>>32), (myID_t)seedval ); } // seed with one 64-bit seed
140 
141  double generate(int i);
142  double iterate();
143 
144  double get_next_float_packbits();
145 #if defined __GNUC__
146 #pragma GCC diagnostic push
147 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
148 #endif
149  inline double convert1double(myuint_t u)
150  {
151  const double one = 1;
152  const myuint_t onemask = *(myuint_t*)&one;
153  myuint_t tmp = (u>>9) | onemask; // bits between 52 and 62 dont affect the result!
154  double d = *(double*)&tmp;
155  return d-1.0;
156  }
157 #if defined __GNUC__
158 #pragma GCC diagnostic pop
159 #endif
162  void seed_vielbein( unsigned int i); // seeds with the i-th unit vector, i = 0..N-1, for testing only
164  myuint_t apply_bigskip(myuint_t* Vout, myuint_t* Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
166 #if defined(__x86_64__)
167  myuint_t mod128(__uint128_t s);
169 #else // on all other platforms, including 32-bit linux, PPC and PPC64, ARM and all Windows
171 #endif
172 
173 private:
174 
176  {
177  std::array<myuint_t, N> V;
179  int counter;
180  };
181 
182  typedef struct rng_state_st rng_state_t; // struct alias
184 };
185 
186 } // namespace CLHEP
187 
188 #endif
double convert1double(myuint_t u)
Definition: MixMaxRng.h:149
void BranchInplace(int id)
Definition: MixMaxRng.cc:756
static constexpr long long int SPECIAL
Definition: MixMaxRng.h:114
unsigned long long int myuint_t
Definition: MixMaxRng.h:47
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
virtual std::istream & getState(std::istream &is)
Definition: MixMaxRng.cc:416
static std::string beginTag()
Definition: MixMaxRng.cc:411
MixMaxRng Branch()
Definition: MixMaxRng.cc:748
unsigned long int myID_t
Definition: MixMaxRng.h:46
myuint_t precalc()
Definition: MixMaxRng.cc:544
myuint_t apply_bigskip(myuint_t *Vout, myuint_t *Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
Definition: MixMaxRng.cc:616
Float_t Y
void restoreStatus(const char filename[]="MixMaxRngState.conf")
Definition: MixMaxRng.cc:127
Float_t tmp
std::string name() const
Definition: MixMaxRng.h:105
static constexpr unsigned int VECTOR_STATE_SIZE
Definition: MixMaxRng.h:120
myuint_t MOD_MULSPEC(myuint_t k)
Definition: MixMaxRng.cc:481
std::array< myuint_t, N > V
Definition: MixMaxRng.h:177
MixMaxRng & operator=(const MixMaxRng &rng)
Definition: MixMaxRng.cc:84
void seed64(myuint_t seedval)
Definition: MixMaxRng.h:139
std::vector< unsigned long > put() const
Definition: MixMaxRng.cc:376
const XML_Char * s
Definition: expat.h:262
myuint_t iterate_raw_vec(myuint_t *Y, myuint_t sumtotOld)
Definition: MixMaxRng.cc:506
void setSeeds(const long *seeds, int seedNum=0)
Definition: MixMaxRng.cc:234
double generate(int i)
Definition: MixMaxRng.cc:281
myuint_t fmodmulM61(myuint_t cum, myuint_t s, myuint_t a)
Definition: MixMaxRng.cc:700
long seed
Definition: chem4.cc:68
double get_next_float_packbits()
Definition: MixMaxRng.cc:556
void seed_vielbein(unsigned int i)
Definition: MixMaxRng.cc:562
static constexpr double INV_M61
Definition: MixMaxRng.h:119
void showStatus() const
Definition: MixMaxRng.cc:213
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
Float_t d
void saveStatus(const char filename[]="MixMaxRngState.conf") const
Definition: MixMaxRng.cc:101
static constexpr myuint_t M61
Definition: MixMaxRng.h:118
double get_next_float()
Definition: MixMaxRng.h:132
static constexpr int rng_get_SPECIALMUL()
Definition: MixMaxRng.cc:276
static constexpr long long int rng_get_SPECIAL()
Definition: MixMaxRng.cc:271
myuint_t MULWU(myuint_t k)
Definition: MixMaxRng.cc:501
double flat()
Definition: MixMaxRng.h:66
void print_state() const
Definition: MixMaxRng.cc:734
void flatArray(const int size, double *vect)
Definition: MixMaxRng.cc:335
double iterate()
Definition: MixMaxRng.cc:305
static constexpr double bar
myuint_t modadd(myuint_t foo, myuint_t bar)
Definition: MixMaxRng.cc:716
void seed_uniquestream(myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
Definition: MixMaxRng.cc:609
static constexpr int BITS
Definition: MixMaxRng.h:117
static const int N
Definition: MixMaxRng.h:51
static std::string engineName()
Definition: MixMaxRng.cc:261
rng_state_t S
Definition: MixMaxRng.h:183
myuint_t get_next()
Definition: MixMaxRng.cc:526
void setSeed(long seed, int dum=0)
Definition: MixMaxRng.cc:223
static constexpr int rng_get_N()
Definition: MixMaxRng.cc:266
static constexpr long long int SPECIALMUL
Definition: MixMaxRng.h:115
void seed_spbox(myuint_t)
Definition: MixMaxRng.cc:582