preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Petsc.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "logging/Logger.hpp"
4#include "precice/span.hpp"
5#include "utils/Parallel.hpp"
6
7namespace precice {
8namespace logging {
9class Logger;
10} // namespace logging
11
12namespace utils {
13
15class Petsc {
16public:
23
25 static void finalize();
26
27private:
29 static bool weInitialized;
30
32};
33} // namespace utils
34} // namespace precice
35
36#ifndef PRECICE_NO_PETSC
37
38#include <string>
39#include <utility>
40#include "petscao.h"
41#include "petscis.h"
42#include "petscksp.h"
43#include "petscmat.h"
44#include "petscvec.h"
45
47namespace precice::utils::petsc {
48
51
52class Matrix;
53
54class Vector {
55public:
56 Vec vector = nullptr;
57
60
62 explicit Vector(const std::string &name = "");
63
67 Vector(const Vector &other);
68
72 Vector &operator=(const Vector &other);
73
77 Vector(Vector &&other) noexcept;
78
82 Vector &operator=(Vector &&other) noexcept;
83
87 Vector(Vec &other, const std::string &name = "");
88
89 ~Vector();
90
93
95 static Vector allocate(const std::string &name = "");
96
100 static Vector allocate(Vector &other, const std::string &name = "");
101
103 static Vector allocate(Vec &other, const std::string &name = "");
104
106 static Vector allocate(Matrix &m, const std::string &name = "", LEFTRIGHT type = LEFT);
107
109 static Vector allocate(Mat &m, const std::string &name = "", LEFTRIGHT type = LEFT);
110
112
114 void swap(Vector &other) noexcept;
115
117 operator Vec &();
118
120 void init(PetscInt rows);
121
122 PetscInt getSize() const;
123
124 PetscInt getLocalSize() const;
125
126 void setValue(PetscInt row, PetscScalar value);
127
128 void arange(double start, double stop);
129
130 void fillWithRandoms();
131
133
134 Vector &copyTo(precice::span<double> destination);
135
137 void sort();
138
139 void assemble();
140
143
145 void write(const std::string &filename, VIEWERFORMAT format = ASCII) const;
146
148 void read(const std::string &filename, VIEWERFORMAT format = ASCII);
149
151 void view() const;
152
154 double l2norm() const;
155
156private:
158};
159
160void swap(Vector &lhs, Vector &rhs) noexcept;
161
162class Matrix {
163public:
164 Mat matrix = nullptr;
165
167
170 Matrix(const Matrix &) = delete;
171 Matrix &operator=(const Matrix &) = delete;
172
173 explicit Matrix(std::string name = "");
174
176 Matrix(Matrix &&) = default;
177 Matrix &operator=(Matrix &&) = default;
178
179 ~Matrix();
180
182 operator Mat &();
183
184 void assemble(MatAssemblyType type = MAT_FINAL_ASSEMBLY);
185
187
192 void init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols,
193 MatType type = nullptr, bool doSetup = true);
194
196 void reset();
197
199
200 MatInfo getInfo(MatInfoType flag) const;
201
202 void setValue(PetscInt row, PetscInt col, PetscScalar value);
203
204 void fillWithRandoms();
205
206 void setColumn(Vector &v, PetscInt col);
207
210
213
216
219
221 PetscInt blockSize() const;
222
224 void write(const std::string &filename, VIEWERFORMAT format = ASCII) const;
225
227 void read(const std::string &filename);
228
230 void view() const;
231
233 void viewDraw() const;
234};
235
237public:
238 KSP ksp;
239
241
244 KSPSolver(const KSPSolver &) = delete;
245 KSPSolver &operator=(const KSPSolver &) = delete;
246
247 explicit KSPSolver(std::string name = "");
248
250 KSPSolver(KSPSolver &&other) = default;
251
252 ~KSPSolver();
253
255 operator KSP &();
256
258 void reset();
259
261 enum struct SolverResult {
262 Converged,
263 Stopped,
264 Diverged
265 };
266
269
272
275
278
280 PetscInt getIterationNumber();
281
283 PetscReal getRealtiveTolerance();
284
286 PetscReal getResidualNorm();
287};
288
290void destroy(KSP *ksp);
291
293void destroy(ISLocalToGlobalMapping *IS);
294
296void destroy(AO *ao);
297
298} // namespace precice::utils::petsc
299
300#endif // PRECICE_NO_PETSC
std::string name
This class provides a lightweight logger.
Definition Logger.hpp:17
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition span.hpp:284
Utility class for managing PETSc operations.
Definition Petsc.hpp:15
static bool weInitialized
Whether we have initialized Petsc or if it was initialized by an application calling us.
Definition Petsc.hpp:29
static void finalize()
Finalizes Petsc environment.
Definition Petsc.cpp:58
static void initialize(utils::Parallel::Communicator comm)
Initializes the Petsc environment.
Definition Petsc.cpp:38
static logging::Logger _log
Definition Petsc.hpp:31
SolverResult getSolverResult()
Returns the current convergence reason as a SolverRestult.
Definition Petsc.cpp:643
PetscReal getRealtiveTolerance()
Returns the relavtive tolerance of the KSP.
Definition Petsc.cpp:745
KSPSolver(KSPSolver &&other)=default
Move constructor, use the implicitly declared.
std::string summaryFor(Vector &b)
Returns a summary the KSP solving for b.
Definition Petsc.cpp:673
SolverResult solveTranspose(Vector &b, Vector &x)
Solves the transposed linear system, returns false it not converged.
Definition Petsc.cpp:667
void reset()
Destroys and recreates the ksp on the same communicator.
Definition Petsc.cpp:636
KSPSolver(const KSPSolver &)=delete
Delete copy and assignment constructor.
PetscReal getResidualNorm()
Returns the last residual norm of the KSP.
Definition Petsc.cpp:736
KSPSolver & operator=(const KSPSolver &)=delete
PetscInt getIterationNumber()
Returns the iteration number of solver, either during or after the solve call.
Definition Petsc.cpp:727
SolverResult solve(Vector &b, Vector &x)
Solves the linear system, returns false it not converged.
Definition Petsc.cpp:661
SolverResult
The state of the KSP after returning from solve()
Definition Petsc.hpp:261
@ Stopped
The solver reached the maximum iterations.
void setColumn(Vector &v, PetscInt col)
Definition Petsc.cpp:513
void read(const std::string &filename)
Reads the matrix from file, stored in PETSc binary format.
Definition Petsc.cpp:579
void write(const std::string &filename, VIEWERFORMAT format=ASCII) const
Writes the matrix to file.
Definition Petsc.cpp:571
void assemble(MatAssemblyType type=MAT_FINAL_ASSEMBLY)
Definition Petsc.cpp:445
void view() const
Prints the matrix.
Definition Petsc.cpp:587
std::pair< PetscInt, PetscInt > getSize() const
Returns (rows, cols) global size.
Definition Petsc.cpp:534
void setValue(PetscInt row, PetscInt col, PetscScalar value)
Definition Petsc.cpp:489
void viewDraw() const
Graphically draws the matrix structure.
Definition Petsc.cpp:595
void init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols, MatType type=nullptr, bool doSetup=true)
Initializes matrix of given size and type.
Definition Petsc.cpp:454
std::pair< PetscInt, PetscInt > ownerRange() const
Returns a pair that mark the beginning and end of the matrix' ownership range.
Definition Petsc.cpp:548
PetscInt blockSize() const
Returns the block size of the matrix.
Definition Petsc.cpp:562
void reset()
Destroys and recreates the matrix on the same communicator.
Definition Petsc.cpp:471
Matrix & operator=(const Matrix &)=delete
Matrix(const Matrix &)=delete
Delete copy and assignment constructor.
Matrix(Matrix &&)=default
Move constructor, use the implicitly declared.
Matrix & operator=(Matrix &&)=default
std::pair< PetscInt, PetscInt > getLocalSize() const
Returns (rows, cols) local size.
Definition Petsc.cpp:541
MatInfo getInfo(MatInfoType flag) const
Get the MatInfo struct for the matrix.
Definition Petsc.cpp:482
std::pair< PetscInt, PetscInt > ownerRangeColumn() const
Returns a pair that mark the beginning and end of the matrix' column ownership range.
Definition Petsc.cpp:555
Vector & copyTo(precice::span< double > destination)
Definition Petsc.cpp:339
double l2norm() const
returns the l2-norm of the vector
Definition Petsc.cpp:395
PetscInt getLocalSize() const
Definition Petsc.cpp:273
void arange(double start, double stop)
Definition Petsc.cpp:289
void read(const std::string &filename, VIEWERFORMAT format=ASCII)
Reads the vector from file.
Definition Petsc.cpp:402
void setValue(PetscInt row, PetscScalar value)
Definition Petsc.cpp:282
void sort()
Sorts the LOCAL partition of the vector.
Definition Petsc.cpp:358
static Vector allocate(const std::string &name="")
Allocates a new vector on the given MPI communicator.
Definition Petsc.cpp:206
Vector & copyFrom(precice::span< const double > source)
Definition Petsc.cpp:322
void view() const
Prints the vector.
Definition Petsc.cpp:408
std::pair< PetscInt, PetscInt > ownerRange() const
Returns a pair that mark the beginning and end of the vectors ownership range. Use first and second t...
Definition Petsc.cpp:382
void write(const std::string &filename, VIEWERFORMAT format=ASCII) const
Writes the vector to file.
Definition Petsc.cpp:389
static logging::Logger _log
Definition Petsc.hpp:157
PetscInt getSize() const
Definition Petsc.cpp:264
void swap(Vector &other) noexcept
Swaps the ownership of two vectors.
Definition Petsc.cpp:244
Vector & operator=(const Vector &other)
Definition Petsc.cpp:161
Vector(const std::string &name="")
Creates a new vector on the given MPI communicator.
Definition Petsc.cpp:180
void init(PetscInt rows)
Sets the size and calls VecSetFromOptions.
Definition Petsc.cpp:255
PETSc related utilities.
Definition Petsc.cpp:81
void destroy(ISLocalToGlobalMapping *IS)
Destroys an ISLocalToGlobalMapping, if IS is not null and PetscIsInitialized.
Definition Petsc.cpp:756
void swap(Vector &lhs, Vector &rhs) noexcept
Definition Petsc.cpp:415
Main namespace of the precice library.