-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpnum.f90
2475 lines (2246 loc) · 69.5 KB
/
mpnum.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
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
! Code converted using TO_F90 by Alan Miller
! Date: 2012-03-16 Time: 11:07:48
!> \file
!! General linear algebra routines.
!!
!! \author Volker Blobel, University Hamburg, 2005-2009 (initial Fortran77 version)
!! \author Claus Kleinwort, DESY (maintenance and developement)
!!
!! \copyright
!! Copyright (c) 2009 - 2015 Deutsches Elektronen-Synchroton,
!! Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY \n\n
!! This library is free software; you can redistribute it and/or modify
!! it under the terms of the GNU Library General Public License as
!! published by the Free Software Foundation; either version 2 of the
!! License, or (at your option) any later version. \n\n
!! This library is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU Library General Public License for more details. \n\n
!! You should have received a copy of the GNU Library General Public
!! License along with this program (see the file COPYING.LIB for more
!! details); if not, write to the Free Software Foundation, Inc.,
!! 675 Mass Ave, Cambridge, MA 02139, USA.
!!
!! ***** Collection of utility routines from V. Blobel *****
!!
!! V. Blobel, Univ. Hamburg
!! Numerical subprograms used in MP-II: matrix equations,
!! and matrix products, double precision
!!
!! Solution by inversion
!! SQMINV
!! SQMINL for LARGE matrix, use OpenMP (CHK)
!!
!! Solution by diagonalization
!! DEVROT, DEVPRT, DEFSOL,DEVINV
!!
!! Solution by Cholesky decomposition of symmetric matrix
!! CHOLDC
!!
!! Solution by Cholesky decomposition of variable-band matrix
!! VABDEC
!!
!! Solution by Cholesky decomposition of bordered band matrix
!! SQMIBB (CHK)
!!
!! Matrix/vector products
!! DBDOT dot vector product
!! DBAXPY multiplication and addition
!! DBSVX symmetric matrix vector
!! DBSVX LARGE symmetric matrix vector (CHK)
!! DBGAX general matrix vector
!! DBAVAT AVAT product
!! DBMPRV print parameter and matrix
!! DBPRV print matrix (CHK)
!!
!! Chi^2 cut values
!! CHINDL
!!
!! Accurate summation (moved to pede.f90)
!! ADDSUM
!!
!! Sorting
!! HEAPF heap sort reals direct
!! SORT1K sort 1-dim key-array (CHK)
!! SORT2K sort 2-dim key-array
!!
!----------------------------------------------------------------------
!> Matrix inversion and solution.
!!
!! Obtain solution of a system of linear equations with symmetric
!! matrix (V * X = B) and the inverse.
!!
!! Method of solution is by elimination selecting the pivot on the
!! diagonal each stage. The rank of the matrix is returned in NRANK.
!! For NRANK ne N, all remaining rows and cols of the resulting
!! matrix V and the corresponding elements of B are set to zero.
!!
!! \param [in,out] V symmetric N-by-N matrix in symmetric storage mode
!! (V(1) = V11, V(2) = V12, V(3) = V22, V(4) = V13, ...),
!! replaced by inverse matrix
!! \param [in,out] B N-vector, replaced by solution vector
!! \param [in] N size of V, B
!! \param [out] NRANK rank of matrix V
!! \param [out] DIAG double precision scratch array
!! \param [out] NEXT INTEGER(mpi) aux array
SUBROUTINE sqminv(v,b,n,nrank,diag,next) ! matrix inversion
USE mpdef
IMPLICIT NONE
INTEGER(mpi) :: i
INTEGER(mpi) :: ij
INTEGER(mpi) :: j
INTEGER(mpi) :: jj
INTEGER(mpi) :: jk
INTEGER(mpi) :: jl
INTEGER(mpi) :: k
INTEGER(mpi) :: kk
INTEGER(mpi) :: l
INTEGER(mpi) :: last
INTEGER(mpi) :: lk
INTEGER(mpi) :: next0
REAL(mpd), INTENT(IN OUT) :: v(*)
REAL(mpd), INTENT(OUT) :: b(n)
INTEGER(mpi), INTENT(IN) :: n
INTEGER(mpi), INTENT(OUT) :: nrank
REAL(mpd), INTENT(OUT) :: diag(n)
INTEGER(mpi), INTENT(OUT) :: next(n)
REAL(mpd) :: vkk
REAL(mpd) :: vjk
!REAL(mpd), PARAMETER :: eps=1.0E-10_mpd
REAL(mpd) eps
! ...
eps = 16.0_mpd * epsilon(eps) ! 16 * precision(mpd)
next0=1
l=1
DO i=1,n
next(i)=i+1 ! set "next" pointer
diag(i)=ABS(v((i*i+i)/2)) ! save abs of diagonal elements
END DO
next(n)=-1 ! end flag
nrank=0
DO i=1,n ! start of loop
k =0
vkk=0.0_mpd
j=next0
last=0
05 IF(j > 0) THEN
jj=(j*j+j)/2
IF(ABS(v(jj)) > MAX(ABS(vkk),eps*diag(j))) THEN
vkk=v(jj)
k=j
l=last
END IF
last=j
j=next(last)
GO TO 05
END IF
IF(k /= 0) THEN ! pivot found
kk=(k*k+k)/2
IF(l == 0) THEN
next0=next(k)
ELSE
next(l)=next(k)
END IF
next(k)=0 ! index is used, reset
nrank=nrank+1 ! increase rank and ...
vkk =1.0/vkk
v(kk) =-vkk
b(k) =b(k)*vkk
jk =kk-k
jl =0
DO j=1,n ! elimination
IF(j == k) THEN
jk=kk
jl=jl+j
ELSE
IF(j < k) THEN
jk=jk+1
ELSE
jk=jk+j-1
END IF
vjk =v(jk)
v(jk)=vkk*vjk
b(j) =b(j)-b(k)*vjk
lk =kk-k
DO l=1,j
jl=jl+1
IF(l == k) THEN
lk=kk
ELSE
IF(l < k) THEN
lk=lk+1
ELSE
lk=lk+l-1
END IF
v(jl)=v(jl)-v(lk)*vjk
END IF
END DO
END IF
END DO
ELSE
DO k=1,n
IF(next(k) /= 0) THEN
b(k)=0.0_mpd ! clear vector element
DO j=1,k
IF(next(j) /= 0) v((k*k-k)/2+j)=0.0_mpd ! clear matrix row/col
END DO
END IF
END DO
GO TO 10
END IF
END DO ! end of loop
10 DO ij=1,(n*n+n)/2
v(ij)=-v(ij) ! finally reverse sign of all matrix elements
END DO
END SUBROUTINE sqminv
!> Matrix inversion for LARGE matrices.
!!
!! Like SQMINV, additional parallelization with OpenMP.
!!
!! \param [in,out] V symmetric N-by-N matrix in symmetric storage mode
!! (V(1) = V11, V(2) = V12, V(3) = V22, V(4) = V13, ...),
!! replaced by inverse matrix
!! \param [in,out] B N-vector, replaced by solution vector
!! \param [in] N size of V, B
!! \param [out] NRANK rank of matrix V
!! \param [out] DIAG double precision scratch array
!! \param [out] NEXT integer aux array
SUBROUTINE sqminl(v,b,n,nrank,diag,next) !
USE mpdef
IMPLICIT NONE
INTEGER(mpi) :: i
INTEGER(mpi) :: j
INTEGER(mpi) :: k
INTEGER(mpi) :: l
INTEGER(mpi) :: last
INTEGER(mpi) :: next0
REAL(mpd), INTENT(IN OUT) :: v(*)
REAL(mpd), INTENT(OUT) :: b(n)
INTEGER(mpi), INTENT(IN) :: n
INTEGER(mpi), INTENT(OUT) :: nrank
REAL(mpd), INTENT(OUT) :: diag(n)
INTEGER(mpi), INTENT(OUT) :: next(n)
INTEGER(mpl) :: i8
INTEGER(mpl) :: j8
INTEGER(mpl) :: jj
INTEGER(mpl) :: k8
INTEGER(mpl) :: kk
INTEGER(mpl) :: kkmk
INTEGER(mpl) :: jk
INTEGER(mpl) :: jl
INTEGER(mpl) :: llk
INTEGER(mpl) :: ljl
REAL(mpd) :: vkk
REAL(mpd) :: vjk
REAL(mpd), PARAMETER :: eps=1.0E-10_mpd
! ...
next0=1
l=1
DO i=1,n
i8=int8(i)
next(i)=i+1 ! set "next" pointer
diag(i)=ABS(v((i8*i8+i8)/2)) ! save abs of diagonal elements
END DO
next(n)=-1 ! end flag
nrank=0
DO i=1,n ! start of loop
k =0
vkk=0.0_mpd
j=next0
last=0
05 IF(j > 0) THEN
j8=int8(j)
jj=(j8*j8+j8)/2
IF(ABS(v(jj)) > MAX(ABS(vkk),eps*diag(j))) THEN
vkk=v(jj)
k=j
l=last
END IF
last=j
j=next(last)
GO TO 05
END IF
IF(k /= 0) THEN ! pivot found
k8=int8(k)
kk=(k8*k8+k8)/2
kkmk=kk-k8
IF(l == 0) THEN
next0=next(k)
ELSE
next(l)=next(k)
END IF
next(k)=0 ! index is used, reset
nrank=nrank+1 ! increase rank and ...
vkk =1.0/vkk
v(kk) =-vkk
b(k) =b(k)*vkk
! elimination
jk =kkmk
DO j=1,n
IF(j == k) THEN
jk=kk
ELSE
IF(j < k) THEN
jk=jk+1
ELSE
jk=jk+int8(j)-1
END IF
v(jk)=v(jk)*vkk
END IF
END DO
! parallelize row loop
! slot of 128 'J' for next idle thread
!$OMP PARALLEL DO &
!$OMP PRIVATE(JL,JK,L,LJL,LLK,VJK,J8) &
!$OMP SCHEDULE(DYNAMIC,128)
DO j=n,1,-1
j8=int8(j)
jl=j8*(j8-1)/2
IF(j /= k) THEN
IF(j < k) THEN
jk=kkmk+j8
ELSE
jk=k8+jl
END IF
vjk =v(jk)/vkk
b(j) =b(j)-b(k)*vjk
ljl=jl
llk=kkmk
DO l=1,MIN(j,k-1)
ljl=ljl+1
llk=llk+1
v(ljl)=v(ljl)-v(llk)*vjk
END DO
ljl=ljl+1
llk=kk
DO l=k+1,j
ljl=ljl+1
llk=llk+l-1
v(ljl)=v(ljl)-v(llk)*vjk
END DO
END IF
END DO
!$OMP END PARALLEL DO
ELSE
DO k=1,n
k8=int8(k)
kk=(k8*k8-k8)/2
IF(next(k) /= 0) THEN
b(k)=0.0_mpd ! clear vector element
DO j=1,k
IF(next(j) /= 0) v(kk+int8(j))=0.0_mpd ! clear matrix row/col
END DO
END IF
END DO
GO TO 10
END IF
END DO ! end of loop
10 DO jj=1,(int8(n)*int8(n)+int8(n))/2
v(jj)=-v(jj) ! finally reverse sign of all matrix elements
END DO
END SUBROUTINE sqminl
!> Diagonalization.
!!
!! Determination of eigenvalues and eigenvectors of
!! symmetric matrix V by Householder method
!!
!! \param [in] n size of matrix
!! \param [out] diag diagonal elements
!! \param [out] u transformation matrix
!! \param [in] v symmetric matrix, unchanged
!! \param [out] work work array
!! \param [out] iwork work array
SUBROUTINE devrot(n,diag,u,v,work,iwork) ! diagonalization
USE mpdef
IMPLICIT NONE
INTEGER(mpi), INTENT(IN) :: n
REAL(mpd), INTENT(OUT) :: diag(n)
REAL(mpd), INTENT(OUT) :: u(n,n)
REAL(mpd), INTENT(IN) :: v(*)
REAL(mpd), INTENT(OUT) :: work(n)
INTEGER(mpi), INTENT(OUT) :: iwork(n)
INTEGER(mpi), PARAMETER :: itmax=30
REAL(mpd), PARAMETER :: tol=EPSILON(tol)
REAL(mpd), PARAMETER :: eps=EPSILON(eps)
REAL(mpd) :: f
REAL(mpd) :: g
REAL(mpd) :: h
REAL(mpd) :: sh
REAL(mpd) :: hh
REAL(mpd) :: b
REAL(mpd) :: p
REAL(mpd) :: r
REAL(mpd) :: s
REAL(mpd) :: c
REAL(mpd) :: workd
INTEGER(mpi) :: ij
INTEGER(mpi) :: i
INTEGER(mpi) :: j
INTEGER(mpi) :: k
INTEGER(mpi) :: l
INTEGER(mpi) :: m
INTEGER(mpi) :: ll
! ...
! 1. part: symmetric matrix V reduced to tridiagonal from
ij=0
DO i=1,n
DO j=1,i
ij=ij+1
u(i,j)=v(ij) ! copy half of symmetric matirx
END DO
END DO
DO i=n,2,-1
l=i-2
f=u(i,i-1)
g=0.0_mpd
IF(l /= 0) THEN
DO k=1,l
IF(ABS(u(i,k)) > tol) g=g+u(i,k)*u(i,k)
END DO
h=g+f*f
END IF
IF(g < tol) THEN ! G too small
work(i)=f ! skip transformation
h =0.0_mpd
ELSE
l=l+1
sh=SQRT(h)
IF(f >= 0.0_mpd) sh=-sh
g=sh
work(i)=sh
h=h-f*g
u(i,i-1)=f-g
f=0.0_mpd
DO j=1,l
u(j,i)=u(i,j)/h
g=0.0_mpd
! form element of a u
DO k=1,j
IF(ABS(u(j,k)) > tol.AND.ABS(u(i,k)) > tol) THEN
g=g+u(j,k)*u(i,k)
END IF
END DO
DO k=j+1,l
IF(ABS(u(k,j)) > tol.AND.ABS(u(i,k)) > tol) THEN
g=g+u(k,j)*u(i,k)
END IF
END DO
work(j)=g/h
f=f+g*u(j,i)
END DO
! form k
hh=f/(h+h)
! form reduced a
DO j=1,l
f=u(i,j)
work(j)=work(j)-hh*f
g=work(j)
DO k=1,j
u(j,k)=u(j,k)-f*work(k)-g*u(i,k)
END DO
END DO
END IF
diag(i)=h
END DO
diag(1)=0.0_mpd
work(1)=0.0_mpd
! accumulation of transformation matrices
DO i=1,n
IF(diag(i) /= 0.0) THEN
DO j=1,i-1
g=0.0_mpd
DO k=1,i-1
g=g+u(i,k)*u(k,j)
END DO
DO k=1,i-1
u(k,j)=u(k,j)-g*u(k,i)
END DO
END DO
END IF
diag(i)=u(i,i)
u(i,i)=1.0_mpd
DO j=1,i-1
u(i,j)=0.0_mpd
u(j,i)=0.0_mpd
END DO
END DO
! 2. part: diagonalization of tridiagonal matrix
DO i=2,n
work(i-1)=work(i)
END DO
work(n)=0.0_mpd
b=0.0_mpd
f=0.0_mpd
DO l=1,n
j=0
h=eps*(ABS(diag(l))+ABS(work(l)))
IF(b < h) b=h
DO m=l,n
IF(ABS(work(m)) <= b) GO TO 10 ! look for small sub-diagonal element
END DO
m=l
10 IF(m == l) GO TO 30
! next iteration
20 IF(j == itmax) THEN
WRITE(*,*) 'DEVROT: Iteration limit reached'
CALL peend(32,'Aborted, iteration limit reached in diagonalization')
STOP
END IF
j=j+1
g=diag(l)
p=(diag(l+1)-g)/(2.0_mpd*work(l))
r=SQRT(1.0_mpd+p*p)
diag(l)=work(l)
IF(p < 0.0_mpd) diag(l)=diag(l)/(p-r)
IF(p >= 0.0_mpd) diag(l)=diag(l)/(p+r)
h=g-diag(l)
DO i=l+1,n
diag(i)=diag(i)-h
END DO
f=f+h
! QL transformation
p=diag(m)
c=1.0_mpd
s=0.0_mpd
DO i=m-1,l,-1 ! reverse loop
g=c*work(i)
h=c*p
IF(ABS(p) >= ABS(work(i))) THEN
c=work(i)/p
r=SQRT(1.0_mpd+c*c)
work(i+1)=s*p*r
s=c/r
c=1.0_mpd/r
ELSE
c=p/work(i)
r=SQRT(1.0_mpd+c*c)
work(i+1)=s*work(i)*r
s=1.0_mpd/r
c=c/r
END IF
p=c*diag(i)-s*g
diag(i+1)=h+s*(c*g+s*diag(i))
! form vector
DO k=1,n
h=u(k,i+1)
u(k,i+1)=s*u(k,i)+c*h
u(k,i)=c*u(k,i)-s*h
END DO
END DO
work(l)=s*p
diag(l)=c*p
IF(ABS(work(l)) > b) GO TO 20 ! next iteration
30 diag(l)=diag(l)+f
END DO
DO i=1,n
iwork(i)=i
END DO
m=1
40 m=1+3*m ! determine initial increment
IF(m <= n) GO TO 40
50 m=m/3
DO j=1,n-m ! sort with increment M
l=j
60 IF(diag(iwork(l+m)) > diag(iwork(l))) THEN ! compare
ll=iwork(l+m) ! exchange the two index values
iwork(l+m)=iwork(l)
iwork(l)=ll
l=l-m
IF(l > 0) GO TO 60
END IF
END DO
IF(m > 1) GO TO 50
DO i=1,n
IF(iwork(i) /= i) THEN
! move vector from position I to the work area
workd=diag(i)
DO l=1,n
work(l)=u(l,i)
END DO
k=i
70 j=k
k=iwork(j)
iwork(j)=j
IF(k /= i) THEN
! move vector from position K to the (free) position J
diag(j)=diag(k)
DO l=1,n
u(l,j)=u(l,k)
END DO
GO TO 70
END IF
! move vector from the work area to position J
diag(j)=workd
DO l=1,n
u(l,j)=work(l)
END DO
END IF
END DO
END SUBROUTINE devrot
!> Calculate significances.
SUBROUTINE devsig(n,diag,u,b,coef)
USE mpdef
IMPLICIT NONE
INTEGER(mpi), INTENT(IN) :: n
REAL(mpd), INTENT(IN) :: diag(n)
REAL(mpd), INTENT(IN) :: u(n,n)
REAL(mpd), INTENT(IN) :: b(n)
REAL(mpd), INTENT(OUT) :: coef(n)
INTEGER(mpi) :: i
INTEGER(mpi) :: j
REAL(mpd) :: dsum
! ...
DO i=1,n
coef(i)=0.0_mpd
IF(diag(i) > 0.0_mpd) THEN
dsum=0.0_mpd
DO j=1,n
dsum=dsum+u(j,i)*b(j)
END DO
coef(i)=ABS(dsum)/SQRT(diag(i))
END IF
END DO
END SUBROUTINE devsig
!> Solution by diagonalization.
!!
!! Solution of matrix equation V * X = B after diagonalization of V.
!!
!! \param [in] N size of matrix
!! \param [in] DIAG diagonal elements
!! \param [in] U transformation matrix
!! \param [in] B r.h.s. of matrix equation (unchanged)
!! \param [out] X solution vector
!! \param [out] WORK work array
SUBROUTINE devsol(n,diag,u,b,x,work)
USE mpdef
IMPLICIT NONE
INTEGER(mpi), INTENT(IN) :: n
REAL(mpd), INTENT(IN) :: diag(n)
REAL(mpd), INTENT(IN) :: u(n,n)
REAL(mpd), INTENT(IN) :: b(n)
REAL(mpd), INTENT(OUT) :: x(n)
REAL(mpd), INTENT(OUT) :: work(n)
INTEGER(mpi) :: i
INTEGER(mpi) :: j
INTEGER(mpi) :: jj
REAL(mpd) :: s
! ...
DO j=1,n
s=0.0_mpd
work(j)=0.0_mpd
IF(diag(j) /= 0.0_mpd) THEN
DO i=1,n
! j-th eigenvector is U(.,J)
s=s+u(i,j)*b(i)
END DO
work(j)=s/diag(j)
END IF
END DO
DO j=1,n
s=0.0_mpd
DO jj=1,n
s=s+u(j,jj)*work(jj)
END DO
x(j)=s
END DO
! WRITE(*,*) 'DEVSOL'
! WRITE(*,*) 'X ',X
END SUBROUTINE devsol
!> Inversion by diagonalization.
!! Get inverse matrix V from DIAG and U.
!!
!! \param [in] N size of matrix
!! \param [in] DIAG diagonal elements
!! \param [in] U transformation matrix
!! \param [out] V smmmetric matrix
SUBROUTINE devinv(n,diag,u,v)
USE mpdef
IMPLICIT NONE
INTEGER(mpi) :: i
INTEGER(mpi) :: ij
INTEGER(mpi) :: j
INTEGER(mpi) :: k
INTEGER(mpi), INTENT(IN) :: n
REAL(mpd), INTENT(IN) :: diag(n)
REAL(mpd), INTENT(IN) :: u(n,n)
REAL(mpd), INTENT(OUT) :: v(*)
REAL(mpd) :: dsum
! ...
ij=0
DO i=1,n
DO j=1,i
ij=ij+1
dsum=0.0_mpd
DO k=1,n
IF(diag(k) /= 0.0_mpd) THEN
dsum=dsum+u(i,k)*u(j,k)/diag(k)
END IF
END DO
v(ij)=dsum
END DO
END DO
END SUBROUTINE devinv
!> Cholesky decomposition.
!!
!! Cholesky decomposition of the matrix G: G = L D L^T
!!
!! - G = symmetric matrix, in symmetric storage mode
!!
!! - L = unit triangular matrix (1's on diagonal)
!!
!! - D = diagonal matrix (elements store on diagonal of L)
!!
!! The sqrts of the usual Cholesky decomposition are avoided by D.
!! Matrices L and D are stored in the place of matrix G; after the
!! decomposition, the solution of matrix equations and the computation
!! of the inverse of the (original) matrix G are done by CHOLSL and CHOLIN.
!!
!! \param [in,out] g symmetric matrix, replaced by D,L
!! \param [in] n size of matrix
!!
SUBROUTINE choldc(g,n)
USE mpdef
IMPLICIT NONE
INTEGER(mpi) :: i
INTEGER(mpi) :: ii
INTEGER(mpi) :: j
INTEGER(mpi) :: jj
INTEGER(mpi) :: k
INTEGER(mpi) :: kk
REAL(mpd), INTENT(IN OUT) :: g(*)
INTEGER(mpi), INTENT(IN) :: n
REAL(mpd) :: ratio
! ...
ii=0
DO i=1,n
ii=ii+i
IF(g(ii) /= 0.0) g(ii)=1.0/g(ii) ! (I,I) div !
jj=ii
DO j=i+1,n
ratio=g(i+jj)*g(ii) ! (I,J) (I,I)
kk=jj
DO k=j,n
g(kk+j)=g(kk+j)-g(kk+i)*ratio ! (K,J) (K,I)
kk=kk+k
END DO ! K
g(i+jj)=ratio ! (I,J)
jj=jj+j
END DO ! J
END DO ! I
RETURN
END SUBROUTINE choldc
!> Solution after decomposition.
!!
!! The matrix equation G X = B is solved for X, where the matrix
!! G in the argument is already decomposed by CHOLDC. The vector B
!! is called X in the argument and the content is replaced by the
!! resulting vector X.
!!
!! \param [in] g decomposed symmetric matrix
!! \param [in,out] x r.h.s vector B, replaced by solution vector X
!! \param [in] n size of matrix
!!
SUBROUTINE cholsl(g,x,n)
USE mpdef
IMPLICIT NONE
REAL(mpd) :: dsum
INTEGER(mpi) :: i
INTEGER(mpi) :: ii
INTEGER(mpi) :: k
INTEGER(mpi) :: kk
REAL(mpd), INTENT(IN) :: g(*)
REAL(mpd), INTENT(IN OUT) :: x(n)
INTEGER(mpi), INTENT(IN) :: n
ii=0
DO i=1,n
dsum=x(i)
DO k=1,i-1
dsum=dsum-g(k+ii)*x(k) ! (K,I)
END DO
x(i)=dsum
ii=ii+i
END DO
DO i=n,1,-1
dsum=x(i)*g(ii) ! (I,I)
kk=ii
DO k=i+1,n
dsum=dsum-g(kk+i)*x(k) ! (K,I)
kk=kk+k
END DO
x(i)=dsum
ii=ii-i
END DO
RETURN
END SUBROUTINE cholsl
!> Inversion after decomposition.
!!
!! The inverse of the (original) matrix G is computed and stored
!! in symmetric storage mode in matrix V. Arrays G and V must be
!! different arrays.
!!
!! \param [in] g decomposed symmetric matrix
!! \param [in,out] v inverse matrix
!! \param [in] n size of matrix
!!
SUBROUTINE cholin(g,v,n)
USE mpdef
IMPLICIT NONE
REAL(mpd) :: dsum
INTEGER(mpi) :: i
INTEGER(mpi) :: ii
INTEGER(mpi) :: j
INTEGER(mpi) :: k
INTEGER(mpi) :: l
INTEGER(mpi) :: m
REAL(mpd), INTENT(IN) :: g(*)
REAL(mpd), INTENT( OUT) :: v(*)
INTEGER(mpi), INTENT(IN) :: n
ii=(n*n-n)/2
DO i=n,1,-1
dsum=g(ii+i) ! (I,I)
DO j=i,1,-1
DO k=j+1,n
l=MIN(i,k)
m=MAX(i,k)
dsum=dsum-g(j+(k*k-k)/2)*v(l+(m*m-m)/2) ! (J,K) (I,K)
END DO
v(ii+j)=dsum ! (I,J)
dsum=0.0_mpd
END DO
ii=ii-i+1
END DO
END SUBROUTINE cholin
! variable band matrix operations ----------------------------------
!> Variable band matrix decomposition.
!!
!! Decomposition: A = L D L^T
!!
!! Variable-band matrix row Doolittle decomposition.
!! A variable-band NxN symmetric matrix, also called skyline, is stored
!! row by row in the array VAL(.). For each row every coefficient
!! between the first non-zero element in the row and the diagonal is
!! stored.
!! The pointer array ILPTR(N) contains the indices in VAL(.) of the
!! diagonal elements. ILPTR(1) is always 1, and ILPTR(N) is equal
!! to the total number of coefficients stored, called the profile.
!! The form of a variable-band matrix is preserved in the L D L^T
!! decomposition no fill-in is created ahead in any row or ahead of the
!! first entry in any column, but existing zero-values will become
!! non-zero. The decomposition is done "in-place".
!!
!! \param [in] n size of matrix
!! \param [in,out] val variable-band matrix, replaced by D,L
!! \param [in] ilptr pointer array
SUBROUTINE vabdec(n,val,ilptr)
USE mpdef
IMPLICIT NONE
INTEGER(mpi) :: i
INTEGER(mpi) :: in
INTEGER(mpi) :: j
INTEGER(mpi) :: k
INTEGER(mpi) :: kj
INTEGER(mpi) :: mj
INTEGER(mpi) :: mk
REAL(mpd) :: sn
REAL(mpd) :: beta
REAL(mpd) :: delta
REAL(mpd) :: theta
INTEGER(mpi), INTENT(IN) :: n
REAL(mpd), INTENT(IN OUT) :: val(*)
INTEGER(mpi), INTENT(IN) :: ilptr(n)
REAL(mpd) :: dgamma
REAL(mpd) :: xi
REAL(mpd) :: valkj
REAL(mpd), PARAMETER :: one=1.0_mpd
REAL(mpd), PARAMETER :: two=2.0_mpd
REAL(mpd), PARAMETER :: eps = EPSILON(eps)
WRITE(*,*) 'Variable band matrix Cholesky decomposition'
dgamma=0.0_mpd
i=1
DO j=1,ilptr(n) ! loop thrugh all matrix elements
IF(ilptr(i) == j) THEN ! diagonal element
IF(val(j) <= 0.0_mpd) GO TO 01 ! exit loop for negative diag
dgamma=MAX(dgamma,ABS(val(j))) ! max diagonal element
i=i+1
END IF
END DO
i=n+1
01 in=i-1 ! IN positive diagonal elements
WRITE(*,*) ' ',in,' positive diagonal elements'
xi=0.0_mpd
i=1
DO j=1,ilptr(in) ! loop for positive diagonal elements
! through all matrix elements
IF(ilptr(i) == j) THEN ! diagonal element
i=i+1
ELSE
xi=MAX(xi,ABS(val(j))) ! Xi = abs(max) off-diagonal element
END IF
END DO
delta=eps*MAX(1.0_mpd,dgamma+xi)
sn=1.0_mpd
IF(n > 1) sn=1.0_mpd/SQRT(REAL(n*n-1,mpd))
beta=SQRT(MAX(eps,dgamma,xi*sn)) ! beta
WRITE(*,*) ' DELTA and BETA ',delta,beta
DO k=2,n
mk=k-ilptr(k)+ilptr(k-1)+1
theta=0.0_mpd
DO j=mk,k
mj=j-ilptr(j)+ilptr(j-1)+1
kj=ilptr(k)-k+j ! index kj
DO i=MAX(mj,mk),j-1
val(kj)=val(kj) & ! L_kj := L_kj - L_ki D_ii L_ji
-val(ilptr(k)-k+i)*val(ilptr(i))*val(ilptr(j)-j+i)
END DO !
theta=MAX(theta,ABS(val(kj))) ! maximum value of row
IF(j /= k) THEN
IF(val(ilptr(j)) /= 0.0_mpd) THEN
val(kj)=val(kj)/val(ilptr(j))
ELSE
val(kj)=0.0_mpd
END IF
END IF ! L_kj := L_kj/D_jj ! D_kk
IF(j == k) THEN
valkj=val(kj)
IF(k <= in) THEN
val(kj)=MAX(ABS(val(kj)),(theta/beta)**2,delta)
IF(valkj /= val(kj)) THEN
WRITE(*,*) ' Index K=',k
WRITE(*,*) ' ',valkj,val(kj), (theta/beta)**2,delta,theta
END IF
END IF
END IF
END DO ! J
END DO ! K
DO k=1,n
IF(val(ilptr(k)) /= 0.0_mpd) val(ilptr(k))=1.0_mpd/val(ilptr(k))
END DO