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 : /** 138 : * Retrieves a functor from the subproblem. This method also leverages the ability to create 139 : * default functors if the user passed an integer or real in the input file 140 : * @param name The name of the functor to retrieve. This should match the actual name of the 141 : * functor created in the input file 142 : * @return The functor 143 : */ 144 : template <typename T> 145 : const Moose::Functor<T> & getFunctorByName(const std::string & name); 146 : 147 : private: 148 : /** 149 : * Retrieves a functor from the passed-in subproblem. This method also leverages the ability to 150 : * create default functors if the user passed an integer or real in the input file 151 : * @param name The actual name of the functor to retrieve instead of the parameter name 152 : * @param subproblem The subproblem to query for the functor 153 : * @param tid The thread ID used to retrieve the functor from the \p subproblem 154 : * @return The functor 155 : */ 156 : template <typename T> 157 : const Moose::Functor<T> & 158 : getFunctorByName(const std::string & name, SubProblem & subproblem, THREAD_ID tid); 159 : 160 : /** 161 : * Whether this interface is for an AD object 162 : */ 163 : virtual bool isADObject() const = 0; 164 : 165 : /** 166 : * Helper function to parse default functor values. This is implemented 167 : * as a specialization for supported types and returns NULL in all other cases. 168 : */ 169 : template <typename T> 170 : const Moose::Functor<T> * defaultFunctor(const std::string & name); 171 : 172 : /// Parameters of the object with this interface 173 : const InputParameters & _fi_params; 174 : 175 : /// The name of the object that this interface belongs to 176 : const std::string _fi_name; 177 : 178 : /// Pointer to subproblem if the subproblem pointer parameter was set 179 : SubProblem * const _fi_subproblem; 180 : 181 : /// Current threaded it 182 : const THREAD_ID _fi_tid; 183 : 184 : /// Storage vector for Moose::Functor<Real> default objects 185 : std::vector<std::unique_ptr<Moose::Functor<Real>>> _default_real_functors; 186 : 187 : /// Storage vector for Moose::Functor<ADReal> default objects 188 : std::vector<std::unique_ptr<Moose::Functor<ADReal>>> _default_ad_real_functors; 189 : }; 190 : 191 : template <typename T> 192 : const Moose::Functor<T> & 193 28820 : FunctorInterface::getFunctor(const std::string & name, SubProblem & subproblem, const THREAD_ID tid) 194 : { 195 : // Check if the supplied parameter is a valid input parameter key 196 28820 : std::string functor_name = deduceFunctorName(name); 197 57637 : return getFunctorByName<T>(functor_name, subproblem, tid); 198 28817 : } 199 : 200 : template <typename T> 201 : const Moose::Functor<T> & 202 : FunctorInterface::getFunctor(const std::string & name, SubProblem & subproblem) 203 : { 204 : return getFunctor<T>(name, subproblem, _fi_tid); 205 : } 206 : 207 : template <typename T> 208 : const Moose::Functor<T> & 209 : FunctorInterface::getFunctor(const std::string & name, const THREAD_ID tid) 210 : { 211 : mooseAssert(_fi_subproblem, "This must be non-null"); 212 : return getFunctor<T>(name, *_fi_subproblem, tid); 213 : } 214 : 215 : template <typename T> 216 : const Moose::Functor<T> & 217 28820 : FunctorInterface::getFunctor(const std::string & name) 218 : { 219 : mooseAssert(_fi_subproblem, "This must be non-null"); 220 28820 : return getFunctor<T>(name, *_fi_subproblem, _fi_tid); 221 : } 222 : 223 : template <typename T> 224 : const Moose::Functor<T> & 225 : FunctorInterface::getFunctorByName(const std::string & name) 226 : { 227 : mooseAssert(_fi_subproblem, "This must be non-null"); 228 : return getFunctorByName<T>(name, *_fi_subproblem, _fi_tid); 229 : } 230 : 231 : template <typename T> 232 : const Moose::Functor<T> & 233 28820 : FunctorInterface::getFunctorByName(const std::string & name, 234 : SubProblem & subproblem, 235 : const THREAD_ID tid) 236 : { 237 : // Check if it's just a constant 238 28820 : const auto * const default_functor = defaultFunctor<T>(name); 239 28820 : if (default_functor) 240 11034 : return *default_functor; 241 : 242 17786 : return subproblem.getFunctor<T>(name, tid, _fi_name, isADObject()); 243 : } 244 : 245 : template <> 246 : const Moose::Functor<Real> * FunctorInterface::defaultFunctor<Real>(const std::string & name); 247 : 248 : template <> 249 : const Moose::Functor<ADReal> * FunctorInterface::defaultFunctor<ADReal>(const std::string & name); 250 : 251 : // General version for types that do not accept default values 252 : template <typename T> 253 : const Moose::Functor<T> * 254 829 : FunctorInterface::defaultFunctor(const std::string & /*name*/) 255 : { 256 829 : return nullptr; 257 : } 258 : 259 : template <typename T> 260 : void 261 1964 : FunctorInterface::checkFunctorSupportsSideIntegration(const std::string & name, bool qp_integration) 262 : { 263 1964 : const std::string functor_name = deduceFunctorName(name); 264 1964 : const auto & functor = getFunctor<T>(name); 265 1964 : if (qp_integration) 266 : { 267 588 : if (!functor.supportsElemSideQpArg()) 268 0 : mooseError("Quadrature point integration was requested, but the functor '", 269 : functor_name, 270 : "' does not support this."); 271 : } 272 : else 273 : { 274 1376 : if (!functor.supportsFaceArg()) 275 0 : mooseError("Face info integration was requested, but the functor '", 276 : functor_name, 277 : "' does not support this."); 278 : } 279 1964 : }