https://mooseframework.inl.gov
SobolCalculators.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 #pragma once
10 
11 #include "Calculators.h"
12 
13 #include "libmesh/dense_matrix.h"
14 
15 namespace StochasticTools
16 {
24 template <typename InType, typename OutType>
25 class SobolCalculator : public Calculator<std::vector<InType>, std::vector<OutType>>
26 {
27 public:
28  SobolCalculator(const libMesh::ParallelObject & other, const std::string & name, bool resample);
29 
30 protected:
31  virtual void initialize() override;
32  virtual void update(const InType & data) override;
33  virtual void finalize(bool is_distributed) override;
34  virtual std::vector<OutType> get() const override { return _sobol; }
35 
36 private:
38  const bool _resample;
40  DenseMatrix<OutType> _amat;
42  std::vector<OutType> _sobol;
43 };
44 
45 template <typename InType, typename OutType>
46 class SobolCalculator<std::vector<InType>, std::vector<OutType>>
47  : public Calculator<std::vector<std::vector<InType>>, std::vector<std::vector<OutType>>>
48 {
49 public:
50  SobolCalculator(const libMesh::ParallelObject & other, const std::string & name, bool resample);
51 
52 protected:
53  virtual void initialize() override;
54  virtual void update(const std::vector<InType> & data) override;
55  virtual void finalize(bool is_distributed) override;
56  virtual std::vector<std::vector<OutType>> get() const override { return _values; }
57 
58 private:
60  const bool _resample;
62  std::vector<SobolCalculator<InType, OutType>> _sobol_calcs;
64  std::vector<std::vector<OutType>> _values;
65 };
66 
67 template <typename InType, typename OutType>
69  const libMesh::ParallelObject & other, const std::string & name, bool resample)
70  : Calculator<std::vector<std::vector<InType>>, std::vector<std::vector<OutType>>>(other, name),
71  _resample(resample)
72 {
73 }
74 
75 template <typename InType, typename OutType>
76 void
77 SobolCalculator<std::vector<InType>, std::vector<OutType>>::initialize()
78 {
79  _sobol_calcs.clear();
80  _values.clear();
81 }
82 
83 template <typename InType, typename OutType>
84 void
85 SobolCalculator<std::vector<InType>, std::vector<OutType>>::update(const std::vector<InType> & data)
86 {
87  // Need to transpose data
88  std::vector<InType> data_update;
89  for (const auto & d : index_range(data))
90  for (const auto & i : index_range(data[d]))
91  {
92  if (data_update.size() <= i)
93  data_update.emplace_back(data.size());
94  data_update[i][d] = data[d][i];
95  }
96 
97  // Build calculators and update
98  for (const auto & i : index_range(data_update))
99  {
100  if (_sobol_calcs.size() <= i)
101  {
102  _sobol_calcs.emplace_back(*this, "SOBOL_" + std::to_string(i), _resample);
103  _sobol_calcs[i].initializeCalculator();
104  }
105  _sobol_calcs[i].updateCalculator(data_update[i]);
106  }
107 }
108 
109 template <typename InType, typename OutType>
110 void
111 SobolCalculator<std::vector<InType>, std::vector<OutType>>::finalize(bool is_distributed)
112 {
113  // Need to create calculators here no data was added
114  if (is_distributed)
115  {
116  auto ncalc = _sobol_calcs.size();
117  this->_communicator.max(ncalc);
118  for (const auto & i : make_range(_sobol_calcs.size(), ncalc))
119  {
120  _sobol_calcs.emplace_back(*this, "SOBOL_" + std::to_string(i), _resample);
121  _sobol_calcs[i].initializeCalculator();
122  }
123  }
124 
125  for (const auto & i : index_range(_sobol_calcs))
126  {
127  _sobol_calcs[i].finalizeCalculator(is_distributed);
128  const auto val = _sobol_calcs[i].getValue();
129 
130  for (const auto & ind : index_range(val))
131  {
132  if (_values.size() <= ind)
133  _values.emplace_back(_sobol_calcs.size());
134  _values[ind][i] = val[ind];
135  }
136  }
137 }
138 
139 } // namespace
virtual void update(const InType &data) override
std::vector< T > resample(const std::vector< T > &data, MooseRandom &generator, const std::size_t seed_index=0)
virtual void initialize() override
This function is used to reset the calculator to its initial state and prepare it for another evaluat...
const bool _resample
Set to true if the resampling matrix exists for computing second-order indices.
void initializeCalculator()
Public function that must be called before updateCalculator and finalizeCalculator.
Definition: Calculators.h:299
Enum for batch type in stochastic tools MultiApp.
std::vector< SobolCalculator< InType, OutType > > _sobol_calcs
Sobol calculator for each entry in vector data.
const std::string name
Definition: Setup.h:21
IntRange< T > make_range(T beg, T end)
virtual void finalize(bool is_distributed) override
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...
const bool _resample
Set to true if the resampling matrix exists for computing second-order indices.
DenseMatrix< OutType > _amat
Matrix containing dot products of data.
SobolCalculator(const libMesh::ParallelObject &other, const std::string &name, bool resample)
auto index_range(const T &sizable)
std::vector< OutType > _sobol
The returned sobol indices.
Calculator for computing Sobol sensitivity indices according to the paper by Saltelli (2002) https://...