preCICE v3.1.2
Loading...
Searching...
No Matches
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
46namespace precice {
47namespace utils {
49namespace petsc {
50
53
54class Matrix;
55
56class Vector {
57public:
58 Vec vector = nullptr;
59
62
64 explicit Vector(const std::string &name = "");
65
69 Vector(const Vector &other);
70
74 Vector &operator=(const Vector &other);
75
79 Vector(Vector &&other) noexcept;
80
84 Vector &operator=(Vector &&other) noexcept;
85
89 Vector(Vec &other, const std::string &name = "");
90
91 ~Vector();
92
95
97 static Vector allocate(const std::string &name = "");
98
102 static Vector allocate(Vector &other, const std::string &name = "");
103
105 static Vector allocate(Vec &other, const std::string &name = "");
106
108 static Vector allocate(Matrix &m, const std::string &name = "", LEFTRIGHT type = LEFT);
109
111 static Vector allocate(Mat &m, const std::string &name = "", LEFTRIGHT type = LEFT);
112
114
116 void swap(Vector &other) noexcept;
117
119 operator Vec &();
120
122 void init(PetscInt rows);
123
124 PetscInt getSize() const;
125
126 PetscInt getLocalSize() const;
127
128 void setValue(PetscInt row, PetscScalar value);
129
130 void arange(double start, double stop);
131
132 void fillWithRandoms();
133
135
136 Vector &copyTo(precice::span<double> destination);
137
139 void sort();
140
141 void assemble();
142
145
147 void write(const std::string &filename, VIEWERFORMAT format = ASCII) const;
148
150 void read(const std::string &filename, VIEWERFORMAT format = ASCII);
151
153 void view() const;
154
156 double l2norm() const;
157
158private:
160};
161
162void swap(Vector &lhs, Vector &rhs) noexcept;
163
164class Matrix {
165public:
166 Mat matrix = nullptr;
167
169
172 Matrix(const Matrix &) = delete;
173 Matrix &operator=(const Matrix &) = delete;
174
175 explicit Matrix(std::string name = "");
176
178 Matrix(Matrix &&) = default;
179 Matrix &operator=(Matrix &&) = default;
180
181 ~Matrix();
182
184 operator Mat &();
185
186 void assemble(MatAssemblyType type = MAT_FINAL_ASSEMBLY);
187
189
194 void init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols,
195 MatType type = nullptr, bool doSetup = true);
196
198 void reset();
199
201
202 MatInfo getInfo(MatInfoType flag) const;
203
204 void setValue(PetscInt row, PetscInt col, PetscScalar value);
205
206 void fillWithRandoms();
207
208 void setColumn(Vector &v, PetscInt col);
209
212
215
218
221
223 PetscInt blockSize() const;
224
226 void write(const std::string &filename, VIEWERFORMAT format = ASCII) const;
227
229 void read(const std::string &filename);
230
232 void view() const;
233
235 void viewDraw() const;
236};
237
239public:
240 KSP ksp;
241
243
246 KSPSolver(const KSPSolver &) = delete;
247 KSPSolver &operator=(const KSPSolver &) = delete;
248
249 explicit KSPSolver(std::string name = "");
250
252 KSPSolver(KSPSolver &&other) = default;
253
254 ~KSPSolver();
255
257 operator KSP &();
258
260 void reset();
261
263 enum struct SolverResult {
264 Converged,
265 Stopped,
266 Diverged
267 };
268
271
274
277
280
282 PetscInt getIterationNumber();
283
285 PetscReal getRealtiveTolerance();
286
288 PetscReal getResidualNorm();
289};
290
292void destroy(KSP *ksp);
293
295void destroy(ISLocalToGlobalMapping *IS);
296
298void destroy(AO *ao);
299
300} // namespace petsc
301} // namespace utils
302} // namespace precice
303
304#endif // PRECICE_NO_PETSC
std::string name
This class provides a lightweight logger.
Definition Logger.hpp:16
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:98
static void initialize(utils::Parallel::Communicator comm)
Initializes the Petsc environment.
Definition Petsc.cpp:78
static logging::Logger _log
Definition Petsc.hpp:31
SolverResult getSolverResult()
Returns the current convergence reason as a SolverRestult.
Definition Petsc.cpp:683
PetscReal getRealtiveTolerance()
Returns the relavtive tolerance of the KSP.
Definition Petsc.cpp:785
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:713
SolverResult solveTranspose(Vector &b, Vector &x)
Solves the transposed linear system, returns false it not converged.
Definition Petsc.cpp:707
void reset()
Destroys and recreates the ksp on the same communicator.
Definition Petsc.cpp:676
KSPSolver(const KSPSolver &)=delete
Delete copy and assignment constructor.
PetscReal getResidualNorm()
Returns the last residual norm of the KSP.
Definition Petsc.cpp:776
KSPSolver & operator=(const KSPSolver &)=delete
PetscInt getIterationNumber()
Returns the iteration number of solver, either during or after the solve call.
Definition Petsc.cpp:767
SolverResult solve(Vector &b, Vector &x)
Solves the linear system, returns false it not converged.
Definition Petsc.cpp:701
SolverResult
The state of the KSP after returning from solve()
Definition Petsc.hpp:263
@ Stopped
The solver reached the maximum iterations.
void setColumn(Vector &v, PetscInt col)
Definition Petsc.cpp:553
void read(const std::string &filename)
Reads the matrix from file, stored in PETSc binary format.
Definition Petsc.cpp:619
void write(const std::string &filename, VIEWERFORMAT format=ASCII) const
Writes the matrix to file.
Definition Petsc.cpp:611
void assemble(MatAssemblyType type=MAT_FINAL_ASSEMBLY)
Definition Petsc.cpp:485
void view() const
Prints the matrix.
Definition Petsc.cpp:627
std::pair< PetscInt, PetscInt > getSize() const
Returns (rows, cols) global size.
Definition Petsc.cpp:574
void setValue(PetscInt row, PetscInt col, PetscScalar value)
Definition Petsc.cpp:529
void viewDraw() const
Graphically draws the matrix structure.
Definition Petsc.cpp:635
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:494
std::pair< PetscInt, PetscInt > ownerRange() const
Returns a pair that mark the beginning and end of the matrix' ownership range.
Definition Petsc.cpp:588
PetscInt blockSize() const
Returns the block size of the matrix.
Definition Petsc.cpp:602
void reset()
Destroys and recreates the matrix on the same communicator.
Definition Petsc.cpp:511
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:581
MatInfo getInfo(MatInfoType flag) const
Get the MatInfo struct for the matrix.
Definition Petsc.cpp:522
std::pair< PetscInt, PetscInt > ownerRangeColumn() const
Returns a pair that mark the beginning and end of the matrix' column ownership range.
Definition Petsc.cpp:595
Vector & copyTo(precice::span< double > destination)
Definition Petsc.cpp:379
double l2norm() const
returns the l2-norm of the vector
Definition Petsc.cpp:435
PetscInt getLocalSize() const
Definition Petsc.cpp:313
void arange(double start, double stop)
Definition Petsc.cpp:329
void read(const std::string &filename, VIEWERFORMAT format=ASCII)
Reads the vector from file.
Definition Petsc.cpp:442
void setValue(PetscInt row, PetscScalar value)
Definition Petsc.cpp:322
void sort()
Sorts the LOCAL partition of the vector.
Definition Petsc.cpp:398
static Vector allocate(const std::string &name="")
Allocates a new vector on the given MPI communicator.
Definition Petsc.cpp:246
Vector & copyFrom(precice::span< const double > source)
Definition Petsc.cpp:362
void view() const
Prints the vector.
Definition Petsc.cpp:448
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:422
void write(const std::string &filename, VIEWERFORMAT format=ASCII) const
Writes the vector to file.
Definition Petsc.cpp:429
static logging::Logger _log
Definition Petsc.hpp:159
PetscInt getSize() const
Definition Petsc.cpp:304
void swap(Vector &other) noexcept
Swaps the ownership of two vectors.
Definition Petsc.cpp:284
Vector & operator=(const Vector &other)
Definition Petsc.cpp:201
Vector(const std::string &name="")
Creates a new vector on the given MPI communicator.
Definition Petsc.cpp:220
void init(PetscInt rows)
Sets the size and calls VecSetFromOptions.
Definition Petsc.cpp:295
PETSc related utilities.
Definition Petsc.cpp:121
void destroy(ISLocalToGlobalMapping *IS)
Destroys an ISLocalToGlobalMapping, if IS is not null and PetscIsInitialized.
Definition Petsc.cpp:796
void swap(Vector &lhs, Vector &rhs) noexcept
Definition Petsc.cpp:455
Main namespace of the precice library.