https://mooseframework.inl.gov
DirectPerturbationSampler.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 "DelimitedFileReader.h"
12 
13 registerMooseObject("StochasticToolsApp", DirectPerturbationSampler);
14 
17 {
19  params.addClassDescription(
20  "Sampler that creates samples for a direct perturbation-based sensitivity study.");
21  params.addRequiredParam<std::vector<Real>>(
22  "nominal_parameter_values",
23  "The nominal values of the parameters around which we shall perturb them.");
24  params.addParam<std::vector<Real>>(
25  "relative_perturbation_intervals",
26  {0.01},
27  "The numbers by which the nominal values are multiplied to get a perturbed value.");
28  MooseEnum perturbation_type("central_difference forward_difference", "central_difference");
29  params.addParam<MooseEnum>("perturbation_method",
30  perturbation_type,
31  "The perturbation method to use for creating samples.");
32 
33  return params;
34 }
35 
37  : Sampler(parameters),
38  _nominal_values(getParam<std::vector<Real>>("nominal_parameter_values")),
39  _relative_intervals(getParam<std::vector<Real>>("relative_perturbation_intervals")),
40  _perturbation_method(getParam<MooseEnum>("perturbation_method"))
41 {
42  // TODO: we need to add capability to do absolute intervals too, in case there is a material
43  // property with a nominal value of 0.0
44  for (const auto interval : _relative_intervals)
45  if (interval <= 0.0 || interval >= 1.0)
46  paramError("relative_perturbation_intervals",
47  "The relative perturbation interval must be between 0 and 1!");
48 
49  // The number of samples will always include an addition sample for the reference point
50  dof_id_type num_samples = 0;
51  if (_perturbation_method == "central_difference")
52  num_samples = 2 * _nominal_values.size() + 1;
53  else
54  num_samples = _nominal_values.size() + 1;
55 
56  // Adjusting the sample matrix
57  setNumberOfRows(num_samples);
59 
60  _absolute_intervals = std::vector<Real>(num_samples - 1, 0);
61  _parameter_vectors = std::vector<std::vector<Real>>(num_samples, _nominal_values);
62 
63  // Depending on what kind of perturbation we selected, the parameter values will change
64  if (_perturbation_method == "central_difference")
65  for (const auto i : index_range(_nominal_values))
66  {
68  // +1 because the first sample is the reference point
69  _parameter_vectors[2 * i + 1][i] += _absolute_intervals[i] / 2;
70  _parameter_vectors[2 * i + 2][i] -= _absolute_intervals[i] / 2;
71  }
72  else
73  for (const auto i : index_range(_nominal_values))
74  {
77  }
78 }
79 
80 Real
82 {
83  mooseAssert(param_index < _absolute_intervals.size(),
84  "We don't have the required absolute interval!");
85  return _absolute_intervals[param_index];
86 }
87 
88 Real
90 {
91  mooseAssert(param_index < _absolute_intervals.size(),
92  "We don't have the required relative interval!");
93  return _relative_intervals[param_index];
94 }
95 
96 Real
97 DirectPerturbationSampler::getNominalValue(const Real param_index) const
98 {
99  mooseAssert(param_index < _nominal_values.size(),
100  "We don't have the required nominal values for the given parameter!");
101  return _nominal_values[param_index];
102 }
103 
104 Real
106 {
107  return _parameter_vectors[row_index][col_index];
108 }
Real getAbsoluteInterval(const Real param_index) const
Return the absolute perturbation interval for a given index.
void setNumberOfRows(dof_id_type n_rows)
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override
Return the sample for the given row and column.
static InputParameters validParams()
Real getRelativeInterval(const Real param_index) const
Return the relative perturbation interval for a given index.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const MooseEnum _perturbation_method
The method which is used for the perturbation (one-sided/two-sided)
A class used to generate samples for a direct perturbation analysis.
registerMooseObject("StochasticToolsApp", DirectPerturbationSampler)
std::vector< Real > _absolute_intervals
The intervals for the perturbations.
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
std::vector< std::vector< Real > > _parameter_vectors
The data matrix created using the parameters.
void paramError(const std::string &param, Args... args) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void setNumberOfCols(dof_id_type n_cols)
const std::vector< Real > _nominal_values
The nominal values of the parameters.
void addClassDescription(const std::string &doc_string)
DirectPerturbationSampler(const InputParameters &parameters)
Real getNominalValue(const Real param_index) const
Return the nominal value of the parameter.
const std::vector< Real > _relative_intervals
The relative intervals that should be used for the perturbation of each parameter.
auto index_range(const T &sizable)
uint8_t dof_id_type