www.mooseframework.org
RandomICBase.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "RandomICBase.h"
11 #include "MooseRandom.h"
12 
13 #include "libmesh/point.h"
14 
15 namespace
16 {
20 inline Real
21 valueHelper(dof_id_type id, MooseRandom & generator, std::map<dof_id_type, Real> & map)
22 {
23  auto it_pair = map.lower_bound(id);
24 
25  // Do we need to generate a new number?
26  if (it_pair == map.end() || it_pair->first != id)
27  it_pair = map.emplace_hint(it_pair, id, generator.rand(id));
28 
29  return it_pair->second;
30 }
31 }
32 
35 {
37  params.addParam<unsigned int>("seed", 0, "Seed value for the random number generator");
38  params.addParam<bool>(
39  "legacy_generator",
40  false,
41  "Determines whether or not the legacy generator (deprecated) should be used.");
42 
43  params.addClassDescription("Base class for generating a random field for a variable.");
44  return params;
45 }
46 
48  : InitialCondition(parameters),
49  _is_nodal(_var.isNodal()),
50  _use_legacy(getParam<bool>("legacy_generator")),
51  _elem_random_generator(nullptr),
52  _node_random_generator(nullptr)
53 {
54  unsigned int processor_seed = getParam<unsigned int>("seed");
55  MooseRandom::seed(processor_seed);
56 
57  if (_use_legacy)
58  {
59  auto proc_id = processor_id();
60  if (proc_id > 0)
61  {
62  for (processor_id_type i = 0; i < proc_id; ++i)
63  processor_seed = MooseRandom::randl();
64  MooseRandom::seed(processor_seed);
65  }
66  }
67  else
68  {
70  std::make_unique<RandomData>(_fe_problem, false, EXEC_INITIAL, MooseRandom::randl());
72  std::make_unique<RandomData>(_fe_problem, true, EXEC_INITIAL, MooseRandom::randl());
73 
74  _elem_random_generator = &_elem_random_data->getGenerator();
75  _node_random_generator = &_node_random_data->getGenerator();
76  }
77 }
78 
79 void
81 {
82  if (!_use_legacy)
83  {
84  _elem_random_data->updateSeeds(EXEC_INITIAL);
85  _node_random_data->updateSeeds(EXEC_INITIAL);
86  }
87 }
88 
89 Real
91 {
92  Real rand_num;
93 
94  if (_use_legacy)
95  {
96  mooseDeprecated("legacy_generator is deprecated. Please set \"legacy_generator = false\". This "
97  "capability will be removed after 11/01/2018");
98 
99  // Random number between 0 and 1
100  rand_num = MooseRandom::rand();
101  }
102  else
103  {
104  if (_current_node)
105  rand_num = valueHelper(_current_node->id(), *_node_random_generator, _node_numbers);
106  else if (_current_elem)
107  rand_num = valueHelper(_current_elem->id(), *_elem_random_generator, _elem_numbers);
108  else
109  mooseError("We can't generate parallel consistent random numbers for this kind of variable "
110  "yet. Please contact the MOOSE team for assistance");
111  }
112 
113  return rand_num;
114 }
std::map< dof_id_type, Real > _node_numbers
Random numbers per node (currently limited to a single value at a time)
Definition: RandomICBase.h:73
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:70
std::unique_ptr< RandomData > _node_random_data
RandomData node object, we cannot inherit from RandomInterface in an InitialCondition.
Definition: RandomICBase.h:61
void mooseDeprecated(Args &&... args) const
Real generateRandom()
Generate a uniformly distributed random number on the interval from 0 to 1.
Definition: RandomICBase.C:90
MooseRandom * _elem_random_generator
Elemental random number generator.
Definition: RandomICBase.h:64
This is a template class that implements the workhorse compute and computeNodal methods.
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
Definition: RandomICBase.C:80
MooseRandom * _node_random_generator
Nodal random number generator.
Definition: RandomICBase.h:67
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
RandomICBase(const InputParameters &parameters)
Constructor.
Definition: RandomICBase.C:47
const Node * _current_node
The current node if the point we are evaluating at also happens to be a node.
std::map< dof_id_type, Real > _elem_numbers
Random numbers per element (currently limited to a single value at a time)
Definition: RandomICBase.h:70
uint8_t processor_id_type
const bool _use_legacy
Boolean to indicate whether we want to use the old (deprecated) generation pattern.
Definition: RandomICBase.h:54
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:43
static Real rand()
This method returns the next random number (Real format) from the generator.
Definition: MooseRandom.h:49
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
const Elem *const & _current_elem
The current element we are on will retrieving values at specific points in the domain.
processor_id_type processor_id() const
static InputParameters validParams()
Definition: RandomICBase.C:34
std::unique_ptr< RandomData > _elem_random_data
RandomData element object, we cannot inherit from RandomInterface in an InitialCondition.
Definition: RandomICBase.h:58
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
Definition: MooseRandom.h:36
uint8_t dof_id_type
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28