-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoisson.F90
131 lines (110 loc) · 3.43 KB
/
poisson.F90
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
program poisson
use, intrinsic :: iso_c_binding
use amgcl
implicit none
integer :: n, n3, idx, nnz, i, j, k, devnum
character(len=32) arg
integer(c_int), allocatable :: ptr(:), col(:)
real(c_double), allocatable :: val(:), rhs(:), x(:)
integer(c_size_t) :: prof, solver, params
type(conv_info) :: cnv
! Device to use:
devnum = -1
if (iargc() > 0) then
call getarg(1, arg)
read(arg, "(i4)") devnum
endif
! Create profiler.
prof = amgcl_profile_create();
! Assemble matrix in CRS format for a Poisson problem in n x n square.
call amgcl_profile_tic(prof, "assemble")
n = 64
n3 = n * n * n
allocate(ptr(n3 + 1))
allocate(col(n3 * 7))
allocate(val(n3 * 7))
ptr(1) = 1
idx = 1
nnz = 0
do k = 1,n
do j = 1,n
do i = 1,n
if (k > 1) then
nnz = nnz + 1
col(nnz) = idx - n * n
val(nnz) = -1
end if
if (j > 1) then
nnz = nnz + 1
col(nnz) = idx - n
val(nnz) = -1
end if
if (i > 1) then
nnz = nnz + 1
col(nnz) = idx - 1
val(nnz) = -1
end if
nnz = nnz + 1
col(nnz) = idx
val(nnz) = 6
if (i < n) then
nnz = nnz + 1
col(nnz) = idx + 1
val(nnz) = -1
end if
if (j < n) then
nnz = nnz + 1
col(nnz) = idx + n
val(nnz) = -1
end if
if (k < n) then
nnz = nnz + 1
col(nnz) = idx + n * n
val(nnz) = -1
end if
idx = idx + 1
ptr(idx) = nnz + 1
end do
end do
end do
call amgcl_profile_toc(prof, "assemble")
allocate(rhs(n3))
allocate(x(n3))
rhs = 1
x = 0
! Create solver parameters.
params = amgcl_params_create()
call amgcl_params_sets(params, "solver.type", "bicgstab")
call amgcl_params_setf(params, "solver.tol", 1e-6)
call amgcl_params_sets(params, "precond.relax.type", "spai0")
! Read parameters from a JSON file.
! An example of JSON file with the above parameters:
! {
! "solver" : {
! "type" : "bicgstab",
! "tol" : 1e-6
! },
! "precond" : {
! "relax" : {
! "type" : "spai0"
! }
! }
! }
! call amgcl_params_read_json(params, "params.json");
! Create solver, printout its structure.
call amgcl_profile_tic(prof, "setup")
solver = amgcl_solver_create(n3, ptr, col, val, devnum, params)
call amgcl_profile_toc(prof, "setup")
call amgcl_solver_report(solver)
! Solve the problem for the given right-hand-side.
call amgcl_profile_tic(prof, "solve")
cnv = amgcl_solver_solve(solver, rhs, x)
call amgcl_profile_toc(prof, "solve")
write(*,"('Iterations: ', I3, ', residual: ', E13.6)") cnv%iterations, cnv%residual
! Show profiling info
call amgcl_profile_report(prof)
! Destroy solver and parameter pack.
call amgcl_solver_destroy(solver)
call amgcl_params_destroy(params)
call amgcl_profile_destroy(prof)
end program poisson