www.mooseframework.org
Functions | Variables
Moose::SlepcSupport Namespace Reference

Functions

InputParameters getSlepcValidParams (InputParameters &params)
 
InputParameters getSlepcEigenProblemValidParams ()
 Retrieve valid params that allow users to specify eigen problem configuration. More...
 
void storeSolveType (FEProblemBase &fe_problem, const InputParameters &params)
 Set solve type into eigen problem (solverParams) More...
 
void setEigenProblemSolverParams (EigenProblem &eigen_problem, const InputParameters &params)
 Retrieve eigen problem params from 'params', and then set these params into SolverParams. More...
 
void slepcSetOptions (EigenProblem &eigen_problem, const InputParameters &params)
 Push all SLEPc/PETSc options into SLEPc/PETSc side. More...
 
void setSlepcEigenSolverTolerances (EigenProblem &eigen_problem, const InputParameters &params)
 Control eigen solver tolerances via SLEPc options. More...
 
void setFreeNonlinearPowerIterations (unsigned int free_power_iterations)
 Set SLEPc/PETSc options to trigger free power iteration. More...
 
void clearFreeNonlinearPowerIterations (const InputParameters &params)
 
void moosePetscSNESFormMatrixTag (SNES snes, Vec x, Mat mat, void *ctx, TagID tag)
 Form matrix according to tag. More...
 
void moosePetscSNESFormMatricesTags (SNES snes, Vec x, std::vector< Mat > &mats, void *ctx, const std::set< TagID > &tags)
 Form multiple matrices for multiple tags. More...
 
PetscErrorCode mooseSlepcEigenFormJacobianA (SNES snes, Vec x, Mat jac, Mat pc, void *ctx)
 Form Jacobian matrix A. More...
 
PetscErrorCode mooseSlepcEigenFormJacobianB (SNES snes, Vec x, Mat jac, Mat pc, void *ctx)
 Form Jacobian matrix B. More...
 
PetscErrorCode mooseSlepcEigenFormFunctionA (SNES snes, Vec x, Vec r, void *ctx)
 Form function residual Ax. More...
 
PetscErrorCode mooseSlepcEigenFormFunctionB (SNES snes, Vec x, Vec r, void *ctx)
 Form function residual Bx. More...
 
PetscErrorCode mooseSlepcEigenFormFunctionAB (SNES snes, Vec x, Vec Ax, Vec Bx, void *ctx)
 Form function residual Ax-Bx. More...
 
PetscErrorCode mooseSlepcStoppingTest (EPS eps, PetscInt its, PetscInt max_it, PetscInt nconv, PetscInt nev, EPSConvergedReason *reason, void *ctx)
 A customized convergence checker. More...
 
PetscErrorCode mooseSlepcEPSGetSNES (EPS eps, SNES *snes)
 Retrieve SNES from EPS. More...
 
PetscErrorCode mooseSlepcEPSMonitor (EPS eps, PetscInt its, PetscInt nconv, PetscScalar *eigr, PetscScalar *eigi, PetscReal *errest, PetscInt nest, void *mctx)
 A customized solver monitor to print out eigenvalue. More...
 
PetscErrorCode mooseSlepcEPSSNESSetUpOptionPrefix (EPS eps)
 Get rid of prefix "-eps_power" for SNES, KSP, PC, etc. More...
 
PetscErrorCode mooseSlepcEPSSNESSetCustomizePC (EPS eps)
 Attach a customized PC. More...
 
PetscErrorCode mooseSlepcEPSSNESKSPSetPCSide (FEProblemBase &problem, EPS eps)
 Allow users to specify PC side. More...
 
void attachCallbacksToMat (EigenProblem &eigen_problem, Mat mat, bool eigen)
 Attach call backs to mat. More...
 
PetscErrorCode mooseMatMult_Eigen (Mat mat, Vec x, Vec y)
 Implement MatMult via function evaluation for Bx. More...
 
PetscErrorCode mooseMatMult_NonEigen (Mat mat, Vec x, Vec y)
 Implement MatMult via function evaluation for Ax. More...
 
void setOperationsForShellMat (EigenProblem &eigen_problem, Mat mat, bool eigen)
 Set operations to shell mat. More...
 
PETSC_EXTERN PetscErrorCode PCCreate_MoosePC (PC pc)
 Create a preconditioner from moose side. More...
 
PETSC_EXTERN PetscErrorCode registerPCToPETSc ()
 Let PETSc know there is a preconditioner. More...
 
PetscErrorCode PCDestroy_MoosePC (PC pc)
 Destroy preconditioner. More...
 
PetscErrorCode PCView_MoosePC (PC pc, PetscViewer viewer)
 View preconditioner. More...
 
PetscErrorCode PCApply_MoosePC (PC pc, Vec x, Vec y)
 Preconditioner application. More...
 
PetscErrorCode PCSetUp_MoosePC (PC pc)
 Setup preconditioner. More...
 
PetscErrorCode mooseSlepcEigenFormFunctionMFFD (void *ctx, Vec x, Vec r)
 Function call for MFFD. More...
 
void setEigenProblemOptions (SolverParams &solver_params)
 
void setWhichEigenPairsOptions (SolverParams &solver_params)
 
void setNewtonPetscOptions (SolverParams &solver_params, const InputParameters &params)
 
void setNonlinearPowerOptions (SolverParams &solver_params)
 
void setEigenSolverOptions (SolverParams &solver_params, const InputParameters &params)
 
PetscErrorCode mooseEPSFormMatrices (EigenProblem &eigen_problem, EPS eps, Vec x, void *ctx)
 
void moosePetscSNESFormFunction (SNES, Vec x, Vec r, void *ctx, TagID tag)
 
PetscErrorCode mooseSlepcEigenFormNorm (SNES, Vec, PetscReal *norm, void *ctx)
 
void mooseMatMult (EigenProblem &eigen_problem, Vec x, Vec r, TagID tag)
 

Variables

const int subspace_factor = 2
 

Function Documentation

◆ attachCallbacksToMat()

void Moose::SlepcSupport::attachCallbacksToMat ( EigenProblem eigen_problem,
Mat  mat,
bool  eigen 
)

Attach call backs to mat.

SLEPc solver will retrieve callbacks we attached here

Definition at line 982 of file SlepcSupport.C.

Referenced by NonlinearEigenSystem::attachSLEPcCallbacks().

983 {
984  // Recall that we are solving the potentially nonlinear problem:
985  // F(x) = A(x) - \lambda B(x) = 0
986  //
987  // To solve this, we can use Newton's method: J \Delta x = -F
988  // Generally we will approximate J using matrix free methods. However, in order to solve the
989  // linearized system efficiently, we typically will need preconditioning. Typically we will build
990  // the preconditioner only from A, but we also have the option to include information from B
991 
992  // Attach the Jacobian computation function. If \p mat is the "eigen" matrix corresponding to B,
993  // then attach our JacobianB computation routine, else the matrix corresponds to A, and we attach
994  // the JacobianA computation routine
995  PetscObjectComposeFunction((PetscObject)mat,
996  "formJacobian",
999 
1000  // Attach the residual computation function. If \p mat is the "eigen" matrix corresponding to B,
1001  // then attach our FunctionB computation routine, else the matrix corresponds to A, and we attach
1002  // the FunctionA computation routine
1003  PetscObjectComposeFunction((PetscObject)mat,
1004  "formFunction",
1007 
1008  // It's also beneficial to be able to evaluate both A and B residuals at once
1009  PetscObjectComposeFunction(
1010  (PetscObject)mat, "formFunctionAB", Moose::SlepcSupport::mooseSlepcEigenFormFunctionAB);
1011 
1012  // Users may choose to provide a custom measure of the norm of B (Bx for a linear system)
1013  if (eigen_problem.bxNormProvided())
1014  PetscObjectComposeFunction(
1015  (PetscObject)mat, "formNorm", Moose::SlepcSupport::mooseSlepcEigenFormNorm);
1016 
1017  // Finally we need to attach the "context" object, which is our EigenProblem, to the matrices so
1018  // that eventually when we get callbacks from SLEPc we can call methods on the EigenProblem
1019  PetscContainer container;
1020  PetscContainerCreate(eigen_problem.comm().get(), &container);
1021  PetscContainerSetPointer(container, &eigen_problem);
1022  PetscObjectCompose((PetscObject)mat, "formJacobianCtx", (PetscObject)container);
1023  PetscObjectCompose((PetscObject)mat, "formFunctionCtx", (PetscObject)container);
1024  if (eigen_problem.bxNormProvided())
1025  PetscObjectCompose((PetscObject)mat, "formNormCtx", (PetscObject)container);
1026  PetscContainerDestroy(&container);
1027 }
PetscErrorCode mooseSlepcEigenFormJacobianB(SNES snes, Vec x, Mat jac, Mat pc, void *ctx)
Form Jacobian matrix B.
Definition: SlepcSupport.C:746
PetscErrorCode mooseSlepcEigenFormFunctionAB(SNES snes, Vec x, Vec Ax, Vec Bx, void *ctx)
Form function residual Ax-Bx.
Definition: SlepcSupport.C:899
const Parallel::Communicator & comm() const
PetscErrorCode mooseSlepcEigenFormFunctionA(SNES snes, Vec x, Vec r, void *ctx)
Form function residual Ax.
Definition: SlepcSupport.C:820
PetscErrorCode mooseSlepcEigenFormNorm(SNES, Vec, PetscReal *norm, void *ctx)
Definition: SlepcSupport.C:973
PetscErrorCode mooseSlepcEigenFormJacobianA(SNES snes, Vec x, Mat jac, Mat pc, void *ctx)
Form Jacobian matrix A.
Definition: SlepcSupport.C:651
PetscErrorCode mooseSlepcEigenFormFunctionB(SNES snes, Vec x, Vec r, void *ctx)
Form function residual Bx.
Definition: SlepcSupport.C:859
bool bxNormProvided() const
Whether a Bx norm postprocessor has been provided.
Definition: EigenProblem.h:235

◆ clearFreeNonlinearPowerIterations()

void Moose::SlepcSupport::clearFreeNonlinearPowerIterations ( const InputParameters params)

Definition at line 365 of file SlepcSupport.C.

Referenced by EigenProblem::doFreeNonlinearPowerIterations().

366 {
367  Moose::PetscSupport::setSinglePetscOption("-eps_power_update", "1");
368  Moose::PetscSupport::setSinglePetscOption("-eps_max_it", "1");
370  stringify(params.get<unsigned int>("nl_max_its")));
372  stringify(params.get<Real>("nl_rel_tol")));
373  Moose::PetscSupport::setSinglePetscOption("-eps_tol", stringify(params.get<Real>("eigen_tol")));
374 }
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:62
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ getSlepcEigenProblemValidParams()

InputParameters Moose::SlepcSupport::getSlepcEigenProblemValidParams ( )

Retrieve valid params that allow users to specify eigen problem configuration.

Definition at line 66 of file SlepcSupport.C.

Referenced by Eigenvalue::validParams().

67 {
69 
70  // We are solving a Non-Hermitian eigenvalue problem by default
71  MooseEnum eigen_problem_type("HERMITIAN NON_HERMITIAN GEN_HERMITIAN GEN_NON_HERMITIAN "
72  "GEN_INDEFINITE POS_GEN_NON_HERMITIAN SLEPC_DEFAULT",
73  "GEN_NON_HERMITIAN");
74  params.addParam<MooseEnum>(
75  "eigen_problem_type",
76  eigen_problem_type,
77  "Type of the eigenvalue problem we are solving "
78  "HERMITIAN: Hermitian "
79  "NON_HERMITIAN: Non-Hermitian "
80  "GEN_HERMITIAN: Generalized Hermitian "
81  "GEN_NON_HERMITIAN: Generalized Non-Hermitian "
82  "GEN_INDEFINITE: Generalized indefinite Hermitian "
83  "POS_GEN_NON_HERMITIAN: Generalized Non-Hermitian with positive (semi-)definite B "
84  "SLEPC_DEFAULT: Use whatever SLEPC has by default ");
85 
86  // Which eigenvalues are we interested in
87  MooseEnum which_eigen_pairs("LARGEST_MAGNITUDE SMALLEST_MAGNITUDE LARGEST_REAL SMALLEST_REAL "
88  "LARGEST_IMAGINARY SMALLEST_IMAGINARY TARGET_MAGNITUDE TARGET_REAL "
89  "TARGET_IMAGINARY ALL_EIGENVALUES SLEPC_DEFAULT");
90  params.addParam<MooseEnum>("which_eigen_pairs",
91  which_eigen_pairs,
92  "Which eigenvalue pairs to obtain from the solution "
93  "LARGEST_MAGNITUDE "
94  "SMALLEST_MAGNITUDE "
95  "LARGEST_REAL "
96  "SMALLEST_REAL "
97  "LARGEST_IMAGINARY "
98  "SMALLEST_IMAGINARY "
99  "TARGET_MAGNITUDE "
100  "TARGET_REAL "
101  "TARGET_IMAGINARY "
102  "ALL_EIGENVALUES "
103  "SLEPC_DEFAULT ");
104 
105  params.addParam<unsigned int>("n_eigen_pairs", 1, "The number of eigen pairs");
106  params.addParam<unsigned int>("n_basis_vectors", 3, "The dimension of eigen subspaces");
107 
108  params.addParam<Real>("eigen_tol", 1.0e-4, "Relative Tolerance for Eigen Solver");
109  params.addParam<unsigned int>("eigen_max_its", 10000, "Max Iterations for Eigen Solver");
110 
111  params.addParam<Real>("l_abs_tol", 1e-50, "Absolute Tolerances for Linear Solver");
112 
113  params.addParam<unsigned int>("free_power_iterations", 4, "The number of free power iterations");
114 
115  params.addParam<unsigned int>(
116  "extra_power_iterations", 0, "The number of extra free power iterations");
117 
118  params.addParamNamesToGroup(
119  "eigen_problem_type which_eigen_pairs n_eigen_pairs n_basis_vectors eigen_tol eigen_max_its "
120  "free_power_iterations extra_power_iterations",
121  "Eigen Solver");
122 
123  return params;
124 }
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters emptyInputParameters()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
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...

◆ getSlepcValidParams()

InputParameters Moose::SlepcSupport::getSlepcValidParams ( InputParameters params)
Returns
InputParameters object containing the SLEPC related parameters

The output of this function should be added to the the parameters object of the overarching class

See also
EigenProblem

Definition at line 37 of file SlepcSupport.C.

Referenced by Eigenvalue::validParams().

38 {
39  MooseEnum solve_type("POWER ARNOLDI KRYLOVSCHUR JACOBI_DAVIDSON "
40  "NONLINEAR_POWER NEWTON PJFNK PJFNKMO JFNK",
41  "PJFNK");
42  params.set<MooseEnum>("solve_type") = solve_type;
43 
44  params.setDocString("solve_type",
45  "POWER: Power / Inverse / RQI "
46  "ARNOLDI: Arnoldi "
47  "KRYLOVSCHUR: Krylov-Schur "
48  "JACOBI_DAVIDSON: Jacobi-Davidson "
49  "NONLINEAR_POWER: Nonlinear Power "
50  "NEWTON: Newton "
51  "PJFNK: Preconditioned Jacobian-free Newton-Kyrlov"
52  "JFNK: Jacobian-free Newton-Kyrlov"
53  "PJFNKMO: Preconditioned Jacobian-free Newton-Kyrlov with Matrix Only");
54 
55  // When the eigenvalue problems is reformed as a coupled nonlinear system,
56  // we use part of Jacobian as the preconditioning matrix.
57  // Because the difference between the Jacobian and the preconditioning matrix is not small,
58  // the linear solver KSP can not reduce the residual much. After several tests,
59  // we find 1e-2 is a reasonable choice.
60  params.set<Real>("l_tol") = 1e-2;
61 
62  return params;
63 }
void setDocString(const std::string &name, const std::string &doc)
Set the doc string of a parameter.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ mooseEPSFormMatrices()

PetscErrorCode Moose::SlepcSupport::mooseEPSFormMatrices ( EigenProblem eigen_problem,
EPS  eps,
Vec  x,
void ctx 
)

Definition at line 514 of file SlepcSupport.C.

Referenced by mooseSlepcEigenFormFunctionA(), mooseSlepcEigenFormFunctionAB(), mooseSlepcEigenFormFunctionB(), and mooseSlepcEigenFormJacobianA().

515 {
516  PetscErrorCode ierr;
517  ST st;
518  Mat A, B;
519  PetscBool aisshell, bisshell;
520  PetscFunctionBegin;
521 
522  if (eigen_problem.constantMatrices() && eigen_problem.wereMatricesFormed())
523  PetscFunctionReturn(0);
524 
525  if (eigen_problem.onLinearSolver())
526  // We reach here during linear iteration when solve type is PJFNKMO.
527  // We will use the matrices assembled at the beginning of this Newton
528  // iteration for the following residual evaluation.
529  PetscFunctionReturn(0);
530 
531  NonlinearEigenSystem & eigen_nl = eigen_problem.getCurrentNonlinearEigenSystem();
532  SNES snes = eigen_nl.getSNES();
533  // Rest ST state so that we can retrieve matrices
534  ierr = EPSGetST(eps, &st);
535  CHKERRQ(ierr);
536  ierr = STResetMatrixState(st);
537  CHKERRQ(ierr);
538  ierr = EPSGetOperators(eps, &A, &B);
539  CHKERRQ(ierr);
540  ierr = PetscObjectTypeCompare((PetscObject)A, MATSHELL, &aisshell);
541  CHKERRQ(ierr);
542  ierr = PetscObjectTypeCompare((PetscObject)B, MATSHELL, &bisshell);
543  CHKERRQ(ierr);
544  if (aisshell || bisshell)
545  {
546  SETERRQ(PetscObjectComm((PetscObject)eps),
547  PETSC_ERR_ARG_INCOMP,
548  "A and B matrices can not be shell matrices when using PJFNKMO \n");
549  }
550  // Form A and B
551  std::vector<Mat> mats = {A, B};
553  snes, x, mats, ctx, {eigen_nl.nonEigenMatrixTag(), eigen_nl.eigenMatrixTag()});
554  eigen_problem.wereMatricesFormed(true);
555  PetscFunctionReturn(0);
556 }
Nonlinear eigenvalue system to be solved.
bool constantMatrices() const
Whether or not matrices are constant.
Definition: EigenProblem.h:210
bool wereMatricesFormed() const
Whether or not constant matrices were already formed.
Definition: EigenProblem.h:220
int eps(unsigned int i, unsigned int j)
2D version
ierr
void moosePetscSNESFormMatricesTags(SNES snes, Vec x, std::vector< Mat > &mats, void *ctx, const std::set< TagID > &tags)
Form multiple matrices for multiple tags.
Definition: SlepcSupport.C:589
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
bool onLinearSolver() const
Whether or not we are in a linear solver iteration.
Definition: EigenProblem.h:200
virtual SNES getSNES() override
Retrieve snes from slepc eigen solver.
TagID eigenMatrixTag() const
Matrix tag ID of right hand side.
TagID nonEigenMatrixTag() const
Matrix tag ID of left hand side.

◆ mooseMatMult()

void Moose::SlepcSupport::mooseMatMult ( EigenProblem eigen_problem,
Vec  x,
Vec  r,
TagID  tag 
)

Definition at line 1030 of file SlepcSupport.C.

Referenced by mooseMatMult_Eigen(), and mooseMatMult_NonEigen().

1031 {
1032  NonlinearSystemBase & nl = eigen_problem.currentNonlinearSystem();
1033  System & sys = nl.system();
1034 
1035  PetscVector<Number> X_global(x, sys.comm()), R(r, sys.comm());
1036 
1037  PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
1038 
1039  // Use the system's update() to get a good local version of the
1040  // parallel solution. This operation does not modify the incoming
1041  // "x" vector, it only localizes information from "x" into
1042  // sys.current_local_solution.
1043  X_global.swap(X_sys);
1044  sys.update();
1045  X_global.swap(X_sys);
1046 
1047  R.zero();
1048 
1049  eigen_problem.computeResidualTag(*sys.current_local_solution.get(), R, tag);
1050 
1051  R.close();
1052 }
virtual void computeResidualTag(const NumericVector< Number > &soln, NumericVector< Number > &residual, TagID tag) override
Form a vector for all kernels and BCs with a given tag.
Definition: EigenProblem.C:286
Nonlinear system to be solved.
NonlinearSystemBase & currentNonlinearSystem()
virtual System & system() override
Get the reference to the libMesh system.

◆ mooseMatMult_Eigen()

PetscErrorCode Moose::SlepcSupport::mooseMatMult_Eigen ( Mat  mat,
Vec  x,
Vec  y 
)

Implement MatMult via function evaluation for Bx.

Definition at line 1055 of file SlepcSupport.C.

Referenced by setOperationsForShellMat().

1056 {
1057  PetscFunctionBegin;
1058  void * ctx = nullptr;
1059  MatShellGetContext(mat, &ctx);
1060 
1061  if (!ctx)
1062  mooseError("No context is set for shell matrix ");
1063 
1064  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
1065  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
1066 
1067  mooseMatMult(*eigen_problem, x, r, eigen_nl.eigenVectorTag());
1068 
1069  if (eigen_problem->negativeSignEigenKernel())
1070  VecScale(r, -1.);
1071 
1072  PetscFunctionReturn(0);
1073 }
Nonlinear eigenvalue system to be solved.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
TagID eigenVectorTag() const
Vector tag ID of right hand side.
bool negativeSignEigenKernel() const
A flag indicates if a negative sign is used in eigen kernels.
Definition: EigenProblem.h:60
void mooseMatMult(EigenProblem &eigen_problem, Vec x, Vec r, TagID tag)
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseMatMult_NonEigen()

PetscErrorCode Moose::SlepcSupport::mooseMatMult_NonEigen ( Mat  mat,
Vec  x,
Vec  y 
)

Implement MatMult via function evaluation for Ax.

Definition at line 1076 of file SlepcSupport.C.

Referenced by setOperationsForShellMat().

1077 {
1078  PetscFunctionBegin;
1079  void * ctx = nullptr;
1080  MatShellGetContext(mat, &ctx);
1081 
1082  if (!ctx)
1083  mooseError("No context is set for shell matrix ");
1084 
1085  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
1086  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
1087 
1088  mooseMatMult(*eigen_problem, x, r, eigen_nl.nonEigenVectorTag());
1089 
1090  PetscFunctionReturn(0);
1091 }
Nonlinear eigenvalue system to be solved.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
TagID nonEigenVectorTag() const
Vector tag ID of left hand side.
void mooseMatMult(EigenProblem &eigen_problem, Vec x, Vec r, TagID tag)
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ moosePetscSNESFormFunction()

void Moose::SlepcSupport::moosePetscSNESFormFunction ( SNES  ,
Vec  x,
Vec  r,
void ctx,
TagID  tag 
)

Definition at line 794 of file SlepcSupport.C.

Referenced by mooseSlepcEigenFormFunctionA(), and mooseSlepcEigenFormFunctionB().

795 {
796  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
797  NonlinearSystemBase & nl = eigen_problem->currentNonlinearSystem();
798  System & sys = nl.system();
799 
800  PetscVector<Number> X_global(x, sys.comm()), R(r, sys.comm());
801 
802  PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
803 
804  // Use the system's update() to get a good local version of the
805  // parallel solution. This operation does not modify the incoming
806  // "x" vector, it only localizes information from "x" into
807  // sys.current_local_solution.
808  X_global.swap(X_sys);
809  sys.update();
810  X_global.swap(X_sys);
811 
812  R.zero();
813 
814  eigen_problem->computeResidualTag(*sys.current_local_solution.get(), R, tag);
815 
816  R.close();
817 }
virtual void computeResidualTag(const NumericVector< Number > &soln, NumericVector< Number > &residual, TagID tag) override
Form a vector for all kernels and BCs with a given tag.
Definition: EigenProblem.C:286
Nonlinear system to be solved.
NonlinearSystemBase & currentNonlinearSystem()
virtual System & system() override
Get the reference to the libMesh system.
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ moosePetscSNESFormMatricesTags()

void Moose::SlepcSupport::moosePetscSNESFormMatricesTags ( SNES  snes,
Vec  x,
std::vector< Mat > &  mats,
void ctx,
const std::set< TagID > &  tags 
)

Form multiple matrices for multiple tags.

Definition at line 589 of file SlepcSupport.C.

Referenced by mooseEPSFormMatrices(), and mooseSlepcEigenFormJacobianA().

591 {
592  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
593  NonlinearSystemBase & nl = eigen_problem->currentNonlinearSystem();
594  System & sys = nl.system();
595 
596  PetscVector<Number> X_global(x, sys.comm());
597 
598  PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
599 
600  // Use the system's update() to get a good local version of the
601  // parallel solution. This operation does not modify the incoming
602  // "x" vector, it only localizes information from "x" into
603  // sys.current_local_solution.
604  X_global.swap(X_sys);
605  sys.update();
606  X_global.swap(X_sys);
607 
608  std::vector<std::unique_ptr<SparseMatrix<Number>>> jacobians;
609 
610  for (auto & mat : mats)
611  {
612  jacobians.emplace_back(std::make_unique<PetscMatrix<Number>>(mat, sys.comm()));
613  jacobians.back()->attach_dof_map(sys.get_dof_map());
614  if (!eigen_problem->constJacobian())
615  jacobians.back()->zero();
616  }
617 
618  eigen_problem->computeMatricesTags(*sys.current_local_solution.get(), jacobians, tags);
619 }
Nonlinear system to be solved.
bool constJacobian() const
Returns _const_jacobian (whether a MOOSE object has specified that the Jacobian is the same as the pr...
NonlinearSystemBase & currentNonlinearSystem()
void computeMatricesTags(const NumericVector< Number > &soln, const std::vector< std::unique_ptr< SparseMatrix< Number >>> &jacobians, const std::set< TagID > &tags)
Form several matrices simultaneously.
Definition: EigenProblem.C:197
virtual System & system() override
Get the reference to the libMesh system.
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ moosePetscSNESFormMatrixTag()

void Moose::SlepcSupport::moosePetscSNESFormMatrixTag ( SNES  snes,
Vec  x,
Mat  mat,
void ctx,
TagID  tag 
)

Form matrix according to tag.

Definition at line 559 of file SlepcSupport.C.

Referenced by mooseSlepcEigenFormJacobianA(), and mooseSlepcEigenFormJacobianB().

560 {
561  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
562  NonlinearSystemBase & nl = eigen_problem->currentNonlinearSystem();
563  System & sys = nl.system();
564 
565  PetscVector<Number> X_global(x, sys.comm());
566 
567  PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
568 
569  // Use the system's update() to get a good local version of the
570  // parallel solution. This operation does not modify the incoming
571  // "x" vector, it only localizes information from "x" into
572  // sys.current_local_solution.
573  X_global.swap(X_sys);
574  sys.update();
575  X_global.swap(X_sys);
576 
577  PetscMatrix<Number> libmesh_mat(mat, sys.comm());
578 
579  // Set the dof maps
580  libmesh_mat.attach_dof_map(sys.get_dof_map());
581 
582  if (!eigen_problem->constJacobian())
583  libmesh_mat.zero();
584 
585  eigen_problem->computeJacobianTag(*sys.current_local_solution.get(), libmesh_mat, tag);
586 }
Nonlinear system to be solved.
bool constJacobian() const
Returns _const_jacobian (whether a MOOSE object has specified that the Jacobian is the same as the pr...
NonlinearSystemBase & currentNonlinearSystem()
virtual System & system() override
Get the reference to the libMesh system.
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21
virtual void computeJacobianTag(const NumericVector< Number > &soln, SparseMatrix< Number > &jacobian, TagID tag) override
Form a Jacobian matrix for all kernels and BCs with a given tag.
Definition: EigenProblem.C:166

◆ mooseSlepcEigenFormFunctionA()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormFunctionA ( SNES  snes,
Vec  x,
Vec  r,
void ctx 
)

Form function residual Ax.

It is a SLEPc callback

Definition at line 820 of file SlepcSupport.C.

Referenced by attachCallbacksToMat().

821 {
822  PetscErrorCode ierr;
823 
824  PetscFunctionBegin;
825 
826  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
827  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
828 
829  if (eigen_problem->solverParams()._eigen_matrix_vector_mult &&
830  (eigen_problem->onLinearSolver() || eigen_problem->constantMatrices()))
831  {
832  EPS eps = eigen_nl.getEPS();
833  Mat A;
834  ST st;
835 
836  ierr = mooseEPSFormMatrices(*eigen_problem, eps, x, ctx);
837  CHKERRQ(ierr);
838 
839  // Rest ST state so that we can restrieve matrices
840  ierr = EPSGetST(eps, &st);
841  CHKERRQ(ierr);
842  ierr = STResetMatrixState(st);
843  CHKERRQ(ierr);
844  ierr = EPSGetOperators(eps, &A, NULL);
845  CHKERRQ(ierr);
846 
847  ierr = MatMult(A, x, r);
848  CHKERRQ(ierr);
849 
850  PetscFunctionReturn(0);
851  }
852 
853  moosePetscSNESFormFunction(snes, x, r, ctx, eigen_nl.nonEigenVectorTag());
854 
855  PetscFunctionReturn(0);
856 }
Nonlinear eigenvalue system to be solved.
bool constantMatrices() const
Whether or not matrices are constant.
Definition: EigenProblem.h:210
int eps(unsigned int i, unsigned int j)
2D version
SolverParams & solverParams()
Get the solver parameters.
TagID nonEigenVectorTag() const
Vector tag ID of left hand side.
ierr
virtual EPS getEPS()
Retrieve EPS (SLEPc eigen solver)
bool _eigen_matrix_vector_mult
Definition: SolverParams.h:28
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
void moosePetscSNESFormFunction(SNES, Vec x, Vec r, void *ctx, TagID tag)
Definition: SlepcSupport.C:794
CHKERRQ(ierr)
bool onLinearSolver() const
Whether or not we are in a linear solver iteration.
Definition: EigenProblem.h:200
void * ctx
PetscErrorCode mooseEPSFormMatrices(EigenProblem &eigen_problem, EPS eps, Vec x, void *ctx)
Definition: SlepcSupport.C:514
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseSlepcEigenFormFunctionAB()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormFunctionAB ( SNES  snes,
Vec  x,
Vec  Ax,
Vec  Bx,
void ctx 
)

Form function residual Ax-Bx.

It is a SLEPc callback

Definition at line 899 of file SlepcSupport.C.

Referenced by attachCallbacksToMat().

900 {
901  PetscErrorCode ierr;
902 
903  PetscFunctionBegin;
904 
905  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
906  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
907  System & sys = eigen_nl.system();
908 
909  if (eigen_problem->solverParams()._eigen_matrix_vector_mult &&
910  (eigen_problem->onLinearSolver() || eigen_problem->constantMatrices()))
911  {
912  EPS eps = eigen_nl.getEPS();
913  ST st;
914  Mat A, B;
915 
916  ierr = mooseEPSFormMatrices(*eigen_problem, eps, x, ctx);
917  CHKERRQ(ierr);
918 
919  // Rest ST state so that we can restrieve matrices
920  ierr = EPSGetST(eps, &st);
921  CHKERRQ(ierr);
922  ierr = STResetMatrixState(st);
923  CHKERRQ(ierr);
924 
925  ierr = EPSGetOperators(eps, &A, &B);
926  CHKERRQ(ierr);
927 
928  ierr = MatMult(A, x, Ax);
929  CHKERRQ(ierr);
930  ierr = MatMult(B, x, Bx);
931  CHKERRQ(ierr);
932 
933  if (eigen_problem->negativeSignEigenKernel())
934  VecScale(Bx, -1.);
935 
936  PetscFunctionReturn(0);
937  }
938 
939  PetscVector<Number> X_global(x, sys.comm()), AX(Ax, sys.comm()), BX(Bx, sys.comm());
940 
941  PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
942 
943  // Use the system's update() to get a good local version of the
944  // parallel solution. This operation does not modify the incoming
945  // "x" vector, it only localizes information from "x" into
946  // sys.current_local_solution.
947  X_global.swap(X_sys);
948  sys.update();
949  X_global.swap(X_sys);
950 
951  if (!eigen_problem->constJacobian())
952  {
953  AX.zero();
954  BX.zero();
955  }
956 
957  eigen_problem->computeResidualAB(*sys.current_local_solution.get(),
958  AX,
959  BX,
960  eigen_nl.nonEigenVectorTag(),
961  eigen_nl.eigenVectorTag());
962 
963  AX.close();
964  BX.close();
965 
966  if (eigen_problem->negativeSignEigenKernel())
967  VecScale(Bx, -1.);
968 
969  PetscFunctionReturn(0);
970 }
Nonlinear eigenvalue system to be solved.
bool constantMatrices() const
Whether or not matrices are constant.
Definition: EigenProblem.h:210
int eps(unsigned int i, unsigned int j)
2D version
SolverParams & solverParams()
Get the solver parameters.
TagID nonEigenVectorTag() const
Vector tag ID of left hand side.
TagID eigenVectorTag() const
Vector tag ID of right hand side.
bool negativeSignEigenKernel() const
A flag indicates if a negative sign is used in eigen kernels.
Definition: EigenProblem.h:60
ierr
virtual EPS getEPS()
Retrieve EPS (SLEPc eigen solver)
bool _eigen_matrix_vector_mult
Definition: SolverParams.h:28
bool constJacobian() const
Returns _const_jacobian (whether a MOOSE object has specified that the Jacobian is the same as the pr...
void computeResidualAB(const NumericVector< Number > &soln, NumericVector< Number > &residualA, NumericVector< Number > &residualB, TagID tagA, TagID tagB)
Form two vetors, where each is associated with one tag, through one element-loop. ...
Definition: EigenProblem.C:317
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
bool onLinearSolver() const
Whether or not we are in a linear solver iteration.
Definition: EigenProblem.h:200
virtual System & system() override
Get the reference to the libMesh system.
void * ctx
PetscErrorCode mooseEPSFormMatrices(EigenProblem &eigen_problem, EPS eps, Vec x, void *ctx)
Definition: SlepcSupport.C:514
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseSlepcEigenFormFunctionB()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormFunctionB ( SNES  snes,
Vec  x,
Vec  r,
void ctx 
)

Form function residual Bx.

It is a SLEPc callback

Definition at line 859 of file SlepcSupport.C.

Referenced by attachCallbacksToMat().

860 {
861  PetscErrorCode ierr;
862 
863  PetscFunctionBegin;
864 
865  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
866  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
867 
868  if (eigen_problem->solverParams()._eigen_matrix_vector_mult &&
869  (eigen_problem->onLinearSolver() || eigen_problem->constantMatrices()))
870  {
871  EPS eps = eigen_nl.getEPS();
872  ST st;
873  Mat B;
874 
875  ierr = mooseEPSFormMatrices(*eigen_problem, eps, x, ctx);
876  CHKERRQ(ierr);
877 
878  // Rest ST state so that we can restrieve matrices
879  ierr = EPSGetST(eps, &st);
880  CHKERRQ(ierr);
881  ierr = STResetMatrixState(st);
882  CHKERRQ(ierr);
883  ierr = EPSGetOperators(eps, NULL, &B);
884  CHKERRQ(ierr);
885 
886  ierr = MatMult(B, x, r);
887  CHKERRQ(ierr);
888  }
889  else
890  moosePetscSNESFormFunction(snes, x, r, ctx, eigen_nl.eigenVectorTag());
891 
892  if (eigen_problem->negativeSignEigenKernel())
893  VecScale(r, -1.);
894 
895  PetscFunctionReturn(0);
896 }
Nonlinear eigenvalue system to be solved.
bool constantMatrices() const
Whether or not matrices are constant.
Definition: EigenProblem.h:210
int eps(unsigned int i, unsigned int j)
2D version
SolverParams & solverParams()
Get the solver parameters.
TagID eigenVectorTag() const
Vector tag ID of right hand side.
bool negativeSignEigenKernel() const
A flag indicates if a negative sign is used in eigen kernels.
Definition: EigenProblem.h:60
ierr
virtual EPS getEPS()
Retrieve EPS (SLEPc eigen solver)
bool _eigen_matrix_vector_mult
Definition: SolverParams.h:28
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
void moosePetscSNESFormFunction(SNES, Vec x, Vec r, void *ctx, TagID tag)
Definition: SlepcSupport.C:794
CHKERRQ(ierr)
bool onLinearSolver() const
Whether or not we are in a linear solver iteration.
Definition: EigenProblem.h:200
void * ctx
PetscErrorCode mooseEPSFormMatrices(EigenProblem &eigen_problem, EPS eps, Vec x, void *ctx)
Definition: SlepcSupport.C:514
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseSlepcEigenFormFunctionMFFD()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormFunctionMFFD ( void ctx,
Vec  x,
Vec  r 
)

Function call for MFFD.

Definition at line 622 of file SlepcSupport.C.

Referenced by mooseSlepcEigenFormJacobianA().

623 {
624  PetscErrorCode ierr;
625  PetscErrorCode (*func)(SNES, Vec, Vec, void *);
626  void * fctx;
627  PetscFunctionBegin;
628 
629  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
630  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
631  SNES snes = eigen_nl.getSNES();
632 
633  eigen_problem->onLinearSolver(true);
634 
635  ierr = SNESGetFunction(snes, NULL, &func, &fctx);
636  CHKERRQ(ierr);
637  if (fctx != ctx)
638  {
639  SETERRQ(
640  PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_INCOMP, "Contexts are not consistent \n");
641  }
642  ierr = (*func)(snes, x, r, ctx);
643  CHKERRQ(ierr);
644 
645  eigen_problem->onLinearSolver(false);
646 
647  PetscFunctionReturn(0);
648 }
Nonlinear eigenvalue system to be solved.
ierr
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
bool onLinearSolver() const
Whether or not we are in a linear solver iteration.
Definition: EigenProblem.h:200
virtual SNES getSNES() override
Retrieve snes from slepc eigen solver.
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseSlepcEigenFormJacobianA()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormJacobianA ( SNES  snes,
Vec  x,
Mat  jac,
Mat  pc,
void ctx 
)

Form Jacobian matrix A.

It is a SLEPc callback

Definition at line 651 of file SlepcSupport.C.

Referenced by attachCallbacksToMat().

652 {
653  PetscBool jisshell, pisshell;
654  PetscBool jismffd;
655  PetscErrorCode ierr;
656 
657  PetscFunctionBegin;
658 
659  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
660  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
661 
662  // If both jacobian and preconditioning are shell matrices,
663  // and then assemble them and return
664  ierr = PetscObjectTypeCompare((PetscObject)jac, MATSHELL, &jisshell);
665  CHKERRQ(ierr);
666  ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
667  CHKERRQ(ierr);
668 
669  if (jismffd && eigen_problem->solverParams()._eigen_matrix_vector_mult)
670  {
671  ierr = MatMFFDSetFunction(jac, Moose::SlepcSupport::mooseSlepcEigenFormFunctionMFFD, ctx);
672  CHKERRQ(ierr);
673 
674  EPS eps = eigen_nl.getEPS();
675 
676  ierr = mooseEPSFormMatrices(*eigen_problem, eps, x, ctx);
677  CHKERRQ(ierr);
678 
679  if (pc != jac)
680  {
681  ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
682  CHKERRQ(ierr);
683  ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
684  CHKERRQ(ierr);
685  }
686  PetscFunctionReturn(0);
687  }
688 
689  ierr = PetscObjectTypeCompare((PetscObject)pc, MATSHELL, &pisshell);
690  CHKERRQ(ierr);
691  if ((jisshell || jismffd) && pisshell)
692  {
693  // Just assemble matrices and return
694  ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
695  CHKERRQ(ierr);
696  ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
697  CHKERRQ(ierr);
698  ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
699  CHKERRQ(ierr);
700  ierr = MatAssemblyEnd(pc, MAT_FINAL_ASSEMBLY);
701  CHKERRQ(ierr);
702 
703  PetscFunctionReturn(0);
704  }
705 
706  // Jacobian and precond matrix are the same
707  if (jac == pc)
708  {
709  if (!pisshell)
710  moosePetscSNESFormMatrixTag(snes, x, pc, ctx, eigen_nl.precondMatrixTag());
711 
712  PetscFunctionReturn(0);
713  }
714  else
715  {
716  if (!jisshell && !jismffd && !pisshell) // We need to form both Jacobian and precond matrix
717  {
718  std::vector<Mat> mats = {jac, pc};
720  snes, x, mats, ctx, {eigen_nl.nonEigenMatrixTag(), eigen_nl.precondMatrixTag()});
721  PetscFunctionReturn(0);
722  }
723  if (!pisshell) // We need to form only precond matrix
724  {
725  moosePetscSNESFormMatrixTag(snes, x, pc, ctx, eigen_nl.precondMatrixTag());
726  ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
727  CHKERRQ(ierr);
728  ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
729  CHKERRQ(ierr);
730  PetscFunctionReturn(0);
731  }
732  if (!jisshell && !jismffd) // We need to form only Jacobian matrix
733  {
734  moosePetscSNESFormMatrixTag(snes, x, jac, ctx, eigen_nl.nonEigenMatrixTag());
735  ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
736  CHKERRQ(ierr);
737  ierr = MatAssemblyEnd(pc, MAT_FINAL_ASSEMBLY);
738  CHKERRQ(ierr);
739  PetscFunctionReturn(0);
740  }
741  }
742  PetscFunctionReturn(0);
743 }
Nonlinear eigenvalue system to be solved.
int eps(unsigned int i, unsigned int j)
2D version
SolverParams & solverParams()
Get the solver parameters.
PetscErrorCode mooseSlepcEigenFormFunctionMFFD(void *ctx, Vec x, Vec r)
Function call for MFFD.
Definition: SlepcSupport.C:622
ierr
virtual EPS getEPS()
Retrieve EPS (SLEPc eigen solver)
bool _eigen_matrix_vector_mult
Definition: SolverParams.h:28
void moosePetscSNESFormMatricesTags(SNES snes, Vec x, std::vector< Mat > &mats, void *ctx, const std::set< TagID > &tags)
Form multiple matrices for multiple tags.
Definition: SlepcSupport.C:589
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
void * ctx
TagID nonEigenMatrixTag() const
Matrix tag ID of left hand side.
PetscErrorCode mooseEPSFormMatrices(EigenProblem &eigen_problem, EPS eps, Vec x, void *ctx)
Definition: SlepcSupport.C:514
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21
void moosePetscSNESFormMatrixTag(SNES snes, Vec x, Mat mat, void *ctx, TagID tag)
Form matrix according to tag.
Definition: SlepcSupport.C:559

◆ mooseSlepcEigenFormJacobianB()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormJacobianB ( SNES  snes,
Vec  x,
Mat  jac,
Mat  pc,
void ctx 
)

Form Jacobian matrix B.

It is a SLEPc callback

Definition at line 746 of file SlepcSupport.C.

Referenced by attachCallbacksToMat().

747 {
748  PetscBool jshell, pshell;
749  PetscBool jismffd;
750  PetscErrorCode ierr;
751 
752  PetscFunctionBegin;
753 
754  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
755  NonlinearEigenSystem & eigen_nl = eigen_problem->getCurrentNonlinearEigenSystem();
756 
757  // If both jacobian and preconditioning are shell matrices,
758  // and then assemble them and return
759  ierr = PetscObjectTypeCompare((PetscObject)jac, MATSHELL, &jshell);
760  CHKERRQ(ierr);
761  ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
762  CHKERRQ(ierr);
763  ierr = PetscObjectTypeCompare((PetscObject)pc, MATSHELL, &pshell);
764  CHKERRQ(ierr);
765  if ((jshell || jismffd) && pshell)
766  {
767  // Just assemble matrices and return
768  ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
769  CHKERRQ(ierr);
770  ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
771  CHKERRQ(ierr);
772  ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
773  CHKERRQ(ierr);
774  ierr = MatAssemblyEnd(pc, MAT_FINAL_ASSEMBLY);
775  CHKERRQ(ierr);
776 
777  PetscFunctionReturn(0);
778  }
779 
780  if (jac != pc && (!jshell && !jshell))
781  SETERRQ(PetscObjectComm((PetscObject)snes),
782  PETSC_ERR_ARG_INCOMP,
783  "Jacobian and precond matrices should be the same for eigen kernels \n");
784 
785  moosePetscSNESFormMatrixTag(snes, x, pc, ctx, eigen_nl.eigenMatrixTag());
786 
787  if (eigen_problem->negativeSignEigenKernel())
788  MatScale(pc, -1.);
789 
790  PetscFunctionReturn(0);
791 }
Nonlinear eigenvalue system to be solved.
bool negativeSignEigenKernel() const
A flag indicates if a negative sign is used in eigen kernels.
Definition: EigenProblem.h:60
ierr
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
TagID eigenMatrixTag() const
Matrix tag ID of right hand side.
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21
void moosePetscSNESFormMatrixTag(SNES snes, Vec x, Mat mat, void *ctx, TagID tag)
Form matrix according to tag.
Definition: SlepcSupport.C:559

◆ mooseSlepcEigenFormNorm()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEigenFormNorm ( SNES  ,
Vec  ,
PetscReal *  norm,
void ctx 
)

Definition at line 973 of file SlepcSupport.C.

Referenced by attachCallbacksToMat().

974 {
975  PetscFunctionBegin;
976  auto * const eigen_problem = static_cast<EigenProblem *>(ctx);
977  *norm = eigen_problem->formNorm();
978  PetscFunctionReturn(0);
979 }
auto norm(const T &a) -> decltype(std::abs(a))
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseSlepcEPSGetSNES()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEPSGetSNES ( EPS  eps,
SNES *  snes 
)

Retrieve SNES from EPS.

It makes sense only for Newton eigenvalue solvers

Definition at line 1257 of file SlepcSupport.C.

Referenced by NonlinearEigenSystem::getSNES(), mooseSlepcEPSSNESKSPSetPCSide(), mooseSlepcEPSSNESSetCustomizePC(), and mooseSlepcEPSSNESSetUpOptionPrefix().

1258 {
1259  PetscErrorCode ierr;
1260  PetscBool same, nonlinear;
1261 
1262  ierr = PetscObjectTypeCompare((PetscObject)eps, EPSPOWER, &same);
1263  LIBMESH_CHKERR(ierr);
1264 
1265  if (!same)
1266  mooseError("It is not eps power, and there is no snes");
1267 
1268  ierr = EPSPowerGetNonlinear(eps, &nonlinear);
1269  LIBMESH_CHKERR(ierr);
1270 
1271  if (!nonlinear)
1272  mooseError("It is not a nonlinear eigen solver");
1273 
1274  ierr = EPSPowerGetSNES(eps, snes);
1275  LIBMESH_CHKERR(ierr);
1276 
1277  return 0;
1278 }
int eps(unsigned int i, unsigned int j)
2D version
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
ierr

◆ mooseSlepcEPSMonitor()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEPSMonitor ( EPS  eps,
PetscInt  its,
PetscInt  nconv,
PetscScalar *  eigr,
PetscScalar *  eigi,
PetscReal *  errest,
PetscInt  nest,
void mctx 
)

A customized solver monitor to print out eigenvalue.

Definition at line 1345 of file SlepcSupport.C.

Referenced by SlepcEigenSolverConfiguration::configure_solver().

1353 {
1354  ST st;
1355  PetscErrorCode ierr = 0;
1356  PetscScalar eigenr, eigeni;
1357 
1358  EigenProblem * eigen_problem = static_cast<EigenProblem *>(mctx);
1359  auto & console = eigen_problem->console();
1360 
1361  auto inverse = eigen_problem->outputInverseEigenvalue();
1362  ierr = EPSGetST(eps, &st);
1363  LIBMESH_CHKERR(ierr);
1364  eigenr = eigr[0];
1365  eigeni = eigi[0];
1366  // Make the eigenvalue consistent with shift type
1367  ierr = STBackTransform(st, 1, &eigenr, &eigeni);
1368  LIBMESH_CHKERR(ierr);
1369 
1370  auto eigenvalue = inverse ? 1.0 / eigenr : eigenr;
1371 
1372  // The term "k-eigenvalue" is adopted from the neutronics community.
1373  console << " Iteration " << its << std::setprecision(10) << std::fixed
1374  << (inverse ? " k-eigenvalue = " : " eigenvalue = ") << eigenvalue << std::endl;
1375 
1376  return 0;
1377 }
int eps(unsigned int i, unsigned int j)
2D version
bool outputInverseEigenvalue() const
Whether or not to output eigenvalue inverse.
Definition: EigenProblem.h:190
void inverse(const std::vector< std::vector< Real >> &m, std::vector< std::vector< Real >> &m_inv)
Inverse the dense square matrix m using LAPACK routines.
Definition: MatrixTools.C:23
ierr
const ConsoleStream & console() const
Return console handle.
Definition: Problem.h:48
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ mooseSlepcEPSSNESKSPSetPCSide()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEPSSNESKSPSetPCSide ( FEProblemBase problem,
EPS  eps 
)

Allow users to specify PC side.

By default, we apply PC from the right side. It is consistent with the nonlinear solver.

Definition at line 1325 of file SlepcSupport.C.

Referenced by SlepcEigenSolverConfiguration::configure_solver().

1326 {
1327  PetscErrorCode ierr;
1328  SNES snes;
1329  KSP ksp;
1330 
1331  // Get SNES from EPS
1332  ierr = mooseSlepcEPSGetSNES(eps, &snes);
1333  LIBMESH_CHKERR(ierr);
1334  // Get KSP from SNES
1335  ierr = SNESGetKSP(snes, &ksp);
1336  LIBMESH_CHKERR(ierr);
1337 
1339 
1341  return 0;
1342 }
int eps(unsigned int i, unsigned int j)
2D version
void petscSetDefaultKSPNormType(FEProblemBase &problem, KSP ksp)
Set norm type.
Definition: PetscSupport.C:454
ierr
PetscErrorCode mooseSlepcEPSGetSNES(EPS eps, SNES *snes)
Retrieve SNES from EPS.
void petscSetDefaultPCSide(FEProblemBase &problem, KSP ksp)
Setup which side we want to apply preconditioner.
Definition: PetscSupport.C:464

◆ mooseSlepcEPSSNESSetCustomizePC()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEPSSNESSetCustomizePC ( EPS  eps)

Attach a customized PC.

It is useful when you want to use PBP or other customized preconditioners

Definition at line 1302 of file SlepcSupport.C.

Referenced by SlepcEigenSolverConfiguration::configure_solver().

1303 {
1304  PetscErrorCode ierr;
1305  SNES snes;
1306  KSP ksp;
1307  PC pc;
1308 
1309  // Get SNES from EPS
1310  ierr = mooseSlepcEPSGetSNES(eps, &snes);
1311  LIBMESH_CHKERR(ierr);
1312  // Get KSP from SNES
1313  ierr = SNESGetKSP(snes, &ksp);
1314  LIBMESH_CHKERR(ierr);
1315  // Get PC from KSP
1316  ierr = KSPGetPC(ksp, &pc);
1317  LIBMESH_CHKERR(ierr);
1318  // Set PC type
1319  ierr = PCSetType(pc, "moosepc");
1320  LIBMESH_CHKERR(ierr);
1321  return 0;
1322 }
int eps(unsigned int i, unsigned int j)
2D version
ierr
PetscErrorCode mooseSlepcEPSGetSNES(EPS eps, SNES *snes)
Retrieve SNES from EPS.

◆ mooseSlepcEPSSNESSetUpOptionPrefix()

PetscErrorCode Moose::SlepcSupport::mooseSlepcEPSSNESSetUpOptionPrefix ( EPS  eps)

Get rid of prefix "-eps_power" for SNES, KSP, PC, etc.

Definition at line 1281 of file SlepcSupport.C.

Referenced by SlepcEigenSolverConfiguration::configure_solver().

1282 {
1283  PetscErrorCode ierr;
1284  SNES snes;
1285  const char * prefix = nullptr;
1286 
1287  ierr = mooseSlepcEPSGetSNES(eps, &snes);
1288  LIBMESH_CHKERR(ierr);
1289  // There is an extra "eps_power" in snes that users do not like it.
1290  // Let us remove that from snes.
1291  // Retrieve option prefix from EPS
1292  ierr = PetscObjectGetOptionsPrefix((PetscObject)eps, &prefix);
1293  LIBMESH_CHKERR(ierr);
1294  // Set option prefix to SNES
1295  ierr = SNESSetOptionsPrefix(snes, prefix);
1296  LIBMESH_CHKERR(ierr);
1297 
1298  return 0;
1299 }
int eps(unsigned int i, unsigned int j)
2D version
ierr
PetscErrorCode mooseSlepcEPSGetSNES(EPS eps, SNES *snes)
Retrieve SNES from EPS.

◆ mooseSlepcStoppingTest()

PetscErrorCode Moose::SlepcSupport::mooseSlepcStoppingTest ( EPS  eps,
PetscInt  its,
PetscInt  max_it,
PetscInt  nconv,
PetscInt  nev,
EPSConvergedReason *  reason,
void ctx 
)

A customized convergence checker.

We need to make solver as converged when doing free power iteration.

Definition at line 1228 of file SlepcSupport.C.

Referenced by SlepcEigenSolverConfiguration::configure_solver().

1235 {
1236  PetscErrorCode ierr;
1237  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
1238 
1239  ierr = EPSStoppingBasic(eps, its, max_it, nconv, nev, reason, NULL);
1240  LIBMESH_CHKERR(ierr);
1241 
1242  // If we do free power iteration, we need to mark the solver as converged.
1243  // It is because SLEPc does not offer a way to copy unconverged solution.
1244  // If the solver is not marked as "converged", we have no way to get solution
1245  // from slepc. Note marking as "converged" has no side-effects at all for us.
1246  // If free power iteration is used as a stand-alone solver, we won't trigger
1247  // as "doFreePowerIteration()" is false.
1248  if (eigen_problem->doFreePowerIteration() && its == max_it && *reason <= 0)
1249  {
1250  *reason = EPS_CONVERGED_USER;
1251  eps->nconv = 1;
1252  }
1253  return 0;
1254 }
int eps(unsigned int i, unsigned int j)
2D version
ierr
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21
bool doFreePowerIteration() const
Whether or not we are doing free power iteration.
Definition: EigenProblem.h:82

◆ PCApply_MoosePC()

PetscErrorCode Moose::SlepcSupport::PCApply_MoosePC ( PC  pc,
Vec  x,
Vec  y 
)

Preconditioner application.

It call libMesh preconditioner to implement this.

Definition at line 1155 of file SlepcSupport.C.

Referenced by PCCreate_MoosePC().

1156 {
1157  void * ctx;
1158  Mat Amat, Pmat;
1159  PetscContainer container;
1160  PetscErrorCode ierr;
1161 
1162  ierr = PCGetOperators(pc, &Amat, &Pmat);
1163  CHKERRQ(ierr);
1164  ierr = PetscObjectQuery((PetscObject)Pmat, "formFunctionCtx", (PetscObject *)&container);
1165  CHKERRQ(ierr);
1166  if (container)
1167  {
1168  ierr = PetscContainerGetPointer(container, &ctx);
1169  CHKERRQ(ierr);
1170  }
1171  else
1172  {
1173  mooseError(" Can not find a context \n");
1174  }
1175 
1176  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
1177  NonlinearEigenSystem & nl_eigen = eigen_problem->getCurrentNonlinearEigenSystem();
1178  auto preconditioner = nl_eigen.preconditioner();
1179 
1180  if (!preconditioner)
1181  mooseError("There is no moose preconditioner in nonlinear eigen system \n");
1182 
1183  PetscVector<Number> x_vec(x, preconditioner->comm());
1184  PetscVector<Number> y_vec(y, preconditioner->comm());
1185 
1186  preconditioner->apply(x_vec, y_vec);
1187 
1188  return 0;
1189 }
Nonlinear eigenvalue system to be solved.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
ierr
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
Preconditioner< Number > * preconditioner() const
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ PCCreate_MoosePC()

PETSC_EXTERN PetscErrorCode Moose::SlepcSupport::PCCreate_MoosePC ( PC  pc)

Create a preconditioner from moose side.

It is used to attach moose preconditioner

Definition at line 1116 of file SlepcSupport.C.

Referenced by registerPCToPETSc().

1117 {
1118  PetscFunctionBegin;
1119 
1120  pc->ops->view = PCView_MoosePC;
1121  pc->ops->destroy = PCDestroy_MoosePC;
1122  pc->ops->setup = PCSetUp_MoosePC;
1123  pc->ops->apply = PCApply_MoosePC;
1124 
1125  PetscFunctionReturn(0);
1126 }
PetscErrorCode PCApply_MoosePC(PC pc, Vec x, Vec y)
Preconditioner application.
PetscErrorCode PCDestroy_MoosePC(PC pc)
Destroy preconditioner.
PetscErrorCode PCSetUp_MoosePC(PC pc)
Setup preconditioner.
PetscErrorCode PCView_MoosePC(PC pc, PetscViewer viewer)
View preconditioner.

◆ PCDestroy_MoosePC()

PetscErrorCode Moose::SlepcSupport::PCDestroy_MoosePC ( PC  pc)

Destroy preconditioner.

Definition at line 1129 of file SlepcSupport.C.

Referenced by PCCreate_MoosePC().

1130 {
1131  PetscFunctionBegin;
1132  /* We do not need to do anything right now, but later we may have some data we need to free here
1133  */
1134  PetscFunctionReturn(0);
1135 }

◆ PCSetUp_MoosePC()

PetscErrorCode Moose::SlepcSupport::PCSetUp_MoosePC ( PC  pc)

Setup preconditioner.

Definition at line 1192 of file SlepcSupport.C.

Referenced by PCCreate_MoosePC().

1193 {
1194  void * ctx;
1195  PetscErrorCode ierr;
1196  Mat Amat, Pmat;
1197  PetscContainer container;
1198 
1199  ierr = PCGetOperators(pc, &Amat, &Pmat);
1200  CHKERRQ(ierr);
1201  ierr = PetscObjectQuery((PetscObject)Pmat, "formFunctionCtx", (PetscObject *)&container);
1202  CHKERRQ(ierr);
1203  if (container)
1204  {
1205  ierr = PetscContainerGetPointer(container, &ctx);
1206  CHKERRQ(ierr);
1207  }
1208  else
1209  {
1210  mooseError(" Can not find a context \n");
1211  }
1212  EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
1213  NonlinearEigenSystem & nl_eigen = eigen_problem->getCurrentNonlinearEigenSystem();
1214  Preconditioner<Number> * preconditioner = nl_eigen.preconditioner();
1215 
1216  if (!preconditioner)
1217  mooseError("There is no moose preconditioner in nonlinear eigen system \n");
1218 
1219  if (!preconditioner->initialized())
1220  preconditioner->init();
1221 
1222  preconditioner->setup();
1223 
1224  return 0;
1225 }
Nonlinear eigenvalue system to be solved.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
ierr
NonlinearEigenSystem & getCurrentNonlinearEigenSystem()
Definition: EigenProblem.h:310
CHKERRQ(ierr)
Preconditioner< Number > * preconditioner() const
void * ctx
Problem for solving eigenvalue problems.
Definition: EigenProblem.h:21

◆ PCView_MoosePC()

PetscErrorCode Moose::SlepcSupport::PCView_MoosePC ( PC  pc,
PetscViewer  viewer 
)

View preconditioner.

Definition at line 1138 of file SlepcSupport.C.

Referenced by PCCreate_MoosePC().

1139 {
1140  PetscErrorCode ierr;
1141  PetscBool iascii;
1142 
1143  PetscFunctionBegin;
1144  ierr = PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
1145  CHKERRQ(ierr);
1146  if (iascii)
1147  {
1148  ierr = PetscViewerASCIIPrintf(viewer, " %s\n", "moosepc");
1149  CHKERRQ(ierr);
1150  }
1151  PetscFunctionReturn(0);
1152 }
ierr
CHKERRQ(ierr)

◆ registerPCToPETSc()

PETSC_EXTERN PetscErrorCode Moose::SlepcSupport::registerPCToPETSc ( )

Let PETSc know there is a preconditioner.

Definition at line 1104 of file SlepcSupport.C.

Referenced by NonlinearEigenSystem::attachPreconditioner().

1105 {
1106  PetscErrorCode ierr;
1107  PetscFunctionBegin;
1108 
1109  ierr = PCRegister("moosepc", PCCreate_MoosePC);
1110  CHKERRQ(ierr);
1111 
1112  PetscFunctionReturn(0);
1113 }
ierr
PETSC_EXTERN PetscErrorCode PCCreate_MoosePC(PC pc)
Create a preconditioner from moose side.
CHKERRQ(ierr)

◆ setEigenProblemOptions()

void Moose::SlepcSupport::setEigenProblemOptions ( SolverParams solver_params)

Definition at line 260 of file SlepcSupport.C.

Referenced by slepcSetOptions().

261 {
262  switch (solver_params._eigen_problem_type)
263  {
266  break;
267 
269  Moose::PetscSupport::setSinglePetscOption("-eps_non_hermitian");
270  break;
271 
273  Moose::PetscSupport::setSinglePetscOption("-eps_gen_hermitian");
274  break;
275 
277  Moose::PetscSupport::setSinglePetscOption("-eps_gen_indefinite");
278  break;
279 
281  Moose::PetscSupport::setSinglePetscOption("-eps_gen_non_hermitian");
282  break;
283 
285  Moose::PetscSupport::setSinglePetscOption("-eps_pos_gen_non_hermitian");
286  break;
287 
289  break;
290 
291  default:
292  mooseError("Unknown eigen solver type \n");
293  }
294 }
Generalized Non-Hermitian.
Definition: MooseTypes.h:789
Generalized Hermitian indefinite.
Definition: MooseTypes.h:788
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
use whatever SLPEC has by default
Definition: MooseTypes.h:791
Generalized Non-Hermitian with positive (semi-)definite B.
Definition: MooseTypes.h:790
Moose::EigenProblemType _eigen_problem_type
Definition: SolverParams.h:25
Non-Hermitian.
Definition: MooseTypes.h:786
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933
Generalized Hermitian.
Definition: MooseTypes.h:787

◆ setEigenProblemSolverParams()

void Moose::SlepcSupport::setEigenProblemSolverParams ( EigenProblem eigen_problem,
const InputParameters params 
)

Retrieve eigen problem params from 'params', and then set these params into SolverParams.

Definition at line 177 of file SlepcSupport.C.

Referenced by Eigenvalue::Eigenvalue().

178 {
179  const std::string & eigen_problem_type = params.get<MooseEnum>("eigen_problem_type");
180  if (!eigen_problem_type.empty())
181  eigen_problem.solverParams()._eigen_problem_type =
182  Moose::stringToEnum<Moose::EigenProblemType>(eigen_problem_type);
183  else
184  mooseError("Have to specify a valid eigen problem type");
185 
186  const std::string & which_eigen_pairs = params.get<MooseEnum>("which_eigen_pairs");
187  if (!which_eigen_pairs.empty())
188  eigen_problem.solverParams()._which_eigen_pairs =
189  Moose::stringToEnum<Moose::WhichEigenPairs>(which_eigen_pairs);
190 
191  // Set necessary parametrs used in EigenSystem::solve(),
192  // i.e. the number of requested eigenpairs nev and the number
193  // of basis vectors ncv used in the solution algorithm. Note that
194  // ncv >= nev must hold and ncv >= 2*nev is recommended
195  unsigned int n_eigen_pairs = params.get<unsigned int>("n_eigen_pairs");
196  unsigned int n_basis_vectors = params.get<unsigned int>("n_basis_vectors");
197 
198  eigen_problem.setNEigenPairsRequired(n_eigen_pairs);
199 
200  eigen_problem.es().parameters.set<unsigned int>("eigenpairs") = n_eigen_pairs;
201 
202  // If the subspace dimension is too small, we increase it automatically
203  if (subspace_factor * n_eigen_pairs > n_basis_vectors)
204  {
205  n_basis_vectors = subspace_factor * n_eigen_pairs;
206  mooseWarning("Number of subspaces in Eigensolver is changed by moose because the value you set "
207  "is too small");
208  }
209 
210  eigen_problem.es().parameters.set<unsigned int>("basis vectors") = n_basis_vectors;
211 
212  // Operators A and B are formed as shell matrices
213  eigen_problem.solverParams()._eigen_matrix_free = params.get<bool>("matrix_free");
214 
215  // Preconditioning is formed as a shell matrix
216  eigen_problem.solverParams()._precond_matrix_free = params.get<bool>("precond_matrix_free");
217 
218  if (params.get<MooseEnum>("solve_type") == "PJFNK")
219  {
220  eigen_problem.solverParams()._eigen_matrix_free = true;
221  }
222  if (params.get<MooseEnum>("solve_type") == "JFNK")
223  {
224  eigen_problem.solverParams()._eigen_matrix_free = true;
225  eigen_problem.solverParams()._precond_matrix_free = true;
226  }
227  // We need matrices so that we can implement residual evaluations
228  if (params.get<MooseEnum>("solve_type") == "PJFNKMO")
229  {
230  eigen_problem.solverParams()._eigen_matrix_free = true;
231  eigen_problem.solverParams()._precond_matrix_free = false;
232  eigen_problem.solverParams()._eigen_matrix_vector_mult = true;
233  // By default, we need to form full matrices, otherwise residual
234  // evaluations will not be accurate
235  eigen_problem.setCoupling(Moose::COUPLING_FULL);
236  }
237 
238  eigen_problem.constantMatrices(params.get<bool>("constant_matrices"));
239 
240  if (eigen_problem.constantMatrices() && params.get<MooseEnum>("solve_type") != "PJFNKMO")
241  {
242  mooseError("constant_matrices flag is only valid for solve type: PJFNKMO");
243  }
244 }
bool constantMatrices() const
Whether or not matrices are constant.
Definition: EigenProblem.h:210
bool _eigen_matrix_free
Definition: SolverParams.h:27
SolverParams & solverParams()
Get the solver parameters.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
void setCoupling(Moose::CouplingType type)
Set the coupling between variables TODO: allow user-defined coupling.
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:333
void setNEigenPairsRequired(unsigned int n_eigen_pairs)
Definition: EigenProblem.h:41
virtual EquationSystems & es() override
bool _eigen_matrix_vector_mult
Definition: SolverParams.h:28
Moose::EigenProblemType _eigen_problem_type
Definition: SolverParams.h:25
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
Moose::WhichEigenPairs _which_eigen_pairs
Definition: SolverParams.h:26
bool _precond_matrix_free
Definition: SolverParams.h:30
const int subspace_factor
Definition: SlepcSupport.C:34

◆ setEigenSolverOptions()

void Moose::SlepcSupport::setEigenSolverOptions ( SolverParams solver_params,
const InputParameters params 
)

Definition at line 440 of file SlepcSupport.C.

Referenced by slepcSetOptions().

441 {
442  // Avoid unused variable warnings when you have SLEPc but not PETSc-dev.
443  libmesh_ignore(params);
444 
445  switch (solver_params._eigen_solve_type)
446  {
447  case Moose::EST_POWER:
448  Moose::PetscSupport::setSinglePetscOption("-eps_type", "power");
449  break;
450 
451  case Moose::EST_ARNOLDI:
452  Moose::PetscSupport::setSinglePetscOption("-eps_type", "arnoldi");
453  break;
454 
456  Moose::PetscSupport::setSinglePetscOption("-eps_type", "krylovschur");
457  break;
458 
461  break;
462 
464  setNonlinearPowerOptions(solver_params);
465  break;
466 
467  case Moose::EST_NEWTON:
468  setNewtonPetscOptions(solver_params, params);
469  break;
470 
471  case Moose::EST_PJFNK:
472  solver_params._eigen_matrix_free = true;
473  solver_params._customized_pc_for_eigen = false;
474  setNewtonPetscOptions(solver_params, params);
475  break;
476 
477  case Moose::EST_JFNK:
478  solver_params._eigen_matrix_free = true;
479  solver_params._customized_pc_for_eigen = true;
480  setNewtonPetscOptions(solver_params, params);
481  break;
482 
483  case Moose::EST_PJFNKMO:
484  solver_params._eigen_matrix_free = true;
485  solver_params._customized_pc_for_eigen = false;
486  solver_params._eigen_matrix_vector_mult = true;
487  setNewtonPetscOptions(solver_params, params);
488  break;
489 
490  default:
491  mooseError("Unknown eigen solver type \n");
492  }
493 }
void setNewtonPetscOptions(SolverParams &solver_params, const InputParameters &params)
Definition: SlepcSupport.C:377
bool _eigen_matrix_free
Definition: SolverParams.h:27
Newton-based eigensolver with an assembled Jacobian matrix (fully coupled by default) ...
Definition: MooseTypes.h:774
Moose::EigenSolveType _eigen_solve_type
Definition: SolverParams.h:24
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
The same as PJFNK except that matrix-vector multiplication is employed to replace residual evaluation...
Definition: MooseTypes.h:776
bool _customized_pc_for_eigen
Definition: SolverParams.h:29
Power / Inverse / RQI.
Definition: MooseTypes.h:769
Krylov-Schur.
Definition: MooseTypes.h:771
void libmesh_ignore(const Args &...)
bool _eigen_matrix_vector_mult
Definition: SolverParams.h:28
Preconditioned Jacobian-free Newton Krylov.
Definition: MooseTypes.h:775
void setNonlinearPowerOptions(SolverParams &solver_params)
Definition: SlepcSupport.C:420
Nonlinear inverse power.
Definition: MooseTypes.h:773
Jacobi-Davidson.
Definition: MooseTypes.h:772
Jacobian-free Newton Krylov.
Definition: MooseTypes.h:777
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ setFreeNonlinearPowerIterations()

void Moose::SlepcSupport::setFreeNonlinearPowerIterations ( unsigned int  free_power_iterations)

Set SLEPc/PETSc options to trigger free power iteration.

Definition at line 350 of file SlepcSupport.C.

Referenced by EigenProblem::doFreeNonlinearPowerIterations().

351 {
352  Moose::PetscSupport::setSinglePetscOption("-eps_power_update", "0");
353  Moose::PetscSupport::setSinglePetscOption("-snes_max_it", "2");
354  // During each power iteration, we want solver converged unless linear solver does not
355  // work. We here use a really loose tolerance for this purpose.
356  // -snes_no_convergence_test is a perfect option, but it was removed from PETSc
357  Moose::PetscSupport::setSinglePetscOption("-snes_rtol", "0.99999999999");
358  Moose::PetscSupport::setSinglePetscOption("-eps_max_it", stringify(free_power_iterations));
359  // We always want the number of free power iterations respected so we don't want to stop early if
360  // we've satisfied a convergence criterion. Consequently we make this tolerance very tight
361  Moose::PetscSupport::setSinglePetscOption("-eps_tol", "1e-50");
362 }
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:62
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ setNewtonPetscOptions()

void Moose::SlepcSupport::setNewtonPetscOptions ( SolverParams solver_params,
const InputParameters params 
)

Definition at line 377 of file SlepcSupport.C.

Referenced by setEigenSolverOptions().

378 {
379 #if !SLEPC_VERSION_LESS_THAN(3, 8, 0) || !PETSC_VERSION_RELEASE
380  // Whether or not we need to involve an initial inverse power
381  bool initial_power = params.get<bool>("_newton_inverse_power");
382 
383  Moose::PetscSupport::setSinglePetscOption("-eps_type", "power");
384  Moose::PetscSupport::setSinglePetscOption("-eps_power_nonlinear", "1");
385  Moose::PetscSupport::setSinglePetscOption("-eps_power_update", "1");
386  // Only one outer iteration in EPS is allowed when Newton/PJFNK/JFNK
387  // is used as the eigen solver
388  Moose::PetscSupport::setSinglePetscOption("-eps_max_it", "1");
389  if (initial_power)
390  {
391  Moose::PetscSupport::setSinglePetscOption("-init_eps_power_snes_max_it", "1");
392  Moose::PetscSupport::setSinglePetscOption("-init_eps_power_ksp_rtol", "1e-2");
394  "-init_eps_max_it", stringify(params.get<unsigned int>("free_power_iterations")));
395  }
396  Moose::PetscSupport::setSinglePetscOption("-eps_target_magnitude", "");
397  if (solver_params._eigen_matrix_free)
398  {
399  Moose::PetscSupport::setSinglePetscOption("-snes_mf_operator", "1");
400  if (initial_power)
401  Moose::PetscSupport::setSinglePetscOption("-init_eps_power_snes_mf_operator", "1");
402  }
403  else
404  {
405  Moose::PetscSupport::setSinglePetscOption("-snes_mf_operator", "0");
406  if (initial_power)
407  Moose::PetscSupport::setSinglePetscOption("-init_eps_power_snes_mf_operator", "0");
408  }
409 #if PETSC_RELEASE_LESS_THAN(3, 13, 0)
410  Moose::PetscSupport::setSinglePetscOption("-st_type", "sinvert");
411  if (initial_power)
412  Moose::PetscSupport::setSinglePetscOption("-init_st_type", "sinvert");
413 #endif
414 #else
415  mooseError("Newton-based eigenvalue solver requires SLEPc 3.7.3 or higher");
416 #endif
417 }
bool _eigen_matrix_free
Definition: SolverParams.h:27
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:62
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ setNonlinearPowerOptions()

void Moose::SlepcSupport::setNonlinearPowerOptions ( SolverParams solver_params)

Definition at line 420 of file SlepcSupport.C.

Referenced by setEigenSolverOptions().

421 {
422 #if !SLEPC_VERSION_LESS_THAN(3, 8, 0) || !PETSC_VERSION_RELEASE
423  Moose::PetscSupport::setSinglePetscOption("-eps_type", "power");
424  Moose::PetscSupport::setSinglePetscOption("-eps_power_nonlinear", "1");
425  Moose::PetscSupport::setSinglePetscOption("-eps_target_magnitude", "");
426  if (solver_params._eigen_matrix_free)
427  Moose::PetscSupport::setSinglePetscOption("-snes_mf_operator", "1");
428  else
429  Moose::PetscSupport::setSinglePetscOption("-snes_mf_operator", "0");
430 
431 #if PETSC_RELEASE_LESS_THAN(3, 13, 0)
432  Moose::PetscSupport::setSinglePetscOption("-st_type", "sinvert");
433 #endif
434 #else
435  mooseError("Nonlinear Inverse Power requires SLEPc 3.7.3 or higher");
436 #endif
437 }
bool _eigen_matrix_free
Definition: SolverParams.h:27
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ setOperationsForShellMat()

void Moose::SlepcSupport::setOperationsForShellMat ( EigenProblem eigen_problem,
Mat  mat,
bool  eigen 
)

Set operations to shell mat.

Definition at line 1094 of file SlepcSupport.C.

Referenced by NonlinearEigenSystem::attachSLEPcCallbacks().

1095 {
1096  MatShellSetContext(mat, &eigen_problem);
1097  MatShellSetOperation(mat,
1098  MATOP_MULT,
1099  eigen ? (void (*)(void))mooseMatMult_Eigen
1100  : (void (*)(void))mooseMatMult_NonEigen);
1101 }
PetscErrorCode mooseMatMult_Eigen(Mat mat, Vec x, Vec y)
Implement MatMult via function evaluation for Bx.
PetscErrorCode mooseMatMult_NonEigen(Mat mat, Vec x, Vec y)
Implement MatMult via function evaluation for Ax.

◆ setSlepcEigenSolverTolerances()

void Moose::SlepcSupport::setSlepcEigenSolverTolerances ( EigenProblem eigen_problem,
const InputParameters params 
)

Control eigen solver tolerances via SLEPc options.

Definition at line 127 of file SlepcSupport.C.

Referenced by slepcSetOptions().

128 {
129  Moose::PetscSupport::setSinglePetscOption("-eps_tol", stringify(params.get<Real>("eigen_tol")));
130 
132  stringify(params.get<unsigned int>("eigen_max_its")));
133 
134  // if it is a nonlinear eigenvalue solver, we need to set tolerances for nonlinear solver and
135  // linear solver
136  if (eigen_problem.isNonlinearEigenvalueSolver())
137  {
138  // nonlinear solver tolerances
140  stringify(params.get<unsigned int>("nl_max_its")));
141 
143  stringify(params.get<unsigned int>("nl_max_funcs")));
144 
146  stringify(params.get<Real>("nl_abs_tol")));
147 
149  stringify(params.get<Real>("nl_rel_tol")));
150 
152  stringify(params.get<Real>("nl_rel_step_tol")));
153 
154  // linear solver
156  stringify(params.get<unsigned int>("l_max_its")));
157 
158  Moose::PetscSupport::setSinglePetscOption("-ksp_rtol", stringify(params.get<Real>("l_tol")));
159 
161  stringify(params.get<Real>("l_abs_tol")));
162  }
163  else
164  { // linear eigenvalue problem
165  // linear solver
167  stringify(params.get<unsigned int>("l_max_its")));
168 
169  Moose::PetscSupport::setSinglePetscOption("-st_ksp_rtol", stringify(params.get<Real>("l_tol")));
170 
172  stringify(params.get<Real>("l_abs_tol")));
173  }
174 }
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
bool isNonlinearEigenvalueSolver() const
Definition: EigenProblem.C:640
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:62
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ setWhichEigenPairsOptions()

void Moose::SlepcSupport::setWhichEigenPairsOptions ( SolverParams solver_params)

Definition at line 297 of file SlepcSupport.C.

Referenced by slepcSetOptions().

298 {
299  switch (solver_params._which_eigen_pairs)
300  {
302  Moose::PetscSupport::setSinglePetscOption("-eps_largest_magnitude");
303  break;
304 
306  Moose::PetscSupport::setSinglePetscOption("-eps_smallest_magnitude");
307  break;
308 
310  Moose::PetscSupport::setSinglePetscOption("-eps_largest_real");
311  break;
312 
314  Moose::PetscSupport::setSinglePetscOption("-eps_smallest_real");
315  break;
316 
318  Moose::PetscSupport::setSinglePetscOption("-eps_largest_imaginary");
319  break;
320 
322  Moose::PetscSupport::setSinglePetscOption("-eps_smallest_imaginary");
323  break;
324 
326  Moose::PetscSupport::setSinglePetscOption("-eps_target_magnitude");
327  break;
328 
330  Moose::PetscSupport::setSinglePetscOption("-eps_target_real");
331  break;
332 
334  Moose::PetscSupport::setSinglePetscOption("-eps_target_imaginary");
335  break;
336 
339  break;
340 
342  break;
343 
344  default:
345  mooseError("Unknown type of WhichEigenPairs \n");
346  }
347 }
smallest magnitude
Definition: MooseTypes.h:800
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
target magnitude
Definition: MooseTypes.h:805
target imaginary
Definition: MooseTypes.h:807
Moose::WhichEigenPairs _which_eigen_pairs
Definition: SolverParams.h:26
smallest imaginary
Definition: MooseTypes.h:804
largest imaginary
Definition: MooseTypes.h:803
all eigenvalues
Definition: MooseTypes.h:808
use whatever we have in SLEPC
Definition: MooseTypes.h:809
largest magnitude
Definition: MooseTypes.h:799
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933

◆ slepcSetOptions()

void Moose::SlepcSupport::slepcSetOptions ( EigenProblem eigen_problem,
const InputParameters params 
)

Push all SLEPc/PETSc options into SLEPc/PETSc side.

Options could come from commandline, SolverParams, params, etc.

Definition at line 496 of file SlepcSupport.C.

Referenced by Eigenvalue::prepareSolverOptions().

497 {
499  eigen_problem.solverParams());
500  // Call "SolverTolerances" first, so some solver specific tolerance such as "eps_max_it"
501  // can be overriden
502  setSlepcEigenSolverTolerances(eigen_problem, params);
503  setEigenSolverOptions(eigen_problem.solverParams(), params);
504  // when Bx norm postprocessor is provided, we switch off the sign normalization
505  if (eigen_problem.bxNormProvided())
506  Moose::PetscSupport::setSinglePetscOption("-eps_power_sign_normalization", "0");
507  setEigenProblemOptions(eigen_problem.solverParams());
508  setWhichEigenPairsOptions(eigen_problem.solverParams());
510 }
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
void addPetscOptionsFromCommandline()
Definition: PetscSupport.C:221
SolverParams & solverParams()
Get the solver parameters.
void setEigenSolverOptions(SolverParams &solver_params, const InputParameters &params)
Definition: SlepcSupport.C:440
void petscSetOptions(const PetscOptions &po, const SolverParams &solver_params)
A function for setting the PETSc options in PETSc from the options supplied to MOOSE.
Definition: PetscSupport.C:239
void setSlepcEigenSolverTolerances(EigenProblem &eigen_problem, const InputParameters &params)
Control eigen solver tolerances via SLEPc options.
Definition: SlepcSupport.C:127
void setWhichEigenPairsOptions(SolverParams &solver_params)
Definition: SlepcSupport.C:297
bool bxNormProvided() const
Whether a Bx norm postprocessor has been provided.
Definition: EigenProblem.h:235
void setSinglePetscOption(const std::string &name, const std::string &value="")
A wrapper function for dealing with different versions of PetscOptionsSetValue.
Definition: PetscSupport.C:933
void setEigenProblemOptions(SolverParams &solver_params)
Definition: SlepcSupport.C:260

◆ storeSolveType()

void Moose::SlepcSupport::storeSolveType ( FEProblemBase fe_problem,
const InputParameters params 
)

Set solve type into eigen problem (solverParams)

Definition at line 247 of file SlepcSupport.C.

Referenced by Eigenvalue::Eigenvalue().

248 {
249  if (!(dynamic_cast<EigenProblem *>(&fe_problem)))
250  return;
251 
252  if (params.isParamValid("solve_type"))
253  {
254  fe_problem.solverParams()._eigen_solve_type =
255  Moose::stringToEnum<Moose::EigenSolveType>(params.get<MooseEnum>("solve_type"));
256  }
257 }
Moose::EigenSolveType _eigen_solve_type
Definition: SolverParams.h:24
SolverParams & solverParams()
Get the solver parameters.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

Variable Documentation

◆ subspace_factor

const int Moose::SlepcSupport::subspace_factor = 2

Definition at line 34 of file SlepcSupport.C.

Referenced by setEigenProblemSolverParams().