https://mooseframework.inl.gov
Loading...
Searching...
No Matches
RandomICBase.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 "RandomICBase.h"
11#include "MooseRandom.h"
12
13#include "libmesh/point.h"
14
15namespace
16{
20inline Real
21valueHelper(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
76 }
77}
78
79void
81{
82 if (!_use_legacy)
83 {
84 _elem_random_data->updateSeeds(EXEC_INITIAL);
85 _node_random_data->updateSeeds(EXEC_INITIAL);
86 }
87}
88
89Real
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}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:363
const ExecFlagType EXEC_INITIAL
Definition Moose.C:30
This is a template class that implements the workhorse compute and computeNodal methods.
const Node * _current_node
The current node if the point we are evaluating at also happens to be a node.
const Elem *const & _current_elem
The current element we are on will retrieving values at specific points in the domain.
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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.
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
Definition MooseRandom.h:38
static Real rand()
This method returns the next random number (Real format) from the generator.
Definition MooseRandom.h:50
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
std::map< dof_id_type, Real > _elem_numbers
Random numbers per element (currently limited to a single value at a time)
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
std::map< dof_id_type, Real > _node_numbers
Random numbers per node (currently limited to a single value at a time)
MooseRandom * _elem_random_generator
Elemental random number generator.
const bool _use_legacy
Boolean to indicate whether we want to use the old (deprecated) generation pattern.
std::unique_ptr< RandomData > _node_random_data
RandomData node object, we cannot inherit from RandomInterface in an InitialCondition.
static InputParameters validParams()
RandomICBase(const InputParameters &parameters)
Constructor.
MooseRandom * _node_random_generator
Nodal random number generator.
std::unique_ptr< RandomData > _elem_random_data
RandomData element object, we cannot inherit from RandomInterface in an InitialCondition.
Real generateRandom()
Generate a uniformly distributed random number on the interval from 0 to 1.
processor_id_type processor_id() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real