OGS
MathLib::PETScLinearSolver Class Reference

Detailed Description

A class of linear solver based on PETSc routines.

All command-line options that are not recognized by OGS are passed on to PETSc, i.e., they potentially affect the linear solver. The linear solver options in the project file take precedence over the command-line options, because the former are processed at a later stage.

Definition at line 25 of file PETScLinearSolver.h.

#include <PETScLinearSolver.h>

Public Member Functions

 PETScLinearSolver (std::string const &prefix, std::string const &petsc_options)
 ~PETScLinearSolver ()
bool solve (PETScMatrix &A, PETScVector &b, PETScVector &x)
PetscInt getNumberOfIterations () const
 Get number of iterations.
double getElapsedTime () const
 Get elapsed wall clock time.
bool canSolveRectangular () const
 Get, if the solver can handle rectangular equation systems.
bool willCompute (MathLib::LinearSolverBehaviour const) const
 Provided for compatibility with Eigen linear solvers.

Private Member Functions

bool canSolverHandleRectangular (std::string_view ksp_type)

Private Attributes

KSP solver_
 Solver type.
PC pc_
 Preconditioner type.
bool can_solve_rectangular_ = false
double elapsed_ctime_ = 0.0
 Clock time.

Constructor & Destructor Documentation

◆ PETScLinearSolver()

MathLib::PETScLinearSolver::PETScLinearSolver ( std::string const & prefix,
std::string const & petsc_options )

Constructor

Parameters
prefixName used to distinguish the options in the command line for this solver. It can be the name of the PDE that owns an instance of this class.
petsc_optionsPETSc options string which is passed to PETSc lib and inserted in the PETSc option database (see https://petsc.org/release/manualpages/Sys/PetscOptionsInsertString/).

Definition at line 10 of file PETScLinearSolver.cpp.

12{
13#if PETSC_VERSION_LT(3, 7, 0)
14 PetscOptionsInsertString(petsc_options.c_str());
15#else
16 PetscOptionsInsertString(nullptr, petsc_options.c_str());
17#endif
18
19 KSPCreate(PETSC_COMM_WORLD, &solver_);
20
21 KSPGetPC(solver_, &pc_);
22
23 if (!prefix.empty())
24 {
25 KSPSetOptionsPrefix(solver_, prefix.c_str());
26 }
27
28 KSPSetInitialGuessNonzero(solver_, PETSC_TRUE);
29 KSPSetFromOptions(solver_); // set run-time options
30
31 KSPType ksp_type;
32 KSPGetType(solver_, &ksp_type);
34}
PC pc_
Preconditioner type.
bool canSolverHandleRectangular(std::string_view ksp_type)

References can_solve_rectangular_, canSolverHandleRectangular(), pc_, and solver_.

◆ ~PETScLinearSolver()

MathLib::PETScLinearSolver::~PETScLinearSolver ( )
inline

Definition at line 40 of file PETScLinearSolver.h.

40{ KSPDestroy(&solver_); }

References solver_.

Member Function Documentation

◆ canSolveRectangular()

bool MathLib::PETScLinearSolver::canSolveRectangular ( ) const
inline

Get, if the solver can handle rectangular equation systems.

Definition at line 56 of file PETScLinearSolver.h.

56{ return can_solve_rectangular_; }

References can_solve_rectangular_.

◆ canSolverHandleRectangular()

bool MathLib::PETScLinearSolver::canSolverHandleRectangular ( std::string_view ksp_type)
inlineprivate

Definition at line 66 of file PETScLinearSolver.h.

67 {
68 // List of KSP types that can handle rectangular matrices
69 constexpr auto rectangular_solvers = std::to_array<std::string_view>(
70 {KSPGMRES, KSPFGMRES, KSPBCGS, KSPBCGSL, KSPTFQMR, KSPCGS});
71
72 return std::ranges::any_of(rectangular_solvers,
73 [&](const auto& solver)
74 { return solver == ksp_type; });
75 }

Referenced by PETScLinearSolver().

◆ getElapsedTime()

double MathLib::PETScLinearSolver::getElapsedTime ( ) const
inline

Get elapsed wall clock time.

Definition at line 53 of file PETScLinearSolver.h.

53{ return elapsed_ctime_; }
double elapsed_ctime_
Clock time.

References elapsed_ctime_.

◆ getNumberOfIterations()

PetscInt MathLib::PETScLinearSolver::getNumberOfIterations ( ) const
inline

Get number of iterations.

Definition at line 45 of file PETScLinearSolver.h.

46 {
47 PetscInt its = 0;
48 KSPGetIterationNumber(solver_, &its);
49 return its;
50 }

References solver_.

◆ solve()

bool MathLib::PETScLinearSolver::solve ( PETScMatrix & A,
PETScVector & b,
PETScVector & x )

Definition at line 36 of file PETScLinearSolver.cpp.

37{
38 BaseLib::RunTime wtimer;
39 wtimer.start();
40
41// define TEST_MEM_PETSC
42#ifdef TEST_MEM_PETSC
43 PetscLogDouble mem1, mem2;
44 PetscMemoryGetCurrentUsage(&mem1);
45#endif
46
47 KSPNormType norm_type;
48 KSPGetNormType(solver_, &norm_type);
49 const char* ksp_type;
50 const char* pc_type;
51 KSPGetType(solver_, &ksp_type);
52 PCGetType(pc_, &pc_type);
53
54 PetscPrintf(PETSC_COMM_WORLD,
55 "\n================================================");
56 PetscPrintf(PETSC_COMM_WORLD,
57 "\nLinear solver %s with %s preconditioner using %s", ksp_type,
58 pc_type, KSPNormTypes[norm_type]);
59
60 KSPSetOperators(solver_, A.getRawMatrix(), A.getRawMatrix());
61
62 KSPSolve(solver_, b.getRawVector(), x.getRawVector());
63
64 KSPConvergedReason reason;
65 KSPGetConvergedReason(solver_, &reason);
66
67 bool converged = true;
68 if (reason > 0)
69 {
70 PetscInt its;
71 KSPGetIterationNumber(solver_, &its);
72 PetscPrintf(PETSC_COMM_WORLD, "\nconverged in %d iterations", its);
73 switch (reason)
74 {
75 case KSP_CONVERGED_RTOL:
76 PetscPrintf(PETSC_COMM_WORLD,
77 " (relative convergence criterion fulfilled).");
78 break;
79 case KSP_CONVERGED_ATOL:
80 PetscPrintf(PETSC_COMM_WORLD,
81 " (absolute convergence criterion fulfilled).");
82 break;
83 default:
84 PetscPrintf(PETSC_COMM_WORLD, ".");
85 }
86
87 PetscPrintf(PETSC_COMM_WORLD,
88 "\n================================================\n");
89 }
90 else if (reason == KSP_DIVERGED_ITS)
91 {
92 PetscPrintf(PETSC_COMM_WORLD,
93 "\nWarning: maximum number of iterations reached.\n");
94 }
95 else
96 {
97 converged = false;
98 if (reason == KSP_DIVERGED_INDEFINITE_PC)
99 {
100 PetscPrintf(PETSC_COMM_WORLD,
101 "\nDivergence because of indefinite preconditioner,");
102 PetscPrintf(PETSC_COMM_WORLD,
103 "\nTry to run again with "
104 "-pc_factor_shift_positive_definite option.\n");
105 }
106 else if (reason == KSP_DIVERGED_BREAKDOWN_BICG)
107 {
108 PetscPrintf(PETSC_COMM_WORLD,
109 "\nKSPBICG method was detected so the method could not "
110 "continue to enlarge the Krylov space.");
111 PetscPrintf(PETSC_COMM_WORLD,
112 "\nTry to run again with another solver.\n");
113 }
114 else if (reason == KSP_DIVERGED_NONSYMMETRIC)
115 {
116 PetscPrintf(PETSC_COMM_WORLD,
117 "\nMatrix or preconditioner is unsymmetric but KSP "
118 "requires symmetric.\n");
119 }
120 else
121 {
122 PetscPrintf(PETSC_COMM_WORLD,
123 "\nDivergence detected, use command option "
124 "-ksp_monitor or -log_summary to check the details.\n");
125 }
126 }
127
128#ifdef TEST_MEM_PETSC
129 PetscMemoryGetCurrentUsage(&mem2);
130 PetscPrintf(
131 PETSC_COMM_WORLD,
132 "###Memory usage by solver. Before: %f After: %f Increase: %d\n", mem1,
133 mem2, (int)(mem2 - mem1));
134#endif
135
136 elapsed_ctime_ += wtimer.elapsed();
137
138 return converged;
139}
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:31
void start()
Start the timer.
Definition RunTime.h:21

References BaseLib::RunTime::elapsed(), elapsed_ctime_, MathLib::PETScMatrix::getRawMatrix(), MathLib::PETScVector::getRawVector(), pc_, solver_, and BaseLib::RunTime::start().

◆ willCompute()

bool MathLib::PETScLinearSolver::willCompute ( MathLib::LinearSolverBehaviour const ) const
inline

Provided for compatibility with Eigen linear solvers.

Definition at line 59 of file PETScLinearSolver.h.

61 {
62 return true;
63 }

Member Data Documentation

◆ can_solve_rectangular_

bool MathLib::PETScLinearSolver::can_solve_rectangular_ = false
private

Definition at line 80 of file PETScLinearSolver.h.

Referenced by PETScLinearSolver(), and canSolveRectangular().

◆ elapsed_ctime_

double MathLib::PETScLinearSolver::elapsed_ctime_ = 0.0
private

Clock time.

Definition at line 82 of file PETScLinearSolver.h.

Referenced by getElapsedTime(), and solve().

◆ pc_

PC MathLib::PETScLinearSolver::pc_
private

Preconditioner type.

Definition at line 78 of file PETScLinearSolver.h.

Referenced by PETScLinearSolver(), and solve().

◆ solver_

KSP MathLib::PETScLinearSolver::solver_
private

Solver type.

Definition at line 77 of file PETScLinearSolver.h.

Referenced by PETScLinearSolver(), ~PETScLinearSolver(), getNumberOfIterations(), and solve().


The documentation for this class was generated from the following files: