https://mooseframework.inl.gov
RandomData.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "RandomData.h"
11 #include "FEProblem.h"
12 #include "MooseMesh.h"
13 #include "RandomInterface.h"
14 
16 
17 RandomData::RandomData(FEProblemBase & fe_problem, const RandomInterface & random_interface)
18  : RandomData(fe_problem,
19  random_interface.isNodal(),
20  random_interface.getResetOnTime(),
21  random_interface.getMasterSeed())
22 {
23 }
24 
26  bool is_nodal,
27  ExecFlagType reset_on,
28  unsigned int seed)
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 }
38 
39 unsigned int
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 }
47 
48 void
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 }
78 
79 void
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 }
Interface for objects that need parallel consistent random numbers without patterns over the course o...
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:71
unsigned int _new_seed
Definition: RandomData.h:62
std::unordered_map< dof_id_type, unsigned int > _seeds
Definition: RandomData.h:64
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
MooseMesh & _rd_mesh
Definition: RandomData.h:54
MeshBase & mesh
unsigned int _master_seed
Definition: RandomData.h:60
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
auto max(const L &left, const R &right)
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
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:153
virtual int & timeStep() const
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:44
MooseRandom _generator
Definition: RandomData.h:56
ExecFlagType _reset_on
Definition: RandomData.h:58
unsigned int getSeed(dof_id_type id)
Get the seed for the passed in elem/node id.
Definition: RandomData.C:40
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:49
FEProblemBase & _rd_problem
Definition: RandomData.h:53
processor_id_type processor_id() const
virtual bool isDistributedMesh() const
Returns the final Mesh distribution type.
Definition: MooseMesh.h:1001
RandomData(FEProblemBase &fe_problem, const RandomInterface &random_interface)
Definition: RandomData.C:17
void ErrorVector unsigned int
uint8_t dof_id_type
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
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28