LCOV - code coverage report
Current view: top level - src/kernels - CHPFCRFF.C (source / functions) Hit Total Coverage
Test: idaholab/moose phase_field: #32971 (54bef8) with base c6cf66 Lines: 109 119 91.6 %
Date: 2026-05-29 20:38:39 Functions: 5 5 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             : #include "CHPFCRFF.h"
      11             : #include "MathUtils.h"
      12             : 
      13             : registerMooseObject("PhaseFieldApp", CHPFCRFF);
      14             : 
      15             : InputParameters
      16          60 : CHPFCRFF::validParams()
      17             : {
      18          60 :   InputParameters params = Kernel::validParams();
      19          60 :   params.addClassDescription(
      20             :       "Cahn-Hilliard residual for the RFF form of the phase field crystal model");
      21         120 :   params.addRequiredCoupledVar("v", "Array of names of the real parts of the L variables");
      22         120 :   MooseEnum log_options("tolerance cancelation expansion nothing");
      23         120 :   params.addRequiredParam<MooseEnum>(
      24             :       "log_approach", log_options, "Which approach will be used to handle the natural log");
      25         120 :   params.addParam<Real>("tol", 1.0e-9, "Tolerance used when the tolerance approach is chosen");
      26         120 :   params.addParam<Real>(
      27         120 :       "n_exp_terms", 4, "Number of terms used in the Taylor expansion of the natural log term");
      28         120 :   params.addParam<MaterialPropertyName>("mob_name", "M", "The mobility used with the kernel");
      29         120 :   params.addParam<MaterialPropertyName>("Dmob_name", "DM", "The D mobility used with the kernel");
      30         120 :   params.addParam<bool>("has_MJac", false, "Jacobian information for the mobility is defined");
      31         120 :   params.addParam<Real>("a", 1.0, "Constants on Taylor Series");
      32         120 :   params.addParam<Real>("b", 1.0, "Constants on Taylor Series");
      33         120 :   params.addParam<Real>("c", 1.0, "Constants on Taylor Series");
      34          60 :   return params;
      35          60 : }
      36             : 
      37          32 : CHPFCRFF::CHPFCRFF(const InputParameters & parameters)
      38             :   : Kernel(parameters),
      39          32 :     _M(getMaterialProperty<Real>("mob_name")),
      40          64 :     _has_MJac(getParam<bool>("has_MJac")),
      41          32 :     _DM(_has_MJac ? &getMaterialProperty<Real>("Dmob_name") : NULL),
      42          64 :     _log_approach(getParam<MooseEnum>("log_approach")),
      43          64 :     _tol(getParam<Real>("tol")),
      44          32 :     _num_L(coupledComponents("v")),
      45          32 :     _vals_var(_num_L),
      46          32 :     _grad_vals(_num_L),
      47          64 :     _n_exp_terms(getParam<Real>("n_exp_terms")),
      48          64 :     _a(getParam<Real>("a")),
      49          64 :     _b(getParam<Real>("b")),
      50          96 :     _c(getParam<Real>("c"))
      51             : {
      52             :   // Loop through grains and load coupled gradients into the arrays
      53         192 :   for (unsigned int i = 0; i < _num_L; ++i)
      54             :   {
      55         160 :     _vals_var[i] = coupled("v", i);
      56         160 :     _grad_vals[i] = &coupledGradient("v", i);
      57             :   }
      58          32 : }
      59             : 
      60             : Real
      61      414720 : CHPFCRFF::computeQpResidual()
      62             : {
      63      414720 :   Real c = _u[_qp];
      64      414720 :   RealGradient grad_c = _grad_u[_qp];
      65             :   RealGradient sum_grad_L;
      66             : 
      67     2488320 :   for (unsigned int i = 0; i < _num_L; ++i)
      68     2073600 :     sum_grad_L += (*_grad_vals[i])[_qp] * 0.5;
      69             : 
      70             :   Real frac = 0.0;
      71             :   Real ln_expansion = 0.0;
      72             : 
      73      414720 :   switch (_log_approach)
      74             :   {
      75       55296 :     case 0: // approach using tolerance
      76       55296 :       if (1.0 + c < _tol)
      77           0 :         frac = 1.0 / _tol;
      78             :       else
      79       55296 :         frac = 1.0 / (1.0 + c);
      80             :       break;
      81             : 
      82             :     case 2:
      83     1589760 :       for (unsigned int i = 2; i < (_n_exp_terms + 2.0); ++i)
      84             :       {
      85             :         // Apply Coefficents to Taylor Series defined in input file
      86             :         Real temp_coeff;
      87     1285632 :         if (i == 2)
      88      304128 :           temp_coeff = _c;
      89      981504 :         else if (i == 3)
      90      304128 :           temp_coeff = _a;
      91      677376 :         else if (i == 4)
      92      304128 :           temp_coeff = _b;
      93             :         else
      94             :           temp_coeff = 1.0;
      95             : 
      96     1285632 :         ln_expansion += temp_coeff * std::pow(-1.0, Real(i)) * std::pow(_u[_qp], Real(i) - 2.0);
      97             :       }
      98             :       break;
      99             :   }
     100             : 
     101             :   RealGradient GradDFDCons;
     102             : 
     103      414720 :   switch (_log_approach)
     104             :   {
     105             :     case 0: // approach using tolerance
     106             :       GradDFDCons = grad_c * frac - sum_grad_L;
     107       55296 :       break;
     108             : 
     109       55296 :     case 1: // approach using cancelation from the mobility
     110       55296 :       GradDFDCons = grad_c - (1.0 + c) * sum_grad_L;
     111       55296 :       break;
     112             : 
     113             :     case 2: // appraoch using substitution
     114             :       GradDFDCons = ln_expansion * grad_c - sum_grad_L;
     115      304128 :       break;
     116             : 
     117           0 :     case 3: // Just using the log
     118           0 :       GradDFDCons = grad_c / (1.0 + c) - sum_grad_L;
     119           0 :       break;
     120             :   }
     121             : 
     122      414720 :   Real residual = _M[_qp] * GradDFDCons * _grad_test[_i][_qp];
     123      414720 :   return residual;
     124             : }
     125             : 
     126             : Real
     127      774144 : CHPFCRFF::computeQpJacobian()
     128             : {
     129      774144 :   Real c = _u[_qp];
     130      774144 :   RealGradient grad_c = _grad_u[_qp];
     131             :   RealGradient sum_grad_L;
     132             : 
     133     4644864 :   for (unsigned int i = 0; i < _num_L; ++i)
     134     3870720 :     sum_grad_L += (*_grad_vals[i])[_qp] * 0.5;
     135             : 
     136             :   Real frac = 0.0;
     137             :   Real dfrac = 0.0;
     138             :   Real ln_expansion = 0.0;
     139             : 
     140      774144 :   switch (_log_approach)
     141             :   {
     142      165888 :     case 0: // approach using tolerance
     143      165888 :       if (1.0 + c < _tol)
     144             :       {
     145           0 :         frac = 1.0 / _tol;
     146           0 :         dfrac = -1.0 / (_tol * _tol);
     147             :       }
     148             :       else
     149             :       {
     150      165888 :         frac = 1.0 / (1.0 + c);
     151      165888 :         dfrac = -1.0 / ((1.0 + c) * (1.0 + c));
     152             :       }
     153             :       break;
     154             : 
     155             :     case 2:
     156     2433024 :       for (unsigned int i = 2; i < (_n_exp_terms + 2.0); ++i)
     157             :       {
     158             :         // Apply Coefficents to Taylor Series defined in input file
     159             :         Real temp_coeff;
     160     1990656 :         if (i == 2)
     161      442368 :           temp_coeff = _c;
     162     1548288 :         else if (i == 3)
     163      442368 :           temp_coeff = _a;
     164     1105920 :         else if (i == 4)
     165      442368 :           temp_coeff = _b;
     166             :         else
     167             :           temp_coeff = 1.0;
     168             : 
     169     1990656 :         ln_expansion += temp_coeff * std::pow(-1.0, Real(i)) * std::pow(_u[_qp], Real(i) - 2.0);
     170             :       }
     171             :       break;
     172             :   }
     173             : 
     174             :   RealGradient dGradDFDConsdC;
     175             :   Real Dln_expansion = 0.0;
     176             : 
     177      774144 :   switch (_log_approach)
     178             :   {
     179      165888 :     case 0: // approach using tolerance
     180      165888 :       dGradDFDConsdC = _grad_phi[_j][_qp] * frac + _phi[_j][_qp] * grad_c * dfrac;
     181      165888 :       break;
     182             : 
     183      165888 :     case 1: // approach using cancelation from the mobility
     184      165888 :       dGradDFDConsdC = _grad_phi[_j][_qp] - _phi[_j][_qp] * sum_grad_L;
     185      165888 :       break;
     186             : 
     187             :     case 2: // appraoch using substitution
     188     2433024 :       for (unsigned int i = 2; i < (_n_exp_terms + 2.0); ++i)
     189             :       {
     190             :         Real temp_coeff;
     191     1990656 :         if (i == 2)
     192      442368 :           temp_coeff = _c;
     193     1548288 :         else if (i == 3)
     194      442368 :           temp_coeff = _a;
     195     1105920 :         else if (i == 4)
     196      442368 :           temp_coeff = _b;
     197             :         else
     198             :           temp_coeff = 1.0;
     199             : 
     200     1990656 :         Dln_expansion += temp_coeff * std::pow(static_cast<Real>(-1.0), static_cast<Real>(i)) *
     201     1990656 :                          (static_cast<Real>(i) - 2.0) *
     202     1990656 :                          std::pow(_u[_qp], static_cast<Real>(i) - 3.0);
     203             :       }
     204             : 
     205      442368 :       dGradDFDConsdC = ln_expansion * _grad_phi[_j][_qp] + _phi[_j][_qp] * Dln_expansion * grad_c;
     206      442368 :       break;
     207             : 
     208           0 :     case 3: // Nothing special
     209             :       dGradDFDConsdC =
     210           0 :           _grad_phi[_j][_qp] / (1.0 + c) - grad_c / ((1.0 + c) * (1.0 + c)) * _phi[_j][_qp];
     211           0 :       break;
     212             :   }
     213             : 
     214      774144 :   return _M[_qp] * dGradDFDConsdC * _grad_test[_i][_qp];
     215             : }
     216             : 
     217             : Real
     218     4976640 : CHPFCRFF::computeQpOffDiagJacobian(unsigned int jvar)
     219             : {
     220     4976640 :   Real c = _u[_qp];
     221             : 
     222    21565440 :   for (unsigned int i = 0; i < _num_L; ++i)
     223    19353600 :     if (jvar == _vals_var[i])
     224             :     {
     225             : 
     226     2764800 :       RealGradient dsum_grad_L = _grad_phi[_j][_qp] * 0.5;
     227             :       RealGradient dGradDFDConsdL;
     228     2764800 :       switch (_log_approach)
     229             :       {
     230             :         case 0: // approach using tolerance
     231             :           dGradDFDConsdL = -dsum_grad_L;
     232      829440 :           break;
     233             : 
     234      829440 :         case 1: // approach using cancelation from the mobility
     235      829440 :           dGradDFDConsdL = -(1.0 + c) * dsum_grad_L;
     236      829440 :           break;
     237             : 
     238             :         case 2: // appraoch using substitution
     239             :           dGradDFDConsdL = -dsum_grad_L;
     240     1105920 :           break;
     241             : 
     242             :         case 3: // nothing special
     243             :           dGradDFDConsdL = -dsum_grad_L;
     244           0 :           break;
     245             :       }
     246             : 
     247     2764800 :       return _M[_qp] * dGradDFDConsdL * _grad_test[_i][_qp];
     248             :     }
     249             : 
     250             :   return 0.0;
     251             : }

Generated by: LCOV version 1.14