www.mooseframework.org
SobolSampler.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "SobolSampler.h"
11 #include "Distribution.h"
12 
13 registerMooseObject("StochasticToolsApp", SobolSampler);
14 
16 
17 InputParameters
19 {
20  InputParameters params = Sampler::validParams();
21  params.addClassDescription("Sobol variance-based sensitivity analysis Sampler.");
22  params.addRequiredParam<dof_id_type>("num_rows", "The number of rows per matrix to generate.");
23  params.addRequiredParam<std::vector<DistributionName>>(
24  "distributions",
25  "The distribution names to be sampled, the number of distributions provided defines the "
26  "number of columns per matrix.");
27  return params;
28 }
29 
30 SobolSampler::SobolSampler(const InputParameters & parameters)
31  : Sampler(parameters),
32  _a_matrix(0, 0),
33  _b_matrix(0, 0),
34  _distribution_names(getParam<std::vector<DistributionName>>("distributions")),
35  _num_rows_per_matrix(getParam<dof_id_type>("num_rows"))
36 {
37  setNumberOfRandomSeeds(2);
38 
39  for (const DistributionName & name : _distribution_names)
40  _distributions.push_back(&getDistributionByName(name));
41 
42  setNumberOfCols(_distribution_names.size());
43  setNumberOfRows(_num_rows_per_matrix * (_distribution_names.size() + 2));
44 }
45 
46 void
48 {
49  _a_matrix.resize(_num_rows_per_matrix, getNumberOfCols());
50  _b_matrix.resize(_num_rows_per_matrix, getNumberOfCols());
51  for (dof_id_type i = 0; i < _num_rows_per_matrix; ++i)
52  for (dof_id_type j = 0; j < getNumberOfCols(); ++j)
53  {
54  _a_matrix(i, j) = _distributions[j]->quantile(this->getRand(0));
55  _b_matrix(i, j) = _distributions[j]->quantile(this->getRand(1));
56  }
57 }
58 
59 Real
60 SobolSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
61 {
62  dof_id_type matrix_index = row_index / _num_rows_per_matrix;
63  dof_id_type r = row_index - matrix_index * _num_rows_per_matrix;
64 
65  if (matrix_index == 0)
66  return _a_matrix(r, col_index);
67  else if (matrix_index == 1)
68  return _b_matrix(r, col_index);
69  else if (col_index == matrix_index - 2)
70  return _b_matrix(r, col_index);
71  else
72  return _a_matrix(r, col_index);
73 }
74 
75 void
77 {
78  _a_matrix.resize(0, 0);
79  _b_matrix.resize(0, 0);
80 }
defineLegacyParams
defineLegacyParams(SobolSampler)
SobolSampler::sampleTearDown
virtual void sampleTearDown() override
Definition: SobolSampler.C:76
SobolSampler::_distribution_names
const std::vector< DistributionName > & _distribution_names
Distribution names.
Definition: SobolSampler.h:43
SobolSampler::_b_matrix
DenseMatrix< Real > _b_matrix
Definition: SobolSampler.h:36
registerMooseObject
registerMooseObject("StochasticToolsApp", SobolSampler)
SobolSampler::_num_rows_per_matrix
const dof_id_type _num_rows_per_matrix
The number of rows per matrix.
Definition: SobolSampler.h:46
SobolSampler::computeSample
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Definition: SobolSampler.C:60
SobolSampler::sampleSetUp
virtual void sampleSetUp() override
Definition: SobolSampler.C:47
SobolSampler::_distributions
std::vector< Distribution const * > _distributions
Storage for distribution objects to be utilized.
Definition: SobolSampler.h:40
SobolSampler::SobolSampler
SobolSampler(const InputParameters &parameters)
Definition: SobolSampler.C:30
validParams
InputParameters validParams()
name
const std::string name
Definition: Setup.h:21
SobolSampler::_a_matrix
DenseMatrix< Real > _a_matrix
Definition: SobolSampler.h:35
SobolSampler.h
SobolSampler::validParams
static InputParameters validParams()
Definition: SobolSampler.C:18
SobolSampler
A class used to perform Monte Carlo Sampling.
Definition: SobolSampler.h:21