forked from rosamu/SPH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.cpp
355 lines (284 loc) · 8.09 KB
/
solver.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* PETSc.c
* Only for one node.
* This class PETSc is created to be used a handy interface
* to the function calls to PETSc. For each algebric equation
* Ax=b, one PETSc instance is needed, since this instance has
* the storage of these three variables.
*/
#include "solver.h"
PETSc::PETSc()
{
x = NULL; /* approx solution, RHS*/
b = NULL;
A = NULL; /* linear system matrix */
ksp = NULL; /* Krylov subspace method context */
nullsp = NULL;
pc = NULL;
KSPCreate(PETSC_COMM_WORLD,&ksp);
}
PETSc::PETSc(int ilower, int iupper, int d_nz, int o_nz)
{
x = NULL; /* approx solution, RHS*/
b = NULL;
A = NULL; /* linear system matrix */
ksp = NULL; /* Krylov subspace method context */
nullsp = NULL;
pc = NULL;
Create(ilower, iupper, d_nz, o_nz);
KSPCreate(PETSC_COMM_WORLD,&ksp);
}
void PETSc::Create(int ilower, int iupper, int d_nz, int o_nz)
{
Create(PETSC_COMM_WORLD, ilower, iupper, d_nz, o_nz);
}
void PETSc::Create(
MPI_Comm Comm,
int ilower,
int iupper,
int d_nz,
int o_nz)
{
int n = iupper - ilower +1;
comm = Comm;
iLower = ilower;
iUpper = iupper;
MatCreateMPIAIJ(PETSC_COMM_WORLD,n,n,PETSC_DECIDE,PETSC_DECIDE,
d_nz,PETSC_NULL,o_nz,PETSC_NULL,&A);
ierr = PetscObjectSetName((PetscObject) A, "A");
ierr = MatSetFromOptions(A);
// b
ierr = VecCreate(PETSC_COMM_WORLD, &b);
ierr = PetscObjectSetName((PetscObject) b, "b");
ierr = VecSetSizes(b, n, PETSC_DECIDE);
ierr = VecSetFromOptions(b);
ierr = VecCreate(PETSC_COMM_WORLD,&x);
ierr = PetscObjectSetName((PetscObject) x, "X");
ierr = VecSetSizes(x, n, PETSC_DECIDE);
ierr = VecSetFromOptions(x);
}
PETSc::~PETSc()
{
if(x!=NULL)
{
VecDestroy(&x);
x = NULL;
}
if(b!=NULL)
{
VecDestroy(&b);
b = NULL;
}
if(A!=NULL)
{
MatDestroy(&A);
A = NULL;
}
if(ksp!=NULL)
{
KSPDestroy(&ksp);
ksp = NULL;
}
if(nullsp!=NULL)
{
MatNullSpaceDestroy(&nullsp);
nullsp = NULL;
}
}
void PETSc::Reset_A() // Reset all entries to zero ;
{
MatZeroEntries(A);
}
void PETSc::Reset_b() // Reset all entries to zero ;
{
VecZeroEntries(b);
}
void PETSc::Reset_x()
{
VecZeroEntries(x);
}
// A
void PETSc::Set_A(PetscInt i, PetscInt j, double val) // A[i][j]=val;
{
ierr = MatSetValues(A,1,&i,1,&j,&val,INSERT_VALUES);
}
void PETSc::Add_A(PetscInt i, PetscInt j, double val) // A[i][j]+=val;
{
ierr = MatSetValues(A,1,&i,1,&j,&val,ADD_VALUES);
}
void PETSc::Get_row_of_A(PetscInt i, PetscInt *ncol, PetscInt **cols, double **row)
{
ierr = MatGetRow(A,i,ncol,(const PetscInt**)cols,
(const PetscScalar**)row);
ierr = MatRestoreRow(A,i,ncol,(const PetscInt**)cols,
(const PetscScalar**)row);
}
// x
void PETSc::Set_x(PetscInt i, double val) // x[i]=val;
{
ierr = VecSetValues(x,1,&i,&val,INSERT_VALUES);
}
void PETSc::Add_x(PetscInt i, double val) // x[i]+=val;
{
ierr = VecSetValues(x,1,&i,&val,ADD_VALUES);
}
void PETSc::Set_b(PetscInt i, double val) // x[i]=val;
{
ierr = VecSetValues(b,1,&i,&val,INSERT_VALUES);
}
void PETSc::Add_b(
PetscInt i,
double val) // x[i]+=val;
{
ierr = VecSetValues(b,1,&i,&val,ADD_VALUES);
}
void PETSc::Get_x(double *p)
{
PetscScalar *values;
VecGetArray(x,&values);
for(int i = 0; i < iUpper-iLower+1; i++)
p[i] = values[i];
VecRestoreArray(x,&values);
}
void PETSc::Get_b(double *p)
{
PetscScalar *values;
VecGetArray(b,&values);
for(int i = 0; i < iUpper-iLower+1; i++)
p[i] = values[i];
VecRestoreArray(b,&values);
}
void PETSc::Get_x(double *p,
int n,
int *global_index)
{
}
void PETSc::SetMaxIter(int val)
{
PetscInt maxits;
double rtol, atol, dtol;
KSPGetTolerances(ksp, &rtol, &atol, &dtol, &maxits);
ierr = KSPSetTolerances(ksp, rtol, atol, dtol, val);
}
void PETSc::SetTol(double val)
{
PetscInt maxits;
double rtol, atol, dtol;
KSPGetTolerances(ksp, &rtol, &atol, &dtol, &maxits);
ierr = KSPSetTolerances(ksp, val, atol, dtol, maxits);
}
void PETSc::SetKDim(int val)
{
}
void PETSc::GetNumIterations(PetscInt *num_iterations)
{
KSPGetIterationNumber(ksp,num_iterations);
}
void PETSc::GetFinalRelativeResidualNorm(double *rel_resid_norm)
{
KSPGetResidualNorm(ksp,rel_resid_norm);
}
void PETSc::Solve_GMRES(void)
{
//start_clock("Before Assemble matrix and vector");
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
ierr = VecAssemblyBegin(x);
ierr = VecAssemblyEnd(x);
ierr = VecAssemblyBegin(b);
ierr = VecAssemblyEnd(b);
//stop_clock("After Assembly matrix and vector");
KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
KSPSetType(ksp,KSPGMRES);
//KSPGetPC(ksp, &pc);
//PCSetType(pc, PCJACOBI);
KSPSetFromOptions(ksp);
KSPSetUp(ksp);
//start_clock("Before KSPSolve");
KSPSolve(ksp,b,x);
// stop_clock("After KSPSolve");
}
void PETSc::Solve(void)
{
//start_clock("Before Assemble matrix and vector");
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
ierr = VecAssemblyBegin(x);
ierr = VecAssemblyEnd(x);
ierr = VecAssemblyBegin(b);
ierr = VecAssemblyEnd(b);
//stop_clock("After Assembly matrix and vector");
KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
KSPSetType(ksp,KSPBCGSL);
KSPBCGSLSetEll(ksp,2);
//KSPGetPC(ksp, &pc);
//PCSetType(pc, PCJACOBI);
KSPSetFromOptions(ksp);
KSPSetUp(ksp);
//start_clock("Before KSPSolve");
KSPSolve(ksp,b,x);
//stop_clock("After KSPSolve");
}
void PETSc::Solve_withPureNeumann_GMRES(void)
{
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
ierr = VecAssemblyBegin(x);
ierr = VecAssemblyEnd(x);
ierr = VecAssemblyBegin(b);
ierr = VecAssemblyEnd(b);
MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,PETSC_NULL,&nullsp);
KSPSetNullSpace(ksp,nullsp);
MatNullSpaceRemove(nullsp,b,PETSC_NULL);
KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
KSPSetType(ksp,KSPGMRES);
//KSPGetPC(ksp, &pc);
//PCSetType(pc, PCASM);
KSPSetFromOptions(ksp);
KSPSetUp(ksp);
//start_clock("Before Petsc Solve in pure neumann solver");
KSPSolve(ksp,b,x);
//stop_clock("After Petsc Solve in pure neumann solver");
}
void PETSc::Solve_withPureNeumann(void)
{
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
ierr = VecAssemblyBegin(x);
ierr = VecAssemblyEnd(x);
ierr = VecAssemblyBegin(b);
ierr = VecAssemblyEnd(b);
MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,PETSC_NULL,&nullsp);
KSPSetNullSpace(ksp,nullsp);
KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
//KSPSetType(ksp,KSPMINRES);
//KSPSetType(ksp,KSPGMRES);
//KSPSetType(ksp,KSPBCGS);
KSPSetType(ksp,KSPBCGSL);
KSPBCGSLSetEll(ksp,2);
//KSPGetPC(ksp, &pc);
//PCSetType(pc, PCASM);
//PCSetType(pc, PCMG);
//PCMGSetLevels(pc, 3, &PETSC_COMM_WORLD);
//PCMGSetType(pc,PC_MG_MULTIPLICATIVE);
//PCMGSetCycleType(pc,PC_MG_CYCLE_V);
//
KSPSetFromOptions(ksp);
KSPSetUp(ksp);
//start_clock("Before Petsc Solve in pure neumann solver");
KSPSolve(ksp,b,x);
//stop_clock("After Petsc Solve in pure neumann solver");
}
void PETSc::Print_A(const char *filename)
{
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_MATLAB);
MatView(A, PETSC_VIEWER_STDOUT_WORLD);
}
void PETSc::Print_b(const char *filename)
{
ierr = VecAssemblyBegin(b);
ierr = VecAssemblyEnd(b);
PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_MATLAB);
VecView(b, PETSC_VIEWER_STDOUT_WORLD);
}