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

Generated by: LCOV version 2.0-1