LCOV - code coverage report
Current view: top level - src - qs_ks_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:c24029e) Lines: 93.8 % 610 572
Test Date: 2026-07-04 06:36:57 Functions: 100.0 % 7 7

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief routines that build the Kohn-Sham matrix (i.e calculate the coulomb
      10              : !>        and xc parts
      11              : !> \author Fawzi Mohamed
      12              : !> \par History
      13              : !>      - 05.2002 moved from qs_scf (see there the history) [fawzi]
      14              : !>      - JGH [30.08.02] multi-grid arrays independent from density and potential
      15              : !>      - 10.2002 introduced pools, uses updated rho as input,
      16              : !>                removed most temporary variables, renamed may vars,
      17              : !>                began conversion to LSD [fawzi]
      18              : !>      - 10.2004 moved calculate_w_matrix here [Joost VandeVondele]
      19              : !>                introduced energy derivative wrt MOs [Joost VandeVondele]
      20              : !>      - SCCS implementation (16.10.2013,MK)
      21              : ! **************************************************************************************************
      22              : MODULE qs_ks_methods
      23              :    USE accint_weights_forces,           ONLY: accint_weight_force
      24              :    USE admm_dm_methods,                 ONLY: admm_dm_calc_rho_aux,&
      25              :                                               admm_dm_merge_ks_matrix
      26              :    USE admm_methods,                    ONLY: admm_mo_calc_rho_aux,&
      27              :                                               admm_mo_calc_rho_aux_kp,&
      28              :                                               admm_mo_merge_ks_matrix,&
      29              :                                               admm_update_ks_atom,&
      30              :                                               calc_admm_mo_derivatives,&
      31              :                                               calc_admm_ovlp_forces,&
      32              :                                               calc_admm_ovlp_forces_kp
      33              :    USE admm_types,                      ONLY: admm_type,&
      34              :                                               get_admm_env
      35              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      36              :                                               get_atomic_kind_set
      37              :    USE cell_types,                      ONLY: cell_type
      38              :    USE cp_control_types,                ONLY: dft_control_type
      39              :    USE cp_dbcsr_api,                    ONLY: &
      40              :         dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_filter, dbcsr_get_info, dbcsr_multiply, &
      41              :         dbcsr_p_type, dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
      42              :         dbcsr_type_symmetric
      43              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      44              :    USE cp_dbcsr_operations,             ONLY: dbcsr_allocate_matrix_set,&
      45              :                                               dbcsr_copy_columns_hack
      46              :    USE cp_ddapc,                        ONLY: qs_ks_ddapc
      47              :    USE cp_fm_types,                     ONLY: cp_fm_type
      48              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      49              :                                               cp_logger_get_default_io_unit,&
      50              :                                               cp_logger_type
      51              :    USE cp_output_handling,              ONLY: cp_p_file,&
      52              :                                               cp_print_key_should_output
      53              :    USE dft_plus_u,                      ONLY: plus_u
      54              :    USE gce_methods,                     ONLY: planar_averaged_v_hartree_3d,&
      55              :                                               planar_counter_charge
      56              :    USE hartree_local_methods,           ONLY: Vh_1c_gg_integrals
      57              :    USE hartree_local_types,             ONLY: ecoul_1center_type
      58              :    USE hfx_ace_methods,                 ONLY: hfx_ace_ks_matrix
      59              :    USE hfx_admm_utils,                  ONLY: hfx_admm_init,&
      60              :                                               hfx_ks_matrix,&
      61              :                                               hfx_ks_matrix_kp
      62              :    USE input_constants,                 ONLY: do_ppl_grid,&
      63              :                                               outer_scf_becke_constraint,&
      64              :                                               outer_scf_hirshfeld_constraint,&
      65              :                                               smeagol_runtype_emtransport
      66              :    USE input_section_types,             ONLY: section_vals_get,&
      67              :                                               section_vals_get_subs_vals,&
      68              :                                               section_vals_type,&
      69              :                                               section_vals_val_get
      70              :    USE kg_correction,                   ONLY: kg_ekin_subset
      71              :    USE kinds,                           ONLY: default_string_length,&
      72              :                                               dp
      73              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      74              :                                               kpoint_type
      75              :    USE lri_environment_methods,         ONLY: v_int_ppl_energy
      76              :    USE lri_environment_types,           ONLY: lri_density_type,&
      77              :                                               lri_environment_type,&
      78              :                                               lri_kind_type
      79              :    USE mathlib,                         ONLY: abnormal_value
      80              :    USE message_passing,                 ONLY: mp_para_env_type
      81              :    USE particle_types,                  ONLY: particle_type
      82              :    USE pw_env_types,                    ONLY: pw_env_get,&
      83              :                                               pw_env_type
      84              :    USE pw_methods,                      ONLY: pw_axpy,&
      85              :                                               pw_copy,&
      86              :                                               pw_integral_ab,&
      87              :                                               pw_integrate_function,&
      88              :                                               pw_scale,&
      89              :                                               pw_transfer,&
      90              :                                               pw_zero
      91              :    USE pw_poisson_methods,              ONLY: pw_poisson_solve
      92              :    USE pw_poisson_types,                ONLY: pw_poisson_implicit,&
      93              :                                               pw_poisson_type
      94              :    USE pw_pool_types,                   ONLY: pw_pool_type
      95              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      96              :                                               pw_r3d_rs_type
      97              :    USE qmmm_image_charge,               ONLY: add_image_pot_to_hartree_pot,&
      98              :                                               calculate_image_pot,&
      99              :                                               integrate_potential_devga_rspace
     100              :    USE qs_cdft_types,                   ONLY: cdft_control_type
     101              :    USE qs_charges_types,                ONLY: qs_charges_type
     102              :    USE qs_core_energies,                ONLY: calculate_ptrace
     103              :    USE qs_dftb_matrices,                ONLY: build_dftb_ks_matrix
     104              :    USE qs_efield_berry,                 ONLY: qs_efield_berry_phase
     105              :    USE qs_efield_local,                 ONLY: qs_efield_local_operator
     106              :    USE qs_energy_types,                 ONLY: qs_energy_type
     107              :    USE qs_environment_types,            ONLY: get_qs_env,&
     108              :                                               qs_environment_type
     109              :    USE qs_force_types,                  ONLY: qs_force_type
     110              :    USE qs_gapw_densities,               ONLY: prepare_gapw_den
     111              :    USE qs_harris_types,                 ONLY: harris_type
     112              :    USE qs_harris_utils,                 ONLY: harris_set_potentials
     113              :    USE qs_integrate_potential,          ONLY: integrate_ppl_rspace,&
     114              :                                               integrate_rho_nlcc,&
     115              :                                               integrate_v_core_rspace
     116              :    USE qs_kind_types,                   ONLY: qs_kind_type
     117              :    USE qs_ks_apply_restraints,          ONLY: qs_ks_cdft_constraint,&
     118              :                                               qs_ks_mulliken_restraint,&
     119              :                                               qs_ks_s2_restraint
     120              :    USE qs_ks_atom,                      ONLY: update_ks_atom
     121              :    USE qs_ks_qmmm_methods,              ONLY: qmmm_calculate_energy,&
     122              :                                               qmmm_modify_hartree_pot
     123              :    USE qs_ks_types,                     ONLY: qs_ks_env_type,&
     124              :                                               set_ks_env
     125              :    USE qs_ks_utils,                     ONLY: &
     126              :         calc_v_sic_rspace, calculate_zmp_potential, compute_matrix_vxc, compute_matrix_vxc_kp, &
     127              :         get_embed_potential_energy, low_spin_roks, print_densities, print_detailed_energy, &
     128              :         sic_explicit_orbitals, sum_up_and_integrate
     129              :    USE qs_local_rho_types,              ONLY: local_rho_type
     130              :    USE qs_mo_types,                     ONLY: get_mo_set,&
     131              :                                               mo_set_type
     132              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
     133              :    USE qs_rho0_ggrid,                   ONLY: integrate_vhg0_rspace
     134              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
     135              :                                               qs_rho_type
     136              :    USE qs_sccs,                         ONLY: sccs
     137              :    USE qs_vxc,                          ONLY: qs_vxc_create
     138              :    USE qs_vxc_atom,                     ONLY: calculate_vxc_atom
     139              :    USE rtp_admm_methods,                ONLY: rtp_admm_calc_rho_aux,&
     140              :                                               rtp_admm_merge_ks_matrix
     141              :    USE se_fock_matrix,                  ONLY: build_se_fock_matrix
     142              :    USE skala_gpw_functional,            ONLY: ensure_native_skala_grid_scope,&
     143              :                                               get_gauxc_section,&
     144              :                                               xc_section_uses_native_skala_grid
     145              :    USE smeagol_interface,               ONLY: smeagol_shift_v_hartree
     146              :    USE surface_dipole,                  ONLY: calc_dipsurf_potential
     147              :    USE tblite_ks_matrix,                ONLY: build_tblite_ks_matrix
     148              :    USE virial_types,                    ONLY: virial_type
     149              :    USE xc_gauxc_functional,             ONLY: apply_gauxc,&
     150              :                                               gauxc_gapw_has_paw_pseudopotentials
     151              :    USE xtb_ks_matrix,                   ONLY: build_xtb_ks_matrix
     152              : #include "./base/base_uses.f90"
     153              : 
     154              :    IMPLICIT NONE
     155              : 
     156              :    PRIVATE
     157              : 
     158              :    LOGICAL, PARAMETER :: debug_this_module = .TRUE.
     159              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_methods'
     160              : 
     161              :    PUBLIC :: calc_rho_tot_gspace, qs_ks_update_qs_env, qs_ks_build_kohn_sham_matrix, &
     162              :              qs_ks_allocate_basics, evaluate_core_matrix_traces, rebuild_ks_matrix
     163              : 
     164              : CONTAINS
     165              : 
     166              : ! **************************************************************************************************
     167              : !> \brief routine where the real calculations are made: the
     168              : !>      KS matrix is calculated
     169              : !> \param qs_env the qs_env to update
     170              : !> \param calculate_forces if true calculate the quantities needed
     171              : !>        to calculate the forces. Defaults to false.
     172              : !> \param just_energy if true updates the energies but not the
     173              : !>        ks matrix. Defaults to false
     174              : !> \param print_active ...
     175              : !> \param ext_ks_matrix ...
     176              : !> \param ext_xc_section ...
     177              : !> \par History
     178              : !>      06.2002 moved from qs_scf to qs_ks_methods, use of ks_env
     179              : !>              new did_change scheme [fawzi]
     180              : !>      10.2002 introduced pools, uses updated rho as input, LSD [fawzi]
     181              : !>      10.2004 build_kohn_sham matrix now also computes the derivatives
     182              : !>              of the total energy wrt to the MO coefs, if instructed to
     183              : !>              do so. This appears useful for orbital dependent functionals
     184              : !>              where the KS matrix alone (however this might be defined)
     185              : !>               does not contain the info to construct this derivative.
     186              : !> \author Matthias Krack
     187              : !> \note
     188              : !>      make rho, energy and qs_charges optional, defaulting
     189              : !>      to qs_env components?
     190              : ! **************************************************************************************************
     191       120269 :    SUBROUTINE qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, &
     192              :                                            print_active, ext_ks_matrix, ext_xc_section)
     193              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     194              :       LOGICAL, INTENT(in)                                :: calculate_forces, just_energy
     195              :       LOGICAL, INTENT(IN), OPTIONAL                      :: print_active
     196              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
     197              :          POINTER                                         :: ext_ks_matrix
     198              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: ext_xc_section
     199              : 
     200              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_build_kohn_sham_matrix'
     201              : 
     202              :       CHARACTER(len=default_string_length)               :: name
     203              :       INTEGER                                            :: ace_rebuild_frequency, atom_a, handle, &
     204              :                                                             iatom, ikind, img, ispin, natom, &
     205              :                                                             nimages, nspins, output_unit
     206       120269 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
     207              :       LOGICAL :: ace_active, do_adiabatic_rescaling, do_ddapc, do_hfx, do_kpoints, do_ppl, dokp, &
     208              :          gapw, gapw_xc, just_energy_xc, lrigpw, my_print, native_grid_diagnostics, &
     209              :          native_grid_use_cuda, native_skala_restore_exc, rigpw, use_gauxc_matrix, use_virial
     210              :       LOGICAL, SAVE :: native_grid_cpu_kpoints_warned = .FALSE.
     211              :       REAL(KIND=dp)                                      :: ecore_ppl, edisp, ee_ener, ekin_mol, &
     212              :                                                             mulliken_order_p, &
     213              :                                                             native_skala_exc_scf, &
     214              :                                                             native_skala_total_scf, vscale
     215       120269 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: native_skala_atom_force
     216              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: h_stress, pv_loc
     217              :       TYPE(admm_type), POINTER                           :: admm_env
     218       120269 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     219              :       TYPE(cdft_control_type), POINTER                   :: cdft_control
     220              :       TYPE(cell_type), POINTER                           :: cell
     221              :       TYPE(cp_logger_type), POINTER                      :: logger
     222       120269 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ksmat, matrix_vxc, mo_derivs
     223       120269 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: ks_matrix, ks_matrix_im, matrix_h, &
     224       120269 :                                                             matrix_h_im, matrix_s, matrix_vxc_kp, &
     225       120269 :                                                             my_rho, rho_ao
     226              :       TYPE(dft_control_type), POINTER                    :: dft_control
     227       120269 :       TYPE(ecoul_1center_type), DIMENSION(:), POINTER    :: ecoul_1c
     228              :       TYPE(harris_type), POINTER                         :: harris_env
     229              :       TYPE(kpoint_type), POINTER                         :: kpoints
     230              :       TYPE(local_rho_type), POINTER                      :: local_rho_set
     231              :       TYPE(lri_density_type), POINTER                    :: lri_density
     232              :       TYPE(lri_environment_type), POINTER                :: lri_env
     233       120269 :       TYPE(lri_kind_type), DIMENSION(:), POINTER         :: lri_v_int
     234              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     235       120269 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     236              :       TYPE(pw_c1d_gs_type)                               :: rho_tot_gspace, v_hartree_gspace
     237              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho_core
     238              :       TYPE(pw_env_type), POINTER                         :: pw_env
     239              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
     240              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     241       120269 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r, v_rspace_embed, v_rspace_new, &
     242       120269 :                                                             v_rspace_new_aux_fit, v_tau_rspace, &
     243       120269 :                                                             v_tau_rspace_aux_fit
     244              :       TYPE(pw_r3d_rs_type), POINTER :: rho0_s_rs, rho_nlcc, rhoz_cneo_s_rs, v_hartree_rspace, &
     245              :          v_sccs_rspace, v_sic_rspace, v_spin_ddapc_rest_r, vee, vppl_rspace
     246              :       TYPE(qs_energy_type), POINTER                      :: energy
     247       120269 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     248       120269 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     249              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     250              :       TYPE(qs_rho_type), POINTER                         :: rho, rho1, rho_struct, rho_xc
     251              :       TYPE(section_vals_type), POINTER                   :: ace_section, &
     252              :                                                             adiabatic_rescaling_section, &
     253              :                                                             gauxc_section, hfx_sections, input, &
     254              :                                                             scf_section, xc_section
     255              :       TYPE(virial_type), POINTER                         :: virial
     256              : 
     257       120269 :       CALL timeset(routineN, handle)
     258       120269 :       NULLIFY (admm_env, atomic_kind_set, cell, dft_control, force, logger, mo_derivs, my_rho, &
     259       120269 :                rho_struct, para_env, pw_env, virial, vppl_rspace, &
     260       120269 :                ace_section, &
     261       120269 :                adiabatic_rescaling_section, hfx_sections, input, scf_section, &
     262       120269 :                xc_section, gauxc_section, matrix_h, matrix_h_im, matrix_s, auxbas_pw_pool, poisson_env, &
     263       120269 :                v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, &
     264       120269 :                matrix_vxc_kp, &
     265       120269 :                vee, rho_nlcc, ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, &
     266       120269 :                rho_core, particle_set, qs_kind_set, kpoints)
     267              : 
     268       120269 :       CPASSERT(ASSOCIATED(qs_env))
     269              : 
     270       120269 :       logger => cp_get_default_logger()
     271       120269 :       my_print = .TRUE.
     272       120269 :       IF (PRESENT(print_active)) my_print = print_active
     273       120269 :       use_gauxc_matrix = .FALSE.
     274       120269 :       native_skala_restore_exc = .FALSE.
     275              : 
     276              :       CALL get_qs_env(qs_env, &
     277              :                       ks_env=ks_env, &
     278              :                       dft_control=dft_control, &
     279              :                       matrix_h_kp=matrix_h, &
     280              :                       matrix_h_im_kp=matrix_h_im, &
     281              :                       matrix_s_kp=matrix_s, &
     282              :                       matrix_ks_kp=ks_matrix, &
     283              :                       matrix_ks_im_kp=ks_matrix_im, &
     284              :                       matrix_vxc=matrix_vxc, &
     285              :                       matrix_vxc_kp=matrix_vxc_kp, &
     286              :                       pw_env=pw_env, &
     287              :                       cell=cell, &
     288              :                       atomic_kind_set=atomic_kind_set, &
     289              :                       para_env=para_env, &
     290              :                       input=input, &
     291              :                       virial=virial, &
     292              :                       v_hartree_rspace=v_hartree_rspace, &
     293              :                       vee=vee, &
     294              :                       rho_nlcc=rho_nlcc, &
     295              :                       rho=rho, &
     296              :                       rho_core=rho_core, &
     297              :                       rho_xc=rho_xc, &
     298              :                       energy=energy, &
     299              :                       force=force, &
     300              :                       kpoints=kpoints, &
     301              :                       do_kpoints=do_kpoints, &
     302              :                       particle_set=particle_set, &
     303              :                       qs_kind_set=qs_kind_set, &
     304       120269 :                       natom=natom)
     305              : 
     306       120269 :       CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
     307              : 
     308       120269 :       nimages = dft_control%nimages
     309       120269 :       nspins = dft_control%nspins
     310              : 
     311              :       ! remap pointer to allow for non-kpoint external ks matrix
     312       120269 :       IF (PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
     313              : 
     314       120269 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     315              : 
     316       120269 :       adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
     317       120269 :       CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
     318       120269 :       just_energy_xc = just_energy
     319       120269 :       IF (do_adiabatic_rescaling) THEN
     320              :          !! If we perform adiabatic rescaling, the xc potential has to be scaled by the xc- and
     321              :          !! HFX-energy. Thus, let us first calculate the energy
     322           44 :          just_energy_xc = .TRUE.
     323              :       END IF
     324              : 
     325       120269 :       CPASSERT(ASSOCIATED(matrix_h))
     326       120269 :       CPASSERT(ASSOCIATED(matrix_s))
     327       120269 :       CPASSERT(ASSOCIATED(rho))
     328       120269 :       CPASSERT(ASSOCIATED(pw_env))
     329       120269 :       CPASSERT(SIZE(ks_matrix, 1) > 0)
     330       120269 :       dokp = (nimages > 1)
     331              : 
     332              :       ! Setup the possible usage of DDAPC charges
     333              :       do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
     334              :                  qs_env%cp_ddapc_ewald%do_decoupling .OR. &
     335              :                  qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
     336       120269 :                  qs_env%cp_ddapc_ewald%do_solvation
     337              : 
     338              :       ! Check if LRIGPW is used
     339       120269 :       lrigpw = dft_control%qs_control%lrigpw
     340       120269 :       rigpw = dft_control%qs_control%rigpw
     341       120269 :       IF (rigpw) THEN
     342           26 :          CPASSERT(nimages == 1)
     343              :       END IF
     344           26 :       IF (lrigpw .AND. rigpw) THEN
     345            0 :          CPABORT(" LRI and RI are not compatible")
     346              :       END IF
     347              : 
     348              :       ! Check for GAPW method : additional terms for local densities
     349       120269 :       gapw = dft_control%qs_control%gapw
     350       120269 :       gapw_xc = dft_control%qs_control%gapw_xc
     351       120269 :       IF (gapw_xc .AND. gapw) THEN
     352            0 :          CPABORT(" GAPW and GAPW_XC are not compatible")
     353              :       END IF
     354       120269 :       IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw)) THEN
     355            0 :          CPABORT(" GAPW/GAPW_XC and LRIGPW are not compatible")
     356              :       END IF
     357       120269 :       IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw)) THEN
     358            0 :          CPABORT(" GAPW/GAPW_XC and RIGPW are not compatible")
     359              :       END IF
     360              : 
     361       120269 :       do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
     362       120269 :       IF (do_ppl) THEN
     363           60 :          CPASSERT(.NOT. gapw)
     364           60 :          CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
     365              :       END IF
     366              : 
     367       120269 :       IF (gapw_xc) THEN
     368         4040 :          CPASSERT(ASSOCIATED(rho_xc))
     369              :       END IF
     370              : 
     371              :       ! gets the tmp grids
     372       120269 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
     373              : 
     374       120269 :       IF (gapw .AND. (poisson_env%parameters%solver == pw_poisson_implicit)) THEN
     375            0 :          CPABORT("The implicit Poisson solver cannot be used in conjunction with GAPW.")
     376              :       END IF
     377              : 
     378              :       ! ***  Prepare densities for gapw ***
     379       120269 :       IF (gapw .OR. gapw_xc) THEN
     380        24544 :          CALL prepare_gapw_den(qs_env, do_rho0=(.NOT. gapw_xc))
     381              :       END IF
     382              : 
     383              :       ! Calculate the Hartree potential
     384       120269 :       CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
     385       120269 :       CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
     386              : 
     387       120269 :       scf_section => section_vals_get_subs_vals(input, "DFT%SCF")
     388              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, scf_section, &
     389              :                                            "PRINT%DETAILED_ENERGY"), &
     390              :                 cp_p_file) .AND. &
     391       120269 :           (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
     392              :           (.NOT. (poisson_env%parameters%solver == pw_poisson_implicit))) THEN
     393          916 :          CALL pw_zero(rho_tot_gspace)
     394          916 :          CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density=.TRUE.)
     395              :          CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%e_hartree, &
     396          916 :                                v_hartree_gspace)
     397          916 :          CALL pw_zero(rho_tot_gspace)
     398          916 :          CALL pw_zero(v_hartree_gspace)
     399              :       END IF
     400              : 
     401              :       ! Get the total density in g-space [ions + electrons]
     402       120269 :       CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
     403              : 
     404       120269 :       IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc) THEN
     405            0 :          CPABORT("GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
     406              :       END IF
     407              : 
     408              :       ! Add the planar counter charge density
     409       120269 :       IF (dft_control%do_pcc) THEN
     410          114 :          CALL planar_counter_charge(rho_tot_gspace, dft_control%pcc_control, auxbas_pw_pool)
     411              :       END IF
     412              : 
     413       120269 :       IF (my_print) THEN
     414       120247 :          CALL print_densities(qs_env, rho)
     415              :       END IF
     416              : 
     417       120269 :       IF (dft_control%do_sccs) THEN
     418              :          ! Self-consistent continuum solvation (SCCS) model
     419              :          NULLIFY (v_sccs_rspace)
     420          160 :          ALLOCATE (v_sccs_rspace)
     421          160 :          CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
     422              : 
     423          160 :          IF (poisson_env%parameters%solver == pw_poisson_implicit) THEN
     424            0 :             CPABORT("The implicit Poisson solver cannot be used together with SCCS.")
     425              :          END IF
     426              : 
     427          160 :          IF (use_virial .AND. calculate_forces) THEN
     428              :             CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
     429            0 :                       h_stress=h_stress)
     430            0 :             virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
     431            0 :             virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
     432              :          ELSE
     433          160 :             CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
     434              :          END IF
     435              :       ELSE
     436              :          ! Getting the Hartree energy and Hartree potential.  Also getting the stress tensor
     437              :          ! from the Hartree term if needed.  No nuclear force information here
     438       120109 :          IF (use_virial .AND. calculate_forces) THEN
     439          476 :             h_stress(:, :) = 0.0_dp
     440              :             CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
     441              :                                   v_hartree_gspace, h_stress=h_stress, &
     442          476 :                                   rho_core=rho_core)
     443         6188 :             virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
     444         6188 :             virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
     445              :          ELSE
     446              :             CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
     447       119633 :                                   v_hartree_gspace, rho_core=rho_core)
     448              :          END IF
     449              :       END IF
     450              : 
     451       120269 :       IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce) THEN
     452           86 :          CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     453              :          CALL planar_averaged_v_hartree_3d(v_hartree_rspace, dft_control, qs_env%scf_control%gce%do_gce, &
     454           86 :                                            qs_env%scf_control%gce%ref_esp, para_env)
     455              :       END IF
     456              : 
     457              :       ! In case decouple periodic images and/or apply restraints to charges
     458       120269 :       IF (do_ddapc) THEN
     459              :          CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
     460              :                           v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
     461         1742 :                           just_energy)
     462              :       ELSE
     463       118527 :          dft_control%qs_control%ddapc_explicit_potential = .FALSE.
     464       118527 :          dft_control%qs_control%ddapc_restraint_is_spin = .FALSE.
     465       118527 :          IF (.NOT. just_energy) THEN
     466       109375 :             CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     467       109375 :             CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
     468              :          END IF
     469              :       END IF
     470       120269 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
     471              : 
     472       120269 :       IF (dft_control%correct_surf_dip) THEN
     473          110 :          IF (dft_control%surf_dip_correct_switch) THEN
     474          110 :             CALL calc_dipsurf_potential(qs_env, energy)
     475          110 :             energy%hartree = energy%hartree + energy%surf_dipole
     476              :          END IF
     477              :       END IF
     478              : 
     479              :       ! SIC
     480              :       CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
     481       120269 :                              just_energy, calculate_forces, auxbas_pw_pool)
     482              : 
     483              :       ! Check if CDFT constraint is needed
     484       120269 :       CALL qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
     485              : 
     486              :       ! Adds the External Potential if requested
     487       120269 :       IF (dft_control%apply_external_potential) THEN
     488              :          ! Compute the energy due to the external potential
     489              :          ee_ener = 0.0_dp
     490          728 :          DO ispin = 1, nspins
     491          728 :             ee_ener = ee_ener + pw_integral_ab(rho_r(ispin), vee)
     492              :          END DO
     493          364 :          IF (.NOT. just_energy) THEN
     494          364 :             IF (gapw) THEN
     495              :                CALL get_qs_env(qs_env=qs_env, &
     496              :                                rho0_s_rs=rho0_s_rs, &
     497           42 :                                rhoz_cneo_s_rs=rhoz_cneo_s_rs)
     498           42 :                CPASSERT(ASSOCIATED(rho0_s_rs))
     499           42 :                IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
     500            0 :                   CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
     501              :                END IF
     502           42 :                ee_ener = ee_ener + pw_integral_ab(rho0_s_rs, vee)
     503           42 :                IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
     504            0 :                   CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
     505              :                END IF
     506              :             END IF
     507              :          END IF
     508              :          ! the sign accounts for the charge of the electrons
     509          364 :          energy%ee = -ee_ener
     510              :       END IF
     511              : 
     512              :       ! Adds the QM/MM potential
     513       120269 :       IF (qs_env%qmmm) THEN
     514              :          CALL qmmm_calculate_energy(qs_env=qs_env, &
     515              :                                     rho=rho_r, &
     516              :                                     v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
     517         6306 :                                     qmmm_energy=energy%qmmm_el)
     518         6306 :          IF (qs_env%qmmm_env_qm%image_charge) THEN
     519              :             CALL calculate_image_pot(v_hartree_rspace=v_hartree_rspace, &
     520              :                                      rho_hartree_gspace=rho_tot_gspace, &
     521              :                                      energy=energy, &
     522              :                                      qmmm_env=qs_env%qmmm_env_qm, &
     523           60 :                                      qs_env=qs_env)
     524           60 :             IF (.NOT. just_energy) THEN
     525              :                CALL add_image_pot_to_hartree_pot(v_hartree=v_hartree_rspace, &
     526              :                                                  v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
     527           60 :                                                  qs_env=qs_env)
     528           60 :                IF (calculate_forces) THEN
     529              :                   CALL integrate_potential_devga_rspace( &
     530              :                      potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
     531              :                      forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
     532           20 :                      qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
     533              :                END IF
     534              :             END IF
     535           60 :             CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
     536           60 :             DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
     537              :          END IF
     538         6306 :          IF (.NOT. just_energy) THEN
     539              :             CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
     540         6222 :                                          v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
     541              :          END IF
     542              :       END IF
     543       120269 :       CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
     544              : 
     545              :       ! SMEAGOL interface
     546       120269 :       IF (dft_control%smeagol_control%smeagol_enabled .AND. &
     547              :           dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
     548            0 :          CPASSERT(ASSOCIATED(dft_control%smeagol_control%aux))
     549              :          CALL smeagol_shift_v_hartree(v_hartree_rspace, cell, &
     550              :                                       dft_control%smeagol_control%aux%HartreeLeadsLeft, &
     551              :                                       dft_control%smeagol_control%aux%HartreeLeadsRight, &
     552              :                                       dft_control%smeagol_control%aux%HartreeLeadsBottom, &
     553              :                                       dft_control%smeagol_control%aux%VBias, &
     554              :                                       dft_control%smeagol_control%aux%minL, &
     555              :                                       dft_control%smeagol_control%aux%maxR, &
     556              :                                       dft_control%smeagol_control%aux%isexplicit_maxR, &
     557            0 :                                       dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
     558              :       END IF
     559              : 
     560              :       ! calculate the density matrix for the fitted mo_coeffs
     561       120269 :       IF (dft_control%do_admm) THEN
     562        13020 :          IF (PRESENT(ext_xc_section)) THEN
     563            0 :             CALL hfx_admm_init(qs_env, calculate_forces, ext_xc_section)
     564              :          ELSE
     565        13020 :             CALL hfx_admm_init(qs_env, calculate_forces)
     566              :          END IF
     567              : 
     568        13020 :          IF (dft_control%do_admm_mo) THEN
     569        12806 :             IF (qs_env%run_rtp) THEN
     570           92 :                CALL rtp_admm_calc_rho_aux(qs_env)
     571              :             ELSE
     572        12714 :                IF (dokp) THEN
     573          156 :                   CALL admm_mo_calc_rho_aux_kp(qs_env)
     574              :                ELSE
     575        12558 :                   CALL admm_mo_calc_rho_aux(qs_env)
     576              :                END IF
     577              :             END IF
     578          214 :          ELSEIF (dft_control%do_admm_dm) THEN
     579          214 :             CALL admm_dm_calc_rho_aux(qs_env)
     580              :          END IF
     581              :       END IF
     582              : 
     583              :       ! only activate stress calculation if
     584       120269 :       IF (use_virial .AND. calculate_forces) virial%pv_calculate = .TRUE.
     585              : 
     586              :       ! *** calculate the xc potential on the pw density ***
     587              :       ! *** associates v_rspace_new if the xc potential needs to be computed.
     588              :       ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
     589       120269 :       IF (dft_control%do_admm) THEN
     590        13020 :          CALL get_qs_env(qs_env, admm_env=admm_env)
     591        13020 :          xc_section => admm_env%xc_section_aux
     592        13020 :          CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
     593              : 
     594              :          ! here we ignore a possible vdW section in admm_env%xc_section_aux
     595              :          CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     596              :                             vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
     597        13020 :                             just_energy=just_energy_xc)
     598              : 
     599        13020 :          IF (admm_env%do_gapw) THEN
     600              :             !compute the potential due to atomic densities
     601              :             CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
     602              :                                     kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
     603              :                                     xc_section_external=xc_section, &
     604              :                                     rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
     605         4394 :                                     calculate_forces=calculate_forces)
     606              : 
     607              :          END IF
     608              : 
     609        13020 :          NULLIFY (rho_struct)
     610              : 
     611        13020 :          IF (use_virial .AND. calculate_forces) THEN
     612           20 :             vscale = 1.0_dp
     613              :             !Note: ADMMS and ADMMP stress tensor only for closed-shell calculations
     614           20 :             IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
     615           20 :             IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
     616          260 :             virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
     617          260 :             virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
     618              :             ! virial%pv_xc will be zeroed in the xc routines
     619              :          END IF
     620        13020 :          xc_section => admm_env%xc_section_primary
     621              :       ELSE
     622       107249 :          xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     623              :          ! build ks matrix with an xc section potentially different from the one defined in input
     624       107249 :          IF (PRESENT(ext_xc_section)) xc_section => ext_xc_section
     625              :       END IF
     626              : 
     627       120269 :       IF (gapw_xc) THEN
     628         4040 :          CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
     629              :       ELSE
     630       116229 :          CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
     631              :       END IF
     632              : 
     633              :       ! zmp
     634       120269 :       IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc) THEN
     635            0 :          energy%exc = 0.0_dp
     636            0 :          CALL calculate_zmp_potential(qs_env, v_rspace_new, rho, exc=energy%exc)
     637              :       ELSE
     638              :          ! Embedding potential (runs regardless of XC method)
     639       120269 :          IF (dft_control%apply_embed_pot) THEN
     640          868 :             NULLIFY (v_rspace_embed)
     641          868 :             energy%embed_corr = 0.0_dp
     642              :             CALL get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, &
     643          868 :                                             energy%embed_corr, just_energy)
     644              :          END IF
     645              : 
     646              :          ! Everything else, either via GauXC or manual XC computation
     647       120269 :          IF (dft_control%use_gauxc) THEN
     648          666 :             IF (xc_section_uses_native_skala_grid(xc_section)) THEN
     649          288 :                CALL ensure_native_skala_grid_scope(xc_section)
     650          288 :                IF ((.NOT. do_kpoints) .AND. nimages /= 1) THEN
     651              :                   CALL cp_abort(__LOCATION__, &
     652              :                                 "Native SKALA grid evaluation supports multiple images only "// &
     653            0 :                                 "for k-point calculations.")
     654              :                END IF
     655          288 :                IF (do_kpoints) THEN
     656           48 :                   CPASSERT(ASSOCIATED(kpoints))
     657           48 :                   gauxc_section => get_gauxc_section(xc_section)
     658           48 :                   CPASSERT(ASSOCIATED(gauxc_section))
     659           48 :                   CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_USE_CUDA", l_val=native_grid_use_cuda)
     660           48 :                   IF (.NOT. native_grid_use_cuda) THEN
     661           48 :                      IF (para_env%mepos == 0 .AND. .NOT. native_grid_cpu_kpoints_warned) THEN
     662              :                         CALL cp_warn(__LOCATION__, &
     663              :                                      "Native SKALA grid evaluation with k-points is using the CPU TorchScript "// &
     664              :                                      "path. For CPU runs, preload libtorch_cpu.so before OpenBLAS if the "// &
     665           12 :                                      "runtime library order is unstable; otherwise use NATIVE_GRID_USE_CUDA T.")
     666           12 :                         native_grid_cpu_kpoints_warned = .TRUE.
     667              :                      END IF
     668              :                   END IF
     669              :                END IF
     670          288 :                IF (dft_control%roks) THEN
     671            0 :                   CPABORT("Native SKALA grid evaluation does not support ROKS.")
     672              :                END IF
     673          288 :                IF (dft_control%do_admm) THEN
     674            0 :                   CPABORT("Native SKALA grid evaluation does not support ADMM.")
     675              :                END IF
     676              :                ! Force-only rebuilds re-enter this path for derivatives and VXC only.
     677              :                ! For analytical stress the XC volume term must stay consistent with
     678              :                ! the rebuilt SKALA energy used by the autograd virial.
     679          288 :                native_skala_restore_exc = calculate_forces .AND. .NOT. use_virial
     680              :                IF (native_skala_restore_exc) THEN
     681           10 :                   native_skala_exc_scf = energy%exc
     682           10 :                   native_skala_total_scf = energy%total
     683              :                END IF
     684          288 :                IF (calculate_forces) THEN
     685          180 :                   ALLOCATE (native_skala_atom_force(3, natom))
     686              :                   CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     687              :                                      vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     688              :                                      edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     689              :                                      just_energy=just_energy_xc, &
     690           60 :                                      native_skala_atom_force=native_skala_atom_force)
     691           60 :                   native_grid_diagnostics = .FALSE.
     692           60 :                   gauxc_section => get_gauxc_section(xc_section)
     693           60 :                   IF (ASSOCIATED(gauxc_section)) THEN
     694              :                      CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_DIAGNOSTICS", &
     695           60 :                                                l_val=native_grid_diagnostics)
     696              :                   END IF
     697           60 :                   IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
     698            0 :                      output_unit = cp_logger_get_default_io_unit()
     699            0 :                      IF (output_unit > 0) THEN
     700            0 :                         DO iatom = 1, natom
     701              :                            WRITE (UNIT=output_unit, FMT="(T2,A,1X,I0,3(1X,ES20.12))") &
     702            0 :                               "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
     703              :                         END DO
     704              :                      END IF
     705              :                   END IF
     706           60 :                   CPASSERT(ASSOCIATED(force))
     707           60 :                   CPASSERT(ASSOCIATED(atomic_kind_set))
     708           60 :                   CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
     709          180 :                   DO iatom = 1, natom
     710          120 :                      ikind = kind_of(iatom)
     711          120 :                      atom_a = atom_of_kind(iatom)
     712              :                      force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
     713          540 :                                                         native_skala_atom_force(:, iatom)
     714              :                   END DO
     715          120 :                   DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
     716              :                ELSE
     717              :                   CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     718              :                                      vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     719              :                                      edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     720          228 :                                      just_energy=just_energy_xc)
     721              :                END IF
     722          288 :                IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
     723          288 :                IF (gapw .OR. gapw_xc) THEN
     724              :                   CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
     725              :                                           xc_section_external=xc_section, &
     726          144 :                                           calculate_forces=calculate_forces)
     727              :                END IF
     728          288 :                IF (edisp /= 0.0_dp) energy%dispersion = edisp
     729          288 :                IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
     730            0 :                   IF (do_kpoints) THEN
     731            0 :                      CALL compute_matrix_vxc_kp(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc_kp=matrix_vxc_kp)
     732            0 :                      CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc_kp)
     733              :                   ELSE
     734            0 :                      CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
     735            0 :                      CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
     736              :                   END IF
     737              :                END IF
     738              :             ELSE
     739          378 :                use_gauxc_matrix = .TRUE.
     740          378 :                CALL apply_gauxc(qs_env, xc_section, calculate_forces)
     741          378 :                IF (gapw_xc .OR. (gapw .AND. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
     742              :                   CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
     743              :                                           xc_section_external=xc_section, &
     744            0 :                                           calculate_forces=calculate_forces)
     745              :                END IF
     746              :             END IF
     747              :          ELSE
     748              :             CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     749              :                                vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     750              :                                edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     751       119603 :                                just_energy=just_energy_xc)
     752       119603 :             IF (edisp /= 0.0_dp) energy%dispersion = edisp
     753       119603 :             IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
     754            2 :                CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
     755            2 :                CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
     756              :             END IF
     757              : 
     758       119603 :             IF (gapw .OR. gapw_xc) THEN
     759              :                CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
     760              :                                        xc_section_external=xc_section, &
     761        24390 :                                        calculate_forces=calculate_forces)
     762              :             END IF
     763              :          END IF
     764              :       END IF
     765              : 
     766              :       ! set hartree and xc potentials for use in Harris method
     767       120269 :       IF (qs_env%harris_method) THEN
     768           80 :          CALL get_qs_env(qs_env, harris_env=harris_env)
     769           80 :          CALL harris_set_potentials(harris_env, v_hartree_rspace, v_rspace_new)
     770              :       END IF
     771              : 
     772       120269 :       NULLIFY (rho_struct)
     773       120269 :       IF (use_virial .AND. calculate_forces) THEN
     774         6188 :          virial%pv_exc = virial%pv_exc - virial%pv_xc
     775         6188 :          virial%pv_virial = virial%pv_virial - virial%pv_xc
     776              :       END IF
     777              : 
     778              :       ! *** Add Hartree-Fock contribution if required ***
     779       120269 :       hfx_sections => section_vals_get_subs_vals(xc_section, "HF")
     780       120269 :       CALL section_vals_get(hfx_sections, explicit=do_hfx)
     781              : 
     782       120269 :       ace_active = .FALSE.
     783       120269 :       ace_rebuild_frequency = 1
     784              : 
     785       120269 :       IF (do_hfx) THEN
     786        28628 :          ace_section => section_vals_get_subs_vals(hfx_sections, "ACE")
     787        28628 :          IF (ASSOCIATED(ace_section)) THEN
     788        28628 :             CALL section_vals_val_get(ace_section, "ACTIVE", l_val=ace_active)
     789        28628 :             CALL section_vals_val_get(ace_section, "REBUILD_FREQUENCY", i_val=ace_rebuild_frequency)
     790              :          END IF
     791              :       END IF
     792              : 
     793       120269 :       IF (do_hfx) THEN
     794        28628 :          IF (dokp) THEN
     795          274 :             IF (ace_active) THEN
     796            0 :                CPABORT("ACE-HFX for k-points is not implemented yet")
     797              :             ELSE
     798          274 :                CALL hfx_ks_matrix_kp(qs_env, ks_matrix, energy, calculate_forces)
     799              :             END IF
     800              : 
     801              :          ELSE
     802              :             ! ext_xc_section may contain a hfx section
     803        28354 :             IF (ace_active) THEN
     804              :                CALL hfx_ace_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
     805              :                                       just_energy, v_rspace_new, v_tau_rspace, &
     806           48 :                                       ace_rebuild_frequency, ext_xc_section=xc_section)
     807              :             ELSE
     808              :                CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
     809        28306 :                                   just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
     810              :             END IF
     811              :          END IF
     812              :       END IF !do_hfx
     813              : 
     814       120269 :       IF (do_ppl .AND. calculate_forces) THEN
     815           12 :          CPASSERT(.NOT. gapw)
     816           26 :          DO ispin = 1, nspins
     817           26 :             CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
     818              :          END DO
     819              :       END IF
     820              : 
     821       120269 :       IF (ASSOCIATED(rho_nlcc) .AND. calculate_forces) THEN
     822           72 :          DO ispin = 1, nspins
     823           36 :             CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
     824           72 :             IF (dft_control%do_admm) CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
     825              :          END DO
     826              :       END IF
     827              : 
     828              :       ! calculate KG correction
     829       120269 :       IF (dft_control%qs_control%do_kg .AND. just_energy) THEN
     830              : 
     831           12 :          CPASSERT(nimages == 1)
     832           12 :          ksmat => ks_matrix(:, 1)
     833           12 :          CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
     834              : 
     835              :          ! subtract kg corr from the total energy
     836           12 :          energy%exc = energy%exc - ekin_mol
     837              : 
     838              :       END IF
     839              : 
     840              :       ! ***  Single atom contributions ***
     841       120269 :       IF (.NOT. just_energy) THEN
     842       110733 :          IF (calculate_forces) THEN
     843              :             ! Getting nuclear force contribution from the core charge density
     844         5957 :             IF ((poisson_env%parameters%solver == pw_poisson_implicit) .AND. &
     845              :                 (poisson_env%parameters%dielectric_params%dielec_core_correction)) THEN
     846           28 :                BLOCK
     847              :                   TYPE(pw_r3d_rs_type) :: v_minus_veps
     848           28 :                   CALL auxbas_pw_pool%create_pw(v_minus_veps)
     849           28 :                   CALL pw_copy(v_hartree_rspace, v_minus_veps)
     850           28 :                   CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
     851           28 :                   CALL integrate_v_core_rspace(v_minus_veps, qs_env)
     852           28 :                   CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
     853              :                END BLOCK
     854              :             ELSE
     855         5929 :                CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
     856              :             END IF
     857              :          END IF
     858              : 
     859       110733 :          IF (.NOT. do_hfx) THEN
     860              :             ! Initialize the Kohn-Sham matrix with the core Hamiltonian matrix
     861              :             ! (sets ks sparsity equal to matrix_h sparsity)
     862       184049 :             DO ispin = 1, nspins
     863       664833 :                DO img = 1, nimages
     864       480784 :                   CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name) ! keep the name
     865       580352 :                   CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
     866              :                END DO
     867              :             END DO
     868              :             ! imaginary part if required
     869        84481 :             IF (qs_env%run_rtp) THEN
     870         2036 :                IF (dft_control%rtp_control%velocity_gauge) THEN
     871          150 :                   CPASSERT(ASSOCIATED(matrix_h_im))
     872          150 :                   CPASSERT(ASSOCIATED(ks_matrix_im))
     873          300 :                   DO ispin = 1, nspins
     874          450 :                      DO img = 1, nimages
     875          150 :                         CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name) ! keep the name
     876          300 :                         CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
     877              :                      END DO
     878              :                   END DO
     879              :                END IF
     880              :             END IF
     881              :          END IF
     882              : 
     883       110733 :          IF (use_virial .AND. calculate_forces) THEN
     884         6188 :             pv_loc = virial%pv_virial
     885              :          END IF
     886              :          ! sum up potentials and integrate
     887              :          ! Pointing my_rho to the density matrix rho_ao
     888       110733 :          my_rho => rho_ao
     889              : 
     890              :          CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
     891              :                                    v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
     892              :                                    v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
     893       110733 :                                    cdft_control, calculate_forces)
     894              : 
     895       110733 :          IF (use_gauxc_matrix) THEN
     896          378 :             IF (dokp) THEN
     897            0 :                CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc_kp)
     898            0 :                CPASSERT(ASSOCIATED(matrix_vxc_kp))
     899            0 :                DO ispin = 1, nspins
     900            0 :                   DO img = 1, nimages
     901              :                      CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc_kp(ispin, img)%matrix, &
     902            0 :                                     1.0_dp, 1.0_dp)
     903              :                   END DO
     904              :                END DO
     905              :             ELSE
     906          378 :                CALL get_qs_env(qs_env=qs_env, matrix_vxc=matrix_vxc)
     907          378 :                CPASSERT(ASSOCIATED(matrix_vxc))
     908          378 :                CPASSERT(nimages == 1)
     909          778 :                DO ispin = 1, nspins
     910          778 :                   CALL dbcsr_add(ks_matrix(ispin, 1)%matrix, matrix_vxc(ispin)%matrix, 1.0_dp, 1.0_dp)
     911              :                END DO
     912              :             END IF
     913              :          END IF
     914              : 
     915       110733 :          IF (gapw .OR. gapw_xc) THEN
     916        23768 :             IF (calculate_forces) THEN
     917          738 :                IF (gapw_xc) THEN
     918          116 :                   CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
     919              :                ELSE
     920          622 :                   CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
     921              :                END IF
     922          738 :                NULLIFY (rho1)
     923          738 :                IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
     924              :                    .NOT. xc_section_uses_native_skala_grid(xc_section)) THEN
     925              :                   ! Molecular GauXC evaluates the XC term outside xc_derivatives.
     926              :                   ! The accurate-XCINT force correction would otherwise try to
     927              :                   ! evaluate the GAUXC section through CP2K's local functional path.
     928              :                   ! Native-grid SKALA can provide this correction through the
     929              :                   ! CP2K grid path and needs it for GAPW_XC force consistency.
     930              :                   CONTINUE
     931              :                ELSE
     932          738 :                   CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
     933              :                END IF
     934              :                !
     935          738 :                IF (dft_control%do_admm) THEN
     936           90 :                   CALL get_qs_env(qs_env, admm_env=admm_env)
     937           90 :                   xc_section => admm_env%xc_section_aux
     938           90 :                   CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
     939           90 :                   vscale = 1.0_dp
     940           90 :                   IF (admm_env%do_admmp) THEN
     941            8 :                      vscale = admm_env%gsi(1)**2
     942           82 :                   ELSE IF (admm_env%do_admms) THEN
     943            6 :                      vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
     944              :                   END IF
     945           90 :                   CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
     946              :                END IF
     947              :             END IF
     948              :          END IF
     949              : 
     950       110733 :          IF (use_virial .AND. calculate_forces) THEN
     951         6188 :             virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
     952              :          END IF
     953       110733 :          IF (dft_control%qs_control%do_kg) THEN
     954          978 :             CPASSERT(nimages == 1)
     955          978 :             ksmat => ks_matrix(:, 1)
     956              : 
     957          978 :             IF (use_virial .AND. calculate_forces) THEN
     958          208 :                pv_loc = virial%pv_virial
     959              :             END IF
     960              : 
     961          978 :             CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
     962              :             ! subtract kg corr from the total energy
     963          978 :             energy%exc = energy%exc - ekin_mol
     964              : 
     965              :             ! virial corrections
     966          978 :             IF (use_virial .AND. calculate_forces) THEN
     967              : 
     968              :                ! Integral contribution
     969          208 :                virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
     970              : 
     971              :                ! GGA contribution
     972          208 :                virial%pv_exc = virial%pv_exc + virial%pv_xc
     973          208 :                virial%pv_virial = virial%pv_virial + virial%pv_xc
     974          208 :                virial%pv_xc = 0.0_dp
     975              :             END IF
     976              :          END IF
     977              : 
     978              :       ELSE
     979              :          IF (do_hfx) THEN
     980              :             IF (.FALSE.) THEN
     981              :                CPWARN("KS matrix no longer correct. Check possible problems with property calculations!")
     982              :             END IF
     983              :          END IF
     984              :       END IF ! .NOT. just energy
     985       120269 :       IF (dft_control%qs_control%ddapc_explicit_potential) THEN
     986          164 :          CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
     987          164 :          DEALLOCATE (v_spin_ddapc_rest_r)
     988              :       END IF
     989              : 
     990       120269 :       IF (calculate_forces .AND. dft_control%qs_control%cdft) THEN
     991          118 :          IF (.NOT. cdft_control%transfer_pot) THEN
     992          212 :             DO iatom = 1, SIZE(cdft_control%group)
     993          114 :                CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
     994          212 :                DEALLOCATE (cdft_control%group(iatom)%weight)
     995              :             END DO
     996           98 :             IF (cdft_control%atomic_charges) THEN
     997           78 :                DO iatom = 1, cdft_control%natoms
     998           78 :                   CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
     999              :                END DO
    1000           26 :                DEALLOCATE (cdft_control%charge)
    1001              :             END IF
    1002           98 :             IF (cdft_control%type == outer_scf_becke_constraint .AND. &
    1003              :                 cdft_control%becke_control%cavity_confine) THEN
    1004           88 :                IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
    1005           64 :                   CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
    1006              :                ELSE
    1007           24 :                   DEALLOCATE (cdft_control%becke_control%cavity_mat)
    1008              :                END IF
    1009           10 :             ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
    1010            2 :                IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
    1011            0 :                   CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
    1012              :                END IF
    1013              :             END IF
    1014           98 :             IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
    1015           98 :             cdft_control%save_pot = .FALSE.
    1016           98 :             cdft_control%need_pot = .TRUE.
    1017           98 :             cdft_control%external_control = .FALSE.
    1018              :          END IF
    1019              :       END IF
    1020              : 
    1021       120269 :       IF (dft_control%do_sccs) THEN
    1022          160 :          CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
    1023          160 :          DEALLOCATE (v_sccs_rspace)
    1024              :       END IF
    1025              : 
    1026       120269 :       IF (gapw) THEN
    1027        20504 :          IF (dft_control%apply_external_potential) THEN
    1028              :             ! Integrals of the Hartree potential with g0_soft
    1029              :             CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
    1030           42 :                                          v_qmmm=vee, scale=-1.0_dp)
    1031              :          END IF
    1032        20504 :          CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
    1033              :          ! Place Vh_1c_gg_integrals after integrate_vhg0_rspace for CNEO calculations
    1034              :          ! because vhg0 integral is needed to build the complete nuclear equation
    1035        20504 :          CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
    1036              :          CALL Vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.FALSE., &
    1037        20504 :                                  core_2nd=.FALSE.)
    1038              :          ! CNEO quantum nuclear core energy (kinetic + Z*erfc(r)/r potential from classical nuclei)
    1039        20504 :          energy%core_cneo = 0.0_dp
    1040        20504 :          IF (ASSOCIATED(local_rho_set%rhoz_cneo_set)) THEN
    1041          184 :             DO iatom = 1, SIZE(local_rho_set%rhoz_cneo_set)
    1042          184 :                energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
    1043              :             END DO
    1044              :          END IF
    1045              :       END IF
    1046              : 
    1047       120269 :       IF (gapw .OR. gapw_xc) THEN
    1048              :          ! Single atom contributions in the KS matrix ***
    1049        24544 :          CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
    1050        24544 :          IF (dft_control%do_admm) THEN
    1051              :             !Single atom contribution to the AUX matrices
    1052              :             !Note: also update ks_aux_fit matrix in case of rtp
    1053         4394 :             CALL admm_update_ks_atom(qs_env, calculate_forces)
    1054              :          END IF
    1055              :       END IF
    1056              : 
    1057              :       !Calculation of Mulliken restraint, if requested
    1058              :       CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
    1059       120269 :                                     ks_matrix, matrix_s, rho, mulliken_order_p)
    1060              : 
    1061              :       ! Add DFT+U contribution, if requested
    1062       120269 :       IF (dft_control%dft_plus_u) THEN
    1063         1768 :          IF (just_energy) THEN
    1064          616 :             CALL plus_u(qs_env=qs_env)
    1065              :          ELSE
    1066         1152 :             CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
    1067              :          END IF
    1068              :       ELSE
    1069       118501 :          energy%dft_plus_u = 0.0_dp
    1070              :       END IF
    1071              : 
    1072              :       ! At this point the ks matrix should be up to date, filter it if requested
    1073       264255 :       DO ispin = 1, nspins
    1074       803587 :          DO img = 1, nimages
    1075              :             CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
    1076       683318 :                               dft_control%qs_control%eps_filter_matrix)
    1077              :          END DO
    1078              :       END DO
    1079              : 
    1080              :       !** merge the auxiliary KS matrix and the primary one
    1081       120269 :       IF (dft_control%do_admm_mo) THEN
    1082        12806 :          IF (qs_env%run_rtp) THEN
    1083           92 :             CALL rtp_admm_merge_ks_matrix(qs_env)
    1084              :          ELSE
    1085        12714 :             CALL admm_mo_merge_ks_matrix(qs_env)
    1086              :          END IF
    1087       107463 :       ELSEIF (dft_control%do_admm_dm) THEN
    1088          214 :          CALL admm_dm_merge_ks_matrix(qs_env)
    1089              :       END IF
    1090              : 
    1091              :       ! External field (nonperiodic case)
    1092       120269 :       CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
    1093              : 
    1094              :       ! Right now we can compute the orbital derivative here, as it depends currently only on the available
    1095              :       ! Kohn-Sham matrix. This might change in the future, in which case more pieces might need to be assembled
    1096              :       ! from this routine, notice that this part of the calculation in not linear scaling
    1097              :       ! right now this operation is only non-trivial because of occupation numbers and the restricted keyword
    1098       120269 :       IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp) THEN
    1099        44027 :          CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
    1100        44027 :          CPASSERT(nimages == 1)
    1101        44027 :          ksmat => ks_matrix(:, 1)
    1102        44027 :          CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
    1103              :       END IF
    1104              : 
    1105              :       ! ADMM overlap forces
    1106       120269 :       IF (calculate_forces .AND. dft_control%do_admm) THEN
    1107          316 :          IF (dokp) THEN
    1108           30 :             CALL calc_admm_ovlp_forces_kp(qs_env)
    1109              :          ELSE
    1110          286 :             CALL calc_admm_ovlp_forces(qs_env)
    1111              :          END IF
    1112              :       END IF
    1113              : 
    1114              :       ! deal with low spin roks
    1115              :       CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
    1116       120269 :                          calculate_forces, auxbas_pw_pool)
    1117              : 
    1118              :       ! deal with sic on explicit orbitals
    1119              :       CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
    1120       120269 :                                  calculate_forces, auxbas_pw_pool)
    1121              : 
    1122              :       ! Periodic external field
    1123       120269 :       CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
    1124              : 
    1125              :       ! adds s2_restraint energy and orbital derivatives
    1126              :       CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
    1127       120269 :                               energy, calculate_forces, just_energy)
    1128              : 
    1129       120269 :       IF (do_ppl) THEN
    1130              :          ! update core energy for grid based local pseudopotential
    1131           60 :          ecore_ppl = 0._dp
    1132          126 :          DO ispin = 1, nspins
    1133          126 :             ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
    1134              :          END DO
    1135           60 :          energy%core = energy%core + ecore_ppl
    1136              :       END IF
    1137              : 
    1138       120269 :       IF (lrigpw) THEN
    1139              :          ! update core energy for ppl_ri method
    1140          432 :          CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
    1141          432 :          IF (lri_env%ppl_ri) THEN
    1142            8 :             ecore_ppl = 0._dp
    1143           16 :             DO ispin = 1, nspins
    1144            8 :                lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
    1145           16 :                CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
    1146              :             END DO
    1147            8 :             energy%core = energy%core + ecore_ppl
    1148              :          END IF
    1149              :       END IF
    1150              : 
    1151              :       ! Sum all energy terms to obtain the total energy
    1152              :       energy%total = energy%core_overlap + energy%core_self + energy%core_cneo + energy%core + &
    1153              :                      energy%hartree + energy%hartree_1c + energy%exc + energy%exc1 + energy%ex + &
    1154              :                      energy%dispersion + energy%gcp + energy%qmmm_el + energy%mulliken + &
    1155              :                      SUM(energy%ddapc_restraint) + energy%s2_restraint + &
    1156              :                      energy%dft_plus_u + energy%kTS + &
    1157              :                      energy%efield + energy%efield_core + energy%ee + &
    1158              :                      energy%ee_core + energy%exc_aux_fit + energy%image_charge + &
    1159       240642 :                      energy%sccs_pol + energy%cdft + energy%exc1_aux_fit
    1160              : 
    1161       120269 :       IF (dft_control%apply_embed_pot) energy%total = energy%total + energy%embed_corr
    1162              : 
    1163       120269 :       IF (native_skala_restore_exc) energy%total = native_skala_total_scf
    1164              : 
    1165       120269 :       IF (abnormal_value(energy%total)) &
    1166            0 :          CPABORT("KS energy is an abnormal value (NaN/Inf).")
    1167              : 
    1168              :       ! Print detailed energy
    1169       120269 :       IF (my_print) THEN
    1170       120247 :          CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
    1171              :       END IF
    1172              : 
    1173       120269 :       CALL timestop(handle)
    1174              : 
    1175       240538 :    END SUBROUTINE qs_ks_build_kohn_sham_matrix
    1176              : 
    1177              : ! **************************************************************************************************
    1178              : !> \brief ...
    1179              : !> \param rho_tot_gspace ...
    1180              : !> \param qs_env ...
    1181              : !> \param rho ...
    1182              : !> \param skip_nuclear_density ...
    1183              : ! **************************************************************************************************
    1184       123995 :    SUBROUTINE calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
    1185              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                :: rho_tot_gspace
    1186              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1187              :       TYPE(qs_rho_type), POINTER                         :: rho
    1188              :       LOGICAL, INTENT(IN), OPTIONAL                      :: skip_nuclear_density
    1189              : 
    1190              :       INTEGER                                            :: ispin
    1191              :       LOGICAL                                            :: my_skip
    1192              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1193       123995 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
    1194              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho0_s_gs, rho_core, rhoz_cneo_s_gs
    1195              :       TYPE(qs_charges_type), POINTER                     :: qs_charges
    1196              : 
    1197       123995 :       my_skip = .FALSE.
    1198          930 :       IF (PRESENT(skip_nuclear_density)) my_skip = skip_nuclear_density
    1199              : 
    1200       123995 :       CALL qs_rho_get(rho, rho_g=rho_g)
    1201       123995 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
    1202              : 
    1203       123995 :       IF (.NOT. my_skip) THEN
    1204       123075 :          NULLIFY (rho_core)
    1205       123075 :          CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
    1206       123075 :          IF (dft_control%qs_control%gapw) THEN
    1207        20824 :             NULLIFY (rho0_s_gs, rhoz_cneo_s_gs)
    1208        20824 :             CALL get_qs_env(qs_env=qs_env, rho0_s_gs=rho0_s_gs, rhoz_cneo_s_gs=rhoz_cneo_s_gs)
    1209        20824 :             CPASSERT(ASSOCIATED(rho0_s_gs))
    1210        20824 :             CALL pw_copy(rho0_s_gs, rho_tot_gspace)
    1211        20824 :             IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
    1212           48 :                CALL pw_axpy(rhoz_cneo_s_gs, rho_tot_gspace)
    1213              :             END IF
    1214        20824 :             IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
    1215         1696 :                CALL pw_axpy(rho_core, rho_tot_gspace)
    1216              :             END IF
    1217              :          ELSE
    1218       102251 :             CALL pw_copy(rho_core, rho_tot_gspace)
    1219              :          END IF
    1220       270177 :          DO ispin = 1, dft_control%nspins
    1221       270177 :             CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
    1222              :          END DO
    1223       123075 :          CALL get_qs_env(qs_env=qs_env, qs_charges=qs_charges)
    1224       123075 :          qs_charges%total_rho_gspace = pw_integrate_function(rho_tot_gspace, isign=-1)
    1225              :       ELSE
    1226         1844 :          DO ispin = 1, dft_control%nspins
    1227         1844 :             CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
    1228              :          END DO
    1229              :       END IF
    1230              : 
    1231       123995 :    END SUBROUTINE calc_rho_tot_gspace
    1232              : 
    1233              : ! **************************************************************************************************
    1234              : !> \brief compute MO derivatives
    1235              : !> \param qs_env the qs_env to update
    1236              : !> \param ks_matrix ...
    1237              : !> \param mo_derivs ...
    1238              : !> \par History
    1239              : !>      01.2014 created, transferred from qs_ks_build_kohn_sham_matrix in
    1240              : !>      separate subroutine
    1241              : !> \author Dorothea Golze
    1242              : ! **************************************************************************************************
    1243        44027 :    SUBROUTINE calc_mo_derivatives(qs_env, ks_matrix, mo_derivs)
    1244              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1245              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_matrix, mo_derivs
    1246              : 
    1247              :       INTEGER                                            :: ispin
    1248              :       LOGICAL                                            :: uniform_occupation
    1249        44027 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: occupation_numbers
    1250              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1251              :       TYPE(dbcsr_type)                                   :: mo_derivs2_tmp1, mo_derivs2_tmp2
    1252              :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_b
    1253              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1254        44027 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
    1255              : 
    1256        44027 :       NULLIFY (dft_control, mo_array, mo_coeff, mo_coeff_b, occupation_numbers)
    1257              : 
    1258              :       CALL get_qs_env(qs_env, &
    1259              :                       dft_control=dft_control, &
    1260        44027 :                       mos=mo_array)
    1261              : 
    1262        95989 :       DO ispin = 1, SIZE(mo_derivs)
    1263              : 
    1264              :          CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, &
    1265        51962 :                          mo_coeff_b=mo_coeff_b, occupation_numbers=occupation_numbers)
    1266              :          CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin)%matrix, mo_coeff_b, &
    1267        51962 :                              0.0_dp, mo_derivs(ispin)%matrix)
    1268              : 
    1269        95989 :          IF (dft_control%restricted) THEN
    1270              :             ! only the first mo_set are actual variables, but we still need both
    1271          636 :             CPASSERT(ispin == 1)
    1272          636 :             CPASSERT(SIZE(mo_array) == 2)
    1273              :             ! use a temporary array with the same size as the first spin for the second spin
    1274              : 
    1275              :             ! uniform_occupation is needed for this case, otherwise we can not
    1276              :             ! reconstruct things in ot, since we irreversibly sum
    1277          636 :             CALL get_mo_set(mo_set=mo_array(1), uniform_occupation=uniform_occupation)
    1278          636 :             CPASSERT(uniform_occupation)
    1279          636 :             CALL get_mo_set(mo_set=mo_array(2), uniform_occupation=uniform_occupation)
    1280          636 :             CPASSERT(uniform_occupation)
    1281              : 
    1282              :             ! The beta-spin might have fewer orbitals than alpa-spin...
    1283              :             ! create temporary matrices with beta_nmo columns
    1284          636 :             CALL get_mo_set(mo_set=mo_array(2), mo_coeff_b=mo_coeff_b)
    1285          636 :             CALL dbcsr_create(mo_derivs2_tmp1, template=mo_coeff_b)
    1286              : 
    1287              :             ! calculate beta derivatives
    1288          636 :             CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(2)%matrix, mo_coeff_b, 0.0_dp, mo_derivs2_tmp1)
    1289              : 
    1290              :             ! create larger matrix with alpha_nmo columns
    1291          636 :             CALL dbcsr_create(mo_derivs2_tmp2, template=mo_derivs(1)%matrix)
    1292          636 :             CALL dbcsr_set(mo_derivs2_tmp2, 0.0_dp)
    1293              : 
    1294              :             ! copy into larger matrix, fills the first beta_nmo columns
    1295              :             CALL dbcsr_copy_columns_hack(mo_derivs2_tmp2, mo_derivs2_tmp1, &
    1296              :                                          mo_array(2)%nmo, 1, 1, &
    1297              :                                          para_env=mo_array(1)%mo_coeff%matrix_struct%para_env, &
    1298          636 :                                          blacs_env=mo_array(1)%mo_coeff%matrix_struct%context)
    1299              : 
    1300              :             ! add beta contribution to alpa mo_derivs
    1301          636 :             CALL dbcsr_add(mo_derivs(1)%matrix, mo_derivs2_tmp2, 1.0_dp, 1.0_dp)
    1302          636 :             CALL dbcsr_release(mo_derivs2_tmp1)
    1303          636 :             CALL dbcsr_release(mo_derivs2_tmp2)
    1304              :          END IF
    1305              :       END DO
    1306              : 
    1307        44027 :       IF (dft_control%do_admm_mo) THEN
    1308         6390 :          CALL calc_admm_mo_derivatives(qs_env, mo_derivs)
    1309              :       END IF
    1310              : 
    1311        44027 :    END SUBROUTINE calc_mo_derivatives
    1312              : 
    1313              : ! **************************************************************************************************
    1314              : !> \brief updates the Kohn Sham matrix of the given qs_env (facility method)
    1315              : !> \param qs_env the qs_env to update
    1316              : !> \param calculate_forces if true calculate the quantities needed
    1317              : !>        to calculate the forces. Defaults to false.
    1318              : !> \param just_energy if true updates the energies but not the
    1319              : !>        ks matrix. Defaults to false
    1320              : !> \param print_active ...
    1321              : !> \par History
    1322              : !>      4.2002 created [fawzi]
    1323              : !>      8.2014 kpoints [JGH]
    1324              : !>     10.2014 refractored [Ole Schuett]
    1325              : !> \author Fawzi Mohamed
    1326              : ! **************************************************************************************************
    1327       259584 :    SUBROUTINE qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, &
    1328              :                                   print_active)
    1329              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1330              :       LOGICAL, INTENT(IN), OPTIONAL                      :: calculate_forces, just_energy, &
    1331              :                                                             print_active
    1332              : 
    1333              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_update_qs_env'
    1334              : 
    1335              :       INTEGER                                            :: handle, unit_nr
    1336              :       LOGICAL                                            :: c_forces, do_rebuild, energy_only, &
    1337              :                                                             forces_up_to_date, potential_changed, &
    1338              :                                                             rho_changed, s_mstruct_changed
    1339              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1340              : 
    1341       259584 :       NULLIFY (ks_env)
    1342       259584 :       unit_nr = cp_logger_get_default_io_unit()
    1343              : 
    1344       259584 :       c_forces = .FALSE.
    1345       259584 :       energy_only = .FALSE.
    1346       259584 :       IF (PRESENT(just_energy)) energy_only = just_energy
    1347       259584 :       IF (PRESENT(calculate_forces)) c_forces = calculate_forces
    1348              : 
    1349       259584 :       IF (c_forces) THEN
    1350        10471 :          CALL timeset(routineN//'_forces', handle)
    1351              :       ELSE
    1352       249113 :          CALL timeset(routineN, handle)
    1353              :       END IF
    1354              : 
    1355       259584 :       CPASSERT(ASSOCIATED(qs_env))
    1356              : 
    1357              :       CALL get_qs_env(qs_env, &
    1358              :                       ks_env=ks_env, &
    1359              :                       rho_changed=rho_changed, &
    1360              :                       s_mstruct_changed=s_mstruct_changed, &
    1361              :                       potential_changed=potential_changed, &
    1362       259584 :                       forces_up_to_date=forces_up_to_date)
    1363              : 
    1364       259584 :       do_rebuild = .FALSE.
    1365       259584 :       do_rebuild = do_rebuild .OR. rho_changed
    1366         8551 :       do_rebuild = do_rebuild .OR. s_mstruct_changed
    1367         8551 :       do_rebuild = do_rebuild .OR. potential_changed
    1368         8551 :       do_rebuild = do_rebuild .OR. (c_forces .AND. .NOT. forces_up_to_date)
    1369              : 
    1370              :       IF (do_rebuild) THEN
    1371       251415 :          CALL evaluate_core_matrix_traces(qs_env)
    1372              : 
    1373              :          ! the ks matrix will be rebuilt so this is fine now
    1374       251415 :          CALL set_ks_env(ks_env, potential_changed=.FALSE.)
    1375              : 
    1376              :          CALL rebuild_ks_matrix(qs_env, &
    1377              :                                 calculate_forces=c_forces, &
    1378              :                                 just_energy=energy_only, &
    1379       251415 :                                 print_active=print_active)
    1380              : 
    1381       251415 :          IF (.NOT. energy_only) THEN
    1382              :             CALL set_ks_env(ks_env, &
    1383              :                             rho_changed=.FALSE., &
    1384              :                             s_mstruct_changed=.FALSE., &
    1385       462859 :                             forces_up_to_date=forces_up_to_date .OR. c_forces)
    1386              :          END IF
    1387              :       END IF
    1388              : 
    1389       259584 :       CALL timestop(handle)
    1390              : 
    1391       259584 :    END SUBROUTINE qs_ks_update_qs_env
    1392              : 
    1393              : ! **************************************************************************************************
    1394              : !> \brief Calculates the traces of the core matrices and the density matrix.
    1395              : !> \param qs_env ...
    1396              : !> \param rho_ao_ext ...
    1397              : !> \author Ole Schuett
    1398              : ! **************************************************************************************************
    1399       274757 :    SUBROUTINE evaluate_core_matrix_traces(qs_env, rho_ao_ext)
    1400              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1401              :       TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
    1402              :          POINTER                                         :: rho_ao_ext
    1403              : 
    1404              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_core_matrix_traces'
    1405              : 
    1406              :       INTEGER                                            :: handle
    1407              :       REAL(KIND=dp)                                      :: energy_core_im
    1408       274757 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrixkp_h, matrixkp_t, rho_ao_kp
    1409              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1410              :       TYPE(qs_energy_type), POINTER                      :: energy
    1411              :       TYPE(qs_rho_type), POINTER                         :: rho
    1412              : 
    1413       274757 :       CALL timeset(routineN, handle)
    1414       274757 :       NULLIFY (energy, rho, dft_control, rho_ao_kp, matrixkp_t, matrixkp_h)
    1415              : 
    1416              :       CALL get_qs_env(qs_env, &
    1417              :                       rho=rho, &
    1418              :                       energy=energy, &
    1419              :                       dft_control=dft_control, &
    1420              :                       kinetic_kp=matrixkp_t, &
    1421       274757 :                       matrix_h_kp=matrixkp_h)
    1422              : 
    1423       274757 :       IF (PRESENT(rho_ao_ext)) THEN
    1424        23266 :          rho_ao_kp => rho_ao_ext
    1425              :       ELSE
    1426       251491 :          CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
    1427              :       END IF
    1428              : 
    1429       274757 :       CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy%core, dft_control%nspins)
    1430              : 
    1431              :       ! Add the imaginary part in the RTP case
    1432       274757 :       IF (qs_env%run_rtp) THEN
    1433         3220 :          IF (dft_control%rtp_control%velocity_gauge) THEN
    1434          150 :             CALL get_qs_env(qs_env, matrix_h_im_kp=matrixkp_h)
    1435          150 :             CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_kp)
    1436          150 :             CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy_core_im, dft_control%nspins)
    1437          150 :             energy%core = energy%core - energy_core_im
    1438              :          END IF
    1439              :       END IF
    1440              : 
    1441              :       ! kinetic energy
    1442       274757 :       IF (ASSOCIATED(matrixkp_t)) &
    1443       120045 :          CALL calculate_ptrace(matrixkp_t, rho_ao_kp, energy%kinetic, dft_control%nspins)
    1444              : 
    1445       274757 :       CALL timestop(handle)
    1446       274757 :    END SUBROUTINE evaluate_core_matrix_traces
    1447              : 
    1448              : ! **************************************************************************************************
    1449              : !> \brief Constructs a new Khon-Sham matrix
    1450              : !> \param qs_env ...
    1451              : !> \param calculate_forces ...
    1452              : !> \param just_energy ...
    1453              : !> \param print_active ...
    1454              : !> \author Ole Schuett
    1455              : ! **************************************************************************************************
    1456       251435 :    SUBROUTINE rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
    1457              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1458              :       LOGICAL, INTENT(IN)                                :: calculate_forces, just_energy
    1459              :       LOGICAL, INTENT(IN), OPTIONAL                      :: print_active
    1460              : 
    1461              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'rebuild_ks_matrix'
    1462              : 
    1463              :       INTEGER                                            :: handle
    1464              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1465              : 
    1466       251435 :       CALL timeset(routineN, handle)
    1467       251435 :       NULLIFY (dft_control)
    1468              : 
    1469       251435 :       CALL get_qs_env(qs_env, dft_control=dft_control)
    1470              : 
    1471       251435 :       IF (dft_control%qs_control%semi_empirical) THEN
    1472              :          CALL build_se_fock_matrix(qs_env, &
    1473              :                                    calculate_forces=calculate_forces, &
    1474        41374 :                                    just_energy=just_energy)
    1475              : 
    1476       210061 :       ELSEIF (dft_control%qs_control%dftb) THEN
    1477              :          CALL build_dftb_ks_matrix(qs_env, &
    1478              :                                    calculate_forces=calculate_forces, &
    1479        29478 :                                    just_energy=just_energy)
    1480              : 
    1481       180583 :       ELSEIF (dft_control%qs_control%xtb) THEN
    1482        60526 :          IF (dft_control%qs_control%xtb_control%do_tblite) THEN
    1483              :             CALL build_tblite_ks_matrix(qs_env, &
    1484              :                                         calculate_forces=calculate_forces, &
    1485        25666 :                                         just_energy=just_energy)
    1486              :          ELSE
    1487              :             CALL build_xtb_ks_matrix(qs_env, &
    1488              :                                      calculate_forces=calculate_forces, &
    1489        34860 :                                      just_energy=just_energy)
    1490              :          END IF
    1491              :       ELSE
    1492              :          CALL qs_ks_build_kohn_sham_matrix(qs_env, &
    1493              :                                            calculate_forces=calculate_forces, &
    1494              :                                            just_energy=just_energy, &
    1495       120057 :                                            print_active=print_active)
    1496              :       END IF
    1497              : 
    1498       251435 :       CALL timestop(handle)
    1499              : 
    1500       251435 :    END SUBROUTINE rebuild_ks_matrix
    1501              : 
    1502              : ! **************************************************************************************************
    1503              : !> \brief Allocate ks_matrix if necessary, take current overlap matrix as template
    1504              : !> \param qs_env ...
    1505              : !> \param is_complex ...
    1506              : !> \par History
    1507              : !>    refactoring 04.03.2011 [MI]
    1508              : !> \author
    1509              : ! **************************************************************************************************
    1510              : 
    1511        27696 :    SUBROUTINE qs_ks_allocate_basics(qs_env, is_complex)
    1512              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1513              :       LOGICAL, INTENT(in)                                :: is_complex
    1514              : 
    1515              :       CHARACTER(LEN=default_string_length)               :: headline
    1516              :       INTEGER                                            :: ic, ispin, nimages, nspins
    1517              :       LOGICAL                                            :: do_kpoints
    1518        27696 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_kp, matrixkp_im_ks, matrixkp_ks
    1519              :       TYPE(dbcsr_type), POINTER                          :: refmatrix
    1520              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1521              :       TYPE(kpoint_type), POINTER                         :: kpoints
    1522              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1523        27696 :          POINTER                                         :: sab_orb
    1524              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1525              : 
    1526        27696 :       NULLIFY (dft_control, ks_env, matrix_s_kp, sab_orb, matrixkp_ks, refmatrix, matrixkp_im_ks, kpoints)
    1527              : 
    1528              :       CALL get_qs_env(qs_env, &
    1529              :                       dft_control=dft_control, &
    1530              :                       matrix_s_kp=matrix_s_kp, &
    1531              :                       ks_env=ks_env, &
    1532              :                       kpoints=kpoints, &
    1533              :                       do_kpoints=do_kpoints, &
    1534              :                       matrix_ks_kp=matrixkp_ks, &
    1535        27696 :                       matrix_ks_im_kp=matrixkp_im_ks)
    1536              : 
    1537        27696 :       IF (do_kpoints) THEN
    1538         3174 :          CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
    1539              :       ELSE
    1540        24522 :          CALL get_qs_env(qs_env, sab_orb=sab_orb)
    1541              :       END IF
    1542              : 
    1543        27696 :       nspins = dft_control%nspins
    1544        27696 :       nimages = dft_control%nimages
    1545              : 
    1546        27696 :       IF (.NOT. ASSOCIATED(matrixkp_ks)) THEN
    1547        27654 :          CALL dbcsr_allocate_matrix_set(matrixkp_ks, nspins, nimages)
    1548        27654 :          refmatrix => matrix_s_kp(1, 1)%matrix
    1549        58804 :          DO ispin = 1, nspins
    1550       284320 :             DO ic = 1, nimages
    1551       225516 :                IF (nspins > 1) THEN
    1552        28060 :                   IF (ispin == 1) THEN
    1553        14030 :                      headline = "KOHN-SHAM MATRIX FOR ALPHA SPIN"
    1554              :                   ELSE
    1555        14030 :                      headline = "KOHN-SHAM MATRIX FOR BETA SPIN"
    1556              :                   END IF
    1557              :                ELSE
    1558       197456 :                   headline = "KOHN-SHAM MATRIX"
    1559              :                END IF
    1560       225516 :                ALLOCATE (matrixkp_ks(ispin, ic)%matrix)
    1561              :                CALL dbcsr_create(matrix=matrixkp_ks(ispin, ic)%matrix, template=refmatrix, &
    1562       225516 :                                  name=TRIM(headline), matrix_type=dbcsr_type_symmetric)
    1563       225516 :                CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_ks(ispin, ic)%matrix, sab_orb)
    1564       256666 :                CALL dbcsr_set(matrixkp_ks(ispin, ic)%matrix, 0.0_dp)
    1565              :             END DO
    1566              :          END DO
    1567        27654 :          CALL set_ks_env(ks_env, matrix_ks_kp=matrixkp_ks)
    1568              :       END IF
    1569              : 
    1570        27696 :       IF (is_complex) THEN
    1571          144 :          IF (.NOT. ASSOCIATED(matrixkp_im_ks)) THEN
    1572          144 :             CPASSERT(nspins == SIZE(matrixkp_ks, 1))
    1573          144 :             CPASSERT(nimages == SIZE(matrixkp_ks, 2))
    1574          144 :             CALL dbcsr_allocate_matrix_set(matrixkp_im_ks, nspins, nimages)
    1575          306 :             DO ispin = 1, nspins
    1576          468 :                DO ic = 1, nimages
    1577          162 :                   IF (nspins > 1) THEN
    1578           36 :                      IF (ispin == 1) THEN
    1579           18 :                         headline = "IMAGINARY KOHN-SHAM MATRIX FOR ALPHA SPIN"
    1580              :                      ELSE
    1581           18 :                         headline = "IMAGINARY KOHN-SHAM MATRIX FOR BETA SPIN"
    1582              :                      END IF
    1583              :                   ELSE
    1584          126 :                      headline = "IMAGINARY KOHN-SHAM MATRIX"
    1585              :                   END IF
    1586          162 :                   ALLOCATE (matrixkp_im_ks(ispin, ic)%matrix)
    1587          162 :                   refmatrix => matrixkp_ks(ispin, ic)%matrix  ! base on real part, but anti-symmetric
    1588              :                   CALL dbcsr_create(matrix=matrixkp_im_ks(ispin, ic)%matrix, template=refmatrix, &
    1589          162 :                                     name=TRIM(headline), matrix_type=dbcsr_type_antisymmetric)
    1590          162 :                   CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_im_ks(ispin, ic)%matrix, sab_orb)
    1591          324 :                   CALL dbcsr_set(matrixkp_im_ks(ispin, ic)%matrix, 0.0_dp)
    1592              :                END DO
    1593              :             END DO
    1594          144 :             CALL set_ks_env(ks_env, matrix_ks_im_kp=matrixkp_im_ks)
    1595              :          END IF
    1596              :       END IF
    1597              : 
    1598        27696 :    END SUBROUTINE qs_ks_allocate_basics
    1599              : 
    1600              : END MODULE qs_ks_methods
        

Generated by: LCOV version 2.0-1