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

#include <Calculators.h>

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

Public Member Functions

OutType compute (const 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 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...
 
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 typename InType::value_type &val) override
 Updating the calculator with a piece of data. More...
 
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 OutType get () const override
 Returns the resulting calculator value. More...
 

Protected Attributes

std::vector< OutType > _storage
 
OutType _median
 
const Parallel::Communicator & _communicator
 

Detailed Description

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

Definition at line 251 of file Calculators.h.

Member Function Documentation

◆ compute()

template<typename InType, typename OutType >
OutType StochasticTools::Calculator< InType, OutType >::compute ( const 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.

289 {
291  for (const auto & val : data)
292  updateCalculator(val);
293  finalizeCalculator(is_distributed);
294  return getValue();
295 }
void updateCalculator(const typename 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
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::Median< 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< InType, OutType >.

Definition at line 185 of file Calculators.C.

186 {
187  // Make sure we aren't doing anything silly like taking the median of an empty vector
188  _median = OutType();
189  auto count = _storage.size();
190  if (is_distributed)
191  this->_communicator.sum(count);
192  if (count == 0)
193  return;
194 
195  if (!is_distributed || this->n_processors() == 1)
196  {
197  std::sort(_storage.begin(), _storage.end());
198  if (count % 2)
199  _median = _storage[count / 2];
200  else
201  _median += (_storage[count / 2] + _storage[count / 2 - 1]) / 2;
202  return;
203  }
204 
205  dof_id_type kgt = count % 2 ? (count / 2) : (count / 2 - 1);
206  dof_id_type klt = kgt;
207  while (true)
208  {
209  // Gather all sizes and figure out current number of values
210  std::vector<std::size_t> sz = {_storage.size()};
211  this->_communicator.allgather(sz);
212  dof_id_type n = std::accumulate(sz.begin(), sz.end(), 0);
213 
214  // Choose the first value for the first processor with values
215  for (const auto & i : index_range(sz))
216  if (sz[i])
217  {
218  if (this->processor_id() == i)
219  _median = _storage[0];
220  this->_communicator.broadcast(_median, i);
221  break;
222  }
223 
224  // Count number of values greater than, less than, and equal to _median
225  std::vector<dof_id_type> m(3, 0);
226  for (const auto & val : _storage)
227  {
228  if (_median < val)
229  m[0]++;
230  else if (val < _median)
231  m[1]++;
232  }
233  this->_communicator.sum(m);
234  m[2] = n - m[0] - m[1];
235 
236  // Remove greater than equal to
237  if ((m[0] + m[2]) <= kgt)
238  {
239  _storage.erase(std::remove_if(_storage.begin(),
240  _storage.end(),
241  [this](const OutType & val) { return val >= _median; }),
242  _storage.end());
243  kgt -= m[0] + m[2];
244  }
245  // Remove less than equal to
246  else if ((m[1] + m[2]) <= klt)
247  {
248  _storage.erase(std::remove_if(_storage.begin(),
249  _storage.end(),
250  [this](const OutType & val) { return val <= _median; }),
251  _storage.end());
252  klt -= m[1] + m[2];
253  }
254  // If the number of points is odd, then we've found it
255  else if (count % 2)
256  break;
257  // Get average of the two middle numbers
258  else
259  {
260  OutType num2;
261  // Find the next greater than
262  if (m[0] > kgt)
263  {
264  num2 = std::numeric_limits<OutType>::max();
265  for (const auto & val : _storage)
266  if (_median < val && val < num2)
267  num2 = val;
268  this->_communicator.min(num2);
269  }
270  // Find the next less than
271  else if (m[1] > klt)
272  {
273  num2 = std::numeric_limits<OutType>::min();
274  for (const auto & val : _storage)
275  if (val < _median && num2 < val)
276  num2 += val;
277  this->_communicator.max(num2);
278  }
279  // Otherwise we know the other number is equal
280  else
281  num2 = _median;
282 
283  _median = (_median + num2) / 2;
284  break;
285  }
286  }
287 }
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
const Parallel::Communicator & _communicator
processor_id_type n_processors() const
void min(const T &r, T &o, Request &req) const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
std::vector< OutType > _storage
Definition: Calculators.h:262
void max(const T &r, T &o, Request &req) const
processor_id_type processor_id() const
auto index_range(const T &sizable)
uint8_t dof_id_type

◆ finalizeCalculator()

template<typename InType , typename OutType >
void StochasticTools::Calculator< InType, 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 OutType StochasticTools::Median< 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< InType, OutType >.

Definition at line 260 of file Calculators.h.

260 { return _median; }

◆ getValue()

template<typename InType , typename OutType >
OutType StochasticTools::Calculator< InType, 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::Median< 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< InType, OutType >.

Definition at line 171 of file Calculators.C.

172 {
173  _storage.clear();
174 }
std::vector< OutType > _storage
Definition: Calculators.h:262

◆ initializeCalculator()

template<typename InType , typename OutType >
void StochasticTools::Calculator< InType, 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.

Referenced by StochasticTools::SobolCalculator< std::vector< InType >, std::vector< OutType > >::finalize(), and StochasticTools::SobolCalculator< std::vector< InType >, std::vector< OutType > >::update().

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()

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

Definition at line 99 of file Calculators.h.

99 { return _name; }
const std::string _name
Definition: Calculators.h:132

◆ update()

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

Updating the calculator with a piece of data.

Sometimes some clever arithmetic is required to avoid storing data.

Implements StochasticTools::Calculator< InType, OutType >.

Definition at line 178 of file Calculators.C.

179 {
180  _storage.push_back(static_cast<OutType>(val));
181 }
std::vector< OutType > _storage
Definition: Calculators.h:262

◆ updateCalculator()

template<typename InType, typename OutType >
void StochasticTools::Calculator< InType, OutType >::updateCalculator ( const typename 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 InType::value_type &)=0
Updating the calculator with a piece of data.

Member Data Documentation

◆ _median

template<typename InType , typename OutType >
OutType StochasticTools::Median< InType, OutType >::_median
protected

Definition at line 263 of file Calculators.h.

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

◆ _storage

template<typename InType , typename OutType >
std::vector<OutType> StochasticTools::Median< InType, OutType >::_storage
protected

Definition at line 262 of file Calculators.h.


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