-
Notifications
You must be signed in to change notification settings - Fork 1
/
almo_scf_optimizer.F
11074 lines (9585 loc) · 485 KB
/
almo_scf_optimizer.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 Optimization routines for all ALMO-based SCF methods
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> 2014.10 as a separate file [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
MODULE almo_scf_optimizer
USE almo_scf_diis_types, ONLY: almo_scf_diis_extrapolate,&
almo_scf_diis_init,&
almo_scf_diis_push,&
almo_scf_diis_release,&
almo_scf_diis_type
USE almo_scf_lbfgs_types, ONLY: lbfgs_create,&
lbfgs_get_direction,&
lbfgs_history_type,&
lbfgs_release,&
lbfgs_seed
USE almo_scf_methods, ONLY: &
almo_scf_ks_blk_to_tv_blk, almo_scf_ks_to_ks_blk, almo_scf_ks_to_ks_xx, &
almo_scf_ks_xx_to_tv_xx, almo_scf_p_blk_to_t_blk, almo_scf_t_rescaling, &
almo_scf_t_to_proj, apply_domain_operators, apply_projector, &
construct_domain_preconditioner, construct_domain_r_down, construct_domain_s_inv, &
construct_domain_s_sqrt, fill_matrix_with_ones, get_overlap, orthogonalize_mos, &
pseudo_invert_diagonal_blk, xalmo_initial_guess
USE almo_scf_qs, ONLY: almo_dm_to_almo_ks,&
almo_dm_to_qs_env,&
almo_scf_update_ks_energy,&
matrix_qs_to_almo
USE almo_scf_types, ONLY: almo_scf_env_type,&
optimizer_options_type
USE cell_types, ONLY: cell_type
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_add_on_diag, dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, &
dbcsr_distribution_get, dbcsr_distribution_type, dbcsr_dot, dbcsr_filter, dbcsr_finalize, &
dbcsr_frobenius_norm, dbcsr_func_dtanh, dbcsr_func_inverse, dbcsr_func_tanh, &
dbcsr_function_of_elements, dbcsr_get_block_p, dbcsr_get_diag, dbcsr_get_info, &
dbcsr_hadamard_product, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, &
dbcsr_nblkcols_total, dbcsr_nblkrows_total, dbcsr_norm, dbcsr_norm_maxabsnorm, &
dbcsr_p_type, dbcsr_print_block_sum, dbcsr_release, dbcsr_reserve_block2d, dbcsr_scale, &
dbcsr_set, dbcsr_set_diag, dbcsr_triu, dbcsr_type, dbcsr_type_no_symmetry, &
dbcsr_work_create
USE cp_dbcsr_cholesky, ONLY: cp_dbcsr_cholesky_decompose,&
cp_dbcsr_cholesky_invert,&
cp_dbcsr_cholesky_restore
USE cp_external_control, ONLY: external_control
USE cp_files, ONLY: close_file,&
open_file
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type,&
cp_to_string
USE cp_output_handling, ONLY: cp_print_key_finished_output,&
cp_print_key_unit_nr
USE ct_methods, ONLY: analytic_line_search,&
ct_step_execute,&
diagonalize_diagonal_blocks
USE ct_types, ONLY: ct_step_env_clean,&
ct_step_env_get,&
ct_step_env_init,&
ct_step_env_set,&
ct_step_env_type
USE domain_submatrix_methods, ONLY: add_submatrices,&
construct_submatrices,&
copy_submatrices,&
init_submatrices,&
maxnorm_submatrices,&
release_submatrices
USE domain_submatrix_types, ONLY: domain_map_type,&
domain_submatrix_type,&
select_row
USE input_constants, ONLY: &
almo_scf_diag, almo_scf_dm_sign, cg_dai_yuan, cg_fletcher, cg_fletcher_reeves, &
cg_hager_zhang, cg_hestenes_stiefel, cg_liu_storey, cg_polak_ribiere, cg_zero, &
op_loc_berry, op_loc_pipek, trustr_cauchy, trustr_dogleg, virt_full, &
xalmo_case_block_diag, xalmo_case_fully_deloc, xalmo_case_normal, xalmo_prec_domain, &
xalmo_prec_full, xalmo_prec_zero
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type
USE iterate_matrix, ONLY: determinant,&
invert_Hotelling,&
matrix_sqrt_Newton_Schulz
USE kinds, ONLY: dp
USE machine, ONLY: m_flush,&
m_walltime
USE message_passing, ONLY: mp_comm_type,&
mp_para_env_type
USE particle_methods, ONLY: get_particle_set
USE particle_types, ONLY: particle_type
USE qs_energy_types, ONLY: qs_energy_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_kind_types, ONLY: qs_kind_type
USE qs_loc_utils, ONLY: compute_berry_operator
USE qs_localization_methods, ONLY: initialize_weights
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'almo_scf_optimizer'
PUBLIC :: almo_scf_block_diagonal, &
almo_scf_xalmo_eigensolver, &
almo_scf_xalmo_trustr, &
almo_scf_xalmo_pcg, &
almo_scf_construct_nlmos
LOGICAL, PARAMETER :: debug_mode = .FALSE.
LOGICAL, PARAMETER :: safe_mode = .FALSE.
LOGICAL, PARAMETER :: almo_mathematica = .FALSE.
INTEGER, PARAMETER :: hessian_path_reuse = 1, &
hessian_path_assemble = 2
CONTAINS
! **************************************************************************************************
!> \brief An SCF procedure that optimizes block-diagonal ALMOs using DIIS
!> \param qs_env ...
!> \param almo_scf_env ...
!> \param optimizer ...
!> \par History
!> 2011.06 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_block_diagonal(qs_env, almo_scf_env, optimizer)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
TYPE(optimizer_options_type), INTENT(IN) :: optimizer
CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_block_diagonal'
INTEGER :: handle, iscf, ispin, nspin, unit_nr
INTEGER, ALLOCATABLE, DIMENSION(:) :: local_nocc_of_domain
LOGICAL :: converged, prepare_to_exit, should_stop, &
use_diis, use_prev_as_guess
REAL(KIND=dp) :: density_rec, energy_diff, energy_new, energy_old, error_norm, &
error_norm_ispin, kTS_sum, prev_error_norm, t1, t2, true_mixing_fraction
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: local_mu
TYPE(almo_scf_diis_type), ALLOCATABLE, &
DIMENSION(:) :: almo_diis
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: matrix_mixing_old_blk
TYPE(qs_energy_type), POINTER :: qs_energy
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
! use DIIS, it's superior to simple mixing
use_diis = .TRUE.
use_prev_as_guess = .FALSE.
nspin = almo_scf_env%nspins
ALLOCATE (local_mu(almo_scf_env%ndomains))
ALLOCATE (local_nocc_of_domain(almo_scf_env%ndomains))
! init mixing matrices
ALLOCATE (matrix_mixing_old_blk(nspin))
ALLOCATE (almo_diis(nspin))
DO ispin = 1, nspin
CALL dbcsr_create(matrix_mixing_old_blk(ispin), &
template=almo_scf_env%matrix_ks_blk(ispin))
CALL almo_scf_diis_init(diis_env=almo_diis(ispin), &
sample_err=almo_scf_env%matrix_ks_blk(ispin), &
sample_var=almo_scf_env%matrix_s_blk(1), &
error_type=1, &
max_length=optimizer%ndiis)
END DO
CALL get_qs_env(qs_env, energy=qs_energy)
energy_old = qs_energy%total
iscf = 0
prepare_to_exit = .FALSE.
true_mixing_fraction = 0.0_dp
error_norm = 1.0E+10_dp ! arbitrary big step
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A,A,A)') REPEAT("-", 20), &
" Optimization of block-diagonal ALMOs ", REPEAT("-", 21)
WRITE (unit_nr, *)
WRITE (unit_nr, '(T2,A13,A6,A23,A14,A14,A9)') "Method", "Iter", &
"Total Energy", "Change", "Convergence", "Time"
WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
END IF
! the real SCF loop
t1 = m_walltime()
DO
iscf = iscf + 1
! obtain projected KS matrix and the DIIS-error vector
CALL almo_scf_ks_to_ks_blk(almo_scf_env)
! inform the DIIS handler about the new KS matrix and its error vector
IF (use_diis) THEN
DO ispin = 1, nspin
CALL almo_scf_diis_push(diis_env=almo_diis(ispin), &
var=almo_scf_env%matrix_ks_blk(ispin), &
err=almo_scf_env%matrix_err_blk(ispin))
END DO
END IF
! get error_norm: choose the largest of the two spins
prev_error_norm = error_norm
DO ispin = 1, nspin
!error_norm=dbcsr_frobenius_norm(almo_scf_env%matrix_err_blk(ispin))
CALL dbcsr_norm(almo_scf_env%matrix_err_blk(ispin), &
dbcsr_norm_maxabsnorm, &
norm_scalar=error_norm_ispin)
IF (ispin .EQ. 1) error_norm = error_norm_ispin
IF (ispin .GT. 1 .AND. error_norm_ispin .GT. error_norm) &
error_norm = error_norm_ispin
END DO
IF (error_norm .LT. almo_scf_env%eps_prev_guess) THEN
use_prev_as_guess = .TRUE.
ELSE
use_prev_as_guess = .FALSE.
END IF
! check convergence
converged = .TRUE.
IF (error_norm .GT. optimizer%eps_error) converged = .FALSE.
! check other exit criteria: max SCF steps and timing
CALL external_control(should_stop, "SCF", &
start_time=qs_env%start_time, &
target_time=qs_env%target_time)
IF (should_stop .OR. iscf >= optimizer%max_iter .OR. converged) THEN
prepare_to_exit = .TRUE.
IF (iscf == 1) energy_new = energy_old
END IF
! if early stopping is on do at least one iteration
IF (optimizer%early_stopping_on .AND. iscf .EQ. 1) &
prepare_to_exit = .FALSE.
IF (.NOT. prepare_to_exit) THEN ! update the ALMOs and density matrix
! perform mixing of KS matrices
IF (iscf .NE. 1) THEN
IF (use_diis) THEN ! use diis instead of mixing
DO ispin = 1, nspin
CALL almo_scf_diis_extrapolate(diis_env=almo_diis(ispin), &
extr_var=almo_scf_env%matrix_ks_blk(ispin))
END DO
ELSE ! use mixing
true_mixing_fraction = almo_scf_env%mixing_fraction
DO ispin = 1, nspin
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), &
matrix_mixing_old_blk(ispin), &
true_mixing_fraction, &
1.0_dp - true_mixing_fraction)
END DO
END IF
END IF
! save the new matrix for the future mixing
DO ispin = 1, nspin
CALL dbcsr_copy(matrix_mixing_old_blk(ispin), &
almo_scf_env%matrix_ks_blk(ispin))
END DO
! obtain ALMOs from the new KS matrix
SELECT CASE (almo_scf_env%almo_update_algorithm)
CASE (almo_scf_diag)
CALL almo_scf_ks_blk_to_tv_blk(almo_scf_env)
CASE (almo_scf_dm_sign)
! update the density matrix
DO ispin = 1, nspin
local_nocc_of_domain(:) = almo_scf_env%nocc_of_domain(:, ispin)
local_mu(:) = almo_scf_env%mu_of_domain(:, ispin)
! RZK UPDATE! the update algorithm is removed because
! RZK UPDATE! it requires updating core LS_SCF routines
! RZK UPDATE! (the code exists in the CVS version)
CPABORT("Density_matrix_sign has not been tested yet")
! RZK UPDATE! CALL density_matrix_sign(almo_scf_env%matrix_p_blk(ispin),&
! RZK UPDATE! local_mu,&
! RZK UPDATE! almo_scf_env%fixed_mu,&
! RZK UPDATE! almo_scf_env%matrix_ks_blk(ispin),&
! RZK UPDATE! !matrix_mixing_old_blk(ispin),&
! RZK UPDATE! almo_scf_env%matrix_s_blk(1), &
! RZK UPDATE! almo_scf_env%matrix_s_blk_inv(1), &
! RZK UPDATE! local_nocc_of_domain,&
! RZK UPDATE! almo_scf_env%eps_filter,&
! RZK UPDATE! almo_scf_env%domain_index_of_ao)
! RZK UPDATE!
almo_scf_env%mu_of_domain(:, ispin) = local_mu(:)
END DO
! obtain ALMOs from matrix_p_blk: T_new = P_blk S_blk T_old
CALL almo_scf_p_blk_to_t_blk(almo_scf_env, ionic=.FALSE.)
DO ispin = 1, almo_scf_env%nspins
CALL orthogonalize_mos(ket=almo_scf_env%matrix_t_blk(ispin), &
overlap=almo_scf_env%matrix_sigma_blk(ispin), &
metric=almo_scf_env%matrix_s_blk(1), &
retain_locality=.TRUE., &
only_normalize=.FALSE., &
nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
eps_filter=almo_scf_env%eps_filter, &
order_lanczos=almo_scf_env%order_lanczos, &
eps_lanczos=almo_scf_env%eps_lanczos, &
max_iter_lanczos=almo_scf_env%max_iter_lanczos)
END DO
END SELECT
! obtain density matrix from ALMOs
DO ispin = 1, almo_scf_env%nspins
!! Application of an occupation-rescaling trick for smearing, if requested
IF (almo_scf_env%smear) THEN
CALL almo_scf_t_rescaling(matrix_t=almo_scf_env%matrix_t_blk(ispin), &
mo_energies=almo_scf_env%mo_energies(:, ispin), &
mu_of_domain=almo_scf_env%mu_of_domain(:, ispin), &
real_ne_of_domain=almo_scf_env%real_ne_of_domain(:, ispin), &
spin_kTS=almo_scf_env%kTS(ispin), &
smear_e_temp=almo_scf_env%smear_e_temp, &
ndomains=almo_scf_env%ndomains, &
nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin))
END IF
CALL almo_scf_t_to_proj(t=almo_scf_env%matrix_t_blk(ispin), &
p=almo_scf_env%matrix_p(ispin), &
eps_filter=almo_scf_env%eps_filter, &
orthog_orbs=.FALSE., &
nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
s=almo_scf_env%matrix_s(1), &
sigma=almo_scf_env%matrix_sigma(ispin), &
sigma_inv=almo_scf_env%matrix_sigma_inv(ispin), &
use_guess=use_prev_as_guess, &
smear=almo_scf_env%smear, &
algorithm=almo_scf_env%sigma_inv_algorithm, &
inverse_accelerator=almo_scf_env%order_lanczos, &
inv_eps_factor=almo_scf_env%matrix_iter_eps_error_factor, &
eps_lanczos=almo_scf_env%eps_lanczos, &
max_iter_lanczos=almo_scf_env%max_iter_lanczos, &
para_env=almo_scf_env%para_env, &
blacs_env=almo_scf_env%blacs_env)
END DO
IF (almo_scf_env%nspins == 1) THEN
CALL dbcsr_scale(almo_scf_env%matrix_p(1), 2.0_dp)
!! Rescaling electronic entropy contribution by spin_factor
IF (almo_scf_env%smear) THEN
almo_scf_env%kTS(1) = almo_scf_env%kTS(1)*2.0_dp
END IF
END IF
IF (almo_scf_env%smear) THEN
kTS_sum = SUM(almo_scf_env%kTS)
ELSE
kTS_sum = 0.0_dp
END IF
! compute the new KS matrix and new energy
CALL almo_dm_to_almo_ks(qs_env, &
almo_scf_env%matrix_p, &
almo_scf_env%matrix_ks, &
energy_new, &
almo_scf_env%eps_filter, &
almo_scf_env%mat_distr_aos, &
smear=almo_scf_env%smear, &
kTS_sum=kTS_sum)
END IF ! prepare_to_exit
energy_diff = energy_new - energy_old
energy_old = energy_new
almo_scf_env%almo_scf_energy = energy_new
t2 = m_walltime()
! brief report on the current SCF loop
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A13,I6,F23.10,E14.5,F14.9,F9.2)') "ALMO SCF DIIS", &
iscf, &
energy_new, energy_diff, error_norm, t2 - t1
END IF
t1 = m_walltime()
IF (prepare_to_exit) EXIT
END DO ! end scf cycle
!! Print number of electrons recovered if smearing was requested
IF (almo_scf_env%smear) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(almo_scf_env%matrix_p(ispin), almo_scf_env%matrix_s(1), density_rec)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A20,F23.10)') "Electrons recovered:", density_rec
END IF
END DO
END IF
IF (.NOT. converged .AND. (.NOT. optimizer%early_stopping_on)) THEN
IF (unit_nr > 0) THEN
CPABORT("SCF for block-diagonal ALMOs not converged!")
END IF
END IF
DO ispin = 1, nspin
CALL dbcsr_release(matrix_mixing_old_blk(ispin))
CALL almo_scf_diis_release(diis_env=almo_diis(ispin))
END DO
DEALLOCATE (almo_diis)
DEALLOCATE (matrix_mixing_old_blk)
DEALLOCATE (local_mu)
DEALLOCATE (local_nocc_of_domain)
CALL timestop(handle)
END SUBROUTINE almo_scf_block_diagonal
! **************************************************************************************************
!> \brief An eigensolver-based SCF to optimize extended ALMOs (i.e. ALMOs on
!> overlapping domains)
!> \param qs_env ...
!> \param almo_scf_env ...
!> \param optimizer ...
!> \par History
!> 2013.03 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_xalmo_eigensolver(qs_env, almo_scf_env, optimizer)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
TYPE(optimizer_options_type), INTENT(IN) :: optimizer
CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_xalmo_eigensolver'
INTEGER :: handle, iscf, ispin, nspin, unit_nr
LOGICAL :: converged, prepare_to_exit, should_stop
REAL(KIND=dp) :: denergy_tot, density_rec, energy_diff, energy_new, energy_old, error_norm, &
error_norm_0, kTS_sum, spin_factor, t1, t2
REAL(KIND=dp), DIMENSION(2) :: denergy_spin
TYPE(almo_scf_diis_type), ALLOCATABLE, &
DIMENSION(:) :: almo_diis
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type) :: matrix_p_almo_scf_converged
TYPE(domain_submatrix_type), ALLOCATABLE, &
DIMENSION(:, :) :: submatrix_mixing_old_blk
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
nspin = almo_scf_env%nspins
IF (nspin == 1) THEN
spin_factor = 2.0_dp
ELSE
spin_factor = 1.0_dp
END IF
! RZK-warning domain_s_sqrt and domain_s_sqrt_inv do not have spin
! components yet (may be used later)
ispin = 1
CALL construct_domain_s_sqrt( &
matrix_s=almo_scf_env%matrix_s(1), &
subm_s_sqrt=almo_scf_env%domain_s_sqrt(:, ispin), &
subm_s_sqrt_inv=almo_scf_env%domain_s_sqrt_inv(:, ispin), &
dpattern=almo_scf_env%quench_t(ispin), &
map=almo_scf_env%domain_map(ispin), &
node_of_domain=almo_scf_env%cpu_of_domain)
! TRY: construct s_inv
!CALL construct_domain_s_inv(&
! matrix_s=almo_scf_env%matrix_s(1),&
! subm_s_inv=almo_scf_env%domain_s_inv(:,ispin),&
! dpattern=almo_scf_env%quench_t(ispin),&
! map=almo_scf_env%domain_map(ispin),&
! node_of_domain=almo_scf_env%cpu_of_domain)
! construct the domain template for the occupied orbitals
DO ispin = 1, nspin
! RZK-warning we need only the matrix structure, not data
! replace construct_submatrices with lighter procedure with
! no heavy communications
CALL construct_submatrices( &
matrix=almo_scf_env%quench_t(ispin), &
submatrix=almo_scf_env%domain_t(:, ispin), &
distr_pattern=almo_scf_env%quench_t(ispin), &
domain_map=almo_scf_env%domain_map(ispin), &
node_of_domain=almo_scf_env%cpu_of_domain, &
job_type=select_row)
END DO
! init mixing matrices
ALLOCATE (submatrix_mixing_old_blk(almo_scf_env%ndomains, nspin))
CALL init_submatrices(submatrix_mixing_old_blk)
ALLOCATE (almo_diis(nspin))
! TRY: construct block-projector
!ALLOCATE(submatrix_tmp(almo_scf_env%ndomains))
!DO ispin=1,nspin
! CALL init_submatrices(submatrix_tmp)
! CALL construct_domain_r_down(&
! matrix_t=almo_scf_env%matrix_t_blk(ispin),&
! matrix_sigma_inv=almo_scf_env%matrix_sigma_inv(ispin),&
! matrix_s=almo_scf_env%matrix_s(1),&
! subm_r_down=submatrix_tmp(:),&
! dpattern=almo_scf_env%quench_t(ispin),&
! map=almo_scf_env%domain_map(ispin),&
! node_of_domain=almo_scf_env%cpu_of_domain,&
! filter_eps=almo_scf_env%eps_filter)
! CALL multiply_submatrices('N','N',1.0_dp,&
! submatrix_tmp(:),&
! almo_scf_env%domain_s_inv(:,1),0.0_dp,&
! almo_scf_env%domain_r_down_up(:,ispin))
! CALL release_submatrices(submatrix_tmp)
!ENDDO
!DEALLOCATE(submatrix_tmp)
DO ispin = 1, nspin
! use s_sqrt since they are already properly constructed
! and have the same distributions as domain_err and domain_ks_xx
CALL almo_scf_diis_init(diis_env=almo_diis(ispin), &
sample_err=almo_scf_env%domain_s_sqrt(:, ispin), &
error_type=1, &
max_length=optimizer%ndiis)
END DO
denergy_tot = 0.0_dp
energy_old = 0.0_dp
iscf = 0
prepare_to_exit = .FALSE.
! the SCF loop
t1 = m_walltime()
DO
iscf = iscf + 1
! obtain projected KS matrix and the DIIS-error vector
CALL almo_scf_ks_to_ks_xx(almo_scf_env)
! inform the DIIS handler about the new KS matrix and its error vector
DO ispin = 1, nspin
CALL almo_scf_diis_push(diis_env=almo_diis(ispin), &
d_var=almo_scf_env%domain_ks_xx(:, ispin), &
d_err=almo_scf_env%domain_err(:, ispin))
END DO
! check convergence
converged = .TRUE.
DO ispin = 1, nspin
!error_norm=dbcsr_frobenius_norm(almo_scf_env%matrix_err_blk(ispin))
CALL dbcsr_norm(almo_scf_env%matrix_err_xx(ispin), &
dbcsr_norm_maxabsnorm, &
norm_scalar=error_norm)
CALL maxnorm_submatrices(almo_scf_env%domain_err(:, ispin), &
norm=error_norm_0)
IF (error_norm .GT. optimizer%eps_error) THEN
converged = .FALSE.
EXIT ! no need to check the other spin
END IF
END DO
! check other exit criteria: max SCF steps and timing
CALL external_control(should_stop, "SCF", &
start_time=qs_env%start_time, &
target_time=qs_env%target_time)
IF (should_stop .OR. iscf >= optimizer%max_iter .OR. converged) THEN
prepare_to_exit = .TRUE.
END IF
! if early stopping is on do at least one iteration
IF (optimizer%early_stopping_on .AND. iscf .EQ. 1) &
prepare_to_exit = .FALSE.
IF (.NOT. prepare_to_exit) THEN ! update the ALMOs and density matrix
! perform mixing of KS matrices
IF (iscf .NE. 1) THEN
IF (.FALSE.) THEN ! use diis instead of mixing
DO ispin = 1, nspin
CALL add_submatrices( &
almo_scf_env%mixing_fraction, &
almo_scf_env%domain_ks_xx(:, ispin), &
1.0_dp - almo_scf_env%mixing_fraction, &
submatrix_mixing_old_blk(:, ispin), &
'N')
END DO
ELSE
DO ispin = 1, nspin
CALL almo_scf_diis_extrapolate(diis_env=almo_diis(ispin), &
d_extr_var=almo_scf_env%domain_ks_xx(:, ispin))
END DO
END IF
END IF
! save the new matrix for the future mixing
DO ispin = 1, nspin
CALL copy_submatrices( &
almo_scf_env%domain_ks_xx(:, ispin), &
submatrix_mixing_old_blk(:, ispin), &
copy_data=.TRUE.)
END DO
! obtain a new set of ALMOs from the updated KS matrix
CALL almo_scf_ks_xx_to_tv_xx(almo_scf_env)
! update the density matrix
DO ispin = 1, nspin
! save the initial density matrix (to get the perturbative energy lowering)
IF (iscf .EQ. 1) THEN
CALL dbcsr_create(matrix_p_almo_scf_converged, &
template=almo_scf_env%matrix_p(ispin))
CALL dbcsr_copy(matrix_p_almo_scf_converged, &
almo_scf_env%matrix_p(ispin))
END IF
!! Application of an occupation-rescaling trick for smearing, if requested
IF (almo_scf_env%smear) THEN
CALL almo_scf_t_rescaling(matrix_t=almo_scf_env%matrix_t_blk(ispin), &
mo_energies=almo_scf_env%mo_energies(:, ispin), &
mu_of_domain=almo_scf_env%mu_of_domain(:, ispin), &
real_ne_of_domain=almo_scf_env%real_ne_of_domain(:, ispin), &
spin_kTS=almo_scf_env%kTS(ispin), &
smear_e_temp=almo_scf_env%smear_e_temp, &
ndomains=almo_scf_env%ndomains, &
nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin))
END IF
! update now
CALL almo_scf_t_to_proj( &
t=almo_scf_env%matrix_t(ispin), &
p=almo_scf_env%matrix_p(ispin), &
eps_filter=almo_scf_env%eps_filter, &
orthog_orbs=.FALSE., &
nocc_of_domain=almo_scf_env%nocc_of_domain(:, ispin), &
s=almo_scf_env%matrix_s(1), &
sigma=almo_scf_env%matrix_sigma(ispin), &
sigma_inv=almo_scf_env%matrix_sigma_inv(ispin), &
use_guess=.TRUE., &
smear=almo_scf_env%smear, &
algorithm=almo_scf_env%sigma_inv_algorithm, &
inverse_accelerator=almo_scf_env%order_lanczos, &
inv_eps_factor=almo_scf_env%matrix_iter_eps_error_factor, &
eps_lanczos=almo_scf_env%eps_lanczos, &
max_iter_lanczos=almo_scf_env%max_iter_lanczos, &
para_env=almo_scf_env%para_env, &
blacs_env=almo_scf_env%blacs_env)
CALL dbcsr_scale(almo_scf_env%matrix_p(ispin), spin_factor)
!! Rescaling electronic entropy contribution by spin_factor
IF (almo_scf_env%smear) THEN
almo_scf_env%kTS(ispin) = almo_scf_env%kTS(ispin)*spin_factor
END IF
! obtain perturbative estimate (at no additional cost)
! of the energy lowering relative to the block-diagonal ALMOs
IF (iscf .EQ. 1) THEN
CALL dbcsr_add(matrix_p_almo_scf_converged, &
almo_scf_env%matrix_p(ispin), -1.0_dp, 1.0_dp)
CALL dbcsr_dot(almo_scf_env%matrix_ks_0deloc(ispin), &
matrix_p_almo_scf_converged, &
denergy_spin(ispin))
CALL dbcsr_release(matrix_p_almo_scf_converged)
!! RS-WARNING: If smearing ALMO is requested, electronic entropy contribution should probably be included here
denergy_tot = denergy_tot + denergy_spin(ispin)
! RZK-warning Energy correction can be evaluated using matrix_x
! as shown in the attempt below and in the PCG procedure.
! Using matrix_x allows immediate decomposition of the energy
! lowering into 2-body components for EDA. However, it does not
! work here because the diagonalization routine does not necessarily
! produce orbitals with the same sign as the block-diagonal ALMOs
! Any fixes?!
!CALL dbcsr_init(matrix_x)
!CALL dbcsr_create(matrix_x,&
! template=almo_scf_env%matrix_t(ispin))
!
!CALL dbcsr_init(matrix_tmp_no)
!CALL dbcsr_create(matrix_tmp_no,&
! template=almo_scf_env%matrix_t(ispin))
!
!CALL dbcsr_copy(matrix_x,&
! almo_scf_env%matrix_t_blk(ispin))
!CALL dbcsr_add(matrix_x,almo_scf_env%matrix_t(ispin),&
! -1.0_dp,1.0_dp)
!CALL dbcsr_dot(matrix_x, almo_scf_env%matrix_err_xx(ispin),denergy)
!denergy=denergy*spin_factor
!IF (unit_nr>0) THEN
! WRITE(unit_nr,*) "_ENERGY-0: ", almo_scf_env%almo_scf_energy
! WRITE(unit_nr,*) "_ENERGY-D: ", denergy
! WRITE(unit_nr,*) "_ENERGY-F: ", almo_scf_env%almo_scf_energy+denergy
!ENDIF
!! RZK-warning update will not work since the energy is overwritten almost immediately
!!CALL almo_scf_update_ks_energy(qs_env,&
!! almo_scf_env%almo_scf_energy+denergy)
!!
!! print out the results of the decomposition analysis
!CALL dbcsr_hadamard_product(matrix_x,&
! almo_scf_env%matrix_err_xx(ispin),&
! matrix_tmp_no)
!CALL dbcsr_scale(matrix_tmp_no,spin_factor)
!CALL dbcsr_filter(matrix_tmp_no,almo_scf_env%eps_filter)
!
!IF (unit_nr>0) THEN
! WRITE(unit_nr,*)
! WRITE(unit_nr,'(T2,A)') "DECOMPOSITION OF THE DELOCALIZATION ENERGY"
!ENDIF
!mynode=dbcsr_mp_mynode(dbcsr_distribution_mp(&
! dbcsr_distribution(matrix_tmp_no)))
!WRITE(mynodestr,'(I6.6)') mynode
!mylogfile='EDA.'//TRIM(ADJUSTL(mynodestr))
!OPEN (iunit,file=mylogfile,status='REPLACE')
!CALL dbcsr_print_block_sum(matrix_tmp_no,iunit)
!CLOSE(iunit)
!
!CALL dbcsr_release(matrix_tmp_no)
!CALL dbcsr_release(matrix_x)
END IF ! iscf.eq.1
END DO
! print out the energy lowering
IF (iscf .EQ. 1) THEN
CALL energy_lowering_report( &
unit_nr=unit_nr, &
ref_energy=almo_scf_env%almo_scf_energy, &
energy_lowering=denergy_tot)
CALL almo_scf_update_ks_energy(qs_env, &
energy=almo_scf_env%almo_scf_energy, &
energy_singles_corr=denergy_tot)
END IF
! compute the new KS matrix and new energy
IF (.NOT. almo_scf_env%perturbative_delocalization) THEN
IF (almo_scf_env%smear) THEN
kTS_sum = SUM(almo_scf_env%kTS)
ELSE
kTS_sum = 0.0_dp
END IF
CALL almo_dm_to_almo_ks(qs_env, &
almo_scf_env%matrix_p, &
almo_scf_env%matrix_ks, &
energy_new, &
almo_scf_env%eps_filter, &
almo_scf_env%mat_distr_aos, &
smear=almo_scf_env%smear, &
kTS_sum=kTS_sum)
END IF
END IF ! prepare_to_exit
IF (almo_scf_env%perturbative_delocalization) THEN
! exit after the first step if we do not need the SCF procedure
CALL almo_dm_to_qs_env(qs_env, almo_scf_env%matrix_p, almo_scf_env%mat_distr_aos)
converged = .TRUE.
prepare_to_exit = .TRUE.
ELSE ! not a perturbative treatment
energy_diff = energy_new - energy_old
energy_old = energy_new
almo_scf_env%almo_scf_energy = energy_new
t2 = m_walltime()
! brief report on the current SCF loop
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A,I6,F20.9,E11.3,E11.3,E11.3,F8.2)') "ALMO SCF", &
iscf, &
energy_new, energy_diff, error_norm, error_norm_0, t2 - t1
END IF
t1 = m_walltime()
END IF
IF (prepare_to_exit) EXIT
END DO ! end scf cycle
!! Print number of electrons recovered if smearing was requested
IF (almo_scf_env%smear) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(almo_scf_env%matrix_p(ispin), almo_scf_env%matrix_s(1), density_rec)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A20,F23.10)') "Electrons recovered:", density_rec
END IF
END DO
END IF
IF (.NOT. converged .AND. .NOT. optimizer%early_stopping_on) THEN
CPABORT("SCF for ALMOs on overlapping domains not converged!")
END IF
DO ispin = 1, nspin
CALL release_submatrices(submatrix_mixing_old_blk(:, ispin))
CALL almo_scf_diis_release(diis_env=almo_diis(ispin))
END DO
DEALLOCATE (almo_diis)
DEALLOCATE (submatrix_mixing_old_blk)
CALL timestop(handle)
END SUBROUTINE almo_scf_xalmo_eigensolver
! **************************************************************************************************
!> \brief Optimization of ALMOs using PCG-like minimizers
!> \param qs_env ...
!> \param almo_scf_env ...
!> \param optimizer controls the optimization algorithm
!> \param quench_t ...
!> \param matrix_t_in ...
!> \param matrix_t_out ...
!> \param assume_t0_q0x - since it is extremely difficult to converge the iterative
!> procedure using T as an optimized variable, assume
!> T = T_0 + (1-R_0)*X and optimize X
!> T_0 is assumed to be the zero-delocalization reference
!> \param perturbation_only - perturbative (do not update Hamiltonian)
!> \param special_case to reduce the overhead special cases are implemented:
!> xalmo_case_normal - no special case (i.e. xALMOs)
!> xalmo_case_block_diag
!> xalmo_case_fully_deloc
!> \par History
!> 2011.11 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_xalmo_pcg(qs_env, almo_scf_env, optimizer, quench_t, &
matrix_t_in, matrix_t_out, assume_t0_q0x, perturbation_only, &
special_case)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
TYPE(optimizer_options_type), INTENT(IN) :: optimizer
TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:), &
INTENT(INOUT) :: quench_t, matrix_t_in, matrix_t_out
LOGICAL, INTENT(IN) :: assume_t0_q0x, perturbation_only
INTEGER, INTENT(IN), OPTIONAL :: special_case
CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_xalmo_pcg'
CHARACTER(LEN=20) :: iter_type
INTEGER :: cg_iteration, dim_op, fixed_line_search_niter, handle, idim0, ielem, ispin, &
iteration, line_search_iteration, max_iter, my_special_case, ndomains, nmo, nspins, &
outer_iteration, outer_max_iter, para_group_handle, prec_type, reim, unit_nr
INTEGER, ALLOCATABLE, DIMENSION(:) :: nocc
LOGICAL :: blissful_neglect, converged, just_started, line_search, normalize_orbitals, &
optimize_theta, outer_prepare_to_exit, penalty_occ_local, penalty_occ_vol, &
prepare_to_exit, reset_conjugator, skip_grad, use_guess
REAL(dp), ALLOCATABLE, DIMENSION(:) :: reim_diag, weights, z2
REAL(kind=dp) :: appr_sec_der, beta, denom, denom2, e0, e1, energy_coeff, energy_diff, &
energy_new, energy_old, eps_skip_gradients, fval, g0, g1, grad_norm, grad_norm_frob, &
line_search_error, localiz_coeff, localization_obj_function, next_step_size_guess, &
penalty_amplitude, penalty_func_new, spin_factor, step_size, t1, t2, tempreal
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: grad_norm_spin, &
penalty_occ_vol_g_prefactor, &
penalty_occ_vol_h_prefactor
TYPE(cell_type), POINTER :: cell
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: qs_matrix_s
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: op_sm_set_almo, op_sm_set_qs
TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: FTsiginv, grad, m_sig_sqrti_ii, m_t_in_local, &
m_theta, prec_vv, prev_grad, prev_minus_prec_grad, prev_step, siginvTFTsiginv, ST, step, &
STsiginv_0, tempNOcc, tempNOcc_1, tempOccOcc
TYPE(domain_submatrix_type), ALLOCATABLE, &
DIMENSION(:, :) :: bad_modes_projector_down, domain_r_down
TYPE(mp_comm_type) :: para_group
CALL timeset(routineN, handle)
my_special_case = xalmo_case_normal
IF (PRESENT(special_case)) my_special_case = special_case
! 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
nspins = almo_scf_env%nspins
! if unprojected XALMOs are optimized
! then we must use the "blissful_neglect" procedure
blissful_neglect = .FALSE.
IF (my_special_case .EQ. xalmo_case_normal .AND. .NOT. assume_t0_q0x) THEN
blissful_neglect = .TRUE.
END IF
IF (unit_nr > 0) THEN
WRITE (unit_nr, *)
SELECT CASE (my_special_case)
CASE (xalmo_case_block_diag)
WRITE (unit_nr, '(T2,A,A,A)') REPEAT("-", 20), &
" Optimization of block-diagonal ALMOs ", REPEAT("-", 21)
CASE (xalmo_case_fully_deloc)
WRITE (unit_nr, '(T2,A,A,A)') REPEAT("-", 20), &
" Optimization of fully delocalized MOs ", REPEAT("-", 20)
CASE (xalmo_case_normal)
IF (blissful_neglect) THEN
WRITE (unit_nr, '(T2,A,A,A)') REPEAT("-", 25), &
" LCP optimization of XALMOs ", REPEAT("-", 26)
ELSE
WRITE (unit_nr, '(T2,A,A,A)') REPEAT("-", 27), &
" Optimization of XALMOs ", REPEAT("-", 28)
END IF
END SELECT
WRITE (unit_nr, *)
WRITE (unit_nr, '(T2,A13,A6,A23,A14,A14,A9)') "Method", "Iter", &
"Objective Function", "Change", "Convergence", "Time"
WRITE (unit_nr, '(T2,A)') REPEAT("-", 79)
END IF
! set local parameters using developer's keywords
! RZK-warning: change to normal keywords later
optimize_theta = almo_scf_env%logical05
eps_skip_gradients = almo_scf_env%real01
! penalty amplitude adjusts the strength of volume conservation
energy_coeff = 1.0_dp !optimizer%opt_penalty%energy_coeff
localiz_coeff = 0.0_dp !optimizer%opt_penalty%occ_loc_coeff
penalty_amplitude = 0.0_dp !optimizer%opt_penalty%occ_vol_coeff
penalty_occ_vol = .FALSE. !( optimizer%opt_penalty%occ_vol_method &
!.NE. penalty_type_none .AND. my_special_case .EQ. xalmo_case_fully_deloc )
penalty_occ_local = .FALSE. !( optimizer%opt_penalty%occ_loc_method &
!.NE. penalty_type_none .AND. my_special_case .EQ. xalmo_case_fully_deloc )
normalize_orbitals = penalty_occ_vol .OR. penalty_occ_local
ALLOCATE (penalty_occ_vol_g_prefactor(nspins))
ALLOCATE (penalty_occ_vol_h_prefactor(nspins))
penalty_occ_vol_g_prefactor(:) = 0.0_dp
penalty_occ_vol_h_prefactor(:) = 0.0_dp
penalty_func_new = 0.0_dp
! preconditioner control
prec_type = optimizer%preconditioner
! control of the line search
fixed_line_search_niter = 0 ! init to zero, change when eps is small enough
IF (nspins == 1) THEN
spin_factor = 2.0_dp
ELSE
spin_factor = 1.0_dp
END IF
ALLOCATE (grad_norm_spin(nspins))
ALLOCATE (nocc(nspins))
! create a local copy of matrix_t_in because
! matrix_t_in and matrix_t_out can be the same matrix
! we need to make sure data in matrix_t_in is intact
! after we start writing to matrix_t_out
ALLOCATE (m_t_in_local(nspins))
DO ispin = 1, nspins
CALL dbcsr_create(m_t_in_local(ispin), &
template=matrix_t_in(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_copy(m_t_in_local(ispin), matrix_t_in(ispin))
END DO
! m_theta contains a set of variational parameters