-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdavidson.f
executable file
·1138 lines (1134 loc) · 44.8 KB
/
davidson.f
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SUBROUTINE DVDSON(N,LIM,DIAG,
: ILOW,IHIGH,ISELEC,NIV,MBLOCK,
: CRITE,CRITC,CRITR,ORTHO,MAXITER,
: WORK,IWRSZ,
: HIEND,NLOOPS,NMV,IERROR)
*=======================================================================
*
* Author: Andreas Stathopoulos, Charlotte F. Fischer
*
* Computer Science Department
* Vanderbilt University
* Nashville, TN 37212
* [email protected] DECEMBER 1993
*
* Copyright (c) by Andreas Stathopoulos and Charlotte F. Fischer
*
* Ammendments (in lower case) by Jonathan Tennyson April 1996
* In particular to use NAG whole matrix diagonaliser F02ABF
* in place of DSPEVX.
*
* DVDSON is a Fortran77 program that finds a few selected
* eigenvalues and their eigenvectors at either end of spectrum of
* a large, symmetric (and usually sparse) matrix, denoted as A.
* The matrix A is only referenced indirectly through the user
* supplied routine OP which implements a block matrix-vector
* operation(see below). Either the range of the eigenvalues wanted
* or an array of the indices of selected ones can be specified.
* DVDSON is a front-end routine for setting up arrays, and initial
* guess (calling SETUP). It also performs detailed error checking.
* DVDRVR is the driver routine that implements a version of the
* Davidson algorithm. The characteristics of this version are:
* o All arrays used by the program are stored in MEMORY.
* o BLOCK method (many vectors may be targeted per iteration.)
* o Eigenvectors are targeted in an optimum way without
* the need to compute all unconverged residuals,
* o It REORTHOGONILIZES the basis in case of orthogonality loss.
* o Finds HIGHEST eigenpairs by using the negative of the A.
* o Finds SELECTED eigenpairs specified by the user.
* o It accepts INITIAL eigenvector ESTIMATES or it can
* CREATE INITIAL ESTIMATES from the diagonal elements.
* o It uses a USER SUPPLIED block matrix-vector operation, OP.
* Depending on the implementation, OP can operate in either
* memory or on disc, and for either sparse or dense matrix.
* o The user can provide STOPPING CRITERIA for eigenvalues,
* and residuals. The user can also CONTROL reorthogonalization
* and block size.
* o On exit INFORMATION is given about the convergence status
* of eigenpairs and the number of loops and OP operations.
*
* The program consists of the following routines:
* DVDSON, SETUP, DVDRVR, ADDABS, TSTSEL,
* MULTBC, OVFLOW, NEWVEC, ORTHNRM.
C
* It also calls some basic BLAS routines:
* DCOPY, DSCAL, DDOT, DAXPY, IDAMAX, DGEMV, DINIT
C
* For solving the small eigenproblem, the routine DSPEVX from
* LAPACK is used. DSPEVX is obtainable from NETLIB, together
* with a series of subroutines that it calls.
*
* All the routines have IMPLICIT DOUBLE PRECISION(A-H,O-Z)
*
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION DIAG(N),WORK(IWRSZ)
DIMENSION ISELEC(LIM)
LOGICAL HIEND
* external op
include "mpif.h"
include "mkl_blas.fi"
*-----------------------------------------------------------------------
* (Important to the following is the concept of NUME, the distance of
* the index of the eigenpair wanted which is farthest from the
* extremes,i.e.,
* if lowest eigepairs i1<i2<...<ik are wanted, NUME=ik
* if highest eigenpairs i1<i2<...<ik are wanted, NUME=N-i1+1
* where i1,...,ik are the indices of the wanted eigenpairs.
* Obviously, NUME.GE.(No. of EiGenpairs wanted). )
C
* on entry
* -------
* OP User supplied routine with calling sequence OP(N,M,B,C).
* B and C are N x M matrices and C stores the result AxB.
* It should be declared external in the main program.
* N Order of the matrix.
* LIM The upper limit on the dimension of the expanding basis.
* NUME.LT.LIM.LE.N must hold. The case LIM=NUME is allowed
* only for LIM=NUME=N. The choice of LIM depends on the
* available workspace (see below). If the space is
* available it is preferable to have a large LIM, but not
* larger than NUME$+$40.
* DIAG Array of size N with the diagonal elements of the
* matrix A.
* ILOW The index of the lowest eigepair to be computed. If
* (ILOW.LE.0).or.(ILOW.GT.N), the selected eigenpairs
* to be computed should be contained in array ISELEC.
* (Modified on exit).
* IHIGH The index of the highest eigenpair to be computed.
* Considered ONLY when ILOW is in the range
* (0.LT.ILOW.LE.N). (Modified on exit).
* ISELEC Array of size LIM holding the user specified indices
* for the eigenpairs to be computed. Considered only when
* (ILOW.LE.0).or.(ILOW.GT.N). The indices are read from
* the first position until a non positive integer is met.
* Example: if N=500, ILOW=0, and ISELEC(1)=495,
* ISELEC(2)=497, ISELEC(3)=-1, the program will find
* 2 of the highest eigenpairs, pairs 495 and 497.
* Any order of indices is acceptable (Modified on exit).
* NIV Number of Initial Vector estimates provided by the user.
* If NIV is in the range: (NUME).LE.(NIV).LE.(LIM),
* the first NIV columns of size N of WORK should contain
* the estimates (see below). In all other cases of NIV,
* the program generates initial estimates.
* MBLOCK Number of vectors to be targeted in each iteration.
* 1.LE.MBLOCK.LE.(No. EiGenpairs wanted) should hold.
* Large block size reduces the number of iterations
* (matrix acceses) but increases the matrix-vector
* multiplies. It should be used when the matrix accese
* is expensive (disc, recomputed or distributed).
* CRITE Convergence threshold for eigenvalues.
* If ABS(EIGVAL-VALOLD) is less than CRITE for all wanted
* eigenvalues, convergence is signaled.
* CRITC Convergence threshold for the coefficients of the last
* added basis vector(s). If all of those corresponding to
* unconverged eigenpairs are less than CRITC convergence
* is signaled.
* CRITR Convergence threshold for residual vector norms. If
* all the residual norms ||Ax_i-l_ix_i|| of the targeted
* x_i are less than CRITR convergence is signaled.
* If ANY of the criteria are satisfied the algorithm stops
* ORTHO The threshold over which loss of orthogonality is
* assumed. Usually ORTHO.LE.CRITR*10 but the process can
* be skipped by setting ORTHO to a large number(eg,1.D+3).
* MAXITER Upper bound on the number of iterations of the
* algorithm. When MAXITER is exceeded the algorithm stops.
* A typical MAXITER can be MAX(200,NUME*40), but it can
* be increased as needed.
* WORK Real array of size IWRSZ. Used for both input and output
* If NIV is in ((NUME).LE.(NIV).LE.(LIM)), on input, WORK
* must have the NIV initial estimates. These NIV N-element
* vectors start from WORK(1) and continue one after the
* other. They must form an orthonormal basis.
* IWRSZ The size of the real workspace. It must be at least as
* large as:
*
* 2*N*LIM + LIM*LIM + (NUME+10)*LIM + NUME
*
* array work space to be passed to OP
* ndim dimension information to be passed to OP
*
* on exit
* -------
* WORK(1) The first NUME*N locations contain the approximations to
* the NUME extreme eigenvectors. If the lowest eigenpairs
* are required, (HIEND=false), eigenvectors appear in
* ascending order, otherwise (HIEND=true), they appear in
* descending order. If only some are requested, the order
* is the above one for all the NUME extreme eigenvectors,
* but convergence has been reached only for the selected
* ones. The rest are the current approximations to the
* non-selected eigenvectors.
* WORK(NUME*N+1)
* The next NUME locations contain the approximations to
* the NUME extreme eigenvalues, corresponding to the above
* NUME eigenvectors. The same ordering and convergence
* status applies here as well.
* WORK(NUME*N+NUME+1)
* The next NUME locations contain the corresponding values
* of ABS(EIGVAL-VALOLD) of the NUME above eigenvalues, of
* the last step of the algorithm.
* WORK(NUME*N+NUME+NUME+1)
* The next NUME locations contain the corresponding
* residual norms of the NUME above eigenvectors, of the
* last step.
* HIEND Logical. If .true. on exit the highest eigenpairs are
* found in descending order. Otherwise, the lowest
* eigenpairs are arranged in ascending order.
* NLOOPS The number of iterations it took to reach convergence.
* This is also the number of matrix references.
* NMV The number of Matrix-vector(M-V) multiplies. Each matrix
* reference can have up to size(block) M-V multiplies.
* IERROR An integer denoting the completions status:
* IERROR = 0 denotes normal completion.
* IERROR = -k denotes error in DSPEVX (k eigenpairs
* not converged)
* 0<IERROR<=2048 denotes some inconsistency as follows:
* If (INT( MOD(IERROR, 2)/1 ) N < LIM
* If (INT( MOD(IERROR, 4)/2 ) LIM < 1
* If (INT( MOD(IERROR, 8)/4 ) ISELEC(1)<1, and no range specified
* If (INT( MOD(IERROR, 16)/8 ) IHIGH > N (in range or ISELEC)
* If (INT( MOD(IERROR, 32)/16 ) IHIGH < ILOW (Invalid range)
* If (INT( MOD(IERROR, 64)/32 ) NEIG >= LIM (Too many wanted)
* If (INT( MOD(IERROR,128)/64 ) Probable duplication in ISELEC
* If (INT( MOD(IERROR,256)/128) NUME >= LIM (max eigen very far)
* If (INT( MOD(IERROR,512)/256) MBLOCK is out of bounds
* If (INT( MOD(IERROR,2048)/1024) Orthogonalization Failed
* If (INT( MOD(IERROR,4096)/2048) NLOOPS > MAXITER
*
* The program will also print an informative message to
* the standard output when NIV is not proper but it will
* continue by picking initial estimates internally.
*-----------------------------------------------------------------------
*
* Checking user input errors, and setting up the problem to solve.
*
IERROR=0
IF (LIM.GT.N) IERROR=IERROR+1
IF (LIM.LE.0) IERROR=IERROR+2
C
HIEND=.false.
C
IF ((ILOW.LE.0).OR.(ILOW.GT.N)) THEN
* ..Look for user choice of eigenpairs in ISELEC
IF (ISELEC(1).LE.0) THEN
* ..Nothing is given in ISELEC
IERROR=IERROR+4
ELSE
* ..Find number of eigenpairs wanted, and their
* ..min/max indices
NEIG=1
ILOW=ISELEC(1)
IHIGH=ISELEC(1)
DO 10 I=2,LIM
IF (ISELEC(I).LE.0) GOTO 20
ILOW=MIN(ILOW,ISELEC(I))
IHIGH=MAX(IHIGH,ISELEC(I))
NEIG=NEIG+1
10 CONTINUE
* ..Check if a very large index is asked for
20 IF (IHIGH.GT.N) IERROR=IERROR+8
ENDIF
ELSE
* ..Look for a range between ILOW and IHIGH
* ..Invalid range. IHIGH>N
IF (IHIGH.GT.N) IERROR=IERROR+8
NEIG=IHIGH-ILOW+1
* ..Invalid range. IHIGH<ILOW
IF (NEIG.LE.0) IERROR=IERROR+16
IF (NEIG.GT.LIM) THEN
* ..Not enough Basis space. Increase LIM or decrease NEIG
IERROR=IERROR+32
ELSE
* ..Fill in the ISELEC with the required indices
DO 40 I=1,NEIG
40 ISELEC(I)=ILOW+I-1
ENDIF
ENDIF
C
IF (IERROR.NE.0) RETURN
C
NUME=IHIGH
* ..Identify if few of the highest eigenpairs are wanted.
IF ((ILOW+IHIGH-1).GT.N) THEN
HIEND=.true.
NUME=N-ILOW+1
* ..Change the problem to a minimum eipenpairs one
* ..by picking the corresponding eigenpairs on the
* ..opposite side of the spectrum.
DO 50 I=1,NEIG
50 ISELEC(I)=N-ISELEC(I)+1
ENDIF
* ..duplications in ISELEC
IF (NEIG.GT.NUME) IERROR=IERROR+64
* ..Not enough Basis space. Increase LIM or decrease NUME
IF ((NUME.GT.LIM).OR.((NUME.EQ.LIM).AND.(NUME.NE.N)))
: IERROR=IERROR+128
* ..Size of Block out of bounds
IF ( (MBLOCK.LT.1).OR.(MBLOCK.GT.NEIG) ) IERROR=IERROR+256
C
* ..Check for enough workspace for Dvdson
IF ((IWRSZ.LT.(LIM*(2*N+LIM+9)+LIM*(LIM+1)/2)+nume))
* IERROR=IERROR+512
C
IF (IERROR.NE.0) RETURN
C
IF (NIV.GT.LIM) THEN
* ..Check number of initial estimates NIV is lower than LIM.
PRINT*,'WARNING: Too many initial estimates.?'
PRINT*,'The routine will pick the appropriate number'
ELSEIF ((NIV.LT.NUME).AND.(NIV.GT.0)) THEN
* ..check if enough initial estimates.
* ..(NIV<1 => program chooses)
PRINT*,'WARNING: Not enough initial estimates'
PRINT*,'The routine will pick the appropriate number'
ENDIF
*
* Assigning space for the real work arrays
*
iBasis =1
ieigval =iBasis +N*LIM
iAB =ieigval +LIM
iS =iAB +N*LIM
iSvec =iS +LIM*(LIM+1)/2
iscra1 =iSvec +LIM*LIM
ioldval =iscra1 +8*LIM
C
IF (HIEND) CALL DSCAL(N,-1.D0,DIAG,1)
C
iSTART=NIV
CALL SETUP(N,LIM,NUME,HIEND,DIAG,
: WORK(iBasis),WORK(iAB),WORK(iS),iSTART)
NLOOPS=1
NMV=ISTART
C
CALL DVDRVR(N,HIEND,LIM,MBLOCK,DIAG,
: NUME,iSTART,NEIG,ISELEC,
: CRITE,CRITC,CRITR,ORTHO,MAXITER,
: WORK(ieigval),WORK(iBasis),WORK(iAB),
: WORK(iS),WORK(iSvec),work(iscra1),
: WORK(ioldval),
: NLOOPS,NMV,IERROR)
IF (HIEND) THEN
CALL DSCAL(N,-1.D0,DIAG,1)
CALL DSCAL(NUME,-1.D0,WORK(ieigval),1)
endif
*
* -Copy the eigenvalues after the eigenvectors
* -Next, copy the difference of eigenvalues between the last two steps
* -Next, copy the residuals for the first NUME estimates
*
CALL DCOPY(NUME,WORK(ieigval),1,WORK(iBasis+N*NUME),1)
CALL DCOPY(NUME,WORK(ioldval),1,WORK(iBasis+(N+1)*NUME),1)
CALL DCOPY(NUME,WORK(iscra1),1,WORK(iBasis+(N+2)*NUME),1)
C
100 RETURN
END
*=======================================================================
SUBROUTINE SETUP(N,LIM,NUME,HIEND,DIAG,
: BASIS,AB,S,NIV)
*=======================================================================
* Subroutine for setting up (i) the initial BASIS if not provided,
* (ii) the product of the matrix A with the Basis into matrix AB,
* and (iii) the small matrix S=B^TAB. If no initial estimates are
* available, the BASIS =(e_i1,e_i2,...,e_iNUME), where i1,i2,...,
* iNUME are the indices of the NUME lowest diagonal elements, and
* e_i the i-th unit vector. (ii) and (iii) are handled by ADDABS.
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION DIAG(N),BASIS(N*LIM),AB(N*LIM)
DIMENSION S(LIM*(LIM+1)/2),MINELEM(LIM)
logical hiend
* external op
include "mpif.h"
include "mkl_blas.fi"
*-----------------------------------------------------------------------
* on entry
* --------
* OP The block matrix-vector operation, passed to ADDABS
* N the order of the matrix A
* LIM The limit on the size of the expanding Basis
* NUME Largest index of the wanted eigenvalues.
* HIEND Logical. True only if the highest eigenpairs are needed.
* DIAG Array of size N with the diagonal elements of A
* MINELEM Array keeping the indices of the NUME lowest diagonals.
*
* on exit
* -------
* BASIS The starting basis.
* AB, S The starting D=AB, and small matrix S=B^TAB
* NIV The starting dimension of BASIS.
*-----------------------------------------------------------------------
C
IF ((NIV.GT.LIM).OR.(NIV.LT.NUME)) THEN
*
* ..Initial estimates are not available. Give as estimates unit
* ..vectors corresponding to the NUME minimum diagonal elements
* ..First find the indices of these NUME elements (in MINELEM).
* ..Array AB is used temporarily as a scratch array.
*
CALL DINIT(N,-1.D0,AB,1)
DO 10 I=1,NUME
* ..imin= the first not gotten elem( NUME<=N )
DO 20 J=1,N
20 IF (AB(J).LT.0) GOTO 30
30 IMIN=J
DO 40 J=IMIN+1,N
40 IF ((AB(J).LT.0).AND.
: (DIAG(J).LT.DIAG(IMIN))) IMIN=J
MINELEM(I)=IMIN
AB(IMIN)=1.D0
10 CONTINUE
*
* ..Build the Basis. B_i=e_(MINELEM(i))
*
CALL DINIT(N*LIM,0.D0,BASIS,1)
DO 50 J=1,NUME
I=(J-1)*N+MINELEM(J)
BASIS(I)=1
50 CONTINUE
C
NIV=NUME
ENDIF
*
* Find the matrix AB by matrix-vector multiplies, as well as the
* small matrix S = B^TAB.
*
KPASS=0
CALL ADDABS(N,LIM,HIEND,KPASS,NIV,BASIS,AB,S)
C
RETURN
END
*=======================================================================
SUBROUTINE DVDRVR(N,HIEND,LIM,MBLOCK,DIAG,
: NUME,NIV,NEIG,ISELEC,
: CRITE,CRITC,CRITR,ORTHO,MAXITER,
: EIGVAL,BASIS,AB,S,SVEC,scra1,
: OLDVAL,
: NLOOPS,NMV,IERROR)
*=======================================================================
* called by DVDSON
*
* Driver routine implementing Davidson's main loop. On entry it
* is given the Basis, the work matrix D=AB and the small symmetric
* matrix to be solved, S=B^TAB (as found by SETUP). In each step
* the small problem is solved by calling DSPEVX.
* TSTSEL tests for eigenvalue convergence and selects the next
* pairs to be considered for targeting (as a block).
* NEWVEC computes the new vectors (block) to be added in the
* expanding basis, and tests for residual convergence.
* ADDABS is the critical step of matrix multiplication. The new
* vectors of D are found Dnew=ABnew, and the new small problem S,
* is calculated. The algorithm is repeated.
* In case of a large expanding basis (KPASS=LIM) the Basis, AB,
* SVEC and S are collapsed.
* At the end the current eigenvector estimates are computed as
* well as the residuals and eigenvalue differences.
*
* Subroutines called:
* DSPEVX, MULTBC, TSTSEL, OVFLOW, NEWVEC, ADDABS,
* DCOPY, DDOT, DAXPY
*-----------------------------------------------------------------------
C
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION DIAG(N)
DIMENSION S(LIM*(LIM+1)/2)
DIMENSION SVEC(LIM*LIM),EIGVAL(LIM)
DIMENSION ISELEC(NEIG)
DIMENSION BASIS(N*LIM),AB(N*LIM)
DIMENSION SCRA1(8*LIM),ISCRA2(LIM),INCV(LIM)
DIMENSION ICV(NUME),OLDVAL(NUME)
LOGICAL RESTART,FIRST,DONE,HIEND,TSTSEL
* external op
include "mpif.h"
include "mkl_blas.fi"
C
*-----------------------------------------------------------------------
*
* on entry
* -------
*
* OP The user specified block-matrix-vector routine
* N The order of the matrix A
* HIEND Logical. True only if the highest eigenpairs are needed.
* LIM The limit on the size of the expanding Basis
* MBLOCK Number of vectors to be targeted in each iteration.
* DIAG Array of size N with the diagonal elements of A
* NUME The largest index of the eigenvalues wanted.
* NIV Starting dimension of expanding basis.
* NEIG Number of eigenvalues wanted.
* ISELEC Array containg the indices of those NEIG eigenpairs.
* CRITE Convergence thresholds for eigenvalues, coefficients
* CRITC,CRITR and residuals.
* BASIS Array with the basis vectors.
* AB Array with the vectors D=AB
* S Array keeping the symmetric matrix of the small problem.
* SVEC Array for holding the eigenvectors of S
* SCRA1 Srcatch array used by DSPEVX.
* ISCRA2 Integer Srcatch array used by TSTSEL.
* INCV Srcatch array used in DSPEVX. Also used in TSTSEL and
* NEWVEC where it holds the Indices of uNConVerged pairs
* ICV It contains "1" to the locations of ConVerged eigenpairs
* OLDVAL Array keeping the previous' step eigenvalue estimates.
*
* on exit
* -------
*
* EIGVAL Array containing the NUME lowest eigenvalues of the
* the matrix A (or -A if the highest are sought).
* Basis On exit Basis stores the NUME corresponding eigenvectors
* OLDVAL On exit it stores the final differences of eigenvalues.
* SCRA1 On exit it stores the NUME corresponding residuals.
* NLOOPS Number of loops taken by the algorithm
* NMV Number of matrix-vector products performed.
*
*-----------------------------------------------------------------------
DO 5 I=1,NUME
EIGVAL(I)=1.D30
ICV(I)=0
5 continue
FIRST =.true.
KPASS =NIV
NNCV =KPASS
C
10 CONTINUE
* (iterations for kpass=NUME,LIM)
*
* Diagonalize the matrix S. Find only the NUME smallest eigenpairs
*
CALL DCOPY(NUME,EIGVAL,1,OLDVAL,1)
CALL MKMAT(KPASS,S,SVEC)
CALL QLDIAG(KPASS,SVEC,EIGVAL)
IERROR=0
* IF (IERROR.NE.0) GOTO 60
*
* TeST for convergence on the absolute difference of eigenvalues between
* successive steps. Also SELect the unconverged eigenpairs and sort them
* by the largest magnitude in the last added NNCV rows of Svec.
*
DONE=TSTSEL(KPASS,NUME,NEIG,ISELEC,SVEC,EIGVAL,ICV,
: CRITE,CRITC,SCRA1,ISCRA2,OLDVAL,NNCV,INCV)
IF ((DONE).OR.(KPASS.GE.N)) GOTO 30
C
IF (KPASS.EQ.LIM) THEN
* Maximum size for expanding basis. Collapse basis, D, and S, Svec
* Consider the basis vectors found in TSTSEL for the newvec.
*
CALL MULTBC(N,LIM,NUME,SVEC,SCRA1,BASIS)
CALL MULTBC(N,LIM,NUME,SVEC,SCRA1,AB)
CALL OVFLOW(NUME,LIM,S,SVEC,EIGVAL)
KPASS=NUME
ENDIF
*
* Compute and add the new vectors. NNCV is set to the number of new
* vectors that have not converged. If none, DONE=true, exit.
*
CALL NEWVEC(N,NUME,LIM,MBLOCK,KPASS,CRITR,ORTHO,NNCV,INCV,
: DIAG,SVEC,EIGVAL,AB,BASIS,ICV,RESTART,DONE)
C
* ..An infinite loop is avoided since after a collapsing Svec=I
* ..=> Res=Di-lBi which is just computed and it is orthogonal.
* ..The following is to prevent an improbable infinite loop.
IF (.NOT.RESTART) THEN
FIRST=.true.
ELSEIF (FIRST) THEN
FIRST=.false.
CALL MULTBC(N,KPASS+NNCV,NUME,SVEC,SCRA1,BASIS)
CALL MULTBC(N,KPASS+NNCV,NUME,SVEC,SCRA1,AB)
CALL OVFLOW(NUME,KPASS+NNCV,S,SVEC,EIGVAL)
KPASS=NUME
GOTO 10
ELSE
IERROR=IERROR+1024
GOTO 30
ENDIF
C
IF (DONE) GOTO 30
*
* Add new columns in D and S, from the NNCV new vectors.
*
CALL ADDABS(N,LIM,HIEND,KPASS,NNCV,BASIS,AB,S)
C
NMV=NMV+NNCV
KPASS=KPASS+NNCV
NLOOPS=NLOOPS+1
C
IF (NLOOPS.LE.MAXITER) GOTO 10
IERROR=IERROR+2048
NLOOPS=NLOOPS-1
KPASS=KPASS-NNCV
30 CONTINUE
*
* Calculate final results. EIGVAL contains the eigenvalues, BASIS the
* eigenvectors, OLDVAL the eigenvalue differences, and SCRA1 residuals.
*
DO 40 I=1,NUME
40 OLDVAL(I)=ABS(OLDVAL(I)-EIGVAL(I))
C
CALL MULTBC(N,KPASS,NUME,SVEC,SCRA1,BASIS)
CALL MULTBC(N,KPASS,NUME,SVEC,SCRA1,AB)
*
* i=1,NUME residual(i)= DCi-liBCi= newDi-linewBi
* temporarily stored in AB(NUME*N+1)
*
DO 50 I=1,NUME
CALL DCOPY(N,AB((I-1)*N+1),1,AB(NUME*N+1),1)
CALL DAXPY(N,-EIGVAL(I),BASIS((I-1)*N+1),1,AB(NUME*N+1),1)
SCRA1(I)=DDOT(N,AB(NUME*N+1),1,AB(NUME*N+1),1)
SCRA1(I)=SQRT(SCRA1(I))
50 CONTINUE
C
60 RETURN
END
*=======================================================================
SUBROUTINE ADDABS(N,LIM,HIEND,KPASS,NNCV,BASIS,AB,S)
*=======================================================================
* Called by: DVDRVR, SETUP
*
* Calculates the new column in the D matrix and the new column
* in the S matrix. The new D column is D(new)=AB(new). S has a
* new row and column, but being symmetric only the new column is
* stored. S(i,kpass+1)=B(i)^T D(kpass+1) for all i.
*
* subroutines called:
* OP, DDOT, DSCAL
*-----------------------------------------------------------------------
use variables
use ABop
use module_sparse
use basisindex_mod
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION BASIS(N*LIM),AB(N*LIM)
DIMENSION S(LIM*(LIM+1)/2)
LOGICAL HIEND
include "mkl_blas.fi"
*-----------------------------------------------------------------------
* on entry
* -------
* N The order of the matrix A
* kpass The current dimension of the expanding sub-basis
* NNCV Number of new basis vectors.
* Basis the basis vectors, including the new NNCV ones.
* on exit
* -------
* AB The new matrix D=AB. (with new NNCV columns)
* S The small matrix with NNCV new columns at the last part
*-----------------------------------------------------------------------
*
* The user specified matrix-vector routine is called with the new
* basis vector B(*,kpass+1) and the result is assigned to AB(idstart)
C
IDSTART=KPASS*N+1
c jjren add
if(diagmethod/="MD") then
call MPI_bcast(NNCV,1,MPI_integer,0,MPI_COMM_WORLD,ierr)
end if
if(opmethod=="comple") then
CALL OP(N,NNCV,BASIS(IDSTART),AB(IDSTART),
: Lrealdim,Rrealdim,subM,ngoodstates,
: operamatbig1,bigcolindex1,bigrowindex1,
: Hbig,Hbigcolindex,Hbigrowindex,
: quantabigL,quantabigR,goodbasis,goodbasiscol)
else if (opmethod=="direct") then
CALL OPDIRECT(N,NNCV,BASIS(IDSTART),AB(IDSTART),
: Lrealdim,Rrealdim,subM,ngoodstates,
: operamatbig1,bigcolindex1,bigrowindex1,
: Hbig,Hbigcolindex,Hbigrowindex,
: quantabigL,quantabigR,goodbasis,goodbasiscol)
else
stop
end if
*
* If highest pairs are sought, use the negative of the matrix
*
IF (HIEND) CALL DSCAL(N*NNCV,-1.D0,AB(IDSTART),1)
*
* The new S is calculated by adding the new last columns
* S(new)=B^T D(new).
*
ISSTART=KPASS*(KPASS+1)/2
DO 20 IV=1,NNCV
IBSTART=1
DO 10 IBV=1,KPASS+IV
SS=DDOT(N,BASIS(IBSTART),1,AB(IDSTART),1)
S(ISSTART + IBV)=SS
IBSTART=IBSTART+N
10 CONTINUE
ISSTART=ISSTART+KPASS+IV
IDSTART=IDSTART+N
20 CONTINUE
C
RETURN
END
*=======================================================================
LOGICAL FUNCTION TSTSEL(KPASS,NUME,NEIG,ISELEC,SVEC,EIGVAL,ICV,
: CRITE,CRITC,ROWLAST,IND,OLDVAL,NNCV,INCV)
*=======================================================================
*
* Called by: DVDRVR
C
* It first checks if the wanted eigenvalues have reached
* convergence and updates OLDVAL. Second, for each wanted and non
* converged eigenvector, it finds the largest absolute coefficient
* of the NNCV last added vectors (from SVEC) and if not coverged,
* places it in ROWLAST. IND has the corresponding indices.
* Third, it sorts ROWLAST in decreasing order and places the
* corresponding indices in the array INCV. The index array INCV
* and the number of unconverged pairs NNCV, are passed to DVDRVR.
* Later in NEWVEC only the first MBLOCK of NNCV pairs will be
* targeted, since if ROWLAST(i) > ROWLAST(j)
* then approximately RESIDUAL(i) > RESIDUAL(j)
*
* Subroutines called
* IDAMAX
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
LOGICAL DONE
DIMENSION SVEC(KPASS*NUME),EIGVAL(NUME)
DIMENSION ICV(NUME)
DIMENSION ROWLAST(NEIG),IND(NEIG),OLDVAL(NUME)
DIMENSION INCV(NEIG),ISELEC(NEIG)
include "mpif.h"
*-----------------------------------------------------------------------
*
* on entry
* -------
* KPASS current dimension of the expanding Basis
* NUME Largest index of the wanted eigenvalues.
* NEIG number of wanted eigenvalues of original matrix
* ISELEC index array of the wanted eigenvalues.
* SVEC the eigenvectors of the small system
* EIGVAL The NUME lowest eigenvalues of the small problem
* ICV Index of converged eigenpairs.ICV(i)=1 iff eigenpair i
* has converged, and ICV(i)=0 if eigenpair i has not.
* CRITE,CRITC Convergence thresholds for eigenvalues and coefficients
* ROWLAST scratch array, keeping the largest absolute coefficient
* of the NNCV last rows of Svec.
* IND scratch array, temporary keeping the indices of Rowlast
* OLDVAL The previous iteration's eigenvalues.
*
* on exit
* -------
* NNCV Number of non converged eigenvectors (to be targeted)
* INCV Index to these columns in decreasing order of magnitude
* TSTSEL true if convergence has been reached
*
*-----------------------------------------------------------------------
C
DONE=.False.
*
* Test all wanted eigenvalues for convergence under CRITE
*
NNCE=0
DO 10 I=1,NEIG
IVAL=ISELEC(I)
10 IF (ABS(OLDVAL(IVAL)-EIGVAL(IVAL)).GE.CRITE) NNCE=NNCE+1
IF (NNCE.EQ.0) THEN
TSTSEL=.TRUE.
RETURN
ENDIF
*
* Find the maximum element of the last NNCV coefficients of unconverged
* eigenvectors. For those unconverged coefficients, put their indices
* to IND and find their number NNCV
*
ICNT=0
DO 30 I=1,NEIG
IF (ICV(ISELEC(I)).EQ.0) THEN
* ..Find coefficient and test for convergence
ICUR=KPASS*ISELEC(I)
TMAX=ABS( SVEC(ICUR) )
DO 20 L=1,NNCV-1
20 TMAX=MAX( TMAX, ABS(SVEC(ICUR-L)) )
IF (TMAX.LT.CRITC) THEN
* ..this coefficient converged
ICV(ISELEC(I))=1
ELSE
* ..Not converged. Add it to the list.
ICNT=ICNT+1
IND(ICNT)=ISELEC(I)
ROWLAST(ICNT)=TMAX
ENDIF
ENDIF
30 CONTINUE
C
NNCV=ICNT
IF (NNCV.EQ.0) DONE=.TRUE.
*
* Sort the ROWLAST elements interchanging their indices as well
*
DO 40 I=1,NNCV
INDX=IDAMAX(NNCV-I+1,ROWLAST(I),1)
INCV(I)=IND(INDX+I-1)
C
TEMP=ROWLAST(INDX+I-1)
ROWLAST(INDX+I-1)=ROWLAST(I)
ROWLAST(I)=TEMP
ITEMP=IND(INDX+I-1)
IND(INDX+I-1)=IND(I)
IND(I)=ITEMP
40 CONTINUE
C
TSTSEL=DONE
RETURN
END
*=======================================================================
SUBROUTINE MULTBC(N,K,M,C,TEMP,B)
*=======================================================================
* called by: DVDRVR
*
* Multiplies B(N,K)*C(K,M) and stores it in B(N,M)
* Used for collapsing the expanding basis to current estimates,
* when basis becomes too large, or for returning the results back
C
* Subroutines called
* DINIT, DGEMV, DCOPY
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION B(N*K),C(K*M),TEMP(M)
include "mpif.h"
include "mkl_blas.fi"
*-----------------------------------------------------------------------
DO 10 IROW=1,N
* CALL DINIT(M,0.d0,TEMP,1)
CALL DGEMV('Transp',K,M, 1.D0, C,K,B(IROW),N, 0.D0 ,TEMP,1)
CALL DCOPY(M,TEMP,1,B(IROW),N)
10 CONTINUE
C
RETURN
END
C
*=======================================================================
SUBROUTINE OVFLOW(NUME,LIM,S,SVEC,EIGVAL)
*=======================================================================
* Called by: DVDRVR
* Called when the upper limit (LIM) has been reached for the basis
* expansion. The new S is computed as S'(i,j)=l(i)delta(i,j) where
* l(i) eigenvalues, and delta of Kronecker, i,j=1,NUME. The new
* eigenvectors of the small matrix are the unit vectors.
*
* Subroutines called:
* DCOPY, DINIT
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION SVEC(LIM*NUME),S((LIM*(LIM+1))/2)
DIMENSION EIGVAL(LIM)
include "mpif.h"
include "mkl_blas.fi"
*-----------------------------------------------------------------------
* on entry
* -------
* NUME The largest index of eigenvalues wanted.
* SVEC the kpass eigenvectors of the smaller system solved
* EIGVAL the eigenvalues of this small system
* on exit
* -------
* S The new small matrix to be solved.
*-----------------------------------------------------------------------
*
* calculation of the new upper S=diag(l1,...,l_NUME) and
* its matrix Svec of eigenvectors (e1,...,e_NUME)
*
CALL DINIT((NUME*(NUME+1))/2,0.d0,S,1)
CALL DINIT(NUME*NUME,0.d0,SVEC,1)
IND=0
ICUR=0
DO 10 I=1,NUME
S(IND+I)=EIGVAL(I)
SVEC(ICUR+I)=1
ICUR=ICUR+NUME
10 IND=IND+I
C
RETURN
END
*=======================================================================
SUBROUTINE NEWVEC(N,NUME,LIM,MBLOCK,KPASS,CRITR,ORTHO,NNCV,INCV,
: DIAG,SVEC,EIGVAL,AB,BASIS,ICV,RESTART,DONE)
*=======================================================================
*
* Called by: DVDRVR
*
* It calculates the new expansion vectors of the basis.
* For each one of the vectors in INCV starting with the largest
* megnitude one, calculate its residual Ri= DCi-liBCi and check
* the ||Ri|| for convergence. If it is converged do not add it
* but look for the immediate larger coefficient and its vector.
* The above procedure continues until MBLOCK vectors have been
* added to the basis, or the upper limit has been encountered.
* Thus only the required MBLOCK residuals are computed. Then,
* calculate the first order correction on the added residuals
* Ri(j) = Ri(j)/(li-Ajj) and orthonormalizes the new vectors
* to the basis and to themselves.
*
* Subroutines called:
* ORTHNRM, DDOT, DGEMV
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION INCV(NUME)
DIMENSION ICV(NUME)
DIMENSION DIAG(N)
DIMENSION BASIS(N*LIM),AB(N*LIM)
DIMENSION SVEC(LIM*NUME)
DIMENSION EIGVAL(LIM)
LOGICAL RESTART,DONE
include "mpif.h"
include "mkl_blas.fi"
*-----------------------------------------------------------------------
* on entry
* --------
* N The order of the matrix A
* NUME The largest index of the eigenvalues wanted.
* LIM The limit on the size of the expanding Basis
* MBLOCK Maximum number of vectora to enter the basis
* KPASS the current dimension of the expanding basis
* CRITR Convergence threshold for residuals
* ORTHO Orthogonality threshold to be passed to ORTHNRM
* NNCV Number of Non ConVerged pairs (MBLOCK will be targeted)
* INCV Index to the corresponding SVEC columns of these pairs.
* DIAG Array of size N with the diagonal elements of A
* SVEC,EIGVAL Arrays holding the eigenvectors and eigenvalues of S
* AB Array with the vectors D=AB
* BASIS the expanding basis having kpass vectors
* ICV Index of converged eigenpairs (ICV(i)=1 <=>i converged)
C
* on exit
* -------
* NNCV The number of vectors finally added to the basis.
* BASIS The new basis incorporating the new vectors at the end
* ICV Index of converged eigenpairs (updated)
* DONE logical, if covergance has been reached.
* RESTART logical, if because of extreme loss of orthogonality
* the Basis should be collapsed to current approximations.
*-----------------------------------------------------------------------
DONE = .FALSE.
NEWSTART= KPASS*N+1
NADDED = 0
ICVC = 0
LIMADD = MIN( LIM, MBLOCK+KPASS )
ICUR = NEWSTART
*
* Compute RESIDUALS for the MBLOCK of the NNCV not converged vectors.
*
DO 10 I=1,NNCV
INDX=INCV(I)
* ..Compute Newv=BASIS*Svec_indx , then
* ..Compute Newv=AB*Svec_indx - eigval*Newv and then
* ..compute the norm of the residual of Newv
CALL DGEMV('N',N,KPASS,1.D0,BASIS,N,SVEC((INDX-1)*KPASS+1),1,
: 0.d0,BASIS(ICUR),1)
CALL DGEMV('N',N,KPASS,1.D0,AB,N,SVEC((INDX-1)*KPASS+1),1,
: -EIGVAL(INDX),BASIS(ICUR),1)
SS = DNRM2(N,BASIS(ICUR),1)
*
* ..Check for convergence of this residual
*
IF (SS.LT.CRITR) THEN
* ..Converged,do not add. Go for next non converged one
ICVC=ICVC+1
ICV( INDX ) = 1
IF (ICVC.LT.NNCV) GOTO 10
* ..All have converged.
DONE=.TRUE.
RETURN
ELSE
* ..Not converged. Add it in the basis
NADDED=NADDED+1
INCV(NADDED)=INDX
IF ((NADDED+KPASS).EQ.LIMADD) GOTO 20
* ..More to be added in the block
ICUR=ICUR+N
ENDIF
10 CONTINUE
C
20 NNCV=NADDED
*
* Diagonal preconditioning: newvect(i)=newvect(i)/(l-Aii)
* If (l-Aii) is very small (or zero) divide by 10.D-6
*
ICUR=NEWSTART-1
DO 50 I=1,NNCV
DO 40 IROW=1,N
DG=EIGVAL(INCV(I))-DIAG(IROW)
IF (ABS(DG).GT.(1.D-13)) THEN
BASIS(ICUR+IROW)=BASIS(ICUR+IROW) / DG
ELSE
BASIS(ICUR+IROW)=BASIS(ICUR+IROW) /1.D-13
ENDIF
40 CONTINUE
ICUR=ICUR+N
50 CONTINUE
*
* ORTHONORMALIZATION
*
CALL ORTHNRM(N,LIM,ORTHO,KPASS,NNCV,AB(NEWSTART),
: BASIS,RESTART)
C
99 RETURN
END
*=======================================================================
SUBROUTINE ORTHNRM(N,LIM,ORTHO,KPASS,NNCV,SCRA1,
: BASIS,RESTART)
*=======================================================================
*
* It orthogonalizes the new NNCV basis vectors starting from the
* kpass+1, to the previous vectors of the basis and to themselves.
* A Gram-Schmidt method is followed after which the residuals
* should be orthogonal to the BASIS. Because of machine arithmetic
* errors this orthogonality may be lost, and a reorthogonalization
* procedure is adopted whenever orthogonality loss is above a
* ORTHO. If after some reorthogonalizations the procedure does not
* converge to orthogonality, the basis is collapsed to the
* current eigenvector approximations.
*
* Subroutines called:
* DAXPY, DDOT, DSCAL
*-----------------------------------------------------------------------
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
DIMENSION BASIS(N*LIM)
DIMENSION SCRA1(N)
LOGICAL RESTART
include "mpif.h"
include "mkl_blas.fi"
*-----------------------------------------------------------------------
* on entry
* --------
* N The order of the matrix A
* LIM The limit on the size of the expanding Basis
* ORTHO The orthogonality threshold
* KPASS The number of basis vectors already in Basis