-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_diis.F
1272 lines (1044 loc) · 50.9 KB
/
qs_diis.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 Apply the direct inversion in the iterative subspace (DIIS) of Pulay
!> in the framework of an SCF iteration for convergence acceleration
!> \par Literature
!> - P. Pulay, Chem. Phys. Lett. 73, 393 (1980)
!> - P. Pulay, J. Comput. Chem. 3, 556 (1982)
!> \par History
!> - Changed to BLACS matrix usage (08.06.2001,MK)
!> - rewritten to include LSD (1st attempt) (01.2003, Joost VandeVondele)
!> - DIIS for ROKS (05.04.06,MK)
!> - DIIS for k-points (04.2023, Augustin Bussy)
!> \author Matthias Krack (28.06.2000)
! **************************************************************************************************
MODULE qs_diis
USE cp_cfm_basic_linalg, ONLY: cp_cfm_trace
USE cp_cfm_types, ONLY: cp_cfm_create,&
cp_cfm_get_info,&
cp_cfm_release,&
cp_cfm_to_cfm,&
cp_cfm_to_fm,&
cp_cfm_type,&
cp_fm_to_cfm
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_dot, dbcsr_maxabs, dbcsr_multiply, &
dbcsr_p_type, dbcsr_release, dbcsr_set, dbcsr_transposed, dbcsr_type
USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm
USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale,&
cp_fm_scale,&
cp_fm_scale_and_add,&
cp_fm_symm,&
cp_fm_trace
USE cp_fm_struct, ONLY: cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_info,&
cp_fm_maxabsval,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_to_fm,&
cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_type,&
cp_to_string
USE cp_output_handling, ONLY: cp_print_key_finished_output,&
cp_print_key_unit_nr
USE dm_ls_scf_types, ONLY: ls_scf_env_type
USE input_section_types, ONLY: section_vals_type
USE kinds, ONLY: default_string_length,&
dp
USE mathlib, ONLY: complex_diag,&
diamat_all
USE message_passing, ONLY: mp_para_env_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE qs_diis_types, ONLY: qs_diis_buffer_type,&
qs_diis_buffer_type_kp,&
qs_diis_buffer_type_sparse
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE string_utilities, ONLY: compress
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_diis'
! Public subroutines
PUBLIC :: qs_diis_b_clear, &
qs_diis_b_create, &
qs_diis_b_step
PUBLIC :: qs_diis_b_clear_sparse, &
qs_diis_b_create_sparse, &
qs_diis_b_step_4lscf
PUBLIC :: qs_diis_b_clear_kp, &
qs_diis_b_create_kp, &
qs_diis_b_step_kp, &
qs_diis_b_calc_err_kp, &
qs_diis_b_info_kp
CONTAINS
! **************************************************************************************************
!> \brief Allocates an SCF DIIS buffer
!> \param diis_buffer the buffer to create
!> \param nbuffer ...
!> \par History
!> 02.2003 created [fawzi]
!> \author fawzi
! **************************************************************************************************
SUBROUTINE qs_diis_b_create(diis_buffer, nbuffer)
TYPE(qs_diis_buffer_type), INTENT(OUT) :: diis_buffer
INTEGER, INTENT(in) :: nbuffer
CHARACTER(len=*), PARAMETER :: routineN = 'qs_diis_b_create'
INTEGER :: handle
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
NULLIFY (diis_buffer%b_matrix)
NULLIFY (diis_buffer%error)
NULLIFY (diis_buffer%param)
diis_buffer%nbuffer = nbuffer
diis_buffer%ncall = 0
CALL timestop(handle)
END SUBROUTINE qs_diis_b_create
! **************************************************************************************************
!> \brief Allocate and initialize a DIIS buffer for nao*nao parameter
!> variables and with a buffer size of nbuffer.
!> \param diis_buffer the buffer to initialize
!> \param matrix_struct the structure for the matrix of the buffer
!> \param nspin ...
!> \param scf_section ...
!> \par History
!> - Creation (07.05.2001, Matthias Krack)
!> - Changed to BLACS matrix usage (08.06.2001,MK)
!> - DIIS for ROKS (05.04.06,MK)
!> \author Matthias Krack
!> \note
!> check to allocate matrixes only when needed, using a linked list?
! **************************************************************************************************
SUBROUTINE qs_diis_b_check_i_alloc(diis_buffer, matrix_struct, nspin, &
scf_section)
TYPE(qs_diis_buffer_type), INTENT(INOUT) :: diis_buffer
TYPE(cp_fm_struct_type), POINTER :: matrix_struct
INTEGER, INTENT(IN) :: nspin
TYPE(section_vals_type), POINTER :: scf_section
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_diis_b_check_i_alloc'
INTEGER :: handle, ibuffer, ispin, nbuffer, &
output_unit
TYPE(cp_logger_type), POINTER :: logger
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
nbuffer = diis_buffer%nbuffer
IF (.NOT. ASSOCIATED(diis_buffer%error)) THEN
ALLOCATE (diis_buffer%error(nbuffer, nspin))
DO ispin = 1, nspin
DO ibuffer = 1, nbuffer
CALL cp_fm_create(diis_buffer%error(ibuffer, ispin), &
name="qs_diis_b%error("// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//","// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//")", &
matrix_struct=matrix_struct)
END DO
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%param)) THEN
ALLOCATE (diis_buffer%param(nbuffer, nspin))
DO ispin = 1, nspin
DO ibuffer = 1, nbuffer
CALL cp_fm_create(diis_buffer%param(ibuffer, ispin), &
name="qs_diis_b%param("// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//","// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//")", &
matrix_struct=matrix_struct)
END DO
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%b_matrix)) THEN
ALLOCATE (diis_buffer%b_matrix(nbuffer + 1, nbuffer + 1))
diis_buffer%b_matrix = 0.0_dp
output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DIIS_INFO", &
extension=".scfLog")
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/,T9,A)") &
"DIIS | The SCF DIIS buffer was allocated and initialized"
END IF
CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
"PRINT%DIIS_INFO")
END IF
CALL timestop(handle)
END SUBROUTINE qs_diis_b_check_i_alloc
! **************************************************************************************************
!> \brief Update the SCF DIIS buffer, and if appropriate does a diis step.
!> \param diis_buffer ...
!> \param mo_array ...
!> \param kc ...
!> \param sc ...
!> \param delta ...
!> \param error_max ...
!> \param diis_step ...
!> \param eps_diis ...
!> \param nmixing ...
!> \param s_matrix ...
!> \param scf_section ...
!> \param roks ...
!> \par History
!> - Creation (07.05.2001, Matthias Krack)
!> - Changed to BLACS matrix usage (08.06.2001, MK)
!> - 03.2003 rewamped [fawzi]
!> - Adapted for high-spin ROKS (08.04.06,MK)
!> \author Matthias Krack
! **************************************************************************************************
SUBROUTINE qs_diis_b_step(diis_buffer, mo_array, kc, sc, delta, error_max, &
diis_step, eps_diis, nmixing, s_matrix, scf_section, roks)
TYPE(qs_diis_buffer_type), POINTER :: diis_buffer
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
TYPE(cp_fm_type), DIMENSION(:), POINTER :: kc
TYPE(cp_fm_type), INTENT(IN) :: sc
REAL(KIND=dp), INTENT(IN) :: delta
REAL(KIND=dp), INTENT(OUT) :: error_max
LOGICAL, INTENT(OUT) :: diis_step
REAL(KIND=dp), INTENT(IN) :: eps_diis
INTEGER, INTENT(IN), OPTIONAL :: nmixing
TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
POINTER :: s_matrix
TYPE(section_vals_type), POINTER :: scf_section
LOGICAL, INTENT(IN), OPTIONAL :: roks
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_diis_b_step'
REAL(KIND=dp), PARAMETER :: eigenvalue_threshold = 1.0E-12_dp
CHARACTER(LEN=2*default_string_length) :: message
INTEGER :: handle, homo, ib, imo, ispin, jb, &
my_nmixing, nao, nb, nb1, nmo, nspin, &
output_unit
LOGICAL :: eigenvectors_discarded, my_roks
REAL(KIND=dp) :: maxocc, tmp
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: ev, occ
REAL(KIND=dp), DIMENSION(:), POINTER :: occa, occb
REAL(KIND=dp), DIMENSION(:, :), POINTER :: a, b
TYPE(cp_fm_struct_type), POINTER :: matrix_struct
TYPE(cp_fm_type), POINTER :: c, new_errors, old_errors, parameters
TYPE(cp_logger_type), POINTER :: logger
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
nspin = SIZE(mo_array)
diis_step = .FALSE.
IF (PRESENT(roks)) THEN
my_roks = .TRUE.
nspin = 1
ELSE
my_roks = .FALSE.
END IF
my_nmixing = 2
IF (PRESENT(nmixing)) my_nmixing = nmixing
NULLIFY (c, new_errors, old_errors, parameters, matrix_struct, a, b, occa, occb)
logger => cp_get_default_logger()
! Quick return, if no DIIS is requested
IF (diis_buffer%nbuffer < 1) THEN
CALL timestop(handle)
RETURN
END IF
CALL cp_fm_get_info(kc(1), &
matrix_struct=matrix_struct)
CALL qs_diis_b_check_i_alloc(diis_buffer, &
matrix_struct=matrix_struct, &
nspin=nspin, &
scf_section=scf_section)
error_max = 0.0_dp
ib = MODULO(diis_buffer%ncall, diis_buffer%nbuffer) + 1
diis_buffer%ncall = diis_buffer%ncall + 1
nb = MIN(diis_buffer%ncall, diis_buffer%nbuffer)
DO ispin = 1, nspin
CALL get_mo_set(mo_set=mo_array(ispin), &
nao=nao, &
nmo=nmo, &
homo=homo, &
mo_coeff=c, &
occupation_numbers=occa, &
maxocc=maxocc)
new_errors => diis_buffer%error(ib, ispin)
parameters => diis_buffer%param(ib, ispin)
! Copy the Kohn-Sham matrix K to the DIIS buffer
CALL cp_fm_to_fm(kc(ispin), parameters)
IF (my_roks) THEN
ALLOCATE (occ(nmo))
CALL get_mo_set(mo_set=mo_array(2), &
occupation_numbers=occb)
DO imo = 1, nmo
occ(imo) = SQRT(occa(imo) + occb(imo))
END DO
CALL cp_fm_to_fm(c, sc)
CALL cp_fm_column_scale(sc, occ(1:homo))
! KC <- K*C
CALL cp_fm_symm("L", "U", nao, homo, 1.0_dp, parameters, sc, 0.0_dp, kc(ispin))
IF (PRESENT(s_matrix)) THEN
CALL copy_dbcsr_to_fm(s_matrix(1)%matrix, new_errors)
! SC <- S*C
CALL cp_fm_symm("L", "U", nao, homo, 1.0_dp, new_errors, c, 0.0_dp, sc)
CALL cp_fm_column_scale(sc, occ(1:homo))
END IF
! new_errors <- KC*(SC)^T - (SC)*(KC)^T = K*P*S - S*P*K
! or for an orthogonal basis
! new_errors <- KC*C^T - C*(KC)^T = K*P - P*K with S = I
CALL parallel_gemm("N", "T", nao, nao, homo, 1.0_dp, sc, kc(ispin), 0.0_dp, new_errors)
CALL parallel_gemm("N", "T", nao, nao, homo, 1.0_dp, kc(ispin), sc, -1.0_dp, new_errors)
DEALLOCATE (occ)
ELSE
! KC <- K*C
CALL cp_fm_symm("L", "U", nao, homo, maxocc, parameters, c, 0.0_dp, kc(ispin))
IF (PRESENT(s_matrix)) THEN
! I guess that this copy can be avoided for LSD
CALL copy_dbcsr_to_fm(s_matrix(1)%matrix, new_errors)
! sc <- S*C
CALL cp_fm_symm("L", "U", nao, homo, 2.0_dp, new_errors, c, 0.0_dp, sc)
! new_errors <- KC*(SC)^T - (SC)*(KC)^T = K*P*S - S*P*K
CALL parallel_gemm("N", "T", nao, nao, homo, 1.0_dp, sc, kc(ispin), 0.0_dp, new_errors)
CALL parallel_gemm("N", "T", nao, nao, homo, 1.0_dp, kc(ispin), sc, -1.0_dp, new_errors)
ELSE
! new_errors <- KC*(C)^T - C*(KC)^T = K*P - P*K
CALL parallel_gemm("N", "T", nao, nao, homo, 1.0_dp, c, kc(ispin), 0.0_dp, new_errors)
CALL parallel_gemm("N", "T", nao, nao, homo, 1.0_dp, kc(ispin), c, -1.0_dp, new_errors)
END IF
END IF
CALL cp_fm_maxabsval(new_errors, tmp)
error_max = MAX(error_max, tmp)
END DO
! Check, if a DIIS step is appropriate
diis_step = ((diis_buffer%ncall >= my_nmixing) .AND. (delta < eps_diis))
output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DIIS_INFO", &
extension=".scfLog")
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/,T9,A,I4,/,(T9,A,ES12.3))") &
"DIIS | Current SCF DIIS buffer size: ", nb, &
"DIIS | Maximum SCF DIIS error vector element:", error_max, &
"DIIS | Current SCF convergence: ", delta, &
"DIIS | Threshold value for a DIIS step: ", eps_diis
IF (error_max < eps_diis) THEN
WRITE (UNIT=output_unit, FMT="(T9,A)") &
"DIIS | => The SCF DIIS buffer will be updated"
ELSE
WRITE (UNIT=output_unit, FMT="(T9,A)") &
"DIIS | => No update of the SCF DIIS buffer"
END IF
IF (diis_step .AND. (error_max < eps_diis)) THEN
WRITE (UNIT=output_unit, FMT="(T9,A,/)") &
"DIIS | => A SCF DIIS step will be performed"
ELSE
WRITE (UNIT=output_unit, FMT="(T9,A,/)") &
"DIIS | => No SCF DIIS step will be performed"
END IF
END IF
! Update the SCF DIIS buffer
IF (error_max < eps_diis) THEN
b => diis_buffer%b_matrix
DO jb = 1, nb
b(jb, ib) = 0.0_dp
DO ispin = 1, nspin
old_errors => diis_buffer%error(jb, ispin)
new_errors => diis_buffer%error(ib, ispin)
CALL cp_fm_trace(old_errors, new_errors, tmp)
b(jb, ib) = b(jb, ib) + tmp
END DO
b(ib, jb) = b(jb, ib)
END DO
ELSE
diis_step = .FALSE.
END IF
! Perform DIIS step
IF (diis_step) THEN
nb1 = nb + 1
ALLOCATE (a(nb1, nb1))
ALLOCATE (b(nb1, nb1))
ALLOCATE (ev(nb1))
! Set up the linear DIIS equation system
b(1:nb, 1:nb) = diis_buffer%b_matrix(1:nb, 1:nb)
b(1:nb, nb1) = -1.0_dp
b(nb1, 1:nb) = -1.0_dp
b(nb1, nb1) = 0.0_dp
! Solve the linear DIIS equation system
ev(1:nb1) = 0.0_dp
CALL diamat_all(b(1:nb1, 1:nb1), ev(1:nb1))
a(1:nb1, 1:nb1) = b(1:nb1, 1:nb1)
eigenvectors_discarded = .FALSE.
DO jb = 1, nb1
IF (ABS(ev(jb)) < eigenvalue_threshold) THEN
IF (output_unit > 0) THEN
IF (.NOT. eigenvectors_discarded) THEN
WRITE (UNIT=output_unit, FMT="(T9,A)") &
"DIIS | Checking eigenvalues of the DIIS error matrix"
END IF
WRITE (UNIT=message, FMT="(T9,A,I6,A,ES10.1,A,ES10.1)") &
"DIIS | Eigenvalue ", jb, " = ", ev(jb), " is smaller than "// &
"threshold ", eigenvalue_threshold
CALL compress(message)
WRITE (UNIT=output_unit, FMT="(T9,A)") TRIM(message)
eigenvectors_discarded = .TRUE.
END IF
a(1:nb1, jb) = 0.0_dp
ELSE
a(1:nb1, jb) = a(1:nb1, jb)/ev(jb)
END IF
END DO
IF ((output_unit > 0) .AND. eigenvectors_discarded) THEN
WRITE (UNIT=output_unit, FMT="(T9,A,/)") &
"DIIS | The corresponding eigenvectors were discarded"
END IF
ev(1:nb) = MATMUL(a(1:nb, 1:nb1), b(nb1, 1:nb1))
! Update Kohn-Sham matrix
DO ispin = 1, nspin
CALL cp_fm_set_all(kc(ispin), 0.0_dp)
DO jb = 1, nb
parameters => diis_buffer%param(jb, ispin)
CALL cp_fm_scale_and_add(1.0_dp, kc(ispin), -ev(jb), parameters)
END DO
END DO
DEALLOCATE (a)
DEALLOCATE (b)
DEALLOCATE (ev)
ELSE
DO ispin = 1, nspin
parameters => diis_buffer%param(ib, ispin)
CALL cp_fm_to_fm(parameters, kc(ispin))
END DO
END IF
CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
"PRINT%DIIS_INFO")
CALL timestop(handle)
END SUBROUTINE qs_diis_b_step
! **************************************************************************************************
!> \brief clears the buffer
!> \param diis_buffer the buffer to clear
!> \par History
!> 02.2003 created [fawzi]
!> \author fawzi
! **************************************************************************************************
PURE SUBROUTINE qs_diis_b_clear(diis_buffer)
TYPE(qs_diis_buffer_type), INTENT(INOUT) :: diis_buffer
diis_buffer%ncall = 0
END SUBROUTINE qs_diis_b_clear
! **************************************************************************************************
!> \brief Update the SCF DIIS buffer in linear scaling SCF (LS-SCF),
!> and if appropriate does a diis step.
!> \param diis_buffer ...
!> \param qs_env ...
!> \param ls_scf_env ...
!> \param unit_nr ...
!> \param iscf ...
!> \param diis_step ...
!> \param eps_diis ...
!> \param nmixing ...
!> \param s_matrix ...
!> \param threshold ...
!> \par History
!> - Adapted for LS-SCF (10-11-14) from qs_diis_b_step
!> \author Fredy W. Aquino
! **************************************************************************************************
SUBROUTINE qs_diis_b_step_4lscf(diis_buffer, qs_env, ls_scf_env, unit_nr, iscf, &
diis_step, eps_diis, nmixing, s_matrix, threshold)
! Note.- Input: ls_scf_env%matrix_p(ispin) , Density Matrix
! matrix_ks (from qs_env) , Kohn-Sham Matrix (IN/OUT)
TYPE(qs_diis_buffer_type_sparse), POINTER :: diis_buffer
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(ls_scf_env_type) :: ls_scf_env
INTEGER, INTENT(IN) :: unit_nr, iscf
LOGICAL, INTENT(OUT) :: diis_step
REAL(KIND=dp), INTENT(IN) :: eps_diis
INTEGER, INTENT(IN), OPTIONAL :: nmixing
TYPE(dbcsr_type), OPTIONAL :: s_matrix
REAL(KIND=dp), INTENT(IN) :: threshold
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_diis_b_step_4lscf'
REAL(KIND=dp), PARAMETER :: eigenvalue_threshold = 1.0E-12_dp
INTEGER :: handle, ib, ispin, jb, my_nmixing, nb, &
nb1, nspin
REAL(KIND=dp) :: error_max, tmp
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: ev
REAL(KIND=dp), DIMENSION(:, :), POINTER :: a, b
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
TYPE(dbcsr_type) :: matrix_KSerr_t, matrix_tmp
TYPE(dbcsr_type), POINTER :: new_errors, old_errors, parameters
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
nspin = ls_scf_env%nspins
diis_step = .FALSE.
my_nmixing = 2
IF (PRESENT(nmixing)) my_nmixing = nmixing
NULLIFY (new_errors, old_errors, parameters, a, b)
logger => cp_get_default_logger()
! Quick return, if no DIIS is requested
IF (diis_buffer%nbuffer < 1) THEN
CALL timestop(handle)
RETURN
END IF
! Getting current Kohn-Sham matrix from qs_env
CALL get_qs_env(qs_env, &
para_env=para_env, &
matrix_ks=matrix_ks)
CALL qs_diis_b_check_i_alloc_sparse( &
diis_buffer, &
ls_scf_env, &
nspin)
error_max = 0.0_dp
ib = MODULO(diis_buffer%ncall, diis_buffer%nbuffer) + 1
diis_buffer%ncall = diis_buffer%ncall + 1
nb = MIN(diis_buffer%ncall, diis_buffer%nbuffer)
! Create scratch arrays
CALL dbcsr_create(matrix_tmp, &
template=ls_scf_env%matrix_ks(1), &
matrix_type='N')
CALL dbcsr_set(matrix_tmp, 0.0_dp) ! reset matrix
CALL dbcsr_create(matrix_KSerr_t, &
template=ls_scf_env%matrix_ks(1), &
matrix_type='N')
CALL dbcsr_set(matrix_KSerr_t, 0.0_dp) ! reset matrix
DO ispin = 1, nspin ! ------ Loop-ispin----START
new_errors => diis_buffer%error(ib, ispin)%matrix
parameters => diis_buffer%param(ib, ispin)%matrix
! Copy the Kohn-Sham matrix K to the DIIS buffer
CALL dbcsr_copy(parameters, & ! out
matrix_ks(ispin)%matrix) ! in
IF (PRESENT(s_matrix)) THEN ! if-s_matrix ---------- START
! Calculate Kohn-Sham error (non-orthogonal)= K*P*S-(K*P*S)^T
! matrix_tmp = P*S
CALL dbcsr_multiply("N", "N", &
1.0_dp, ls_scf_env%matrix_p(ispin), &
s_matrix, &
0.0_dp, matrix_tmp, &
filter_eps=threshold)
! new_errors= K*P*S
CALL dbcsr_multiply("N", "N", &
1.0_dp, matrix_ks(ispin)%matrix, &
matrix_tmp, &
0.0_dp, new_errors, &
filter_eps=threshold)
! matrix_KSerr_t= transpose(K*P*S)
CALL dbcsr_transposed(matrix_KSerr_t, &
new_errors)
! new_errors=K*P*S-transpose(K*P*S)
CALL dbcsr_add(new_errors, &
matrix_KSerr_t, &
1.0_dp, -1.0_dp)
ELSE ! if-s_matrix ---------- MID
! Calculate Kohn-Sham error (orthogonal)= K*P - P*K
! new_errors=K*P
CALL dbcsr_multiply("N", "N", &
1.0_dp, matrix_ks(ispin)%matrix, &
ls_scf_env%matrix_p(ispin), &
0.0_dp, new_errors, &
filter_eps=threshold)
! matrix_KSerr_t= transpose(K*P)
CALL dbcsr_transposed(matrix_KSerr_t, &
new_errors)
! new_errors=K*P-transpose(K*P)
CALL dbcsr_add(new_errors, &
matrix_KSerr_t, &
1.0_dp, -1.0_dp)
END IF ! if-s_matrix ---------- END
tmp = dbcsr_maxabs(new_errors)
error_max = MAX(error_max, tmp)
END DO ! ------ Loop-ispin----END
! Check, if a DIIS step is appropriate
diis_step = (diis_buffer%ncall >= my_nmixing)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(A29,I3,A3,4(I3,A1))') &
"DIIS: (ncall,nbuffer,ib,nb)=(", iscf, ")=(", &
diis_buffer%ncall, ",", diis_buffer%nbuffer, ",", ib, ",", nb, ")"
WRITE (unit_nr, '(A57,I3,A3,L1,A1,F10.8,A1,F4.2,A1,L1,A1)') &
"DIIS: (diis_step,error_max,eps_diis,error_max<eps_diis)=(", &
iscf, ")=(", diis_step, ",", error_max, ",", eps_diis, ",", &
(error_max < eps_diis), ")"
WRITE (unit_nr, '(A75)') &
"DIIS: diis_step=T : Perform DIIS error_max<eps_diis=T : Update DIIS buffer"
END IF
! Update the SCF DIIS buffer
IF (error_max < eps_diis) THEN
b => diis_buffer%b_matrix
DO jb = 1, nb
b(jb, ib) = 0.0_dp
DO ispin = 1, nspin
old_errors => diis_buffer%error(jb, ispin)%matrix
new_errors => diis_buffer%error(ib, ispin)%matrix
CALL dbcsr_dot(old_errors, &
new_errors, &
tmp) ! out : < f_i | f_j >
b(jb, ib) = b(jb, ib) + tmp
END DO ! end-loop-ispin
b(ib, jb) = b(jb, ib)
END DO ! end-loop-jb
ELSE
diis_step = .FALSE.
END IF
! Perform DIIS step
IF (diis_step) THEN
nb1 = nb + 1
ALLOCATE (a(nb1, nb1))
ALLOCATE (b(nb1, nb1))
ALLOCATE (ev(nb1))
! Set up the linear DIIS equation system
b(1:nb, 1:nb) = diis_buffer%b_matrix(1:nb, 1:nb)
b(1:nb, nb1) = -1.0_dp
b(nb1, 1:nb) = -1.0_dp
b(nb1, nb1) = 0.0_dp
! Solve the linear DIIS equation system
CALL diamat_all(b(1:nb1, 1:nb1), ev(1:nb1))
a(1:nb1, 1:nb1) = b(1:nb1, 1:nb1)
DO jb = 1, nb1
IF (ABS(ev(jb)) < eigenvalue_threshold) THEN
a(1:nb1, jb) = 0.0_dp
ELSE
a(1:nb1, jb) = a(1:nb1, jb)/ev(jb)
END IF
END DO ! end-loop-jb
ev(1:nb) = MATMUL(a(1:nb, 1:nb1), b(nb1, 1:nb1))
! Update Kohn-Sham matrix
IF (iscf .GE. ls_scf_env%iter_ini_diis) THEN ! if-iscf-to-updateKS------ START
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(A40,I3)') 'DIIS: Updating Kohn-Sham matrix at iscf=', iscf
END IF
DO ispin = 1, nspin
CALL dbcsr_set(matrix_ks(ispin)%matrix, & ! reset matrix
0.0_dp)
DO jb = 1, nb
parameters => diis_buffer%param(jb, ispin)%matrix
CALL dbcsr_add(matrix_ks(ispin)%matrix, parameters, &
1.0_dp, -ev(jb))
END DO ! end-loop-jb
END DO ! end-loop-ispin
END IF ! if-iscf-to-updateKS------ END
DEALLOCATE (a)
DEALLOCATE (b)
DEALLOCATE (ev)
ELSE
DO ispin = 1, nspin
parameters => diis_buffer%param(ib, ispin)%matrix
CALL dbcsr_copy(parameters, & ! out
matrix_ks(ispin)%matrix) ! in
END DO ! end-loop-ispin
END IF
CALL dbcsr_release(matrix_tmp)
CALL dbcsr_release(matrix_KSerr_t)
CALL timestop(handle)
END SUBROUTINE qs_diis_b_step_4lscf
! **************************************************************************************************
!> \brief Allocate and initialize a DIIS buffer with a buffer size of nbuffer.
!> \param diis_buffer the buffer to initialize
!> \param ls_scf_env ...
!> \param nspin ...
!> \par History
!> - Adapted from qs_diis_b_check_i_alloc for sparse matrices and
!> used in LS-SCF module (ls_scf_main) (10-11-14)
!> \author Fredy W. Aquino
!> \note
!> check to allocate matrices only when needed
! **************************************************************************************************
SUBROUTINE qs_diis_b_check_i_alloc_sparse(diis_buffer, ls_scf_env, &
nspin)
TYPE(qs_diis_buffer_type_sparse), INTENT(INOUT) :: diis_buffer
TYPE(ls_scf_env_type) :: ls_scf_env
INTEGER, INTENT(IN) :: nspin
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_diis_b_check_i_alloc_sparse'
INTEGER :: handle, ibuffer, ispin, nbuffer
TYPE(cp_logger_type), POINTER :: logger
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
nbuffer = diis_buffer%nbuffer
IF (.NOT. ASSOCIATED(diis_buffer%error)) THEN
ALLOCATE (diis_buffer%error(nbuffer, nspin))
DO ispin = 1, nspin
DO ibuffer = 1, nbuffer
ALLOCATE (diis_buffer%error(ibuffer, ispin)%matrix)
CALL dbcsr_create(diis_buffer%error(ibuffer, ispin)%matrix, &
template=ls_scf_env%matrix_ks(1), &
matrix_type='N')
END DO
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%param)) THEN
ALLOCATE (diis_buffer%param(nbuffer, nspin))
DO ispin = 1, nspin
DO ibuffer = 1, nbuffer
ALLOCATE (diis_buffer%param(ibuffer, ispin)%matrix)
CALL dbcsr_create(diis_buffer%param(ibuffer, ispin)%matrix, &
template=ls_scf_env%matrix_ks(1), &
matrix_type='N')
END DO
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%b_matrix)) THEN
ALLOCATE (diis_buffer%b_matrix(nbuffer + 1, nbuffer + 1))
diis_buffer%b_matrix = 0.0_dp
END IF
CALL timestop(handle)
END SUBROUTINE qs_diis_b_check_i_alloc_sparse
! **************************************************************************************************
!> \brief clears the DIIS buffer in LS-SCF calculation
!> \param diis_buffer the buffer to clear
!> \par History
!> 10-11-14 created [FA] modified from qs_diis_b_clear
!> \author Fredy W. Aquino
! **************************************************************************************************
PURE SUBROUTINE qs_diis_b_clear_sparse(diis_buffer)
TYPE(qs_diis_buffer_type_sparse), INTENT(INOUT) :: diis_buffer
diis_buffer%ncall = 0
END SUBROUTINE qs_diis_b_clear_sparse
! **************************************************************************************************
!> \brief Allocates an SCF DIIS buffer for LS-SCF calculation
!> \param diis_buffer the buffer to create
!> \param nbuffer ...
!> \par History
!> 10-11-14 created [FA] modified from qs_diis_b_create
!> \author Fredy W. Aquino
! **************************************************************************************************
PURE SUBROUTINE qs_diis_b_create_sparse(diis_buffer, nbuffer)
TYPE(qs_diis_buffer_type_sparse), INTENT(OUT) :: diis_buffer
INTEGER, INTENT(in) :: nbuffer
NULLIFY (diis_buffer%b_matrix)
NULLIFY (diis_buffer%error)
NULLIFY (diis_buffer%param)
diis_buffer%nbuffer = nbuffer
diis_buffer%ncall = 0
END SUBROUTINE qs_diis_b_create_sparse
! **************************************************************************************************
!> \brief Allocates an SCF DIIS buffer for k-points
!> \param diis_buffer the buffer to create
!> \param nbuffer ...
! **************************************************************************************************
SUBROUTINE qs_diis_b_create_kp(diis_buffer, nbuffer)
TYPE(qs_diis_buffer_type_kp), INTENT(OUT) :: diis_buffer
INTEGER, INTENT(in) :: nbuffer
NULLIFY (diis_buffer%b_matrix)
NULLIFY (diis_buffer%error)
NULLIFY (diis_buffer%param)
NULLIFY (diis_buffer%smat)
diis_buffer%nbuffer = nbuffer
diis_buffer%ncall = 0
END SUBROUTINE qs_diis_b_create_kp
! **************************************************************************************************
!> \brief Allocate and initialize a DIIS buffer for nao*nao parameter
!> variables and with a buffer size of nbuffer, in the k-point case
!> \param diis_buffer the buffer to initialize
!> \param matrix_struct the structure for the matrix of the buffer note: this is in the kp subgroup
!> \param nspin ...
!> \param nkp ...
!> \param scf_section ...
! **************************************************************************************************
SUBROUTINE qs_diis_b_check_i_alloc_kp(diis_buffer, matrix_struct, nspin, nkp, scf_section)
TYPE(qs_diis_buffer_type_kp), INTENT(INOUT) :: diis_buffer
TYPE(cp_fm_struct_type), POINTER :: matrix_struct
INTEGER, INTENT(IN) :: nspin, nkp
TYPE(section_vals_type), POINTER :: scf_section
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_diis_b_check_i_alloc_kp'
INTEGER :: handle, ibuffer, ikp, ispin, nbuffer, &
output_unit
TYPE(cp_logger_type), POINTER :: logger
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
nbuffer = diis_buffer%nbuffer
IF (.NOT. ASSOCIATED(diis_buffer%error)) THEN
ALLOCATE (diis_buffer%error(nbuffer, nspin, nkp))
DO ikp = 1, nkp
DO ispin = 1, nspin
DO ibuffer = 1, nbuffer
CALL cp_cfm_create(diis_buffer%error(ibuffer, ispin, ikp), &
name="qs_diis_b%error("// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//","// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//")", &
matrix_struct=matrix_struct)
END DO
END DO
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%param)) THEN
ALLOCATE (diis_buffer%param(nbuffer, nspin, nkp))
DO ikp = 1, nkp
DO ispin = 1, nspin
DO ibuffer = 1, nbuffer
CALL cp_cfm_create(diis_buffer%param(ibuffer, ispin, ikp), &
name="qs_diis_b%param("// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//","// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//")", &
matrix_struct=matrix_struct)
END DO
END DO
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%smat)) THEN
ALLOCATE (diis_buffer%smat(nkp))
DO ikp = 1, nkp
CALL cp_cfm_create(diis_buffer%smat(ikp), &
name="kp_cfm_smat("// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//","// &
TRIM(ADJUSTL(cp_to_string(ibuffer)))//")", &
matrix_struct=matrix_struct)
END DO
END IF
IF (.NOT. ASSOCIATED(diis_buffer%b_matrix)) THEN
ALLOCATE (diis_buffer%b_matrix(nbuffer + 1, nbuffer + 1))
diis_buffer%b_matrix = 0.0_dp
output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DIIS_INFO", &
extension=".scfLog")
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/,T9,A)") &
"DIIS | The SCF DIIS buffer was allocated and initialized"
END IF
CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
"PRINT%DIIS_INFO")
END IF
CALL timestop(handle)
END SUBROUTINE qs_diis_b_check_i_alloc_kp
! **************************************************************************************************
!> \brief clears the buffer
!> \param diis_buffer the buffer to clear
! **************************************************************************************************
PURE SUBROUTINE qs_diis_b_clear_kp(diis_buffer)
TYPE(qs_diis_buffer_type_kp), INTENT(INOUT) :: diis_buffer
diis_buffer%ncall = 0
END SUBROUTINE qs_diis_b_clear_kp
! **************************************************************************************************
!> \brief Update info about the current buffer step ib and the current number of buffers nb
!> \param diis_buffer ...
!> \param ib ...
!> \param nb ...
! **************************************************************************************************
SUBROUTINE qs_diis_b_info_kp(diis_buffer, ib, nb)
TYPE(qs_diis_buffer_type_kp), POINTER :: diis_buffer
INTEGER, INTENT(OUT) :: ib, nb
ib = MODULO(diis_buffer%ncall, diis_buffer%nbuffer) + 1
diis_buffer%ncall = diis_buffer%ncall + 1
nb = MIN(diis_buffer%ncall, diis_buffer%nbuffer)
END SUBROUTINE qs_diis_b_info_kp
! **************************************************************************************************
!> \brief Calculate and store the error for a given k-point
!> \param diis_buffer ...
!> \param ib ...
!> \param mos ...