Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
linear_solver.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_LA_LINEAR_SOLVER_HPP_
25#define VULPES_LA_LINEAR_SOLVER_HPP_
26
27#include <petscmat.h>
28#include <petscsys.h>
29
31#include "vulpes/la/gamg.hpp"
34#include "vulpes/la/vector.hpp"
35
36namespace vulpes {
37namespace la {
38
48 public:
52 enum Type {
53 CG, // Conjugate Gradient
54 GMRES, // Generalized Minimal Residual
55 BiCGStab, // Biconjugate Gradient Stabilized
56 MINRES, // Minimal Residual
57 PrecondOnly // Apply only preconditioner
58 };
59
66 SOR, // Successive Over-Relaxation
67 ASM, // Additive Schwarz Method
69 BoomerAMG, // HYPRE BoomerAMG
70 EuclidILU, // HYPRE ILU preconditioner
71 LU, // Direct LU factorization
72 Cholesky, // Direct Cholesky factorization
73 SuperLU, // SuperLU_DIST direct solver
74 MUMPS, // MUMPS direct solver
75 GAMG // PETSc smoothed aggregation AMG
76 };
77
85 explicit LinearSolver(Type solver_type = CG, PrecondType pc_type = Jacobi);
86
87 LinearSolver(const LinearSolver&) = delete;
89
94 void setType(Type solver_type);
96 void setPreconditioner(PrecondType pc_type);
102 void setTolerances(
103 PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt maxits);
105 void setRelativeTolerance(PetscReal rtol);
107 void setAbsoluteTolerance(PetscReal abstol);
109 void setMaxIterations(PetscInt maxits);
111 void setInitialGuessNonzero(bool nonzero);
113 void setGMRESRestart(PetscInt restart);
115 void setSORParameters(
116 PetscReal omega, PetscInt iterations = 1, PetscReal rtol = 0.0);
120 void setOptionsGAMG(const DataGAMG& data);
122 void setOperators(const SparseMatrix& A);
124 void setOperators(const SparseMatrix& A, const SparseMatrix& P);
128 void solve(const Vector& b, Vector& x);
132 void applyPreconditioner(const Vector& b, Vector& x);
134 PetscInt getIterationCount();
136 void setNormType(KSPNormType type);
138 PetscReal getResidualNorm();
139
141 KSPConvergedReason getConvergenceReason();
143 PC getPC();
145 const auto& getKSP() const { return m_ksp; }
148
150
151 protected:
152 void init(Type solver_type = CG, PrecondType pc_type = Jacobi);
154 void setUp();
155
156 KSP m_ksp;
159};
160
161} // namespace la
162} // namespace vulpes
163
164#endif // VULPES_LA_LINEAR_SOLVER_HPP_
LinearSolver & operator=(const LinearSolver &)=delete
Type
Enumeration of available solver types.
Definition linear_solver.hpp:52
@ PrecondOnly
Definition linear_solver.hpp:57
@ GMRES
Definition linear_solver.hpp:54
@ CG
Definition linear_solver.hpp:53
@ MINRES
Definition linear_solver.hpp:56
@ BiCGStab
Definition linear_solver.hpp:55
void setOptionsGAMG(const DataGAMG &data)
Set options for the GAMG preconditioner.
Definition linear_solver.cpp:458
KSPConvergedReason getConvergenceReason()
Get the convergence reason.
Definition linear_solver.cpp:262
KSP m_ksp
Definition linear_solver.hpp:156
void setSORParameters(PetscReal omega, PetscInt iterations=1, PetscReal rtol=0.0)
Set the SOR parameters.
Definition linear_solver.cpp:193
LinearSolver(Type solver_type=CG, PrecondType pc_type=Jacobi)
Construct a LinearSolver with specified solver and preconditioner types.
Definition linear_solver.cpp:36
PetscInt getIterationCount()
Get the number of iterations taken by the solver.
Definition linear_solver.cpp:246
bool m_point_block_smoother
Use point-block Jacobi on the GAMG levels, see DataGAMG.
Definition linear_solver.hpp:158
void setType(Type solver_type)
Set the type of the linear solver.
Definition linear_solver.cpp:52
void setOptionsBoomerAMG(DataBoomerAMG data)
Set options for the BoomerAMG preconditioner.
Definition linear_solver.cpp:352
void init(Type solver_type=CG, PrecondType pc_type=Jacobi)
Definition linear_solver.cpp:40
void setInitialGuessNonzero(bool nonzero)
Set the initial guess to nonzero.
Definition linear_solver.cpp:184
PetscReal getResidualNorm()
Get the residual norm after the solve.
Definition linear_solver.cpp:256
void setRelativeTolerance(PetscReal rtol)
Set the relative tolerance.
Definition linear_solver.cpp:158
PrecondType
Enumeration of available preconditioner types.
Definition linear_solver.hpp:63
@ BoomerAMG
Definition linear_solver.hpp:69
@ EuclidILU
Definition linear_solver.hpp:70
@ Jacobi
Definition linear_solver.hpp:65
@ SuperLU
Definition linear_solver.hpp:73
@ LU
Definition linear_solver.hpp:71
@ MUMPS
Definition linear_solver.hpp:74
@ Cholesky
Definition linear_solver.hpp:72
@ BlockJacobi
Definition linear_solver.hpp:68
@ SOR
Definition linear_solver.hpp:66
@ GAMG
Definition linear_solver.hpp:75
@ ASM
Definition linear_solver.hpp:67
@ NoPrecond
Definition linear_solver.hpp:64
void setMaxIterations(PetscInt maxits)
Set the maximum iterations.
Definition linear_solver.cpp:176
void solve(const Vector &b, Vector &x)
Solve the linear system Ax = b.
Definition linear_solver.cpp:231
void setUp()
Set up the KSP, applying the solver specific level smoother.
Definition linear_solver.cpp:213
void setTolerances(PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt maxits)
Set the solver tolerances.
Definition linear_solver.cpp:153
void setAbsoluteTolerance(PetscReal abstol)
Set the absolute tolerance.
Definition linear_solver.cpp:167
PC getPC()
Get the preconditioner.
Definition linear_solver.cpp:268
void printConvergenceInfo()
Print convergence information.
Definition linear_solver.cpp:274
void setNormType(KSPNormType type)
Set the norm type for convergence testing.
Definition linear_solver.cpp:252
void applyPreconditioner(const Vector &b, Vector &x)
Apply the preconditioner only, x = P^{-1} b: the result of solve() for type PrecondOnly,...
Definition linear_solver.cpp:235
LinearSolver(const LinearSolver &)=delete
~LinearSolver()
Definition linear_solver.cpp:289
const auto & getKSP() const
Get the KSP object.
Definition linear_solver.hpp:145
void setPreconditioner(PrecondType pc_type)
Set the type of the preconditioner.
Definition linear_solver.cpp:78
void setGMRESRestart(PetscInt restart)
Set the GMRES restart parameter.
Definition linear_solver.cpp:189
void setOperators(const SparseMatrix &A)
Set the system matrix.
Definition linear_solver.cpp:201
Definition sparse_matrix.hpp:52
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
Definition boomer_amg.hpp:27
Definition cell_manager.hpp:38
Definition boomer_amg.hpp:29
Definition gamg.hpp:45