www.mooseframework.org
SobolSampler.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 #include "SobolSampler.h"
9 
10 template <>
11 InputParameters
13 {
14  InputParameters params = validParams<Sampler>();
15  params.addClassDescription("Sobol variance-based sensitivity analysis Sampler.");
16  params.addRequiredParam<unsigned int>(
17  "n_samples", "Number of Monte Carlo samples to perform for each distribution.");
18  return params;
19 }
20 
21 SobolSampler::SobolSampler(const InputParameters & parameters)
22  : Sampler(parameters),
23  _num_samples(getParam<unsigned int>("n_samples")),
24  _a_matrix(0, 0),
25  _b_matrix(0, 0)
26 {
27  setNumberOfRequiedRandomSeeds(2);
28 }
29 
30 void
32 {
33  _a_matrix.resize(_num_samples, _distributions.size());
34  _b_matrix.resize(_num_samples, _distributions.size());
35  for (std::size_t i = 0; i < _num_samples; ++i)
36  for (auto j = beginIndex(_distributions); j < _distributions.size(); ++j)
37  {
38  _a_matrix(i, j) = _distributions[j]->quantile(this->rand(0));
39  _b_matrix(i, j) = _distributions[j]->quantile(this->rand(1));
40  }
41 }
42 
43 void
45 {
46  _a_matrix.resize(0, 0);
47  _b_matrix.resize(0, 0);
48 }
49 
50 std::vector<DenseMatrix<Real>>
52 {
53  // Create the output vector
54  auto n = _distributions.size() + 2;
55  std::vector<DenseMatrix<Real>> output(n);
56 
57  // Include the A and B matrices
58  output[0] = _a_matrix;
59  output[1] = _b_matrix;
60 
61  // Create the AB matrices
62  for (auto idx = beginIndex(_distributions, 2); idx < n; ++idx)
63  {
64  output[idx] = _a_matrix;
65  for (std::size_t i = 0; i < _num_samples; ++i)
66  output[idx](i, idx - 2) = _b_matrix(i, idx - 2);
67  }
68 
69  return output;
70 }
DenseMatrix< Real > _b_matrix
Definition: SobolSampler.h:36
InputParameters validParams< SobolSampler >()
Definition: SobolSampler.C:12
SobolSampler(const InputParameters &parameters)
Definition: SobolSampler.C:21
virtual std::vector< DenseMatrix< Real > > sample() override
Definition: SobolSampler.C:51
DenseMatrix< Real > _a_matrix
Definition: SobolSampler.h:35
virtual void sampleSetUp() override
Definition: SobolSampler.C:31
const std::size_t _num_samples
Number of Monte Carlo samples to create for each Sobol matrix.
Definition: SobolSampler.h:31
virtual void sampleTearDown() override
Definition: SobolSampler.C:44