www.mooseframework.org
RandomData.C
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 #include "RandomData.h"
16 #include "FEProblem.h"
17 #include "MooseMesh.h"
18 #include "RandomInterface.h"
19 
20 const unsigned int MASTER = std::numeric_limits<unsigned int>::max();
21 
22 RandomData::RandomData(FEProblemBase & problem, const RandomInterface & random_interface)
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 }
32 
33 unsigned int
34 RandomData::getSeed(dof_id_type id)
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 }
41 
42 void
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 }
72 
73 void
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 }
106 
107 template <typename T>
108 void
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 }
Interface for objects that need parallel consistent random numbers without patterns over the course o...
RandomData(FEProblemBase &problem, const RandomInterface &random_interface)
Definition: RandomData.C:22
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:76
unsigned int _new_seed
Definition: RandomData.h:66
std::unordered_map< dof_id_type, unsigned int > _seeds
Definition: RandomData.h:68
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
MooseMesh & _rd_mesh
Definition: RandomData.h:58
unsigned int _master_seed
Definition: RandomData.h:64
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
bool _is_nodal
Definition: RandomData.h:61
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2355
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
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
MooseRandom _generator
Definition: RandomData.h:60
ExecFlagType _reset_on
Definition: RandomData.h:62
unsigned int getSeed(dof_id_type id)
Get the seed for the passed in elem/node id.
Definition: RandomData.C:34
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
void updateSeeds(ExecFlagType exec_flag)
This method is called to reset or update the seeds based on the reset_on flag and the passed executio...
Definition: RandomData.C:43
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