https://mooseframework.inl.gov
StatisticsReporter.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 "StatisticsReporter.h"
11 
12 // MOOSE includes
13 #include "MooseVariable.h"
15 #include "ThreadedNodeLoop.h"
16 
17 #include "libmesh/quadrature.h"
18 
19 #include <numeric>
20 
21 registerMooseObject("StochasticToolsApp", StatisticsReporter);
22 
25 {
27  params.addClassDescription(
28  "Compute statistical values of a given VectorPostprocessor objects and vectors.");
29 
30  params.addParam<std::vector<VectorPostprocessorName>>(
31  "vectorpostprocessors",
32  "List of VectorPostprocessor(s) to utilized for statistic computations.");
33 
34  params.addParam<std::vector<ReporterName>>(
35  "reporters", {}, "List of Reporter values to utilized for statistic computations.");
36 
39  "compute",
40  stats,
41  "The statistic(s) to compute for each of the supplied vector postprocessors.");
42 
43  // Confidence Levels
45  params.addParam<MooseEnum>(
46  "ci_method", ci, "The method to use for computing confidence level intervals.");
47 
48  params.addParam<std::vector<Real>>(
49  "ci_levels",
50  std::vector<Real>({0.1, 0.9}),
51  "A vector of confidence levels to consider, values must be in (0, 1).");
52  params.addParam<unsigned int>(
53  "ci_replicates",
54  10000,
55  "The number of replicates to use when computing confidence level intervals.");
56  params.addParam<unsigned int>("ci_seed",
57  1,
58  "The random number generator seed used for creating replicates "
59  "while computing confidence level intervals.");
60  return params;
61 }
62 
64  : GeneralReporter(parameters),
65  _compute_stats(getParam<MultiMooseEnum>("compute")),
66  _ci_method(getParam<MooseEnum>("ci_method")),
67  _ci_levels(getParam<std::vector<Real>>("ci_levels")),
68  _ci_replicates(getParam<unsigned int>("ci_replicates")),
69  _ci_seed(getParam<unsigned int>("ci_seed")),
70  _initialized(false)
71 {
72  // CI levels error checking
73  if (_ci_method.isValid())
74  {
75  if (_ci_levels.empty())
76  paramError("ci_levels",
77  "If the 'ci_method' parameter is supplied then the 'ci_levels' must also be "
78  "supplied with values in (0, 1).");
79 
80  else if (*std::min_element(_ci_levels.begin(), _ci_levels.end()) <= 0)
81  paramError("ci_levels", "The supplied levels must be greater than zero.");
82 
83  else if (*std::max_element(_ci_levels.begin(), _ci_levels.end()) >= 1)
84  paramError("ci_levels", "The supplied levels must be less than 1.0");
85  }
86 
87  if ((!isParamValid("reporters") && !isParamValid("vectorpostprocessors")) ||
88  (getParam<std::vector<ReporterName>>("reporters").empty() &&
89  getParam<std::vector<VectorPostprocessorName>>("vectorpostprocessors").empty()))
90  mooseError(
91  "The 'vectorpostprocessors' and/or 'reporters' parameters must be defined and non-empty.");
92 }
93 
94 void
96 {
97  if (_initialized)
98  return;
99 
100  // Stats for Reporters
101  if (isParamValid("reporters"))
102  {
103  std::vector<std::string> unsupported_types;
104  const auto & reporter_names = getParam<std::vector<ReporterName>>("reporters");
105  for (const auto & r_name : reporter_names)
106  {
107  if (hasReporterValueByName<std::vector<Real>>(r_name))
108  declareValueHelper<std::vector<Real>, Real>(r_name);
109  else if (hasReporterValueByName<std::vector<int>>(r_name))
110  declareValueHelper<std::vector<int>, Real>(r_name);
111  else if (hasReporterValueByName<std::vector<std::vector<Real>>>(r_name))
112  declareValueHelper<std::vector<std::vector<Real>>, std::vector<Real>>(r_name);
113  else
114  unsupported_types.emplace_back(r_name.getCombinedName());
115  }
116 
117  if (!unsupported_types.empty())
118  paramError("reporters",
119  "The following reporter value(s) do not have a type supported by the "
120  "StatisticsReporter:\n",
121  MooseUtils::join(unsupported_types, ", "));
122  }
123 
124  // Stats for VPP
125  if (isParamValid("vectorpostprocessors"))
126  {
127  const auto & vpp_names = getParam<std::vector<VectorPostprocessorName>>("vectorpostprocessors");
128  for (const auto & vpp_name : vpp_names)
129  {
130  const VectorPostprocessor & vpp_object =
132  const std::set<std::string> & vpp_vectors = vpp_object.getVectorNames();
133  for (const auto & vec_name : vpp_vectors)
134  {
135  ReporterName r_name(vpp_name, vec_name);
136  declareValueHelper<std::vector<Real>, Real>(r_name);
137  }
138  }
139  }
140 
141  _initialized = true;
142 }
143 
144 void
145 StatisticsReporter::store(nlohmann::json & json) const
146 {
147  Reporter::store(json);
148  if (_ci_method.isValid())
149  json["confidence_intervals"] = {{"method", _ci_method},
150  {"levels", _ci_levels},
151  {"replicates", _ci_replicates},
152  {"seed", _ci_seed}};
153 }
154 
155 template <typename InType, typename OutType>
156 void
158 {
159  const auto & mode = _fe_problem.getReporterData().getReporterMode(r_name);
160  const auto & data = getReporterValueByName<InType>(r_name);
161  for (const auto & item : _compute_stats)
162  {
163  const std::string s_name =
164  r_name.getObjectName() + "_" + r_name.getValueName() + "_" + item.name();
165  if (_ci_method.isValid())
166  declareValueByName<std::pair<OutType, std::vector<OutType>>,
169  data,
170  mode,
171  item,
172  _ci_method,
173  _ci_levels,
175  _ci_seed);
176  else
177  declareValueByName<std::pair<OutType, std::vector<OutType>>,
179  s_name, REPORTER_MODE_ROOT, data, mode, item);
180  }
181 }
182 
183 template void
184 StatisticsReporter::declareValueHelper<std::vector<Real>, Real>(const ReporterName & r_name);
185 template void
186 StatisticsReporter::declareValueHelper<std::vector<int>, Real>(const ReporterName & r_name);
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
T & declareValueByName(const ReporterValueName &value_name, Args &&... args)
StatisticsReporter(const InputParameters &parameters)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const ReporterMode REPORTER_MODE_ROOT
ReporterContext that utilizes a Calculator object to compute its value and confidence levels...
const unsigned int & _ci_seed
Compute several metrics for supplied data.
void declareValueHelper(const ReporterName &r_name)
Helper for adding statistic reporter values.
virtual void store(nlohmann::json &json) const override
static InputParameters validParams()
const std::vector< Real > & _ci_levels
MultiMooseEnum makeCalculatorEnum()
Definition: Calculators.C:16
MooseEnum makeBootstrapCalculatorEnum()
const unsigned int & _ci_replicates
void addRequiredParam(const std::string &name, const std::string &doc_string)
virtual void store(nlohmann::json &json) const
bool isParamValid(const std::string &name) const
const ReporterData & getReporterData() const
bool _initialized
Whether or not initialize() has been called for reporter value declaration.
registerMooseObject("StochasticToolsApp", StatisticsReporter)
const T & getParam(const std::string &name) const
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
static InputParameters validParams()
void paramError(const std::string &param, Args... args) const
const std::string & getObjectName() const
const MooseEnum & _ci_method
const ReporterProducerEnum & getReporterMode(const ReporterName &reporter_name) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void initialize() final
This is where the reporter values are declared Note: unfortunetly this cannot be in the constructor s...
FEProblemBase & _fe_problem
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
const std::string & getValueName() const
const MultiMooseEnum & _compute_stats
virtual bool isValid() const override
const std::set< std::string > & getVectorNames() const
void ErrorVector unsigned int
bool hasReporterValueByName(const ReporterName &reporter_name) const