LCOV - code coverage report
Current view: top level - include/kokkos/materials - KokkosMaterial.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31761 (28487c) with base 701993 Lines: 59 62 95.2 %
Date: 2025-11-11 13:51:07 Functions: 31 121 25.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://www.mooseframework.org
       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             : // initQpStatefulProperties() and computeQpProperties() are intentionally hidden
      13             : // but some compilers generate ugly warnings
      14             : 
      15             : #if defined(__clang__)
      16             : #pragma clang diagnostic push
      17             : #pragma clang diagnostic ignored "-Woverloaded-virtual"
      18             : #elif defined(__GNUC__) || defined(__GNUG__)
      19             : #pragma GCC diagnostic push
      20             : #pragma GCC diagnostic ignored "-Woverloaded-virtual"
      21             : #endif
      22             : 
      23             : #include "KokkosMaterialBase.h"
      24             : #include "KokkosDatum.h"
      25             : 
      26             : #include "Coupleable.h"
      27             : #include "MaterialPropertyInterface.h"
      28             : 
      29             : namespace Moose
      30             : {
      31             : namespace Kokkos
      32             : {
      33             : 
      34             : /**
      35             :  * The base class for a user to derive their own Kokkos materials.
      36             :  *
      37             :  * The user should define initQpStatefulProperties() and computeQpProperties() as inlined public
      38             :  * methods in their derived class (not virtual override). The signature of computeQpProperties()
      39             :  * expected to be defined in the derived class is as follows:
      40             :  *
      41             :  * @param qp The local quadrature point index
      42             :  * @param datum The Datum object of the current thread
      43             :  *
      44             :  * KOKKOS_FUNCTION void computeQpProperties(const unsigned int qp, Datum & datum) const;
      45             :  *
      46             :  * The signature of initQpStatefulProperties() can be found in the code below, and its definition in
      47             :  * the derived class is optional. If it is defined in the derived class, it will hide the default
      48             :  * definition in the base class.
      49             :  */
      50             : class Material : public MaterialBase, public Coupleable, public MaterialPropertyInterface
      51             : {
      52             : public:
      53             :   static InputParameters validParams();
      54             : 
      55             :   /**
      56             :    * Constructor
      57             :    */
      58             :   Material(const InputParameters & parameters);
      59             : 
      60             :   /**
      61             :    * Copy constructor for parallel dispatch
      62             :    */
      63             :   Material(const Material & object);
      64             : 
      65             :   /**
      66             :    * Dispatch stateful material property initialization
      67             :    */
      68             :   virtual void initStatefulProperties(unsigned int) override;
      69             :   /**
      70             :    * Dispatch material property evaluation
      71             :    */
      72             :   virtual void computeProperties() override;
      73             : 
      74             :   /**
      75             :    * Default methods to prevent compile errors even when these methods were not defined in the
      76             :    * derived class
      77             :    */
      78             :   ///@{
      79             :   /**
      80             :    * Initialize stateful material properties on a quadrature point
      81             :    * @param qp The local quadrature point index
      82             :    * @param datum The Datum object of the current thread
      83             :    */
      84           0 :   KOKKOS_FUNCTION void initQpStatefulProperties(const unsigned int /* qp */,
      85             :                                                 Datum & /* datum */) const
      86             :   {
      87           0 :   }
      88             :   /**
      89             :    * Get the function pointer of the default initQpStatefulProperties()
      90             :    * @returns The function pointer
      91             :    */
      92     1580871 :   static auto defaultInitStateful() { return &Material::initQpStatefulProperties; }
      93             :   ///@}
      94             : 
      95             :   /**
      96             :    * Shims for hook methods that can be leveraged to implement static polymorphism
      97             :    */
      98             :   ///{@
      99             :   template <typename Derived>
     100             :   KOKKOS_FUNCTION void
     101       91712 :   initQpStatefulPropertiesShim(const Derived & material, const unsigned int qp, Datum & datum) const
     102             :   {
     103       91712 :     material.initQpStatefulProperties(qp, datum);
     104       91712 :   }
     105             :   template <typename Derived>
     106             :   KOKKOS_FUNCTION void
     107     5790216 :   computeQpPropertiesShim(const Derived & material, const unsigned int qp, Datum & datum) const
     108             :   {
     109     5790216 :     material.computeQpProperties(qp, datum);
     110     5790216 :   }
     111             :   ///@}
     112             : 
     113             :   /**
     114             :    * The parallel computation entry functions called by Kokkos
     115             :    */
     116             :   ///@{
     117             :   template <typename Derived>
     118             :   KOKKOS_FUNCTION void operator()(ElementInit, const ThreadID tid, const Derived & material) const;
     119             :   template <typename Derived>
     120             :   KOKKOS_FUNCTION void operator()(SideInit, const ThreadID tid, const Derived & material) const;
     121             :   template <typename Derived>
     122             :   KOKKOS_FUNCTION void operator()(NeighborInit, const ThreadID tid, const Derived & material) const;
     123             :   template <typename Derived>
     124             :   KOKKOS_FUNCTION void
     125             :   operator()(ElementCompute, const ThreadID tid, const Derived & material) const;
     126             :   template <typename Derived>
     127             :   KOKKOS_FUNCTION void operator()(SideCompute, const ThreadID tid, const Derived & material) const;
     128             :   template <typename Derived>
     129             :   KOKKOS_FUNCTION void
     130             :   operator()(NeighborCompute, const ThreadID tid, const Derived & material) const;
     131             :   ///@}
     132             : 
     133             : protected:
     134             :   /**
     135             :    * Override of the MaterialPropertyInterface function to perform additional checks and add
     136             :    * dependencies
     137             :    */
     138             :   void getKokkosMaterialPropertyHook(const std::string & prop_name_in,
     139             :                                      const unsigned int state) override final;
     140             : 
     141         871 :   virtual void checkMaterialProperty(const std::string & name, const unsigned int state) override
     142             :   {
     143             :     // Avoid performing duplicate checks for triple block/face/neighbor materials
     144         871 :     if (boundaryRestricted() || !_bnd)
     145         299 :       MaterialPropertyInterface::checkMaterialProperty(name, state);
     146         871 :   }
     147             : 
     148        1027 :   virtual bool isBoundaryMaterial() const override { return _bnd; }
     149             : 
     150       17413 :   virtual const std::unordered_set<unsigned int> & getMatPropDependencies() const override
     151             :   {
     152       17413 :     return MaterialPropertyInterface::getMatPropDependencies();
     153             :   }
     154             : 
     155        1644 :   virtual const MaterialData & materialData() const override { return _material_data; }
     156        2048 :   virtual MaterialData & materialData() override { return _material_data; }
     157         838 :   virtual MaterialDataType materialDataType() override { return _material_data_type; }
     158             : 
     159             :   /**
     160             :    * Flag whether the material is on faces
     161             :    */
     162             :   const bool _bnd;
     163             :   /**
     164             :    * Flag whether the material is on neighbor faces
     165             :    */
     166             :   const bool _neighbor;
     167             : 
     168             : private:
     169             :   /**
     170             :    * Dummy members unused for Kokkos materials
     171             :    */
     172             :   ///@{
     173             :   const QBase * const & _qrule;
     174           0 :   virtual const QBase & qRule() const override { return *_qrule; }
     175             :   ///@}
     176             : };
     177             : 
     178             : template <typename Derived>
     179             : KOKKOS_FUNCTION void
     180       16504 : Material::operator()(ElementInit, const ThreadID tid, const Derived & material) const
     181             : {
     182       16504 :   auto elem = kokkosElementID(tid);
     183             : 
     184       16504 :   Datum datum(elem, libMesh::invalid_uint, kokkosAssembly(), kokkosSystems());
     185             : 
     186      106236 :   for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
     187             :   {
     188       89732 :     datum.reinit();
     189       89732 :     material.initQpStatefulPropertiesShim(material, qp, datum);
     190             :   }
     191       16504 : }
     192             : 
     193             : template <typename Derived>
     194             : KOKKOS_FUNCTION void
     195         410 : Material::operator()(SideInit, const ThreadID tid, const Derived & material) const
     196             : {
     197         410 :   auto [elem, side] = kokkosElementSideID(tid);
     198             : 
     199         410 :   Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
     200             : 
     201        1520 :   for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
     202             :   {
     203        1110 :     datum.reinit();
     204        1110 :     material.initQpStatefulPropertiesShim(material, qp, datum);
     205             :   }
     206         410 : }
     207             : 
     208             : template <typename Derived>
     209             : KOKKOS_FUNCTION void
     210         290 : Material::operator()(NeighborInit, const ThreadID tid, const Derived & material) const
     211             : {
     212         290 :   auto [elem, side] = kokkosElementSideID(tid);
     213             : 
     214         290 :   Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
     215             : 
     216        1160 :   for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
     217             :   {
     218         870 :     datum.reinit();
     219         870 :     material.initQpStatefulPropertiesShim(material, qp, datum);
     220             :   }
     221         290 : }
     222             : 
     223             : template <typename Derived>
     224             : KOKKOS_FUNCTION void
     225      863252 : Material::operator()(ElementCompute, const ThreadID tid, const Derived & material) const
     226             : {
     227      863252 :   auto elem = kokkosElementID(tid);
     228             : 
     229      863252 :   Datum datum(elem, libMesh::invalid_uint, kokkosAssembly(), kokkosSystems());
     230             : 
     231     6439568 :   for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
     232             :   {
     233     5576316 :     datum.reinit();
     234     5576316 :     material.computeQpPropertiesShim(material, qp, datum);
     235             :   }
     236      863252 : }
     237             : 
     238             : template <typename Derived>
     239             : KOKKOS_FUNCTION void
     240       40830 : Material::operator()(SideCompute, const ThreadID tid, const Derived & material) const
     241             : {
     242       40830 :   auto [elem, side] = kokkosElementSideID(tid);
     243             : 
     244       40830 :   Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
     245             : 
     246      152960 :   for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
     247             :   {
     248      112130 :     datum.reinit();
     249      112130 :     material.computeQpPropertiesShim(material, qp, datum);
     250             :   }
     251       40830 : }
     252             : 
     253             : template <typename Derived>
     254             : KOKKOS_FUNCTION void
     255       35650 : Material::operator()(NeighborCompute, const ThreadID tid, const Derived & material) const
     256             : {
     257       35650 :   auto [elem, side] = kokkosElementSideID(tid);
     258             : 
     259       35650 :   Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
     260             : 
     261      137420 :   for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
     262             :   {
     263      101770 :     datum.reinit();
     264      101770 :     material.computeQpPropertiesShim(material, qp, datum);
     265             :   }
     266       35650 : }
     267             : 
     268             : } // namespace Kokkos
     269             : } // namespace Moose

Generated by: LCOV version 1.14