https://mooseframework.inl.gov
RandomInterface.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 "Moose.h"
11 #include "RandomInterface.h"
12 #include "RandomData.h"
13 #include "MooseRandom.h"
14 #include "FEProblemBase.h"
15 #include "Assembly.h"
16 
19 {
20 
22  params.addParam<unsigned int>("seed", 0, "The seed for the master random number generator");
23 
24  params.addParamNamesToGroup("seed", "Advanced");
25  return params;
26 }
27 
29  FEProblemBase & problem,
30  THREAD_ID tid,
31  bool is_nodal)
32  : _random_data(nullptr),
33  _generator(nullptr),
34  _ri_problem(problem),
35  _ri_name(parameters.getObjectName()),
36  _master_seed(parameters.get<unsigned int>("seed")),
37  _is_nodal(is_nodal),
38  _reset_on(EXEC_LINEAR),
39  _curr_node(problem.assembly(tid, 0).node()),
40  _curr_element(problem.assembly(tid, 0).elem())
41 {
42 }
43 
44 #ifdef MOOSE_KOKKOS_ENABLED
46  : _random_data(object._random_data),
47  _generator(object._generator),
48  _ri_problem(object._ri_problem),
49  _ri_name(object._ri_name),
50  _master_seed(object._master_seed),
51  _is_nodal(object._is_nodal),
52  _reset_on(object._reset_on),
53  _curr_node(object._curr_node),
54  _curr_element(object._curr_element)
55 {
56 }
57 #endif
58 
60 
61 void
63 {
64  _reset_on = exec_flag;
66 }
67 
68 void
70 {
71  _random_data = random_data;
73 }
74 
75 unsigned int
76 RandomInterface::getSeed(std::size_t id)
77 {
78  mooseAssert(_random_data, "RandomData object is NULL!");
79 
80  return _random_data->getSeed(id);
81 }
82 
83 unsigned long
85 {
86  mooseAssert(_generator, "Random Generator is NULL, did you call setRandomResetFrequency()?");
87 
88  dof_id_type id;
89  if (_is_nodal)
90  id = _curr_node->id();
91  else
92  id = _curr_element->id();
93 
94  return _generator->randl(id);
95 }
96 
97 Real
99 {
100  mooseAssert(_generator, "Random Generator is NULL, did you call setRandomResetFrequency()?");
101 
102  dof_id_type id;
103  if (_is_nodal)
104  id = _curr_node->id();
105  else
106  id = _curr_element->id();
107 
108  return _generator->rand(id);
109 }
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 long getRandomLong() const
Returns the next random number (long) from the generator tied to this object (elem/node).
RandomInterface(const InputParameters &parameters, FEProblemBase &problem, THREAD_ID tid, bool is_nodal)
const Node *const & _curr_node
RandomData * _random_data
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1133
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void registerRandomInterface(RandomInterface &random_interface, const std::string &name)
unsigned int getSeed(std::size_t id)
Get the seed for the passed in elem/node id.
void setRandomResetFrequency(ExecFlagType exec_flag)
This interface should be called from a derived class to enable random number generation in this objec...
ExecFlagType _reset_on
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
InputParameters emptyInputParameters()
const std::string _ri_name
static InputParameters validParams()
const Elem *const & _curr_element
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:31
FEProblemBase & _ri_problem
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
void setRandomDataPointer(RandomData *random_data)
unsigned int getSeed(dof_id_type id)
Get the seed for the passed in elem/node id.
Definition: RandomData.C:40
static Real rand()
This method returns the next random number (Real format) from the generator.
Definition: MooseRandom.h:50
MooseRandom * _generator
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...
MooseRandom & getGenerator()
Return the underlying MooseRandom generator object for this data instance.
Definition: RandomData.h:41
void ErrorVector unsigned int
Real getRandomReal() const
Returns the next random number (Real) from the generator tied to this object (elem/node).
unsigned int THREAD_ID
Definition: MooseTypes.h:209
uint8_t dof_id_type
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...