LCOV - code coverage report
Current view: top level - include/kokkos/bcs - KokkosNodalBC.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31782 (615931) with base 7edd10 Lines: 32 37 86.5 %
Date: 2025-11-11 23:21:15 Functions: 19 22 86.4 %
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             : #include "KokkosNodalBCBase.h"
      13             : 
      14             : namespace Moose
      15             : {
      16             : namespace Kokkos
      17             : {
      18             : 
      19             : /**
      20             :  * The base class for a user to derive their own Kokkos nodal boundary conditions.
      21             :  *
      22             :  * The user should define computeQpResidual(), computeQpJacobian(), and computeQpOffDiagJacobian()
      23             :  * as inlined public methods in their derived class (not virtual override). The signature of
      24             :  * computeQpResidual() expected to be defined in the derived class is as follows:
      25             :  *
      26             :  * @param qp The dummy quadrature point index (= 0)
      27             :  * @param datum The AssemblyDatum object of the current thread
      28             :  * @returns The residual contribution
      29             :  *
      30             :  * KOKKOS_FUNCTION Real computeQpResidual(const unsigned int qp, AssemblyDatum & datum) const;
      31             :  *
      32             :  * The signatures of computeQpJacobian() and computeOffDiagQpJacobian() can be found in the code
      33             :  * below, and their definition in the derived class is optional. If they are defined in the derived
      34             :  * class, they will hide the default definitions in the base class.
      35             :  */
      36             : class NodalBC : public NodalBCBase
      37             : {
      38             : public:
      39             :   static InputParameters validParams();
      40             : 
      41             :   /**
      42             :    * Constructor
      43             :    */
      44             :   NodalBC(const InputParameters & parameters);
      45             : 
      46             :   /**
      47             :    * Dispatch residual calculation
      48             :    */
      49             :   virtual void computeResidual() override;
      50             :   /**
      51             :    * Dispatch diagonal and off-diagonal Jacobian calculation
      52             :    */
      53             :   virtual void computeJacobian() override;
      54             : 
      55             :   /**
      56             :    * Default methods to prevent compile errors even when these methods were not defined in the
      57             :    * derived class
      58             :    */
      59             :   ///@{
      60             :   /**
      61             :    * Compute diagonal Jacobian contribution on a node
      62             :    * @param qp The dummy quadrature point index (= 0)
      63             :    * @param datum The AssemblyDatum object of the current thread
      64             :    * @returns The diagonal Jacobian contribution
      65             :    */
      66       47174 :   KOKKOS_FUNCTION Real computeQpJacobian(const unsigned int /* qp */,
      67             :                                          AssemblyDatum & /* datum */) const
      68             :   {
      69       47174 :     return 1;
      70             :   }
      71             :   /**
      72             :    * Compute off-diagonal Jacobian contribution on a node
      73             :    * @param jvar The variable number for column
      74             :    * @param qp The dummy quadrature point index (= 0)
      75             :    * @param datum The AssemblyDatum object of the current thread
      76             :    * @returns The off-diagonal Jacobian contribution
      77             :    */
      78           0 :   KOKKOS_FUNCTION Real computeQpOffDiagJacobian(const unsigned int /* jvar */,
      79             :                                                 const unsigned int /* qp */,
      80             :                                                 AssemblyDatum & /* datum */) const
      81             :   {
      82           0 :     return 0;
      83             :   }
      84             :   /**
      85             :    * Get the function pointer of the default computeQpJacobian()
      86             :    * @returns The function pointer
      87             :    */
      88      112937 :   static auto defaultJacobian() { return &NodalBC::computeQpJacobian; }
      89             :   /**
      90             :    * Get the function pointer of the default computeQpOffDiagJacobian()
      91             :    * @returns The function pointer
      92             :    */
      93      112937 :   static auto defaultOffDiagJacobian() { return &NodalBC::computeQpOffDiagJacobian; }
      94             :   ///@}
      95             : 
      96             :   /**
      97             :    * Shims for hook methods that can be leveraged to implement static polymorphism
      98             :    */
      99             :   ///{@
     100             :   template <typename Derived>
     101      331136 :   KOKKOS_FUNCTION Real computeQpResidualShim(const Derived & bc,
     102             :                                              const unsigned int qp,
     103             :                                              AssemblyDatum & datum) const
     104             :   {
     105      331136 :     return bc.computeQpResidual(qp, datum);
     106             :   }
     107             :   template <typename Derived>
     108       47570 :   KOKKOS_FUNCTION Real computeQpJacobianShim(const Derived & bc,
     109             :                                              const unsigned int qp,
     110             :                                              AssemblyDatum & datum) const
     111             :   {
     112       47570 :     return bc.computeQpJacobian(qp, datum);
     113             :   }
     114             :   template <typename Derived>
     115         414 :   KOKKOS_FUNCTION Real computeQpOffDiagJacobianShim(const Derived & bc,
     116             :                                                     const unsigned int jvar,
     117             :                                                     const unsigned int qp,
     118             :                                                     AssemblyDatum & datum) const
     119             :   {
     120         414 :     return bc.computeQpOffDiagJacobian(jvar, qp, datum);
     121             :   }
     122             :   ///@}
     123             : 
     124             :   /**
     125             :    * The parallel computation entry functions called by Kokkos
     126             :    */
     127             :   ///@{
     128             :   template <typename Derived>
     129             :   KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived & bc) const;
     130             :   template <typename Derived>
     131             :   KOKKOS_FUNCTION void operator()(JacobianLoop, const ThreadID tid, const Derived & bc) const;
     132             :   template <typename Derived>
     133             :   KOKKOS_FUNCTION void
     134             :   operator()(OffDiagJacobianLoop, const ThreadID tid, const Derived & bc) const;
     135             :   ///@}
     136             : 
     137             : protected:
     138             :   /**
     139             :    * Current solution at nodes
     140             :    */
     141             :   const VariableValue _u;
     142             : };
     143             : 
     144             : template <typename Derived>
     145             : KOKKOS_FUNCTION void
     146      331136 : NodalBC::operator()(ResidualLoop, const ThreadID tid, const Derived & bc) const
     147             : {
     148      331136 :   auto node = kokkosBoundaryNodeID(tid);
     149      331136 :   auto & sys = kokkosSystem(_kokkos_var.sys());
     150             : 
     151      331136 :   if (!sys.isNodalDefined(node, _kokkos_var.var()))
     152           0 :     return;
     153             : 
     154      331136 :   AssemblyDatum datum(node, kokkosAssembly(), kokkosSystems(), _kokkos_var, _kokkos_var.var());
     155             : 
     156      331136 :   Real local_re = bc.computeQpResidualShim(bc, 0, datum);
     157             : 
     158      331136 :   accumulateTaggedNodalResidual(false, local_re, node);
     159             : }
     160             : 
     161             : template <typename Derived>
     162             : KOKKOS_FUNCTION void
     163       47570 : NodalBC::operator()(JacobianLoop, const ThreadID tid, const Derived & bc) const
     164             : {
     165       47570 :   auto node = kokkosBoundaryNodeID(tid);
     166       47570 :   auto & sys = kokkosSystem(_kokkos_var.sys());
     167             : 
     168       47570 :   if (!sys.isNodalDefined(node, _kokkos_var.var()))
     169           0 :     return;
     170             : 
     171       47570 :   AssemblyDatum datum(node, kokkosAssembly(), kokkosSystems(), _kokkos_var, _kokkos_var.var());
     172             : 
     173       47570 :   Real local_ke = bc.computeQpJacobianShim(bc, 0, datum);
     174             : 
     175             :   // This initializes the row to zero except the diagonal
     176       47570 :   accumulateTaggedNodalMatrix(false, local_ke, node, _kokkos_var.var());
     177             : }
     178             : 
     179             : template <typename Derived>
     180             : KOKKOS_FUNCTION void
     181         414 : NodalBC::operator()(OffDiagJacobianLoop, const ThreadID tid, const Derived & bc) const
     182             : {
     183         414 :   auto node = kokkosBoundaryNodeID(_thread(tid, 1));
     184         414 :   auto & sys = kokkosSystem(_kokkos_var.sys());
     185         414 :   auto jvar = sys.getCoupling(_kokkos_var.var())[_thread(tid, 0)];
     186             : 
     187         414 :   if (!sys.isNodalDefined(node, _kokkos_var.var()))
     188           0 :     return;
     189             : 
     190         414 :   AssemblyDatum datum(node, kokkosAssembly(), kokkosSystems(), _kokkos_var, jvar);
     191             : 
     192         414 :   Real local_ke = bc.computeQpOffDiagJacobianShim(bc, jvar, 0, datum);
     193             : 
     194         414 :   accumulateTaggedNodalMatrix(true, local_ke, node, jvar);
     195             : }
     196             : 
     197             : } // namespace Kokkos
     198             : } // namespace Moose

Generated by: LCOV version 1.14