-
Notifications
You must be signed in to change notification settings - Fork 1
/
Petsc.hpp
193 lines (129 loc) · 5.18 KB
/
Petsc.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once
#include <string>
#include <utility>
#include "petscvec.h"
#include "petscmat.h"
#include "petscksp.h"
#include "petscis.h"
namespace petsc {
enum VIEWERFORMAT { ASCII, BINARY };
class Matrix;
class Vector
{
public:
Vec vector = nullptr;
enum LEFTRIGHT { LEFT, RIGHT };
/// Creates a new vector on the given MPI communicator.
explicit Vector(std::string name = "");
/// Use Vec v as vector.
Vector(Vec &v, std::string name = "");
/// Duplicates type, row layout etc. (not values) of v.
Vector(Vector &v, std::string name = "");
/// Constructs a vector with the same number of rows (default) or columns.
Vector(Mat &m, std::string name = "", LEFTRIGHT type = LEFT);
/// Constructs a vector with the same number of rows (default) or columns.
Vector(Matrix &m, std::string name = "", LEFTRIGHT type = LEFT);
/// Delete copy and assignment constructor
/** Copying and assignement of this class would involve copying the pointer to
the PETSc object and finallly cause double destruction of it.
*/
Vector(const Vector&) = delete;
Vector& operator=(const Vector&) = delete;
~Vector();
/// Enables implicit conversion into a reference to a PETSc Vec type
operator Vec&();
/// Sets the size and calls VecSetFromOptions
void init(PetscInt rows);
int getSize();
int getLocalSize();
void setValue(PetscInt row, PetscScalar value);
void arange(double start, double stop);
void fillWithRandoms();
/// Sorts the LOCAL partion of the vector
void sort();
void assemble();
/// Returns a pair that mark the beginning and end of the vectors ownership range. Use first und second to access.
std::pair<PetscInt, PetscInt> ownerRange();
/// Writes the vector to file.
void write(std::string filename, VIEWERFORMAT format = ASCII);
/// Reads the vector from file.
void read(std::string filename, VIEWERFORMAT format = ASCII);
void view();
};
class Matrix
{
public:
Mat matrix = nullptr;
/// Delete copy and assignment constructor
/** Copying and assignment of this class would involve copying the pointer to
the PETSc object and finallly cause double destruction of it.
*/
Matrix(const Matrix&) = delete;
Matrix& operator=(const Matrix&) = delete;
explicit Matrix(std::string name = "");
/// Move constructor, use the implicitly declared.
Matrix(Matrix&& other) = default;
~Matrix();
/// Enables implicit conversion into a reference to a PETSc Mat type
operator Mat&();
void assemble(MatAssemblyType type = MAT_FINAL_ASSEMBLY);
/// Initializes matrix of given size and type
/** @param[in] localRows,localCols The number of rows/cols that are local to the processor
@param[in] globalRows,globalCols The number of global rows/cols.
@param[in] type PETSc type of the matrix
@param[in] doSetup Call MatSetup(). Not calling MatSetup can have performance gains when using preallocation
*/
void init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols,
MatType type = nullptr, bool doSetup = true);
/// Destroys and recreates the matrix on the same communicator
void reset();
/// Get the MatInfo struct for the matrix.
/** See http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatInfo.html for description of fields. */
MatInfo getInfo(MatInfoType flag);
void setValue(PetscInt row, PetscInt col, PetscScalar value);
void fillWithRandoms();
void setColumn(Vector &v, int col);
/// Returns (rows, cols) global size
std::pair<PetscInt, PetscInt> getSize();
/// Returns (rows, cols) local size
std::pair<PetscInt, PetscInt> getLocalSize();
/// Returns a pair that mark the beginning and end of the matrix' ownership range.
std::pair<PetscInt, PetscInt> ownerRange();
/// Returns a pair that mark the beginning and end of the matrix' column ownership range.
std::pair<PetscInt, PetscInt> ownerRangeColumn();
/// Returns the block size of the matrix
PetscInt blockSize() const;
/// Writes the matrix to file.
void write(std::string filename, VIEWERFORMAT format = ASCII);
/// Reads the matrix from file, stored in PETSc binary format
void read(std::string filename);
/// Prints the matrix
void view();
void viewDraw();
};
class KSPSolver
{
public:
KSP ksp;
/// Delete copy and assignment constructor
/** Copying and assignment of this class would involve copying the pointer to
the PETSc object and finallly cause double destruction of it.
*/
KSPSolver(const KSPSolver&) = delete;
KSPSolver& operator=(const KSPSolver&) = delete;
explicit KSPSolver(std::string name = "");
/// Move constructor, use the implicitly declared.
KSPSolver(KSPSolver&& other) = default;
~KSPSolver();
/// Enables implicit conversion into a reference to a PETSc KSP type
operator KSP&();
/// Destroys and recreates the ksp on the same communicator
void reset();
/// Solves the linear system, returns false it not converged
bool solve(Vector &b, Vector &x);
};
/// Destroys an KSP, if ksp is not null and PetscIsInitialized
void destroy(KSP * ksp);
/// Destroys an ISLocalToGlobalMapping, if IS is not null and PetscIsInitialized
void destroy(ISLocalToGlobalMapping * IS);
}