-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpetsctest.py
More file actions
38 lines (31 loc) · 748 Bytes
/
petsctest.py
File metadata and controls
38 lines (31 loc) · 748 Bytes
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
from petsc4py import PETSc as Pet
import numpy as np
import scipy.sparse
A = np.array([[2,0,0,0],[0,2,0,0],[0,0,2,0],[0,0,0,2]])
print A
print A.shape
A_csr = scipy.sparse.csr_matrix(A)
A_petsc = Pet.Mat().createAIJ(size=A_csr.shape,
csr = (A_csr.indptr, A_csr.indices, A_csr.data))
x,b = A_petsc.getVecs()
b.set(1)
print b.getArray()
# create linear solver
ksp = Pet.KSP()
ksp.create(Pet.COMM_WORLD)
ksp.setType('cg')
pc = ksp.getPC()
pc.setType(pc.Type.GAMG)
ksp.setOperators(A_petsc)
ksp.solve(b,x)
print x.getArray()
# and incomplete Cholesky
#ksp.getPC().setType('icc')
# obtain sol & rhs vectors
#x, b = A.createVecs()
#x.set(0)
#b.set(1)
# and next solve
#ksp.setOperators(A)
#ksp.setFromOptions()
#ksp.solve(b, x)