https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | List of all members
NestedMonteCarloSampler Class Reference

A class used to perform nested Monte Carlo Sampling. More...

#include <NestedMonteCarloSampler.h>

Inheritance diagram for NestedMonteCarloSampler:
[legend]

Public Types

enum  SampleMode { SampleMode::GLOBAL, SampleMode::LOCAL }
 
typedef DataFileName DataFileParameterType
 

Public Member Functions

 NestedMonteCarloSampler (const InputParameters &parameters)
 
std::vector< RealgetNextLocalRow ()
 
dof_id_type getNumberOfRows () const
 
dof_id_type getNumberOfCols () const
 
dof_id_type getNumberOfLocalRows () const
 
const LocalRankConfiggetRankConfig (bool batch_mode) const
 
virtual bool isAdaptiveSamplingCompleted () const
 
libMesh::Parallel::CommunicatorgetLocalComm ()
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
virtual const std::string & name () const
 
std::string typeAndName () const
 
std::string errorPrefix (const std::string &error_type) const
 
void callMooseError (std::string msg, const bool with_prefix) const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
const InputParametersparameters () const
 
MooseObjectName uniqueName () const
 
const T & getParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const T * queryParam (const std::string &name) const
 
const T & getRenamedParam (const std::string &old_name, const std::string &new_name) const
 
getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &nm) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void mooseError (Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &relative_path) const
 
std::string getDataFilePath (const std::string &relative_path) const
 
virtual void initialSetup ()
 
virtual void timestepSetup ()
 
virtual void jacobianSetup ()
 
virtual void residualSetup ()
 
virtual void subdomainSetup ()
 
virtual void customSetup (const ExecFlagType &)
 
const ExecFlagEnumgetExecuteOnEnum () const
 
PerfGraphperfGraph ()
 
T & getSampler (const std::string &name)
 
SamplergetSampler (const std::string &name)
 
T & getSamplerByName (const SamplerName &name)
 
SamplergetSamplerByName (const SamplerName &name)
 
const VectorPostprocessorValuegetVectorPostprocessorValue (const std::string &param_name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValue (const std::string &param_name, const std::string &vector_name, bool needs_broadcast) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueByName (const VectorPostprocessorName &name, const std::string &vector_name, bool needs_broadcast) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOld (const std::string &param_name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOld (const std::string &param_name, const std::string &vector_name, bool needs_broadcast) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOldByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
const VectorPostprocessorValuegetVectorPostprocessorValueOldByName (const VectorPostprocessorName &name, const std::string &vector_name, bool needs_broadcast) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValue (const std::string &param_name, const std::string &vector_name) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValueByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValueOld (const std::string &param_name, const std::string &vector_name) const
 
const ScatterVectorPostprocessorValuegetScatterVectorPostprocessorValueOldByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
bool hasVectorPostprocessor (const std::string &param_name, const std::string &vector_name) const
 
bool hasVectorPostprocessor (const std::string &param_name) const
 
bool hasVectorPostprocessorByName (const VectorPostprocessorName &name, const std::string &vector_name) const
 
bool hasVectorPostprocessorByName (const VectorPostprocessorName &name) const
 
const VectorPostprocessorName & getVectorPostprocessorName (const std::string &param_name) const
 
DenseMatrix< RealgetGlobalSamples ()
 
DenseMatrix< RealgetGlobalSamples ()
 
DenseMatrix< RealgetLocalSamples ()
 
DenseMatrix< RealgetLocalSamples ()
 
dof_id_type getLocalRowBegin () const
 
dof_id_type getLocalRowBegin () const
 
dof_id_type getLocalRowEnd () const
 
dof_id_type getLocalRowEnd () const
 
const DistributiongetDistribution (const std::string &name) const
 
const T & getDistribution (const std::string &name) const
 
const DistributiongetDistribution (const std::string &name) const
 
const T & getDistribution (const std::string &name) const
 
const DistributiongetDistributionByName (const DistributionName &name) const
 
const T & getDistributionByName (const std::string &name) const
 
const DistributiongetDistributionByName (const DistributionName &name) const
 
const T & getDistributionByName (const std::string &name) const
 
bool isVectorPostprocessorDistributed (const std::string &param_name) const
 
bool isVectorPostprocessorDistributed (const std::string &param_name) const
 
bool isVectorPostprocessorDistributedByName (const VectorPostprocessorName &name) const
 
bool isVectorPostprocessorDistributedByName (const VectorPostprocessorName &name) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static InputParameters validParams ()
 

Public Attributes

const ConsoleStream _console
 

Protected Types

enum  CommMethod
 

Protected Member Functions

virtual Real computeSample (dof_id_type row_index, dof_id_type col_index) override
 Return the sample for the given row and column. More...
 
virtual void sampleSetUp (const SampleMode mode) override
 Here we need to precompute rows that might not be assigned to this processor. More...
 
void setNumberOfRandomSeeds (std::size_t number)
 
Real getRand (unsigned int index=0)
 
uint32_t getRandl (unsigned int index, uint32_t lower, uint32_t upper)
 
virtual LocalRankConfig constructRankConfig (bool batch_mode) const
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level) const
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level, const std::string &live_message, const bool print_dots=true) const
 
std::string timedSectionName (const std::string &section_name) const
 
virtual void addVectorPostprocessorDependencyHelper (const VectorPostprocessorName &) const
 
const ReporterNamegetReporterName (const std::string &param_name) const
 
virtual void addReporterDependencyHelper (const ReporterName &)
 
void setNumberOfRows (dof_id_type n_rows)
 
void setNumberOfRows (dof_id_type n_rows)
 
void setNumberOfCols (dof_id_type n_cols)
 
void setNumberOfCols (dof_id_type n_cols)
 
virtual void sampleTearDown (const SampleMode)
 
virtual void sampleTearDown (const SampleMode)
 
virtual void computeSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeLocalSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeLocalSampleMatrix (DenseMatrix< Real > &matrix)
 
virtual void computeSampleRow (dof_id_type i, std::vector< Real > &data)
 
virtual void computeSampleRow (dof_id_type i, std::vector< Real > &data)
 
virtual void advanceGenerators (const dof_id_type count)
 
virtual void advanceGenerators (const dof_id_type count)
 
virtual void advanceGenerator (const unsigned int seed_index, const dof_id_type count)
 
virtual void advanceGenerator (const unsigned int seed_index, const dof_id_type count)
 
void setAutoAdvanceGenerators (const bool state)
 
void setAutoAdvanceGenerators (const bool state)
 
void shuffle (std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
 
void shuffle (std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
 
virtual void executeSetUp ()
 
virtual void executeSetUp ()
 
virtual void executeTearDown ()
 
virtual void executeTearDown ()
 
void saveGeneratorState ()
 
void saveGeneratorState ()
 
void restoreGeneratorState ()
 
void restoreGeneratorState ()
 
const T & getReporterValue (const std::string &param_name, const std::size_t time_index=0)
 
const T & getReporterValue (const std::string &param_name, ReporterMode mode, const std::size_t time_index=0)
 
const T & getReporterValue (const std::string &param_name, const std::size_t time_index=0)
 
const T & getReporterValue (const std::string &param_name, ReporterMode mode, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, ReporterMode mode, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, const std::size_t time_index=0)
 
const T & getReporterValueByName (const ReporterName &reporter_name, ReporterMode mode, const std::size_t time_index=0)
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValue (const std::string &param_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 
bool hasReporterValueByName (const ReporterName &reporter_name) const
 

Protected Attributes

std::vector< const Distribution * > _distributions
 Storage for distribution objects to be utilized. More...
 
std::vector< std::size_t > _loop_index
 The loop index for distribution. More...
 
std::vector< dof_id_type_loop_mod
 Helper for determining if a set of columns need to be recomputed: if (row_index % _loop_mod[_loop_index[col_index]] == 0) More...
 
std::vector< Real_row_data
 Storage for row data (to be used when not recomputing a column) More...
 
 NONE
 
 LOCAL
 
 SEMI_LOCAL
 
const dof_id_type _min_procs_per_row
 
const dof_id_type _max_procs_per_row
 
libMesh::Parallel::Communicator _local_comm
 
const bool & _enabled
 
MooseApp_app
 
const std::string _type
 
const std::string _name
 
const InputParameters_pars
 
Factory_factory
 
ActionFactory_action_factory
 
const ExecFlagEnum_execute_enum
 
const ExecFlagType_current_execute_flag
 
MooseApp_pg_moose_app
 
const std::string _prefix
 
const Parallel::Communicator & _communicator
 

Detailed Description

A class used to perform nested Monte Carlo Sampling.

Definition at line 17 of file NestedMonteCarloSampler.h.

Constructor & Destructor Documentation

◆ NestedMonteCarloSampler()

NestedMonteCarloSampler::NestedMonteCarloSampler ( const InputParameters parameters)

Definition at line 31 of file NestedMonteCarloSampler.C.

33 {
34  // Grab inputs and make sure size is consistent
35  const auto & dnames = getParam<std::vector<std::vector<DistributionName>>>("distributions");
36  const auto & nrows = getParam<std::vector<dof_id_type>>("num_rows");
37  if (dnames.size() != nrows.size())
38  paramError("distributions",
39  "There must be a set of distributions for each loop defined by 'num_rows'.");
40 
41  // Gather distribution pointers and fill in loop index
42  const std::size_t nloop = dnames.size();
43  for (const auto & n : make_range(nloop))
44  for (const auto & name : dnames[n])
45  {
47  _loop_index.push_back(n);
48  }
49 
50  // Compute what row indices need to recompute which columns
51  _loop_mod.resize(nloop);
52  std::partial_sum(
53  nrows.rbegin(), nrows.rend(), _loop_mod.rbegin(), std::multiplies<dof_id_type>());
54  _loop_mod.erase(_loop_mod.begin());
55  _loop_mod.push_back(1);
56 
57  // Allocate row storage
58  _row_data.resize(_distributions.size());
59 
60  setNumberOfRows(std::accumulate(nrows.begin(), nrows.end(), 1, std::multiplies<dof_id_type>()));
62 }
void setNumberOfRows(dof_id_type n_rows)
std::vector< const Distribution * > _distributions
Storage for distribution objects to be utilized.
std::vector< std::size_t > _loop_index
The loop index for distribution.
virtual const std::string & name() const
Sampler(const InputParameters &parameters)
void paramError(const std::string &param, Args... args) const
const Distribution & getDistributionByName(const DistributionName &name) const
void setNumberOfCols(dof_id_type n_cols)
std::vector< dof_id_type > _loop_mod
Helper for determining if a set of columns need to be recomputed: if (row_index % _loop_mod[_loop_ind...
IntRange< T > make_range(T beg, T end)
const InputParameters & parameters() const
std::vector< Real > _row_data
Storage for row data (to be used when not recomputing a column)

Member Function Documentation

◆ computeSample()

Real NestedMonteCarloSampler::computeSample ( dof_id_type  row_index,
dof_id_type  col_index 
)
overrideprotectedvirtual

Return the sample for the given row and column.

Implements Sampler.

Definition at line 86 of file NestedMonteCarloSampler.C.

Referenced by sampleSetUp().

87 {
88  const Real rn = getRand();
89  const auto & loop = _loop_index[col_index];
90  if (row_index % _loop_mod[loop] == 0)
91  _row_data[col_index] = _distributions[col_index]->quantile(rn);
92  return _row_data[col_index];
93 }
std::vector< const Distribution * > _distributions
Storage for distribution objects to be utilized.
std::vector< std::size_t > _loop_index
The loop index for distribution.
Real getRand(unsigned int index=0)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< dof_id_type > _loop_mod
Helper for determining if a set of columns need to be recomputed: if (row_index % _loop_mod[_loop_ind...
std::vector< Real > _row_data
Storage for row data (to be used when not recomputing a column)

◆ sampleSetUp()

void NestedMonteCarloSampler::sampleSetUp ( const SampleMode  mode)
overrideprotectedvirtual

Here we need to precompute rows that might not be assigned to this processor.

Reimplemented from Sampler.

Definition at line 65 of file NestedMonteCarloSampler.C.

66 {
67  if (mode == Sampler::SampleMode::GLOBAL || getNumberOfRows() == 0)
68  return;
69 
70  dof_id_type curr_row = 0;
71  for (const auto & mod : _loop_mod)
72  {
73  if (getLocalRowBegin() % mod == 0)
74  break;
75 
76  const dof_id_type target_row = std::floor(getLocalRowBegin() / mod) * mod;
77  advanceGenerators((target_row - curr_row) * getNumberOfCols());
78  for (const auto & j : make_range(getNumberOfCols()))
79  computeSample(target_row, j);
80  curr_row = target_row + 1;
81  }
83 }
virtual void advanceGenerators(const dof_id_type count)
dof_id_type getLocalRowBegin() const
void restoreGeneratorState()
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Return the sample for the given row and column.
dof_id_type getNumberOfRows() const
std::vector< dof_id_type > _loop_mod
Helper for determining if a set of columns need to be recomputed: if (row_index % _loop_mod[_loop_ind...
IntRange< T > make_range(T beg, T end)
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
dof_id_type getNumberOfCols() const
uint8_t dof_id_type

◆ validParams()

InputParameters NestedMonteCarloSampler::validParams ( )
static

Definition at line 16 of file NestedMonteCarloSampler.C.

17 {
19  params.addClassDescription("Monte Carlo sampler for nested loops of parameters.");
20  params.addRequiredParam<std::vector<dof_id_type>>(
21  "num_rows",
22  "The number of rows for each loop of parameters. The first number represents the outermost "
23  "loop.");
24  params.addRequiredParam<std::vector<std::vector<DistributionName>>>(
25  "distributions",
26  "Sets of distribution names to be sampled. Each set defines the parameters for the nested "
27  "loop, with the first set being the outermost loop.");
28  return params;
29 }
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)

Member Data Documentation

◆ _distributions

std::vector<const Distribution *> NestedMonteCarloSampler::_distributions
protected

Storage for distribution objects to be utilized.

Definition at line 32 of file NestedMonteCarloSampler.h.

Referenced by computeSample(), and NestedMonteCarloSampler().

◆ _loop_index

std::vector<std::size_t> NestedMonteCarloSampler::_loop_index
protected

The loop index for distribution.

Definition at line 34 of file NestedMonteCarloSampler.h.

Referenced by computeSample(), and NestedMonteCarloSampler().

◆ _loop_mod

std::vector<dof_id_type> NestedMonteCarloSampler::_loop_mod
protected

Helper for determining if a set of columns need to be recomputed: if (row_index % _loop_mod[_loop_index[col_index]] == 0)

Definition at line 37 of file NestedMonteCarloSampler.h.

Referenced by computeSample(), NestedMonteCarloSampler(), and sampleSetUp().

◆ _row_data

std::vector<Real> NestedMonteCarloSampler::_row_data
protected

Storage for row data (to be used when not recomputing a column)

Definition at line 39 of file NestedMonteCarloSampler.h.

Referenced by computeSample(), and NestedMonteCarloSampler().


The documentation for this class was generated from the following files: