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

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

#include <LatinHypercubeSampler.h>

Inheritance diagram for LatinHypercubeSampler:
[legend]

Public Types

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

Public Member Functions

 LatinHypercubeSampler (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 void sampleSetUp (const Sampler::SampleMode mode) override
 
virtual Real computeSample (dof_id_type row_index, dof_id_type col_index) override
 
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< Distribution const * > _distributions
 Storage for distribution objects to be utilized. 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
 

Private Attributes

std::vector< std::vector< Real > > _probabilities
 
bool _is_local
 

Detailed Description

A class used to perform Monte Carlo Sampling.

WARNING! This Sampler manages the generators advancement for parallel operation manually. All the calls to get random values from the generators occur in sampleSetUp. At the end of sampleSetUp all the generators should be in the final state.

Definition at line 21 of file LatinHypercubeSampler.h.

Constructor & Destructor Documentation

◆ LatinHypercubeSampler()

LatinHypercubeSampler::LatinHypercubeSampler ( const InputParameters parameters)

Definition at line 27 of file LatinHypercubeSampler.C.

29 {
30  const auto & distribution_names = getParam<std::vector<DistributionName>>("distributions");
31  for (const DistributionName & name : distribution_names)
33 
34  setNumberOfRows(getParam<dof_id_type>("num_rows"));
35  setNumberOfCols(distribution_names.size());
36  setNumberOfRandomSeeds(2 * distribution_names.size());
37 
38  // The use of MooseRandom in this Sampler is fairly complex. There are two sets of random
39  // generators. The first set (n = number columns) is used to generate the random probability
40  // within each bin of the Latin hypercube sample. The second set (n) is used to shuffle the
41  // probability values. Mainly due to how the shuffle operates, it is necessary for the management
42  // of advancement of the generators to be handled manually.
44 }
void setNumberOfRows(dof_id_type n_rows)
virtual const std::string & name() const
Sampler(const InputParameters &parameters)
const Distribution & getDistributionByName(const DistributionName &name) const
void setAutoAdvanceGenerators(const bool state)
void setNumberOfCols(dof_id_type n_cols)
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.
const InputParameters & parameters() const
void setNumberOfRandomSeeds(std::size_t number)

Member Function Documentation

◆ computeSample()

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

Implements Sampler.

Definition at line 95 of file LatinHypercubeSampler.C.

96 {
97  // NOTE: All calls to generators (getRand, etc.) occur in sampleSetup
98  auto row = _is_local ? row_index - getLocalRowBegin() : row_index;
99  return _distributions[col_index]->quantile(_probabilities[col_index][row]);
100 }
std::vector< std::vector< Real > > _probabilities
dof_id_type getLocalRowBegin() const
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.

◆ sampleSetUp()

void LatinHypercubeSampler::sampleSetUp ( const Sampler::SampleMode  mode)
overrideprotectedvirtual

Reimplemented from Sampler.

Definition at line 47 of file LatinHypercubeSampler.C.

48 {
49  // All calls to the generators occur in here. Calls to the random number generators
50  // (i.e., getRand) are complete by the end of this function.
51 
52  // Flag to indicate what vector index to use in computeSample method
54 
55  const Real bin_size = 1. / getNumberOfRows();
57  if (mode == Sampler::SampleMode::GLOBAL)
58  {
59  for (dof_id_type col = 0; col < getNumberOfCols(); ++col)
60  {
61  std::vector<Real> & local = _probabilities[col];
62  local.resize(getNumberOfRows());
63  for (dof_id_type row = 0; row < getNumberOfRows(); ++row)
64  {
65  const auto lower = row * bin_size;
66  const auto upper = (row + 1) * bin_size;
67  local[row] = getRand(col) * (upper - lower) + lower;
68  }
69  shuffle(local, col + getNumberOfCols(), CommMethod::NONE);
70  }
71  }
72 
73  else
74  {
75  for (dof_id_type col = 0; col < getNumberOfCols(); ++col)
76  {
77  std::vector<Real> & local = _probabilities[col];
78  local.resize(getNumberOfLocalRows());
80  for (dof_id_type row = getLocalRowBegin(); row < getLocalRowEnd(); ++row)
81  {
82  const auto lower = row * bin_size;
83  const auto upper = (row + 1) * bin_size;
84  local[row - getLocalRowBegin()] = getRand(col) * (upper - lower) + lower;
85  }
87 
88  // Do not advance generator for shuffle, the shuffle handles it
89  shuffle(local, col + getNumberOfCols(), CommMethod::SEMI_LOCAL);
90  }
91  }
92 }
void shuffle(std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
std::vector< std::vector< Real > > _probabilities
virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count)
dof_id_type getLocalRowBegin() const
dof_id_type getNumberOfLocalRows() const
Real getRand(unsigned int index=0)
dof_id_type getLocalRowEnd() const
dof_id_type getNumberOfRows() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
dof_id_type getNumberOfCols() const
uint8_t dof_id_type

◆ validParams()

InputParameters LatinHypercubeSampler::validParams ( )
static

Definition at line 15 of file LatinHypercubeSampler.C.

16 {
18  params.addClassDescription("Latin Hypercube Sampler.");
19  params.addRequiredParam<dof_id_type>("num_rows", "The size of the square matrix to generate.");
20  params.addRequiredParam<std::vector<DistributionName>>(
21  "distributions",
22  "The distribution names to be sampled, the number of distributions provided defines the "
23  "number of columns per matrix.");
24  return params;
25 }
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
uint8_t dof_id_type

Member Data Documentation

◆ _distributions

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

Storage for distribution objects to be utilized.

Definition at line 33 of file LatinHypercubeSampler.h.

Referenced by computeSample(), and LatinHypercubeSampler().

◆ _is_local

bool LatinHypercubeSampler::_is_local
private

Definition at line 40 of file LatinHypercubeSampler.h.

Referenced by computeSample(), and sampleSetUp().

◆ _probabilities

std::vector<std::vector<Real> > LatinHypercubeSampler::_probabilities
private

Definition at line 37 of file LatinHypercubeSampler.h.

Referenced by computeSample(), and sampleSetUp().


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