https://mooseframework.inl.gov
Loading...
Searching...
No Matches
AuxKernel.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 "AuxKernel.h"
11
12// local includes
13#include "FEProblem.h"
14#include "SubProblem.h"
15#include "AuxiliarySystem.h"
16#include "MooseTypes.h"
17#include "Assembly.h"
19
20#include "libmesh/numeric_vector.h"
21#include "libmesh/dof_map.h"
22#include "libmesh/quadrature.h"
23#include "libmesh/boundary_info.h"
24
25template <typename ComputeValueType>
28{
31 if (typeid(AuxKernelTempl<ComputeValueType>).name() == typeid(VectorAuxKernel).name())
32 params.registerBase("VectorAuxKernel");
33 if (typeid(AuxKernelTempl<ComputeValueType>).name() == typeid(ArrayAuxKernel).name())
34 params.registerBase("ArrayAuxKernel");
35
36 params.registerSystemAttributeName("AuxKernel");
38 return params;
39}
40
41template <typename ComputeValueType>
43 : AuxKernelBase(parameters),
44 MooseVariableInterface<ComputeValueType>(
45 this,
46 parameters.getCheckedPointerParam<SystemBase *>("_sys")
47 ->getVariable(parameters.get<THREAD_ID>("_tid"),
48 parameters.get<AuxVariableName>("variable"))
49 .isNodal(),
50 "variable",
51 Moose::VarKindType::VAR_AUXILIARY,
52 std::is_same<Real, ComputeValueType>::value
53 ? Moose::VarFieldType::VAR_FIELD_STANDARD
54 : (std::is_same<RealVectorValue, ComputeValueType>::value
55 ? Moose::VarFieldType::VAR_FIELD_VECTOR
56 : Moose::VarFieldType::VAR_FIELD_ARRAY)),
57
58 _var(_aux_sys.getActualFieldVariable<ComputeValueType>(
59 _tid, parameters.get<AuxVariableName>("variable"))),
60 _u(isNodal() ? _var.nodalValueArray() : _var.sln()),
61
62 _test(_bnd ? _var.phiFace() : _var.phi()),
63 _q_point(_bnd ? _assembly.qPointsFace() : _assembly.qPoints()),
64 _qrule(_bnd ? _assembly.qRuleFace() : _assembly.qRule()),
65 _JxW(_bnd ? _assembly.JxWFace() : _assembly.JxW()),
66 _coord(_assembly.coordTransformation()),
67
68 _current_elem(_assembly.elem()),
69 _current_side(_assembly.side()),
70 _current_elem_volume(_assembly.elemVolume()),
71 _current_side_volume(_assembly.sideElemVolume()),
72
73 _current_node(_assembly.node()),
74 _current_boundary_id(_assembly.currentBoundaryID()),
75 _solution(_aux_sys.solution()),
76
77 _current_lower_d_elem(_assembly.lowerDElem())
78{
79
80 if (!_bnd || isNodal())
81 // If we're not boundary restricted then we cannot be a coincident lower-d calculation
82 _coincident_lower_d_calc = false;
84
85template <typename ComputeValueType>
87AuxKernelTempl<ComputeValueType>::coupledDot(const std::string & var_name, unsigned int comp) const
88{
89 auto var = getVar(var_name, comp);
90 if (var->kind() == Moose::VAR_AUXILIARY)
92 name(),
93 ": Unable to couple time derivative of an auxiliary variable into the auxiliary system.");
94
95 return Coupleable::coupledDot(var_name, comp);
96}
98template <typename ComputeValueType>
99const VariableValue &
101 unsigned int comp) const
102{
103 auto var = getVar(var_name, comp);
104 if (var->kind() == Moose::VAR_AUXILIARY)
106 name(),
107 ": Unable to couple time derivative of an auxiliary variable into the auxiliary system.");
108
109 return Coupleable::coupledDotDu(var_name, comp);
110}
111
112template <>
113void
115{
116 mooseAssert(_n_shapes == 1,
117 "Should only be calling setDofValue if there is one dof for the aux var");
118 _var.setDofValue(value, 0);
119}
120
121template <>
122void
124{
125 mooseError("Not implemented");
126}
127
128template <typename ComputeValueType>
129void
131{
132 mooseAssert(_coincident_lower_d_calc.has_value(),
133 "We should have set _coincident_lower_d_calc by now");
134 if (_coincident_lower_d_calc.value())
135 _var.insertLower(_aux_sys.solution());
136 else
137 _var.insert(_aux_sys.solution());
138}
139
140template <typename ComputeValueType>
141void
143{
144 precalculateValue();
145
146 if (isNodal()) /* nodal variables */
147 {
148 if (_var.isNodalDefined())
149 {
150 _qp = 0;
151 ComputeValueType value = computeValue();
152 // update variable data, which is referenced by other kernels, so the value is up-to-date
153 _var.setNodalValue(value);
154 }
155 }
156 else /* elemental variables */
157 {
158 _n_shapes =
159 _coincident_lower_d_calc.value() ? _var.dofIndicesLower().size() : _var.numberOfDofs();
160
161 if (_n_shapes == 1) /* p0 */
162 {
163 ComputeValueType value = 0;
164 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
165 value += _JxW[_qp] * _coord[_qp] * computeValue();
166 value /= (_bnd ? _current_side_volume : _current_elem_volume);
167 if (_var.isFV())
168 setDofValueHelper(value);
169 else
170 {
171 // update the variable data referenced by other kernels.
172 // Note that this will update the values at the quadrature points too
173 // (because this is an Elemental variable)
174 if (_coincident_lower_d_calc.value())
175 {
176 _local_sol.resize(1);
177 if constexpr (std::is_same<Real, ComputeValueType>::value)
178 _local_sol(0) = value;
179 else
180 mooseAssert(false, "We should not enter the single dof branch with a vector variable");
181 _var.setLowerDofValues(_local_sol);
182 }
183 else
184 _var.setNodalValue(value);
185 }
186 }
187 else /* high-order */
188 {
189 _local_re.resize(_n_shapes);
190 _local_re.zero();
191 _local_ke.resize(_n_shapes, _n_shapes);
192 _local_ke.zero();
193
194 const auto & test = _coincident_lower_d_calc.value() ? _var.phiLower() : _test;
195
196 // assemble the local mass matrix and the load
197 for (unsigned int i = 0; i < test.size(); i++)
198 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
199 {
200 ComputeValueType t = _JxW[_qp] * _coord[_qp] * test[i][_qp];
201 _local_re(i) += t * computeValue();
202 for (unsigned int j = 0; j < test.size(); j++)
203 _local_ke(i, j) += t * test[j][_qp];
204 }
205 // mass matrix is always SPD but in case of boundary restricted, it will be rank deficient
206 _local_sol.resize(_n_shapes);
207 if (_bnd)
208 _local_ke.svd_solve(_local_re, _local_sol);
209 else
210 _local_ke.cholesky_solve(_local_re, _local_sol);
211
212 _coincident_lower_d_calc.value() ? _var.setLowerDofValues(_local_sol)
213 : _var.setDofValues(_local_sol);
214 }
215 }
216}
217
218template <>
219void
221{
222 precalculateValue();
223
224 if (isNodal()) /* nodal variables */
225 {
226 if (_var.isNodalDefined())
227 {
228 _qp = 0;
229 RealEigenVector value = computeValue();
230 // update variable data, which is referenced by other kernels, so the value is up-to-date
231 _var.setNodalValue(value);
232 }
233 }
234 else /* elemental variables */
235 {
236 mooseAssert(
237 _var.numberOfDofs() % _var.count() == 0,
238 "The number of degrees of freedom should be cleanly divisible by the variable count");
239 _n_shapes = _var.numberOfDofs() / _var.count();
240 if (_n_shapes == 1) /* p0 */
241 {
242 RealEigenVector value = RealEigenVector::Zero(_var.count());
243 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
244 value += _JxW[_qp] * _coord[_qp] * computeValue();
245 value /= (_bnd ? _current_side_volume : _current_elem_volume);
246 // update the variable data referenced by other kernels.
247 // Note that this will update the values at the quadrature points too
248 // (because this is an Elemental variable)
249 _var.setNodalValue(value);
250 }
251 else /* high-order */
252 {
253 _local_re.resize(_n_shapes);
254 for (unsigned int i = 0; i < _local_re.size(); ++i)
255 _local_re(i) = RealEigenVector::Zero(_var.count());
256 _local_ke.resize(_n_shapes, _n_shapes);
257 _local_ke.zero();
258
259 // assemble the local mass matrix and the load
260 for (unsigned int i = 0; i < _test.size(); i++)
261 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
262 {
263 Real t = _JxW[_qp] * _coord[_qp] * _test[i][_qp];
264 _local_re(i) += t * computeValue();
265 for (unsigned int j = 0; j < _test.size(); j++)
266 _local_ke(i, j) += t * _test[j][_qp];
267 }
268
269 // mass matrix is always SPD
270 _local_sol.resize(_n_shapes);
271 for (unsigned int i = 0; i < _local_re.size(); ++i)
272 _local_sol(i) = RealEigenVector::Zero(_var.count());
273 DenseVector<Number> re(_n_shapes);
274 DenseVector<Number> sol(_n_shapes);
275 for (unsigned int i = 0; i < _var.count(); ++i)
276 {
277 for (unsigned int j = 0; j < _n_shapes; ++j)
278 re(j) = _local_re(j)(i);
279
280 if (_bnd)
281 _local_ke.svd_solve(re, sol);
282 else
283 _local_ke.cholesky_solve(re, sol);
284
285 for (unsigned int j = 0; j < _n_shapes; ++j)
286 _local_sol(j)(i) = sol(j);
287 }
288
289 _var.setDofValues(_local_sol);
290 }
291 }
292}
293
294template <typename ComputeValueType>
297{
298 if (_sys.solutionStatesInitialized())
299 mooseError("The solution states have already been initialized when calling ",
300 type(),
301 "::uOld().\n\n",
302 "Make sure to call uOld() within the object constructor.");
303
304 return isNodal() ? _var.nodalValueOldArray() : _var.slnOld();
305}
306
307template <typename ComputeValueType>
310{
311 if (_sys.solutionStatesInitialized())
312 mooseError("The solution states have already been initialized when calling ",
313 type(),
314 "::uOlder().\n\n",
315 "Make sure to call uOlder() within the object constructor.");
316
317 return isNodal() ? _var.nodalValueOlderArray() : _var.slnOlder();
318}
319
320template <typename ComputeValueType>
321bool
326
327template <typename ComputeValueType>
328void
330{
331 mooseAssert(_bnd && !isNodal(),
332 "We can never be a lower-dimensional calculation if we are not boundary restricted "
333 "or if we are a nodal auxiliary kernel");
334
335 // MOOSE maintains three copies of finite element data. One is for the current canonical element,
336 // one is for the element neighbor, and one is for a lower-d element. These three copies have
337 // supported all of MOOSE's finite element use cases to date, including mortar. For AuxKernel, we
338 // do not use the neighbor data copy, but we do use the other two copies. The numberOfDofs() API
339 // returns the number of degrees of freedom for the canonical element. If there are none, then
340 // there must be degrees of freedom associated with the lower-d element
341 _coincident_lower_d_calc = !_var.numberOfDofs();
342
343 if (_coincident_lower_d_calc.value())
344 {
345 static const std::string lower_error = "Make sure that the lower-d variable lives on a "
346 "lower-d block that is a superset of the boundary";
347 if (!_current_lower_d_elem)
348 mooseError("No lower-dimensional element. ", lower_error);
349 if (!_var.dofIndicesLower().size())
350 mooseError("No degrees of freedom. ", lower_error);
351 }
352}
353
354template <typename ComputeValueType>
355std::set<MooseVariableFieldBase *>
357 const libMesh::Node & node, const std::set<MooseVariableFieldBase *> & vars_to_check)
358{
359 if (node.n_dofs(_var.sys().number(), _var.number()))
360 return MooseVariableDependencyInterface::checkVariables(node, vars_to_check);
361 else
362 // the aux kernel early returns if _var has no dofs
363 return {};
364}
365
366// Explicitly instantiates the three versions of the AuxKernelTempl class
367template class AuxKernelTempl<Real>;
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
OutputTools< Real >::VariableValue VariableValue
Definition MooseTypes.h:348
unsigned int THREAD_ID
Definition MooseTypes.h:237
Base class for auxiliary kernels.
static InputParameters validParams()
Base class for creating new auxiliary kernels and auxiliary boundary conditions.
Definition AuxKernel.h:28
AuxKernelTempl(const InputParameters &parameters)
Definition AuxKernel.C:42
void insert()
Insert the just computed values into the auxiliary solution vector.
Definition AuxKernel.C:130
const OutputTools< ComputeValueType >::VariableValue & uOld() const
Retrieves the old value of the variable that this AuxKernel operates on.
Definition AuxKernel.C:296
virtual void compute() override
Computes the value and stores it in the solution vector.
Definition AuxKernel.C:142
void determineWhetherCoincidentLowerDCalc()
Determines whether we're a coincident lower-d calculation.
Definition AuxKernel.C:329
virtual const VariableValue & coupledDotDu(const std::string &var_name, unsigned int comp=0) const override
Time derivative of a coupled variable with respect to the coefficients.
Definition AuxKernel.C:100
virtual const VariableValue & coupledDot(const std::string &var_name, unsigned int comp=0) const override
Time derivative of a coupled variable.
Definition AuxKernel.C:87
bool isMortar()
Definition AuxKernel.C:322
void setDofValueHelper(const ComputeValueType &dof_value)
Currently only used when the auxiliary variable is a finite volume variable, this helps call through ...
const OutputTools< ComputeValueType >::VariableValue & uOlder() const
Retrieves the older value of the variable that this AuxKernel operates on.
Definition AuxKernel.C:309
virtual std::set< MooseVariableFieldBase * > checkVariables(const libMesh::Node &node, const std::set< MooseVariableFieldBase * > &vars_to_check) override
Check whether all of the supplied variables have degree of freedom indices on the supplied node.
Definition AuxKernel.C:356
static InputParameters validParams()
Definition AuxKernel.C:27
virtual const VariableValue & coupledDot(const std::string &var_name, unsigned int comp=0) const
Time derivative of a coupled variable.
virtual const VariableValue & coupledDotDu(const std::string &var_name, unsigned int comp=0) const
Time derivative of a coupled variable with respect to the coefficients.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void registerSystemAttributeName(const std::string &value)
This method is used to define the MOOSE system name that is used by the TheWarehouse object for stori...
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System.
forward declarations
Definition MooseArray.h:18
virtual std::set< MooseVariableFieldBase * > checkVariables(const libMesh::Node &node, const std::set< MooseVariableFieldBase * > &vars_to_check)
Check whether all of the supplied variables have degree of freedom indices on the supplied node.
Interface for objects that need to get values of MooseVariables.
Base class for creating new nodally-based mortar auxiliary kernels.
Base class for a system (of equations)
Definition SystemBase.h:87
unsigned int n_dofs(const unsigned int s, const unsigned int var=libMesh::invalid_uint) const
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
@ VAR_AUXILIARY
Definition MooseTypes.h:776