-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcgsarkar2.f90
executable file
·752 lines (706 loc) · 25.6 KB
/
cgsarkar2.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
!integration with DDSCAT
!changed subroutine names by adding 90 at the end
SUBROUTINE gacg90(cx,cy,n,matvec, cmatvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: GACG --- generalized augumented conjugate gradient method
! for unsymmetric case
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ay, bk, bw2, q2, sk, sk2
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cbq(:), cbw(:), cq(:), cw(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec, cmatvec
! .. Intrinsic Functions ..
INTRINSIC conjg, sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cbq(2*n), cbw(2*n), cq(2*n), cw(2*n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient gacg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' gacg '
END IF
iter = 0
ay=2._wp*dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cq)
call matvec(cx,cq,n)
! CALL prod(n,'c',cx,'u',cq(n+1))
call cmatvec(cx,cq(n+1),n)
!
cq(1:n) = cy(1:n) - cq(1:n)
cq(n+1:2*n) = conjg(cy(1:n)) - cq(n+1:2*n)
cw(1:n) = cq(1:n)
cw(n+1:2*n) = cq(n+1:2*n)
! CALL prod(n,'u',cq(n+1),'u',cbq)
call matvec(cq(n+1),cbq,n)
cbq(n+1:2*n)=cmplx(0.0_wp,0.0_wp,kind=wp)
! CALL prod(n,'c',cq,'u',cbq(n+1))
call matvec(cq,cbq(n+1),n)
!
sk = 2.0_wp*sum(cbq(1:n)*conjg(cq(1:n)))
cbw(1:2*n) = cbq(1:2*n)
bw2=dot_product(cbw(1:2*n),cbw(1:2*n))
30 CONTINUE
ak = sk/bw2
cx(1:n)=cx(1:n)+ak*cw(n+1:2*n)
cq(1:2*n)=cq(1:2*n)-ak*cbw(1:2*n)
q2=dot_product(cq(1:2*n),cq(1:2*n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(q2/ay)= ', iter, sqrt(q2/ay)
cg%itno=iter
IF ((sqrt(q2/ay)).LT.epsilon_err) GO TO 50
! CALL prod(n,'u',cq(n+1),'u',cbq)
call matvec(cq(n+1),cbq,n)
! CALL prod(n,'c',cq,'u',cbq(n+1))
call cmatvec(cq,cbq(n+1),n)
!
sk2 = 2.0_wp*sum(cbq(1:n)*conjg(cq(1:n)))
bk = sk2/sk
! Warning there are differences in results (very small) when the code is run in double precision
! two times. First time we use section 1, which is "fortran 77" construct with do loops. Second time I run
! section 2 which is "fortran 90" type construct. I do not know what is the problem. This problems is not
! occuring if the code is run in single precision. One would think that there should not be any difference.
! PJF
!section 1
! DO i = 1, 2*n
! cw(i) = cq(i) + bk*cw(i)
! cbw(i) = cbq(i) + bk*cbw(i)
! END DO
! section 2
cw(1:2*n) = cq(1:2*n) + bk*cw(1:2*n)
cbw(1:2*n) = cbq(1:2*n) + bk*cbw(1:2*n)
bw2=dot_product(cbw(1:2*n),cbw(1:2*n))
sk = sk2
iter = iter + 1
IF (iter.GT.maxit) GO TO 50
GO TO 30
50 CONTINUE
deallocate(cbq, cbw, cq, cw,STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient gacg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' gacg '
END IF
RETURN
END SUBROUTINE gacg90
SUBROUTINE gbicg90(cx,cy,n,matvec, cmatvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: GBICG --- generalized bi-conjugate gradient
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
COMPLEX (wp) :: cak, cbk, csk, csk2
REAL (wp) :: ay, ek
INTEGER :: i
! .. Local Arrays ..
COMPLEX (wp),allocatable :: cap(:), caw(:), cp(:), cq(:), cr(:), cw(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec, cmatvec
! .. Intrinsic Functions ..
INTRINSIC conjg, sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cap(n), caw(n), cp(n), cq(n), cr(n), cw(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient gbicg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' gbicg '
END IF
iter = 0._wp
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cr)
call matvec(cx,cr,n)
csk = (0._wp,0._wp)
cr(1:n) = cy(1:n) - cr(1:n)
cq(1:n) = conjg(cr(1:n))
cw(1:n) = cq(1:n)
cp(1:n) = cr(1:n)
csk= sum(cr(1:n)*cr(1:n))
20 CONTINUE
! CALL prod(n,'c',cw,'u',caw)
call cmatvec(cw,caw,n)
! CALL prod(n,'u',cp,'u',cap)
call matvec(cp,cap,n)
cak = sum(cap(1:n)*conjg(cw(1:n)))
cak = csk/cak
cx(1:n)=cx(1:n)+cak*cp(1:n)
cr(1:n)=cr(1:n)-cak*cap(1:n)
cq(1:n)=cq(1:n)-conjg(cak)*caw(1:n)
csk2 = sum(cr(1:n)*conjg(cq(1:n)))
ek=dot_product(cr(1:n),cr(1:n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(ek/ay)= ', iter, sqrt(ek/ay)
cg%itno=iter
IF ((sqrt(ek/ay)).LT.epsilon_err) GO TO 40
cbk = csk2/csk
cp(1:n) = cr(1:n) + cbk*cp(1:n)
cw(1:n) = cq(1:n) + conjg(cbk)*cw(1:n)
csk = csk2
iter = iter + 1
IF (iter.GT.maxit) GO TO 40
GO TO 20
40 CONTINUE
deallocate(cap, caw, cp, cq, cr, cw, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient gbicg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' gbicg '
END IF
RETURN
END SUBROUTINE gbicg90
SUBROUTINE gmcg90(cx,cy,n,matvec, cmatvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: GMCG --- modified conjugate method for unsymmetric complex
! matrices
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ay, bk, bk2, q2, w2
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cbw(:), cq(:), cw(:), cxh(:), cz(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec, cmatvec
! .. Intrinsic Functions ..
INTRINSIC conjg, sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cbw(2*n), cq(2*n), cw(2*n), cxh(n), cz(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient gmcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' gmcg '
END IF
iter = 0
cz(1:n) = cx(1:n)
cxh(1:n) = cx(1:n)
ay=2._wp*dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cq)
call matvec(cx,cq,n)
! CALL prod(n,'c',cz,'u',cq(n+1))
call cmatvec(cz,cq(n+1),n)
!
cq(1:n) = cy(1:n) - cq(1:n)
cq(n+1:2*n) = conjg(cy(1:n)) - cq(n+1:2*n)
cw(1:n) = cq(1:n)
cw(n+1:2*n) = cq(n+1:2*n)
w2=dot_product(cw(1:2*n),cw(1:2*n))
30 CONTINUE
! CALL prod(n,'u',cw(n+1),'u',cbw)
call matvec(cw(n+1),cbw,n)
! CALL prod(n,'c',cw,'u',cbw(n+1))
call cmatvec(cw,cbw(n+1),n)
!
ak = 2.0_wp*sum(cbw(1:n)*conjg(cw(1:n)))
IF (ak.EQ.0._wp) THEN
PRINT *, ' gmcg. ak = 0 '
STOP
END IF
ak = w2/ak
cx(1:n)=cx(1:n)+ak*cw(n+1:2*n)
cz(1:n)=cz(1:n)+ak*cw(1:n)
cq(1:2*n)=cq(1:2*n)-ak*cbw(1:2*n)
q2=dot_product(cq(1:2*n),cq(1:2*n))
bk = q2/w2
bk2 = 1._wp + bk
cxh(1:n) = (cx(1:n)+bk*cxh(1:n))/bk2
cw(1:2*n) = (cq(1:2*n)+bk*cw(1:2*n))/bk2
w2=dot_product(cw(1:2*n),cw(1:2*n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(w2/ay)= ', iter, sqrt(w2/ay)
cg%itno=iter
IF ((sqrt(w2/ay)).LT.epsilon_err) GO TO 60
iter = iter + 1
IF (iter.GT.maxit) GO TO 60
GO TO 30
60 CONTINUE
cx(1:n) = cxh(1:n)
deallocate(cbw, cq, cw, cxh, cz, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient gmcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' gmcg '
END IF
RETURN
END SUBROUTINE gmcg90
SUBROUTINE rcg90(cx,cy,n,matvec, cmatvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: RCG --- residual minimized; the residuals are minimized
! at each iteration, no scaling is introduced.
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! Warning there are differences in results (very small) when the code is run in double precision
! two times. First time we use section 1, which is "fortran 77" construct with do loops. Second time I run
! section 2 which is "fortran 90" type construct. I do not know what is the problem. This problems is not
! occuring if the code is run in single precision. One would think that there should not be any difference.
! PJF
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ay, ek, qk, sk, sk2
! .. Local Arrays ..
COMPLEX (wp),allocatable :: cp(:), cprod(:), cr(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec, cmatvec
! .. Intrinsic Functions ..
INTRINSIC sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cp(n), cprod(n), cr(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient rcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' rcg '
END IF
iter = 0
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cprod)
call matvec(cx,cprod,n)
cr(1:n) = cy(1:n) - cprod(1:n)
! CALL prod(n,'c',cr,'u',cp)
call cmatvec(cr,cp,n)
sk=dot_product(cp(1:n),cp(1:n))
20 CONTINUE
! CALL prod(n,'u',cp,'u',cprod)
call matvec(cp,cprod,n)
ak=dot_product(cprod(1:n),cprod(1:n))
ak = sk/ak
cx(1:n)=cx(1:n)+ak*cp(1:n)
cr(1:n)=cr(1:n)-ak*cprod(1:n)
ek=dot_product(cr(1:n),cr(1:n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(ek/ay)= ', iter, sqrt(ek/ay)
cg%itno=iter
IF ((sqrt(ek/ay)).LT.epsilon_err) GO TO 40
! CALL prod(n,'c',cr,'u',cprod)
call cmatvec(cr,cprod,n)
!
sk2=dot_product(cprod(1:n),cprod(1:n))
qk = sk2/sk
cp(1:n) = cprod(1:n) + qk*cp(1:n)
iter = iter + 1
sk = sk2
IF (iter.GT.maxit) GO TO 40
GO TO 20
40 CONTINUE
deallocate(cp, cprod, cr, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient rcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' rcg '
END IF
RETURN
END SUBROUTINE rcg90
SUBROUTINE sacg90(cx,cy,n,matvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: SACG --- augumented conjugate gradient method for symmetric
! case.
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! Warning there are differences in results (very small) when the code is run in double precision
! two times. First time we use section 1, which is "fortran 77" construct with do loops. Second time I run
! section 2 which is "fortran 90" type construct. I do not know what is the problem. This problems is not
! occuring if the code is run in single precision. One would think that there should not be any difference.
! PJF
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ap2, ay, bk, r2, sk, sk2
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cap(:), car(:), cp(:), cr(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec
! .. Intrinsic Functions ..
INTRINSIC sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cap(n), car(n), cp(n), cr(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient sacg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' sacg '
END IF
iter = 0
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cr)
call matvec(cx,cr,n)
cr(1:n) = cy(1:n) - cr(1:n)
cp(1:n) = cr(1:n)
! CALL prod(n,'u',cr,'c',car)
call matvec(conjg(cr),car,n)
!
sk = sum(car(1:n)*conjg(cr(1:n)))
cap(1:n) = car(1:n)
ap2=dot_product(cap(1:n),cap(1:n))
30 CONTINUE
ak = sk/ap2
cx(1:n)=cx(1:n)+ak*conjg(cp(1:n))
cr(1:n)=cr(1:n)-ak*cap(1:n)
r2=dot_product(cr(1:n),cr(1:n))
car(1:n)=cmplx(0.0_wp,0.0_wp,kind=wp)
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(r2/ay)= ', iter, sqrt(r2/ay)
cg%itno=iter
IF ((sqrt(r2/ay)).LT.epsilon_err) GO TO 50
! CALL prod(n,'u',cr,'c',car)
call matvec(conjg(cr),car,n)
!
sk2 = sum(car(1:n)*conjg(cr(1:n)))
bk = sk2/sk
cp(1:n) = cr(1:n) + bk*cp(1:n)
cap(1:n) = car(1:n) + bk*cap(1:n)
ap2=dot_product(cap(1:n),cap(1:n))
sk = sk2
iter = iter + 1
IF (iter.GT.maxit) GO TO 50
GO TO 30
50 CONTINUE
deallocate(cap, car, cp, cr, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient sacg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' sacg '
END IF
RETURN
END SUBROUTINE sacg90
SUBROUTINE sbicg90(cx,cy,n,matvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: SBICG --- simplified biconjugate gradient method specialized
! to symmetric matrices
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) :: epsilon_err, time
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
COMPLEX (wp) :: cak, cbk, csk, csk2
REAL (wp) :: ay, ek
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cap(:), cp(:), cr(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec
! .. Intrinsic Functions ..
INTRINSIC sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cap(n), cp(n), cr(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient sbicg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' sbicg '
END IF
iter = 0
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cr)
call matvec(cx,cr,n)
cr(1:n) = cy(1:n) - cr(1:n)
cp(1:n) = cr(1:n)
csk = sum(cr(1:n)*cr(1:n))
20 CONTINUE
! CALL prod(n,'u',cp,'u',cap)
call matvec(cp,cap,n)
cak = sum(cap(1:n)*cp(1:n))
cak = csk/cak
cx(1:n)=cx(1:n)+cak*cp(1:n)
cr(1:n)=cr(1:n)-cak*cap(1:n)
csk2 = sum(cr(1:n)*cr(1:n))
ek=dot_product(cr(1:n),cr(1:n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(ek/ay)= ', iter, sqrt(ek/ay)
cg%itno=iter
IF ((sqrt(ek/ay)).LT.epsilon_err) GO TO 40
cbk = csk2/csk
cp(1:n) = cr(1:n) + cbk*cp(1:n)
csk = csk2
iter = iter + 1
IF (iter.GT.maxit) GO TO 40
GO TO 20
40 CONTINUE
deallocate(cap, cp, cr, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient sbicg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' sbicg '
END IF
RETURN
END SUBROUTINE sbicg90
SUBROUTINE smcg90(cx,cy,n,matvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: SMCG --- modified conjugate gradient method for symmetric
! case.
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ay, bk, bk2, p2, r2
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cap(:), cp(:), cr(:), cxh(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec
! .. Intrinsic Functions ..
INTRINSIC sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cap(n), cp(n), cr(n), cxh(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient smcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' smcg '
END IF
iter = 0
cxh(1:n) = cx(1:n)
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cr)
call matvec(cx,cr,n)
cr(1:n) = cy(1:n) - cr(1:n)
cp(1:n) = cr(1:n)
p2=dot_product(cp(1:n),cp(1:n))
30 CONTINUE
! CALL prod(n,'u',cp,'c',cap)
call matvec(conjg(cp),cap,n)
!
ak = sum(cap(1:n)*conjg(cp(1:n)))
ak = p2/ak
cx(1:n)=cx(1:n)+ak*conjg(cp(1:n))
cr(1:n)=cr(1:n)-ak*cap(1:n)
r2=dot_product(cr(1:n),cr(1:n))
bk = r2/p2
bk2 = 1._wp + bk
cxh(1:n) = (cx(1:n)+bk*cxh(1:n))/bk2
cp(1:n) = (cr(1:n)+bk*cp(1:n))/bk2
p2=dot_product(cp(1:n),cp(1:n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(p2/ay)= ', iter, sqrt(p2/ay)
cg%itno=iter
IF ((sqrt(p2/ay)).LT.epsilon_err) GO TO 60
iter = iter + 1
IF (iter.GT.maxit) GO TO 60
GO TO 30
60 CONTINUE
cx(1:n) = cxh(1:n)
deallocate(cap, cp, cr, cxh, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient smcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' smcg '
END IF
RETURN
END SUBROUTINE smcg90
SUBROUTINE srcg90(cx,cy,n,matvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: SRCG --- search directions scaled at each iteration
! and the residuals are minimized
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ay, ek, sk
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cp(:), cprod(:), cr(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec, cmatvec
! .. Intrinsic Functions ..
INTRINSIC sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cp(n), cprod(n), cr(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient srcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' srcg '
END IF
iter = 0
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cprod)
call matvec(cx,cprod,n)
cr(1:n) = cy(1:n) - cprod(1:n)
! CALL prod(n,'c',cr,'u',cprod)
call cmatvec(cr,cprod,n)
!
sk=dot_product(cprod(1:n),cprod(1:n))
cp(1:n) = cprod(1:n)/sk
30 CONTINUE
! CALL prod(n,'u',cp,'u',cprod)
call matvec(cp,cprod,n)
ak=dot_product(cprod(1:n),cprod(1:n))
ak = 1._wp/ak
cx(1:n)=cx(1:n)+ak*cp(1:n)
cr(1:n)=cr(1:n)-ak*cprod(1:n)
ek=dot_product(cr(1:n),cr(1:n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(ek/ay)= ', iter, sqrt(ek/ay)
cg%itno=iter
IF ((sqrt(ek/ay)).LT.epsilon_err) GO TO 40
! CALL prod(n,'c',cr,'u',cprod)
call cmatvec(cr,cprod,n)
!
sk=dot_product(cprod(1:n),cprod(1:n))
cp(1:n)=cp(1:n)+(1._wp/sk)*cprod(1:n)
iter = iter + 1
IF (iter.GT.maxit) GO TO 40
GO TO 30
40 CONTINUE
deallocate(cp, cprod, cr, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient srcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' srcg '
END IF
RETURN
END SUBROUTINE srcg90
SUBROUTINE xcg90(cx,cy,n,matvec, cmatvec, cg)
USE ddprecision, ONLY : wp
use cgmodule
implicit none
type (cgstruct) cg
! Purpose: XCG --- the error between the true solution and the
! approximate solution is minimized at each iteration
! History: 1988 (TKS); original version
! 92/06/09 (PJF); re-written using "BLAS"-type style
! 92/09/11 (TLS); Removed unnecessary zeroing.
! Warning there are differences in results (very small) when the code is run in double precision
! two times. First time we use section 1, which is "fortran 77" construct with do loops. Second time I run
! section 2 which is "fortran 90" type construct. I do not know what is the problem. This problems is not
! occuring if the code is run in single precision. One would think that there should not be any difference.
! PJF
! .. Parameters ..
! .. Scalar Arguments ..
REAL (wp) ::epsilon_err
INTEGER :: iter, maxit, n, alloc_error, dealloc_error,ioerr
! .. Array Arguments ..
COMPLEX (wp) :: cx(n), cy(n)
! .. Local Scalars ..
REAL (wp) :: ak, ay, qk, sk, sk2
! .. Local Arrays ..
COMPLEX (wp), allocatable :: cp(:), cprod(:), cr(:)
! .. External Functions ..
! .. External Subroutines ..
EXTERNAL matvec, cmatvec
! .. Intrinsic Functions ..
INTRINSIC sqrt
maxit=cg%maxit
epsilon_err=cg%epsilon_err
ioerr=cg%ioerr
allocate(cp(n), cprod(n), cr(n),STAT=alloc_error)
IF (alloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Allocation Error Detected in conjugate gradient xcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' xcg '
END IF
iter = 0
ay=dot_product(cy(1:n),cy(1:n))
! CALL prod(n,'u',cx,'u',cprod)
call matvec(cx,cprod,n)
cr(1:n) = cy(1:n) - cprod(1:n)
sk=dot_product(cr(1:n),cr(1:n))
! CALL prod(n,'c',cr,'u',cp)
call cmatvec(cr,cp,n)
!
20 CONTINUE
! CALL prod(n,'u',cp,'u',cprod)
call matvec(cp,cprod,n)
ak=dot_product(cp(1:n),cp(1:n))
ak = sk/ak
cx(1:n)=cx(1:n)+ak*cp(1:n)
cr(1:n)=cr(1:n)-ak*cprod(1:n)
sk2=dot_product(cr(1:n),cr(1:n))
if(cg%print.eq.'print') WRITE (*,fmt=*) 'sqrt(sk2/ay)= ', iter, sqrt(sk2/ay)
cg%itno=iter
IF ((sqrt(sk2/ay)).LT.epsilon_err) GO TO 40
! CALL prod(n,'c',cr,'u',cprod)
call cmatvec(cr,cprod,n)
qk = sk2/sk
cp(1:n) = cprod(1:n) + qk*cp(1:n)
iter = iter + 1
sk = sk2
IF (iter.GT.maxit) GO TO 40
GO TO 20
40 CONTINUE
deallocate(cp, cprod, cr, STAT=dealloc_error)
IF (dealloc_error/=0) THEN
WRITE (ioerr,fmt='(a)') 'Deallcation Error Detected in conjugate gradient xcg'
WRITE (ioerr,fmt='(''Aborting'')')
stop ' xcg '
END IF
RETURN
END SUBROUTINE xcg90