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
45class Sampler : public MooseObject,
46 public SetupInterface,
48 public PerfGraphInterface,
49 public SamplerInterface,
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
99 std::vector<Real> getNextLocalRow();
100
106 dof_id_type getNumberOfRows() const;
107 dof_id_type getNumberOfCols() const;
108 dof_id_type getNumberOfLocalRows() const;
109
111
114 dof_id_type getLocalRowBegin() const;
115 dof_id_type getLocalRowEnd() const;
117
122 const LocalRankConfig & getRankConfig(bool batch_mode) const
123 {
124 return batch_mode ? _rank_config.second : _rank_config.first;
125 }
126
130 virtual bool isAdaptiveSamplingCompleted() const
131 {
132 mooseError("This method should be overridden in adaptive sampling classes.");
133 return false;
134 }
135
140
141protected:
142 // The following methods are the basic methods that should be utilized my most application
143 // developers that are creating a custom Sampler.
144
146
150 void setNumberOfRows(dof_id_type n_rows);
151 void setNumberOfCols(dof_id_type n_cols);
153
160 void setNumberOfRandomSeeds(std::size_t number);
161
170 Real getRand(std::size_t n, unsigned int index = 0) const;
171
182 unsigned int
183 getRandl(std::size_t n, unsigned int lower, unsigned int upper, unsigned int index = 0) const;
184
191 virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) = 0;
192
193 // The following methods are advanced methods that should not be needed by application developers,
194 // but exist for special cases.
195
197
204 virtual void computeSampleMatrix(DenseMatrix<Real> & matrix);
205 virtual void computeLocalSampleMatrix(DenseMatrix<Real> & matrix);
207
209
217 virtual void computeSampleRow(dof_id_type i, std::vector<Real> & data);
218
222 virtual void advanceGenerators(const dof_id_type count);
223 virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count);
224 void setAutoAdvanceGenerators(const bool state);
225
227
234 virtual void executeSetUp() {}
235 virtual void executeTearDown() {}
237
242 virtual LocalRankConfig constructRankConfig(bool batch_mode) const;
243
245 const dof_id_type _min_procs_per_row;
247 const dof_id_type _max_procs_per_row;
248
251
252private:
254
263 void init(); // sets up MooseRandom
264 void reinit(); // partitions sampler output
266 friend void FEProblemBase::addSampler(const std::string & type,
267 const std::string & name,
273 void execute();
274 friend void FEProblemBase::objectExecuteHelper<Sampler>(const std::vector<Sampler *> & objects);
275
279 void checkReinitStatus() const;
280
284 void advanceGeneratorsInternal(const dof_id_type count);
285
287 std::vector<std::unique_ptr<MooseRandomStateless>> _generators;
288
290 dof_id_type _n_local_rows;
291
293 dof_id_type _local_row_begin;
294
296 dof_id_type _local_row_end;
297
299 dof_id_type _n_rows;
300
302 dof_id_type _n_cols;
303
305 std::size_t _n_seeds;
306
308 dof_id_type _next_local_row;
309
312
315
318
321
323 const dof_id_type _limit_get_global_samples;
324
326 const dof_id_type _limit_get_local_samples;
327
329 const dof_id_type _limit_get_next_local_row;
330
333 std::pair<LocalRankConfig, LocalRankConfig> _rank_config;
334
337};
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.
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:326
void checkReinitStatus() const
Helper function for reinit() errors.
Definition Sampler.C:395
void setNumberOfCols(dof_id_type n_cols)
Definition Sampler.C:159
const dof_id_type _limit_get_local_samples
Max number of entries for matrix returned by getLocalSamples.
Definition Sampler.h:326
dof_id_type _next_local_row
Iterator index for getNextLocalRow method.
Definition Sampler.h:308
dof_id_type _local_row_begin
Global row index for start of data for this processor.
Definition Sampler.h:293
std::pair< LocalRankConfig, LocalRankConfig > _rank_config
The partitioning of the sampler matrix, built in reinit() first is for normal mode and send is for ba...
Definition Sampler.h:333
std::vector< Real > getNextLocalRow()
Return the "next" local row.
Definition Sampler.C:242
Real getRand(std::size_t n, unsigned int index=0) const
Get nth random number from the generator.
Definition Sampler.C:346
const dof_id_type _limit_get_global_samples
Max number of entries for matrix returned by getGlobalSamples.
Definition Sampler.h:323
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:287
void reinit()
Definition Sampler.C:118
void advanceGeneratorsInternal(const dof_id_type count)
Advance method for internal use that considers the auto advance flag.
Definition Sampler.C:333
SampleMode
Definition Sampler.h:55
bool _initialized
Flag to indicate if the init method for this class was called.
Definition Sampler.h:314
DenseMatrix< Real > getLocalSamples()
Definition Sampler.C:218
dof_id_type getNumberOfLocalRows() const
Definition Sampler.C:374
const dof_id_type _max_procs_per_row
The maximum number of processors that are associated with a set of rows.
Definition Sampler.h:247
void execute()
Advance MooseRandomStateless generators so that new calls to sample methods will create new numbers.
Definition Sampler.C:183
bool _has_executed
Flag for initial execute to allow the first set of random numbers to be always be the same.
Definition Sampler.h:320
dof_id_type _n_rows
Total number of rows in the sample matrix.
Definition Sampler.h:299
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:353
dof_id_type _n_cols
Total number of columns in the sample matrix.
Definition Sampler.h:302
dof_id_type _n_local_rows
Number of rows for this processor.
Definition Sampler.h:290
dof_id_type getLocalRowEnd() const
Definition Sampler.C:388
const dof_id_type _limit_get_next_local_row
Max number of entries for matrix returned by getNextLocalRow.
Definition Sampler.h:329
void init()
Functions called by MOOSE to setup the Sampler for use.
Definition Sampler.C:90
dof_id_type getLocalRowBegin() const
Return the beginning/end local row index for this processor.
Definition Sampler.C:381
bool _auto_advance_generators
Flag for disabling automatic generator advancing.
Definition Sampler.h:336
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index)=0
Base class must override this method to supply the sample distribution data.
libMesh::Parallel::Communicator _local_comm
Communicator that was split based on samples that have rows.
Definition Sampler.h:250
virtual void executeSetUp()
Callbacks for before and after execute.
Definition Sampler.h:234
virtual LocalRankConfig constructRankConfig(bool batch_mode) const
This is where the sampler partitioning is defined.
Definition Sampler.C:142
dof_id_type getNumberOfRows() const
Return the number of samples.
Definition Sampler.C:360
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:122
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:318
virtual void executeTearDown()
Definition Sampler.h:235
virtual void computeSampleMatrix(DenseMatrix< Real > &matrix)
Methods to populate the global or local sample matrix.
Definition Sampler.C:274
void setAutoAdvanceGenerators(const bool state)
Definition Sampler.C:340
bool _needs_reinit
Flag to indicate if the reinit method should be called during execute.
Definition Sampler.h:317
static InputParameters validParams()
Definition Sampler.C:17
virtual void computeLocalSampleMatrix(DenseMatrix< Real > &matrix)
Definition Sampler.C:290
std::size_t _n_seeds
Number of seeds.
Definition Sampler.h:305
libMesh::Parallel::Communicator & getLocalComm()
Return the parallel communicator.
Definition Sampler.h:139
dof_id_type getNumberOfCols() const
Definition Sampler.C:367
dof_id_type _local_row_end
Global row index for end of data for this processor.
Definition Sampler.h:296
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:149
bool _next_local_row_requires_state_restore
Flag for restoring state during getNextLocalRow iteration.
Definition Sampler.h:311
const dof_id_type _min_procs_per_row
The minimum number of processors that are associated with a set of rows.
Definition Sampler.h:245
virtual void computeSampleRow(dof_id_type i, std::vector< Real > &data)
Method to populate a complete row of sample data.
Definition Sampler.C:307
void setNumberOfRandomSeeds(std::size_t number)
Set the number of seeds required by the sampler.
Definition Sampler.C:169
DenseMatrix< Real > getGlobalSamples()
Return the sampled complete or distributed sample data.
Definition Sampler.C:197
virtual bool isAdaptiveSamplingCompleted() const
Returns true if the adaptive sampling is completed.
Definition Sampler.h:130
Holds app partitioning information relevant to the a particular rank for a multiapp scenario.
Definition MultiApp.h:47