Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 23 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),
35  _new_seed(0)
36 {
37 }
unsigned int _new_seed
Definition: RandomData.h:62
MooseMesh & _rd_mesh
Definition: RandomData.h:54
unsigned int _master_seed
Definition: RandomData.h:60
auto max(const L &left, const R &right)
bool _is_nodal
Definition: RandomData.h:57
unsigned int _current_master_seed
Definition: RandomData.h:61
ExecFlagType _reset_on
Definition: RandomData.h:58
virtual MooseMesh & mesh() override
FEProblemBase & _rd_problem
Definition: RandomData.h:53

◆ ~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 41 of file RandomData.h.

Referenced by RandomInterface::setRandomDataPointer().

41 { return _generator; }
MooseRandom _generator
Definition: RandomData.h:56

◆ 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:64

◆ 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:64
bool isDistributedMesh() const
Returns the final Mesh distribution type.
Definition: MooseMesh.h:988
MooseMesh & _rd_mesh
Definition: RandomData.h:54
MeshBase & mesh
uint8_t processor_id_type
processor_id_type n_processors() const
bool _is_nodal
Definition: RandomData.h:57
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3443
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
unsigned int _current_master_seed
Definition: RandomData.h:61
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:44
MooseRandom _generator
Definition: RandomData.h:56
FEProblemBase & _rd_problem
Definition: RandomData.h:53
processor_id_type processor_id() const
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:1175
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:62
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:141
void updateGenerators()
Definition: RandomData.C:80
unsigned int _master_seed
Definition: RandomData.h:60
unsigned int _current_master_seed
Definition: RandomData.h:61
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:153
virtual int & timeStep() const
MooseRandom _generator
Definition: RandomData.h:56
ExecFlagType _reset_on
Definition: RandomData.h:58
FEProblemBase & _rd_problem
Definition: RandomData.h:53
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28

Member Data Documentation

◆ _current_master_seed

unsigned int RandomData::_current_master_seed
private

Definition at line 61 of file RandomData.h.

Referenced by updateGenerators(), and updateSeeds().

◆ _generator

MooseRandom RandomData::_generator
private

Definition at line 56 of file RandomData.h.

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

◆ _is_nodal

bool RandomData::_is_nodal
private

Definition at line 57 of file RandomData.h.

Referenced by updateGenerators().

◆ _master_seed

unsigned int RandomData::_master_seed
private

Definition at line 60 of file RandomData.h.

Referenced by updateSeeds().

◆ _new_seed

unsigned int RandomData::_new_seed
private

Definition at line 62 of file RandomData.h.

Referenced by updateSeeds().

◆ _rd_mesh

MooseMesh& RandomData::_rd_mesh
private

Definition at line 54 of file RandomData.h.

Referenced by updateGenerators().

◆ _rd_problem

FEProblemBase& RandomData::_rd_problem
private

Definition at line 53 of file RandomData.h.

Referenced by updateGenerators(), and updateSeeds().

◆ _reset_on

ExecFlagType RandomData::_reset_on
private

Definition at line 58 of file RandomData.h.

Referenced by updateSeeds().

◆ _seeds

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

Definition at line 64 of file RandomData.h.

Referenced by getSeed(), and updateGenerators().


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