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 "CoupledSwitchingTimeDerivative.h" 11 : 12 : registerMooseObject("PhaseFieldApp", CoupledSwitchingTimeDerivative); 13 : registerMooseObject("PhaseFieldApp", ADCoupledSwitchingTimeDerivative); 14 : 15 : template <bool is_ad> 16 : InputParameters 17 543 : CoupledSwitchingTimeDerivativeTempl<is_ad>::validParams() 18 : { 19 543 : InputParameters params = CoupledSwitchingTimeDerivativeBase<is_ad>::validParams(); 20 543 : params.addClassDescription( 21 : "Coupled time derivative Kernel that multiplies the time derivative by " 22 : "$\\frac{dh_\\alpha}{d\\eta_i} F_\\alpha + \\frac{dh_\\beta}{d\\eta_i} F_\\beta + \\dots$"); 23 1086 : params.addRequiredParam<std::vector<MaterialPropertyName>>( 24 : "Fj_names", "List of functions for each phase. Place in same order as hj_names!"); 25 1086 : params.addRequiredParam<std::vector<MaterialPropertyName>>( 26 : "hj_names", "Switching Function Materials that provide h. Place in same order as Fj_names!"); 27 1086 : params.addCoupledVar("args", "Vector of variable arguments of Fj and hj"); 28 1086 : params.deprecateCoupledVar("args", "coupled_variables", "02/27/2024"); 29 543 : return params; 30 0 : } 31 : template <bool is_ad> 32 285 : CoupledSwitchingTimeDerivativeTempl<is_ad>::CoupledSwitchingTimeDerivativeTempl( 33 : const InputParameters & parameters) 34 : : DerivativeMaterialInterface<JvarMapKernelInterface<CoupledSwitchingTimeDerivativeBase<is_ad>>>( 35 : parameters), 36 285 : _v_name(this->coupledName("v", 0)), 37 855 : _Fj_names(this->template getParam<std::vector<MaterialPropertyName>>("Fj_names")), 38 285 : _num_j(_Fj_names.size()), 39 285 : _prop_Fj(_num_j), 40 570 : _hj_names(this->template getParam<std::vector<MaterialPropertyName>>("hj_names")), 41 570 : _prop_dhjdetai(_num_j) 42 : { 43 : // check passed in parameter vectors 44 285 : if (_num_j != _hj_names.size()) 45 0 : this->paramError("hj_names", "Need to pass in as many hj_names as Fj_names"); 46 : 47 : // reserve space and set phase material properties 48 909 : for (unsigned int n = 0; n < _num_j; ++n) 49 : { 50 : // get phase free energy 51 624 : _prop_Fj[n] = &this->template getGenericMaterialProperty<Real, is_ad>(_Fj_names[n]); 52 : 53 : // get switching function derivatives wrt eta_i, the nonlinear variable 54 624 : _prop_dhjdetai[n] = 55 1248 : &this->template getMaterialPropertyDerivative<Real, is_ad>(_hj_names[n], _v_name); 56 : } 57 285 : } 58 : 59 207 : CoupledSwitchingTimeDerivative::CoupledSwitchingTimeDerivative(const InputParameters & parameters) 60 : : CoupledSwitchingTimeDerivativeTempl<false>(parameters), 61 414 : _prop_dFjdv(_num_j), 62 207 : _prop_dFjdarg(_num_j), 63 207 : _prop_d2hjdetai2(_num_j), 64 414 : _prop_d2hjdetaidarg(_num_j) 65 : { 66 : // reserve space and set phase material properties 67 648 : for (unsigned int n = 0; n < _num_j; ++n) 68 : { 69 : // get phase free energy and derivatives 70 441 : _prop_dFjdv[n] = &getMaterialPropertyDerivative<Real>(_Fj_names[n], _var.name()); 71 441 : _prop_dFjdarg[n].resize(_n_args); 72 : 73 : // get switching function and derivatives wrt eta_i, the nonlinear variable 74 441 : _prop_d2hjdetai2[n] = &getMaterialPropertyDerivative<Real>(_hj_names[n], _v_name, _v_name); 75 441 : _prop_d2hjdetaidarg[n].resize(_n_args); 76 : 77 2397 : for (unsigned int i = 0; i < _n_args; ++i) 78 : { 79 : // Get derivatives of all Fj wrt all coupled variables 80 1956 : _prop_dFjdarg[n][i] = &getMaterialPropertyDerivative<Real>(_Fj_names[n], i); 81 : 82 : // Get second derivatives of all hj wrt eta_i and all coupled variables 83 1956 : _prop_d2hjdetaidarg[n][i] = &getMaterialPropertyDerivative<Real>(_hj_names[n], _v_name, i); 84 : } 85 : } 86 207 : } 87 : 88 : template <bool is_ad> 89 : void 90 231 : CoupledSwitchingTimeDerivativeTempl<is_ad>::initialSetup() 91 : { 92 693 : for (unsigned int n = 0; n < _num_j; ++n) 93 1386 : this->template validateNonlinearCoupling<GenericReal<is_ad>>(_hj_names[n]); 94 231 : } 95 : 96 : void 97 180 : CoupledSwitchingTimeDerivative::initialSetup() 98 : { 99 180 : CoupledSwitchingTimeDerivativeTempl<false>::initialSetup(); 100 540 : for (unsigned int n = 0; n < _num_j; ++n) 101 1080 : validateNonlinearCoupling<Real>(_Fj_names[n]); 102 180 : } 103 : 104 : Real 105 8392800 : CoupledSwitchingTimeDerivative::computeQpResidual() 106 : { 107 : Real sum = 0.0; 108 25178400 : for (unsigned int n = 0; n < _num_j; ++n) 109 16785600 : sum += (*_prop_dhjdetai[n])[_qp] * (*_prop_Fj[n])[_qp]; 110 : 111 8392800 : return CoupledTimeDerivative::computeQpResidual() * sum; 112 : } 113 : 114 : ADReal 115 503220 : ADCoupledSwitchingTimeDerivative::precomputeQpResidual() 116 : { 117 503220 : ADReal sum = 0.0; 118 1509660 : for (unsigned int n = 0; n < _num_j; ++n) 119 2012880 : sum += (*_prop_dhjdetai[n])[_qp] * (*_prop_Fj[n])[_qp]; 120 : 121 1006440 : return _v_dot[_qp] * sum; 122 : } 123 : 124 : Real 125 7337600 : CoupledSwitchingTimeDerivative::computeQpJacobian() 126 : { 127 : Real sum = 0.0; 128 22012800 : for (unsigned int n = 0; n < _num_j; ++n) 129 14675200 : sum += (*_prop_dhjdetai[n])[_qp] * (*_prop_dFjdv[n])[_qp]; 130 : 131 7337600 : return CoupledTimeDerivative::computeQpResidual() * sum * _phi[_j][_qp]; 132 : } 133 : 134 : Real 135 19206400 : CoupledSwitchingTimeDerivative::computeQpOffDiagJacobian(unsigned int jvar) 136 : { 137 : // get the coupled variable jvar is referring to 138 : const unsigned int cvar = mapJvarToCvar(jvar); 139 : 140 19206400 : if (jvar == _v_var) 141 : { 142 : Real sum = 0.0; 143 : 144 21091200 : for (unsigned int n = 0; n < _num_j; ++n) 145 14060800 : sum += 146 14060800 : ((*_prop_d2hjdetai2[n])[_qp] * _v_dot[_qp] + (*_prop_dhjdetai[n])[_qp] * _dv_dot[_qp]) * 147 14060800 : (*_prop_Fj[n])[_qp]; 148 : 149 7030400 : return _phi[_j][_qp] * sum * _test[_i][_qp]; 150 : } 151 : 152 : Real sum = 0.0; 153 36528000 : for (unsigned int n = 0; n < _num_j; ++n) 154 24352000 : sum += (*_prop_d2hjdetaidarg[n][cvar])[_qp] * (*_prop_Fj[n])[_qp] + 155 24352000 : (*_prop_dhjdetai[n])[_qp] * (*_prop_dFjdarg[n][cvar])[_qp]; 156 : 157 12176000 : return CoupledTimeDerivative::computeQpResidual() * sum * _phi[_j][_qp]; 158 : } 159 : 160 : template class CoupledSwitchingTimeDerivativeTempl<false>; 161 : template class CoupledSwitchingTimeDerivativeTempl<true>;