-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvers.f90
506 lines (386 loc) · 15.5 KB
/
solvers.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
! Part of Zinc FE package. Author: John Blackburn
module solvers
implicit none
private
public solve,freesolvers
integer leniw,lenw,itol
integer, parameter :: nsave=10,iunit=1,isym=0
double precision, allocatable :: rwork(:),Ax(:)
integer, allocatable :: iwork(:),Ap(:),Ai(:)
logical :: init=.true.
contains
subroutine sor(ndofred,RR,vecred,lenQ,iQ,jQ,Qval,tol,itmax,iter,residDiag,ierr,iunit, &
irowst,irowed,omega,nstride) ! <-- these are SOR specific
integer ndofred,lenQ,iQ(:),jQ(:),itmax,iter,irowst(:),irowed(:),iunit,ierr,nstride
double precision RR(:),vecred(:),Qval(:),tol,residDiag,omega
integer i,ip,jQ1
double precision Qval1,fac,tot,lhs,rhs,lhsI,rhsI
double precision lhsDiag,rhsDiag,lhsIDiag,rhsIDiag,resid,pivot
if (iunit>0) write (iunit,'(a5,3a13)') 'iter','resid','lhs','rhs'
write (*,'(a5,3a13)') 'iter','resid','lhs','rhs'
! print *,'SOR:'
! print *,ndofred
do iter=0,itmax
if (mod(iter,nstride)==0) then
resid=0; residDiag=0
lhs=0; lhsDiag=0
rhs=0; rhsDiag=0
! print '(a5,9a13)','i','lhsI','rhsI','pivot','lhsIDiag','rhsIDiag','resid','residDiag','lhs','rhs'
do i=1,ndofred
lhsI=0
rhsI=RR(i)
pivot=0
do ip=irowst(i),irowed(i)
jQ1=jQ(ip)
if (jQ1==-1) then
print *,'Bad vecred:',ip
stop
endif
lhsI=lhsI+Qval(ip)*vecred(jQ1)
if (jQ1==i) pivot=Qval(ip)
enddo
lhsIDiag=lhsI/pivot
rhsIDiag=rhsI/pivot
resid=resid+(lhsI-rhsI)**2
lhs=lhs+lhsI**2
rhs=rhs+rhsI**2
residDiag=residDiag+(lhsIDiag-rhsIDiag)**2
lhsDiag=lhsDiag+lhsIDiag**2
rhsDiag=rhsDiag+rhsIDiag**2
! print '(i5,9e13.5)',i,lhsI,rhsI,pivot,lhsIDiag,rhsIDiag,resid,residDiag,lhs,rhs
enddo
! stop
resid=sqrt(resid)/sqrt(lhs); residDiag=sqrt(residDiag)/sqrt(lhsDiag)
lhs=sqrt(lhs); lhsDiag=sqrt(lhsDiag)
rhs=sqrt(rhs); rhsDiag=sqrt(rhsDiag)
if (iunit>0) write (iunit,'(i5,6e13.5)') iter,resid,lhs,rhs,residDiag,lhsDiag,rhsDiag
write (*,'(i5,6e13.5)') iter,resid,lhs,rhs,residDiag,lhsDiag,rhsDiag
if (residDiag<tol) then
ierr=0 ! converged
return
endif
endif
do i=1,ndofred
tot=0
do ip=irowst(i),irowed(i)
jQ1=jQ(ip)
Qval1=Qval(ip)
if (jQ1==i) then
fac=Qval1
else
tot=tot+Qval1*vecred(jQ1)
endif
enddo
vecred(i)=(1-omega)*vecred(i)+omega*(RR(i)-tot)/fac
enddo
enddo
ierr=1 ! reached itmax
end subroutine sor
! ######################################################################
subroutine ssor(ndofred,RR,vecred,lenQ,iQ,jQ,Qval,tol,itmax,iter,residDiag,ierr,iunit, &
irowst,irowed,omega,nstride)
integer ndofred,lenQ,iQ(:),jQ(:),itmax,iter,irowst(:),irowed(:),iunit,ierr,nstride
double precision RR(:),vecred(:),Qval(:),tol,residDiag,omega
integer i,ip,jQ1
double precision Qval1,fac,tot,lhs,rhs,lhsI,rhsI
double precision lhsDiag,rhsDiag,lhsIDiag,rhsIDiag,resid,pivot
if (iunit>0) write (iunit,'(a5,3a13)') 'iter','resid','lhs','rhs'
write (*,'(a5,3a13)') 'iter','resid','lhs','rhs'
do iter=0,itmax
if (mod(iter,nstride)==0) then
resid=0; residDiag=0
lhs=0; lhsDiag=0
rhs=0; rhsDiag=0
do i=1,ndofred
lhsI=0
rhsI=RR(i)
pivot=0
do ip=irowst(i),irowed(i)
jQ1=jQ(ip)
lhsI=lhsI+Qval(ip)*vecred(jQ1)
if (jQ1==i) pivot=Qval(ip)
enddo
lhsIDiag=lhsI/pivot
rhsIDiag=rhsI/pivot
resid=resid+(lhsI-rhsI)**2
lhs=lhs+lhsI**2
rhs=rhs+rhsI**2
residDiag=residDiag+(lhsIDiag-rhsIDiag)**2
lhsDiag=lhsDiag+lhsIDiag**2
rhsDiag=rhsDiag+rhsIDiag**2
enddo
resid=sqrt(resid)/sqrt(lhs); residDiag=sqrt(residDiag)/sqrt(lhsDiag)
lhs=sqrt(lhs); lhsDiag=sqrt(lhsDiag)
rhs=sqrt(rhs); rhsDiag=sqrt(rhsDiag)
if (iunit>0) write (iunit,'(i5,6e13.5)') iter,resid,lhs,rhs,residDiag,lhsDiag,rhsDiag
write (*,'(i5,6e13.5)') iter,resid,lhs,rhs,residDiag,lhsDiag,rhsDiag
if (residDiag<tol) then
ierr=0 ! converged
return
endif
endif
do i=1,ndofred
tot=0
do ip=irowst(i),irowed(i)
jQ1=jQ(ip)
Qval1=Qval(ip)
if (jQ1==i) then
fac=Qval1
else
tot=tot+Qval1*vecred(jQ1)
endif
enddo
vecred(i)=(1-omega)*vecred(i)+omega*(RR(i)-tot)/fac
enddo
do i=ndofred,1,-1
tot=0
do ip=irowst(i),irowed(i)
jQ1=jQ(ip)
Qval1=Qval(ip)
if (jQ1==i) then
fac=Qval1
else
tot=tot+Qval1*vecred(jQ1)
endif
enddo
vecred(i)=(1-omega)*vecred(i)+omega*(RR(i)-tot)/fac
enddo
enddo
ierr=1 ! reached itmax
end subroutine ssor
! ######################################################################
subroutine solve(ndofx,vecx,rhsx,iter,err,ierrslap,resid,lhs,rhs,residDiag,lhsDiag,rhsDiag)
! ----------------------------------------------------------------------
! Solve Q * vecx = rhsx. On input vecx is initial guess, on output holds solution
! The matrix is always Q [stored as iQ,jQ,Qval] but can actually hold Jacobian
! rhsx might be RR or negative residual vector
! ndofx (ndof or ndofred) and lenQ will always be the same if this is called repeatedly for NL
! NOTE: calling the slap routines converts iQ, jQ, Qval to column format
! ie it DESTROYS iQ,jQ,Qval
! ----------------------------------------------------------------------
use common
use umfpack_mod
use iofile
integer ndofx,iter,ierrslap
double precision vecx(:),rhsx(:),err
double precision lhs,rhs,lhsI,rhsI,resid
double precision lhsDiag,rhsDiag,lhsIDiag,rhsIDiag,residDiag,pivot
double precision lhsvec(ndofx) ! auto arrays
integer i,j,ip,ist,ied,iQx,ret,jQ1
integer Numeric,Symbolic ! C pointers
! ----------------------------------------------------------------------
! First call, allocate workspace arrays for SLAP and UMFPACK
! ----------------------------------------------------------------------
! print *,'Initsolvers:'
! print *,ndof,ndofred,ndofx
if (init) then
print *,'call initsolvers'
init=.false.
call initsolvers(ndofx)
endif
! ----------------------------------------------------------------------
! Run sor, ssor (this file) or SLAP routines
! ----------------------------------------------------------------------
if (key_sim == 1) then
print *,'Starting SOR solver'
call sor(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,tol,itmax,iter,err,ierrslap,iunit, &
irowst,irowed,omega,nstride)
else if (key_sim == 0) then
print *,'Starting Symmetric SOR (SSOR) solver'
call ssor(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,tol,itmax,iter,err,ierrslap,iunit, &
irowst,irowed,omega,nstride)
else if (key_sim == 3) then
print *,'Starting GMRES (Diag scaled)' ! d s d gmr
call dsdgmr(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,isym,nsave,itol,tol,&
itmax,iter,err,ierrslap,iunit,rwork,lenw,iwork,leniw)
else if (key_sim == 4) then
print *,'Starting GMRES (Incomplete LU)' ! d s lu gm
call dslugm(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,isym,nsave,itol,tol,&
itmax,iter,err,ierrslap,iunit,rwork,lenw,iwork,leniw)
else if (key_sim == 5) then ! d s d bcg
print *,'Starting Biconjugate Gradient (Diag scaled)'
call dsdbcg(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,isym,itol,tol, &
itmax,iter,err,ierrslap,iunit,rwork,lenw,iwork,leniw)
else if (key_sim == 6) then ! d s lu bc
print *,'Starting Biconjugate Gradient (Incomplete LU)'
call dslubc(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,isym,itol,tol, &
itmax,iter,err,ierrslap,iunit,rwork,lenw,iwork,leniw)
else if (key_sim == 7) then ! d s d cg
print *,'Starting Conjugate Gradient method (Diag scaled)'
print *,'WARNING: this should only be used for symmetric, +ve definite systems'
call dsdcg (ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,isym,itol,tol, &
itmax,iter,err,ierrslap,iunit,rwork,lenw,iwork,leniw)
else if (key_sim == 8) then ! d s ic cg
print *,'Starting Conjugate Gradient method (Incomplete Cholesky factorisation)'
print *,'WARNING: this should only be used for symmetric, +ve definite systems'
call dsiccg(ndofx,rhsx,vecx,lenQ,iQ,jQ,Qval,isym,itol,tol, &
itmax,iter,err,ierrslap,iunit,rwork,lenw,iwork,leniw)
! ----------------------------------------------------------------------
! Run UMFPACK
! ----------------------------------------------------------------------
else if (key_sim == 9) then
print *,'Starting UMFPACK (direct solver)'
! print *,ndofx,lenQ,init
do ip=1,lenQ ! need base 0
iQ(ip)=iQ(ip)-1
jQ(ip)=jQ(ip)-1
enddo
! allocate (Ap(ndofx+1),Ai(lenQ),Ax(lenQ),stat=allocerr) ! In column format
print *,'Set to column format'
ret=umfpack_di_triplet_to_col(ndofx,ndofx,lenQ,iQ,jQ,Qval,Ap,Ai,Ax,NULL)
if (ret /= UMFPACK_OK) call prterr("umfpack_di_triplet_to_col failed")
print *,'Symbolic calculation'
ret=umfpack_di_symbolic(ndofx, ndofx, Ap, Ai, Ax, Symbolic, NULL, NULL)
if (ret /= UMFPACK_OK) call prterr("umfpack_di_symbolic failed")
print *,'Numeric calculation'
ret=umfpack_di_numeric(Ap, Ai, Ax, Symbolic, Numeric, NULL, NULL)
if (ret /= UMFPACK_OK) call prterr("umfpack_di_numeric failed")
print *,'Symbolic=',Symbolic
print *,'Numeric=',Numeric
call umfpack_di_free_symbolic(Symbolic)
print *,'Solve'
ret=umfpack_di_solve(UMFPACK_A, Ap, Ai, Ax, vecx, rhsx, Numeric, NULL, NULL)
if (ret /= UMFPACK_OK) call prterr("umfpack_di_solve failed")
call umfpack_di_free_numeric(Numeric)
! deallocate (Ap,Ai,Ax)
do ip=1,lenQ ! back to base 1
iQ(ip)=iQ(ip)+1
jQ(ip)=jQ(ip)+1
enddo
iter=-1 ! irrelevant for Direct solvers
err=-1
ierrslap=-1
else
call prterr("solve: unknown key_sim option")
endif
! ----------------------------------------------------------------------
! Calculate BOTH "regular" and reduced/diag residual. In case of SLAP routine,
! iQ, jQ, Qval have been changed to SLAP column format
! ----------------------------------------------------------------------
if (key_sim==1.or.key_sim==0.or.key_sim==9) then
resid=0; residDiag=0
lhs=0; lhsDiag=0
rhs=0; rhsDiag=0
do i=1,ndofx
lhsI=0
rhsI=rhsx(i)
pivot=0
do ip=irowst(i),irowed(i)
jQ1=jQ(ip)
if (jQ1>ndofx) then
print *,'error',ip,iQ(ip),i,jQ1
stop
endif
if (jQ1==i) pivot=Qval(ip)
lhsI=lhsI+Qval(ip)*vecx(jQ1)
enddo
lhsIDiag=lhsI/pivot
rhsIDiag=rhsI/pivot
resid=resid+(lhsI-rhsI)**2
lhs=lhs+lhsI**2
rhs=rhs+rhsI**2
residDiag=residDiag+(lhsIDiag-rhsIDiag)**2
lhsDiag=lhsDiag+lhsIDiag**2
rhsDiag=rhsDiag+rhsIDiag**2
enddo
resid=sqrt(resid)/sqrt(lhs); residDiag=sqrt(residDiag)/sqrt(lhsDiag)
lhs=sqrt(lhs); lhsDiag=sqrt(lhsDiag)
rhs=sqrt(rhs); rhsDiag=sqrt(rhsDiag)
else
lhsvec=0
do j=1,ndofx ! columns
ist=jQ(j)
ied=jQ(j+1)-1
do ip=ist,ied
iQx=iQ(ip)
lhsvec(iQx)=lhsvec(iQx)+Qval(ip)*vecx(j)
enddo
enddo
resid=0
lhs=0
rhs=0
residDiag=0
lhsDiag=0
rhsDiag=0
do i=1,ndofx
lhsI=lhsvec(i)
rhsI=rhsx(i)
lhsIDiag=lhsvec(i)/Qval(jQ(i))
rhsIDiag=rhsx(i)/Qval(jQ(i))
resid=resid+(lhsI-rhsI)**2
lhs=lhs+lhsI**2
rhs=rhs+rhsI**2
residDiag=residDiag+(lhsIDiag-rhsIDiag)**2
lhsDiag=lhsDiag+lhsIDiag**2
rhsDiag=rhsDiag+rhsIDiag**2
enddo
resid=sqrt(resid)/sqrt(lhs)
lhs=sqrt(lhs)
rhs=sqrt(rhs)
residDiag=sqrt(residDiag)/sqrt(lhsDiag)
lhsDiag=sqrt(lhsDiag)
rhsDiag=sqrt(rhsDiag)
endif
print *,'Direct residual:'
write (*,'(3e13.5)') resid,lhs,rhs
print *,'Normalised residual:'
write (*,'(3e13.5)') residDiag,lhsDiag,rhsDiag
end subroutine solve
! ######################################################################
subroutine initsolvers(ndofx)
! ----------------------------------------------------------------------
! Set parameters for SLAP and allocate workspace arrays
! Note that lenQ is guaranteed not to vary simply as a result
! of non-linearity so we can allocate once only
! (alternative would be to make leniw etc "big enough")
! itol=2 means use |D^-1 (Qu-R)|_2/|D^-1 R|_2 for tol
! This is good for removeFixed=NO
! ----------------------------------------------------------------------
use common, only : lenQ,key_sim
use iofile
integer ndofx,allocerr
if (key_sim == 3) then ! dsdgmr
lenw=2+ndofx*(nsave+7)+nsave*(nsave+3)
leniw=31
itol=0
else if (key_sim == 4) then ! dslugm
lenw=1+ndofx*(nsave+7)+nsave*(nsave+3)+lenQ*2
leniw=lenQ*2+4*ndofx+32
itol=0
else if (key_sim == 5) then ! dsdbcg
lenw=8*ndofx+1
leniw=11
itol=2
else if (key_sim == 6) then ! dslubc
lenw=lenQ*2+8*ndofx
leniw=lenQ*2+4*ndofx+12
itol=2
else if (key_sim == 7) then ! dsdcg
lenw=5*ndofx+1
leniw=11
itol=2
else if (key_sim == 8) then ! dsiccg
lenw=lenQ+5*ndofx
leniw=lenQ+ndofx+11
itol=2
endif
if (key_sim>=3.and.key_sim<=8) then
allocate (rwork(lenw),iwork(leniw),stat=allocerr)
if (allocerr.ne.0) call prterr('Failed to allocate SLAP work variables')
endif
if (key_sim==9) then
! print *,'allocating Ap,Ai,Ax'
allocate (Ap(ndofx+1),Ai(lenQ),Ax(lenQ),stat=allocerr) ! In column format
if (allocerr /= 0) call prterr("callslap: Failed to allocate Ap, Ai, Ax")
endif
end subroutine initsolvers
! ######################################################################
subroutine freesolvers
if (allocated(rwork)) then
deallocate (rwork,iwork)
endif
if (allocated(Ax)) then
deallocate (Ax,Ap,Ai)
endif
end subroutine freesolvers
end module solvers