https://mooseframework.inl.gov
SobolStatistics.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 "SobolStatistics.h"
11 #include "SobolSampler.h"
12 #include "SobolCalculators.h"
13 #include "BootstrapCalculators.h"
14 
15 // MOOSE includes
16 #include "MooseVariable.h"
18 #include "ThreadedNodeLoop.h"
19 
20 #include "libmesh/quadrature.h"
21 
22 #include <numeric>
23 
24 registerMooseObjectDeprecated("StochasticToolsApp", SobolStatistics, "11/03/2021 12:00");
25 
28 {
30  params.addClassDescription(
31  "Compute SOBOL statistics values of a given VectorPostprocessor objects and vectors.");
32  params.addParam<SamplerName>("sampler", "SobolSampler object.");
33  params.addParam<VectorPostprocessorName>(
34  "results", "StochasticResults object containing data to use for calculation.");
35 
36  params.addParam<std::vector<Real>>(
37  "ci_levels",
38  std::vector<Real>(),
39  "A vector of confidence levels to consider, values must be in (0, 1).");
40  params.addParam<unsigned int>(
41  "ci_replicates",
42  10000,
43  "The number of replicates to use when computing confidence level intervals.");
44  params.addParam<unsigned int>("ci_seed",
45  1,
46  "The random number generator seed used for creating replicates "
47  "while computing confidence level intervals.");
48  return params;
49 }
50 
52  : GeneralVectorPostprocessor(parameters),
53  _sobol_sampler(getSampler<SobolSampler>("sampler")),
54  _ci_levels(getParam<std::vector<Real>>("ci_levels")),
55  _ci_replicates(getParam<unsigned int>("ci_replicates")),
56  _ci_seed(getParam<unsigned int>("ci_seed"))
57 {
58 }
59 
60 void
62 {
63  const VectorPostprocessorName & vpp_name = getParam<VectorPostprocessorName>("results");
65  const std::set<std::string> & vpp_vectors = vpp_object.getVectorNames();
66  _sobol_ci_vectors.resize(_ci_levels.size());
67  for (const auto & vec_name : vpp_vectors)
68  {
69  ReporterName rname(vpp_name, vec_name);
70  _result_vectors.push_back(std::make_pair(
71  &getReporterValueByName<VectorPostprocessorValue>(rname), vpp_object.isDistributed()));
72  _sobol_stat_vectors.push_back(&declareVector(vpp_name + "_" + vec_name));
73 
74  for (const auto & l : index_range(_ci_levels))
75  {
76  std::stringstream vname;
77  vname << vpp_name << "_" << vec_name << "_" << _ci_levels[l] * 100 << "CI";
78  _sobol_ci_vectors[l].push_back(&declareVector(vname.str()));
79  }
80  }
81 }
82 
83 void
85 {
86  TIME_SECTION("execute", 3, "Executing Sobol Statistics");
87 
89  *this, "SOBOL", _sobol_sampler.resample());
90  auto boot_calc = _ci_levels.empty()
91  ? nullptr
92  : makeBootstrapCalculator(MooseEnum("percentile", "percentile"),
93  *this,
94  _ci_levels,
96  _ci_seed,
97  calc);
98  for (std::size_t i = 0; i < _result_vectors.size(); ++i)
99  {
100  const std::size_t ncol = _sobol_sampler.resample() ? 2 * _sobol_sampler.getNumberOfCols() + 2
102  const std::vector<std::vector<Real>> data =
103  StochasticTools::reshapeVector(*(_result_vectors[i].first), ncol, /*row_major =*/true);
104  (*_sobol_stat_vectors[i]) = calc.compute(data, _result_vectors[i].second);
105 
106  if (boot_calc)
107  {
108  std::vector<std::vector<Real>> sobol_ci = boot_calc->compute(data, _result_vectors[i].second);
109  for (const auto & l : index_range(sobol_ci))
110  (*_sobol_ci_vectors[l][i]) = std::move(sobol_ci[l]);
111  }
112  }
113 }
A class used to perform Monte Carlo sampling for performing Sobol sensitivity analysis.
Definition: SobolSampler.h:23
std::vector< std::vector< VectorPostprocessorValue * > > _sobol_ci_vectors
Confidence interval vectors computed by this object.
std::vector< std::vector< T > > reshapeVector(const std::vector< T > &vec, std::size_t n, bool row_major)
Reshape a vector into matrix-like vector of vectors.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
std::vector< std::pair< const VectorPostprocessorValue *, bool > > _result_vectors
Result vectors from StocasticResults object.
std::vector< OutType > compute(const std::vector< InType > &, bool)
Evaluate the calculator on the full vector of data.
Definition: Calculators.h:288
bool resample() const
Resampling flag, see SobolStatistics.
Definition: SobolSampler.h:31
const std::vector< Real > & _ci_levels
registerMooseObjectDeprecated("StochasticToolsApp", SobolStatistics, "11/03/2021 12:00")
SobolStatistics(const InputParameters &parameters)
virtual void execute() override
virtual void initialSetup() override
static InputParameters validParams()
std::vector< VectorPostprocessorValue * > _sobol_stat_vectors
Vectors computed by this object.
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Computes Sobol sensitivity indices, see SobolCalculators.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const SobolSampler & _sobol_sampler
The sampler that generated the samples that produced results for the _results_vectors.
FEProblemBase & _fe_problem
const unsigned int & _ci_seed
void addClassDescription(const std::string &doc_string)
static InputParameters validParams()
const unsigned int & _ci_replicates
const std::set< std::string > & getVectorNames() const
void ErrorVector unsigned int
auto index_range(const T &sizable)
dof_id_type getNumberOfCols() const
Calculator for computing Sobol sensitivity indices according to the paper by Saltelli (2002) https://...
std::unique_ptr< BootstrapCalculator< InType, OutType > > makeBootstrapCalculator(const MooseEnum &, const libMesh::ParallelObject &, const std::vector< Real > &, unsigned int, unsigned int, StochasticTools::Calculator< InType, OutType > &)