forked from gravitationalwave01/eDDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmrpim2.f90
471 lines (399 loc) · 12 KB
/
qmrpim2.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
! correspondence with DDSCAT
! xi - initial guess of cxpol on input; cxpol on output
! b - cxe (right hand side, not changed)
! xr - A xi (use matvec) work vector
! lda - nat3
! ndim - nat3 ?
! nlar work array dimension >= 12
! wrk - cxsc NOTE!!! THAT MXCXSC has to be set =10 in main DDSCAT
! maxit - itermx ? mxiter?
! nloop - itern
! tol - tolr (input)
! tole - achieved relative error
! ipar(12) - convergence; ipar(12)=0 converged
! Example call from getfml.f90 (DDSCAT)
! nlar=10
! call pimqmrcg(nat3,matvec,cxe,nat3,nlar,cxpol,cxscr1, cxsc, mxiter,&
! itern,tol,tolr,multiplications)
! IMPORTANT NOTES
!
! Change DDSCAT.f90
! MXCXSC=10*MXN3
!
! (PJF) NOTE: nlar needs to be handled more gracefully i.e. test should be done if NLAR is
! sufficiently large. Nlar has to be at least 10 because I use 1 for vector xs and 9
! vectors for conjugate gradient. This is how it was implemented by PCC and AR
!
! Bruce - we need to discuss the nlar issue for all CG routines.
! it needs to be set in DDSCAT and transfered to getfml so one can add some tests
! this is simple
!(PJF) in pimqmrcg I assumed that A matrix is symmetric. I.e. the code is not
! general for non-symmetric cases. I hope that A is indeed symmetric.
subroutine pimqmrcg(ndim,matvec, &
b,lda,nlar,xi,xr,wrk,maxit,itno,tol,tole,ncompte)
!
! Interface to QMR solver
! ndim - dimension
! matvec - external Ax routine
! b - right hand side
! lda - leading dimension
! nlar - number of needed scratch vectors (9+1)
! xi - on input initial guess, on output the result
! xr - scratch vector
! wrk - work array
! maxit - maximum number of iterations
! itno - number of iterations
! tol - needed relative error
! tole - achived relative error
! ncompte - cumber of Ax multiplications
!
! History
! (PJF) = Piotr J. Flatau
! (PCC) = P. C. Chaumet
! (AR) = A. Rahmani
! February 4, 2010 P. C. Chaumet and A. Rahmani
! May 6, 2010 (PJF) converted to Fortran90, introduce DDPRECISION to
! handle single/double precision easily,
! introduced pointer/target to split work array
! license: GNU GPL
USE DDPRECISION,ONLY: WP
IMPLICIT NONE
! .. Parameters ..
INTEGER :: LDA,NLAR
! PARAMETER NLAR has to be 9+1=10 to accomodate vector xs
! .. Local Scalars ..
CHARACTER :: CMSGNM*70
COMPLEX(WP) :: EPSILON,GAMMA,ICOMP,KAPPA,KSI,LAMBDA,MU,RHO,TAU,THETA
REAL(WP) :: NORM,TOL,TOLE
INTEGER :: I,ITLAST,ITNO,J,MAXIT,NCOMPTE,NDIM,NOU,NT,STATUS,STEPERR
INTEGER :: IPAR
! ..
! .. Local Arrays ..
COMPLEX(WP) :: &
B(LDA), &
DOTS(4), &
XI(LDA), &
XR(LDA)
COMPLEX(WP), TARGET:: WRK(LDA,NLAR)
COMPLEX(WP), POINTER:: XS(:), WRK2(:,:)
! ..
! .. External Subroutines ..
EXTERNAL pimzqmr, matvec
! ..
! .. Intrinsic Functions ..
INTRINSIC real
! ..
! FLATAU split wrk array to 2 arrays which are needed by pimzqmr
xs => wrk(1:lda,1)
wrk2 => wrk(1:lda,2:nlar)
!
nou = 0
ncompte = 0
ITLAST=1
10 CALL pimzqmr(xs,xi,xr,b,wrk2,norm,lda,ndim,nlar,lambda,kappa,theta,gamma, &
ksi,rho,epsilon,mu,tau,dots,nou,nt,itno,maxit,tole,tol,status,steperr)
IF(ITNO.GT.ITLAST)THEN
WRITE(CMSGNM,FMT='(A,I8,A,1P,E10.3)') &
'IT=',ITNO-2,' f.err=',TOLE
CALL WRIMSG('QMRCCG',CMSGNM)
ITLAST=ITNO
ENDIF
! print*, itno, tole
IF (status.LT.0) THEN
WRITE (*,fmt=*) 'stop nstat', status, steperr
STOP
END IF
ncompte = ncompte + 1
IF (nt.EQ.1) THEN
! original code
! DO i = 1, ndim
! xr(i) = 0._wp
! DO j = 1, ndim
! xr(i) = xr(i) + mat(i,j)*xi(j)
! END DO
! END DO
call matvec(xi,xr,ipar)
ELSE IF (nt.EQ.2) THEN
! Flatau. NOTE this only work for symmetric problems
! I am not 100% sure if DDSCAT is always symmetric
!original code (is this right, mat is not conjugated here?)
! DO i = 1, ndim
! xr(i) = 0._wp
! DO j = 1, ndim
! xr(i) = xr(i) + (mat(j,i))*xi(j)
! END DO
! END DO
call matvec(xi,xr,ipar)
END IF
IF (status.NE.1) GO TO 10
IF(STEPERR.EQ.0)THEN
! WRITE (*,fmt=*) 'ITNO has reached MAXIT', itno, maxit
WRITE(CMSGNM,FMT='(A,I6,A,I6)')'IT=',ITNO,' has reached MAXIT=',MAXIT
CALL ERRMSG('FATAL','pimqmrcg',CMSGNM)
ENDIF
! FLATAU after all is done assign xs (solution) to xi
xi(1:lda)=xs(1:lda)
return
END
!****************************************
SUBROUTINE pimzqmr(xs,xi,xr,b,wrk,norm,lda,ndim,nlar,lambda,kappa,theta, &
gamma,ksi,rho,epsilon,mu,tau,dots,nou,nt,itno,maxit,tole,tol,status, &
steperr)
USE DDPRECISION,ONLY: WP
!****************************************************************
! Iterative solver QMR
!****************************************************************
! Authors: P. C. Chaumet and A. Rahmani
! Date: 04/02/2010
! Purpose: iterative solver for linear system Ax=b. There is no
! condition on the matrix. Notice that the products A x and At x are
! provided by the user.
! Reference: if you use this routine in your research, please
! reference, as appropriate: P. C. Chaumet and A. Rahmani, Efficient
! discrete dipole approximation for magneto-dielectric scatterers
! Opt. Lett. 34, 917 (2009). R. D. Da Cunha and T. Hopkins,
! Appl. Numer. Math. 19, 33 (1995).
! History
! (PJF) = Piotr J. Flatau
! (PCC) = P. C. Chaumet
! (AR) = A. Rahmani
! Originally written by R. D. Da Cunha and T. Hopkins
! February 4, 2010 Modified by P. C. Chaumet and A. Rahmani
! May 6, 2010 (PJF) converted to Fortran90, introduce DDPRECISION to
! handle sing/double precision easily, introduced pointer/target
! to split work array
IMPLICIT NONE
! .. Array Arguments ..
COMPLEX (wp) :: b(lda), dots(4), wrk(lda,nlar), xi(lda), xr(lda), &
xs(lda)
! ..
! .. Local Scalars ..
COMPLEX (wp) :: absgamma2, abstau02, den, epsilon0, gamma0, kappa0, &
ksi0, lambda0, mu0, rho0, tau0, tmp1
INTEGER :: i
! ..
! .. Local Arrays ..
! ..
! .. Scalar Arguments ..
COMPLEX (wp) :: epsilon, gamma, kappa, ksi, lambda, mu, rho, tau, theta
REAL (wp) :: norm, tol, tole
INTEGER :: itno, lda, maxit, ndim, nlar, nou, nt, status, steperr
! ..
! ..
! .. Intrinsic Functions ..
INTRINSIC abs, conjg, sqrt
! ..
IF (nou.EQ.0) GO TO 10
IF (nou.EQ.1) GO TO 20
IF (nou.EQ.2) GO TO 30
IF (nou.EQ.3) GO TO 40
IF (nou.EQ.4) GO TO 50
IF (nou.EQ.5) GO TO 100
! 1. lambda=1, kappa=-1, theta=-1
10 lambda = (1._wp,0._wp)
kappa = -(1._wp,0._wp)
theta = -(1._wp,0._wp)
norm = 0._wp
DO i = 1, ndim
norm = norm + b(i)*conjg(b(i))
END DO
norm = sqrt(norm)
! Loop
status = 0
steperr = -1
itno = 0
! 2. wtilde=vtilde=r=b-Ax
! r=b-Ax
! A*x=wrk(3)
nou = 1
nt = 1
DO i = 1, ndim
xs(i) = xi(i)
END DO
! compute A*xi
RETURN
20 DO i = 1, ndim
wrk(i,3) = xr(i)
wrk(i,1) = b(i) - wrk(i,3)
wrk(i,7) = wrk(i,1)
wrk(i,8) = wrk(i,1)
END DO
! 3. p=q=d=s=0
DO i = 1, ndim
wrk(i,2) = 0._wp
wrk(i,4) = 0._wp
wrk(i,5) = 0._wp
wrk(i,6) = 0._wp
END DO
! 4. gamma=||vtilde||_{2}, ksi=||wtilde||_{2},
! rho=wtilde^{T}vtilde, epsilon=(A^{T}wtilde)^{T}vtilde, mu=0
dots(1) = 0._wp
dots(2) = 0._wp
dots(3) = 0._wp
DO i = 1, ndim
dots(1) = dots(1) + wrk(i,7)*conjg(wrk(i,7))
dots(2) = dots(2) + wrk(i,8)*conjg(wrk(i,8))
dots(3) = dots(3) + wrk(i,7)*wrk(i,8)
END DO
! Compute A^{T}wtilde
! CALL TMATVEC(WRK(IWTILDE),WRK(IATWTILDE),IPAR)
DO i = 1, ndim
xi(i) = wrk(i,8)
END DO
nt = 2
nou = 2
RETURN
30 dots(4) = 0._wp
DO i = 1, ndim
wrk(i,9) = xr(i)
dots(4) = dots(4) + wrk(i,7)*wrk(i,9)
END DO
! Accumulate simultaneously partial inner-products
! CALL PDZSUM(4,DOTS)
gamma = sqrt(dots(1))
ksi = sqrt(dots(2))
rho = dots(3)
epsilon = dots(4)
mu = 0._wp
! 5. tau=epsilon/rho
IF (rho.EQ.0._wp) THEN
itno = 0
status = -3
steperr = 5
GO TO 200
END IF
tau = epsilon/rho
100 itno = itno + 1
! 6. p=1/gamma*vtilde-mu*p
IF (gamma.EQ.0._wp) THEN
status = -3
steperr = 6
GO TO 200
END IF
DO i = 1, ndim
wrk(i,2) = wrk(i,7)/gamma - mu*wrk(i,2)
END DO
! 7. q=1/ksi*A^{T}wtilde-(gamma*mu)/ksi*q
IF (ksi.EQ.0._wp) THEN
status = -3
steperr = 7
GO TO 200
END IF
DO i = 1, ndim
wrk(i,4) = (wrk(i,9)-gamma*mu*wrk(i,4))/ksi
END DO
! 8. vtilde=Ap-tau/gamma*vtilde
DO i = 1, ndim
xi(i) = wrk(i,2)
END DO
nt = 1
nou = 3
RETURN
40 DO i = 1, ndim
wrk(i,3) = xr(i)
wrk(i,7) = wrk(i,3) - tau/gamma*wrk(i,7)
END DO
! 9. wtilde=q-tau/ksi*wtilde
DO i = 1, ndim
wrk(i,8) = wrk(i,4) - tau/ksi*wrk(i,8)
END DO
! 11. gamma=||vtilde||_{2}, ksi=||wtilde||_{2},
! rho=wtilde^{T}vtilde, epsilon=(A^{T}wtilde)^{T}vtilde
dots(1) = 0._wp
dots(2) = 0._wp
dots(3) = 0._wp
DO i = 1, ndim
dots(1) = dots(1) + wrk(i,7)*conjg(wrk(i,7))
dots(2) = dots(2) + wrk(i,8)*conjg(wrk(i,8))
dots(3) = dots(3) + wrk(i,7)*wrk(i,8)
END DO
! Compute A^{T}wtilde
DO i = 1, ndim
xi(i) = wrk(i,8)
END DO
nt = 2
nou = 4
RETURN
50 dots(4) = 0._wp
DO i = 1, ndim
wrk(i,9) = xr(i)
dots(4) = dots(4) + wrk(i,7)*wrk(i,9)
END DO
! Accumulate simultaneously partial inner-products
! CALL PDZSUM(4,DOTS)
gamma0 = gamma
gamma = sqrt(dots(1))
ksi0 = ksi
ksi = sqrt(dots(2))
rho0 = rho
rho = dots(3)
epsilon0 = epsilon
epsilon = dots(4)
! 12. mu=(gamma0*ksi0*rho)/(gamma*tau*rho0)
den = gamma*tau*rho0
IF (den.EQ.0._wp) THEN
status = -3
steperr = 12
GO TO 200
END IF
mu0 = mu
mu = (gamma0*ksi0*rho)/den
! 13. tau=epsilon/rho-gamma*mu
IF (rho.EQ.0._wp) THEN
status = -3
steperr = 13
GO TO 200
END IF
tau0 = tau
tau = epsilon/rho - gamma*mu
! 14. theta=(|tau0|^2*(1-lambda))/(lambda*|tau|^2+|gamma|^2)
abstau02 = abs(tau0)**2._wp
absgamma2 = abs(gamma)**2._wp
den = lambda*abstau02 + absgamma2
IF (den.EQ.0._wp) THEN
status = -3
steperr = 14
GO TO 200
END IF
theta = (abstau02*((1._wp,0._wp)-lambda))/den
! 15. kappa=(-gamma0*CONJG(tau0)*kappa0)/(gamma0*|tau|^2+|gamma|^2)
kappa0 = kappa
kappa = -(gamma0*conjg(tau0)*kappa0)/den
! 16. lambda=(lambda0*|tau0|^2)/(gamma0*|tau|^2+|gamma|^2)
lambda0 = lambda
lambda = lambda0*abstau02/den
! 17. d=theta*d+kappa*p
DO i = 1, ndim
wrk(i,5) = theta*wrk(i,5) + kappa*wrk(i,2)
END DO
! 18. s=theta*s+kappa*A*p
DO i = 1, ndim
wrk(i,6) = theta*wrk(i,6) + kappa*wrk(i,3)
END DO
! 19. x=x+d
DO i = 1, ndim
xs(i) = xs(i) + wrk(i,5)
END DO
! 20. r=r-s
DO i = 1, ndim
wrk(i,1) = wrk(i,1) - wrk(i,6)
END DO
! criterion to stop
tmp1 = 0._wp
DO i = 1, ndim
tmp1 = tmp1 + wrk(i,1)*conjg(wrk(i,1))
END DO
tole = sqrt(abs(tmp1))/norm
IF (tole.LE.tol) THEN
nou = 5
status = 1
RETURN
END IF
IF (itno.GT.maxit) THEN
status = 1
steperr = 0
RETURN
END IF
GO TO 100
200 RETURN
END SUBROUTINE pimzqmr