https://mooseframework.inl.gov
LatinHypercubeSampler.C
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 #include "LatinHypercubeSampler.h"
11 #include "Distribution.h"
12 registerMooseObjectAliased("StochasticToolsApp", LatinHypercubeSampler, "LatinHypercube");
13 
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 }
26 
28  : Sampler(parameters)
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 }
45 
46 void
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 }
93 
94 Real
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 }
void setNumberOfRows(dof_id_type n_rows)
LatinHypercubeSampler(const InputParameters &parameters)
void shuffle(std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
std::vector< std::vector< Real > > _probabilities
static InputParameters validParams()
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)
virtual const std::string & name() const
void addRequiredParam(const std::string &name, const std::string &doc_string)
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
dof_id_type getLocalRowEnd() const
static InputParameters validParams()
virtual void sampleSetUp(const Sampler::SampleMode mode) override
dof_id_type getNumberOfRows() const
const Distribution & getDistributionByName(const DistributionName &name) const
void setAutoAdvanceGenerators(const bool state)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void setNumberOfCols(dof_id_type n_cols)
registerMooseObjectAliased("StochasticToolsApp", LatinHypercubeSampler, "LatinHypercube")
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.
void addClassDescription(const std::string &doc_string)
A class used to perform Monte Carlo Sampling.
dof_id_type getNumberOfCols() const
uint8_t dof_id_type
void setNumberOfRandomSeeds(std::size_t number)