-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolver.f90
265 lines (177 loc) · 5.54 KB
/
solver.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
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
SUBROUTINE solver_BICG3(coef,jcoef,b,x,n,m,eps,p,r,r2,v,ss,t,itmax,myid,iDIMstart,iDIMend,DIMcount)
!-----------------------------------------------------------------------!
! !
! BICONJUGUATE GRADIENT FILL-IN 2 !
! !
! ATTENTION : coef, jcoef et b are modified in this routine !
! --------- !
! !
!-----------------------------------------------------------------------!
use omp_lib
implicit none
!---------------------------------------------------!
! VARIABLES IN CALL !
!---------------------------------------------------!
integer, intent(in) :: n,m,itmax
double precision, intent(in) :: eps
double precision :: starttime
integer, dimension(m), intent(inout) :: jcoef
double precision, dimension(n,m), intent (inout) :: coef
double precision, dimension(n), intent (inout) :: b,x
double precision, dimension(n), intent(inout) :: p,r,r2 ,v,ss,t
!---------------------------------------------------!
! VARIABLES IN CALL OF MPI !
!---------------------------------------------------!
integer :: myid, iDIMstart, iDIMend
integer ,dimension(1:40) :: DIMcount
!---------------------------------------------------!
! LOCAL VARIABLES !
!---------------------------------------------------!
double precision :: alpha,beta,nu,mu,norm0,norm,sum,scal,norm1,norm2 ,omega,rho1,rho2
integer :: i,j,col, nx,ny,nz,ik,k
!iDIMstart, iDIMend
!---------------------------------------------------!
! BI CONJUGUATE GRADIENT !
!---------------------------------------------------!
norm0=norm2(b,n)
call matmul_ell(p,coef,jcoef,x,n,m)
!$OMP PARALLEL DO
do i=1,n; r(i)=b(i)-p(i); end do
!$OMP END PARALLEL DO
!$OMP PARALLEL DO
do i=1,n; r2(i) = r(i) ; end do
!$OMP END PARALLEL DO
rho1 = 1
alpha = 1
omega = 1
!$OMP PARALLEL DO
do i=1,n; v(i) = 0; end do
!$OMP END PARALLEL DO
!$OMP PARALLEL DO
do i=1,n; p(i) = 0; end do
!$OMP END PARALLEL DO
norm=0.
!$OMP PARALLEL DO
do i=1,n; norm=norm+r(i)*r(i); end do
!$OMP END PARALLEL DO
norm=sqrt(norm)/norm0
j=0
do while (norm>eps.and.j.lt.itmax)
j=j+1
rho2 = scal(r2,r,n)
beta = (rho2/rho1) * (alpha/omega)
!$OMP PARALLEL DO
do i=1,n; p(i) = r(i) + beta*(p(i)-omega*v(i)); end do
!$OMP END PARALLEL DO
call matmul_ell(v,coef,jcoef,p,n,m)
alpha = rho2 / scal(r2,v,n)
!$OMP PARALLEL DO
do i=1,n; ss(i) = r(i) - alpha*v(i); end do
!$OMP END PARALLEL DO
call matmul_ell(t,coef,jcoef,ss,n,m)
omega = scal(t,ss,n) / scal(t,t,n)
!$OMP PARALLEL DO
do i=1,n; x(i)=x(i)+alpha*p(i)+omega*ss(i); end do
!$OMP END PARALLEL DO
!$OMP PARALLEL DO
do i=1,n; r(i)=ss(i)-omega*t(i); end do
!$OMP END PARALLEL DO
rho1 = rho2
norm=0.0
!$OMP PARALLEL DO
do i=1,n; norm=norm+r(i)*r(i); end do
!$OMP END PARALLEL DO
norm=sqrt(norm)/norm0
end do
!---------------------------------------------------!
! SOLUTION SCALING !
!---------------------------------------------------!
if(myid==0)then
if(j.ge.itmax) then
print*, ' '
print*, 'non convergence =', j, norm
else
print*, ' '
print*, ' Iterations BICG3 =', j, norm
endif
endif
return
END SUBROUTINE solver_BICG3
SUBROUTINE matmul_ell(x,coef,jcoef,y,n,m)
implicit none
integer, intent(in) :: n,m
real*8, dimension(n,m), intent (in) :: coef
integer, dimension(m), intent(in) :: jcoef
real*8, dimension(n), intent(in) :: y
real*8, dimension(n), intent(out) :: x
integer :: i,j,col,a,b
!$OMP PARALLEL DO
do i=1,n
x(i)=coef(i,1)*y(i)
end do
!$OMP END PARALLEL DO
a = 0
b = 0
do j=2,m
col=jcoef(j)
if(j.eq.2)then
a = 0
b = 1
elseif (j.eq.3) then
a = 1
b = 2
elseif (j.eq.4) then
a = 2
b = 3
end if
!$OMP PARALLEL DO
do i=1,n-col
x(i)=x(i)+coef(i,j+a)*y(i+col)
end do
!$OMP END PARALLEL DO
!$OMP PARALLEL DO
do i=1+col,n
x(i)=x(i)+coef(i,j+b)*y(i-col)
end do
!$OMP END PARALLEL DO
!x(i+col)=x(i+col)+coef(i+col,j+b)*y(i)
end do
return
END SUBROUTINE matmul_ell
FUNCTION scal(x,y,n) result(res)
use omp_lib
implicit none
integer, intent(in) :: n
double precision, dimension(n), intent(in) :: x,y
integer :: i
double precision :: res
res=0.
!$OMP PARALLEL DO REDUCTION(+:res)
do i=1,n; res=res+x(i)*y(i); end do
!$OMP END PARALLEL DO
END FUNCTION
FUNCTION norm1(x,n) result(res)
use omp_lib
implicit none
integer, intent(in) :: n
double precision, dimension(n), intent(in) :: x
integer :: i
double precision :: res
res=0.
!$OMP PARALLEL DO REDUCTION(+:res)
do i=1,n; res=res+x(i)*x(i); end do
!$OMP END PARALLEL DO
END FUNCTION
FUNCTION norm2(x,n) result(res)
use omp_lib
implicit none
integer, intent(in) :: n
double precision, dimension(n), intent(in) :: x
integer :: i
double precision :: res
res=0.
!$OMP PARALLEL DO REDUCTION(+:res)
do i=1,n; res=res+x(i)*x(i); end do
!$OMP END PARALLEL DO
res=sqrt(res)
END FUNCTION