https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Statistics.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 "Statistics.h"
11
12// MOOSE includes
13#include "MooseVariable.h"
15#include "ThreadedNodeLoop.h"
16
17#include "libmesh/quadrature.h"
18
19#include <numeric>
20
21registerADMooseObjectDeprecated("StochasticToolsApp", Statistics, "07/01/2021 12:00");
22
25{
28 "Compute statistical values of a given VectorPostprocessor objects and vectors.");
29
30 params.addRequiredParam<std::vector<VectorPostprocessorName>>(
31 "vectorpostprocessors",
32 "List of VectorPostprocessor(s) to utilized for statistic computations.");
33
36 "compute",
37 stats,
38 "The statistic(s) to compute for each of the supplied vector postprocessors.");
39
40 // Confidence Levels
42 params.addParam<MooseEnum>(
43 "ci_method", ci, "The method to use for computing confidence level intervals.");
44
45 params.addParam<std::vector<Real>>(
46 "ci_levels", "A vector of confidence levels to consider, values must be in (0, 0.5].");
47 params.addParam<unsigned int>(
48 "ci_replicates",
49 10000,
50 "The number of replicates to use when computing confidence level intervals.");
51 params.addParam<unsigned int>("ci_seed",
52 1,
53 "The random number generator seed used for creating replicates "
54 "while computing confidence level intervals.");
55
56 // Compute values are computed on rank 0 and broadcast
57 params.set<MooseEnum>("parallel_type") = "REPLICATED";
58 params.suppressParameter<MooseEnum>("parallel_type");
59 params.set<bool>("_auto_boradcast") = true;
60
61 return params;
62}
63
65 : GeneralVectorPostprocessor(parameters),
66 _compute_stats(getParam<MultiMooseEnum>("compute")),
67 _ci_method(getParam<MooseEnum>("ci_method")),
68 _ci_levels(_ci_method.isValid() ? computeLevels(getParam<std::vector<Real>>("ci_levels"))
69 : std::vector<Real>()),
70 _replicates(getParam<unsigned int>("ci_replicates")),
71 _seed(getParam<unsigned int>("ci_seed")),
72 _stat_type_vector(declareVector("stat_type"))
73{
74 for (const auto & item : _compute_stats)
75 {
76 _stat_type_vector.push_back(item.id());
77 for (const auto & level : _ci_levels)
78 _stat_type_vector.push_back(item.id() + level);
79 }
80}
81
82void
84{
85 TIME_SECTION("initialSetup", 3, "Setting Up Statistics");
86
87 const auto & vpp_names = getParam<std::vector<VectorPostprocessorName>>("vectorpostprocessors");
88 for (const auto & vpp_name : vpp_names)
89 {
90 const VectorPostprocessor & vpp_object =
92 const std::set<std::string> & vpp_vectors = vpp_object.getVectorNames();
93 for (const auto & vec_name : vpp_vectors)
94 {
95 // Store VectorPostprocessor name and vector name from which stats will be computed
96 _compute_from_names.emplace_back(vpp_name, vec_name, vpp_object.isDistributed());
97
98 // Create the vector where the statistics will be stored
99 std::string name = vpp_name + "_" + vec_name;
100 _stat_vectors.push_back(&declareVector(name));
101 }
102 }
103}
104
105void
107{
109 for (const auto & vec : _stat_vectors)
110 vec->clear();
111}
112
113void
115{
116 TIME_SECTION("execute", 3, "Executing Statistics");
117
118 for (std::size_t i = 0; i < _compute_from_names.size(); ++i)
119 {
120 const std::string & vpp_name = std::get<0>(_compute_from_names[i]);
121 const std::string & vec_name = std::get<1>(_compute_from_names[i]);
122 const bool is_distributed = std::get<2>(_compute_from_names[i]);
123 const VectorPostprocessorValue & data =
124 getVectorPostprocessorValueByName(vpp_name, vec_name, true);
125
126 if (is_distributed || processor_id() == 0)
127 {
128 for (const auto & item : _compute_stats)
129 {
130 std::unique_ptr<StochasticTools::Calculator<std::vector<Real>, Real>> calc_ptr =
132 _stat_vectors[i]->emplace_back(calc_ptr->compute(data, is_distributed));
133
134 if (_ci_method.isValid())
135 {
136 auto ci_calc_ptr = StochasticTools::makeBootstrapCalculator<std::vector<Real>, Real>(
137 _ci_method, *this, _ci_levels, _replicates, _seed, *calc_ptr);
138 std::vector<Real> ci = ci_calc_ptr->compute(data, is_distributed);
139 _stat_vectors[i]->insert(_stat_vectors[i]->end(), ci.begin(), ci.end());
140 }
141 }
142 }
143 }
144}
145
146std::vector<Real>
147Statistics::computeLevels(const std::vector<Real> & levels_in) const
148{
149 if (levels_in.empty())
150 paramError("ci_levels",
151 "If the 'ci_method' parameter is supplied then the 'ci_levels' must also be "
152 "supplied with values in (0, 0.5].");
153
154 else if (*std::min_element(levels_in.begin(), levels_in.end()) <= 0)
155 paramError("ci_levels", "The supplied levels must be greater than zero.");
156
157 else if (*std::max_element(levels_in.begin(), levels_in.end()) > 0.5)
158 paramError("ci_levels", "The supplied levels must be less than or equal to 0.5");
159
160 std::list<Real> levels_out;
161 for (auto it = levels_in.rbegin(); it != levels_in.rend(); ++it)
162 {
163 if (*it == 0.5)
164 levels_out.push_back(*it);
165
166 else
167 {
168 levels_out.push_front(*it);
169 levels_out.push_back(1 - *it);
170 }
171 }
172 return std::vector<Real>(levels_out.begin(), levels_out.end());
173}
std::vector< Real > VectorPostprocessorValue
registerADMooseObjectDeprecated("StochasticToolsApp", Statistics, "07/01/2021 12:00")
void isValid(MooseObject *obj)
void ErrorVector unsigned int
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
static InputParameters validParams()
void suppressParameter(const std::string &name)
void addRequiredParam(const std::string &name, const std::string &doc_string)
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)
T & set(const std::string &name, bool quiet_mode=false)
const std::string & name() const
void paramError(const std::string &param, Args... args) const
virtual bool isValid() const override
Compute several metrics for supplied VPP vectors.
Definition Statistics.h:22
const std::vector< Real > _ci_levels
Confidence levels to compute (see computeLevels)
Definition Statistics.h:42
Statistics(const InputParameters &parameters)
Definition Statistics.C:64
virtual void initialSetup() override
Definition Statistics.C:83
const unsigned int _seed
Confidence level seed.
Definition Statistics.h:48
void initialize() override final
Not used; all parallel computation is wrapped in the Statistics objects.
Definition Statistics.C:106
const unsigned int _replicates
Confidence level replicates.
Definition Statistics.h:45
std::vector< Real > computeLevels(const std::vector< Real > &levels_in) const
Helper function for converting confidence levels given in (0, 0.5] into levels in (0,...
Definition Statistics.C:147
static InputParameters validParams()
Definition Statistics.C:24
virtual void execute() override
Definition Statistics.C:114
VectorPostprocessorValue & _stat_type_vector
The VPP vector that will hold the statistics identifiers.
Definition Statistics.h:51
std::vector< VectorPostprocessorValue * > _stat_vectors
The VPP vectors being computed.
Definition Statistics.h:56
const MooseEnum & _ci_method
Bootstrap Confidence Level method.
Definition Statistics.h:39
const MultiMooseEnum & _compute_stats
The selected statistics to compute.
Definition Statistics.h:36
std::vector< std::tuple< std::string, std::string, bool > > _compute_from_names
VPPs names to be computed from (Vectorpostprocessor name, vector name, is_distribute)
Definition Statistics.h:59
FEProblemBase & _fe_problem
const VectorPostprocessorValue & getVectorPostprocessorValueByName(const VectorPostprocessorName &name, const std::string &vector_name) const
bool isDistributed() const
bool containsCompleteHistory() const
const std::set< std::string > & getVectorNames() const
VectorPostprocessorValue & declareVector(const std::string &vector_name)
processor_id_type processor_id() const
std::unique_ptr< Calculator< InType, OutType > > makeCalculator(const MooseEnumItem &item, const libMesh::ParallelObject &other)
MultiMooseEnum makeCalculatorEnum()
Definition Calculators.C:16
MooseEnum makeBootstrapCalculatorEnum()