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 : protected: 29 : ///@{ 30 : /** 31 : * doco-normal-methods-begin 32 : * Returns read-only reference to a Reporter value that is provided by an input parameter. 33 : * @tparam T The C++ type of the Reporter value being consumed 34 : * @param param_name The name of the parameter that gives the name of the Reporter, which 35 : * must be a ReporterName parameter (i.e., getParam<ReporterName>(param_name)). 36 : * @param mode The mode that the object will consume the Reporter value 37 : * @pararm time_index (optional) If zero is provided the current value is returned. Use a positive 38 : * index to return previous values (1 = older, 2 = older, etc.). The maximum 39 : * number of old values is dictated by the ReporterData object. 40 : */ 41 : template <typename T> 42 : const T & getReporterValue(const std::string & param_name, const std::size_t time_index = 0); 43 : template <typename T> 44 : const T & getReporterValue(const std::string & param_name, 45 : ReporterMode mode, 46 : const std::size_t time_index = 0); 47 : // doco-normal-methods-end 48 : ///@} 49 : 50 : ///@{ 51 : /** 52 : * Returns read-only reference to a Reporter value that is provided by name directly. 53 : * @tparam T The C++ type of the Reporter value being consumed 54 : * @param reporter_name A ReporterName object that for the desired Reporter value. 55 : * @param mode The mode that the object will consume the Reporter value 56 : * @pararm time_index (optional) If zero is provided the current value is returned. Use a positive 57 : * index to return previous values (1 = older, 2 = older, etc.). The maximum 58 : * number of old values is dictated by the ReporterData object. 59 : */ 60 : template <typename T> 61 : const T & getReporterValueByName(const ReporterName & reporter_name, 62 : const std::size_t time_index = 0); 63 : template <typename T> 64 : const T & getReporterValueByName(const ReporterName & reporter_name, 65 : ReporterMode mode, 66 : const std::size_t time_index = 0); 67 : ///@} 68 : 69 : ///@{ 70 : /** 71 : * Return True if the Reporter value exists. 72 : * @tparam T The C++ type of the Reporter value being consumed 73 : * @param reporter_name A ReporterName object that for the desired Reporter value. 74 : */ 75 : bool hasReporterValue(const std::string & param_name) const; 76 : bool hasReporterValueByName(const ReporterName & reporter_name) const; 77 : template <typename T> 78 : bool hasReporterValue(const std::string & param_name) const; 79 : template <typename T> 80 : bool hasReporterValueByName(const ReporterName & reporter_name) const; 81 : 82 : ///@} 83 : 84 : /** 85 : * @returns The ReporterName associated with the parametre \p param_name. 86 : * 87 : * Performs error checking to mak sure that the parameter is valid. 88 : */ 89 : const ReporterName & getReporterName(const std::string & param_name) const; 90 : 91 : /** 92 : * A method that can be overridden to update the UO dependencies. 93 : * 94 : * This is needed because the get methods for this interface cannot be virtual because of the 95 : * template parameter. See GeneralUserObject for how it is utilized. 96 : */ 97 916 : virtual void addReporterDependencyHelper(const ReporterName & /*state_name*/) {} 98 : 99 : private: 100 : /** 101 : * @returns True if all Reporters have been added (the task associated with adding them is 102 : * complete) 103 : */ 104 : bool reportersAdded() const; 105 : 106 : /** 107 : * Helpers for "possibly" checking if a Reporter value exists. This is only 108 : * able to check for existance after all Reporters have been added (after 109 : * the task creating them has been called). If called before said task, this 110 : * will do nothing, hence the "possibly". This allows us to have errors reported 111 : * directly by the object requesting the Reporter instead of through a system with 112 : * less context. 113 : */ 114 : template <typename T> 115 : void possiblyCheckHasReporter(const ReporterName & reporter_name, 116 : const std::string & param_name = "") const; 117 : 118 : /// Parameters for the MooseObject inherting from this interface 119 : const InputParameters & _ri_params; 120 : 121 : /// Provides access to FEProblemBase::getReporterData 122 : FEProblemBase & _ri_fe_problem_base; 123 : 124 : /// The ReporterData 125 : const ReporterData & _ri_reporter_data; 126 : 127 : /// The MooseObject needing this interface 128 : const MooseObject & _ri_moose_object; 129 : }; 130 : 131 : template <typename T> 132 : const T & 133 272 : ReporterInterface::getReporterValue(const std::string & param_name, const std::size_t time_index) 134 : { 135 272 : return getReporterValue<T>(param_name, REPORTER_MODE_UNSET, time_index); 136 : } 137 : 138 : template <typename T> 139 : const T & 140 952 : ReporterInterface::getReporterValue(const std::string & param_name, 141 : ReporterMode mode, 142 : const std::size_t time_index) 143 : { 144 952 : const auto & reporter_name = getReporterName(param_name); 145 : 146 944 : possiblyCheckHasReporter<T>(reporter_name, param_name); 147 : 148 940 : return getReporterValueByName<T>(reporter_name, mode, time_index); 149 : } 150 : 151 : template <typename T> 152 : const T & 153 372 : ReporterInterface::getReporterValueByName(const ReporterName & reporter_name, 154 : const std::size_t time_index) 155 : { 156 372 : return getReporterValueByName<T>(reporter_name, REPORTER_MODE_UNSET, time_index); 157 : } 158 : 159 : template <typename T> 160 : const T & 161 1312 : ReporterInterface::getReporterValueByName(const ReporterName & reporter_name, 162 : ReporterMode mode, 163 : const std::size_t time_index) 164 : { 165 1312 : possiblyCheckHasReporter<T>(reporter_name); 166 : 167 1308 : addReporterDependencyHelper(reporter_name); 168 : 169 1308 : return _ri_reporter_data.getReporterValue<T>(reporter_name, _ri_moose_object, mode, time_index); 170 : } 171 : 172 : template <typename T> 173 : bool 174 4 : ReporterInterface::hasReporterValue(const std::string & param_name) const 175 : { 176 4 : if (!reportersAdded()) 177 4 : _ri_moose_object.mooseError( 178 : "Cannot call hasReporterValue() until all Reporters have been constructed."); 179 : 180 0 : return hasReporterValueByName<T>(getReporterName(param_name)); 181 : } 182 : 183 : template <typename T> 184 : bool 185 1476 : ReporterInterface::hasReporterValueByName(const ReporterName & reporter_name) const 186 : { 187 1476 : if (!reportersAdded()) 188 4 : _ri_moose_object.mooseError( 189 : "Cannot call hasReporterValueByName() until all Reporters have been constructed."); 190 : 191 1472 : return _ri_reporter_data.hasReporterValue<T>(reporter_name); 192 : } 193 : 194 : template <typename T> 195 : void 196 2256 : ReporterInterface::possiblyCheckHasReporter(const ReporterName & reporter_name, 197 : const std::string & param_name /* = "" */) const 198 : { 199 2256 : if (reportersAdded() && !hasReporterValueByName<T>(reporter_name)) 200 : { 201 8 : std::stringstream oss; 202 8 : oss << "A Reporter value with the name \"" << reporter_name << "\" and type \"" 203 8 : << MooseUtils::prettyCppType<T>() << "\" was not found."; 204 : 205 8 : if (_ri_params.isParamValid(param_name)) 206 4 : _ri_moose_object.paramError(param_name, oss.str()); 207 : else 208 4 : _ri_moose_object.mooseError(oss.str()); 209 0 : } 210 2248 : }