LCOV - code coverage report
Current view: top level - src - qs_ks_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 92.9 % 774 719
Test Date: 2026-11-24 01:22:08 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 gapw_gspace_reconstruction,      ONLY: integrate_gapw_composite_vxc_gspace
      55              :    USE gce_methods,                     ONLY: planar_averaged_v_hartree_3d,&
      56              :                                               planar_counter_charge
      57              :    USE hartree_local_methods,           ONLY: Vh_1c_gg_integrals
      58              :    USE hartree_local_types,             ONLY: ecoul_1center_type
      59              :    USE hfx_ace_methods,                 ONLY: hfx_ace_ks_matrix
      60              :    USE hfx_admm_utils,                  ONLY: hfx_admm_init,&
      61              :                                               hfx_ks_matrix,&
      62              :                                               hfx_ks_matrix_kp
      63              :    USE input_constants,                 ONLY: do_ppl_grid,&
      64              :                                               outer_scf_becke_constraint,&
      65              :                                               outer_scf_hirshfeld_constraint,&
      66              :                                               smeagol_runtype_emtransport
      67              :    USE input_section_types,             ONLY: section_vals_get,&
      68              :                                               section_vals_get_subs_vals,&
      69              :                                               section_vals_type,&
      70              :                                               section_vals_val_get
      71              :    USE kg_correction,                   ONLY: kg_ekin_subset
      72              :    USE kinds,                           ONLY: default_string_length,&
      73              :                                               dp
      74              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      75              :                                               kpoint_type
      76              :    USE lri_environment_methods,         ONLY: v_int_ppl_energy
      77              :    USE lri_environment_types,           ONLY: lri_density_type,&
      78              :                                               lri_environment_type,&
      79              :                                               lri_kind_type
      80              :    USE mathlib,                         ONLY: abnormal_value
      81              :    USE message_passing,                 ONLY: mp_para_env_type
      82              :    USE particle_types,                  ONLY: particle_type
      83              :    USE pw_env_types,                    ONLY: pw_env_get,&
      84              :                                               pw_env_type
      85              :    USE pw_methods,                      ONLY: pw_axpy,&
      86              :                                               pw_copy,&
      87              :                                               pw_integral_ab,&
      88              :                                               pw_integrate_function,&
      89              :                                               pw_scale,&
      90              :                                               pw_transfer,&
      91              :                                               pw_zero
      92              :    USE pw_poisson_methods,              ONLY: pw_poisson_solve
      93              :    USE pw_poisson_types,                ONLY: pw_poisson_implicit,&
      94              :                                               pw_poisson_type
      95              :    USE pw_pool_types,                   ONLY: pw_pool_type
      96              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      97              :                                               pw_r3d_rs_type
      98              :    USE qmmm_image_charge,               ONLY: add_image_pot_to_hartree_pot,&
      99              :                                               calculate_image_pot,&
     100              :                                               integrate_potential_devga_rspace
     101              :    USE qs_cdft_types,                   ONLY: cdft_control_type
     102              :    USE qs_cdft_utils,                   ONLY: cdft_constraint_print
     103              :    USE qs_charges_types,                ONLY: qs_charges_type
     104              :    USE qs_core_energies,                ONLY: calculate_ptrace
     105              :    USE qs_dftb_matrices,                ONLY: build_dftb_ks_matrix
     106              :    USE qs_efield_berry,                 ONLY: qs_efield_berry_phase
     107              :    USE qs_efield_local,                 ONLY: qs_efield_local_operator
     108              :    USE qs_energy_types,                 ONLY: qs_energy_type
     109              :    USE qs_environment_types,            ONLY: get_qs_env,&
     110              :                                               qs_environment_type
     111              :    USE qs_force_types,                  ONLY: qs_force_type
     112              :    USE qs_gapw_densities,               ONLY: prepare_gapw_den
     113              :    USE qs_harris_types,                 ONLY: harris_type
     114              :    USE qs_harris_utils,                 ONLY: harris_set_potentials
     115              :    USE qs_integrate_potential,          ONLY: integrate_ppl_rspace,&
     116              :                                               integrate_rho_nlcc,&
     117              :                                               integrate_v_core_rspace,&
     118              :                                               integrate_v_rspace
     119              :    USE qs_kind_types,                   ONLY: qs_kind_type
     120              :    USE qs_ks_apply_restraints,          ONLY: qs_ks_cdft_constraint,&
     121              :                                               qs_ks_mulliken_restraint,&
     122              :                                               qs_ks_s2_restraint
     123              :    USE qs_ks_atom,                      ONLY: update_ks_atom
     124              :    USE qs_ks_qmmm_methods,              ONLY: qmmm_calculate_energy,&
     125              :                                               qmmm_modify_hartree_pot
     126              :    USE qs_ks_types,                     ONLY: qs_ks_env_type,&
     127              :                                               set_ks_env
     128              :    USE qs_ks_utils,                     ONLY: &
     129              :         calc_v_sic_rspace, calculate_zmp_potential, check_sum_energies, compute_matrix_vxc, &
     130              :         get_embed_potential_energy, low_spin_roks, print_densities, print_detailed_energy, &
     131              :         sic_explicit_orbitals, sum_up_and_integrate
     132              :    USE qs_local_rho_types,              ONLY: local_rho_type
     133              :    USE qs_mo_types,                     ONLY: get_mo_set,&
     134              :                                               mo_set_type
     135              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
     136              :    USE qs_rho0_ggrid,                   ONLY: integrate_vhg0_rspace
     137              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
     138              :                                               qs_rho_type
     139              :    USE qs_sccs,                         ONLY: sccs,&
     140              :                                               sccs_core_fill_coefficients
     141              :    USE qs_scf_types,                    ONLY: qs_scf_env_type
     142              :    USE qs_vxc,                          ONLY: qs_vxc_create
     143              :    USE qs_vxc_atom,                     ONLY: calculate_vxc_atom,&
     144              :                                               gapw_cdft_one_center
     145              :    USE rtp_admm_methods,                ONLY: rtp_admm_calc_rho_aux,&
     146              :                                               rtp_admm_merge_ks_matrix
     147              :    USE se_fock_matrix,                  ONLY: build_se_fock_matrix
     148              :    USE skala_gpw_functional,            ONLY: &
     149              :         ensure_native_skala_grid_scope, get_gauxc_section, get_skala_native_section, &
     150              :         native_skala_gapw_atom_composite_requested, native_skala_gapw_composite_direct_ao, &
     151              :         native_skala_gapw_composite_reference, native_skala_uses_atom_composite_grid, &
     152              :         skala_execution_use_cuda, skala_gapw_representation, xc_section_uses_native_skala_grid
     153              :    USE smeagol_interface,               ONLY: smeagol_shift_v_hartree
     154              :    USE string_utilities,                ONLY: uppercase
     155              :    USE surface_dipole,                  ONLY: calc_dipsurf_potential
     156              :    USE tblite_ks_matrix,                ONLY: build_tblite_ks_matrix
     157              :    USE virial_types,                    ONLY: virial_type
     158              :    USE xc_gauxc_functional,             ONLY: apply_gauxc,&
     159              :                                               gauxc_gapw_all_all_electron,&
     160              :                                               gauxc_gapw_all_pseudopotentials,&
     161              :                                               gauxc_gapw_has_paw_pseudopotentials
     162              :    USE xc_input_constants,              ONLY: skala_gapw_direct_valence,&
     163              :                                               skala_gapw_paw_one_center
     164              :    USE xtb_ks_matrix,                   ONLY: build_xtb_ks_matrix
     165              : #include "./base/base_uses.f90"
     166              : 
     167              :    IMPLICIT NONE
     168              : 
     169              :    PRIVATE
     170              : 
     171              :    LOGICAL, PARAMETER :: debug_this_module = .TRUE.
     172              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_methods'
     173              : 
     174              :    PUBLIC :: calc_rho_tot_gspace, qs_ks_update_qs_env, qs_ks_build_kohn_sham_matrix, &
     175              :              qs_ks_allocate_basics, evaluate_core_matrix_traces, rebuild_ks_matrix
     176              : 
     177              : CONTAINS
     178              : 
     179              : ! **************************************************************************************************
     180              : !> \brief routine where the real calculations are made: the
     181              : !>      KS matrix is calculated
     182              : !> \param qs_env the qs_env to update
     183              : !> \param calculate_forces if true calculate the quantities needed
     184              : !>        to calculate the forces. Defaults to false.
     185              : !> \param just_energy if true updates the energies but not the
     186              : !>        ks matrix. Defaults to false
     187              : !> \param print_active ...
     188              : !> \param ext_ks_matrix ...
     189              : !> \param ext_xc_section ...
     190              : !> \par History
     191              : !>      06.2002 moved from qs_scf to qs_ks_methods, use of ks_env
     192              : !>              new did_change scheme [fawzi]
     193              : !>      10.2002 introduced pools, uses updated rho as input, LSD [fawzi]
     194              : !>      10.2004 build_kohn_sham matrix now also computes the derivatives
     195              : !>              of the total energy wrt to the MO coefs, if instructed to
     196              : !>              do so. This appears useful for orbital dependent functionals
     197              : !>              where the KS matrix alone (however this might be defined)
     198              : !>               does not contain the info to construct this derivative.
     199              : !> \author Matthias Krack
     200              : !> \note
     201              : !>      make rho, energy and qs_charges optional, defaulting
     202              : !>      to qs_env components?
     203              : ! **************************************************************************************************
     204       140165 :    SUBROUTINE qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, &
     205              :                                            print_active, ext_ks_matrix, ext_xc_section)
     206              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     207              :       LOGICAL, INTENT(in)                                :: calculate_forces, just_energy
     208              :       LOGICAL, INTENT(IN), OPTIONAL                      :: print_active
     209              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
     210              :          POINTER                                         :: ext_ks_matrix
     211              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: ext_xc_section
     212              : 
     213              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_build_kohn_sham_matrix'
     214              : 
     215              :       CHARACTER(len=default_string_length)               :: gauxc_model_name, name
     216              :       INTEGER :: ace_rebuild_frequency, atom_a, gapw_representation, handle, iatom, ikind, img, &
     217              :          ispin, natom, nimages, nspins, output_unit
     218       140165 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
     219              :       LOGICAL :: ace_active, do_adiabatic_rescaling, do_ddapc, do_hfx, do_kpoints, do_ppl, dokp, &
     220              :          gapw, gapw_all_all_electron, gapw_all_pseudopotentials, gapw_mixed_core_representations, &
     221              :          gapw_xc, gauxc_model_none, just_energy_xc, lrigpw, my_print, &
     222              :          native_direct_valence_atom_composite, native_gapw_all_electron_atom_composite, &
     223              :          native_gapw_atom_composite_reference, native_gapw_atom_composite_requested, &
     224              :          native_gapw_composite_direct_ao, native_gapw_composite_reference, &
     225              :          native_gapw_periodic_atom_composite, native_grid_diagnostics, native_grid_use_cuda, &
     226              :          native_skala_evaluator, native_skala_restore_exc
     227              :       LOGICAL :: paw_composite_representation, rigpw, use_gauxc_matrix, use_virial
     228              :       LOGICAL, SAVE :: native_grid_cpu_kpoints_warned = .FALSE.
     229              :       REAL(KIND=dp) :: ecore_ppl, edisp, ee_ener, ekin_mol, mulliken_order_p, &
     230              :          native_skala_composite_contraction, native_skala_composite_rho_contraction, &
     231              :          native_skala_composite_tau_contraction, native_skala_exc_scf, native_skala_total_scf, &
     232              :          vscale
     233       140165 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: cdft_gapw_values
     234       140165 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: cdft_charge_correction, &
     235       140165 :                                                             cdft_electronic_charge, &
     236       140165 :                                                             native_skala_atom_force
     237              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: h_stress, native_skala_composite_virial, &
     238              :                                                             pv_loc
     239              :       TYPE(admm_type), POINTER                           :: admm_env
     240       140165 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     241              :       TYPE(cdft_control_type), POINTER                   :: cdft_control
     242              :       TYPE(cell_type), POINTER                           :: cell
     243              :       TYPE(cp_logger_type), POINTER                      :: logger
     244       140165 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ksmat, matrix_vhxc, mo_derivs
     245       140165 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: ks_matrix, ks_matrix_im, matrix_h, &
     246       140165 :                                                             matrix_h_im, matrix_s, matrix_vxc, &
     247       140165 :                                                             my_rho, rho_ao
     248              :       TYPE(dft_control_type), POINTER                    :: dft_control
     249       140165 :       TYPE(ecoul_1center_type), DIMENSION(:), POINTER    :: ecoul_1c
     250              :       TYPE(harris_type), POINTER                         :: harris_env
     251              :       TYPE(kpoint_type), POINTER                         :: kpoints
     252              :       TYPE(local_rho_type), POINTER                      :: local_rho_set
     253              :       TYPE(lri_density_type), POINTER                    :: lri_density
     254              :       TYPE(lri_environment_type), POINTER                :: lri_env
     255       140165 :       TYPE(lri_kind_type), DIMENSION(:), POINTER         :: lri_v_int
     256              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     257              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     258       140165 :          POINTER                                         :: sab_orb
     259       140165 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     260              :       TYPE(pw_c1d_gs_type)                               :: rho_tot_gspace, v_hartree_gspace
     261              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho_core
     262              :       TYPE(pw_env_type), POINTER                         :: pw_env
     263              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
     264              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     265       420495 :       TYPE(pw_r3d_rs_type), DIMENSION(2)                 :: v_hxc_rspace
     266       280330 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r, v_rspace_embed, v_rspace_new, &
     267       140165 :                                                             v_rspace_new_aux_fit, v_tau_rspace, &
     268       140165 :                                                             v_tau_rspace_aux_fit
     269              :       TYPE(pw_r3d_rs_type), POINTER :: rho0_s_rs, rho_nlcc, rhoz_cneo_s_rs, v_hartree_rspace, &
     270              :          v_sccs_rspace, v_sic_rspace, v_spin_ddapc_rest_r, vee, vppl_rspace
     271              :       TYPE(qs_energy_type), POINTER                      :: energy
     272       140165 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     273       140165 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     274              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     275              :       TYPE(qs_rho_type), POINTER                         :: rho, rho1, rho_struct, rho_xc
     276              :       TYPE(section_vals_type), POINTER :: ace_section, adiabatic_rescaling_section, gauxc_section, &
     277              :          hfx_sections, input, native_section, scf_section, xc_section
     278              :       TYPE(virial_type), POINTER                         :: virial
     279              : 
     280       140165 :       CALL timeset(routineN, handle)
     281       140165 :       NULLIFY (admm_env, atomic_kind_set, cell, dft_control, force, logger, mo_derivs, my_rho, &
     282       140165 :                rho_struct, para_env, pw_env, virial, vppl_rspace, &
     283       140165 :                ace_section, &
     284       140165 :                adiabatic_rescaling_section, hfx_sections, input, scf_section, &
     285       140165 :                xc_section, gauxc_section, native_section, matrix_h, matrix_h_im, matrix_s, &
     286       140165 :                auxbas_pw_pool, poisson_env, &
     287       140165 :                v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, &
     288       140165 :                matrix_vhxc, &
     289       140165 :                vee, rho_nlcc, ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, &
     290       140165 :                rho_core, sab_orb, particle_set, qs_kind_set, kpoints)
     291              : 
     292       140165 :       CPASSERT(ASSOCIATED(qs_env))
     293              : 
     294       140165 :       logger => cp_get_default_logger()
     295       140165 :       my_print = .TRUE.
     296       140165 :       IF (PRESENT(print_active)) my_print = print_active
     297       140165 :       use_gauxc_matrix = .FALSE.
     298       140165 :       native_gapw_atom_composite_reference = .FALSE.
     299       140165 :       native_gapw_atom_composite_requested = .FALSE.
     300       140165 :       native_gapw_all_electron_atom_composite = .FALSE.
     301       140165 :       native_direct_valence_atom_composite = .FALSE.
     302       140165 :       gapw_all_all_electron = .FALSE.
     303       140165 :       gapw_all_pseudopotentials = .FALSE.
     304       140165 :       gapw_mixed_core_representations = .FALSE.
     305       140165 :       native_gapw_composite_direct_ao = .FALSE.
     306       140165 :       native_gapw_composite_reference = .FALSE.
     307       140165 :       native_gapw_periodic_atom_composite = .FALSE.
     308       140165 :       native_grid_diagnostics = .FALSE.
     309       140165 :       native_skala_composite_contraction = 0.0_dp
     310       140165 :       native_skala_composite_rho_contraction = 0.0_dp
     311       140165 :       native_skala_composite_tau_contraction = 0.0_dp
     312       140165 :       native_skala_restore_exc = .FALSE.
     313              : 
     314              :       CALL get_qs_env(qs_env, &
     315              :                       ks_env=ks_env, &
     316              :                       dft_control=dft_control, &
     317              :                       matrix_h_kp=matrix_h, &
     318              :                       matrix_h_im_kp=matrix_h_im, &
     319              :                       matrix_s_kp=matrix_s, &
     320              :                       matrix_ks_kp=ks_matrix, &
     321              :                       matrix_ks_im_kp=ks_matrix_im, &
     322              :                       matrix_vhxc=matrix_vhxc, &
     323              :                       matrix_vxc_kp=matrix_vxc, &
     324              :                       pw_env=pw_env, &
     325              :                       cell=cell, &
     326              :                       atomic_kind_set=atomic_kind_set, &
     327              :                       para_env=para_env, &
     328              :                       input=input, &
     329              :                       virial=virial, &
     330              :                       v_hartree_rspace=v_hartree_rspace, &
     331              :                       vee=vee, &
     332              :                       rho_nlcc=rho_nlcc, &
     333              :                       rho=rho, &
     334              :                       rho_core=rho_core, &
     335              :                       rho_xc=rho_xc, &
     336              :                       sab_orb=sab_orb, &
     337              :                       energy=energy, &
     338              :                       force=force, &
     339              :                       kpoints=kpoints, &
     340              :                       do_kpoints=do_kpoints, &
     341              :                       particle_set=particle_set, &
     342              :                       qs_kind_set=qs_kind_set, &
     343       140165 :                       natom=natom)
     344              : 
     345              :       ! Reject force/stress requests before assembling even the baseline KS matrix.
     346       140165 :       IF (dft_control%low_spin_roks .AND. dft_control%qs_control%gapw) THEN
     347          290 :          IF (.NOT. virial%pv_numer .AND. &
     348              :              (virial%pv_calculate .OR. (virial%pv_availability .AND. calculate_forces))) THEN
     349            0 :             CALL cp_abort(__LOCATION__, "GAPW low spin ROKS stress is not implemented.")
     350              :          END IF
     351          290 :          IF (calculate_forces) THEN
     352            0 :             CALL cp_abort(__LOCATION__, "GAPW low spin ROKS forces are not implemented.")
     353              :          END IF
     354              :       END IF
     355              : 
     356       140165 :       CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
     357              : 
     358       140165 :       nimages = dft_control%nimages
     359       140165 :       nspins = dft_control%nspins
     360              : 
     361              :       ! remap pointer to allow for non-kpoint external ks matrix
     362       140165 :       IF (PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
     363              : 
     364       140165 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     365              : 
     366       140165 :       adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
     367       140165 :       CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
     368       140165 :       just_energy_xc = just_energy
     369       140165 :       IF (do_adiabatic_rescaling) THEN
     370              :          !! If we perform adiabatic rescaling, the xc potential has to be scaled by the xc- and
     371              :          !! HFX-energy. Thus, let us first calculate the energy
     372           44 :          just_energy_xc = .TRUE.
     373              :       END IF
     374              : 
     375       140165 :       CPASSERT(ASSOCIATED(matrix_h))
     376       140165 :       CPASSERT(ASSOCIATED(matrix_s))
     377       140165 :       CPASSERT(ASSOCIATED(rho))
     378       140165 :       CPASSERT(ASSOCIATED(pw_env))
     379       140165 :       CPASSERT(SIZE(ks_matrix, 1) > 0)
     380       140165 :       dokp = (nimages > 1)
     381              : 
     382              :       ! Setup the possible usage of DDAPC charges
     383              :       do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
     384              :                  qs_env%cp_ddapc_ewald%do_decoupling .OR. &
     385              :                  qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
     386       140165 :                  qs_env%cp_ddapc_ewald%do_solvation
     387              : 
     388              :       ! Check if LRIGPW is used
     389       140165 :       lrigpw = dft_control%qs_control%lrigpw
     390       140165 :       rigpw = dft_control%qs_control%rigpw
     391       140165 :       IF (rigpw) THEN
     392           26 :          CPASSERT(nimages == 1)
     393              :       END IF
     394           26 :       IF (lrigpw .AND. rigpw) THEN
     395            0 :          CPABORT(" LRI and RI are not compatible")
     396              :       END IF
     397              : 
     398              :       ! Check for GAPW method : additional terms for local densities
     399       140165 :       gapw = dft_control%qs_control%gapw
     400       140165 :       gapw_xc = dft_control%qs_control%gapw_xc
     401       140165 :       IF (gapw_xc .AND. gapw) THEN
     402            0 :          CPABORT(" GAPW and GAPW_XC are not compatible")
     403              :       END IF
     404       140165 :       IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw)) THEN
     405            0 :          CPABORT(" GAPW/GAPW_XC and LRIGPW are not compatible")
     406              :       END IF
     407       140165 :       IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw)) THEN
     408            0 :          CPABORT(" GAPW/GAPW_XC and RIGPW are not compatible")
     409              :       END IF
     410              : 
     411       140165 :       do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
     412       140165 :       IF (do_ppl) THEN
     413           60 :          CPASSERT(.NOT. gapw)
     414           60 :          CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
     415              :       END IF
     416              : 
     417       140165 :       IF (gapw_xc) THEN
     418         4446 :          CPASSERT(ASSOCIATED(rho_xc))
     419              :       END IF
     420              : 
     421              :       ! gets the tmp grids
     422       140165 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
     423              : 
     424       140165 :       IF (gapw .AND. (poisson_env%parameters%solver == pw_poisson_implicit)) THEN
     425            0 :          CPABORT("The implicit Poisson solver cannot be used in conjunction with GAPW.")
     426              :       END IF
     427              : 
     428              :       ! ***  Prepare densities for gapw ***
     429       140165 :       IF (gapw .OR. gapw_xc) THEN
     430        29128 :          CALL prepare_gapw_den(qs_env, do_rho0=(.NOT. gapw_xc))
     431              :       END IF
     432              : 
     433              :       ! Calculate the Hartree potential
     434       140165 :       CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
     435       140165 :       CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
     436              : 
     437       140165 :       scf_section => section_vals_get_subs_vals(input, "DFT%SCF")
     438              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, scf_section, &
     439              :                                            "PRINT%DETAILED_ENERGY"), &
     440              :                 cp_p_file) .AND. &
     441       140165 :           (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
     442              :           (.NOT. (poisson_env%parameters%solver == pw_poisson_implicit))) THEN
     443          918 :          CALL pw_zero(rho_tot_gspace)
     444          918 :          CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density=.TRUE.)
     445              :          CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%e_hartree, &
     446          918 :                                v_hartree_gspace)
     447          918 :          CALL pw_zero(rho_tot_gspace)
     448          918 :          CALL pw_zero(v_hartree_gspace)
     449              :       END IF
     450              : 
     451              :       ! Get the total density in g-space [ions + electrons]
     452       140165 :       CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
     453              : 
     454       140165 :       IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc) THEN
     455            0 :          CPABORT("GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
     456              :       END IF
     457              : 
     458              :       ! Add the planar counter charge density
     459       140165 :       IF (dft_control%do_pcc) THEN
     460          428 :          CALL planar_counter_charge(rho_tot_gspace, dft_control%pcc_control, auxbas_pw_pool)
     461              :       END IF
     462              : 
     463       140165 :       IF (my_print) THEN
     464       140107 :          CALL print_densities(qs_env, rho)
     465              :       END IF
     466              : 
     467       140165 :       IF (dft_control%do_sccs) THEN
     468              :          ! Self-consistent continuum solvation (SCCS) model
     469              :          NULLIFY (v_sccs_rspace)
     470          262 :          ALLOCATE (v_sccs_rspace)
     471          262 :          CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
     472              : 
     473          262 :          IF (poisson_env%parameters%solver == pw_poisson_implicit) THEN
     474            0 :             CPABORT("The implicit Poisson solver cannot be used together with SCCS.")
     475              :          END IF
     476              : 
     477          262 :          IF (use_virial .AND. calculate_forces) THEN
     478              :             CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
     479            0 :                       h_stress=h_stress)
     480            0 :             virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
     481            0 :             virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
     482              :          ELSE
     483          262 :             CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
     484              :          END IF
     485              :       ELSE
     486              :          ! Getting the Hartree energy and Hartree potential.  Also getting the stress tensor
     487              :          ! from the Hartree term if needed.  No nuclear force information here
     488       139903 :          IF (use_virial .AND. calculate_forces) THEN
     489          562 :             h_stress(:, :) = 0.0_dp
     490              :             CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
     491              :                                   v_hartree_gspace, h_stress=h_stress, &
     492          562 :                                   rho_core=rho_core)
     493         7306 :             virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
     494         7306 :             virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
     495              :          ELSE
     496              :             CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
     497       139341 :                                   v_hartree_gspace, rho_core=rho_core)
     498              :          END IF
     499              :       END IF
     500              : 
     501       140165 :       IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce) THEN
     502          404 :          CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     503              :          CALL planar_averaged_v_hartree_3d(v_hartree_rspace, dft_control, qs_env%scf_control%gce%do_gce, &
     504          404 :                                            qs_env%scf_control%gce%ref_esp, para_env)
     505              :       END IF
     506              : 
     507              :       ! In case decouple periodic images and/or apply restraints to charges
     508       140165 :       IF (do_ddapc) THEN
     509              :          CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
     510              :                           v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
     511         2394 :                           just_energy)
     512              :       ELSE
     513       137771 :          dft_control%qs_control%ddapc_explicit_potential = .FALSE.
     514       137771 :          dft_control%qs_control%ddapc_restraint_is_spin = .FALSE.
     515       137771 :          IF (.NOT. just_energy) THEN
     516       127413 :             CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     517       127413 :             CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
     518              :          END IF
     519              :       END IF
     520       140165 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
     521              : 
     522       140165 :       IF (dft_control%correct_surf_dip) THEN
     523          110 :          IF (dft_control%surf_dip_correct_switch) THEN
     524          110 :             CALL calc_dipsurf_potential(qs_env, energy)
     525          110 :             energy%hartree = energy%hartree + energy%surf_dipole
     526              :          END IF
     527              :       END IF
     528              : 
     529              :       ! SIC
     530              :       CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
     531       140165 :                              just_energy, calculate_forces, auxbas_pw_pool)
     532              : 
     533              :       ! Check if CDFT constraint is needed
     534       140165 :       CALL qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
     535              : 
     536              :       ! Adds the External Potential if requested
     537       140165 :       IF (dft_control%apply_external_potential) THEN
     538              :          ! Compute the energy due to the external potential
     539              :          ee_ener = 0.0_dp
     540          728 :          DO ispin = 1, nspins
     541          728 :             ee_ener = ee_ener + pw_integral_ab(rho_r(ispin), vee)
     542              :          END DO
     543          364 :          IF (.NOT. just_energy) THEN
     544          364 :             IF (gapw) THEN
     545              :                CALL get_qs_env(qs_env=qs_env, &
     546              :                                rho0_s_rs=rho0_s_rs, &
     547           42 :                                rhoz_cneo_s_rs=rhoz_cneo_s_rs)
     548           42 :                CPASSERT(ASSOCIATED(rho0_s_rs))
     549           42 :                IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
     550            0 :                   CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
     551              :                END IF
     552           42 :                ee_ener = ee_ener + pw_integral_ab(rho0_s_rs, vee)
     553           42 :                IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
     554            0 :                   CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
     555              :                END IF
     556              :             END IF
     557              :          END IF
     558              :          ! the sign accounts for the charge of the electrons
     559          364 :          energy%ee = -ee_ener
     560              :       END IF
     561              : 
     562              :       ! Adds the QM/MM potential
     563       140165 :       IF (qs_env%qmmm) THEN
     564              :          CALL qmmm_calculate_energy(qs_env=qs_env, &
     565              :                                     rho=rho_r, &
     566              :                                     v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
     567         6334 :                                     qmmm_energy=energy%qmmm_el)
     568         6334 :          IF (qs_env%qmmm_env_qm%image_charge) THEN
     569              :             CALL calculate_image_pot(v_hartree_rspace=v_hartree_rspace, &
     570              :                                      rho_hartree_gspace=rho_tot_gspace, &
     571              :                                      energy=energy, &
     572              :                                      qmmm_env=qs_env%qmmm_env_qm, &
     573           60 :                                      qs_env=qs_env)
     574           60 :             IF (.NOT. just_energy) THEN
     575              :                CALL add_image_pot_to_hartree_pot(v_hartree=v_hartree_rspace, &
     576              :                                                  v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
     577           60 :                                                  qs_env=qs_env)
     578           60 :                IF (calculate_forces) THEN
     579              :                   CALL integrate_potential_devga_rspace( &
     580              :                      potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
     581              :                      forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
     582           20 :                      qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
     583              :                END IF
     584              :             END IF
     585           60 :             CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
     586           60 :             DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
     587              :          END IF
     588         6334 :          IF (.NOT. just_energy) THEN
     589              :             CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
     590         6244 :                                          v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
     591              :          END IF
     592              :       END IF
     593       140165 :       CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
     594              : 
     595              :       ! SMEAGOL interface
     596       140165 :       IF (dft_control%smeagol_control%smeagol_enabled .AND. &
     597              :           dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
     598            0 :          CPASSERT(ASSOCIATED(dft_control%smeagol_control%aux))
     599              :          CALL smeagol_shift_v_hartree(v_hartree_rspace, cell, &
     600              :                                       dft_control%smeagol_control%aux%HartreeLeadsLeft, &
     601              :                                       dft_control%smeagol_control%aux%HartreeLeadsRight, &
     602              :                                       dft_control%smeagol_control%aux%HartreeLeadsBottom, &
     603              :                                       dft_control%smeagol_control%aux%VBias, &
     604              :                                       dft_control%smeagol_control%aux%minL, &
     605              :                                       dft_control%smeagol_control%aux%maxR, &
     606              :                                       dft_control%smeagol_control%aux%isexplicit_maxR, &
     607            0 :                                       dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
     608              :       END IF
     609              : 
     610              :       ! calculate the density matrix for the fitted mo_coeffs
     611       140165 :       IF (dft_control%do_admm) THEN
     612        13906 :          IF (PRESENT(ext_xc_section)) THEN
     613            0 :             CALL hfx_admm_init(qs_env, calculate_forces, ext_xc_section)
     614              :          ELSE
     615        13906 :             CALL hfx_admm_init(qs_env, calculate_forces)
     616              :          END IF
     617              : 
     618        13906 :          IF (dft_control%do_admm_mo) THEN
     619        13692 :             IF (qs_env%run_rtp) THEN
     620          112 :                CALL rtp_admm_calc_rho_aux(qs_env)
     621              :             ELSE
     622        13580 :                IF (dokp) THEN
     623          386 :                   CALL admm_mo_calc_rho_aux_kp(qs_env)
     624              :                ELSE
     625        13194 :                   CALL admm_mo_calc_rho_aux(qs_env)
     626              :                END IF
     627              :             END IF
     628          214 :          ELSE IF (dft_control%do_admm_dm) THEN
     629          214 :             CALL admm_dm_calc_rho_aux(qs_env)
     630              :          END IF
     631              :       END IF
     632              : 
     633              :       ! only activate stress calculation if
     634       140165 :       IF (use_virial .AND. calculate_forces) virial%pv_calculate = .TRUE.
     635              : 
     636              :       ! *** calculate the xc potential on the pw density ***
     637              :       ! *** associates v_rspace_new if the xc potential needs to be computed.
     638              :       ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
     639       140165 :       IF (dft_control%do_admm) THEN
     640        13906 :          CALL get_qs_env(qs_env, admm_env=admm_env)
     641        13906 :          xc_section => admm_env%xc_section_aux
     642        13906 :          CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
     643              : 
     644              :          ! here we ignore a possible vdW section in admm_env%xc_section_aux
     645              :          CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     646              :                             vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
     647        13906 :                             just_energy=just_energy_xc)
     648              : 
     649        13906 :          IF (admm_env%do_gapw) THEN
     650              :             !compute the potential due to atomic densities
     651              :             CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
     652              :                                     kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
     653              :                                     xc_section_external=xc_section, &
     654              :                                     rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
     655         4970 :                                     calculate_forces=calculate_forces)
     656              : 
     657              :          END IF
     658              : 
     659        13906 :          NULLIFY (rho_struct)
     660              : 
     661        13906 :          IF (use_virial .AND. calculate_forces) THEN
     662           24 :             vscale = 1.0_dp
     663              :             !Note: ADMMS and ADMMP stress tensor only for closed-shell calculations
     664           24 :             IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
     665           24 :             IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
     666          312 :             virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
     667          312 :             virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
     668              :             ! virial%pv_xc will be zeroed in the xc routines
     669              :          END IF
     670        13906 :          xc_section => admm_env%xc_section_primary
     671              :       ELSE
     672       126259 :          xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     673              :          ! build ks matrix with an xc section potentially different from the one defined in input
     674       126259 :          IF (PRESENT(ext_xc_section)) xc_section => ext_xc_section
     675              :       END IF
     676              : 
     677       140165 :       IF (gapw_xc) THEN
     678         4446 :          CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
     679              :       ELSE
     680       135719 :          CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
     681              :       END IF
     682              : 
     683              :       ! zmp
     684       140165 :       IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc) THEN
     685          106 :          energy%exc = 0.0_dp
     686          106 :          CALL calculate_zmp_potential(qs_env, v_rspace_new, rho, exc=energy%exc)
     687              :       ELSE
     688              :          ! Embedding potential (runs regardless of XC method)
     689       140059 :          IF (dft_control%apply_embed_pot) THEN
     690          822 :             NULLIFY (v_rspace_embed)
     691          822 :             energy%embed_corr = 0.0_dp
     692              :             CALL get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, &
     693          822 :                                             energy%embed_corr, just_energy)
     694              :          END IF
     695              : 
     696              :          ! Everything else, either via GauXC or manual XC computation
     697       140059 :          native_skala_evaluator = xc_section_uses_native_skala_grid(xc_section)
     698       140059 :          paw_composite_representation = .FALSE.
     699       140059 :          IF (gapw .OR. gapw_xc) THEN
     700              :             paw_composite_representation = xc_section_uses_native_skala_grid(xc_section) .AND. &
     701              :                                            skala_gapw_representation(xc_section) == skala_gapw_paw_one_center .AND. &
     702        29128 :                                            gauxc_gapw_has_paw_pseudopotentials(qs_kind_set)
     703              :          END IF
     704       140059 :          IF (dft_control%use_gauxc) THEN
     705          962 :             IF (native_skala_evaluator) THEN
     706          356 :                IF (gapw .AND. dft_control%low_spin_roks) THEN
     707              :                   CALL cp_abort(__LOCATION__, &
     708            0 :                                 "GAPW low spin ROKS requires the molecular GauXC grid, not native Skala grids.")
     709              :                END IF
     710          356 :                CALL ensure_native_skala_grid_scope(xc_section)
     711          356 :                gapw_representation = skala_gapw_representation(xc_section)
     712          356 :                IF (gapw .OR. gapw_xc) THEN
     713          168 :                   gapw_all_all_electron = gauxc_gapw_all_all_electron(qs_kind_set)
     714          168 :                   gapw_all_pseudopotentials = gauxc_gapw_all_pseudopotentials(qs_kind_set)
     715              :                   gapw_mixed_core_representations = &
     716          168 :                      .NOT. gapw_all_all_electron .AND. .NOT. gapw_all_pseudopotentials
     717              :                END IF
     718              :                native_direct_valence_atom_composite = &
     719              :                   xc_section_uses_native_skala_grid(xc_section) .AND. &
     720              :                   native_skala_uses_atom_composite_grid(xc_section) .AND. &
     721              :                   ((.NOT. gapw .AND. .NOT. gapw_xc) .OR. &
     722              :                    (gapw_representation == skala_gapw_direct_valence .AND. &
     723          356 :                     gapw_all_pseudopotentials))
     724              :                native_gapw_composite_reference = &
     725          356 :                   native_skala_gapw_composite_reference(xc_section) .AND. (gapw .OR. gapw_xc)
     726              :                native_gapw_atom_composite_requested = &
     727              :                   native_skala_gapw_atom_composite_requested(xc_section) .AND. &
     728          356 :                   (gapw .OR. gapw_xc)
     729              :                native_gapw_atom_composite_reference = &
     730          356 :                   native_gapw_atom_composite_requested .OR. paw_composite_representation
     731              :                native_gapw_periodic_atom_composite = &
     732          356 :                   native_skala_uses_atom_composite_grid(xc_section)
     733          356 :                IF (gapw_mixed_core_representations .AND. &
     734              :                    .NOT. native_gapw_periodic_atom_composite) THEN
     735              :                   CALL cp_abort(__LOCATION__, &
     736              :                                 "Mixed all-electron and pseudopotential GAPW kinds require "// &
     737              :                                 "LAYOUT ATOM_COMPOSITE so that their primitive "// &
     738            0 :                                 "fields are combined before Skala feature construction.")
     739              :                END IF
     740              :                native_gapw_atom_composite_reference = &
     741              :                   native_gapw_atom_composite_reference .OR. &
     742              :                   (gapw_mixed_core_representations .AND. &
     743          356 :                    native_gapw_periodic_atom_composite)
     744              :                native_gapw_all_electron_atom_composite = &
     745              :                   (gapw .OR. gapw_xc) .AND. &
     746              :                   gapw_all_all_electron .AND. &
     747              :                   (.NOT. native_gapw_composite_reference) .AND. &
     748          356 :                   native_gapw_periodic_atom_composite
     749              :                native_gapw_atom_composite_reference = &
     750              :                   native_gapw_atom_composite_reference .OR. &
     751          356 :                   native_gapw_all_electron_atom_composite
     752          356 :                IF (paw_composite_representation) THEN
     753          136 :                   IF (ANY(cell%perd /= 0)) THEN
     754              :                      native_gapw_composite_reference = &
     755          104 :                         .NOT. native_gapw_periodic_atom_composite
     756              :                      native_gapw_atom_composite_reference = &
     757          104 :                         native_gapw_periodic_atom_composite
     758              :                   ELSE
     759            8 :                      native_gapw_composite_reference = .FALSE.
     760            8 :                      native_gapw_atom_composite_reference = .TRUE.
     761              :                   END IF
     762              :                END IF
     763          356 :                IF (native_gapw_composite_reference .AND. native_gapw_atom_composite_reference) THEN
     764              :                   CALL cp_abort(__LOCATION__, &
     765            0 :                                 "Select only one native-grid GAPW composite reference route.")
     766              :                END IF
     767              :                native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
     768          356 :                                                  native_skala_gapw_composite_direct_ao(xc_section)
     769          356 :                native_section => get_skala_native_section(xc_section)
     770          356 :                IF (ASSOCIATED(native_section)) THEN
     771              :                   CALL section_vals_val_get(native_section, "DIAGNOSTICS", &
     772          356 :                                             l_val=native_grid_diagnostics)
     773              :                END IF
     774          356 :                IF ((.NOT. do_kpoints) .AND. nimages /= 1) THEN
     775              :                   CALL cp_abort(__LOCATION__, &
     776              :                                 "Native SKALA grid evaluation supports multiple images only "// &
     777            0 :                                 "for k-point calculations.")
     778              :                END IF
     779          356 :                IF (do_kpoints) THEN
     780           36 :                   CPASSERT(ASSOCIATED(kpoints))
     781           36 :                   native_grid_use_cuda = skala_execution_use_cuda(xc_section)
     782           36 :                   IF (.NOT. native_grid_use_cuda) THEN
     783           36 :                      IF (para_env%mepos == 0 .AND. .NOT. native_grid_cpu_kpoints_warned) THEN
     784              :                         CALL cp_warn(__LOCATION__, &
     785              :                                      "Native SKALA grid evaluation with k-points is using the CPU TorchScript "// &
     786              :                                      "path. Use a mutually compatible BLAS, ScaLAPACK, OpenMP, and LibTorch "// &
     787            3 :                                      "runtime stack, or select USE_GPU GPU.")
     788            3 :                         native_grid_cpu_kpoints_warned = .TRUE.
     789              :                      END IF
     790              :                   END IF
     791              :                END IF
     792          356 :                IF (dft_control%roks) THEN
     793            0 :                   CPABORT("Native SKALA grid evaluation does not support ROKS.")
     794              :                END IF
     795          356 :                IF (dft_control%do_admm) THEN
     796            0 :                   CPABORT("Native SKALA grid evaluation does not support ADMM.")
     797              :                END IF
     798              :                ! Force and stress rebuilds re-enter this path for derivatives and VXC only. The
     799              :                ! autograd virial already contains the rebuilt XC volume term when qs_vxc_create
     800              :                ! returns, so the externally reported energy must be restored to its SCF value.
     801          356 :                native_skala_restore_exc = calculate_forces
     802          356 :                IF (native_skala_restore_exc) THEN
     803           66 :                   native_skala_exc_scf = energy%exc
     804           66 :                   native_skala_total_scf = energy%total
     805              :                END IF
     806          356 :                IF (calculate_forces) THEN
     807          198 :                   ALLOCATE (native_skala_atom_force(3, natom))
     808           66 :                   native_skala_atom_force = 0.0_dp
     809           66 :                   IF (native_gapw_atom_composite_reference .OR. &
     810              :                       native_direct_valence_atom_composite) THEN
     811              :                      CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     812              :                                         vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     813              :                                         edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     814              :                                         just_energy=just_energy_xc, qs_env_external=qs_env, &
     815              :                                         native_gapw_composite_override=native_gapw_composite_reference, &
     816           58 :                                         native_skala_defer_to_atom_composite=.TRUE.)
     817              :                   ELSE
     818              :                      CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     819              :                                         vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     820              :                                         edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     821              :                                         just_energy=just_energy_xc, &
     822              :                                         native_skala_atom_force=native_skala_atom_force, &
     823              :                                         qs_env_external=qs_env, &
     824            8 :                                         native_gapw_composite_override=native_gapw_composite_reference)
     825              :                   END IF
     826              :                   IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
     827           66 :                       ASSOCIATED(v_rspace_new) .AND. ASSOCIATED(v_tau_rspace)) THEN
     828            2 :                      IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
     829            0 :                         output_unit = cp_logger_get_default_io_unit()
     830            0 :                         IF (output_unit > 0) THEN
     831            0 :                            DO iatom = 1, natom
     832              :                               WRITE (UNIT=output_unit, FMT="(T2,A,1X,I0,3(1X,ES20.12))") &
     833            0 :                                  "SKALA_GPW| Native regular-grid atom force", iatom, &
     834            0 :                                  native_skala_atom_force(:, iatom)
     835              :                            END DO
     836              :                         END IF
     837              :                      END IF
     838            2 :                      IF (use_virial) THEN
     839            2 :                         native_skala_composite_virial = 0.0_dp
     840              :                         CALL integrate_gapw_composite_vxc_gspace( &
     841              :                            qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
     842              :                            atom_force=native_skala_atom_force, &
     843              :                            strain_virial=native_skala_composite_virial, &
     844              :                            one_center_contraction=native_skala_composite_contraction, &
     845              :                            one_center_rho_contraction=native_skala_composite_rho_contraction, &
     846            2 :                            one_center_tau_contraction=native_skala_composite_tau_contraction)
     847            2 :                         IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
     848            0 :                            output_unit = cp_logger_get_default_io_unit()
     849            0 :                            IF (output_unit > 0) THEN
     850              :                               WRITE (UNIT=output_unit, FMT="(T2,A)") &
     851            0 :                                  "SKALA_GPW| Composite-direction one-center virial"
     852            0 :                               DO iatom = 1, 3
     853              :                                  WRITE (UNIT=output_unit, FMT="(T2,A,1X,3ES20.10)") &
     854            0 :                                     "SKALA_GPW|", native_skala_composite_virial(iatom, :)
     855              :                               END DO
     856              :                            END IF
     857              :                         END IF
     858           26 :                         virial%pv_xc = virial%pv_xc + native_skala_composite_virial
     859              :                      ELSE
     860              :                         CALL integrate_gapw_composite_vxc_gspace( &
     861              :                            qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
     862              :                            atom_force=native_skala_atom_force, &
     863              :                            one_center_contraction=native_skala_composite_contraction, &
     864              :                            one_center_rho_contraction=native_skala_composite_rho_contraction, &
     865            0 :                            one_center_tau_contraction=native_skala_composite_tau_contraction)
     866              :                      END IF
     867              :                   END IF
     868           66 :                   IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
     869            7 :                      output_unit = cp_logger_get_default_io_unit()
     870            7 :                      IF (output_unit > 0) THEN
     871              :                         WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
     872            7 :                            "SKALA_GPW| Composite-direction one-center VXC contraction", &
     873           14 :                            native_skala_composite_contraction
     874              :                         WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
     875            7 :                            "SKALA_GPW| Composite-direction one-center rho contraction", &
     876           14 :                            native_skala_composite_rho_contraction
     877              :                         WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
     878            7 :                            "SKALA_GPW| Composite-direction one-center tau contraction", &
     879           14 :                            native_skala_composite_tau_contraction
     880           21 :                         DO iatom = 1, natom
     881              :                            WRITE (UNIT=output_unit, FMT="(T2,A,1X,I0,3(1X,ES20.12))") &
     882           21 :                               "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
     883              :                         END DO
     884              :                      END IF
     885              :                   END IF
     886           66 :                   CPASSERT(ASSOCIATED(force))
     887           66 :                   CPASSERT(ASSOCIATED(atomic_kind_set))
     888           66 :                   CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
     889          198 :                   DO iatom = 1, natom
     890          132 :                      ikind = kind_of(iatom)
     891          132 :                      atom_a = atom_of_kind(iatom)
     892              :                      force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
     893          594 :                                                         native_skala_atom_force(:, iatom)
     894              :                   END DO
     895           66 :                   DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
     896              :                ELSE
     897              :                   CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     898              :                                      vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     899              :                                      edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     900              :                                      just_energy=just_energy_xc, qs_env_external=qs_env, &
     901              :                                      native_gapw_composite_override=native_gapw_composite_reference, &
     902              :                                      native_skala_defer_to_atom_composite= &
     903              :                                      (native_gapw_atom_composite_reference .OR. &
     904          290 :                                       native_direct_valence_atom_composite))
     905              :                   IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
     906          290 :                       ASSOCIATED(v_rspace_new) .AND. ASSOCIATED(v_tau_rspace)) THEN
     907              :                      CALL integrate_gapw_composite_vxc_gspace( &
     908              :                         qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
     909              :                         one_center_contraction=native_skala_composite_contraction, &
     910              :                         one_center_rho_contraction=native_skala_composite_rho_contraction, &
     911            2 :                         one_center_tau_contraction=native_skala_composite_tau_contraction)
     912            2 :                      IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
     913            0 :                         output_unit = cp_logger_get_default_io_unit()
     914            0 :                         IF (output_unit > 0) THEN
     915              :                            WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
     916            0 :                               "SKALA_GPW| Composite-direction one-center VXC contraction", &
     917            0 :                               native_skala_composite_contraction
     918              :                            WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
     919            0 :                               "SKALA_GPW| Composite-direction one-center rho contraction", &
     920            0 :                               native_skala_composite_rho_contraction
     921              :                            WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
     922            0 :                               "SKALA_GPW| Composite-direction one-center tau contraction", &
     923            0 :                               native_skala_composite_tau_contraction
     924              :                         END IF
     925              :                      END IF
     926              :                   END IF
     927              :                END IF
     928          356 :                IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
     929          356 :                IF (native_direct_valence_atom_composite) THEN
     930          126 :                   CPASSERT(ASSOCIATED(v_rspace_new))
     931          126 :                   CPASSERT(ASSOCIATED(v_tau_rspace))
     932              :                   CALL calculate_vxc_atom( &
     933              :                      qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
     934              :                      calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
     935          126 :                      composite_vxc_tau=v_tau_rspace, direct_valence_atom_grid=.TRUE.)
     936          126 :                   energy%exc = 0.0_dp
     937          230 :                ELSE IF ((gapw .OR. gapw_xc) .AND. .NOT. native_gapw_composite_reference) THEN
     938          144 :                   IF (native_gapw_atom_composite_reference) THEN
     939          128 :                      CPASSERT(ASSOCIATED(v_rspace_new))
     940          128 :                      CPASSERT(ASSOCIATED(v_tau_rspace))
     941              :                      CALL calculate_vxc_atom( &
     942              :                         qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
     943              :                         calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
     944          128 :                         composite_vxc_tau=v_tau_rspace, atom_composite_grid=.TRUE.)
     945          128 :                      energy%exc = 0.0_dp
     946              :                   ELSE
     947              :                      CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
     948              :                                              xc_section_external=xc_section, &
     949           16 :                                              calculate_forces=calculate_forces)
     950              :                   END IF
     951              :                END IF
     952          356 :                IF (edisp /= 0.0_dp) energy%dispersion = edisp
     953          356 :                IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
     954              :                   CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, &
     955              :                                           matrix_vxc=matrix_vxc, &
     956            0 :                                           gapw_full_basis=native_gapw_composite_direct_ao)
     957            0 :                   CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc)
     958              :                END IF
     959              :             ELSE
     960          606 :                use_gauxc_matrix = .TRUE.
     961          606 :                CALL apply_gauxc(qs_env, xc_section, calculate_forces)
     962          606 :                IF (gapw_xc .OR. (gapw .AND. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
     963              :                   CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
     964              :                                           xc_section_external=xc_section, &
     965            2 :                                           calculate_forces=calculate_forces)
     966              :                END IF
     967              :             END IF
     968              :          ELSE
     969              :             CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     970              :                                vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
     971              :                                edisp=edisp, dispersion_env=qs_env%dispersion_env, &
     972       139097 :                                just_energy=just_energy_xc)
     973       139097 :             IF (edisp /= 0.0_dp) energy%dispersion = edisp
     974       139097 :             IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
     975            2 :                CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
     976            2 :                CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc)
     977              :             END IF
     978              : 
     979       139097 :             IF (gapw .OR. gapw_xc) THEN
     980              :                CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
     981              :                                        xc_section_external=xc_section, &
     982        28818 :                                        calculate_forces=calculate_forces)
     983              :             END IF
     984              :          END IF
     985              :       END IF
     986              : 
     987        29422 :       IF (gapw .AND. dft_control%qs_control%cdft) THEN
     988         1380 :          ALLOCATE (cdft_gapw_values(SIZE(cdft_control%group)))
     989          460 :          IF (cdft_control%atomic_charges) THEN
     990              :             ALLOCATE (cdft_charge_correction(natom, nspins), &
     991          532 :                       cdft_electronic_charge(cdft_control%natoms, nspins))
     992              :             CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, &
     993           76 :                                       cdft_gapw_values, cdft_charge_correction)
     994          228 :             DO ispin = 1, nspins
     995          624 :                DO iatom = 1, cdft_control%natoms
     996              :                   cdft_electronic_charge(iatom, ispin) = &
     997          548 :                      pw_integral_ab(cdft_control%charge(iatom), rho_r(ispin), local_only=.TRUE.)
     998              :                END DO
     999              :             END DO
    1000           76 :             CALL para_env%sum(cdft_electronic_charge)
    1001          228 :             DO ispin = 1, nspins
    1002          624 :                DO iatom = 1, cdft_control%natoms
    1003              :                   cdft_electronic_charge(iatom, ispin) = &
    1004              :                      cdft_electronic_charge(iatom, ispin) + &
    1005          548 :                      cdft_charge_correction(cdft_control%atoms(iatom), ispin)
    1006              :                END DO
    1007              :             END DO
    1008              :          ELSE
    1009          384 :             CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, cdft_gapw_values)
    1010              :          END IF
    1011          996 :          cdft_control%value = cdft_control%value + cdft_gapw_values
    1012          996 :          energy%cdft = energy%cdft + DOT_PRODUCT(cdft_control%strength, cdft_gapw_values)
    1013          460 :          IF (cdft_control%atomic_charges) THEN
    1014           76 :             CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
    1015           76 :             DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
    1016              :          ELSE
    1017          384 :             CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
    1018              :          END IF
    1019          460 :          DEALLOCATE (cdft_gapw_values)
    1020              :       END IF
    1021              : 
    1022       140165 :       IF (dft_control%mtlr_dft_with_perturbation .AND. .NOT. just_energy) THEN
    1023         1844 :          IF (do_adiabatic_rescaling .OR. use_gauxc_matrix) THEN
    1024            0 :             CPABORT("MTLR requires an XC potential on the real-space grid.")
    1025              :          END IF
    1026         1844 :          IF (.NOT. ASSOCIATED(matrix_vhxc)) THEN
    1027           80 :             ALLOCATE (matrix_vhxc(nspins))
    1028           48 :             DO ispin = 1, nspins
    1029           32 :                NULLIFY (matrix_vhxc(ispin)%matrix)
    1030           32 :                ALLOCATE (matrix_vhxc(ispin)%matrix)
    1031              :                CALL dbcsr_create(matrix_vhxc(ispin)%matrix, &
    1032           32 :                                  template=ks_matrix(1, 1)%matrix)
    1033           32 :                CALL cp_dbcsr_alloc_block_from_nbl(matrix_vhxc(ispin)%matrix, sab_orb)
    1034           48 :                CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
    1035              :             END DO
    1036           16 :             CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
    1037              :          ELSE
    1038         1828 :             CPASSERT(SIZE(matrix_vhxc) == nspins)
    1039         5484 :             DO ispin = 1, nspins
    1040         5484 :                CPASSERT(ASSOCIATED(matrix_vhxc(ispin)%matrix))
    1041              :             END DO
    1042              :          END IF
    1043         5532 :          DO ispin = 1, nspins
    1044         3688 :             CALL auxbas_pw_pool%create_pw(v_hxc_rspace(ispin))
    1045         3688 :             CALL pw_zero(v_hxc_rspace(ispin))
    1046         3688 :             CALL pw_axpy(v_hartree_rspace, v_hxc_rspace(ispin))
    1047         3688 :             IF (ASSOCIATED(v_rspace_new)) THEN
    1048              :                CALL pw_axpy(v_rspace_new(ispin), &
    1049              :                             v_hxc_rspace(ispin), &
    1050         3688 :                             v_rspace_new(ispin)%pw_grid%dvol)
    1051              :             END IF
    1052         3688 :             CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
    1053              :             CALL integrate_v_rspace(v_hxc_rspace(ispin), &
    1054              :                                     hmat=matrix_vhxc(ispin), &
    1055              :                                     qs_env=qs_env, &
    1056         3688 :                                     calculate_forces=.FALSE.)
    1057         5532 :             CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
    1058              :          END DO
    1059              :       END IF
    1060              : 
    1061              :       ! set hartree and xc potentials for use in Harris method
    1062       140165 :       IF (qs_env%harris_method) THEN
    1063          116 :          CALL get_qs_env(qs_env, harris_env=harris_env)
    1064          116 :          CALL harris_set_potentials(harris_env, v_hartree_rspace, v_rspace_new)
    1065              :       END IF
    1066              : 
    1067       140165 :       NULLIFY (rho_struct)
    1068       140165 :       IF (use_virial .AND. calculate_forces) THEN
    1069         7306 :          virial%pv_exc = virial%pv_exc - virial%pv_xc
    1070         7306 :          virial%pv_virial = virial%pv_virial - virial%pv_xc
    1071              :       END IF
    1072              : 
    1073              :       ! *** Add Hartree-Fock contribution if required ***
    1074       140165 :       hfx_sections => section_vals_get_subs_vals(xc_section, "HF")
    1075       140165 :       CALL section_vals_get(hfx_sections, explicit=do_hfx)
    1076              : 
    1077       140165 :       ace_active = .FALSE.
    1078       140165 :       ace_rebuild_frequency = 1
    1079              : 
    1080       140165 :       IF (do_hfx) THEN
    1081        31746 :          ace_section => section_vals_get_subs_vals(hfx_sections, "ACE")
    1082        31746 :          IF (ASSOCIATED(ace_section)) THEN
    1083        31746 :             CALL section_vals_val_get(ace_section, "ACTIVE", l_val=ace_active)
    1084        31746 :             CALL section_vals_val_get(ace_section, "REBUILD_FREQUENCY", i_val=ace_rebuild_frequency)
    1085              :          END IF
    1086              :       END IF
    1087              : 
    1088       140165 :       IF (do_hfx) THEN
    1089        31746 :          IF (dokp) THEN
    1090          508 :             IF (ace_active) THEN
    1091            0 :                CPABORT("ACE-HFX for k-points is not implemented yet")
    1092              :             ELSE
    1093          508 :                CALL hfx_ks_matrix_kp(qs_env, ks_matrix, energy, calculate_forces)
    1094              :             END IF
    1095              : 
    1096              :          ELSE
    1097              :             ! ext_xc_section may contain a hfx section
    1098        31238 :             IF (ace_active) THEN
    1099              :                CALL hfx_ace_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
    1100              :                                       just_energy, v_rspace_new, v_tau_rspace, &
    1101           48 :                                       ace_rebuild_frequency, ext_xc_section=xc_section)
    1102              :             ELSE
    1103              :                CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
    1104        31190 :                                   just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
    1105              :             END IF
    1106              :          END IF
    1107              :       END IF !do_hfx
    1108              : 
    1109       140165 :       IF (do_ppl .AND. calculate_forces) THEN
    1110           12 :          CPASSERT(.NOT. gapw)
    1111           26 :          DO ispin = 1, nspins
    1112           26 :             CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
    1113              :          END DO
    1114              :       END IF
    1115              : 
    1116              :       ! The atom-composite evaluator differentiates its atom-centered NLCC field directly.
    1117              :       IF (ASSOCIATED(rho_nlcc) .AND. calculate_forces .AND. &
    1118       140165 :           .NOT. native_gapw_atom_composite_reference .AND. &
    1119              :           .NOT. native_direct_valence_atom_composite) THEN
    1120           92 :          DO ispin = 1, nspins
    1121           46 :             CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
    1122           92 :             IF (dft_control%do_admm) CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
    1123              :          END DO
    1124              :       END IF
    1125              : 
    1126              :       ! calculate KG correction
    1127       140165 :       IF (dft_control%qs_control%do_kg .AND. just_energy) THEN
    1128              : 
    1129           12 :          CPASSERT(nimages == 1)
    1130           12 :          ksmat => ks_matrix(:, 1)
    1131           12 :          CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
    1132              : 
    1133              :          ! subtract kg corr from the total energy
    1134           12 :          energy%exc = energy%exc - ekin_mol
    1135              : 
    1136              :       END IF
    1137              : 
    1138              :       ! ***  Single atom contributions ***
    1139       140165 :       IF (.NOT. just_energy) THEN
    1140       129417 :          IF (calculate_forces) THEN
    1141              :             ! Getting nuclear force contribution from the core charge density
    1142         6373 :             IF ((poisson_env%parameters%solver == pw_poisson_implicit) .AND. &
    1143              :                 (poisson_env%parameters%dielectric_params%dielec_core_correction)) THEN
    1144           28 :                BLOCK
    1145              :                   TYPE(pw_r3d_rs_type) :: v_minus_veps
    1146           28 :                   CALL auxbas_pw_pool%create_pw(v_minus_veps)
    1147           28 :                   CALL pw_copy(v_hartree_rspace, v_minus_veps)
    1148           28 :                   CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
    1149           28 :                   CALL integrate_v_core_rspace(v_minus_veps, qs_env)
    1150           28 :                   CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
    1151              :                END BLOCK
    1152              :             ELSE
    1153         6345 :                CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
    1154              :             END IF
    1155              :             ! SCCS with core fill: the dielectric function depends on rho_elec + rho_fill(R),
    1156              :             ! which yields an additional nuclear force contribution from v_sccs. The fill
    1157              :             ! Gaussians enter integrate_v_core_rspace with the sign convention of rho_core
    1158              :             ! (-ccore*exp(-calpha*r**2)), hence the negated potential
    1159         6373 :             IF (dft_control%do_sccs) THEN
    1160            6 :                IF (dft_control%sccs_control%core_fill) THEN
    1161              :                   BLOCK
    1162            2 :                      REAL(KIND=dp), DIMENSION(:), ALLOCATABLE           :: calpha, ccore
    1163              :                      TYPE(pw_r3d_rs_type)                               :: v_minus_vsccs
    1164            2 :                      CALL sccs_core_fill_coefficients(qs_env, calpha, ccore)
    1165            2 :                      CALL auxbas_pw_pool%create_pw(v_minus_vsccs)
    1166            2 :                      CALL pw_copy(v_sccs_rspace, v_minus_vsccs)
    1167            2 :                      CALL pw_scale(v_minus_vsccs, -1.0_dp)
    1168            2 :                      CALL integrate_v_core_rspace(v_minus_vsccs, qs_env, calpha=calpha, ccore=ccore)
    1169            2 :                      CALL auxbas_pw_pool%give_back_pw(v_minus_vsccs)
    1170            4 :                      DEALLOCATE (calpha, ccore)
    1171              :                   END BLOCK
    1172              :                END IF
    1173              :             END IF
    1174              :          END IF
    1175              : 
    1176       129417 :          IF (.NOT. do_hfx) THEN
    1177              :             ! Initialize the Kohn-Sham matrix with the core Hamiltonian matrix
    1178              :             ! (sets ks sparsity equal to matrix_h sparsity)
    1179       220523 :             DO ispin = 1, nspins
    1180      1459903 :                DO img = 1, nimages
    1181      1239380 :                   CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name) ! keep the name
    1182      1359730 :                   CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
    1183              :                END DO
    1184              :             END DO
    1185              :             ! imaginary part if required
    1186       100173 :             IF (qs_env%run_rtp) THEN
    1187         2672 :                IF (dft_control%rtp_control%velocity_gauge) THEN
    1188          414 :                   CPASSERT(ASSOCIATED(matrix_h_im))
    1189          414 :                   CPASSERT(ASSOCIATED(ks_matrix_im))
    1190          828 :                   DO ispin = 1, nspins
    1191         1242 :                      DO img = 1, nimages
    1192          414 :                         CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name) ! keep the name
    1193          828 :                         CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
    1194              :                      END DO
    1195              :                   END DO
    1196              :                END IF
    1197              :             END IF
    1198              :          END IF
    1199              : 
    1200       129417 :          IF (use_virial .AND. calculate_forces) THEN
    1201         7306 :             pv_loc = virial%pv_virial
    1202              :          END IF
    1203              :          ! sum up potentials and integrate
    1204              :          ! Pointing my_rho to the density matrix rho_ao
    1205       129417 :          my_rho => rho_ao
    1206              : 
    1207              :          CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
    1208              :                                    v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
    1209              :                                    v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
    1210       129417 :                                    cdft_control, calculate_forces)
    1211              : 
    1212       129417 :          IF (use_gauxc_matrix) THEN
    1213          594 :             CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc)
    1214          594 :             CPASSERT(ASSOCIATED(matrix_vxc))
    1215         1230 :             DO ispin = 1, nspins
    1216         1866 :                DO img = 1, nimages
    1217              :                   CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc(ispin, img)%matrix, &
    1218         1272 :                                  1.0_dp, 1.0_dp)
    1219              :                END DO
    1220              :             END DO
    1221              :          END IF
    1222              : 
    1223       129417 :          IF (gapw .OR. gapw_xc) THEN
    1224        28080 :             IF (calculate_forces) THEN
    1225          918 :                IF (gapw_xc) THEN
    1226          124 :                   CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
    1227              :                ELSE
    1228          794 :                   CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
    1229              :                END IF
    1230          918 :                NULLIFY (rho1)
    1231          918 :                IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
    1232              :                    .NOT. native_skala_evaluator) THEN
    1233            2 :                   gauxc_model_none = .FALSE.
    1234            2 :                   gauxc_section => get_gauxc_section(xc_section)
    1235            2 :                   IF (ASSOCIATED(gauxc_section)) THEN
    1236            2 :                      CALL section_vals_val_get(gauxc_section, "MODEL", c_val=gauxc_model_name)
    1237            2 :                      gauxc_model_name = ADJUSTL(gauxc_model_name)
    1238            2 :                      CALL uppercase(gauxc_model_name)
    1239              :                      gauxc_model_none = (TRIM(gauxc_model_name) == "" .OR. &
    1240            2 :                                          TRIM(gauxc_model_name) == "NONE")
    1241              :                   END IF
    1242            2 :                   IF (gauxc_model_none .AND. &
    1243              :                       (gapw_xc .OR. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
    1244            0 :                      CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
    1245              :                   END IF
    1246              :                ELSE
    1247          916 :                   CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
    1248              :                END IF
    1249              :                !
    1250          918 :                IF (dft_control%do_admm) THEN
    1251           92 :                   CALL get_qs_env(qs_env, admm_env=admm_env)
    1252           92 :                   xc_section => admm_env%xc_section_aux
    1253           92 :                   CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
    1254           92 :                   vscale = 1.0_dp
    1255           92 :                   IF (admm_env%do_admmp) THEN
    1256            8 :                      vscale = admm_env%gsi(1)**2
    1257           84 :                   ELSE IF (admm_env%do_admms) THEN
    1258            6 :                      vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
    1259              :                   END IF
    1260           92 :                   CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
    1261              :                END IF
    1262              :             END IF
    1263              :          END IF
    1264              : 
    1265       129417 :          IF (use_virial .AND. calculate_forces) THEN
    1266         7306 :             virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
    1267              :          END IF
    1268       129417 :          IF (dft_control%qs_control%do_kg) THEN
    1269          900 :             CPASSERT(nimages == 1)
    1270          900 :             ksmat => ks_matrix(:, 1)
    1271              : 
    1272          900 :             IF (use_virial .AND. calculate_forces) THEN
    1273          208 :                pv_loc = virial%pv_virial
    1274              :             END IF
    1275              : 
    1276          900 :             CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
    1277              :             ! subtract kg corr from the total energy
    1278          900 :             energy%exc = energy%exc - ekin_mol
    1279              : 
    1280              :             ! virial corrections
    1281          900 :             IF (use_virial .AND. calculate_forces) THEN
    1282              : 
    1283              :                ! Integral contribution
    1284          208 :                virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
    1285              : 
    1286              :                ! GGA contribution
    1287          208 :                virial%pv_exc = virial%pv_exc + virial%pv_xc
    1288          208 :                virial%pv_virial = virial%pv_virial + virial%pv_xc
    1289          208 :                virial%pv_xc = 0.0_dp
    1290              :             END IF
    1291              :          END IF
    1292              : 
    1293              :       ELSE
    1294              :          ! Atom-composite XC also creates potential buffers for energy-only calls.
    1295              :          ! These calls skip sum_up_and_integrate, which normally returns the buffers.
    1296        10748 :          IF (ASSOCIATED(v_rspace_new)) THEN
    1297            0 :             DO ispin = 1, SIZE(v_rspace_new)
    1298            0 :                CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
    1299              :             END DO
    1300            0 :             DEALLOCATE (v_rspace_new)
    1301              :          END IF
    1302        10748 :          IF (ASSOCIATED(v_tau_rspace)) THEN
    1303            0 :             DO ispin = 1, SIZE(v_tau_rspace)
    1304            0 :                CALL auxbas_pw_pool%give_back_pw(v_tau_rspace(ispin))
    1305              :             END DO
    1306            0 :             DEALLOCATE (v_tau_rspace)
    1307              :          END IF
    1308              :          IF (do_hfx) THEN
    1309              :             IF (.FALSE.) THEN
    1310              :                CPWARN("KS matrix no longer correct. Check possible problems with property calculations!")
    1311              :             END IF
    1312              :          END IF
    1313              :       END IF ! .NOT. just energy
    1314       140165 :       IF (dft_control%qs_control%ddapc_explicit_potential) THEN
    1315          164 :          CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
    1316          164 :          DEALLOCATE (v_spin_ddapc_rest_r)
    1317              :       END IF
    1318              : 
    1319       140165 :       IF (calculate_forces .AND. dft_control%qs_control%cdft) THEN
    1320          126 :          IF (.NOT. cdft_control%transfer_pot) THEN
    1321          230 :             DO iatom = 1, SIZE(cdft_control%group)
    1322          124 :                CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
    1323          230 :                DEALLOCATE (cdft_control%group(iatom)%weight)
    1324              :             END DO
    1325          106 :             IF (cdft_control%atomic_charges) THEN
    1326           78 :                DO iatom = 1, cdft_control%natoms
    1327           78 :                   CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
    1328              :                END DO
    1329           26 :                DEALLOCATE (cdft_control%charge)
    1330              :             END IF
    1331          106 :             IF (cdft_control%type == outer_scf_becke_constraint .AND. &
    1332              :                 cdft_control%becke_control%cavity_confine) THEN
    1333           88 :                IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
    1334           64 :                   CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
    1335              :                ELSE
    1336           24 :                   DEALLOCATE (cdft_control%becke_control%cavity_mat)
    1337              :                END IF
    1338           18 :             ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
    1339            6 :                IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
    1340            0 :                   CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
    1341              :                END IF
    1342              :             END IF
    1343          106 :             IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
    1344          106 :             cdft_control%save_pot = .FALSE.
    1345          106 :             cdft_control%need_pot = .TRUE.
    1346          106 :             cdft_control%external_control = .FALSE.
    1347              :          END IF
    1348              :       END IF
    1349              : 
    1350       140165 :       IF (dft_control%do_sccs) THEN
    1351          262 :          CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
    1352          262 :          DEALLOCATE (v_sccs_rspace)
    1353              :       END IF
    1354              : 
    1355       140165 :       IF (gapw) THEN
    1356        24682 :          IF (dft_control%apply_external_potential) THEN
    1357              :             ! Integrals of the Hartree potential with g0_soft
    1358              :             CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
    1359           42 :                                          v_qmmm=vee, scale=-1.0_dp)
    1360              :          END IF
    1361        24682 :          CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
    1362              :          ! Place Vh_1c_gg_integrals after integrate_vhg0_rspace for CNEO calculations
    1363              :          ! because vhg0 integral is needed to build the complete nuclear equation
    1364        24682 :          CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
    1365              :          CALL Vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.FALSE., &
    1366        24682 :                                  core_2nd=.FALSE.)
    1367              :          ! CNEO quantum nuclear core energy (kinetic + Z*erfc(r)/r potential from classical nuclei)
    1368        24682 :          energy%core_cneo = 0.0_dp
    1369        24682 :          IF (ASSOCIATED(local_rho_set%rhoz_cneo_set)) THEN
    1370          184 :             DO iatom = 1, SIZE(local_rho_set%rhoz_cneo_set)
    1371          184 :                energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
    1372              :             END DO
    1373              :          END IF
    1374              :       END IF
    1375              : 
    1376       140165 :       IF (gapw .OR. gapw_xc) THEN
    1377              :          ! Single atom contributions in the KS matrix ***
    1378        29128 :          CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
    1379        29128 :          IF (dft_control%do_admm) THEN
    1380              :             !Single atom contribution to the AUX matrices
    1381              :             !Note: also update ks_aux_fit matrix in case of rtp
    1382         4970 :             CALL admm_update_ks_atom(qs_env, calculate_forces)
    1383              :          END IF
    1384              :       END IF
    1385              : 
    1386              :       !Calculation of Mulliken restraint, if requested
    1387              :       CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
    1388       140165 :                                     ks_matrix, matrix_s, rho, mulliken_order_p)
    1389              : 
    1390              :       ! Add DFT+U contribution, if requested
    1391       140165 :       IF (dft_control%dft_plus_u) THEN
    1392         6564 :          IF (just_energy) THEN
    1393          896 :             CALL plus_u(qs_env=qs_env)
    1394              :          ELSE
    1395         5668 :             CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
    1396              :          END IF
    1397              :       ELSE
    1398       133601 :          energy%dft_plus_u = 0.0_dp
    1399              :       END IF
    1400              : 
    1401              :       ! At this point the ks matrix should be up to date, filter it if requested
    1402       310265 :       DO ispin = 1, nspins
    1403      1854115 :          DO img = 1, nimages
    1404              :             CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
    1405      1713950 :                               dft_control%qs_control%eps_filter_matrix)
    1406              :          END DO
    1407              :       END DO
    1408              : 
    1409              :       !** merge the auxiliary KS matrix and the primary one
    1410       140165 :       IF (dft_control%do_admm_mo) THEN
    1411        13692 :          IF (qs_env%run_rtp) THEN
    1412          112 :             CALL rtp_admm_merge_ks_matrix(qs_env)
    1413              :          ELSE
    1414        13580 :             CALL admm_mo_merge_ks_matrix(qs_env)
    1415              :          END IF
    1416       126473 :       ELSE IF (dft_control%do_admm_dm) THEN
    1417          214 :          CALL admm_dm_merge_ks_matrix(qs_env)
    1418              :       END IF
    1419              : 
    1420              :       ! External field (nonperiodic case)
    1421       140165 :       CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
    1422              : 
    1423              :       ! Right now we can compute the orbital derivative here, as it depends currently only on the available
    1424              :       ! Kohn-Sham matrix. This might change in the future, in which case more pieces might need to be assembled
    1425              :       ! from this routine, notice that this part of the calculation in not linear scaling
    1426              :       ! right now this operation is only non-trivial because of occupation numbers and the restricted keyword
    1427       140165 :       IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp) THEN
    1428        46747 :          CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
    1429        46747 :          CPASSERT(nimages == 1)
    1430        46747 :          ksmat => ks_matrix(:, 1)
    1431        46747 :          CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
    1432              :       END IF
    1433              : 
    1434              :       ! ADMM overlap forces
    1435       140165 :       IF (calculate_forces .AND. dft_control%do_admm) THEN
    1436          322 :          IF (dokp) THEN
    1437           34 :             CALL calc_admm_ovlp_forces_kp(qs_env)
    1438              :          ELSE
    1439          288 :             CALL calc_admm_ovlp_forces(qs_env)
    1440              :          END IF
    1441              :       END IF
    1442              : 
    1443              :       ! deal with low spin roks
    1444              :       CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
    1445       140165 :                          calculate_forces, auxbas_pw_pool)
    1446              : 
    1447              :       ! deal with sic on explicit orbitals
    1448              :       CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
    1449       140165 :                                  calculate_forces, auxbas_pw_pool)
    1450              : 
    1451              :       ! Periodic external field
    1452       140165 :       CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
    1453              : 
    1454              :       ! adds s2_restraint energy and orbital derivatives
    1455              :       CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
    1456       140165 :                               energy, calculate_forces, just_energy)
    1457              : 
    1458       140165 :       IF (do_ppl) THEN
    1459              :          ! update core energy for grid based local pseudopotential
    1460           60 :          ecore_ppl = 0._dp
    1461          126 :          DO ispin = 1, nspins
    1462          126 :             ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
    1463              :          END DO
    1464           60 :          energy%core = energy%core + ecore_ppl
    1465              :       END IF
    1466              : 
    1467       140165 :       IF (lrigpw) THEN
    1468              :          ! update core energy for ppl_ri method
    1469          468 :          CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
    1470          468 :          IF (lri_env%ppl_ri) THEN
    1471            8 :             ecore_ppl = 0._dp
    1472           16 :             DO ispin = 1, nspins
    1473            8 :                lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
    1474           16 :                CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
    1475              :             END DO
    1476            8 :             energy%core = energy%core + ecore_ppl
    1477              :          END IF
    1478              :       END IF
    1479              : 
    1480              :       ! Sum all energy terms to obtain the total energy
    1481       140165 :       IF (native_skala_restore_exc) THEN
    1482           66 :          energy%total = native_skala_total_scf
    1483           66 :          IF (abnormal_value(energy%total)) THEN
    1484              :             CALL cp_abort(__LOCATION__, &
    1485            0 :                           "SCF energy restored by native Skala is an abnormal value (NaN/Inf).")
    1486              :          END IF
    1487              :       ELSE
    1488       140099 :          CALL check_sum_energies(energy)
    1489              :       END IF
    1490              : 
    1491              :       ! Print detailed energy
    1492       140165 :       IF (my_print) THEN
    1493       140107 :          CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
    1494              :       END IF
    1495              : 
    1496       140165 :       CALL timestop(handle)
    1497              : 
    1498       420495 :    END SUBROUTINE qs_ks_build_kohn_sham_matrix
    1499              : 
    1500              : ! **************************************************************************************************
    1501              : !> \brief ...
    1502              : !> \param rho_tot_gspace ...
    1503              : !> \param qs_env ...
    1504              : !> \param rho ...
    1505              : !> \param skip_nuclear_density ...
    1506              : ! **************************************************************************************************
    1507       143953 :    SUBROUTINE calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
    1508              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                :: rho_tot_gspace
    1509              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1510              :       TYPE(qs_rho_type), POINTER                         :: rho
    1511              :       LOGICAL, INTENT(IN), OPTIONAL                      :: skip_nuclear_density
    1512              : 
    1513              :       INTEGER                                            :: ispin
    1514              :       LOGICAL                                            :: my_skip
    1515              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1516       143953 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
    1517              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho0_s_gs, rho_core, rhoz_cneo_s_gs
    1518              :       TYPE(qs_charges_type), POINTER                     :: qs_charges
    1519              : 
    1520       143953 :       my_skip = .FALSE.
    1521          932 :       IF (PRESENT(skip_nuclear_density)) my_skip = skip_nuclear_density
    1522              : 
    1523       143953 :       CALL qs_rho_get(rho, rho_g=rho_g)
    1524       143953 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
    1525              : 
    1526       143953 :       IF (.NOT. my_skip) THEN
    1527       143031 :          NULLIFY (rho_core)
    1528       143031 :          CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
    1529       143031 :          IF (dft_control%qs_control%gapw) THEN
    1530        25000 :             NULLIFY (rho0_s_gs, rhoz_cneo_s_gs)
    1531        25000 :             CALL get_qs_env(qs_env=qs_env, rho0_s_gs=rho0_s_gs, rhoz_cneo_s_gs=rhoz_cneo_s_gs)
    1532        25000 :             CPASSERT(ASSOCIATED(rho0_s_gs))
    1533        25000 :             CALL pw_copy(rho0_s_gs, rho_tot_gspace)
    1534        25000 :             IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
    1535           48 :                CALL pw_axpy(rhoz_cneo_s_gs, rho_tot_gspace)
    1536              :             END IF
    1537        25000 :             IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
    1538         2924 :                CALL pw_axpy(rho_core, rho_tot_gspace)
    1539              :             END IF
    1540              :          ELSE
    1541       118031 :             CALL pw_copy(rho_core, rho_tot_gspace)
    1542              :          END IF
    1543       316323 :          DO ispin = 1, dft_control%nspins
    1544       316323 :             CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
    1545              :          END DO
    1546       143031 :          CALL get_qs_env(qs_env=qs_env, qs_charges=qs_charges)
    1547       143031 :          qs_charges%total_rho_gspace = pw_integrate_function(rho_tot_gspace, isign=-1)
    1548              :       ELSE
    1549         1848 :          DO ispin = 1, dft_control%nspins
    1550         1848 :             CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
    1551              :          END DO
    1552              :       END IF
    1553              : 
    1554       143953 :    END SUBROUTINE calc_rho_tot_gspace
    1555              : 
    1556              : ! **************************************************************************************************
    1557              : !> \brief compute MO derivatives
    1558              : !> \param qs_env the qs_env to update
    1559              : !> \param ks_matrix ...
    1560              : !> \param mo_derivs ...
    1561              : !> \par History
    1562              : !>      01.2014 created, transferred from qs_ks_build_kohn_sham_matrix in
    1563              : !>      separate subroutine
    1564              : !> \author Dorothea Golze
    1565              : ! **************************************************************************************************
    1566        46747 :    SUBROUTINE calc_mo_derivatives(qs_env, ks_matrix, mo_derivs)
    1567              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1568              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_matrix, mo_derivs
    1569              : 
    1570              :       INTEGER                                            :: ispin
    1571              :       LOGICAL                                            :: uniform_occupation
    1572        46747 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: occupation_numbers
    1573              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1574              :       TYPE(dbcsr_type)                                   :: mo_derivs2_tmp1, mo_derivs2_tmp2
    1575              :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_b
    1576              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1577        46747 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
    1578              : 
    1579        46747 :       NULLIFY (dft_control, mo_array, mo_coeff, mo_coeff_b, occupation_numbers)
    1580              : 
    1581              :       CALL get_qs_env(qs_env, &
    1582              :                       dft_control=dft_control, &
    1583        46747 :                       mos=mo_array)
    1584              : 
    1585       103795 :       DO ispin = 1, SIZE(mo_derivs)
    1586              : 
    1587              :          CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, &
    1588        57048 :                          mo_coeff_b=mo_coeff_b, occupation_numbers=occupation_numbers)
    1589              :          CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin)%matrix, mo_coeff_b, &
    1590        57048 :                              0.0_dp, mo_derivs(ispin)%matrix)
    1591              : 
    1592       103795 :          IF (dft_control%restricted) THEN
    1593              :             ! only the first mo_set are actual variables, but we still need both
    1594          666 :             CPASSERT(ispin == 1)
    1595          666 :             CPASSERT(SIZE(mo_array) == 2)
    1596              :             ! use a temporary array with the same size as the first spin for the second spin
    1597              : 
    1598              :             ! uniform_occupation is needed for this case, otherwise we can not
    1599              :             ! reconstruct things in ot, since we irreversibly sum
    1600          666 :             CALL get_mo_set(mo_set=mo_array(1), uniform_occupation=uniform_occupation)
    1601          666 :             CPASSERT(uniform_occupation)
    1602          666 :             CALL get_mo_set(mo_set=mo_array(2), uniform_occupation=uniform_occupation)
    1603          666 :             CPASSERT(uniform_occupation)
    1604              : 
    1605              :             ! The beta-spin might have fewer orbitals than alpa-spin...
    1606              :             ! create temporary matrices with beta_nmo columns
    1607          666 :             CALL get_mo_set(mo_set=mo_array(2), mo_coeff_b=mo_coeff_b)
    1608          666 :             CALL dbcsr_create(mo_derivs2_tmp1, template=mo_coeff_b)
    1609              : 
    1610              :             ! calculate beta derivatives
    1611          666 :             CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(2)%matrix, mo_coeff_b, 0.0_dp, mo_derivs2_tmp1)
    1612              : 
    1613              :             ! create larger matrix with alpha_nmo columns
    1614          666 :             CALL dbcsr_create(mo_derivs2_tmp2, template=mo_derivs(1)%matrix)
    1615          666 :             CALL dbcsr_set(mo_derivs2_tmp2, 0.0_dp)
    1616              : 
    1617              :             ! copy into larger matrix, fills the first beta_nmo columns
    1618              :             CALL dbcsr_copy_columns_hack(mo_derivs2_tmp2, mo_derivs2_tmp1, &
    1619              :                                          mo_array(2)%nmo, 1, 1, &
    1620              :                                          para_env=mo_array(1)%mo_coeff%matrix_struct%para_env, &
    1621          666 :                                          blacs_env=mo_array(1)%mo_coeff%matrix_struct%context)
    1622              : 
    1623              :             ! add beta contribution to alpa mo_derivs
    1624          666 :             CALL dbcsr_add(mo_derivs(1)%matrix, mo_derivs2_tmp2, 1.0_dp, 1.0_dp)
    1625          666 :             CALL dbcsr_release(mo_derivs2_tmp1)
    1626          666 :             CALL dbcsr_release(mo_derivs2_tmp2)
    1627              :          END IF
    1628              :       END DO
    1629              : 
    1630        46747 :       IF (dft_control%do_admm_mo) THEN
    1631         6864 :          CALL calc_admm_mo_derivatives(qs_env, mo_derivs)
    1632              :       END IF
    1633              : 
    1634        46747 :    END SUBROUTINE calc_mo_derivatives
    1635              : 
    1636              : ! **************************************************************************************************
    1637              : !> \brief updates the Kohn Sham matrix of the given qs_env (facility method)
    1638              : !> \param qs_env the qs_env to update
    1639              : !> \param calculate_forces if true calculate the quantities needed
    1640              : !>        to calculate the forces. Defaults to false.
    1641              : !> \param just_energy if true updates the energies but not the
    1642              : !>        ks matrix. Defaults to false
    1643              : !> \param print_active ...
    1644              : !> \par History
    1645              : !>      4.2002 created [fawzi]
    1646              : !>      8.2014 kpoints [JGH]
    1647              : !>     10.2014 refractored [Ole Schuett]
    1648              : !> \author Fawzi Mohamed
    1649              : ! **************************************************************************************************
    1650       287140 :    SUBROUTINE qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, &
    1651              :                                   print_active)
    1652              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1653              :       LOGICAL, INTENT(IN), OPTIONAL                      :: calculate_forces, just_energy, &
    1654              :                                                             print_active
    1655              : 
    1656              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_update_qs_env'
    1657              : 
    1658              :       INTEGER                                            :: handle, unit_nr
    1659              :       LOGICAL                                            :: c_forces, do_rebuild, energy_only, &
    1660              :                                                             forces_up_to_date, potential_changed, &
    1661              :                                                             rho_changed, s_mstruct_changed
    1662              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1663              : 
    1664       287140 :       NULLIFY (ks_env)
    1665       287140 :       unit_nr = cp_logger_get_default_io_unit()
    1666              : 
    1667       287140 :       c_forces = .FALSE.
    1668       287140 :       energy_only = .FALSE.
    1669       287140 :       IF (PRESENT(just_energy)) energy_only = just_energy
    1670       287140 :       IF (PRESENT(calculate_forces)) c_forces = calculate_forces
    1671              : 
    1672       287140 :       IF (c_forces) THEN
    1673        10963 :          CALL timeset(routineN//'_forces', handle)
    1674              :       ELSE
    1675       276177 :          CALL timeset(routineN, handle)
    1676              :       END IF
    1677              : 
    1678       287140 :       CPASSERT(ASSOCIATED(qs_env))
    1679              : 
    1680              :       CALL get_qs_env(qs_env, &
    1681              :                       ks_env=ks_env, &
    1682              :                       rho_changed=rho_changed, &
    1683              :                       s_mstruct_changed=s_mstruct_changed, &
    1684              :                       potential_changed=potential_changed, &
    1685       287140 :                       forces_up_to_date=forces_up_to_date)
    1686              : 
    1687       287140 :       do_rebuild = .FALSE.
    1688       287140 :       do_rebuild = do_rebuild .OR. rho_changed
    1689         9109 :       do_rebuild = do_rebuild .OR. s_mstruct_changed
    1690         9109 :       do_rebuild = do_rebuild .OR. potential_changed
    1691         9109 :       do_rebuild = do_rebuild .OR. (c_forces .AND. .NOT. forces_up_to_date)
    1692              : 
    1693              :       IF (do_rebuild) THEN
    1694       278415 :          CALL evaluate_core_matrix_traces(qs_env)
    1695              : 
    1696              :          ! the ks matrix will be rebuilt so this is fine now
    1697       278415 :          CALL set_ks_env(ks_env, potential_changed=.FALSE.)
    1698              : 
    1699              :          CALL rebuild_ks_matrix(qs_env, &
    1700              :                                 calculate_forces=c_forces, &
    1701              :                                 just_energy=energy_only, &
    1702       278415 :                                 print_active=print_active)
    1703              : 
    1704       278415 :          IF (.NOT. energy_only) THEN
    1705              :             CALL set_ks_env(ks_env, &
    1706              :                             rho_changed=.FALSE., &
    1707              :                             s_mstruct_changed=.FALSE., &
    1708       513363 :                             forces_up_to_date=forces_up_to_date .OR. c_forces)
    1709              :          END IF
    1710              :       END IF
    1711              : 
    1712       287140 :       CALL timestop(handle)
    1713              : 
    1714       287140 :    END SUBROUTINE qs_ks_update_qs_env
    1715              : 
    1716              : ! **************************************************************************************************
    1717              : !> \brief Calculates the traces of the core matrices and the density matrix.
    1718              : !> \param qs_env ...
    1719              : !> \param rho_ao_ext ...
    1720              : !> \author Ole Schuett
    1721              : ! **************************************************************************************************
    1722       304057 :    SUBROUTINE evaluate_core_matrix_traces(qs_env, rho_ao_ext)
    1723              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1724              :       TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
    1725              :          POINTER                                         :: rho_ao_ext
    1726              : 
    1727              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_core_matrix_traces'
    1728              : 
    1729              :       INTEGER                                            :: handle
    1730              :       REAL(KIND=dp)                                      :: energy_core_im
    1731       304057 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrixkp_h, matrixkp_t, rho_ao_kp
    1732              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1733              :       TYPE(qs_energy_type), POINTER                      :: energy
    1734              :       TYPE(qs_rho_type), POINTER                         :: rho
    1735              : 
    1736       304057 :       CALL timeset(routineN, handle)
    1737       304057 :       NULLIFY (energy, rho, dft_control, rho_ao_kp, matrixkp_t, matrixkp_h)
    1738              : 
    1739              :       CALL get_qs_env(qs_env, &
    1740              :                       rho=rho, &
    1741              :                       energy=energy, &
    1742              :                       dft_control=dft_control, &
    1743              :                       kinetic_kp=matrixkp_t, &
    1744       304057 :                       matrix_h_kp=matrixkp_h)
    1745              : 
    1746       304057 :       IF (PRESENT(rho_ao_ext)) THEN
    1747        24780 :          rho_ao_kp => rho_ao_ext
    1748              :       ELSE
    1749       279277 :          CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
    1750              :       END IF
    1751              : 
    1752       304057 :       CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy%core, dft_control%nspins)
    1753              : 
    1754              :       ! kinetic energy
    1755       304057 :       IF (ASSOCIATED(matrixkp_t)) THEN
    1756       139749 :          CALL calculate_ptrace(matrixkp_t, rho_ao_kp, energy%kinetic, dft_control%nspins)
    1757              :       END IF
    1758              : 
    1759              :       ! Add the imaginary part in the RTP case: Re Tr(P*H) = P_re.H_re + P_im.H_im
    1760       304057 :       IF (qs_env%run_rtp) THEN
    1761         3928 :          IF (dft_control%rtp_control%velocity_gauge) THEN
    1762          414 :             CALL get_qs_env(qs_env, matrix_h_im_kp=matrixkp_h)
    1763          414 :             CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_kp)
    1764          414 :             CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy_core_im, dft_control%nspins)
    1765          414 :             energy%core = energy%core + energy_core_im
    1766              :          END IF
    1767              :       END IF
    1768              : 
    1769       304057 :       CALL timestop(handle)
    1770       304057 :    END SUBROUTINE evaluate_core_matrix_traces
    1771              : 
    1772              : ! **************************************************************************************************
    1773              : !> \brief Constructs a new Khon-Sham matrix
    1774              : !> \param qs_env ...
    1775              : !> \param calculate_forces ...
    1776              : !> \param just_energy ...
    1777              : !> \param print_active ...
    1778              : !> \author Ole Schuett
    1779              : ! **************************************************************************************************
    1780       278435 :    SUBROUTINE rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
    1781              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1782              :       LOGICAL, INTENT(IN)                                :: calculate_forces, just_energy
    1783              :       LOGICAL, INTENT(IN), OPTIONAL                      :: print_active
    1784              : 
    1785              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'rebuild_ks_matrix'
    1786              : 
    1787              :       INTEGER                                            :: handle
    1788              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1789              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
    1790              : 
    1791       278435 :       CALL timeset(routineN, handle)
    1792       278435 :       NULLIFY (dft_control, scf_env)
    1793              : 
    1794       278435 :       CALL get_qs_env(qs_env, dft_control=dft_control, scf_env=scf_env)
    1795       278435 :       IF (.NOT. just_energy .AND. ASSOCIATED(scf_env)) scf_env%ot_ks_valid = .FALSE.
    1796              : 
    1797       278435 :       IF (dft_control%qs_control%semi_empirical) THEN
    1798              :          CALL build_se_fock_matrix(qs_env, &
    1799              :                                    calculate_forces=calculate_forces, &
    1800        41346 :                                    just_energy=just_energy)
    1801              : 
    1802       237089 :       ELSE IF (dft_control%qs_control%dftb) THEN
    1803              :          CALL build_dftb_ks_matrix(qs_env, &
    1804              :                                    calculate_forces=calculate_forces, &
    1805        31420 :                                    just_energy=just_energy)
    1806              : 
    1807       205669 :       ELSE IF (dft_control%qs_control%xtb) THEN
    1808        65922 :          IF (dft_control%qs_control%xtb_control%do_tblite) THEN
    1809              :             CALL build_tblite_ks_matrix(qs_env, &
    1810              :                                         calculate_forces=calculate_forces, &
    1811        28230 :                                         just_energy=just_energy)
    1812              :          ELSE
    1813              :             CALL build_xtb_ks_matrix(qs_env, &
    1814              :                                      calculate_forces=calculate_forces, &
    1815        37692 :                                      just_energy=just_energy)
    1816              :          END IF
    1817              :       ELSE
    1818              :          CALL qs_ks_build_kohn_sham_matrix(qs_env, &
    1819              :                                            calculate_forces=calculate_forces, &
    1820              :                                            just_energy=just_energy, &
    1821       139747 :                                            print_active=print_active)
    1822              :       END IF
    1823              : 
    1824       278435 :       CALL timestop(handle)
    1825              : 
    1826       278435 :    END SUBROUTINE rebuild_ks_matrix
    1827              : 
    1828              : ! **************************************************************************************************
    1829              : !> \brief Allocate ks_matrix if necessary, take current overlap matrix as template
    1830              : !> \param qs_env ...
    1831              : !> \param is_complex ...
    1832              : !> \par History
    1833              : !>    refactoring 04.03.2011 [MI]
    1834              : !> \author
    1835              : ! **************************************************************************************************
    1836              : 
    1837        29966 :    SUBROUTINE qs_ks_allocate_basics(qs_env, is_complex)
    1838              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1839              :       LOGICAL, INTENT(in)                                :: is_complex
    1840              : 
    1841              :       CHARACTER(LEN=default_string_length)               :: headline
    1842              :       INTEGER                                            :: ic, ispin, nimages, nspins
    1843              :       LOGICAL                                            :: do_kpoints
    1844        29966 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_kp, matrixkp_im_ks, matrixkp_ks
    1845              :       TYPE(dbcsr_type), POINTER                          :: refmatrix
    1846              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1847              :       TYPE(kpoint_type), POINTER                         :: kpoints
    1848              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1849        29966 :          POINTER                                         :: sab_orb
    1850              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1851              : 
    1852        29966 :       NULLIFY (dft_control, ks_env, matrix_s_kp, sab_orb, matrixkp_ks, refmatrix, matrixkp_im_ks, kpoints)
    1853              : 
    1854              :       CALL get_qs_env(qs_env, &
    1855              :                       dft_control=dft_control, &
    1856              :                       matrix_s_kp=matrix_s_kp, &
    1857              :                       ks_env=ks_env, &
    1858              :                       kpoints=kpoints, &
    1859              :                       do_kpoints=do_kpoints, &
    1860              :                       matrix_ks_kp=matrixkp_ks, &
    1861        29966 :                       matrix_ks_im_kp=matrixkp_im_ks)
    1862              : 
    1863        29966 :       IF (do_kpoints) THEN
    1864         4126 :          CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
    1865              :       ELSE
    1866        25840 :          CALL get_qs_env(qs_env, sab_orb=sab_orb)
    1867              :       END IF
    1868              : 
    1869        29966 :       nspins = dft_control%nspins
    1870        29966 :       nimages = dft_control%nimages
    1871              : 
    1872        29966 :       IF (.NOT. ASSOCIATED(matrixkp_ks)) THEN
    1873        29910 :          CALL dbcsr_allocate_matrix_set(matrixkp_ks, nspins, nimages)
    1874        29910 :          refmatrix => matrix_s_kp(1, 1)%matrix
    1875        64134 :          DO ispin = 1, nspins
    1876       416046 :             DO ic = 1, nimages
    1877       351912 :                IF (nspins > 1) THEN
    1878        75524 :                   IF (ispin == 1) THEN
    1879        37762 :                      headline = "KOHN-SHAM MATRIX FOR ALPHA SPIN"
    1880              :                   ELSE
    1881        37762 :                      headline = "KOHN-SHAM MATRIX FOR BETA SPIN"
    1882              :                   END IF
    1883              :                ELSE
    1884       276388 :                   headline = "KOHN-SHAM MATRIX"
    1885              :                END IF
    1886       351912 :                ALLOCATE (matrixkp_ks(ispin, ic)%matrix)
    1887              :                CALL dbcsr_create(matrix=matrixkp_ks(ispin, ic)%matrix, template=refmatrix, &
    1888       351912 :                                  name=TRIM(headline), matrix_type=dbcsr_type_symmetric)
    1889       351912 :                CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_ks(ispin, ic)%matrix, sab_orb)
    1890       386136 :                CALL dbcsr_set(matrixkp_ks(ispin, ic)%matrix, 0.0_dp)
    1891              :             END DO
    1892              :          END DO
    1893        29910 :          CALL set_ks_env(ks_env, matrix_ks_kp=matrixkp_ks)
    1894              :       END IF
    1895              : 
    1896        29966 :       IF (is_complex) THEN
    1897          216 :          IF (.NOT. ASSOCIATED(matrixkp_im_ks)) THEN
    1898          216 :             CPASSERT(nspins == SIZE(matrixkp_ks, 1))
    1899          216 :             CPASSERT(nimages == SIZE(matrixkp_ks, 2))
    1900          216 :             CALL dbcsr_allocate_matrix_set(matrixkp_im_ks, nspins, nimages)
    1901          456 :             DO ispin = 1, nspins
    1902          696 :                DO ic = 1, nimages
    1903          240 :                   IF (nspins > 1) THEN
    1904           48 :                      IF (ispin == 1) THEN
    1905           24 :                         headline = "IMAGINARY KOHN-SHAM MATRIX FOR ALPHA SPIN"
    1906              :                      ELSE
    1907           24 :                         headline = "IMAGINARY KOHN-SHAM MATRIX FOR BETA SPIN"
    1908              :                      END IF
    1909              :                   ELSE
    1910          192 :                      headline = "IMAGINARY KOHN-SHAM MATRIX"
    1911              :                   END IF
    1912          240 :                   ALLOCATE (matrixkp_im_ks(ispin, ic)%matrix)
    1913          240 :                   refmatrix => matrixkp_ks(ispin, ic)%matrix  ! base on real part, but anti-symmetric
    1914              :                   CALL dbcsr_create(matrix=matrixkp_im_ks(ispin, ic)%matrix, template=refmatrix, &
    1915          240 :                                     name=TRIM(headline), matrix_type=dbcsr_type_antisymmetric)
    1916          240 :                   CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_im_ks(ispin, ic)%matrix, sab_orb)
    1917          480 :                   CALL dbcsr_set(matrixkp_im_ks(ispin, ic)%matrix, 0.0_dp)
    1918              :                END DO
    1919              :             END DO
    1920          216 :             CALL set_ks_env(ks_env, matrix_ks_im_kp=matrixkp_im_ks)
    1921              :          END IF
    1922              :       END IF
    1923              : 
    1924        29966 :    END SUBROUTINE qs_ks_allocate_basics
    1925              : 
    1926              : END MODULE qs_ks_methods
        

Generated by: LCOV version 2.0-1