www.mooseframework.org
Public Member Functions | Private Member Functions | Private Attributes | List of all members
RandomData Class Reference

#include <RandomData.h>

Public Member Functions

 RandomData (FEProblemBase &problem, const RandomInterface &random_interface)
 
void updateSeeds (ExecFlagType exec_flag)
 This method is called to reset or update the seeds based on the reset_on flag and the passed execution point. More...
 
MooseRandomgetGenerator ()
 Return the underlying MooseRandom generator object for this data instance. More...
 
unsigned int getSeed (dof_id_type id)
 Get the seed for the passed in elem/node id. More...
 

Private Member Functions

void updateGenerators ()
 
template<typename T >
void updateGeneratorHelper (T it, T end_it)
 

Private Attributes

FEProblemBase_rd_problem
 
MooseMesh_rd_mesh
 
MooseRandom _generator
 
bool _is_nodal
 
ExecFlagType _reset_on
 
unsigned int _master_seed
 
unsigned int _current_master_seed
 
unsigned int _new_seed
 
std::unordered_map< dof_id_type, unsigned int > _seeds
 

Detailed Description

Definition at line 28 of file RandomData.h.

Constructor & Destructor Documentation

RandomData::RandomData ( FEProblemBase problem,
const RandomInterface random_interface 
)

Definition at line 22 of file RandomData.C.

23  : _rd_problem(problem),
24  _rd_mesh(problem.mesh()),
25  _is_nodal(random_interface.isNodal()),
26  _reset_on(random_interface.getResetOnTime()),
27  _master_seed(random_interface.getMasterSeed()),
28  _current_master_seed(std::numeric_limits<unsigned int>::max()),
29  _new_seed(0)
30 {
31 }
unsigned int _new_seed
Definition: RandomData.h:66
MooseMesh & _rd_mesh
Definition: RandomData.h:58
unsigned int _master_seed
Definition: RandomData.h:64
bool isNodal() const
bool _is_nodal
Definition: RandomData.h:61
unsigned int _current_master_seed
Definition: RandomData.h:65
ExecFlagType getResetOnTime() const
unsigned int getMasterSeed() const
ExecFlagType _reset_on
Definition: RandomData.h:62
virtual MooseMesh & mesh() override
FEProblemBase & _rd_problem
Definition: RandomData.h:57

Member Function Documentation

MooseRandom& RandomData::getGenerator ( )
inline

Return the underlying MooseRandom generator object for this data instance.

Definition at line 42 of file RandomData.h.

Referenced by RandomInterface::setRandomDataPointer().

42 { return _generator; }
MooseRandom _generator
Definition: RandomData.h:60
unsigned int RandomData::getSeed ( dof_id_type  id)

Get the seed for the passed in elem/node id.

Parameters
id- dof object id
Returns
current seed for this id

Definition at line 34 of file RandomData.C.

Referenced by getGenerator(), and RandomInterface::getSeed().

35 {
36  mooseAssert(_seeds.find(id) != _seeds.end(),
37  "Call to updateSeeds() is stale! Check your initialize() or timestepSetup() calls");
38 
39  return _seeds[id];
40 }
std::unordered_map< dof_id_type, unsigned int > _seeds
Definition: RandomData.h:68
template<typename T >
void RandomData::updateGeneratorHelper ( it,
end_it 
)
private

Definition at line 109 of file RandomData.C.

Referenced by getGenerator(), and updateGenerators().

110 {
111  processor_id_type processor_id = _rd_problem.processor_id();
112 
113  for (; it != end_it; ++it)
114  {
115  dof_id_type id = (*it)->id();
116  uint32_t rand_int = _generator.randl(MASTER);
117 
118  if (processor_id == (*it)->processor_id())
119  {
120  // Only save states for local dofs
121  _seeds[id] = rand_int;
122 
123  // Update the individual dof object generators
124  _generator.seed(static_cast<unsigned int>(id), _seeds[id]);
125  }
126  }
127 }
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:76
std::unordered_map< dof_id_type, unsigned int > _seeds
Definition: RandomData.h:68
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
MooseRandom _generator
Definition: RandomData.h:60
FEProblemBase & _rd_problem
Definition: RandomData.h:57
const unsigned int MASTER
Definition: RandomData.C:20
void RandomData::updateGenerators ( )
private

When using parallel mesh it's not worth generating parallel consistent numbers. Each processor may not be aware of which entities belong on another mesh. We do have to be careful to not generate the same patterns on different processors however. To do that, we will use the MASTER generator to generate new master seeds for each processor based on their individual processor ids before generating seeds for the mesh entities.

Definition at line 74 of file RandomData.C.

Referenced by getGenerator(), and updateSeeds().

75 {
76  // Set the master seed and repopulate all of the child generators
78 
88  {
89  unsigned int parallel_seed = 0;
90  for (processor_id_type proc_id = 0; proc_id < _rd_problem.n_processors(); ++proc_id)
91  if (proc_id == _rd_problem.processor_id())
92  parallel_seed = _generator.randl(MASTER);
93  else
94  _generator.randl(MASTER); // Generate but throw away numbers that aren't mine
95 
96  _generator.seed(MASTER, parallel_seed);
97  }
98 
99  if (_is_nodal)
100  updateGeneratorHelper(_rd_mesh.getMesh().active_nodes_begin(),
101  _rd_mesh.getMesh().active_nodes_end());
102  else
103  updateGeneratorHelper(_rd_mesh.getMesh().active_elements_begin(),
104  _rd_mesh.getMesh().active_elements_end());
105 }
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:76
MooseMesh & _rd_mesh
Definition: RandomData.h:58
bool _is_nodal
Definition: RandomData.h:61
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2355
unsigned int _current_master_seed
Definition: RandomData.h:65
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
MooseRandom _generator
Definition: RandomData.h:60
FEProblemBase & _rd_problem
Definition: RandomData.h:57
void updateGeneratorHelper(T it, T end_it)
Definition: RandomData.C:109
bool isDistributedMesh() const
Returns the final Mesh distribution type.
Definition: MooseMesh.h:737
const unsigned int MASTER
Definition: RandomData.C:20
void RandomData::updateSeeds ( ExecFlagType  exec_flag)

This method is called to reset or update the seeds based on the reset_on flag and the passed execution point.

Set the seed. This part is critical! If this is done incorrectly, it may lead to difficult to detect patterns in your random numbers either within a single run or over the course of several runs. We will default to _master_seed + the current time step.

case EXEC_TIMESTEP_BEGIN: // reset and advance every timestep case EXEC_TIMESTEP_END: // reset and advance every timestep case EXEC_LINEAR: // Reset every residual, advance every timestep case EXEC_NONLINEAR: // Reset every Jacobian, advance every timestep

Definition at line 43 of file RandomData.C.

44 {
50  if (exec_flag == EXEC_INITIAL)
52  else
61  // If the _new_seed has been updated, we need to update all of the generators
63  {
66  _generator.saveState(); // Save states so that we can reset on demand
67  }
68 
69  if (_reset_on == exec_flag)
70  _generator.restoreState(); // Restore states here
71 }
unsigned int _new_seed
Definition: RandomData.h:66
Object is evaluated only once at the beginning of the simulation.
Definition: MooseTypes.h:94
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:131
void updateGenerators()
Definition: RandomData.C:74
unsigned int _master_seed
Definition: RandomData.h:64
virtual int & timeStep() const
unsigned int _current_master_seed
Definition: RandomData.h:65
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:144
MooseRandom _generator
Definition: RandomData.h:60
ExecFlagType _reset_on
Definition: RandomData.h:62
FEProblemBase & _rd_problem
Definition: RandomData.h:57

Member Data Documentation

unsigned int RandomData::_current_master_seed
private

Definition at line 65 of file RandomData.h.

Referenced by updateGenerators(), and updateSeeds().

MooseRandom RandomData::_generator
private

Definition at line 60 of file RandomData.h.

Referenced by getGenerator(), updateGeneratorHelper(), updateGenerators(), and updateSeeds().

bool RandomData::_is_nodal
private

Definition at line 61 of file RandomData.h.

Referenced by updateGenerators().

unsigned int RandomData::_master_seed
private

Definition at line 64 of file RandomData.h.

Referenced by updateSeeds().

unsigned int RandomData::_new_seed
private

Definition at line 66 of file RandomData.h.

Referenced by updateSeeds().

MooseMesh& RandomData::_rd_mesh
private

Definition at line 58 of file RandomData.h.

Referenced by updateGenerators().

FEProblemBase& RandomData::_rd_problem
private

Definition at line 57 of file RandomData.h.

Referenced by updateGeneratorHelper(), updateGenerators(), and updateSeeds().

ExecFlagType RandomData::_reset_on
private

Definition at line 62 of file RandomData.h.

Referenced by updateSeeds().

std::unordered_map<dof_id_type, unsigned int> RandomData::_seeds
private

Definition at line 68 of file RandomData.h.

Referenced by getSeed(), and updateGeneratorHelper().


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