LCOV - code coverage report
Current view: top level - src - hfx_admm_utils.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:262480d) Lines: 901 1054 85.5 %
Date: 2024-11-22 07:00:40 Functions: 11 11 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief Utilities for hfx and admm methods
      10             : !>
      11             : !>
      12             : !> \par History
      13             : !>     refactoring 03-2011 [MI]
      14             : !>     Made GAPW compatible 12.2019 (A. Bussy)
      15             : !> \author MI
      16             : ! **************************************************************************************************
      17             : MODULE hfx_admm_utils
      18             :    USE admm_dm_types,                   ONLY: admm_dm_create
      19             :    USE admm_methods,                    ONLY: kpoint_calc_admm_matrices,&
      20             :                                               scale_dm
      21             :    USE admm_types,                      ONLY: admm_env_create,&
      22             :                                               admm_gapw_r3d_rs_type,&
      23             :                                               admm_type,&
      24             :                                               get_admm_env,&
      25             :                                               set_admm_env
      26             :    USE atomic_kind_types,               ONLY: atomic_kind_type
      27             :    USE basis_set_container_types,       ONLY: add_basis_set_to_container
      28             :    USE basis_set_types,                 ONLY: copy_gto_basis_set,&
      29             :                                               get_gto_basis_set,&
      30             :                                               gto_basis_set_type
      31             :    USE cell_types,                      ONLY: cell_type,&
      32             :                                               plane_distance
      33             :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      34             :    USE cp_control_types,                ONLY: admm_control_type,&
      35             :                                               dft_control_type
      36             :    USE cp_dbcsr_api,                    ONLY: dbcsr_add,&
      37             :                                               dbcsr_copy,&
      38             :                                               dbcsr_create,&
      39             :                                               dbcsr_init_p,&
      40             :                                               dbcsr_p_type,&
      41             :                                               dbcsr_set,&
      42             :                                               dbcsr_type,&
      43             :                                               dbcsr_type_no_symmetry
      44             :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      45             :    USE cp_dbcsr_operations,             ONLY: cp_dbcsr_m_by_n_from_row_template,&
      46             :                                               dbcsr_allocate_matrix_set
      47             :    USE cp_fm_pool_types,                ONLY: cp_fm_pool_p_type
      48             :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      49             :                                               cp_fm_struct_release,&
      50             :                                               cp_fm_struct_type
      51             :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      52             :                                               cp_fm_get_info,&
      53             :                                               cp_fm_type
      54             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      55             :                                               cp_logger_get_default_io_unit,&
      56             :                                               cp_logger_type,&
      57             :                                               cp_to_string
      58             :    USE distribution_1d_types,           ONLY: distribution_1d_type
      59             :    USE distribution_2d_types,           ONLY: distribution_2d_type
      60             :    USE external_potential_types,        ONLY: copy_potential
      61             :    USE hfx_derivatives,                 ONLY: derivatives_four_center
      62             :    USE hfx_energy_potential,            ONLY: integrate_four_center
      63             :    USE hfx_pw_methods,                  ONLY: pw_hfx
      64             :    USE hfx_ri,                          ONLY: hfx_ri_update_forces,&
      65             :                                               hfx_ri_update_ks
      66             :    USE hfx_ri_kp,                       ONLY: hfx_ri_update_forces_kp,&
      67             :                                               hfx_ri_update_ks_kp
      68             :    USE hfx_types,                       ONLY: hfx_type
      69             :    USE input_constants,                 ONLY: &
      70             :         do_admm_aux_exch_func_bee, do_admm_aux_exch_func_bee_libxc, do_admm_aux_exch_func_default, &
      71             :         do_admm_aux_exch_func_default_libxc, do_admm_aux_exch_func_none, &
      72             :         do_admm_aux_exch_func_opt, do_admm_aux_exch_func_opt_libxc, do_admm_aux_exch_func_pbex, &
      73             :         do_admm_aux_exch_func_pbex_libxc, do_admm_aux_exch_func_sx_libxc, &
      74             :         do_admm_basis_projection, do_admm_charge_constrained_projection, do_admm_purify_none, &
      75             :         do_potential_coulomb, do_potential_id, do_potential_long, do_potential_mix_cl, &
      76             :         do_potential_mix_cl_trunc, do_potential_short, do_potential_truncated, &
      77             :         xc_funct_no_shortcut, xc_none
      78             :    USE input_section_types,             ONLY: section_vals_duplicate,&
      79             :                                               section_vals_get,&
      80             :                                               section_vals_get_subs_vals,&
      81             :                                               section_vals_get_subs_vals2,&
      82             :                                               section_vals_remove_values,&
      83             :                                               section_vals_type,&
      84             :                                               section_vals_val_get,&
      85             :                                               section_vals_val_set
      86             :    USE kinds,                           ONLY: dp
      87             :    USE kpoint_methods,                  ONLY: kpoint_initialize_mos
      88             :    USE kpoint_transitional,             ONLY: kpoint_transitional_release,&
      89             :                                               set_2d_pointer
      90             :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      91             :                                               kpoint_type
      92             :    USE libint_2c_3c,                    ONLY: cutoff_screen_factor
      93             :    USE mathlib,                         ONLY: erfc_cutoff
      94             :    USE message_passing,                 ONLY: mp_para_env_type
      95             :    USE molecule_types,                  ONLY: molecule_type
      96             :    USE particle_types,                  ONLY: particle_type
      97             :    USE paw_proj_set_types,              ONLY: get_paw_proj_set,&
      98             :                                               paw_proj_set_type
      99             :    USE pw_env_types,                    ONLY: pw_env_get,&
     100             :                                               pw_env_type
     101             :    USE pw_poisson_types,                ONLY: pw_poisson_type
     102             :    USE pw_pool_types,                   ONLY: pw_pool_type
     103             :    USE pw_types,                        ONLY: pw_r3d_rs_type
     104             :    USE qs_energy_types,                 ONLY: qs_energy_type
     105             :    USE qs_environment_types,            ONLY: get_qs_env,&
     106             :                                               qs_environment_type,&
     107             :                                               set_qs_env
     108             :    USE qs_interactions,                 ONLY: init_interaction_radii
     109             :    USE qs_kind_types,                   ONLY: get_qs_kind,&
     110             :                                               get_qs_kind_set,&
     111             :                                               init_gapw_basis_set,&
     112             :                                               init_gapw_nlcc,&
     113             :                                               qs_kind_type
     114             :    USE qs_ks_types,                     ONLY: qs_ks_env_type
     115             :    USE qs_local_rho_types,              ONLY: local_rho_set_create
     116             :    USE qs_matrix_pools,                 ONLY: mpools_get
     117             :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
     118             :                                               get_mo_set,&
     119             :                                               init_mo_set,&
     120             :                                               mo_set_type
     121             :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type,&
     122             :                                               release_neighbor_list_sets
     123             :    USE qs_neighbor_lists,               ONLY: atom2d_build,&
     124             :                                               atom2d_cleanup,&
     125             :                                               build_neighbor_lists,&
     126             :                                               local_atoms_type,&
     127             :                                               pair_radius_setup,&
     128             :                                               write_neighbor_lists
     129             :    USE qs_oce_methods,                  ONLY: build_oce_matrices
     130             :    USE qs_oce_types,                    ONLY: allocate_oce_set,&
     131             :                                               create_oce_set
     132             :    USE qs_overlap,                      ONLY: build_overlap_matrix
     133             :    USE qs_rho_atom_methods,             ONLY: init_rho_atom
     134             :    USE qs_rho_methods,                  ONLY: qs_rho_rebuild
     135             :    USE qs_rho_types,                    ONLY: qs_rho_create,&
     136             :                                               qs_rho_get,&
     137             :                                               qs_rho_type
     138             :    USE rt_propagation_types,            ONLY: rt_prop_type
     139             :    USE task_list_methods,               ONLY: generate_qs_task_list
     140             :    USE task_list_types,                 ONLY: allocate_task_list,&
     141             :                                               deallocate_task_list
     142             :    USE virial_types,                    ONLY: virial_type
     143             :    USE xc_adiabatic_utils,              ONLY: rescale_xc_potential
     144             : #include "./base/base_uses.f90"
     145             : 
     146             :    IMPLICIT NONE
     147             : 
     148             :    PRIVATE
     149             : 
     150             :    ! *** Public subroutines ***
     151             :    PUBLIC :: hfx_ks_matrix, hfx_admm_init, aux_admm_init, create_admm_xc_section, &
     152             :              tddft_hfx_matrix, hfx_ks_matrix_kp
     153             : 
     154             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_admm_utils'
     155             : 
     156             : CONTAINS
     157             : 
     158             : ! **************************************************************************************************
     159             : !> \brief ...
     160             : !> \param qs_env ...
     161             : !> \param calculate_forces ...
     162             : ! **************************************************************************************************
     163       20328 :    SUBROUTINE hfx_admm_init(qs_env, calculate_forces)
     164             : 
     165             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     166             :       LOGICAL, INTENT(IN), OPTIONAL                      :: calculate_forces
     167             : 
     168             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'hfx_admm_init'
     169             : 
     170             :       INTEGER                                            :: handle, ispin, n_rep_hf, nao_aux_fit, &
     171             :                                                             natoms, nelectron, nmo
     172             :       LOGICAL                                            :: calc_forces, do_kpoints, &
     173             :                                                             s_mstruct_changed, use_virial
     174             :       REAL(dp)                                           :: maxocc
     175             :       TYPE(admm_type), POINTER                           :: admm_env
     176             :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     177             :       TYPE(cp_fm_struct_type), POINTER                   :: aux_fit_fm_struct
     178             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff_aux_fit
     179       10164 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_aux_fit_kp
     180             :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_b
     181             :       TYPE(dft_control_type), POINTER                    :: dft_control
     182       10164 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos, mos_aux_fit
     183             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     184       10164 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     185             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     186             :       TYPE(section_vals_type), POINTER                   :: hfx_sections, input, xc_section
     187             :       TYPE(virial_type), POINTER                         :: virial
     188             : 
     189       10164 :       CALL timeset(routineN, handle)
     190             : 
     191       10164 :       NULLIFY (admm_env, hfx_sections, mos, mos_aux_fit, para_env, virial, &
     192       10164 :                mo_coeff_aux_fit, xc_section, ks_env, dft_control, input, &
     193       10164 :                qs_kind_set, mo_coeff_b, aux_fit_fm_struct, blacs_env)
     194             : 
     195             :       CALL get_qs_env(qs_env, &
     196             :                       mos=mos, &
     197             :                       admm_env=admm_env, &
     198             :                       para_env=para_env, &
     199             :                       blacs_env=blacs_env, &
     200             :                       s_mstruct_changed=s_mstruct_changed, &
     201             :                       ks_env=ks_env, &
     202             :                       dft_control=dft_control, &
     203             :                       input=input, &
     204             :                       virial=virial, &
     205       10164 :                       do_kpoints=do_kpoints)
     206             : 
     207       10164 :       calc_forces = .FALSE.
     208       10164 :       IF (PRESENT(calculate_forces)) calc_forces = .TRUE.
     209             : 
     210       10164 :       hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%HF")
     211             : 
     212       10164 :       CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
     213       10164 :       IF (n_rep_hf > 1) &
     214           0 :          CPABORT("ADMM can handle only one HF section.")
     215             : 
     216       10164 :       IF (.NOT. ASSOCIATED(admm_env)) THEN
     217             :          ! setup admm environment
     218         442 :          CALL get_qs_env(qs_env, input=input, natom=natoms, qs_kind_set=qs_kind_set)
     219         442 :          CALL get_qs_kind_set(qs_kind_set, nsgf=nao_aux_fit, basis_type="AUX_FIT")
     220         442 :          CALL admm_env_create(admm_env, dft_control%admm_control, mos, para_env, natoms, nao_aux_fit)
     221         442 :          CALL set_qs_env(qs_env, admm_env=admm_env)
     222         442 :          xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     223             :          CALL create_admm_xc_section(x_data=qs_env%x_data, xc_section=xc_section, &
     224         442 :                                      admm_env=admm_env)
     225             : 
     226             :          ! Initialize the GAPW data types
     227         442 :          IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) &
     228          84 :             CALL init_admm_gapw(qs_env)
     229             : 
     230             :          ! ADMM neighbor lists and overlap matrices
     231         442 :          CALL admm_init_hamiltonians(admm_env, qs_env, "AUX_FIT")
     232             : 
     233             :          !The aux_fit task list and densities
     234         442 :          ALLOCATE (admm_env%rho_aux_fit)
     235         442 :          CALL qs_rho_create(admm_env%rho_aux_fit)
     236         442 :          ALLOCATE (admm_env%rho_aux_fit_buffer)
     237         442 :          CALL qs_rho_create(admm_env%rho_aux_fit_buffer)
     238         442 :          CALL admm_update_s_mstruct(admm_env, qs_env, "AUX_FIT")
     239         442 :          IF (admm_env%do_gapw) CALL update_admm_gapw(qs_env)
     240             : 
     241             :          !The ADMM KS matrices
     242         442 :          CALL admm_alloc_ks_matrices(admm_env, qs_env)
     243             : 
     244             :          !The aux_fit MOs and derivatives
     245        1884 :          ALLOCATE (mos_aux_fit(dft_control%nspins))
     246        1000 :          DO ispin = 1, dft_control%nspins
     247         558 :             CALL get_mo_set(mo_set=mos(ispin), nmo=nmo, nelectron=nelectron, maxocc=maxocc)
     248             :             CALL allocate_mo_set(mo_set=mos_aux_fit(ispin), &
     249             :                                  nao=nao_aux_fit, &
     250             :                                  nmo=nmo, &
     251             :                                  nelectron=nelectron, &
     252             :                                  n_el_f=REAL(nelectron, dp), &
     253             :                                  maxocc=maxocc, &
     254        1000 :                                  flexible_electron_count=dft_control%relax_multiplicity)
     255             :          END DO
     256         442 :          admm_env%mos_aux_fit => mos_aux_fit
     257             : 
     258        1000 :          DO ispin = 1, dft_control%nspins
     259         558 :             CALL get_mo_set(mo_set=mos(ispin), nmo=nmo)
     260             :             CALL cp_fm_struct_create(aux_fit_fm_struct, context=blacs_env, para_env=para_env, &
     261         558 :                                      nrow_global=nao_aux_fit, ncol_global=nmo)
     262         558 :             CALL get_mo_set(mos_aux_fit(ispin), mo_coeff=mo_coeff_aux_fit, mo_coeff_b=mo_coeff_b)
     263         558 :             IF (.NOT. ASSOCIATED(mo_coeff_aux_fit)) THEN
     264             :                CALL init_mo_set(mos_aux_fit(ispin), fm_struct=aux_fit_fm_struct, &
     265         558 :                                 name="qs_env%mo_aux_fit"//TRIM(ADJUSTL(cp_to_string(ispin))))
     266             :             END IF
     267         558 :             CALL cp_fm_struct_release(aux_fit_fm_struct)
     268             : 
     269        1558 :             IF (.NOT. ASSOCIATED(mo_coeff_b)) THEN
     270         558 :                CALL cp_fm_get_info(mos_aux_fit(ispin)%mo_coeff, ncol_global=nmo)
     271         558 :                CALL dbcsr_init_p(mos_aux_fit(ispin)%mo_coeff_b)
     272         558 :                CALL get_admm_env(admm_env, matrix_s_aux_fit_kp=matrix_s_aux_fit_kp)
     273             :                CALL cp_dbcsr_m_by_n_from_row_template(mos_aux_fit(ispin)%mo_coeff_b, &
     274             :                                                       template=matrix_s_aux_fit_kp(1, 1)%matrix, &
     275         558 :                                                       n=nmo, sym=dbcsr_type_no_symmetry)
     276             :             END IF
     277             :          END DO
     278             : 
     279         442 :          IF (qs_env%requires_mo_derivs) THEN
     280        1038 :             ALLOCATE (admm_env%mo_derivs_aux_fit(dft_control%nspins))
     281         550 :             DO ispin = 1, dft_control%nspins
     282         306 :                CALL get_mo_set(admm_env%mos_aux_fit(ispin), mo_coeff=mo_coeff_aux_fit)
     283         550 :                CALL cp_fm_create(admm_env%mo_derivs_aux_fit(ispin), mo_coeff_aux_fit%matrix_struct)
     284             :             END DO
     285             :          END IF
     286             : 
     287         884 :          IF (do_kpoints) THEN
     288             :             BLOCK
     289             :                TYPE(kpoint_type), POINTER :: kpoints
     290          28 :                TYPE(mo_set_type), DIMENSION(:, :), POINTER           :: mos_aux_fit_kp
     291          28 :                TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER        :: ao_mo_fm_pools_aux_fit
     292             :                TYPE(cp_fm_struct_type), POINTER                      :: ao_ao_fm_struct
     293             :                INTEGER                                               :: ic, ik, ikk, is
     294             :                INTEGER, PARAMETER                                    :: nwork1 = 4
     295             :                LOGICAL                                               :: use_real_wfn
     296             : 
     297          28 :                NULLIFY (ao_mo_fm_pools_aux_fit, mos_aux_fit_kp)
     298             : 
     299          28 :                CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
     300          28 :                CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn)
     301             : 
     302             :                !Test combinations of input values. So far, only ADMM2 is availavle
     303          28 :                IF (.NOT. admm_env%purification_method == do_admm_purify_none) &
     304           0 :                   CPABORT("Only ADMM_PURIFICATION_METHOD NONE implemeted for ADMM K-points")
     305          28 :                IF (.NOT. (dft_control%admm_control%method == do_admm_basis_projection &
     306             :                           .OR. dft_control%admm_control%method == do_admm_charge_constrained_projection)) &
     307           0 :                   CPABORT("Only BASIS_PROJECTION and CHARGE_CONSTRAINED_PROJECTION implemented for KP")
     308          28 :                IF (admm_env%do_admms .OR. admm_env%do_admmp .OR. admm_env%do_admmq) THEN
     309          12 :                   IF (use_real_wfn) CPABORT("Only KP-HFX ADMM2 is implemented with REAL wavefunctions")
     310             :                END IF
     311             : 
     312          28 :                CALL kpoint_initialize_mos(kpoints, admm_env%mos_aux_fit, for_aux_fit=.TRUE.)
     313             : 
     314          28 :                CALL mpools_get(kpoints%mpools_aux_fit, ao_mo_fm_pools=ao_mo_fm_pools_aux_fit)
     315         188 :                DO ik = 1, SIZE(kpoints%kp_aux_env)
     316         160 :                   mos_aux_fit_kp => kpoints%kp_aux_env(ik)%kpoint_env%mos
     317         160 :                   ikk = kpoints%kp_range(1) + ik - 1
     318         396 :                   DO ispin = 1, SIZE(mos_aux_fit_kp, 2)
     319         784 :                      DO ic = 1, SIZE(mos_aux_fit_kp, 1)
     320         416 :                         CALL get_mo_set(mos_aux_fit_kp(ic, ispin), mo_coeff=mo_coeff_aux_fit, mo_coeff_b=mo_coeff_b)
     321             : 
     322             :                         ! no sparse matrix representation of kpoint MO vectors
     323         416 :                         CPASSERT(.NOT. ASSOCIATED(mo_coeff_b))
     324             : 
     325         624 :                         IF (.NOT. ASSOCIATED(mo_coeff_aux_fit)) THEN
     326             :                            CALL init_mo_set(mos_aux_fit_kp(ic, ispin), &
     327             :                                             fm_pool=ao_mo_fm_pools_aux_fit(ispin)%pool, &
     328             :                                             name="kpoints_"//TRIM(ADJUSTL(cp_to_string(ikk)))// &
     329         416 :                                             "%mo_aux_fit"//TRIM(ADJUSTL(cp_to_string(ispin))))
     330             :                         END IF
     331             :                      END DO
     332             :                   END DO
     333             :                END DO
     334             : 
     335         140 :                ALLOCATE (admm_env%scf_work_aux_fit(nwork1))
     336             : 
     337             :                ! create an ao_ao parallel matrix structure
     338             :                CALL cp_fm_struct_create(ao_ao_fm_struct, context=blacs_env, para_env=para_env, &
     339             :                                         nrow_global=nao_aux_fit, &
     340          28 :                                         ncol_global=nao_aux_fit)
     341             : 
     342         140 :                DO is = 1, nwork1
     343             :                   CALL cp_fm_create(admm_env%scf_work_aux_fit(is), &
     344             :                                     matrix_struct=ao_ao_fm_struct, &
     345         140 :                                     name="SCF-WORK_MATRIX-AUX-"//TRIM(ADJUSTL(cp_to_string(is))))
     346             :                END DO
     347          28 :                CALL cp_fm_struct_release(ao_ao_fm_struct)
     348             : 
     349             :                ! Create and populate the internal ADMM overlap matrices at each KP
     350          56 :                CALL kpoint_calc_admm_matrices(qs_env, calc_forces)
     351             : 
     352             :             END BLOCK
     353             :          END IF
     354             : 
     355        9722 :       ELSE IF (s_mstruct_changed) THEN
     356         356 :          CALL admm_init_hamiltonians(admm_env, qs_env, "AUX_FIT")
     357         356 :          CALL admm_update_s_mstruct(admm_env, qs_env, "AUX_FIT")
     358         356 :          CALL admm_alloc_ks_matrices(admm_env, qs_env)
     359         356 :          IF (admm_env%do_gapw) CALL update_admm_gapw(qs_env)
     360         356 :          IF (do_kpoints) CALL kpoint_calc_admm_matrices(qs_env, calc_forces)
     361             :       END IF
     362             : 
     363       10164 :       IF (admm_env%do_gapw .AND. dft_control%do_admm_dm) THEN
     364           0 :          CPABORT("GAPW ADMM not implemented for MCWEENY or NONE_DM purification.")
     365             :       END IF
     366             : 
     367             :       !ADMMS and ADMMP stress tensors only available for close-shell systesms, because virial cannot
     368             :       !be scaled by gsi spin component wise
     369       10164 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     370        1186 :       IF (use_virial .AND. admm_env%do_admms .AND. dft_control%nspins == 2) THEN
     371           0 :          CPABORT("ADMMS stress tensor is only available for closed-shell systems")
     372             :       END IF
     373        1186 :       IF (use_virial .AND. admm_env%do_admmp .AND. dft_control%nspins == 2) THEN
     374           0 :          CPABORT("ADMMP stress tensor is only available for closed-shell systems")
     375             :       END IF
     376             : 
     377       10164 :       IF (dft_control%do_admm_dm .AND. .NOT. ASSOCIATED(admm_env%admm_dm)) THEN
     378          14 :          CALL admm_dm_create(admm_env%admm_dm, dft_control%admm_control, nspins=dft_control%nspins, natoms=natoms)
     379             :       END IF
     380             : 
     381       10164 :       CALL timestop(handle)
     382             : 
     383       10164 :    END SUBROUTINE hfx_admm_init
     384             : 
     385             : ! **************************************************************************************************
     386             : !> \brief Minimal setup routine for admm_env
     387             : !>        No forces
     388             : !>        No k-points
     389             : !>        No DFT correction terms
     390             : !> \param qs_env ...
     391             : !> \param mos ...
     392             : !> \param admm_env ...
     393             : !> \param admm_control ...
     394             : !> \param basis_type ...
     395             : ! **************************************************************************************************
     396           4 :    SUBROUTINE aux_admm_init(qs_env, mos, admm_env, admm_control, basis_type)
     397             : 
     398             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     399             :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     400             :       TYPE(admm_type), POINTER                           :: admm_env
     401             :       TYPE(admm_control_type), POINTER                   :: admm_control
     402             :       CHARACTER(LEN=*)                                   :: basis_type
     403             : 
     404             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'aux_admm_init'
     405             : 
     406             :       INTEGER                                            :: handle, ispin, nao_aux_fit, natoms, &
     407             :                                                             nelectron, nmo
     408             :       LOGICAL                                            :: do_kpoints
     409             :       REAL(dp)                                           :: maxocc
     410             :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     411             :       TYPE(cp_fm_struct_type), POINTER                   :: aux_fit_fm_struct
     412             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff_aux_fit
     413           4 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_aux_fit_kp
     414             :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_b
     415             :       TYPE(dft_control_type), POINTER                    :: dft_control
     416           4 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos_aux_fit
     417             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     418           4 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     419             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     420             : 
     421           4 :       CALL timeset(routineN, handle)
     422             : 
     423           4 :       CPASSERT(.NOT. ASSOCIATED(admm_env))
     424             : 
     425             :       CALL get_qs_env(qs_env, &
     426             :                       para_env=para_env, &
     427             :                       blacs_env=blacs_env, &
     428             :                       ks_env=ks_env, &
     429             :                       dft_control=dft_control, &
     430           4 :                       do_kpoints=do_kpoints)
     431             : 
     432           4 :       CPASSERT(.NOT. do_kpoints)
     433           4 :       IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
     434           0 :          CPABORT("AUX ADMM not possible with GAPW")
     435             :       END IF
     436             : 
     437             :       ! setup admm environment
     438           4 :       CALL get_qs_env(qs_env, natom=natoms, qs_kind_set=qs_kind_set)
     439           4 :       CALL get_qs_kind_set(qs_kind_set, nsgf=nao_aux_fit, basis_type=basis_type)
     440             :       !
     441           4 :       CALL admm_env_create(admm_env, admm_control, mos, para_env, natoms, nao_aux_fit)
     442             :       ! no XC correction used
     443           4 :       NULLIFY (admm_env%xc_section_aux, admm_env%xc_section_primary)
     444             :       ! ADMM neighbor lists and overlap matrices
     445           4 :       CALL admm_init_hamiltonians(admm_env, qs_env, basis_type)
     446           4 :       NULLIFY (admm_env%rho_aux_fit, admm_env%rho_aux_fit_buffer)
     447             :       !The ADMM KS matrices
     448           4 :       CALL admm_alloc_ks_matrices(admm_env, qs_env)
     449             :       !The aux_fit MOs and derivatives
     450          16 :       ALLOCATE (mos_aux_fit(dft_control%nspins))
     451           8 :       DO ispin = 1, dft_control%nspins
     452           4 :          CALL get_mo_set(mo_set=mos(ispin), nmo=nmo, nelectron=nelectron, maxocc=maxocc)
     453             :          CALL allocate_mo_set(mo_set=mos_aux_fit(ispin), nao=nao_aux_fit, nmo=nmo, &
     454             :                               nelectron=nelectron, n_el_f=REAL(nelectron, dp), &
     455           8 :                               maxocc=maxocc, flexible_electron_count=0.0_dp)
     456             :       END DO
     457           4 :       admm_env%mos_aux_fit => mos_aux_fit
     458             : 
     459           8 :       DO ispin = 1, dft_control%nspins
     460           4 :          CALL get_mo_set(mo_set=mos(ispin), nmo=nmo)
     461             :          CALL cp_fm_struct_create(aux_fit_fm_struct, context=blacs_env, para_env=para_env, &
     462           4 :                                   nrow_global=nao_aux_fit, ncol_global=nmo)
     463           4 :          CALL get_mo_set(mos_aux_fit(ispin), mo_coeff=mo_coeff_aux_fit, mo_coeff_b=mo_coeff_b)
     464           4 :          IF (.NOT. ASSOCIATED(mo_coeff_aux_fit)) THEN
     465             :             CALL init_mo_set(mos_aux_fit(ispin), fm_struct=aux_fit_fm_struct, &
     466           4 :                              name="mo_aux_fit"//TRIM(ADJUSTL(cp_to_string(ispin))))
     467             :          END IF
     468           4 :          CALL cp_fm_struct_release(aux_fit_fm_struct)
     469             : 
     470          12 :          IF (.NOT. ASSOCIATED(mo_coeff_b)) THEN
     471           4 :             CALL cp_fm_get_info(mos_aux_fit(ispin)%mo_coeff, ncol_global=nmo)
     472           4 :             CALL dbcsr_init_p(mos_aux_fit(ispin)%mo_coeff_b)
     473           4 :             CALL get_admm_env(admm_env, matrix_s_aux_fit_kp=matrix_s_aux_fit_kp)
     474             :             CALL cp_dbcsr_m_by_n_from_row_template(mos_aux_fit(ispin)%mo_coeff_b, &
     475             :                                                    template=matrix_s_aux_fit_kp(1, 1)%matrix, &
     476           4 :                                                    n=nmo, sym=dbcsr_type_no_symmetry)
     477             :          END IF
     478             :       END DO
     479             : 
     480           4 :       CALL timestop(handle)
     481             : 
     482           8 :    END SUBROUTINE aux_admm_init
     483             : 
     484             : ! **************************************************************************************************
     485             : !> \brief Sets up the admm_gapw env
     486             : !> \param qs_env ...
     487             : ! **************************************************************************************************
     488          84 :    SUBROUTINE init_admm_gapw(qs_env)
     489             : 
     490             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     491             : 
     492             :       INTEGER                                            :: ikind, nkind
     493             :       TYPE(admm_gapw_r3d_rs_type), POINTER               :: admm_gapw_env
     494             :       TYPE(admm_type), POINTER                           :: admm_env
     495          84 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     496             :       TYPE(dft_control_type), POINTER                    :: dft_control
     497             :       TYPE(gto_basis_set_type), POINTER                  :: aux_fit_basis, aux_fit_soft_basis, &
     498             :                                                             orb_basis, soft_basis
     499             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     500          84 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: admm_kind_set, qs_kind_set
     501             :       TYPE(section_vals_type), POINTER                   :: input
     502             : 
     503          84 :       NULLIFY (admm_kind_set, aux_fit_basis, atomic_kind_set, aux_fit_soft_basis, &
     504          84 :                dft_control, input, orb_basis, para_env, qs_kind_set, soft_basis)
     505             : 
     506             :       CALL get_qs_env(qs_env, admm_env=admm_env, &
     507             :                       atomic_kind_set=atomic_kind_set, &
     508             :                       dft_control=dft_control, &
     509             :                       input=input, &
     510             :                       para_env=para_env, &
     511          84 :                       qs_kind_set=qs_kind_set)
     512             : 
     513          84 :       admm_env%do_gapw = .TRUE.
     514          84 :       ALLOCATE (admm_env%admm_gapw_env)
     515          84 :       admm_gapw_env => admm_env%admm_gapw_env
     516          84 :       NULLIFY (admm_gapw_env%local_rho_set)
     517          84 :       NULLIFY (admm_gapw_env%admm_kind_set)
     518          84 :       NULLIFY (admm_gapw_env%task_list)
     519             : 
     520             :       !Create a new kind set for the ADMM stuff (paw_proj soft AUX_FIT basis, etc)
     521          84 :       nkind = SIZE(qs_kind_set)
     522        2094 :       ALLOCATE (admm_gapw_env%admm_kind_set(nkind))
     523          84 :       admm_kind_set => admm_gapw_env%admm_kind_set
     524             : 
     525             :       !In this new kind set, we want the AUX_FIT basis to be known as ORB, such that GAPW routines work
     526         246 :       DO ikind = 1, nkind
     527             :          !copying over simple data  of interest from qs_kind_set
     528         162 :          admm_kind_set(ikind)%name = qs_kind_set(ikind)%name
     529         162 :          admm_kind_set(ikind)%element_symbol = qs_kind_set(ikind)%element_symbol
     530         162 :          admm_kind_set(ikind)%natom = qs_kind_set(ikind)%natom
     531         162 :          admm_kind_set(ikind)%hard_radius = qs_kind_set(ikind)%hard_radius
     532         162 :          admm_kind_set(ikind)%max_rad_local = qs_kind_set(ikind)%max_rad_local
     533         162 :          admm_kind_set(ikind)%gpw_type_forced = qs_kind_set(ikind)%gpw_type_forced
     534         162 :          admm_kind_set(ikind)%ngrid_rad = qs_kind_set(ikind)%ngrid_rad
     535         162 :          admm_kind_set(ikind)%ngrid_ang = qs_kind_set(ikind)%ngrid_ang
     536             : 
     537             :          !copying potentials of interest from qs_kind_set
     538         162 :          IF (ASSOCIATED(qs_kind_set(ikind)%all_potential)) THEN
     539          32 :             CALL copy_potential(qs_kind_set(ikind)%all_potential, admm_kind_set(ikind)%all_potential)
     540             :          END IF
     541         162 :          IF (ASSOCIATED(qs_kind_set(ikind)%gth_potential)) THEN
     542         130 :             CALL copy_potential(qs_kind_set(ikind)%gth_potential, admm_kind_set(ikind)%gth_potential)
     543             :          END IF
     544         162 :          IF (ASSOCIATED(qs_kind_set(ikind)%sgp_potential)) THEN
     545           0 :             CALL copy_potential(qs_kind_set(ikind)%sgp_potential, admm_kind_set(ikind)%sgp_potential)
     546             :          END IF
     547             : 
     548         162 :          NULLIFY (orb_basis)
     549         162 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=aux_fit_basis, basis_type="AUX_FIT")
     550         162 :          CALL copy_gto_basis_set(aux_fit_basis, orb_basis)
     551         246 :          CALL add_basis_set_to_container(admm_kind_set(ikind)%basis_sets, orb_basis, "ORB")
     552             :       END DO
     553             : 
     554             :       !Create the corresponding soft basis set (and projectors)
     555             :       CALL init_gapw_basis_set(admm_kind_set, dft_control%qs_control, input, &
     556          84 :                                modify_qs_control=.FALSE.)
     557             : 
     558             :       !Make sure the basis and the projectors are well initialized
     559          84 :       CALL init_interaction_radii(dft_control%qs_control, admm_kind_set)
     560             : 
     561             :       !We also init the atomic grids and harmonics
     562          84 :       CALL local_rho_set_create(admm_gapw_env%local_rho_set)
     563             :       CALL init_rho_atom(admm_gapw_env%local_rho_set%rho_atom_set, &
     564          84 :                          atomic_kind_set, admm_kind_set, dft_control, para_env)
     565             : 
     566             :       !Make sure that any NLCC potential is well initialized
     567          84 :       CALL init_gapw_nlcc(admm_kind_set)
     568             : 
     569             :       !Need to have access to the soft AUX_FIT basis from the qs_env => add it to the qs_kinds
     570         246 :       DO ikind = 1, nkind
     571         162 :          NULLIFY (aux_fit_soft_basis)
     572         162 :          CALL get_qs_kind(admm_kind_set(ikind), basis_set=soft_basis, basis_type="ORB_SOFT")
     573         162 :          CALL copy_gto_basis_set(soft_basis, aux_fit_soft_basis)
     574         246 :          CALL add_basis_set_to_container(qs_kind_set(ikind)%basis_sets, aux_fit_soft_basis, "AUX_FIT_SOFT")
     575             :       END DO
     576             : 
     577          84 :    END SUBROUTINE init_admm_gapw
     578             : 
     579             : ! **************************************************************************************************
     580             : !> \brief Builds the ADMM nmeighbor lists and overlap matrix on the model of qs_energies_init_hamiltonians()
     581             : !> \param admm_env ...
     582             : !> \param qs_env ...
     583             : !> \param aux_basis_type ...
     584             : ! **************************************************************************************************
     585         802 :    SUBROUTINE admm_init_hamiltonians(admm_env, qs_env, aux_basis_type)
     586             : 
     587             :       TYPE(admm_type), POINTER                           :: admm_env
     588             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     589             :       CHARACTER(len=*)                                   :: aux_basis_type
     590             : 
     591             :       CHARACTER(len=*), PARAMETER :: routineN = 'admm_init_hamiltonians'
     592             : 
     593             :       INTEGER                                            :: handle, hfx_pot, ikind, nkind
     594             :       LOGICAL                                            :: do_kpoints, mic, molecule_only
     595             :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: aux_fit_present, orb_present
     596             :       REAL(dp)                                           :: eps_schwarz, omega, pdist, roperator, &
     597             :                                                             subcells
     598             :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: aux_fit_radius, orb_radius
     599             :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: pair_radius
     600         802 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     601             :       TYPE(cell_type), POINTER                           :: cell
     602         802 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_aux_fit_kp, &
     603         802 :                                                             matrix_s_aux_fit_vs_orb_kp
     604             :       TYPE(dft_control_type), POINTER                    :: dft_control
     605             :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
     606             :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
     607             :       TYPE(gto_basis_set_type), POINTER                  :: aux_fit_basis_set, orb_basis_set
     608             :       TYPE(kpoint_type), POINTER                         :: kpoints
     609         802 :       TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:)  :: atom2d
     610         802 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     611             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     612         802 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     613         802 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     614             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     615             :       TYPE(section_vals_type), POINTER                   :: hfx_sections, neighbor_list_section
     616             : 
     617         802 :       NULLIFY (particle_set, cell, kpoints, distribution_1d, distribution_2d, molecule_set, &
     618         802 :                atomic_kind_set, dft_control, neighbor_list_section, aux_fit_basis_set, orb_basis_set, &
     619         802 :                ks_env, para_env, qs_kind_set, matrix_s_aux_fit_kp, matrix_s_aux_fit_vs_orb_kp)
     620             : 
     621         802 :       CALL timeset(routineN, handle)
     622             : 
     623             :       CALL get_qs_env(qs_env, nkind=nkind, particle_set=particle_set, cell=cell, kpoints=kpoints, &
     624             :                       local_particles=distribution_1d, distribution_2d=distribution_2d, &
     625             :                       molecule_set=molecule_set, atomic_kind_set=atomic_kind_set, do_kpoints=do_kpoints, &
     626         802 :                       dft_control=dft_control, para_env=para_env, qs_kind_set=qs_kind_set)
     627        3208 :       ALLOCATE (orb_present(nkind), aux_fit_present(nkind))
     628        5614 :       ALLOCATE (orb_radius(nkind), aux_fit_radius(nkind), pair_radius(nkind, nkind))
     629        2286 :       aux_fit_radius(:) = 0.0_dp
     630             : 
     631         802 :       molecule_only = .FALSE.
     632         802 :       IF (dft_control%qs_control%do_kg) molecule_only = .TRUE.
     633         802 :       mic = molecule_only
     634         802 :       IF (kpoints%nkp > 0) THEN
     635          38 :          mic = .FALSE.
     636         764 :       ELSEIF (dft_control%qs_control%semi_empirical) THEN
     637           0 :          mic = .TRUE.
     638             :       END IF
     639             : 
     640         802 :       pdist = dft_control%qs_control%pairlist_radius
     641             : 
     642         802 :       CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
     643         802 :       neighbor_list_section => section_vals_get_subs_vals(qs_env%input, "DFT%PRINT%NEIGHBOR_LISTS")
     644             : 
     645        3890 :       ALLOCATE (atom2d(nkind))
     646             :       CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
     647         802 :                         molecule_set, molecule_only, particle_set=particle_set)
     648             : 
     649        2286 :       DO ikind = 1, nkind
     650        1484 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, basis_type="ORB")
     651        1484 :          IF (ASSOCIATED(orb_basis_set)) THEN
     652        1484 :             orb_present(ikind) = .TRUE.
     653        1484 :             CALL get_gto_basis_set(gto_basis_set=orb_basis_set, kind_radius=orb_radius(ikind))
     654             :          ELSE
     655           0 :             orb_present(ikind) = .FALSE.
     656             :          END IF
     657             : 
     658        1484 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=aux_fit_basis_set, basis_type=aux_basis_type)
     659        2286 :          IF (ASSOCIATED(aux_fit_basis_set)) THEN
     660        1484 :             aux_fit_present(ikind) = .TRUE.
     661        1484 :             CALL get_gto_basis_set(gto_basis_set=aux_fit_basis_set, kind_radius=aux_fit_radius(ikind))
     662             :          ELSE
     663           0 :             aux_fit_present(ikind) = .FALSE.
     664             :          END IF
     665             :       END DO
     666             : 
     667         802 :       IF (pdist < 0.0_dp) THEN
     668             :          pdist = MAX(plane_distance(1, 0, 0, cell), &
     669             :                      plane_distance(0, 1, 0, cell), &
     670           2 :                      plane_distance(0, 0, 1, cell))
     671             :       END IF
     672             : 
     673             :       !In case of K-points, we need to add the HFX potential range to sab_aux_fit, because it is used
     674             :       !to populate AUX density and KS matrices
     675         802 :       roperator = 0.0_dp
     676         802 :       IF (do_kpoints) THEN
     677          38 :          hfx_sections => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF")
     678          38 :          CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%POTENTIAL_TYPE", i_val=hfx_pot)
     679             : 
     680             :          SELECT CASE (hfx_pot)
     681             :          CASE (do_potential_id)
     682          26 :             roperator = 0.0_dp
     683             :          CASE (do_potential_truncated)
     684          26 :             CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%CUTOFF_RADIUS", r_val=roperator)
     685             :          CASE (do_potential_short)
     686           0 :             CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%OMEGA", r_val=omega)
     687           0 :             CALL section_vals_val_get(hfx_sections, "SCREENING%EPS_SCHWARZ", r_val=eps_schwarz)
     688           0 :             CALL erfc_cutoff(eps_schwarz, omega, roperator)
     689             :          CASE DEFAULT
     690          38 :             CPABORT("HFX potential not available for K-points (NYI)")
     691             :          END SELECT
     692             :       END IF
     693             : 
     694         802 :       CALL pair_radius_setup(aux_fit_present, aux_fit_present, aux_fit_radius, aux_fit_radius, pair_radius, pdist)
     695        5274 :       pair_radius = pair_radius + cutoff_screen_factor*roperator
     696             :       CALL build_neighbor_lists(admm_env%sab_aux_fit, particle_set, atom2d, cell, pair_radius, &
     697         802 :                                 mic=mic, molecular=molecule_only, subcells=subcells, nlname="sab_aux_fit")
     698             :       CALL build_neighbor_lists(admm_env%sab_aux_fit_asymm, particle_set, atom2d, cell, pair_radius, &
     699             :                                 mic=mic, symmetric=.FALSE., molecular=molecule_only, subcells=subcells, &
     700         802 :                                 nlname="sab_aux_fit_asymm")
     701         802 :       CALL pair_radius_setup(aux_fit_present, orb_present, aux_fit_radius, orb_radius, pair_radius)
     702             :       CALL build_neighbor_lists(admm_env%sab_aux_fit_vs_orb, particle_set, atom2d, cell, pair_radius, &
     703             :                                 mic=mic, symmetric=.FALSE., molecular=molecule_only, subcells=subcells, &
     704         802 :                                 nlname="sab_aux_fit_vs_orb")
     705             : 
     706             :       CALL write_neighbor_lists(admm_env%sab_aux_fit, particle_set, cell, para_env, neighbor_list_section, &
     707         802 :                                 "/SAB_AUX_FIT", "sab_aux_fit", "AUX_FIT_ORBITAL AUX_FIT_ORBITAL")
     708             :       CALL write_neighbor_lists(admm_env%sab_aux_fit_vs_orb, particle_set, cell, para_env, neighbor_list_section, &
     709         802 :                                 "/SAB_AUX_FIT_VS_ORB", "sab_aux_fit_vs_orb", "ORBITAL AUX_FIT_ORBITAL")
     710             : 
     711         802 :       CALL atom2d_cleanup(atom2d)
     712             : 
     713             :       !The ADMM overlap matrices (initially in qs_core_hamiltonian.F)
     714         802 :       CALL get_qs_env(qs_env, ks_env=ks_env)
     715             : 
     716         802 :       CALL kpoint_transitional_release(admm_env%matrix_s_aux_fit)
     717             :       CALL build_overlap_matrix(ks_env, matrixkp_s=matrix_s_aux_fit_kp, &
     718             :                                 matrix_name="AUX_FIT_OVERLAP", &
     719             :                                 basis_type_a=aux_basis_type, &
     720             :                                 basis_type_b=aux_basis_type, &
     721         802 :                                 sab_nl=admm_env%sab_aux_fit)
     722         802 :       CALL set_2d_pointer(admm_env%matrix_s_aux_fit, matrix_s_aux_fit_kp)
     723         802 :       CALL kpoint_transitional_release(admm_env%matrix_s_aux_fit_vs_orb)
     724             :       CALL build_overlap_matrix(ks_env, matrixkp_s=matrix_s_aux_fit_vs_orb_kp, &
     725             :                                 matrix_name="MIXED_OVERLAP", &
     726             :                                 basis_type_a=aux_basis_type, &
     727             :                                 basis_type_b="ORB", &
     728         802 :                                 sab_nl=admm_env%sab_aux_fit_vs_orb)
     729         802 :       CALL set_2d_pointer(admm_env%matrix_s_aux_fit_vs_orb, matrix_s_aux_fit_vs_orb_kp)
     730             : 
     731         802 :       CALL timestop(handle)
     732             : 
     733        2406 :    END SUBROUTINE admm_init_hamiltonians
     734             : 
     735             : ! **************************************************************************************************
     736             : !> \brief Updates the ADMM task_list and density based on the model of qs_env_update_s_mstruct()
     737             : !> \param admm_env ...
     738             : !> \param qs_env ...
     739             : !> \param aux_basis_type ...
     740             : ! **************************************************************************************************
     741         798 :    SUBROUTINE admm_update_s_mstruct(admm_env, qs_env, aux_basis_type)
     742             : 
     743             :       TYPE(admm_type), POINTER                           :: admm_env
     744             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     745             :       CHARACTER(len=*)                                   :: aux_basis_type
     746             : 
     747             :       CHARACTER(len=*), PARAMETER :: routineN = 'admm_update_s_mstruct'
     748             : 
     749             :       INTEGER                                            :: handle
     750             :       LOGICAL                                            :: skip_load_balance_distributed
     751             :       TYPE(dft_control_type), POINTER                    :: dft_control
     752             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     753             : 
     754         798 :       NULLIFY (ks_env, dft_control)
     755             : 
     756         798 :       CALL timeset(routineN, handle)
     757             : 
     758         798 :       CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control)
     759             : 
     760             :       !The aux_fit task_list
     761         798 :       skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
     762         798 :       IF (ASSOCIATED(admm_env%task_list_aux_fit)) CALL deallocate_task_list(admm_env%task_list_aux_fit)
     763         798 :       CALL allocate_task_list(admm_env%task_list_aux_fit)
     764             :       CALL generate_qs_task_list(ks_env, admm_env%task_list_aux_fit, &
     765             :                                  reorder_rs_grid_ranks=.FALSE., soft_valid=.FALSE., &
     766             :                                  basis_type=aux_basis_type, &
     767             :                                  skip_load_balance_distributed=skip_load_balance_distributed, &
     768         798 :                                  sab_orb_external=admm_env%sab_aux_fit)
     769             : 
     770             :       !The aux_fit densities
     771         798 :       CALL qs_rho_rebuild(admm_env%rho_aux_fit, qs_env=qs_env, admm=.TRUE.)
     772         798 :       CALL qs_rho_rebuild(admm_env%rho_aux_fit_buffer, qs_env=qs_env, admm=.TRUE.)
     773             : 
     774         798 :       CALL timestop(handle)
     775             : 
     776         798 :    END SUBROUTINE admm_update_s_mstruct
     777             : 
     778             : ! **************************************************************************************************
     779             : !> \brief Update the admm_gapw_env internals to the current qs_env (i.e. atomic positions)
     780             : !> \param qs_env ...
     781             : ! **************************************************************************************************
     782         240 :    SUBROUTINE update_admm_gapw(qs_env)
     783             : 
     784             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     785             : 
     786             :       CHARACTER(len=*), PARAMETER                        :: routineN = 'update_admm_gapw'
     787             : 
     788             :       INTEGER                                            :: handle, ikind, nkind
     789             :       LOGICAL                                            :: paw_atom
     790             :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: aux_present, oce_present
     791             :       REAL(dp)                                           :: subcells
     792             :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: aux_radius, oce_radius
     793         240 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: pair_radius
     794             :       TYPE(admm_gapw_r3d_rs_type), POINTER               :: admm_gapw_env
     795             :       TYPE(admm_type), POINTER                           :: admm_env
     796         240 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     797             :       TYPE(cell_type), POINTER                           :: cell
     798             :       TYPE(dft_control_type), POINTER                    :: dft_control
     799             :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
     800             :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
     801             :       TYPE(gto_basis_set_type), POINTER                  :: aux_fit_basis
     802         240 :       TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:)  :: atom2d
     803         240 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     804             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     805         240 :          POINTER                                         :: sap_oce
     806         240 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     807             :       TYPE(paw_proj_set_type), POINTER                   :: paw_proj
     808         240 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: admm_kind_set, qs_kind_set
     809             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     810             : 
     811         240 :       NULLIFY (ks_env, qs_kind_set, admm_kind_set, aux_fit_basis, cell, distribution_1d)
     812         240 :       NULLIFY (distribution_2d, paw_proj, particle_set, molecule_set, admm_env, admm_gapw_env)
     813         240 :       NULLIFY (dft_control, atomic_kind_set, sap_oce)
     814             : 
     815         240 :       CALL timeset(routineN, handle)
     816             : 
     817             :       CALL get_qs_env(qs_env, ks_env=ks_env, qs_kind_set=qs_kind_set, admm_env=admm_env, &
     818         240 :                       dft_control=dft_control)
     819         240 :       admm_gapw_env => admm_env%admm_gapw_env
     820         240 :       admm_kind_set => admm_gapw_env%admm_kind_set
     821         240 :       nkind = SIZE(qs_kind_set)
     822             : 
     823             :       !Update the task lisft for the AUX_FIT_SOFT basis
     824         240 :       IF (ASSOCIATED(admm_gapw_env%task_list)) CALL deallocate_task_list(admm_gapw_env%task_list)
     825         240 :       CALL allocate_task_list(admm_gapw_env%task_list)
     826             : 
     827             :       !note: we set soft_valid to .FALSE. want to use AUX_FIT_SOFT and not the normal ORB SOFT basis
     828             :       CALL generate_qs_task_list(ks_env, admm_gapw_env%task_list, reorder_rs_grid_ranks=.FALSE., &
     829             :                                  soft_valid=.FALSE., basis_type="AUX_FIT_SOFT", &
     830             :                                  skip_load_balance_distributed=dft_control%qs_control%skip_load_balance_distributed, &
     831         240 :                                  sab_orb_external=admm_env%sab_aux_fit)
     832             : 
     833             :       !Update the precomputed oce integrals
     834             :       !a sap_oce neighbor list is required => build it here
     835         960 :       ALLOCATE (aux_present(nkind), oce_present(nkind))
     836        1480 :       aux_present = .FALSE.; oce_present = .FALSE.
     837         960 :       ALLOCATE (aux_radius(nkind), oce_radius(nkind))
     838        1480 :       aux_radius = 0.0_dp; oce_radius = 0.0_dp
     839             : 
     840         740 :       DO ikind = 1, nkind
     841         500 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=aux_fit_basis, basis_type="AUX_FIT")
     842         500 :          IF (ASSOCIATED(aux_fit_basis)) THEN
     843         500 :             aux_present(ikind) = .TRUE.
     844         500 :             CALL get_gto_basis_set(aux_fit_basis, kind_radius=aux_radius(ikind))
     845             :          END IF
     846             : 
     847             :          !note: get oce info from admm_kind_set
     848         500 :          CALL get_qs_kind(admm_kind_set(ikind), paw_atom=paw_atom, paw_proj_set=paw_proj)
     849         740 :          IF (paw_atom) THEN
     850         316 :             oce_present(ikind) = .TRUE.
     851         316 :             CALL get_paw_proj_set(paw_proj, rcprj=oce_radius(ikind))
     852             :          END IF
     853             :       END DO
     854             : 
     855         960 :       ALLOCATE (pair_radius(nkind, nkind))
     856        1852 :       pair_radius = 0.0_dp
     857         240 :       CALL pair_radius_setup(aux_present, oce_present, aux_radius, oce_radius, pair_radius)
     858             : 
     859             :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
     860             :                       distribution_2d=distribution_2d, local_particles=distribution_1d, &
     861         240 :                       particle_set=particle_set, molecule_set=molecule_set)
     862         240 :       CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
     863             : 
     864        1220 :       ALLOCATE (atom2d(nkind))
     865             :       CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
     866         240 :                         molecule_set, .FALSE., particle_set)
     867             :       CALL build_neighbor_lists(sap_oce, particle_set, atom2d, cell, pair_radius, &
     868         240 :                                 subcells=subcells, operator_type="ABBA", nlname="AUX_PAW-PRJ")
     869         240 :       CALL atom2d_cleanup(atom2d)
     870             : 
     871             :       !actually compute the oce matrices
     872         240 :       CALL create_oce_set(admm_gapw_env%oce)
     873         240 :       CALL allocate_oce_set(admm_gapw_env%oce, nkind)
     874             : 
     875             :       !always compute the derivative, cheap anyways
     876             :       CALL build_oce_matrices(admm_gapw_env%oce%intac, calculate_forces=.TRUE., nder=1, &
     877             :                               qs_kind_set=admm_kind_set, particle_set=particle_set, &
     878         240 :                               sap_oce=sap_oce, eps_fit=dft_control%qs_control%gapw_control%eps_fit)
     879             : 
     880         240 :       CALL release_neighbor_list_sets(sap_oce)
     881             : 
     882         240 :       CALL timestop(handle)
     883             : 
     884         720 :    END SUBROUTINE update_admm_gapw
     885             : 
     886             : ! **************************************************************************************************
     887             : !> \brief Allocates the various ADMM KS matrices
     888             : !> \param admm_env ...
     889             : !> \param qs_env ...
     890             : ! **************************************************************************************************
     891         802 :    SUBROUTINE admm_alloc_ks_matrices(admm_env, qs_env)
     892             : 
     893             :       TYPE(admm_type), POINTER                           :: admm_env
     894             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     895             : 
     896             :       CHARACTER(len=*), PARAMETER :: routineN = 'admm_alloc_ks_matrices'
     897             : 
     898             :       INTEGER                                            :: handle, ic, ispin
     899         802 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_aux_fit_dft_kp, &
     900         802 :                                                             matrix_ks_aux_fit_hfx_kp, &
     901         802 :                                                             matrix_ks_aux_fit_kp, &
     902         802 :                                                             matrix_s_aux_fit_kp
     903             :       TYPE(dft_control_type), POINTER                    :: dft_control
     904             : 
     905         802 :       NULLIFY (dft_control, matrix_s_aux_fit_kp, matrix_ks_aux_fit_kp, matrix_ks_aux_fit_dft_kp, matrix_ks_aux_fit_hfx_kp)
     906             : 
     907         802 :       CALL timeset(routineN, handle)
     908             : 
     909         802 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     910         802 :       CALL get_admm_env(admm_env, matrix_s_aux_fit_kp=matrix_s_aux_fit_kp)
     911             : 
     912         802 :       CALL kpoint_transitional_release(admm_env%matrix_ks_aux_fit)
     913         802 :       CALL kpoint_transitional_release(admm_env%matrix_ks_aux_fit_dft)
     914         802 :       CALL kpoint_transitional_release(admm_env%matrix_ks_aux_fit_hfx)
     915             : 
     916         802 :       CALL dbcsr_allocate_matrix_set(matrix_ks_aux_fit_kp, dft_control%nspins, dft_control%nimages)
     917         802 :       CALL dbcsr_allocate_matrix_set(matrix_ks_aux_fit_dft_kp, dft_control%nspins, dft_control%nimages)
     918         802 :       CALL dbcsr_allocate_matrix_set(matrix_ks_aux_fit_hfx_kp, dft_control%nspins, dft_control%nimages)
     919             : 
     920        1772 :       DO ispin = 1, dft_control%nspins
     921        5184 :          DO ic = 1, dft_control%nimages
     922        3412 :             ALLOCATE (matrix_ks_aux_fit_kp(ispin, ic)%matrix)
     923             :             CALL dbcsr_create(matrix_ks_aux_fit_kp(ispin, ic)%matrix, template=matrix_s_aux_fit_kp(1, ic)%matrix, &
     924        3412 :                               name="KOHN-SHAM_MATRIX for ADMM")
     925        3412 :             CALL cp_dbcsr_alloc_block_from_nbl(matrix_ks_aux_fit_kp(ispin, ic)%matrix, admm_env%sab_aux_fit)
     926        3412 :             CALL dbcsr_set(matrix_ks_aux_fit_kp(ispin, ic)%matrix, 0.0_dp)
     927             : 
     928        3412 :             ALLOCATE (matrix_ks_aux_fit_dft_kp(ispin, ic)%matrix)
     929             :             CALL dbcsr_create(matrix_ks_aux_fit_dft_kp(ispin, ic)%matrix, template=matrix_s_aux_fit_kp(1, 1)%matrix, &
     930        3412 :                               name="KOHN-SHAM_MATRIX for ADMM")
     931        3412 :             CALL cp_dbcsr_alloc_block_from_nbl(matrix_ks_aux_fit_dft_kp(ispin, ic)%matrix, admm_env%sab_aux_fit)
     932        3412 :             CALL dbcsr_set(matrix_ks_aux_fit_dft_kp(ispin, ic)%matrix, 0.0_dp)
     933             : 
     934        3412 :             ALLOCATE (matrix_ks_aux_fit_hfx_kp(ispin, ic)%matrix)
     935             :             CALL dbcsr_create(matrix_ks_aux_fit_hfx_kp(ispin, ic)%matrix, template=matrix_s_aux_fit_kp(1, 1)%matrix, &
     936        3412 :                               name="KOHN-SHAM_MATRIX for ADMM")
     937        3412 :             CALL cp_dbcsr_alloc_block_from_nbl(matrix_ks_aux_fit_hfx_kp(ispin, ic)%matrix, admm_env%sab_aux_fit)
     938        4382 :             CALL dbcsr_set(matrix_ks_aux_fit_hfx_kp(ispin, ic)%matrix, 0.0_dp)
     939             :          END DO
     940             :       END DO
     941             : 
     942             :       CALL set_admm_env(admm_env, &
     943             :                         matrix_ks_aux_fit_kp=matrix_ks_aux_fit_kp, &
     944             :                         matrix_ks_aux_fit_dft_kp=matrix_ks_aux_fit_dft_kp, &
     945         802 :                         matrix_ks_aux_fit_hfx_kp=matrix_ks_aux_fit_hfx_kp)
     946             : 
     947         802 :       CALL timestop(handle)
     948             : 
     949         802 :    END SUBROUTINE admm_alloc_ks_matrices
     950             : 
     951             : ! **************************************************************************************************
     952             : !> \brief Add the HFX K-point contribution to the real-space Hamiltonians
     953             : !> \param qs_env ...
     954             : !> \param matrix_ks ...
     955             : !> \param energy ...
     956             : !> \param calculate_forces ...
     957             : ! **************************************************************************************************
     958         190 :    SUBROUTINE hfx_ks_matrix_kp(qs_env, matrix_ks, energy, calculate_forces)
     959             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     960             :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks
     961             :       TYPE(qs_energy_type), POINTER                      :: energy
     962             :       LOGICAL, INTENT(in)                                :: calculate_forces
     963             : 
     964             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'hfx_ks_matrix_kp'
     965             : 
     966             :       INTEGER                                            :: handle, img, irep, ispin, n_rep_hf, &
     967             :                                                             nimages, nspins
     968             :       LOGICAL                                            :: do_adiabatic_rescaling, &
     969             :                                                             s_mstruct_changed, use_virial
     970             :       REAL(dp)                                           :: eh1, ehfx, eold
     971         190 :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: hf_energy
     972         190 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_ks_aux_fit_im, matrix_ks_im
     973         190 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h, matrix_ks_aux_fit_hfx_kp, &
     974         190 :                                                             matrix_ks_aux_fit_kp, matrix_ks_orb, &
     975         190 :                                                             rho_ao_orb
     976             :       TYPE(dft_control_type), POINTER                    :: dft_control
     977         190 :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
     978             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     979             :       TYPE(pw_env_type), POINTER                         :: pw_env
     980             :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
     981             :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     982             :       TYPE(qs_rho_type), POINTER                         :: rho_orb
     983             :       TYPE(section_vals_type), POINTER                   :: adiabatic_rescaling_section, &
     984             :                                                             hfx_sections, input
     985             :       TYPE(virial_type), POINTER                         :: virial
     986             : 
     987         190 :       CALL timeset(routineN, handle)
     988             : 
     989         190 :       NULLIFY (auxbas_pw_pool, dft_control, hfx_sections, input, &
     990         190 :                para_env, poisson_env, pw_env, virial, matrix_ks_im, &
     991         190 :                matrix_ks_orb, rho_ao_orb, matrix_h, matrix_ks_aux_fit_kp, &
     992         190 :                matrix_ks_aux_fit_im, matrix_ks_aux_fit_hfx_kp)
     993             : 
     994             :       CALL get_qs_env(qs_env=qs_env, &
     995             :                       dft_control=dft_control, &
     996             :                       input=input, &
     997             :                       matrix_h_kp=matrix_h, &
     998             :                       para_env=para_env, &
     999             :                       pw_env=pw_env, &
    1000             :                       virial=virial, &
    1001             :                       matrix_ks_im=matrix_ks_im, &
    1002             :                       s_mstruct_changed=s_mstruct_changed, &
    1003         190 :                       x_data=x_data)
    1004             : 
    1005             :       ! No RTP
    1006         190 :       IF (qs_env%run_rtp) CPABORT("No RTP implementation with K-points HFX")
    1007             : 
    1008             :       ! No adiabatic rescaling
    1009         190 :       adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
    1010         190 :       CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
    1011         190 :       IF (do_adiabatic_rescaling) CPABORT("No adiabatic rescaling implementation with K-points HFX")
    1012             : 
    1013         190 :       IF (dft_control%do_admm) THEN
    1014             :          CALL get_admm_env(qs_env%admm_env, matrix_ks_aux_fit_kp=matrix_ks_aux_fit_kp, &
    1015             :                            matrix_ks_aux_fit_im=matrix_ks_aux_fit_im, &
    1016          90 :                            matrix_ks_aux_fit_hfx_kp=matrix_ks_aux_fit_hfx_kp)
    1017             :       END IF
    1018             : 
    1019         190 :       nspins = dft_control%nspins
    1020         190 :       nimages = dft_control%nimages
    1021             : 
    1022         190 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
    1023         294 :       IF (use_virial .AND. calculate_forces) virial%pv_fock_4c = 0.0_dp
    1024             : 
    1025         190 :       hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%HF")
    1026         190 :       CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
    1027             : 
    1028             :       ! *** Initialize the auxiliary ks matrix to zero if required
    1029         190 :       IF (dft_control%do_admm) THEN
    1030         204 :          DO ispin = 1, nspins
    1031        6172 :             DO img = 1, nimages
    1032        6082 :                CALL dbcsr_set(matrix_ks_aux_fit_kp(ispin, img)%matrix, 0.0_dp)
    1033             :             END DO
    1034             :          END DO
    1035             :       END IF
    1036         458 :       DO ispin = 1, nspins
    1037       10328 :          DO img = 1, nimages
    1038       10138 :             CALL dbcsr_set(matrix_ks(ispin, img)%matrix, 0.0_dp)
    1039             :          END DO
    1040             :       END DO
    1041             : 
    1042         570 :       ALLOCATE (hf_energy(n_rep_hf))
    1043             : 
    1044         190 :       eold = 0.0_dp
    1045             : 
    1046         380 :       DO irep = 1, n_rep_hf
    1047             : 
    1048             :          ! fetch the correct matrices for normal HFX or ADMM
    1049         190 :          IF (dft_control%do_admm) THEN
    1050          90 :             CALL get_admm_env(qs_env%admm_env, matrix_ks_aux_fit_kp=matrix_ks_orb, rho_aux_fit=rho_orb)
    1051             :          ELSE
    1052         100 :             CALL get_qs_env(qs_env=qs_env, matrix_ks_kp=matrix_ks_orb, rho=rho_orb)
    1053             :          END IF
    1054         190 :          CALL qs_rho_get(rho_struct=rho_orb, rho_ao_kp=rho_ao_orb)
    1055             : 
    1056             :          ! Finally the real hfx calulation
    1057             :          ehfx = 0.0_dp
    1058             : 
    1059         190 :          IF (.NOT. x_data(irep, 1)%do_hfx_ri) THEN
    1060           0 :             CPABORT("Only RI-HFX is implemented for K-points")
    1061             :          END IF
    1062             : 
    1063             :          CALL hfx_ri_update_ks_kp(qs_env, x_data(irep, 1)%ri_data, matrix_ks_orb, ehfx, &
    1064             :                                   rho_ao_orb, s_mstruct_changed, nspins, &
    1065         190 :                                   x_data(irep, 1)%general_parameter%fraction)
    1066             : 
    1067         190 :          IF (calculate_forces) THEN
    1068             :             !Scale auxiliary density matrix for ADMMP (see Merlot2014) with gsi(ispin) to scale force
    1069          42 :             IF (dft_control%do_admm) THEN
    1070          24 :                CALL scale_dm(qs_env, rho_ao_orb, scale_back=.FALSE.)
    1071             :             END IF
    1072             : 
    1073             :             CALL hfx_ri_update_forces_kp(qs_env, x_data(irep, 1)%ri_data, nspins, &
    1074             :                                          x_data(irep, 1)%general_parameter%fraction, &
    1075          42 :                                          rho_ao_orb, use_virial=use_virial)
    1076             : 
    1077          42 :             IF (dft_control%do_admm) THEN
    1078          24 :                CALL scale_dm(qs_env, rho_ao_orb, scale_back=.TRUE.)
    1079             :             END IF
    1080             :          END IF
    1081             : 
    1082         190 :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
    1083         190 :          eh1 = ehfx - eold
    1084         190 :          CALL pw_hfx(qs_env, eh1, hfx_sections, poisson_env, auxbas_pw_pool, irep)
    1085         570 :          eold = ehfx
    1086             : 
    1087             :       END DO
    1088             : 
    1089             :       ! *** Set the total HFX energy
    1090         190 :       energy%ex = ehfx
    1091             : 
    1092             :       ! *** Add Core-Hamiltonian-Matrix ***
    1093         458 :       DO ispin = 1, nspins
    1094       10328 :          DO img = 1, nimages
    1095             :             CALL dbcsr_add(matrix_ks(ispin, img)%matrix, matrix_h(1, img)%matrix, &
    1096       10138 :                            1.0_dp, 1.0_dp)
    1097             :          END DO
    1098             :       END DO
    1099         190 :       IF (use_virial .AND. calculate_forces) THEN
    1100         104 :          virial%pv_exx = virial%pv_exx - virial%pv_fock_4c
    1101         104 :          virial%pv_virial = virial%pv_virial - virial%pv_fock_4c
    1102           8 :          virial%pv_calculate = .FALSE.
    1103             :       END IF
    1104             : 
    1105             :       !update the hfx aux_fit matrix
    1106         190 :       IF (dft_control%do_admm) THEN
    1107         204 :          DO ispin = 1, nspins
    1108        6172 :             DO img = 1, nimages
    1109             :                CALL dbcsr_add(matrix_ks_aux_fit_hfx_kp(ispin, img)%matrix, matrix_ks_aux_fit_kp(ispin, img)%matrix, &
    1110        6082 :                               0.0_dp, 1.0_dp)
    1111             :             END DO
    1112             :          END DO
    1113             :       END IF
    1114             : 
    1115         190 :       CALL timestop(handle)
    1116             : 
    1117         760 :    END SUBROUTINE hfx_ks_matrix_kp
    1118             : 
    1119             : ! **************************************************************************************************
    1120             : !> \brief Add the hfx contributions to the Hamiltonian
    1121             : !>
    1122             : !> \param qs_env ...
    1123             : !> \param matrix_ks ...
    1124             : !> \param rho ...
    1125             : !> \param energy ...
    1126             : !> \param calculate_forces ...
    1127             : !> \param just_energy ...
    1128             : !> \param v_rspace_new ...
    1129             : !> \param v_tau_rspace ...
    1130             : !> \par History
    1131             : !>     refactoring 03-2011 [MI]
    1132             : ! **************************************************************************************************
    1133             : 
    1134       23346 :    SUBROUTINE hfx_ks_matrix(qs_env, matrix_ks, rho, energy, calculate_forces, &
    1135             :                             just_energy, v_rspace_new, v_tau_rspace)
    1136             : 
    1137             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1138             :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks
    1139             :       TYPE(qs_rho_type), POINTER                         :: rho
    1140             :       TYPE(qs_energy_type), POINTER                      :: energy
    1141             :       LOGICAL, INTENT(in)                                :: calculate_forces, just_energy
    1142             :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: v_rspace_new, v_tau_rspace
    1143             : 
    1144             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'hfx_ks_matrix'
    1145             : 
    1146             :       INTEGER                                            :: handle, img, irep, ispin, mspin, &
    1147             :                                                             n_rep_hf, nimages, ns, nspins
    1148             :       LOGICAL                                            :: distribute_fock_matrix, &
    1149             :                                                             do_adiabatic_rescaling, &
    1150             :                                                             hfx_treat_lsd_in_core, &
    1151             :                                                             s_mstruct_changed, use_virial
    1152             :       REAL(dp)                                           :: eh1, ehfx, ehfxrt, eold
    1153       23346 :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: hf_energy
    1154       23346 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_1d, matrix_ks_aux_fit, &
    1155       23346 :          matrix_ks_aux_fit_hfx, matrix_ks_aux_fit_im, matrix_ks_im, rho_ao_1d, rho_ao_resp
    1156       23346 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h, matrix_h_im, matrix_ks_orb, &
    1157       23346 :                                                             rho_ao_orb
    1158             :       TYPE(dft_control_type), POINTER                    :: dft_control
    1159       23346 :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
    1160       23346 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
    1161             :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1162             :       TYPE(pw_env_type), POINTER                         :: pw_env
    1163             :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
    1164             :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1165             :       TYPE(qs_rho_type), POINTER                         :: rho_orb
    1166             :       TYPE(rt_prop_type), POINTER                        :: rtp
    1167             :       TYPE(section_vals_type), POINTER                   :: adiabatic_rescaling_section, &
    1168             :                                                             hfx_sections, input
    1169             :       TYPE(virial_type), POINTER                         :: virial
    1170             : 
    1171       23346 :       CALL timeset(routineN, handle)
    1172             : 
    1173       23346 :       NULLIFY (auxbas_pw_pool, dft_control, hfx_sections, input, &
    1174       23346 :                para_env, poisson_env, pw_env, virial, matrix_ks_im, &
    1175       23346 :                matrix_ks_orb, rho_ao_orb, matrix_h, matrix_h_im, matrix_ks_aux_fit, &
    1176       23346 :                matrix_ks_aux_fit_im, matrix_ks_aux_fit_hfx)
    1177             : 
    1178             :       CALL get_qs_env(qs_env=qs_env, &
    1179             :                       dft_control=dft_control, &
    1180             :                       input=input, &
    1181             :                       matrix_h_kp=matrix_h, &
    1182             :                       matrix_h_im_kp=matrix_h_im, &
    1183             :                       para_env=para_env, &
    1184             :                       pw_env=pw_env, &
    1185             :                       virial=virial, &
    1186             :                       matrix_ks_im=matrix_ks_im, &
    1187             :                       s_mstruct_changed=s_mstruct_changed, &
    1188       23346 :                       x_data=x_data)
    1189             : 
    1190       23346 :       IF (dft_control%do_admm) THEN
    1191             :          CALL get_admm_env(qs_env%admm_env, mos_aux_fit=mo_array, matrix_ks_aux_fit=matrix_ks_aux_fit, &
    1192        9850 :                            matrix_ks_aux_fit_im=matrix_ks_aux_fit_im, matrix_ks_aux_fit_hfx=matrix_ks_aux_fit_hfx)
    1193             :       ELSE
    1194       13496 :          CALL get_qs_env(qs_env=qs_env, mos=mo_array)
    1195             :       END IF
    1196             : 
    1197       23346 :       nspins = dft_control%nspins
    1198       23346 :       nimages = dft_control%nimages
    1199             : 
    1200       23346 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
    1201             : 
    1202       23554 :       IF (use_virial .AND. calculate_forces) virial%pv_fock_4c = 0.0_dp
    1203             : 
    1204       23346 :       hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%HF")
    1205       23346 :       CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
    1206             :       CALL section_vals_val_get(hfx_sections, "TREAT_LSD_IN_CORE", l_val=hfx_treat_lsd_in_core, &
    1207       23346 :                                 i_rep_section=1)
    1208       23346 :       adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
    1209       23346 :       CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
    1210             : 
    1211             :       ! *** Initialize the auxiliary ks matrix to zero if required
    1212       23346 :       IF (dft_control%do_admm) THEN
    1213       21608 :          DO ispin = 1, nspins
    1214       21608 :             CALL dbcsr_set(matrix_ks_aux_fit(ispin)%matrix, 0.0_dp)
    1215             :          END DO
    1216             :       END IF
    1217       51448 :       DO ispin = 1, nspins
    1218       79550 :          DO img = 1, nimages
    1219       56204 :             CALL dbcsr_set(matrix_ks(ispin, img)%matrix, 0.0_dp)
    1220             :          END DO
    1221             :       END DO
    1222             : 
    1223       23346 :       CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
    1224             : 
    1225       70038 :       ALLOCATE (hf_energy(n_rep_hf))
    1226             : 
    1227       23346 :       eold = 0.0_dp
    1228             : 
    1229       46760 :       DO irep = 1, n_rep_hf
    1230             :          ! Remember: Vhfx is added, energy is calclulated from total Vhfx,
    1231             :          ! so energy of last iteration is correct
    1232             : 
    1233       23414 :          IF (do_adiabatic_rescaling .AND. hfx_treat_lsd_in_core) &
    1234           0 :             CPABORT("HFX_TREAT_LSD_IN_CORE not implemented for adiabatically rescaled hybrids")
    1235             :          ! everything is calculated with adiabatic rescaling but the potential is not added in a first step
    1236       23414 :          distribute_fock_matrix = .NOT. do_adiabatic_rescaling
    1237             : 
    1238       23414 :          mspin = 1
    1239       23414 :          IF (hfx_treat_lsd_in_core) mspin = nspins
    1240             : 
    1241             :          ! fetch the correct matrices for normal HFX or ADMM
    1242       23414 :          IF (dft_control%do_admm) THEN
    1243        9850 :             CALL get_admm_env(qs_env%admm_env, matrix_ks_aux_fit=matrix_ks_1d, rho_aux_fit=rho_orb)
    1244        9850 :             ns = SIZE(matrix_ks_1d)
    1245        9850 :             matrix_ks_orb(1:ns, 1:1) => matrix_ks_1d(1:ns)
    1246             :          ELSE
    1247       13564 :             CALL get_qs_env(qs_env=qs_env, matrix_ks_kp=matrix_ks_orb, rho=rho_orb)
    1248             :          END IF
    1249       23414 :          CALL qs_rho_get(rho_struct=rho_orb, rho_ao_kp=rho_ao_orb)
    1250             :          ! Finally the real hfx calulation
    1251       23414 :          ehfx = 0.0_dp
    1252             : 
    1253       23414 :          IF (x_data(irep, 1)%do_hfx_ri) THEN
    1254             : 
    1255             :             CALL hfx_ri_update_ks(qs_env, x_data(irep, 1)%ri_data, matrix_ks_orb, ehfx, &
    1256             :                                   mo_array, rho_ao_orb, &
    1257             :                                   s_mstruct_changed, nspins, &
    1258        1108 :                                   x_data(irep, 1)%general_parameter%fraction)
    1259        1108 :             IF (dft_control%do_admm) THEN
    1260             :                !for ADMMS, we need the exchange matrix k(d) for both spins
    1261         280 :                DO ispin = 1, nspins
    1262             :                   CALL dbcsr_copy(matrix_ks_aux_fit_hfx(ispin)%matrix, matrix_ks_orb(ispin, 1)%matrix, &
    1263         280 :                                   name="HF exch. part of matrix_ks_aux_fit for ADMMS")
    1264             :                END DO
    1265             :             END IF
    1266             : 
    1267             :          ELSE
    1268             : 
    1269       44624 :             DO ispin = 1, mspin
    1270             :                CALL integrate_four_center(qs_env, x_data, matrix_ks_orb, eh1, rho_ao_orb, hfx_sections, &
    1271             :                                           para_env, s_mstruct_changed, irep, distribute_fock_matrix, &
    1272       22318 :                                           ispin=ispin)
    1273       44624 :                ehfx = ehfx + eh1
    1274             :             END DO
    1275             :          END IF
    1276             : 
    1277       23414 :          IF (calculate_forces .AND. .NOT. do_adiabatic_rescaling) THEN
    1278             :             !Scale auxiliary density matrix for ADMMP (see Merlot2014) with gsi(ispin) to scale force
    1279         720 :             IF (dft_control%do_admm) THEN
    1280         238 :                CALL scale_dm(qs_env, rho_ao_orb, scale_back=.FALSE.)
    1281             :             END IF
    1282         720 :             NULLIFY (rho_ao_resp)
    1283             : 
    1284         720 :             IF (x_data(irep, 1)%do_hfx_ri) THEN
    1285             : 
    1286             :                CALL hfx_ri_update_forces(qs_env, x_data(irep, 1)%ri_data, nspins, &
    1287             :                                          x_data(irep, 1)%general_parameter%fraction, &
    1288             :                                          rho_ao=rho_ao_orb, mos=mo_array, &
    1289             :                                          rho_ao_resp=rho_ao_resp, &
    1290          48 :                                          use_virial=use_virial)
    1291             : 
    1292             :             ELSE
    1293             : 
    1294             :                CALL derivatives_four_center(qs_env, rho_ao_orb, rho_ao_resp, hfx_sections, &
    1295         672 :                                             para_env, irep, use_virial)
    1296             : 
    1297             :             END IF
    1298             : 
    1299             :             !Scale auxiliary density matrix for ADMMP back with 1/gsi(ispin)
    1300         720 :             IF (dft_control%do_admm) THEN
    1301         238 :                CALL scale_dm(qs_env, rho_ao_orb, scale_back=.TRUE.)
    1302             :             END IF
    1303             :          END IF
    1304             : 
    1305             :          !! If required, the calculation of the forces will be done later with adiabatic rescaling
    1306       23414 :          IF (do_adiabatic_rescaling) hf_energy(irep) = ehfx
    1307             : 
    1308             :          ! special case RTP/EMD we have a full complex density and HFX has a contribution from the imaginary part
    1309       23414 :          ehfxrt = 0.0_dp
    1310       23414 :          IF (qs_env%run_rtp) THEN
    1311             : 
    1312         414 :             CALL get_qs_env(qs_env=qs_env, rtp=rtp)
    1313         860 :             DO ispin = 1, nspins
    1314         860 :                CALL dbcsr_set(matrix_ks_im(ispin)%matrix, 0.0_dp)
    1315             :             END DO
    1316         414 :             IF (dft_control%do_admm) THEN
    1317             :                ! matrix_ks_orb => matrix_ks_aux_fit_im
    1318          76 :                ns = SIZE(matrix_ks_aux_fit_im)
    1319          76 :                matrix_ks_orb(1:ns, 1:1) => matrix_ks_aux_fit_im(1:ns)
    1320         152 :                DO ispin = 1, nspins
    1321         152 :                   CALL dbcsr_set(matrix_ks_aux_fit_im(ispin)%matrix, 0.0_dp)
    1322             :                END DO
    1323             :             ELSE
    1324             :                ! matrix_ks_orb => matrix_ks_im
    1325         338 :                ns = SIZE(matrix_ks_im)
    1326         338 :                matrix_ks_orb(1:ns, 1:1) => matrix_ks_im(1:ns)
    1327             :             END IF
    1328             : 
    1329         414 :             CALL qs_rho_get(rho_orb, rho_ao_im=rho_ao_1d)
    1330         414 :             ns = SIZE(rho_ao_1d)
    1331         414 :             rho_ao_orb(1:ns, 1:1) => rho_ao_1d(1:ns)
    1332             : 
    1333         414 :             ehfxrt = 0.0_dp
    1334             : 
    1335         414 :             IF (x_data(irep, 1)%do_hfx_ri) THEN
    1336             :                CALL hfx_ri_update_ks(qs_env, x_data(irep, 1)%ri_data, matrix_ks_orb, ehfx, &
    1337             :                                      mo_array, rho_ao_orb, &
    1338             :                                      .FALSE., nspins, &
    1339           0 :                                      x_data(irep, 1)%general_parameter%fraction)
    1340           0 :                IF (dft_control%do_admm) THEN
    1341             :                   !for ADMMS, we need the exchange matrix k(d) for both spins
    1342           0 :                   DO ispin = 1, nspins
    1343             :                      CALL dbcsr_copy(matrix_ks_aux_fit_hfx(ispin)%matrix, matrix_ks_orb(ispin, 1)%matrix, &
    1344           0 :                                      name="HF exch. part of matrix_ks_aux_fit for ADMMS")
    1345             :                   END DO
    1346             :                END IF
    1347             : 
    1348             :             ELSE
    1349         828 :                DO ispin = 1, mspin
    1350             :                   CALL integrate_four_center(qs_env, x_data, matrix_ks_orb, eh1, rho_ao_orb, hfx_sections, &
    1351             :                                              para_env, .FALSE., irep, distribute_fock_matrix, &
    1352         414 :                                              ispin=ispin)
    1353         828 :                   ehfxrt = ehfxrt + eh1
    1354             :                END DO
    1355             :             END IF
    1356             : 
    1357         414 :             IF (calculate_forces .AND. .NOT. do_adiabatic_rescaling) THEN
    1358         232 :                NULLIFY (rho_ao_resp)
    1359             : 
    1360         232 :                IF (x_data(irep, 1)%do_hfx_ri) THEN
    1361             : 
    1362             :                   CALL hfx_ri_update_forces(qs_env, x_data(irep, 1)%ri_data, nspins, &
    1363             :                                             x_data(irep, 1)%general_parameter%fraction, &
    1364             :                                             rho_ao=rho_ao_orb, mos=mo_array, &
    1365           0 :                                             use_virial=use_virial)
    1366             : 
    1367             :                ELSE
    1368             :                   CALL derivatives_four_center(qs_env, rho_ao_orb, rho_ao_resp, hfx_sections, &
    1369         232 :                                                para_env, irep, use_virial)
    1370             :                END IF
    1371             :             END IF
    1372             : 
    1373             :             !! If required, the calculation of the forces will be done later with adiabatic rescaling
    1374         414 :             IF (do_adiabatic_rescaling) hf_energy(irep) = ehfx + ehfxrt
    1375             : 
    1376         414 :             IF (dft_control%rtp_control%velocity_gauge) THEN
    1377           0 :                CPASSERT(ASSOCIATED(matrix_h_im))
    1378           0 :                DO ispin = 1, nspins
    1379             :                   CALL dbcsr_add(matrix_ks_im(ispin)%matrix, matrix_h_im(1, 1)%matrix, &
    1380           0 :                                  1.0_dp, 1.0_dp)
    1381             :                END DO
    1382             :             END IF
    1383             : 
    1384             :          END IF
    1385             : 
    1386       46760 :          IF (.NOT. qs_env%run_rtp) THEN
    1387             :             CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
    1388       23000 :                             poisson_env=poisson_env)
    1389       23000 :             eh1 = ehfx - eold
    1390       23000 :             CALL pw_hfx(qs_env, eh1, hfx_sections, poisson_env, auxbas_pw_pool, irep)
    1391       23000 :             eold = ehfx
    1392             :          END IF
    1393             : 
    1394             :       END DO
    1395             : 
    1396             :       ! *** Set the total HFX energy
    1397       23346 :       energy%ex = ehfx + ehfxrt
    1398             : 
    1399             :       ! *** Add Core-Hamiltonian-Matrix ***
    1400       51448 :       DO ispin = 1, nspins
    1401       79550 :          DO img = 1, nimages
    1402             :             CALL dbcsr_add(matrix_ks(ispin, img)%matrix, matrix_h(1, img)%matrix, &
    1403       56204 :                            1.0_dp, 1.0_dp)
    1404             :          END DO
    1405             :       END DO
    1406       23346 :       IF (use_virial .AND. calculate_forces) THEN
    1407         208 :          virial%pv_exx = virial%pv_exx - virial%pv_fock_4c
    1408         208 :          virial%pv_virial = virial%pv_virial - virial%pv_fock_4c
    1409          16 :          virial%pv_calculate = .FALSE.
    1410             :       END IF
    1411             : 
    1412             :       !! If we perform adiabatic rescaling we are now able to rescale the xc-potential
    1413       23346 :       IF (do_adiabatic_rescaling) THEN
    1414             :          CALL rescale_xc_potential(qs_env, matrix_ks, rho, energy, v_rspace_new, v_tau_rspace, &
    1415          56 :                                    hf_energy, just_energy, calculate_forces, use_virial)
    1416             :       END IF ! do_adiabatic_rescaling
    1417             : 
    1418             :       !update the hfx aux_fit matrixIF (dft_control%do_admm) THEN
    1419       23346 :       IF (dft_control%do_admm) THEN
    1420       21608 :          DO ispin = 1, nspins
    1421             :             CALL dbcsr_add(matrix_ks_aux_fit_hfx(ispin)%matrix, matrix_ks_aux_fit(ispin)%matrix, &
    1422       21608 :                            0.0_dp, 1.0_dp)
    1423             :          END DO
    1424             :       END IF
    1425             : 
    1426       23346 :       CALL timestop(handle)
    1427             : 
    1428      116730 :    END SUBROUTINE hfx_ks_matrix
    1429             : 
    1430             : ! **************************************************************************************************
    1431             : !> \brief This routine modifies the xc section depending on the potential type
    1432             : !>        used for the HF exchange and the resulting correction term. Currently
    1433             : !>        three types of corrections are implemented:
    1434             : !>
    1435             : !>        coulomb:     Ex,hf = Ex,hf' + (PBEx-PBEx')
    1436             : !>        shortrange:  Ex,hf = Ex,hf' + (XWPBEX-XWPBEX')
    1437             : !>        truncated:   Ex,hf = Ex,hf' + ( (XWPBEX0-PBE_HOLE_TC_LR) -(XWPBEX0-PBE_HOLE_TC_LR)' )
    1438             : !>
    1439             : !>        with ' denoting the auxiliary basis set and
    1440             : !>
    1441             : !>          PBEx:           PBE exchange functional
    1442             : !>          XWPBEX:         PBE exchange hole for short-range potential (erfc(omega*r)/r)
    1443             : !>          XWPBEX0:        PBE exchange hole for standard coulomb potential
    1444             : !>          PBE_HOLE_TC_LR: PBE exchange hole for longrange truncated coulomb potential
    1445             : !>
    1446             : !>        Above explanation is correct for the deafult case. If a specific functional is requested
    1447             : !>        for the correction term (cfun), we get
    1448             : !>        Ex,hf = Ex,hf' + (cfun-cfun')
    1449             : !>        for all cases of operators.
    1450             : !>
    1451             : !> \param x_data ...
    1452             : !> \param xc_section the original xc_section
    1453             : !> \param admm_env the ADMM environment
    1454             : !> \par History
    1455             : !>      12.2009 created [Manuel Guidon]
    1456             : !>      05.2021 simplify for case of no correction [JGH]
    1457             : !> \author Manuel Guidon
    1458             : ! **************************************************************************************************
    1459         466 :    SUBROUTINE create_admm_xc_section(x_data, xc_section, admm_env)
    1460             :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
    1461             :       TYPE(section_vals_type), POINTER                   :: xc_section
    1462             :       TYPE(admm_type), POINTER                           :: admm_env
    1463             : 
    1464             :       LOGICAL, PARAMETER                                 :: debug_functional = .FALSE.
    1465             : #if defined (__LIBXC)
    1466             :       REAL(KIND=dp), PARAMETER :: x_factor_c = 0.930525736349100025_dp
    1467             : #endif
    1468             : 
    1469             :       CHARACTER(LEN=20)                                  :: name_x_func
    1470             :       INTEGER                                            :: hfx_potential_type, ifun, iounit, nfun
    1471             :       LOGICAL                                            :: funct_found
    1472             :       REAL(dp)                                           :: cutoff_radius, hfx_fraction, omega, &
    1473             :                                                             scale_coulomb, scale_longrange, scale_x
    1474             :       TYPE(cp_logger_type), POINTER                      :: logger
    1475             :       TYPE(section_vals_type), POINTER                   :: xc_fun, xc_fun_section
    1476             : 
    1477         466 :       logger => cp_get_default_logger()
    1478         466 :       NULLIFY (admm_env%xc_section_aux, admm_env%xc_section_primary)
    1479             : 
    1480             :       !! ** Duplicate existing xc-section
    1481         466 :       CALL section_vals_duplicate(xc_section, admm_env%xc_section_aux)
    1482         466 :       CALL section_vals_duplicate(xc_section, admm_env%xc_section_primary)
    1483             :       !** Now modify the auxiliary basis
    1484             :       !** First remove all functionals
    1485         466 :       xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_aux, "XC_FUNCTIONAL")
    1486             : 
    1487             :       !* Overwrite possible shortcut
    1488             :       CALL section_vals_val_set(xc_fun_section, "_SECTION_PARAMETERS_", &
    1489         466 :                                 i_val=xc_funct_no_shortcut)
    1490             : 
    1491             :       !** Get number of Functionals in the list
    1492         466 :       ifun = 0
    1493         466 :       nfun = 0
    1494         376 :       DO
    1495         842 :          ifun = ifun + 1
    1496         842 :          xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1497         842 :          IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1498         376 :          nfun = nfun + 1
    1499             :       END DO
    1500             : 
    1501             :       ifun = 0
    1502         842 :       DO ifun = 1, nfun
    1503         376 :          xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=1)
    1504         376 :          IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1505         842 :          CALL section_vals_remove_values(xc_fun)
    1506             :       END DO
    1507             : 
    1508         466 :       IF (ASSOCIATED(x_data)) THEN
    1509         458 :          hfx_potential_type = x_data(1, 1)%potential_parameter%potential_type
    1510         458 :          hfx_fraction = x_data(1, 1)%general_parameter%fraction
    1511             :       ELSE
    1512           8 :          CPWARN("ADMM requested without a DFT%XC%HF section. It will be ignored for the SCF.")
    1513           8 :          admm_env%aux_exch_func = do_admm_aux_exch_func_none
    1514             :       END IF
    1515             : 
    1516             :       !in case of no admm exchange corr., no auxiliary exchange functional needed
    1517         466 :       IF (admm_env%aux_exch_func == do_admm_aux_exch_func_none) THEN
    1518             :          CALL section_vals_val_set(xc_fun_section, "_SECTION_PARAMETERS_", &
    1519          90 :                                    i_val=xc_none)
    1520             :          hfx_fraction = 0.0_dp
    1521             :       ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_default) THEN
    1522             :          ! default PBE Functional
    1523             :          !! ** Add functionals evaluated with auxiliary basis
    1524         182 :          SELECT CASE (hfx_potential_type)
    1525             :          CASE (do_potential_coulomb)
    1526             :             CALL section_vals_val_set(xc_fun_section, "PBE%_SECTION_PARAMETERS_", &
    1527         182 :                                       l_val=.TRUE.)
    1528             :             CALL section_vals_val_set(xc_fun_section, "PBE%SCALE_X", &
    1529         182 :                                       r_val=-hfx_fraction)
    1530             :             CALL section_vals_val_set(xc_fun_section, "PBE%SCALE_C", &
    1531         182 :                                       r_val=0.0_dp)
    1532             :          CASE (do_potential_short)
    1533           6 :             omega = x_data(1, 1)%potential_parameter%omega
    1534             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1535           6 :                                       l_val=.TRUE.)
    1536             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1537           6 :                                       r_val=-hfx_fraction)
    1538             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1539           6 :                                       r_val=0.0_dp)
    1540             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1541           6 :                                       r_val=omega)
    1542             :          CASE (do_potential_truncated)
    1543          42 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    1544             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    1545          42 :                                       l_val=.TRUE.)
    1546             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1547          42 :                                       r_val=hfx_fraction)
    1548             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1549          42 :                                       r_val=cutoff_radius)
    1550             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1551          42 :                                       l_val=.TRUE.)
    1552             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1553          42 :                                       r_val=0.0_dp)
    1554             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1555          42 :                                       r_val=-hfx_fraction)
    1556             :          CASE (do_potential_long)
    1557           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1558             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1559           2 :                                       l_val=.TRUE.)
    1560             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1561           2 :                                       r_val=hfx_fraction)
    1562             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1563           2 :                                       r_val=-hfx_fraction)
    1564             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1565           2 :                                       r_val=omega)
    1566             :          CASE (do_potential_mix_cl)
    1567           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1568           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    1569           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    1570             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1571           2 :                                       l_val=.TRUE.)
    1572             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1573           2 :                                       r_val=hfx_fraction*scale_longrange)
    1574             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1575           2 :                                       r_val=-hfx_fraction*(scale_longrange + scale_coulomb))
    1576             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1577           2 :                                       r_val=omega)
    1578             :          CASE (do_potential_mix_cl_trunc)
    1579           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1580           2 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    1581           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    1582           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    1583             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    1584           2 :                                       l_val=.TRUE.)
    1585             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1586           2 :                                       r_val=hfx_fraction*(scale_longrange + scale_coulomb))
    1587             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1588           2 :                                       r_val=cutoff_radius)
    1589             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1590           2 :                                       l_val=.TRUE.)
    1591             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1592           2 :                                       r_val=hfx_fraction*scale_longrange)
    1593             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1594           2 :                                       r_val=-hfx_fraction*(scale_longrange + scale_coulomb))
    1595             :             CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1596           2 :                                       r_val=omega)
    1597             :          CASE DEFAULT
    1598         236 :             CPABORT("Unknown potential operator!")
    1599             :          END SELECT
    1600             : 
    1601             :          !** Now modify the functionals for the primary basis
    1602         236 :          xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_primary, "XC_FUNCTIONAL")
    1603             :          !* Overwrite possible shortcut
    1604             :          CALL section_vals_val_set(xc_fun_section, "_SECTION_PARAMETERS_", &
    1605         236 :                                    i_val=xc_funct_no_shortcut)
    1606             : 
    1607         182 :          SELECT CASE (hfx_potential_type)
    1608             :          CASE (do_potential_coulomb)
    1609         182 :             ifun = 0
    1610         182 :             funct_found = .FALSE.
    1611             :             DO
    1612         332 :                ifun = ifun + 1
    1613         332 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1614         332 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1615         332 :                IF (xc_fun%section%name == "PBE") THEN
    1616         148 :                   funct_found = .TRUE.
    1617             :                END IF
    1618             :             END DO
    1619         182 :             IF (.NOT. funct_found) THEN
    1620             :                CALL section_vals_val_set(xc_fun_section, "PBE%_SECTION_PARAMETERS_", &
    1621          34 :                                          l_val=.TRUE.)
    1622             :                CALL section_vals_val_set(xc_fun_section, "PBE%SCALE_X", &
    1623          34 :                                          r_val=hfx_fraction)
    1624             :                CALL section_vals_val_set(xc_fun_section, "PBE%SCALE_C", &
    1625          34 :                                          r_val=0.0_dp)
    1626             :             ELSE
    1627             :                CALL section_vals_val_get(xc_fun_section, "PBE%SCALE_X", &
    1628         148 :                                          r_val=scale_x)
    1629         148 :                scale_x = scale_x + hfx_fraction
    1630             :                CALL section_vals_val_set(xc_fun_section, "PBE%SCALE_X", &
    1631         148 :                                          r_val=scale_x)
    1632             :             END IF
    1633             :          CASE (do_potential_short)
    1634           6 :             omega = x_data(1, 1)%potential_parameter%omega
    1635           6 :             ifun = 0
    1636           6 :             funct_found = .FALSE.
    1637             :             DO
    1638          18 :                ifun = ifun + 1
    1639          18 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1640          18 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1641          18 :                IF (xc_fun%section%name == "XWPBE") THEN
    1642           6 :                   funct_found = .TRUE.
    1643             :                END IF
    1644             :             END DO
    1645           6 :             IF (.NOT. funct_found) THEN
    1646             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1647           0 :                                          l_val=.TRUE.)
    1648             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1649           0 :                                          r_val=hfx_fraction)
    1650             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1651           0 :                                          r_val=0.0_dp)
    1652             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1653           0 :                                          r_val=omega)
    1654             :             ELSE
    1655             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X", &
    1656           6 :                                          r_val=scale_x)
    1657           6 :                scale_x = scale_x + hfx_fraction
    1658             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1659           6 :                                          r_val=scale_x)
    1660             :             END IF
    1661             :          CASE (do_potential_long)
    1662           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1663           2 :             ifun = 0
    1664           2 :             funct_found = .FALSE.
    1665             :             DO
    1666          10 :                ifun = ifun + 1
    1667          10 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1668          10 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1669          10 :                IF (xc_fun%section%name == "XWPBE") THEN
    1670           0 :                   funct_found = .TRUE.
    1671             :                END IF
    1672             :             END DO
    1673           2 :             IF (.NOT. funct_found) THEN
    1674             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1675           2 :                                          l_val=.TRUE.)
    1676             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1677           2 :                                          r_val=-hfx_fraction)
    1678             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1679           2 :                                          r_val=hfx_fraction)
    1680             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1681           2 :                                          r_val=omega)
    1682             :             ELSE
    1683             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X", &
    1684           0 :                                          r_val=scale_x)
    1685           0 :                scale_x = scale_x - hfx_fraction
    1686             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1687           0 :                                          r_val=scale_x)
    1688             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X0", &
    1689           0 :                                          r_val=scale_x)
    1690           0 :                scale_x = scale_x + hfx_fraction
    1691             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1692           0 :                                          r_val=scale_x)
    1693             : 
    1694             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1695           0 :                                          r_val=omega)
    1696             :             END IF
    1697             :          CASE (do_potential_truncated)
    1698          42 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    1699          42 :             ifun = 0
    1700          42 :             funct_found = .FALSE.
    1701             :             DO
    1702          62 :                ifun = ifun + 1
    1703          62 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1704          62 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1705          62 :                IF (xc_fun%section%name == "PBE_HOLE_T_C_LR") THEN
    1706           0 :                   funct_found = .TRUE.
    1707             :                END IF
    1708             :             END DO
    1709          42 :             IF (.NOT. funct_found) THEN
    1710             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    1711          42 :                                          l_val=.TRUE.)
    1712             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1713          42 :                                          r_val=-hfx_fraction)
    1714             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1715          42 :                                          r_val=cutoff_radius)
    1716             :             ELSE
    1717             :                CALL section_vals_val_get(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1718           0 :                                          r_val=scale_x)
    1719           0 :                scale_x = scale_x - hfx_fraction
    1720             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1721           0 :                                          r_val=scale_x)
    1722             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1723           0 :                                          r_val=cutoff_radius)
    1724             :             END IF
    1725          42 :             ifun = 0
    1726          42 :             funct_found = .FALSE.
    1727             :             DO
    1728         104 :                ifun = ifun + 1
    1729         104 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1730         104 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1731         104 :                IF (xc_fun%section%name == "XWPBE") THEN
    1732           0 :                   funct_found = .TRUE.
    1733             :                END IF
    1734             :             END DO
    1735          42 :             IF (.NOT. funct_found) THEN
    1736             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1737          42 :                                          l_val=.TRUE.)
    1738             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1739          42 :                                          r_val=hfx_fraction)
    1740             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1741          42 :                                          r_val=0.0_dp)
    1742             : 
    1743             :             ELSE
    1744             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X0", &
    1745           0 :                                          r_val=scale_x)
    1746           0 :                scale_x = scale_x + hfx_fraction
    1747             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1748           0 :                                          r_val=scale_x)
    1749             :             END IF
    1750             :          CASE (do_potential_mix_cl_trunc)
    1751           2 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    1752           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1753           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    1754           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    1755           2 :             ifun = 0
    1756           2 :             funct_found = .FALSE.
    1757             :             DO
    1758           6 :                ifun = ifun + 1
    1759           6 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1760           6 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1761           6 :                IF (xc_fun%section%name == "PBE_HOLE_T_C_LR") THEN
    1762           0 :                   funct_found = .TRUE.
    1763             :                END IF
    1764             :             END DO
    1765           2 :             IF (.NOT. funct_found) THEN
    1766             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    1767           2 :                                          l_val=.TRUE.)
    1768             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1769           2 :                                          r_val=-hfx_fraction*(scale_coulomb + scale_longrange))
    1770             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1771           2 :                                          r_val=cutoff_radius)
    1772             : 
    1773             :             ELSE
    1774             :                CALL section_vals_val_get(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1775           0 :                                          r_val=scale_x)
    1776           0 :                scale_x = scale_x - hfx_fraction*(scale_coulomb + scale_longrange)
    1777             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1778           0 :                                          r_val=scale_x)
    1779             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1780           0 :                                          r_val=cutoff_radius)
    1781             :             END IF
    1782           2 :             ifun = 0
    1783           2 :             funct_found = .FALSE.
    1784             :             DO
    1785           8 :                ifun = ifun + 1
    1786           8 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1787           8 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1788           8 :                IF (xc_fun%section%name == "XWPBE") THEN
    1789           2 :                   funct_found = .TRUE.
    1790             :                END IF
    1791             :             END DO
    1792           2 :             IF (.NOT. funct_found) THEN
    1793             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1794           0 :                                          l_val=.TRUE.)
    1795             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1796           0 :                                          r_val=hfx_fraction*(scale_coulomb + scale_longrange))
    1797             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1798           0 :                                          r_val=-hfx_fraction*scale_longrange)
    1799             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1800           0 :                                          r_val=omega)
    1801             : 
    1802             :             ELSE
    1803             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X0", &
    1804           2 :                                          r_val=scale_x)
    1805           2 :                scale_x = scale_x + hfx_fraction*(scale_coulomb + scale_longrange)
    1806             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1807           2 :                                          r_val=scale_x)
    1808             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X", &
    1809           2 :                                          r_val=scale_x)
    1810           2 :                scale_x = scale_x - hfx_fraction*scale_longrange
    1811             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1812           2 :                                          r_val=scale_x)
    1813             : 
    1814             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1815           2 :                                          r_val=omega)
    1816             :             END IF
    1817             :          CASE (do_potential_mix_cl)
    1818           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1819           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    1820           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    1821           2 :             ifun = 0
    1822           2 :             funct_found = .FALSE.
    1823             :             DO
    1824           6 :                ifun = ifun + 1
    1825           6 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1826           6 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1827           6 :                IF (xc_fun%section%name == "XWPBE") THEN
    1828           2 :                   funct_found = .TRUE.
    1829             :                END IF
    1830             :             END DO
    1831         238 :             IF (.NOT. funct_found) THEN
    1832             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%_SECTION_PARAMETERS_", &
    1833           0 :                                          l_val=.TRUE.)
    1834             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1835           0 :                                          r_val=hfx_fraction*(scale_coulomb + scale_longrange))
    1836             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1837           0 :                                          r_val=-hfx_fraction*scale_longrange)
    1838             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1839           0 :                                          r_val=omega)
    1840             : 
    1841             :             ELSE
    1842             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X0", &
    1843           2 :                                          r_val=scale_x)
    1844           2 :                scale_x = scale_x + hfx_fraction*(scale_coulomb + scale_longrange)
    1845             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X0", &
    1846           2 :                                          r_val=scale_x)
    1847             : 
    1848             :                CALL section_vals_val_get(xc_fun_section, "XWPBE%SCALE_X", &
    1849           2 :                                          r_val=scale_x)
    1850           2 :                scale_x = scale_x - hfx_fraction*scale_longrange
    1851             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%SCALE_X", &
    1852           2 :                                          r_val=scale_x)
    1853             : 
    1854             :                CALL section_vals_val_set(xc_fun_section, "XWPBE%OMEGA", &
    1855           2 :                                          r_val=omega)
    1856             :             END IF
    1857             :          END SELECT
    1858             :       ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_default_libxc) THEN
    1859             :          ! default PBE Functional
    1860             :          !! ** Add functionals evaluated with auxiliary basis
    1861             : #if defined (__LIBXC)
    1862           2 :          SELECT CASE (hfx_potential_type)
    1863             :          CASE (do_potential_coulomb)
    1864             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    1865           2 :                                       l_val=.TRUE.)
    1866             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    1867           2 :                                       r_val=-hfx_fraction)
    1868             :          CASE (do_potential_short)
    1869           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1870             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    1871           2 :                                       l_val=.TRUE.)
    1872             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1873           2 :                                       r_val=-hfx_fraction)
    1874             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    1875           2 :                                       r_val=omega)
    1876             :          CASE (do_potential_truncated)
    1877           0 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    1878             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    1879           0 :                                       l_val=.TRUE.)
    1880             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1881           0 :                                       r_val=hfx_fraction)
    1882             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1883           0 :                                       r_val=cutoff_radius)
    1884             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    1885           0 :                                       l_val=.TRUE.)
    1886             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1887           0 :                                       r_val=-hfx_fraction)
    1888             :          CASE (do_potential_long)
    1889           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1890             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    1891           2 :                                       l_val=.TRUE.)
    1892             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1893           2 :                                       r_val=hfx_fraction)
    1894             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    1895           2 :                                       r_val=omega)
    1896             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    1897           2 :                                       l_val=.TRUE.)
    1898             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    1899           2 :                                       r_val=-hfx_fraction)
    1900             :          CASE (do_potential_mix_cl)
    1901           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1902           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    1903           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    1904             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    1905           2 :                                       l_val=.TRUE.)
    1906             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1907           2 :                                       r_val=hfx_fraction*scale_longrange)
    1908             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    1909           2 :                                       r_val=omega)
    1910             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    1911           2 :                                       l_val=.TRUE.)
    1912             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    1913           2 :                                       r_val=-hfx_fraction*(scale_longrange + scale_coulomb))
    1914             :          CASE (do_potential_mix_cl_trunc)
    1915           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1916           2 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    1917           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    1918           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    1919             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    1920           2 :                                       l_val=.TRUE.)
    1921             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    1922           2 :                                       r_val=hfx_fraction*(scale_longrange + scale_coulomb))
    1923             :             CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    1924           2 :                                       r_val=cutoff_radius)
    1925             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    1926           2 :                                       l_val=.TRUE.)
    1927             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1928           2 :                                       r_val=hfx_fraction*scale_longrange)
    1929             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    1930           2 :                                       r_val=omega)
    1931             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    1932           2 :                                       l_val=.TRUE.)
    1933             :             CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    1934           2 :                                       r_val=-hfx_fraction*(scale_longrange + scale_coulomb))
    1935             :          CASE DEFAULT
    1936          10 :             CPABORT("Unknown potential operator!")
    1937             :          END SELECT
    1938             : 
    1939             :          !** Now modify the functionals for the primary basis
    1940          10 :          xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_primary, "XC_FUNCTIONAL")
    1941             :          !* Overwrite possible shortcut
    1942             :          CALL section_vals_val_set(xc_fun_section, "_SECTION_PARAMETERS_", &
    1943          10 :                                    i_val=xc_funct_no_shortcut)
    1944             : 
    1945           2 :          SELECT CASE (hfx_potential_type)
    1946             :          CASE (do_potential_coulomb)
    1947           2 :             ifun = 0
    1948           2 :             funct_found = .FALSE.
    1949             :             DO
    1950           4 :                ifun = ifun + 1
    1951           4 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1952           4 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1953           4 :                IF (xc_fun%section%name == "GGA_X_PBE") THEN
    1954           0 :                   funct_found = .TRUE.
    1955             :                END IF
    1956             :             END DO
    1957           2 :             IF (.NOT. funct_found) THEN
    1958             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    1959           2 :                                          l_val=.TRUE.)
    1960             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    1961           2 :                                          r_val=hfx_fraction)
    1962             :             ELSE
    1963             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_PBE%SCALE", &
    1964           0 :                                          r_val=scale_x)
    1965           0 :                scale_x = scale_x + hfx_fraction
    1966             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    1967           0 :                                          r_val=scale_x)
    1968             :             END IF
    1969             :          CASE (do_potential_short)
    1970           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1971           2 :             ifun = 0
    1972           2 :             funct_found = .FALSE.
    1973             :             DO
    1974           4 :                ifun = ifun + 1
    1975           4 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    1976           4 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    1977           4 :                IF (xc_fun%section%name == "GGA_X_WPBEH") THEN
    1978           0 :                   funct_found = .TRUE.
    1979             :                END IF
    1980             :             END DO
    1981           2 :             IF (.NOT. funct_found) THEN
    1982             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    1983           2 :                                          l_val=.TRUE.)
    1984             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1985           2 :                                          r_val=hfx_fraction)
    1986             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    1987           2 :                                          r_val=omega)
    1988             :             ELSE
    1989             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1990           0 :                                          r_val=scale_x)
    1991           0 :                scale_x = scale_x + hfx_fraction
    1992             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    1993           0 :                                          r_val=scale_x)
    1994             :             END IF
    1995             :          CASE (do_potential_long)
    1996           2 :             omega = x_data(1, 1)%potential_parameter%omega
    1997           2 :             ifun = 0
    1998           2 :             funct_found = .FALSE.
    1999             :             DO
    2000           4 :                ifun = ifun + 1
    2001           4 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2002           4 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2003           4 :                IF (xc_fun%section%name == "GGA_X_WPBEH") THEN
    2004           0 :                   funct_found = .TRUE.
    2005             :                END IF
    2006             :             END DO
    2007           2 :             IF (.NOT. funct_found) THEN
    2008             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    2009           2 :                                          l_val=.TRUE.)
    2010             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2011           2 :                                          r_val=-hfx_fraction)
    2012             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    2013           2 :                                          r_val=omega)
    2014             :             ELSE
    2015             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2016           0 :                                          r_val=scale_x)
    2017           0 :                scale_x = scale_x - hfx_fraction
    2018             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2019           0 :                                          r_val=scale_x)
    2020             : 
    2021             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    2022           0 :                                          r_val=omega)
    2023             :             END IF
    2024           2 :             ifun = 0
    2025           2 :             funct_found = .FALSE.
    2026             :             DO
    2027           6 :                ifun = ifun + 1
    2028           6 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2029           6 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2030           6 :                IF (xc_fun%section%name == "GGA_X_PBE") THEN
    2031           0 :                   funct_found = .TRUE.
    2032             :                END IF
    2033             :             END DO
    2034           2 :             IF (.NOT. funct_found) THEN
    2035             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    2036           2 :                                          l_val=.TRUE.)
    2037             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2038           2 :                                          r_val=hfx_fraction)
    2039             :             ELSE
    2040             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_PBE%SCALE", &
    2041           0 :                                          r_val=scale_x)
    2042           0 :                scale_x = scale_x + hfx_fraction
    2043             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2044           0 :                                          r_val=scale_x)
    2045             :             END IF
    2046             :          CASE (do_potential_truncated)
    2047           0 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    2048           0 :             ifun = 0
    2049           0 :             funct_found = .FALSE.
    2050             :             DO
    2051           0 :                ifun = ifun + 1
    2052           0 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2053           0 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2054           0 :                IF (xc_fun%section%name == "PBE_HOLE_T_C_LR") THEN
    2055           0 :                   funct_found = .TRUE.
    2056             :                END IF
    2057             :             END DO
    2058           0 :             IF (.NOT. funct_found) THEN
    2059             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    2060           0 :                                          l_val=.TRUE.)
    2061             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    2062           0 :                                          r_val=-hfx_fraction)
    2063             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    2064           0 :                                          r_val=cutoff_radius)
    2065             : 
    2066             :             ELSE
    2067             :                CALL section_vals_val_get(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    2068           0 :                                          r_val=scale_x)
    2069           0 :                scale_x = scale_x - hfx_fraction
    2070             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    2071           0 :                                          r_val=scale_x)
    2072             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    2073           0 :                                          r_val=cutoff_radius)
    2074             :             END IF
    2075           0 :             ifun = 0
    2076           0 :             funct_found = .FALSE.
    2077             :             DO
    2078           0 :                ifun = ifun + 1
    2079           0 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2080           0 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2081           0 :                IF (xc_fun%section%name == "GGA_X_PBE") THEN
    2082           0 :                   funct_found = .TRUE.
    2083             :                END IF
    2084             :             END DO
    2085           0 :             IF (.NOT. funct_found) THEN
    2086             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    2087           0 :                                          l_val=.TRUE.)
    2088             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2089           0 :                                          r_val=hfx_fraction)
    2090             : 
    2091             :             ELSE
    2092             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_PBE%SCALE", &
    2093           0 :                                          r_val=scale_x)
    2094           0 :                scale_x = scale_x + hfx_fraction
    2095             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2096           0 :                                          r_val=scale_x)
    2097             :             END IF
    2098             :          CASE (do_potential_mix_cl_trunc)
    2099           2 :             cutoff_radius = x_data(1, 1)%potential_parameter%cutoff_radius
    2100           2 :             omega = x_data(1, 1)%potential_parameter%omega
    2101           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    2102           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    2103           2 :             ifun = 0
    2104           2 :             funct_found = .FALSE.
    2105             :             DO
    2106           4 :                ifun = ifun + 1
    2107           4 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2108           4 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2109           4 :                IF (xc_fun%section%name == "PBE_HOLE_T_C_LR") THEN
    2110           0 :                   funct_found = .TRUE.
    2111             :                END IF
    2112             :             END DO
    2113           2 :             IF (.NOT. funct_found) THEN
    2114             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%_SECTION_PARAMETERS_", &
    2115           2 :                                          l_val=.TRUE.)
    2116             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    2117           2 :                                          r_val=-hfx_fraction*(scale_coulomb + scale_longrange))
    2118             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    2119           2 :                                          r_val=cutoff_radius)
    2120             : 
    2121             :             ELSE
    2122             :                CALL section_vals_val_get(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    2123           0 :                                          r_val=scale_x)
    2124           0 :                scale_x = scale_x - hfx_fraction*(scale_coulomb + scale_longrange)
    2125             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%SCALE_X", &
    2126           0 :                                          r_val=scale_x)
    2127             :                CALL section_vals_val_set(xc_fun_section, "PBE_HOLE_T_C_LR%CUTOFF_RADIUS", &
    2128           0 :                                          r_val=cutoff_radius)
    2129             :             END IF
    2130           2 :             ifun = 0
    2131           2 :             funct_found = .FALSE.
    2132             :             DO
    2133           6 :                ifun = ifun + 1
    2134           6 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2135           6 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2136           6 :                IF (xc_fun%section%name == "GGA_X_WPBEH") THEN
    2137           0 :                   funct_found = .TRUE.
    2138             :                END IF
    2139             :             END DO
    2140           2 :             IF (.NOT. funct_found) THEN
    2141             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    2142           2 :                                          l_val=.TRUE.)
    2143             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2144           2 :                                          r_val=-hfx_fraction*scale_longrange)
    2145             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    2146           2 :                                          r_val=omega)
    2147             : 
    2148             :             ELSE
    2149             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2150           0 :                                          r_val=scale_x)
    2151           0 :                scale_x = scale_x - hfx_fraction*scale_longrange
    2152             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2153           0 :                                          r_val=scale_x)
    2154             : 
    2155             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    2156           0 :                                          r_val=omega)
    2157             :             END IF
    2158           2 :             ifun = 0
    2159           2 :             funct_found = .FALSE.
    2160             :             DO
    2161           8 :                ifun = ifun + 1
    2162           8 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2163           8 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2164           8 :                IF (xc_fun%section%name == "GGA_X_PBE") THEN
    2165           0 :                   funct_found = .TRUE.
    2166             :                END IF
    2167             :             END DO
    2168           2 :             IF (.NOT. funct_found) THEN
    2169             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    2170           2 :                                          l_val=.TRUE.)
    2171             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2172           2 :                                          r_val=hfx_fraction*(scale_coulomb + scale_longrange))
    2173             :             ELSE
    2174             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_PBE%SCALE", &
    2175           0 :                                          r_val=scale_x)
    2176           0 :                scale_x = scale_x + hfx_fraction*(scale_coulomb + scale_longrange)
    2177             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2178           0 :                                          r_val=scale_x)
    2179             :             END IF
    2180             :          CASE (do_potential_mix_cl)
    2181           2 :             omega = x_data(1, 1)%potential_parameter%omega
    2182           2 :             scale_coulomb = x_data(1, 1)%potential_parameter%scale_coulomb
    2183           2 :             scale_longrange = x_data(1, 1)%potential_parameter%scale_longrange
    2184           2 :             ifun = 0
    2185           2 :             funct_found = .FALSE.
    2186             :             DO
    2187           4 :                ifun = ifun + 1
    2188           4 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2189           4 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2190           4 :                IF (xc_fun%section%name == "GGA_X_WPBEH") THEN
    2191           0 :                   funct_found = .TRUE.
    2192             :                END IF
    2193             :             END DO
    2194           2 :             IF (.NOT. funct_found) THEN
    2195             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_SECTION_PARAMETERS_", &
    2196           2 :                                          l_val=.TRUE.)
    2197             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2198           2 :                                          r_val=-hfx_fraction*scale_longrange)
    2199             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    2200           2 :                                          r_val=omega)
    2201             : 
    2202             :             ELSE
    2203             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2204           0 :                                          r_val=scale_x)
    2205           0 :                scale_x = scale_x - hfx_fraction*scale_longrange
    2206             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%SCALE", &
    2207           0 :                                          r_val=scale_x)
    2208             : 
    2209             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_WPBEH%_OMEGA", &
    2210           0 :                                          r_val=omega)
    2211             :             END IF
    2212           2 :             ifun = 0
    2213           2 :             funct_found = .FALSE.
    2214             :             DO
    2215           6 :                ifun = ifun + 1
    2216           6 :                xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2217           6 :                IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2218           6 :                IF (xc_fun%section%name == "GGA_X_PBE") THEN
    2219           0 :                   funct_found = .TRUE.
    2220             :                END IF
    2221             :             END DO
    2222          12 :             IF (.NOT. funct_found) THEN
    2223             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%_SECTION_PARAMETERS_", &
    2224           2 :                                          l_val=.TRUE.)
    2225             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2226           2 :                                          r_val=hfx_fraction*(scale_coulomb + scale_longrange))
    2227             :             ELSE
    2228             :                CALL section_vals_val_get(xc_fun_section, "GGA_X_PBE%SCALE", &
    2229           0 :                                          r_val=scale_x)
    2230           0 :                scale_x = scale_x + hfx_fraction*(scale_coulomb + scale_longrange)
    2231             :                CALL section_vals_val_set(xc_fun_section, "GGA_X_PBE%SCALE", &
    2232           0 :                                          r_val=scale_x)
    2233             :             END IF
    2234             :          END SELECT
    2235             : #else
    2236             :          CALL cp_abort(__LOCATION__, "In order use a LibXC-based ADMM "// &
    2237             :                        "exchange correction functionals, you have to compile and link against LibXC!")
    2238             : #endif
    2239             : 
    2240             :          ! PBEX (always bare form), OPTX and Becke88 functional
    2241             :       ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_pbex .OR. &
    2242             :                admm_env%aux_exch_func == do_admm_aux_exch_func_opt .OR. &
    2243             :                admm_env%aux_exch_func == do_admm_aux_exch_func_bee) THEN
    2244         118 :          IF (admm_env%aux_exch_func == do_admm_aux_exch_func_pbex) THEN
    2245          88 :             name_x_func = 'PBE'
    2246          30 :          ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt) THEN
    2247          14 :             name_x_func = 'OPTX'
    2248          16 :          ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_bee) THEN
    2249          16 :             name_x_func = 'BECKE88'
    2250             :          END IF
    2251             :          !primary basis
    2252             :          CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_SECTION_PARAMETERS_", &
    2253         118 :                                    l_val=.TRUE.)
    2254             :          CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE_X", &
    2255         118 :                                    r_val=-hfx_fraction)
    2256             : 
    2257         118 :          IF (admm_env%aux_exch_func == do_admm_aux_exch_func_pbex) THEN
    2258          88 :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE_C", r_val=0.0_dp)
    2259             :          END IF
    2260             : 
    2261         118 :          IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt) THEN
    2262          14 :             IF (admm_env%aux_exch_func_param) THEN
    2263             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%A1", &
    2264           0 :                                          r_val=admm_env%aux_x_param(1))
    2265             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%A2", &
    2266           0 :                                          r_val=admm_env%aux_x_param(2))
    2267             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%GAMMA", &
    2268           0 :                                          r_val=admm_env%aux_x_param(3))
    2269             :             END IF
    2270             :          END IF
    2271             : 
    2272             :          !** Now modify the functionals for the primary basis
    2273         118 :          xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_primary, "XC_FUNCTIONAL")
    2274             :          !* Overwrite possible L")
    2275             :          !* Overwrite possible shortcut
    2276             :          CALL section_vals_val_set(xc_fun_section, "_SECTION_PARAMETERS_", &
    2277         118 :                                    i_val=xc_funct_no_shortcut)
    2278             : 
    2279         118 :          ifun = 0
    2280         118 :          funct_found = .FALSE.
    2281             :          DO
    2282         208 :             ifun = ifun + 1
    2283         208 :             xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2284         208 :             IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2285         208 :             IF (xc_fun%section%name == TRIM(name_x_func)) THEN
    2286          44 :                funct_found = .TRUE.
    2287             :             END IF
    2288             :          END DO
    2289         118 :          IF (.NOT. funct_found) THEN
    2290             :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_SECTION_PARAMETERS_", &
    2291          74 :                                       l_val=.TRUE.)
    2292             :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE_X", &
    2293          74 :                                       r_val=hfx_fraction)
    2294          74 :             IF (admm_env%aux_exch_func == do_admm_aux_exch_func_pbex) THEN
    2295             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE_C", &
    2296          46 :                                          r_val=0.0_dp)
    2297          28 :             ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt) THEN
    2298          14 :                IF (admm_env%aux_exch_func_param) THEN
    2299             :                   CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%A1", &
    2300           0 :                                             r_val=admm_env%aux_x_param(1))
    2301             :                   CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%A2", &
    2302           0 :                                             r_val=admm_env%aux_x_param(2))
    2303             :                   CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%GAMMA", &
    2304           0 :                                             r_val=admm_env%aux_x_param(3))
    2305             :                END IF
    2306             :             END IF
    2307             : 
    2308             :          ELSE
    2309             :             CALL section_vals_val_get(xc_fun_section, TRIM(name_x_func)//"%SCALE_X", &
    2310          44 :                                       r_val=scale_x)
    2311          44 :             scale_x = scale_x + hfx_fraction
    2312             :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE_X", &
    2313          44 :                                       r_val=scale_x)
    2314          44 :             IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt) THEN
    2315           0 :                CPASSERT(.NOT. admm_env%aux_exch_func_param)
    2316             :             END IF
    2317             :          END IF
    2318             : 
    2319             :       ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_pbex_libxc .OR. &
    2320             :                admm_env%aux_exch_func == do_admm_aux_exch_func_opt_libxc .OR. &
    2321             :                admm_env%aux_exch_func == do_admm_aux_exch_func_sx_libxc .OR. &
    2322             :                admm_env%aux_exch_func == do_admm_aux_exch_func_bee_libxc) THEN
    2323             : #if defined(__LIBXC)
    2324          12 :          IF (admm_env%aux_exch_func == do_admm_aux_exch_func_pbex_libxc) THEN
    2325           2 :             name_x_func = 'GGA_X_PBE'
    2326          10 :          ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt_libxc) THEN
    2327           2 :             name_x_func = 'GGA_X_OPTX'
    2328           8 :          ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_bee_libxc) THEN
    2329           2 :             name_x_func = 'GGA_X_B88'
    2330           6 :          ELSE IF (admm_env%aux_exch_func == do_admm_aux_exch_func_sx_libxc) THEN
    2331           6 :             name_x_func = 'LDA_X'
    2332             :          END IF
    2333             :          !primary basis
    2334             :          CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_SECTION_PARAMETERS_", &
    2335          12 :                                    l_val=.TRUE.)
    2336             :          CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE", &
    2337          12 :                                    r_val=-hfx_fraction)
    2338             : 
    2339          12 :          IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt_libxc) THEN
    2340           2 :             IF (admm_env%aux_exch_func_param) THEN
    2341             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_A", &
    2342           0 :                                          r_val=admm_env%aux_x_param(1))
    2343             :                ! LibXC rescales the second parameter of the OPTX functional (see documentation there)
    2344             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_B", &
    2345           0 :                                          r_val=admm_env%aux_x_param(2)/x_factor_c)
    2346             :                CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_GAMMA", &
    2347           0 :                                          r_val=admm_env%aux_x_param(3))
    2348             :             END IF
    2349             :          END IF
    2350             : 
    2351             :          !** Now modify the functionals for the primary basis
    2352          12 :          xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_primary, "XC_FUNCTIONAL")
    2353             :          !* Overwrite possible L")
    2354             :          !* Overwrite possible shortcut
    2355             :          CALL section_vals_val_set(xc_fun_section, "_SECTION_PARAMETERS_", &
    2356          12 :                                    i_val=xc_funct_no_shortcut)
    2357             : 
    2358          12 :          ifun = 0
    2359          12 :          funct_found = .FALSE.
    2360             :          DO
    2361          24 :             ifun = ifun + 1
    2362          24 :             xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2363          24 :             IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2364          24 :             IF (xc_fun%section%name == TRIM(name_x_func)) THEN
    2365           0 :                funct_found = .TRUE.
    2366             :             END IF
    2367             :          END DO
    2368          12 :          IF (.NOT. funct_found) THEN
    2369             :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_SECTION_PARAMETERS_", &
    2370          12 :                                       l_val=.TRUE.)
    2371             :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE", &
    2372          12 :                                       r_val=hfx_fraction)
    2373          12 :             IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt_libxc) THEN
    2374           2 :                IF (admm_env%aux_exch_func_param) THEN
    2375             :                   CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_A", &
    2376           0 :                                             r_val=admm_env%aux_x_param(1))
    2377             :                   ! LibXC rescales the second parameter of the OPTX functional (see documentation there)
    2378             :                   CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_B", &
    2379           0 :                                             r_val=admm_env%aux_x_param(2)/x_factor_c)
    2380             :                   CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%_GAMMA", &
    2381           0 :                                             r_val=admm_env%aux_x_param(3))
    2382             :                END IF
    2383             :             END IF
    2384             : 
    2385             :          ELSE
    2386             :             CALL section_vals_val_get(xc_fun_section, TRIM(name_x_func)//"%SCALE", &
    2387           0 :                                       r_val=scale_x)
    2388           0 :             scale_x = scale_x + hfx_fraction
    2389             :             CALL section_vals_val_set(xc_fun_section, TRIM(name_x_func)//"%SCALE", &
    2390           0 :                                       r_val=scale_x)
    2391           0 :             IF (admm_env%aux_exch_func == do_admm_aux_exch_func_opt_libxc) THEN
    2392           0 :                CPASSERT(.NOT. admm_env%aux_exch_func_param)
    2393             :             END IF
    2394             :          END IF
    2395             : #else
    2396             :          CALL cp_abort(__LOCATION__, "In order use a LibXC-based ADMM "// &
    2397             :                        "exchange correction functionals, you have to compile and link against LibXC!")
    2398             : #endif
    2399             : 
    2400             :       ELSE
    2401           0 :          CPABORT("Unknown exchange correction functional!")
    2402             :       END IF
    2403             : 
    2404             :       IF (debug_functional) THEN
    2405             :          iounit = cp_logger_get_default_io_unit(logger)
    2406             :          IF (iounit > 0) THEN
    2407             :             WRITE (iounit, "(A)") " ADMM Primary Basis Set Functional"
    2408             :          END IF
    2409             :          xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_primary, "XC_FUNCTIONAL")
    2410             :          ifun = 0
    2411             :          funct_found = .FALSE.
    2412             :          DO
    2413             :             ifun = ifun + 1
    2414             :             xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2415             :             IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2416             : 
    2417             :             scale_x = -1000.0_dp
    2418             :             IF (xc_fun%section%name /= "LYP" .AND. xc_fun%section%name /= "VWN") THEN
    2419             :                CALL section_vals_val_get(xc_fun, "SCALE_X", r_val=scale_x)
    2420             :             END IF
    2421             :             IF (xc_fun%section%name == "XWPBE") THEN
    2422             :                CALL section_vals_val_get(xc_fun, "SCALE_X0", r_val=hfx_fraction)
    2423             :                IF (iounit > 0) THEN
    2424             :                   WRITE (iounit, "(T5,A,T25,2F10.3)") TRIM(xc_fun%section%name), scale_x, hfx_fraction
    2425             :                END IF
    2426             :             ELSE
    2427             :                IF (iounit > 0) THEN
    2428             :                   WRITE (iounit, "(T5,A,T25,F10.3)") TRIM(xc_fun%section%name), scale_x
    2429             :                END IF
    2430             :             END IF
    2431             :          END DO
    2432             : 
    2433             :          IF (iounit > 0) THEN
    2434             :             WRITE (iounit, "(A)") " Auxiliary Basis Set Functional"
    2435             :          END IF
    2436             :          xc_fun_section => section_vals_get_subs_vals(admm_env%xc_section_aux, "XC_FUNCTIONAL")
    2437             :          ifun = 0
    2438             :          funct_found = .FALSE.
    2439             :          DO
    2440             :             ifun = ifun + 1
    2441             :             xc_fun => section_vals_get_subs_vals2(xc_fun_section, i_section=ifun)
    2442             :             IF (.NOT. ASSOCIATED(xc_fun)) EXIT
    2443             :             scale_x = -1000.0_dp
    2444             :             IF (xc_fun%section%name /= "LYP" .AND. xc_fun%section%name /= "VWN") THEN
    2445             :                CALL section_vals_val_get(xc_fun, "SCALE_X", r_val=scale_x)
    2446             :             END IF
    2447             :             IF (xc_fun%section%name == "XWPBE") THEN
    2448             :                CALL section_vals_val_get(xc_fun, "SCALE_X0", r_val=hfx_fraction)
    2449             :                IF (iounit > 0) THEN
    2450             :                   WRITE (iounit, "(T5,A,T25,2F10.3)") TRIM(xc_fun%section%name), scale_x, hfx_fraction
    2451             :                END IF
    2452             :             ELSE
    2453             :                IF (iounit > 0) THEN
    2454             :                   WRITE (iounit, "(T5,A,T25,F10.3)") TRIM(xc_fun%section%name), scale_x
    2455             :                END IF
    2456             :             END IF
    2457             :          END DO
    2458             :       END IF
    2459             : 
    2460         466 :    END SUBROUTINE create_admm_xc_section
    2461             : 
    2462             : ! **************************************************************************************************
    2463             : !> \brief Add the hfx contributions to the Hamiltonian
    2464             : !>
    2465             : !> \param matrix_ks Kohn-Sham matrix (updated on exit)
    2466             : !> \param rho_ao    electron density expressed in terms of atomic orbitals
    2467             : !> \param qs_env    Quickstep environment
    2468             : !> \param update_energy whether to update energy (default: yes)
    2469             : !> \param recalc_integrals whether to recalculate integrals (default: value of HF%TREAT_LSD_IN_CORE)
    2470             : !> \param external_hfx_sections ...
    2471             : !> \param external_x_data ...
    2472             : !> \param external_para_env ...
    2473             : !> \note
    2474             : !>     Simplified version of subroutine hfx_ks_matrix()
    2475             : ! **************************************************************************************************
    2476        7515 :    SUBROUTINE tddft_hfx_matrix(matrix_ks, rho_ao, qs_env, update_energy, recalc_integrals, &
    2477        7515 :                                external_hfx_sections, external_x_data, external_para_env)
    2478             :       TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT), &
    2479             :          TARGET                                          :: matrix_ks, rho_ao
    2480             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2481             :       LOGICAL, INTENT(IN), OPTIONAL                      :: update_energy, recalc_integrals
    2482             :       TYPE(section_vals_type), OPTIONAL, POINTER         :: external_hfx_sections
    2483             :       TYPE(hfx_type), DIMENSION(:, :), OPTIONAL, TARGET  :: external_x_data
    2484             :       TYPE(mp_para_env_type), OPTIONAL, POINTER          :: external_para_env
    2485             : 
    2486             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'tddft_hfx_matrix'
    2487             : 
    2488             :       INTEGER                                            :: handle, irep, ispin, mspin, n_rep_hf, &
    2489             :                                                             nspins
    2490             :       LOGICAL                                            :: distribute_fock_matrix, &
    2491             :                                                             hfx_treat_lsd_in_core, &
    2492             :                                                             my_update_energy, s_mstruct_changed
    2493             :       REAL(KIND=dp)                                      :: eh1, ehfx
    2494        7515 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_kp, rho_ao_kp
    2495             :       TYPE(dft_control_type), POINTER                    :: dft_control
    2496        7515 :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
    2497             :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2498             :       TYPE(qs_energy_type), POINTER                      :: energy
    2499             :       TYPE(section_vals_type), POINTER                   :: hfx_sections, input
    2500             : 
    2501        7515 :       CALL timeset(routineN, handle)
    2502             : 
    2503        7515 :       NULLIFY (dft_control, hfx_sections, input, para_env, matrix_ks_kp, rho_ao_kp)
    2504             : 
    2505             :       CALL get_qs_env(qs_env=qs_env, &
    2506             :                       dft_control=dft_control, &
    2507             :                       energy=energy, &
    2508             :                       input=input, &
    2509             :                       para_env=para_env, &
    2510             :                       s_mstruct_changed=s_mstruct_changed, &
    2511        7515 :                       x_data=x_data)
    2512             : 
    2513             :       ! This should probably be the HF section from the TDDFPT XC section!
    2514        7515 :       hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%HF")
    2515             : 
    2516        7515 :       IF (PRESENT(external_hfx_sections)) hfx_sections => external_hfx_sections
    2517        7515 :       IF (PRESENT(external_x_data)) x_data => external_x_data
    2518        7515 :       IF (PRESENT(external_para_env)) para_env => external_para_env
    2519             : 
    2520        7515 :       my_update_energy = .TRUE.
    2521        7515 :       IF (PRESENT(update_energy)) my_update_energy = update_energy
    2522             : 
    2523        7515 :       IF (PRESENT(recalc_integrals)) s_mstruct_changed = recalc_integrals
    2524             : 
    2525        7515 :       CPASSERT(dft_control%nimages == 1)
    2526        7515 :       nspins = dft_control%nspins
    2527             : 
    2528        7515 :       CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
    2529             :       CALL section_vals_val_get(hfx_sections, "TREAT_LSD_IN_CORE", l_val=hfx_treat_lsd_in_core, &
    2530        7515 :                                 i_rep_section=1)
    2531             : 
    2532        7515 :       CALL section_vals_get(hfx_sections, n_repetition=n_rep_hf)
    2533        7515 :       distribute_fock_matrix = .TRUE.
    2534             : 
    2535        7515 :       mspin = 1
    2536        7515 :       IF (hfx_treat_lsd_in_core) mspin = nspins
    2537             : 
    2538        7515 :       matrix_ks_kp(1:nspins, 1:1) => matrix_ks(1:nspins)
    2539        7515 :       rho_ao_kp(1:nspins, 1:1) => rho_ao(1:nspins)
    2540             : 
    2541       14890 :       DO irep = 1, n_rep_hf
    2542             :          ! the real hfx calulation
    2543        7375 :          ehfx = 0.0_dp
    2544             : 
    2545       14890 :          IF (x_data(irep, 1)%do_hfx_ri) THEN
    2546             :             CALL hfx_ri_update_ks(qs_env, x_data(irep, 1)%ri_data, matrix_ks_kp, ehfx, &
    2547             :                                   rho_ao=rho_ao_kp, geometry_did_change=s_mstruct_changed, &
    2548         356 :                                   nspins=nspins, hf_fraction=x_data(irep, 1)%general_parameter%fraction)
    2549             : 
    2550             :          ELSE
    2551       14038 :             DO ispin = 1, mspin
    2552             :                CALL integrate_four_center(qs_env, x_data, matrix_ks_kp, eh1, rho_ao_kp, hfx_sections, para_env, &
    2553        7019 :                                           s_mstruct_changed, irep, distribute_fock_matrix, ispin=ispin)
    2554       14038 :                ehfx = ehfx + eh1
    2555             :             END DO
    2556             :          END IF
    2557             :       END DO
    2558        7515 :       IF (my_update_energy) energy%ex = ehfx
    2559             : 
    2560        7515 :       CALL timestop(handle)
    2561        7515 :    END SUBROUTINE tddft_hfx_matrix
    2562             : 
    2563             : END MODULE hfx_admm_utils

Generated by: LCOV version 1.15