LCOV - code coverage report
Current view: top level - src - qs_scf_initialization.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:b4bd748) Lines: 464 503 92.2 %
Date: 2025-03-09 07:56:22 Functions: 12 12 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief Utility routines for qs_scf
      10             : ! **************************************************************************************************
      11             : MODULE qs_scf_initialization
      12             :    USE cp_control_types,                ONLY: dft_control_type
      13             :    USE cp_dbcsr_api,                    ONLY: dbcsr_create,&
      14             :                                               dbcsr_init_p,&
      15             :                                               dbcsr_p_type,&
      16             :                                               dbcsr_type,&
      17             :                                               dbcsr_type_no_symmetry
      18             :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      19             :                                               copy_fm_to_dbcsr,&
      20             :                                               cp_dbcsr_m_by_n_from_row_template,&
      21             :                                               cp_dbcsr_sm_fm_multiply
      22             :    USE cp_dbcsr_output,                 ONLY: write_fm_with_basis_info
      23             :    USE cp_fm_basic_linalg,              ONLY: cp_fm_column_scale,&
      24             :                                               cp_fm_row_scale,&
      25             :                                               cp_fm_transpose,&
      26             :                                               cp_fm_triangular_invert
      27             :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose
      28             :    USE cp_fm_diag,                      ONLY: choose_eigv_solver,&
      29             :                                               cp_fm_power
      30             :    USE cp_fm_pool_types,                ONLY: cp_fm_pool_p_type,&
      31             :                                               fm_pool_get_el_struct
      32             :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      33             :                                               cp_fm_struct_get,&
      34             :                                               cp_fm_struct_release,&
      35             :                                               cp_fm_struct_type
      36             :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      37             :                                               cp_fm_get_info,&
      38             :                                               cp_fm_release,&
      39             :                                               cp_fm_set_all,&
      40             :                                               cp_fm_to_fm,&
      41             :                                               cp_fm_to_fm_triangular,&
      42             :                                               cp_fm_type
      43             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      44             :                                               cp_logger_type,&
      45             :                                               cp_to_string
      46             :    USE cp_output_handling,              ONLY: cp_p_file,&
      47             :                                               cp_print_key_finished_output,&
      48             :                                               cp_print_key_should_output,&
      49             :                                               cp_print_key_unit_nr
      50             :    USE input_constants,                 ONLY: &
      51             :         broy_mix, cholesky_dbcsr, cholesky_inverse, cholesky_off, diag_block_davidson, &
      52             :         diag_block_krylov, diag_filter_matrix, diag_ot, diag_standard, direct_p_mix, kerker_mix, &
      53             :         multisec_mix, no_mix, ot2cdft, outer_scf_none, plus_u_lowdin, pulay_mix, &
      54             :         smeagol_runtype_emtransport, wfi_frozen_method_nr, wfi_ps_method_nr, &
      55             :         wfi_use_guess_method_nr
      56             :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      57             :                                               section_vals_type,&
      58             :                                               section_vals_val_get
      59             :    USE kinds,                           ONLY: dp
      60             :    USE kpoint_types,                    ONLY: kpoint_type
      61             :    USE message_passing,                 ONLY: mp_para_env_type
      62             :    USE parallel_gemm_api,               ONLY: parallel_gemm
      63             :    USE pw_types,                        ONLY: pw_c1d_gs_type
      64             :    USE qmmm_image_charge,               ONLY: conditional_calc_image_matrix
      65             :    USE qs_block_davidson_types,         ONLY: block_davidson_allocate,&
      66             :                                               block_davidson_env_create
      67             :    USE qs_cdft_opt_types,               ONLY: cdft_opt_type_copy
      68             :    USE qs_density_mixing_types,         ONLY: direct_mixing_nr,&
      69             :                                               mixing_storage_create,&
      70             :                                               mixing_storage_release,&
      71             :                                               no_mixing_nr
      72             :    USE qs_environment_types,            ONLY: get_qs_env,&
      73             :                                               qs_environment_type,&
      74             :                                               set_qs_env
      75             :    USE qs_fb_distribution_methods,      ONLY: fb_distribution_build
      76             :    USE qs_fb_env_methods,               ONLY: fb_env_build_atomic_halos,&
      77             :                                               fb_env_build_rcut_auto,&
      78             :                                               fb_env_read_input,&
      79             :                                               fb_env_write_info
      80             :    USE qs_fb_env_types,                 ONLY: fb_env_create,&
      81             :                                               fb_env_has_data
      82             :    USE qs_harris_types,                 ONLY: harris_type
      83             :    USE qs_harris_utils,                 ONLY: harris_density_update
      84             :    USE qs_initial_guess,                ONLY: calculate_first_density_matrix
      85             :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      86             :                                               qs_kind_type,&
      87             :                                               set_qs_kind
      88             :    USE qs_ks_types,                     ONLY: qs_ks_did_change
      89             :    USE qs_matrix_pools,                 ONLY: mpools_get
      90             :    USE qs_mixing_utils,                 ONLY: charge_mixing_init,&
      91             :                                               mixing_allocate,&
      92             :                                               mixing_init
      93             :    USE qs_mo_occupation,                ONLY: set_mo_occupation
      94             :    USE qs_mo_types,                     ONLY: get_mo_set,&
      95             :                                               init_mo_set,&
      96             :                                               mo_set_type,&
      97             :                                               set_mo_set
      98             :    USE qs_outer_scf,                    ONLY: outer_loop_extrapolate,&
      99             :                                               outer_loop_switch,&
     100             :                                               outer_loop_variables_count
     101             :    USE qs_rho_atom_types,               ONLY: rho_atom_type
     102             :    USE qs_rho_methods,                  ONLY: duplicate_rho_type,&
     103             :                                               qs_rho_update_rho
     104             :    USE qs_rho_types,                    ONLY: qs_rho_create,&
     105             :                                               qs_rho_get,&
     106             :                                               qs_rho_type
     107             :    USE qs_scf_diagonalization,          ONLY: diag_subspace_allocate
     108             :    USE qs_scf_lanczos,                  ONLY: krylov_space_allocate
     109             :    USE qs_scf_output,                   ONLY: qs_scf_initial_info
     110             :    USE qs_scf_types,                    ONLY: &
     111             :         block_davidson_diag_method_nr, block_krylov_diag_method_nr, diag_subspace_env_create, &
     112             :         filter_matrix_diag_method_nr, general_diag_method_nr, krylov_space_create, &
     113             :         ot_diag_method_nr, ot_method_nr, qs_scf_env_type, scf_env_create, smeagol_method_nr, &
     114             :         special_diag_method_nr
     115             :    USE qs_wf_history_methods,           ONLY: reorthogonalize_vectors,&
     116             :                                               wfi_extrapolate,&
     117             :                                               wfi_get_method_label,&
     118             :                                               wfi_update
     119             :    USE scf_control_types,               ONLY: scf_control_type
     120             :    USE xas_env_types,                   ONLY: xas_environment_type
     121             :    USE xas_restart,                     ONLY: xas_initialize_rho
     122             : #include "./base/base_uses.f90"
     123             : 
     124             :    IMPLICIT NONE
     125             : 
     126             :    PRIVATE
     127             : 
     128             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_initialization'
     129             : 
     130             :    PUBLIC:: qs_scf_env_initialize, qs_scf_env_init_basic
     131             : 
     132             : CONTAINS
     133             : 
     134             : ! **************************************************************************************************
     135             : !> \brief initializes input parameters if needed or restores values from
     136             : !>        previous runs to fill scf_env with the values required for scf
     137             : !> \param qs_env the qs_environment where to perform the scf procedure
     138             : !> \param scf_env ...
     139             : !> \param scf_control ...
     140             : !> \param scf_section ...
     141             : ! **************************************************************************************************
     142       19461 :    SUBROUTINE qs_scf_env_initialize(qs_env, scf_env, scf_control, scf_section)
     143             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     144             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     145             :       TYPE(scf_control_type), OPTIONAL, POINTER          :: scf_control
     146             :       TYPE(section_vals_type), OPTIONAL, POINTER         :: scf_section
     147             : 
     148             :       TYPE(dft_control_type), POINTER                    :: dft_control
     149             :       TYPE(scf_control_type), POINTER                    :: my_scf_control
     150             :       TYPE(section_vals_type), POINTER                   :: dft_section, input, my_scf_section
     151             : 
     152       19461 :       CALL get_qs_env(qs_env, input=input, dft_control=dft_control)
     153             : 
     154       19461 :       IF (PRESENT(scf_control)) THEN
     155          82 :          my_scf_control => scf_control
     156             :       ELSE
     157       19379 :          CALL get_qs_env(qs_env, scf_control=my_scf_control)
     158             :       END IF
     159             : 
     160       19461 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     161       19461 :       IF (PRESENT(scf_section)) THEN
     162          82 :          my_scf_section => scf_section
     163             :       ELSE
     164       19379 :          my_scf_section => section_vals_get_subs_vals(dft_section, "SCF")
     165             :       END IF
     166             : 
     167       19461 :       CALL qs_scf_ensure_scf_env(qs_env, scf_env)
     168             : 
     169       19461 :       CALL section_vals_val_get(my_scf_section, "CHOLESKY", i_val=scf_env%cholesky_method)
     170             : 
     171       19461 :       CALL qs_scf_ensure_mos(qs_env)
     172             : 
     173             :       ! set flags for diagonalization
     174             :       CALL qs_scf_ensure_diagonalization(scf_env, my_scf_section, qs_env, &
     175       19461 :                                          my_scf_control, qs_env%has_unit_metric)
     176             :       ! set parameters for mixing/DIIS during scf
     177       19461 :       CALL qs_scf_ensure_mixing(my_scf_control, my_scf_section, scf_env, dft_control)
     178             : 
     179       19461 :       CALL qs_scf_ensure_work_matrices(qs_env, scf_env)
     180             : 
     181       19461 :       CALL qs_scf_ensure_mixing_store(qs_env, scf_env)
     182             : 
     183             :       ! Initialize outer loop variables: handle CDFT and regular outer loop separately
     184       19461 :       IF (dft_control%qs_control%cdft) THEN
     185             :          CALL qs_scf_ensure_cdft_loop_vars(qs_env, scf_env, dft_control, &
     186         326 :                                            scf_control=my_scf_control)
     187             :       ELSE
     188       19135 :          CALL qs_scf_ensure_outer_loop_vars(scf_env, my_scf_control)
     189             :       END IF
     190             : 
     191       19461 :       CALL init_scf_run(scf_env, qs_env, my_scf_section, my_scf_control)
     192             : 
     193       19461 :    END SUBROUTINE qs_scf_env_initialize
     194             : 
     195             : ! **************************************************************************************************
     196             : !> \brief initializes input parameters if needed for non-scf calclulations using diagonalization
     197             : !> \param qs_env the qs_environment where to perform the scf procedure
     198             : !> \param scf_env ...
     199             : ! **************************************************************************************************
     200           2 :    SUBROUTINE qs_scf_env_init_basic(qs_env, scf_env)
     201             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     202             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     203             : 
     204             :       TYPE(dft_control_type), POINTER                    :: dft_control
     205             :       TYPE(scf_control_type), POINTER                    :: scf_control
     206             :       TYPE(section_vals_type), POINTER                   :: dft_section, input, scf_section
     207             : 
     208           2 :       CALL get_qs_env(qs_env, input=input, dft_control=dft_control)
     209             : 
     210           2 :       CALL get_qs_env(qs_env, scf_control=scf_control)
     211           2 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     212           2 :       scf_section => section_vals_get_subs_vals(dft_section, "SCF")
     213             : 
     214           2 :       CALL qs_scf_ensure_scf_env(qs_env, scf_env)
     215             : 
     216           2 :       CALL section_vals_val_get(scf_section, "CHOLESKY", i_val=scf_env%cholesky_method)
     217           2 :       scf_control%use_diag = .TRUE.
     218           2 :       scf_control%diagonalization%method = diag_standard
     219             : 
     220           2 :       CALL qs_scf_ensure_mos(qs_env)
     221             : 
     222             :       ! set flags for diagonalization
     223             :       CALL qs_scf_ensure_diagonalization(scf_env, scf_section, qs_env, &
     224           2 :                                          scf_control, qs_env%has_unit_metric)
     225           2 :       CALL qs_scf_ensure_work_matrices(qs_env, scf_env)
     226             : 
     227           2 :       CALL init_scf_run(scf_env, qs_env, scf_section, scf_control)
     228             : 
     229           2 :    END SUBROUTINE qs_scf_env_init_basic
     230             : 
     231             : ! **************************************************************************************************
     232             : !> \brief makes sure scf_env is allocated (might already be from before)
     233             : !>        in case it is present the g-space mixing storage is reset
     234             : !> \param qs_env ...
     235             : !> \param scf_env ...
     236             : ! **************************************************************************************************
     237       19463 :    SUBROUTINE qs_scf_ensure_scf_env(qs_env, scf_env)
     238             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     239             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     240             : 
     241       19463 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
     242             :       TYPE(qs_rho_type), POINTER                         :: rho
     243             : 
     244       19463 :       NULLIFY (rho_g)
     245             : 
     246       25672 :       IF (.NOT. ASSOCIATED(scf_env)) THEN ! i.e. for MD this is associated on the second step (it so seems)
     247        6209 :          ALLOCATE (scf_env)
     248        6209 :          CALL scf_env_create(scf_env)
     249             :       ELSE
     250             :          ! Reallocate mixing store, if the g space grid (cell) has changed
     251       13304 :          SELECT CASE (scf_env%mixing_method)
     252             :          CASE (kerker_mix, pulay_mix, broy_mix, multisec_mix)
     253       13254 :             IF (ASSOCIATED(scf_env%mixing_store)) THEN
     254             :                ! The current mixing_store data structure does not allow for an unique
     255             :                ! grid comparison, but the probability that the 1d lengths of the old and
     256             :                ! the new grid are accidentily equal is rather low
     257          50 :                CALL get_qs_env(qs_env, rho=rho)
     258          50 :                CALL qs_rho_get(rho, rho_g=rho_g)
     259          50 :                IF (ASSOCIATED(scf_env%mixing_store%rhoin)) THEN
     260          30 :                   IF (SIZE(rho_g(1)%pw_grid%gsq) /= SIZE(scf_env%mixing_store%rhoin(1)%cc)) THEN
     261           0 :                      CALL mixing_storage_release(scf_env%mixing_store)
     262           0 :                      DEALLOCATE (scf_env%mixing_store)
     263             :                   END IF
     264             :                END IF
     265             :             END IF
     266             :          END SELECT
     267             :       END IF
     268             : 
     269       19463 :    END SUBROUTINE qs_scf_ensure_scf_env
     270             : 
     271             : ! **************************************************************************************************
     272             : !> \brief performs allocation of outer SCF variables
     273             : !> \param scf_env the SCF environment which contains the outer SCF variables
     274             : !> \param scf_control control settings for the outer SCF loop
     275             : !> \param nvar (optional) set number of outer SCF variables externally if CDFT SCF is active
     276             : ! **************************************************************************************************
     277       19461 :    SUBROUTINE qs_scf_ensure_outer_loop_vars(scf_env, scf_control, nvar)
     278             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     279             :       TYPE(scf_control_type), POINTER                    :: scf_control
     280             :       INTEGER, OPTIONAL                                  :: nvar
     281             : 
     282             :       INTEGER                                            :: nhistory, nvariables
     283             : 
     284       19461 :       IF (scf_control%outer_scf%have_scf) THEN
     285        3843 :          nhistory = scf_control%outer_scf%max_scf + 1
     286        3843 :          IF (PRESENT(nvar)) THEN
     287         326 :             IF (nvar > 0) THEN
     288             :                nvariables = nvar
     289             :             ELSE
     290           0 :                nvariables = outer_loop_variables_count(scf_control)
     291             :             END IF
     292             :          ELSE
     293        3517 :             nvariables = outer_loop_variables_count(scf_control)
     294             :          END IF
     295       15372 :          ALLOCATE (scf_env%outer_scf%variables(nvariables, nhistory))
     296       11529 :          ALLOCATE (scf_env%outer_scf%count(nhistory))
     297       72553 :          scf_env%outer_scf%count = 0
     298       11529 :          ALLOCATE (scf_env%outer_scf%gradient(nvariables, nhistory))
     299       11529 :          ALLOCATE (scf_env%outer_scf%energy(nhistory))
     300             :       END IF
     301             : 
     302       19461 :    END SUBROUTINE qs_scf_ensure_outer_loop_vars
     303             : 
     304             : ! **************************************************************************************************
     305             : !> \brief performs allocation of CDFT SCF variables
     306             : !> \param qs_env the qs_env where to perform the allocation
     307             : !> \param scf_env the currently active scf_env
     308             : !> \param dft_control the dft_control that holds the cdft_control type
     309             : !> \param scf_control the currently active scf_control
     310             : ! **************************************************************************************************
     311         326 :    SUBROUTINE qs_scf_ensure_cdft_loop_vars(qs_env, scf_env, dft_control, scf_control)
     312             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     313             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     314             :       TYPE(dft_control_type), POINTER                    :: dft_control
     315             :       TYPE(scf_control_type), POINTER                    :: scf_control
     316             : 
     317             :       INTEGER                                            :: nhistory, nvariables
     318             :       LOGICAL                                            :: do_kpoints
     319         326 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: gradient_history, outer_scf_history, &
     320         326 :                                                             variable_history
     321             : 
     322         326 :       NULLIFY (outer_scf_history, gradient_history, variable_history)
     323         326 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     324             :       ! Test kpoints
     325         326 :       IF (do_kpoints) &
     326           0 :          CPABORT("CDFT calculation not possible with kpoints")
     327             :       ! Check that OUTER_SCF section in DFT&SCF is active
     328             :       ! This section must always be active to facilitate
     329             :       ! switching of the CDFT and SCF control parameters in outer_loop_switch
     330         326 :       IF (.NOT. scf_control%outer_scf%have_scf) &
     331           0 :          CPABORT("Section SCF&OUTER_SCF must be active for CDFT calculations.")
     332             :       ! Initialize CDFT and outer_loop variables (constraint settings active in scf_control)
     333         326 :       IF (dft_control%qs_control%cdft_control%constraint_control%have_scf) THEN
     334         326 :          nhistory = dft_control%qs_control%cdft_control%constraint_control%max_scf + 1
     335         326 :          IF (scf_control%outer_scf%type /= outer_scf_none) THEN
     336             :             nvariables = outer_loop_variables_count(scf_control, &
     337          62 :                                                     dft_control%qs_control%cdft_control)
     338             :          ELSE
     339             :             ! First iteration: scf_control has not yet been updated
     340         264 :             nvariables = SIZE(dft_control%qs_control%cdft_control%target)
     341             :          END IF
     342        1304 :          ALLOCATE (dft_control%qs_control%cdft_control%constraint%variables(nvariables, nhistory))
     343         978 :          ALLOCATE (dft_control%qs_control%cdft_control%constraint%count(nhistory))
     344        2246 :          dft_control%qs_control%cdft_control%constraint%count = 0
     345         978 :          ALLOCATE (dft_control%qs_control%cdft_control%constraint%gradient(nvariables, nhistory))
     346         978 :          ALLOCATE (dft_control%qs_control%cdft_control%constraint%energy(nhistory))
     347         326 :          CALL qs_scf_ensure_outer_loop_vars(scf_env, scf_control, nvariables)
     348             :       END IF
     349             :       ! Executed only on first call (OT settings active in scf_control)
     350             :       ! Save OT settings and constraint initial values in CDFT control
     351             :       ! Then switch to constraint outer_scf settings for proper initialization of history
     352         326 :       IF (scf_control%outer_scf%have_scf) THEN
     353         326 :          IF (scf_control%outer_scf%type == outer_scf_none) THEN
     354         264 :             dft_control%qs_control%cdft_control%ot_control%have_scf = .TRUE.
     355         264 :             dft_control%qs_control%cdft_control%ot_control%max_scf = scf_control%outer_scf%max_scf
     356         264 :             dft_control%qs_control%cdft_control%ot_control%eps_scf = scf_control%outer_scf%eps_scf
     357         264 :             dft_control%qs_control%cdft_control%ot_control%step_size = scf_control%outer_scf%step_size
     358         264 :             dft_control%qs_control%cdft_control%ot_control%type = scf_control%outer_scf%type
     359         264 :             dft_control%qs_control%cdft_control%ot_control%optimizer = scf_control%outer_scf%optimizer
     360         264 :             dft_control%qs_control%cdft_control%ot_control%diis_buffer_length = scf_control%outer_scf%diis_buffer_length
     361         264 :             dft_control%qs_control%cdft_control%ot_control%bisect_trust_count = scf_control%outer_scf%bisect_trust_count
     362             :             CALL cdft_opt_type_copy(dft_control%qs_control%cdft_control%ot_control%cdft_opt_control, &
     363         264 :                                     scf_control%outer_scf%cdft_opt_control)
     364             :             ! In case constraint and OT extrapolation orders are different, make sure to use former
     365         264 :             nvariables = SIZE(dft_control%qs_control%cdft_control%target)
     366             :             IF (scf_control%outer_scf%extrapolation_order /= &
     367             :                 dft_control%qs_control%cdft_control%constraint_control%extrapolation_order &
     368         264 :                 .OR. nvariables /= 1) THEN
     369         256 :                DEALLOCATE (qs_env%outer_scf_history)
     370         256 :                DEALLOCATE (qs_env%gradient_history)
     371         256 :                DEALLOCATE (qs_env%variable_history)
     372         256 :                nhistory = dft_control%qs_control%cdft_control%constraint_control%extrapolation_order
     373        1024 :                ALLOCATE (outer_scf_history(nvariables, nhistory))
     374         768 :                ALLOCATE (gradient_history(nvariables, 2))
     375        1324 :                gradient_history = 0.0_dp
     376         512 :                ALLOCATE (variable_history(nvariables, 2))
     377        1324 :                variable_history = 0.0_dp
     378             :                CALL set_qs_env(qs_env, outer_scf_history=outer_scf_history, &
     379         256 :                                gradient_history=gradient_history, variable_history=variable_history)
     380             :             END IF
     381         264 :             CALL outer_loop_switch(scf_env, scf_control, dft_control%qs_control%cdft_control, ot2cdft)
     382             :          END IF
     383             :       END IF
     384             : 
     385         326 :    END SUBROUTINE qs_scf_ensure_cdft_loop_vars
     386             : 
     387             : ! **************************************************************************************************
     388             : !> \brief performs allocation of the mixing storage
     389             : !> \param qs_env ...
     390             : !> \param scf_env ...
     391             : ! **************************************************************************************************
     392       19461 :    SUBROUTINE qs_scf_ensure_mixing_store(qs_env, scf_env)
     393             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     394             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     395             : 
     396             :       TYPE(dft_control_type), POINTER                    :: dft_control
     397             : 
     398       19461 :       NULLIFY (dft_control)
     399       19461 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
     400             : 
     401       19461 :       IF (scf_env%mixing_method > 0) THEN
     402             :          CALL mixing_allocate(qs_env, scf_env%mixing_method, scf_env%p_mix_new, &
     403             :                               scf_env%p_delta, dft_control%nspins, &
     404       13914 :                               scf_env%mixing_store)
     405             :       ELSE
     406        5547 :          NULLIFY (scf_env%p_mix_new)
     407             :       END IF
     408             : 
     409       19461 :    END SUBROUTINE qs_scf_ensure_mixing_store
     410             : 
     411             : ! **************************************************************************************************
     412             : !> \brief Performs allocation of the SCF work matrices
     413             : !>        In case of kpoints we probably don't need most of these matrices,
     414             : !>        maybe we have to initialize some matrices in the fm_pool in kpoints
     415             : !> \param qs_env ...
     416             : !> \param scf_env ...
     417             : ! **************************************************************************************************
     418       58389 :    SUBROUTINE qs_scf_ensure_work_matrices(qs_env, scf_env)
     419             : 
     420             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     421             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     422             : 
     423             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_scf_ensure_work_matrices'
     424             : 
     425             :       INTEGER                                            :: handle, is, nao, nrow_block, nw
     426             :       LOGICAL                                            :: do_kpoints
     427       19463 :       TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER     :: ao_mo_fm_pools
     428             :       TYPE(cp_fm_struct_type), POINTER                   :: ao_ao_fmstruct, ao_mo_fmstruct
     429       19463 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s
     430             :       TYPE(dbcsr_type), POINTER                          :: ref_matrix
     431             :       TYPE(dft_control_type), POINTER                    :: dft_control
     432       19463 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     433             :       TYPE(scf_control_type), POINTER                    :: scf_control
     434             : 
     435       19463 :       CALL timeset(routineN, handle)
     436             : 
     437       19463 :       NULLIFY (ao_mo_fm_pools, ao_mo_fmstruct, ao_ao_fmstruct, dft_control, matrix_s, mos)
     438             : 
     439             :       CALL get_qs_env(qs_env=qs_env, &
     440             :                       dft_control=dft_control, &
     441             :                       matrix_s_kp=matrix_s, &
     442             :                       mos=mos, &
     443             :                       scf_control=scf_control, &
     444       19463 :                       do_kpoints=do_kpoints)
     445       19463 :       CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
     446             : 
     447             :       ! create an ao_ao parallel matrix structure
     448       19463 :       ao_mo_fmstruct => fm_pool_get_el_struct(ao_mo_fm_pools(1)%pool)
     449       19463 :       CALL cp_fm_struct_get(ao_mo_fmstruct, nrow_block=nrow_block)
     450       19463 :       CALL get_mo_set(mos(1), nao=nao)
     451             :       CALL cp_fm_struct_create(fmstruct=ao_ao_fmstruct, &
     452             :                                nrow_block=nrow_block, &
     453             :                                ncol_block=nrow_block, &
     454             :                                nrow_global=nao, &
     455             :                                ncol_global=nao, &
     456       19463 :                                template_fmstruct=ao_mo_fmstruct)
     457             : 
     458       19463 :       IF ((scf_env%method /= ot_method_nr) .AND. &
     459             :           (scf_env%method /= block_davidson_diag_method_nr)) THEN
     460       13900 :          IF (.NOT. ASSOCIATED(scf_env%scf_work1)) THEN
     461       12452 :             nw = dft_control%nspins
     462       12452 :             IF (do_kpoints) nw = 4
     463       53166 :             ALLOCATE (scf_env%scf_work1(nw))
     464       28262 :             DO is = 1, SIZE(scf_env%scf_work1)
     465             :                CALL cp_fm_create(scf_env%scf_work1(is), &
     466             :                                  matrix_struct=ao_ao_fmstruct, &
     467       28262 :                                  name="SCF-WORK_MATRIX-1-"//TRIM(ADJUSTL(cp_to_string(is))))
     468             :             END DO
     469             :          END IF
     470             :          IF ((.NOT. ASSOCIATED(scf_env%ortho)) .AND. &
     471       13900 :              (scf_env%method /= ot_diag_method_nr) .AND. &
     472             :              (scf_env%method /= special_diag_method_nr)) THEN
     473             :             ! Initialize fm matrix to store the Cholesky decomposition
     474        9788 :             ALLOCATE (scf_env%ortho)
     475             :             CALL cp_fm_create(scf_env%ortho, &
     476             :                               matrix_struct=ao_ao_fmstruct, &
     477        9788 :                               name="SCF-ORTHO_MATRIX")
     478             :             ! Initialize dbcsr matrix to store the Cholesky decomposition
     479        9788 :             IF (scf_env%cholesky_method == cholesky_dbcsr) THEN
     480          58 :                ref_matrix => matrix_s(1, 1)%matrix
     481          58 :                CALL dbcsr_init_p(scf_env%ortho_dbcsr)
     482             :                CALL dbcsr_create(scf_env%ortho_dbcsr, template=ref_matrix, &
     483          58 :                                  matrix_type=dbcsr_type_no_symmetry)
     484          58 :                CALL dbcsr_init_p(scf_env%buf1_dbcsr)
     485             :                CALL dbcsr_create(scf_env%buf1_dbcsr, template=ref_matrix, &
     486          58 :                                  matrix_type=dbcsr_type_no_symmetry)
     487          58 :                CALL dbcsr_init_p(scf_env%buf2_dbcsr)
     488             :                CALL dbcsr_create(scf_env%buf2_dbcsr, template=ref_matrix, &
     489          58 :                                  matrix_type=dbcsr_type_no_symmetry)
     490        9730 :             ELSE IF (scf_env%cholesky_method == cholesky_inverse .OR. &
     491             :                      (scf_control%level_shift /= 0.0_dp .AND. &
     492             :                       scf_env%cholesky_method == cholesky_off)) THEN
     493          44 :                ALLOCATE (scf_env%ortho_m1)
     494             :                CALL cp_fm_create(scf_env%ortho_m1, &
     495             :                                  matrix_struct=ao_ao_fmstruct, &
     496          44 :                                  name="SCF-ORTHO_MATRIX-1")
     497             :             END IF
     498             :          END IF
     499       13900 :          IF (.NOT. ASSOCIATED(scf_env%scf_work2)) THEN
     500       12452 :             ALLOCATE (scf_env%scf_work2)
     501             :             CALL cp_fm_create(scf_env%scf_work2, &
     502             :                               matrix_struct=ao_ao_fmstruct, &
     503       12452 :                               name="SCF-WORK_MATRIX-2")
     504             :          END IF
     505             :       END IF
     506             : 
     507       19463 :       IF (dft_control%dft_plus_u) THEN
     508          80 :          IF (dft_control%plus_u_method_id == plus_u_lowdin) THEN
     509           8 :             IF (.NOT. ASSOCIATED(scf_env%scf_work2)) THEN
     510           4 :                ALLOCATE (scf_env%scf_work2)
     511             :                CALL cp_fm_create(scf_env%scf_work2, &
     512             :                                  matrix_struct=ao_ao_fmstruct, &
     513           4 :                                  name="SCF-WORK_MATRIX-2")
     514             :             END IF
     515           8 :             IF (.NOT. ASSOCIATED(scf_env%s_half)) THEN
     516           8 :                ALLOCATE (scf_env%s_half)
     517             :                CALL cp_fm_create(scf_env%s_half, &
     518             :                                  matrix_struct=ao_ao_fmstruct, &
     519           8 :                                  name="S**(1/2) MATRIX")
     520             :             END IF
     521             :          END IF
     522             :       END IF
     523             : 
     524       19463 :       IF (do_kpoints) THEN
     525         914 :          IF (.NOT. ASSOCIATED(scf_env%scf_work1)) THEN
     526           0 :             nw = 4
     527           0 :             ALLOCATE (scf_env%scf_work1(nw))
     528           0 :             DO is = 1, SIZE(scf_env%scf_work1)
     529             :                CALL cp_fm_create(scf_env%scf_work1(is), &
     530             :                                  matrix_struct=ao_ao_fmstruct, &
     531           0 :                                  name="SCF-WORK_MATRIX-1-"//TRIM(ADJUSTL(cp_to_string(is))))
     532             :             END DO
     533             :          END IF
     534             :       END IF
     535             : 
     536       19463 :       CALL cp_fm_struct_release(ao_ao_fmstruct)
     537             : 
     538       19463 :       CALL timestop(handle)
     539             : 
     540       19463 :    END SUBROUTINE qs_scf_ensure_work_matrices
     541             : 
     542             : ! **************************************************************************************************
     543             : !> \brief performs allocation of the MO matrices
     544             : !> \param qs_env ...
     545             : ! **************************************************************************************************
     546       19463 :    SUBROUTINE qs_scf_ensure_mos(qs_env)
     547             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     548             : 
     549             :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_scf_ensure_mos'
     550             : 
     551             :       INTEGER                                            :: handle, ic, ik, ikk, ispin, nmo, nmo_mat
     552       19463 :       TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER     :: ao_mo_fm_pools
     553             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff, mo_coeff_last
     554       19463 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mo_derivs
     555       19463 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s
     556             :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_b
     557             :       TYPE(dft_control_type), POINTER                    :: dft_control
     558             :       TYPE(kpoint_type), POINTER                         :: kpoints
     559       19463 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos, mos_last_converged
     560       19463 :       TYPE(mo_set_type), DIMENSION(:, :), POINTER        :: mos_k
     561             :       TYPE(xas_environment_type), POINTER                :: xas_env
     562             : 
     563       19463 :       CALL timeset(routineN, handle)
     564             : 
     565       19463 :       NULLIFY (ao_mo_fm_pools, dft_control, mos, xas_env, matrix_s, mos_last_converged, mo_coeff_last)
     566             : 
     567             :       CALL get_qs_env(qs_env=qs_env, &
     568             :                       dft_control=dft_control, &
     569             :                       mos=mos, &
     570             :                       matrix_s_kp=matrix_s, &
     571       19463 :                       xas_env=xas_env)
     572       19463 :       CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
     573       19463 :       IF (dft_control%switch_surf_dip) THEN
     574           2 :          CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
     575             :       END IF
     576             : 
     577       19463 :       nmo_mat = dft_control%nspins
     578       19463 :       IF (dft_control%restricted) nmo_mat = 1 ! right now, there might be more mos than needed derivs
     579             : 
     580             : !   *** finish initialization of the MOs ***
     581       19463 :       CPASSERT(ASSOCIATED(mos))
     582       41442 :       DO ispin = 1, SIZE(mos)
     583       21979 :          CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, mo_coeff_b=mo_coeff_b)
     584       21979 :          IF (.NOT. ASSOCIATED(mo_coeff)) THEN
     585             :             CALL init_mo_set(mos(ispin), &
     586             :                              fm_pool=ao_mo_fm_pools(ispin)%pool, &
     587        7664 :                              name="qs_env%mo"//TRIM(ADJUSTL(cp_to_string(ispin))))
     588             :          END IF
     589       41442 :          IF (.NOT. ASSOCIATED(mo_coeff_b)) THEN
     590        7664 :             CALL cp_fm_get_info(mos(ispin)%mo_coeff, ncol_global=nmo)
     591        7664 :             CALL dbcsr_init_p(mos(ispin)%mo_coeff_b)
     592             :             CALL cp_dbcsr_m_by_n_from_row_template(mos(ispin)%mo_coeff_b, template=matrix_s(1, 1)%matrix, n=nmo, &
     593        7664 :                                                    sym=dbcsr_type_no_symmetry)
     594             :          END IF
     595             :       END DO
     596             : !   *** get the mo_derivs OK if needed ***
     597       19463 :       IF (qs_env%requires_mo_derivs) THEN
     598        5553 :          CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
     599        5553 :          IF (.NOT. ASSOCIATED(mo_derivs)) THEN
     600        8417 :             ALLOCATE (mo_derivs(nmo_mat))
     601        4503 :             DO ispin = 1, nmo_mat
     602        2546 :                CALL get_mo_set(mos(ispin), mo_coeff_b=mo_coeff_b)
     603        2546 :                NULLIFY (mo_derivs(ispin)%matrix)
     604        2546 :                CALL dbcsr_init_p(mo_derivs(ispin)%matrix)
     605             :                CALL dbcsr_create(mo_derivs(ispin)%matrix, template=mo_coeff_b, &
     606        4503 :                                  name="mo_derivs", matrix_type=dbcsr_type_no_symmetry)
     607             :             END DO
     608        1957 :             CALL set_qs_env(qs_env, mo_derivs=mo_derivs)
     609             :          END IF
     610             : 
     611             :       ELSE
     612             :          ! nothing should be done
     613             :       END IF
     614             : 
     615             : !   *** finish initialization of the MOs for ADMM and derivs if needed ***
     616       19463 :       IF (dft_control%do_admm) THEN
     617         790 :          IF (dft_control%restricted) CPABORT("ROKS with ADMM is not implemented")
     618             :       END IF
     619             : 
     620             : ! *** finish initialization of mos_last_converged *** [SGh]
     621       19463 :       IF (dft_control%switch_surf_dip) THEN
     622           2 :          CPASSERT(ASSOCIATED(mos_last_converged))
     623           4 :          DO ispin = 1, SIZE(mos_last_converged)
     624           2 :             CALL get_mo_set(mos_last_converged(ispin), mo_coeff=mo_coeff_last)
     625           4 :             IF (.NOT. ASSOCIATED(mo_coeff_last)) THEN
     626             :                CALL init_mo_set(mos_last_converged(ispin), &
     627             :                                 fm_ref=mos(ispin)%mo_coeff, &
     628           2 :                                 name="qs_env%mos_last_converged"//TRIM(ADJUSTL(cp_to_string(ispin))))
     629             :             END IF
     630             :          END DO
     631             :       END IF
     632             :       ! kpoints: we have to initialize all the k-point MOs
     633       19463 :       CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
     634       19463 :       IF (kpoints%nkp /= 0) THEN
     635             :          ! check for some incompatible options
     636         914 :          IF (qs_env%requires_mo_derivs) THEN
     637           2 :             CPWARN("MO derivative methods flag has been switched off for kpoint calculation")
     638             :             ! we switch it off to make band structure calculations
     639             :             ! possible for OT gamma point calculations
     640           2 :             qs_env%requires_mo_derivs = .FALSE.
     641             :          END IF
     642         914 :          IF (dft_control%do_xas_calculation) &
     643           0 :             CPABORT("No XAS implemented with kpoints")
     644        3660 :          DO ik = 1, SIZE(kpoints%kp_env)
     645        2746 :             CALL mpools_get(kpoints%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
     646        2746 :             mos_k => kpoints%kp_env(ik)%kpoint_env%mos
     647        2746 :             ikk = kpoints%kp_range(1) + ik - 1
     648        2746 :             CPASSERT(ASSOCIATED(mos_k))
     649        6858 :             DO ispin = 1, SIZE(mos_k, 2)
     650       12326 :                DO ic = 1, SIZE(mos_k, 1)
     651        6382 :                   CALL get_mo_set(mos_k(ic, ispin), mo_coeff=mo_coeff, mo_coeff_b=mo_coeff_b)
     652        6382 :                   IF (.NOT. ASSOCIATED(mo_coeff)) THEN
     653             :                      CALL init_mo_set(mos_k(ic, ispin), &
     654             :                                       fm_pool=ao_mo_fm_pools(ispin)%pool, &
     655             :                                       name="kpoints_"//TRIM(ADJUSTL(cp_to_string(ikk)))// &
     656        2626 :                                       "%mo"//TRIM(ADJUSTL(cp_to_string(ispin))))
     657             :                   END IF
     658             :                   ! no sparse matrix representation of kpoint MO vectors
     659        9580 :                   CPASSERT(.NOT. ASSOCIATED(mo_coeff_b))
     660             :                END DO
     661             :             END DO
     662             :          END DO
     663             :       END IF
     664             : 
     665       19463 :       CALL timestop(handle)
     666             : 
     667       19463 :    END SUBROUTINE qs_scf_ensure_mos
     668             : 
     669             : ! **************************************************************************************************
     670             : !> \brief sets flag for mixing/DIIS during scf
     671             : !> \param scf_control ...
     672             : !> \param scf_section ...
     673             : !> \param scf_env ...
     674             : !> \param dft_control ...
     675             : ! **************************************************************************************************
     676       19461 :    SUBROUTINE qs_scf_ensure_mixing(scf_control, scf_section, scf_env, dft_control)
     677             :       TYPE(scf_control_type), POINTER                    :: scf_control
     678             :       TYPE(section_vals_type), POINTER                   :: scf_section
     679             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     680             :       TYPE(dft_control_type), POINTER                    :: dft_control
     681             : 
     682             :       TYPE(section_vals_type), POINTER                   :: mixing_section
     683             : 
     684       19461 :       SELECT CASE (scf_control%mixing_method)
     685             :       CASE (no_mix)
     686           0 :          scf_env%mixing_method = no_mixing_nr
     687           0 :          scf_env%p_mix_alpha = 1.0_dp
     688             :       CASE (direct_p_mix, kerker_mix, pulay_mix, broy_mix, multisec_mix)
     689       19461 :          scf_env%mixing_method = scf_control%mixing_method
     690       19461 :          mixing_section => section_vals_get_subs_vals(scf_section, "MIXING")
     691       19461 :          IF (.NOT. ASSOCIATED(scf_env%mixing_store)) THEN
     692       24828 :             ALLOCATE (scf_env%mixing_store)
     693             :             CALL mixing_storage_create(scf_env%mixing_store, mixing_section, scf_env%mixing_method, &
     694        6207 :                                        dft_control%qs_control%cutoff)
     695             :          END IF
     696             :       CASE DEFAULT
     697       19461 :          CPABORT("Unknown mixing method")
     698             :       END SELECT
     699             : 
     700             :       ! Disable DIIS for OT and g-space density mixing methods
     701       19461 :       IF (scf_env%method == ot_method_nr) THEN
     702             :          ! No mixing is used with OT
     703        5547 :          scf_env%mixing_method = no_mixing_nr
     704        5547 :          scf_env%p_mix_alpha = 1.0_dp
     705        5547 :          scf_env%skip_diis = .TRUE.
     706             :       END IF
     707             : 
     708       19461 :       IF (scf_control%use_diag .AND. scf_env%mixing_method == no_mixing_nr) THEN
     709           0 :          CPABORT("Diagonalization procedures without mixing are not recommendable")
     710             :       END IF
     711             : 
     712       19461 :       IF (scf_env%mixing_method > direct_mixing_nr) THEN
     713         246 :          scf_env%skip_diis = .TRUE.
     714         246 :          scf_env%p_mix_alpha = scf_env%mixing_store%alpha
     715         246 :          IF (scf_env%mixing_store%beta == 0.0_dp) THEN
     716           0 :             CPABORT("Mixing employing the Kerker damping factor needs BETA /= 0.0")
     717             :          END IF
     718             :       END IF
     719             : 
     720       19461 :       IF (scf_env%mixing_method == direct_mixing_nr) THEN
     721       13668 :          scf_env%p_mix_alpha = scf_env%mixing_store%alpha
     722       13668 :          IF (scf_control%eps_diis < scf_control%eps_scf) THEN
     723          42 :             scf_env%skip_diis = .TRUE.
     724          42 :             CPWARN("the DIIS scheme is disabled, since EPS_DIIS < EPS_SCF")
     725             :          END IF
     726             :       END IF
     727             : 
     728       19461 :    END SUBROUTINE qs_scf_ensure_mixing
     729             : 
     730             : ! **************************************************************************************************
     731             : !> \brief sets flags for diagonalization and ensure that everything is
     732             : !>        allocated
     733             : !> \param scf_env ...
     734             : !> \param scf_section ...
     735             : !> \param qs_env ...
     736             : !> \param scf_control ...
     737             : !> \param has_unit_metric ...
     738             : ! **************************************************************************************************
     739       19463 :    SUBROUTINE qs_scf_ensure_diagonalization(scf_env, scf_section, qs_env, &
     740             :                                             scf_control, has_unit_metric)
     741             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     742             :       TYPE(section_vals_type), POINTER                   :: scf_section
     743             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     744             :       TYPE(scf_control_type), POINTER                    :: scf_control
     745             :       LOGICAL                                            :: has_unit_metric
     746             : 
     747             :       INTEGER                                            :: ispin, nao, nmo
     748             :       LOGICAL                                            :: do_kpoints, need_coeff_b, not_se_or_tb
     749             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     750             :       TYPE(dft_control_type), POINTER                    :: dft_control
     751       19463 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     752             : 
     753       19463 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints, dft_control=dft_control, mos=mos)
     754             :       not_se_or_tb = .NOT. (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
     755       19463 :                             dft_control%qs_control%semi_empirical)
     756       19463 :       need_coeff_b = .FALSE.
     757       19463 :       scf_env%needs_ortho = .FALSE.
     758             : 
     759       19463 :       IF (dft_control%smeagol_control%smeagol_enabled .AND. &
     760             :           dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
     761           0 :          scf_env%method = smeagol_method_nr
     762           0 :          scf_env%skip_diis = .TRUE.
     763           0 :          scf_control%use_diag = .FALSE.
     764             : 
     765           0 :          IF (.NOT. do_kpoints) THEN
     766           0 :             CPABORT("SMEAGOL requires kpoint calculations")
     767             :          END IF
     768           0 :          CPWARN_IF(scf_control%use_ot, "OT is irrelevant to NEGF method")
     769             :       END IF
     770             : 
     771       19463 :       IF (scf_control%use_diag) THEN
     772             :          ! sanity check whether combinations are allowed
     773       13916 :          IF (dft_control%restricted) &
     774           0 :             CPABORT("OT only for restricted (ROKS)")
     775       13948 :          SELECT CASE (scf_control%diagonalization%method)
     776             :          CASE (diag_ot, diag_block_krylov, diag_block_davidson)
     777          32 :             IF (.NOT. not_se_or_tb) &
     778       13916 :                CPABORT("TB and SE not possible with OT diagonalization")
     779             :          END SELECT
     780       27790 :          SELECT CASE (scf_control%diagonalization%method)
     781             :             ! Diagonalization: additional check whether we are in an orthonormal basis
     782             :          CASE (diag_standard)
     783       13874 :             scf_env%method = general_diag_method_nr
     784       13874 :             scf_env%needs_ortho = (.NOT. has_unit_metric) .AND. (.NOT. do_kpoints)
     785       13874 :             IF (has_unit_metric) THEN
     786        2656 :                scf_env%method = special_diag_method_nr
     787             :             END IF
     788             :             ! OT Diagonalization: not possible with ROKS
     789             :          CASE (diag_ot)
     790           8 :             IF (dft_control%roks) &
     791           0 :                CPABORT("ROKS with OT diagonalization not possible")
     792           8 :             IF (do_kpoints) &
     793           0 :                CPABORT("OT diagonalization not possible with kpoint calculations")
     794           8 :             scf_env%method = ot_diag_method_nr
     795           8 :             need_coeff_b = .TRUE.
     796             :             ! Block Krylov diagonlization: not possible with ROKS,
     797             :             ! allocation of additional matrices is needed
     798             :          CASE (diag_block_krylov)
     799           8 :             IF (dft_control%roks) &
     800           0 :                CPABORT("ROKS with block PF diagonalization not possible")
     801           8 :             IF (do_kpoints) &
     802           0 :                CPABORT("Block Krylov diagonalization not possible with kpoint calculations")
     803           8 :             scf_env%method = block_krylov_diag_method_nr
     804           8 :             scf_env%needs_ortho = .TRUE.
     805           8 :             IF (.NOT. ASSOCIATED(scf_env%krylov_space)) &
     806           4 :                CALL krylov_space_create(scf_env%krylov_space, scf_section)
     807           8 :             CALL krylov_space_allocate(scf_env%krylov_space, scf_control, mos)
     808             :             ! Block davidson diagonlization: allocation of additional matrices is needed
     809             :          CASE (diag_block_davidson)
     810          16 :             IF (do_kpoints) &
     811           0 :                CPABORT("Block Davidson diagonalization not possible with kpoint calculations")
     812          16 :             scf_env%method = block_davidson_diag_method_nr
     813          16 :             IF (.NOT. ASSOCIATED(scf_env%block_davidson_env)) &
     814             :                CALL block_davidson_env_create(scf_env%block_davidson_env, dft_control%nspins, &
     815          12 :                                               scf_section)
     816          34 :             DO ispin = 1, dft_control%nspins
     817          18 :                CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nao=nao, nmo=nmo)
     818          34 :                CALL block_davidson_allocate(scf_env%block_davidson_env(ispin), mo_coeff, nao, nmo)
     819             :             END DO
     820          10 :             need_coeff_b = .TRUE.
     821             :             ! Filter matrix diagonalisation method
     822             :          CASE (diag_filter_matrix)
     823          10 :             scf_env%method = filter_matrix_diag_method_nr
     824          10 :             IF (.NOT. fb_env_has_data(scf_env%filter_matrix_env)) THEN
     825          10 :                CALL fb_env_create(scf_env%filter_matrix_env)
     826             :             END IF
     827          10 :             CALL fb_env_read_input(scf_env%filter_matrix_env, scf_section)
     828          10 :             CALL fb_env_build_rcut_auto(scf_env%filter_matrix_env, qs_env)
     829          10 :             CALL fb_env_write_info(scf_env%filter_matrix_env, qs_env, scf_section)
     830          10 :             CALL fb_distribution_build(scf_env%filter_matrix_env, qs_env, scf_section)
     831          10 :             CALL fb_env_build_atomic_halos(scf_env%filter_matrix_env, qs_env, scf_section)
     832             :          CASE DEFAULT
     833       13916 :             CPABORT("Unknown diagonalization method")
     834             :          END SELECT
     835             :          ! Check if subspace diagonlization is requested: allocation of additional matrices is needed
     836       13916 :          IF (scf_control%do_diag_sub) THEN
     837           2 :             scf_env%needs_ortho = .TRUE.
     838           2 :             IF (.NOT. ASSOCIATED(scf_env%subspace_env)) &
     839             :                CALL diag_subspace_env_create(scf_env%subspace_env, scf_section, &
     840           2 :                                              dft_control%qs_control%cutoff)
     841           2 :             CALL diag_subspace_allocate(scf_env%subspace_env, qs_env, mos)
     842           2 :             IF (do_kpoints) &
     843           0 :                CPABORT("No subspace diagonlization with kpoint calculation")
     844             :          END IF
     845             :          ! OT: check if OT is used instead of diagonlization. Not possible with added MOS at the moment
     846        5547 :       ELSEIF (scf_control%use_ot) THEN
     847        5547 :          scf_env%method = ot_method_nr
     848        5547 :          need_coeff_b = .TRUE.
     849       16641 :          IF (SUM(ABS(scf_control%added_mos)) > 0) &
     850           0 :             CPABORT("OT with ADDED_MOS/=0 not implemented")
     851        5547 :          IF (dft_control%restricted .AND. dft_control%nspins .NE. 2) &
     852           0 :             CPABORT("nspin must be 2 for restricted (ROKS)")
     853        5547 :          IF (do_kpoints) &
     854           0 :             CPABORT("OT not possible with kpoint calculations")
     855           0 :       ELSEIF (scf_env%method /= smeagol_method_nr) THEN
     856           0 :          CPABORT("OT or DIAGONALIZATION have to be set")
     857             :       END IF
     858       41442 :       DO ispin = 1, dft_control%nspins
     859       41442 :          mos(ispin)%use_mo_coeff_b = need_coeff_b
     860             :       END DO
     861             : 
     862       19463 :    END SUBROUTINE qs_scf_ensure_diagonalization
     863             : 
     864             : ! **************************************************************************************************
     865             : !> \brief performs those initialisations that need to be done only once
     866             : !>       (e.g. that only depend on the atomic positions)
     867             : !>       this will be called in scf
     868             : !> \param scf_env ...
     869             : !> \param qs_env ...
     870             : !> \param scf_section ...
     871             : !> \param scf_control ...
     872             : !> \par History
     873             : !>      03.2006 created [Joost VandeVondele]
     874             : ! **************************************************************************************************
     875       19463 :    SUBROUTINE init_scf_run(scf_env, qs_env, scf_section, scf_control)
     876             : 
     877             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     878             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     879             :       TYPE(section_vals_type), POINTER                   :: scf_section
     880             :       TYPE(scf_control_type), POINTER                    :: scf_control
     881             : 
     882             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'init_scf_run'
     883             : 
     884             :       INTEGER                                            :: after, handle, homo, ii, ikind, ispin, &
     885             :                                                             iw, nao, ndep, needed_evals, nmo, &
     886             :                                                             output_unit
     887             :       LOGICAL                                            :: dft_plus_u_atom, do_kpoints, &
     888             :                                                             init_u_ramping_each_scf, omit_headers, &
     889             :                                                             s_minus_half_available
     890             :       REAL(KIND=dp)                                      :: u_ramping
     891       19463 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: evals
     892       19463 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eigenvalues
     893             :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     894             :       TYPE(cp_fm_type)                                   :: evecs
     895             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     896             :       TYPE(cp_logger_type), POINTER                      :: logger
     897       19463 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     898             :       TYPE(dft_control_type), POINTER                    :: dft_control
     899       19463 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     900             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     901       19463 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     902             :       TYPE(qs_kind_type), POINTER                        :: qs_kind
     903             :       TYPE(qs_rho_type), POINTER                         :: rho
     904             :       TYPE(xas_environment_type), POINTER                :: xas_env
     905             : 
     906       19463 :       CALL timeset(routineN, handle)
     907             : 
     908       19463 :       NULLIFY (qs_kind_set, matrix_s, dft_control, mos, qs_kind, rho, xas_env, mo_coeff)
     909             : 
     910       19463 :       logger => cp_get_default_logger()
     911             : 
     912       19463 :       CPASSERT(ASSOCIATED(scf_env))
     913       19463 :       CPASSERT(ASSOCIATED(qs_env))
     914       19463 :       NULLIFY (para_env)
     915             : 
     916       19463 :       s_minus_half_available = .FALSE.
     917             :       CALL get_qs_env(qs_env, &
     918             :                       dft_control=dft_control, &
     919             :                       qs_kind_set=qs_kind_set, &
     920             :                       mos=mos, &
     921             :                       rho=rho, &
     922             :                       nelectron_total=scf_env%nelectron, &
     923             :                       do_kpoints=do_kpoints, &
     924             :                       para_env=para_env, &
     925       19463 :                       xas_env=xas_env)
     926             : 
     927             :       output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
     928       19463 :                                          extension=".scfLog")
     929       19463 :       CALL qs_scf_initial_info(output_unit, mos, dft_control)
     930             :       CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
     931       19463 :                                         "PRINT%PROGRAM_RUN_INFO")
     932             : 
     933             :       ! calc ortho matrix
     934       19463 :       ndep = 0
     935       19463 :       IF (scf_env%needs_ortho) THEN
     936       10312 :          CALL get_qs_env(qs_env, matrix_s=matrix_s)
     937       10312 :          CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, scf_env%ortho)
     938       10312 :          IF (scf_env%cholesky_method > cholesky_off) THEN
     939       10274 :             CALL cp_fm_cholesky_decompose(scf_env%ortho)
     940       10274 :             IF (scf_env%cholesky_method == cholesky_dbcsr) THEN
     941          58 :                CALL cp_fm_triangular_invert(scf_env%ortho)
     942          58 :                CALL cp_fm_set_all(scf_env%scf_work2, 0.0_dp)
     943          58 :                CALL cp_fm_to_fm_triangular(scf_env%ortho, scf_env%scf_work2, "U")
     944          58 :                CALL copy_fm_to_dbcsr(scf_env%scf_work2, scf_env%ortho_dbcsr)
     945       10216 :             ELSE IF (scf_env%cholesky_method == cholesky_inverse) THEN
     946          30 :                CALL cp_fm_to_fm(scf_env%ortho, scf_env%ortho_m1)
     947          30 :                CALL cp_fm_triangular_invert(scf_env%ortho_m1)
     948             :             END IF
     949             :          ELSE
     950          38 :             CALL cp_fm_get_info(scf_env%ortho, ncol_global=nao)
     951         114 :             ALLOCATE (evals(nao))
     952        1694 :             evals = 0
     953             : 
     954          38 :             CALL cp_fm_create(evecs, scf_env%ortho%matrix_struct)
     955             : 
     956             :             ! Perform an EVD
     957          38 :             CALL choose_eigv_solver(scf_env%ortho, evecs, evals)
     958             : 
     959             :             ! Determine the number of neglectable eigenvalues assuming that the eigenvalues are in ascending order
     960             :             ! (Required by Lapack)
     961             :             ndep = 0
     962          84 :             DO ii = 1, nao
     963          84 :                IF (evals(ii) > scf_control%eps_eigval) THEN
     964          38 :                   ndep = ii - 1
     965          38 :                   EXIT
     966             :                END IF
     967             :             END DO
     968          38 :             needed_evals = nao - ndep
     969             : 
     970             :             ! Set the eigenvalue of the eigenvectors belonging to the linear subspace to zero
     971          84 :             evals(1:ndep) = 0.0_dp
     972             :             ! Determine the eigenvalues of the inverse square root
     973        1648 :             evals(ndep + 1:nao) = 1.0_dp/SQRT(evals(ndep + 1:nao))
     974             : 
     975             :             ! Create reduced matrices
     976          38 :             NULLIFY (fm_struct)
     977             :             CALL cp_fm_struct_create(fm_struct, template_fmstruct=scf_env%ortho%matrix_struct, &
     978          38 :                                      nrow_global=nao, ncol_global=needed_evals)
     979             : 
     980          38 :             ALLOCATE (scf_env%ortho_red, scf_env%scf_work2_red)
     981          38 :             CALL cp_fm_create(scf_env%ortho_red, fm_struct)
     982          38 :             CALL cp_fm_create(scf_env%scf_work2_red, fm_struct)
     983          38 :             CALL cp_fm_struct_release(fm_struct)
     984             : 
     985          38 :             IF (scf_control%level_shift /= 0.0_dp) THEN
     986             :                CALL cp_fm_struct_create(fm_struct, template_fmstruct=scf_env%ortho%matrix_struct, &
     987           6 :                                         nrow_global=needed_evals, ncol_global=nao)
     988             : 
     989           6 :                ALLOCATE (scf_env%ortho_m1_red)
     990           6 :                CALL cp_fm_create(scf_env%ortho_m1_red, fm_struct)
     991           6 :                CALL cp_fm_struct_release(fm_struct)
     992             :             END IF
     993             : 
     994         164 :             ALLOCATE (scf_env%scf_work1_red(SIZE(scf_env%scf_work1)))
     995          88 :             DO ispin = 1, SIZE(scf_env%scf_work1)
     996             :                CALL cp_fm_struct_create(fm_struct, template_fmstruct=scf_env%ortho%matrix_struct, &
     997          50 :                                         nrow_global=needed_evals, ncol_global=needed_evals)
     998          50 :                CALL cp_fm_create(scf_env%scf_work1_red(ispin), fm_struct)
     999          88 :                CALL cp_fm_struct_release(fm_struct)
    1000             :             END DO
    1001             : 
    1002             :             ! Scale the eigenvalues and copy them to
    1003          38 :             CALL cp_fm_to_fm(evecs, scf_env%ortho_red, needed_evals, ndep + 1, 1)
    1004             : 
    1005          38 :             IF (scf_control%level_shift /= 0.0_dp) THEN
    1006           6 :                CALL cp_fm_transpose(scf_env%ortho_red, scf_env%ortho_m1_red)
    1007             :             END IF
    1008             : 
    1009          38 :             CALL cp_fm_column_scale(scf_env%ortho_red, evals(ndep + 1:))
    1010             : 
    1011             :             ! Copy the linear dependent columns to the mo sets and set their orbital energies
    1012             :             ! to a very large value to reduce the probability of occupying them
    1013          88 :             DO ispin = 1, SIZE(mos)
    1014          50 :                CALL get_mo_set(mos(ispin), nmo=nmo, mo_coeff=mo_coeff, homo=homo, eigenvalues=eigenvalues)
    1015          50 :                IF (needed_evals < nmo) THEN
    1016           2 :                   IF (needed_evals < homo) THEN
    1017             :                      CALL cp_abort(__LOCATION__, &
    1018             :                                    "The numerical rank of the overlap matrix is lower than the "// &
    1019             :                                    "number of orbitals to be occupied! Check the geometry or increase "// &
    1020           0 :                                    "EPS_DEFAULT or EPS_PGF_ORB!")
    1021             :                   END IF
    1022             :                   CALL cp_warn(__LOCATION__, &
    1023             :                                "The numerical rank of the overlap matrix is lower than the number of requested MOs! "// &
    1024             :                              "Reduce the number of MOs to the number of available MOs. If necessary, request a lower number of "// &
    1025           2 :                                "MOs or increase EPS_DEFAULT or EPS_PGF_ORB.")
    1026           2 :                   CALL set_mo_set(mos(ispin), nmo=needed_evals)
    1027             :                END IF
    1028             :                ! Copy the last columns to mo_coeff if the container is large enough
    1029          50 :                CALL cp_fm_to_fm(evecs, mo_coeff, MIN(ndep, MAX(0, nmo - needed_evals)), 1, needed_evals + 1)
    1030             :                ! Set the corresponding eigenvalues to a large value
    1031             :                ! This prevents their occupation but still keeps the information on them
    1032         148 :                eigenvalues(needed_evals + 1:MIN(nao, nmo)) = 1.0_dp/scf_control%eps_eigval
    1033             :             END DO
    1034             : 
    1035             :             ! Obtain ortho from (P)DGEMM, skip the linear dependent columns
    1036             :             CALL parallel_gemm("N", "T", nao, nao, needed_evals, 1.0_dp, scf_env%ortho_red, evecs, &
    1037          38 :                                0.0_dp, scf_env%ortho, b_first_col=ndep + 1)
    1038             : 
    1039          38 :             IF (scf_control%level_shift /= 0.0_dp) THEN
    1040             :                ! We need SQRT(evals) of the eigenvalues of H, so 1/SQRT(evals) of ortho_red
    1041         168 :                evals(ndep + 1:nao) = 1.0_dp/evals(ndep + 1:nao)
    1042           6 :                CALL cp_fm_row_scale(scf_env%ortho_m1_red, evals(ndep + 1:))
    1043             : 
    1044             :                CALL parallel_gemm("T", "T", nao, nao, needed_evals, 1.0_dp, scf_env%ortho_m1_red, evecs, &
    1045           6 :                                   0.0_dp, scf_env%ortho_m1, b_first_col=ndep + 1)
    1046             :             END IF
    1047             : 
    1048          38 :             CALL cp_fm_release(evecs)
    1049             : 
    1050         114 :             s_minus_half_available = .TRUE.
    1051             :          END IF
    1052             : 
    1053       10312 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, &
    1054             :                                               qs_env%input, "DFT%PRINT%AO_MATRICES/ORTHO"), cp_p_file)) THEN
    1055             :             iw = cp_print_key_unit_nr(logger, qs_env%input, "DFT%PRINT%AO_MATRICES/ORTHO", &
    1056           4 :                                       extension=".Log")
    1057           4 :             CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
    1058           4 :             CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%OMIT_HEADERS", l_val=omit_headers)
    1059           4 :             after = MIN(MAX(after, 1), 16)
    1060             :             CALL write_fm_with_basis_info(scf_env%ortho, 4, after, qs_env, &
    1061           4 :                                           para_env, output_unit=iw, omit_headers=omit_headers)
    1062             :             CALL cp_print_key_finished_output(iw, logger, qs_env%input, &
    1063           4 :                                               "DFT%PRINT%AO_MATRICES/ORTHO")
    1064             :          END IF
    1065             :       END IF
    1066             : 
    1067       19463 :       CALL get_mo_set(mo_set=mos(1), nao=nao)
    1068             : 
    1069             :       ! DFT+U methods based on Lowdin charges need S^(1/2)
    1070       19463 :       IF (dft_control%dft_plus_u) THEN
    1071          80 :          CALL get_qs_env(qs_env, matrix_s=matrix_s)
    1072          80 :          IF (dft_control%plus_u_method_id == plus_u_lowdin) THEN
    1073           8 :             IF (s_minus_half_available) THEN
    1074             :                CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, scf_env%ortho, scf_env%s_half, &
    1075           0 :                                             nao)
    1076             :             ELSE
    1077           8 :                CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, scf_env%s_half)
    1078             :                CALL cp_fm_power(scf_env%s_half, scf_env%scf_work2, 0.5_dp, &
    1079           8 :                                 scf_control%eps_eigval, ndep)
    1080             :             END IF
    1081             :          END IF
    1082         240 :          DO ikind = 1, SIZE(qs_kind_set)
    1083         160 :             qs_kind => qs_kind_set(ikind)
    1084             :             CALL get_qs_kind(qs_kind=qs_kind, &
    1085             :                              dft_plus_u_atom=dft_plus_u_atom, &
    1086             :                              u_ramping=u_ramping, &
    1087         160 :                              init_u_ramping_each_scf=init_u_ramping_each_scf)
    1088         240 :             IF (dft_plus_u_atom .AND. (u_ramping /= 0.0_dp)) THEN
    1089          24 :                IF (init_u_ramping_each_scf) THEN
    1090          12 :                   CALL set_qs_kind(qs_kind=qs_kind, u_minus_j=0.0_dp)
    1091             :                END IF
    1092             :             END IF
    1093             :          END DO
    1094             :       END IF
    1095             : 
    1096             :       output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
    1097       19463 :                                          extension=".scfLog")
    1098       19463 :       IF (output_unit > 0) THEN
    1099             :          WRITE (UNIT=output_unit, FMT="(T2,A,T71,I10)") &
    1100        9914 :             "Number of independent orbital functions:", nao - ndep
    1101             :       END IF
    1102             :       CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
    1103       19463 :                                         "PRINT%PROGRAM_RUN_INFO")
    1104             : 
    1105             :       ! extrapolate outer loop variables
    1106       19463 :       IF (scf_control%outer_scf%have_scf) THEN
    1107        3845 :          CALL outer_loop_extrapolate(qs_env)
    1108             :       END IF
    1109             : 
    1110             :       ! initializes rho and the mos
    1111       19463 :       IF (ASSOCIATED(qs_env%xas_env)) THEN
    1112             :          ! if just optimized wfn, e.g. ground state
    1113             :          ! changes come from a perturbation, e.g., the occupation numbers
    1114             :          ! it could be generalized for other cases, at the moment used only for core level spectroscopy
    1115             :          ! initialize the density with the localized mos
    1116          82 :          CALL xas_initialize_rho(qs_env, scf_env, scf_control)
    1117             :       ELSE
    1118             :          CALL scf_env_initial_rho_setup(scf_env, qs_env=qs_env, &
    1119       19381 :                                         scf_section=scf_section, scf_control=scf_control)
    1120             :       END IF
    1121             : 
    1122             :       ! Frozen density approximation
    1123       19463 :       IF (ASSOCIATED(qs_env%wf_history)) THEN
    1124       19463 :          IF (qs_env%wf_history%interpolation_method_nr == wfi_frozen_method_nr) THEN
    1125          12 :             IF (.NOT. ASSOCIATED(qs_env%wf_history%past_states(1)%snapshot)) THEN
    1126           4 :                CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
    1127           4 :                ALLOCATE (qs_env%wf_history%past_states(1)%snapshot%rho_frozen)
    1128           4 :                CALL qs_rho_create(qs_env%wf_history%past_states(1)%snapshot%rho_frozen)
    1129             :                CALL duplicate_rho_type(rho_input=rho, &
    1130             :                                        rho_output=qs_env%wf_history%past_states(1)%snapshot%rho_frozen, &
    1131           4 :                                        qs_env=qs_env)
    1132             :             END IF
    1133             :          END IF
    1134             :       END IF
    1135             : 
    1136             :       !image charge method, calculate image_matrix if required
    1137       19463 :       IF (qs_env%qmmm) THEN
    1138        3802 :          IF (qs_env%qmmm .AND. qs_env%qmmm_env_qm%image_charge) THEN
    1139             :             CALL conditional_calc_image_matrix(qs_env=qs_env, &
    1140          20 :                                                qmmm_env=qs_env%qmmm_env_qm)
    1141             :          END IF
    1142             :       END IF
    1143             : 
    1144       19463 :       CALL timestop(handle)
    1145             : 
    1146       38926 :    END SUBROUTINE init_scf_run
    1147             : 
    1148             : ! **************************************************************************************************
    1149             : !> \brief Initializes rho and the mos, so that an scf cycle can start
    1150             : !> \param scf_env the scf env in which to do the scf
    1151             : !> \param qs_env the qs env the scf_env lives in
    1152             : !> \param scf_section ...
    1153             : !> \param scf_control ...
    1154             : !> \par History
    1155             : !>      02.2003 created [fawzi]
    1156             : !> \author fawzi
    1157             : ! **************************************************************************************************
    1158       19381 :    SUBROUTINE scf_env_initial_rho_setup(scf_env, qs_env, scf_section, scf_control)
    1159             :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
    1160             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1161             :       TYPE(section_vals_type), POINTER                   :: scf_section
    1162             :       TYPE(scf_control_type), POINTER                    :: scf_control
    1163             : 
    1164             :       CHARACTER(len=*), PARAMETER :: routineN = 'scf_env_initial_rho_setup'
    1165             : 
    1166             :       INTEGER                                            :: extrapolation_method_nr, handle, ispin, &
    1167             :                                                             nmo, output_unit
    1168             :       LOGICAL                                            :: do_harris, orthogonal_wf
    1169             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1170             :       TYPE(cp_logger_type), POINTER                      :: logger
    1171             :       TYPE(dft_control_type), POINTER                    :: dft_control
    1172             :       TYPE(harris_type), POINTER                         :: harris_env
    1173       19381 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1174             :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1175             :       TYPE(qs_rho_type), POINTER                         :: rho
    1176       19381 :       TYPE(rho_atom_type), DIMENSION(:), POINTER         :: rho_atom
    1177             : 
    1178       19381 :       CALL timeset(routineN, handle)
    1179       19381 :       NULLIFY (mo_coeff, rho, dft_control, para_env, mos)
    1180       19381 :       logger => cp_get_default_logger()
    1181       19381 :       CPASSERT(ASSOCIATED(scf_env))
    1182       19381 :       CPASSERT(ASSOCIATED(qs_env))
    1183             : 
    1184             :       CALL get_qs_env(qs_env, &
    1185             :                       rho=rho, &
    1186             :                       mos=mos, &
    1187             :                       dft_control=dft_control, &
    1188       19381 :                       para_env=para_env)
    1189             : 
    1190       19381 :       do_harris = qs_env%harris_method
    1191             : 
    1192       19381 :       extrapolation_method_nr = wfi_use_guess_method_nr
    1193       19381 :       IF (ASSOCIATED(qs_env%wf_history)) THEN
    1194             :          CALL wfi_extrapolate(qs_env%wf_history, &
    1195             :                               qs_env=qs_env, dt=1.0_dp, &
    1196             :                               extrapolation_method_nr=extrapolation_method_nr, &
    1197       19381 :                               orthogonal_wf=orthogonal_wf)
    1198             :          ! wfi_use_guess_method_nr the wavefunctions are not yet initialized
    1199             :          IF ((.NOT. orthogonal_wf) .AND. &
    1200       19381 :              (scf_env%method == ot_method_nr) .AND. &
    1201             :              (.NOT. (extrapolation_method_nr == wfi_use_guess_method_nr))) THEN
    1202           0 :             DO ispin = 1, SIZE(mos)
    1203           0 :                CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
    1204           0 :                CALL reorthogonalize_vectors(qs_env, v_matrix=mo_coeff, n_col=nmo)
    1205             :                CALL set_mo_occupation(mo_set=mos(ispin), &
    1206           0 :                                       smear=scf_control%smear)
    1207             :             END DO
    1208             :          END IF
    1209             :       END IF
    1210             : 
    1211       19381 :       IF (.NOT. do_harris) THEN
    1212             :          output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
    1213       19365 :                                             extension=".scfLog")
    1214       19365 :          IF (output_unit > 0) THEN
    1215             :             WRITE (UNIT=output_unit, FMT="(/,T2,A,I0)") &
    1216             :                "Extrapolation method: "// &
    1217        9865 :                TRIM(wfi_get_method_label(extrapolation_method_nr))
    1218        9865 :             IF (extrapolation_method_nr == wfi_ps_method_nr) THEN
    1219             :                WRITE (UNIT=output_unit, FMT="(T2,A,I0,A)") &
    1220         156 :                   "Extrapolation order:  ", &
    1221         312 :                   MAX((MIN(qs_env%wf_history%memory_depth, qs_env%wf_history%snapshot_count) - 1), 0)
    1222             :             END IF
    1223             :          END IF
    1224             :          CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
    1225       19365 :                                            "PRINT%PROGRAM_RUN_INFO")
    1226             :       END IF
    1227             : 
    1228             :       IF (do_harris) THEN
    1229          16 :          CALL get_qs_env(qs_env, harris_env=harris_env)
    1230          16 :          CALL harris_density_update(qs_env, harris_env)
    1231          16 :          CALL qs_rho_update_rho(rho, qs_env=qs_env)
    1232          16 :          CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
    1233       19365 :       ELSE IF (extrapolation_method_nr == wfi_use_guess_method_nr) THEN
    1234        6751 :          CALL calculate_first_density_matrix(scf_env=scf_env, qs_env=qs_env)
    1235        6751 :          CALL qs_rho_update_rho(rho, qs_env=qs_env)
    1236        6751 :          CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
    1237             :       END IF
    1238             : 
    1239             :       ! Some preparation for the mixing
    1240       19381 :       IF (scf_env%mixing_method > 1) THEN
    1241         240 :          IF (dft_control%qs_control%gapw) THEN
    1242          38 :             CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
    1243             :             CALL mixing_init(scf_env%mixing_method, rho, scf_env%mixing_store, &
    1244          38 :                              para_env, rho_atom=rho_atom)
    1245         202 :          ELSEIF (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb) THEN
    1246          36 :             CALL charge_mixing_init(scf_env%mixing_store)
    1247         166 :          ELSEIF (dft_control%qs_control%semi_empirical) THEN
    1248           0 :             CPABORT('SE Code not possible')
    1249             :          ELSE
    1250             :             CALL mixing_init(scf_env%mixing_method, rho, scf_env%mixing_store, &
    1251         166 :                              para_env)
    1252             :          END IF
    1253             :       END IF
    1254             : 
    1255       41196 :       DO ispin = 1, SIZE(mos) !fm->dbcsr
    1256       41196 :          IF (mos(ispin)%use_mo_coeff_b) THEN
    1257             :             CALL copy_fm_to_dbcsr(mos(ispin)%mo_coeff, &
    1258        6537 :                                   mos(ispin)%mo_coeff_b) !fm->dbcsr
    1259             :          END IF
    1260             :       END DO !fm->dbcsr
    1261             : 
    1262       19381 :       CALL timestop(handle)
    1263             : 
    1264       19381 :    END SUBROUTINE scf_env_initial_rho_setup
    1265             : 
    1266             : END MODULE qs_scf_initialization

Generated by: LCOV version 1.15