https://mooseframework.inl.gov
Calculators.h
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 #pragma once
11 
12 #include "MooseTypes.h"
13 #include "MultiMooseEnum.h"
14 #include "MathUtils.h"
15 
16 #include "libmesh/parallel.h"
17 
18 #include <vector>
19 #include <memory>
20 #include <numeric>
21 
22 class MooseEnumItem;
23 
24 namespace StochasticTools
25 {
26 
27 /*
28  * Free function that returns the available statistics available to the Statistics object(s)
29  */
31 
32 /* Base class for computing statistics (e.g., mean, min) for use with Statistics object
33  *
34  * The purpose of these objects are to provide an API for computing statistics in serial or parallel
35  * without any state. This allows future statistics to be quickly added and for each statistic
36  * to be used with the BootstrapCalculator for computing bootstrap statistics such as confidence
37  * level intervals.
38  *
39  * The calculators are evaluating using the following sequence:
40  * \code
41  * auto calculator = makeCalculator<InType, OutType>(*this, type);
42  * calculator->initializeCalculator();
43  * for (const typename InType::value_type & val : data)
44  * calculator->updateCalculator(data);
45  * calculator->finalizeCalculator(is_distributed);
46  * OutType stat = calculator->getValue();
47  * \endcode
48  * The base Calculator class does this automatically with an input vector of data:
49  * \code
50  * auto calculator = makeCalculator<InType, OutType>(*this, type);
51  * OutType stat = calculator->compute(data, is_distributed);
52  * \endcode
53  * The base class has state checks to make sure these functions are called
54  * in the correct order.
55  *
56  * To create new Calculator objects first create the Calculator class and then update the
57  * the initialize, update, finalize, and get virtual functions.
58  *
59  * Explicit instantiations are generated in the C file.
60  */
61 template <typename InType, typename OutType>
62 class Calculator : public libMesh::ParallelObject
63 {
64 public:
65  Calculator(const libMesh::ParallelObject & other, const std::string & name)
67  {
68  }
69 
70  virtual ~Calculator() = default;
75  OutType compute(const InType &, bool);
76 
81  void initializeCalculator();
86  void updateCalculator(const typename InType::value_type &);
92  void finalizeCalculator(bool);
97  OutType getValue() const;
98 
99  const std::string & name() const { return _name; }
100 
101 protected:
106  virtual void initialize() = 0;
111  virtual void update(const typename InType::value_type &) = 0;
117  virtual void finalize(bool) = 0;
122  virtual OutType get() const = 0;
123 
124 private:
126  {
130  };
131 
132  const std::string _name;
134 };
135 
136 template <typename InType, typename OutType>
137 class Mean : public Calculator<InType, OutType>
138 {
139 public:
141 
142 protected:
143  virtual void initialize() override;
144  virtual void update(const typename InType::value_type & val) override;
145  virtual void finalize(bool is_distributed) override;
146  virtual OutType get() const override { return _sum; }
147 
149  OutType _sum;
150 };
151 
152 template <typename InType, typename OutType>
153 class MeanAbsoluteValue : public Mean<InType, OutType>
154 {
155 public:
157 
158 protected:
159  virtual void update(const typename InType::value_type & val) override;
160 };
161 
162 template <typename InType, typename OutType>
163 class Ratio : public Calculator<InType, OutType>
164 {
165 public:
167 
168 protected:
169  virtual void initialize() override;
170  virtual void update(const typename InType::value_type & val) override;
171  virtual void finalize(bool is_distributed) override;
172  virtual OutType get() const override { return _max / _min; }
173 
174  OutType _min;
175  OutType _max;
176 };
177 
178 template <typename InType, typename OutType>
179 class Min : public Ratio<InType, OutType>
180 {
181 public:
183 
184 protected:
185  virtual OutType get() const override { return this->_min; }
186 };
187 
188 template <typename InType, typename OutType>
189 class Max : public Ratio<InType, OutType>
190 {
191 public:
193 
194 protected:
195  virtual OutType get() const override { return this->_max; }
196 };
197 
198 template <typename InType, typename OutType>
199 class Sum : public Mean<InType, OutType>
200 {
201 public:
203 
204 protected:
205  virtual void finalize(bool is_distributed) override;
206 };
207 
208 template <typename InType, typename OutType>
209 class StdDev : public Calculator<InType, OutType>
210 {
211 public:
213 
214 protected:
215  virtual void initialize() override;
216  virtual void update(const typename InType::value_type & val) override;
217  virtual void finalize(bool is_distributed) override;
218  virtual OutType get() const override { return _sum_of_square; }
219 
221  OutType _sum;
222  OutType _sum_of_square;
223 };
224 
225 template <typename InType, typename OutType>
226 class StdErr : public StdDev<InType, OutType>
227 {
228 public:
230 
231 protected:
232  virtual void finalize(bool is_distributed) override;
233 };
234 
235 template <typename InType, typename OutType>
236 class L2Norm : public Calculator<InType, OutType>
237 {
238 public:
240 
241 protected:
242  virtual void initialize() override;
243  virtual void update(const typename InType::value_type & val) override;
244  virtual void finalize(bool is_distributed) override;
245  virtual OutType get() const override { return _l2_norm; }
246 
247  OutType _l2_norm;
248 };
249 
250 template <typename InType, typename OutType>
251 class Median : public Calculator<InType, OutType>
252 {
253 public:
255 
256 protected:
257  virtual void initialize() override;
258  virtual void update(const typename InType::value_type & val) override;
259  virtual void finalize(bool is_distributed) override;
260  virtual OutType get() const override { return _median; }
261 
262  std::vector<OutType> _storage;
263  OutType _median;
264 };
265 
266 /*
267  * Free function for building a const Calculator object for use by Statistics object.
268  *
269  * Explicit instantiations in C file.
270  */
271 template <typename InType = std::vector<Real>, typename OutType = Real>
272 std::unique_ptr<Calculator<InType, OutType>> makeCalculator(const MooseEnumItem & item,
273  const libMesh::ParallelObject & other);
274 
275 /*
276  * Simple struct that makeCalculator wraps around, this is so building calculators
277  * can be partially specialized.
278  */
279 template <typename InType, typename OutType>
281 {
282  static std::unique_ptr<Calculator<InType, OutType>> build(const MooseEnumItem & item,
283  const libMesh::ParallelObject & other);
284 };
285 
286 template <typename InType, typename OutType>
287 OutType
288 Calculator<InType, OutType>::compute(const InType & data, bool is_distributed)
289 {
290  initializeCalculator();
291  for (const auto & val : data)
292  updateCalculator(val);
293  finalizeCalculator(is_distributed);
294  return getValue();
295 }
296 
297 template <typename InType, typename OutType>
298 void
300 {
301  initialize();
302  _state = CalculatorState::INITIALIZED;
303 }
304 
305 template <typename InType, typename OutType>
306 void
307 Calculator<InType, OutType>::updateCalculator(const typename InType::value_type & val)
308 {
309  mooseAssert(_state == CalculatorState::INITIALIZED, "Calculator is in wrong state.");
310  update(val);
311 }
312 
313 template <typename InType, typename OutType>
314 void
316 {
317  if (_state != CalculatorState::INITIALIZED)
318  ::mooseError("Calculator is in wrong state.");
319  finalize(is_distributed);
320  _state = CalculatorState::FINALIZED;
321 }
322 
323 template <typename InType, typename OutType>
324 OutType
326 {
327  if (_state != CalculatorState::FINALIZED)
328  ::mooseError("Calculator is in wrong state.");
329  return get();
330 }
331 
332 // makeCalculator //////////////////////////////////////////////////////////////////////////////////
333 template <typename InType, typename OutType>
334 std::unique_ptr<Calculator<InType, OutType>>
336 {
337  return CalculatorBuilder<InType, OutType>::build(item, other);
338 }
339 
340 } // namespace
ParallelObject(const Parallel::Communicator &comm_in)
const std::string _name
Definition: Calculators.h:132
static std::unique_ptr< Calculator< InType, OutType > > build(const MooseEnumItem &item, const libMesh::ParallelObject &other)
Definition: Calculators.C:293
void updateCalculator(const typename InType::value_type &)
Public function to update calculator with a piece of data.
Definition: Calculators.h:307
virtual void initialize()=0
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:108
OutType compute(const InType &, bool)
Evaluate the calculator on the full vector of data.
Definition: Calculators.h:288
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
Definition: Calculators.C:72
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:135
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:63
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:185
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
MultiMooseEnum makeCalculatorEnum()
Definition: Calculators.C:16
virtual void finalize(bool)=0
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
virtual void update(const typename InType::value_type &)=0
Updating the calculator with a piece of data.
void initialize(EquationSystems &es, const std::string &system_name)
std::unique_ptr< Calculator< InType, OutType > > makeCalculator(const MooseEnumItem &item, const libMesh::ParallelObject &other)
Definition: Calculators.h:335
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition: Calculators.C:55
void initializeCalculator()
Public function that must be called before updateCalculator and finalizeCalculator.
Definition: Calculators.h:299
Enum for batch type in stochastic tools MultiApp.
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:161
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition: Calculators.C:33
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition: Calculators.C:154
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
Definition: Calculators.C:25
Calculator(const libMesh::ParallelObject &other, const std::string &name)
Definition: Calculators.h:65
std::vector< OutType > _storage
Definition: Calculators.h:262
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition: Calculators.C:178
virtual ~Calculator()=default
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
Definition: Calculators.C:117
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
Definition: Calculators.C:147
OutType getValue() const
Public function to return the calculated value _state must be FINALIZED.
Definition: Calculators.h:325
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition: Calculators.C:81
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:41
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
Definition: Calculators.C:171
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Definition: Calculators.C:90
const std::string & name() const
Definition: Calculators.h:99
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition: Calculators.C:125
void finalizeCalculator(bool)
Public function to finalize the resulting calculator value.
Definition: Calculators.h:315
uint8_t dof_id_type