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 
33 template <>
36 {
37  InputParameters params = validParams<InitialCondition>();
38  params.addParam<unsigned int>("seed", 0, "Seed value for the random number generator");
39  params.addParam<bool>(
40  "legacy_generator",
41  false,
42  "Determines whether or not the legacy generator (deprecated) should be used.");
43 
44  params.addClassDescription("Base class for generating a random field for a variable.");
45  return params;
46 }
47 
49  : InitialCondition(parameters),
50  _is_nodal(_var.isNodal()),
51  _use_legacy(getParam<bool>("legacy_generator")),
52  _elem_random_generator(nullptr),
53  _node_random_generator(nullptr)
54 {
55  unsigned int processor_seed = getParam<unsigned int>("seed");
56  MooseRandom::seed(processor_seed);
57 
58  if (_use_legacy)
59  {
60  auto proc_id = processor_id();
61  if (proc_id > 0)
62  {
63  for (processor_id_type i = 0; i < proc_id; ++i)
64  processor_seed = MooseRandom::randl();
65  MooseRandom::seed(processor_seed);
66  }
67  }
68  else
69  {
71  libmesh_make_unique<RandomData>(_fe_problem, false, EXEC_INITIAL, MooseRandom::randl());
73  libmesh_make_unique<RandomData>(_fe_problem, true, EXEC_INITIAL, MooseRandom::randl());
74 
75  _elem_random_generator = &_elem_random_data->getGenerator();
76  _node_random_generator = &_node_random_data->getGenerator();
77  }
78 }
79 
80 void
82 {
83  if (!_use_legacy)
84  {
85  _elem_random_data->updateSeeds(EXEC_INITIAL);
86  _node_random_data->updateSeeds(EXEC_INITIAL);
87  }
88 }
89 
90 Real
92 {
93  Real rand_num;
94 
95  if (_use_legacy)
96  {
97  mooseDeprecated("legacy_generator is deprecated. Please set \"legacy_generator = false\". This "
98  "capability will be removed after 11/01/2018");
99 
100  // Random number between 0 and 1
101  rand_num = MooseRandom::rand();
102  }
103  else
104  {
105  if (_current_node)
106  rand_num = valueHelper(_current_node->id(), *_node_random_generator, _node_numbers);
107  else if (_current_elem)
108  rand_num = valueHelper(_current_elem->id(), *_elem_random_generator, _elem_numbers);
109  else
110  mooseError("We can't generate parallel consistent random numbers for this kind of variable "
111  "yet. Please contact the MOOSE team for assistance");
112  }
113 
114  return rand_num;
115 }
std::map< dof_id_type, Real > _node_numbers
Random numbers per node (currently limited to a single value at a time)
Definition: RandomICBase.h:75
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:70
static double rand()
This method returns the next random number (double format) from the generator.
Definition: MooseRandom.h:49
std::unique_ptr< RandomData > _node_random_data
RandomData node object, we cannot inherit from RandomInterface in an InitialCondition.
Definition: RandomICBase.h:63
Real generateRandom()
Generate a uniformly distributed random number on the interval from 0 to 1.
Definition: RandomICBase.C:91
MooseRandom * _elem_random_generator
Elemental random number generator.
Definition: RandomICBase.h:66
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:81
MooseRandom * _node_random_generator
Nodal random number generator.
Definition: RandomICBase.h:69
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
RandomICBase(const InputParameters &parameters)
Constructor.
Definition: RandomICBase.C:48
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:72
const bool _use_legacy
Boolean to indicate whether we want to use the old (deprecated) generation pattern.
Definition: RandomICBase.h:56
InputParameters validParams< RandomICBase >()
Definition: RandomICBase.C:35
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:43
void mooseDeprecated(Args &&... args) const
Definition: MooseObject.h:161
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.
std::unique_ptr< RandomData > _elem_random_data
RandomData element object, we cannot inherit from RandomInterface in an InitialCondition.
Definition: RandomICBase.h:60
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
Definition: MooseRandom.h:36
const ExecFlagType EXEC_INITIAL