OGS
PETScLinearSolver.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <petscksp.h>
7
8#include <algorithm>
9#include <array>
10#include <string>
11
13#include "PETScMatrix.h"
14#include "PETScVector.h"
15
16namespace MathLib
17{
26{
27public:
37 PETScLinearSolver(std::string const& prefix,
38 std::string const& petsc_options);
39
40 ~PETScLinearSolver() { KSPDestroy(&solver_); }
41 // TODO check if some args in LinearSolver interface can be made const&.
43
45 PetscInt getNumberOfIterations() const
46 {
47 PetscInt its = 0;
48 KSPGetIterationNumber(solver_, &its);
49 return its;
50 }
51
53 double getElapsedTime() const { return elapsed_ctime_; }
54
57
60 MathLib::LinearSolverBehaviour const /*linear_solver_behaviour*/) const
61 {
62 return true;
63 }
64
65private:
66 bool canSolverHandleRectangular(std::string_view ksp_type)
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 }
76
77 KSP solver_;
78 PC pc_;
79
81
82 double elapsed_ctime_ = 0.0;
83};
84
85} // namespace MathLib
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:21
Wrapper class for PETSc vector.
Definition PETScVector.h:28