https://mooseframework.inl.gov
EigenProblemSolve.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 // MOOSE includes
11 #include "EigenProblem.h"
12 #include "Factory.h"
13 #include "MooseApp.h"
14 #include "NonlinearEigenSystem.h"
15 #include "PetscSupport.h"
16 #include "SlepcSupport.h"
17 #include "UserObject.h"
18 
19 #include "libmesh/petsc_solver_exception.h"
20 
21 // Needed for LIBMESH_CHECK_ERR
23 
26 {
28 
29  params.addClassDescription(
30  "Solve object for a standard/generalized linear or nonlinear eigenvalue problem");
31 
32  params += FEProblemSolve::validParams();
33 
34  params.addParam<bool>(
35  "matrix_free",
36  false,
37  "Whether or not to use a matrix free fashion to form operators. "
38  "If true, shell matrices will be used and meanwhile a preconditioning matrix"
39  "may be formed as well.");
40 
41  params.addParam<bool>(
42  "precond_matrix_free",
43  false,
44  "Whether or not to use a matrix free fashion for forming the preconditioning matrix. "
45  "If true, a shell matrix will be used for preconditioner.");
46 
47  params.addParam<bool>("constant_matrices",
48  false,
49  "Whether or not to use constant matrices so that we can use them to form "
50  "residuals on both linear and "
51  "nonlinear iterations");
52 
53  params.addParam<bool>(
54  "precond_matrix_includes_eigen",
55  false,
56  "Whether or not to include eigen kernels in the preconditioning matrix. "
57  "If true, the preconditioning matrix could be singular with the converged eigenvalue if the "
58  "full matrix is assembled and the derivative of eigenvalue with respect to the solution "
59  "vector is not considered.");
60 
61  params.addPrivateParam<bool>("_use_eigen_value", true);
62 
63  params.addParam<Real>("initial_eigenvalue", 1, "Initial eigenvalue");
64  params.addParam<PostprocessorName>(
65  "normalization", "Postprocessor evaluating norm of eigenvector for normalization");
66  params.addParam<Real>("normal_factor",
67  "Normalize eigenvector to make a defined norm equal to this factor");
68 
69  params.addParam<bool>("auto_initialization",
70  true,
71  "If true, we will set an initial eigen vector in moose, otherwise EPS "
72  "solver will initialize eigen vector");
73 
74  params.addParamNamesToGroup("matrix_free precond_matrix_free constant_matrices "
75  "precond_matrix_includes_eigen",
76  "Matrix and Matrix-Free");
77  params.addParamNamesToGroup("initial_eigenvalue auto_initialization",
78  "Eigenvector and eigenvalue initialization");
79  params.addParamNamesToGroup("normalization normal_factor", "Solution normalization");
80 
81  // If Newton and Inverse Power is combined in SLEPc side
82  params.addPrivateParam<bool>("_newton_inverse_power", false);
83 
84 // Add slepc options and eigen problems
85 #ifdef LIBMESH_HAVE_SLEPC
87 
89 #endif
90  return params;
91 }
92 
94  : FEProblemSolve(ex),
95  _eigen_problem(*getCheckedPointerParam<EigenProblem *>(
96  "_eigen_problem", "This might happen if you don't have a mesh")),
97  _normalization(isParamValid("normalization") ? &getPostprocessorValue("normalization")
98  : nullptr)
99 {
100 // Extract and store SLEPc options
101 #ifdef LIBMESH_HAVE_SLEPC
102  mooseAssert(_problem.numSolverSystems() == 1,
103  "The Eigenvalue executioner only currently supports a single solver system.");
104 
105  const auto & params = ex.parameters();
107 
110  _eigen_problem.solverParams(/*solver_sys_num=*/0)._eigen_problem_type);
111 
112  // pass two control parameters to eigen problem
113  _eigen_problem.solverParams(/*solver_sys_num=*/0)._free_power_iterations =
114  getParam<unsigned int>("free_power_iterations");
115  _eigen_problem.solverParams(/*solver_sys_num=*/0)._extra_power_iterations =
116  getParam<unsigned int>("extra_power_iterations");
117 
118  if (!isParamValid("normalization") && isParamValid("normal_factor"))
119  paramError("normal_factor",
120  "Cannot set scaling factor without defining normalization postprocessor.");
121 
122  if (isParamValid("normalization"))
123  {
124  const auto & normpp = getParam<PostprocessorName>("normalization");
125  if (isParamValid("normal_factor"))
126  _eigen_problem.setNormalization(normpp, getParam<Real>("normal_factor"));
127  else
129  }
130 
131  _eigen_problem.setInitialEigenvalue(getParam<Real>("initial_eigenvalue"));
132 
133  // Set a flag to nonlinear eigen system
134  _eigen_problem.getNonlinearEigenSystem(/*nl_sys_num=*/0)
135  .precondMatrixIncludesEigenKernels(getParam<bool>("precond_matrix_includes_eigen"));
136 #else
137  mooseError("SLEPc is required to use Eigenvalue executioner, please use '--download-slepc in "
138  "PETSc configuration'");
139 #endif
140  // SLEPc older than 3.13.0 can not take initial guess from moose
141  // It may generate converge issues
142 #if PETSC_RELEASE_LESS_THAN(3, 13, 0)
143  mooseError(
144  "Please use SLEPc-3.13.0 or higher. Old versions of SLEPc likely produce bad convergence");
145 #endif
146 
147  // To avoid petsc unused option warnings, ensure we do not set irrelevant options.
156 }
157 
158 void
160 {
161  if (isParamValid("normalization"))
162  {
163  const auto & normpp = getParam<PostprocessorName>("normalization");
164  const auto & exec = _eigen_problem.getUserObject<UserObject>(normpp).getExecuteOnEnum();
165  if (!exec.isValueSet(EXEC_LINEAR))
166  mooseError("Normalization postprocessor ", normpp, " requires execute_on = 'linear'");
167  }
168 
169 #ifdef LIBMESH_HAVE_SLEPC
170  // Options need to be setup once only
172  {
173  // Parent application has the default data base
174  if (!_app.isUltimateMaster())
175  LibmeshPetscCall(PetscOptionsPush(_eigen_problem.petscOptionsDatabase()));
177  _eigen_problem, _eigen_problem.solverParams(/*eigen_sys_num=*/0), _pars);
178  if (!_app.isUltimateMaster())
179  LibmeshPetscCall(PetscOptionsPop());
181  }
182 #endif
183 }
FEProblemBase & _problem
Reference to FEProblem.
Definition: SolveObject.h:47
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
bool _eigen_matrix_free
Definition: SolverParams.h:27
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition: MooseApp.h:840
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:394
T & getUserObject(const std::string &name, unsigned int tid=0) const
Get the user object by its name.
void precondMatrixIncludesEigenKernels(bool precond_matrix_includes_eigen)
If the preconditioning matrix includes eigen kernels.
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:467
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
virtual void initialSetup() override
Method that should be executed once, before any solve calls.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
bool & petscOptionsInserted()
If PETSc options are already inserted.
InputParameters getSlepcValidParams(InputParameters &params)
Definition: SlepcSupport.C:37
void setNormalization(const PostprocessorName &pp, const Real value=std::numeric_limits< Real >::max())
Set postprocessor and normalization factor &#39;Postprocessor&#39; is often used to compute an integral of ph...
Definition: EigenProblem.C:630
Moose::EigenProblemType _eigen_problem_type
Definition: SolverParams.h:25
static InputParameters validParams()
Definition: Executioner.C:26
void slepcSetOptions(EigenProblem &eigen_problem, SolverParams &solver_params, const InputParameters &params)
Push all SLEPc/PETSc options into SLEPc/PETSc side.
Definition: SlepcSupport.C:550
void dontAddPetscFlag(const std::string &flag, PetscOptions &petsc_options)
Function to ensure that a particular petsc option is not added to the PetscOptions storage object to ...
Executioners are objects that do the actual work of solving your problem.
Definition: Executioner.h:30
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:385
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:31
static InputParameters validParams()
NonlinearEigenSystem & getNonlinearEigenSystem(const unsigned int nl_sys_num)
Definition: EigenProblem.h:299
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscOptions & petscOptionsDatabase()
void dontAddNonlinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -snes_converged_reason is not added to the PetscOptions storage object to be ...
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
unsigned int _free_power_iterations
Definition: SolverParams.h:31
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
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 dontAddLinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be l...
void setInitialEigenvalue(const Real initial_eigenvalue)
Set an initial eigenvalue for initial normalization.
Definition: EigenProblem.h:69
void setEigenProblemSolverParams(EigenProblem &eigen_problem, const InputParameters &params)
Retrieve eigen problem params from &#39;params&#39;, and then set these params into SolverParams.
Definition: SlepcSupport.C:212
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:209
unsigned int _extra_power_iterations
Definition: SolverParams.h:32
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21
void storeSolveType(FEProblemBase &fe_problem, const InputParameters &params)
Set solve type into eigen problem (solverParams)
Definition: SlepcSupport.C:286
EigenProblem & _eigen_problem
virtual std::size_t numSolverSystems() const override
void setEigenproblemType(Moose::EigenProblemType eigen_problem_type)
Set eigen problem type.
Base class for user-specific data.
Definition: UserObject.h:19
InputParameters getSlepcEigenProblemValidParams()
Retrieve valid params that allow users to specify eigen problem configuration.
Definition: SlepcSupport.C:66
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
EigenProblemSolve(Executioner &ex)