28 params.
addParam<
unsigned int>(
"seed", 0,
"Random number generator initial seed");
37 params.
addParam<dof_id_type>(
"limit_get_global_samples",
38 0.1 * std::numeric_limits<unsigned int>::max(),
39 "The maximum allowed number of items in the DenseMatrix returned by "
40 "getGlobalSamples method.");
42 "limit_get_local_samples",
43 0.1 * std::numeric_limits<unsigned int>::max(),
44 "The maximum allowed number of items in the DenseMatrix returned by getLocalSamples method.");
47 0.1 * std::numeric_limits<unsigned int>::max(),
48 "The maximum allowed number of items in the std::vector returned by getSampleRow method.");
53 "This will ensure that the sampler is partitioned properly when "
54 "'MultiApp/*/min_procs_per_app' is specified. It is not recommended to use otherwise.");
57 std::numeric_limits<unsigned int>::max(),
58 "This will ensure that the sampler is partitioned properly when "
59 "'MultiApp/*/max_procs_per_app' is specified. It is not recommended to use otherwise.");
72 _min_procs_per_row(getParam<unsigned
int>(
"min_procs_per_row") > n_processors()
74 : getParam<unsigned
int>(
"min_procs_per_row")),
75 _max_procs_per_row(getParam<unsigned
int>(
"max_procs_per_row")),
78 _n_local_rows(declareRecoverableData<dof_id_type>(
"n_local_rows")),
79 _local_row_begin(declareRecoverableData<dof_id_type>(
"local_row_begin")),
80 _local_row_end(declareRecoverableData<dof_id_type>(
"local_row_end")),
81 _n_rows(declareRecoverableData<dof_id_type>(
"n_rows")),
82 _n_cols(declareRecoverableData<dof_id_type>(
"n_cols")),
86 _has_executed(declareRecoverableData<bool>(
"has_executed")),
87 _limit_get_global_samples(getParam<dof_id_type>(
"limit_get_global_samples")),
88 _limit_get_local_samples(getParam<dof_id_type>(
"limit_get_local_samples")),
89 _limit_get_row(getParam<dof_id_type>(
"limit_get_row")),
92 _auto_advance_generators(true)
102 mooseError(
"The Sampler::init() method is called automatically and should not be called.");
108 const unsigned int seed = getParam<unsigned int>(
"seed");
110 seed_generator.
seed(0, seed);
114 for (std::size_t i = 0; i <
_n_seeds; ++i)
116 const auto gseed = seed_generator.
randl(0);
117 _generators[i] = std::make_unique<MooseRandomStateless>(gseed);
132 mooseError(
"Sampler has inconsistent partitionings for normal and batch mode.");
153 mooseError(
"The number of rows cannot be zero.");
163 mooseError(
"The number of columns cannot be zero.");
173 mooseError(
"The 'setNumberOfRandomSeeds()' method can not be called after the Sampler has been "
175 "this method should be called in the constructor of the Sampler object.");
178 mooseError(
"The number of seeds must be larger than zero.");
200 TIME_SECTION(
"getGlobalSamples", 1,
"Retrieving Global Samples");
206 "The number of entries in the DenseMatrix (",
208 ") exceeds the allowed limit of ",
220 TIME_SECTION(
"getLocalSamples", 1,
"Retrieving Local Samples");
226 "The number of entries in the DenseMatrix (",
228 ") exceeds the allowed limit of ",
246 "The number of entries in the std::vector (",
248 ") exceeds the allowed limit of ",
251 mooseAssert(row_index <
_n_rows,
252 "Requested row " + std::to_string(row_index) +
" is greater than sampler size.");
254 std::vector<Real> row(
_n_cols, 0);
263 mooseAssert(row_index <
_n_rows,
264 "Requested row " + std::to_string(row_index) +
" is greater than sampler size.");
265 mooseAssert(col_index <
_n_cols,
"Column index out of range.");
272 TIME_SECTION(
"computeSampleMatrix", 2,
"Computing Sample Matrix");
274 for (dof_id_type i = 0; i <
_n_rows; ++i)
276 std::vector<Real> row(
_n_cols, 0);
278 mooseAssert(row.size() ==
_n_cols,
"The row of sample data is not sized correctly.");
279 std::copy(row.begin(), row.end(), matrix.get_values().begin() + i *
_n_cols);
286 TIME_SECTION(
"computeLocalSampleMatrix", 2,
"Computing Local Sample Matrix");
290 std::vector<Real> row(
_n_cols, 0);
292 mooseAssert(row.size() ==
_n_cols,
"The row of sample data is not sized correctly.");
301 for (dof_id_type j = 0; j <
_n_cols; ++j)
308 TIME_SECTION(
"advanceGenerators", 2,
"Advancing Generators");
310 for (std::size_t j = 0; j <
_generators.size(); ++j)
316 mooseAssert(seed_index <
_generators.size(),
"The seed number index does not exists.");
336 mooseAssert(index <
_generators.size(),
"The seed number index does not exists.");
341Sampler::getRandl(std::size_t n,
unsigned int lower,
unsigned int upper,
unsigned int index)
const
343 mooseAssert(index <
_generators.size(),
"The seed number index does not exists.");
386 mooseError(
"A call to 'setNumberOfRows()/Columns()' was made after initialization, as such the "
387 "expected Sampler output has changed and a new sample must be created. However, a "
388 "call to Sampler::reinit() was not performed. The renit() method is automatically "
389 "called during Sampler execution, which occurs according to the 'execute_on' "
390 "settings of the Sampler object. An adjustment to this parameter may be required. "
391 "It is recommended that calls to 'setNumberOfRows()/Columns() occur within the "
392 "Sampler::executeSetUp() method; this will ensure that the reinitialize is handled "
393 "correctly. Have a nice day.");
const ExecFlagType EXEC_PRE_MULTIAPP_SETUP
const ExecFlagType EXEC_INITIAL
LocalRankConfig rankConfig(processor_id_type rank, processor_id_type nprocs, dof_id_type napps, processor_id_type min_app_procs, processor_id_type max_app_procs, bool batch_mode=false)
Returns app partitioning information relevant to the given rank for a multiapp scenario with the give...
void ErrorVector unsigned int
Interface for objects that need to use distributions.
static InputParameters validParams()
A MultiMooseEnum object to hold "execute_on" flags.
void addAvailableFlags(const ExecFlagType &flag, Args... flags)
Add additional execute_on flags to the list of possible flags.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Every object that can be built by the factory should be derived from this class.
static InputParameters validParams()
A deterministic, indexable random number generator built on top of the randistrs library.
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
static uint32_t randl()
This method returns the next random number (long format) from the generator.
static void seed(unsigned int seed)
The method seeds the random number generator.
Interface for objects interacting with the PerfGraph.
Interface to allow object to consume Reporter values.
A class for creating restricted objects.
Interface for objects that need to use samplers.
virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count)
void checkReinitStatus() const
Helper function for reinit() errors.
void setNumberOfCols(dof_id_type n_cols)
const dof_id_type _limit_get_local_samples
Max number of entries for matrix returned by getLocalSamples.
Real getSample(dof_id_type row_index, dof_id_type col_index) const
Return a single sample value by global row and column index.
Real getRand(std::size_t n, unsigned int index=0) const
Get nth random number from the generator.
const dof_id_type _limit_get_global_samples
Max number of entries for matrix returned by getGlobalSamples.
void advanceGeneratorsInternal(const dof_id_type count)
Advance method for internal use that considers the auto advance flag.
bool & _has_executed
Flag for initial execute to allow the first set of random numbers to be always be the same.
bool _initialized
Flag to indicate if the init method for this class was called.
DenseMatrix< Real > getLocalSamples()
dof_id_type getNumberOfLocalRows() const
const dof_id_type _max_procs_per_row
The maximum number of processors that are associated with a set of rows.
void execute()
Advance MooseRandomStateless generators so that new calls to sample methods will create new numbers.
std::vector< std::unique_ptr< MooseRandomStateless > > & _generators
Random number generators, don't give users access. Control it via the interface from this class.
unsigned int getRandl(std::size_t n, unsigned int lower, unsigned int upper, unsigned int index=0) const
Get nth random integer from the generator within the specified range [lower, upper)
dof_id_type & _local_row_end
Global row index for end of data for this processor.
dof_id_type & _local_row_begin
Global row index for start of data for this processor.
dof_id_type getLocalRowEnd() const
void init()
Functions called by MOOSE to setup the Sampler for use.
dof_id_type getLocalRowBegin() const
Return the beginning/end local row index for this processor.
bool _auto_advance_generators
Flag for disabling automatic generator advancing.
virtual void executeSetUp()
Callbacks for before and after execute.
std::vector< Real > getSampleRow(dof_id_type row_index) const
Return a single sample row by global row.
virtual LocalRankConfig constructRankConfig(bool batch_mode) const
This is where the sampler partitioning is defined.
dof_id_type getNumberOfRows() const
Return the number of samples.
const dof_id_type _limit_get_row
Max number of entries for matrix returned by getSampleRow.
virtual void advanceGenerators(const dof_id_type count)
Method for advancing the random number generator(s) by the supplied number or calls to rand().
virtual void executeTearDown()
dof_id_type & _n_cols
Total number of columns in the sample matrix.
virtual void computeSampleMatrix(DenseMatrix< Real > &matrix)
Methods to populate the global or local sample matrix.
virtual void computeSampleRow(dof_id_type i, std::vector< Real > &data) const
Method to populate a complete row of sample data.
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) const =0
Base class must override this method to supply the sample distribution data.
void setAutoAdvanceGenerators(const bool state)
bool _needs_reinit
Flag to indicate if the reinit method should be called during execute.
static InputParameters validParams()
virtual void computeLocalSampleMatrix(DenseMatrix< Real > &matrix)
std::size_t _n_seeds
Number of seeds.
dof_id_type getNumberOfCols() const
void setNumberOfRows(dof_id_type n_rows)
These methods must be called within the constructor of child classes to define the size of the matrix...
dof_id_type & _n_rows
Total number of rows in the sample matrix.
dof_id_type & _n_local_rows
Number of rows for this processor.
const dof_id_type _min_procs_per_row
The minimum number of processors that are associated with a set of rows.
Sampler(const InputParameters ¶meters)
std::pair< LocalRankConfig, LocalRankConfig > & _rank_config
The partitioning of the sampler matrix, built in reinit() first is for normal mode and second is for ...
void setNumberOfRandomSeeds(std::size_t number)
Set the number of seeds required by the sampler.
DenseMatrix< Real > getGlobalSamples()
Return the sampled complete or distributed sample data.
static InputParameters validParams()
processor_id_type processor_id() const
processor_id_type n_processors() const
Holds app partitioning information relevant to the a particular rank for a multiapp scenario.