20#include "libmesh/threads.h"
27 if (std::is_same<T, Real>::value)
28 params.registerBase(
"Kernel");
29 else if (std::is_same<T, RealVectorValue>::value)
30 params.registerBase(
"VectorKernel");
32 ::mooseError(
"unsupported ADKernelTempl specialization");
43 Moose::VarKindType::VAR_SOLVER,
44 std::is_same<T, Real>::value ?
Moose::VarFieldType::VAR_FIELD_STANDARD
45 :
Moose::VarFieldType::VAR_FIELD_VECTOR),
47 _var(*this->mooseVariable()),
49 _grad_test(_var.adGradPhi()),
50 _regular_grad_test(_var.gradPhi()),
52 _grad_u(_var.adGradSln()),
53 _ad_JxW(_assembly.adJxW()),
54 _ad_coord(_assembly.adCoordTransformation()),
55 _ad_q_point(_assembly.adQPoints()),
56 _phi(_assembly.phi(_var)),
57 _grad_phi(_assembly.template adGradPhi<T>(_var)),
58 _regular_grad_phi(_assembly.gradPhi(_var)),
72 paramError(
"save_in",
"cannot use solution variable as save-in variable");
77 "saved-in auxiliary variable is incompatible with the object's nonlinear variable: ",
92 paramError(
"diag_save_in",
"cannot use solution variable as diag save-in variable");
97 "saved-in auxiliary variable is incompatible with the object's nonlinear variable: ",
112 precalculateResidual();
114 if (_residuals_nonad.size() != _test.size())
115 _residuals_nonad.resize(_test.size(), 0);
116 for (
auto & r : _residuals_nonad)
119 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
121 const auto jxw_c = _JxW[_qp] * _coord[_qp];
122 for (_i = 0; _i < _test.size(); _i++)
123 _residuals_nonad[_i] += jxw_c * raw_value(computeQpResidual());
126 addResiduals(_assembly, _residuals_nonad, _var.dofIndices(), _var.scalingFactor());
129 for (
unsigned int i = 0; i < _save_in.size(); i++)
130 _save_in[i]->sys().solution().add_vector(_residuals_nonad.data(), _save_in[i]->dofIndices());
137 if (_residuals.size() != _test.size())
138 _residuals.resize(_test.size(), 0);
139 for (
auto & r : _residuals)
142 precalculateResidual();
143 if (_use_displaced_mesh)
144 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
147 _r *= _ad_coord[_qp];
148 for (_i = 0; _i < _test.size(); _i++)
149 _residuals[_i] += _r * computeQpResidual();
152 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
154 const auto jxw_c = _JxW[_qp] * _coord[_qp];
155 for (_i = 0; _i < _test.size(); _i++)
156 _residuals[_i] += jxw_c * computeQpResidual();
166 if (_has_diag_save_in && !_sys.computingScalingJacobian())
167 mooseError(
"_local_ke not computed for global AD indexing. Save-in is deprecated anyway. Use "
168 "the tagging system instead.");
175 computeResidualsForJacobian();
176 addJacobian(_assembly, _residuals, dofIndices(), _var.scalingFactor());
190 if (_my_elem != _current_elem)
193 _my_elem = _current_elem;
207 computeResidualsForJacobian();
208 addResidualsAndJacobian(_assembly, _residuals, _var.dofIndices(), _var.scalingFactor());
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
An interface for accessing Moose::Functors for systems that care about automatic differentiation,...
static InputParameters validParams()
void computeOffDiagJacobian(unsigned int) override
Computes this object's contribution to off-diagonal blocks of the system Jacobian matrix.
void computeJacobian() override
Compute this object's contribution to the diagonal Jacobian entries.
void computeResidual() override
Compute this object's contribution to the residual.
virtual void computeResidualsForJacobian()
compute the _residuals member for filling the Jacobian.
void computeADJacobian()
compute all the Jacobian entries
static InputParameters validParams()
MooseVariableFE< T > & _var
This is a regular kernel so we cast to a regular MooseVariable.
ADKernelTempl(const InputParameters ¶meters)
void computeOffDiagJacobianScalar(unsigned int jvar) override
Computes jacobian block with respect to a scalar variable.
void computeResidualAndJacobian() override
Compute this object's contribution to the residual and Jacobian simultaneously.
void jacobianSetup() override
Gets called just before the Jacobian is computed and before this object is asked to do its job.
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
This is the common base class for the three main kernel types implemented in MOOSE,...
std::vector< MooseVariableFEBase * > _save_in
bool _has_diag_save_in
The aux variables to save the diagonal Jacobian contributions to.
std::vector< AuxVariableName > _save_in_strings
static InputParameters validParams()
bool _has_save_in
The aux variables to save the residual contributions to.
std::vector< MooseVariableFEBase * > _diag_save_in
std::vector< AuxVariableName > _diag_save_in_strings
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
const libMesh::FEType & feType() const
Get the type of finite element object.
SystemBase & sys()
Get the system this variable is part of.
void addMooseVariableDependency(MooseVariableFieldBase *var)
Call this function to add the passed in MooseVariableFieldBase as a variable that this object depends...
Interface for objects that need to get values of MooseVariables.
MooseVariableFE< T > * mooseVariable() const
Return the MooseVariableFE object that this interface acts on.
THREAD_ID _tid
The thread ID for this kernel.
SubProblem & _subproblem
Reference to this kernel's SubProblem.
SystemBase & _sys
Reference to the EquationSystem object.
FEProblemBase & _fe_problem
Reference to this kernel's FEProblemBase.
virtual MooseVariable & getStandardVariable(const THREAD_ID tid, const std::string &var_name)=0
Returns the variable reference for requested MooseVariable which may be in any system.
virtual void haveADObjects(bool have_ad_objects)
Method for setting whether we have any ad objects.
virtual void addVariableToZeroOnJacobian(std::string var_name)
Adds this variable to the list of variables to be zeroed during each Jacobian evaluation.
virtual void addVariableToZeroOnResidual(std::string var_name)
Adds this variable to the list of variables to be zeroed during each residual evaluation.
unsigned int number() const
Gets the number of this system.
virtual bool hasVariable(const std::string &var_name) const
Query a system for a variable.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::string incompatVarMsg(MooseVariableFieldBase &var1, MooseVariableFieldBase &var2)
Builds and returns a string of the form: