LCOV - code coverage report
Current view: top level - src/kernels - CHPFCRFF.C (source / functions) Hit Total Coverage
Test: idaholab/moose phase_field: #31405 (292dce) with base fef103 Lines: 109 119 91.6 %
Date: 2025-09-04 07:55:36 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          92 : CHPFCRFF::validParams()
      17             : {
      18          92 :   InputParameters params = Kernel::validParams();
      19          92 :   params.addClassDescription(
      20             :       "Cahn-Hilliard residual for the RFF form of the phase field crystal model");
      21         184 :   params.addRequiredCoupledVar("v", "Array of names of the real parts of the L variables");
      22         184 :   MooseEnum log_options("tolerance cancelation expansion nothing");
      23         184 :   params.addRequiredParam<MooseEnum>(
      24             :       "log_approach", log_options, "Which approach will be used to handle the natural log");
      25         184 :   params.addParam<Real>("tol", 1.0e-9, "Tolerance used when the tolerance approach is chosen");
      26         184 :   params.addParam<Real>(
      27         184 :       "n_exp_terms", 4, "Number of terms used in the Taylor expansion of the natural log term");
      28         184 :   params.addParam<MaterialPropertyName>("mob_name", "M", "The mobility used with the kernel");
      29         184 :   params.addParam<MaterialPropertyName>("Dmob_name", "DM", "The D mobility used with the kernel");
      30         184 :   params.addParam<bool>("has_MJac", false, "Jacobian information for the mobility is defined");
      31         184 :   params.addParam<Real>("a", 1.0, "Constants on Taylor Series");
      32         184 :   params.addParam<Real>("b", 1.0, "Constants on Taylor Series");
      33         184 :   params.addParam<Real>("c", 1.0, "Constants on Taylor Series");
      34          92 :   return params;
      35          92 : }
      36             : 
      37          48 : CHPFCRFF::CHPFCRFF(const InputParameters & parameters)
      38             :   : Kernel(parameters),
      39          48 :     _M(getMaterialProperty<Real>("mob_name")),
      40          96 :     _has_MJac(getParam<bool>("has_MJac")),
      41          48 :     _DM(_has_MJac ? &getMaterialProperty<Real>("Dmob_name") : NULL),
      42          96 :     _log_approach(getParam<MooseEnum>("log_approach")),
      43          96 :     _tol(getParam<Real>("tol")),
      44          48 :     _num_L(coupledComponents("v")),
      45          48 :     _vals_var(_num_L),
      46          48 :     _grad_vals(_num_L),
      47          96 :     _n_exp_terms(getParam<Real>("n_exp_terms")),
      48          96 :     _a(getParam<Real>("a")),
      49          96 :     _b(getParam<Real>("b")),
      50         144 :     _c(getParam<Real>("c"))
      51             : {
      52             :   // Loop through grains and load coupled gradients into the arrays
      53         288 :   for (unsigned int i = 0; i < _num_L; ++i)
      54             :   {
      55         240 :     _vals_var[i] = coupled("v", i);
      56         240 :     _grad_vals[i] = &coupledGradient("v", i);
      57             :   }
      58          48 : }
      59             : 
      60             : Real
      61      483840 : CHPFCRFF::computeQpResidual()
      62             : {
      63      483840 :   Real c = _u[_qp];
      64      483840 :   RealGradient grad_c = _grad_u[_qp];
      65             :   RealGradient sum_grad_L;
      66             : 
      67     2903040 :   for (unsigned int i = 0; i < _num_L; ++i)
      68     2419200 :     sum_grad_L += (*_grad_vals[i])[_qp] * 0.5;
      69             : 
      70             :   Real frac = 0.0;
      71             :   Real ln_expansion = 0.0;
      72             : 
      73      483840 :   switch (_log_approach)
      74             :   {
      75       64512 :     case 0: // approach using tolerance
      76       64512 :       if (1.0 + c < _tol)
      77           0 :         frac = 1.0 / _tol;
      78             :       else
      79       64512 :         frac = 1.0 / (1.0 + c);
      80             :       break;
      81             : 
      82             :     case 2:
      83     1854720 :       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     1499904 :         if (i == 2)
      88      354816 :           temp_coeff = _c;
      89     1145088 :         else if (i == 3)
      90      354816 :           temp_coeff = _a;
      91      790272 :         else if (i == 4)
      92      354816 :           temp_coeff = _b;
      93             :         else
      94             :           temp_coeff = 1.0;
      95             : 
      96     1499904 :         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      483840 :   switch (_log_approach)
     104             :   {
     105             :     case 0: // approach using tolerance
     106             :       GradDFDCons = grad_c * frac - sum_grad_L;
     107       64512 :       break;
     108             : 
     109       64512 :     case 1: // approach using cancelation from the mobility
     110       64512 :       GradDFDCons = grad_c - (1.0 + c) * sum_grad_L;
     111       64512 :       break;
     112             : 
     113             :     case 2: // appraoch using substitution
     114             :       GradDFDCons = ln_expansion * grad_c - sum_grad_L;
     115      354816 :       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      483840 :   Real residual = _M[_qp] * GradDFDCons * _grad_test[_i][_qp];
     123      483840 :   return residual;
     124             : }
     125             : 
     126             : Real
     127      903168 : CHPFCRFF::computeQpJacobian()
     128             : {
     129      903168 :   Real c = _u[_qp];
     130      903168 :   RealGradient grad_c = _grad_u[_qp];
     131             :   RealGradient sum_grad_L;
     132             : 
     133     5419008 :   for (unsigned int i = 0; i < _num_L; ++i)
     134     4515840 :     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      903168 :   switch (_log_approach)
     141             :   {
     142      193536 :     case 0: // approach using tolerance
     143      193536 :       if (1.0 + c < _tol)
     144             :       {
     145           0 :         frac = 1.0 / _tol;
     146           0 :         dfrac = -1.0 / (_tol * _tol);
     147             :       }
     148             :       else
     149             :       {
     150      193536 :         frac = 1.0 / (1.0 + c);
     151      193536 :         dfrac = -1.0 / ((1.0 + c) * (1.0 + c));
     152             :       }
     153             :       break;
     154             : 
     155             :     case 2:
     156     2838528 :       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     2322432 :         if (i == 2)
     161      516096 :           temp_coeff = _c;
     162     1806336 :         else if (i == 3)
     163      516096 :           temp_coeff = _a;
     164     1290240 :         else if (i == 4)
     165      516096 :           temp_coeff = _b;
     166             :         else
     167             :           temp_coeff = 1.0;
     168             : 
     169     2322432 :         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      903168 :   switch (_log_approach)
     178             :   {
     179      193536 :     case 0: // approach using tolerance
     180      193536 :       dGradDFDConsdC = _grad_phi[_j][_qp] * frac + _phi[_j][_qp] * grad_c * dfrac;
     181      193536 :       break;
     182             : 
     183      193536 :     case 1: // approach using cancelation from the mobility
     184      193536 :       dGradDFDConsdC = _grad_phi[_j][_qp] - _phi[_j][_qp] * sum_grad_L;
     185      193536 :       break;
     186             : 
     187             :     case 2: // appraoch using substitution
     188     2838528 :       for (unsigned int i = 2; i < (_n_exp_terms + 2.0); ++i)
     189             :       {
     190             :         Real temp_coeff;
     191     2322432 :         if (i == 2)
     192      516096 :           temp_coeff = _c;
     193     1806336 :         else if (i == 3)
     194      516096 :           temp_coeff = _a;
     195     1290240 :         else if (i == 4)
     196      516096 :           temp_coeff = _b;
     197             :         else
     198             :           temp_coeff = 1.0;
     199             : 
     200     2322432 :         Dln_expansion += temp_coeff * std::pow(static_cast<Real>(-1.0), static_cast<Real>(i)) *
     201     2322432 :                          (static_cast<Real>(i) - 2.0) *
     202     2322432 :                          std::pow(_u[_qp], static_cast<Real>(i) - 3.0);
     203             :       }
     204             : 
     205      516096 :       dGradDFDConsdC = ln_expansion * _grad_phi[_j][_qp] + _phi[_j][_qp] * Dln_expansion * grad_c;
     206      516096 :       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      903168 :   return _M[_qp] * dGradDFDConsdC * _grad_test[_i][_qp];
     215             : }
     216             : 
     217             : Real
     218     5806080 : CHPFCRFF::computeQpOffDiagJacobian(unsigned int jvar)
     219             : {
     220     5806080 :   Real c = _u[_qp];
     221             : 
     222    25159680 :   for (unsigned int i = 0; i < _num_L; ++i)
     223    22579200 :     if (jvar == _vals_var[i])
     224             :     {
     225             : 
     226     3225600 :       RealGradient dsum_grad_L = _grad_phi[_j][_qp] * 0.5;
     227             :       RealGradient dGradDFDConsdL;
     228     3225600 :       switch (_log_approach)
     229             :       {
     230             :         case 0: // approach using tolerance
     231             :           dGradDFDConsdL = -dsum_grad_L;
     232      967680 :           break;
     233             : 
     234      967680 :         case 1: // approach using cancelation from the mobility
     235      967680 :           dGradDFDConsdL = -(1.0 + c) * dsum_grad_L;
     236      967680 :           break;
     237             : 
     238             :         case 2: // appraoch using substitution
     239             :           dGradDFDConsdL = -dsum_grad_L;
     240     1290240 :           break;
     241             : 
     242             :         case 3: // nothing special
     243             :           dGradDFDConsdL = -dsum_grad_L;
     244           0 :           break;
     245             :       }
     246             : 
     247     3225600 :       return _M[_qp] * dGradDFDConsdL * _grad_test[_i][_qp];
     248             :     }
     249             : 
     250             :   return 0.0;
     251             : }

Generated by: LCOV version 1.14