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
41 {
42 PetscCallAbort(PETSC_COMM_WORLD, KSPDestroy(&solver_));
43 }
44 // TODO check if some args in LinearSolver interface can be made const&.
46
48 PetscInt getNumberOfIterations() const
49 {
50 PetscInt its = 0;
51 PetscCallAbort(PETSC_COMM_WORLD, KSPGetIterationNumber(solver_, &its));
52 return its;
53 }
54
56 double getElapsedTime() const { return elapsed_ctime_; }
57
60
63 MathLib::LinearSolverBehaviour const /*linear_solver_behaviour*/) const
64 {
65 return true;
66 }
67
68private:
69 bool canSolverHandleRectangular(std::string_view ksp_type)
70 {
71 // List of KSP types that can handle rectangular matrices
72 constexpr auto rectangular_solvers = std::to_array<std::string_view>(
73 {KSPGMRES, KSPFGMRES, KSPBCGS, KSPBCGSL, KSPTFQMR, KSPCGS});
74
75 return std::ranges::any_of(rectangular_solvers,
76 [&](const auto& solver)
77 { return solver == ksp_type; });
78 }
79
80 KSP solver_;
81 PC pc_;
82
84
85 double elapsed_ctime_ = 0.0;
86};
87
88} // 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