https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Sampler.h
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#pragma once
11
12#include "Shuffle.h"
13#include "DenseMatrix.h"
14#include "MooseObject.h"
15#include "MooseRandom.h"
16#include "SetupInterface.h"
18#include "PerfGraphInterface.h"
19#include "SamplerInterface.h"
20#include "MultiApp.h"
22#include "ReporterInterface.h"
24
44class Sampler : public MooseObject,
45 public SetupInterface,
47 public PerfGraphInterface,
48 public SamplerInterface,
50 public ReporterInterface,
51 public Restartable
52{
53public:
54 enum class SampleMode
55 {
56 GLOBAL = 0,
57 LOCAL = 1
58 };
59
62
63 // The public members define the API that is exposed to application developers that are using
64 // Sampler objects to perform calculations, so be very careful when adding items here since
65 // they are exposed to any other object via the SamplerInterface.
66 //
67 // It is also important to point out that when Sampler objects, when used, are not const. This is
68 // due to the fact that calling the various get methods below must store various pieces of data as
69 // well as control the state of the random number generators.
70
72
78 DenseMatrix<Real> getGlobalSamples();
79 DenseMatrix<Real> getLocalSamples();
81
88 std::vector<Real> getSampleRow(dof_id_type row_index) const;
89
97 Real getSample(dof_id_type row_index, dof_id_type col_index) const;
98
104 dof_id_type getNumberOfRows() const;
105 dof_id_type getNumberOfCols() const;
106 dof_id_type getNumberOfLocalRows() const;
107
109
112 dof_id_type getLocalRowBegin() const;
113 dof_id_type getLocalRowEnd() const;
115
120 const LocalRankConfig & getRankConfig(bool batch_mode) const
121 {
122 return batch_mode ? _rank_config.second : _rank_config.first;
123 }
124
128 virtual bool isAdaptiveSamplingCompleted() const
129 {
130 mooseError("This method should be overridden in adaptive sampling classes.");
131 return false;
132 }
133
134protected:
135 // The following methods are the basic methods that should be utilized my most application
136 // developers that are creating a custom Sampler.
137
139
143 void setNumberOfRows(dof_id_type n_rows);
144 void setNumberOfCols(dof_id_type n_cols);
146
153 void setNumberOfRandomSeeds(std::size_t number);
154
163 Real getRand(std::size_t n, unsigned int index = 0) const;
164
175 unsigned int
176 getRandl(std::size_t n, unsigned int lower, unsigned int upper, unsigned int index = 0) const;
177
184 virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) const = 0;
185
186 // The following methods are advanced methods that should not be needed by application developers,
187 // but exist for special cases.
188
190
197 virtual void computeSampleMatrix(DenseMatrix<Real> & matrix);
198 virtual void computeLocalSampleMatrix(DenseMatrix<Real> & matrix);
200
209 virtual void computeSampleRow(dof_id_type i, std::vector<Real> & data) const;
210
214 virtual void advanceGenerators(const dof_id_type count);
215 virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count);
216 void setAutoAdvanceGenerators(const bool state);
217
219
226 virtual void executeSetUp() {}
227 virtual void executeTearDown() {}
229
234 virtual LocalRankConfig constructRankConfig(bool batch_mode) const;
235
237 const dof_id_type _min_procs_per_row;
239 const dof_id_type _max_procs_per_row;
240
241private:
243
252 void init(); // sets up MooseRandom
253 void reinit(); // partitions sampler output
255 friend void FEProblemBase::addSampler(const std::string & type,
256 const std::string & name,
262 void execute();
263 friend void FEProblemBase::objectExecuteHelper<Sampler>(const std::vector<Sampler *> & objects);
264
268 void checkReinitStatus() const;
269
273 void advanceGeneratorsInternal(const dof_id_type count);
274
276 std::vector<std::unique_ptr<MooseRandomStateless>> & _generators;
277
279 dof_id_type & _n_local_rows;
280
282 dof_id_type & _local_row_begin;
283
285 dof_id_type & _local_row_end;
286
288 dof_id_type & _n_rows;
289
291 dof_id_type & _n_cols;
292
294 std::size_t _n_seeds;
295
298
301
304
306 const dof_id_type _limit_get_global_samples;
307
309 const dof_id_type _limit_get_local_samples;
310
312 const dof_id_type _limit_get_row;
313
316 std::pair<LocalRankConfig, LocalRankConfig> & _rank_config;
317
320};
unsigned int count
Definition MortarUtils.C:53
Interface for objects that need to use distributions.
virtual void addSampler(const std::string &type, const std::string &name, InputParameters &parameters)
The following functions will enable MOOSE to have the capability to import Samplers.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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...
Definition MooseBase.h:271
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
Interface for objects interacting with the PerfGraph.
Interface to allow object to consume Reporter values.
A class for creating restricted objects.
Definition Restartable.h:29
Interface for objects that need to use samplers.
This is the base class for Samplers as used within the Stochastic Tools module.
Definition Sampler.h:52
virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count)
Definition Sampler.C:314
void checkReinitStatus() const
Helper function for reinit() errors.
Definition Sampler.C:383
void setNumberOfCols(dof_id_type n_cols)
Definition Sampler.C:160
const dof_id_type _limit_get_local_samples
Max number of entries for matrix returned by getLocalSamples.
Definition Sampler.h:309
Real getSample(dof_id_type row_index, dof_id_type col_index) const
Return a single sample value by global row and column index.
Definition Sampler.C:260
Real getRand(std::size_t n, unsigned int index=0) const
Get nth random number from the generator.
Definition Sampler.C:334
const dof_id_type _limit_get_global_samples
Max number of entries for matrix returned by getGlobalSamples.
Definition Sampler.h:306
void reinit()
Definition Sampler.C:125
void advanceGeneratorsInternal(const dof_id_type count)
Advance method for internal use that considers the auto advance flag.
Definition Sampler.C:321
bool & _has_executed
Flag for initial execute to allow the first set of random numbers to be always be the same.
Definition Sampler.h:303
SampleMode
Definition Sampler.h:55
bool _initialized
Flag to indicate if the init method for this class was called.
Definition Sampler.h:297
DenseMatrix< Real > getLocalSamples()
Definition Sampler.C:218
dof_id_type getNumberOfLocalRows() const
Definition Sampler.C:362
const dof_id_type _max_procs_per_row
The maximum number of processors that are associated with a set of rows.
Definition Sampler.h:239
void execute()
Advance MooseRandomStateless generators so that new calls to sample methods will create new numbers.
Definition Sampler.C:184
std::vector< std::unique_ptr< MooseRandomStateless > > & _generators
Random number generators, don't give users access. Control it via the interface from this class.
Definition Sampler.h:276
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)
Definition Sampler.C:341
dof_id_type & _local_row_end
Global row index for end of data for this processor.
Definition Sampler.h:285
dof_id_type & _local_row_begin
Global row index for start of data for this processor.
Definition Sampler.h:282
dof_id_type getLocalRowEnd() const
Definition Sampler.C:376
void init()
Functions called by MOOSE to setup the Sampler for use.
Definition Sampler.C:97
dof_id_type getLocalRowBegin() const
Return the beginning/end local row index for this processor.
Definition Sampler.C:369
bool _auto_advance_generators
Flag for disabling automatic generator advancing.
Definition Sampler.h:319
virtual void executeSetUp()
Callbacks for before and after execute.
Definition Sampler.h:226
std::vector< Real > getSampleRow(dof_id_type row_index) const
Return a single sample row by global row.
Definition Sampler.C:241
virtual LocalRankConfig constructRankConfig(bool batch_mode) const
This is where the sampler partitioning is defined.
Definition Sampler.C:143
dof_id_type getNumberOfRows() const
Return the number of samples.
Definition Sampler.C:348
const dof_id_type _limit_get_row
Max number of entries for matrix returned by getSampleRow.
Definition Sampler.h:312
const LocalRankConfig & getRankConfig(bool batch_mode) const
Reference to rank configuration defining the partitioning of the sampler matrix This is primarily use...
Definition Sampler.h:120
virtual void advanceGenerators(const dof_id_type count)
Method for advancing the random number generator(s) by the supplied number or calls to rand().
Definition Sampler.C:306
virtual void executeTearDown()
Definition Sampler.h:227
dof_id_type & _n_cols
Total number of columns in the sample matrix.
Definition Sampler.h:291
virtual void computeSampleMatrix(DenseMatrix< Real > &matrix)
Methods to populate the global or local sample matrix.
Definition Sampler.C:270
virtual void computeSampleRow(dof_id_type i, std::vector< Real > &data) const
Method to populate a complete row of sample data.
Definition Sampler.C:299
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)
Definition Sampler.C:328
bool _needs_reinit
Flag to indicate if the reinit method should be called during execute.
Definition Sampler.h:300
static InputParameters validParams()
Definition Sampler.C:17
virtual void computeLocalSampleMatrix(DenseMatrix< Real > &matrix)
Definition Sampler.C:284
std::size_t _n_seeds
Number of seeds.
Definition Sampler.h:294
dof_id_type getNumberOfCols() const
Definition Sampler.C:355
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...
Definition Sampler.C:150
dof_id_type & _n_rows
Total number of rows in the sample matrix.
Definition Sampler.h:288
dof_id_type & _n_local_rows
Number of rows for this processor.
Definition Sampler.h:279
const dof_id_type _min_procs_per_row
The minimum number of processors that are associated with a set of rows.
Definition Sampler.h:237
std::pair< LocalRankConfig, LocalRankConfig > & _rank_config
The partitioning of the sampler matrix, built in reinit() first is for normal mode and second is for ...
Definition Sampler.h:316
void setNumberOfRandomSeeds(std::size_t number)
Set the number of seeds required by the sampler.
Definition Sampler.C:170
DenseMatrix< Real > getGlobalSamples()
Return the sampled complete or distributed sample data.
Definition Sampler.C:198
virtual bool isAdaptiveSamplingCompleted() const
Returns true if the adaptive sampling is completed.
Definition Sampler.h:128
Holds app partitioning information relevant to the a particular rank for a multiapp scenario.
Definition MultiApp.h:47