Skip to content

Commit

Permalink
Create template version of Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
aeslaughter committed Jan 14, 2021
1 parent 7320e33 commit 1198934
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 139 deletions.
Expand Up @@ -39,7 +39,7 @@ class ReporterStatisticsContext : public ReporterContext<T>
const ReporterProducerEnum & _data_mode;

/// Storage for the Calculator object for the desired stat, this is created in constructor
std::unique_ptr<const StochasticTools::Calculator> _calc_ptr;
std::unique_ptr<const StochasticTools::Calculator<std::vector<Real>, Real>> _calc_ptr;

/// Storage for the BootstrapCalculator for the desired confidence interval calculations (optional)
const StochasticTools::BootstrapCalculator * _ci_calc_ptr;
Expand Down
24 changes: 15 additions & 9 deletions modules/stochastic_tools/include/utils/BootstrapCalculators.h
Expand Up @@ -19,6 +19,7 @@ class MooseRandom;

namespace StochasticTools
{
template <typename InType, typename OutType>
class Calculator;
class BootstrapCalculator;

Expand Down Expand Up @@ -59,10 +60,10 @@ class BootstrapCalculator : public libMesh::ParallelObject
* Compute the bootstrap confidence level intervals.
* @param data Vector of data from which statistics are to be computed
* @param calc Calculator object defining the statistic to be computed
* @param is_distributed Flag indicating if the data is distributed in parallel
* @param is_distributed Flag indicating if the dMuata is distributed in parallel
*/
virtual std::vector<Real> compute(const std::vector<Real> & data,
const Calculator & calc,
const Calculator<std::vector<Real>, Real> & calc,
const bool is_distributed) const = 0;

///@{
Expand All @@ -75,8 +76,9 @@ class BootstrapCalculator : public libMesh::ParallelObject

protected:
// Compute Bootstrap estimates of a statistic
std::vector<Real>
computeBootstrapEstimates(const std::vector<Real> &, const Calculator &, const bool) const;
std::vector<Real> computeBootstrapEstimates(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const;

// Randomly shuffle a vector of data
std::vector<Real> shuffle(const std::vector<Real> &, MooseRandom &, const bool) const;
Expand Down Expand Up @@ -105,8 +107,9 @@ class Percentile : public BootstrapCalculator
unsigned int replicates,
unsigned int seed);

virtual std::vector<Real>
compute(const std::vector<Real> &, const Calculator &, const bool) const override;
virtual std::vector<Real> compute(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const override;
};

/*
Expand All @@ -120,12 +123,15 @@ class BiasCorrectedAccelerated : public BootstrapCalculator
unsigned int replicates,
unsigned int seed);

virtual std::vector<Real>
compute(const std::vector<Real> &, const Calculator &, const bool) const override;
virtual std::vector<Real> compute(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const override;

private:
// Compute the acceleration, see Efron and Tibshirani (2003), Ch. 14, Eq. 14.15, p 186.
Real acceleration(const std::vector<Real> &, const Calculator &, const bool) const;
Real acceleration(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const;
};

} // namespace
129 changes: 89 additions & 40 deletions modules/stochastic_tools/include/utils/Calculators.h
Expand Up @@ -17,19 +17,23 @@ class MooseEnumItem;

namespace StochasticTools
{
class Calculator;

/*
* Free function for building a const Calculator object for use by Statistics object
*/
std::unique_ptr<const Calculator> makeCalculator(const MooseEnumItem & item,
const libMesh::ParallelObject & other);

/*
* Free function that returns the available statistics available to the Statistics object(s)
*/
MultiMooseEnum makeCalculatorEnum();

class CalculatorBase : public libMesh::ParallelObject
{
public:
CalculatorBase(const libMesh::ParallelObject &, const std::string & name);
virtual ~CalculatorBase() = default;
const std::string & name() const;

private:
const std::string _name;
};

/* Base class for computing statistics (e.g., mean, min) for use with Statistics object
*
* The purpose of these objects are to provide an API for computing statistics in serial or parallel
Expand All @@ -43,72 +47,117 @@ MultiMooseEnum makeCalculatorEnum();
* To create new Calculator objects first create the Calculator class and then update the
* above free functions above.
*/
class Calculator : public libMesh::ParallelObject
template <typename InType = std::vector<Real>, typename OutType = Real>
class Calculator : public CalculatorBase
{
public:
Calculator(const libMesh::ParallelObject &, const std::string & stat);
virtual ~Calculator() = default;
const std::string & name() const;
virtual Real compute(const std::vector<Real> &, bool) const = 0;

private:
const std::string _stat;
Calculator(const libMesh::ParallelObject &, const std::string & name);
virtual OutType compute(const InType &, bool) const = 0;
};

class Mean : public Calculator
template <typename InType, typename OutType>
class Mean : public Calculator<InType, OutType>
{
public:
Mean(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

class Min : public Calculator
template <typename InType, typename OutType>
class Min : public Calculator<InType, OutType>
{
public:
Min(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

class Max : public Calculator
template <typename InType, typename OutType>
class Max : public Calculator<InType, OutType>
{
public:
Max(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

class Sum : public Calculator
template <typename InType, typename OutType>
class Sum : public Calculator<InType, OutType>
{
public:
Sum(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

class StdDev : public Calculator
template <typename InType, typename OutType>
class StdDev : public Calculator<InType, OutType>
{
public:
StdDev(const libMesh::ParallelObject &);
StdDev(const libMesh::ParallelObject &, const std::string & stat);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

class StdErr : public StdDev
template <typename InType, typename OutType>
class StdErr : public StdDev<InType, OutType>
{
public:
StdErr(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using StdDev<InType, OutType>::StdDev;
virtual OutType compute(const InType &, bool) const override;
};

class Ratio : public Calculator
template <typename InType, typename OutType>
class Ratio : public Calculator<InType, OutType>
{
public:
Ratio(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

class L2Norm : public Calculator
template <typename InType, typename OutType>
class L2Norm : public Calculator<InType, OutType>
{
public:
L2Norm(const libMesh::ParallelObject &);
virtual Real compute(const std::vector<Real> &, bool) const override;
using Calculator<InType, OutType>::Calculator;
virtual OutType compute(const InType &, bool) const override;
};

/*
* Free function for building a const Calculator object for use by Statistics object
*/
std::unique_ptr<const Calculator<std::vector<Real>, Real>>
makeCalculator(const MooseEnumItem & item, const libMesh::ParallelObject & other);

/*
template <typename InType, typename OutType>
std::unique_ptr<const Calculator<InType, OutType>> makeCalculator(const MooseEnumItem & item,
const libMesh::ParallelObject &
other)
{
if (item == "min")
return libmesh_make_unique<const Min<InType, OutType>>(other, item);
else if (item == "max")
return libmesh_make_unique<const Max<InType, OutType>>(other, item);
else if (item == "sum")
return libmesh_make_unique<const Sum<InType, OutType>>(other, item);
else if (item == "mean" || item == "average") // average is deprecated
return libmesh_make_unique<const Mean<InType, OutType>>(other, item);
else if (item == "stddev")
return libmesh_make_unique<const StdDev<InType, OutType>>(other, item);
else if (item == "stderr")
return libmesh_make_unique<const StdErr<InType, OutType>>(other, item);
else if (item == "norm2")
return libmesh_make_unique<const L2Norm<InType, OutType>>(other, item);
else if (item == "ratio")
return libmesh_make_unique<const Ratio<InType, OutType>>(other, item);
::mooseError("Failed to create Statistics::Calculator object for ", item);
return nullptr;
}
*/

} // namespace
8 changes: 4 additions & 4 deletions modules/stochastic_tools/src/utils/BootstrapCalculators.C
Expand Up @@ -66,7 +66,7 @@ BootstrapCalculator::BootstrapCalculator(const libMesh::ParallelObject & other,

std::vector<Real>
BootstrapCalculator::computeBootstrapEstimates(const std::vector<Real> & data,
const Calculator & calc,
const Calculator<std::vector<Real>, Real> & calc,
const bool is_distributed) const
{
MooseRandom generator;
Expand Down Expand Up @@ -198,7 +198,7 @@ Percentile::Percentile(const libMesh::ParallelObject & other,

std::vector<Real>
Percentile::compute(const std::vector<Real> & data,
const Calculator & calc,
const Calculator<std::vector<Real>, Real> & calc,
const bool is_distributed) const
{
// Bootstrap estimates
Expand Down Expand Up @@ -228,7 +228,7 @@ BiasCorrectedAccelerated::BiasCorrectedAccelerated(const libMesh::ParallelObject

std::vector<Real>
BiasCorrectedAccelerated::compute(const std::vector<Real> & data,
const Calculator & calc,
const Calculator<std::vector<Real>, Real> & calc,
const bool is_distributed) const
{
if (is_distributed)
Expand Down Expand Up @@ -263,7 +263,7 @@ BiasCorrectedAccelerated::compute(const std::vector<Real> & data,

Real
BiasCorrectedAccelerated::acceleration(const std::vector<Real> & data,
const Calculator & calc,
const Calculator<std::vector<Real>, Real> & calc,
const bool is_distributed) const
{
// Jackknife statistics
Expand Down

0 comments on commit 1198934

Please sign in to comment.