LCOV - code coverage report
Current view: top level - include/utils - JvarMapInterface.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 34 35 97.1 %
Date: 2025-07-17 01:28:37 Functions: 10 10 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 "MooseVariableFE.h"
      13             : #include "NonlinearSystemBase.h"
      14             : #include "Enumerate.h"
      15             : 
      16             : template <class T>
      17             : class JvarMapInterfaceBase;
      18             : 
      19             : /**
      20             :  * Interface class ("Veneer") for Kernel to provide a mapping from 'jvar' in
      21             :  * computeQpOffDiagJacobian into the _coupled_moose_vars array.
      22             :  *
      23             :  * This class is useful in conjunction with DerivativeMaterialInterface,
      24             :  * where vectors of material property derivatives with respect to all coupled
      25             :  * variables (iterating over _coupled_moose_vars array) are generated.
      26             :  * The mapping enabled the look up of the correct material property derivatives
      27             :  * for the current jvar.
      28             :  */
      29             : template <class T>
      30             : class JvarMapKernelInterface : public JvarMapInterfaceBase<T>
      31             : {
      32             : public:
      33             :   JvarMapKernelInterface(const InputParameters & parameters);
      34             :   virtual void computeOffDiagJacobian(unsigned int jvar) override;
      35             : };
      36             : 
      37             : /**
      38             :  * Interface class ("Veneer") for IntegratedBC to provide a mapping from 'jvar' in
      39             :  * computeJacobianBlock into the _coupled_moose_vars array.
      40             :  *
      41             :  * This class is useful in conjunction with DerivativeMaterialInterface,
      42             :  * where vectors of material property derivatives with respect to all coupled
      43             :  * variables (iterating over _coupled_moose_vars array) are generated.
      44             :  * The mapping enabled the look up of the correct material property derivatives
      45             :  * for the current jvar.
      46             :  */
      47             : template <class T>
      48             : class JvarMapIntegratedBCInterface : public JvarMapInterfaceBase<T>
      49             : {
      50             : public:
      51             :   JvarMapIntegratedBCInterface(const InputParameters & parameters);
      52             :   virtual void computeOffDiagJacobian(unsigned int jvar) override;
      53             : };
      54             : 
      55             : /**
      56             :  * Base class ("Veneer") that implements the actual mapping from 'jvar' in
      57             :  * into the _coupled_moose_vars array.
      58             :  */
      59             : template <class T>
      60             : class JvarMapInterfaceBase : public T
      61             : {
      62             : public:
      63             :   typedef std::vector<int> JvarMap;
      64             : 
      65             :   static InputParameters validParams();
      66             : 
      67             :   JvarMapInterfaceBase(const InputParameters & parameters);
      68             : 
      69             :   /// Return index into the _coupled_moose_vars array for a given jvar
      70             :   unsigned int mapJvarToCvar(unsigned int jvar);
      71             : 
      72             :   /**
      73             :    * Return an index into a specific coupled variable vector for a given jvar. A
      74             :    * negative return value indicates that the jvar value does not point to a
      75             :    * variable in the couple variable vector corresponding to the mapped parameter.
      76             :    */
      77             :   int mapJvarToCvar(unsigned int jvar, const JvarMap & jvar_map);
      78             : 
      79             :   /// Obtain the map connecting libmesh variable ID number to its position in the _coupled_moose_vars vector
      80             :   const JvarMap & getJvarMap() { return _jvar_map; }
      81             : 
      82             :   /// Make a specific map for a given parameter name representing a couple variable (vector)
      83             :   const JvarMap & getParameterJvarMap(std::string parameter_name);
      84             : 
      85             :   /**
      86             :    * Set the cvar value to the mapped jvar value and return true if the mapping exists.
      87             :    * Otherwise return false.
      88             :    *
      89             :    * @param[in] jvar Variable number passed as argument to computeQpOffDiagJacobian
      90             :    * @param[out] cvar Corresponding index into the _coupled_moose_vars array
      91             :    * @return true if the requested variable is coupled, false otherwise
      92             :    */
      93             :   bool mapJvarToCvar(unsigned int jvar, unsigned int & cvar);
      94             : 
      95             : protected:
      96             :   /// number of coupled moose variables
      97             :   const unsigned int _n_args;
      98             : 
      99             : private:
     100             :   /// number of nonlinear variables in the system
     101             :   const std::size_t _jvar_max_size;
     102             : 
     103             :   /// look-up table to determine the _coupled_moose_vars index for the jvar parameter
     104             :   JvarMap _jvar_map;
     105             : 
     106             :   /// map of local look-up tables for specific parameters
     107             :   std::map<std::string, JvarMap> _jvar_local_map;
     108             : 
     109             :   friend class JvarMapKernelInterface<T>;
     110             :   friend class JvarMapIntegratedBCInterface<T>;
     111             : };
     112             : 
     113             : template <class T>
     114             : InputParameters
     115       14290 : JvarMapInterfaceBase<T>::validParams()
     116             : {
     117       14290 :   auto params = T::validParams();
     118       14290 :   params.addCoupledVar("args", "Vector of nonlinear variable arguments this object depends on");
     119       14290 :   params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
     120             : 
     121       14290 :   return params;
     122           0 : }
     123             : 
     124             : template <class T>
     125        1397 : JvarMapInterfaceBase<T>::JvarMapInterfaceBase(const InputParameters & parameters)
     126             :   : T(parameters),
     127        2794 :     _n_args(this->_coupled_standard_moose_vars.size()),
     128        1397 :     _jvar_max_size(this->_sys.nVariables()),
     129        2794 :     _jvar_map(_jvar_max_size, -1)
     130             : {
     131             :   // populate map
     132        3336 :   for (auto it : Moose::enumerate(this->_coupled_moose_vars))
     133             :   {
     134         542 :     auto number = it.value()->number();
     135             : 
     136             :     // skip AuxVars as off-diagonal jacobian entries are not calculated for them
     137         542 :     if (number < _jvar_max_size)
     138         542 :       _jvar_map[number] = it.index();
     139             :   }
     140             : 
     141             :   // mark the kernel variable for the check in computeOffDiagJacobian
     142        1397 :   _jvar_map[this->_var.number()] = 0;
     143        1397 : }
     144             : 
     145             : template <class T>
     146             : unsigned int
     147     1572920 : JvarMapInterfaceBase<T>::mapJvarToCvar(unsigned int jvar)
     148             : {
     149             :   mooseAssert(jvar < _jvar_max_size,
     150             :               "Calling mapJvarToCvar for an invalid Moose variable number. Maybe an AuxVariable?");
     151     1572920 :   int cit = _jvar_map[jvar];
     152             : 
     153             :   mooseAssert(cit >= 0, "Calling mapJvarToCvar for a variable not coupled to this kernel.");
     154     1572920 :   return cit;
     155             : }
     156             : 
     157             : template <class T>
     158             : int
     159             : JvarMapInterfaceBase<T>::mapJvarToCvar(unsigned int jvar, const JvarMap & jvar_map)
     160             : {
     161             :   mooseAssert(jvar < _jvar_max_size,
     162             :               "Calling mapJvarToCvar for an invalid Moose variable number. Maybe an AuxVariable?");
     163             :   return jvar_map[jvar];
     164             : }
     165             : 
     166             : template <class T>
     167             : const typename JvarMapInterfaceBase<T>::JvarMap &
     168          78 : JvarMapInterfaceBase<T>::getParameterJvarMap(std::string parameter_name)
     169             : {
     170          78 :   auto & jvar_map = _jvar_local_map[parameter_name];
     171          78 :   jvar_map.assign(_jvar_max_size, -1);
     172             : 
     173             :   // populate local map
     174          78 :   const auto num = this->coupledComponents(parameter_name);
     175         468 :   for (std::size_t i = 0; i < num; ++i)
     176             :   {
     177         390 :     const auto number = this->getVar(parameter_name, i)->number();
     178             : 
     179             :     // skip AuxVars as off-diagonal jacobian entries are not calculated for them
     180         390 :     if (number < _jvar_max_size)
     181         390 :       jvar_map[number] = i;
     182             :   }
     183             : 
     184          78 :   return jvar_map;
     185             : }
     186             : 
     187             : template <class T>
     188        1397 : JvarMapKernelInterface<T>::JvarMapKernelInterface(const InputParameters & parameters)
     189        1397 :   : JvarMapInterfaceBase<T>(parameters)
     190             : {
     191        1397 : }
     192             : 
     193             : template <class T>
     194             : JvarMapIntegratedBCInterface<T>::JvarMapIntegratedBCInterface(const InputParameters & parameters)
     195             :   : JvarMapInterfaceBase<T>(parameters)
     196             : {
     197             : }
     198             : 
     199             : template <class T>
     200             : void
     201      234384 : JvarMapKernelInterface<T>::computeOffDiagJacobian(const unsigned int jvar)
     202             : {
     203             :   // the Kernel is not coupled to the variable; no need to loop over QPs
     204      234384 :   if (this->_jvar_map[jvar] < 0)
     205       27963 :     return;
     206             : 
     207             :   // call the underlying class' off-diagonal Jacobian
     208      206421 :   T::computeOffDiagJacobian(jvar);
     209             : }
     210             : 
     211             : template <class T>
     212             : void
     213             : JvarMapIntegratedBCInterface<T>::computeOffDiagJacobian(const unsigned int jvar)
     214             : {
     215             :   // the Kernel is not coupled to the variable; no need to loop over QPs
     216             :   if (this->_jvar_map[jvar] < 0)
     217             :     return;
     218             : 
     219             :   // call the underlying class' off-diagonal Jacobian
     220             :   T::computeOffDiagJacobian(jvar);
     221             : }

Generated by: LCOV version 1.14