LCOV - code coverage report
Current view: top level - src - qs_scf.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:561f475) Lines: 91.5 % 766 701
Test Date: 2026-06-21 06:48:54 Functions: 100.0 % 8 8

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Routines for the Quickstep SCF run.
      10              : !> \par History
      11              : !>      - Joost VandeVondele (02.2002)
      12              : !>           added code for: incremental (pab and gvg) update
      13              : !>                            initialisation (init_cube, l_info)
      14              : !>      - Joost VandeVondele (02.2002)
      15              : !>           called the poisson code of the classical part
      16              : !>           this takes into account the spherical cutoff and allows for
      17              : !>           isolated systems
      18              : !>      - Joost VandeVondele (02.2002)
      19              : !>           added multiple grid feature
      20              : !>           changed to spherical cutoff consistently (?)
      21              : !>           therefore removed the gradient correct functionals
      22              : !>      - updated with the new QS data structures (10.04.02,MK)
      23              : !>      - copy_matrix replaced by transfer_matrix (11.04.02,MK)
      24              : !>      - nrebuild_rho and nrebuild_gvg unified (12.04.02,MK)
      25              : !>      - set_mo_occupation for smearing of the MO occupation numbers
      26              : !>        (17.04.02,MK)
      27              : !>      - MO level shifting added (22.04.02,MK)
      28              : !>      - Usage of TYPE mo_set_p_type
      29              : !>      - Joost VandeVondele (05.2002)
      30              : !>            added cholesky based diagonalisation
      31              : !>      - 05.2002 added pao method [fawzi]
      32              : !>      - parallel FFT (JGH 22.05.2002)
      33              : !>      - 06.2002 moved KS matrix construction to qs_build_KS_matrix.F [fawzi]
      34              : !>      - started to include more LSD (01.2003,Joost VandeVondele)
      35              : !>      - 02.2003 scf_env [fawzi]
      36              : !>      - got rid of nrebuild (01.2004, Joost VandeVondele)
      37              : !>      - 10.2004 removed pao [fawzi]
      38              : !>      - 03.2006 large cleaning action [Joost VandeVondele]
      39              : !>      - High-spin ROKS added (05.04.06,MK)
      40              : !>      - Mandes (10.2013)
      41              : !>        intermediate energy communication with external communicator added
      42              : !>      - kpoints (08.2014, JGH)
      43              : !>      - unified k-point and gamma-point code (2014.11) [Ole Schuett]
      44              : !>      - added extra SCF loop for CDFT constraints (12.2015) [Nico Holmberg]
      45              : !> \author Matthias Krack (30.04.2001)
      46              : ! **************************************************************************************************
      47              : MODULE qs_scf
      48              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      49              :    USE cp_control_types,                ONLY: dft_control_type
      50              :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      51              :                                               dbcsr_deallocate_matrix,&
      52              :                                               dbcsr_get_info,&
      53              :                                               dbcsr_init_p,&
      54              :                                               dbcsr_p_type,&
      55              :                                               dbcsr_set,&
      56              :                                               dbcsr_type
      57              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      58              :                                               dbcsr_deallocate_matrix_set
      59              :    USE cp_files,                        ONLY: close_file
      60              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      61              :                                               cp_fm_release,&
      62              :                                               cp_fm_to_fm,&
      63              :                                               cp_fm_type
      64              :    USE cp_log_handling,                 ONLY: cp_add_default_logger,&
      65              :                                               cp_get_default_logger,&
      66              :                                               cp_logger_release,&
      67              :                                               cp_logger_type,&
      68              :                                               cp_rm_default_logger,&
      69              :                                               cp_to_string
      70              :    USE cp_output_handling,              ONLY: cp_add_iter_level,&
      71              :                                               cp_iterate,&
      72              :                                               cp_p_file,&
      73              :                                               cp_print_key_should_output,&
      74              :                                               cp_print_key_unit_nr,&
      75              :                                               cp_rm_iter_level
      76              :    USE cp_result_methods,               ONLY: get_results,&
      77              :                                               test_for_result
      78              :    USE cp_result_types,                 ONLY: cp_result_type
      79              :    USE ec_env_types,                    ONLY: energy_correction_type
      80              :    USE input_constants,                 ONLY: &
      81              :         broyden_type_1, broyden_type_1_explicit, broyden_type_1_explicit_ls, broyden_type_1_ls, &
      82              :         broyden_type_2, broyden_type_2_explicit, broyden_type_2_explicit_ls, broyden_type_2_ls, &
      83              :         cdft2ot, history_guess, ot2cdft, ot_precond_full_all, ot_precond_full_single, &
      84              :         ot_precond_full_single_inverse, ot_precond_none, ot_precond_s_inverse, &
      85              :         outer_scf_becke_constraint, outer_scf_hirshfeld_constraint, outer_scf_optimizer_broyden, &
      86              :         outer_scf_optimizer_newton_ls, tblite_scc_mixer_tblite
      87              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      88              :                                               section_vals_type
      89              :    USE kinds,                           ONLY: default_path_length,&
      90              :                                               default_string_length,&
      91              :                                               dp
      92              :    USE kpoint_io,                       ONLY: write_kpoints_restart
      93              :    USE kpoint_types,                    ONLY: kpoint_type
      94              :    USE machine,                         ONLY: m_flush,&
      95              :                                               m_walltime
      96              :    USE mathlib,                         ONLY: invert_matrix
      97              :    USE message_passing,                 ONLY: mp_comm_type,&
      98              :                                               mp_para_env_type
      99              :    USE particle_types,                  ONLY: particle_type
     100              :    USE preconditioner,                  ONLY: prepare_preconditioner,&
     101              :                                               restart_preconditioner
     102              :    USE pw_env_types,                    ONLY: pw_env_get,&
     103              :                                               pw_env_type
     104              :    USE pw_pool_types,                   ONLY: pw_pool_type
     105              :    USE qs_block_davidson_types,         ONLY: block_davidson_deallocate
     106              :    USE qs_cdft_scf_utils,               ONLY: build_diagonal_jacobian,&
     107              :                                               create_tmp_logger,&
     108              :                                               initialize_inverse_jacobian,&
     109              :                                               prepare_jacobian_stencil,&
     110              :                                               print_inverse_jacobian,&
     111              :                                               restart_inverse_jacobian
     112              :    USE qs_cdft_types,                   ONLY: cdft_control_type
     113              :    USE qs_charge_mixing,                ONLY: charge_mixing_scc_error
     114              :    USE qs_charges_types,                ONLY: qs_charges_type
     115              :    USE qs_density_matrices,             ONLY: calculate_density_matrix
     116              :    USE qs_density_mixing_types,         ONLY: gspace_mixing_nr
     117              :    USE qs_diis,                         ONLY: qs_diis_b_clear,&
     118              :                                               qs_diis_b_clear_kp,&
     119              :                                               qs_diis_b_create,&
     120              :                                               qs_diis_b_create_kp
     121              :    USE qs_energy_types,                 ONLY: qs_energy_type
     122              :    USE qs_environment_types,            ONLY: get_qs_env,&
     123              :                                               qs_environment_type,&
     124              :                                               set_qs_env
     125              :    USE qs_integrate_potential,          ONLY: integrate_v_rspace
     126              :    USE qs_kind_types,                   ONLY: qs_kind_type
     127              :    USE qs_ks_methods,                   ONLY: evaluate_core_matrix_traces,&
     128              :                                               qs_ks_update_qs_env
     129              :    USE qs_ks_types,                     ONLY: get_ks_env,&
     130              :                                               qs_ks_did_change,&
     131              :                                               qs_ks_env_type
     132              :    USE qs_mo_io,                        ONLY: write_mo_set_to_restart
     133              :    USE qs_mo_methods,                   ONLY: make_basis_simple,&
     134              :                                               make_basis_sm
     135              :    USE qs_mo_occupation,                ONLY: set_mo_occupation
     136              :    USE qs_mo_types,                     ONLY: deallocate_mo_set,&
     137              :                                               duplicate_mo_set,&
     138              :                                               get_mo_set,&
     139              :                                               mo_set_type,&
     140              :                                               reassign_allocated_mos
     141              :    USE qs_ot,                           ONLY: qs_ot_new_preconditioner
     142              :    USE qs_ot_scf,                       ONLY: ot_scf_init,&
     143              :                                               ot_scf_read_input
     144              :    USE qs_outer_scf,                    ONLY: outer_loop_gradient,&
     145              :                                               outer_loop_optimize,&
     146              :                                               outer_loop_purge_history,&
     147              :                                               outer_loop_switch,&
     148              :                                               outer_loop_update_qs_env
     149              :    USE qs_rho_methods,                  ONLY: qs_rho_update_rho
     150              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
     151              :                                               qs_rho_type
     152              :    USE qs_scf_initialization,           ONLY: qs_scf_env_initialize
     153              :    USE qs_scf_loop_utils,               ONLY: qs_scf_check_inner_exit,&
     154              :                                               qs_scf_check_outer_exit,&
     155              :                                               qs_scf_density_mixing,&
     156              :                                               qs_scf_inner_finalize,&
     157              :                                               qs_scf_new_mos,&
     158              :                                               qs_scf_new_mos_kp,&
     159              :                                               qs_scf_rho_update,&
     160              :                                               qs_scf_set_loop_flags
     161              :    USE qs_scf_output,                   ONLY: qs_scf_cdft_info,&
     162              :                                               qs_scf_cdft_initial_info,&
     163              :                                               qs_scf_loop_info,&
     164              :                                               qs_scf_loop_print,&
     165              :                                               qs_scf_outer_loop_info,&
     166              :                                               qs_scf_write_mos
     167              :    USE qs_scf_post_scf,                 ONLY: qs_scf_compute_properties
     168              :    USE qs_scf_types,                    ONLY: &
     169              :         block_davidson_diag_method_nr, block_krylov_diag_method_nr, filter_matrix_diag_method_nr, &
     170              :         general_diag_method_nr, ot_diag_method_nr, ot_method_nr, qs_scf_env_type, &
     171              :         smeagol_method_nr, special_diag_method_nr
     172              :    USE qs_wf_history_methods,           ONLY: wfi_purge_history,&
     173              :                                               wfi_update
     174              :    USE scf_control_types,               ONLY: scf_control_type
     175              :    USE smeagol_interface,               ONLY: run_smeagol_bulktrans,&
     176              :                                               run_smeagol_emtrans
     177              :    USE tblite_interface,                ONLY: tb_get_energy,&
     178              :                                               tb_native_scc_mixer_active,&
     179              :                                               tb_scf_mixer_error,&
     180              :                                               tb_update_charges
     181              : #include "./base/base_uses.f90"
     182              : 
     183              :    IMPLICIT NONE
     184              : 
     185              :    PRIVATE
     186              : 
     187              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf'
     188              :    LOGICAL, PRIVATE                     :: reuse_precond = .FALSE.
     189              :    LOGICAL, PRIVATE                     :: used_history = .FALSE.
     190              : 
     191              :    PUBLIC :: scf, scf_env_cleanup, scf_env_do_scf, cdft_scf, init_scf_loop
     192              : 
     193              : CONTAINS
     194              : 
     195              : ! **************************************************************************************************
     196              : !> \brief perform an scf procedure in the given qs_env
     197              : !> \param qs_env the qs_environment where to perform the scf procedure
     198              : !> \param has_converged ...
     199              : !> \param total_scf_steps ...
     200              : !> \par History
     201              : !>      02.2003 introduced scf_env, moved real work to scf_env_do_scf [fawzi]
     202              : !> \author fawzi
     203              : !> \note
     204              : ! **************************************************************************************************
     205        23175 :    SUBROUTINE scf(qs_env, has_converged, total_scf_steps)
     206              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     207              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: has_converged
     208              :       INTEGER, INTENT(OUT), OPTIONAL                     :: total_scf_steps
     209              : 
     210              :       INTEGER                                            :: ihistory, max_scf_tmp, tsteps
     211              :       LOGICAL                                            :: converged, outer_scf_loop, should_stop
     212              :       LOGICAL, SAVE                                      :: first_step_flag = .TRUE.
     213        23175 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: gradient_history, variable_history
     214              :       TYPE(cp_logger_type), POINTER                      :: logger
     215              :       TYPE(dft_control_type), POINTER                    :: dft_control
     216              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     217              :       TYPE(scf_control_type), POINTER                    :: scf_control
     218              :       TYPE(section_vals_type), POINTER                   :: dft_section, input, scf_section
     219              : 
     220        23175 :       NULLIFY (scf_env)
     221        23175 :       logger => cp_get_default_logger()
     222        23175 :       CPASSERT(ASSOCIATED(qs_env))
     223        23175 :       IF (PRESENT(has_converged)) THEN
     224            0 :          has_converged = .FALSE.
     225              :       END IF
     226        23175 :       IF (PRESENT(total_scf_steps)) THEN
     227            0 :          total_scf_steps = 0
     228              :       END IF
     229              :       CALL get_qs_env(qs_env, scf_env=scf_env, input=input, &
     230        23175 :                       dft_control=dft_control, scf_control=scf_control)
     231        23175 :       IF (scf_control%max_scf > 0) THEN
     232              : 
     233        22533 :          dft_section => section_vals_get_subs_vals(input, "DFT")
     234        22533 :          scf_section => section_vals_get_subs_vals(dft_section, "SCF")
     235              : 
     236        22533 :          IF (.NOT. ASSOCIATED(scf_env)) THEN
     237         6539 :             CALL qs_scf_env_initialize(qs_env, scf_env)
     238              :             ! Moved here from qs_scf_env_initialize to be able to have more scf_env
     239         6539 :             CALL set_qs_env(qs_env, scf_env=scf_env)
     240              :          ELSE
     241        15994 :             CALL qs_scf_env_initialize(qs_env, scf_env)
     242              :          END IF
     243              : 
     244        22533 :          IF ((scf_control%density_guess == history_guess) .AND. (first_step_flag)) THEN
     245            2 :             max_scf_tmp = scf_control%max_scf
     246            2 :             scf_control%max_scf = 1
     247            2 :             outer_scf_loop = scf_control%outer_scf%have_scf
     248            2 :             scf_control%outer_scf%have_scf = .FALSE.
     249              :          END IF
     250              : 
     251        22533 :          IF (.NOT. dft_control%qs_control%cdft) THEN
     252              :             CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
     253        22189 :                                 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
     254              :          ELSE
     255              :             ! Third SCF loop needed for CDFT with OT to properly restart OT inner loop
     256          344 :             CALL cdft_scf(qs_env=qs_env, should_stop=should_stop)
     257              :          END IF
     258              : 
     259              :          ! If SCF has not converged, then we should not start MP2
     260        22533 :          IF (ASSOCIATED(qs_env%mp2_env)) qs_env%mp2_env%hf_fail = .NOT. converged
     261              : 
     262              :          ! Add the converged outer_scf SCF gradient(s)/variable(s) to history
     263        22533 :          IF (scf_control%outer_scf%have_scf) THEN
     264         4257 :             ihistory = scf_env%outer_scf%iter_count
     265              :             CALL get_qs_env(qs_env, gradient_history=gradient_history, &
     266         4257 :                             variable_history=variable_history)
     267              :             ! We only store the latest two values
     268         8544 :             gradient_history(:, 1) = gradient_history(:, 2)
     269        17088 :             gradient_history(:, 2) = scf_env%outer_scf%gradient(:, ihistory)
     270         8544 :             variable_history(:, 1) = variable_history(:, 2)
     271        17088 :             variable_history(:, 2) = scf_env%outer_scf%variables(:, ihistory)
     272              :             ! Reset flag
     273         4257 :             IF (used_history) used_history = .FALSE.
     274              :             ! Update a counter and check if the Jacobian should be deallocated
     275         4257 :             IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian)) THEN
     276           64 :                scf_control%outer_scf%cdft_opt_control%ijacobian(2) = scf_control%outer_scf%cdft_opt_control%ijacobian(2) + 1
     277              :                IF (scf_control%outer_scf%cdft_opt_control%ijacobian(2) >= &
     278           64 :                    scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) .AND. &
     279              :                    scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) > 0) &
     280           50 :                   scf_env%outer_scf%deallocate_jacobian = .TRUE.
     281              :             END IF
     282              :          END IF
     283              :          !   *** add the converged wavefunction to the wavefunction history
     284        22533 :          IF ((ASSOCIATED(qs_env%wf_history)) .AND. &
     285              :              ((scf_control%density_guess /= history_guess) .OR. &
     286              :               (.NOT. first_step_flag))) THEN
     287        22531 :             IF (.NOT. dft_control%qs_control%cdft) THEN
     288        22187 :                CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
     289              :             ELSE
     290          344 :                IF (dft_control%qs_control%cdft_control%should_purge) THEN
     291            0 :                   CALL wfi_purge_history(qs_env)
     292            0 :                   CALL outer_loop_purge_history(qs_env)
     293            0 :                   dft_control%qs_control%cdft_control%should_purge = .FALSE.
     294              :                ELSE
     295          344 :                   CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
     296              :                END IF
     297              :             END IF
     298            2 :          ELSE IF ((scf_control%density_guess == history_guess) .AND. &
     299              :                   (first_step_flag)) THEN
     300            2 :             scf_control%max_scf = max_scf_tmp
     301            2 :             scf_control%outer_scf%have_scf = outer_scf_loop
     302            2 :             first_step_flag = .FALSE.
     303              :          END IF
     304              : 
     305              :          ! *** compute properties that depend on the converged wavefunction
     306        22533 :          IF (.NOT. (should_stop)) CALL qs_scf_compute_properties(qs_env)
     307              : 
     308              :          ! *** SMEAGOL interface ***
     309        22533 :          IF (.NOT. (should_stop)) THEN
     310              :             ! compute properties that depend on the converged wavefunction ..
     311        22533 :             CALL run_smeagol_emtrans(qs_env, last=.TRUE., iter=0)
     312              :             ! .. or save matrices related to bulk leads
     313        22533 :             CALL run_smeagol_bulktrans(qs_env)
     314              :          END IF
     315              : 
     316              :          ! *** cleanup
     317        22533 :          CALL scf_env_cleanup(scf_env)
     318        22533 :          IF (dft_control%qs_control%cdft) &
     319          344 :             CALL cdft_control_cleanup(dft_control%qs_control%cdft_control)
     320              : 
     321        22533 :          IF (PRESENT(has_converged)) THEN
     322            0 :             has_converged = converged
     323              :          END IF
     324        22533 :          IF (PRESENT(total_scf_steps)) THEN
     325            0 :             total_scf_steps = tsteps
     326              :          END IF
     327              : 
     328              :       END IF
     329              : 
     330        23175 :    END SUBROUTINE scf
     331              : 
     332              : ! **************************************************************************************************
     333              : !> \brief perform an scf loop
     334              : !> \param scf_env the scf_env where to perform the scf procedure
     335              : !> \param scf_control ...
     336              : !> \param qs_env the qs_env, the scf_env lives in
     337              : !> \param converged will be true / false if converged is reached
     338              : !> \param should_stop ...
     339              : !> \param total_scf_steps ...
     340              : !> \par History
     341              : !>      long history, see cvs and qs_scf module history
     342              : !>      02.2003 introduced scf_env [fawzi]
     343              : !>      09.2005 Frozen density approximation [TdK]
     344              : !>      06.2007 Check for SCF iteration count early [jgh]
     345              : !>      10.2019 switch_surf_dip [SGh]
     346              : !> \author Matthias Krack
     347              : !> \note
     348              : ! **************************************************************************************************
     349        22873 :    SUBROUTINE scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
     350              : 
     351              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     352              :       TYPE(scf_control_type), POINTER                    :: scf_control
     353              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     354              :       LOGICAL, INTENT(OUT)                               :: converged, should_stop
     355              :       INTEGER, INTENT(OUT)                               :: total_scf_steps
     356              : 
     357              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'scf_env_do_scf'
     358              : 
     359              :       CHARACTER(LEN=default_string_length)               :: description, name
     360              :       INTEGER                                            :: ext_master_id, handle, handle2, i_tmp, &
     361              :                                                             ic, ispin, iter_count, output_unit, &
     362              :                                                             scf_energy_message_tag, total_steps
     363              :       LOGICAL :: density_full_step, diis_step, do_kpoints, energy_only, exit_inner_loop, &
     364              :          exit_outer_loop, inner_loop_converged, internal_tblite_density_full_step, &
     365              :          internal_tblite_mixer, just_energy, outer_loop_converged, tblite_native_mixer
     366              :       REAL(KIND=dp)                                      :: t1, t2
     367              :       REAL(KIND=dp), DIMENSION(3)                        :: res_val_3
     368        22873 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     369              :       TYPE(cp_logger_type), POINTER                      :: logger
     370              :       TYPE(cp_result_type), POINTER                      :: results
     371        22873 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_ks
     372        22873 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao_kp
     373              :       TYPE(dft_control_type), POINTER                    :: dft_control
     374              :       TYPE(energy_correction_type), POINTER              :: ec_env
     375              :       TYPE(kpoint_type), POINTER                         :: kpoints
     376        22873 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos, mos_last_converged
     377              :       TYPE(mp_comm_type)                                 :: external_comm
     378              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     379        22873 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     380              :       TYPE(pw_env_type), POINTER                         :: pw_env
     381              :       TYPE(qs_charges_type), POINTER                     :: qs_charges
     382              :       TYPE(qs_energy_type), POINTER                      :: energy
     383        22873 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     384              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     385              :       TYPE(qs_rho_type), POINTER                         :: rho
     386              :       TYPE(section_vals_type), POINTER                   :: dft_section, input, scf_section
     387              : 
     388        22873 :       CALL timeset(routineN, handle)
     389              : 
     390        22873 :       NULLIFY (dft_control, rho, energy, &
     391        22873 :                logger, qs_charges, ks_env, mos, atomic_kind_set, qs_kind_set, &
     392        22873 :                particle_set, dft_section, input, &
     393        22873 :                scf_section, para_env, results, kpoints, pw_env, rho_ao_kp, mos_last_converged)
     394              : 
     395        22873 :       CPASSERT(ASSOCIATED(scf_env))
     396        22873 :       CPASSERT(ASSOCIATED(qs_env))
     397              : 
     398        22873 :       logger => cp_get_default_logger()
     399        22873 :       t1 = m_walltime()
     400              : 
     401              :       CALL get_qs_env(qs_env=qs_env, &
     402              :                       energy=energy, &
     403              :                       particle_set=particle_set, &
     404              :                       qs_charges=qs_charges, &
     405              :                       ks_env=ks_env, &
     406              :                       atomic_kind_set=atomic_kind_set, &
     407              :                       qs_kind_set=qs_kind_set, &
     408              :                       rho=rho, &
     409              :                       mos=mos, &
     410              :                       input=input, &
     411              :                       dft_control=dft_control, &
     412              :                       do_kpoints=do_kpoints, &
     413              :                       kpoints=kpoints, &
     414              :                       results=results, &
     415              :                       pw_env=pw_env, &
     416        22873 :                       para_env=para_env)
     417              :       tblite_native_mixer = dft_control%qs_control%xtb_control%do_tblite .AND. &
     418              :                             scf_env%method /= ot_method_nr .AND. &
     419        22873 :                             tb_native_scc_mixer_active(dft_control)
     420              :       internal_tblite_mixer = (dft_control%qs_control%dftb .AND. &
     421              :                                dft_control%qs_control%dftb_control%tblite_scc_mixer == tblite_scc_mixer_tblite) .OR. &
     422              :                               (dft_control%qs_control%xtb .AND. &
     423              :                                .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
     424        22873 :                                dft_control%qs_control%xtb_control%tblite_scc_mixer == tblite_scc_mixer_tblite)
     425              :       internal_tblite_density_full_step = dft_control%qs_control%xtb .AND. &
     426              :                                           .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
     427        22873 :                                           dft_control%qs_control%xtb_control%tblite_scc_mixer == tblite_scc_mixer_tblite
     428              : 
     429        22873 :       CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
     430              : 
     431        22873 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     432        22873 :       scf_section => section_vals_get_subs_vals(dft_section, "SCF")
     433              : 
     434              :       output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
     435        22873 :                                          extension=".scfLog")
     436              : 
     437        22873 :       IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
     438        11617 :          "SCF WAVEFUNCTION OPTIMIZATION"
     439              : 
     440              :       ! when switch_surf_dip is switched on, indicate storing mos from the last converged step
     441        22873 :       IF (dft_control%switch_surf_dip) THEN
     442            2 :          CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
     443            4 :          DO ispin = 1, dft_control%nspins
     444            4 :             CALL reassign_allocated_mos(mos(ispin), mos_last_converged(ispin))
     445              :          END DO
     446            2 :          IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
     447            1 :             "COPIED mos_last_converged ---> mos"
     448              :       END IF
     449              : 
     450        22873 :       IF ((output_unit > 0) .AND. (.NOT. scf_control%use_ot)) THEN
     451              :          WRITE (UNIT=output_unit, &
     452              :                 FMT="(/,T3,A,T12,A,T31,A,T39,A,T59,A,T75,A,/,T3,A)") &
     453         8305 :             "Step", "Update method", "Time", "Convergence", "Total energy", "Change", &
     454        16610 :             REPEAT("-", 78)
     455              :       END IF
     456        22873 :       CALL cp_add_iter_level(logger%iter_info, "QS_SCF")
     457              : 
     458              :       ! check for external communicator and if the intermediate energy should be sent
     459        91492 :       res_val_3(:) = -1.0_dp
     460        22873 :       description = "[EXT_SCF_ENER_COMM]"
     461        22873 :       IF (test_for_result(results, description=description)) THEN
     462              :          CALL get_results(results, description=description, &
     463            0 :                           values=res_val_3, n_entries=i_tmp)
     464            0 :          CPASSERT(i_tmp == 3)
     465            0 :          IF (ALL(res_val_3(:) <= 0.0)) &
     466              :             CALL cp_abort(__LOCATION__, &
     467              :                           " Trying to access result ("//TRIM(description)// &
     468            0 :                           ") which is not correctly stored.")
     469            0 :          CALL external_comm%set_handle(NINT(res_val_3(1)))
     470              :       END IF
     471        22873 :       ext_master_id = NINT(res_val_3(2))
     472        22873 :       scf_energy_message_tag = NINT(res_val_3(3))
     473              : 
     474              :       ! *** outer loop of the scf, can treat other variables,
     475              :       ! *** such as lagrangian multipliers
     476        22873 :       scf_env%outer_scf%iter_count = 0
     477        22873 :       iter_count = 0
     478        22873 :       total_steps = 0
     479        22873 :       energy%tot_old = 0.0_dp
     480              : 
     481          856 :       scf_outer_loop: DO
     482              : 
     483              :          CALL init_scf_loop(scf_env=scf_env, qs_env=qs_env, &
     484        23729 :                             scf_section=scf_section)
     485              : 
     486              :          CALL qs_scf_set_loop_flags(scf_env, diis_step, &
     487        23729 :                                     energy_only, just_energy, exit_inner_loop)
     488              : 
     489              :          ! decide whether to switch off dipole correction for convergence purposes
     490        23729 :          dft_control%surf_dip_correct_switch = dft_control%correct_surf_dip
     491        23729 :          IF ((dft_control%correct_surf_dip) .AND. (scf_control%outer_scf%have_scf) .AND. &
     492              :              (scf_env%outer_scf%iter_count > FLOOR(scf_control%outer_scf%max_scf/2.0_dp))) THEN
     493            0 :             IF (dft_control%switch_surf_dip) THEN
     494            0 :                dft_control%surf_dip_correct_switch = .FALSE.
     495            0 :                IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
     496            0 :                   "SURFACE DIPOLE CORRECTION switched off"
     497              :             END IF
     498              :          END IF
     499              : 
     500       217057 :          scf_loop: DO
     501              : 
     502       217057 :             CALL timeset(routineN//"_inner_loop", handle2)
     503              : 
     504       217057 :             IF (.NOT. just_energy) scf_env%iter_count = scf_env%iter_count + 1
     505       217057 :             iter_count = iter_count + 1
     506       217057 :             CALL cp_iterate(logger%iter_info, last=.FALSE., iter_nr=iter_count)
     507              : 
     508       217057 :             IF (output_unit > 0) CALL m_flush(output_unit)
     509              : 
     510       217057 :             total_steps = total_steps + 1
     511       217057 :             just_energy = energy_only
     512              : 
     513              :             CALL qs_ks_update_qs_env(qs_env, just_energy=just_energy, &
     514       217057 :                                      calculate_forces=.FALSE.)
     515              : 
     516              :             ! print 'heavy weight' or relatively expensive quantities
     517       217057 :             CALL qs_scf_loop_print(qs_env, scf_env, para_env)
     518              : 
     519       217057 :             IF (do_kpoints) THEN
     520              :                ! kpoints
     521        28768 :                IF (dft_control%hairy_probes .EQV. .TRUE.) THEN
     522            0 :                   scf_control%smear%do_smear = .FALSE.
     523            0 :                   CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, dft_control%probe)
     524              :                ELSE
     525        28768 :                   CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step)
     526              :                END IF
     527              :             ELSE
     528              :                ! Gamma points only
     529       188289 :                IF (dft_control%hairy_probes .EQV. .TRUE.) THEN
     530           14 :                   scf_control%smear%do_smear = .FALSE.
     531              :                   CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only, &
     532           14 :                                       dft_control%probe)
     533              :                ELSE
     534       188275 :                   CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
     535              :                END IF
     536              :             END IF
     537              : 
     538              :             ! Print requested MO information (can be computationally expensive with OT)
     539       217057 :             CALL qs_scf_write_mos(qs_env, scf_env, final_mos=.FALSE.)
     540              : 
     541       217057 :             IF (dft_control%qs_control%xtb_control%do_tblite) THEN
     542        23300 :                IF (scf_env%method == ot_method_nr) THEN
     543           68 :                   CALL tb_update_charges(qs_env, dft_control, qs_env%tb_tblite, .FALSE., .TRUE.)
     544           68 :                   CALL evaluate_core_matrix_traces(qs_env)
     545              :                ELSE
     546        23232 :                   CPASSERT(scf_env%mixing_method > 0)
     547        23232 :                   CALL tb_update_charges(qs_env, dft_control, qs_env%tb_tblite, .FALSE., .FALSE.)
     548        23232 :                   CALL evaluate_core_matrix_traces(qs_env, rho_ao_ext=scf_env%p_mix_new)
     549              :                END IF
     550        23300 :                CALL tb_get_energy(qs_env, qs_env%tb_tblite, energy)
     551              :             END IF
     552              : 
     553       217057 :             density_full_step = diis_step .OR. tblite_native_mixer .OR. internal_tblite_density_full_step
     554       217057 :             CALL qs_scf_density_mixing(scf_env, rho, para_env, density_full_step)
     555       217057 :             IF (dft_control%qs_control%xtb_control%do_tblite .AND. &
     556              :                 .NOT. (dft_control%qs_control%do_ls_scf .OR. scf_control%use_ot)) THEN
     557              :                scf_env%iter_delta = MAX(scf_env%iter_delta, &
     558              :                                         tb_scf_mixer_error(dft_control, qs_env%tb_tblite, &
     559        23232 :                                                            scf_control%eps_scf))
     560              :             END IF
     561       217057 :             IF (dft_control%qs_control%dftb .OR. &
     562              :                 (dft_control%qs_control%xtb .AND. .NOT. dft_control%qs_control%xtb_control%do_tblite)) THEN
     563              :                scf_env%iter_delta = MAX(scf_env%iter_delta, &
     564        52926 :                                         charge_mixing_scc_error(scf_env%mixing_store, scf_control%eps_scf))
     565              :             END IF
     566       217057 :             IF (tblite_native_mixer) THEN
     567        21608 :                scf_env%iter_param = dft_control%qs_control%xtb_control%tblite_mixer_damping
     568        21608 :                scf_env%iter_method = "TBLite/Diag"
     569       195449 :             ELSEIF (internal_tblite_mixer) THEN
     570           30 :                scf_env%iter_method = "TBLite/Diag"
     571           30 :                IF (dft_control%qs_control%dftb) THEN
     572           18 :                   scf_env%iter_param = dft_control%qs_control%dftb_control%tblite_mixer_damping
     573              :                ELSE
     574           12 :                   scf_env%iter_param = dft_control%qs_control%xtb_control%tblite_mixer_damping
     575              :                END IF
     576              :             END IF
     577              : 
     578       217057 :             t2 = m_walltime()
     579              : 
     580       217057 :             CALL qs_scf_loop_info(scf_env, output_unit, just_energy, t1, t2, energy)
     581              : 
     582       217057 :             IF (.NOT. just_energy) energy%tot_old = energy%total
     583              : 
     584              :             ! check for external communicator and if the intermediate energy should be sent
     585       217057 :             IF (scf_energy_message_tag > 0) THEN
     586            0 :                CALL external_comm%send(energy%total, ext_master_id, scf_energy_message_tag)
     587              :             END IF
     588              : 
     589              :             CALL qs_scf_check_inner_exit(qs_env, scf_env, scf_control, should_stop, just_energy, &
     590       217057 :                                          exit_inner_loop, inner_loop_converged, output_unit)
     591              : 
     592              :             ! In case we decide to exit we perform few more check to see if this one
     593              :             ! is really the last SCF step
     594       217057 :             IF (exit_inner_loop) THEN
     595              : 
     596        23729 :                CALL qs_scf_inner_finalize(scf_env, qs_env, density_full_step, output_unit)
     597              : 
     598              :                CALL qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, &
     599        23729 :                                             outer_loop_converged, exit_outer_loop)
     600              : 
     601              :                ! Let's tag the last SCF cycle so we can print informations only of the last step
     602        23729 :                IF (exit_outer_loop) CALL cp_iterate(logger%iter_info, last=.TRUE., iter_nr=iter_count)
     603              : 
     604              :             END IF
     605              : 
     606       217057 :             IF (do_kpoints) THEN
     607        28768 :                CALL write_kpoints_restart(rho_ao_kp, kpoints, scf_env, dft_section, particle_set, qs_kind_set)
     608              :             ELSE
     609              :                ! Write wavefunction restart file
     610       188289 :                IF (scf_env%method == ot_method_nr) THEN
     611              :                   ! With OT: provide the Kohn-Sham matrix for the calculation of the MO eigenvalues
     612        76998 :                   CALL get_ks_env(ks_env=ks_env, matrix_ks=matrix_ks)
     613              :                   CALL write_mo_set_to_restart(mos, particle_set, dft_section=dft_section, qs_kind_set=qs_kind_set, &
     614        76998 :                                                matrix_ks=matrix_ks)
     615              :                ELSE
     616       111291 :                   CALL write_mo_set_to_restart(mos, particle_set, dft_section=dft_section, qs_kind_set=qs_kind_set)
     617              :                END IF
     618              : 
     619              :             END IF
     620              : 
     621              :             ! Exit if we have finished with the SCF inner loop
     622       217057 :             IF (exit_inner_loop) THEN
     623        23729 :                CALL timestop(handle2)
     624              :                EXIT scf_loop
     625              :             END IF
     626              : 
     627       193328 :             IF (.NOT. BTEST(cp_print_key_should_output(logger%iter_info, &
     628              :                                                        scf_section, "PRINT%ITERATION_INFO/TIME_CUMUL"), cp_p_file)) &
     629       193328 :                t1 = m_walltime()
     630              : 
     631              :             ! mixing methods have the new density matrix in p_mix_new
     632       193328 :             IF (scf_env%mixing_method > 0) THEN
     633      1239232 :                DO ic = 1, SIZE(rho_ao_kp, 2)
     634      2421641 :                   DO ispin = 1, dft_control%nspins
     635      1182409 :                      CALL dbcsr_get_info(rho_ao_kp(ispin, ic)%matrix, name=name) ! keep the name
     636      2298050 :                      CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
     637              :                   END DO
     638              :                END DO
     639              :             END IF
     640              : 
     641              :             CALL qs_scf_rho_update(rho, qs_env, scf_env, ks_env, &
     642       193328 :                                    mix_rho=scf_env%mixing_method >= gspace_mixing_nr)
     643              : 
     644       193328 :             CALL timestop(handle2)
     645              : 
     646              :          END DO scf_loop
     647              : 
     648        23729 :          IF (.NOT. scf_control%outer_scf%have_scf) EXIT scf_outer_loop
     649              : 
     650              :          ! In case we use the OUTER SCF loop let's print some info..
     651              :          CALL qs_scf_outer_loop_info(output_unit, scf_control, scf_env, &
     652         5451 :                                      energy, total_steps, should_stop, outer_loop_converged)
     653              : 
     654              :          ! Save MOs to converged MOs if outer_loop_converged and surf_dip_correct_switch is true
     655         5451 :          IF (exit_outer_loop) THEN
     656         4595 :             IF ((dft_control%switch_surf_dip) .AND. (outer_loop_converged) .AND. &
     657              :                 (dft_control%surf_dip_correct_switch)) THEN
     658            4 :                DO ispin = 1, dft_control%nspins
     659            4 :                   CALL reassign_allocated_mos(mos_last_converged(ispin), mos(ispin))
     660              :                END DO
     661            2 :                IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
     662            1 :                   "COPIED mos ---> mos_last_converged"
     663              :             END IF
     664              :          END IF
     665              : 
     666         5451 :          IF (exit_outer_loop) EXIT scf_outer_loop
     667              : 
     668              :          !
     669          856 :          CALL outer_loop_optimize(scf_env, scf_control)
     670          856 :          CALL outer_loop_update_qs_env(qs_env, scf_env)
     671        23729 :          CALL qs_ks_did_change(ks_env, potential_changed=.TRUE.)
     672              : 
     673              :       END DO scf_outer_loop
     674              : 
     675        22873 :       converged = inner_loop_converged .AND. outer_loop_converged
     676        22873 :       total_scf_steps = total_steps
     677              : 
     678        22873 :       IF (dft_control%qs_control%cdft) &
     679              :          dft_control%qs_control%cdft_control%total_steps = &
     680          682 :          dft_control%qs_control%cdft_control%total_steps + total_steps
     681              : 
     682        22873 :       IF (.NOT. converged) THEN
     683         2246 :          IF (scf_control%ignore_convergence_failure .OR. should_stop) THEN
     684         2246 :             CALL cp_warn(__LOCATION__, "SCF run NOT converged")
     685              :          ELSE
     686              :             CALL cp_abort(__LOCATION__, &
     687              :                           "SCF run NOT converged. To continue the calculation "// &
     688            0 :                           "regardless, please set the keyword IGNORE_CONVERGENCE_FAILURE.")
     689              :          END IF
     690              :       END IF
     691              : 
     692              :       ! Skip Harris functional calculation if ground-state is NOT converged
     693        22873 :       IF (qs_env%energy_correction) THEN
     694          676 :          CALL get_qs_env(qs_env, ec_env=ec_env)
     695          676 :          ec_env%do_skip = .FALSE.
     696          676 :          IF (ec_env%skip_ec .AND. .NOT. converged) ec_env%do_skip = .TRUE.
     697              :       END IF
     698              : 
     699              :       ! if needed copy mo_coeff dbcsr->fm for later use in post_scf!fm->dbcsr
     700        48876 :       DO ispin = 1, SIZE(mos) !fm -> dbcsr
     701        48876 :          IF (mos(ispin)%use_mo_coeff_b) THEN !fm->dbcsr
     702         7617 :             IF (.NOT. ASSOCIATED(mos(ispin)%mo_coeff_b)) & !fm->dbcsr
     703            0 :                CPABORT("mo_coeff_b is not allocated") !fm->dbcsr
     704              :             CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, & !fm->dbcsr
     705         7617 :                                   mos(ispin)%mo_coeff) !fm -> dbcsr
     706              :          END IF !fm->dbcsr
     707              :       END DO !fm -> dbcsr
     708              : 
     709        22873 :       CALL cp_rm_iter_level(logger%iter_info, level_name="QS_SCF")
     710        22873 :       CALL timestop(handle)
     711              : 
     712        22873 :    END SUBROUTINE scf_env_do_scf
     713              : 
     714              : ! **************************************************************************************************
     715              : !> \brief inits those objects needed if you want to restart the scf with, say
     716              : !>        only a new initial guess, or different density functional or ...
     717              : !>        this will happen just before the scf loop starts
     718              : !> \param scf_env ...
     719              : !> \param qs_env ...
     720              : !> \param scf_section ...
     721              : !> \par History
     722              : !>      03.2006 created [Joost VandeVondele]
     723              : ! **************************************************************************************************
     724        26559 :    SUBROUTINE init_scf_loop(scf_env, qs_env, scf_section)
     725              : 
     726              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     727              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     728              :       TYPE(section_vals_type), POINTER                   :: scf_section
     729              : 
     730              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'init_scf_loop'
     731              : 
     732              :       INTEGER                                            :: handle, ispin, nmo, number_of_OT_envs
     733              :       LOGICAL                                            :: do_kpoints, do_rotation, &
     734              :                                                             has_unit_metric, is_full_all
     735              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     736        26559 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_ks, matrix_s
     737              :       TYPE(dbcsr_type), POINTER                          :: orthogonality_metric
     738              :       TYPE(dft_control_type), POINTER                    :: dft_control
     739              :       TYPE(kpoint_type), POINTER                         :: kpoints
     740        26559 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     741              :       TYPE(scf_control_type), POINTER                    :: scf_control
     742              : 
     743        26559 :       CALL timeset(routineN, handle)
     744              : 
     745        26559 :       NULLIFY (scf_control, matrix_s, matrix_ks, dft_control, mos, mo_coeff, kpoints)
     746              : 
     747        26559 :       CPASSERT(ASSOCIATED(scf_env))
     748        26559 :       CPASSERT(ASSOCIATED(qs_env))
     749              : 
     750              :       CALL get_qs_env(qs_env=qs_env, &
     751              :                       scf_control=scf_control, &
     752              :                       dft_control=dft_control, &
     753              :                       do_kpoints=do_kpoints, &
     754              :                       kpoints=kpoints, &
     755        26559 :                       mos=mos)
     756              : 
     757              :       ! if using mo_coeff_b then copy to fm
     758        56795 :       DO ispin = 1, SIZE(mos) !fm->dbcsr
     759        56795 :          IF (mos(1)%use_mo_coeff_b) THEN !fm->dbcsr
     760         8648 :             CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, mos(ispin)%mo_coeff) !fm->dbcsr
     761              :          END IF !fm->dbcsr
     762              :       END DO !fm->dbcsr
     763              : 
     764              :       ! this just guarantees that all mo_occupations match the eigenvalues, if smear
     765        56795 :       DO ispin = 1, dft_control%nspins
     766              :          ! do not reset mo_occupations if the maximum overlap method is in use
     767        56795 :          IF (.NOT. scf_control%diagonalization%mom) THEN
     768              :             !if the hair probes section is present, this sends hairy_probes to set_mo_occupation subroutine
     769              :             !and switches off the standard smearing
     770        30192 :             IF (dft_control%hairy_probes .EQV. .TRUE.) THEN
     771            4 :                IF (scf_env%outer_scf%iter_count > 0) THEN
     772            0 :                   scf_control%smear%do_smear = .FALSE.
     773              :                   CALL set_mo_occupation(mo_set=mos(ispin), &
     774              :                                          smear=scf_control%smear, &
     775            0 :                                          probe=dft_control%probe)
     776              :                END IF
     777              :             ELSE
     778              :                CALL set_mo_occupation(mo_set=mos(ispin), &
     779        30188 :                                       smear=scf_control%smear)
     780              :             END IF
     781              :          END IF
     782              :       END DO
     783              : 
     784        26559 :       SELECT CASE (scf_env%method)
     785              :       CASE DEFAULT
     786              : 
     787            0 :          CPABORT("Unknown SCF method <"//TRIM(cp_to_string(scf_env%method))//"> found. Check the code!")
     788              : 
     789              :       CASE (filter_matrix_diag_method_nr)
     790              : 
     791           10 :          IF (.NOT. scf_env%skip_diis) THEN
     792            0 :             IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
     793            0 :                ALLOCATE (scf_env%scf_diis_buffer)
     794            0 :                CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
     795              :             END IF
     796            0 :             CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
     797              :          END IF
     798              : 
     799              :       CASE (general_diag_method_nr, special_diag_method_nr, block_krylov_diag_method_nr, smeagol_method_nr)
     800        19262 :          IF (.NOT. scf_env%skip_diis) THEN
     801        18536 :             IF (do_kpoints) THEN
     802         2870 :                IF (.NOT. ASSOCIATED(kpoints%scf_diis_buffer)) THEN
     803         2250 :                   ALLOCATE (kpoints%scf_diis_buffer)
     804         2250 :                   CALL qs_diis_b_create_kp(kpoints%scf_diis_buffer, nbuffer=scf_control%max_diis)
     805              :                END IF
     806         2870 :                CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
     807              :             ELSE
     808        15666 :                IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
     809         4324 :                   ALLOCATE (scf_env%scf_diis_buffer)
     810         4324 :                   CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
     811              :                END IF
     812        15666 :                CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
     813              :             END IF
     814              :          END IF
     815              : 
     816              :       CASE (ot_diag_method_nr)
     817            8 :          CALL get_qs_env(qs_env, matrix_ks=matrix_ks, matrix_s=matrix_s)
     818              : 
     819            8 :          IF (.NOT. scf_env%skip_diis) THEN
     820            6 :             IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
     821            6 :                ALLOCATE (scf_env%scf_diis_buffer)
     822            6 :                CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
     823              :             END IF
     824            6 :             CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
     825              :          END IF
     826              : 
     827              :          ! disable DFTB and SE for now
     828              :          IF (dft_control%qs_control%dftb .OR. &
     829            8 :              dft_control%qs_control%xtb .OR. &
     830              :              dft_control%qs_control%semi_empirical) THEN
     831            0 :             CPABORT("DFTB and SE not available with OT/DIAG")
     832              :          END IF
     833              : 
     834              :          ! if an old preconditioner is still around (i.e. outer SCF is active),
     835              :          ! remove it if this could be worthwhile
     836              :          CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
     837              :                                      scf_control%diagonalization%ot_settings%preconditioner_type, &
     838            8 :                                      dft_control%nspins)
     839              : 
     840              :          CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
     841              :                                      scf_control%diagonalization%ot_settings%preconditioner_type, &
     842              :                                      scf_control%diagonalization%ot_settings%precond_solver_type, &
     843            8 :                                      scf_control%diagonalization%ot_settings%energy_gap, dft_control%nspins)
     844              : 
     845              :       CASE (block_davidson_diag_method_nr)
     846              :          ! Preconditioner initialized within the loop, when required
     847              :       CASE (ot_method_nr)
     848              :          CALL get_qs_env(qs_env, &
     849              :                          has_unit_metric=has_unit_metric, &
     850              :                          matrix_s=matrix_s, &
     851         7261 :                          matrix_ks=matrix_ks)
     852              : 
     853              :          ! reortho the wavefunctions if we are having an outer scf and
     854              :          ! this is not the first iteration
     855              :          ! this is useful to avoid the build-up of numerical noise
     856              :          ! however, we can not play this trick if restricted (don't mix non-equivalent orbs)
     857         7261 :          IF (scf_control%do_outer_scf_reortho) THEN
     858         6691 :             IF (scf_control%outer_scf%have_scf .AND. .NOT. dft_control%restricted) THEN
     859         4645 :                IF (scf_env%outer_scf%iter_count > 0) THEN
     860         1829 :                   DO ispin = 1, dft_control%nspins
     861          993 :                      CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
     862         1829 :                      IF (has_unit_metric) THEN
     863          108 :                         CALL make_basis_simple(mo_coeff, nmo)
     864              :                      ELSE
     865          885 :                         CALL make_basis_sm(mo_coeff, nmo, matrix_s(1)%matrix)
     866              :                      END IF
     867              :                   END DO
     868              :                END IF
     869              :             END IF
     870              :          ELSE
     871              :             ! dont need any dirty trick for the numerically stable irac algorithm.
     872              :          END IF
     873              : 
     874         7261 :          IF (.NOT. ASSOCIATED(scf_env%qs_ot_env)) THEN
     875              : 
     876              :             ! restricted calculations require just one set of OT orbitals
     877         7261 :             number_of_OT_envs = dft_control%nspins
     878         7261 :             IF (dft_control%restricted) number_of_OT_envs = 1
     879              : 
     880      1199328 :             ALLOCATE (scf_env%qs_ot_env(number_of_OT_envs))
     881              : 
     882              :             ! XXX Joost XXX should disentangle reading input from this part
     883         7261 :             IF (scf_env%outer_scf%iter_count > 0) THEN
     884          856 :                IF (scf_env%iter_delta < scf_control%eps_diis) THEN
     885            4 :                   scf_env%qs_ot_env(1)%settings%ot_state = 1
     886              :                END IF
     887              :             END IF
     888              :             !
     889         7261 :             CALL ot_scf_read_input(scf_env%qs_ot_env, scf_section)
     890              :             !
     891         7261 :             IF (scf_env%outer_scf%iter_count > 0) THEN
     892          856 :                IF (scf_env%qs_ot_env(1)%settings%ot_state == 1) THEN
     893              :                   scf_control%max_scf = MAX(scf_env%qs_ot_env(1)%settings%max_scf_diis, &
     894            4 :                                             scf_control%max_scf)
     895              :                END IF
     896              :             END IF
     897              : 
     898              :             ! keep a note that we are restricted
     899         7261 :             IF (dft_control%restricted) THEN
     900           92 :                scf_env%qs_ot_env(1)%restricted = .TRUE.
     901              :                ! requires rotation
     902           92 :                IF (.NOT. scf_env%qs_ot_env(1)%settings%do_rotation) &
     903              :                   CALL cp_abort(__LOCATION__, &
     904              :                                 "Restricted calculation with OT requires orbital rotation. Please "// &
     905            0 :                                 "activate the OT%ROTATION keyword!")
     906              :             ELSE
     907        15601 :                scf_env%qs_ot_env(:)%restricted = .FALSE.
     908              :             END IF
     909              : 
     910              :             ! this will rotate the MOs to be eigen states, which is not compatible with rotation
     911              :             ! e.g. mo_derivs here do not yet include potentially different occupations numbers
     912         7261 :             do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
     913              :             ! only full all needs rotation
     914         7261 :             is_full_all = scf_env%qs_ot_env(1)%settings%preconditioner_type == ot_precond_full_all
     915         7261 :             IF (do_rotation .AND. is_full_all) &
     916            0 :                CPABORT('PRECONDITIONER FULL_ALL is not compatible with ROTATION.')
     917              : 
     918              :             ! might need the KS matrix to init properly
     919              :             CALL qs_ks_update_qs_env(qs_env, just_energy=.FALSE., &
     920         7261 :                                      calculate_forces=.FALSE.)
     921              : 
     922              :             ! if an old preconditioner is still around (i.e. outer SCF is active),
     923              :             ! remove it if this could be worthwhile
     924         7261 :             IF (.NOT. reuse_precond) &
     925              :                CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
     926              :                                            scf_env%qs_ot_env(1)%settings%preconditioner_type, &
     927         7261 :                                            dft_control%nspins)
     928              : 
     929              :             !
     930              :             ! preconditioning still needs to be done correctly with has_unit_metric
     931              :             ! notice that a big part of the preconditioning (S^-1) is fine anyhow
     932              :             !
     933         7261 :             IF (has_unit_metric) THEN
     934         1154 :                NULLIFY (orthogonality_metric)
     935              :             ELSE
     936         6107 :                orthogonality_metric => matrix_s(1)%matrix
     937              :             END IF
     938              : 
     939         7261 :             IF (.NOT. reuse_precond) &
     940              :                CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
     941              :                                            scf_env%qs_ot_env(1)%settings%preconditioner_type, &
     942              :                                            scf_env%qs_ot_env(1)%settings%precond_solver_type, &
     943              :                                            scf_env%qs_ot_env(1)%settings%energy_gap, dft_control%nspins, &
     944              :                                            has_unit_metric=has_unit_metric, &
     945         7261 :                                            chol_type=scf_env%qs_ot_env(1)%settings%cholesky_type)
     946         7261 :             IF (reuse_precond) reuse_precond = .FALSE.
     947              : 
     948              :             CALL ot_scf_init(mo_array=mos, matrix_s=orthogonality_metric, &
     949              :                              broyden_adaptive_sigma=qs_env%broyden_adaptive_sigma, &
     950         7261 :                              qs_ot_env=scf_env%qs_ot_env, matrix_ks=matrix_ks(1)%matrix)
     951              : 
     952        12515 :             SELECT CASE (scf_env%qs_ot_env(1)%settings%preconditioner_type)
     953              :             CASE (ot_precond_none)
     954              :             CASE (ot_precond_full_all, ot_precond_full_single_inverse)
     955        11485 :                DO ispin = 1, SIZE(scf_env%qs_ot_env)
     956              :                   CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
     957        11485 :                                                 scf_env%ot_preconditioner(ispin)%preconditioner)
     958              :                END DO
     959              :             CASE (ot_precond_s_inverse, ot_precond_full_single)
     960          152 :                DO ispin = 1, SIZE(scf_env%qs_ot_env)
     961              :                   CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
     962          152 :                                                 scf_env%ot_preconditioner(1)%preconditioner)
     963              :                END DO
     964              :             CASE DEFAULT
     965         8714 :                DO ispin = 1, SIZE(scf_env%qs_ot_env)
     966              :                   CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
     967         2670 :                                                 scf_env%ot_preconditioner(1)%preconditioner)
     968              :                END DO
     969              :             END SELECT
     970              :          END IF
     971              : 
     972              :          ! if we have non-uniform occupations we should be using rotation
     973         7261 :          do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
     974        42436 :          DO ispin = 1, SIZE(mos)
     975        15877 :             IF (.NOT. mos(ispin)%uniform_occupation) THEN
     976            0 :                CPASSERT(do_rotation)
     977              :             END IF
     978              :          END DO
     979              :       END SELECT
     980              : 
     981              :       ! another safety check
     982        26559 :       IF (dft_control%low_spin_roks) THEN
     983           24 :          CPASSERT(scf_env%method == ot_method_nr)
     984           24 :          do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
     985           24 :          CPASSERT(do_rotation)
     986              :       END IF
     987              : 
     988        26559 :       CALL timestop(handle)
     989              : 
     990        26559 :    END SUBROUTINE init_scf_loop
     991              : 
     992              : ! **************************************************************************************************
     993              : !> \brief perform cleanup operations (like releasing temporary storage)
     994              : !>      at the end of the scf
     995              : !> \param scf_env ...
     996              : !> \par History
     997              : !>      02.2003 created [fawzi]
     998              : !> \author fawzi
     999              : ! **************************************************************************************************
    1000        22579 :    SUBROUTINE scf_env_cleanup(scf_env)
    1001              :       TYPE(qs_scf_env_type), INTENT(INOUT)               :: scf_env
    1002              : 
    1003              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'scf_env_cleanup'
    1004              : 
    1005              :       INTEGER                                            :: handle
    1006              : 
    1007        22579 :       CALL timeset(routineN, handle)
    1008              : 
    1009              :       ! Release SCF work storage
    1010        22579 :       CALL cp_fm_release(scf_env%scf_work1)
    1011              : 
    1012        22579 :       IF (ASSOCIATED(scf_env%scf_work1_red)) THEN
    1013           48 :          CALL cp_fm_release(scf_env%scf_work1_red)
    1014              :       END IF
    1015        22579 :       IF (ASSOCIATED(scf_env%scf_work2)) THEN
    1016        16348 :          CALL cp_fm_release(scf_env%scf_work2)
    1017        16348 :          DEALLOCATE (scf_env%scf_work2)
    1018              :          NULLIFY (scf_env%scf_work2)
    1019              :       END IF
    1020        22579 :       IF (ASSOCIATED(scf_env%scf_work2_red)) THEN
    1021           48 :          CALL cp_fm_release(scf_env%scf_work2_red)
    1022           48 :          DEALLOCATE (scf_env%scf_work2_red)
    1023              :          NULLIFY (scf_env%scf_work2_red)
    1024              :       END IF
    1025        22579 :       IF (ASSOCIATED(scf_env%ortho)) THEN
    1026        13684 :          CALL cp_fm_release(scf_env%ortho)
    1027        13684 :          DEALLOCATE (scf_env%ortho)
    1028              :          NULLIFY (scf_env%ortho)
    1029              :       END IF
    1030        22579 :       IF (ASSOCIATED(scf_env%ortho_red)) THEN
    1031           48 :          CALL cp_fm_release(scf_env%ortho_red)
    1032           48 :          DEALLOCATE (scf_env%ortho_red)
    1033              :          NULLIFY (scf_env%ortho_red)
    1034              :       END IF
    1035        22579 :       IF (ASSOCIATED(scf_env%ortho_m1)) THEN
    1036           52 :          CALL cp_fm_release(scf_env%ortho_m1)
    1037           52 :          DEALLOCATE (scf_env%ortho_m1)
    1038              :          NULLIFY (scf_env%ortho_m1)
    1039              :       END IF
    1040        22579 :       IF (ASSOCIATED(scf_env%ortho_m1_red)) THEN
    1041            6 :          CALL cp_fm_release(scf_env%ortho_m1_red)
    1042            6 :          DEALLOCATE (scf_env%ortho_m1_red)
    1043              :          NULLIFY (scf_env%ortho_m1_red)
    1044              :       END IF
    1045              : 
    1046        22579 :       IF (ASSOCIATED(scf_env%ortho_dbcsr)) THEN
    1047           58 :          CALL dbcsr_deallocate_matrix(scf_env%ortho_dbcsr)
    1048              :       END IF
    1049        22579 :       IF (ASSOCIATED(scf_env%buf1_dbcsr)) THEN
    1050           58 :          CALL dbcsr_deallocate_matrix(scf_env%buf1_dbcsr)
    1051              :       END IF
    1052        22579 :       IF (ASSOCIATED(scf_env%buf2_dbcsr)) THEN
    1053           58 :          CALL dbcsr_deallocate_matrix(scf_env%buf2_dbcsr)
    1054              :       END IF
    1055              : 
    1056        22579 :       IF (ASSOCIATED(scf_env%p_mix_new)) THEN
    1057        16366 :          CALL dbcsr_deallocate_matrix_set(scf_env%p_mix_new)
    1058              :       END IF
    1059              : 
    1060        22579 :       IF (ASSOCIATED(scf_env%p_delta)) THEN
    1061          682 :          CALL dbcsr_deallocate_matrix_set(scf_env%p_delta)
    1062              :       END IF
    1063              : 
    1064              :       ! Method dependent cleanup
    1065        22597 :       SELECT CASE (scf_env%method)
    1066              :       CASE (ot_method_nr)
    1067              :          !
    1068              :       CASE (ot_diag_method_nr)
    1069              :          !
    1070              :       CASE (general_diag_method_nr)
    1071              :          !
    1072              :       CASE (special_diag_method_nr)
    1073              :          !
    1074              :       CASE (block_krylov_diag_method_nr)
    1075              :       CASE (block_davidson_diag_method_nr)
    1076           18 :          CALL block_davidson_deallocate(scf_env%block_davidson_env)
    1077              :       CASE (filter_matrix_diag_method_nr)
    1078              :          !
    1079              :       CASE (smeagol_method_nr)
    1080              :          !
    1081              :       CASE DEFAULT
    1082        22579 :          CPABORT("unknown scf method method:"//cp_to_string(scf_env%method))
    1083              :       END SELECT
    1084              : 
    1085        22579 :       IF (ASSOCIATED(scf_env%outer_scf%variables)) THEN
    1086         4261 :          DEALLOCATE (scf_env%outer_scf%variables)
    1087              :       END IF
    1088        22579 :       IF (ASSOCIATED(scf_env%outer_scf%count)) THEN
    1089         4261 :          DEALLOCATE (scf_env%outer_scf%count)
    1090              :       END IF
    1091        22579 :       IF (ASSOCIATED(scf_env%outer_scf%gradient)) THEN
    1092         4261 :          DEALLOCATE (scf_env%outer_scf%gradient)
    1093              :       END IF
    1094        22579 :       IF (ASSOCIATED(scf_env%outer_scf%energy)) THEN
    1095         4261 :          DEALLOCATE (scf_env%outer_scf%energy)
    1096              :       END IF
    1097        22579 :       IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. &
    1098              :           scf_env%outer_scf%deallocate_jacobian) THEN
    1099           50 :          DEALLOCATE (scf_env%outer_scf%inv_jacobian)
    1100              :       END IF
    1101              : 
    1102        22579 :       CALL timestop(handle)
    1103              : 
    1104        22579 :    END SUBROUTINE scf_env_cleanup
    1105              : 
    1106              : ! **************************************************************************************************
    1107              : !> \brief perform a CDFT scf procedure in the given qs_env
    1108              : !> \param qs_env the qs_environment where to perform the scf procedure
    1109              : !> \param should_stop flag determining if calculation should stop
    1110              : !> \par History
    1111              : !>      12.2015 Created
    1112              : !> \author Nico Holmberg
    1113              : ! **************************************************************************************************
    1114          688 :    SUBROUTINE cdft_scf(qs_env, should_stop)
    1115              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1116              :       LOGICAL, INTENT(OUT)                               :: should_stop
    1117              : 
    1118              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cdft_scf'
    1119              : 
    1120              :       INTEGER                                            :: handle, iatom, ispin, ivar, nmo, nvar, &
    1121              :                                                             output_unit, tsteps
    1122              :       LOGICAL                                            :: cdft_loop_converged, converged, &
    1123              :                                                             exit_cdft_loop, first_iteration, &
    1124              :                                                             my_uocc, uniform_occupation
    1125          344 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: mo_occupations
    1126              :       TYPE(cdft_control_type), POINTER                   :: cdft_control
    1127              :       TYPE(cp_logger_type), POINTER                      :: logger
    1128          344 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s, rho_ao
    1129              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1130          344 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1131              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1132              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1133              :       TYPE(qs_energy_type), POINTER                      :: energy
    1134              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1135              :       TYPE(qs_rho_type), POINTER                         :: rho
    1136              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
    1137              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1138              :       TYPE(section_vals_type), POINTER                   :: dft_section, input, scf_section
    1139              : 
    1140          344 :       NULLIFY (scf_env, ks_env, energy, rho, matrix_s, rho_ao, cdft_control, logger, &
    1141          344 :                dft_control, pw_env, auxbas_pw_pool, energy, ks_env, scf_env, dft_section, &
    1142          344 :                input, scf_section, scf_control, mos, mo_occupations)
    1143          688 :       logger => cp_get_default_logger()
    1144              : 
    1145          344 :       CPASSERT(ASSOCIATED(qs_env))
    1146              :       CALL get_qs_env(qs_env, scf_env=scf_env, energy=energy, &
    1147              :                       dft_control=dft_control, scf_control=scf_control, &
    1148          344 :                       ks_env=ks_env, input=input)
    1149              : 
    1150          344 :       CALL timeset(routineN//"_loop", handle)
    1151          344 :       dft_section => section_vals_get_subs_vals(input, "DFT")
    1152          344 :       scf_section => section_vals_get_subs_vals(dft_section, "SCF")
    1153              :       output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
    1154          344 :                                          extension=".scfLog")
    1155          344 :       first_iteration = .TRUE.
    1156              : 
    1157          344 :       cdft_control => dft_control%qs_control%cdft_control
    1158              : 
    1159          344 :       scf_env%outer_scf%iter_count = 0
    1160          344 :       cdft_control%total_steps = 0
    1161              : 
    1162              :       ! Write some info about the CDFT calculation
    1163          344 :       IF (output_unit > 0) THEN
    1164              :          WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
    1165          191 :             "CDFT EXTERNAL SCF WAVEFUNCTION OPTIMIZATION"
    1166          191 :          CALL qs_scf_cdft_initial_info(output_unit, cdft_control)
    1167              :       END IF
    1168          344 :       IF (cdft_control%reuse_precond) THEN
    1169            0 :          reuse_precond = .FALSE.
    1170            0 :          cdft_control%nreused = 0
    1171              :       END IF
    1172          568 :       cdft_outer_loop: DO
    1173              :          ! Change outer_scf settings to OT settings
    1174          568 :          CALL outer_loop_switch(scf_env, scf_control, cdft_control, cdft2ot)
    1175              :          ! Solve electronic structure with fixed value of constraint
    1176              :          CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
    1177          568 :                              converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
    1178              :          ! Decide whether to reuse the preconditioner on the next iteration
    1179          568 :          IF (cdft_control%reuse_precond) THEN
    1180              :             ! For convergence in exactly one step, the preconditioner is always reused (assuming max_reuse > 0)
    1181              :             ! usually this means that the electronic structure has already converged to the correct state
    1182              :             ! but the constraint optimizer keeps jumping over the optimal solution
    1183              :             IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count == 1 &
    1184            0 :                 .AND. cdft_control%total_steps /= 1) &
    1185            0 :                cdft_control%nreused = cdft_control%nreused - 1
    1186              :             ! SCF converged in less than precond_freq steps
    1187              :             IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count <= cdft_control%precond_freq .AND. &
    1188            0 :                 cdft_control%total_steps /= 1 .AND. cdft_control%nreused < cdft_control%max_reuse) THEN
    1189            0 :                reuse_precond = .TRUE.
    1190            0 :                cdft_control%nreused = cdft_control%nreused + 1
    1191              :             ELSE
    1192            0 :                reuse_precond = .FALSE.
    1193            0 :                cdft_control%nreused = 0
    1194              :             END IF
    1195              :          END IF
    1196              :          ! Update history purging counters
    1197          568 :          IF (first_iteration .AND. cdft_control%purge_history) THEN
    1198            0 :             cdft_control%istep = cdft_control%istep + 1
    1199            0 :             IF (scf_env%outer_scf%iter_count > 1) THEN
    1200            0 :                cdft_control%nbad_conv = cdft_control%nbad_conv + 1
    1201            0 :                IF (cdft_control%nbad_conv >= cdft_control%purge_freq .AND. &
    1202              :                    cdft_control%istep >= cdft_control%purge_offset) THEN
    1203            0 :                   cdft_control%nbad_conv = 0
    1204            0 :                   cdft_control%istep = 0
    1205            0 :                   cdft_control%should_purge = .TRUE.
    1206              :                END IF
    1207              :             END IF
    1208              :          END IF
    1209          568 :          first_iteration = .FALSE.
    1210              :          ! Change outer_scf settings to CDFT settings
    1211          568 :          CALL outer_loop_switch(scf_env, scf_control, cdft_control, ot2cdft)
    1212              :          CALL qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, &
    1213          568 :                                       cdft_loop_converged, exit_cdft_loop)
    1214              :          CALL qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, &
    1215              :                                energy, cdft_control%total_steps, &
    1216          568 :                                should_stop, cdft_loop_converged, cdft_loop=.TRUE.)
    1217          568 :          IF (exit_cdft_loop) EXIT cdft_outer_loop
    1218              :          ! Check if the inverse Jacobian needs to be calculated
    1219          224 :          CALL qs_calculate_inverse_jacobian(qs_env)
    1220              :          ! Check if a line search should be performed to find an optimal step size for the optimizer
    1221          224 :          CALL qs_cdft_line_search(qs_env)
    1222              :          ! Optimize constraint
    1223          224 :          CALL outer_loop_optimize(scf_env, scf_control)
    1224          224 :          CALL outer_loop_update_qs_env(qs_env, scf_env)
    1225          568 :          CALL qs_ks_did_change(ks_env, potential_changed=.TRUE.)
    1226              :       END DO cdft_outer_loop
    1227              : 
    1228          344 :       cdft_control%ienergy = cdft_control%ienergy + 1
    1229              : 
    1230              :       ! Store needed arrays for ET coupling calculation
    1231          344 :       IF (cdft_control%do_et) THEN
    1232          186 :          CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, mos=mos)
    1233          186 :          nvar = SIZE(cdft_control%target)
    1234              :          ! Matrix representation of weight function
    1235          748 :          ALLOCATE (cdft_control%wmat(nvar))
    1236          376 :          DO ivar = 1, nvar
    1237          190 :             CALL dbcsr_init_p(cdft_control%wmat(ivar)%matrix)
    1238              :             CALL dbcsr_copy(cdft_control%wmat(ivar)%matrix, matrix_s(1)%matrix, &
    1239          190 :                             name="ET_RESTRAINT_MATRIX")
    1240          190 :             CALL dbcsr_set(cdft_control%wmat(ivar)%matrix, 0.0_dp)
    1241              :             CALL integrate_v_rspace(cdft_control%group(ivar)%weight, &
    1242              :                                     hmat=cdft_control%wmat(ivar), qs_env=qs_env, &
    1243              :                                     calculate_forces=.FALSE., &
    1244          376 :                                     gapw=dft_control%qs_control%gapw)
    1245              :          END DO
    1246              :          ! Overlap matrix
    1247          186 :          CALL dbcsr_init_p(cdft_control%matrix_s%matrix)
    1248              :          CALL dbcsr_copy(cdft_control%matrix_s%matrix, matrix_s(1)%matrix, &
    1249          186 :                          name="OVERLAP")
    1250              :          ! Molecular orbital coefficients
    1251          186 :          NULLIFY (cdft_control%mo_coeff)
    1252          920 :          ALLOCATE (cdft_control%mo_coeff(dft_control%nspins))
    1253          548 :          DO ispin = 1, dft_control%nspins
    1254              :             CALL cp_fm_create(matrix=cdft_control%mo_coeff(ispin), &
    1255              :                               matrix_struct=qs_env%mos(ispin)%mo_coeff%matrix_struct, &
    1256          362 :                               name="MO_COEFF_A"//TRIM(ADJUSTL(cp_to_string(ispin)))//"MATRIX")
    1257              :             CALL cp_fm_to_fm(qs_env%mos(ispin)%mo_coeff, &
    1258          548 :                              cdft_control%mo_coeff(ispin))
    1259              :          END DO
    1260              :          ! Density matrix
    1261          186 :          IF (cdft_control%calculate_metric) THEN
    1262           24 :             CALL get_qs_env(qs_env, rho=rho)
    1263           24 :             CALL qs_rho_get(rho, rho_ao=rho_ao)
    1264          120 :             ALLOCATE (cdft_control%matrix_p(dft_control%nspins))
    1265           72 :             DO ispin = 1, dft_control%nspins
    1266           48 :                NULLIFY (cdft_control%matrix_p(ispin)%matrix)
    1267           48 :                CALL dbcsr_init_p(cdft_control%matrix_p(ispin)%matrix)
    1268              :                CALL dbcsr_copy(cdft_control%matrix_p(ispin)%matrix, rho_ao(ispin)%matrix, &
    1269           72 :                                name="DENSITY MATRIX")
    1270              :             END DO
    1271              :          END IF
    1272              :          ! Copy occupation numbers if non-uniform occupation
    1273          186 :          uniform_occupation = .TRUE.
    1274          548 :          DO ispin = 1, dft_control%nspins
    1275          362 :             CALL get_mo_set(mo_set=mos(ispin), uniform_occupation=my_uocc)
    1276          604 :             uniform_occupation = uniform_occupation .AND. my_uocc
    1277              :          END DO
    1278          186 :          IF (.NOT. uniform_occupation) THEN
    1279          140 :             ALLOCATE (cdft_control%occupations(dft_control%nspins))
    1280           84 :             DO ispin = 1, dft_control%nspins
    1281              :                CALL get_mo_set(mo_set=mos(ispin), &
    1282              :                                nmo=nmo, &
    1283           56 :                                occupation_numbers=mo_occupations)
    1284          168 :                ALLOCATE (cdft_control%occupations(ispin)%array(nmo))
    1285          588 :                cdft_control%occupations(ispin)%array(1:nmo) = mo_occupations(1:nmo)
    1286              :             END DO
    1287              :          END IF
    1288              :       END IF
    1289              : 
    1290              :       ! Deallocate constraint storage if forces are not needed
    1291              :       ! In case of a simulation with multiple force_evals,
    1292              :       ! deallocate only if weight function should not be copied to different force_evals
    1293          344 :       IF (.NOT. (cdft_control%save_pot .OR. cdft_control%transfer_pot)) THEN
    1294          162 :          CALL get_qs_env(qs_env, pw_env=pw_env)
    1295          162 :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    1296          336 :          DO iatom = 1, SIZE(cdft_control%group)
    1297          174 :             CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
    1298          336 :             DEALLOCATE (cdft_control%group(iatom)%weight)
    1299              :          END DO
    1300          162 :          IF (cdft_control%atomic_charges) THEN
    1301          256 :             DO iatom = 1, cdft_control%natoms
    1302          256 :                CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
    1303              :             END DO
    1304           84 :             DEALLOCATE (cdft_control%charge)
    1305              :          END IF
    1306          162 :          IF (cdft_control%type == outer_scf_becke_constraint .AND. &
    1307              :              cdft_control%becke_control%cavity_confine) THEN
    1308          120 :             IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
    1309          110 :                CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
    1310              :             ELSE
    1311           10 :                DEALLOCATE (cdft_control%becke_control%cavity_mat)
    1312              :             END IF
    1313           42 :          ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
    1314           20 :             IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
    1315            0 :                CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
    1316              :             END IF
    1317              :          END IF
    1318          162 :          IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
    1319          162 :          cdft_control%need_pot = .TRUE.
    1320          162 :          cdft_control%external_control = .FALSE.
    1321              :       END IF
    1322              : 
    1323          344 :       CALL timestop(handle)
    1324              : 
    1325          344 :    END SUBROUTINE cdft_scf
    1326              : 
    1327              : ! **************************************************************************************************
    1328              : !> \brief perform cleanup operations for cdft_control
    1329              : !> \param cdft_control container for the external CDFT SCF loop variables
    1330              : !> \par History
    1331              : !>      12.2015 created [Nico Holmberg]
    1332              : !> \author Nico Holmberg
    1333              : ! **************************************************************************************************
    1334          344 :    SUBROUTINE cdft_control_cleanup(cdft_control)
    1335              :       TYPE(cdft_control_type), POINTER                   :: cdft_control
    1336              : 
    1337          344 :       IF (ASSOCIATED(cdft_control%constraint%variables)) &
    1338          344 :          DEALLOCATE (cdft_control%constraint%variables)
    1339          344 :       IF (ASSOCIATED(cdft_control%constraint%count)) &
    1340          344 :          DEALLOCATE (cdft_control%constraint%count)
    1341          344 :       IF (ASSOCIATED(cdft_control%constraint%gradient)) &
    1342          344 :          DEALLOCATE (cdft_control%constraint%gradient)
    1343          344 :       IF (ASSOCIATED(cdft_control%constraint%energy)) &
    1344          344 :          DEALLOCATE (cdft_control%constraint%energy)
    1345          344 :       IF (ASSOCIATED(cdft_control%constraint%inv_jacobian) .AND. &
    1346              :           cdft_control%constraint%deallocate_jacobian) &
    1347            4 :          DEALLOCATE (cdft_control%constraint%inv_jacobian)
    1348              : 
    1349          344 :    END SUBROUTINE cdft_control_cleanup
    1350              : 
    1351              : ! **************************************************************************************************
    1352              : !> \brief Calculates the finite difference inverse Jacobian
    1353              : !> \param qs_env the qs_environment_type where to compute the Jacobian
    1354              : !> \par History
    1355              : !>      01.2017 created [Nico Holmberg]
    1356              : ! **************************************************************************************************
    1357          224 :    SUBROUTINE qs_calculate_inverse_jacobian(qs_env)
    1358              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1359              : 
    1360              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_calculate_inverse_jacobian'
    1361              : 
    1362              :       CHARACTER(len=default_path_length)                 :: project_name
    1363              :       INTEGER                                            :: counter, handle, i, ispin, iter_count, &
    1364              :                                                             iwork, j, max_scf, nspins, nsteps, &
    1365              :                                                             nvar, nwork, output_unit, pwork, &
    1366              :                                                             tsteps, twork
    1367              :       LOGICAL                                            :: converged, explicit_jacobian, &
    1368              :                                                             should_build, should_stop, &
    1369              :                                                             use_md_history
    1370              :       REAL(KIND=dp)                                      :: inv_error, step_size
    1371          224 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: coeff, dh, step_multiplier
    1372          224 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: jacobian
    1373          224 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: energy
    1374          224 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: gradient, inv_jacobian
    1375              :       TYPE(cdft_control_type), POINTER                   :: cdft_control
    1376              :       TYPE(cp_logger_type), POINTER                      :: logger, tmp_logger
    1377          224 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: p_rmpv
    1378          224 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao_kp
    1379              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1380          224 :       TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:)       :: mos_stashed
    1381          224 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1382              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1383              :       TYPE(qs_energy_type), POINTER                      :: energy_qs
    1384              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1385              :       TYPE(qs_rho_type), POINTER                         :: rho
    1386              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
    1387              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1388              : 
    1389          224 :       NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
    1390          224 :                ks_env, scf_env, scf_control, dft_control, cdft_control, &
    1391          224 :                inv_jacobian, para_env, tmp_logger, energy_qs)
    1392          448 :       logger => cp_get_default_logger()
    1393              : 
    1394          224 :       CPASSERT(ASSOCIATED(qs_env))
    1395              :       CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
    1396              :                       scf_control=scf_control, mos=mos, rho=rho, &
    1397              :                       dft_control=dft_control, &
    1398          224 :                       para_env=para_env, energy=energy_qs)
    1399          224 :       explicit_jacobian = .FALSE.
    1400          224 :       should_build = .FALSE.
    1401          224 :       use_md_history = .FALSE.
    1402          224 :       iter_count = scf_env%outer_scf%iter_count
    1403              :       ! Quick exit if optimizer does not require Jacobian
    1404          224 :       IF (.NOT. ASSOCIATED(scf_control%outer_scf%cdft_opt_control)) RETURN
    1405              :       ! Check if Jacobian should be calculated and initialize
    1406          118 :       CALL timeset(routineN, handle)
    1407          118 :       CALL initialize_inverse_jacobian(scf_control, scf_env, explicit_jacobian, should_build, used_history)
    1408          118 :       IF (scf_control%outer_scf%cdft_opt_control%jacobian_restart) THEN
    1409              :          ! Restart from previously calculated inverse Jacobian
    1410            6 :          should_build = .FALSE.
    1411            6 :          CALL restart_inverse_jacobian(qs_env)
    1412              :       END IF
    1413          118 :       IF (should_build) THEN
    1414           78 :          scf_env%outer_scf%deallocate_jacobian = .FALSE.
    1415              :          ! Actually need to (re)build the Jacobian
    1416           78 :          IF (explicit_jacobian) THEN
    1417              :             ! Build Jacobian with finite differences
    1418           62 :             cdft_control => dft_control%qs_control%cdft_control
    1419           62 :             IF (.NOT. ASSOCIATED(cdft_control)) &
    1420              :                CALL cp_abort(__LOCATION__, &
    1421              :                              "Optimizers that need the explicit Jacobian can"// &
    1422            0 :                              " only be used together with a valid CDFT constraint.")
    1423              :             ! Redirect output from Jacobian calculation to a new file by creating a temporary logger
    1424           62 :             project_name = logger%iter_info%project_name
    1425           62 :             CALL create_tmp_logger(para_env, project_name, "-JacobianInfo.out", output_unit, tmp_logger)
    1426              :             ! Save last converged state so we can roll back to it (mo_coeff and some outer_loop variables)
    1427           62 :             nspins = dft_control%nspins
    1428          310 :             ALLOCATE (mos_stashed(nspins))
    1429          186 :             DO ispin = 1, nspins
    1430          186 :                CALL duplicate_mo_set(mos_stashed(ispin), mos(ispin))
    1431              :             END DO
    1432           62 :             CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
    1433           62 :             p_rmpv => rho_ao_kp(:, 1)
    1434              :             ! Allocate work
    1435           62 :             nvar = SIZE(scf_env%outer_scf%variables, 1)
    1436           62 :             max_scf = scf_control%outer_scf%max_scf + 1
    1437          248 :             ALLOCATE (gradient(nvar, max_scf))
    1438         1310 :             gradient = scf_env%outer_scf%gradient
    1439          186 :             ALLOCATE (energy(max_scf))
    1440          594 :             energy = scf_env%outer_scf%energy
    1441          248 :             ALLOCATE (jacobian(nvar, nvar))
    1442           62 :             jacobian = 0.0_dp
    1443           62 :             nsteps = cdft_control%total_steps
    1444              :             ! Setup finite difference scheme
    1445           62 :             CALL prepare_jacobian_stencil(qs_env, output_unit, nwork, pwork, coeff, step_multiplier, dh)
    1446           62 :             twork = pwork - nwork
    1447          148 :             DO i = 1, nvar
    1448          282 :                jacobian(i, :) = coeff(0)*scf_env%outer_scf%gradient(i, iter_count)
    1449              :             END DO
    1450              :             ! Calculate the Jacobian by perturbing each Lagrangian and recalculating the energy self-consistently
    1451           62 :             CALL cp_add_default_logger(tmp_logger)
    1452          148 :             DO i = 1, nvar
    1453           86 :                IF (output_unit > 0) THEN
    1454           43 :                   WRITE (output_unit, FMT="(A)") " "
    1455           43 :                   WRITE (output_unit, FMT="(A)") " #####################################"
    1456              :                   WRITE (output_unit, '(A,I3,A,I3,A)') &
    1457           43 :                      " ###  Constraint        ", i, " of ", nvar, " ###"
    1458           43 :                   WRITE (output_unit, FMT="(A)") " #####################################"
    1459              :                END IF
    1460           86 :                counter = 0
    1461          332 :                DO iwork = nwork, pwork
    1462          184 :                   IF (iwork == 0) CYCLE
    1463           98 :                   counter = counter + 1
    1464           98 :                   IF (output_unit > 0) THEN
    1465           49 :                      WRITE (output_unit, FMT="(A)") " #####################################"
    1466              :                      WRITE (output_unit, '(A,I3,A,I3,A)') &
    1467           49 :                         " ###  Energy evaluation ", counter, " of ", twork, " ###"
    1468           49 :                      WRITE (output_unit, FMT="(A)") " #####################################"
    1469              :                   END IF
    1470           98 :                   IF (SIZE(scf_control%outer_scf%cdft_opt_control%jacobian_step) == 1) THEN
    1471           90 :                      step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(1)
    1472              :                   ELSE
    1473            8 :                      step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(i)
    1474              :                   END IF
    1475          244 :                   scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count)
    1476              :                   scf_env%outer_scf%variables(i, iter_count + 1) = scf_env%outer_scf%variables(i, iter_count) + &
    1477           98 :                                                                    step_multiplier(iwork)*step_size
    1478           98 :                   CALL outer_loop_update_qs_env(qs_env, scf_env)
    1479           98 :                   CALL qs_ks_did_change(ks_env, potential_changed=.TRUE.)
    1480           98 :                   CALL outer_loop_switch(scf_env, scf_control, cdft_control, cdft2ot)
    1481              :                   CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
    1482           98 :                                       converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
    1483           98 :                   CALL outer_loop_switch(scf_env, scf_control, cdft_control, ot2cdft)
    1484              :                   ! Update (iter_count + 1) element of gradient and print constraint info
    1485           98 :                   scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
    1486           98 :                   CALL outer_loop_gradient(qs_env, scf_env)
    1487              :                   CALL qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, &
    1488              :                                         energy_qs, cdft_control%total_steps, &
    1489           98 :                                         should_stop=.FALSE., outer_loop_converged=.FALSE., cdft_loop=.FALSE.)
    1490           98 :                   scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
    1491              :                   ! Update Jacobian
    1492          244 :                   DO j = 1, nvar
    1493          244 :                      jacobian(j, i) = jacobian(j, i) + coeff(iwork)*scf_env%outer_scf%gradient(j, iter_count + 1)
    1494              :                   END DO
    1495              :                   ! Reset everything to last converged state
    1496          244 :                   scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
    1497         2026 :                   scf_env%outer_scf%gradient = gradient
    1498          878 :                   scf_env%outer_scf%energy = energy
    1499           98 :                   cdft_control%total_steps = nsteps
    1500          294 :                   DO ispin = 1, nspins
    1501          196 :                      CALL deallocate_mo_set(mos(ispin))
    1502          196 :                      CALL duplicate_mo_set(mos(ispin), mos_stashed(ispin))
    1503              :                      CALL calculate_density_matrix(mos(ispin), &
    1504          294 :                                                    p_rmpv(ispin)%matrix)
    1505              :                   END DO
    1506           98 :                   CALL qs_rho_update_rho(rho, qs_env=qs_env)
    1507          368 :                   CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
    1508              :                END DO
    1509              :             END DO
    1510           62 :             CALL cp_rm_default_logger()
    1511           62 :             CALL cp_logger_release(tmp_logger)
    1512              :             ! Finalize and invert Jacobian
    1513          148 :             DO j = 1, nvar
    1514          282 :                DO i = 1, nvar
    1515          220 :                   jacobian(i, j) = jacobian(i, j)/dh(j)
    1516              :                END DO
    1517              :             END DO
    1518           62 :             IF (.NOT. ASSOCIATED(scf_env%outer_scf%inv_jacobian)) &
    1519          102 :                ALLOCATE (scf_env%outer_scf%inv_jacobian(nvar, nvar))
    1520           62 :             inv_jacobian => scf_env%outer_scf%inv_jacobian
    1521           62 :             CALL invert_matrix(jacobian, inv_jacobian, inv_error)
    1522           62 :             scf_control%outer_scf%cdft_opt_control%broyden_update = .FALSE.
    1523              :             ! Release temporary storage
    1524          186 :             DO ispin = 1, nspins
    1525          186 :                CALL deallocate_mo_set(mos_stashed(ispin))
    1526              :             END DO
    1527           62 :             DEALLOCATE (mos_stashed, jacobian, gradient, energy, coeff, step_multiplier, dh)
    1528          186 :             IF (output_unit > 0) THEN
    1529              :                WRITE (output_unit, FMT="(/,A)") &
    1530           31 :                   " ================================== JACOBIAN CALCULATED =================================="
    1531           31 :                CALL close_file(unit_number=output_unit)
    1532              :             END IF
    1533              :          ELSE
    1534              :             ! Build a strictly diagonal Jacobian from history and invert it
    1535           16 :             CALL build_diagonal_jacobian(qs_env, used_history)
    1536              :          END IF
    1537              :       END IF
    1538          118 :       IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. para_env%is_source()) THEN
    1539              :          ! Write restart file for inverse Jacobian
    1540           55 :          CALL print_inverse_jacobian(logger, scf_env%outer_scf%inv_jacobian, iter_count)
    1541              :       END IF
    1542              :       ! Update counter
    1543          118 :       scf_control%outer_scf%cdft_opt_control%ijacobian(1) = scf_control%outer_scf%cdft_opt_control%ijacobian(1) + 1
    1544          118 :       CALL timestop(handle)
    1545              : 
    1546          448 :    END SUBROUTINE qs_calculate_inverse_jacobian
    1547              : 
    1548              : ! **************************************************************************************************
    1549              : !> \brief Perform backtracking line search to find the optimal step size for the CDFT constraint
    1550              : !>        optimizer. Assumes that the CDFT gradient function is a smooth function of the constraint
    1551              : !>        variables.
    1552              : !> \param qs_env the qs_environment_type where to perform the line search
    1553              : !> \par History
    1554              : !>      02.2017 created [Nico Holmberg]
    1555              : ! **************************************************************************************************
    1556          224 :    SUBROUTINE qs_cdft_line_search(qs_env)
    1557              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1558              : 
    1559              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_cdft_line_search'
    1560              : 
    1561              :       CHARACTER(len=default_path_length)                 :: project_name
    1562              :       INTEGER                                            :: handle, i, ispin, iter_count, &
    1563              :                                                             max_linesearch, max_scf, nspins, &
    1564              :                                                             nsteps, nvar, output_unit, tsteps
    1565              :       LOGICAL :: continue_ls, continue_ls_exit, converged, do_linesearch, found_solution, &
    1566              :          reached_maxls, should_exit, should_stop, sign_changed
    1567          224 :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: positive_sign
    1568              :       REAL(KIND=dp)                                      :: alpha, alpha_ls, factor, norm_ls
    1569          224 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: energy
    1570          224 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: gradient, inv_jacobian
    1571              :       REAL(KIND=dp), EXTERNAL                            :: dnrm2
    1572              :       TYPE(cdft_control_type), POINTER                   :: cdft_control
    1573              :       TYPE(cp_logger_type), POINTER                      :: logger, tmp_logger
    1574          224 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: p_rmpv
    1575          224 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao_kp
    1576              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1577          224 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1578              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1579              :       TYPE(qs_energy_type), POINTER                      :: energy_qs
    1580              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1581              :       TYPE(qs_rho_type), POINTER                         :: rho
    1582              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
    1583              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1584              : 
    1585          224 :       CALL timeset(routineN, handle)
    1586              : 
    1587          224 :       NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
    1588          224 :                ks_env, scf_env, scf_control, dft_control, &
    1589          224 :                cdft_control, inv_jacobian, para_env, &
    1590          224 :                tmp_logger, energy_qs)
    1591          224 :       logger => cp_get_default_logger()
    1592              : 
    1593          224 :       CPASSERT(ASSOCIATED(qs_env))
    1594              :       CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
    1595              :                       scf_control=scf_control, mos=mos, rho=rho, &
    1596              :                       dft_control=dft_control, &
    1597          224 :                       para_env=para_env, energy=energy_qs)
    1598          224 :       do_linesearch = .FALSE.
    1599          224 :       SELECT CASE (scf_control%outer_scf%optimizer)
    1600              :       CASE DEFAULT
    1601              :          do_linesearch = .FALSE.
    1602              :       CASE (outer_scf_optimizer_newton_ls)
    1603           24 :          do_linesearch = .TRUE.
    1604              :       CASE (outer_scf_optimizer_broyden)
    1605          224 :          SELECT CASE (scf_control%outer_scf%cdft_opt_control%broyden_type)
    1606              :          CASE (broyden_type_1, broyden_type_2, broyden_type_1_explicit, broyden_type_2_explicit)
    1607            0 :             do_linesearch = .FALSE.
    1608              :          CASE (broyden_type_1_ls, broyden_type_1_explicit_ls, broyden_type_2_ls, broyden_type_2_explicit_ls)
    1609            0 :             cdft_control => dft_control%qs_control%cdft_control
    1610            0 :             IF (.NOT. ASSOCIATED(cdft_control)) &
    1611              :                CALL cp_abort(__LOCATION__, &
    1612              :                              "Optimizers that perform a line search can"// &
    1613            0 :                              " only be used together with a valid CDFT constraint")
    1614            0 :             IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian)) &
    1615           24 :                do_linesearch = .TRUE.
    1616              :          END SELECT
    1617              :       END SELECT
    1618              :       IF (do_linesearch) THEN
    1619            8 :          BLOCK
    1620            8 :             TYPE(mo_set_type), DIMENSION(:), ALLOCATABLE :: mos_ls, mos_stashed
    1621            8 :             cdft_control => dft_control%qs_control%cdft_control
    1622            8 :             IF (.NOT. ASSOCIATED(cdft_control)) &
    1623              :                CALL cp_abort(__LOCATION__, &
    1624              :                              "Optimizers that perform a line search can"// &
    1625            0 :                              " only be used together with a valid CDFT constraint")
    1626            8 :             CPASSERT(ASSOCIATED(scf_env%outer_scf%inv_jacobian))
    1627            8 :             CPASSERT(ASSOCIATED(scf_control%outer_scf%cdft_opt_control))
    1628            8 :             alpha = scf_control%outer_scf%cdft_opt_control%newton_step_save
    1629            8 :             iter_count = scf_env%outer_scf%iter_count
    1630              :             ! Redirect output from line search procedure to a new file by creating a temporary logger
    1631            8 :             project_name = logger%iter_info%project_name
    1632            8 :             CALL create_tmp_logger(para_env, project_name, "-LineSearch.out", output_unit, tmp_logger)
    1633              :             ! Save last converged state so we can roll back to it (mo_coeff and some outer_loop variables)
    1634            8 :             nspins = dft_control%nspins
    1635           40 :             ALLOCATE (mos_stashed(nspins))
    1636           24 :             DO ispin = 1, nspins
    1637           24 :                CALL duplicate_mo_set(mos_stashed(ispin), mos(ispin))
    1638              :             END DO
    1639            8 :             CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
    1640            8 :             p_rmpv => rho_ao_kp(:, 1)
    1641            8 :             nsteps = cdft_control%total_steps
    1642              :             ! Allocate work
    1643            8 :             nvar = SIZE(scf_env%outer_scf%variables, 1)
    1644            8 :             max_scf = scf_control%outer_scf%max_scf + 1
    1645            8 :             max_linesearch = scf_control%outer_scf%cdft_opt_control%max_ls
    1646            8 :             continue_ls = scf_control%outer_scf%cdft_opt_control%continue_ls
    1647            8 :             factor = scf_control%outer_scf%cdft_opt_control%factor_ls
    1648            8 :             continue_ls_exit = .FALSE.
    1649            8 :             found_solution = .FALSE.
    1650           32 :             ALLOCATE (gradient(nvar, max_scf))
    1651          104 :             gradient = scf_env%outer_scf%gradient
    1652           24 :             ALLOCATE (energy(max_scf))
    1653           56 :             energy = scf_env%outer_scf%energy
    1654            8 :             reached_maxls = .FALSE.
    1655              :             ! Broyden optimizers: perform update of inv_jacobian if necessary
    1656            8 :             IF (scf_control%outer_scf%cdft_opt_control%broyden_update) THEN
    1657            0 :                CALL outer_loop_optimize(scf_env, scf_control)
    1658              :                ! Reset the variables and prevent a reupdate of inv_jacobian
    1659            0 :                scf_env%outer_scf%variables(:, iter_count + 1) = 0
    1660            0 :                scf_control%outer_scf%cdft_opt_control%broyden_update = .FALSE.
    1661              :             END IF
    1662              :             ! Print some info
    1663            8 :             IF (output_unit > 0) THEN
    1664              :                WRITE (output_unit, FMT="(/,A)") &
    1665            4 :                   " ================================== LINE SEARCH STARTED  =================================="
    1666              :                WRITE (output_unit, FMT="(A,I5,A)") &
    1667            4 :                   " Evaluating optimal step size for optimizer using a maximum of", max_linesearch, " steps"
    1668            4 :                IF (continue_ls) THEN
    1669              :                   WRITE (output_unit, FMT="(A)") &
    1670            2 :                      " Line search continues until best step size is found or max steps are reached"
    1671              :                END IF
    1672              :                WRITE (output_unit, '(/,A,F5.3)') &
    1673            4 :                   " Initial step size: ", alpha
    1674              :                WRITE (output_unit, '(/,A,F5.3)') &
    1675            4 :                   " Step size update factor: ", factor
    1676              :                WRITE (output_unit, '(/,A,I10,A,I10)') &
    1677            4 :                   " Energy evaluation: ", cdft_control%ienergy, ", CDFT SCF iteration: ", iter_count
    1678              :             END IF
    1679              :             ! Perform backtracking line search
    1680            8 :             CALL cp_add_default_logger(tmp_logger)
    1681           16 :             DO i = 1, max_linesearch
    1682           16 :                IF (output_unit > 0) THEN
    1683            8 :                   WRITE (output_unit, FMT="(A)") " "
    1684            8 :                   WRITE (output_unit, FMT="(A)") " #####################################"
    1685              :                   WRITE (output_unit, '(A,I10,A)') &
    1686            8 :                      " ###  Line search step: ", i, " ###"
    1687            8 :                   WRITE (output_unit, FMT="(A)") " #####################################"
    1688              :                END IF
    1689           16 :                inv_jacobian => scf_env%outer_scf%inv_jacobian
    1690              :                ! Newton update of CDFT variables with a step size of alpha
    1691              :                scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count) - alpha* &
    1692          128 :                                                                 MATMUL(inv_jacobian, scf_env%outer_scf%gradient(:, iter_count))
    1693              :                ! With updated CDFT variables, perform SCF
    1694           16 :                CALL outer_loop_update_qs_env(qs_env, scf_env)
    1695           16 :                CALL qs_ks_did_change(ks_env, potential_changed=.TRUE.)
    1696           16 :                CALL outer_loop_switch(scf_env, scf_control, cdft_control, cdft2ot)
    1697              :                CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
    1698           16 :                                    converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
    1699           16 :                CALL outer_loop_switch(scf_env, scf_control, cdft_control, ot2cdft)
    1700              :                ! Update (iter_count + 1) element of gradient and print constraint info
    1701           16 :                scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
    1702           16 :                CALL outer_loop_gradient(qs_env, scf_env)
    1703              :                CALL qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, &
    1704              :                                      energy_qs, cdft_control%total_steps, &
    1705           16 :                                      should_stop=.FALSE., outer_loop_converged=.FALSE., cdft_loop=.FALSE.)
    1706           16 :                scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
    1707              :                ! Store sign of initial gradient for each variable for continue_ls
    1708           16 :                IF (continue_ls .AND. .NOT. ALLOCATED(positive_sign)) THEN
    1709           12 :                   ALLOCATE (positive_sign(nvar))
    1710            8 :                   DO ispin = 1, nvar
    1711            8 :                      positive_sign(ispin) = scf_env%outer_scf%gradient(ispin, iter_count + 1) >= 0.0_dp
    1712              :                   END DO
    1713              :                END IF
    1714              :                ! Check if the L2 norm of the gradient decreased
    1715           16 :                inv_jacobian => scf_env%outer_scf%inv_jacobian
    1716           16 :                IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) < &
    1717              :                    dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count), 1)) THEN
    1718              :                   ! Optimal step size found
    1719           14 :                   IF (.NOT. continue_ls) THEN
    1720              :                      should_exit = .TRUE.
    1721              :                   ELSE
    1722              :                      ! But line search continues for at least one more iteration in an attempt to find a better solution
    1723              :                      ! if max number of steps is not exceeded
    1724           10 :                      IF (found_solution) THEN
    1725              :                         ! Check if the norm also decreased w.r.t. to previously found solution
    1726            6 :                         IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) > norm_ls) THEN
    1727              :                            ! Norm increased => accept previous solution and exit
    1728              :                            continue_ls_exit = .TRUE.
    1729              :                         END IF
    1730              :                      END IF
    1731              :                      ! Store current state and the value of alpha
    1732           10 :                      IF (.NOT. continue_ls_exit) THEN
    1733           10 :                         should_exit = .FALSE.
    1734           10 :                         alpha_ls = alpha
    1735           10 :                         found_solution = .TRUE.
    1736           10 :                         norm_ls = dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1)
    1737              :                         ! Check if the sign of the gradient has changed for all variables (w.r.t initial gradient)
    1738              :                         ! In this case we should exit because further line search steps will just increase the norm
    1739           10 :                         sign_changed = .TRUE.
    1740           20 :                         DO ispin = 1, nvar
    1741              :                            sign_changed = sign_changed .AND. (positive_sign(ispin) .NEQV. &
    1742           28 :                                                               scf_env%outer_scf%gradient(ispin, iter_count + 1) >= 0.0_dp)
    1743              :                         END DO
    1744           10 :                         IF (.NOT. ALLOCATED(mos_ls)) THEN
    1745           16 :                            ALLOCATE (mos_ls(nspins))
    1746              :                         ELSE
    1747           18 :                            DO ispin = 1, nspins
    1748           18 :                               CALL deallocate_mo_set(mos_ls(ispin))
    1749              :                            END DO
    1750              :                         END IF
    1751           30 :                         DO ispin = 1, nspins
    1752           30 :                            CALL duplicate_mo_set(mos_ls(ispin), mos(ispin))
    1753              :                         END DO
    1754           10 :                         alpha = alpha*factor
    1755              :                         ! Exit on last iteration
    1756           10 :                         IF (i == max_linesearch) continue_ls_exit = .TRUE.
    1757              :                         ! Exit if constraint target is satisfied to requested tolerance
    1758           20 :                         IF (SQRT(MAXVAL(scf_env%outer_scf%gradient(:, scf_env%outer_scf%iter_count + 1)**2)) < &
    1759              :                             scf_control%outer_scf%eps_scf) &
    1760            2 :                            continue_ls_exit = .TRUE.
    1761              :                         ! Exit if line search jumped over the optimal step length
    1762           10 :                         IF (sign_changed) continue_ls_exit = .TRUE.
    1763              :                      END IF
    1764              :                   END IF
    1765              :                ELSE
    1766              :                   ! Gradient increased => alpha is too large (if the gradient function is smooth)
    1767            2 :                   should_exit = .FALSE.
    1768              :                   ! Update alpha using Armijo's scheme
    1769            2 :                   alpha = alpha*factor
    1770              :                END IF
    1771           14 :                IF (continue_ls_exit) THEN
    1772              :                   ! Continuation of line search did not yield a better alpha, use previously located solution and exit
    1773            4 :                   alpha = alpha_ls
    1774           12 :                   DO ispin = 1, nspins
    1775            8 :                      CALL deallocate_mo_set(mos(ispin))
    1776            8 :                      CALL duplicate_mo_set(mos(ispin), mos_ls(ispin))
    1777              :                      CALL calculate_density_matrix(mos(ispin), &
    1778            8 :                                                    p_rmpv(ispin)%matrix)
    1779           12 :                      CALL deallocate_mo_set(mos_ls(ispin))
    1780              :                   END DO
    1781            4 :                   CALL qs_rho_update_rho(rho, qs_env=qs_env)
    1782            4 :                   CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
    1783            4 :                   DEALLOCATE (mos_ls)
    1784              :                   should_exit = .TRUE.
    1785              :                END IF
    1786              :                ! Reached max steps and SCF converged: continue with last iterated step size
    1787           12 :                IF (.NOT. should_exit .AND. &
    1788              :                    (i == max_linesearch .AND. converged .AND. .NOT. found_solution)) THEN
    1789            0 :                   should_exit = .TRUE.
    1790            0 :                   reached_maxls = .TRUE.
    1791            0 :                   alpha = alpha*(1.0_dp/factor)
    1792              :                END IF
    1793              :                ! Reset outer SCF environment to last converged state
    1794           32 :                scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
    1795          208 :                scf_env%outer_scf%gradient = gradient
    1796          112 :                scf_env%outer_scf%energy = energy
    1797              :                ! Exit line search if a suitable step size was found
    1798           16 :                IF (should_exit) EXIT
    1799              :                ! Reset the electronic structure
    1800            8 :                cdft_control%total_steps = nsteps
    1801           24 :                DO ispin = 1, nspins
    1802           16 :                   CALL deallocate_mo_set(mos(ispin))
    1803           16 :                   CALL duplicate_mo_set(mos(ispin), mos_stashed(ispin))
    1804              :                   CALL calculate_density_matrix(mos(ispin), &
    1805           24 :                                                 p_rmpv(ispin)%matrix)
    1806              :                END DO
    1807            8 :                CALL qs_rho_update_rho(rho, qs_env=qs_env)
    1808           24 :                CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
    1809              :             END DO
    1810            8 :             scf_control%outer_scf%cdft_opt_control%newton_step = alpha
    1811            8 :             IF (.NOT. should_exit) THEN
    1812              :                CALL cp_warn(__LOCATION__, &
    1813            0 :                             "Line search did not converge. CDFT SCF proceeds with fixed step size.")
    1814            0 :                scf_control%outer_scf%cdft_opt_control%newton_step = scf_control%outer_scf%cdft_opt_control%newton_step_save
    1815              :             END IF
    1816            8 :             IF (reached_maxls) &
    1817              :                CALL cp_warn(__LOCATION__, &
    1818            0 :                             "Line search did not converge. CDFT SCF proceeds with lasted iterated step size.")
    1819            8 :             CALL cp_rm_default_logger()
    1820            8 :             CALL cp_logger_release(tmp_logger)
    1821              :             ! Release temporary storage
    1822           24 :             DO ispin = 1, nspins
    1823           24 :                CALL deallocate_mo_set(mos_stashed(ispin))
    1824              :             END DO
    1825            8 :             DEALLOCATE (mos_stashed, gradient, energy)
    1826            8 :             IF (ALLOCATED(positive_sign)) DEALLOCATE (positive_sign)
    1827           20 :             IF (output_unit > 0) THEN
    1828              :                WRITE (output_unit, FMT="(/,A)") &
    1829            4 :                   " ================================== LINE SEARCH COMPLETE =================================="
    1830            4 :                CALL close_file(unit_number=output_unit)
    1831              :             END IF
    1832              :          END BLOCK
    1833              :       END IF
    1834              : 
    1835          224 :       CALL timestop(handle)
    1836              : 
    1837          224 :    END SUBROUTINE qs_cdft_line_search
    1838              : 
    1839           16 : END MODULE qs_scf
        

Generated by: LCOV version 2.0-1