https://mooseframework.inl.gov
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 
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  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
37 {
38  _calcs.clear();
39  _values.clear();
40 }
41 
42 template <typename InType, typename OutType, template <typename, typename> class CalcType>
43 void
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 
57 template <typename InType, typename OutType, template <typename, typename> class CalcType>
58 void
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 
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:
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:
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
static std::unique_ptr< Calculator< InType, OutType > > build(const MooseEnumItem &item, const libMesh::ParallelObject &other)
Definition: Calculators.C:293
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
BootstrapCalculator(const libMesh::ParallelObject &other, const std::string &name, const std::vector< Real > &levels, unsigned int replicates, unsigned int seed, StochasticTools::Calculator< InType, OutType > &calc)
Enum for batch type in stochastic tools MultiApp.
const std::string name
Definition: Setup.h:20
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
Base class for computing bootstrap confidence level intervals.
Calculator(const libMesh::ParallelObject &other, const std::string &name)
Definition: Calculators.h:65
OutType acceleration(const InType &, const bool)
IntRange< T > make_range(T beg, T end)
virtual std::vector< OutType > compute(const InType &, const bool) override
virtual std::vector< OutType > compute(const InType &, const bool) override
std::vector< CalcType< InType, OutType > > _calcs
auto index_range(const T &sizable)
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 > &)
virtual void update(const InType &data) override