LCOV - code coverage report
Current view: top level - include/kernels - CHBulk.h (source / functions) Hit Total Coverage
Test: idaholab/moose phase_field: #32971 (54bef8) with base c6cf66 Lines: 25 26 96.2 %
Date: 2026-05-29 20:38:39 Functions: 9 11 81.8 %
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 "KernelGrad.h"
      13             : #include "JvarMapInterface.h"
      14             : #include "DerivativeMaterialInterface.h"
      15             : 
      16             : /**
      17             :  * This is the Cahn-Hilliard equation base class that implements the bulk or
      18             :  * local energy term of the equation. It is templated on the type of the mobility,
      19             :  * which can be either a number (Real) or a tensor (RealValueTensor).
      20             :  * See M.R. Tonks et al. / Computational Materials Science 51 (2012) 20-29 for more information.
      21             :  * Note that the function computeGradDFDCons MUST be overridden in any kernel that inherits from
      22             :  * CHBulk. Use CHMath as an example of how this works.
      23             :  */
      24             : template <typename T>
      25             : class CHBulk : public DerivativeMaterialInterface<JvarMapKernelInterface<KernelGrad>>
      26             : {
      27             : public:
      28             :   CHBulk(const InputParameters & parameters);
      29             : 
      30             :   static InputParameters validParams();
      31             :   virtual void initialSetup();
      32             : 
      33             : protected:
      34             :   virtual RealGradient precomputeQpResidual();
      35             :   virtual RealGradient precomputeQpJacobian();
      36             :   virtual Real computeQpOffDiagJacobian(unsigned int jvar);
      37             : 
      38             :   enum PFFunctionType
      39             :   {
      40             :     Jacobian,
      41             :     Residual
      42             :   };
      43             : 
      44             :   virtual RealGradient computeGradDFDCons(PFFunctionType type) = 0;
      45             : 
      46             :   /// Mobility
      47             :   const MaterialProperty<T> & _M;
      48             : 
      49             :   /// Mobility derivative w.r.t. concentration
      50             :   const MaterialProperty<T> & _dMdc;
      51             : 
      52             :   /// Mobility derivative w.r.t coupled variables
      53             :   std::vector<const MaterialProperty<T> *> _dMdarg;
      54             : };
      55             : 
      56             : template <typename T>
      57         211 : CHBulk<T>::CHBulk(const InputParameters & parameters)
      58             :   : DerivativeMaterialInterface<JvarMapKernelInterface<KernelGrad>>(parameters),
      59         211 :     _M(getMaterialProperty<T>("mob_name")),
      60         422 :     _dMdc(getMaterialPropertyDerivative<T>("mob_name", _var.name()))
      61             : {
      62             :   // Get number of coupled variables
      63         211 :   unsigned int nvar = _coupled_moose_vars.size();
      64             : 
      65             :   // reserve space for derivatives
      66         211 :   _dMdarg.resize(nvar);
      67             : 
      68             :   // Iterate over all coupled variables
      69         256 :   for (unsigned int i = 0; i < nvar; ++i)
      70          45 :     _dMdarg[i] = &getMaterialPropertyDerivative<T>("mob_name", _coupled_moose_vars[i]->name());
      71         211 : }
      72             : 
      73             : template <typename T>
      74             : InputParameters
      75         393 : CHBulk<T>::validParams()
      76             : {
      77         393 :   InputParameters params = KernelGrad::validParams();
      78         393 :   params.addClassDescription("Cahn-Hilliard base Kernel");
      79         786 :   params.addParam<MaterialPropertyName>("mob_name", "M", "The mobility used with the kernel");
      80         786 :   params.addCoupledVar("coupled_variables", "Vector of variable arguments of the mobility");
      81         393 :   return params;
      82           0 : }
      83             : 
      84             : template <typename T>
      85             : void
      86          86 : CHBulk<T>::initialSetup()
      87             : {
      88         258 :   validateNonlinearCoupling<Real>("mob_name");
      89          86 : }
      90             : 
      91             : template <typename T>
      92             : RealGradient
      93     7421300 : CHBulk<T>::precomputeQpResidual()
      94             : {
      95     7421300 :   return _M[_qp] * computeGradDFDCons(Residual);
      96             : }
      97             : 
      98             : template <typename T>
      99             : RealGradient
     100    75537488 : CHBulk<T>::precomputeQpJacobian()
     101             : {
     102    75537488 :   RealGradient grad_value = _M[_qp] * computeGradDFDCons(Jacobian) +
     103    75537488 :                             _dMdc[_qp] * _phi[_j][_qp] * computeGradDFDCons(Residual);
     104             : 
     105    75537488 :   return grad_value;
     106             : }
     107             : 
     108             : template <typename T>
     109             : Real
     110    22962176 : CHBulk<T>::computeQpOffDiagJacobian(unsigned int jvar)
     111             : {
     112             :   // get the coupled variable jvar is referring to
     113             :   const unsigned int cvar = mapJvarToCvar(jvar);
     114             : 
     115    22962176 :   return (*_dMdarg[cvar])[_qp] * _phi[_j][_qp] * computeGradDFDCons(Residual) * _grad_test[_i][_qp];
     116             : }

Generated by: LCOV version 1.14