Line data Source code
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 "BootstrapCalculators.h"
13 :
14 : namespace StochasticTools
15 : {
16 :
17 : template <typename InType, typename OutType, template <typename, typename> class CalcType>
18 : class VectorCalculator : public Calculator<std::vector<InType>, std::vector<OutType>>
19 : {
20 : public:
21 : using Calculator<std::vector<InType>, std::vector<OutType>>::Calculator;
22 :
23 : protected:
24 : virtual void initialize() override;
25 : virtual void update(const InType & data) override;
26 : virtual void finalize(bool is_distributed) override;
27 2460960 : virtual std::vector<OutType> get() const override { return _values; }
28 :
29 : private:
30 : std::vector<CalcType<InType, OutType>> _calcs;
31 : std::vector<OutType> _values;
32 : };
33 :
34 : template <typename InType, typename OutType, template <typename, typename> class CalcType>
35 : void
36 2462160 : VectorCalculator<InType, OutType, CalcType>::initialize()
37 : {
38 2462160 : _calcs.clear();
39 : _values.clear();
40 2462160 : }
41 :
42 : template <typename InType, typename OutType, template <typename, typename> class CalcType>
43 : void
44 97813466 : VectorCalculator<InType, OutType, CalcType>::update(const InType & data)
45 : {
46 1506160128 : for (const auto & i : index_range(data))
47 : {
48 1408346662 : if (i >= _calcs.size())
49 : {
50 32528004 : _calcs.emplace_back(*this, this->name() + "_" + std::to_string(i));
51 16264002 : _calcs.back().initializeCalculator();
52 : }
53 1408346662 : _calcs[i].updateCalculator(data[i]);
54 : }
55 97813466 : }
56 :
57 : template <typename InType, typename OutType, template <typename, typename> class CalcType>
58 : void
59 2462160 : VectorCalculator<InType, OutType, CalcType>::finalize(bool is_distributed)
60 : {
61 : // Need to make calculator objects if some data added
62 2462160 : if (is_distributed)
63 : {
64 1220016 : auto ncalc = _calcs.size();
65 1220016 : this->_communicator.max(ncalc);
66 1220016 : for (const auto & i : make_range(_calcs.size(), ncalc))
67 : {
68 0 : _calcs.emplace_back(*this, this->name() + "_" + std::to_string(i));
69 0 : _calcs.back().initializeCalculator();
70 : }
71 : }
72 :
73 2462160 : _values.reserve(_calcs.size());
74 18726162 : for (auto & cc : _calcs)
75 : {
76 16264002 : cc.finalizeCalculator(is_distributed);
77 16264002 : _values.push_back(cc.getValue());
78 : }
79 2462160 : }
80 :
81 : template <typename InType, typename OutType>
82 : class Percentile<std::vector<InType>, std::vector<OutType>>
83 : : public BootstrapCalculator<std::vector<InType>, std::vector<OutType>>
84 : {
85 : public:
86 339 : 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 : */
94 : template <typename InType, typename OutType>
95 : class BiasCorrectedAccelerated<std::vector<InType>, std::vector<OutType>>
96 : : public BootstrapCalculator<std::vector<InType>, std::vector<OutType>>
97 : {
98 : public:
99 34 : 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 :
103 : private:
104 : std::vector<OutType> acceleration(const std::vector<InType> &, const bool);
105 : };
106 :
107 : template <typename InType, typename OutType>
108 : struct 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 :
114 : template <typename InType, typename OutType>
115 : struct BootstrapCalculatorBuilder<std::vector<InType>, std::vector<OutType>>
116 : {
117 : static std::unique_ptr<BootstrapCalculator<std::vector<InType>, std::vector<OutType>>>
118 : build(const MooseEnum &,
119 : const libMesh::ParallelObject &,
120 : const std::vector<Real> &,
121 : unsigned int,
122 : unsigned int,
123 : StochasticTools::Calculator<std::vector<InType>, std::vector<OutType>> &);
124 : };
125 :
126 : } // namespace
|