LCOV - code coverage report
Current view: top level - include/reporters - StatisticsReporter.h (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 23 23 100.0 %
Date: 2025-07-25 05:00:46 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "GeneralReporter.h"
      13             : #include "VectorCalculators.h"
      14             : 
      15             : #include "nlohmann/json.h"
      16             : 
      17             : /**
      18             :  * ReporterContext that utilizes a Calculator object to compute its value and confidence levels
      19             :  */
      20             : template <typename InType, typename OutType>
      21             : class ReporterStatisticsContext
      22             :   : public ReporterGeneralContext<std::pair<OutType, std::vector<OutType>>>
      23             : {
      24             : public:
      25             :   ReporterStatisticsContext(const libMesh::ParallelObject & other,
      26             :                             const MooseObject & producer,
      27             :                             ReporterState<std::pair<OutType, std::vector<OutType>>> & state,
      28             :                             const InType & data,
      29             :                             const ReporterProducerEnum & mode,
      30             :                             const MooseEnumItem & stat);
      31             : 
      32             :   ReporterStatisticsContext(const libMesh::ParallelObject & other,
      33             :                             const MooseObject & producer,
      34             :                             ReporterState<std::pair<OutType, std::vector<OutType>>> & state,
      35             :                             const InType & data,
      36             :                             const ReporterProducerEnum & mode,
      37             :                             const MooseEnumItem & stat,
      38             :                             const MooseEnum & ci_method,
      39             :                             const std::vector<Real> & ci_levels,
      40             :                             unsigned int ci_replicates,
      41             :                             unsigned int ci_seed);
      42             : 
      43             :   virtual void finalize() override;
      44             :   virtual void storeInfo(nlohmann::json & json) const override;
      45             : 
      46             : private:
      47             :   /// Data used for the statistic calculation
      48             :   const InType & _data;
      49             : 
      50             :   /// Mode in which the above data was produced
      51             :   const ReporterProducerEnum & _data_mode;
      52             : 
      53             :   /// Storage for the Calculator object for the desired stat, this is created in constructor
      54             :   std::unique_ptr<StochasticTools::Calculator<InType, OutType>> _calc_ptr;
      55             : 
      56             :   /// Storage for the BootstrapCalculator for the desired confidence interval calculations (optional)
      57             :   std::unique_ptr<StochasticTools::BootstrapCalculator<InType, OutType>> _ci_calc_ptr = nullptr;
      58             : };
      59             : 
      60             : template <typename InType, typename OutType>
      61        3672 : ReporterStatisticsContext<InType, OutType>::ReporterStatisticsContext(
      62             :     const libMesh::ParallelObject & other,
      63             :     const MooseObject & producer,
      64             :     ReporterState<std::pair<OutType, std::vector<OutType>>> & state,
      65             :     const InType & data,
      66             :     const ReporterProducerEnum & mode,
      67             :     const MooseEnumItem & stat)
      68             :   : ReporterGeneralContext<std::pair<OutType, std::vector<OutType>>>(other, producer, state),
      69        3672 :     _data(data),
      70        3672 :     _data_mode(mode),
      71        3672 :     _calc_ptr(StochasticTools::makeCalculator<InType, OutType>(stat, other))
      72             : {
      73        3672 : }
      74             : 
      75             : template <typename InType, typename OutType>
      76        2296 : ReporterStatisticsContext<InType, OutType>::ReporterStatisticsContext(
      77             :     const libMesh::ParallelObject & other,
      78             :     const MooseObject & producer,
      79             :     ReporterState<std::pair<OutType, std::vector<OutType>>> & state,
      80             :     const InType & data,
      81             :     const ReporterProducerEnum & mode,
      82             :     const MooseEnumItem & stat,
      83             :     const MooseEnum & ci_method,
      84             :     const std::vector<Real> & ci_levels,
      85             :     unsigned int ci_replicates,
      86             :     unsigned int ci_seed)
      87        2296 :   : ReporterStatisticsContext<InType, OutType>(other, producer, state, data, mode, stat)
      88             : {
      89        2296 :   _ci_calc_ptr = StochasticTools::makeBootstrapCalculator<InType, OutType>(
      90             :       ci_method, other, ci_levels, ci_replicates, ci_seed, *_calc_ptr);
      91        2296 : }
      92             : 
      93             : template <typename InType, typename OutType>
      94             : void
      95        3992 : ReporterStatisticsContext<InType, OutType>::finalize()
      96             : {
      97        3992 :   if (_data_mode == REPORTER_MODE_DISTRIBUTED || this->processor_id() == 0)
      98             :   {
      99        3140 :     this->_state.value().first = _calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);
     100             : 
     101        2812 :     if (_ci_calc_ptr)
     102        3128 :       this->_state.value().second =
     103        1656 :           _ci_calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);
     104             :   }
     105             : 
     106        3992 :   ReporterGeneralContext<std::pair<OutType, std::vector<OutType>>>::finalize();
     107        3992 : }
     108             : 
     109             : template <typename InType, typename OutType>
     110             : void
     111        2010 : ReporterStatisticsContext<InType, OutType>::storeInfo(nlohmann::json & json) const
     112             : {
     113        2010 :   ReporterGeneralContext<std::pair<OutType, std::vector<OutType>>>::storeInfo(json);
     114        2010 :   json["stat"] = _calc_ptr->name();
     115        2010 : }
     116             : 
     117             : /**
     118             :  * Compute several metrics for supplied data.
     119             :  *
     120             :  * This class uses Calculator objects defined in StatisticsReporter.h and is setup such that if a
     121             :  * new calculation is needed it can be added in StatisticsReporter.h without modification of this
     122             :  * object.
     123             :  */
     124             : class StatisticsReporter : public GeneralReporter
     125             : {
     126             : public:
     127             :   static InputParameters validParams();
     128             :   StatisticsReporter(const InputParameters & parameters);
     129             : 
     130             :   /**
     131             :    * This is where the reporter values are declared
     132             :    * Note: unfortunetly this cannot be in the constructor since the reporter values
     133             :    *       containing the data might not exist yet. So we put it here to give the
     134             :    *       values their last chance to exist.
     135             :    */
     136             :   virtual void initialize() final;
     137             : 
     138             :   /// Not used; all operations are  wrapped in the ReporterStatisticsContext
     139         934 :   virtual void execute() final{};
     140         934 :   virtual void finalize() final{};
     141             :   virtual void store(nlohmann::json & json) const override;
     142             : 
     143             : private:
     144             :   // Statistics to be computed
     145             :   const MultiMooseEnum & _compute_stats;
     146             : 
     147             :   // CI Method to be computed (optional)
     148             :   const MooseEnum & _ci_method;
     149             : 
     150             :   // CI levels to be computed
     151             :   const std::vector<Real> & _ci_levels;
     152             : 
     153             :   // Number of CI replicates to use in Bootstrap methods
     154             :   const unsigned int & _ci_replicates;
     155             : 
     156             :   // Random seed for producing CI replicates
     157             :   const unsigned int & _ci_seed;
     158             : 
     159             :   /// Whether or not initialize() has been called for reporter value declaration
     160             :   bool _initialized;
     161             : 
     162             :   /**
     163             :    * Helper for adding statistic reporter values
     164             :    *
     165             :    * @param r_name ReporterName of the data from which the statistics will be computed
     166             :    */
     167             :   template <typename InType, typename OutType>
     168             :   void declareValueHelper(const ReporterName & r_name);
     169             : };

Generated by: LCOV version 1.14