-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterate_matrix.F
2342 lines (1962 loc) · 97.3 KB
/
iterate_matrix.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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Routines useful for iterative matrix calculations
!> \par History
!> 2010.10 created [Joost VandeVondele]
!> \author Joost VandeVondele
! **************************************************************************************************
MODULE iterate_matrix
USE arnoldi_api, ONLY: arnoldi_data_type,&
arnoldi_extremal
USE bibliography, ONLY: Richters2018,&
cite_reference
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_add_on_diag, dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, &
dbcsr_distribution_get, dbcsr_distribution_type, dbcsr_filter, dbcsr_frobenius_norm, &
dbcsr_gershgorin_norm, dbcsr_get_diag, dbcsr_get_info, dbcsr_get_matrix_type, &
dbcsr_get_occupation, dbcsr_multiply, dbcsr_norm, dbcsr_norm_maxabsnorm, dbcsr_p_type, &
dbcsr_release, dbcsr_scale, dbcsr_set, dbcsr_set_diag, dbcsr_trace, dbcsr_transposed, &
dbcsr_type, dbcsr_type_no_symmetry
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE input_constants, ONLY: ls_scf_submatrix_sign_direct,&
ls_scf_submatrix_sign_direct_muadj,&
ls_scf_submatrix_sign_direct_muadj_lowmem,&
ls_scf_submatrix_sign_ns
USE kinds, ONLY: dp,&
int_8
USE machine, ONLY: m_flush,&
m_walltime
USE mathconstants, ONLY: ifac
USE mathlib, ONLY: abnormal_value
USE message_passing, ONLY: mp_comm_type
USE submatrix_dissection, ONLY: submatrix_dissection_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'iterate_matrix'
TYPE :: eigbuf
REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: eigvals
REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: eigvecs
END TYPE eigbuf
INTERFACE purify_mcweeny
MODULE PROCEDURE purify_mcweeny_orth, purify_mcweeny_nonorth
END INTERFACE
PUBLIC :: invert_Hotelling, matrix_sign_Newton_Schulz, matrix_sqrt_Newton_Schulz, &
matrix_sqrt_proot, matrix_sign_proot, matrix_sign_submatrix, matrix_exponential, &
matrix_sign_submatrix_mu_adjust, purify_mcweeny, invert_Taylor, determinant
CONTAINS
! *****************************************************************************
!> \brief Computes the determinant of a symmetric positive definite matrix
!> using the trace of the matrix logarithm via Mercator series:
!> det(A) = det(S)det(I+X)det(S), where S=diag(sqrt(Aii),..,sqrt(Ann))
!> det(I+X) = Exp(Trace(Ln(I+X)))
!> Ln(I+X) = X - X^2/2 + X^3/3 - X^4/4 + ..
!> The series converges only if the Frobenius norm of X is less than 1.
!> If it is more than one we compute (recursevily) the determinant of
!> the square root of (I+X).
!> \param matrix ...
!> \param det - determinant
!> \param threshold ...
!> \par History
!> 2015.04 created [Rustam Z Khaliullin]
!> \author Rustam Z. Khaliullin
! **************************************************************************************************
RECURSIVE SUBROUTINE determinant(matrix, det, threshold)
TYPE(dbcsr_type), INTENT(INOUT) :: matrix
REAL(KIND=dp), INTENT(INOUT) :: det
REAL(KIND=dp), INTENT(IN) :: threshold
CHARACTER(LEN=*), PARAMETER :: routineN = 'determinant'
INTEGER :: handle, i, max_iter_lanczos, nsize, &
order_lanczos, sign_iter, unit_nr
INTEGER(KIND=int_8) :: flop1
INTEGER, SAVE :: recursion_depth = 0
REAL(KIND=dp) :: det0, eps_lanczos, frobnorm, maxnorm, &
occ_matrix, t1, t2, trace
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: diagonal
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type) :: tmp1, tmp2, tmp3
CALL timeset(routineN, handle)
! get a useful output_unit
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
unit_nr = -1
END IF
! Note: tmp1 and tmp2 have the same matrix type as the
! initial matrix (tmp3 does not have symmetry constraints)
! this might lead to uninteded results with anti-symmetric
! matrices
CALL dbcsr_create(tmp1, template=matrix, &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(tmp2, template=matrix, &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(tmp3, template=matrix, &
matrix_type=dbcsr_type_no_symmetry)
! compute the product of the diagonal elements
BLOCK
TYPE(mp_comm_type) :: group
INTEGER :: group_handle
CALL dbcsr_get_info(matrix, nfullrows_total=nsize, group=group_handle)
CALL group%set_handle(group_handle)
ALLOCATE (diagonal(nsize))
CALL dbcsr_get_diag(matrix, diagonal)
CALL group%sum(diagonal)
det = PRODUCT(diagonal)
END BLOCK
! create diagonal SQRTI matrix
diagonal(:) = 1.0_dp/(SQRT(diagonal(:)))
!ROLL CALL dbcsr_copy(tmp1,matrix)
CALL dbcsr_desymmetrize(matrix, tmp1)
CALL dbcsr_set(tmp1, 0.0_dp)
CALL dbcsr_set_diag(tmp1, diagonal)
CALL dbcsr_filter(tmp1, threshold)
DEALLOCATE (diagonal)
! normalize the main diagonal, off-diagonal elements are scaled to
! make the norm of the matrix less than 1
CALL dbcsr_multiply("N", "N", 1.0_dp, &
matrix, &
tmp1, &
0.0_dp, tmp3, &
filter_eps=threshold)
CALL dbcsr_multiply("N", "N", 1.0_dp, &
tmp1, &
tmp3, &
0.0_dp, tmp2, &
filter_eps=threshold)
! subtract the main diagonal to create matrix X
CALL dbcsr_add_on_diag(tmp2, -1.0_dp)
frobnorm = dbcsr_frobenius_norm(tmp2)
IF (unit_nr > 0) THEN
IF (recursion_depth .EQ. 0) THEN
WRITE (unit_nr, '()')
ELSE
WRITE (unit_nr, '(T6,A28,1X,I15)') &
"Recursive iteration:", recursion_depth
END IF
WRITE (unit_nr, '(T6,A28,1X,F15.10)') &
"Frobenius norm:", frobnorm
CALL m_flush(unit_nr)
END IF
IF (frobnorm .GE. 1.0_dp) THEN
CALL dbcsr_add_on_diag(tmp2, 1.0_dp)
! these controls should be provided as input
order_lanczos = 3
eps_lanczos = 1.0E-4_dp
max_iter_lanczos = 40
CALL matrix_sqrt_Newton_Schulz( &
tmp3, & ! output sqrt
tmp1, & ! output sqrti
tmp2, & ! input original
threshold=threshold, &
order=order_lanczos, &
eps_lanczos=eps_lanczos, &
max_iter_lanczos=max_iter_lanczos)
recursion_depth = recursion_depth + 1
CALL determinant(tmp3, det0, threshold)
recursion_depth = recursion_depth - 1
det = det*det0*det0
ELSE
! create accumulator
CALL dbcsr_copy(tmp1, tmp2)
! re-create to make use of symmetry
!ROLL CALL dbcsr_create(tmp3,template=matrix)
IF (unit_nr > 0) WRITE (unit_nr, *)
! initialize the sign of the term
sign_iter = -1
DO i = 1, 100
t1 = m_walltime()
! multiply X^i by X
! note that the first iteration evaluates X^2
! because the trace of X^1 is zero by construction
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp1, tmp2, &
0.0_dp, tmp3, &
filter_eps=threshold, &
flop=flop1)
CALL dbcsr_copy(tmp1, tmp3)
! get trace
CALL dbcsr_trace(tmp1, trace)
trace = trace*sign_iter/(1.0_dp*(i + 1))
sign_iter = -sign_iter
! update the determinant
det = det*EXP(trace)
occ_matrix = dbcsr_get_occupation(tmp1)
CALL dbcsr_norm(tmp1, &
dbcsr_norm_maxabsnorm, norm_scalar=maxnorm)
t2 = m_walltime()
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T6,A,1X,I3,1X,F7.5,F16.10,F10.3,F11.3)') &
"Determinant iter", i, occ_matrix, &
det, t2 - t1, &
flop1/(1.0E6_dp*MAX(0.001_dp, t2 - t1))
CALL m_flush(unit_nr)
END IF
! exit if the trace is close to zero
IF (maxnorm < threshold) EXIT
END DO ! end iterations
IF (unit_nr > 0) THEN
WRITE (unit_nr, '()')
CALL m_flush(unit_nr)
END IF
END IF ! decide to do sqrt or not
IF (unit_nr > 0) THEN
IF (recursion_depth .EQ. 0) THEN
WRITE (unit_nr, '(T6,A28,1X,F15.10)') &
"Final determinant:", det
WRITE (unit_nr, '()')
ELSE
WRITE (unit_nr, '(T6,A28,1X,F15.10)') &
"Recursive determinant:", det
END IF
CALL m_flush(unit_nr)
END IF
CALL dbcsr_release(tmp1)
CALL dbcsr_release(tmp2)
CALL dbcsr_release(tmp3)
CALL timestop(handle)
END SUBROUTINE determinant
! **************************************************************************************************
!> \brief invert a symmetric positive definite diagonally dominant matrix
!> \param matrix_inverse ...
!> \param matrix ...
!> \param threshold convergence threshold nased on the max abs
!> \param use_inv_as_guess logical whether input can be used as guess for inverse
!> \param norm_convergence convergence threshold for the 2-norm, useful for approximate solutions
!> \param filter_eps filter_eps for matrix multiplications, if not passed nothing is filteres
!> \param accelerator_order ...
!> \param max_iter_lanczos ...
!> \param eps_lanczos ...
!> \param silent ...
!> \par History
!> 2010.10 created [Joost VandeVondele]
!> 2011.10 guess option added [Rustam Z Khaliullin]
!> \author Joost VandeVondele
! **************************************************************************************************
SUBROUTINE invert_Taylor(matrix_inverse, matrix, threshold, use_inv_as_guess, &
norm_convergence, filter_eps, accelerator_order, &
max_iter_lanczos, eps_lanczos, silent)
TYPE(dbcsr_type), INTENT(INOUT), TARGET :: matrix_inverse, matrix
REAL(KIND=dp), INTENT(IN) :: threshold
LOGICAL, INTENT(IN), OPTIONAL :: use_inv_as_guess
REAL(KIND=dp), INTENT(IN), OPTIONAL :: norm_convergence, filter_eps
INTEGER, INTENT(IN), OPTIONAL :: accelerator_order, max_iter_lanczos
REAL(KIND=dp), INTENT(IN), OPTIONAL :: eps_lanczos
LOGICAL, INTENT(IN), OPTIONAL :: silent
CHARACTER(LEN=*), PARAMETER :: routineN = 'invert_Taylor'
INTEGER :: accelerator_type, handle, i, &
my_max_iter_lanczos, nrows, unit_nr
INTEGER(KIND=int_8) :: flop2
LOGICAL :: converged, use_inv_guess
REAL(KIND=dp) :: coeff, convergence, maxnorm_matrix, &
my_eps_lanczos, occ_matrix, t1, t2
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: p_diagonal
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type), TARGET :: tmp1, tmp2, tmp3_sym
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
unit_nr = -1
END IF
IF (PRESENT(silent)) THEN
IF (silent) unit_nr = -1
END IF
convergence = threshold
IF (PRESENT(norm_convergence)) convergence = norm_convergence
accelerator_type = 0
IF (PRESENT(accelerator_order)) accelerator_type = accelerator_order
IF (accelerator_type .GT. 1) accelerator_type = 1
use_inv_guess = .FALSE.
IF (PRESENT(use_inv_as_guess)) use_inv_guess = use_inv_as_guess
my_max_iter_lanczos = 64
my_eps_lanczos = 1.0E-3_dp
IF (PRESENT(max_iter_lanczos)) my_max_iter_lanczos = max_iter_lanczos
IF (PRESENT(eps_lanczos)) my_eps_lanczos = eps_lanczos
CALL dbcsr_create(tmp1, template=matrix_inverse, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(tmp2, template=matrix_inverse, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(tmp3_sym, template=matrix_inverse)
CALL dbcsr_get_info(matrix, nfullrows_total=nrows)
ALLOCATE (p_diagonal(nrows))
! generate the initial guess
IF (.NOT. use_inv_guess) THEN
SELECT CASE (accelerator_type)
CASE (0)
! use tmp1 to hold off-diagonal elements
CALL dbcsr_desymmetrize(matrix, tmp1)
p_diagonal(:) = 0.0_dp
CALL dbcsr_set_diag(tmp1, p_diagonal)
!CALL dbcsr_print(tmp1)
! invert the main diagonal
CALL dbcsr_get_diag(matrix, p_diagonal)
DO i = 1, nrows
IF (p_diagonal(i) .NE. 0.0_dp) THEN
p_diagonal(i) = 1.0_dp/p_diagonal(i)
END IF
END DO
CALL dbcsr_set(matrix_inverse, 0.0_dp)
CALL dbcsr_add_on_diag(matrix_inverse, 1.0_dp)
CALL dbcsr_set_diag(matrix_inverse, p_diagonal)
CASE DEFAULT
CPABORT("Illegal accelerator order")
END SELECT
ELSE
CPABORT("Guess is NYI")
END IF
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp1, matrix_inverse, &
0.0_dp, tmp2, filter_eps=filter_eps)
IF (unit_nr > 0) WRITE (unit_nr, *)
! scale the approximate inverse to be within the convergence radius
t1 = m_walltime()
! done with the initial guess, start iterations
converged = .FALSE.
CALL dbcsr_desymmetrize(matrix_inverse, tmp1)
coeff = 1.0_dp
DO i = 1, 100
! coeff = +/- 1
coeff = -1.0_dp*coeff
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp1, tmp2, 0.0_dp, &
tmp3_sym, &
flop=flop2, filter_eps=filter_eps)
!flop=flop2)
CALL dbcsr_add(matrix_inverse, tmp3_sym, 1.0_dp, coeff)
CALL dbcsr_release(tmp1)
CALL dbcsr_create(tmp1, template=matrix_inverse, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_desymmetrize(tmp3_sym, tmp1)
! for the convergence check
CALL dbcsr_norm(tmp3_sym, &
dbcsr_norm_maxabsnorm, norm_scalar=maxnorm_matrix)
t2 = m_walltime()
occ_matrix = dbcsr_get_occupation(matrix_inverse)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T6,A,1X,I3,1X,F10.8,E12.3,F12.3,F13.3)') "Taylor iter", i, occ_matrix, &
maxnorm_matrix, t2 - t1, &
flop2/(1.0E6_dp*MAX(0.001_dp, t2 - t1))
CALL m_flush(unit_nr)
END IF
IF (maxnorm_matrix < convergence) THEN
converged = .TRUE.
EXIT
END IF
t1 = m_walltime()
END DO
!last convergence check
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix, matrix_inverse, 0.0_dp, tmp1, &
filter_eps=filter_eps)
CALL dbcsr_add_on_diag(tmp1, -1.0_dp)
!frob_matrix = dbcsr_frobenius_norm(tmp1)
CALL dbcsr_norm(tmp1, dbcsr_norm_maxabsnorm, norm_scalar=maxnorm_matrix)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T6,A,E12.5)') "Final Taylor error", maxnorm_matrix
WRITE (unit_nr, '()')
CALL m_flush(unit_nr)
END IF
IF (maxnorm_matrix > convergence) THEN
converged = .FALSE.
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) 'Final convergence check failed'
END IF
END IF
IF (.NOT. converged) THEN
CPABORT("Taylor inversion did not converge")
END IF
CALL dbcsr_release(tmp1)
CALL dbcsr_release(tmp2)
CALL dbcsr_release(tmp3_sym)
DEALLOCATE (p_diagonal)
CALL timestop(handle)
END SUBROUTINE invert_Taylor
! **************************************************************************************************
!> \brief invert a symmetric positive definite matrix by Hotelling's method
!> explicit symmetrization makes this code not suitable for other matrix types
!> Currently a bit messy with the options, to to be cleaned soon
!> \param matrix_inverse ...
!> \param matrix ...
!> \param threshold convergence threshold nased on the max abs
!> \param use_inv_as_guess logical whether input can be used as guess for inverse
!> \param norm_convergence convergence threshold for the 2-norm, useful for approximate solutions
!> \param filter_eps filter_eps for matrix multiplications, if not passed nothing is filteres
!> \param accelerator_order ...
!> \param max_iter_lanczos ...
!> \param eps_lanczos ...
!> \param silent ...
!> \par History
!> 2010.10 created [Joost VandeVondele]
!> 2011.10 guess option added [Rustam Z Khaliullin]
!> \author Joost VandeVondele
! **************************************************************************************************
SUBROUTINE invert_Hotelling(matrix_inverse, matrix, threshold, use_inv_as_guess, &
norm_convergence, filter_eps, accelerator_order, &
max_iter_lanczos, eps_lanczos, silent)
TYPE(dbcsr_type), INTENT(INOUT), TARGET :: matrix_inverse, matrix
REAL(KIND=dp), INTENT(IN) :: threshold
LOGICAL, INTENT(IN), OPTIONAL :: use_inv_as_guess
REAL(KIND=dp), INTENT(IN), OPTIONAL :: norm_convergence, filter_eps
INTEGER, INTENT(IN), OPTIONAL :: accelerator_order, max_iter_lanczos
REAL(KIND=dp), INTENT(IN), OPTIONAL :: eps_lanczos
LOGICAL, INTENT(IN), OPTIONAL :: silent
CHARACTER(LEN=*), PARAMETER :: routineN = 'invert_Hotelling'
INTEGER :: accelerator_type, handle, i, &
my_max_iter_lanczos, unit_nr
INTEGER(KIND=int_8) :: flop1, flop2
LOGICAL :: arnoldi_converged, converged, &
use_inv_guess
REAL(KIND=dp) :: convergence, frob_matrix, gershgorin_norm, max_ev, maxnorm_matrix, min_ev, &
my_eps_lanczos, my_filter_eps, occ_matrix, scalingf, t1, t2
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type), TARGET :: tmp1, tmp2
!TYPE(arnoldi_data_type) :: my_arnoldi
!TYPE(dbcsr_p_type), DIMENSION(1) :: mymat
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
unit_nr = -1
END IF
IF (PRESENT(silent)) THEN
IF (silent) unit_nr = -1
END IF
convergence = threshold
IF (PRESENT(norm_convergence)) convergence = norm_convergence
accelerator_type = 1
IF (PRESENT(accelerator_order)) accelerator_type = accelerator_order
IF (accelerator_type .GT. 1) accelerator_type = 1
use_inv_guess = .FALSE.
IF (PRESENT(use_inv_as_guess)) use_inv_guess = use_inv_as_guess
my_max_iter_lanczos = 64
my_eps_lanczos = 1.0E-3_dp
IF (PRESENT(max_iter_lanczos)) my_max_iter_lanczos = max_iter_lanczos
IF (PRESENT(eps_lanczos)) my_eps_lanczos = eps_lanczos
my_filter_eps = threshold
IF (PRESENT(filter_eps)) my_filter_eps = filter_eps
! generate the initial guess
IF (.NOT. use_inv_guess) THEN
SELECT CASE (accelerator_type)
CASE (0)
gershgorin_norm = dbcsr_gershgorin_norm(matrix)
frob_matrix = dbcsr_frobenius_norm(matrix)
CALL dbcsr_set(matrix_inverse, 0.0_dp)
CALL dbcsr_add_on_diag(matrix_inverse, 1/MIN(gershgorin_norm, frob_matrix))
CASE (1)
! initialize matrix to unity and use arnoldi (below) to scale it into the convergence range
CALL dbcsr_set(matrix_inverse, 0.0_dp)
CALL dbcsr_add_on_diag(matrix_inverse, 1.0_dp)
CASE DEFAULT
CPABORT("Illegal accelerator order")
END SELECT
! everything commutes, therefore our all products will be symmetric
CALL dbcsr_create(tmp1, template=matrix_inverse)
ELSE
! It is unlikely that our guess will commute with the matrix, therefore the first product will
! be non symmetric
CALL dbcsr_create(tmp1, template=matrix_inverse, matrix_type=dbcsr_type_no_symmetry)
END IF
CALL dbcsr_create(tmp2, template=matrix_inverse)
IF (unit_nr > 0) WRITE (unit_nr, *)
! scale the approximate inverse to be within the convergence radius
t1 = m_walltime()
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_inverse, matrix, &
0.0_dp, tmp1, flop=flop1, filter_eps=my_filter_eps)
IF (accelerator_type == 1) THEN
! scale the matrix to get into the convergence range
CALL arnoldi_extremal(tmp1, max_eV, min_eV, threshold=my_eps_lanczos, &
max_iter=my_max_iter_lanczos, converged=arnoldi_converged)
!mymat(1)%matrix => tmp1
!CALL setup_arnoldi_data(my_arnoldi, mymat, max_iter=30, threshold=1.0E-3_dp, selection_crit=1, &
! nval_request=2, nrestarts=2, generalized_ev=.FALSE., iram=.TRUE.)
!CALL arnoldi_ev(mymat, my_arnoldi)
!max_eV = REAL(get_selected_ritz_val(my_arnoldi, 2), dp)
!min_eV = REAL(get_selected_ritz_val(my_arnoldi, 1), dp)
!CALL deallocate_arnoldi_data(my_arnoldi)
IF (unit_nr > 0) THEN
WRITE (unit_nr, *)
WRITE (unit_nr, '(T6,A,1X,L1,A,E12.3)') "Lanczos converged: ", arnoldi_converged, " threshold:", my_eps_lanczos
WRITE (unit_nr, '(T6,A,1X,E12.3,E12.3)') "Est. extremal eigenvalues:", max_eV, min_eV
WRITE (unit_nr, '(T6,A,1X,E12.3)') "Est. condition number :", max_eV/MAX(min_eV, EPSILON(min_eV))
END IF
! 2.0 would be the correct scaling however, we should make sure here, that we are in the convergence radius
scalingf = 1.9_dp/(max_eV + min_eV)
CALL dbcsr_scale(tmp1, scalingf)
CALL dbcsr_scale(matrix_inverse, scalingf)
min_ev = min_ev*scalingf
END IF
! done with the initial guess, start iterations
converged = .FALSE.
DO i = 1, 100
! tmp1 = S^-1 S
! for the convergence check
CALL dbcsr_add_on_diag(tmp1, -1.0_dp)
CALL dbcsr_norm(tmp1, &
dbcsr_norm_maxabsnorm, norm_scalar=maxnorm_matrix)
CALL dbcsr_add_on_diag(tmp1, +1.0_dp)
! tmp2 = S^-1 S S^-1
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp1, matrix_inverse, 0.0_dp, tmp2, &
flop=flop2, filter_eps=my_filter_eps)
! S^-1_{n+1} = 2 S^-1 - S^-1 S S^-1
CALL dbcsr_add(matrix_inverse, tmp2, 2.0_dp, -1.0_dp)
CALL dbcsr_filter(matrix_inverse, my_filter_eps)
t2 = m_walltime()
occ_matrix = dbcsr_get_occupation(matrix_inverse)
! use the scalar form of the algorithm to trace the EV
IF (accelerator_type == 1) THEN
min_ev = min_ev*(2.0_dp - min_ev)
IF (PRESENT(norm_convergence)) maxnorm_matrix = ABS(min_eV - 1.0_dp)
END IF
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T6,A,1X,I3,1X,F10.8,E12.3,F12.3,F13.3)') "Hotelling iter", i, occ_matrix, &
maxnorm_matrix, t2 - t1, &
(flop1 + flop2)/(1.0E6_dp*MAX(0.001_dp, t2 - t1))
CALL m_flush(unit_nr)
END IF
IF (maxnorm_matrix < convergence) THEN
converged = .TRUE.
EXIT
END IF
! scale the matrix for improved convergence
IF (accelerator_type == 1) THEN
min_ev = min_ev*2.0_dp/(min_ev + 1.0_dp)
CALL dbcsr_scale(matrix_inverse, 2.0_dp/(min_ev + 1.0_dp))
END IF
t1 = m_walltime()
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_inverse, matrix, &
0.0_dp, tmp1, flop=flop1, filter_eps=my_filter_eps)
END DO
IF (.NOT. converged) THEN
CPABORT("Hotelling inversion did not converge")
END IF
! try to symmetrize the output matrix
IF (dbcsr_get_matrix_type(matrix_inverse) == dbcsr_type_no_symmetry) THEN
CALL dbcsr_transposed(tmp2, matrix_inverse)
CALL dbcsr_add(matrix_inverse, tmp2, 0.5_dp, 0.5_dp)
END IF
IF (unit_nr > 0) THEN
! WRITE(unit_nr,'(T6,A,1X,I3,1X,F10.8,E12.3)') "Final Hotelling ",i,occ_matrix,&
! !frob_matrix/frob_matrix_base
! maxnorm_matrix
WRITE (unit_nr, '()')
CALL m_flush(unit_nr)
END IF
CALL dbcsr_release(tmp1)
CALL dbcsr_release(tmp2)
CALL timestop(handle)
END SUBROUTINE invert_Hotelling
! **************************************************************************************************
!> \brief compute the sign a matrix using Newton-Schulz iterations
!> \param matrix_sign ...
!> \param matrix ...
!> \param threshold ...
!> \param sign_order ...
!> \param iounit ...
!> \par History
!> 2010.10 created [Joost VandeVondele]
!> 2019.05 extended to order byxond 2 [Robert Schade]
!> \author Joost VandeVondele, Robert Schade
! **************************************************************************************************
SUBROUTINE matrix_sign_Newton_Schulz(matrix_sign, matrix, threshold, sign_order, iounit)
TYPE(dbcsr_type), INTENT(INOUT) :: matrix_sign, matrix
REAL(KIND=dp), INTENT(IN) :: threshold
INTEGER, INTENT(IN), OPTIONAL :: sign_order, iounit
CHARACTER(LEN=*), PARAMETER :: routineN = 'matrix_sign_Newton_Schulz'
INTEGER :: count, handle, i, order, unit_nr
INTEGER(KIND=int_8) :: flops
REAL(KIND=dp) :: a0, a1, a2, a3, a4, a5, floptot, &
frob_matrix, frob_matrix_base, &
gersh_matrix, occ_matrix, prefactor, &
t1, t2
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type) :: tmp1, tmp2, tmp3, tmp4
CALL timeset(routineN, handle)
IF (PRESENT(iounit)) THEN
unit_nr = iounit
ELSE
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
unit_nr = -1
END IF
END IF
IF (PRESENT(sign_order)) THEN
order = sign_order
ELSE
order = 2
END IF
CALL dbcsr_create(tmp1, template=matrix_sign)
CALL dbcsr_create(tmp2, template=matrix_sign)
IF (ABS(order) .GE. 4) THEN
CALL dbcsr_create(tmp3, template=matrix_sign)
END IF
IF (ABS(order) .GT. 4) THEN
CALL dbcsr_create(tmp4, template=matrix_sign)
END IF
CALL dbcsr_copy(matrix_sign, matrix)
CALL dbcsr_filter(matrix_sign, threshold)
! scale the matrix to get into the convergence range
frob_matrix = dbcsr_frobenius_norm(matrix_sign)
gersh_matrix = dbcsr_gershgorin_norm(matrix_sign)
CALL dbcsr_scale(matrix_sign, 1/MIN(frob_matrix, gersh_matrix))
IF (unit_nr > 0) WRITE (unit_nr, *)
count = 0
DO i = 1, 100
floptot = 0_dp
t1 = m_walltime()
! tmp1 = X * X
CALL dbcsr_multiply("N", "N", -1.0_dp, matrix_sign, matrix_sign, 0.0_dp, tmp1, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
! check convergence (frob norm of what should be the identity matrix minus identity matrix)
frob_matrix_base = dbcsr_frobenius_norm(tmp1)
CALL dbcsr_add_on_diag(tmp1, +1.0_dp)
frob_matrix = dbcsr_frobenius_norm(tmp1)
! f(y) approx 1/sqrt(1-y)
! f(y)=1+y/2+3/8*y^2+5/16*y^3+35/128*y^4+63/256*y^5+231/1024*y^6
! f2(y)=1+y/2=1/2*(2+y)
! f3(y)=1+y/2+3/8*y^2=3/8*(8/3+4/3*y+y^2)
! f4(y)=1+y/2+3/8*y^2+5/16*y^3=5/16*(16/5+8/5*y+6/5*y^2+y^3)
! f5(y)=1+y/2+3/8*y^2+5/16*y^3+35/128*y^4=35/128*(128/35+128/70*y+48/35*y^2+8/7*y^3+y^4)
! z(y)=(y+a_0)*y+a_1
! f5(y)=35/128*((z(y)+y+a_2)*z(y)+a_3)
! =35/128*((a_1^2+a_1a_2+a_3)+(2*a_0a_1+a_1+a_0a_2)y+(a_0^2+a_0+2a_1+a_2)y^2+(2a_0+1)y^3+y^4)
! a_0=1/14
! a_1=23819/13720
! a_2=1269/980-2a_1=-3734/1715
! a_3=832591127/188238400
! f6(y)=1+y/2+3/8*y^2+5/16*y^3+35/128*y^4+63/256*y^5
! =63/256*(256/63 + (128 y)/63 + (32 y^2)/21 + (80 y^3)/63 + (10 y^4)/9 + y^5)
! f7(y)=1+y/2+3/8*y^2+5/16*y^3+35/128*y^4+63/256*y^5+231/1024*y^6
! =231/1024*(1024/231+512/231*y+128/77*y^2+320/231*y^3+40/33*y^4+12/11*y^5+y^6)
! z(y)=(y+a_0)*y+a_1
! w(y)=(y+a_2)*z(y)+a_3
! f7(y)=(w(y)+z(y)+a_4)*w(y)+a_5
! a_0= 1.3686502058092053653287666647611728507211996691324048468010382350359929055186612505791532871573242422
! a_1= 1.7089671854477436685850554669524985556296280184497503489303331821456795715195510972774979091893741568
! a_2=-1.3231956603546599107833121193066273961757451236778593922555836895814474509732067051246078326118696968
! a_3= 3.9876642330847931291749479958277754186675336169578593000744380254770411483327581042259415937710270453
! a_4=-3.7273299006476825027065704937541279833880400042556351139273912137942678919776364526511485025132991667
! a_5= 4.9369932474103023792021351907971943220607580694533770325967170245194362399287150565595441897740173578
!
! y=1-X*X
! tmp1 = I-x*x
IF (order .EQ. 2) THEN
prefactor = 0.5_dp
! update the above to 3*I-X*X
CALL dbcsr_add_on_diag(tmp1, +2.0_dp)
occ_matrix = dbcsr_get_occupation(matrix_sign)
ELSE IF (order .EQ. 3) THEN
! with one multiplication
! tmp1=y
CALL dbcsr_copy(tmp2, tmp1)
CALL dbcsr_scale(tmp1, 4.0_dp/3.0_dp)
CALL dbcsr_add_on_diag(tmp1, 8.0_dp/3.0_dp)
! tmp2=y^2
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp2, tmp2, 1.0_dp, tmp1, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
prefactor = 3.0_dp/8.0_dp
ELSE IF (order .EQ. 4) THEN
! with two multiplications
! tmp1=y
CALL dbcsr_copy(tmp3, tmp1)
CALL dbcsr_scale(tmp1, 8.0_dp/5.0_dp)
CALL dbcsr_add_on_diag(tmp1, 16.0_dp/5.0_dp)
!
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp3, tmp3, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add(tmp1, tmp2, 1.0_dp, 6.0_dp/5.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp2, tmp3, 1.0_dp, tmp1, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
prefactor = 5.0_dp/16.0_dp
ELSE IF (order .EQ. -5) THEN
! with three multiplications
! tmp1=y
CALL dbcsr_copy(tmp3, tmp1)
CALL dbcsr_scale(tmp1, 128.0_dp/70.0_dp)
CALL dbcsr_add_on_diag(tmp1, 128.0_dp/35.0_dp)
!
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp3, tmp3, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add(tmp1, tmp2, 1.0_dp, 48.0_dp/35.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp2, tmp3, 0.0_dp, tmp4, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add(tmp1, tmp4, 1.0_dp, 8.0_dp/7.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp4, tmp3, 1.0_dp, tmp1, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
prefactor = 35.0_dp/128.0_dp
ELSE IF (order .EQ. 5) THEN
! with two multiplications
! z(y)=(y+a_0)*y+a_1
! f5(y)=35/128*((z(y)+y+a_2)*z(y)+a_3)
! =35/128*((a_1^2+a_1a_2+a_3)+(2*a_0a_1+a_1+a_0a_2)y+(a_0^2+a_0+2a_1+a_2)y^2+(2a_0+1)y^3+y^4)
! a_0=1/14
! a_1=23819/13720
! a_2=1269/980-2a_1=-3734/1715
! a_3=832591127/188238400
a0 = 1.0_dp/14.0_dp
a1 = 23819.0_dp/13720.0_dp
a2 = -3734_dp/1715.0_dp
a3 = 832591127_dp/188238400.0_dp
! tmp1=y
! tmp3=z
CALL dbcsr_copy(tmp3, tmp1)
CALL dbcsr_add_on_diag(tmp3, a0)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp3, tmp1, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add_on_diag(tmp2, a1)
CALL dbcsr_add_on_diag(tmp1, a2)
CALL dbcsr_add(tmp1, tmp2, 1.0_dp, 1.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp1, tmp2, 0.0_dp, tmp3, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add_on_diag(tmp3, a3)
CALL dbcsr_copy(tmp1, tmp3)
prefactor = 35.0_dp/128.0_dp
ELSE IF (order .EQ. 6) THEN
! with four multiplications
! f6(y)=63/256*(256/63 + (128 y)/63 + (32 y^2)/21 + (80 y^3)/63 + (10 y^4)/9 + y^5)
! tmp1=y
CALL dbcsr_copy(tmp3, tmp1)
CALL dbcsr_scale(tmp1, 128.0_dp/63.0_dp)
CALL dbcsr_add_on_diag(tmp1, 256.0_dp/63.0_dp)
!
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp3, tmp3, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add(tmp1, tmp2, 1.0_dp, 32.0_dp/21.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp2, tmp3, 0.0_dp, tmp4, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add(tmp1, tmp4, 1.0_dp, 80.0_dp/63.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp4, tmp3, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add(tmp1, tmp2, 1.0_dp, 10.0_dp/9.0_dp)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp2, tmp3, 1.0_dp, tmp1, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
prefactor = 63.0_dp/256.0_dp
ELSE IF (order .EQ. 7) THEN
! with three multiplications
a0 = 1.3686502058092053653287666647611728507211996691324048468010382350359929055186612505791532871573242422_dp
a1 = 1.7089671854477436685850554669524985556296280184497503489303331821456795715195510972774979091893741568_dp
a2 = -1.3231956603546599107833121193066273961757451236778593922555836895814474509732067051246078326118696968_dp
a3 = 3.9876642330847931291749479958277754186675336169578593000744380254770411483327581042259415937710270453_dp
a4 = -3.7273299006476825027065704937541279833880400042556351139273912137942678919776364526511485025132991667_dp
a5 = 4.9369932474103023792021351907971943220607580694533770325967170245194362399287150565595441897740173578_dp
! =231/1024*(1024/231+512/231*y+128/77*y^2+320/231*y^3+40/33*y^4+12/11*y^5+y^6)
! z(y)=(y+a_0)*y+a_1
! w(y)=(y+a_2)*z(y)+a_3
! f7(y)=(w(y)+z(y)+a_4)*w(y)+a_5
! tmp1=y
! tmp3=z
CALL dbcsr_copy(tmp3, tmp1)
CALL dbcsr_add_on_diag(tmp3, a0)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp3, tmp1, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add_on_diag(tmp2, a1)
! tmp4=w
CALL dbcsr_copy(tmp4, tmp1)
CALL dbcsr_add_on_diag(tmp4, a2)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp4, tmp2, 0.0_dp, tmp3, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add_on_diag(tmp3, a3)
CALL dbcsr_add(tmp2, tmp3, 1.0_dp, 1.0_dp)
CALL dbcsr_add_on_diag(tmp2, a4)
CALL dbcsr_multiply("N", "N", 1.0_dp, tmp2, tmp3, 0.0_dp, tmp1, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
CALL dbcsr_add_on_diag(tmp1, a5)
prefactor = 231.0_dp/1024.0_dp
ELSE
CPABORT("requested order is not implemented.")
END IF
! tmp2 = X * prefactor *
CALL dbcsr_multiply("N", "N", prefactor, matrix_sign, tmp1, 0.0_dp, tmp2, &
filter_eps=threshold, flop=flops)
floptot = floptot + flops
! done iterating
! CALL dbcsr_filter(tmp2,threshold)
CALL dbcsr_copy(matrix_sign, tmp2)
t2 = m_walltime()
occ_matrix = dbcsr_get_occupation(matrix_sign)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T6,A,1X,I3,1X,F10.8,E12.3,F12.3,F13.3)') "NS sign iter ", i, occ_matrix, &
frob_matrix/frob_matrix_base, t2 - t1, &
floptot/(1.0E6_dp*MAX(0.001_dp, t2 - t1))
CALL m_flush(unit_nr)
END IF
! frob_matrix/frob_matrix_base < SQRT(threshold)
IF (frob_matrix*frob_matrix < (threshold*frob_matrix_base*frob_matrix_base)) EXIT
END DO
! this check is not really needed
CALL dbcsr_multiply("N", "N", +1.0_dp, matrix_sign, matrix_sign, 0.0_dp, tmp1, &
filter_eps=threshold)
frob_matrix_base = dbcsr_frobenius_norm(tmp1)
CALL dbcsr_add_on_diag(tmp1, -1.0_dp)
frob_matrix = dbcsr_frobenius_norm(tmp1)
occ_matrix = dbcsr_get_occupation(matrix_sign)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T6,A,1X,I3,1X,F10.8,E12.3)') "Final NS sign iter", i, occ_matrix, &
frob_matrix/frob_matrix_base
WRITE (unit_nr, '()')
CALL m_flush(unit_nr)
END IF
CALL dbcsr_release(tmp1)
CALL dbcsr_release(tmp2)
IF (ABS(order) .GE. 4) THEN
CALL dbcsr_release(tmp3)
END IF
IF (ABS(order) .GT. 4) THEN
CALL dbcsr_release(tmp4)
END IF
CALL timestop(handle)
END SUBROUTINE matrix_sign_Newton_Schulz