https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
MooseRandomgetGenerator ()
 Return the underlying MooseRandom generator object for this data instance.
 
unsigned int getSeed (dof_id_type id)
 Get the seed for the passed in elem/node id.
 

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}
bool isNodal() const
unsigned int getMasterSeed() const
ExecFlagType getResetOnTime() const

◆ 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}
virtual MooseMesh & mesh() override
unsigned int _new_seed
Definition RandomData.h:62
ExecFlagType _reset_on
Definition RandomData.h:58
MooseMesh & _rd_mesh
Definition RandomData.h:54
unsigned int _current_master_seed
Definition RandomData.h:61
unsigned int _master_seed
Definition RandomData.h:60
FEProblemBase & _rd_problem
Definition RandomData.h:53
bool _is_nodal
Definition RandomData.h:57

◆ ~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.

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

Referenced by RandomInterface::setRandomDataPointer().

◆ 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.

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

Referenced by RandomInterface::getSeed().

◆ 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.

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}
const unsigned int MASTER
Definition RandomData.C:15
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition MooseMesh.C:3549
virtual bool isDistributedMesh() const
Returns the final Mesh distribution type.
Definition MooseMesh.h:1143
const std::unordered_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:1236
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition MooseRandom.h:71
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition MooseRandom.h:44
processor_id_type processor_id() const
processor_id_type n_processors() const
MeshBase & mesh
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)

Referenced by updateSeeds().

◆ 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}
const ExecFlagType EXEC_INITIAL
Definition Moose.C:30
virtual int & timeStep() const
void restoreState()
This method restores the last saved generator state.
void saveState()
This method saves the current state of all generators which can be restored at a later time (i....
void updateGenerators()
Definition RandomData.C:80

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: