OGS
PETScLinearSolver.h
Go to the documentation of this file.
1
16
17#pragma once
18
19#include <petscksp.h>
20
21#include <algorithm>
22#include <array>
23#include <string>
24
26#include "PETScMatrix.h"
27#include "PETScVector.h"
28
29namespace MathLib
30{
39{
40public:
50 PETScLinearSolver(std::string const& prefix,
51 std::string const& petsc_options);
52
53 ~PETScLinearSolver() { KSPDestroy(&solver_); }
54 // TODO check if some args in LinearSolver interface can be made const&.
56
58 PetscInt getNumberOfIterations() const
59 {
60 PetscInt its = 0;
61 KSPGetIterationNumber(solver_, &its);
62 return its;
63 }
64
66 double getElapsedTime() const { return elapsed_ctime_; }
67
70
73 MathLib::LinearSolverBehaviour const /*linear_solver_behaviour*/) const
74 {
75 return true;
76 }
77
78private:
79 bool canSolverHandleRectangular(std::string_view ksp_type)
80 {
81 // List of KSP types that can handle rectangular matrices
82 constexpr auto rectangular_solvers = std::to_array<std::string_view>(
83 {KSPGMRES, KSPFGMRES, KSPBCGS, KSPBCGSL, KSPTFQMR, KSPCGS});
84
85 return std::ranges::any_of(rectangular_solvers,
86 [&](const auto& solver)
87 { return solver == ksp_type; });
88 }
89
90 KSP solver_;
91 PC pc_;
92
94
95 double elapsed_ctime_ = 0.0;
96};
97
98} // namespace MathLib
Declaration of class PETScMatrix, which provides an interface to PETSc matrix routines.
Declaration of class PETScVector, which provides an interface to PETSc vector routines.
double elapsed_ctime_
Clock time.
bool willCompute(MathLib::LinearSolverBehaviour const) const
Provided for compatibility with Eigen linear solvers.
PETScLinearSolver(std::string const &prefix, std::string const &petsc_options)
PetscInt getNumberOfIterations() const
Get number of iterations.
PC pc_
Preconditioner type.
bool solve(PETScMatrix &A, PETScVector &b, PETScVector &x)
double getElapsedTime() const
Get elapsed wall clock time.
bool canSolverHandleRectangular(std::string_view ksp_type)
bool canSolveRectangular() const
Get, if the solver can handle rectangular equation systems.
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:32
Wrapper class for PETSc vector.
Definition PETScVector.h:41