-
Notifications
You must be signed in to change notification settings - Fork 24
/
demo.py
60 lines (48 loc) · 1.18 KB
/
demo.py
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
import numpy as np
import scipy.sparse as sparse
import scipy.sparse.linalg as splinalg
import pyamgx
pyamgx.initialize()
# Initialize config and resources:
cfg = pyamgx.Config().create_from_dict({
"config_version": 2,
"determinism_flag": 1,
"exception_handling" : 1,
"solver": {
"monitor_residual": 1,
"solver": "BICGSTAB",
"convergence": "RELATIVE_INI_CORE",
"preconditioner": {
"solver": "NOSOLVER"
}
}
})
rsc = pyamgx.Resources().create_simple(cfg)
# Create matrices and vectors:
A = pyamgx.Matrix().create(rsc)
b = pyamgx.Vector().create(rsc)
x = pyamgx.Vector().create(rsc)
# Create solver:
solver = pyamgx.Solver().create(rsc, cfg)
# Upload system:
M = sparse.csr_matrix(np.random.rand(5, 5))
rhs = np.random.rand(5)
sol = np.zeros(5, dtype=np.float64)
A.upload_CSR(M)
b.upload(rhs)
x.upload(sol)
# Setup and solve system:
solver.setup(A)
solver.solve(b, x)
# Download solution
x.download(sol)
print("pyamgx solution: ", sol)
print("scipy solution: ", splinalg.spsolve(M, rhs))
# Clean up:
A.destroy()
x.destroy()
b.destroy()
solver.destroy()
rsc.destroy()
cfg.destroy()
pyamgx.finalize()