www.mooseframework.org
MooseRandom.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef MOOSERANDOM_H
16 #define MOOSERANDOM_H
17 
18 // MOOSE includes
19 #include "MooseError.h"
20 #include "DataIO.h"
21 
22 #include <unordered_map>
23 
24 // External library includes
25 #include "randistrs.h"
26 
43 {
44 public:
49  static inline void seed(unsigned int seed) { mt_seed32new(seed); }
50 
55  static inline double rand() { return mt_ldrand(); }
56 
65  static inline double randNormal(double mean, double sigma) { return rd_normal(mean, sigma); }
66 
70  static inline double randNormal() { return randNormal(0.0, 1.0); }
71 
76  static inline uint32_t randl() { return mt_lrand(); }
77 
83  inline void seed(unsigned int i, unsigned int seed) { mts_seed32new(&(_states[i].first), seed); }
84 
90  inline double rand(unsigned int i)
91  {
92  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
93  return mts_ldrand(&(_states[i].first));
94  }
95 
105  inline double randNormal(unsigned int i, double mean, double sigma)
106  {
107  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
108  return rds_normal(&(_states[i].first), mean, sigma);
109  }
110 
114  inline double randNormal(unsigned int i) { return randNormal(i, 0.0, 1.0); }
115 
121  inline uint32_t randl(unsigned int i)
122  {
123  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
124  return mts_lrand(&(_states[i].first));
125  }
126 
131  void saveState()
132  {
133  _saved = true;
134  std::for_each(_states.begin(),
135  _states.end(),
136  [](std::pair<const unsigned int, std::pair<mt_state, mt_state>> & pair) {
137  pair.second.second = pair.second.first;
138  });
139  }
140 
145  {
146  mooseAssert(_saved, "saveState() must be called prior to restoreState().");
147  std::for_each(_states.begin(),
148  _states.end(),
149  [](std::pair<const unsigned int, std::pair<mt_state, mt_state>> & pair) {
150  pair.second.first = pair.second.second;
151  });
152  }
153 
157  inline unsigned int size() { return _states.size(); }
158 
159 private:
165  std::unordered_map<unsigned int, std::pair<mt_state, mt_state>> _states;
166 
167  // for restart capability
168  friend void dataStore<MooseRandom>(std::ostream & stream, MooseRandom & v, void * context);
169  friend void dataLoad<MooseRandom>(std::istream & stream, MooseRandom & v, void * context);
170 
172  bool _saved = false;
173 };
174 
175 template <>
176 inline void
177 dataStore(std::ostream & stream, MooseRandom & v, void * context)
178 {
179  storeHelper(stream, v._states, context);
180 }
181 template <>
182 inline void
183 dataLoad(std::istream & stream, MooseRandom & v, void * context)
184 {
185  loadHelper(stream, v._states, context);
186 }
187 
188 #endif // MOOSERANDOM_H
void seed(unsigned int i, unsigned int seed)
The method seeds one of the independent random number generators.
Definition: MooseRandom.h:83
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:76
static double rand()
This method returns the next random number (double format) from the generator.
Definition: MooseRandom.h:55
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:172
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:131
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
double rand(unsigned int i)
This method returns the next random number (double format) from the specified generator.
Definition: MooseRandom.h:90
friend void dataStore(std::ostream &stream, MooseRandom &v, void *context)
Definition: MooseRandom.h:177
double randNormal(unsigned int i, double mean, double sigma)
This method returns the next random number (double format) from the specified generator, drawn from a normal distribution centered around mean, with a width of sigma.
Definition: MooseRandom.h:105
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:528
friend void dataLoad(std::istream &stream, MooseRandom &v, void *context)
Definition: MooseRandom.h:183
unsigned int size()
Return the number of states.
Definition: MooseRandom.h:157
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:144
static double randNormal()
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:70
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
static double randNormal(double mean, double sigma)
This method returns the next random number (double format) from the generator, drawn from a normal di...
Definition: MooseRandom.h:65
double randNormal(unsigned int i)
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:114
uint32_t randl(unsigned int i)
This method returns the next random number (long format) from the specified generator.
Definition: MooseRandom.h:121
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:592
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
Definition: MooseRandom.h:42