forked from idaholab/moose
/
Eigenvalue.C
252 lines (208 loc) · 7.72 KB
/
Eigenvalue.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
// MOOSE includes
#include "Eigenvalue.h"
#include "EigenProblem.h"
#include "Factory.h"
#include "MooseApp.h"
#include "NonlinearEigenSystem.h"
#include "SlepcSupport.h"
registerMooseObject("MooseApp", Eigenvalue);
defineLegacyParams(Eigenvalue);
InputParameters
Eigenvalue::validParams()
{
InputParameters params = Steady::validParams();
params.addClassDescription("Eigenvalue solves a standard/generalized eigenvaue problem");
params.addParam<bool>(
"matrix_free",
false,
"Whether or not to use a matrix free fashion to form operators. "
"If true, shell matrices will be used and meanwhile a preconditioning matrix"
"may be formed as well.");
params.addParam<bool>(
"precond_matrix_free",
false,
"Whether or not to use a matrix free fashion for forming the preconditioning matrix. "
"If true, a shell matrix will be used for preconditioner.");
params.addParam<bool>("precond_matrix_includes_eigen",
false,
"Whether or not to include eigen kernels in the preconditioning matrix. "
"If true, the preconditioning matrix will include eigen kernels.");
params.addPrivateParam<bool>("_use_eigen_value", true);
params.addParam<PostprocessorName>(
"normalization", "Postprocessor evaluating norm of eigenvector for normalization");
params.addParam<Real>(
"normal_factor", 1.0, "Normalize eigenvector to make a defined norm equal to this factor");
params.addParam<bool>("auto_initialization",
true,
"If true, we will set an initial eigen vector in moose, otherwise EPS "
"solver will initial eigen vector");
params.addParam<bool>(
"newton_inverse_power", false, "If Newton and Inverse Power is combined in SLEPc side");
// Add slepc options and eigen problems
#ifdef LIBMESH_HAVE_SLEPC
Moose::SlepcSupport::getSlepcValidParams(params);
params += Moose::SlepcSupport::getSlepcEigenProblemValidParams();
#endif
return params;
}
Eigenvalue::Eigenvalue(const InputParameters & parameters)
: Steady(parameters),
_eigen_problem(*getCheckedPointerParam<EigenProblem *>(
"_eigen_problem", "This might happen if you don't have a mesh")),
_normalization(isParamValid("normalization") ? &getPostprocessorValue("normalization")
: nullptr)
{
// Extract and store SLEPc options
#if LIBMESH_HAVE_SLEPC
Moose::SlepcSupport::storeSlepcOptions(_fe_problem, parameters);
Moose::SlepcSupport::storeSlepcEigenProblemOptions(_eigen_problem, parameters);
_eigen_problem.setEigenproblemType(_eigen_problem.solverParams()._eigen_problem_type);
// If need to initialize eigen vector
_eigen_problem.needInitializeEigenVector(getParam<bool>("auto_initialization"));
#endif
if (!parameters.isParamValid("normalization") && parameters.isParamSetByUser("normal_factor"))
paramError("normal_factor",
"Cannot set scaling factor without defining normalization postprocessor.");
}
void
Eigenvalue::init()
{
#if LIBMESH_HAVE_SLEPC
// Set a flag to nonlinear eigen system
_eigen_problem.getNonlinearEigenSystem().precondMatrixIncludesEigenKernels(
getParam<bool>("precond_matrix_includes_eigen"));
#endif
Steady::init();
#if LIBMESH_HAVE_SLEPC
// Make sure all PETSc options are setup correctly
prepareSolverOptions();
// Let do an initial solve if a nonlinear eigen solver but not power is used.
// The initial solver is a Inverse Power, and it is used to compute a good initial
// guess for Newton
auto free_power_iterations = _pars.get<unsigned int>("free_power_iterations");
if (free_power_iterations
&& _eigen_problem.isNonlinearEigenvalueSolver()
&& _eigen_problem.solverParams()._eigen_solve_type != Moose::EST_NONLINEAR_POWER )
{
_eigen_problem.doInitialFreePowerIteration(true);
// Set free power iterations
setFreeNonlinearPowerIterations(free_power_iterations);
// Provide vector of ones to solver
if (_eigen_problem.needInitializeEigenVector())
_eigen_problem.initEigenvector(1.0);
_console << " Free power iteration starts" << std::endl;
// Call solver
_eigen_problem.solve();
// Clear free power iterations
clearFreeNonlinearPowerIterations();
_eigen_problem.doInitialFreePowerIteration(false);
}
#endif
}
void
Eigenvalue::execute()
{
// Let us do extra power iterations here if necessary
auto extra_power_iterations = _pars.get<unsigned int>("extra_power_iterations");
if (extra_power_iterations
&& _eigen_problem.isNonlinearEigenvalueSolver()
&& _eigen_problem.solverParams()._eigen_solve_type != Moose::EST_NONLINEAR_POWER )
{
_eigen_problem.doInitialFreePowerIteration(true);
// Set free power iterations
setFreeNonlinearPowerIterations(extra_power_iterations);
_console << " Extra Free power iteration starts" << std::endl;
// Call solver
_eigen_problem.solve();
// Clear free power iterations
clearFreeNonlinearPowerIterations();
_eigen_problem.doInitialFreePowerIteration(false);
}
if (_eigen_problem.solverParams()._eigen_solve_type != Moose::EST_NONLINEAR_POWER)
_console << " Nonlinear Newton iteration starts" << std::endl;
else
_console << " Nonlinear power iteration starts" << std::endl;
Steady::execute();
}
void Eigenvalue::prepareSolverOptions()
{
#if LIBMESH_HAVE_SLEPC
#if PETSC_RELEASE_LESS_THAN(3, 12, 0)
// Make sure the SLEPc options are setup for this app
Moose::SlepcSupport::slepcSetOptions(_eigen_problem, _pars);
#else
// Options need to be setup once only
if (!_eigen_problem.petscOptionsInserted())
{
// Master app has the default data base
if (!_app.isUltimateMaster())
PetscOptionsPush(_eigen_problem.petscOptionsDatabase());
Moose::SlepcSupport::slepcSetOptions(_eigen_problem, _pars);
if (!_app.isUltimateMaster())
PetscOptionsPop();
_eigen_problem.petscOptionsInserted() = true;
}
#endif
#endif
}
void
Eigenvalue::postSolve()
{
#ifdef LIBMESH_HAVE_SLEPC
if (_normalization)
{
Real val = getParam<Real>("normal_factor");
if (MooseUtils::absoluteFuzzyEqual(*_normalization, 0.0))
mooseError("Cannot normalize eigenvector by 0");
else
val /= *_normalization;
if (!MooseUtils::absoluteFuzzyEqual(val, 1.0))
{
_eigen_problem.scaleEigenvector(val);
// update all aux variables and user objects
for (const ExecFlagType & flag : _app.getExecuteOnEnum().items())
_problem.execute(flag);
}
}
#endif
}
void
Eigenvalue::setFreeNonlinearPowerIterations(unsigned int free_power_iterations)
{
#if LIBMESH_HAVE_SLEPC
#if PETSC_RELEASE_LESS_THAN(3, 12, 0)
// Master app has the default data base
if (!_app.isUltimateMaster())
PetscOptionsPush(_eigen_problem.petscOptionsDatabase());
#endif
Moose::SlepcSupport::setFreeNonlinearPowerIterations(free_power_iterations);
#if PETSC_RELEASE_LESS_THAN(3, 12, 0)
if (!_app.isUltimateMaster())
PetscOptionsPop();
#endif
#endif
}
void
Eigenvalue::clearFreeNonlinearPowerIterations()
{
#if LIBMESH_HAVE_SLEPC
#if PETSC_RELEASE_LESS_THAN(3, 12, 0)
// Master app has the default data base
if (!_app.isUltimateMaster())
PetscOptionsPush(_eigen_problem.petscOptionsDatabase());
#endif
Moose::SlepcSupport::clearFreeNonlinearPowerIterations(_pars);
#if PETSC_RELEASE_LESS_THAN(3, 12, 0)
if (!_app.isUltimateMaster())
PetscOptionsPop();
#endif
#endif
}