https://mooseframework.inl.gov
Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
StochasticTools::SobolCalculator< InType, OutType > Class Template Referenceabstract

Calculator for computing Sobol sensitivity indices according to the paper by Saltelli (2002) https://doi.org/10.1016/S0010-4655(02)00280-1. More...

#include <SobolCalculators.h>

Inheritance diagram for StochasticTools::SobolCalculator< InType, OutType >:
[legend]

Public Member Functions

 SobolCalculator (const libMesh::ParallelObject &other, const std::string &name, bool resample)
 
std::vector< OutType > compute (const std::vector< InType > &, bool)
 Evaluate the calculator on the full vector of data. More...
 
void initializeCalculator ()
 Public function that must be called before updateCalculator and finalizeCalculator. More...
 
void updateCalculator (const typename std::vector< InType > ::value_type &)
 Public function to update calculator with a piece of data. More...
 
void finalizeCalculator (bool)
 Public function to finalize the resulting calculator value. More...
 
std::vector< OutType > getValue () const
 Public function to return the calculated value _state must be FINALIZED. More...
 
const std::string & name () const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Protected Member Functions

virtual void initialize () override
 This function is used to reset the calculator to its initial state and prepare it for another evaluation. More...
 
virtual void update (const InType &data) override
 
virtual void finalize (bool is_distributed) override
 This is used to compute the resulting calculator value by performing necessary arithmetic and parallel communication. More...
 
virtual std::vector< OutType > get () const override
 Returns the resulting calculator value. More...
 
virtual void update (const typename std::vector< InType > ::value_type &)=0
 Updating the calculator with a piece of data. More...
 

Protected Attributes

const Parallel::Communicator & _communicator
 

Private Attributes

const bool _resample
 Set to true if the resampling matrix exists for computing second-order indices. More...
 
DenseMatrix< OutType > _amat
 Matrix containing dot products of data. More...
 
std::vector< OutType > _sobol
 The returned sobol indices. More...
 

Detailed Description

template<typename InType, typename OutType>
class StochasticTools::SobolCalculator< InType, OutType >

Calculator for computing Sobol sensitivity indices according to the paper by Saltelli (2002) https://doi.org/10.1016/S0010-4655(02)00280-1.

The data provided is stacked vectors provided by the SobolSampler. Example use of this object is also available in the stochastic_tools unit testing.

Definition at line 24 of file SobolCalculators.h.

Constructor & Destructor Documentation

◆ SobolCalculator()

template<typename InType , typename OutType >
StochasticTools::SobolCalculator< InType, OutType >::SobolCalculator ( const libMesh::ParallelObject other,
const std::string &  name,
bool  resample 
)

Definition at line 14 of file SobolCalculators.C.

17  : Calculator<std::vector<InType>, std::vector<OutType>>(other, name), _resample(resample)
18 {
19 }
const bool _resample
Set to true if the resampling matrix exists for computing second-order indices.

Member Function Documentation

◆ compute()

std::vector< OutType > StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::compute ( const std::vector< InType > &  data,
bool  is_distributed 
)
inherited

Evaluate the calculator on the full vector of data.

This is basically a convenient wrapper around initializeCalculator, updateCalculator, finalizeCalculator, and getvalue.

Definition at line 288 of file Calculators.h.

Referenced by SobolStatistics::execute(), and TEST().

289 {
291  for (const auto & val : data)
292  updateCalculator(val);
293  finalizeCalculator(is_distributed);
294  return getValue();
295 }
void updateCalculator(const typename std::vector< InType > ::value_type &)
Public function to update calculator with a piece of data.
Definition: Calculators.h:307
void initializeCalculator()
Public function that must be called before updateCalculator and finalizeCalculator.
Definition: Calculators.h:299
std::vector< OutType > getValue() const
Public function to return the calculated value _state must be FINALIZED.
Definition: Calculators.h:325
void finalizeCalculator(bool)
Public function to finalize the resulting calculator value.
Definition: Calculators.h:315

◆ finalize()

template<typename InType , typename OutType >
void StochasticTools::SobolCalculator< InType, OutType >::finalize ( bool  )
overrideprotectedvirtual

This is used to compute the resulting calculator value by performing necessary arithmetic and parallel communication.

This only called once after all the input data is entered through update.

Implements StochasticTools::Calculator< std::vector< InType >, std::vector< OutType > >.

Definition at line 47 of file SobolCalculators.C.

48 {
49  // The size of the A matrix
50  std::size_t s = _amat.n();
51 
52  if (is_distributed)
53  {
54  this->_communicator.max(s);
55  if (_amat.n() == 0)
56  _amat.resize(s, s);
57  mooseAssert(_amat.n() == s, "Size of data updating SobolCalculator has changed.");
58  this->_communicator.sum(_amat.get_values());
59  }
60 
61  // Number of independent parameters
62  const std::size_t n = (s - 2) / (_resample ? 2 : 1);
63 
64  // The data to output
65  DenseMatrix<OutType> S(n, n);
66  std::vector<OutType> ST(n);
67 
68  // First order
69  {
70  auto E = _amat(0, s - 1);
71  auto V = _amat(s - 1, s - 1) - E;
72  for (std::size_t i = 0; i < n; ++i)
73  {
74  auto U0 = _amat(i + 1, s - 1);
75  if (_resample)
76  {
77  auto U1 = _amat(0, i + n + 1);
78  S(i, i) = (((U0 + U1) / 2) - E) / V;
79  }
80  else
81  S(i, i) = (U0 - E) / V;
82  }
83  }
84 
85  // Total-effect
86  {
87  auto E = _amat(0, s - 1);
88  auto V = _amat(0, 0) - E;
89  for (std::size_t i = 0; i < n; ++i)
90  {
91  auto U0 = _amat(0, i + 1);
92  if (_resample)
93  {
94  auto U1 = _amat(i + n + 1, s - 1);
95  ST[i] = 1 - (((U0 + U1) / 2) - E) / V;
96  }
97  else
98  ST[i] = 1 - (U0 - E) / V;
99  }
100  }
101 
102  // Second-order
103  if (_resample)
104  {
105  for (std::size_t i = 0; i < n; ++i)
106  {
107  auto E = _amat(i + 1, i + n + 1);
108  for (std::size_t j = 0; j < i; ++j)
109  {
110  auto V = _amat(j + n + 1, j + n + 1) - E;
111  auto U0 = _amat(i + 1, j + n + 1);
112  auto U1 = _amat(j + 1, i + n + 1);
113  auto Sc = (((U0 + U1) / 2) - E) / V;
114  auto S2 = Sc - S(i, i) - S(j, j);
115  S(j, i) = S2;
116  }
117  }
118  }
119 
120  // Output the data
121  _sobol.clear();
122  if (_resample)
123  _sobol.reserve(n * (n + 3) / 2);
124  else
125  _sobol.reserve(2 * n);
126 
127  // First-order
128  for (std::size_t i = 0; i < n; ++i)
129  _sobol.push_back(S(i, i));
130 
131  // Total-effect
132  for (std::size_t i = 0; i < n; ++i)
133  _sobol.push_back(ST[i]);
134 
135  // Second-order
136  if (_resample)
137  {
138  for (std::size_t i = 0; i < n; ++i)
139  for (std::size_t j = i + 1; j < n; ++j)
140  _sobol.push_back(S(i, j));
141  }
142 }
const Parallel::Communicator & _communicator
static const std::string S
Definition: NS.h:163
void max(const T &r, T &o, Request &req) const
const bool _resample
Set to true if the resampling matrix exists for computing second-order indices.
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
DenseMatrix< OutType > _amat
Matrix containing dot products of data.
std::vector< OutType > _sobol
The returned sobol indices.

◆ finalizeCalculator()

void StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::finalizeCalculator ( bool  is_distributed)
inherited

Public function to finalize the resulting calculator value.

_state must be INITLIALIZED Sets _state to FINALIZED

Definition at line 315 of file Calculators.h.

316 {
317  if (_state != CalculatorState::INITIALIZED)
318  ::mooseError("Calculator is in wrong state.");
319  finalize(is_distributed);
320  _state = CalculatorState::FINALIZED;
321 }
virtual void finalize(bool)=0
This is used to compute the resulting calculator value by performing necessary arithmetic and paralle...

◆ get()

template<typename InType, typename OutType>
virtual std::vector<OutType> StochasticTools::SobolCalculator< InType, OutType >::get ( ) const
inlineoverrideprotectedvirtual

Returns the resulting calculator value.

It is important to not modify member data here so the calculator can retain its state.

Implements StochasticTools::Calculator< std::vector< InType >, std::vector< OutType > >.

Definition at line 33 of file SobolCalculators.h.

33 { return _sobol; }
std::vector< OutType > _sobol
The returned sobol indices.

◆ getValue()

std::vector< OutType > StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::getValue ( ) const
inherited

Public function to return the calculated value _state must be FINALIZED.

Definition at line 325 of file Calculators.h.

326 {
327  if (_state != CalculatorState::FINALIZED)
328  ::mooseError("Calculator is in wrong state.");
329  return get();
330 }

◆ initialize()

template<typename InType , typename OutType >
void StochasticTools::SobolCalculator< InType, OutType >::initialize ( )
overrideprotectedvirtual

This function is used to reset the calculator to its initial state and prepare it for another evaluation.

This usually involves clearing class members.

Implements StochasticTools::Calculator< std::vector< InType >, std::vector< OutType > >.

Definition at line 23 of file SobolCalculators.C.

24 {
25  _amat.resize(0, 0);
26 }
DenseMatrix< OutType > _amat
Matrix containing dot products of data.

◆ initializeCalculator()

void StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::initializeCalculator ( )
inherited

Public function that must be called before updateCalculator and finalizeCalculator.

Sets _state to INITIALIZED

Definition at line 299 of file Calculators.h.

300 {
301  initialize();
302  _state = CalculatorState::INITIALIZED;
303 }
virtual void initialize()=0
This function is used to reset the calculator to its initial state and prepare it for another evaluat...

◆ name()

const std::string& StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::name ( ) const
inlineinherited

Definition at line 99 of file Calculators.h.

◆ update() [1/2]

template<typename InType , typename OutType >
void StochasticTools::SobolCalculator< InType, OutType >::update ( const InType &  data)
overrideprotectedvirtual

Definition at line 30 of file SobolCalculators.C.

31 {
32  if (_amat.n() == 0)
33  {
34  _amat.resize(data.size(), data.size());
35  if (_amat.n() < 2 || (_resample && _amat.n() % 2 != 0))
36  mooseError("Size of input data is inconsistent with Sobol resampling scheme.");
37  }
38  mooseAssert(_amat.n() == data.size(), "Size of data updating SobolCalculator has changed.");
39 
40  for (const auto & c : index_range(data))
41  for (const auto & r : make_range(c + 1))
42  _amat(r, c) += data[r] * data[c];
43 }
void mooseError(Args &&... args)
IntRange< T > make_range(T beg, T end)
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.
auto index_range(const T &sizable)

◆ update() [2/2]

virtual void StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::update ( const typename std::vector< InType > ::value_type &  )
protectedpure virtualinherited

Updating the calculator with a piece of data.

Sometimes some clever arithmetic is required to avoid storing data.

◆ updateCalculator()

void StochasticTools::Calculator< std::vector< InType > , std::vector< OutType > >::updateCalculator ( const typename std::vector< InType > ::value_type &  val)
inherited

Public function to update calculator with a piece of data.

_state mush be INITIALIZED

Definition at line 307 of file Calculators.h.

308 {
309  mooseAssert(_state == CalculatorState::INITIALIZED, "Calculator is in wrong state.");
310  update(val);
311 }
virtual void update(const typename std::vector< InType > ::value_type &)=0
Updating the calculator with a piece of data.

Member Data Documentation

◆ _amat

template<typename InType, typename OutType>
DenseMatrix<OutType> StochasticTools::SobolCalculator< InType, OutType >::_amat
private

Matrix containing dot products of data.

Definition at line 39 of file SobolCalculators.h.

◆ _resample

template<typename InType, typename OutType>
const bool StochasticTools::SobolCalculator< InType, OutType >::_resample
private

Set to true if the resampling matrix exists for computing second-order indices.

Definition at line 37 of file SobolCalculators.h.

◆ _sobol

template<typename InType, typename OutType>
std::vector<OutType> StochasticTools::SobolCalculator< InType, OutType >::_sobol
private

The returned sobol indices.

Definition at line 41 of file SobolCalculators.h.

Referenced by StochasticTools::SobolCalculator< InType, OutType >::get().


The documentation for this class was generated from the following files: