LCOV - code coverage report
Current view: top level - include/kokkos/base - KokkosVariableValue.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31653 (2d163b) with base 0cc44f Lines: 60 67 89.6 %
Date: 2025-11-04 20:38:02 Functions: 11 12 91.7 %
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 "KokkosDatum.h"
      13             : 
      14             : #include "MooseVariableBase.h"
      15             : 
      16             : namespace Moose
      17             : {
      18             : namespace Kokkos
      19             : {
      20             : 
      21             : /**
      22             :  * The Kokkos wrapper classes for MOOSE-like shape function access
      23             :  */
      24             : ///@{
      25             : class VariablePhiValue
      26             : {
      27             : public:
      28             :   /**
      29             :    * Get the current shape function
      30             :    * @param datum The AssemblyDatum object of the current thread
      31             :    * @param i The element-local DOF index
      32             :    * @param qp The local quadrature point index
      33             :    * @returns The shape function
      34             :    */
      35    20264240 :   KOKKOS_FUNCTION Real operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
      36             :   {
      37    20264240 :     auto & elem = datum.elem();
      38    20264240 :     auto side = datum.side();
      39    20264240 :     auto fe = datum.jfe();
      40             : 
      41           0 :     return side == libMesh::invalid_uint
      42    20264240 :                ? datum.assembly().getPhi(elem.subdomain, elem.type, fe)(i, qp)
      43    20264240 :                : datum.assembly().getPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp);
      44             :   }
      45             : };
      46             : 
      47             : class VariablePhiGradient
      48             : {
      49             : public:
      50             :   /**
      51             :    * Get the gradient of the current shape function
      52             :    * @param datum The AssemblyDatum object of the current thread
      53             :    * @param i The element-local DOF index
      54             :    * @param qp The local quadrature point index
      55             :    * @returns The gradient of the shape function
      56             :    */
      57    22396352 :   KOKKOS_FUNCTION Real3 operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
      58             :   {
      59    22396352 :     auto & elem = datum.elem();
      60    22396352 :     auto side = datum.side();
      61    22396352 :     auto fe = datum.jfe();
      62             : 
      63           0 :     return datum.J(qp) *
      64             :            (side == libMesh::invalid_uint
      65    22396352 :                 ? datum.assembly().getGradPhi(elem.subdomain, elem.type, fe)(i, qp)
      66    44792704 :                 : datum.assembly().getGradPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp));
      67             :   }
      68             : };
      69             : 
      70             : class VariableTestValue
      71             : {
      72             : public:
      73             :   /**
      74             :    * Get the current test function
      75             :    * @param datum The AssemblyDatum object of the current thread
      76             :    * @param i The element-local DOF index
      77             :    * @param qp The local quadrature point index
      78             :    * @returns The test function
      79             :    */
      80   104915244 :   KOKKOS_FUNCTION Real operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
      81             :   {
      82   104915244 :     auto & elem = datum.elem();
      83   104915244 :     auto side = datum.side();
      84   104915244 :     auto fe = datum.ife();
      85             : 
      86           0 :     return side == libMesh::invalid_uint
      87   104915244 :                ? datum.assembly().getPhi(elem.subdomain, elem.type, fe)(i, qp)
      88   104915244 :                : datum.assembly().getPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp);
      89             :   }
      90             : };
      91             : 
      92             : class VariableTestGradient
      93             : {
      94             : public:
      95             :   /**
      96             :    * Get the gradient of the current test function
      97             :    * @param datum The AssemblyDatum object of the current thread
      98             :    * @param i The element-local DOF index
      99             :    * @param qp The local quadrature point index
     100             :    * @returns The gradient of the test function
     101             :    */
     102    57181232 :   KOKKOS_FUNCTION Real3 operator()(AssemblyDatum & datum, unsigned int i, unsigned int qp) const
     103             :   {
     104    57181232 :     auto & elem = datum.elem();
     105    57181232 :     auto side = datum.side();
     106    57181232 :     auto fe = datum.ife();
     107             : 
     108           0 :     return datum.J(qp) *
     109             :            (side == libMesh::invalid_uint
     110    57181232 :                 ? datum.assembly().getGradPhi(elem.subdomain, elem.type, fe)(i, qp)
     111   114362464 :                 : datum.assembly().getGradPhiFace(elem.subdomain, elem.type, fe)(side)(i, qp));
     112             :   }
     113             : };
     114             : ///@}
     115             : 
     116             : /**
     117             :  * The Kokkos wrapper classes for MOOSE-like variable value access
     118             :  */
     119             : ///@{
     120             : class VariableValue
     121             : {
     122             : public:
     123             :   /**
     124             :    * Default constructor
     125             :    */
     126          72 :   VariableValue() = default;
     127             :   /**
     128             :    * Constructor
     129             :    * @param var The Kokkos variable
     130             :    * @param dof Whether to get DOF values
     131             :    */
     132         901 :   VariableValue(Variable var, bool dof = false) : _var(var), _dof(dof) {}
     133             :   /**
     134             :    * Constructor
     135             :    * @param var The MOOSE variable
     136             :    * @param tag The vector tag name
     137             :    * @param dof Whether to get DOF values
     138             :    */
     139        4041 :   VariableValue(const MooseVariableBase & var,
     140             :                 const TagName & tag = Moose::SOLUTION_TAG,
     141         886 :                 bool dof = false)
     142        3155 :     : _var(var, tag), _dof(dof)
     143             :   {
     144        4041 :   }
     145             : 
     146             :   /**
     147             :    * Get whether the variable was coupled
     148             :    * @returns Whether the variable was coupled
     149             :    */
     150       28032 :   KOKKOS_FUNCTION operator bool() const { return _var.coupled(); }
     151             : 
     152             :   /**
     153             :    * Get the current variable value
     154             :    * @param datum The Datum object of the current thread
     155             :    * @param qp The local quadrature point index
     156             :    * @param comp The variable component
     157             :    * @returns The variable value
     158             :    */
     159             :   KOKKOS_FUNCTION Real operator()(Datum & datum, unsigned int qp, unsigned int comp = 0) const;
     160             : 
     161             : private:
     162             :   /**
     163             :    * Coupled Kokkos variable
     164             :    */
     165             :   Variable _var;
     166             :   /**
     167             :    * Flag whether DOF values are requested
     168             :    */
     169             :   bool _dof = false;
     170             : };
     171             : 
     172             : class VariableGradient
     173             : {
     174             : public:
     175             :   /**
     176             :    * Default constructor
     177             :    */
     178             :   VariableGradient() = default;
     179             :   /**
     180             :    * Constructor
     181             :    * @param var The Kokkos variable
     182             :    */
     183           0 :   VariableGradient(Variable var) : _var(var) {}
     184             :   /**
     185             :    * Constructor
     186             :    * @param var The MOOSE variable
     187             :    * @param tag The vector tag name
     188             :    */
     189        1738 :   VariableGradient(const MooseVariableBase & var, const TagName & tag = Moose::SOLUTION_TAG)
     190        1368 :     : _var(var, tag)
     191             :   {
     192        1738 :   }
     193             : 
     194             :   /**
     195             :    * Get whether the variable was coupled
     196             :    * @returns Whether the variable was coupled
     197             :    */
     198             :   KOKKOS_FUNCTION operator bool() const { return _var.coupled(); }
     199             : 
     200             :   /**
     201             :    * Get the current variable gradient
     202             :    * @param datum The Datum object of the current thread
     203             :    * @param idx The local quadrature point or DOF index
     204             :    * @param comp The variable component
     205             :    * @returns The variable gradient
     206             :    */
     207             :   KOKKOS_FUNCTION Real3 operator()(Datum & datum, unsigned int idx, unsigned int comp = 0) const;
     208             : 
     209             : private:
     210             :   /**
     211             :    * Coupled Kokkos variable
     212             :    */
     213             :   Variable _var;
     214             : };
     215             : ///@}
     216             : 
     217             : KOKKOS_FUNCTION inline Real
     218    34545797 : VariableValue::operator()(Datum & datum, unsigned int idx, unsigned int comp) const
     219             : {
     220             :   KOKKOS_ASSERT(_var.initialized());
     221             : 
     222    34545797 :   if (_var.coupled())
     223             :   {
     224    34530561 :     auto & sys = datum.system(_var.sys(comp));
     225    34530561 :     auto var = _var.var(comp);
     226    34530561 :     auto tag = _var.tag();
     227             : 
     228    34530561 :     if (_dof)
     229             :     {
     230             :       unsigned int dof;
     231             : 
     232     3885117 :       if (datum.isNodal())
     233             :       {
     234     3884517 :         auto node = datum.node();
     235     3884517 :         dof = sys.getNodeLocalDofIndex(node, 0, var);
     236             :       }
     237             :       else
     238             :       {
     239         600 :         auto elem = datum.elem().id;
     240         600 :         dof = sys.getElemLocalDofIndex(elem, idx, var);
     241             :       }
     242             : 
     243     3885117 :       return sys.getVectorDofValue(dof, tag);
     244             :     }
     245             :     else
     246             :     {
     247    30645444 :       auto & elem = datum.elem();
     248    30645444 :       auto side = datum.side();
     249    30645444 :       auto offset = datum.qpOffset();
     250             : 
     251    30645444 :       return side == libMesh::invalid_uint ? sys.getVectorQpValue(elem, offset + idx, var, tag)
     252    30645444 :                                            : sys.getVectorQpValueFace(elem, side, idx, var, tag);
     253             :     }
     254             :   }
     255             :   else
     256       15236 :     return _var.value(comp);
     257             : }
     258             : 
     259             : KOKKOS_FUNCTION inline Real3
     260    34783344 : VariableGradient::operator()(Datum & datum, unsigned int qp, unsigned int comp) const
     261             : {
     262             :   KOKKOS_ASSERT(_var.initialized());
     263             : 
     264    34783344 :   if (_var.coupled())
     265             :   {
     266             :     KOKKOS_ASSERT(!datum.isNodal());
     267             : 
     268    34783344 :     auto & elem = datum.elem();
     269    34783344 :     auto side = datum.side();
     270    34783344 :     auto offset = datum.qpOffset();
     271             : 
     272             :     return side == libMesh::invalid_uint
     273    34783344 :                ? datum.system(_var.sys(comp))
     274    34783344 :                      .getVectorQpGrad(elem, offset + qp, _var.var(comp), _var.tag())
     275           0 :                : datum.system(_var.sys(comp))
     276    34783344 :                      .getVectorQpGradFace(elem, side, datum.J(qp), qp, _var.var(comp), _var.tag());
     277             :   }
     278             :   else
     279           0 :     return Real3(0);
     280             : }
     281             : 
     282             : } // namespace Kokkos
     283             : } // namespace Moose

Generated by: LCOV version 1.14