LCOV - code coverage report
Current view: top level - src - qs_environment.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 92.2 % 1215 1120
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 8 8

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

Generated by: LCOV version 2.0-1