-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinAlgWrapper.hpp
151 lines (112 loc) · 2.98 KB
/
LinAlgWrapper.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
#ifndef _LINALGWRAPPER_H
#define _LINALGWRAPPER_H
#include <petscksp.h>
#include <iostream>
#include <vector>
void clearLinAlg(){
PetscInt state;
PetscFinalized(&state);
if (!state) PetscFinalize();
}
class LinVector{
public:
LinVector(unsigned int length) {LinVector(); set_sizes(length); VecSetFromOptions(_vec);};
void set_size(unsigned int length){VecSetSizes(_vec, PETSC_DECIDE, length);};
unsigned int get_size() { PetscInt sz; VecGetSize(_vec, &sz); return sz;};
void set(double value){VecSet(_vec, value);};
void set_values(unsigned int nvals, double & vals, unsigned int & inds = NULL){
if (inds==NULL && nvals==get_size()){
PetscInt ix[nvals];
for (auto i=0; i<nvals; i++) ix[i] = i;
VecSetValues(_vec, nvals, ix, &vals, INSERT_VALUES);
}
}
void get_values();
void assemble() {VecAssemblyBegin(_vec); VecAssemblyEnd(_vec);};
LinVector duplicate(){
LinVector outvec;
VecDuplicate(_vec, &outvec._vec);
return outvec;
}
LinVector subset(unsigned int start_ind, unsigned int end_ind){
LinVector outvec;
//im not sure how to implement this best
return outvec;
}
void print_summary() {VecView(_vec,PETSC_VIEWER_STDOUT_WORLD);};
protected:
LinVector(){
int mpi_init;
MPI_Initialized(&mpi_init);
if (!mpi_init) MPI_Init(NULL, NULL);
PetscBool init;
PetscInitialized(&init);
if (!init) PetscInitializeNoArguments();
VecCreate(PETSC_COMM_WORLD,&_vec);
}
~LinVector(){
VecDestroy(_vec);
};
void set_type();
void get_type();
private:
//bool size_set;
//bool values_set;
//bool type_set;
//bool assembly_set;
Vec _vec; // petsc vector type
};
class LinMatrix{
public:
LinMatrix(unsigned int rows, unsigned int cols) {LinMatrix(); set_sizes(rows, cols); MatSetFromOptions(_mat);};
void set_size(unsigned int rows, unsigned int cols){MatSetSizes(_mat, PETSC_DECIDE, PETSC_DECIDE, rows, cols);};
unsigned int get_size() { PetscInt nrows, ncols; MatGetSize(_vec, &nrows, &ncols); return nrows*ncols;};
void set_values(unsigned int nvals, double & vals, unsigned int & inds = NULL){
if (inds==NULL && nvals==get_size()){
PetscInt ix[nvals];
for (auto i=0; i<nvals; i++) ix[i] = i;
VecSetValues(_vec, nvals, ix, &vals, INSERT_VALUES);
}
}
void set_values_row_major(double & vals);
void set_values_col_major(double & vals);
void get_values();
void assemble() {MatAssemblyBegin(_mat); MatAssemblyEnd(_mat);};
set_row();
set_column();
get_row();
get_column();
protected:
LinMatrix(){
int mpi_init;
MPI_Initialized(&mpi_init);
if (!mpi_init) MPI_Init(NULL, NULL);
PetscBool init;
PetscInitialized(&init);
if (!init) PetscInitializeNoArguments();
MatCreate(PETSC_COMM_WORLD,&_mat);
}
~LinMatrix(){
MatDestroy(&_mat);
}
private:
Mat _mat; // petsc matrix type
};
class LinSolver{
public:
LinSolver();
~LinSolver();
private:
KSP _ksp; // petsc linear solver type
};
class NonlinSolver{
public:
private:
};
class LinPreconditioner{
public:
LinPreconditioner();
~LinPreconditioner();
private:
};
#endif