-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_scf.F
1718 lines (1554 loc) · 86.8 KB
/
qs_scf.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 for the Quickstep SCF run.
!> \par History
!> - Joost VandeVondele (02.2002)
!> added code for: incremental (pab and gvg) update
!> initialisation (init_cube, l_info)
!> - Joost VandeVondele (02.2002)
!> called the poisson code of the classical part
!> this takes into account the spherical cutoff and allows for
!> isolated systems
!> - Joost VandeVondele (02.2002)
!> added multiple grid feature
!> changed to spherical cutoff consistently (?)
!> therefore removed the gradient correct functionals
!> - updated with the new QS data structures (10.04.02,MK)
!> - copy_matrix replaced by transfer_matrix (11.04.02,MK)
!> - nrebuild_rho and nrebuild_gvg unified (12.04.02,MK)
!> - set_mo_occupation for smearing of the MO occupation numbers
!> (17.04.02,MK)
!> - MO level shifting added (22.04.02,MK)
!> - Usage of TYPE mo_set_p_type
!> - Joost VandeVondele (05.2002)
!> added cholesky based diagonalisation
!> - 05.2002 added pao method [fawzi]
!> - parallel FFT (JGH 22.05.2002)
!> - 06.2002 moved KS matrix construction to qs_build_KS_matrix.F [fawzi]
!> - started to include more LSD (01.2003,Joost VandeVondele)
!> - 02.2003 scf_env [fawzi]
!> - got rid of nrebuild (01.2004, Joost VandeVondele)
!> - 10.2004 removed pao [fawzi]
!> - 03.2006 large cleaning action [Joost VandeVondele]
!> - High-spin ROKS added (05.04.06,MK)
!> - Mandes (10.2013)
!> intermediate energy communication with external communicator added
!> - kpoints (08.2014, JGH)
!> - unified k-point and gamma-point code (2014.11) [Ole Schuett]
!> - added extra SCF loop for CDFT constraints (12.2015) [Nico Holmberg]
!> \author Matthias Krack (30.04.2001)
! **************************************************************************************************
MODULE qs_scf
USE atomic_kind_types, ONLY: atomic_kind_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: dbcsr_copy,&
dbcsr_deallocate_matrix,&
dbcsr_get_info,&
dbcsr_init_p,&
dbcsr_p_type,&
dbcsr_set,&
dbcsr_type
USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
dbcsr_deallocate_matrix_set
USE cp_files, ONLY: close_file
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_release,&
cp_fm_to_fm,&
cp_fm_type
USE cp_log_handling, ONLY: cp_add_default_logger,&
cp_get_default_logger,&
cp_logger_release,&
cp_logger_type,&
cp_rm_default_logger,&
cp_to_string
USE cp_output_handling, ONLY: cp_add_iter_level,&
cp_iterate,&
cp_p_file,&
cp_print_key_should_output,&
cp_print_key_unit_nr,&
cp_rm_iter_level
USE cp_result_methods, ONLY: get_results,&
test_for_result
USE cp_result_types, ONLY: cp_result_type
USE ec_env_types, ONLY: energy_correction_type
USE input_constants, ONLY: &
broyden_type_1, broyden_type_1_explicit, broyden_type_1_explicit_ls, broyden_type_1_ls, &
broyden_type_2, broyden_type_2_explicit, broyden_type_2_explicit_ls, broyden_type_2_ls, &
cdft2ot, history_guess, ot2cdft, ot_precond_full_all, ot_precond_full_single, &
ot_precond_full_single_inverse, ot_precond_none, ot_precond_s_inverse, &
outer_scf_becke_constraint, outer_scf_hirshfeld_constraint, outer_scf_optimizer_broyden, &
outer_scf_optimizer_newton_ls
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type
USE kinds, ONLY: default_path_length,&
default_string_length,&
dp
USE kpoint_io, ONLY: write_kpoints_restart
USE kpoint_types, ONLY: kpoint_type
USE machine, ONLY: m_flush,&
m_walltime
USE mathlib, ONLY: invert_matrix
USE message_passing, ONLY: mp_comm_type,&
mp_para_env_type
USE particle_types, ONLY: particle_type
USE preconditioner, ONLY: prepare_preconditioner,&
restart_preconditioner
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_pool_types, ONLY: pw_pool_type
USE qs_block_davidson_types, ONLY: block_davidson_deallocate
USE qs_cdft_scf_utils, ONLY: build_diagonal_jacobian,&
create_tmp_logger,&
initialize_inverse_jacobian,&
prepare_jacobian_stencil,&
print_inverse_jacobian,&
restart_inverse_jacobian
USE qs_cdft_types, ONLY: cdft_control_type
USE qs_charges_types, ONLY: qs_charges_type
USE qs_density_matrices, ONLY: calculate_density_matrix
USE qs_density_mixing_types, ONLY: gspace_mixing_nr
USE qs_diis, ONLY: qs_diis_b_clear,&
qs_diis_b_clear_kp,&
qs_diis_b_create,&
qs_diis_b_create_kp
USE qs_energy_types, ONLY: qs_energy_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_integrate_potential, ONLY: integrate_v_rspace
USE qs_kind_types, ONLY: qs_kind_type
USE qs_ks_methods, ONLY: qs_ks_update_qs_env
USE qs_ks_types, ONLY: qs_ks_did_change,&
qs_ks_env_type
USE qs_mo_io, ONLY: write_mo_set_to_restart
USE qs_mo_methods, ONLY: make_basis_simple,&
make_basis_sm
USE qs_mo_occupation, ONLY: set_mo_occupation
USE qs_mo_types, ONLY: deallocate_mo_set,&
duplicate_mo_set,&
get_mo_set,&
mo_set_type,&
reassign_allocated_mos
USE qs_ot, ONLY: qs_ot_new_preconditioner
USE qs_ot_scf, ONLY: ot_scf_init,&
ot_scf_read_input
USE qs_outer_scf, ONLY: outer_loop_gradient,&
outer_loop_optimize,&
outer_loop_purge_history,&
outer_loop_switch,&
outer_loop_update_qs_env
USE qs_rho_methods, ONLY: qs_rho_update_rho
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE qs_scf_initialization, ONLY: qs_scf_env_initialize
USE qs_scf_loop_utils, ONLY: qs_scf_check_inner_exit,&
qs_scf_check_outer_exit,&
qs_scf_density_mixing,&
qs_scf_inner_finalize,&
qs_scf_new_mos,&
qs_scf_new_mos_kp,&
qs_scf_rho_update,&
qs_scf_set_loop_flags
USE qs_scf_output, ONLY: qs_scf_cdft_info,&
qs_scf_cdft_initial_info,&
qs_scf_loop_info,&
qs_scf_loop_print,&
qs_scf_outer_loop_info,&
qs_scf_write_mos
USE qs_scf_post_scf, ONLY: qs_scf_compute_properties
USE qs_scf_types, ONLY: &
block_davidson_diag_method_nr, block_krylov_diag_method_nr, filter_matrix_diag_method_nr, &
general_diag_method_nr, ot_diag_method_nr, ot_method_nr, qs_scf_env_type, &
smeagol_method_nr, special_diag_method_nr
USE qs_wf_history_methods, ONLY: wfi_purge_history,&
wfi_update
USE scf_control_types, ONLY: scf_control_type
USE smeagol_interface, ONLY: run_smeagol_bulktrans,&
run_smeagol_emtrans
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf'
LOGICAL, PRIVATE :: reuse_precond = .FALSE.
LOGICAL, PRIVATE :: used_history = .FALSE.
PUBLIC :: scf, scf_env_cleanup, scf_env_do_scf, cdft_scf, init_scf_loop
CONTAINS
! **************************************************************************************************
!> \brief perform an scf procedure in the given qs_env
!> \param qs_env the qs_environment where to perform the scf procedure
!> \param has_converged ...
!> \param total_scf_steps ...
!> \par History
!> 02.2003 introduced scf_env, moved real work to scf_env_do_scf [fawzi]
!> \author fawzi
!> \note
! **************************************************************************************************
SUBROUTINE scf(qs_env, has_converged, total_scf_steps)
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(OUT), OPTIONAL :: has_converged
INTEGER, INTENT(OUT), OPTIONAL :: total_scf_steps
INTEGER :: ihistory, max_scf_tmp, tsteps
LOGICAL :: converged, outer_scf_loop, should_stop
LOGICAL, SAVE :: first_step_flag = .TRUE.
REAL(KIND=dp), DIMENSION(:, :), POINTER :: gradient_history, variable_history
TYPE(cp_logger_type), POINTER :: logger
TYPE(dft_control_type), POINTER :: dft_control
TYPE(qs_scf_env_type), POINTER :: scf_env
TYPE(scf_control_type), POINTER :: scf_control
TYPE(section_vals_type), POINTER :: dft_section, input, scf_section
NULLIFY (scf_env)
logger => cp_get_default_logger()
CPASSERT(ASSOCIATED(qs_env))
IF (PRESENT(has_converged)) THEN
has_converged = .FALSE.
END IF
IF (PRESENT(total_scf_steps)) THEN
total_scf_steps = 0
END IF
CALL get_qs_env(qs_env, scf_env=scf_env, input=input, &
dft_control=dft_control, scf_control=scf_control)
IF (scf_control%max_scf > 0) THEN
dft_section => section_vals_get_subs_vals(input, "DFT")
scf_section => section_vals_get_subs_vals(dft_section, "SCF")
IF (.NOT. ASSOCIATED(scf_env)) THEN
CALL qs_scf_env_initialize(qs_env, scf_env)
! Moved here from qs_scf_env_initialize to be able to have more scf_env
CALL set_qs_env(qs_env, scf_env=scf_env)
ELSE
CALL qs_scf_env_initialize(qs_env, scf_env)
END IF
IF ((scf_control%density_guess .EQ. history_guess) .AND. (first_step_flag)) THEN
max_scf_tmp = scf_control%max_scf
scf_control%max_scf = 1
outer_scf_loop = scf_control%outer_scf%have_scf
scf_control%outer_scf%have_scf = .FALSE.
END IF
IF (.NOT. dft_control%qs_control%cdft) THEN
CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
ELSE
! Third SCF loop needed for CDFT with OT to properly restart OT inner loop
CALL cdft_scf(qs_env=qs_env, should_stop=should_stop)
END IF
! If SCF has not converged, then we should not start MP2
IF (ASSOCIATED(qs_env%mp2_env)) qs_env%mp2_env%hf_fail = .NOT. converged
! Add the converged outer_scf SCF gradient(s)/variable(s) to history
IF (scf_control%outer_scf%have_scf) THEN
ihistory = scf_env%outer_scf%iter_count
CALL get_qs_env(qs_env, gradient_history=gradient_history, &
variable_history=variable_history)
! We only store the latest two values
gradient_history(:, 1) = gradient_history(:, 2)
gradient_history(:, 2) = scf_env%outer_scf%gradient(:, ihistory)
variable_history(:, 1) = variable_history(:, 2)
variable_history(:, 2) = scf_env%outer_scf%variables(:, ihistory)
! Reset flag
IF (used_history) used_history = .FALSE.
! Update a counter and check if the Jacobian should be deallocated
IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian)) THEN
scf_control%outer_scf%cdft_opt_control%ijacobian(2) = scf_control%outer_scf%cdft_opt_control%ijacobian(2) + 1
IF (scf_control%outer_scf%cdft_opt_control%ijacobian(2) .GE. &
scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) .AND. &
scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) > 0) &
scf_env%outer_scf%deallocate_jacobian = .TRUE.
END IF
END IF
! *** add the converged wavefunction to the wavefunction history
IF ((ASSOCIATED(qs_env%wf_history)) .AND. &
((scf_control%density_guess .NE. history_guess) .OR. &
(.NOT. first_step_flag))) THEN
IF (.NOT. dft_control%qs_control%cdft) THEN
CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
ELSE
IF (dft_control%qs_control%cdft_control%should_purge) THEN
CALL wfi_purge_history(qs_env)
CALL outer_loop_purge_history(qs_env)
dft_control%qs_control%cdft_control%should_purge = .FALSE.
ELSE
CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
END IF
END IF
ELSE IF ((scf_control%density_guess .EQ. history_guess) .AND. &
(first_step_flag)) THEN
scf_control%max_scf = max_scf_tmp
scf_control%outer_scf%have_scf = outer_scf_loop
first_step_flag = .FALSE.
END IF
! *** compute properties that depend on the converged wavefunction
IF (.NOT. (should_stop)) CALL qs_scf_compute_properties(qs_env)
! *** SMEAGOL interface ***
IF (.NOT. (should_stop)) THEN
! compute properties that depend on the converged wavefunction ..
CALL run_smeagol_emtrans(qs_env, last=.TRUE., iter=0)
! .. or save matrices related to bulk leads
CALL run_smeagol_bulktrans(qs_env)
END IF
! *** cleanup
CALL scf_env_cleanup(scf_env)
IF (dft_control%qs_control%cdft) &
CALL cdft_control_cleanup(dft_control%qs_control%cdft_control)
IF (PRESENT(has_converged)) THEN
has_converged = converged
END IF
IF (PRESENT(total_scf_steps)) THEN
total_scf_steps = tsteps
END IF
END IF
END SUBROUTINE scf
! **************************************************************************************************
!> \brief perform an scf loop
!> \param scf_env the scf_env where to perform the scf procedure
!> \param scf_control ...
!> \param qs_env the qs_env, the scf_env lives in
!> \param converged will be true / false if converged is reached
!> \param should_stop ...
!> \param total_scf_steps ...
!> \par History
!> long history, see cvs and qs_scf module history
!> 02.2003 introduced scf_env [fawzi]
!> 09.2005 Frozen density approximation [TdK]
!> 06.2007 Check for SCF iteration count early [jgh]
!> 10.2019 switch_surf_dip [SGh]
!> \author Matthias Krack
!> \note
! **************************************************************************************************
SUBROUTINE scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
TYPE(qs_scf_env_type), POINTER :: scf_env
TYPE(scf_control_type), POINTER :: scf_control
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(OUT) :: converged, should_stop
INTEGER, INTENT(OUT) :: total_scf_steps
CHARACTER(LEN=*), PARAMETER :: routineN = 'scf_env_do_scf'
CHARACTER(LEN=default_string_length) :: description, name
INTEGER :: ext_master_id, handle, handle2, i_tmp, &
ic, ispin, iter_count, output_unit, &
scf_energy_message_tag, total_steps
LOGICAL :: diis_step, do_kpoints, energy_only, exit_inner_loop, exit_outer_loop, &
inner_loop_converged, just_energy, outer_loop_converged
REAL(KIND=dp) :: t1, t2
REAL(KIND=dp), DIMENSION(3) :: res_val_3
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cp_logger_type), POINTER :: logger
TYPE(cp_result_type), POINTER :: results
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
TYPE(dft_control_type), POINTER :: dft_control
TYPE(energy_correction_type), POINTER :: ec_env
TYPE(kpoint_type), POINTER :: kpoints
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos, mos_last_converged
TYPE(mp_comm_type) :: external_comm
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(pw_env_type), POINTER :: pw_env
TYPE(qs_charges_type), POINTER :: qs_charges
TYPE(qs_energy_type), POINTER :: energy
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_rho_type), POINTER :: rho
TYPE(section_vals_type), POINTER :: dft_section, input, scf_section
CALL timeset(routineN, handle)
NULLIFY (dft_control, rho, energy, &
logger, qs_charges, ks_env, mos, atomic_kind_set, qs_kind_set, &
particle_set, dft_section, input, &
scf_section, para_env, results, kpoints, pw_env, rho_ao_kp, mos_last_converged)
CPASSERT(ASSOCIATED(scf_env))
CPASSERT(ASSOCIATED(qs_env))
logger => cp_get_default_logger()
t1 = m_walltime()
CALL get_qs_env(qs_env=qs_env, &
energy=energy, &
particle_set=particle_set, &
qs_charges=qs_charges, &
ks_env=ks_env, &
atomic_kind_set=atomic_kind_set, &
qs_kind_set=qs_kind_set, &
rho=rho, &
mos=mos, &
input=input, &
dft_control=dft_control, &
do_kpoints=do_kpoints, &
kpoints=kpoints, &
results=results, &
pw_env=pw_env, &
para_env=para_env)
CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
dft_section => section_vals_get_subs_vals(input, "DFT")
scf_section => section_vals_get_subs_vals(dft_section, "SCF")
output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
extension=".scfLog")
IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
"SCF WAVEFUNCTION OPTIMIZATION"
! when switch_surf_dip is switched on, indicate storing mos from the last converged step
IF (dft_control%switch_surf_dip) THEN
CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
DO ispin = 1, dft_control%nspins
CALL reassign_allocated_mos(mos(ispin), mos_last_converged(ispin))
END DO
IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
"COPIED mos_last_converged ---> mos"
END IF
IF ((output_unit > 0) .AND. (.NOT. scf_control%use_ot)) THEN
WRITE (UNIT=output_unit, &
FMT="(/,T3,A,T12,A,T31,A,T39,A,T59,A,T75,A,/,T3,A)") &
"Step", "Update method", "Time", "Convergence", "Total energy", "Change", &
REPEAT("-", 78)
END IF
CALL cp_add_iter_level(logger%iter_info, "QS_SCF")
! check for external communicator and if the intermediate energy should be sent
res_val_3(:) = -1.0_dp
description = "[EXT_SCF_ENER_COMM]"
IF (test_for_result(results, description=description)) THEN
CALL get_results(results, description=description, &
values=res_val_3, n_entries=i_tmp)
CPASSERT(i_tmp .EQ. 3)
IF (ALL(res_val_3(:) .LE. 0.0)) &
CALL cp_abort(__LOCATION__, &
" Trying to access result ("//TRIM(description)// &
") which is not correctly stored.")
CALL external_comm%set_handle(NINT(res_val_3(1)))
END IF
ext_master_id = NINT(res_val_3(2))
scf_energy_message_tag = NINT(res_val_3(3))
! *** outer loop of the scf, can treat other variables,
! *** such as lagrangian multipliers
scf_env%outer_scf%iter_count = 0
iter_count = 0
total_steps = 0
energy%tot_old = 0.0_dp
scf_outer_loop: DO
CALL init_scf_loop(scf_env=scf_env, qs_env=qs_env, &
scf_section=scf_section)
CALL qs_scf_set_loop_flags(scf_env, diis_step, &
energy_only, just_energy, exit_inner_loop)
! decide whether to switch off dipole correction for convergence purposes
dft_control%surf_dip_correct_switch = dft_control%correct_surf_dip
IF ((dft_control%correct_surf_dip) .AND. (scf_control%outer_scf%have_scf) .AND. &
(scf_env%outer_scf%iter_count > FLOOR(scf_control%outer_scf%max_scf/2.0_dp))) THEN
IF (dft_control%switch_surf_dip) THEN
dft_control%surf_dip_correct_switch = .FALSE.
IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
"SURFACE DIPOLE CORRECTION switched off"
END IF
END IF
scf_loop: DO
CALL timeset(routineN//"_inner_loop", handle2)
scf_env%iter_count = scf_env%iter_count + 1
iter_count = iter_count + 1
CALL cp_iterate(logger%iter_info, last=.FALSE., iter_nr=iter_count)
IF (output_unit > 0) CALL m_flush(output_unit)
total_steps = total_steps + 1
just_energy = energy_only
CALL qs_ks_update_qs_env(qs_env, just_energy=just_energy, &
calculate_forces=.FALSE.)
! print 'heavy weight' or relatively expensive quantities
CALL qs_scf_loop_print(qs_env, scf_env, para_env)
IF (do_kpoints) THEN
! kpoints
CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step)
ELSE
! Gamma points only
CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
END IF
! Print requested MO information (can be computationally expensive with OT)
CALL qs_scf_write_mos(qs_env, scf_env, final_mos=.FALSE.)
CALL qs_scf_density_mixing(scf_env, rho, para_env, diis_step)
t2 = m_walltime()
CALL qs_scf_loop_info(scf_env, output_unit, just_energy, t1, t2, energy)
IF (.NOT. just_energy) energy%tot_old = energy%total
! check for external communicator and if the intermediate energy should be sent
IF (scf_energy_message_tag .GT. 0) THEN
CALL external_comm%send(energy%total, ext_master_id, scf_energy_message_tag)
END IF
CALL qs_scf_check_inner_exit(qs_env, scf_env, scf_control, should_stop, exit_inner_loop, &
inner_loop_converged, output_unit)
! In case we decide to exit we perform few more check to see if this one
! is really the last SCF step
IF (exit_inner_loop) THEN
CALL qs_scf_inner_finalize(scf_env, qs_env, diis_step, output_unit)
CALL qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, &
outer_loop_converged, exit_outer_loop)
! Let's tag the last SCF cycle so we can print informations only of the last step
IF (exit_outer_loop) CALL cp_iterate(logger%iter_info, last=.TRUE., iter_nr=iter_count)
END IF
IF (do_kpoints) THEN
CALL write_kpoints_restart(rho_ao_kp, kpoints, scf_env, dft_section, particle_set, qs_kind_set)
ELSE
! Write Wavefunction restart file
CALL write_mo_set_to_restart(mos, particle_set, dft_section=dft_section, qs_kind_set=qs_kind_set)
END IF
! Exit if we have finished with the SCF inner loop
IF (exit_inner_loop) THEN
CALL timestop(handle2)
EXIT scf_loop
END IF
IF (.NOT. BTEST(cp_print_key_should_output(logger%iter_info, &
scf_section, "PRINT%ITERATION_INFO/TIME_CUMUL"), cp_p_file)) &
t1 = m_walltime()
! mixing methods have the new density matrix in p_mix_new
IF (scf_env%mixing_method > 0) THEN
DO ic = 1, SIZE(rho_ao_kp, 2)
DO ispin = 1, dft_control%nspins
CALL dbcsr_get_info(rho_ao_kp(ispin, ic)%matrix, name=name) ! keep the name
CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
END DO
END DO
END IF
CALL qs_scf_rho_update(rho, qs_env, scf_env, ks_env, &
mix_rho=scf_env%mixing_method >= gspace_mixing_nr)
CALL timestop(handle2)
END DO scf_loop
IF (.NOT. scf_control%outer_scf%have_scf) EXIT scf_outer_loop
! In case we use the OUTER SCF loop let's print some info..
CALL qs_scf_outer_loop_info(output_unit, scf_control, scf_env, &
energy, total_steps, should_stop, outer_loop_converged)
! save mos to converged mos if outer_loop_converged and surf_dip_correct_switch is true
IF (exit_outer_loop) THEN
IF ((dft_control%switch_surf_dip) .AND. (outer_loop_converged) .AND. &
(dft_control%surf_dip_correct_switch)) THEN
DO ispin = 1, dft_control%nspins
CALL reassign_allocated_mos(mos_last_converged(ispin), mos(ispin))
END DO
IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
"COPIED mos ---> mos_last_converged"
END IF
END IF
IF (exit_outer_loop) EXIT scf_outer_loop
!
CALL outer_loop_optimize(scf_env, scf_control)
CALL outer_loop_update_qs_env(qs_env, scf_env)
CALL qs_ks_did_change(ks_env, potential_changed=.TRUE.)
END DO scf_outer_loop
converged = inner_loop_converged .AND. outer_loop_converged
total_scf_steps = total_steps
IF (dft_control%qs_control%cdft) &
dft_control%qs_control%cdft_control%total_steps = &
dft_control%qs_control%cdft_control%total_steps + total_steps
IF (.NOT. converged) THEN
IF (scf_control%ignore_convergence_failure .OR. should_stop) THEN
CALL cp_warn(__LOCATION__, "SCF run NOT converged")
ELSE
CALL cp_abort(__LOCATION__, &
"SCF run NOT converged. To continue the calculation "// &
"regardless, please set the keyword IGNORE_CONVERGENCE_FAILURE.")
END IF
END IF
! Skip Harris functional calculation if ground-state is NOT converged
IF (qs_env%energy_correction) THEN
CALL get_qs_env(qs_env, ec_env=ec_env)
ec_env%do_skip = .FALSE.
IF (ec_env%skip_ec .AND. .NOT. converged) ec_env%do_skip = .TRUE.
END IF
! if needed copy mo_coeff dbcsr->fm for later use in post_scf!fm->dbcsr
DO ispin = 1, SIZE(mos) !fm -> dbcsr
IF (mos(ispin)%use_mo_coeff_b) THEN !fm->dbcsr
IF (.NOT. ASSOCIATED(mos(ispin)%mo_coeff_b)) & !fm->dbcsr
CPABORT("mo_coeff_b is not allocated") !fm->dbcsr
CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, & !fm->dbcsr
mos(ispin)%mo_coeff) !fm -> dbcsr
END IF !fm->dbcsr
END DO !fm -> dbcsr
CALL cp_rm_iter_level(logger%iter_info, level_name="QS_SCF")
CALL timestop(handle)
END SUBROUTINE scf_env_do_scf
! **************************************************************************************************
!> \brief inits those objects needed if you want to restart the scf with, say
!> only a new initial guess, or different density functional or ...
!> this will happen just before the scf loop starts
!> \param scf_env ...
!> \param qs_env ...
!> \param scf_section ...
!> \par History
!> 03.2006 created [Joost VandeVondele]
! **************************************************************************************************
SUBROUTINE init_scf_loop(scf_env, qs_env, scf_section)
TYPE(qs_scf_env_type), POINTER :: scf_env
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(section_vals_type), POINTER :: scf_section
CHARACTER(LEN=*), PARAMETER :: routineN = 'init_scf_loop'
INTEGER :: handle, ispin, nmo, number_of_OT_envs
LOGICAL :: do_kpoints, do_rotation, &
has_unit_metric, is_full_all
TYPE(cp_fm_type), POINTER :: mo_coeff
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
TYPE(dbcsr_type), POINTER :: orthogonality_metric
TYPE(dft_control_type), POINTER :: dft_control
TYPE(kpoint_type), POINTER :: kpoints
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
TYPE(scf_control_type), POINTER :: scf_control
CALL timeset(routineN, handle)
NULLIFY (scf_control, matrix_s, matrix_ks, dft_control, mos, mo_coeff, kpoints)
CPASSERT(ASSOCIATED(scf_env))
CPASSERT(ASSOCIATED(qs_env))
CALL get_qs_env(qs_env=qs_env, &
scf_control=scf_control, &
dft_control=dft_control, &
do_kpoints=do_kpoints, &
kpoints=kpoints, &
mos=mos)
! if using mo_coeff_b then copy to fm
DO ispin = 1, SIZE(mos) !fm->dbcsr
IF (mos(1)%use_mo_coeff_b) THEN !fm->dbcsr
CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, mos(ispin)%mo_coeff) !fm->dbcsr
END IF !fm->dbcsr
END DO !fm->dbcsr
! this just guarantees that all mo_occupations match the eigenvalues, if smear
DO ispin = 1, dft_control%nspins
! do not reset mo_occupations if the maximum overlap method is in use
IF (.NOT. scf_control%diagonalization%mom) &
CALL set_mo_occupation(mo_set=mos(ispin), &
smear=scf_control%smear)
END DO
SELECT CASE (scf_env%method)
CASE DEFAULT
CPABORT("unknown scf method method:"//cp_to_string(scf_env%method))
CASE (filter_matrix_diag_method_nr)
IF (.NOT. scf_env%skip_diis) THEN
IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
ALLOCATE (scf_env%scf_diis_buffer)
CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
END IF
CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
END IF
CASE (general_diag_method_nr, special_diag_method_nr, block_krylov_diag_method_nr, smeagol_method_nr)
IF (.NOT. scf_env%skip_diis) THEN
IF (do_kpoints) THEN
IF (.NOT. ASSOCIATED(kpoints%scf_diis_buffer)) THEN
ALLOCATE (kpoints%scf_diis_buffer)
CALL qs_diis_b_create_kp(kpoints%scf_diis_buffer, nbuffer=scf_control%max_diis)
END IF
CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
ELSE
IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
ALLOCATE (scf_env%scf_diis_buffer)
CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
END IF
CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
END IF
END IF
CASE (ot_diag_method_nr)
CALL get_qs_env(qs_env, matrix_ks=matrix_ks, matrix_s=matrix_s)
IF (.NOT. scf_env%skip_diis) THEN
IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
ALLOCATE (scf_env%scf_diis_buffer)
CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
END IF
CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
END IF
! disable DFTB and SE for now
IF (dft_control%qs_control%dftb .OR. &
dft_control%qs_control%xtb .OR. &
dft_control%qs_control%semi_empirical) THEN
CPABORT("DFTB and SE not available with OT/DIAG")
END IF
! if an old preconditioner is still around (i.e. outer SCF is active),
! remove it if this could be worthwhile
CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
scf_control%diagonalization%ot_settings%preconditioner_type, &
dft_control%nspins)
CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
scf_control%diagonalization%ot_settings%preconditioner_type, &
scf_control%diagonalization%ot_settings%precond_solver_type, &
scf_control%diagonalization%ot_settings%energy_gap, dft_control%nspins)
CASE (block_davidson_diag_method_nr)
! Preconditioner initialized within the loop, when required
CASE (ot_method_nr)
CALL get_qs_env(qs_env, &
has_unit_metric=has_unit_metric, &
matrix_s=matrix_s, &
matrix_ks=matrix_ks)
! reortho the wavefunctions if we are having an outer scf and
! this is not the first iteration
! this is useful to avoid the build-up of numerical noise
! however, we can not play this trick if restricted (don't mix non-equivalent orbs)
IF (scf_control%do_outer_scf_reortho) THEN
IF (scf_control%outer_scf%have_scf .AND. .NOT. dft_control%restricted) THEN
IF (scf_env%outer_scf%iter_count > 0) THEN
DO ispin = 1, dft_control%nspins
CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
IF (has_unit_metric) THEN
CALL make_basis_simple(mo_coeff, nmo)
ELSE
CALL make_basis_sm(mo_coeff, nmo, matrix_s(1)%matrix)
END IF
END DO
END IF
END IF
ELSE
! dont need any dirty trick for the numerically stable irac algorithm.
END IF
IF (.NOT. ASSOCIATED(scf_env%qs_ot_env)) THEN
! restricted calculations require just one set of OT orbitals
number_of_OT_envs = dft_control%nspins
IF (dft_control%restricted) number_of_OT_envs = 1
ALLOCATE (scf_env%qs_ot_env(number_of_OT_envs))
! XXX Joost XXX should disentangle reading input from this part
CALL ot_scf_read_input(scf_env%qs_ot_env, scf_section)
! keep a note that we are restricted
IF (dft_control%restricted) THEN
scf_env%qs_ot_env(1)%restricted = .TRUE.
! requires rotation
IF (.NOT. scf_env%qs_ot_env(1)%settings%do_rotation) &
CALL cp_abort(__LOCATION__, &
"Restricted calculation with OT requires orbital rotation. Please "// &
"activate the OT%ROTATION keyword!")
ELSE
scf_env%qs_ot_env(:)%restricted = .FALSE.
END IF
! this will rotate the MOs to be eigen states, which is not compatible with rotation
! e.g. mo_derivs here do not yet include potentially different occupations numbers
do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
! only full all needs rotation
is_full_all = scf_env%qs_ot_env(1)%settings%preconditioner_type == ot_precond_full_all
IF (do_rotation .AND. is_full_all) &
CPABORT('PRECONDITIONER FULL_ALL is not compatible with ROTATION.')
! might need the KS matrix to init properly
CALL qs_ks_update_qs_env(qs_env, just_energy=.FALSE., &
calculate_forces=.FALSE.)
! if an old preconditioner is still around (i.e. outer SCF is active),
! remove it if this could be worthwhile
IF (.NOT. reuse_precond) &
CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
scf_env%qs_ot_env(1)%settings%preconditioner_type, &
dft_control%nspins)
!
! preconditioning still needs to be done correctly with has_unit_metric
! notice that a big part of the preconditioning (S^-1) is fine anyhow
!
IF (has_unit_metric) THEN
NULLIFY (orthogonality_metric)
ELSE
orthogonality_metric => matrix_s(1)%matrix
END IF
IF (.NOT. reuse_precond) &
CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
scf_env%qs_ot_env(1)%settings%preconditioner_type, &
scf_env%qs_ot_env(1)%settings%precond_solver_type, &
scf_env%qs_ot_env(1)%settings%energy_gap, dft_control%nspins, &
has_unit_metric=has_unit_metric, &
chol_type=scf_env%qs_ot_env(1)%settings%cholesky_type)
IF (reuse_precond) reuse_precond = .FALSE.
CALL ot_scf_init(mo_array=mos, matrix_s=orthogonality_metric, &
broyden_adaptive_sigma=qs_env%broyden_adaptive_sigma, &
qs_ot_env=scf_env%qs_ot_env, matrix_ks=matrix_ks(1)%matrix)
SELECT CASE (scf_env%qs_ot_env(1)%settings%preconditioner_type)
CASE (ot_precond_none)
CASE (ot_precond_full_all, ot_precond_full_single_inverse)
DO ispin = 1, SIZE(scf_env%qs_ot_env)
CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
scf_env%ot_preconditioner(ispin)%preconditioner)
END DO
CASE (ot_precond_s_inverse, ot_precond_full_single)
DO ispin = 1, SIZE(scf_env%qs_ot_env)
CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
scf_env%ot_preconditioner(1)%preconditioner)
END DO
CASE DEFAULT
DO ispin = 1, SIZE(scf_env%qs_ot_env)
CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
scf_env%ot_preconditioner(1)%preconditioner)
END DO
END SELECT
END IF
! if we have non-uniform occupations we should be using rotation
do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
DO ispin = 1, SIZE(mos)
IF (.NOT. mos(ispin)%uniform_occupation) THEN
CPASSERT(do_rotation)
END IF
END DO
END SELECT
! another safety check
IF (dft_control%low_spin_roks) THEN
CPASSERT(scf_env%method == ot_method_nr)
do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
CPASSERT(do_rotation)
END IF
CALL timestop(handle)
END SUBROUTINE init_scf_loop
! **************************************************************************************************
!> \brief perform cleanup operations (like releasing temporary storage)
!> at the end of the scf
!> \param scf_env ...
!> \par History
!> 02.2003 created [fawzi]
!> \author fawzi
! **************************************************************************************************
SUBROUTINE scf_env_cleanup(scf_env)
TYPE(qs_scf_env_type), INTENT(INOUT) :: scf_env
CHARACTER(len=*), PARAMETER :: routineN = 'scf_env_cleanup'
INTEGER :: handle
CALL timeset(routineN, handle)
! Release SCF work storage
CALL cp_fm_release(scf_env%scf_work1)
IF (ASSOCIATED(scf_env%scf_work2)) THEN
CALL cp_fm_release(scf_env%scf_work2)
DEALLOCATE (scf_env%scf_work2)
END IF
IF (ASSOCIATED(scf_env%ortho)) THEN
CALL cp_fm_release(scf_env%ortho)
DEALLOCATE (scf_env%ortho)
END IF
IF (ASSOCIATED(scf_env%ortho_m1)) THEN
CALL cp_fm_release(scf_env%ortho_m1)
DEALLOCATE (scf_env%ortho_m1)
END IF
IF (ASSOCIATED(scf_env%ortho_dbcsr)) THEN
CALL dbcsr_deallocate_matrix(scf_env%ortho_dbcsr)
END IF
IF (ASSOCIATED(scf_env%buf1_dbcsr)) THEN
CALL dbcsr_deallocate_matrix(scf_env%buf1_dbcsr)
END IF
IF (ASSOCIATED(scf_env%buf2_dbcsr)) THEN
CALL dbcsr_deallocate_matrix(scf_env%buf2_dbcsr)
END IF
IF (ASSOCIATED(scf_env%p_mix_new)) THEN
CALL dbcsr_deallocate_matrix_set(scf_env%p_mix_new)
END IF
IF (ASSOCIATED(scf_env%p_delta)) THEN
CALL dbcsr_deallocate_matrix_set(scf_env%p_delta)
END IF
! Method dependent cleanup
SELECT CASE (scf_env%method)
CASE (ot_method_nr)
!
CASE (ot_diag_method_nr)
!
CASE (general_diag_method_nr)
!
CASE (special_diag_method_nr)
!
CASE (block_krylov_diag_method_nr)
CASE (block_davidson_diag_method_nr)
CALL block_davidson_deallocate(scf_env%block_davidson_env)
CASE (filter_matrix_diag_method_nr)
!
CASE (smeagol_method_nr)
!
CASE DEFAULT
CPABORT("unknown scf method method:"//cp_to_string(scf_env%method))
END SELECT
IF (ASSOCIATED(scf_env%outer_scf%variables)) THEN
DEALLOCATE (scf_env%outer_scf%variables)
END IF
IF (ASSOCIATED(scf_env%outer_scf%count)) THEN
DEALLOCATE (scf_env%outer_scf%count)
END IF
IF (ASSOCIATED(scf_env%outer_scf%gradient)) THEN
DEALLOCATE (scf_env%outer_scf%gradient)
END IF
IF (ASSOCIATED(scf_env%outer_scf%energy)) THEN
DEALLOCATE (scf_env%outer_scf%energy)
END IF
IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. &
scf_env%outer_scf%deallocate_jacobian) THEN
DEALLOCATE (scf_env%outer_scf%inv_jacobian)
END IF
CALL timestop(handle)
END SUBROUTINE scf_env_cleanup
! **************************************************************************************************
!> \brief perform a CDFT scf procedure in the given qs_env
!> \param qs_env the qs_environment where to perform the scf procedure
!> \param should_stop flag determining if calculation should stop
!> \par History
!> 12.2015 Created
!> \author Nico Holmberg
! **************************************************************************************************
SUBROUTINE cdft_scf(qs_env, should_stop)
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(OUT) :: should_stop
CHARACTER(len=*), PARAMETER :: routineN = 'cdft_scf'
INTEGER :: handle, iatom, ispin, ivar, nmo, nvar, &
output_unit, tsteps