https://mooseframework.inl.gov
Kernel.C
Go to the documentation of this file.
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 "Kernel.h"
11 
12 // MOOSE includes
13 #include "Assembly.h"
14 #include "MooseVariableFE.h"
15 #include "MooseVariableScalar.h"
16 #include "SubProblem.h"
17 #include "NonlinearSystem.h"
18 #include "FEProblemBase.h"
19 
20 #include "libmesh/threads.h"
21 #include "libmesh/quadrature.h"
22 
25 {
27  params.registerBase("Kernel");
28  return params;
29 }
30 
31 Kernel::Kernel(const InputParameters & parameters)
32  : KernelBase(parameters),
34  false,
35  "variable",
38  _var(*mooseVariable()),
39  _test(_var.phi()),
40  _grad_test(_var.gradPhi()),
41  _phi(_assembly.phi(_var)),
42  _grad_phi(_assembly.gradPhi(_var)),
43  _u(_is_implicit ? _var.sln() : _var.slnOld()),
44  _grad_u(_is_implicit ? _var.gradSln() : _var.gradSlnOld())
45 {
47  _save_in.resize(_save_in_strings.size());
48  _diag_save_in.resize(_diag_save_in_strings.size());
49 
50  for (unsigned int i = 0; i < _save_in_strings.size(); i++)
51  {
53 
55  paramError("save_in", "cannot use solution variable as save-in variable");
56 
57  if (var->feType() != _var.feType())
58  paramError(
59  "save_in",
60  "saved-in auxiliary variable is incompatible with the object's nonlinear variable: ",
62 
63  _save_in[i] = var;
66  }
67 
68  _has_save_in = _save_in.size() > 0;
69 
70  for (unsigned int i = 0; i < _diag_save_in_strings.size(); i++)
71  {
73 
75  paramError("diag_save_in", "cannot use solution variable as diag save-in variable");
76 
77  if (var->feType() != _var.feType())
78  paramError(
79  "diag_save_in",
80  "saved-in auxiliary variable is incompatible with the object's nonlinear variable: ",
82 
83  _diag_save_in[i] = var;
86  }
87 
88  _has_diag_save_in = _diag_save_in.size() > 0;
89 }
90 
91 void
93 {
94  if (!hasVectorTags())
95  return;
96 
98 
100  for (_i = 0; _i < _test.size(); _i++)
101  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
103 
105 
106  if (_has_save_in)
107  for (const auto & var : _save_in)
108  var->sys().solution().add_vector(_local_re, var->dofIndices());
109 }
110 
111 void
113 {
115 
117  for (_i = 0; _i < _test.size(); _i++)
118  for (_j = 0; _j < _phi.size(); _j++)
119  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
121 
123 
125  {
127  for (const auto & var : _diag_save_in)
128  var->sys().solution().add_vector(diag, var->dofIndices());
129  }
130 }
131 
132 void
133 Kernel::computeOffDiagJacobian(const unsigned int jvar_num)
134 {
135  if (jvar_num == _var.number())
136  computeJacobian();
137  else
138  {
139  const auto & jvar = getVariable(jvar_num);
140  prepareMatrixTag(_assembly, _var.number(), jvar_num);
141 
142  // It's possible that this variable has not been requested for coupling anywhere for our
143  // associated system/assembly. E.g. in one ALE simulation, the displacement diffusion kernel
144  // runs on the reference mesh and all the velocity variable couplings are on the displaced mesh
145  if (jvar.dofIndices().empty())
146  return;
147 
148  // phi_size may not be equal to _phi.size(), e.g. when jvar is a vector variable
149  const auto phi_size = jvar.phiSize();
150  mooseAssert(
151  phi_size * jvar.count() == _local_ke.n(),
152  "The size of the phi container does not match the number of local Jacobian columns");
153 
154  mooseAssert(_local_ke.m() == _test.size(),
155  "If these are not the same, then we shouldn't even be calling this method");
156 
157  precalculateOffDiagJacobian(jvar_num);
158  if (jvar.count() == 1)
159  {
160  for (_i = 0; _i < _test.size(); _i++)
161  for (_j = 0; _j < phi_size; _j++)
162  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
163  _local_ke(_i, _j) += _JxW[_qp] * _coord[_qp] * computeQpOffDiagJacobian(jvar.number());
164  }
165  else
166  {
167  const auto n = cast_int<unsigned int>(phi_size);
168  for (_i = 0; _i < _test.size(); _i++)
169  for (_j = 0; _j < phi_size; _j++)
170  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
171  {
172  const RealEigenVector v =
173  _JxW[_qp] * _coord[_qp] *
175  static_cast<ArrayMooseVariable &>(const_cast<MooseVariableFieldBase &>(jvar)));
176  for (unsigned int k = 0; k < v.size(); ++k)
177  _local_ke(_i, _j + k * n) += v(k);
178  }
179  }
180 
182  }
183 }
184 
185 void
186 Kernel::computeOffDiagJacobianScalar(const unsigned int jvar)
187 {
190 
191  for (_i = 0; _i < _test.size(); _i++)
192  for (_j = 0; _j < jv.order(); _j++)
193  for (_qp = 0; _qp < _qrule->n_points(); _qp++)
195 
197 }
198 
199 void
201 {
202  computeResidual();
203 
204  for (const auto & [ivariable, jvariable] : _fe_problem.couplingEntries(_tid, _sys.number()))
205  {
206  const unsigned int ivar = ivariable->number();
207  const unsigned int jvar = jvariable->number();
208 
209  if (ivar != _var.number())
210  continue;
211 
212  if (_is_implicit)
213  {
214  prepareShapes(jvar);
216  }
217  }
218 
220 }
VarFieldType
Definition: MooseTypes.h:770
virtual void computeResidual() override
Compute this Kernel&#39;s contribution to the residual.
Definition: Kernel.C:92
const libMesh::FEType & feType() const
Get the type of finite element object.
std::vector< std::pair< MooseVariableFEBase *, MooseVariableFEBase * > > & couplingEntries(const THREAD_ID tid, const unsigned int nl_sys_num)
static InputParameters validParams()
Definition: Kernel.C:24
void accumulateTaggedLocalResidual()
Local residual blocks will be appended by adding the current local kernel residual.
virtual Real computeQpResidual()=0
Compute this Kernel&#39;s contribution to the residual at the current quadrature point.
bool hasVectorTags() const
virtual RealEigenVector computeQpOffDiagJacobianArray(const ArrayMooseVariable &jvar)
For coupling array variables.
Definition: Kernel.h:66
MooseVariable & _var
This is a regular kernel so we cast to a regular MooseVariable.
Definition: Kernel.h:72
Class for stuff related to variables.
Definition: Adaptivity.h:31
std::vector< MooseVariableFEBase * > _save_in
Definition: KernelBase.h:65
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:439
std::string incompatVarMsg(MooseVariableFieldBase &var1, MooseVariableFieldBase &var2)
Builds and returns a string of the form:
Definition: MooseError.C:26
const MooseArray< Real > & _JxW
The current quadrature point weight value.
Definition: KernelBase.h:52
std::vector< AuxVariableName > _diag_save_in_strings
Definition: KernelBase.h:71
virtual void precalculateOffDiagJacobian(unsigned int)
unsigned int number() const
Get variable number coming from libMesh.
virtual Real computeQpOffDiagJacobian(unsigned int)
For coupling standard variables.
Definition: Kernel.h:56
virtual void precalculateResidual()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
bool computingScalingJacobian() const
Whether we are computing an initial Jacobian for automatic variable scaling.
Definition: SystemBase.C:1552
virtual void prepareShapes(unsigned int var_num)
Prepare shape functions.
virtual void computeResidualAndJacobian() override
Compute the residual and Jacobian together.
Definition: Kernel.C:200
unsigned int m() const
THREAD_ID _tid
The thread ID for this kernel.
bool _has_diag_save_in
The aux variables to save the diagonal Jacobian contributions to.
Definition: KernelBase.h:69
DenseVector< Real > getJacobianDiagonal(DenseMatrix< Number > &ke)
Definition: Assembly.h:1895
DenseMatrix< Number > _local_ke
Holds local Jacobian entries as they are accumulated by this Kernel.
const MooseVariableFieldBase & getVariable(unsigned int jvar_num) const
Retrieve the variable object from our system associated with jvar_num.
Kernel(const InputParameters &parameters)
Definition: Kernel.C:31
SystemBase & _sys
Reference to the EquationSystem object.
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System...
MooseVariableFE< Real > * mooseVariable() const
Return the MooseVariableFE object that this interface acts on.
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:259
const VariableTestValue & _test
the current test function
Definition: Kernel.h:75
This is the common base class for the three main kernel types implemented in MOOSE, Kernel, VectorKernel and ArrayKernel.
Definition: KernelBase.h:23
virtual void precalculateJacobian()
std::vector< MooseVariableFEBase * > _diag_save_in
Definition: KernelBase.h:70
SubProblem & _subproblem
Reference to this kernel&#39;s SubProblem.
VarKindType
Framework-wide stuff.
Definition: MooseTypes.h:763
const QBase *const & _qrule
active quadrature rule
Definition: KernelBase.h:49
bool _has_save_in
The aux variables to save the residual contributions to.
Definition: KernelBase.h:64
FEProblemBase & _fe_problem
Reference to this kernel&#39;s FEProblemBase.
virtual Real computeQpJacobian()
Compute this Kernel&#39;s contribution to the Jacobian at the current quadrature point.
Definition: Kernel.h:51
unsigned int _i
current index for the test function
Definition: KernelBase.h:58
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.
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1157
void accumulateTaggedLocalMatrix()
Local Jacobian blocks will be appended by adding the current local kernel Jacobian.
const MooseArray< Real > & _coord
The scaling factor to convert from cartesian to another coordinate system (e.g rz, spherical, etc.)
Definition: KernelBase.h:55
Assembly & _assembly
Reference to this Kernel&#39;s assembly object.
virtual void computeJacobian() override
Compute this Kernel&#39;s contribution to the diagonal Jacobian entries.
Definition: Kernel.C:112
libMesh::Order order() const
Get the order of this variable Note: Order enum can be implicitly converted to unsigned int...
std::vector< AuxVariableName > _save_in_strings
Definition: KernelBase.h:66
unsigned int _j
current index for the shape function
Definition: KernelBase.h:61
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name) const
Gets a reference to a scalar variable with specified number.
Definition: SystemBase.C:145
void addMooseVariableDependency(MooseVariableFieldBase *var)
Call this function to add the passed in MooseVariableFieldBase as a variable that this object depends...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void addVariableToZeroOnResidual(std::string var_name)
Adds this variable to the list of variables to be zeroed during each residual evaluation.
Definition: SystemBase.C:174
DenseVector< Number > _local_re
Holds local residual entries as they are accumulated by this Kernel.
Interface for objects that need to get values of MooseVariables.
Class for scalar variables (they are different).
virtual void addVariableToZeroOnJacobian(std::string var_name)
Adds this variable to the list of variables to be zeroed during each Jacobian evaluation.
Definition: SystemBase.C:180
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealEigenVector
Definition: MooseTypes.h:147
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
virtual Real computeQpOffDiagJacobianScalar(unsigned int)
For coupling scalar variables.
Definition: Kernel.h:61
unsigned int n() const
void prepareVectorTag(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual according to active tags.
static InputParameters validParams()
Definition: KernelBase.C:21
void prepareMatrixTag(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing element jacobian according to the active tags.
virtual void computeOffDiagJacobian(unsigned int jvar) override
Computes d-residual / d-jvar... storing the result in Ke.
Definition: Kernel.C:133
const VariablePhiValue & _phi
the current shape functions
Definition: Kernel.h:81
SystemBase & sys()
Get the system this variable is part of.
bool _is_implicit
If the object is using implicit or explicit form.
virtual void computeOffDiagJacobianScalar(unsigned int jvar) override
Computes jacobian block with respect to a scalar variable.
Definition: Kernel.C:186
unsigned int _qp
The current quadrature point index.
Definition: KernelBase.h:43