LCOV - code coverage report
Current view: top level - src - qs_environment.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:06f838d) Lines: 93.2 % 1101 1026
Test Date: 2026-06-05 07:04:50 Functions: 100.0 % 7 7

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \par History
      10              : !>      - Merged with the Quickstep MODULE method_specification (17.01.2002,MK)
      11              : !>      - USE statements cleaned, added
      12              : !>        (25.09.2002,MK)
      13              : !>      - Added more LSD structure (01.2003,Joost VandeVondele)
      14              : !>      - New molecule data types introduced (Sep. 2003,MK)
      15              : !>      - Cleaning; getting rid of pnode (02.10.2003,MK)
      16              : !>      - Sub-system setup added (08.10.2003,MK)
      17              : !> \author MK (18.05.2000)
      18              : ! **************************************************************************************************
      19              : MODULE qs_environment
      20              :    USE almo_scf_env_methods,            ONLY: almo_scf_env_create
      21              :    USE atom_kind_orbitals,              ONLY: calculate_atomic_relkin
      22              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      23              :    USE auto_basis,                      ONLY: create_lri_aux_basis_set,&
      24              :                                               create_ri_aux_basis_set
      25              :    USE basis_set_container_types,       ONLY: add_basis_set_to_container
      26              :    USE basis_set_types,                 ONLY: basis_sort_zet,&
      27              :                                               create_primitive_basis_set,&
      28              :                                               deallocate_gto_basis_set,&
      29              :                                               gto_basis_set_type
      30              :    USE bibliography,                    ONLY: Iannuzzi2006,&
      31              :                                               Iannuzzi2007,&
      32              :                                               cite_reference,&
      33              :                                               cp2kqs2020
      34              :    USE cell_types,                      ONLY: cell_type
      35              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      36              :                                               cp_blacs_env_release,&
      37              :                                               cp_blacs_env_type
      38              :    USE cp_control_types,                ONLY: dft_control_type,&
      39              :                                               dftb_control_type,&
      40              :                                               gapw_control_type,&
      41              :                                               qs_control_type,&
      42              :                                               semi_empirical_control_type,&
      43              :                                               xtb_control_type
      44              :    USE cp_control_utils,                ONLY: &
      45              :         read_ddapc_section, read_dft_control, read_mgrid_section, read_qs_section, &
      46              :         read_rixs_control, read_tddfpt2_control, write_admm_control, write_dft_control, &
      47              :         write_qs_control
      48              :    USE cp_ddapc_types,                  ONLY: cp_ddapc_ewald_create
      49              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      50              :                                               cp_logger_get_default_io_unit,&
      51              :                                               cp_logger_type,&
      52              :                                               cp_to_string
      53              :    USE cp_output_handling,              ONLY: cp_p_file,&
      54              :                                               cp_print_key_finished_output,&
      55              :                                               cp_print_key_should_output,&
      56              :                                               cp_print_key_unit_nr
      57              :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      58              :    USE cp_symmetry,                     ONLY: write_symmetry
      59              :    USE distribution_1d_types,           ONLY: distribution_1d_release,&
      60              :                                               distribution_1d_type
      61              :    USE distribution_methods,            ONLY: distribute_molecules_1d
      62              :    USE ec_env_types,                    ONLY: energy_correction_type
      63              :    USE ec_environment,                  ONLY: ec_env_create,&
      64              :                                               ec_write_input
      65              :    USE et_coupling_types,               ONLY: et_coupling_create
      66              :    USE ewald_environment_types,         ONLY: ewald_env_create,&
      67              :                                               ewald_env_get,&
      68              :                                               ewald_env_set,&
      69              :                                               ewald_environment_type,&
      70              :                                               read_ewald_section,&
      71              :                                               read_ewald_section_tb
      72              :    USE ewald_pw_methods,                ONLY: ewald_pw_grid_update
      73              :    USE ewald_pw_types,                  ONLY: ewald_pw_create,&
      74              :                                               ewald_pw_type
      75              :    USE exstates_types,                  ONLY: excited_energy_type,&
      76              :                                               exstate_create
      77              :    USE external_potential_types,        ONLY: get_potential,&
      78              :                                               init_potential,&
      79              :                                               set_potential
      80              :    USE fist_nonbond_env_types,          ONLY: fist_nonbond_env_create,&
      81              :                                               fist_nonbond_env_type
      82              :    USE gamma,                           ONLY: init_md_ftable
      83              :    USE global_types,                    ONLY: global_environment_type
      84              :    USE hartree_local_methods,           ONLY: init_coulomb_local
      85              :    USE header,                          ONLY: dftb_header,&
      86              :                                               qs_header,&
      87              :                                               se_header,&
      88              :                                               tblite_header,&
      89              :                                               xtb_header
      90              :    USE hfx_types,                       ONLY: compare_hfx_sections,&
      91              :                                               hfx_create
      92              :    USE input_constants,                 ONLY: &
      93              :         debug_run, dispersion_d2, dispersion_d3, dispersion_d3bj, do_et_ddapc, do_method_am1, &
      94              :         do_method_dftb, do_method_gapw, do_method_gapw_xc, do_method_gpw, do_method_lrigpw, &
      95              :         do_method_mndo, do_method_mndod, do_method_ofgpw, do_method_pdg, do_method_pm3, &
      96              :         do_method_pm6, do_method_pm6fm, do_method_pnnl, do_method_rigpw, do_method_rm1, &
      97              :         do_method_xtb, do_qmmm_gauss, do_qmmm_swave, general_roks, gfn1xtb, hden_atomic, &
      98              :         kg_tnadd_embed_ri, linear_response_run, rel_none, rel_trans_atom, smear_fermi_dirac, &
      99              :         tblite_scc_mixer_tblite, tddfpt_kernel_none, vdw_pairpot_dftd2, vdw_pairpot_dftd3, &
     100              :         vdw_pairpot_dftd3bj, vdw_pairpot_dftd4, wfi_linear_ps_method_nr, wfi_linear_wf_method_nr, &
     101              :         wfi_use_prev_wf_method_nr, xc_vdw_fun_none, xc_vdw_fun_nonloc, xc_vdw_fun_pairpot, &
     102              :         xtb_vdw_type_d3, xtb_vdw_type_d4, xtb_vdw_type_none
     103              :    USE input_section_types,             ONLY: section_get_ival,&
     104              :                                               section_get_ivals,&
     105              :                                               section_vals_get,&
     106              :                                               section_vals_get_subs_vals,&
     107              :                                               section_vals_type,&
     108              :                                               section_vals_val_get
     109              :    USE kg_environment,                  ONLY: kg_env_create
     110              :    USE kinds,                           ONLY: default_string_length,&
     111              :                                               dp
     112              :    USE kpoint_methods,                  ONLY: kpoint_env_initialize,&
     113              :                                               kpoint_initialize,&
     114              :                                               kpoint_initialize_mos
     115              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
     116              :                                               kpoint_create,&
     117              :                                               kpoint_reset_initialization,&
     118              :                                               kpoint_type,&
     119              :                                               read_kpoint_section,&
     120              :                                               set_kpoint_info,&
     121              :                                               write_kpoint_info
     122              :    USE lri_environment_init,            ONLY: lri_env_basis,&
     123              :                                               lri_env_init
     124              :    USE lri_environment_types,           ONLY: lri_environment_type
     125              :    USE machine,                         ONLY: m_flush
     126              :    USE mathconstants,                   ONLY: pi
     127              :    USE message_passing,                 ONLY: mp_para_env_type
     128              :    USE molecule_kind_types,             ONLY: molecule_kind_type,&
     129              :                                               write_molecule_kind_set
     130              :    USE molecule_types,                  ONLY: molecule_type
     131              :    USE mp2_setup,                       ONLY: read_mp2_section
     132              :    USE mp2_types,                       ONLY: mp2_env_create,&
     133              :                                               mp2_type
     134              :    USE multipole_types,                 ONLY: do_multipole_none
     135              :    USE orbital_pointers,                ONLY: init_orbital_pointers
     136              :    USE orbital_transformation_matrices, ONLY: init_spherical_harmonics
     137              :    USE particle_methods,                ONLY: write_particle_distances,&
     138              :                                               write_qs_particle_coordinates,&
     139              :                                               write_structure_data
     140              :    USE particle_types,                  ONLY: particle_type
     141              :    USE physcon,                         ONLY: kelvin
     142              :    USE pw_env_types,                    ONLY: pw_env_type
     143              :    USE qmmm_types_low,                  ONLY: qmmm_env_qm_type
     144              :    USE qs_basis_rotation_methods,       ONLY: qs_basis_rotation
     145              :    USE qs_dftb_parameters,              ONLY: qs_dftb_param_init
     146              :    USE qs_dftb_types,                   ONLY: qs_dftb_atom_type,&
     147              :                                               qs_dftb_pairpot_type
     148              :    USE qs_dftb_utils,                   ONLY: get_dftb_atom_param
     149              :    USE qs_dispersion_nonloc,            ONLY: qs_dispersion_nonloc_init
     150              :    USE qs_dispersion_pairpot,           ONLY: qs_dispersion_pairpot_init
     151              :    USE qs_dispersion_types,             ONLY: qs_dispersion_type
     152              :    USE qs_dispersion_utils,             ONLY: qs_dispersion_env_set,&
     153              :                                               qs_write_dispersion
     154              :    USE qs_energy_types,                 ONLY: allocate_qs_energy,&
     155              :                                               qs_energy_type
     156              :    USE qs_environment_methods,          ONLY: qs_env_setup
     157              :    USE qs_environment_types,            ONLY: get_qs_env,&
     158              :                                               qs_environment_type,&
     159              :                                               set_qs_env
     160              :    USE qs_force_types,                  ONLY: qs_force_type
     161              :    USE qs_gcp_types,                    ONLY: qs_gcp_type
     162              :    USE qs_gcp_utils,                    ONLY: qs_gcp_env_set,&
     163              :                                               qs_gcp_init
     164              :    USE qs_harris_types,                 ONLY: harris_rhoin_init,&
     165              :                                               harris_type
     166              :    USE qs_harris_utils,                 ONLY: harris_env_create,&
     167              :                                               harris_write_input
     168              :    USE qs_interactions,                 ONLY: init_interaction_radii,&
     169              :                                               init_se_nlradius,&
     170              :                                               write_core_charge_radii,&
     171              :                                               write_paw_radii,&
     172              :                                               write_pgf_orb_radii,&
     173              :                                               write_ppl_radii,&
     174              :                                               write_ppnl_radii
     175              :    USE qs_kind_types,                   ONLY: &
     176              :         check_qs_kind_set, get_qs_kind, get_qs_kind_set, init_cneo_basis_set, init_gapw_basis_set, &
     177              :         init_gapw_nlcc, init_qs_kind_set, qs_kind_type, set_qs_kind, write_gto_basis_sets, &
     178              :         write_qs_kind_set
     179              :    USE qs_ks_types,                     ONLY: qs_ks_env_create,&
     180              :                                               qs_ks_env_type,&
     181              :                                               set_ks_env
     182              :    USE qs_local_rho_types,              ONLY: local_rho_type
     183              :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
     184              :                                               mo_set_type
     185              :    USE qs_rho0_ggrid,                   ONLY: rho0_s_grid_create
     186              :    USE qs_rho0_methods,                 ONLY: init_rho0
     187              :    USE qs_rho0_types,                   ONLY: rho0_mpole_type
     188              :    USE qs_rho_atom_methods,             ONLY: init_rho_atom
     189              :    USE qs_rho_atom_types,               ONLY: rho_atom_type
     190              :    USE qs_subsys_methods,               ONLY: qs_subsys_create
     191              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
     192              :                                               qs_subsys_set,&
     193              :                                               qs_subsys_type
     194              :    USE qs_wf_history_methods,           ONLY: wfi_create,&
     195              :                                               wfi_create_for_kp
     196              :    USE qs_wf_history_types,             ONLY: qs_wf_history_type,&
     197              :                                               wfi_release
     198              :    USE rel_control_types,               ONLY: rel_c_create,&
     199              :                                               rel_c_read_parameters,&
     200              :                                               rel_control_type
     201              :    USE scf_control_types,               ONLY: scf_c_create,&
     202              :                                               scf_c_read_parameters,&
     203              :                                               scf_c_write_parameters,&
     204              :                                               scf_control_type
     205              :    USE semi_empirical_expns3_methods,   ONLY: semi_empirical_expns3_setup
     206              :    USE semi_empirical_int_arrays,       ONLY: init_se_intd_array
     207              :    USE semi_empirical_mpole_methods,    ONLY: nddo_mpole_setup
     208              :    USE semi_empirical_mpole_types,      ONLY: nddo_mpole_type
     209              :    USE semi_empirical_store_int_types,  ONLY: semi_empirical_si_create,&
     210              :                                               semi_empirical_si_type
     211              :    USE semi_empirical_types,            ONLY: se_taper_create,&
     212              :                                               se_taper_type
     213              :    USE semi_empirical_utils,            ONLY: se_cutoff_compatible
     214              :    USE tblite_interface,                ONLY: tb_get_basis,&
     215              :                                               tb_init_geometry,&
     216              :                                               tb_init_wf,&
     217              :                                               tb_set_calculator
     218              :    USE transport,                       ONLY: transport_env_create
     219              :    USE xtb_parameters,                  ONLY: init_xtb_basis,&
     220              :                                               xtb_parameters_init,&
     221              :                                               xtb_parameters_set
     222              :    USE xtb_potentials,                  ONLY: xtb_pp_radius
     223              :    USE xtb_types,                       ONLY: allocate_xtb_atom_param,&
     224              :                                               set_xtb_atom_param
     225              : #include "./base/base_uses.f90"
     226              : 
     227              :    IMPLICIT NONE
     228              : 
     229              :    PRIVATE
     230              : 
     231              :    ! *** Global parameters ***
     232              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_environment'
     233              : 
     234              :    ! *** Public subroutines ***
     235              :    PUBLIC :: qs_init
     236              : 
     237              : CONTAINS
     238              : 
     239              : ! **************************************************************************************************
     240              : !> \brief Read the input and the database files for the setup of the
     241              : !>      QUICKSTEP environment.
     242              : !> \param qs_env ...
     243              : !> \param para_env ...
     244              : !> \param root_section ...
     245              : !> \param globenv ...
     246              : !> \param cp_subsys ...
     247              : !> \param kpoint_env ...
     248              : !> \param qmmm ...
     249              : !> \param qmmm_env_qm ...
     250              : !> \param force_env_section ...
     251              : !> \param subsys_section ...
     252              : !> \param use_motion_section ...
     253              : !> \param silent ...
     254              : !> \author Creation (22.05.2000,MK)
     255              : ! **************************************************************************************************
     256        59010 :    SUBROUTINE qs_init(qs_env, para_env, root_section, globenv, cp_subsys, kpoint_env, &
     257              :                       qmmm, qmmm_env_qm, force_env_section, subsys_section, &
     258              :                       use_motion_section, silent)
     259              : 
     260              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     261              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     262              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: root_section
     263              :       TYPE(global_environment_type), OPTIONAL, POINTER   :: globenv
     264              :       TYPE(cp_subsys_type), OPTIONAL, POINTER            :: cp_subsys
     265              :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoint_env
     266              :       LOGICAL, INTENT(IN), OPTIONAL                      :: qmmm
     267              :       TYPE(qmmm_env_qm_type), OPTIONAL, POINTER          :: qmmm_env_qm
     268              :       TYPE(section_vals_type), POINTER                   :: force_env_section, subsys_section
     269              :       LOGICAL, INTENT(IN)                                :: use_motion_section
     270              :       LOGICAL, INTENT(IN), OPTIONAL                      :: silent
     271              : 
     272              :       CHARACTER(LEN=default_string_length)               :: basis_type
     273              :       INTEGER                                            :: ikind, method_id, nelectron_total, &
     274              :                                                             nkind, nkp_grid(3), tddfpt_kernel
     275              :       LOGICAL :: dftb_kpoint_sym_restricted, do_active_space, do_admm, do_admm_rpa, do_bse, &
     276              :          do_debug_fdiff, do_debug_forces, do_debug_stress_tensor, do_dftb_scc, do_dftb_scc_high_l, &
     277              :          do_ec_hfx, do_et, do_exx, do_gw, do_hfx, do_kpoints, do_linear_response, do_mp2, &
     278              :          do_ri_mp2, do_ri_rpa, do_ri_sos_mp2, do_tddfpt, do_tddfpt_unsupported_kpoints, &
     279              :          do_wfc_low_scaling, do_wfc_low_scaling_kpoints, do_xtb_tblite, final_kpoint_reinit, &
     280              :          is_identical, is_semi, kpoint_verbose, mp2_present, my_qmmm, owned_kpoints, qmmm_decoupl, &
     281              :          same_except_frac, use_real_wfn, use_ref_cell
     282         8430 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: rtmat
     283         8430 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     284              :       TYPE(cell_type), POINTER                           :: my_cell, my_cell_ref
     285              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     286              :       TYPE(dft_control_type), POINTER                    :: dft_control
     287              :       TYPE(distribution_1d_type), POINTER                :: local_particles
     288              :       TYPE(energy_correction_type), POINTER              :: ec_env
     289              :       TYPE(excited_energy_type), POINTER                 :: exstate_env
     290              :       TYPE(harris_type), POINTER                         :: harris_env
     291              :       TYPE(kpoint_type), POINTER                         :: kpoints
     292              :       TYPE(lri_environment_type), POINTER                :: lri_env
     293         8430 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     294         8430 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     295              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     296              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     297              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
     298              :       TYPE(rel_control_type), POINTER                    :: rel_control
     299              :       TYPE(scf_control_type), POINTER                    :: scf_control
     300              :       TYPE(section_vals_type), POINTER :: active_space_section, admm_section, dft_section, &
     301              :          ec_hfx_section, ec_section, et_coupling_section, gw_section, hfx_section, kpoint_section, &
     302              :          mp2_section, rpa_hfx_section, tddfpt_section, transport_section
     303              : 
     304         8430 :       NULLIFY (my_cell, my_cell_ref, atomic_kind_set, particle_set, &
     305         8430 :                qs_kind_set, kpoint_section, dft_section, ec_section, &
     306         8430 :                subsys, ks_env, dft_control, blacs_env)
     307              : 
     308         8430 :       CALL set_qs_env(qs_env, input=force_env_section)
     309         8430 :       IF (.NOT. ASSOCIATED(subsys_section)) THEN
     310          108 :          subsys_section => section_vals_get_subs_vals(force_env_section, "SUBSYS")
     311              :       END IF
     312              : 
     313              :       ! QMMM
     314         8430 :       my_qmmm = .FALSE.
     315         8430 :       IF (PRESENT(qmmm)) my_qmmm = qmmm
     316         8430 :       qmmm_decoupl = .FALSE.
     317         8430 :       IF (PRESENT(qmmm_env_qm)) THEN
     318          394 :          IF (qmmm_env_qm%qmmm_coupl_type == do_qmmm_gauss .OR. &
     319              :              qmmm_env_qm%qmmm_coupl_type == do_qmmm_swave) THEN
     320              :             ! For GAUSS/SWAVE methods there could be a DDAPC decoupling requested
     321          458 :             qmmm_decoupl = my_qmmm .AND. qmmm_env_qm%periodic .AND. qmmm_env_qm%multipole
     322              :          END IF
     323          394 :          qs_env%qmmm_env_qm => qmmm_env_qm
     324              :       END IF
     325         8430 :       CALL set_qs_env(qs_env=qs_env, qmmm=my_qmmm)
     326              : 
     327              :       ! Possibly initialize arrays for SE
     328         8430 :       CALL section_vals_val_get(force_env_section, "DFT%QS%METHOD", i_val=method_id)
     329         1000 :       SELECT CASE (method_id)
     330              :       CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pdg, &
     331              :             do_method_pm3, do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
     332         1000 :          CALL init_se_intd_array()
     333         1000 :          is_semi = .TRUE.
     334              :       CASE (do_method_xtb, do_method_dftb)
     335         1426 :          is_semi = .TRUE.
     336              :       CASE DEFAULT
     337         8430 :          is_semi = .FALSE.
     338              :       END SELECT
     339              : 
     340        33720 :       ALLOCATE (subsys)
     341              :       CALL qs_subsys_create(subsys, para_env, &
     342              :                             force_env_section=force_env_section, &
     343              :                             subsys_section=subsys_section, &
     344              :                             use_motion_section=use_motion_section, &
     345              :                             root_section=root_section, &
     346              :                             cp_subsys=cp_subsys, &
     347         8430 :                             elkind=is_semi, silent=silent)
     348              : 
     349         8430 :       ALLOCATE (ks_env)
     350         8430 :       CALL qs_ks_env_create(ks_env)
     351         8430 :       CALL set_ks_env(ks_env, subsys=subsys)
     352         8430 :       CALL set_qs_env(qs_env, ks_env=ks_env)
     353              : 
     354              :       CALL qs_subsys_get(subsys, &
     355              :                          cell=my_cell, &
     356              :                          cell_ref=my_cell_ref, &
     357              :                          use_ref_cell=use_ref_cell, &
     358              :                          atomic_kind_set=atomic_kind_set, &
     359              :                          qs_kind_set=qs_kind_set, &
     360         8430 :                          particle_set=particle_set)
     361              : 
     362         8430 :       CALL set_ks_env(ks_env, para_env=para_env)
     363         8430 :       IF (PRESENT(globenv)) THEN
     364              :          CALL cp_blacs_env_create(blacs_env, para_env, globenv%blacs_grid_layout, &
     365         8424 :                                   globenv%blacs_repeatable)
     366              :       ELSE
     367            6 :          CALL cp_blacs_env_create(blacs_env, para_env)
     368              :       END IF
     369         8430 :       CALL set_ks_env(ks_env, blacs_env=blacs_env)
     370         8430 :       CALL cp_blacs_env_release(blacs_env)
     371              : 
     372              :       !   *** Setup the grids for the G-space Interpolation if any
     373              :       CALL cp_ddapc_ewald_create(qs_env%cp_ddapc_ewald, qmmm_decoupl, my_cell, &
     374         8430 :                                  force_env_section, subsys_section, para_env)
     375              : 
     376              :       ! kpoints
     377         8430 :       IF (PRESENT(kpoint_env)) THEN
     378            2 :          owned_kpoints = .FALSE.
     379            2 :          kpoints => kpoint_env
     380            2 :          CALL set_qs_env(qs_env=qs_env, kpoints=kpoints)
     381            2 :          CALL kpoint_initialize(kpoints, particle_set, my_cell)
     382              :       ELSE
     383         8428 :          owned_kpoints = .TRUE.
     384         8428 :          NULLIFY (kpoints)
     385         8428 :          CALL kpoint_create(kpoints)
     386         8428 :          CALL set_qs_env(qs_env=qs_env, kpoints=kpoints)
     387         8428 :          kpoint_section => section_vals_get_subs_vals(qs_env%input, "DFT%KPOINTS")
     388         8428 :          CALL read_kpoint_section(kpoints, kpoint_section, my_cell%hmat, my_cell)
     389         8428 :          CALL get_kpoint_info(kpoints, verbose=kpoint_verbose)
     390         8428 :          IF (kpoint_verbose) CALL set_kpoint_info(kpoints, verbose=.FALSE.)
     391              :          do_hfx = .FALSE.
     392         8428 :          hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF")
     393         8428 :          CALL section_vals_get(hfx_section, explicit=do_hfx)
     394              :          do_exx = .FALSE.
     395         8428 :          rpa_hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%HF")
     396         8428 :          CALL section_vals_get(rpa_hfx_section, explicit=do_exx)
     397              :          do_admm = .FALSE.
     398         8428 :          admm_section => section_vals_get_subs_vals(qs_env%input, "DFT%AUXILIARY_DENSITY_MATRIX_METHOD")
     399         8428 :          CALL section_vals_get(admm_section, explicit=do_admm)
     400              :          do_gw = .FALSE.
     401         8428 :          gw_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%GW")
     402         8428 :          CALL section_vals_get(gw_section, explicit=do_gw)
     403         8428 :          IF (.NOT. do_gw) THEN
     404         8320 :             gw_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%BANDSTRUCTURE%GW")
     405         8320 :             CALL section_vals_get(gw_section, explicit=do_gw)
     406              :          END IF
     407              :          do_tddfpt = .FALSE.
     408         8428 :          do_tddfpt_unsupported_kpoints = .FALSE.
     409         8428 :          do_bse = .FALSE.
     410         8428 :          tddfpt_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%TDDFPT")
     411         8428 :          CALL section_vals_get(tddfpt_section, explicit=do_tddfpt)
     412         8428 :          IF (do_tddfpt) THEN
     413          662 :             CALL section_vals_val_get(tddfpt_section, "KERNEL", i_val=tddfpt_kernel)
     414          662 :             do_tddfpt_unsupported_kpoints = tddfpt_kernel /= tddfpt_kernel_none
     415          662 :             IF (.NOT. do_tddfpt_unsupported_kpoints) THEN
     416           58 :                CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn)
     417           58 :                IF (use_real_wfn) &
     418            0 :                   CALL cp_abort(__LOCATION__, "K-point TDDFPT requires complex wavefunctions.")
     419              :             END IF
     420          662 :             CALL section_vals_val_get(tddfpt_section, "DO_BSE", l_val=do_bse)
     421          662 :             IF (.NOT. do_bse) &
     422          660 :                CALL section_vals_val_get(tddfpt_section, "DO_BSE_W_ONLY", l_val=do_bse)
     423          662 :             IF (.NOT. do_bse) &
     424          658 :                CALL section_vals_val_get(tddfpt_section, "DO_BSE_GW_ONLY", l_val=do_bse)
     425              :          END IF
     426              :          do_active_space = .FALSE.
     427         8428 :          active_space_section => section_vals_get_subs_vals(qs_env%input, "DFT%ACTIVE_SPACE")
     428         8428 :          CALL section_vals_get(active_space_section, explicit=do_active_space)
     429         8428 :          do_xtb_tblite = .FALSE.
     430         8428 :          IF (method_id == do_method_xtb) THEN
     431              :             CALL section_vals_val_get(qs_env%input, "DFT%QS%XTB%TBLITE%_SECTION_PARAMETERS_", &
     432         1134 :                                       l_val=do_xtb_tblite)
     433              :          END IF
     434         8428 :          do_dftb_scc = .FALSE.
     435         8428 :          IF (method_id == do_method_dftb) THEN
     436              :             CALL section_vals_val_get(qs_env%input, "DFT%QS%DFTB%SELF_CONSISTENT", &
     437          292 :                                       l_val=do_dftb_scc)
     438              :          END IF
     439         8428 :          do_linear_response = .FALSE.
     440         8428 :          IF (PRESENT(globenv)) do_linear_response = globenv%run_type_id == linear_response_run
     441            4 :          do_debug_fdiff = .FALSE.
     442         8424 :          IF (PRESENT(globenv)) do_debug_fdiff = globenv%run_type_id == debug_run
     443         8428 :          IF (do_debug_fdiff .AND. PRESENT(root_section)) THEN
     444              :             CALL section_vals_val_get(root_section, "DEBUG%DEBUG_FORCES", &
     445          778 :                                       l_val=do_debug_forces)
     446              :             CALL section_vals_val_get(root_section, "DEBUG%DEBUG_STRESS_TENSOR", &
     447          778 :                                       l_val=do_debug_stress_tensor)
     448          958 :             do_debug_fdiff = do_debug_forces .OR. do_debug_stress_tensor
     449              :          END IF
     450         8428 :          do_mp2 = .FALSE.
     451         8428 :          do_ri_mp2 = .FALSE.
     452         8428 :          do_ri_sos_mp2 = .FALSE.
     453         8428 :          do_ri_rpa = .FALSE.
     454         8428 :          do_wfc_low_scaling = .FALSE.
     455         8428 :          do_wfc_low_scaling_kpoints = .FALSE.
     456         8428 :          mp2_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION")
     457         8428 :          CALL section_vals_get(mp2_section, explicit=mp2_present)
     458         8428 :          IF (mp2_present) THEN
     459              :             CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%MP2%_SECTION_PARAMETERS_", &
     460          476 :                                       l_val=do_mp2)
     461              :             CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_MP2%_SECTION_PARAMETERS_", &
     462          476 :                                       l_val=do_ri_mp2)
     463              :             CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_SOS_MP2%_SECTION_PARAMETERS_", &
     464          476 :                                       l_val=do_ri_sos_mp2)
     465              :             CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%_SECTION_PARAMETERS_", &
     466          476 :                                       l_val=do_ri_rpa)
     467              :             CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%LOW_SCALING%_SECTION_PARAMETERS_", &
     468          476 :                                       l_val=do_wfc_low_scaling)
     469              :             CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%LOW_SCALING%DO_KPOINTS", &
     470          476 :                                       l_val=do_wfc_low_scaling_kpoints)
     471          476 :             IF (.NOT. do_bse) &
     472              :                CALL section_vals_val_get(qs_env%input, &
     473              :                                          "DFT%XC%WF_CORRELATION%RI_RPA%GW%BSE%_SECTION_PARAMETERS_", &
     474          472 :                                          l_val=do_bse)
     475              :          END IF
     476              :          CALL restrict_unsupported_atomic_kpoint_symmetry(kpoints, method_id, do_hfx, do_exx, do_gw, &
     477              :                                                           do_tddfpt_unsupported_kpoints, &
     478              :                                                           do_active_space, do_linear_response, &
     479              :                                                           do_debug_fdiff, &
     480              :                                                           do_mp2 .OR. do_ri_mp2 .OR. do_ri_sos_mp2, &
     481              :                                                           do_ri_rpa .AND. .NOT. do_gw, do_bse, &
     482              :                                                           do_wfc_low_scaling, do_wfc_low_scaling_kpoints, &
     483        24922 :                                                           do_xtb_tblite, do_admm, .FALSE.)
     484         8428 :          CALL kpoint_initialize(kpoints, particle_set, my_cell)
     485              :       END IF
     486              : 
     487              :       CALL qs_init_subsys(qs_env, para_env, subsys, my_cell, my_cell_ref, use_ref_cell, &
     488         8430 :                           subsys_section, silent=silent)
     489              : 
     490         8430 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     491         8430 :       IF (owned_kpoints) THEN
     492         8428 :          do_dftb_scc_high_l = .FALSE.
     493         8428 :          IF (method_id == do_method_dftb .AND. do_dftb_scc) THEN
     494          218 :             do_dftb_scc_high_l = dftb_kind_set_has_high_l(qs_kind_set)
     495              :          END IF
     496              :          CALL restrict_unsupported_atomic_kpoint_symmetry(kpoints, method_id, do_hfx, do_exx, do_gw, &
     497              :                                                           do_tddfpt_unsupported_kpoints, &
     498              :                                                           do_active_space, do_linear_response, &
     499              :                                                           do_debug_fdiff, &
     500              :                                                           do_mp2 .OR. do_ri_mp2 .OR. do_ri_sos_mp2, &
     501              :                                                           do_ri_rpa .AND. .NOT. do_gw, do_bse, &
     502              :                                                           do_wfc_low_scaling, do_wfc_low_scaling_kpoints, &
     503              :                                                           do_xtb_tblite, do_admm, do_dftb_scc_high_l, &
     504        24922 :                                                           restricted=dftb_kpoint_sym_restricted)
     505         8428 :          final_kpoint_reinit = dftb_kpoint_sym_restricted .OR. kpoint_verbose
     506              :          IF (final_kpoint_reinit) THEN
     507          218 :             CALL kpoint_reset_initialization(kpoints)
     508          218 :             CALL set_kpoint_info(kpoints, verbose=kpoint_verbose)
     509          218 :             CALL kpoint_initialize(kpoints, particle_set, my_cell)
     510              :          END IF
     511         8428 :          dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     512         8428 :          CALL write_kpoint_info(kpoints, dft_section=dft_section)
     513              :       END IF
     514         8430 :       IF (method_id == do_method_lrigpw .OR. dft_control%qs_control%lri_optbas) THEN
     515           48 :          CALL get_qs_env(qs_env=qs_env, lri_env=lri_env)
     516           48 :          CALL lri_env_basis("LRI", qs_env, lri_env, qs_kind_set)
     517         8382 :       ELSE IF (method_id == do_method_rigpw) THEN
     518              :          CALL cp_warn(__LOCATION__, "Experimental code: "// &
     519            2 :                       "RIGPW should only be used for testing.")
     520            2 :          CALL get_qs_env(qs_env=qs_env, lri_env=lri_env)
     521            2 :          CALL lri_env_basis("RI", qs_env, lri_env, qs_kind_set)
     522              :       END IF
     523              : 
     524         8430 :       IF (my_qmmm .AND. PRESENT(qmmm_env_qm) .AND. .NOT. dft_control%qs_control%commensurate_mgrids) THEN
     525          132 :          IF (qmmm_env_qm%qmmm_coupl_type == do_qmmm_gauss .OR. qmmm_env_qm%qmmm_coupl_type == do_qmmm_swave) THEN
     526              :             CALL cp_abort(__LOCATION__, "QM/MM with coupling GAUSS or S-WAVE requires "// &
     527            0 :                           "keyword FORCE_EVAL/DFT/MGRID/COMMENSURATE to be enabled.")
     528              :          END IF
     529              :       END IF
     530              : 
     531              :       ! more kpoint stuff
     532         8430 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints, blacs_env=blacs_env)
     533         8430 :       IF (do_kpoints) THEN
     534          456 :          IF (dft_control%qs_control%do_ls_scf) &
     535            0 :             CPABORT("DFT%KPOINTS are not implemented with QS/LS_SCF; use a real-space supercell instead.")
     536          456 :          CALL kpoint_env_initialize(kpoints, para_env, blacs_env, with_aux_fit=dft_control%do_admm)
     537          456 :          CALL kpoint_initialize_mos(kpoints, qs_env%mos)
     538          456 :          CALL get_qs_env(qs_env=qs_env, wf_history=wf_history)
     539          456 :          CALL wfi_create_for_kp(wf_history)
     540              :       END IF
     541              :       ! basis set symmetry rotations
     542         8430 :       IF (do_kpoints) THEN
     543          456 :          CALL qs_basis_rotation(qs_env, kpoints)
     544              :       END IF
     545              : 
     546              :       do_hfx = .FALSE.
     547         8430 :       hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF")
     548         8430 :       CALL section_vals_get(hfx_section, explicit=do_hfx)
     549         8430 :       CALL get_qs_env(qs_env, dft_control=dft_control, scf_control=scf_control, nelectron_total=nelectron_total)
     550         8430 :       IF (do_hfx) THEN
     551              :          ! Retrieve particle_set and atomic_kind_set (needed for both kinds of initialization)
     552         5288 :          nkp_grid = 1
     553         1322 :          IF (do_kpoints) CALL get_kpoint_info(kpoints, nkp_grid=nkp_grid)
     554         1322 :          IF (dft_control%do_admm) THEN
     555          512 :             basis_type = 'AUX_FIT'
     556              :          ELSE
     557          810 :             basis_type = 'ORB'
     558              :          END IF
     559              :          CALL hfx_create(qs_env%x_data, para_env, hfx_section, atomic_kind_set, &
     560              :                          qs_kind_set, particle_set, dft_control, my_cell, orb_basis=basis_type, &
     561         1322 :                          nelectron_total=nelectron_total, nkp_grid=nkp_grid)
     562              :       END IF
     563              : 
     564         8430 :       mp2_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION")
     565         8430 :       CALL section_vals_get(mp2_section, explicit=mp2_present)
     566         8430 :       IF (mp2_present) THEN
     567          476 :          CPASSERT(ASSOCIATED(qs_env%mp2_env))
     568          476 :          CALL read_mp2_section(qs_env%input, qs_env%mp2_env)
     569              :          ! create the EXX section if necessary
     570              :          do_exx = .FALSE.
     571          476 :          rpa_hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%HF")
     572          476 :          CALL section_vals_get(rpa_hfx_section, explicit=do_exx)
     573          476 :          IF (do_exx) THEN
     574              : 
     575              :             ! do_exx in call of hfx_create decides whether to go without ADMM (do_exx=.TRUE.) or with
     576              :             ! ADMM (do_exx=.FALSE.)
     577          142 :             CALL section_vals_val_get(mp2_section, "RI_RPA%ADMM", l_val=do_admm_rpa)
     578              : 
     579              :             ! Reuse the HFX integrals from the qs_env if applicable
     580          142 :             qs_env%mp2_env%ri_rpa%reuse_hfx = .TRUE.
     581          142 :             IF (.NOT. do_hfx) qs_env%mp2_env%ri_rpa%reuse_hfx = .FALSE.
     582          142 :             CALL compare_hfx_sections(hfx_section, rpa_hfx_section, is_identical, same_except_frac)
     583          142 :             IF (.NOT. (is_identical .OR. same_except_frac)) qs_env%mp2_env%ri_rpa%reuse_hfx = .FALSE.
     584          142 :             IF (dft_control%do_admm .AND. .NOT. do_admm_rpa) qs_env%mp2_env%ri_rpa%reuse_hfx = .FALSE.
     585              : 
     586          142 :             IF (.NOT. qs_env%mp2_env%ri_rpa%reuse_hfx) THEN
     587          124 :                IF (do_admm_rpa) THEN
     588           10 :                   basis_type = 'AUX_FIT'
     589              :                ELSE
     590          114 :                   basis_type = 'ORB'
     591              :                END IF
     592              :                CALL hfx_create(qs_env%mp2_env%ri_rpa%x_data, para_env, rpa_hfx_section, atomic_kind_set, &
     593              :                                qs_kind_set, particle_set, dft_control, my_cell, orb_basis=basis_type, &
     594          124 :                                nelectron_total=nelectron_total)
     595              :             ELSE
     596           18 :                qs_env%mp2_env%ri_rpa%x_data => qs_env%x_data
     597              :             END IF
     598              :          END IF
     599              :       END IF
     600              : 
     601         8430 :       IF (dft_control%qs_control%do_kg) THEN
     602           94 :          CALL cite_reference(Iannuzzi2006)
     603           94 :          CALL kg_env_create(qs_env, qs_env%kg_env, qs_kind_set, qs_env%input)
     604              :       END IF
     605              : 
     606         8430 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     607              :       CALL section_vals_val_get(dft_section, "EXCITED_STATES%_SECTION_PARAMETERS_", &
     608         8430 :                                 l_val=qs_env%excited_state)
     609         8430 :       NULLIFY (exstate_env)
     610         8430 :       CALL exstate_create(exstate_env, qs_env%excited_state, dft_section)
     611         8430 :       CALL set_qs_env(qs_env, exstate_env=exstate_env)
     612              : 
     613              :       et_coupling_section => section_vals_get_subs_vals(qs_env%input, &
     614         8430 :                                                         "PROPERTIES%ET_COUPLING")
     615         8430 :       CALL section_vals_get(et_coupling_section, explicit=do_et)
     616         8430 :       IF (do_et) CALL et_coupling_create(qs_env%et_coupling)
     617              : 
     618         8430 :       transport_section => section_vals_get_subs_vals(qs_env%input, "DFT%TRANSPORT")
     619         8430 :       CALL section_vals_get(transport_section, explicit=qs_env%do_transport)
     620         8430 :       IF (qs_env%do_transport) THEN
     621            0 :          CALL transport_env_create(qs_env)
     622              :       END IF
     623              : 
     624         8430 :       CALL get_qs_env(qs_env, harris_env=harris_env)
     625         8430 :       IF (qs_env%harris_method) THEN
     626              :          ! initialize the Harris input density and potential integrals
     627            8 :          CALL get_qs_env(qs_env, local_particles=local_particles)
     628              :          CALL harris_rhoin_init(harris_env%rhoin, "RHOIN", qs_kind_set, atomic_kind_set, &
     629            8 :                                 local_particles, dft_control%nspins)
     630              :          ! Print information of the HARRIS section
     631            8 :          CALL harris_write_input(harris_env)
     632              :       END IF
     633              : 
     634         8430 :       NULLIFY (ec_env)
     635         8430 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     636              :       CALL section_vals_val_get(dft_section, "ENERGY_CORRECTION%_SECTION_PARAMETERS_", &
     637         8430 :                                 l_val=qs_env%energy_correction)
     638         8430 :       ec_section => section_vals_get_subs_vals(qs_env%input, "DFT%ENERGY_CORRECTION")
     639         8430 :       CALL ec_env_create(qs_env, ec_env, dft_section, ec_section)
     640         8430 :       CALL set_qs_env(qs_env, ec_env=ec_env)
     641              : 
     642         8430 :       IF (qs_env%energy_correction) THEN
     643              :          ! Energy correction with Hartree-Fock exchange
     644          298 :          ec_hfx_section => section_vals_get_subs_vals(ec_section, "XC%HF")
     645          298 :          CALL section_vals_get(ec_hfx_section, explicit=do_ec_hfx)
     646              : 
     647          298 :          IF (ec_env%do_ec_hfx) THEN
     648              : 
     649              :             ! kpoints and HFX not yet compatible
     650           28 :             IF (ec_env%do_kpoints) THEN
     651              :                CALL cp_abort(__LOCATION__, &
     652              :                              "Energy correction methods with hybrid functionals "// &
     653            0 :                              "and kpoints is not yet available.")
     654              :             END IF
     655              : 
     656              :             ! Hybrid functionals require same basis
     657           28 :             IF (ec_env%basis_inconsistent) THEN
     658              :                CALL cp_abort(__LOCATION__, &
     659              :                              "Energy correction methods with hybrid functionals: "// &
     660              :                              "correction and ground state need to use the same basis. "// &
     661            0 :                              "Checked by comparing basis set names only.")
     662              :             END IF
     663              : 
     664              :             ! Similar to RPA_HFX we can check if HFX integrals from the qs_env can be reused
     665           28 :             IF (ec_env%do_ec_admm .AND. .NOT. dft_control%do_admm) THEN
     666            0 :                CALL cp_abort(__LOCATION__, "Need an ADMM input section for ADMM EC to work")
     667              :             END IF
     668              : 
     669           28 :             ec_env%reuse_hfx = .TRUE.
     670           28 :             IF (.NOT. do_hfx) ec_env%reuse_hfx = .FALSE.
     671           28 :             CALL compare_hfx_sections(hfx_section, ec_hfx_section, is_identical, same_except_frac)
     672           28 :             IF (.NOT. (is_identical .OR. same_except_frac)) ec_env%reuse_hfx = .FALSE.
     673           28 :             IF (dft_control%do_admm .AND. .NOT. ec_env%do_ec_admm) ec_env%reuse_hfx = .FALSE.
     674              : 
     675           28 :             IF (.NOT. ec_env%reuse_hfx) THEN
     676           12 :                IF (ec_env%do_ec_admm) THEN
     677            2 :                   basis_type = 'AUX_FIT'
     678              :                ELSE
     679           10 :                   basis_type = 'ORB'
     680              :                END IF
     681              :                CALL hfx_create(ec_env%x_data, para_env, ec_hfx_section, atomic_kind_set, &
     682              :                                qs_kind_set, particle_set, dft_control, my_cell, orb_basis=basis_type, &
     683           12 :                                nelectron_total=nelectron_total)
     684              :             ELSE
     685           16 :                ec_env%x_data => qs_env%x_data
     686              :             END IF
     687              :          END IF
     688              : 
     689              :          ! Print information of the EC section
     690          298 :          CALL ec_write_input(ec_env)
     691              : 
     692              :       END IF
     693              : 
     694         8430 :       IF (dft_control%qs_control%do_almo_scf) THEN
     695           66 :          CALL almo_scf_env_create(qs_env)
     696              :       END IF
     697              : 
     698              :       ! see if we have atomic relativistic corrections
     699         8430 :       CALL get_qs_env(qs_env, rel_control=rel_control)
     700         8430 :       IF (rel_control%rel_method /= rel_none) THEN
     701           18 :          IF (rel_control%rel_transformation == rel_trans_atom) THEN
     702           18 :             nkind = SIZE(atomic_kind_set)
     703           46 :             DO ikind = 1, nkind
     704           28 :                NULLIFY (rtmat)
     705           28 :                CALL calculate_atomic_relkin(atomic_kind_set(ikind), qs_kind_set(ikind), rel_control, rtmat)
     706           46 :                IF (ASSOCIATED(rtmat)) CALL set_qs_kind(qs_kind_set(ikind), reltmat=rtmat)
     707              :             END DO
     708              :          END IF
     709              :       END IF
     710              : 
     711         8430 :    END SUBROUTINE qs_init
     712              : 
     713              : ! **************************************************************************************************
     714              : !> \brief Restrict atomic k-point symmetry for methods not supporting it yet
     715              : !> \param kpoints ...
     716              : !> \param method_id ...
     717              : !> \param do_hfx ...
     718              : !> \param do_exx ...
     719              : !> \param do_gw ...
     720              : !> \param do_tddfpt ...
     721              : !> \param do_active_space ...
     722              : !> \param do_linear_response ...
     723              : !> \param do_debug_fdiff ...
     724              : !> \param do_mp2 ...
     725              : !> \param do_rpa ...
     726              : !> \param do_bse ...
     727              : !> \param do_wfc_low_scaling ...
     728              : !> \param do_wfc_low_scaling_kpoints ...
     729              : !> \param do_xtb_tblite ...
     730              : !> \param do_admm ...
     731              : !> \param do_dftb_scc_high_l ...
     732              : !> \param restricted ...
     733              : ! **************************************************************************************************
     734        16856 :    SUBROUTINE restrict_unsupported_atomic_kpoint_symmetry(kpoints, method_id, do_hfx, do_exx, do_gw, &
     735              :                                                           do_tddfpt, do_active_space, do_linear_response, &
     736              :                                                           do_debug_fdiff, &
     737              :                                                           do_mp2, do_rpa, do_bse, do_wfc_low_scaling, &
     738              :                                                           do_wfc_low_scaling_kpoints, do_xtb_tblite, &
     739              :                                                           do_admm, do_dftb_scc_high_l, restricted)
     740              :       TYPE(kpoint_type), POINTER                         :: kpoints
     741              :       INTEGER, INTENT(IN)                                :: method_id
     742              :       LOGICAL, INTENT(IN) :: do_hfx, do_exx, do_gw, do_tddfpt, do_active_space, &
     743              :          do_linear_response, do_debug_fdiff, do_mp2, do_rpa, do_bse, do_wfc_low_scaling, &
     744              :          do_wfc_low_scaling_kpoints, do_xtb_tblite, do_admm, do_dftb_scc_high_l
     745              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: restricted
     746              : 
     747              :       CHARACTER(LEN=default_string_length)               :: kp_scheme, reason
     748              :       LOGICAL                                            :: full_grid, inversion_symmetry_only, &
     749              :                                                             kpoint_symmetry
     750              : 
     751        16856 :       IF (PRESENT(restricted)) restricted = .FALSE.
     752              : 
     753              :       reason = unsupported_kpoint_method_reason(method_id, do_gw, do_tddfpt, do_linear_response, &
     754        16856 :                                                 do_mp2, do_bse, do_xtb_tblite)
     755        16856 :       IF (LEN_TRIM(reason) > 0) THEN
     756         3648 :          CALL get_kpoint_info(kpoints, kp_scheme=kp_scheme)
     757         3648 :          IF (LEN_TRIM(kp_scheme) > 0 .AND. TRIM(kp_scheme) /= "NONE") THEN
     758            0 :             IF (TRIM(reason) == "GW") THEN
     759              :                CALL cp_abort(__LOCATION__, &
     760              :                              "DFT%KPOINTS are not supported with GW; use "// &
     761              :                              "WF_CORRELATION%LOW_SCALING%KPOINTS and RI_RPA%GW%KPOINTS_SELF_ENERGY "// &
     762            0 :                              "for GW k-point sampling.")
     763              :             ELSE
     764              :                CALL cp_abort(__LOCATION__, &
     765              :                              "DFT%KPOINTS are not supported with "//TRIM(reason)// &
     766            0 :                              "; remove DFT%KPOINTS for these calculations.")
     767              :             END IF
     768              :          END IF
     769              :       END IF
     770        16856 :       IF (do_active_space) THEN
     771          164 :          CALL get_kpoint_info(kpoints, kp_scheme=kp_scheme)
     772          164 :          IF (LEN_TRIM(kp_scheme) > 0 .AND. TRIM(kp_scheme) /= "NONE" .AND. &
     773              :              TRIM(kp_scheme) /= "GAMMA") THEN
     774              :             CALL cp_abort(__LOCATION__, &
     775              :                           "Only Gamma-point DFT%KPOINTS are supported with ACTIVE_SPACE; "// &
     776            0 :                           "use SCHEME GAMMA, SCHEME NONE, or remove DFT%KPOINTS.")
     777              :          END IF
     778              :       END IF
     779              : 
     780              :       CALL get_kpoint_info(kpoints, symmetry=kpoint_symmetry, full_grid=full_grid, &
     781        16856 :                            inversion_symmetry_only=inversion_symmetry_only)
     782        17228 :       IF (.NOT. (kpoint_symmetry .AND. .NOT. full_grid .AND. .NOT. inversion_symmetry_only)) RETURN
     783              : 
     784              :       reason = unsupported_atomic_kpoint_symmetry_reason(method_id, do_hfx, do_exx, do_gw, &
     785              :                                                          do_tddfpt, do_active_space, do_linear_response, &
     786              :                                                          do_debug_fdiff, &
     787              :                                                          do_mp2, do_rpa, do_bse, do_wfc_low_scaling, &
     788              :                                                          do_wfc_low_scaling_kpoints, do_xtb_tblite, &
     789          382 :                                                          do_admm, do_dftb_scc_high_l)
     790          382 :       IF (LEN_TRIM(reason) == 0) RETURN
     791              : 
     792              :       CALL cp_warn(__LOCATION__, &
     793              :                    "Atomic k-point symmetry is currently not implemented for "//TRIM(reason)// &
     794           10 :                    "; restricting to inversion/time-reversal symmetry.")
     795           10 :       CALL set_kpoint_info(kpoints, inversion_symmetry_only=.TRUE.)
     796           10 :       IF (PRESENT(restricted)) restricted = .TRUE.
     797              : 
     798              :    END SUBROUTINE restrict_unsupported_atomic_kpoint_symmetry
     799              : 
     800              : ! **************************************************************************************************
     801              : !> \brief Return the reason why k-points are not enabled for a method
     802              : !> \param method_id ...
     803              : !> \param do_gw ...
     804              : !> \param do_tddfpt ...
     805              : !> \param do_linear_response ...
     806              : !> \param do_mp2 ...
     807              : !> \param do_bse ...
     808              : !> \param do_xtb_tblite ...
     809              : !> \return reason
     810              : ! **************************************************************************************************
     811        16856 :    FUNCTION unsupported_kpoint_method_reason(method_id, do_gw, do_tddfpt, do_linear_response, &
     812              :                                              do_mp2, do_bse, do_xtb_tblite) RESULT(reason)
     813              :       INTEGER, INTENT(IN)                                :: method_id
     814              :       LOGICAL, INTENT(IN)                                :: do_gw, do_tddfpt, do_linear_response, &
     815              :                                                             do_mp2, do_bse, do_xtb_tblite
     816              :       CHARACTER(LEN=default_string_length)               :: reason
     817              : 
     818              :       reason = ""
     819              :       MARK_USED(do_gw)
     820              :       MARK_USED(do_mp2)
     821              :       MARK_USED(do_xtb_tblite)
     822              : 
     823        16856 :       IF (do_bse) THEN
     824           68 :          reason = "BSE"
     825           68 :          RETURN
     826              :       END IF
     827        16788 :       IF (do_tddfpt) THEN
     828         1200 :          reason = "TDDFPT/TDDFT"
     829         1200 :          RETURN
     830              :       END IF
     831        15588 :       IF (do_linear_response) THEN
     832          376 :          reason = "LINEAR_RESPONSE/DFPT"
     833          376 :          RETURN
     834              :       END IF
     835        15216 :       SELECT CASE (method_id)
     836              :       CASE (do_method_rigpw)
     837            4 :          reason = "RIGPW"
     838              :       CASE (do_method_ofgpw)
     839            0 :          reason = "OFGPW"
     840              :       CASE (do_method_mndo, do_method_mndod, do_method_am1, do_method_pm3, &
     841              :             do_method_pm6, do_method_pm6fm, do_method_pdg, do_method_rm1, do_method_pnnl)
     842         2000 :          reason = "semiempirical methods"
     843              :       CASE DEFAULT
     844        15212 :          reason = ""
     845              :       END SELECT
     846              : 
     847              :    END FUNCTION unsupported_kpoint_method_reason
     848              : 
     849              : ! **************************************************************************************************
     850              : !> \brief Return the reason why atomic k-point symmetry is not enabled
     851              : !> \param method_id ...
     852              : !> \param do_hfx ...
     853              : !> \param do_exx ...
     854              : !> \param do_gw ...
     855              : !> \param do_tddfpt ...
     856              : !> \param do_active_space ...
     857              : !> \param do_linear_response ...
     858              : !> \param do_debug_fdiff ...
     859              : !> \param do_mp2 ...
     860              : !> \param do_rpa ...
     861              : !> \param do_bse ...
     862              : !> \param do_wfc_low_scaling ...
     863              : !> \param do_wfc_low_scaling_kpoints ...
     864              : !> \param do_xtb_tblite ...
     865              : !> \param do_admm ...
     866              : !> \param do_dftb_scc_high_l ...
     867              : !> \return reason
     868              : ! **************************************************************************************************
     869          382 :    FUNCTION unsupported_atomic_kpoint_symmetry_reason(method_id, do_hfx, do_exx, do_gw, do_tddfpt, &
     870              :                                                       do_active_space, do_linear_response, do_debug_fdiff, &
     871              :                                                       do_mp2, do_rpa, do_bse, do_wfc_low_scaling, &
     872              :                                                       do_wfc_low_scaling_kpoints, do_xtb_tblite, &
     873              :                                                       do_admm, do_dftb_scc_high_l) RESULT(reason)
     874              :       INTEGER, INTENT(IN)                                :: method_id
     875              :       LOGICAL, INTENT(IN) :: do_hfx, do_exx, do_gw, do_tddfpt, do_active_space, &
     876              :          do_linear_response, do_debug_fdiff, do_mp2, do_rpa, do_bse, do_wfc_low_scaling, &
     877              :          do_wfc_low_scaling_kpoints, do_xtb_tblite, do_admm, do_dftb_scc_high_l
     878              :       CHARACTER(LEN=default_string_length)               :: reason
     879              : 
     880          382 :       reason = ""
     881              :       MARK_USED(do_debug_fdiff)
     882              :       MARK_USED(do_xtb_tblite)
     883              : 
     884          454 :       SELECT CASE (method_id)
     885              :       CASE (do_method_dftb)
     886           72 :          IF (do_dftb_scc_high_l) reason = "SCC-DFTB with d orbitals"
     887              :       CASE (do_method_lrigpw)
     888            2 :          reason = "LRIGPW"
     889              :       CASE (do_method_rigpw)
     890            0 :          reason = "RIGPW"
     891              :       CASE (do_method_mndo, do_method_mndod, do_method_am1, do_method_pm3, &
     892              :             do_method_pm6, do_method_pm6fm, do_method_pdg, do_method_rm1, do_method_pnnl)
     893            0 :          reason = "semiempirical methods"
     894              :       CASE DEFAULT
     895          382 :          reason = ""
     896              :       END SELECT
     897              : 
     898          382 :       IF (LEN_TRIM(reason) > 0) RETURN
     899          376 :       IF ((do_hfx .OR. do_exx) .AND. do_admm) THEN
     900            0 :          reason = "HFX/HF with ADMM"
     901          376 :       ELSE IF (do_bse) THEN
     902            0 :          reason = "BSE"
     903          376 :       ELSE IF (do_gw) THEN
     904            2 :          reason = "GW"
     905          374 :       ELSE IF (do_tddfpt) THEN
     906            0 :          reason = "TDDFPT/TDDFT"
     907          374 :       ELSE IF (do_active_space) THEN
     908            0 :          reason = "ACTIVE_SPACE"
     909          374 :       ELSE IF (do_linear_response) THEN
     910            0 :          reason = "LINEAR_RESPONSE/DFPT"
     911          374 :       ELSE IF (do_mp2) THEN
     912            0 :          reason = "MP2"
     913          374 :       ELSE IF (do_rpa .AND. do_wfc_low_scaling_kpoints) THEN
     914            2 :          reason = "LOW_SCALING RPA"
     915          372 :       ELSE IF (do_wfc_low_scaling) THEN
     916            0 :          reason = "LOW_SCALING WF_CORRELATION"
     917          372 :       ELSE IF (do_rpa) THEN
     918            0 :          reason = "RPA"
     919              :       END IF
     920              : 
     921              :    END FUNCTION unsupported_atomic_kpoint_symmetry_reason
     922              : 
     923              : ! **************************************************************************************************
     924              : !> \brief Return whether the DFTB kind set contains d orbitals
     925              : !> \param qs_kind_set ...
     926              : !> \return has_high_l
     927              : ! **************************************************************************************************
     928          218 :    FUNCTION dftb_kind_set_has_high_l(qs_kind_set) RESULT(has_high_l)
     929              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     930              :       LOGICAL                                            :: has_high_l
     931              : 
     932              :       INTEGER                                            :: ikind, lmax
     933              :       LOGICAL                                            :: any_defined, defined
     934              :       TYPE(qs_dftb_atom_type), POINTER                   :: dftb_parameter
     935              : 
     936          218 :       has_high_l = .TRUE.
     937          218 :       IF (.NOT. ASSOCIATED(qs_kind_set)) RETURN
     938              : 
     939          218 :       any_defined = .FALSE.
     940          686 :       DO ikind = 1, SIZE(qs_kind_set)
     941          472 :          NULLIFY (dftb_parameter)
     942          472 :          CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_parameter)
     943          472 :          IF (.NOT. ASSOCIATED(dftb_parameter)) CYCLE
     944              :          defined = .FALSE.
     945              :          lmax = -1
     946          472 :          CALL get_dftb_atom_param(dftb_parameter, defined=defined, lmax=lmax)
     947          472 :          IF (.NOT. defined) CYCLE
     948          472 :          any_defined = .TRUE.
     949         1158 :          IF (lmax > 1) RETURN
     950              :       END DO
     951              : 
     952          214 :       IF (any_defined) has_high_l = .FALSE.
     953              : 
     954              :    END FUNCTION dftb_kind_set_has_high_l
     955              : 
     956              : ! **************************************************************************************************
     957              : !> \brief Initialize the qs environment (subsys)
     958              : !> \param qs_env ...
     959              : !> \param para_env ...
     960              : !> \param subsys ...
     961              : !> \param cell ...
     962              : !> \param cell_ref ...
     963              : !> \param use_ref_cell ...
     964              : !> \param subsys_section ...
     965              : !> \param silent ...
     966              : !> \author Creation (22.05.2000,MK)
     967              : ! **************************************************************************************************
     968         8430 :    SUBROUTINE qs_init_subsys(qs_env, para_env, subsys, cell, cell_ref, use_ref_cell, subsys_section, &
     969              :                              silent)
     970              : 
     971              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     972              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     973              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     974              :       TYPE(cell_type), POINTER                           :: cell, cell_ref
     975              :       LOGICAL, INTENT(in)                                :: use_ref_cell
     976              :       TYPE(section_vals_type), POINTER                   :: subsys_section
     977              :       LOGICAL, INTENT(in), OPTIONAL                      :: silent
     978              : 
     979              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_init_subsys'
     980              : 
     981              :       CHARACTER(len=2)                                   :: element_symbol
     982              :       INTEGER :: gfn_type, handle, ikind, ispin, iw, lmax_sphere, maxl, maxlgto, maxlgto_lri, &
     983              :          maxlgto_nuc, maxlppl, maxlppnl, method_id, multiplicity, my_ival, n_ao, n_mo_add, natom, &
     984              :          nelectron, ngauss, nkind, nlumo_dos, nlumo_molden, nlumo_required, output_unit, &
     985              :          sort_basis, tnadd_method
     986              :       INTEGER, DIMENSION(2)                              :: n_mo, nelectron_spin
     987              :       INTEGER, DIMENSION(5)                              :: occ
     988         8430 :       INTEGER, DIMENSION(:), POINTER                     :: mo_index_range
     989              :       LOGICAL :: all_potential_present, be_silent, cneo_potential_present, do_kpoints, do_ri_hfx, &
     990              :          do_ri_mp2, do_ri_rpa, do_ri_sos_mp2, do_rpa_ri_exx, do_wfc_im_time, e1terms, &
     991              :          has_unit_metric, lribas, mp2_present, orb_gradient, paw_atom
     992              :       REAL(KIND=dp)                                      :: alpha, ccore, ewald_rcut, fxx, maxocc, &
     993              :                                                             rc, rcut, total_zeff_corr, &
     994              :                                                             verlet_skin, zeff_correction
     995         8430 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     996              :       TYPE(cp_logger_type), POINTER                      :: logger
     997              :       TYPE(dft_control_type), POINTER                    :: dft_control
     998              :       TYPE(dftb_control_type), POINTER                   :: dftb_control
     999              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    1000              :       TYPE(ewald_environment_type), POINTER              :: ewald_env
    1001              :       TYPE(ewald_pw_type), POINTER                       :: ewald_pw
    1002              :       TYPE(fist_nonbond_env_type), POINTER               :: se_nonbond_env
    1003              :       TYPE(gapw_control_type), POINTER                   :: gapw_control
    1004              :       TYPE(gto_basis_set_type), POINTER                  :: aux_fit_basis, lri_aux_basis, &
    1005              :                                                             rhoin_basis, ri_aux_basis_set, &
    1006              :                                                             ri_hfx_basis, ri_xas_basis, &
    1007              :                                                             tmp_basis_set
    1008              :       TYPE(harris_type), POINTER                         :: harris_env
    1009              :       TYPE(local_rho_type), POINTER                      :: local_rho_set
    1010              :       TYPE(lri_environment_type), POINTER                :: lri_env
    1011         8430 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos, mos_last_converged
    1012         8430 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    1013         8430 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    1014              :       TYPE(mp2_type), POINTER                            :: mp2_env
    1015              :       TYPE(nddo_mpole_type), POINTER                     :: se_nddo_mpole
    1016         8430 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1017              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1018              :       TYPE(qs_control_type), POINTER                     :: qs_control
    1019              :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), &
    1020         8430 :          POINTER                                         :: dftb_potential
    1021              :       TYPE(qs_dispersion_type), POINTER                  :: dispersion_env
    1022              :       TYPE(qs_energy_type), POINTER                      :: energy
    1023         8430 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
    1024              :       TYPE(qs_gcp_type), POINTER                         :: gcp_env
    1025         8430 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1026              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
    1027              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1028              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
    1029              :       TYPE(rho0_mpole_type), POINTER                     :: rho0_mpole
    1030         8430 :       TYPE(rho_atom_type), DIMENSION(:), POINTER         :: rho_atom_set
    1031              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1032              :       TYPE(se_taper_type), POINTER                       :: se_taper
    1033              :       TYPE(section_vals_type), POINTER :: dft_section, et_coupling_section, et_ddapc_section, &
    1034              :          ewald_section, harris_section, lri_section, mp2_section, nl_section, poisson_section, &
    1035              :          pp_section, print_section, qs_section, rixs_section, se_section, tddfpt_section, &
    1036              :          xc_section
    1037              :       TYPE(semi_empirical_control_type), POINTER         :: se_control
    1038              :       TYPE(semi_empirical_si_type), POINTER              :: se_store_int_env
    1039              :       TYPE(xtb_control_type), POINTER                    :: xtb_control
    1040              : 
    1041         8430 :       CALL timeset(routineN, handle)
    1042         8430 :       NULLIFY (logger)
    1043         8430 :       logger => cp_get_default_logger()
    1044         8430 :       output_unit = cp_logger_get_default_io_unit(logger)
    1045              : 
    1046         8430 :       be_silent = .FALSE.
    1047         8430 :       IF (PRESENT(silent)) be_silent = silent
    1048              : 
    1049         8430 :       CALL cite_reference(cp2kqs2020)
    1050              : 
    1051              :       ! Initialise the Quickstep environment
    1052         8430 :       NULLIFY (mos, se_taper)
    1053         8430 :       NULLIFY (dft_control)
    1054         8430 :       NULLIFY (energy)
    1055         8430 :       NULLIFY (force)
    1056         8430 :       NULLIFY (local_molecules)
    1057         8430 :       NULLIFY (local_particles)
    1058         8430 :       NULLIFY (scf_control)
    1059         8430 :       NULLIFY (dft_section)
    1060         8430 :       NULLIFY (et_coupling_section)
    1061         8430 :       NULLIFY (ks_env)
    1062         8430 :       NULLIFY (mos_last_converged)
    1063         8430 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
    1064         8430 :       qs_section => section_vals_get_subs_vals(dft_section, "QS")
    1065         8430 :       et_coupling_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%ET_COUPLING")
    1066              :       ! reimplemented TDDFPT
    1067         8430 :       tddfpt_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%TDDFPT")
    1068         8430 :       rixs_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%RIXS")
    1069              : 
    1070              :       CALL qs_subsys_get(subsys, particle_set=particle_set, &
    1071              :                          qs_kind_set=qs_kind_set, &
    1072              :                          atomic_kind_set=atomic_kind_set, &
    1073              :                          molecule_set=molecule_set, &
    1074         8430 :                          molecule_kind_set=molecule_kind_set)
    1075              : 
    1076              :       ! Read the input section with the DFT control parameters
    1077         8430 :       CALL read_dft_control(dft_control, dft_section, cell)
    1078              : 
    1079              :       ! Set periodicity flag
    1080        33720 :       dft_control%qs_control%periodicity = SUM(cell%perd)
    1081              : 
    1082              :       ! Read the input section with the Quickstep control parameters
    1083         8430 :       CALL read_qs_section(dft_control%qs_control, qs_section, cell)
    1084              : 
    1085              :       ! Print the Quickstep program banner (copyright and version number)
    1086         8430 :       IF (.NOT. be_silent) THEN
    1087         8424 :          iw = cp_print_key_unit_nr(logger, dft_section, "PRINT%PROGRAM_BANNER", extension=".Log")
    1088         8424 :          CALL section_vals_val_get(qs_section, "METHOD", i_val=method_id)
    1089         6002 :          SELECT CASE (method_id)
    1090              :          CASE DEFAULT
    1091         6002 :             CALL qs_header(iw)
    1092              :          CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pdg, &
    1093              :                do_method_pm3, do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
    1094         1000 :             CALL se_header(iw)
    1095              :          CASE (do_method_dftb)
    1096          292 :             CALL dftb_header(iw)
    1097              :          CASE (do_method_xtb)
    1098         8424 :             IF (dft_control%qs_control%xtb_control%do_tblite) THEN
    1099          152 :                CALL tblite_header(iw, dft_control%qs_control%xtb_control%tblite_method)
    1100              :             ELSE
    1101          978 :                gfn_type = dft_control%qs_control%xtb_control%gfn_type
    1102          978 :                CALL xtb_header(iw, gfn_type)
    1103              :             END IF
    1104              :          END SELECT
    1105              :          CALL cp_print_key_finished_output(iw, logger, dft_section, &
    1106         8424 :                                            "PRINT%PROGRAM_BANNER")
    1107              :       END IF
    1108              : 
    1109         8430 :       IF (dft_control%do_sccs .AND. dft_control%qs_control%gapw) THEN
    1110            0 :          CPABORT("SCCS is not yet implemented with GAPW")
    1111              :       END IF
    1112         8430 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
    1113         8430 :       IF (do_kpoints) THEN
    1114              :          IF (dft_control%nspins == 2 .AND. dft_control%qs_control%xtb .AND. &
    1115              :              .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
    1116              :              dft_control%qs_control%xtb_control%gfn_type == gfn1xtb .AND. &
    1117          456 :              dft_control%qs_control%xtb_control%tblite_scc_mixer == tblite_scc_mixer_tblite .AND. &
    1118              :              .NOT. dft_control%qs_control%xtb_control%tblite_mixer_damping_explicit) THEN
    1119              :             CALL cp_warn(__LOCATION__, &
    1120              :                          "Reducing XTB/TBLITE_MIXER/DAMPING to 0.25 for CP2K-internal GFN1-xTB "// &
    1121              :                          "UKS k-point calculations with SCC_MIXER TBLITE. Set XTB/TBLITE_MIXER/DAMPING "// &
    1122            2 :                          "explicitly to override this conservative fallback.")
    1123            2 :             dft_control%qs_control%xtb_control%tblite_mixer_damping = 0.25_dp
    1124              :          END IF
    1125              :          ! reset some of the settings for wfn extrapolation for kpoints
    1126          456 :          SELECT CASE (dft_control%qs_control%wf_interpolation_method_nr)
    1127              :          CASE (wfi_linear_wf_method_nr, wfi_linear_ps_method_nr)
    1128              :             CALL cp_warn(__LOCATION__, "Linear WFN-based extrapolation methods are not "// &
    1129            0 :                          "implemented for k-points. Switching to USE_PREV_WF.")
    1130          456 :             dft_control%qs_control%wf_interpolation_method_nr = wfi_use_prev_wf_method_nr
    1131              :          END SELECT
    1132              :       END IF
    1133              : 
    1134              :       ! Check if any kind of electron transfer calculation has to be performed
    1135         8430 :       CALL section_vals_val_get(et_coupling_section, "TYPE_OF_CONSTRAINT", i_val=my_ival)
    1136         8430 :       dft_control%qs_control%et_coupling_calc = .FALSE.
    1137         8430 :       IF (my_ival == do_et_ddapc) THEN
    1138            0 :          et_ddapc_section => section_vals_get_subs_vals(et_coupling_section, "DDAPC_RESTRAINT_A")
    1139            0 :          dft_control%qs_control%et_coupling_calc = .TRUE.
    1140            0 :          dft_control%qs_control%ddapc_restraint = .TRUE.
    1141            0 :          CALL read_ddapc_section(dft_control%qs_control, ddapc_restraint_section=et_ddapc_section)
    1142              :       END IF
    1143              : 
    1144         8430 :       CALL read_mgrid_section(dft_control%qs_control, dft_section)
    1145              : 
    1146              :       ! Reimplemented TDDFPT
    1147         8430 :       CALL read_tddfpt2_control(dft_control%tddfpt2_control, tddfpt_section, dft_control%qs_control)
    1148              : 
    1149              :       ! RIXS
    1150         8430 :       CALL section_vals_get(rixs_section, explicit=qs_env%do_rixs)
    1151         8430 :       IF (qs_env%do_rixs) THEN
    1152           16 :          CALL read_rixs_control(dft_control%rixs_control, rixs_section, dft_control%qs_control)
    1153              :       END IF
    1154              : 
    1155              :       ! Create relativistic control section
    1156              :       BLOCK
    1157              :          TYPE(rel_control_type), POINTER :: rel_control
    1158         8430 :          ALLOCATE (rel_control)
    1159         8430 :          CALL rel_c_create(rel_control)
    1160         8430 :          CALL rel_c_read_parameters(rel_control, dft_section)
    1161         8430 :          CALL set_qs_env(qs_env, rel_control=rel_control)
    1162              :       END BLOCK
    1163              : 
    1164              :       ! Read DFTB parameter files
    1165         8430 :       IF (dft_control%qs_control%method_id == do_method_dftb) THEN
    1166          292 :          NULLIFY (ewald_env, ewald_pw, dftb_potential)
    1167          292 :          dftb_control => dft_control%qs_control%dftb_control
    1168              :          CALL qs_dftb_param_init(atomic_kind_set, qs_kind_set, dftb_control, dftb_potential, &
    1169          292 :                                  subsys_section=subsys_section, para_env=para_env)
    1170          292 :          CALL set_qs_env(qs_env, dftb_potential=dftb_potential)
    1171              :          ! check for Ewald
    1172          292 :          IF (dftb_control%do_ewald) THEN
    1173         2432 :             ALLOCATE (ewald_env)
    1174          152 :             CALL ewald_env_create(ewald_env, para_env)
    1175          152 :             poisson_section => section_vals_get_subs_vals(dft_section, "POISSON")
    1176          152 :             CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
    1177          152 :             ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
    1178          152 :             print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
    1179          152 :             CALL get_qs_kind_set(qs_kind_set, basis_rcut=ewald_rcut)
    1180              :             CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
    1181          152 :                                        cell_periodic=cell%perd)
    1182          152 :             ALLOCATE (ewald_pw)
    1183          152 :             CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, print_section=print_section)
    1184          152 :             CALL set_qs_env(qs_env, ewald_env=ewald_env, ewald_pw=ewald_pw)
    1185              :          END IF
    1186         8138 :       ELSEIF (dft_control%qs_control%method_id == do_method_xtb) THEN
    1187              :          ! Read xTB parameter file
    1188         1134 :          xtb_control => dft_control%qs_control%xtb_control
    1189         1134 :          CALL get_qs_env(qs_env, nkind=nkind)
    1190         1134 :          IF (xtb_control%do_tblite) THEN
    1191              :             ! put geometry to tblite
    1192          152 :             CALL tb_init_geometry(qs_env, qs_env%tb_tblite)
    1193              :             ! select tblite method
    1194              :             CALL tb_set_calculator(qs_env%tb_tblite, xtb_control%tblite_method, &
    1195          152 :                                    xtb_control%tblite_accuracy, xtb_control%tblite_param_file)
    1196              :             !set up wave function
    1197          152 :             CALL tb_init_wf(qs_env%tb_tblite, dft_control)
    1198              :             !get basis set
    1199          416 :             DO ikind = 1, nkind
    1200          264 :                qs_kind => qs_kind_set(ikind)
    1201              :                ! Setup proper xTB parameters
    1202          264 :                CPASSERT(.NOT. ASSOCIATED(qs_kind%xtb_parameter))
    1203          264 :                CALL allocate_xtb_atom_param(qs_kind%xtb_parameter)
    1204              :                ! Set default parameters
    1205          264 :                CALL get_qs_kind(qs_kind, element_symbol=element_symbol)
    1206              : 
    1207          264 :                NULLIFY (tmp_basis_set)
    1208          264 :                CALL tb_get_basis(qs_env%tb_tblite, tmp_basis_set, element_symbol, qs_kind%xtb_parameter, occ)
    1209          264 :                CALL add_basis_set_to_container(qs_kind%basis_sets, tmp_basis_set, "ORB")
    1210          264 :                CALL set_xtb_atom_param(qs_kind%xtb_parameter, occupation=occ)
    1211              : 
    1212              :                !setting the potential for the computation
    1213          264 :                zeff_correction = 0.0_dp
    1214              :                CALL init_potential(qs_kind%all_potential, itype="BARE", &
    1215         1736 :                                    zeff=REAL(SUM(occ), dp), zeff_correction=zeff_correction)
    1216              :             END DO
    1217              :          ELSE
    1218          982 :             NULLIFY (ewald_env, ewald_pw)
    1219         3138 :             DO ikind = 1, nkind
    1220         2156 :                qs_kind => qs_kind_set(ikind)
    1221              :                ! Setup proper xTB parameters
    1222         2156 :                CPASSERT(.NOT. ASSOCIATED(qs_kind%xtb_parameter))
    1223         2156 :                CALL allocate_xtb_atom_param(qs_kind%xtb_parameter)
    1224              :                ! Set default parameters
    1225         2156 :                gfn_type = dft_control%qs_control%xtb_control%gfn_type
    1226         2156 :                CALL get_qs_kind(qs_kind, element_symbol=element_symbol)
    1227              :                CALL xtb_parameters_init(qs_kind%xtb_parameter, gfn_type, element_symbol, &
    1228              :                                         xtb_control%parameter_file_path, xtb_control%parameter_file_name, &
    1229         2156 :                                         para_env)
    1230              :                ! set dependent parameters
    1231         2156 :                CALL xtb_parameters_set(qs_kind%xtb_parameter)
    1232              :                ! Generate basis set
    1233         2156 :                NULLIFY (tmp_basis_set)
    1234         2156 :                IF (qs_kind%xtb_parameter%z == 1) THEN
    1235              :                   ! special case hydrogen
    1236          474 :                   ngauss = xtb_control%h_sto_ng
    1237              :                ELSE
    1238         1682 :                   ngauss = xtb_control%sto_ng
    1239              :                END IF
    1240         2156 :                IF (qs_kind%xtb_parameter%defined) THEN
    1241         2154 :                   CALL init_xtb_basis(qs_kind%xtb_parameter, tmp_basis_set, ngauss)
    1242         2154 :                   CALL add_basis_set_to_container(qs_kind%basis_sets, tmp_basis_set, "ORB")
    1243              :                ELSE
    1244            2 :                   CALL set_qs_kind(qs_kind, ghost=.TRUE.)
    1245            2 :                   IF (ASSOCIATED(qs_kind%all_potential)) THEN
    1246            2 :                      DEALLOCATE (qs_kind%all_potential%elec_conf)
    1247            2 :                      DEALLOCATE (qs_kind%all_potential)
    1248              :                   END IF
    1249              :                END IF
    1250              :                ! potential
    1251         3138 :                IF (qs_kind%xtb_parameter%defined) THEN
    1252         2154 :                   zeff_correction = 0.0_dp
    1253              :                   CALL init_potential(qs_kind%all_potential, itype="BARE", &
    1254         2154 :                                       zeff=qs_kind%xtb_parameter%zeff, zeff_correction=zeff_correction)
    1255         2154 :                   CALL get_potential(qs_kind%all_potential, alpha_core_charge=alpha)
    1256         2154 :                   ccore = qs_kind%xtb_parameter%zeff*SQRT((alpha/pi)**3)
    1257         2154 :                   CALL set_potential(qs_kind%all_potential, ccore_charge=ccore)
    1258         2154 :                   qs_kind%xtb_parameter%zeff = qs_kind%xtb_parameter%zeff - zeff_correction
    1259              :                END IF
    1260              :             END DO
    1261              :             !
    1262              :             ! set repulsive potential range
    1263              :             !
    1264         3928 :             ALLOCATE (xtb_control%rcpair(nkind, nkind))
    1265          982 :             CALL xtb_pp_radius(qs_kind_set, xtb_control%rcpair, xtb_control%eps_pair, xtb_control%kf)
    1266              :             ! check for Ewald
    1267          982 :             IF (xtb_control%do_ewald) THEN
    1268         3296 :                ALLOCATE (ewald_env)
    1269          206 :                CALL ewald_env_create(ewald_env, para_env)
    1270          206 :                poisson_section => section_vals_get_subs_vals(dft_section, "POISSON")
    1271          206 :                CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
    1272          206 :                ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
    1273          206 :                print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
    1274          206 :                IF (gfn_type == 0) THEN
    1275              :                   CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
    1276           48 :                                              silent=silent, pset="EEQ", cell_periodic=cell%perd)
    1277              :                ELSE
    1278              :                   CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
    1279          158 :                                              silent=silent, cell_periodic=cell%perd)
    1280              :                END IF
    1281          206 :                ALLOCATE (ewald_pw)
    1282          206 :                CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, print_section=print_section)
    1283          206 :                CALL set_qs_env(qs_env, ewald_env=ewald_env, ewald_pw=ewald_pw)
    1284              :             END IF
    1285              :          END IF
    1286              :       END IF
    1287              :       ! lri or ri env initialization
    1288         8430 :       lri_section => section_vals_get_subs_vals(qs_section, "LRIGPW")
    1289              :       IF (dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    1290         8430 :           dft_control%qs_control%lri_optbas .OR. &
    1291              :           dft_control%qs_control%method_id == do_method_rigpw) THEN
    1292           50 :          CALL lri_env_init(lri_env, lri_section)
    1293           50 :          CALL set_qs_env(qs_env, lri_env=lri_env)
    1294              :       END IF
    1295              : 
    1296              :       ! Check basis and fill in missing parts
    1297         8430 :       CALL check_qs_kind_set(qs_kind_set, dft_control, subsys_section=subsys_section)
    1298              : 
    1299              :       ! Check that no all-electron potential is present if GPW or GAPW_XC
    1300         8430 :       CALL get_qs_kind_set(qs_kind_set, all_potential_present=all_potential_present)
    1301              :       IF ((dft_control%qs_control%method_id == do_method_gpw) .OR. &
    1302         8430 :           (dft_control%qs_control%method_id == do_method_gapw_xc) .OR. &
    1303              :           (dft_control%qs_control%method_id == do_method_ofgpw)) THEN
    1304         4850 :          IF (all_potential_present) THEN
    1305            0 :             CPABORT("All-electron calculations with GPW, GAPW_XC, and OFGPW are not implemented")
    1306              :          END IF
    1307              :       END IF
    1308              : 
    1309              :       ! Check that no cneo potential is present if not GAPW
    1310         8430 :       CALL get_qs_kind_set(qs_kind_set, cneo_potential_present=cneo_potential_present)
    1311         8430 :       IF (cneo_potential_present .AND. &
    1312              :           dft_control%qs_control%method_id /= do_method_gapw) THEN
    1313            0 :          CPABORT("CNEO calculations require GAPW method")
    1314              :       END IF
    1315              : 
    1316              :       ! DFT+U
    1317         8430 :       CALL get_qs_kind_set(qs_kind_set, dft_plus_u_atom_present=dft_control%dft_plus_u)
    1318              : 
    1319         8430 :       IF (dft_control%do_admm) THEN
    1320              :          ! Check if ADMM basis is available
    1321          520 :          CALL get_qs_env(qs_env, nkind=nkind)
    1322         1482 :          DO ikind = 1, nkind
    1323          962 :             NULLIFY (aux_fit_basis)
    1324          962 :             qs_kind => qs_kind_set(ikind)
    1325          962 :             CALL get_qs_kind(qs_kind, basis_set=aux_fit_basis, basis_type="AUX_FIT")
    1326         1482 :             IF (.NOT. (ASSOCIATED(aux_fit_basis))) THEN
    1327              :                ! AUX_FIT basis set is not available
    1328            0 :                CPABORT("AUX_FIT basis set is not defined. ")
    1329              :             END IF
    1330              :          END DO
    1331              :       END IF
    1332              : 
    1333         8430 :       lribas = .FALSE.
    1334         8430 :       e1terms = .FALSE.
    1335         8430 :       IF (dft_control%qs_control%method_id == do_method_lrigpw) THEN
    1336           42 :          lribas = .TRUE.
    1337           42 :          CALL get_qs_env(qs_env, lri_env=lri_env)
    1338           42 :          e1terms = lri_env%exact_1c_terms
    1339              :       END IF
    1340         8430 :       IF (dft_control%qs_control%do_kg) THEN
    1341           94 :          CALL section_vals_val_get(dft_section, "KG_METHOD%TNADD_METHOD", i_val=tnadd_method)
    1342           94 :          IF (tnadd_method == kg_tnadd_embed_ri) lribas = .TRUE.
    1343              :       END IF
    1344         8420 :       IF (lribas) THEN
    1345              :          ! Check if LRI_AUX basis is available, auto-generate if needed
    1346           52 :          CALL get_qs_env(qs_env, nkind=nkind)
    1347          150 :          DO ikind = 1, nkind
    1348           98 :             NULLIFY (lri_aux_basis)
    1349           98 :             qs_kind => qs_kind_set(ikind)
    1350           98 :             CALL get_qs_kind(qs_kind, basis_set=lri_aux_basis, basis_type="LRI_AUX")
    1351          150 :             IF (.NOT. (ASSOCIATED(lri_aux_basis))) THEN
    1352              :                ! LRI_AUX basis set is not yet loaded
    1353              :                CALL cp_warn(__LOCATION__, "Automatic Generation of LRI_AUX basis. "// &
    1354           36 :                             "This is experimental code.")
    1355              :                ! Generate a default basis
    1356           36 :                CALL create_lri_aux_basis_set(lri_aux_basis, qs_kind, dft_control%auto_basis_lri_aux, e1terms)
    1357           36 :                CALL add_basis_set_to_container(qs_kind%basis_sets, lri_aux_basis, "LRI_AUX")
    1358              :             END IF
    1359              :          END DO
    1360              :       END IF
    1361              : 
    1362         8430 :       CALL section_vals_val_get(qs_env%input, "DFT%XC%HF%RI%_SECTION_PARAMETERS_", l_val=do_ri_hfx)
    1363              :       CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%HF%RI%_SECTION_PARAMETERS_", &
    1364         8430 :                                 l_val=do_rpa_ri_exx)
    1365         8430 :       IF (do_ri_hfx .OR. do_rpa_ri_exx) THEN
    1366          114 :          CALL get_qs_env(qs_env, nkind=nkind)
    1367          114 :          CALL section_vals_val_get(qs_env%input, "DFT%SORT_BASIS", i_val=sort_basis)
    1368          306 :          DO ikind = 1, nkind
    1369          192 :             NULLIFY (ri_hfx_basis)
    1370          192 :             qs_kind => qs_kind_set(ikind)
    1371              :             CALL get_qs_kind(qs_kind=qs_kind, basis_set=ri_hfx_basis, &
    1372          192 :                              basis_type="RI_HFX")
    1373         8622 :             IF (.NOT. (ASSOCIATED(ri_hfx_basis))) THEN
    1374          186 :                CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto)
    1375          186 :                IF (dft_control%do_admm) THEN
    1376              :                   CALL create_ri_aux_basis_set(ri_hfx_basis, qs_kind, dft_control%auto_basis_ri_hfx, &
    1377           62 :                                                basis_type="AUX_FIT", basis_sort=sort_basis)
    1378              :                ELSE
    1379              :                   CALL create_ri_aux_basis_set(ri_hfx_basis, qs_kind, dft_control%auto_basis_ri_hfx, &
    1380          124 :                                                basis_sort=sort_basis)
    1381              :                END IF
    1382          186 :                CALL add_basis_set_to_container(qs_kind%basis_sets, ri_hfx_basis, "RI_HFX")
    1383              :             END IF
    1384              :          END DO
    1385              :       END IF
    1386              : 
    1387         8430 :       IF (dft_control%qs_control%method_id == do_method_rigpw) THEN
    1388              :          ! Check if RI_HXC basis is available, auto-generate if needed
    1389            2 :          CALL get_qs_env(qs_env, nkind=nkind)
    1390            4 :          DO ikind = 1, nkind
    1391            2 :             NULLIFY (ri_hfx_basis)
    1392            2 :             qs_kind => qs_kind_set(ikind)
    1393            2 :             CALL get_qs_kind(qs_kind, basis_set=ri_hfx_basis, basis_type="RI_HXC")
    1394            4 :             IF (.NOT. (ASSOCIATED(ri_hfx_basis))) THEN
    1395              :                ! Generate a default basis
    1396            2 :                CALL create_ri_aux_basis_set(ri_hfx_basis, qs_kind, dft_control%auto_basis_ri_hxc)
    1397            2 :                CALL add_basis_set_to_container(qs_kind%basis_sets, ri_hfx_basis, "RI_HXC")
    1398              :             END IF
    1399              :          END DO
    1400              :       END IF
    1401              : 
    1402              :       ! Harris method
    1403         8430 :       NULLIFY (harris_env)
    1404              :       CALL section_vals_val_get(dft_section, "HARRIS_METHOD%_SECTION_PARAMETERS_", &
    1405         8430 :                                 l_val=qs_env%harris_method)
    1406         8430 :       harris_section => section_vals_get_subs_vals(dft_section, "HARRIS_METHOD")
    1407         8430 :       CALL harris_env_create(qs_env, harris_env, harris_section)
    1408         8430 :       CALL set_qs_env(qs_env, harris_env=harris_env)
    1409              :       !
    1410         8430 :       IF (qs_env%harris_method) THEN
    1411            8 :          CALL get_qs_env(qs_env, nkind=nkind)
    1412              :          ! Check if RI_HXC basis is available, auto-generate if needed
    1413           30 :          DO ikind = 1, nkind
    1414           22 :             NULLIFY (tmp_basis_set)
    1415           22 :             qs_kind => qs_kind_set(ikind)
    1416           22 :             CALL get_qs_kind(qs_kind, basis_set=rhoin_basis, basis_type="RHOIN")
    1417           30 :             IF (.NOT. (ASSOCIATED(rhoin_basis))) THEN
    1418              :                ! Generate a default basis
    1419           22 :                CALL create_ri_aux_basis_set(tmp_basis_set, qs_kind, dft_control%auto_basis_ri_hxc)
    1420           22 :                IF (qs_env%harris_env%density_source == hden_atomic) THEN
    1421           22 :                   CALL create_primitive_basis_set(tmp_basis_set, rhoin_basis, lmax=0)
    1422           22 :                   CALL deallocate_gto_basis_set(tmp_basis_set)
    1423              :                ELSE
    1424            0 :                   rhoin_basis => tmp_basis_set
    1425              :                END IF
    1426           22 :                CALL add_basis_set_to_container(qs_kind%basis_sets, rhoin_basis, "RHOIN")
    1427              :             END IF
    1428              :          END DO
    1429              :       END IF
    1430              : 
    1431         8430 :       mp2_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION")
    1432         8430 :       CALL section_vals_get(mp2_section, explicit=mp2_present)
    1433         8430 :       IF (mp2_present) THEN
    1434              : 
    1435              :          ! basis should be sorted for imaginary time RPA/GW
    1436          476 :          CALL section_vals_val_get(qs_env%input, "DFT%SORT_BASIS", i_val=sort_basis)
    1437              :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%LOW_SCALING%_SECTION_PARAMETERS_", &
    1438          476 :                                    l_val=do_wfc_im_time)
    1439              : 
    1440          476 :          IF (do_wfc_im_time .AND. sort_basis /= basis_sort_zet) THEN
    1441              :             CALL cp_warn(__LOCATION__, &
    1442           10 :                          "Low-scaling RPA requires SORT_BASIS EXP keyword (in DFT input section) for good performance")
    1443              :          END IF
    1444              : 
    1445              :          ! Check if RI_AUX basis (for MP2/RPA) is given, auto-generate if not
    1446          476 :          CALL mp2_env_create(qs_env%mp2_env)
    1447          476 :          CALL get_qs_env(qs_env, mp2_env=mp2_env, nkind=nkind)
    1448          476 :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_MP2%_SECTION_PARAMETERS_", l_val=do_ri_mp2)
    1449          476 :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_SOS_MP2%_SECTION_PARAMETERS_", l_val=do_ri_sos_mp2)
    1450          476 :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%_SECTION_PARAMETERS_", l_val=do_ri_rpa)
    1451          476 :          IF (do_ri_mp2 .OR. do_ri_sos_mp2 .OR. do_ri_rpa) THEN
    1452         1282 :             DO ikind = 1, nkind
    1453          844 :                NULLIFY (ri_aux_basis_set)
    1454          844 :                qs_kind => qs_kind_set(ikind)
    1455              :                CALL get_qs_kind(qs_kind=qs_kind, basis_set=ri_aux_basis_set, &
    1456          844 :                                 basis_type="RI_AUX")
    1457         1320 :                IF (.NOT. (ASSOCIATED(ri_aux_basis_set))) THEN
    1458              :                   ! RI_AUX basis set is not yet loaded
    1459              :                   ! Generate a default basis
    1460            8 :                   CALL create_ri_aux_basis_set(ri_aux_basis_set, qs_kind, dft_control%auto_basis_ri_aux, basis_sort=sort_basis)
    1461            8 :                   CALL add_basis_set_to_container(qs_kind%basis_sets, ri_aux_basis_set, "RI_AUX")
    1462              :                   ! Add a flag, which allows to check if the basis was generated
    1463              :                   !  when applying ERI_METHOD OS to mp2, ri-rpa, gw etc
    1464            8 :                   qs_env%mp2_env%ri_aux_auto_generated = .TRUE.
    1465              :                END IF
    1466              :             END DO
    1467              :          END IF
    1468              : 
    1469              :       END IF
    1470              : 
    1471         8430 :       IF (dft_control%do_xas_tdp_calculation .OR. qs_env%do_rixs) THEN
    1472              :          ! Check if RI_XAS basis is given, auto-generate if not
    1473           68 :          CALL get_qs_env(qs_env, nkind=nkind)
    1474          178 :          DO ikind = 1, nkind
    1475          110 :             NULLIFY (ri_xas_basis)
    1476          110 :             qs_kind => qs_kind_set(ikind)
    1477          110 :             CALL get_qs_kind(qs_kind, basis_Set=ri_xas_basis, basis_type="RI_XAS")
    1478         8540 :             IF (.NOT. ASSOCIATED(ri_xas_basis)) THEN
    1479              :                ! Generate a default basis
    1480          106 :                CALL create_ri_aux_basis_set(ri_xas_basis, qs_kind, dft_control%auto_basis_ri_xas)
    1481          106 :                CALL add_basis_set_to_container(qs_kind%basis_sets, ri_xas_basis, "RI_XAS")
    1482              :             END IF
    1483              :          END DO
    1484              :       END IF
    1485              : 
    1486              :       ! Initialize the spherical harmonics and the orbital transformation matrices
    1487         8430 :       CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto, maxlppl=maxlppl, maxlppnl=maxlppnl)
    1488              : 
    1489              :       ! CNEO nuclear basis contributes to GAPW rho0
    1490         8430 :       IF (cneo_potential_present) THEN
    1491            8 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_nuc, basis_type="NUC")
    1492            8 :          maxlgto = MAX(maxlgto, maxlgto_nuc)
    1493              :       END IF
    1494         8430 :       lmax_sphere = dft_control%qs_control%gapw_control%lmax_sphere
    1495         8430 :       IF (lmax_sphere < 0) THEN
    1496         8294 :          lmax_sphere = 2*maxlgto
    1497         8294 :          dft_control%qs_control%gapw_control%lmax_sphere = lmax_sphere
    1498              :       END IF
    1499         8430 :       IF (dft_control%qs_control%method_id == do_method_lrigpw .OR. dft_control%qs_control%lri_optbas) THEN
    1500           48 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_lri, basis_type="LRI_AUX")
    1501              :          !take maxlgto from lri basis if larger (usually)
    1502           48 :          maxlgto = MAX(maxlgto, maxlgto_lri)
    1503         8382 :       ELSE IF (dft_control%qs_control%method_id == do_method_rigpw) THEN
    1504            2 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_lri, basis_type="RI_HXC")
    1505            2 :          maxlgto = MAX(maxlgto, maxlgto_lri)
    1506              :       END IF
    1507         8430 :       IF (dft_control%do_xas_tdp_calculation .OR. qs_env%do_rixs) THEN
    1508              :          !done as a precaution
    1509           68 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_lri, basis_type="RI_XAS")
    1510           68 :          maxlgto = MAX(maxlgto, maxlgto_lri)
    1511              :       END IF
    1512         8430 :       maxl = MAX(2*maxlgto, maxlppl, maxlppnl, lmax_sphere) + 1
    1513              : 
    1514         8430 :       CALL init_orbital_pointers(maxl)
    1515              : 
    1516         8430 :       CALL init_spherical_harmonics(maxl, 0)
    1517              : 
    1518              :       !  Initialise the qs_kind_set
    1519         8430 :       CALL init_qs_kind_set(qs_kind_set)
    1520              : 
    1521              :       ! Initialise GAPW soft basis and projectors
    1522         8430 :       IF (dft_control%qs_control%method_id == do_method_gapw .OR. &
    1523              :           dft_control%qs_control%method_id == do_method_gapw_xc) THEN
    1524         1288 :          qs_control => dft_control%qs_control
    1525         1288 :          CALL init_gapw_basis_set(qs_kind_set, qs_control, qs_env%input)
    1526              :       END IF
    1527              : 
    1528              :       ! Initialise CNEO nuclear soft basis
    1529         8430 :       IF (cneo_potential_present) THEN
    1530            8 :          CALL init_cneo_basis_set(qs_kind_set, qs_control)
    1531              :       END IF
    1532              : 
    1533              :       ! Initialize the pretabulation for the calculation of the
    1534              :       ! incomplete Gamma function F_n(t) after McMurchie-Davidson
    1535         8430 :       CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto)
    1536         8430 :       maxl = MAX(3*maxlgto + 1, 0)
    1537         8430 :       CALL init_md_ftable(maxl)
    1538              : 
    1539              :       ! Initialize the atomic interaction radii
    1540         8430 :       CALL init_interaction_radii(dft_control%qs_control, qs_kind_set)
    1541              :       !
    1542         8430 :       IF (dft_control%qs_control%method_id == do_method_xtb) THEN
    1543         1134 :          IF (.NOT. dft_control%qs_control%xtb_control%do_tblite) THEN
    1544              :             ! cutoff radius
    1545          982 :             CALL get_qs_env(qs_env, nkind=nkind)
    1546         3138 :             DO ikind = 1, nkind
    1547         2156 :                qs_kind => qs_kind_set(ikind)
    1548         3138 :                IF (qs_kind%xtb_parameter%defined) THEN
    1549         2154 :                   CALL get_qs_kind(qs_kind, basis_set=tmp_basis_set)
    1550         2154 :                   rcut = xtb_control%coulomb_sr_cut
    1551         2154 :                   fxx = 2.0_dp*xtb_control%coulomb_sr_eps*qs_kind%xtb_parameter%eta**2
    1552         2154 :                   fxx = 0.80_dp*(1.0_dp/fxx)**0.3333_dp
    1553         2154 :                   rcut = MIN(rcut, xtb_control%coulomb_sr_cut)
    1554         2154 :                   qs_kind%xtb_parameter%rcut = MIN(rcut, fxx)
    1555              :                ELSE
    1556            2 :                   qs_kind%xtb_parameter%rcut = 0.0_dp
    1557              :                END IF
    1558              :             END DO
    1559              :          END IF
    1560              :       END IF
    1561              : 
    1562         8430 :       IF (.NOT. be_silent) THEN
    1563         8424 :          CALL write_pgf_orb_radii("orb", atomic_kind_set, qs_kind_set, subsys_section)
    1564         8424 :          CALL write_pgf_orb_radii("aux", atomic_kind_set, qs_kind_set, subsys_section)
    1565         8424 :          CALL write_pgf_orb_radii("lri", atomic_kind_set, qs_kind_set, subsys_section)
    1566         8424 :          CALL write_pgf_orb_radii("nuc", atomic_kind_set, qs_kind_set, subsys_section)
    1567         8424 :          CALL write_core_charge_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1568         8424 :          CALL write_ppl_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1569         8424 :          CALL write_ppnl_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1570         8424 :          CALL write_paw_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1571              :       END IF
    1572              : 
    1573              :       ! Distribute molecules and atoms using the new data structures
    1574              :       CALL distribute_molecules_1d(atomic_kind_set=atomic_kind_set, &
    1575              :                                    particle_set=particle_set, &
    1576              :                                    local_particles=local_particles, &
    1577              :                                    molecule_kind_set=molecule_kind_set, &
    1578              :                                    molecule_set=molecule_set, &
    1579              :                                    local_molecules=local_molecules, &
    1580         8430 :                                    force_env_section=qs_env%input)
    1581              : 
    1582              :       ! SCF parameters
    1583       244470 :       ALLOCATE (scf_control)
    1584              :       ! set (non)-self consistency
    1585         8430 :       IF (dft_control%qs_control%dftb) THEN
    1586          292 :          scf_control%non_selfconsistent = .NOT. dft_control%qs_control%dftb_control%self_consistent
    1587              :       END IF
    1588         8430 :       IF (dft_control%qs_control%xtb) THEN
    1589         1134 :          IF (dft_control%qs_control%xtb_control%do_tblite) THEN
    1590          152 :             scf_control%non_selfconsistent = .FALSE.
    1591              :          ELSE
    1592          982 :             scf_control%non_selfconsistent = (dft_control%qs_control%xtb_control%gfn_type == 0)
    1593              :          END IF
    1594              :       END IF
    1595         8430 :       IF (qs_env%harris_method) THEN
    1596            8 :          scf_control%non_selfconsistent = .TRUE.
    1597              :       END IF
    1598         8430 :       CALL scf_c_create(scf_control)
    1599         8430 :       CALL scf_c_read_parameters(scf_control, dft_section)
    1600         8430 :       IF (.NOT. dft_control%qs_control%do_ls_scf) THEN
    1601         8290 :          SELECT CASE (dft_control%qs_control%method_id)
    1602              :          CASE (do_method_dftb)
    1603          248 :             IF (dft_control%qs_control%dftb_control%tblite_scc_mixer == tblite_scc_mixer_tblite) &
    1604            2 :                scf_control%max_scf = dft_control%qs_control%dftb_control%tblite_mixer_iterations
    1605              :          CASE (do_method_xtb)
    1606         1098 :             IF (dft_control%qs_control%xtb_control%tblite_scc_mixer == tblite_scc_mixer_tblite) &
    1607         8062 :                scf_control%max_scf = dft_control%qs_control%xtb_control%tblite_mixer_iterations
    1608              :          END SELECT
    1609              :       END IF
    1610              : 
    1611              :       ! Allocate the data structure for Quickstep energies
    1612         8430 :       CALL allocate_qs_energy(energy)
    1613              : 
    1614              :       ! Check for orthogonal basis
    1615         8430 :       has_unit_metric = .FALSE.
    1616         8430 :       IF (dft_control%qs_control%semi_empirical) THEN
    1617         1000 :          IF (dft_control%qs_control%se_control%orthogonal_basis) has_unit_metric = .TRUE.
    1618              :       END IF
    1619         8430 :       IF (dft_control%qs_control%dftb) THEN
    1620          292 :          IF (dft_control%qs_control%dftb_control%orthogonal_basis) has_unit_metric = .TRUE.
    1621              :       END IF
    1622         8430 :       CALL set_qs_env(qs_env, has_unit_metric=has_unit_metric)
    1623              : 
    1624              :       !  Activate the interpolation
    1625              :       CALL wfi_create(wf_history, &
    1626              :                       interpolation_method_nr= &
    1627              :                       dft_control%qs_control%wf_interpolation_method_nr, &
    1628              :                       extrapolation_order=dft_control%qs_control%wf_extrapolation_order, &
    1629         8430 :                       has_unit_metric=has_unit_metric)
    1630              : 
    1631              :       ! Set the current Quickstep environment
    1632              :       CALL set_qs_env(qs_env=qs_env, &
    1633              :                       scf_control=scf_control, &
    1634         8430 :                       wf_history=wf_history)
    1635              : 
    1636              :       CALL qs_subsys_set(subsys, &
    1637              :                          cell_ref=cell_ref, &
    1638              :                          use_ref_cell=use_ref_cell, &
    1639              :                          energy=energy, &
    1640         8430 :                          force=force)
    1641              : 
    1642         8430 :       CALL get_qs_env(qs_env, ks_env=ks_env)
    1643         8430 :       CALL set_ks_env(ks_env, dft_control=dft_control)
    1644              : 
    1645              :       CALL qs_subsys_set(subsys, local_molecules=local_molecules, &
    1646         8430 :                          local_particles=local_particles, cell=cell)
    1647              : 
    1648         8430 :       CALL distribution_1d_release(local_particles)
    1649         8430 :       CALL distribution_1d_release(local_molecules)
    1650         8430 :       CALL wfi_release(wf_history)
    1651              : 
    1652              :       CALL get_qs_env(qs_env=qs_env, &
    1653              :                       atomic_kind_set=atomic_kind_set, &
    1654              :                       dft_control=dft_control, &
    1655         8430 :                       scf_control=scf_control)
    1656              : 
    1657              :       ! Decide what conditions need mo_derivs
    1658              :       ! right now, this only appears to be OT
    1659         8430 :       IF (dft_control%qs_control%do_ls_scf .OR. &
    1660              :           dft_control%qs_control%do_almo_scf) THEN
    1661          454 :          CALL set_qs_env(qs_env=qs_env, requires_mo_derivs=.FALSE.)
    1662              :       ELSE
    1663         7976 :          IF (scf_control%use_ot) THEN
    1664         2240 :             CALL set_qs_env(qs_env=qs_env, requires_mo_derivs=.TRUE.)
    1665              :          ELSE
    1666         5736 :             CALL set_qs_env(qs_env=qs_env, requires_mo_derivs=.FALSE.)
    1667              :          END IF
    1668              :       END IF
    1669              : 
    1670              :       ! XXXXXXX this is backwards XXXXXXXX
    1671         8430 :       IF (dft_control%qs_control%xtb_control%do_tblite .AND. .NOT. scf_control%use_ot) THEN
    1672          148 :          IF (.NOT. scf_control%smear%do_smear) THEN
    1673              :             ! set tblite default smearing
    1674           98 :             scf_control%smear%do_smear = .TRUE.
    1675           98 :             scf_control%smear%method = smear_fermi_dirac
    1676           98 :             scf_control%smear%electronic_temperature = 300._dp/kelvin
    1677           98 :             scf_control%smear%eps_fermi_dirac = 1.E-6_dp
    1678              :          END IF
    1679              :       END IF
    1680         8430 :       dft_control%smear = scf_control%smear%do_smear
    1681              : 
    1682              :       ! Periodic efield needs equal occupation and orbital gradients
    1683         8430 :       IF (.NOT. (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb)) THEN
    1684         7004 :          IF (dft_control%apply_period_efield) THEN
    1685           30 :             CALL get_qs_env(qs_env=qs_env, requires_mo_derivs=orb_gradient)
    1686           30 :             IF (.NOT. orb_gradient) THEN
    1687              :                CALL cp_abort(__LOCATION__, "Periodic Efield needs orbital gradient and direct optimization."// &
    1688            0 :                              " Use the OT optimization method.")
    1689              :             END IF
    1690           30 :             IF (dft_control%smear) THEN
    1691              :                CALL cp_abort(__LOCATION__, "Periodic Efield needs equal occupation numbers."// &
    1692            0 :                              " Smearing option is not possible.")
    1693              :             END IF
    1694              :          END IF
    1695              :       END IF
    1696              : 
    1697              :       ! Initialize the GAPW local densities and potentials
    1698         8430 :       IF (dft_control%qs_control%method_id == do_method_gapw .OR. &
    1699              :           dft_control%qs_control%method_id == do_method_gapw_xc) THEN
    1700              :          ! Allocate and initialize the set of atomic densities
    1701         1288 :          NULLIFY (rho_atom_set)
    1702         1288 :          gapw_control => dft_control%qs_control%gapw_control
    1703         1288 :          CALL init_rho_atom(rho_atom_set, atomic_kind_set, qs_kind_set, dft_control, para_env)
    1704         1288 :          CALL set_qs_env(qs_env=qs_env, rho_atom_set=rho_atom_set)
    1705         1288 :          IF (dft_control%qs_control%method_id /= do_method_gapw_xc) THEN
    1706         1110 :             CALL get_qs_env(qs_env=qs_env, local_rho_set=local_rho_set, natom=natom)
    1707              :             ! Allocate and initialize the compensation density rho0
    1708         1110 :             CALL init_rho0(local_rho_set, qs_env, gapw_control)
    1709              :             ! Allocate and Initialize the local coulomb term
    1710         1110 :             CALL init_coulomb_local(qs_env%hartree_local, natom)
    1711              :          END IF
    1712              :          ! NLCC
    1713         1288 :          CALL init_gapw_nlcc(qs_kind_set)
    1714              :          ! Accurate XC integration
    1715         1288 :          IF (gapw_control%accurate_xcint) THEN
    1716          246 :             CPASSERT(.NOT. ASSOCIATED(gapw_control%aw))
    1717          246 :             CALL get_qs_env(qs_env, nkind=nkind)
    1718          738 :             ALLOCATE (gapw_control%aw(nkind))
    1719          246 :             alpha = gapw_control%aweights
    1720          700 :             DO ikind = 1, nkind
    1721          454 :                qs_kind => qs_kind_set(ikind)
    1722          454 :                CALL get_qs_kind(qs_kind, hard_radius=rc, paw_atom=paw_atom)
    1723          700 :                IF (paw_atom) THEN
    1724          446 :                   gapw_control%aw(ikind) = alpha*(1.2_dp/rc)**2
    1725              :                ELSE
    1726            8 :                   gapw_control%aw(ikind) = 0.0_dp
    1727              :                END IF
    1728              :             END DO
    1729              :          END IF
    1730         7142 :       ELSE IF (dft_control%qs_control%method_id == do_method_lrigpw) THEN
    1731              :          ! allocate local ri environment
    1732              :          ! nothing to do here?
    1733         7100 :       ELSE IF (dft_control%qs_control%method_id == do_method_rigpw) THEN
    1734              :          ! allocate ri environment
    1735              :          ! nothing to do here?
    1736         7098 :       ELSE IF (dft_control%qs_control%semi_empirical) THEN
    1737         1000 :          NULLIFY (se_store_int_env, se_nddo_mpole, se_nonbond_env)
    1738         1000 :          natom = SIZE(particle_set)
    1739         1000 :          se_section => section_vals_get_subs_vals(qs_section, "SE")
    1740         1000 :          se_control => dft_control%qs_control%se_control
    1741              : 
    1742              :          ! Make the cutoff radii choice a bit smarter
    1743         1000 :          CALL se_cutoff_compatible(se_control, se_section, cell, output_unit)
    1744              : 
    1745         1998 :          SELECT CASE (dft_control%qs_control%method_id)
    1746              :          CASE DEFAULT
    1747              :          CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pm3, &
    1748              :                do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
    1749              :             ! Neighbor lists have to be MAX(interaction range, orbital range)
    1750              :             ! set new kind radius
    1751         1000 :             CALL init_se_nlradius(se_control, atomic_kind_set, qs_kind_set, subsys_section)
    1752              :          END SELECT
    1753              :          ! Initialize to zero the max multipole to treat in the EWALD scheme..
    1754         1000 :          se_control%max_multipole = do_multipole_none
    1755              :          ! check for Ewald
    1756         1000 :          IF (se_control%do_ewald .OR. se_control%do_ewald_gks) THEN
    1757          512 :             ALLOCATE (ewald_env)
    1758           32 :             CALL ewald_env_create(ewald_env, para_env)
    1759           32 :             poisson_section => section_vals_get_subs_vals(dft_section, "POISSON")
    1760           32 :             CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
    1761           32 :             ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
    1762              :             print_section => section_vals_get_subs_vals(qs_env%input, &
    1763           32 :                                                         "PRINT%GRID_INFORMATION")
    1764           32 :             CALL read_ewald_section(ewald_env, ewald_section)
    1765              :             ! Create ewald grids
    1766           32 :             ALLOCATE (ewald_pw)
    1767              :             CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, &
    1768           32 :                                  print_section=print_section)
    1769              :             ! Initialize ewald grids
    1770           32 :             CALL ewald_pw_grid_update(ewald_pw, ewald_env, cell%hmat)
    1771              :             ! Setup the nonbond environment (real space part of Ewald)
    1772           32 :             CALL ewald_env_get(ewald_env, rcut=ewald_rcut)
    1773              :             ! Setup the maximum level of multipoles to be treated in the periodic SE scheme
    1774           32 :             IF (se_control%do_ewald) THEN
    1775           30 :                CALL ewald_env_get(ewald_env, max_multipole=se_control%max_multipole)
    1776              :             END IF
    1777              :             CALL section_vals_val_get(se_section, "NEIGHBOR_LISTS%VERLET_SKIN", &
    1778           32 :                                       r_val=verlet_skin)
    1779           32 :             ALLOCATE (se_nonbond_env)
    1780              :             CALL fist_nonbond_env_create(se_nonbond_env, atomic_kind_set, do_nonbonded=.TRUE., &
    1781              :                                          do_electrostatics=.TRUE., verlet_skin=verlet_skin, ewald_rcut=ewald_rcut, &
    1782           32 :                                          ei_scale14=0.0_dp, vdw_scale14=0.0_dp, shift_cutoff=.FALSE.)
    1783              :             ! Create and Setup NDDO multipole environment
    1784           32 :             CALL nddo_mpole_setup(se_nddo_mpole, natom)
    1785              :             CALL set_qs_env(qs_env, ewald_env=ewald_env, ewald_pw=ewald_pw, &
    1786           32 :                             se_nonbond_env=se_nonbond_env, se_nddo_mpole=se_nddo_mpole)
    1787              :             ! Handle the residual integral part 1/R^3
    1788              :             CALL semi_empirical_expns3_setup(qs_kind_set, se_control, &
    1789           32 :                                              dft_control%qs_control%method_id)
    1790              :          END IF
    1791              :          ! Taper function
    1792              :          CALL se_taper_create(se_taper, se_control%integral_screening, se_control%do_ewald, &
    1793              :                               se_control%taper_cou, se_control%range_cou, &
    1794              :                               se_control%taper_exc, se_control%range_exc, &
    1795              :                               se_control%taper_scr, se_control%range_scr, &
    1796         1000 :                               se_control%taper_lrc, se_control%range_lrc)
    1797         1000 :          CALL set_qs_env(qs_env, se_taper=se_taper)
    1798              :          ! Store integral environment
    1799         1000 :          CALL semi_empirical_si_create(se_store_int_env, se_section)
    1800         1000 :          CALL set_qs_env(qs_env, se_store_int_env=se_store_int_env)
    1801              :       END IF
    1802              : 
    1803              :       ! Initialize possible dispersion parameters
    1804              :       IF (dft_control%qs_control%method_id == do_method_gpw .OR. &
    1805              :           dft_control%qs_control%method_id == do_method_gapw .OR. &
    1806              :           dft_control%qs_control%method_id == do_method_gapw_xc .OR. &
    1807              :           dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    1808         8430 :           dft_control%qs_control%method_id == do_method_rigpw .OR. &
    1809              :           dft_control%qs_control%method_id == do_method_ofgpw) THEN
    1810        30020 :          ALLOCATE (dispersion_env)
    1811         6004 :          NULLIFY (xc_section)
    1812         6004 :          xc_section => section_vals_get_subs_vals(dft_section, "XC")
    1813         6004 :          CALL qs_dispersion_env_set(dispersion_env, xc_section)
    1814         6004 :          IF (dispersion_env%type == xc_vdw_fun_pairpot) THEN
    1815          178 :             NULLIFY (pp_section)
    1816          178 :             pp_section => section_vals_get_subs_vals(xc_section, "VDW_POTENTIAL%PAIR_POTENTIAL")
    1817          178 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, pp_section, para_env)
    1818         5826 :          ELSE IF (dispersion_env%type == xc_vdw_fun_nonloc) THEN
    1819           50 :             NULLIFY (nl_section)
    1820           50 :             nl_section => section_vals_get_subs_vals(xc_section, "VDW_POTENTIAL%NON_LOCAL")
    1821           50 :             CALL qs_dispersion_nonloc_init(dispersion_env, para_env)
    1822              :          END IF
    1823         6004 :          CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1824         2426 :       ELSE IF (dft_control%qs_control%method_id == do_method_dftb) THEN
    1825         1460 :          ALLOCATE (dispersion_env)
    1826              :          ! set general defaults
    1827              :          dispersion_env%doabc = .FALSE.
    1828              :          dispersion_env%c9cnst = .FALSE.
    1829              :          dispersion_env%lrc = .FALSE.
    1830              :          dispersion_env%srb = .FALSE.
    1831              :          dispersion_env%verbose = .FALSE.
    1832              :          NULLIFY (dispersion_env%c6ab, dispersion_env%maxci, dispersion_env%r0ab, dispersion_env%rcov, &
    1833              :                   dispersion_env%r2r4, dispersion_env%cn, dispersion_env%cnkind, dispersion_env%cnlist, &
    1834              :                   dispersion_env%d3_exclude_pair)
    1835              :          NULLIFY (dispersion_env%q_mesh, dispersion_env%kernel, dispersion_env%d2phi_dk2, &
    1836              :                   dispersion_env%d2y_dx2, dispersion_env%dftd_section)
    1837              :          NULLIFY (dispersion_env%sab_vdw, dispersion_env%sab_cn)
    1838          292 :          IF (dftb_control%dispersion .AND. dftb_control%dispersion_type == dispersion_d3) THEN
    1839           14 :             dispersion_env%type = xc_vdw_fun_pairpot
    1840           14 :             dispersion_env%pp_type = vdw_pairpot_dftd3
    1841           14 :             dispersion_env%eps_cn = dftb_control%epscn
    1842           14 :             dispersion_env%s6 = dftb_control%sd3(1)
    1843           14 :             dispersion_env%sr6 = dftb_control%sd3(2)
    1844           14 :             dispersion_env%s8 = dftb_control%sd3(3)
    1845           14 :             dispersion_env%domol = .FALSE.
    1846           14 :             dispersion_env%kgc8 = 0._dp
    1847           14 :             dispersion_env%rc_disp = dftb_control%rcdisp
    1848           14 :             dispersion_env%exp_pre = 0._dp
    1849           14 :             dispersion_env%scaling = 0._dp
    1850           14 :             dispersion_env%nd3_exclude_pair = 0
    1851           14 :             dispersion_env%parameter_file_name = dftb_control%dispersion_parameter_file
    1852           14 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1853          278 :          ELSEIF (dftb_control%dispersion .AND. dftb_control%dispersion_type == dispersion_d3bj) THEN
    1854            2 :             dispersion_env%type = xc_vdw_fun_pairpot
    1855            2 :             dispersion_env%pp_type = vdw_pairpot_dftd3bj
    1856            2 :             dispersion_env%eps_cn = dftb_control%epscn
    1857            2 :             dispersion_env%s6 = dftb_control%sd3bj(1)
    1858            2 :             dispersion_env%a1 = dftb_control%sd3bj(2)
    1859            2 :             dispersion_env%s8 = dftb_control%sd3bj(3)
    1860            2 :             dispersion_env%a2 = dftb_control%sd3bj(4)
    1861            2 :             dispersion_env%domol = .FALSE.
    1862            2 :             dispersion_env%kgc8 = 0._dp
    1863            2 :             dispersion_env%rc_disp = dftb_control%rcdisp
    1864            2 :             dispersion_env%exp_pre = 0._dp
    1865            2 :             dispersion_env%scaling = 0._dp
    1866            2 :             dispersion_env%nd3_exclude_pair = 0
    1867            2 :             dispersion_env%parameter_file_name = dftb_control%dispersion_parameter_file
    1868            2 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1869          276 :          ELSEIF (dftb_control%dispersion .AND. dftb_control%dispersion_type == dispersion_d2) THEN
    1870            2 :             dispersion_env%type = xc_vdw_fun_pairpot
    1871            2 :             dispersion_env%pp_type = vdw_pairpot_dftd2
    1872            2 :             dispersion_env%exp_pre = dftb_control%exp_pre
    1873            2 :             dispersion_env%scaling = dftb_control%scaling
    1874            2 :             dispersion_env%parameter_file_name = dftb_control%dispersion_parameter_file
    1875            2 :             dispersion_env%rc_disp = dftb_control%rcdisp
    1876            2 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1877              :          ELSE
    1878          274 :             dispersion_env%type = xc_vdw_fun_none
    1879              :          END IF
    1880          292 :          CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1881         2134 :       ELSE IF (dft_control%qs_control%method_id == do_method_xtb) THEN
    1882         1134 :          IF (.NOT. (dft_control%qs_control%xtb_control%do_tblite)) THEN
    1883         4910 :             ALLOCATE (dispersion_env)
    1884              :             ! set general defaults
    1885              :             dispersion_env%doabc = .FALSE.
    1886              :             dispersion_env%c9cnst = .FALSE.
    1887              :             dispersion_env%lrc = .FALSE.
    1888              :             dispersion_env%srb = .FALSE.
    1889              :             dispersion_env%verbose = .FALSE.
    1890              :             NULLIFY (dispersion_env%c6ab, dispersion_env%maxci, &
    1891              :                      dispersion_env%r0ab, dispersion_env%rcov, &
    1892              :                      dispersion_env%r2r4, dispersion_env%cn, &
    1893              :                      dispersion_env%cnkind, dispersion_env%cnlist, &
    1894              :                      dispersion_env%d3_exclude_pair)
    1895              :             NULLIFY (dispersion_env%q_mesh, dispersion_env%kernel, dispersion_env%d2phi_dk2, &
    1896              :                      dispersion_env%d2y_dx2, dispersion_env%dftd_section)
    1897              :             NULLIFY (dispersion_env%sab_vdw, dispersion_env%sab_cn)
    1898          982 :             dispersion_env%type = xc_vdw_fun_pairpot
    1899          982 :             dispersion_env%eps_cn = xtb_control%epscn
    1900          982 :             dispersion_env%s6 = xtb_control%s6
    1901          982 :             dispersion_env%s8 = xtb_control%s8
    1902          982 :             dispersion_env%a1 = xtb_control%a1
    1903          982 :             dispersion_env%a2 = xtb_control%a2
    1904          982 :             dispersion_env%domol = .FALSE.
    1905          982 :             dispersion_env%kgc8 = 0._dp
    1906          982 :             dispersion_env%rc_disp = xtb_control%rcdisp
    1907          982 :             dispersion_env%rc_d4 = xtb_control%rcdisp
    1908          982 :             dispersion_env%exp_pre = 0._dp
    1909          982 :             dispersion_env%scaling = 0._dp
    1910          982 :             dispersion_env%nd3_exclude_pair = 0
    1911          982 :             dispersion_env%parameter_file_name = xtb_control%dispersion_parameter_file
    1912              :             !
    1913         1324 :             SELECT CASE (xtb_control%vdw_type)
    1914              :             CASE (xtb_vdw_type_none, xtb_vdw_type_d3)
    1915          342 :                dispersion_env%pp_type = vdw_pairpot_dftd3bj
    1916          342 :                CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1917          342 :                IF (xtb_control%vdw_type == xtb_vdw_type_none) dispersion_env%type = xc_vdw_fun_none
    1918              :             CASE (xtb_vdw_type_d4)
    1919          640 :                dispersion_env%pp_type = vdw_pairpot_dftd4
    1920          640 :                dispersion_env%ref_functional = "none"
    1921              :                CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, &
    1922          640 :                                                dispersion_env, para_env=para_env)
    1923          640 :                dispersion_env%cnfun = 2
    1924              :             CASE DEFAULT
    1925          982 :                CPABORT("vdw type")
    1926              :             END SELECT
    1927          982 :             CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1928              :          END IF
    1929         1000 :       ELSE IF (dft_control%qs_control%semi_empirical) THEN
    1930         5000 :          ALLOCATE (dispersion_env)
    1931              :          ! set general defaults
    1932              :          dispersion_env%doabc = .FALSE.
    1933              :          dispersion_env%c9cnst = .FALSE.
    1934              :          dispersion_env%lrc = .FALSE.
    1935              :          dispersion_env%srb = .FALSE.
    1936              :          dispersion_env%verbose = .FALSE.
    1937              :          NULLIFY (dispersion_env%c6ab, dispersion_env%maxci, dispersion_env%r0ab, dispersion_env%rcov, &
    1938              :                   dispersion_env%r2r4, dispersion_env%cn, dispersion_env%cnkind, dispersion_env%cnlist, &
    1939              :                   dispersion_env%d3_exclude_pair)
    1940              :          NULLIFY (dispersion_env%q_mesh, dispersion_env%kernel, dispersion_env%d2phi_dk2, &
    1941              :                   dispersion_env%d2y_dx2, dispersion_env%dftd_section)
    1942              :          NULLIFY (dispersion_env%sab_vdw, dispersion_env%sab_cn)
    1943         1000 :          IF (se_control%dispersion) THEN
    1944            6 :             dispersion_env%type = xc_vdw_fun_pairpot
    1945            6 :             dispersion_env%pp_type = vdw_pairpot_dftd3
    1946            6 :             dispersion_env%eps_cn = se_control%epscn
    1947            6 :             dispersion_env%s6 = se_control%sd3(1)
    1948            6 :             dispersion_env%sr6 = se_control%sd3(2)
    1949            6 :             dispersion_env%s8 = se_control%sd3(3)
    1950            6 :             dispersion_env%domol = .FALSE.
    1951            6 :             dispersion_env%kgc8 = 0._dp
    1952            6 :             dispersion_env%rc_disp = se_control%rcdisp
    1953            6 :             dispersion_env%exp_pre = 0._dp
    1954            6 :             dispersion_env%scaling = 0._dp
    1955            6 :             dispersion_env%nd3_exclude_pair = 0
    1956            6 :             dispersion_env%parameter_file_name = se_control%dispersion_parameter_file
    1957            6 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1958              :          ELSE
    1959          994 :             dispersion_env%type = xc_vdw_fun_none
    1960              :          END IF
    1961         1000 :          CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1962              :       END IF
    1963              : 
    1964              :       ! Initialize possible geomertical counterpoise correction potential
    1965              :       IF (dft_control%qs_control%method_id == do_method_gpw .OR. &
    1966              :           dft_control%qs_control%method_id == do_method_gapw .OR. &
    1967              :           dft_control%qs_control%method_id == do_method_gapw_xc .OR. &
    1968              :           dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    1969         8430 :           dft_control%qs_control%method_id == do_method_rigpw .OR. &
    1970              :           dft_control%qs_control%method_id == do_method_ofgpw) THEN
    1971         6004 :          ALLOCATE (gcp_env)
    1972         6004 :          NULLIFY (xc_section)
    1973         6004 :          xc_section => section_vals_get_subs_vals(dft_section, "XC")
    1974         6004 :          CALL qs_gcp_env_set(gcp_env, xc_section)
    1975         6004 :          CALL qs_gcp_init(qs_env, gcp_env)
    1976         6004 :          CALL set_qs_env(qs_env, gcp_env=gcp_env)
    1977              :       END IF
    1978              : 
    1979              :       ! Allocate the MO data types
    1980         8430 :       CALL get_qs_kind_set(qs_kind_set, nsgf=n_ao, nelectron=nelectron)
    1981              : 
    1982              :       ! The total number of electrons
    1983         8430 :       nelectron = nelectron - dft_control%charge
    1984              : 
    1985         8430 :       IF (dft_control%multiplicity == 0) THEN
    1986         7020 :          IF (MODULO(nelectron, 2) == 0) THEN
    1987         6535 :             dft_control%multiplicity = 1
    1988              :          ELSE
    1989          485 :             dft_control%multiplicity = 2
    1990              :          END IF
    1991              :       END IF
    1992              : 
    1993         8430 :       multiplicity = dft_control%multiplicity
    1994              : 
    1995         8430 :       IF ((dft_control%nspins < 1) .OR. (dft_control%nspins > 2)) THEN
    1996            0 :          CPABORT("nspins should be 1 or 2 for the time being ...")
    1997              :       END IF
    1998              : 
    1999         8430 :       IF ((MODULO(nelectron, 2) /= 0) .AND. (dft_control%nspins == 1)) THEN
    2000           12 :          IF (.NOT. dft_control%qs_control%ofgpw .AND. .NOT. dft_control%smear) THEN
    2001            0 :             CPABORT("Use the LSD option for an odd number of electrons")
    2002              :          END IF
    2003              :       END IF
    2004              : 
    2005              :       ! The transition potential method to calculate XAS needs LSD
    2006         8430 :       IF (dft_control%do_xas_calculation) THEN
    2007           42 :          IF (dft_control%nspins == 1) THEN
    2008            0 :             CPABORT("Use the LSD option for XAS with transition potential")
    2009              :          END IF
    2010              :       END IF
    2011              : 
    2012              :       ! assigning the number of states per spin initial version, not yet very
    2013              :       ! general. Should work for an even number of electrons and a single
    2014              :       ! additional electron this set of options that requires full matrices,
    2015              :       ! however, makes things a bit ugly right now.... we try to make a
    2016              :       ! distinction between the number of electrons per spin and the number of
    2017              :       ! MOs per spin this should allow the use of fractional occupations later on
    2018         8430 :       IF (dft_control%qs_control%ofgpw) THEN
    2019              : 
    2020            0 :          IF (dft_control%nspins == 1) THEN
    2021            0 :             maxocc = nelectron
    2022            0 :             nelectron_spin(1) = nelectron
    2023            0 :             nelectron_spin(2) = 0
    2024            0 :             n_mo(1) = 1
    2025            0 :             n_mo(2) = 0
    2026              :          ELSE
    2027            0 :             IF (MODULO(nelectron + multiplicity - 1, 2) /= 0) THEN
    2028            0 :                CPABORT("LSD: try to use a different multiplicity")
    2029              :             END IF
    2030            0 :             nelectron_spin(1) = (nelectron + multiplicity - 1)/2
    2031            0 :             nelectron_spin(2) = (nelectron - multiplicity + 1)/2
    2032            0 :             IF (nelectron_spin(1) < 0) THEN
    2033            0 :                CPABORT("LSD: too few electrons for this multiplicity")
    2034              :             END IF
    2035            0 :             maxocc = MAXVAL(nelectron_spin)
    2036            0 :             n_mo(1) = MIN(nelectron_spin(1), 1)
    2037            0 :             n_mo(2) = MIN(nelectron_spin(2), 1)
    2038              :          END IF
    2039              : 
    2040              :       ELSE
    2041              : 
    2042         8430 :          IF (dft_control%nspins == 1) THEN
    2043         6679 :             maxocc = 2.0_dp
    2044         6679 :             nelectron_spin(1) = nelectron
    2045         6679 :             nelectron_spin(2) = 0
    2046         6679 :             IF (MODULO(nelectron, 2) == 0) THEN
    2047         6667 :                n_mo(1) = nelectron/2
    2048              :             ELSE
    2049           12 :                n_mo(1) = INT(nelectron/2._dp) + 1
    2050              :             END IF
    2051         6679 :             n_mo(2) = 0
    2052              :          ELSE
    2053         1751 :             maxocc = 1.0_dp
    2054              : 
    2055              :             ! The simplist spin distribution is written here. Special cases will
    2056              :             ! need additional user input
    2057         1751 :             IF (MODULO(nelectron + multiplicity - 1, 2) /= 0) THEN
    2058            0 :                CPABORT("LSD: try to use a different multiplicity")
    2059              :             END IF
    2060              : 
    2061         1751 :             nelectron_spin(1) = (nelectron + multiplicity - 1)/2
    2062         1751 :             nelectron_spin(2) = (nelectron - multiplicity + 1)/2
    2063              : 
    2064         1751 :             IF (nelectron_spin(2) < 0) THEN
    2065            0 :                CPABORT("LSD: too few electrons for this multiplicity")
    2066              :             END IF
    2067              : 
    2068         1751 :             n_mo(1) = nelectron_spin(1)
    2069         1751 :             n_mo(2) = nelectron_spin(2)
    2070              : 
    2071              :          END IF
    2072              : 
    2073              :       END IF
    2074              : 
    2075              :       ! Read the total_zeff_corr here [SGh]
    2076         8430 :       CALL get_qs_kind_set(qs_kind_set, total_zeff_corr=total_zeff_corr)
    2077              :       ! store it in qs_env
    2078         8430 :       qs_env%total_zeff_corr = total_zeff_corr
    2079              : 
    2080              :       ! Store the number of electrons once and for all
    2081              :       CALL qs_subsys_set(subsys, &
    2082              :                          nelectron_total=nelectron, &
    2083         8430 :                          nelectron_spin=nelectron_spin)
    2084              : 
    2085              :       ! Ensure that all orbitals requested for printout are added even
    2086              :       ! if the keyword ADDED_MOS was not specified or set properly
    2087         8430 :       mo_index_range => section_get_ivals(dft_section, "PRINT%MO%MO_INDEX_RANGE")
    2088         8430 :       CPASSERT(ASSOCIATED(mo_index_range))
    2089         8466 :       IF (ALL(mo_index_range > 0)) THEN
    2090           18 :          IF (mo_index_range(1) > mo_index_range(2)) THEN
    2091              :             CALL cp_abort(__LOCATION__, &
    2092              :                           "The upper orbital index ("// &
    2093              :                           TRIM(ADJUSTL(cp_to_string(mo_index_range(2))))// &
    2094              :                           ") of the MO_INDEX_RANGE should be equal or larger "// &
    2095              :                           "than the lower orbital index ("// &
    2096              :                           TRIM(ADJUSTL(cp_to_string(mo_index_range(1))))// &
    2097            0 :                           ") for printout.")
    2098              :          END IF
    2099              :          ! Adapt ADDED_MOS automatically if needed for printout
    2100           18 :          IF (.NOT. scf_control%use_ot) THEN
    2101              :             scf_control%added_mos(1) = MIN(MAX(scf_control%added_mos(1), &
    2102              :                                                mo_index_range(2) - n_mo(1)), &
    2103           12 :                                            n_ao - n_mo(1))
    2104           12 :             IF (dft_control%nspins == 2) THEN
    2105              :                scf_control%added_mos(2) = MIN(MAX(scf_control%added_mos(2), &
    2106              :                                                   mo_index_range(2) - n_mo(2)), &
    2107            8 :                                               n_ao - n_mo(2))
    2108              :             END IF
    2109              :          END IF
    2110         8412 :       ELSE IF (mo_index_range(2) < 0) THEN
    2111            0 :          IF (.NOT. scf_control%use_ot) THEN
    2112              :             ! Add all available orbitals
    2113            0 :             scf_control%added_mos(1) = n_ao - n_mo(1)
    2114            0 :             IF (dft_control%nspins == 2) THEN
    2115              :                ! Ensure the same number for the spin-down (beta) orbitals
    2116            0 :                scf_control%added_mos(2) = n_ao - n_mo(2)
    2117              :             END IF
    2118              :          END IF
    2119              :       END IF
    2120              : 
    2121         8430 :       nlumo_dos = section_get_ival(dft_section, "PRINT%DOS%NLUMO")
    2122         8430 :       nlumo_molden = section_get_ival(dft_section, "PRINT%MO_MOLDEN%NLUMO")
    2123         8430 :       nlumo_required = MAX(nlumo_dos, nlumo_molden)
    2124         8430 :       IF (nlumo_dos == -1 .OR. nlumo_molden == -1) nlumo_required = -1
    2125         8430 :       IF (.NOT. scf_control%use_ot .AND. nlumo_required /= 0) THEN
    2126           10 :          IF (nlumo_required == -1) THEN
    2127            4 :             IF (scf_control%added_mos(1) /= -1 .OR. &
    2128              :                 (dft_control%nspins == 2 .AND. scf_control%added_mos(2) /= -1)) THEN
    2129              :                CALL cp_warn(__LOCATION__, &
    2130              :                             "NLUMO requested by DOS/PDOS/Molden exceeds SCF%ADDED_MOS. "// &
    2131              :                             "For diagonalization calculations, SCF%ADDED_MOS is "// &
    2132            2 :                             "increased to provide the requested unoccupied orbitals.")
    2133              :             END IF
    2134            4 :             scf_control%added_mos(1) = -1
    2135            4 :             IF (dft_control%nspins == 2) scf_control%added_mos(2) = -1
    2136              :          ELSE
    2137            6 :             IF (scf_control%added_mos(1) >= 0 .AND. &
    2138              :                 nlumo_required > scf_control%added_mos(1)) THEN
    2139              :                CALL cp_warn(__LOCATION__, &
    2140              :                             "NLUMO requested by DOS/PDOS/Molden exceeds SCF%ADDED_MOS. "// &
    2141              :                             "For diagonalization calculations, SCF%ADDED_MOS is "// &
    2142            6 :                             "increased to provide the requested unoccupied orbitals.")
    2143            6 :                scf_control%added_mos(1) = nlumo_required
    2144              :             END IF
    2145            6 :             IF (dft_control%nspins == 2 .AND. scf_control%added_mos(2) > 0 .AND. &
    2146              :                 nlumo_required > scf_control%added_mos(2)) THEN
    2147            0 :                scf_control%added_mos(2) = nlumo_required
    2148              :             END IF
    2149              :          END IF
    2150              :       END IF
    2151              : 
    2152         8430 :       IF (dft_control%nspins == 2) THEN
    2153              :          ! Check and set number of added (unoccupied) orbitals for beta spin
    2154         1751 :          IF (scf_control%added_mos(2) < 0) THEN
    2155          140 :             n_mo_add = n_ao - n_mo(2)  ! use all available MOs
    2156         1611 :          ELSE IF (scf_control%added_mos(2) > 0) THEN
    2157              :             n_mo_add = scf_control%added_mos(2)
    2158              :          ELSE
    2159         1447 :             n_mo_add = scf_control%added_mos(1)
    2160              :          END IF
    2161         1751 :          IF (n_mo_add > n_ao - n_mo(2)) THEN
    2162           20 :             CPWARN("More ADDED_MOs requested for beta spin than available.")
    2163              :          END IF
    2164         1751 :          scf_control%added_mos(2) = MIN(n_mo_add, n_ao - n_mo(2))
    2165         1751 :          n_mo(2) = n_mo(2) + scf_control%added_mos(2)
    2166              :       END IF
    2167              : 
    2168              :       ! proceed alpha orbitals after the beta orbitals; this is essential to avoid
    2169              :       ! reduction in the number of available unoccupied molecular orbitals.
    2170              :       ! E.g. n_ao = 10, nelectrons = 10, multiplicity = 3 implies n_mo(1) = 6, n_mo(2) = 4;
    2171              :       ! added_mos(1:2) = (6,undef) should increase the number of molecular orbitals as
    2172              :       ! n_mo(1) = min(n_ao, n_mo(1) + added_mos(1)) = 10, n_mo(2) = 10.
    2173              :       ! However, if we try to proceed alpha orbitals first, this leads us n_mo(1:2) = (10,8)
    2174              :       ! due to the following assignment instruction above:
    2175              :       !   IF (scf_control%added_mos(2) > 0) THEN ... ELSE; n_mo_add = scf_control%added_mos(1); END IF
    2176         8430 :       IF (dft_control%qs_control%xtb_control%do_tblite .AND. .NOT. scf_control%use_ot) THEN
    2177          148 :          scf_control%added_mos(1) = n_ao - n_mo(1)  ! tblite needs all MO's
    2178         8282 :       ELSE IF (scf_control%added_mos(1) < 0) THEN
    2179          692 :          scf_control%added_mos(1) = n_ao - n_mo(1)  ! use all available MOs
    2180         7590 :       ELSE IF (scf_control%added_mos(1) > n_ao - n_mo(1)) THEN
    2181              :          CALL cp_warn(__LOCATION__, &
    2182              :                       "More added MOs requested than available. "// &
    2183              :                       "The full set of unoccupied MOs will be used. "// &
    2184              :                       "Use 'ADDED_MOS -1' to always use all available MOs "// &
    2185          118 :                       "and to get rid of this warning.")
    2186              :       END IF
    2187         8430 :       scf_control%added_mos(1) = MIN(scf_control%added_mos(1), n_ao - n_mo(1))
    2188         8430 :       n_mo(1) = n_mo(1) + scf_control%added_mos(1)
    2189              : 
    2190         8430 :       IF (dft_control%nspins == 2) THEN
    2191         1751 :          IF (n_mo(2) > n_mo(1)) &
    2192              :             CALL cp_warn(__LOCATION__, &
    2193              :                          "More beta than alpha MOs requested. "// &
    2194            0 :                          "The number of beta MOs will be reduced to the number alpha MOs.")
    2195         1751 :          n_mo(2) = MIN(n_mo(1), n_mo(2))
    2196         1751 :          CPASSERT(n_mo(1) >= nelectron_spin(1))
    2197         1751 :          CPASSERT(n_mo(2) >= nelectron_spin(2))
    2198              :       END IF
    2199              : 
    2200              :       ! kpoints
    2201         8430 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
    2202         8430 :       IF (do_kpoints .AND. dft_control%nspins == 2) THEN
    2203              :          ! we need equal number of calculated states
    2204           34 :          IF (n_mo(2) /= n_mo(1)) &
    2205              :             CALL cp_warn(__LOCATION__, &
    2206              :                          "Kpoints: Different number of MOs requested. "// &
    2207           12 :                          "The number of beta MOs will be set to the number alpha MOs.")
    2208           34 :          n_mo(2) = n_mo(1)
    2209           34 :          CPASSERT(n_mo(1) >= nelectron_spin(1))
    2210           34 :          CPASSERT(n_mo(2) >= nelectron_spin(2))
    2211              :       END IF
    2212              : 
    2213              :       ! Compatibility checks for smearing
    2214         8430 :       IF (scf_control%smear%do_smear) THEN
    2215         1074 :          IF (dft_control%qs_control%xtb_control%do_tblite .AND. scf_control%use_ot) THEN
    2216            0 :             CPABORT("CP2K/tblite with OT does not support smearing.")
    2217              :          END IF
    2218         1074 :          IF (scf_control%added_mos(1) == 0) THEN
    2219            0 :             CPABORT("Extra MOs (ADDED_MOS) are required for smearing")
    2220              :          END IF
    2221              :       END IF
    2222              : 
    2223              :       ! Some options require that all MOs are computed ...
    2224              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
    2225              :                                            "PRINT%MO/CARTESIAN"), &
    2226              :                 cp_p_file) .OR. &
    2227              :           (scf_control%level_shift /= 0.0_dp) .OR. &
    2228         8430 :           (scf_control%diagonalization%eps_jacobi /= 0.0_dp) .OR. &
    2229              :           (dft_control%roks .AND. (.NOT. scf_control%use_ot))) THEN
    2230         8594 :          n_mo(:) = n_ao
    2231              :       END IF
    2232              : 
    2233              :       ! Compatibility checks for ROKS
    2234         8430 :       IF (dft_control%roks .AND. (.NOT. scf_control%use_ot)) THEN
    2235           44 :          IF (scf_control%roks_scheme == general_roks) THEN
    2236            0 :             CPWARN("General ROKS scheme is not yet tested!")
    2237              :          END IF
    2238           44 :          IF (scf_control%smear%do_smear) THEN
    2239              :             CALL cp_abort(__LOCATION__, &
    2240              :                           "The options ROKS and SMEAR are not compatible. "// &
    2241            0 :                           "Try UKS instead of ROKS")
    2242              :          END IF
    2243              :       END IF
    2244         8430 :       IF (dft_control%low_spin_roks) THEN
    2245            8 :          SELECT CASE (dft_control%qs_control%method_id)
    2246              :          CASE DEFAULT
    2247              :          CASE (do_method_xtb, do_method_dftb)
    2248              :             CALL cp_abort(__LOCATION__, &
    2249            0 :                           "xTB/DFTB methods are not compatible with low spin ROKS.")
    2250              :          CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pm3, &
    2251              :                do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
    2252              :             CALL cp_abort(__LOCATION__, &
    2253            8 :                           "SE methods are not compatible with low spin ROKS.")
    2254              :          END SELECT
    2255              :       END IF
    2256              : 
    2257              :       ! in principle the restricted calculation could be performed
    2258              :       ! using just one set of MOs and special casing most of the code
    2259              :       ! right now we'll just take care of what is effectively an additional constraint
    2260              :       ! at as few places as possible, just duplicating the beta orbitals
    2261         8430 :       IF (dft_control%restricted .AND. (output_unit > 0)) THEN
    2262              :          ! it is really not yet tested till the end ! Joost
    2263           23 :          WRITE (output_unit, *) ""
    2264           23 :          WRITE (output_unit, *) " **************************************"
    2265           23 :          WRITE (output_unit, *) " restricted calculation cutting corners"
    2266           23 :          WRITE (output_unit, *) " experimental feature, check code      "
    2267           23 :          WRITE (output_unit, *) " **************************************"
    2268              :       END IF
    2269              : 
    2270              :       ! no point in allocating these things here ?
    2271         8430 :       IF (dft_control%qs_control%do_ls_scf) THEN
    2272          388 :          NULLIFY (mos)
    2273              :       ELSE
    2274        33897 :          ALLOCATE (mos(dft_control%nspins))
    2275        17813 :          DO ispin = 1, dft_control%nspins
    2276              :             CALL allocate_mo_set(mo_set=mos(ispin), &
    2277              :                                  nao=n_ao, &
    2278              :                                  nmo=n_mo(ispin), &
    2279              :                                  nelectron=nelectron_spin(ispin), &
    2280              :                                  n_el_f=REAL(nelectron_spin(ispin), dp), &
    2281              :                                  maxocc=maxocc, &
    2282        17813 :                                  flexible_electron_count=dft_control%relax_multiplicity)
    2283              :          END DO
    2284              :       END IF
    2285              : 
    2286         8430 :       CALL set_qs_env(qs_env, mos=mos)
    2287              : 
    2288              :       ! allocate mos when switch_surf_dip is triggered [SGh]
    2289         8430 :       IF (dft_control%switch_surf_dip) THEN
    2290            8 :          ALLOCATE (mos_last_converged(dft_control%nspins))
    2291            4 :          DO ispin = 1, dft_control%nspins
    2292              :             CALL allocate_mo_set(mo_set=mos_last_converged(ispin), &
    2293              :                                  nao=n_ao, &
    2294              :                                  nmo=n_mo(ispin), &
    2295              :                                  nelectron=nelectron_spin(ispin), &
    2296              :                                  n_el_f=REAL(nelectron_spin(ispin), dp), &
    2297              :                                  maxocc=maxocc, &
    2298            4 :                                  flexible_electron_count=dft_control%relax_multiplicity)
    2299              :          END DO
    2300            2 :          CALL set_qs_env(qs_env, mos_last_converged=mos_last_converged)
    2301              :       END IF
    2302              : 
    2303         8430 :       IF (.NOT. be_silent) THEN
    2304              :          ! Print the DFT control parameters
    2305         8424 :          CALL write_dft_control(dft_control, dft_section)
    2306              : 
    2307              :          ! Print the vdW control parameters
    2308              :          IF (dft_control%qs_control%method_id == do_method_gpw .OR. &
    2309              :              dft_control%qs_control%method_id == do_method_gapw .OR. &
    2310              :              dft_control%qs_control%method_id == do_method_gapw_xc .OR. &
    2311              :              dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    2312              :              dft_control%qs_control%method_id == do_method_rigpw .OR. &
    2313              :              dft_control%qs_control%method_id == do_method_dftb .OR. &
    2314              :              (dft_control%qs_control%method_id == do_method_xtb .AND. &
    2315         8424 :               (.NOT. dft_control%qs_control%xtb_control%do_tblite)) .OR. &
    2316              :              dft_control%qs_control%method_id == do_method_ofgpw) THEN
    2317         7272 :             CALL get_qs_env(qs_env, dispersion_env=dispersion_env)
    2318         7272 :             CALL qs_write_dispersion(qs_env, dispersion_env)
    2319              :          END IF
    2320              : 
    2321              :          ! Print the Quickstep control parameters
    2322         8424 :          CALL write_qs_control(dft_control%qs_control, dft_section)
    2323              : 
    2324              :          ! Print the ADMM control parameters
    2325         8424 :          IF (dft_control%do_admm) THEN
    2326          520 :             CALL write_admm_control(dft_control%admm_control, dft_section)
    2327              :          END IF
    2328              : 
    2329              :          ! Print XES/XAS control parameters
    2330         8424 :          IF (dft_control%do_xas_calculation) THEN
    2331           42 :             CALL cite_reference(Iannuzzi2007)
    2332              :             !CALL write_xas_control(dft_control%xas_control,dft_section)
    2333              :          END IF
    2334              : 
    2335              :          ! Print the unnormalized basis set information (input data)
    2336         8424 :          CALL write_gto_basis_sets(qs_kind_set, subsys_section)
    2337              : 
    2338              :          ! Print the atomic kind set
    2339         8424 :          CALL write_qs_kind_set(qs_kind_set, subsys_section)
    2340              : 
    2341              :          ! Print the molecule kind set
    2342         8424 :          CALL write_molecule_kind_set(molecule_kind_set, subsys_section)
    2343              : 
    2344              :          ! Print the total number of kinds, atoms, basis functions etc.
    2345         8424 :          CALL write_total_numbers(qs_kind_set, particle_set, qs_env%input)
    2346              : 
    2347              :          ! Print the atomic coordinates
    2348         8424 :          CALL write_qs_particle_coordinates(particle_set, qs_kind_set, subsys_section, label="QUICKSTEP")
    2349              : 
    2350              :          ! Print the interatomic distances
    2351         8424 :          CALL write_particle_distances(particle_set, cell, subsys_section)
    2352              : 
    2353              :          ! Print the requested structure data
    2354         8424 :          CALL write_structure_data(particle_set, cell, subsys_section)
    2355              : 
    2356              :          ! Print symmetry information
    2357         8424 :          CALL write_symmetry(particle_set, cell, subsys_section)
    2358              : 
    2359              :          ! Print the SCF parameters
    2360         8424 :          IF ((.NOT. dft_control%qs_control%do_ls_scf) .AND. &
    2361              :              (.NOT. dft_control%qs_control%do_almo_scf)) THEN
    2362         7970 :             CALL scf_c_write_parameters(scf_control, dft_section)
    2363              :          END IF
    2364              :       END IF
    2365              : 
    2366              :       ! Sets up pw_env, qs_charges, mpools ...
    2367         8430 :       CALL qs_env_setup(qs_env)
    2368              : 
    2369              :       ! Allocate and initialise rho0 soft on the global grid
    2370         8430 :       IF (dft_control%qs_control%method_id == do_method_gapw) THEN
    2371         1110 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, rho0_mpole=rho0_mpole)
    2372         1110 :          CALL rho0_s_grid_create(pw_env, rho0_mpole)
    2373              :       END IF
    2374              : 
    2375         8430 :       IF (output_unit > 0) CALL m_flush(output_unit)
    2376         8430 :       CALL timestop(handle)
    2377              : 
    2378        92730 :    END SUBROUTINE qs_init_subsys
    2379              : 
    2380              : ! **************************************************************************************************
    2381              : !> \brief Write the total number of kinds, atoms, etc. to the logical unit
    2382              : !>      number lunit.
    2383              : !> \param qs_kind_set ...
    2384              : !> \param particle_set ...
    2385              : !> \param force_env_section ...
    2386              : !> \author Creation (06.10.2000)
    2387              : ! **************************************************************************************************
    2388         8424 :    SUBROUTINE write_total_numbers(qs_kind_set, particle_set, force_env_section)
    2389              : 
    2390              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2391              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2392              :       TYPE(section_vals_type), POINTER                   :: force_env_section
    2393              : 
    2394              :       INTEGER                                            :: maxlgto, maxlppl, maxlppnl, natom, &
    2395              :                                                             natom_q, ncgf, nkind, nkind_q, npgf, &
    2396              :                                                             nset, nsgf, nshell, output_unit
    2397              :       TYPE(cp_logger_type), POINTER                      :: logger
    2398              : 
    2399         8424 :       NULLIFY (logger)
    2400         8424 :       logger => cp_get_default_logger()
    2401              :       output_unit = cp_print_key_unit_nr(logger, force_env_section, "PRINT%TOTAL_NUMBERS", &
    2402         8424 :                                          extension=".Log")
    2403              : 
    2404         8424 :       IF (output_unit > 0) THEN
    2405         4231 :          natom = SIZE(particle_set)
    2406         4231 :          nkind = SIZE(qs_kind_set)
    2407              : 
    2408              :          CALL get_qs_kind_set(qs_kind_set, &
    2409              :                               maxlgto=maxlgto, &
    2410              :                               ncgf=ncgf, &
    2411              :                               npgf=npgf, &
    2412              :                               nset=nset, &
    2413              :                               nsgf=nsgf, &
    2414              :                               nshell=nshell, &
    2415              :                               maxlppl=maxlppl, &
    2416         4231 :                               maxlppnl=maxlppnl)
    2417              : 
    2418              :          WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
    2419         4231 :             "TOTAL NUMBERS AND MAXIMUM NUMBERS"
    2420              : 
    2421         4231 :          IF (nset + npgf + ncgf > 0) THEN
    2422              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T71,I10))") &
    2423         4231 :                "Total number of", &
    2424         4231 :                "- Atomic kinds:                  ", nkind, &
    2425         4231 :                "- Atoms:                         ", natom, &
    2426         4231 :                "- Shell sets:                    ", nset, &
    2427         4231 :                "- Shells:                        ", nshell, &
    2428         4231 :                "- Primitive Cartesian functions: ", npgf, &
    2429         4231 :                "- Cartesian basis functions:     ", ncgf, &
    2430         8462 :                "- Spherical basis functions:     ", nsgf
    2431            0 :          ELSE IF (nshell + nsgf > 0) THEN
    2432              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T71,I10))") &
    2433            0 :                "Total number of", &
    2434            0 :                "- Atomic kinds:                  ", nkind, &
    2435            0 :                "- Atoms:                         ", natom, &
    2436            0 :                "- Shells:                        ", nshell, &
    2437            0 :                "- Spherical basis functions:     ", nsgf
    2438              :          ELSE
    2439              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T71,I10))") &
    2440            0 :                "Total number of", &
    2441            0 :                "- Atomic kinds:                  ", nkind, &
    2442            0 :                "- Atoms:                         ", natom
    2443              :          END IF
    2444              : 
    2445         4231 :          IF ((maxlppl > -1) .AND. (maxlppnl > -1)) THEN
    2446              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T75,I6))") &
    2447         2139 :                "Maximum angular momentum of the", &
    2448         2139 :                "- Orbital basis functions:                   ", maxlgto, &
    2449         2139 :                "- Local part of the GTH pseudopotential:     ", maxlppl, &
    2450         4278 :                "- Non-local part of the GTH pseudopotential: ", maxlppnl
    2451         2092 :          ELSEIF (maxlppl > -1) THEN
    2452              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T75,I6))") &
    2453          539 :                "Maximum angular momentum of the", &
    2454          539 :                "- Orbital basis functions:                   ", maxlgto, &
    2455         1078 :                "- Local part of the GTH pseudopotential:     ", maxlppl
    2456              :          ELSE
    2457              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,T75,I6)") &
    2458         1553 :                "Maximum angular momentum of the orbital basis functions: ", maxlgto
    2459              :          END IF
    2460              : 
    2461              :          ! LRI_AUX BASIS
    2462              :          CALL get_qs_kind_set(qs_kind_set, &
    2463              :                               maxlgto=maxlgto, &
    2464              :                               ncgf=ncgf, &
    2465              :                               npgf=npgf, &
    2466              :                               nset=nset, &
    2467              :                               nsgf=nsgf, &
    2468              :                               nshell=nshell, &
    2469         4231 :                               basis_type="LRI_AUX")
    2470         4231 :          IF (nset + npgf + ncgf > 0) THEN
    2471              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    2472          175 :                "LRI_AUX Basis: ", &
    2473          175 :                "Total number of", &
    2474          175 :                "- Shell sets:                    ", nset, &
    2475          175 :                "- Shells:                        ", nshell, &
    2476          175 :                "- Primitive Cartesian functions: ", npgf, &
    2477          175 :                "- Cartesian basis functions:     ", ncgf, &
    2478          350 :                "- Spherical basis functions:     ", nsgf
    2479              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    2480          175 :                "  Maximum angular momentum ", maxlgto
    2481              :          END IF
    2482              : 
    2483              :          ! RI_HXC BASIS
    2484              :          CALL get_qs_kind_set(qs_kind_set, &
    2485              :                               maxlgto=maxlgto, &
    2486              :                               ncgf=ncgf, &
    2487              :                               npgf=npgf, &
    2488              :                               nset=nset, &
    2489              :                               nsgf=nsgf, &
    2490              :                               nshell=nshell, &
    2491         4231 :                               basis_type="RI_HXC")
    2492         4231 :          IF (nset + npgf + ncgf > 0) THEN
    2493              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    2494          147 :                "RI_HXC Basis: ", &
    2495          147 :                "Total number of", &
    2496          147 :                "- Shell sets:                    ", nset, &
    2497          147 :                "- Shells:                        ", nshell, &
    2498          147 :                "- Primitive Cartesian functions: ", npgf, &
    2499          147 :                "- Cartesian basis functions:     ", ncgf, &
    2500          294 :                "- Spherical basis functions:     ", nsgf
    2501              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    2502          147 :                "  Maximum angular momentum ", maxlgto
    2503              :          END IF
    2504              : 
    2505              :          ! AUX_FIT BASIS
    2506              :          CALL get_qs_kind_set(qs_kind_set, &
    2507              :                               maxlgto=maxlgto, &
    2508              :                               ncgf=ncgf, &
    2509              :                               npgf=npgf, &
    2510              :                               nset=nset, &
    2511              :                               nsgf=nsgf, &
    2512              :                               nshell=nshell, &
    2513         4231 :                               basis_type="AUX_FIT")
    2514         4231 :          IF (nset + npgf + ncgf > 0) THEN
    2515              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    2516          407 :                "AUX_FIT ADMM-Basis: ", &
    2517          407 :                "Total number of", &
    2518          407 :                "- Shell sets:                    ", nset, &
    2519          407 :                "- Shells:                        ", nshell, &
    2520          407 :                "- Primitive Cartesian functions: ", npgf, &
    2521          407 :                "- Cartesian basis functions:     ", ncgf, &
    2522          814 :                "- Spherical basis functions:     ", nsgf
    2523              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    2524          407 :                "  Maximum angular momentum ", maxlgto
    2525              :          END IF
    2526              : 
    2527              :          ! NUCLEAR BASIS
    2528              :          CALL get_qs_kind_set(qs_kind_set, &
    2529              :                               nkind_q=nkind_q, &
    2530              :                               natom_q=natom_q, &
    2531              :                               maxlgto=maxlgto, &
    2532              :                               ncgf=ncgf, &
    2533              :                               npgf=npgf, &
    2534              :                               nset=nset, &
    2535              :                               nsgf=nsgf, &
    2536              :                               nshell=nshell, &
    2537         4231 :                               basis_type="NUC")
    2538         4231 :          IF (nset + npgf + ncgf > 0) THEN
    2539              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    2540          150 :                "Nuclear Basis: ", &
    2541          150 :                "Total number of", &
    2542          150 :                "- Quantum atomic kinds:          ", nkind_q, &
    2543          150 :                "- Quantum atoms:                 ", natom_q, &
    2544          150 :                "- Shell sets:                    ", nset, &
    2545          150 :                "- Shells:                        ", nshell, &
    2546          150 :                "- Primitive Cartesian functions: ", npgf, &
    2547          150 :                "- Cartesian basis functions:     ", ncgf, &
    2548          300 :                "- Spherical basis functions:     ", nsgf
    2549              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    2550          150 :                "  Maximum angular momentum ", maxlgto
    2551              :          END IF
    2552              : 
    2553              :       END IF
    2554              :       CALL cp_print_key_finished_output(output_unit, logger, force_env_section, &
    2555         8424 :                                         "PRINT%TOTAL_NUMBERS")
    2556              : 
    2557         8424 :    END SUBROUTINE write_total_numbers
    2558              : 
    2559              : END MODULE qs_environment
        

Generated by: LCOV version 2.0-1