https://mooseframework.inl.gov
Loading...
Searching...
No Matches
EigenKernel.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 "EigenKernel.h"
11
12// MOOSE includes
13#include "Assembly.h"
15#include "Executioner.h"
16#include "MooseApp.h"
17#include "MooseEigenSystem.h"
18#include "MooseVariableFE.h"
19#include "FEProblemBase.h"
20
21#include "libmesh/quadrature.h"
22
25{
27 params.addParam<bool>(
28 "eigen", true, "Use for eigenvalue problem (true) or source problem (false)");
29 params.addParam<PostprocessorName>(
30 "eigen_postprocessor", 1.0, "The name of the postprocessor that provides the eigenvalue.");
31 params.registerBase("EigenKernel");
32 return params;
33}
34
36 : Kernel(parameters),
37 _eigen(getParam<bool>("eigen")),
38 _eigen_sys(
39 dynamic_cast<MooseEigenSystem *>(&_fe_problem.getNonlinearSystemBase(_sys.number()))),
40 _eigenvalue(NULL)
41{
42 // The name to the postprocessor storing the eigenvalue
43 std::string eigen_pp_name;
44
45 // If the "eigen_postprocessor" is given, use it. The isParamValid does not work here because of
46 // the default value, which
47 // you don't want to use if an EigenExecutioner exists.
48 if (!isDefaultPostprocessorValue("eigen_postprocessor"))
49 eigen_pp_name = getPostprocessorName("eigen_postprocessor");
50
51 // Attempt to extract the eigenvalue postprocessor from the Executioner
52 else
53 {
55 if (exec)
56 eigen_pp_name = exec->getParam<PostprocessorName>("bx_norm");
57 }
58
59 // If the postprocessor name was not provided and an EigenExecutionerBase is not being used,
60 // use the default value from the "eigen_postprocessor" parameter
61 if (eigen_pp_name.empty())
62 _eigenvalue = &getPostprocessorValue("eigen_postprocessor");
63
64 // If the name does exist, then use the postprocessor value
65 else
66 {
67 if (_is_implicit)
69 else
70 {
72 if (exec)
73 _eigenvalue = &exec->eigenvalueOld();
74 else
76 }
77 }
78}
79
80void
82{
84
86
87 mooseAssert(*_eigenvalue != 0.0, "Can't divide by zero eigenvalue in EigenKernel!");
88 Real one_over_eigen = 1.0 / *_eigenvalue;
89 for (_i = 0; _i < _test.size(); _i++)
90 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
91 _local_re(_i) += _JxW[_qp] * _coord[_qp] * one_over_eigen * computeQpResidual();
92
94 if (_has_save_in)
95 for (const auto & var : _save_in)
96 var->sys().solution().add_vector(_local_re, var->dofIndices());
97}
98
99void
101{
102 if (!_is_implicit)
103 return;
104
106
108 mooseAssert(*_eigenvalue != 0.0, "Can't divide by zero eigenvalue in EigenKernel!");
109 Real one_over_eigen = 1.0 / *_eigenvalue;
110 for (_i = 0; _i < _test.size(); _i++)
111 for (_j = 0; _j < _phi.size(); _j++)
112 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
113 _local_ke(_i, _j) += _JxW[_qp] * _coord[_qp] * one_over_eigen * computeQpJacobian();
114
116
118 {
119 DenseVector<Number> diag = _assembly.getJacobianDiagonal(_local_ke);
120 for (const auto & var : _diag_save_in)
121 var->sys().solution().add_vector(diag, var->dofIndices());
122 }
123}
124
125void
126EigenKernel::computeOffDiagJacobian(const unsigned int jvar_num)
127{
128 if (!_is_implicit)
129 return;
130
131 if (jvar_num == _var.number())
133 else
134 {
135 const auto & jvar = getVariable(jvar_num);
136
138
139 // This (undisplaced) jvar could potentially yield the wrong phi size if this object is acting
140 // on the displaced mesh
141 auto phi_size = jvar.dofIndices().size();
142 mooseAssert(
143 phi_size * jvar.count() == _local_ke.n(),
144 "The size of the phi container does not match the number of local Jacobian columns");
145
146 if (_local_ke.m() != _test.size())
147 return;
148
150 mooseAssert(*_eigenvalue != 0.0, "Can't divide by zero eigenvalue in EigenKernel!");
151 Real one_over_eigen = 1.0 / *_eigenvalue;
152 if (jvar.count() == 1)
153 {
154 for (_i = 0; _i < _test.size(); _i++)
155 for (_j = 0; _j < phi_size; _j++)
156 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
157 _local_ke(_i, _j) +=
158 _JxW[_qp] * _coord[_qp] * one_over_eigen * computeQpOffDiagJacobian(jvar.number());
159 }
160 else
161 {
162 unsigned int n = phi_size;
163 for (_i = 0; _i < _test.size(); _i++)
164 for (_j = 0; _j < n; _j++)
165 for (_qp = 0; _qp < _qrule->n_points(); _qp++)
166 {
167 RealEigenVector v = _JxW[_qp] * _coord[_qp] * one_over_eigen *
169 const_cast<MooseVariableFieldBase &>(jvar)));
170 for (unsigned int k = 0; k < v.size(); ++k)
171 _local_ke(_i, _j + k * n) += v(k);
172 }
173 }
174
176 }
177}
178
179bool
181{
182 bool flag = MooseObject::enabled();
183 if (_eigen)
184 {
185 if (!_eigen_sys)
186 mooseError("Eigen kernel ",
187 name(),
188 " requires a MooseEigenSystem and was designed to work with old eigenvalue",
189 " executioners such as 'NonlinearEigen'. It is suggested to use the new",
190 " eigenvalue executioner 'Eigenvalue' along with kernel tagging");
191
192 if (_is_implicit)
193 return flag && (!_eigen_sys->activeOnOld());
194 else
195 return flag && _eigen_sys->activeOnOld();
196 }
197 else
198 return flag;
199}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
DenseVector< Real > getJacobianDiagonal(const DenseMatrix< Number > &ke)
Definition Assembly.h:1892
This class provides reusable routines for eigenvalue executioners.
const Real & eigenvalueOld()
The old eigenvalue used by inverse power iterations.
MooseEigenSystem * _eigen_sys
EigenKernel always lives in EigenSystem.
Definition EigenKernel.h:41
static InputParameters validParams()
Definition EigenKernel.C:24
virtual void computeOffDiagJacobian(unsigned int jvar) override
Computes d-residual / d-jvar... storing the result in Ke.
EigenKernel(const InputParameters &parameters)
Definition EigenKernel.C:35
virtual bool enabled() const override
Return the enabled status of the object.
bool _eigen
flag for as an eigen kernel or a normal kernel
Definition EigenKernel.h:38
const Real * _eigenvalue
A pointer to the eigenvalue that is stored in a postprocessor This is a pointer so that the method fo...
Definition EigenKernel.h:47
virtual void computeJacobian() override
Compute this Kernel's contribution to the diagonal Jacobian entries.
virtual void computeResidual() override
Compute this Kernel's contribution to the residual.
Definition EigenKernel.C:81
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System.
std::vector< MooseVariableFEBase * > _save_in
Definition KernelBase.h:65
bool _has_diag_save_in
The aux variables to save the diagonal Jacobian contributions to.
Definition KernelBase.h:69
const MooseArray< Real > & _coord
The scaling factor to convert from cartesian to another coordinate system (e.g rz,...
Definition KernelBase.h:55
unsigned int _qp
The current quadrature point index.
Definition KernelBase.h:43
bool _has_save_in
The aux variables to save the residual contributions to.
Definition KernelBase.h:64
const MooseArray< Real > & _JxW
The current quadrature point weight value.
Definition KernelBase.h:52
unsigned int _j
current index for the shape function
Definition KernelBase.h:61
std::vector< MooseVariableFEBase * > _diag_save_in
Definition KernelBase.h:70
unsigned int _i
current index for the test function
Definition KernelBase.h:58
const QBase *const & _qrule
active quadrature rule
Definition KernelBase.h:49
virtual Real computeQpResidual()=0
Compute this Kernel's contribution to the residual at the current quadrature point.
MooseVariable & _var
This is a regular kernel so we cast to a regular MooseVariable.
Definition Kernel.h:72
const VariablePhiValue & _phi
the current shape functions
Definition Kernel.h:81
virtual RealEigenVector computeQpOffDiagJacobianArray(const ArrayMooseVariable &jvar)
For coupling array variables.
Definition Kernel.h:66
virtual Real computeQpOffDiagJacobian(unsigned int)
For coupling standard variables.
Definition Kernel.h:56
const VariableTestValue & _test
the current test function
Definition Kernel.h:75
static InputParameters validParams()
Definition Kernel.C:24
virtual Real computeQpJacobian()
Compute this Kernel's contribution to the Jacobian at the current quadrature point.
Definition Kernel.h:51
Executioner * getExecutioner() const
Retrieve the Executioner for this App.
Definition MooseApp.C:2015
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
bool activeOnOld()
Return if eigen kernels should be on old solution.
virtual bool enabled() const
Return the enabled status of the object.
Definition MooseObject.h:49
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
unsigned int number() const
Get variable number coming from libMesh.
This class provides an interface for common operations on field variables of both FE and FV types wit...
bool isDefaultPostprocessorValue(const std::string &param_name, const unsigned int index=0) const
Determine whether or not the Postprocessor is a default value.
const PostprocessorName & getPostprocessorName(const std::string &param_name, const unsigned int index=0) const
Get the name of a postprocessor.
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
Retrieve the value of the Postprocessor.
const PostprocessorValue & getPostprocessorValueOldByName(const PostprocessorName &name) const
const PostprocessorValue & getPostprocessorValue(const std::string &param_name, const unsigned int index=0) const
doco-normal-methods-begin Retrieve the value of a Postprocessor or one of it's old or older values
virtual void precalculateJacobian()
Assembly & _assembly
Reference to this Kernel's assembly object.
const MooseVariableFieldBase & getVariable(unsigned int jvar_num) const
Retrieve the variable object from our system associated with jvar_num.
SystemBase & _sys
Reference to the EquationSystem object.
virtual void precalculateOffDiagJacobian(unsigned int)
virtual void precalculateResidual()
bool computingScalingJacobian() const
Whether we are computing an initial Jacobian for automatic variable scaling.
DenseMatrix< Number > _local_ke
Holds local Jacobian entries as they are accumulated by this Kernel.
void accumulateTaggedLocalMatrix()
Local Jacobian blocks will be appended by adding the current local kernel Jacobian.
void prepareMatrixTag(Assembly &assembly, unsigned int ivar, unsigned int jvar)
Prepare data for computing element jacobian according to the active tags.
void accumulateTaggedLocalResidual()
Local residual blocks will be appended by adding the current local kernel residual.
void prepareVectorTag(Assembly &assembly, unsigned int ivar)
Prepare data for computing element residual according to active tags.
DenseVector< Number > _local_re
Holds local residual entries as they are accumulated by this Kernel.
bool _is_implicit
If the object is using implicit or explicit form.
unsigned int n() const
unsigned int m() const