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

Generated by: LCOV version 1.14