LCOV - code coverage report
Current view: top level - include/reporters - ReporterInterface.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 29 31 93.5 %
Date: 2026-05-29 20:35:17 Functions: 52 55 94.5 %
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             : #pragma once
      10             : 
      11             : #include "MooseTypes.h"
      12             : #include "ReporterData.h"
      13             : #include "MooseObject.h"
      14             : 
      15             : // Forward declarations
      16             : class FEProblemBase;
      17             : class InputParameters;
      18             : 
      19             : /**
      20             :  * Interface to allow object to consume Reporter values.
      21             :  */
      22             : class ReporterInterface
      23             : {
      24             : public:
      25             :   static InputParameters validParams();
      26             :   ReporterInterface(const MooseObject * moose_object);
      27             : 
      28             : #ifdef MOOSE_KOKKOS_ENABLED
      29             :   /**
      30             :    * Special constructor used for Kokkos functor copy during parallel dispatch
      31             :    */
      32             :   ReporterInterface(const ReporterInterface & object, const Moose::Kokkos::FunctorCopy & key);
      33             : #endif
      34             : 
      35             : protected:
      36             :   ///@{
      37             :   /**
      38             :    * doco-normal-methods-begin
      39             :    * Returns read-only reference to a Reporter value that is provided by an input parameter.
      40             :    * @tparam T The C++ type of the Reporter value being consumed
      41             :    * @param param_name The name of the parameter that gives the name of the Reporter, which
      42             :    *                   must be a ReporterName parameter (i.e., getParam<ReporterName>(param_name)).
      43             :    * @param mode The mode that the object will consume the Reporter value
      44             :    * @pararm time_index (optional) If zero is provided the current value is returned. Use a positive
      45             :    *                    index to return previous values (1 = older, 2 = older, etc.). The maximum
      46             :    *                    number of old values is dictated by the ReporterData object.
      47             :    */
      48             :   template <typename T>
      49             :   const T & getReporterValue(const std::string & param_name, const std::size_t time_index = 0);
      50             :   template <typename T>
      51             :   const T & getReporterValue(const std::string & param_name,
      52             :                              ReporterMode mode,
      53             :                              const std::size_t time_index = 0);
      54             :   // doco-normal-methods-end
      55             :   ///@}
      56             : 
      57             :   ///@{
      58             :   /**
      59             :    * Returns read-only reference to a Reporter value that is provided by name directly.
      60             :    * @tparam T The C++ type of the Reporter value being consumed
      61             :    * @param reporter_name A ReporterName object that for the desired Reporter value.
      62             :    * @param mode The mode that the object will consume the Reporter value
      63             :    * @pararm time_index (optional) If zero is provided the current value is returned. Use a positive
      64             :    *                    index to return previous values (1 = older, 2 = older, etc.). The maximum
      65             :    *                    number of old values is dictated by the ReporterData object.
      66             :    */
      67             :   template <typename T>
      68             :   const T & getReporterValueByName(const ReporterName & reporter_name,
      69             :                                    const std::size_t time_index = 0);
      70             :   template <typename T>
      71             :   const T & getReporterValueByName(const ReporterName & reporter_name,
      72             :                                    ReporterMode mode,
      73             :                                    const std::size_t time_index = 0);
      74             :   ///@}
      75             : 
      76             :   ///@{
      77             :   /**
      78             :    * Return True if the Reporter value exists.
      79             :    * @tparam T The C++ type of the Reporter value being consumed
      80             :    * @param reporter_name A ReporterName object that for the desired Reporter value.
      81             :    */
      82             :   bool hasReporterValue(const std::string & param_name) const;
      83             :   bool hasReporterValueByName(const ReporterName & reporter_name) const;
      84             :   template <typename T>
      85             :   bool hasReporterValue(const std::string & param_name) const;
      86             :   template <typename T>
      87             :   bool hasReporterValueByName(const ReporterName & reporter_name) const;
      88             : 
      89             :   ///@}
      90             : 
      91             :   /**
      92             :    * Get the reporter context to allow non-typed operations with the data
      93             :    * @param reporter_name A ReporterName object that for the desired Reporter context.
      94             :    * @returns ReporterContextBase of the reporter value
      95             :    */
      96             :   const ReporterContextBase &
      97             :   getReporterContextBaseByName(const ReporterName & reporter_name) const;
      98             : 
      99             :   /**
     100             :    * @returns The ReporterName associated with the parametre \p param_name.
     101             :    *
     102             :    * Performs error checking to mak sure that the parameter is valid.
     103             :    */
     104             :   const ReporterName & getReporterName(const std::string & param_name) const;
     105             : 
     106             :   /**
     107             :    * A method that can be overridden to update the UO dependencies.
     108             :    *
     109             :    * This is needed because the get methods for this interface cannot be virtual because of the
     110             :    * template parameter. See GeneralUserObject for how it is utilized.
     111             :    */
     112         886 :   virtual void addReporterDependencyHelper(const ReporterName & /*state_name*/) {}
     113             : 
     114             : private:
     115             :   /**
     116             :    * @returns True if all Reporters have been added (the task associated with adding them is
     117             :    * complete)
     118             :    */
     119             :   bool reportersAdded() const;
     120             : 
     121             :   /**
     122             :    * Helpers for "possibly" checking if a Reporter value exists. This is only
     123             :    * able to check for existance after all Reporters have been added (after
     124             :    * the task creating them has been called). If called before said task, this
     125             :    * will do nothing, hence the "possibly". This allows us to have errors reported
     126             :    * directly by the object requesting the Reporter instead of through a system with
     127             :    * less context.
     128             :    */
     129             :   template <typename T>
     130             :   void possiblyCheckHasReporter(const ReporterName & reporter_name,
     131             :                                 const std::string & param_name = "") const;
     132             : 
     133             :   /// Parameters for the MooseObject inherting from this interface
     134             :   const InputParameters & _ri_params;
     135             : 
     136             :   /// Provides access to FEProblemBase::getReporterData
     137             :   FEProblemBase & _ri_fe_problem_base;
     138             : 
     139             :   /// The ReporterData
     140             :   const ReporterData & _ri_reporter_data;
     141             : 
     142             :   /// The MooseObject needing this interface
     143             :   const MooseObject & _ri_moose_object;
     144             : };
     145             : 
     146             : template <typename T>
     147             : const T &
     148         261 : ReporterInterface::getReporterValue(const std::string & param_name, const std::size_t time_index)
     149             : {
     150         261 :   return getReporterValue<T>(param_name, REPORTER_MODE_UNSET, time_index);
     151             : }
     152             : 
     153             : template <typename T>
     154             : const T &
     155         921 : ReporterInterface::getReporterValue(const std::string & param_name,
     156             :                                     ReporterMode mode,
     157             :                                     const std::size_t time_index)
     158             : {
     159         921 :   const auto & reporter_name = getReporterName(param_name);
     160             : 
     161         915 :   possiblyCheckHasReporter<T>(reporter_name, param_name);
     162             : 
     163         912 :   return getReporterValueByName<T>(reporter_name, mode, time_index);
     164             : }
     165             : 
     166             : template <typename T>
     167             : const T &
     168         525 : ReporterInterface::getReporterValueByName(const ReporterName & reporter_name,
     169             :                                           const std::size_t time_index)
     170             : {
     171         525 :   return getReporterValueByName<T>(reporter_name, REPORTER_MODE_UNSET, time_index);
     172             : }
     173             : 
     174             : template <typename T>
     175             : const T &
     176        1650 : ReporterInterface::getReporterValueByName(const ReporterName & reporter_name,
     177             :                                           ReporterMode mode,
     178             :                                           const std::size_t time_index)
     179             : {
     180        1650 :   possiblyCheckHasReporter<T>(reporter_name);
     181             : 
     182        1647 :   addReporterDependencyHelper(reporter_name);
     183             : 
     184        1647 :   return _ri_reporter_data.getReporterValue<T>(reporter_name, _ri_moose_object, mode, time_index);
     185             : }
     186             : 
     187             : template <typename T>
     188             : bool
     189           3 : ReporterInterface::hasReporterValue(const std::string & param_name) const
     190             : {
     191           3 :   if (!reportersAdded())
     192           3 :     _ri_moose_object.mooseError(
     193             :         "Cannot call hasReporterValue() until all Reporters have been constructed.");
     194             : 
     195           0 :   return hasReporterValueByName<T>(getReporterName(param_name));
     196             : }
     197             : 
     198             : template <typename T>
     199             : bool
     200        1433 : ReporterInterface::hasReporterValueByName(const ReporterName & reporter_name) const
     201             : {
     202        1433 :   if (!reportersAdded())
     203           3 :     _ri_moose_object.mooseError(
     204             :         "Cannot call hasReporterValueByName() until all Reporters have been constructed.");
     205             : 
     206        1430 :   return _ri_reporter_data.hasReporterValue<T>(reporter_name);
     207             : }
     208             : 
     209             : template <typename T>
     210             : void
     211        2565 : ReporterInterface::possiblyCheckHasReporter(const ReporterName & reporter_name,
     212             :                                             const std::string & param_name /* = "" */) const
     213             : {
     214        2565 :   if (reportersAdded() && !hasReporterValueByName<T>(reporter_name))
     215             :   {
     216           6 :     std::stringstream oss;
     217           6 :     oss << "A Reporter value with the name \"" << reporter_name << "\" and type \""
     218           6 :         << MooseUtils::prettyCppType<T>() << "\" was not found.";
     219             : 
     220           6 :     if (_ri_params.isParamValid(param_name))
     221           3 :       _ri_moose_object.paramError(param_name, oss.str());
     222             :     else
     223           3 :       _ri_moose_object.mooseError(oss.str());
     224           0 :   }
     225        2559 : }

Generated by: LCOV version 1.14