https://mooseframework.inl.gov
Loading...
Searching...
No Matches
VectorCalculators.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
13
14namespace StochasticTools
15{
16
17template <typename InType, typename OutType, template <typename, typename> class CalcType>
18class VectorCalculator : public Calculator<std::vector<InType>, std::vector<OutType>>
19{
20public:
21 using Calculator<std::vector<InType>, std::vector<OutType>>::Calculator;
22
23protected:
24 virtual void initialize() override;
25 virtual void update(const InType & data) override;
26 virtual void finalize(bool is_distributed) override;
27 virtual std::vector<OutType> get() const override { return _values; }
28
29private:
30 std::vector<CalcType<InType, OutType>> _calcs;
31 std::vector<OutType> _values;
32};
33
34template <typename InType, typename OutType, template <typename, typename> class CalcType>
35void
37{
38 _calcs.clear();
39 _values.clear();
40}
41
42template <typename InType, typename OutType, template <typename, typename> class CalcType>
43void
45{
46 for (const auto & i : index_range(data))
47 {
48 if (i >= _calcs.size())
49 {
50 _calcs.emplace_back(*this, this->name() + "_" + std::to_string(i));
51 _calcs.back().initializeCalculator();
52 }
53 _calcs[i].updateCalculator(data[i]);
54 }
55}
56
57template <typename InType, typename OutType, template <typename, typename> class CalcType>
58void
60{
61 // Need to make calculator objects if some data added
62 if (is_distributed)
63 {
64 auto ncalc = _calcs.size();
65 this->_communicator.max(ncalc);
66 for (const auto & i : make_range(_calcs.size(), ncalc))
67 {
68 _calcs.emplace_back(*this, this->name() + "_" + std::to_string(i));
69 _calcs.back().initializeCalculator();
70 }
71 }
72
73 _values.reserve(_calcs.size());
74 for (auto & cc : _calcs)
75 {
76 cc.finalizeCalculator(is_distributed);
77 _values.push_back(cc.getValue());
78 }
79}
80
81template <typename InType, typename OutType>
82class Percentile<std::vector<InType>, std::vector<OutType>>
83 : public BootstrapCalculator<std::vector<InType>, std::vector<OutType>>
84{
85public:
86 using BootstrapCalculator<std::vector<InType>, std::vector<OutType>>::BootstrapCalculator;
87 virtual std::vector<std::vector<OutType>> compute(const std::vector<InType> &,
88 const bool) override;
89};
90
91/*
92 * Implement BCa method of Efron and Tibshirani (2003), Chapter 14.
93 */
94template <typename InType, typename OutType>
95class BiasCorrectedAccelerated<std::vector<InType>, std::vector<OutType>>
96 : public BootstrapCalculator<std::vector<InType>, std::vector<OutType>>
97{
98public:
99 using BootstrapCalculator<std::vector<InType>, std::vector<OutType>>::BootstrapCalculator;
100 virtual std::vector<std::vector<OutType>> compute(const std::vector<InType> &,
101 const bool) override;
102
103private:
104 std::vector<OutType> acceleration(const std::vector<InType> &, const bool);
105};
106
107template <typename InType, typename OutType>
108struct CalculatorBuilder<std::vector<InType>, std::vector<OutType>>
109{
110 static std::unique_ptr<Calculator<std::vector<InType>, std::vector<OutType>>>
111 build(const MooseEnumItem & item, const libMesh::ParallelObject & other);
112};
113
114template <typename InType, typename OutType>
115struct BootstrapCalculatorBuilder<std::vector<InType>, std::vector<OutType>>
116{
117 static std::unique_ptr<BootstrapCalculator<std::vector<InType>, std::vector<OutType>>>
118 build(const MooseEnum &,
120 const std::vector<Real> &,
121 unsigned int,
122 unsigned int,
123 StochasticTools::Calculator<std::vector<InType>, std::vector<OutType>> &);
124};
125
126} // namespace
const std::string name
Definition Setup.h:21
OutType acceleration(const InType &, const bool)
virtual std::vector< OutType > compute(const InType &, const bool) override
Base class for computing bootstrap confidence level intervals.
virtual std::vector< OutType > compute(const InType &, const bool) override
virtual std::vector< 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...
std::vector< CalcType< InType, OutType > > _calcs
virtual void update(const InType &data) override
Enum for batch type in stochastic tools MultiApp.
static std::unique_ptr< BootstrapCalculator< InType, OutType > > build(const MooseEnum &, const libMesh::ParallelObject &, const std::vector< Real > &, unsigned int, unsigned int, StochasticTools::Calculator< InType, OutType > &)
static std::unique_ptr< Calculator< InType, OutType > > build(const MooseEnumItem &item, const libMesh::ParallelObject &other)