https://mooseframework.inl.gov
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"
17 #include "DistributionInterface.h"
18 #include "PerfGraphInterface.h"
19 #include "SamplerInterface.h"
20 #include "MultiApp.h"
22 #include "ReporterInterface.h"
23 #include "MooseRandomStateless.h"
24 
45 class Sampler : public MooseObject,
46  public SetupInterface,
47  public DistributionInterface,
48  public PerfGraphInterface,
49  public SamplerInterface,
51  public ReporterInterface
52 {
53 public:
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 
81 
99  std::vector<Real> getNextLocalRow();
100 
109 
111 
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 
141 protected:
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 
248 
251 
252 private:
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 
291 
294 
297 
300 
303 
305  std::size_t _n_seeds;
306 
309 
312 
315 
318 
321 
324 
327 
330 
333  std::pair<LocalRankConfig, LocalRankConfig> _rank_config;
334 
337 };
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
void init()
Functions called by MOOSE to setup the Sampler for use.
Definition: Sampler.C:90
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.
void advanceGeneratorsInternal(const dof_id_type count)
Advance method for internal use that considers the auto advance flag.
Definition: Sampler.C:333
DenseMatrix< Real > getLocalSamples()
Definition: Sampler.C:218
dof_id_type _local_row_begin
Global row index for start of data for this processor.
Definition: Sampler.h:293
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.
static InputParameters validParams()
Definition: Sampler.C:17
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
This is the base class for Samplers as used within the Stochastic Tools module.
Definition: Sampler.h:45
dof_id_type _n_local_rows
Number of rows for this processor.
Definition: Sampler.h:290
virtual void executeSetUp()
Callbacks for before and after execute.
Definition: Sampler.h:234
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
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
DenseMatrix< Real > getGlobalSamples()
Return the sampled complete or distributed sample data.
Definition: Sampler.C:197
Interface for objects that need to use samplers.
virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count)
Definition: Sampler.C:326
std::vector< Real > getNextLocalRow()
Return the "next" local row.
Definition: Sampler.C:242
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
dof_id_type getLocalRowBegin() const
Return the beginning/end local row index for this processor.
Definition: Sampler.C:381
void checkReinitStatus() const
Helper function for reinit() errors.
Definition: Sampler.C:395
virtual void computeLocalSampleMatrix(DenseMatrix< Real > &matrix)
Definition: Sampler.C:290
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
virtual void computeSampleMatrix(DenseMatrix< Real > &matrix)
Methods to populate the global or local sample matrix.
Definition: Sampler.C:274
const dof_id_type _limit_get_next_local_row
Max number of entries for matrix returned by getNextLocalRow.
Definition: Sampler.h:329
dof_id_type getNumberOfLocalRows() const
Definition: Sampler.C:374
Sampler(const InputParameters &parameters)
Definition: Sampler.C:63
virtual void computeSampleRow(dof_id_type i, std::vector< Real > &data)
Method to populate a complete row of sample data.
Definition: Sampler.C:307
virtual bool isAdaptiveSamplingCompleted() const
Returns true if the adaptive sampling is completed.
Definition: Sampler.h:130
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
Real getRand(std::size_t n, unsigned int index=0) const
Get nth random number from the generator.
Definition: Sampler.C:346
bool _needs_reinit
Flag to indicate if the reinit method should be called during execute.
Definition: Sampler.h:317
libMesh::Parallel::Communicator & getLocalComm()
Return the parallel communicator.
Definition: Sampler.h:139
const dof_id_type _limit_get_global_samples
Max number of entries for matrix returned by getGlobalSamples.
Definition: Sampler.h:323
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
const dof_id_type _limit_get_local_samples
Max number of entries for matrix returned by getLocalSamples.
Definition: Sampler.h:326
Interface for objects that need to use distributions.
Interface to allow object to consume Reporter values.
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
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::size_t _n_seeds
Number of seeds.
Definition: Sampler.h:305
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
void reinit()
Definition: Sampler.C:118
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
Interface for objects interacting with the PerfGraph.
virtual LocalRankConfig constructRankConfig(bool batch_mode) const
This is where the sampler partitioning is defined.
Definition: Sampler.C:142
dof_id_type getLocalRowEnd() const
Definition: Sampler.C:388
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
libMesh::Parallel::Communicator _local_comm
Communicator that was split based on samples that have rows.
Definition: Sampler.h:250
dof_id_type _local_row_end
Global row index for end of data for this processor.
Definition: Sampler.h:296
dof_id_type getNumberOfRows() const
Return the number of samples.
Definition: Sampler.C:360
void setAutoAdvanceGenerators(const bool state)
Definition: Sampler.C:340
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void setNumberOfCols(dof_id_type n_cols)
Definition: Sampler.C:159
virtual void executeTearDown()
Definition: Sampler.h:235
bool _initialized
Flag to indicate if the init method for this class was called.
Definition: Sampler.h:314
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:281
bool _auto_advance_generators
Flag for disabling automatic generator advancing.
Definition: Sampler.h:336
bool _next_local_row_requires_state_restore
Flag for restoring state during getNextLocalRow iteration.
Definition: Sampler.h:311
Holds app partitioning information relevant to the a particular rank for a multiapp scenario...
Definition: MultiApp.h:46
dof_id_type _n_rows
Total number of rows in the sample matrix.
Definition: Sampler.h:299
SampleMode
Definition: Sampler.h:54
std::vector< std::unique_ptr< MooseRandomStateless > > _generators
Random number generators, don&#39;t give users access. Control it via the interface from this class...
Definition: Sampler.h:287
void execute()
Advance MooseRandomStateless generators so that new calls to sample methods will create new numbers...
Definition: Sampler.C:183
dof_id_type _n_cols
Total number of columns in the sample matrix.
Definition: Sampler.h:302
dof_id_type getNumberOfCols() const
Definition: Sampler.C:367
uint8_t dof_id_type
void setNumberOfRandomSeeds(std::size_t number)
Set the number of seeds required by the sampler.
Definition: Sampler.C:169
dof_id_type _next_local_row
Iterator index for getNextLocalRow method.
Definition: Sampler.h:308