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 &fe_problem, const RandomInterface &random_interface)
 
 RandomData (FEProblemBase &fe_problem, bool is_nodal, ExecFlagType reset_on, unsigned int seed)
 
 ~RandomData ()=default
 
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 ()
 

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 24 of file RandomData.h.

Constructor & Destructor Documentation

◆ RandomData() [1/2]

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

Definition at line 17 of file RandomData.C.

18  : RandomData(fe_problem,
19  random_interface.isNodal(),
20  random_interface.getResetOnTime(),
21  random_interface.getMasterSeed())
22 {
23 }
ExecFlagType getResetOnTime() const
bool isNodal() const
unsigned int getMasterSeed() const
RandomData(FEProblemBase &fe_problem, const RandomInterface &random_interface)
Definition: RandomData.C:17

◆ RandomData() [2/2]

RandomData::RandomData ( FEProblemBase fe_problem,
bool  is_nodal,
ExecFlagType  reset_on,
unsigned int  seed 
)

Definition at line 25 of file RandomData.C.

29  : _rd_problem(fe_problem),
30  _rd_mesh(fe_problem.mesh()),
31  _is_nodal(is_nodal),
32  _reset_on(reset_on),
33  _master_seed(seed),
34  _current_master_seed(std::numeric_limits<unsigned int>::max()),
35  _new_seed(0)
36 {
37 }
unsigned int _new_seed
Definition: RandomData.h:63
MooseMesh & _rd_mesh
Definition: RandomData.h:55
unsigned int _master_seed
Definition: RandomData.h:61
bool _is_nodal
Definition: RandomData.h:58
unsigned int _current_master_seed
Definition: RandomData.h:62
ExecFlagType _reset_on
Definition: RandomData.h:59
virtual MooseMesh & mesh() override
FEProblemBase & _rd_problem
Definition: RandomData.h:54

◆ ~RandomData()

RandomData::~RandomData ( )
default

Member Function Documentation

◆ getGenerator()

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:57

◆ getSeed()

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 40 of file RandomData.C.

Referenced by RandomInterface::getSeed().

41 {
42  mooseAssert(_seeds.find(id) != _seeds.end(),
43  "Call to updateSeeds() is stale! Check your initialize() or timestepSetup() calls");
44 
45  return _seeds[id];
46 }
std::unordered_map< dof_id_type, unsigned int > _seeds
Definition: RandomData.h:65

◆ updateGenerators()

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 80 of file RandomData.C.

Referenced by updateSeeds().

81 {
82  // Set the master seed and repopulate all of the child generators
84 
94  {
95  unsigned int parallel_seed = 0;
96  for (processor_id_type proc_id = 0; proc_id < _rd_problem.n_processors(); ++proc_id)
97  if (proc_id == _rd_problem.processor_id())
98  parallel_seed = _generator.randl(MASTER);
99  else
100  _generator.randl(MASTER); // Generate but throw away numbers that aren't mine
101 
102  _generator.seed(MASTER, parallel_seed);
103  }
104 
105  auto processor_id = _rd_problem.processor_id();
106 
107  if (_is_nodal)
108  {
109  const auto & node_to_elem = _rd_mesh.nodeToElemMap();
110  auto & mesh = _rd_mesh.getMesh();
111 
112  for (const auto node_ptr :
113  as_range(_rd_mesh.getMesh().active_nodes_begin(), _rd_mesh.getMesh().active_nodes_end()))
114  {
115  auto id = node_ptr->id();
116  auto rand_int = _generator.randl(MASTER);
117 
118  // Only save states for nodes attached to active elements
119  auto elem_id_it = node_to_elem.find(id);
120 
121  if (elem_id_it != node_to_elem.end())
122  {
123  for (auto elem_id : elem_id_it->second)
124  {
125  const auto * elem_ptr = mesh.elem_ptr(elem_id);
126 
127  if (elem_ptr && processor_id == elem_ptr->processor_id())
128  {
129  _seeds[id] = rand_int;
130 
131  // Update the individual dof object generators
132  _generator.seed(id, rand_int);
133  break;
134  }
135  }
136  }
137  }
138  }
139  else
140  {
141  for (const auto & elem_ptr : as_range(_rd_mesh.getMesh().active_elements_begin(),
142  _rd_mesh.getMesh().active_elements_end()))
143  {
144  auto id = elem_ptr->id();
145  auto rand_int = _generator.randl(MASTER);
146 
147  // Only save states for local elements
148  if (processor_id == elem_ptr->processor_id())
149  {
150  _seeds[id] = rand_int;
151 
152  // Update the individual dof object generators
153  _generator.seed(id, rand_int);
154  }
155  }
156  }
157 }
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:71
std::unordered_map< dof_id_type, unsigned int > _seeds
Definition: RandomData.h:65
bool isDistributedMesh() const
Returns the final Mesh distribution type.
Definition: MooseMesh.h:750
MooseMesh & _rd_mesh
Definition: RandomData.h:55
bool _is_nodal
Definition: RandomData.h:58
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2512
unsigned int _current_master_seed
Definition: RandomData.h:62
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:44
MooseRandom _generator
Definition: RandomData.h:57
FEProblemBase & _rd_problem
Definition: RandomData.h:54
const std::map< dof_id_type, std::vector< dof_id_type > > & nodeToElemMap()
If not already created, creates a map from every node to all elements to which they are connected...
Definition: MooseMesh.C:732
const unsigned int MASTER
Definition: RandomData.C:15

◆ updateSeeds()

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 49 of file RandomData.C.

50 {
56  if (exec_flag == EXEC_INITIAL)
58  else
67  // If the _new_seed has been updated, we need to update all of the generators
69  {
72  _generator.saveState(); // Save states so that we can reset on demand
73  }
74 
75  if (_reset_on == exec_flag)
76  _generator.restoreState(); // Restore states here
77 }
unsigned int _new_seed
Definition: RandomData.h:63
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:126
void updateGenerators()
Definition: RandomData.C:80
unsigned int _master_seed
Definition: RandomData.h:61
unsigned int _current_master_seed
Definition: RandomData.h:62
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:139
virtual int & timeStep() const
MooseRandom _generator
Definition: RandomData.h:57
ExecFlagType _reset_on
Definition: RandomData.h:59
FEProblemBase & _rd_problem
Definition: RandomData.h:54
const ExecFlagType EXEC_INITIAL

Member Data Documentation

◆ _current_master_seed

unsigned int RandomData::_current_master_seed
private

Definition at line 62 of file RandomData.h.

Referenced by updateGenerators(), and updateSeeds().

◆ _generator

MooseRandom RandomData::_generator
private

Definition at line 57 of file RandomData.h.

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

◆ _is_nodal

bool RandomData::_is_nodal
private

Definition at line 58 of file RandomData.h.

Referenced by updateGenerators().

◆ _master_seed

unsigned int RandomData::_master_seed
private

Definition at line 61 of file RandomData.h.

Referenced by updateSeeds().

◆ _new_seed

unsigned int RandomData::_new_seed
private

Definition at line 63 of file RandomData.h.

Referenced by updateSeeds().

◆ _rd_mesh

MooseMesh& RandomData::_rd_mesh
private

Definition at line 55 of file RandomData.h.

Referenced by updateGenerators().

◆ _rd_problem

FEProblemBase& RandomData::_rd_problem
private

Definition at line 54 of file RandomData.h.

Referenced by updateGenerators(), and updateSeeds().

◆ _reset_on

ExecFlagType RandomData::_reset_on
private

Definition at line 59 of file RandomData.h.

Referenced by updateSeeds().

◆ _seeds

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

Definition at line 65 of file RandomData.h.

Referenced by getSeed(), and updateGenerators().


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