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 "ADReal.h" 13 : #include "MooseTypes.h" 14 : #include "SubProblem.h" 15 : #include "MooseFunctorForward.h" 16 : 17 : #include <vector> 18 : #include <memory> 19 : #include <string> 20 : 21 : class InputParameters; 22 : class MooseObject; 23 : 24 : /** 25 : * \class FunctorInterface 26 : * \brief An interface for accessing \p Moose::Functors 27 : */ 28 : class FunctorInterface 29 : { 30 : public: 31 : static InputParameters validParams(); 32 : 33 : FunctorInterface(const MooseObject * moose_object); 34 : 35 : #ifdef MOOSE_KOKKOS_ENABLED 36 : /** 37 : * Special constructor used for Kokkos functor copy during parallel dispatch 38 : */ 39 : FunctorInterface(const FunctorInterface & object, const Moose::Kokkos::FunctorCopy & key); 40 : #endif 41 : 42 : /** 43 : * Helper to look up a functor name through the input parameter keys 44 : * @param name The input parameter name that we are trying to deduce the functor name for 45 : * @param params The input parameters object that we will be checking for parameters named \p name 46 : * @return The functor name 47 : */ 48 : static std::string deduceFunctorName(const std::string & name, const InputParameters & params); 49 : 50 : protected: 51 : /** 52 : * Retrieves a functor from the subproblem. This method also leverages the ability to create 53 : * default functors if the user passed an integer or real in the input file 54 : * @param name The name of the functor to retrieve. This should match the functor parameter name, 55 : * \emph not the actual name of the functor created in the input file 56 : * @return The functor 57 : */ 58 : template <typename T> 59 : const Moose::Functor<T> & getFunctor(const std::string & name); 60 : 61 : /** 62 : * Retrieves a functor from the subproblem. This method also leverages the ability to create 63 : * default functors if the user passed an integer or real in the input file 64 : * @param name The name of the functor to retrieve. This should match the functor parameter name, 65 : * \emph not the actual name of the functor created in the input file 66 : * @param tid The thread ID used to retrieve the functor from this interface's subproblem 67 : * @return The functor 68 : */ 69 : template <typename T> 70 : const Moose::Functor<T> & getFunctor(const std::string & name, THREAD_ID tid); 71 : 72 : /** 73 : * Retrieves a functor from the passed-in subproblem. This method also leverages the ability to 74 : * create default functors if the user passed an integer or real in the input file 75 : * @param name The name of the functor to retrieve. This should match the functor parameter name, 76 : * \emph not the actual name of the functor created in the input file 77 : * @param subproblem The subproblem to query for the functor 78 : * @return The functor 79 : */ 80 : template <typename T> 81 : const Moose::Functor<T> & getFunctor(const std::string & name, SubProblem & subproblem); 82 : 83 : /** 84 : * Retrieves a functor from the passed-in subproblem. This method also leverages the ability to 85 : * create default functors if the user passed an integer or real in the input file 86 : * @param name The name of the functor to retrieve. This should match the functor parameter name, 87 : * \emph not the actual name of the functor created in the input file 88 : * @param subproblem The subproblem to query for the functor 89 : * @param tid The thread ID used to retrieve the functor from the \p subproblem 90 : * @return The functor 91 : */ 92 : template <typename T> 93 : const Moose::Functor<T> & 94 : getFunctor(const std::string & name, SubProblem & subproblem, THREAD_ID tid); 95 : 96 : /** 97 : * Checks the subproblem for the given functor. This will not query default functors 98 : * potentially stored in this object, e.g. this method will return false if the user passed an 99 : * int or real to the functor param in the input file 100 : * @param name The name of the functor to check. This should match the functor parameter name, 101 : * \emph not the actual name of the functor created in the input file 102 : * @return Whether the subproblem has the specified functor 103 : */ 104 : bool isFunctor(const std::string & name) const; 105 : 106 : /** 107 : * Checks the passed-in subproblem for the given functor. This will not query default functors 108 : * potentially stored in this object, e.g. this method will return false if the user passed an int 109 : * or real to the functor param in the input file 110 : * @param name The name of the functor to check. This should match the functor parameter name, 111 : * \emph not the actual name of the functor created in the input file 112 : * @param subproblem The subproblem to query for the functor 113 : * @return Whether the subproblem has the specified functor 114 : */ 115 : bool isFunctor(const std::string & name, const SubProblem & subproblem) const; 116 : 117 : /** 118 : * Small helper to look up a functor name through the input parameter keys 119 : */ 120 : std::string deduceFunctorName(const std::string & name) const; 121 : 122 : /** 123 : * Helper method to create an elemental argument for a functor that includes whether to perform 124 : * skewness corrections 125 : */ 126 : Moose::ElemArg makeElemArg(const Elem * elem, bool correct_skewnewss = false) const; 127 : 128 : /** 129 : * Throws error if the functor does not support the requested side integration 130 : * 131 : * @param[in] name Name of functor or functor parameter 132 : * @param[in] qp_integration True if performing qp integration, false if face info 133 : */ 134 : template <typename T> 135 : void checkFunctorSupportsSideIntegration(const std::string & name, bool qp_integration); 136 : 137 : private: 138 : /** 139 : * Retrieves a functor from the passed-in subproblem. This method also leverages the ability to 140 : * create default functors if the user passed an integer or real in the input file 141 : * @param name The actual name of the functor to retrieve instead of the parameter name 142 : * @param subproblem The subproblem to query for the functor 143 : * @param tid The thread ID used to retrieve the functor from the \p subproblem 144 : * @return The functor 145 : */ 146 : template <typename T> 147 : const Moose::Functor<T> & 148 : getFunctorByName(const std::string & name, SubProblem & subproblem, THREAD_ID tid); 149 : 150 : /** 151 : * Whether this interface is for an AD object 152 : */ 153 : virtual bool isADObject() const = 0; 154 : 155 : /** 156 : * Helper function to parse default functor values. This is implemented 157 : * as a specialization for supported types and returns NULL in all other cases. 158 : */ 159 : template <typename T> 160 : const Moose::Functor<T> * defaultFunctor(const std::string & name); 161 : 162 : /// Parameters of the object with this interface 163 : const InputParameters & _fi_params; 164 : 165 : /// The name of the object that this interface belongs to 166 : const std::string _fi_name; 167 : 168 : /// Pointer to subproblem if the subproblem pointer parameter was set 169 : SubProblem * const _fi_subproblem; 170 : 171 : /// Current threaded it 172 : const THREAD_ID _fi_tid; 173 : 174 : /// Storage vector for Moose::Functor<Real> default objects 175 : std::vector<std::unique_ptr<Moose::Functor<Real>>> _default_real_functors; 176 : 177 : /// Storage vector for Moose::Functor<ADReal> default objects 178 : std::vector<std::unique_ptr<Moose::Functor<ADReal>>> _default_ad_real_functors; 179 : }; 180 : 181 : template <typename T> 182 : const Moose::Functor<T> & 183 36397 : FunctorInterface::getFunctor(const std::string & name, SubProblem & subproblem, const THREAD_ID tid) 184 : { 185 : // Check if the supplied parameter is a valid input parameter key 186 36397 : std::string functor_name = deduceFunctorName(name); 187 72790 : return getFunctorByName<T>(functor_name, subproblem, tid); 188 36393 : } 189 : 190 : template <typename T> 191 : const Moose::Functor<T> & 192 : FunctorInterface::getFunctor(const std::string & name, SubProblem & subproblem) 193 : { 194 : return getFunctor<T>(name, subproblem, _fi_tid); 195 : } 196 : 197 : template <typename T> 198 : const Moose::Functor<T> & 199 : FunctorInterface::getFunctor(const std::string & name, const THREAD_ID tid) 200 : { 201 : mooseAssert(_fi_subproblem, "This must be non-null"); 202 : return getFunctor<T>(name, *_fi_subproblem, tid); 203 : } 204 : 205 : template <typename T> 206 : const Moose::Functor<T> & 207 36397 : FunctorInterface::getFunctor(const std::string & name) 208 : { 209 : mooseAssert(_fi_subproblem, "This must be non-null"); 210 36397 : return getFunctor<T>(name, *_fi_subproblem, _fi_tid); 211 : } 212 : 213 : template <typename T> 214 : const Moose::Functor<T> & 215 36397 : FunctorInterface::getFunctorByName(const std::string & name, 216 : SubProblem & subproblem, 217 : const THREAD_ID tid) 218 : { 219 : // Check if it's just a constant 220 36397 : const auto * const default_functor = defaultFunctor<T>(name); 221 36397 : if (default_functor) 222 12704 : return *default_functor; 223 : 224 23693 : return subproblem.getFunctor<T>(name, tid, _fi_name, isADObject()); 225 : } 226 : 227 : template <> 228 : const Moose::Functor<Real> * FunctorInterface::defaultFunctor<Real>(const std::string & name); 229 : 230 : template <> 231 : const Moose::Functor<ADReal> * FunctorInterface::defaultFunctor<ADReal>(const std::string & name); 232 : 233 : // General version for types that do not accept default values 234 : template <typename T> 235 : const Moose::Functor<T> * 236 959 : FunctorInterface::defaultFunctor(const std::string & /*name*/) 237 : { 238 959 : return nullptr; 239 : } 240 : 241 : template <typename T> 242 : void 243 2066 : FunctorInterface::checkFunctorSupportsSideIntegration(const std::string & name, bool qp_integration) 244 : { 245 2066 : const std::string functor_name = deduceFunctorName(name); 246 2066 : const auto & functor = getFunctor<T>(name); 247 2066 : if (qp_integration) 248 : { 249 580 : if (!functor.supportsElemSideQpArg()) 250 0 : mooseError("Quadrature point integration was requested, but the functor '", 251 : functor_name, 252 : "' does not support this."); 253 : } 254 : else 255 : { 256 1486 : if (!functor.supportsFaceArg()) 257 0 : mooseError("Face info integration was requested, but the functor '", 258 : functor_name, 259 : "' does not support this."); 260 : } 261 2066 : }