LCOV - code coverage report
Current view: top level - include/functions - FunctionInterface.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 7edd10 Lines: 7 7 100.0 %
Date: 2025-11-11 08:32:45 Functions: 2 2 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 "MooseTypes.h"
      13             : #include "MooseObject.h"
      14             : #include "InputParameters.h"
      15             : 
      16             : #define usingFunctionInterfaceMembers                                                              \
      17             :   using FunctionInterface::getFunction;                                                            \
      18             :   using FunctionInterface::getFunctionByName
      19             : 
      20             : // Forward declarations
      21             : class FEProblemBase;
      22             : class Function;
      23             : class InputParameters;
      24             : 
      25             : namespace Moose
      26             : {
      27             : class FunctionBase;
      28             : }
      29             : 
      30             : #ifdef MOOSE_KOKKOS_ENABLED
      31             : namespace Moose::Kokkos
      32             : {
      33             : class Function;
      34             : }
      35             : #endif
      36             : 
      37             : template <typename T>
      38             : InputParameters validParams();
      39             : 
      40             : /**
      41             :  * Interface for objects that need to use functions
      42             :  *
      43             :  * Inherit from this class at a very low level to make the getFunction method
      44             :  * available.
      45             :  */
      46             : class FunctionInterface
      47             : {
      48             : public:
      49             :   FunctionInterface(const MooseObject * moose_object);
      50             : 
      51             : #ifdef MOOSE_KOKKOS_ENABLED
      52             :   /**
      53             :    * Special constructor used for Kokkos functor copy during parallel dispatch
      54             :    */
      55             :   FunctionInterface(const FunctionInterface & object, const Moose::Kokkos::FunctorCopy & key);
      56             : #endif
      57             : 
      58             :   static InputParameters validParams();
      59             : 
      60             :   /**
      61             :    * Get a function with a given name
      62             :    * @param name The name of the parameter key of the function to retrieve
      63             :    * @return The function with name associated with the parameter 'name'
      64             :    */
      65             :   const Function & getFunction(const std::string & name) const;
      66             : 
      67             :   /**
      68             :    * Get a function with a given name
      69             :    * @param name The name of the function to retrieve
      70             :    * @return The function with name 'name'
      71             :    */
      72             :   const Function & getFunctionByName(const FunctionName & name) const;
      73             : 
      74             :   /**
      75             :    * Determine if the function exists
      76             :    * @param param_name The name of the function parameter
      77             :    * @param index The index of the function
      78             :    * @return True if the function exists
      79             :    */
      80             :   bool hasFunction(const std::string & param_name) const;
      81             : 
      82             :   /**
      83             :    * Determine if the function exists
      84             :    * @param name The name of the function
      85             :    * @return True if the function exists
      86             :    */
      87             :   bool hasFunctionByName(const FunctionName & name) const;
      88             : 
      89             : #ifdef MOOSE_KOKKOS_ENABLED
      90             :   /**
      91             :    * Get a Kokkos function of an abstract type with a given name
      92             :    * Calling this function will error out currently if Kokkos was configured with GPU
      93             :    * @param name The name of the parameter key of the Kokkos function to retrieve
      94             :    * @return The copy of the Kokkos function of the abstract type with name associated with the
      95             :    * parameter 'name'
      96             :    */
      97             :   Moose::Kokkos::Function getKokkosFunction(const std::string & name) const;
      98             : 
      99             :   /**
     100             :    * Get a Kokkos function of an abstract type with a given name
     101             :    * Calling this function will error out currently if Kokkos was configured with GPU
     102             :    * @param name The name of the Kokkos function to retrieve
     103             :    * @return The copy of the Kokkos function of the abstract type with name 'name'
     104             :    */
     105             :   Moose::Kokkos::Function getKokkosFunctionByName(const FunctionName & name) const;
     106             : 
     107             :   /**
     108             :    * Get a Kokkos function of a concrete type with a given name
     109             :    * @tparam T The Kokkos function type
     110             :    * @param name The name of the parameter key of the Kokkos function to retrieve
     111             :    * @return The reference of the Kokkos function of the concrete type with name associated with the
     112             :    * parameter 'name'. Always store it in a reference wrapper if to be used on GPU.
     113             :    */
     114             :   template <typename T>
     115             :   const T & getKokkosFunction(const std::string & name) const;
     116             : 
     117             :   /**
     118             :    * Get a Kokkos function of a concrete type with a given name
     119             :    * @tparam T The Kokkos function type
     120             :    * @param name The name of the Kokkos function to retrieve
     121             :    * @return The reference of the Kokkos function of the concrete type with name 'name'. Always
     122             :    * store it in a reference wrapper if to be used on GPU.
     123             :    */
     124             :   template <typename T>
     125             :   const T & getKokkosFunctionByName(const FunctionName & name) const;
     126             : 
     127             :   /**
     128             :    * Determine if the Kokkos function exists
     129             :    * @param param_name The name of the Kokkos function parameter
     130             :    * @param index The index of the Kokkos function
     131             :    * @return True if the Kokkos function exists
     132             :    */
     133             :   bool hasKokkosFunction(const std::string & param_name) const;
     134             : 
     135             :   /**
     136             :    * Determine if the Kokkos function exists
     137             :    * @param name The name of the Kokkos function
     138             :    * @return True if the Kokkos function exists
     139             :    */
     140             :   bool hasKokkosFunctionByName(const FunctionName & name) const;
     141             : #endif
     142             : 
     143             : private:
     144             : #ifdef MOOSE_KOKKOS_ENABLED
     145             :   /// Helper function to retrieve a Kokkos function
     146             :   const Moose::FunctionBase * getKokkosFunctionByNameHelper(const FunctionName & name) const;
     147             : #endif
     148             : 
     149             :   /// The object
     150             :   const MooseObject & _fni_object;
     151             : 
     152             :   /// Parameters of the object with this interface
     153             :   const InputParameters & _fni_params;
     154             : 
     155             :   /// Reference to FEProblemBase instance
     156             :   FEProblemBase & _fni_feproblem;
     157             : 
     158             :   /// Thread ID
     159             :   const THREAD_ID _fni_tid;
     160             : };
     161             : 
     162             : #ifdef MOOSE_KOKKOS_ENABLED
     163             : template <typename T>
     164             : const T &
     165          14 : FunctionInterface::getKokkosFunction(const std::string & name) const
     166             : {
     167          14 :   return getKokkosFunctionByName<T>(_fni_params.get<FunctionName>(name));
     168             : }
     169             : 
     170             : template <typename T>
     171             : const T &
     172          14 : FunctionInterface::getKokkosFunctionByName(const FunctionName & name) const
     173             : {
     174          14 :   auto function = dynamic_cast<const T *>(getKokkosFunctionByNameHelper(name));
     175             : 
     176          14 :   if (!function)
     177           2 :     _fni_object.mooseError(
     178             :         "Kokkos function '", name, "' is not of type '", MooseUtils::prettyCppType<T>(), "'");
     179             : 
     180          12 :   return *function;
     181             : }
     182             : #endif

Generated by: LCOV version 1.14