https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
14
15// MOOSE includes
16#include "MooseVariable.h"
18#include "ThreadedNodeLoop.h"
19
20#include "libmesh/quadrature.h"
21
22#include <numeric>
23
24registerMooseObjectDeprecated("StochasticToolsApp", SobolStatistics, "11/03/2021 12:00");
25
28{
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
60void
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
83void
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,
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}
registerMooseObjectDeprecated("StochasticToolsApp", SobolStatistics, "11/03/2021 12:00")
void ErrorVector unsigned int
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
dof_id_type getNumberOfCols() const
A class used to perform Monte Carlo sampling for performing Sobol sensitivity analysis.
bool resample() const
Resampling flag, see SobolStatistics.
Computes Sobol sensitivity indices, see SobolCalculators.
const unsigned int & _ci_seed
SobolStatistics(const InputParameters &parameters)
std::vector< VectorPostprocessorValue * > _sobol_stat_vectors
Vectors computed by this object.
std::vector< std::pair< const VectorPostprocessorValue *, bool > > _result_vectors
Result vectors from StocasticResults object.
static InputParameters validParams()
virtual void execute() override
std::vector< std::vector< VectorPostprocessorValue * > > _sobol_ci_vectors
Confidence interval vectors computed by this object.
const unsigned int & _ci_replicates
const SobolSampler & _sobol_sampler
The sampler that generated the samples that produced results for the _results_vectors.
const std::vector< Real > & _ci_levels
virtual void initialSetup() override
OutType compute(const InType &, bool)
Evaluate the calculator on the full vector of data.
Calculator for computing Sobol sensitivity indices according to the paper by Saltelli (2002) https://...
FEProblemBase & _fe_problem
bool isDistributed() const
const std::set< std::string > & getVectorNames() const
VectorPostprocessorValue & declareVector(const std::string &vector_name)
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.