20 #include "libmesh/quadrature.h" 30 params.
addClassDescription(
"Compute SOBOL statistics values of a given VectorPostprocessor or " 31 "Reporter objects and vectors.");
32 params.
addParam<SamplerName>(
"sampler",
"SobolSampler object.");
34 params.
addParam<std::vector<VectorPostprocessorName>>(
35 "vectorpostprocessors",
36 "List of VectorPostprocessor(s) to utilized for statistic computations.");
38 params.
addParam<std::vector<ReporterName>>(
39 "reporters", {},
"List of Reporter values to utilized for statistic computations.");
44 "A vector of confidence levels to consider, values must be in (0, 1).");
48 "The number of replicates to use when computing confidence level intervals.");
49 params.
addParam<
unsigned int>(
"ci_seed",
51 "The random number generator seed used for creating replicates " 52 "while computing confidence level intervals.");
59 _ci_levels(getParam<
std::vector<
Real>>(
"ci_levels")),
60 _ci_replicates(getParam<unsigned
int>(
"ci_replicates")),
61 _ci_seed(getParam<unsigned
int>(
"ci_seed")),
68 paramError(
"ci_levels",
"The supplied levels must be greater than zero.");
70 paramError(
"ci_levels",
"The supplied levels must be less than 1.0");
74 (
getParam<std::vector<ReporterName>>(
"reporters").empty() &&
75 getParam<std::vector<VectorPostprocessorName>>(
"vectorpostprocessors").empty()))
77 "The 'vectorpostprocessors' and/or 'reporters' parameters must be defined and non-empty.");
89 std::vector<std::string> unsupported_types;
90 const auto & reporter_names = getParam<std::vector<ReporterName>>(
"reporters");
91 for (
const auto & r_name : reporter_names)
94 declareValueHelper<Real>(r_name);
98 unsupported_types.emplace_back(r_name.getCombinedName());
101 if (!unsupported_types.empty())
103 "The following reporter value(s) do not have a type supported by the " 104 "StatisticsReporter:\n",
111 const auto & vpp_names = getParam<std::vector<VectorPostprocessorName>>(
"vectorpostprocessors");
112 for (
const auto & vpp_name : vpp_names)
116 const std::set<std::string> & vpp_vectors = vpp_object.
getVectorNames();
117 for (
const auto & vec_name : vpp_vectors)
120 declareValueHelper<Real>(r_name);
133 json[
"confidence_intervals"] = {{
"method",
"percentile"},
139 json[
"indices"].push_back(
"FIRST_ORDER");
140 json[
"indices"].push_back(
"TOTAL");
142 json[
"indices"].push_back(
"SECOND_ORDER");
145 template <
typename T>
150 const auto & data = getReporterValueByName<std::vector<T>>(r_name);
168 template <
typename InType,
typename OutType>
180 _calc(other,
"SOBOL", _sampler.
resample())
184 template <
typename InType,
typename OutType>
193 const std::vector<Real> & ci_levels,
194 unsigned int ci_replicates,
195 unsigned int ci_seed)
199 StochasticTools::makeBootstrapCalculator<std::vector<InType>, std::vector<OutType>>(
200 ci_method, other, ci_levels, ci_replicates, ci_seed,
_calc);
203 template <
typename InType,
typename OutType>
207 auto & val = this->_state.value();
212 if (is_dist || this->processor_id() == 0)
214 const std::size_t ncol =
215 _sampler.resample() ? 2 * _sampler.getNumberOfCols() + 2 : _sampler.getNumberOfCols() + 2;
216 const std::vector<InType> data_reshape =
219 val.first = _calc.compute(data_reshape, is_dist);
221 val.second = _ci_calc_ptr->compute(data_reshape, is_dist);
227 template <
typename InType,
typename OutType>
231 storeSobol(json, this->_state.value(), _sampler.getNumberOfCols());
234 template <
typename InType,
typename OutType>
240 if (val.first.empty())
244 const unsigned int nlevels = val.second.size();
246 std::pair<std::vector<OutType>, std::vector<std::vector<OutType>>> first_order;
247 std::pair<std::vector<OutType>, std::vector<std::vector<OutType>>> total;
249 first_order.first.assign(val.first.begin(), val.first.begin() + nparam);
250 total.first.assign(val.first.begin() + nparam, val.first.begin() + nparam + nparam);
253 first_order.second.resize(nlevels);
254 total.second.resize(nlevels);
257 first_order.second[l].assign(val.second[l].begin(), val.second[l].begin() + nparam);
258 total.second[l].assign(val.second[l].begin() + nparam,
259 val.second[l].begin() + nparam + nparam);
262 json[
"FIRST_ORDER"] = first_order;
263 json[
"TOTAL"] = total;
265 if (val.first.size() > 2 * nparam)
267 std::pair<std::vector<std::vector<OutType>>, std::vector<std::vector<std::vector<OutType>>>>
270 second_order.first.resize(nparam, std::vector<OutType>(nparam));
272 second_order.first[i][i] = val.first[i];
273 unsigned int ind = 2 * nparam;
277 second_order.first[i][
j] = val.first[ind++];
278 second_order.first[
j][i] = second_order.first[i][
j];
283 second_order.second.resize(nlevels, second_order.first);
288 second_order.second[l][i][i] = val.second[l][i];
293 second_order.second[l][i][
j] = val.second[l][ind++];
294 second_order.second[l][
j][i] = second_order.second[l][i][
j];
298 json[
"SECOND_ORDER"] = second_order;
302 template void SobolReporter::declareValueHelper<Real>(
const ReporterName & r_name);
304 template void SobolReporter::declareValueHelper<std::vector<Real>>(
const ReporterName & r_name);
A class used to perform Monte Carlo sampling for performing Sobol sensitivity analysis.
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
T & declareValueByName(const ReporterValueName &value_name, Args &&... args)
const SobolSampler & _sobol_sampler
The sampler that generated the samples that produced results for the _results_vectors.
bool resample() const
Resampling flag, see SobolStatistics.
const ReporterMode REPORTER_MODE_ROOT
std::unique_ptr< StochasticTools::BootstrapCalculator< std::vector< InType >, std::vector< OutType > > > _ci_calc_ptr
Storage for the BootstrapCalculator for the desired confidence interval calculations (optional) ...
void declareValueHelper(const ReporterName &r_name)
Helper for adding Sobol reporter values.
std::vector< T > resample(const std::vector< T > &data, MooseRandom &generator, const std::size_t seed_index=0)
static InputParameters validParams()
const std::vector< Real > & _ci_levels
CI levels to be computed.
virtual void store(nlohmann::json &json) const
bool isParamValid(const std::string &name) const
const ReporterData & getReporterData() const
static void storeSobol(nlohmann::json &json, const SobolState< OutType > &val, unsigned int nparam)
const unsigned int & _ci_seed
Random seed for producing CI replicates.
std::pair< std::vector< OutType >, std::vector< std::vector< OutType > >> SobolState
virtual void finalize() override
const T & getParam(const std::string &name) const
bool _initialized
Whether or not initialize() has been called for reporter value declaration.
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
void paramError(const std::string ¶m, Args... args) const
const ReporterMode REPORTER_MODE_DISTRIBUTED
const std::string & getObjectName() const
const unsigned int & _ci_replicates
Number of CI replicates to use in Bootstrap methods.
SobolReporter(const InputParameters ¶meters)
virtual void initialize() override
virtual void store(nlohmann::json &json) const override
const ReporterProducerEnum & getReporterMode(const ReporterName &reporter_name) const
virtual void store(nlohmann::json &json) const override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
FEProblemBase & _fe_problem
registerMooseObject("StochasticToolsApp", SobolReporter)
static InputParameters validParams()
StochasticTools::SobolCalculator< InType, OutType > _calc
Storage for the SobolCalculator object, this is created in constructor.
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
const std::string & getValueName() const
SobolReporterContext(const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< SobolState< OutType >> &state, const InType &data, const ReporterProducerEnum &mode, const SobolSampler &sampler)
const std::set< std::string > & getVectorNames() const
void ErrorVector unsigned int
dof_id_type getNumberOfCols() const
Computes Sobol sensitivity indices, see SobolCalculators.
bool hasReporterValueByName(const ReporterName &reporter_name) const