https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
22class MooseEnumItem;
23
24namespace 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 */
61template <typename InType, typename OutType>
63{
64public:
65 Calculator(const libMesh::ParallelObject & other, const std::string & name)
67 {
68 }
69
70 virtual ~Calculator() = default;
75 OutType compute(const InType &, bool);
76
86 void updateCalculator(const typename InType::value_type &);
97 OutType getValue() const;
98
99 const std::string & name() const { return _name; }
100
101protected:
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
124private:
131
132 const std::string _name;
134};
135
136template <typename InType, typename OutType>
137class Mean : public Calculator<InType, OutType>
138{
139public:
140 using Calculator<InType, OutType>::Calculator;
141
142protected:
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
148 dof_id_type _count;
149 OutType _sum;
150};
151
152template <typename InType, typename OutType>
153class MeanAbsoluteValue : public Mean<InType, OutType>
154{
155public:
156 using Mean<InType, OutType>::Mean;
157
158protected:
159 virtual void update(const typename InType::value_type & val) override;
160};
161
162template <typename InType, typename OutType>
163class Ratio : public Calculator<InType, OutType>
164{
165public:
166 using Calculator<InType, OutType>::Calculator;
167
168protected:
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
178template <typename InType, typename OutType>
179class Min : public Ratio<InType, OutType>
180{
181public:
182 using Ratio<InType, OutType>::Ratio;
183
184protected:
185 virtual OutType get() const override { return this->_min; }
186};
187
188template <typename InType, typename OutType>
189class Max : public Ratio<InType, OutType>
190{
191public:
192 using Ratio<InType, OutType>::Ratio;
193
194protected:
195 virtual OutType get() const override { return this->_max; }
196};
197
198template <typename InType, typename OutType>
199class Sum : public Mean<InType, OutType>
200{
201public:
202 using Mean<InType, OutType>::Mean;
203
204protected:
205 virtual void finalize(bool is_distributed) override;
206};
207
208template <typename InType, typename OutType>
209class StdDev : public Calculator<InType, OutType>
210{
211public:
212 using Calculator<InType, OutType>::Calculator;
213
214protected:
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
220 dof_id_type _count;
221 OutType _sum;
223};
224
225template <typename InType, typename OutType>
226class StdErr : public StdDev<InType, OutType>
227{
228public:
229 using StdDev<InType, OutType>::StdDev;
230
231protected:
232 virtual void finalize(bool is_distributed) override;
233};
234
235template <typename InType, typename OutType>
236class L2Norm : public Calculator<InType, OutType>
237{
238public:
239 using Calculator<InType, OutType>::Calculator;
240
241protected:
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
250template <typename InType, typename OutType>
251class Median : public Calculator<InType, OutType>
252{
253public:
254 using Calculator<InType, OutType>::Calculator;
255
256protected:
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 */
271template <typename InType = std::vector<Real>, typename OutType = Real>
272std::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 */
279template <typename InType, typename OutType>
281{
282 static std::unique_ptr<Calculator<InType, OutType>> build(const MooseEnumItem & item,
283 const libMesh::ParallelObject & other);
284};
285
286template <typename InType, typename OutType>
287OutType
288Calculator<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
297template <typename InType, typename OutType>
298void
300{
301 initialize();
302 _state = CalculatorState::INITIALIZED;
303}
304
305template <typename InType, typename OutType>
306void
307Calculator<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
313template <typename InType, typename OutType>
314void
316{
317 if (_state != CalculatorState::INITIALIZED)
318 ::mooseError("Calculator is in wrong state.");
319 finalize(is_distributed);
320 _state = CalculatorState::FINALIZED;
321}
322
323template <typename InType, typename OutType>
324OutType
326{
327 if (_state != CalculatorState::FINALIZED)
328 ::mooseError("Calculator is in wrong state.");
329 return get();
330}
331
332// makeCalculator //////////////////////////////////////////////////////////////////////////////////
333template <typename InType, typename OutType>
334std::unique_ptr<Calculator<InType, OutType>>
336{
338}
339
340} // namespace
void updateCalculator(const typename InType::value_type &)
Public function to update calculator with a piece of data.
Calculator(const libMesh::ParallelObject &other, const std::string &name)
Definition Calculators.h:65
virtual void update(const typename InType::value_type &)=0
Updating the calculator with a piece of data.
const std::string & name() const
Definition Calculators.h:99
virtual ~Calculator()=default
virtual void initialize()=0
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
void initializeCalculator()
Public function that must be called before updateCalculator and finalizeCalculator.
virtual void finalize(bool)=0
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
void finalizeCalculator(bool)
Public function to finalize the resulting calculator value.
virtual OutType get() const =0
Returns the resulting calculator value.
OutType getValue() const
Public function to return the calculated value _state must be FINALIZED.
OutType compute(const InType &, bool)
Evaluate the calculator on the full vector of data.
const std::string _name
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
virtual OutType get() const override
Returns the resulting calculator value.
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
virtual OutType get() const override
Returns the resulting calculator value.
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition Calculators.C:55
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
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition Calculators.C:33
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 OutType get() const override
Returns the resulting calculator value.
std::vector< OutType > _storage
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
virtual OutType get() const override
Returns the resulting calculator value.
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
virtual OutType get() const override
Returns the resulting calculator value.
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
virtual void initialize() override
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...
virtual OutType get() const override
Returns the resulting calculator value.
virtual void update(const typename InType::value_type &val) override
Updating the calculator with a piece of data.
Definition Calculators.C:81
virtual OutType get() const override
Returns the resulting calculator value.
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:90
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
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
ParallelObject(const Parallel::Communicator &comm_in)
void initialize(EquationSystems &es, const std::string &system_name)
Enum for batch type in stochastic tools MultiApp.
std::unique_ptr< Calculator< InType, OutType > > makeCalculator(const MooseEnumItem &item, const libMesh::ParallelObject &other)
MultiMooseEnum makeCalculatorEnum()
Definition Calculators.C:16
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
static std::unique_ptr< Calculator< InType, OutType > > build(const MooseEnumItem &item, const libMesh::ParallelObject &other)