https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
11#include "Distribution.h"
13
14registerMooseObjectAliased("StochasticToolsApp", LatinHypercubeSampler, "LatinHypercube");
15
18{
20 params.addClassDescription("Latin Hypercube Sampler.");
21 params.addRequiredParam<dof_id_type>("num_rows", "The size of the square matrix to generate.");
22 params.addRequiredParam<std::vector<DistributionName>>(
23 "distributions",
24 "The distribution names to be sampled, the number of distributions provided defines the "
25 "number of columns per matrix.");
26 return params;
27}
28
30 : Sampler(parameters),
31 _shufflers(declareRestartableData<std::vector<std::unique_ptr<MooseRandomPerturbation>>>(
32 "lhs_shufflers"))
33{
34 const auto & distribution_names = getParam<std::vector<DistributionName>>("distributions");
35 for (const DistributionName & name : distribution_names)
37
38 setNumberOfRows(getParam<dof_id_type>("num_rows"));
39 setNumberOfCols(distribution_names.size());
40 // Generator 0: within-bin uniform draws. Generator 1: column shuffler seeds.
42}
43
44void
46{
47 _shufflers.clear();
48 for (const auto col : make_range(getNumberOfCols()))
49 {
50 const auto seed = getRandl(col, 0, std::numeric_limits<uint32_t>::max(), 1);
51 _shufflers.push_back(std::make_unique<MooseRandomPerturbation>(seed, getNumberOfRows()));
52 }
53}
54
55Real
56LatinHypercubeSampler::computeSample(dof_id_type row_index, dof_id_type col_index) const
57{
58 mooseAssert(_shufflers.size() > 0, "Shufflers have not been initialized.");
59
60 // Divide [0,1] into N equal bins of width 1/N.
61 const Real bin_size = 1. / getNumberOfRows();
62
63 // Map row_index to a shuffled bin via the column's bijective permutation.
64 // Because permute() is a bijection on [0, N), each row gets a distinct bin,
65 // which is the core LHS stratification guarantee.
66 const auto bin = _shufflers[col_index]->permute(row_index);
67
68 // Draw a uniform random point within the selected bin.
69 const auto lower = bin * bin_size;
70 const auto upper = (bin + 1) * bin_size;
71 const Real probability =
72 getRand(row_index * getNumberOfCols() + col_index) * (upper - lower) + lower;
73
74 // Transform the probability through the inverse CDF to obtain the sample value.
75 return _distributions[col_index]->quantile(probability);
76}
registerMooseObjectAliased("StochasticToolsApp", LatinHypercubeSampler, "LatinHypercube")
const Distribution & getDistributionByName(const DistributionName &name) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
Implements Latin Hypercube Sampling (LHS) over a set of distributions.
LatinHypercubeSampler(const InputParameters &parameters)
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) const override
Return the sample value for the given row and column.
virtual void executeTearDown() override
Constructs one MooseRandomPerturbation per column, each seeded independently from generator 1.
std::vector< Distribution const * > _distributions
Distribution objects, one per column, whose quantile functions are sampled.
static InputParameters validParams()
std::vector< std::unique_ptr< MooseRandomPerturbation > > & _shufflers
Per-column pseudo-random permuters that enforce the LHS bin assignment.
const std::string & name() const
void setNumberOfCols(dof_id_type n_cols)
Real getRand(std::size_t n, unsigned int index=0) const
unsigned int getRandl(std::size_t n, unsigned int lower, unsigned int upper, unsigned int index=0) const
dof_id_type getNumberOfRows() const
static InputParameters validParams()
dof_id_type getNumberOfCols() const
void setNumberOfRows(dof_id_type n_rows)
void setNumberOfRandomSeeds(std::size_t number)