LCOV - code coverage report
Current view: top level - src - qs_environment.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 91.1 % 880 802
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 3 3

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 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              :    USE cp_output_handling,              ONLY: cp_p_file,&
      53              :                                               cp_print_key_finished_output,&
      54              :                                               cp_print_key_should_output,&
      55              :                                               cp_print_key_unit_nr
      56              :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      57              :    USE cp_symmetry,                     ONLY: write_symmetry
      58              :    USE distribution_1d_types,           ONLY: distribution_1d_release,&
      59              :                                               distribution_1d_type
      60              :    USE distribution_methods,            ONLY: distribute_molecules_1d
      61              :    USE ec_env_types,                    ONLY: energy_correction_type
      62              :    USE ec_environment,                  ONLY: ec_env_create,&
      63              :                                               ec_write_input
      64              :    USE et_coupling_types,               ONLY: et_coupling_create
      65              :    USE ewald_environment_types,         ONLY: ewald_env_create,&
      66              :                                               ewald_env_get,&
      67              :                                               ewald_env_set,&
      68              :                                               ewald_environment_type,&
      69              :                                               read_ewald_section,&
      70              :                                               read_ewald_section_tb
      71              :    USE ewald_pw_methods,                ONLY: ewald_pw_grid_update
      72              :    USE ewald_pw_types,                  ONLY: ewald_pw_create,&
      73              :                                               ewald_pw_type
      74              :    USE exstates_types,                  ONLY: excited_energy_type,&
      75              :                                               exstate_create
      76              :    USE external_potential_types,        ONLY: get_potential,&
      77              :                                               init_potential,&
      78              :                                               set_potential
      79              :    USE fist_nonbond_env_types,          ONLY: fist_nonbond_env_create,&
      80              :                                               fist_nonbond_env_type
      81              :    USE gamma,                           ONLY: init_md_ftable
      82              :    USE global_types,                    ONLY: global_environment_type
      83              :    USE hartree_local_methods,           ONLY: init_coulomb_local
      84              :    USE header,                          ONLY: dftb_header,&
      85              :                                               qs_header,&
      86              :                                               se_header,&
      87              :                                               tblite_header,&
      88              :                                               xtb_header
      89              :    USE hfx_types,                       ONLY: compare_hfx_sections,&
      90              :                                               hfx_create
      91              :    USE input_constants,                 ONLY: &
      92              :         dispersion_d2, dispersion_d3, dispersion_d3bj, do_et_ddapc, do_method_am1, do_method_dftb, &
      93              :         do_method_gapw, do_method_gapw_xc, do_method_gpw, do_method_lrigpw, do_method_mndo, &
      94              :         do_method_mndod, do_method_ofgpw, do_method_pdg, do_method_pm3, do_method_pm6, &
      95              :         do_method_pm6fm, do_method_pnnl, do_method_rigpw, do_method_rm1, do_method_xtb, &
      96              :         do_qmmm_gauss, do_qmmm_swave, general_roks, hden_atomic, kg_tnadd_embed_ri, rel_none, &
      97              :         rel_trans_atom, vdw_pairpot_dftd2, vdw_pairpot_dftd3, vdw_pairpot_dftd3bj, &
      98              :         vdw_pairpot_dftd4, wfi_aspc_nr, wfi_linear_ps_method_nr, wfi_linear_wf_method_nr, &
      99              :         wfi_ps_method_nr, wfi_use_guess_method_nr, xc_vdw_fun_none, xc_vdw_fun_nonloc, &
     100              :         xc_vdw_fun_pairpot, xtb_vdw_type_d3, xtb_vdw_type_d4, xtb_vdw_type_none
     101              :    USE input_section_types,             ONLY: section_vals_get,&
     102              :                                               section_vals_get_subs_vals,&
     103              :                                               section_vals_type,&
     104              :                                               section_vals_val_get
     105              :    USE kg_environment,                  ONLY: kg_env_create
     106              :    USE kinds,                           ONLY: default_string_length,&
     107              :                                               dp
     108              :    USE kpoint_methods,                  ONLY: kpoint_env_initialize,&
     109              :                                               kpoint_initialize,&
     110              :                                               kpoint_initialize_mos
     111              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
     112              :                                               kpoint_create,&
     113              :                                               kpoint_type,&
     114              :                                               read_kpoint_section,&
     115              :                                               write_kpoint_info
     116              :    USE lri_environment_init,            ONLY: lri_env_basis,&
     117              :                                               lri_env_init
     118              :    USE lri_environment_types,           ONLY: lri_environment_type
     119              :    USE machine,                         ONLY: m_flush
     120              :    USE mathconstants,                   ONLY: pi
     121              :    USE message_passing,                 ONLY: mp_para_env_type
     122              :    USE molecule_kind_types,             ONLY: molecule_kind_type,&
     123              :                                               write_molecule_kind_set
     124              :    USE molecule_types,                  ONLY: molecule_type
     125              :    USE mp2_setup,                       ONLY: read_mp2_section
     126              :    USE mp2_types,                       ONLY: mp2_env_create,&
     127              :                                               mp2_type
     128              :    USE multipole_types,                 ONLY: do_multipole_none
     129              :    USE orbital_pointers,                ONLY: init_orbital_pointers
     130              :    USE orbital_transformation_matrices, ONLY: init_spherical_harmonics
     131              :    USE particle_methods,                ONLY: write_particle_distances,&
     132              :                                               write_qs_particle_coordinates,&
     133              :                                               write_structure_data
     134              :    USE particle_types,                  ONLY: particle_type
     135              :    USE pw_env_types,                    ONLY: pw_env_type
     136              :    USE qmmm_types_low,                  ONLY: qmmm_env_qm_type
     137              :    USE qs_basis_rotation_methods,       ONLY: qs_basis_rotation
     138              :    USE qs_dftb_parameters,              ONLY: qs_dftb_param_init
     139              :    USE qs_dftb_types,                   ONLY: qs_dftb_pairpot_type
     140              :    USE qs_dispersion_nonloc,            ONLY: qs_dispersion_nonloc_init
     141              :    USE qs_dispersion_pairpot,           ONLY: qs_dispersion_pairpot_init
     142              :    USE qs_dispersion_types,             ONLY: qs_dispersion_type
     143              :    USE qs_dispersion_utils,             ONLY: qs_dispersion_env_set,&
     144              :                                               qs_write_dispersion
     145              :    USE qs_energy_types,                 ONLY: allocate_qs_energy,&
     146              :                                               qs_energy_type
     147              :    USE qs_environment_methods,          ONLY: qs_env_setup
     148              :    USE qs_environment_types,            ONLY: get_qs_env,&
     149              :                                               qs_environment_type,&
     150              :                                               set_qs_env
     151              :    USE qs_force_types,                  ONLY: qs_force_type
     152              :    USE qs_gcp_types,                    ONLY: qs_gcp_type
     153              :    USE qs_gcp_utils,                    ONLY: qs_gcp_env_set,&
     154              :                                               qs_gcp_init
     155              :    USE qs_harris_types,                 ONLY: harris_rhoin_init,&
     156              :                                               harris_type
     157              :    USE qs_harris_utils,                 ONLY: harris_env_create,&
     158              :                                               harris_write_input
     159              :    USE qs_interactions,                 ONLY: init_interaction_radii,&
     160              :                                               init_se_nlradius,&
     161              :                                               write_core_charge_radii,&
     162              :                                               write_paw_radii,&
     163              :                                               write_pgf_orb_radii,&
     164              :                                               write_ppl_radii,&
     165              :                                               write_ppnl_radii
     166              :    USE qs_kind_types,                   ONLY: &
     167              :         check_qs_kind_set, get_qs_kind, get_qs_kind_set, init_cneo_basis_set, init_gapw_basis_set, &
     168              :         init_gapw_nlcc, init_qs_kind_set, qs_kind_type, set_qs_kind, write_gto_basis_sets, &
     169              :         write_qs_kind_set
     170              :    USE qs_ks_types,                     ONLY: qs_ks_env_create,&
     171              :                                               qs_ks_env_type,&
     172              :                                               set_ks_env
     173              :    USE qs_local_rho_types,              ONLY: local_rho_type
     174              :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
     175              :                                               mo_set_type
     176              :    USE qs_rho0_ggrid,                   ONLY: rho0_s_grid_create
     177              :    USE qs_rho0_methods,                 ONLY: init_rho0
     178              :    USE qs_rho0_types,                   ONLY: rho0_mpole_type
     179              :    USE qs_rho_atom_methods,             ONLY: init_rho_atom
     180              :    USE qs_rho_atom_types,               ONLY: rho_atom_type
     181              :    USE qs_subsys_methods,               ONLY: qs_subsys_create
     182              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
     183              :                                               qs_subsys_set,&
     184              :                                               qs_subsys_type
     185              :    USE qs_wf_history_methods,           ONLY: wfi_create,&
     186              :                                               wfi_create_for_kp
     187              :    USE qs_wf_history_types,             ONLY: qs_wf_history_type,&
     188              :                                               wfi_release
     189              :    USE rel_control_types,               ONLY: rel_c_create,&
     190              :                                               rel_c_read_parameters,&
     191              :                                               rel_control_type
     192              :    USE scf_control_types,               ONLY: scf_c_create,&
     193              :                                               scf_c_read_parameters,&
     194              :                                               scf_c_write_parameters,&
     195              :                                               scf_control_type
     196              :    USE semi_empirical_expns3_methods,   ONLY: semi_empirical_expns3_setup
     197              :    USE semi_empirical_int_arrays,       ONLY: init_se_intd_array
     198              :    USE semi_empirical_mpole_methods,    ONLY: nddo_mpole_setup
     199              :    USE semi_empirical_mpole_types,      ONLY: nddo_mpole_type
     200              :    USE semi_empirical_store_int_types,  ONLY: semi_empirical_si_create,&
     201              :                                               semi_empirical_si_type
     202              :    USE semi_empirical_types,            ONLY: se_taper_create,&
     203              :                                               se_taper_type
     204              :    USE semi_empirical_utils,            ONLY: se_cutoff_compatible
     205              :    USE tblite_interface,                ONLY: tb_get_basis,&
     206              :                                               tb_init_geometry,&
     207              :                                               tb_init_wf,&
     208              :                                               tb_set_calculator
     209              :    USE transport,                       ONLY: transport_env_create
     210              :    USE xtb_parameters,                  ONLY: init_xtb_basis,&
     211              :                                               xtb_parameters_init,&
     212              :                                               xtb_parameters_set
     213              :    USE xtb_potentials,                  ONLY: xtb_pp_radius
     214              :    USE xtb_types,                       ONLY: allocate_xtb_atom_param,&
     215              :                                               set_xtb_atom_param
     216              : #include "./base/base_uses.f90"
     217              : 
     218              :    IMPLICIT NONE
     219              : 
     220              :    PRIVATE
     221              : 
     222              :    ! *** Global parameters ***
     223              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_environment'
     224              : 
     225              :    ! *** Public subroutines ***
     226              :    PUBLIC :: qs_init
     227              : 
     228              : CONTAINS
     229              : 
     230              : ! **************************************************************************************************
     231              : !> \brief Read the input and the database files for the setup of the
     232              : !>      QUICKSTEP environment.
     233              : !> \param qs_env ...
     234              : !> \param para_env ...
     235              : !> \param root_section ...
     236              : !> \param globenv ...
     237              : !> \param cp_subsys ...
     238              : !> \param kpoint_env ...
     239              : !> \param cell ...
     240              : !> \param cell_ref ...
     241              : !> \param qmmm ...
     242              : !> \param qmmm_env_qm ...
     243              : !> \param force_env_section ...
     244              : !> \param subsys_section ...
     245              : !> \param use_motion_section ...
     246              : !> \param silent ...
     247              : !> \author Creation (22.05.2000,MK)
     248              : ! **************************************************************************************************
     249        52108 :    SUBROUTINE qs_init(qs_env, para_env, root_section, globenv, cp_subsys, kpoint_env, cell, cell_ref, &
     250              :                       qmmm, qmmm_env_qm, force_env_section, subsys_section, &
     251              :                       use_motion_section, silent)
     252              : 
     253              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     254              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     255              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: root_section
     256              :       TYPE(global_environment_type), OPTIONAL, POINTER   :: globenv
     257              :       TYPE(cp_subsys_type), OPTIONAL, POINTER            :: cp_subsys
     258              :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoint_env
     259              :       TYPE(cell_type), OPTIONAL, POINTER                 :: cell, cell_ref
     260              :       LOGICAL, INTENT(IN), OPTIONAL                      :: qmmm
     261              :       TYPE(qmmm_env_qm_type), OPTIONAL, POINTER          :: qmmm_env_qm
     262              :       TYPE(section_vals_type), POINTER                   :: force_env_section, subsys_section
     263              :       LOGICAL, INTENT(IN)                                :: use_motion_section
     264              :       LOGICAL, INTENT(IN), OPTIONAL                      :: silent
     265              : 
     266              :       CHARACTER(LEN=default_string_length)               :: basis_type
     267              :       INTEGER                                            :: ikind, method_id, nelectron_total, &
     268              :                                                             nkind, nkp_grid(3)
     269              :       LOGICAL :: do_admm_rpa, do_ec_hfx, do_et, do_exx, do_hfx, do_kpoints, is_identical, is_semi, &
     270              :          mp2_present, my_qmmm, qmmm_decoupl, same_except_frac, use_ref_cell
     271         7444 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: rtmat
     272         7444 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     273              :       TYPE(cell_type), POINTER                           :: my_cell, my_cell_ref
     274              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     275              :       TYPE(dft_control_type), POINTER                    :: dft_control
     276              :       TYPE(distribution_1d_type), POINTER                :: local_particles
     277              :       TYPE(energy_correction_type), POINTER              :: ec_env
     278              :       TYPE(excited_energy_type), POINTER                 :: exstate_env
     279              :       TYPE(harris_type), POINTER                         :: harris_env
     280              :       TYPE(kpoint_type), POINTER                         :: kpoints
     281              :       TYPE(lri_environment_type), POINTER                :: lri_env
     282         7444 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     283         7444 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     284              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     285              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     286              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
     287              :       TYPE(rel_control_type), POINTER                    :: rel_control
     288              :       TYPE(scf_control_type), POINTER                    :: scf_control
     289              :       TYPE(section_vals_type), POINTER :: dft_section, ec_hfx_section, ec_section, &
     290              :          et_coupling_section, hfx_section, kpoint_section, mp2_section, rpa_hfx_section, &
     291              :          transport_section
     292              : 
     293         7444 :       NULLIFY (my_cell, my_cell_ref, atomic_kind_set, particle_set, &
     294         7444 :                qs_kind_set, kpoint_section, dft_section, ec_section, &
     295         7444 :                subsys, ks_env, dft_control, blacs_env)
     296              : 
     297         7444 :       CALL set_qs_env(qs_env, input=force_env_section)
     298         7444 :       IF (.NOT. ASSOCIATED(subsys_section)) THEN
     299          108 :          subsys_section => section_vals_get_subs_vals(force_env_section, "SUBSYS")
     300              :       END IF
     301              : 
     302              :       ! QMMM
     303         7444 :       my_qmmm = .FALSE.
     304         7444 :       IF (PRESENT(qmmm)) my_qmmm = qmmm
     305         7444 :       qmmm_decoupl = .FALSE.
     306         7444 :       IF (PRESENT(qmmm_env_qm)) THEN
     307          394 :          IF (qmmm_env_qm%qmmm_coupl_type == do_qmmm_gauss .OR. &
     308              :              qmmm_env_qm%qmmm_coupl_type == do_qmmm_swave) THEN
     309              :             ! For GAUSS/SWAVE methods there could be a DDAPC decoupling requested
     310          458 :             qmmm_decoupl = my_qmmm .AND. qmmm_env_qm%periodic .AND. qmmm_env_qm%multipole
     311              :          END IF
     312          394 :          qs_env%qmmm_env_qm => qmmm_env_qm
     313              :       END IF
     314         7444 :       CALL set_qs_env(qs_env=qs_env, qmmm=my_qmmm)
     315              : 
     316              :       ! Possibly initialize arrays for SE
     317         7444 :       CALL section_vals_val_get(force_env_section, "DFT%QS%METHOD", i_val=method_id)
     318          998 :       SELECT CASE (method_id)
     319              :       CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pdg, &
     320              :             do_method_pm3, do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
     321          998 :          CALL init_se_intd_array()
     322          998 :          is_semi = .TRUE.
     323              :       CASE (do_method_xtb, do_method_dftb)
     324         1166 :          is_semi = .TRUE.
     325              :       CASE DEFAULT
     326         7444 :          is_semi = .FALSE.
     327              :       END SELECT
     328              : 
     329        29776 :       ALLOCATE (subsys)
     330              :       CALL qs_subsys_create(subsys, para_env, &
     331              :                             force_env_section=force_env_section, &
     332              :                             subsys_section=subsys_section, &
     333              :                             use_motion_section=use_motion_section, &
     334              :                             root_section=root_section, &
     335              :                             cp_subsys=cp_subsys, cell=cell, cell_ref=cell_ref, &
     336         7444 :                             elkind=is_semi, silent=silent)
     337              : 
     338         7444 :       ALLOCATE (ks_env)
     339         7444 :       CALL qs_ks_env_create(ks_env)
     340         7444 :       CALL set_ks_env(ks_env, subsys=subsys)
     341         7444 :       CALL set_qs_env(qs_env, ks_env=ks_env)
     342              : 
     343              :       CALL qs_subsys_get(subsys, &
     344              :                          cell=my_cell, &
     345              :                          cell_ref=my_cell_ref, &
     346              :                          use_ref_cell=use_ref_cell, &
     347              :                          atomic_kind_set=atomic_kind_set, &
     348              :                          qs_kind_set=qs_kind_set, &
     349         7444 :                          particle_set=particle_set)
     350              : 
     351         7444 :       CALL set_ks_env(ks_env, para_env=para_env)
     352         7444 :       IF (PRESENT(globenv)) THEN
     353              :          CALL cp_blacs_env_create(blacs_env, para_env, globenv%blacs_grid_layout, &
     354         7438 :                                   globenv%blacs_repeatable)
     355              :       ELSE
     356            6 :          CALL cp_blacs_env_create(blacs_env, para_env)
     357              :       END IF
     358         7444 :       CALL set_ks_env(ks_env, blacs_env=blacs_env)
     359         7444 :       CALL cp_blacs_env_release(blacs_env)
     360              : 
     361              :       !   *** Setup the grids for the G-space Interpolation if any
     362              :       CALL cp_ddapc_ewald_create(qs_env%cp_ddapc_ewald, qmmm_decoupl, my_cell, &
     363         7444 :                                  force_env_section, subsys_section, para_env)
     364              : 
     365              :       ! kpoints
     366         7444 :       IF (PRESENT(kpoint_env)) THEN
     367            2 :          kpoints => kpoint_env
     368            2 :          CALL set_qs_env(qs_env=qs_env, kpoints=kpoints)
     369            2 :          CALL kpoint_initialize(kpoints, particle_set, my_cell)
     370              :       ELSE
     371         7442 :          NULLIFY (kpoints)
     372         7442 :          CALL kpoint_create(kpoints)
     373         7442 :          CALL set_qs_env(qs_env=qs_env, kpoints=kpoints)
     374         7442 :          kpoint_section => section_vals_get_subs_vals(qs_env%input, "DFT%KPOINTS")
     375         7442 :          CALL read_kpoint_section(kpoints, kpoint_section, my_cell%hmat)
     376         7442 :          CALL kpoint_initialize(kpoints, particle_set, my_cell)
     377         7442 :          dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     378         7442 :          CALL write_kpoint_info(kpoints, dft_section)
     379              :       END IF
     380              : 
     381              :       CALL qs_init_subsys(qs_env, para_env, subsys, my_cell, my_cell_ref, use_ref_cell, &
     382         7444 :                           subsys_section, silent=silent)
     383              : 
     384         7444 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     385         7444 :       IF (method_id == do_method_lrigpw .OR. dft_control%qs_control%lri_optbas) THEN
     386           46 :          CALL get_qs_env(qs_env=qs_env, lri_env=lri_env)
     387           46 :          CALL lri_env_basis("LRI", qs_env, lri_env, qs_kind_set)
     388         7398 :       ELSE IF (method_id == do_method_rigpw) THEN
     389              :          CALL cp_warn(__LOCATION__, "Experimental code: "// &
     390            0 :                       "RIGPW should only be used for testing.")
     391            0 :          CALL get_qs_env(qs_env=qs_env, lri_env=lri_env)
     392            0 :          CALL lri_env_basis("RI", qs_env, lri_env, qs_kind_set)
     393              :       END IF
     394              : 
     395         7444 :       IF (my_qmmm .AND. PRESENT(qmmm_env_qm) .AND. .NOT. dft_control%qs_control%commensurate_mgrids) THEN
     396          132 :          IF (qmmm_env_qm%qmmm_coupl_type == do_qmmm_gauss .OR. qmmm_env_qm%qmmm_coupl_type == do_qmmm_swave) THEN
     397              :             CALL cp_abort(__LOCATION__, "QM/MM with coupling GAUSS or S-WAVE requires "// &
     398            0 :                           "keyword FORCE_EVAL/DFT/MGRID/COMMENSURATE to be enabled.")
     399              :          END IF
     400              :       END IF
     401              : 
     402              :       ! more kpoint stuff
     403         7444 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints, blacs_env=blacs_env)
     404         7444 :       IF (do_kpoints) THEN
     405          170 :          CALL kpoint_env_initialize(kpoints, para_env, blacs_env, with_aux_fit=dft_control%do_admm)
     406          170 :          CALL kpoint_initialize_mos(kpoints, qs_env%mos)
     407          170 :          CALL get_qs_env(qs_env=qs_env, wf_history=wf_history)
     408          170 :          CALL wfi_create_for_kp(wf_history)
     409              :       END IF
     410              :       ! basis set symmetry rotations
     411         7444 :       IF (do_kpoints) THEN
     412          170 :          CALL qs_basis_rotation(qs_env, kpoints)
     413              :       END IF
     414              : 
     415              :       do_hfx = .FALSE.
     416         7444 :       hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF")
     417         7444 :       CALL section_vals_get(hfx_section, explicit=do_hfx)
     418         7444 :       CALL get_qs_env(qs_env, dft_control=dft_control, scf_control=scf_control, nelectron_total=nelectron_total)
     419         7444 :       IF (do_hfx) THEN
     420              :          ! Retrieve particle_set and atomic_kind_set (needed for both kinds of initialization)
     421         4800 :          nkp_grid = 1
     422         1200 :          IF (do_kpoints) CALL get_kpoint_info(kpoints, nkp_grid=nkp_grid)
     423         1200 :          IF (dft_control%do_admm) THEN
     424          456 :             basis_type = 'AUX_FIT'
     425              :          ELSE
     426          744 :             basis_type = 'ORB'
     427              :          END IF
     428              :          CALL hfx_create(qs_env%x_data, para_env, hfx_section, atomic_kind_set, &
     429              :                          qs_kind_set, particle_set, dft_control, my_cell, orb_basis=basis_type, &
     430         1200 :                          nelectron_total=nelectron_total, nkp_grid=nkp_grid)
     431              :       END IF
     432              : 
     433         7444 :       mp2_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION")
     434         7444 :       CALL section_vals_get(mp2_section, explicit=mp2_present)
     435         7444 :       IF (mp2_present) THEN
     436          470 :          CPASSERT(ASSOCIATED(qs_env%mp2_env))
     437          470 :          CALL read_mp2_section(qs_env%input, qs_env%mp2_env)
     438              :          ! create the EXX section if necessary
     439              :          do_exx = .FALSE.
     440          470 :          rpa_hfx_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%HF")
     441          470 :          CALL section_vals_get(rpa_hfx_section, explicit=do_exx)
     442          470 :          IF (do_exx) THEN
     443              : 
     444              :             ! do_exx in call of hfx_create decides whether to go without ADMM (do_exx=.TRUE.) or with
     445              :             ! ADMM (do_exx=.FALSE.)
     446          142 :             CALL section_vals_val_get(mp2_section, "RI_RPA%ADMM", l_val=do_admm_rpa)
     447              : 
     448              :             ! Reuse the HFX integrals from the qs_env if applicable
     449          142 :             qs_env%mp2_env%ri_rpa%reuse_hfx = .TRUE.
     450          142 :             IF (.NOT. do_hfx) qs_env%mp2_env%ri_rpa%reuse_hfx = .FALSE.
     451          142 :             CALL compare_hfx_sections(hfx_section, rpa_hfx_section, is_identical, same_except_frac)
     452          142 :             IF (.NOT. (is_identical .OR. same_except_frac)) qs_env%mp2_env%ri_rpa%reuse_hfx = .FALSE.
     453          142 :             IF (dft_control%do_admm .AND. .NOT. do_admm_rpa) qs_env%mp2_env%ri_rpa%reuse_hfx = .FALSE.
     454              : 
     455          142 :             IF (.NOT. qs_env%mp2_env%ri_rpa%reuse_hfx) THEN
     456          124 :                IF (do_admm_rpa) THEN
     457           10 :                   basis_type = 'AUX_FIT'
     458              :                ELSE
     459          114 :                   basis_type = 'ORB'
     460              :                END IF
     461              :                CALL hfx_create(qs_env%mp2_env%ri_rpa%x_data, para_env, rpa_hfx_section, atomic_kind_set, &
     462              :                                qs_kind_set, particle_set, dft_control, my_cell, orb_basis=basis_type, &
     463          124 :                                nelectron_total=nelectron_total)
     464              :             ELSE
     465           18 :                qs_env%mp2_env%ri_rpa%x_data => qs_env%x_data
     466              :             END IF
     467              :          END IF
     468              :       END IF
     469              : 
     470         7444 :       IF (dft_control%qs_control%do_kg) THEN
     471           66 :          CALL cite_reference(Iannuzzi2006)
     472           66 :          CALL kg_env_create(qs_env, qs_env%kg_env, qs_kind_set, qs_env%input)
     473              :       END IF
     474              : 
     475         7444 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     476              :       CALL section_vals_val_get(dft_section, "EXCITED_STATES%_SECTION_PARAMETERS_", &
     477         7444 :                                 l_val=qs_env%excited_state)
     478         7444 :       NULLIFY (exstate_env)
     479         7444 :       CALL exstate_create(exstate_env, qs_env%excited_state, dft_section)
     480         7444 :       CALL set_qs_env(qs_env, exstate_env=exstate_env)
     481              : 
     482              :       et_coupling_section => section_vals_get_subs_vals(qs_env%input, &
     483         7444 :                                                         "PROPERTIES%ET_COUPLING")
     484         7444 :       CALL section_vals_get(et_coupling_section, explicit=do_et)
     485         7444 :       IF (do_et) CALL et_coupling_create(qs_env%et_coupling)
     486              : 
     487         7444 :       transport_section => section_vals_get_subs_vals(qs_env%input, "DFT%TRANSPORT")
     488         7444 :       CALL section_vals_get(transport_section, explicit=qs_env%do_transport)
     489         7444 :       IF (qs_env%do_transport) THEN
     490            0 :          CALL transport_env_create(qs_env)
     491              :       END IF
     492              : 
     493         7444 :       CALL get_qs_env(qs_env, harris_env=harris_env)
     494         7444 :       IF (qs_env%harris_method) THEN
     495              :          ! initialize the Harris input density and potential integrals
     496            6 :          CALL get_qs_env(qs_env, local_particles=local_particles)
     497              :          CALL harris_rhoin_init(harris_env%rhoin, "RHOIN", qs_kind_set, atomic_kind_set, &
     498            6 :                                 local_particles, dft_control%nspins)
     499              :          ! Print information of the HARRIS section
     500            6 :          CALL harris_write_input(harris_env)
     501              :       END IF
     502              : 
     503         7444 :       NULLIFY (ec_env)
     504         7444 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     505              :       CALL section_vals_val_get(dft_section, "ENERGY_CORRECTION%_SECTION_PARAMETERS_", &
     506         7444 :                                 l_val=qs_env%energy_correction)
     507         7444 :       ec_section => section_vals_get_subs_vals(qs_env%input, "DFT%ENERGY_CORRECTION")
     508         7444 :       CALL ec_env_create(qs_env, ec_env, dft_section, ec_section)
     509         7444 :       CALL set_qs_env(qs_env, ec_env=ec_env)
     510              : 
     511         7444 :       IF (qs_env%energy_correction) THEN
     512              :          ! Energy correction with Hartree-Fock exchange
     513          268 :          ec_hfx_section => section_vals_get_subs_vals(ec_section, "XC%HF")
     514          268 :          CALL section_vals_get(ec_hfx_section, explicit=do_ec_hfx)
     515              : 
     516          268 :          IF (ec_env%do_ec_hfx) THEN
     517              : 
     518              :             ! Hybrid functionals require same basis
     519           24 :             IF (ec_env%basis_inconsistent) THEN
     520              :                CALL cp_abort(__LOCATION__, &
     521              :                              "Energy correction methods with hybrid functionals: "// &
     522              :                              "correction and ground state need to use the same basis. "// &
     523            0 :                              "Checked by comparing basis set names only.")
     524              :             END IF
     525              : 
     526              :             ! Similar to RPA_HFX we can check if HFX integrals from the qs_env can be reused
     527           24 :             IF (ec_env%do_ec_admm .AND. .NOT. dft_control%do_admm) THEN
     528            0 :                CALL cp_abort(__LOCATION__, "Need an ADMM input section for ADMM EC to work")
     529              :             END IF
     530              : 
     531           24 :             ec_env%reuse_hfx = .TRUE.
     532           24 :             IF (.NOT. do_hfx) ec_env%reuse_hfx = .FALSE.
     533           24 :             CALL compare_hfx_sections(hfx_section, ec_hfx_section, is_identical, same_except_frac)
     534           24 :             IF (.NOT. (is_identical .OR. same_except_frac)) ec_env%reuse_hfx = .FALSE.
     535           24 :             IF (dft_control%do_admm .AND. .NOT. ec_env%do_ec_admm) ec_env%reuse_hfx = .FALSE.
     536              : 
     537           24 :             IF (.NOT. ec_env%reuse_hfx) THEN
     538           10 :                IF (ec_env%do_ec_admm) THEN
     539            2 :                   basis_type = 'AUX_FIT'
     540              :                ELSE
     541            8 :                   basis_type = 'ORB'
     542              :                END IF
     543              :                CALL hfx_create(ec_env%x_data, para_env, ec_hfx_section, atomic_kind_set, &
     544              :                                qs_kind_set, particle_set, dft_control, my_cell, orb_basis=basis_type, &
     545           10 :                                nelectron_total=nelectron_total)
     546              :             ELSE
     547           14 :                ec_env%x_data => qs_env%x_data
     548              :             END IF
     549              :          END IF
     550              : 
     551              :          ! Print information of the EC section
     552          268 :          CALL ec_write_input(ec_env)
     553              : 
     554              :       END IF
     555              : 
     556         7444 :       IF (dft_control%qs_control%do_almo_scf) THEN
     557           66 :          CALL almo_scf_env_create(qs_env)
     558              :       END IF
     559              : 
     560              :       ! see if we have atomic relativistic corrections
     561         7444 :       CALL get_qs_env(qs_env, rel_control=rel_control)
     562         7444 :       IF (rel_control%rel_method /= rel_none) THEN
     563           16 :          IF (rel_control%rel_transformation == rel_trans_atom) THEN
     564           16 :             nkind = SIZE(atomic_kind_set)
     565           42 :             DO ikind = 1, nkind
     566           26 :                NULLIFY (rtmat)
     567           26 :                CALL calculate_atomic_relkin(atomic_kind_set(ikind), qs_kind_set(ikind), rel_control, rtmat)
     568           42 :                IF (ASSOCIATED(rtmat)) CALL set_qs_kind(qs_kind_set(ikind), reltmat=rtmat)
     569              :             END DO
     570              :          END IF
     571              :       END IF
     572              : 
     573         7444 :    END SUBROUTINE qs_init
     574              : 
     575              : ! **************************************************************************************************
     576              : !> \brief Initialize the qs environment (subsys)
     577              : !> \param qs_env ...
     578              : !> \param para_env ...
     579              : !> \param subsys ...
     580              : !> \param cell ...
     581              : !> \param cell_ref ...
     582              : !> \param use_ref_cell ...
     583              : !> \param subsys_section ...
     584              : !> \param silent ...
     585              : !> \author Creation (22.05.2000,MK)
     586              : ! **************************************************************************************************
     587         7444 :    SUBROUTINE qs_init_subsys(qs_env, para_env, subsys, cell, cell_ref, use_ref_cell, subsys_section, &
     588              :                              silent)
     589              : 
     590              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     591              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     592              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     593              :       TYPE(cell_type), POINTER                           :: cell, cell_ref
     594              :       LOGICAL, INTENT(in)                                :: use_ref_cell
     595              :       TYPE(section_vals_type), POINTER                   :: subsys_section
     596              :       LOGICAL, INTENT(in), OPTIONAL                      :: silent
     597              : 
     598              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_init_subsys'
     599              : 
     600              :       CHARACTER(len=2)                                   :: element_symbol
     601              :       INTEGER :: gfn_type, handle, ikind, ispin, iw, lmax_sphere, maxl, maxlgto, maxlgto_lri, &
     602              :          maxlgto_nuc, maxlppl, maxlppnl, method_id, multiplicity, my_ival, n_ao, n_mo_add, natom, &
     603              :          nelectron, ngauss, nkind, output_unit, sort_basis, tnadd_method
     604              :       INTEGER, DIMENSION(2)                              :: n_mo, nelectron_spin
     605              :       INTEGER, DIMENSION(5)                              :: occ
     606              :       LOGICAL :: all_potential_present, be_silent, cneo_potential_present, do_kpoints, do_ri_hfx, &
     607              :          do_ri_mp2, do_ri_rpa, do_ri_sos_mp2, do_rpa_ri_exx, do_wfc_im_time, e1terms, &
     608              :          has_unit_metric, lribas, mp2_present, orb_gradient
     609              :       REAL(KIND=dp)                                      :: alpha, ccore, ewald_rcut, fxx, maxocc, &
     610              :                                                             rcut, total_zeff_corr, verlet_skin, &
     611              :                                                             zeff_correction
     612         7444 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     613              :       TYPE(cp_logger_type), POINTER                      :: logger
     614              :       TYPE(dft_control_type), POINTER                    :: dft_control
     615              :       TYPE(dftb_control_type), POINTER                   :: dftb_control
     616              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
     617              :       TYPE(ewald_environment_type), POINTER              :: ewald_env
     618              :       TYPE(ewald_pw_type), POINTER                       :: ewald_pw
     619              :       TYPE(fist_nonbond_env_type), POINTER               :: se_nonbond_env
     620              :       TYPE(gapw_control_type), POINTER                   :: gapw_control
     621              :       TYPE(gto_basis_set_type), POINTER                  :: aux_fit_basis, lri_aux_basis, &
     622              :                                                             rhoin_basis, ri_aux_basis_set, &
     623              :                                                             ri_hfx_basis, ri_xas_basis, &
     624              :                                                             tmp_basis_set
     625              :       TYPE(harris_type), POINTER                         :: harris_env
     626              :       TYPE(local_rho_type), POINTER                      :: local_rho_set
     627              :       TYPE(lri_environment_type), POINTER                :: lri_env
     628         7444 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos, mos_last_converged
     629         7444 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
     630         7444 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     631              :       TYPE(mp2_type), POINTER                            :: mp2_env
     632              :       TYPE(nddo_mpole_type), POINTER                     :: se_nddo_mpole
     633         7444 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     634              :       TYPE(pw_env_type), POINTER                         :: pw_env
     635              :       TYPE(qs_control_type), POINTER                     :: qs_control
     636              :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), &
     637         7444 :          POINTER                                         :: dftb_potential
     638              :       TYPE(qs_dispersion_type), POINTER                  :: dispersion_env
     639              :       TYPE(qs_energy_type), POINTER                      :: energy
     640         7444 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     641              :       TYPE(qs_gcp_type), POINTER                         :: gcp_env
     642         7444 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     643              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
     644              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     645              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
     646              :       TYPE(rho0_mpole_type), POINTER                     :: rho0_mpole
     647         7444 :       TYPE(rho_atom_type), DIMENSION(:), POINTER         :: rho_atom_set
     648              :       TYPE(scf_control_type), POINTER                    :: scf_control
     649              :       TYPE(se_taper_type), POINTER                       :: se_taper
     650              :       TYPE(section_vals_type), POINTER :: dft_section, et_coupling_section, et_ddapc_section, &
     651              :          ewald_section, harris_section, lri_section, mp2_section, nl_section, poisson_section, &
     652              :          pp_section, print_section, qs_section, rixs_section, se_section, tddfpt_section, &
     653              :          xc_section
     654              :       TYPE(semi_empirical_control_type), POINTER         :: se_control
     655              :       TYPE(semi_empirical_si_type), POINTER              :: se_store_int_env
     656              :       TYPE(xtb_control_type), POINTER                    :: xtb_control
     657              : 
     658         7444 :       CALL timeset(routineN, handle)
     659         7444 :       NULLIFY (logger)
     660         7444 :       logger => cp_get_default_logger()
     661         7444 :       output_unit = cp_logger_get_default_io_unit(logger)
     662              : 
     663         7444 :       be_silent = .FALSE.
     664         7444 :       IF (PRESENT(silent)) be_silent = silent
     665              : 
     666         7444 :       CALL cite_reference(cp2kqs2020)
     667              : 
     668              :       ! Initialise the Quickstep environment
     669         7444 :       NULLIFY (mos, se_taper)
     670         7444 :       NULLIFY (dft_control)
     671         7444 :       NULLIFY (energy)
     672         7444 :       NULLIFY (force)
     673         7444 :       NULLIFY (local_molecules)
     674         7444 :       NULLIFY (local_particles)
     675         7444 :       NULLIFY (scf_control)
     676         7444 :       NULLIFY (dft_section)
     677         7444 :       NULLIFY (et_coupling_section)
     678         7444 :       NULLIFY (ks_env)
     679         7444 :       NULLIFY (mos_last_converged)
     680         7444 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     681         7444 :       qs_section => section_vals_get_subs_vals(dft_section, "QS")
     682         7444 :       et_coupling_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%ET_COUPLING")
     683              :       ! reimplemented TDDFPT
     684         7444 :       tddfpt_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%TDDFPT")
     685         7444 :       rixs_section => section_vals_get_subs_vals(qs_env%input, "PROPERTIES%RIXS")
     686              : 
     687              :       CALL qs_subsys_get(subsys, particle_set=particle_set, &
     688              :                          qs_kind_set=qs_kind_set, &
     689              :                          atomic_kind_set=atomic_kind_set, &
     690              :                          molecule_set=molecule_set, &
     691         7444 :                          molecule_kind_set=molecule_kind_set)
     692              : 
     693              :       !   *** Read the input section with the DFT control parameters ***
     694         7444 :       CALL read_dft_control(dft_control, dft_section)
     695              : 
     696              :       ! set periodicity flag
     697        29776 :       dft_control%qs_control%periodicity = SUM(cell%perd)
     698              : 
     699              :       !  Read the input section with the Quickstep control parameters
     700         7444 :       CALL read_qs_section(dft_control%qs_control, qs_section)
     701              : 
     702              :       !   *** Print the Quickstep program banner (copyright and version number) ***
     703         7444 :       IF (.NOT. be_silent) THEN
     704         7438 :          iw = cp_print_key_unit_nr(logger, dft_section, "PRINT%PROGRAM_BANNER", extension=".Log")
     705         7438 :          CALL section_vals_val_get(qs_section, "METHOD", i_val=method_id)
     706         5278 :          SELECT CASE (method_id)
     707              :          CASE DEFAULT
     708         5278 :             CALL qs_header(iw)
     709              :          CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pdg, &
     710              :                do_method_pm3, do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
     711          998 :             CALL se_header(iw)
     712              :          CASE (do_method_dftb)
     713          222 :             CALL dftb_header(iw)
     714              :          CASE (do_method_xtb)
     715         7438 :             IF (dft_control%qs_control%xtb_control%do_tblite) THEN
     716            0 :                CALL tblite_header(iw, dft_control%qs_control%xtb_control%tblite_method)
     717              :             ELSE
     718          940 :                gfn_type = dft_control%qs_control%xtb_control%gfn_type
     719          940 :                CALL xtb_header(iw, gfn_type)
     720              :             END IF
     721              :          END SELECT
     722              :          CALL cp_print_key_finished_output(iw, logger, dft_section, &
     723         7438 :                                            "PRINT%PROGRAM_BANNER")
     724              :       END IF
     725              : 
     726         7444 :       IF (dft_control%do_sccs .AND. dft_control%qs_control%gapw) THEN
     727            0 :          CPABORT("SCCS is not yet implemented with GAPW")
     728              :       END IF
     729         7444 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     730         7444 :       IF (do_kpoints) THEN
     731              :          ! reset some of the settings for wfn extrapolation for kpoints
     732          274 :          SELECT CASE (dft_control%qs_control%wf_interpolation_method_nr)
     733              :          CASE (wfi_linear_wf_method_nr, wfi_linear_ps_method_nr, wfi_ps_method_nr, wfi_aspc_nr)
     734          170 :             dft_control%qs_control%wf_interpolation_method_nr = wfi_use_guess_method_nr
     735              :          END SELECT
     736              :       END IF
     737              : 
     738              :       !   *******  check if any kind of electron transfer calculation has to be performed
     739         7444 :       CALL section_vals_val_get(et_coupling_section, "TYPE_OF_CONSTRAINT", i_val=my_ival)
     740         7444 :       dft_control%qs_control%et_coupling_calc = .FALSE.
     741         7444 :       IF (my_ival == do_et_ddapc) THEN
     742            0 :          et_ddapc_section => section_vals_get_subs_vals(et_coupling_section, "DDAPC_RESTRAINT_A")
     743            0 :          dft_control%qs_control%et_coupling_calc = .TRUE.
     744            0 :          dft_control%qs_control%ddapc_restraint = .TRUE.
     745            0 :          CALL read_ddapc_section(dft_control%qs_control, ddapc_restraint_section=et_ddapc_section)
     746              :       END IF
     747              : 
     748         7444 :       CALL read_mgrid_section(dft_control%qs_control, dft_section)
     749              : 
     750              :       ! reimplemented TDDFPT
     751         7444 :       CALL read_tddfpt2_control(dft_control%tddfpt2_control, tddfpt_section, dft_control%qs_control)
     752              : 
     753              :       ! rixs
     754         7444 :       CALL section_vals_get(rixs_section, explicit=qs_env%do_rixs)
     755         7444 :       IF (qs_env%do_rixs) THEN
     756           14 :          CALL read_rixs_control(dft_control%rixs_control, rixs_section, dft_control%qs_control)
     757              :       END IF
     758              : 
     759              :       !   Create relativistic control section
     760              :       BLOCK
     761              :          TYPE(rel_control_type), POINTER :: rel_control
     762         7444 :          ALLOCATE (rel_control)
     763         7444 :          CALL rel_c_create(rel_control)
     764         7444 :          CALL rel_c_read_parameters(rel_control, dft_section)
     765         7444 :          CALL set_qs_env(qs_env, rel_control=rel_control)
     766              :       END BLOCK
     767              : 
     768              :       !   *** Read DFTB parameter files ***
     769         7444 :       IF (dft_control%qs_control%method_id == do_method_dftb) THEN
     770          222 :          NULLIFY (ewald_env, ewald_pw, dftb_potential)
     771          222 :          dftb_control => dft_control%qs_control%dftb_control
     772              :          CALL qs_dftb_param_init(atomic_kind_set, qs_kind_set, dftb_control, dftb_potential, &
     773          222 :                                  subsys_section=subsys_section, para_env=para_env)
     774          222 :          CALL set_qs_env(qs_env, dftb_potential=dftb_potential)
     775              :          ! check for Ewald
     776          222 :          IF (dftb_control%do_ewald) THEN
     777         1888 :             ALLOCATE (ewald_env)
     778          118 :             CALL ewald_env_create(ewald_env, para_env)
     779          118 :             poisson_section => section_vals_get_subs_vals(dft_section, "POISSON")
     780          118 :             CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
     781          118 :             ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
     782          118 :             print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
     783          118 :             CALL get_qs_kind_set(qs_kind_set, basis_rcut=ewald_rcut)
     784          118 :             CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat)
     785          118 :             ALLOCATE (ewald_pw)
     786          118 :             CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, print_section=print_section)
     787          118 :             CALL set_qs_env(qs_env, ewald_env=ewald_env, ewald_pw=ewald_pw)
     788              :          END IF
     789         7222 :       ELSEIF (dft_control%qs_control%method_id == do_method_xtb) THEN
     790              :          !   *** Read xTB parameter file ***
     791          944 :          xtb_control => dft_control%qs_control%xtb_control
     792          944 :          CALL get_qs_env(qs_env, nkind=nkind)
     793          944 :          IF (xtb_control%do_tblite) THEN
     794              :             ! put geometry to tblite
     795            0 :             CALL tb_init_geometry(qs_env, qs_env%tb_tblite)
     796              :             ! select tblite method
     797            0 :             CALL tb_set_calculator(qs_env%tb_tblite, xtb_control%tblite_method)
     798              :             !set up wave function
     799            0 :             CALL tb_init_wf(qs_env%tb_tblite)
     800              :             !get basis set
     801            0 :             DO ikind = 1, nkind
     802            0 :                qs_kind => qs_kind_set(ikind)
     803              :                ! Setup proper xTB parameters
     804            0 :                CPASSERT(.NOT. ASSOCIATED(qs_kind%xtb_parameter))
     805            0 :                CALL allocate_xtb_atom_param(qs_kind%xtb_parameter)
     806              :                ! Set default parameters
     807            0 :                CALL get_qs_kind(qs_kind, element_symbol=element_symbol)
     808              : 
     809            0 :                NULLIFY (tmp_basis_set)
     810            0 :                CALL tb_get_basis(qs_env%tb_tblite, tmp_basis_set, element_symbol, qs_kind%xtb_parameter, occ)
     811            0 :                CALL add_basis_set_to_container(qs_kind%basis_sets, tmp_basis_set, "ORB")
     812            0 :                CALL set_xtb_atom_param(qs_kind%xtb_parameter, occupation=occ)
     813              : 
     814              :                !setting the potential for the computation
     815            0 :                zeff_correction = 0.0_dp
     816              :                CALL init_potential(qs_kind%all_potential, itype="BARE", &
     817            0 :                                    zeff=REAL(SUM(occ), dp), zeff_correction=zeff_correction)
     818              :             END DO
     819              :          ELSE
     820          944 :             NULLIFY (ewald_env, ewald_pw)
     821         3040 :             DO ikind = 1, nkind
     822         2096 :                qs_kind => qs_kind_set(ikind)
     823              :                ! Setup proper xTB parameters
     824         2096 :                CPASSERT(.NOT. ASSOCIATED(qs_kind%xtb_parameter))
     825         2096 :                CALL allocate_xtb_atom_param(qs_kind%xtb_parameter)
     826              :                ! Set default parameters
     827         2096 :                gfn_type = dft_control%qs_control%xtb_control%gfn_type
     828         2096 :                CALL get_qs_kind(qs_kind, element_symbol=element_symbol)
     829              :                CALL xtb_parameters_init(qs_kind%xtb_parameter, gfn_type, element_symbol, &
     830              :                                         xtb_control%parameter_file_path, xtb_control%parameter_file_name, &
     831         2096 :                                         para_env)
     832              :                ! set dependent parameters
     833         2096 :                CALL xtb_parameters_set(qs_kind%xtb_parameter)
     834              :                ! Generate basis set
     835         2096 :                NULLIFY (tmp_basis_set)
     836         2096 :                IF (qs_kind%xtb_parameter%z == 1) THEN
     837              :                   ! special case hydrogen
     838          456 :                   ngauss = xtb_control%h_sto_ng
     839              :                ELSE
     840         1640 :                   ngauss = xtb_control%sto_ng
     841              :                END IF
     842         2096 :                IF (qs_kind%xtb_parameter%defined) THEN
     843         2094 :                   CALL init_xtb_basis(qs_kind%xtb_parameter, tmp_basis_set, ngauss)
     844         2094 :                   CALL add_basis_set_to_container(qs_kind%basis_sets, tmp_basis_set, "ORB")
     845              :                ELSE
     846            2 :                   CALL set_qs_kind(qs_kind, ghost=.TRUE.)
     847            2 :                   IF (ASSOCIATED(qs_kind%all_potential)) THEN
     848            2 :                      DEALLOCATE (qs_kind%all_potential%elec_conf)
     849            2 :                      DEALLOCATE (qs_kind%all_potential)
     850              :                   END IF
     851              :                END IF
     852              :                ! potential
     853         3040 :                IF (qs_kind%xtb_parameter%defined) THEN
     854         2094 :                   zeff_correction = 0.0_dp
     855              :                   CALL init_potential(qs_kind%all_potential, itype="BARE", &
     856         2094 :                                       zeff=qs_kind%xtb_parameter%zeff, zeff_correction=zeff_correction)
     857         2094 :                   CALL get_potential(qs_kind%all_potential, alpha_core_charge=alpha)
     858         2094 :                   ccore = qs_kind%xtb_parameter%zeff*SQRT((alpha/pi)**3)
     859         2094 :                   CALL set_potential(qs_kind%all_potential, ccore_charge=ccore)
     860         2094 :                   qs_kind%xtb_parameter%zeff = qs_kind%xtb_parameter%zeff - zeff_correction
     861              :                END IF
     862              :             END DO
     863              :             !
     864              :             ! set repulsive potential range
     865              :             !
     866         3776 :             ALLOCATE (xtb_control%rcpair(nkind, nkind))
     867          944 :             CALL xtb_pp_radius(qs_kind_set, xtb_control%rcpair, xtb_control%eps_pair, xtb_control%kf)
     868              :             ! check for Ewald
     869          944 :             IF (xtb_control%do_ewald) THEN
     870         2848 :                ALLOCATE (ewald_env)
     871          178 :                CALL ewald_env_create(ewald_env, para_env)
     872          178 :                poisson_section => section_vals_get_subs_vals(dft_section, "POISSON")
     873          178 :                CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
     874          178 :                ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
     875          178 :                print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
     876          178 :                IF (gfn_type == 0) THEN
     877              :                   CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
     878           34 :                                              silent=silent, pset="EEQ")
     879              :                ELSE
     880              :                   CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
     881          144 :                                              silent=silent)
     882              :                END IF
     883          178 :                ALLOCATE (ewald_pw)
     884          178 :                CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, print_section=print_section)
     885          178 :                CALL set_qs_env(qs_env, ewald_env=ewald_env, ewald_pw=ewald_pw)
     886              :             END IF
     887              :          END IF
     888              :       END IF
     889              :       ! lri or ri env initialization
     890         7444 :       lri_section => section_vals_get_subs_vals(qs_section, "LRIGPW")
     891              :       IF (dft_control%qs_control%method_id == do_method_lrigpw .OR. &
     892         7444 :           dft_control%qs_control%lri_optbas .OR. &
     893              :           dft_control%qs_control%method_id == do_method_rigpw) THEN
     894           46 :          CALL lri_env_init(lri_env, lri_section)
     895           46 :          CALL set_qs_env(qs_env, lri_env=lri_env)
     896              :       END IF
     897              : 
     898              :       !   *** Check basis and fill in missing parts ***
     899         7444 :       CALL check_qs_kind_set(qs_kind_set, dft_control, subsys_section=subsys_section)
     900              : 
     901              :       !   *** Check that no all-electron potential is present if GPW or GAPW_XC
     902         7444 :       CALL get_qs_kind_set(qs_kind_set, all_potential_present=all_potential_present)
     903              :       IF ((dft_control%qs_control%method_id == do_method_gpw) .OR. &
     904         7444 :           (dft_control%qs_control%method_id == do_method_gapw_xc) .OR. &
     905              :           (dft_control%qs_control%method_id == do_method_ofgpw)) THEN
     906         4368 :          IF (all_potential_present) THEN
     907            0 :             CPABORT("All-electron calculations with GPW, GAPW_XC, and OFGPW are not implemented")
     908              :          END IF
     909              :       END IF
     910              : 
     911              :       !   *** Check that no cneo potential is present if not GAPW
     912         7444 :       CALL get_qs_kind_set(qs_kind_set, cneo_potential_present=cneo_potential_present)
     913         7444 :       IF (cneo_potential_present .AND. &
     914              :           dft_control%qs_control%method_id /= do_method_gapw) THEN
     915            0 :          CPABORT("CNEO calculations require GAPW method")
     916              :       END IF
     917              : 
     918              :       ! DFT+U
     919         7444 :       CALL get_qs_kind_set(qs_kind_set, dft_plus_u_atom_present=dft_control%dft_plus_u)
     920              : 
     921         7444 :       IF (dft_control%do_admm) THEN
     922              :          ! Check if ADMM basis is available
     923          464 :          CALL get_qs_env(qs_env, nkind=nkind)
     924         1314 :          DO ikind = 1, nkind
     925          850 :             NULLIFY (aux_fit_basis)
     926          850 :             qs_kind => qs_kind_set(ikind)
     927          850 :             CALL get_qs_kind(qs_kind, basis_set=aux_fit_basis, basis_type="AUX_FIT")
     928         1314 :             IF (.NOT. (ASSOCIATED(aux_fit_basis))) THEN
     929              :                ! AUX_FIT basis set is not available
     930            0 :                CPABORT("AUX_FIT basis set is not defined. ")
     931              :             END IF
     932              :          END DO
     933              :       END IF
     934              : 
     935         7444 :       lribas = .FALSE.
     936         7444 :       e1terms = .FALSE.
     937         7444 :       IF (dft_control%qs_control%method_id == do_method_lrigpw) THEN
     938           40 :          lribas = .TRUE.
     939           40 :          CALL get_qs_env(qs_env, lri_env=lri_env)
     940           40 :          e1terms = lri_env%exact_1c_terms
     941              :       END IF
     942         7444 :       IF (dft_control%qs_control%do_kg) THEN
     943           66 :          CALL section_vals_val_get(dft_section, "KG_METHOD%TNADD_METHOD", i_val=tnadd_method)
     944           66 :          IF (tnadd_method == kg_tnadd_embed_ri) lribas = .TRUE.
     945              :       END IF
     946         7442 :       IF (lribas) THEN
     947              :          ! Check if LRI_AUX basis is available, auto-generate if needed
     948           42 :          CALL get_qs_env(qs_env, nkind=nkind)
     949          122 :          DO ikind = 1, nkind
     950           80 :             NULLIFY (lri_aux_basis)
     951           80 :             qs_kind => qs_kind_set(ikind)
     952           80 :             CALL get_qs_kind(qs_kind, basis_set=lri_aux_basis, basis_type="LRI_AUX")
     953          122 :             IF (.NOT. (ASSOCIATED(lri_aux_basis))) THEN
     954              :                ! LRI_AUX basis set is not yet loaded
     955              :                CALL cp_warn(__LOCATION__, "Automatic Generation of LRI_AUX basis. "// &
     956           18 :                             "This is experimental code.")
     957              :                ! Generate a default basis
     958           18 :                CALL create_lri_aux_basis_set(lri_aux_basis, qs_kind, dft_control%auto_basis_lri_aux, e1terms)
     959           18 :                CALL add_basis_set_to_container(qs_kind%basis_sets, lri_aux_basis, "LRI_AUX")
     960              :             END IF
     961              :          END DO
     962              :       END IF
     963              : 
     964         7444 :       CALL section_vals_val_get(qs_env%input, "DFT%XC%HF%RI%_SECTION_PARAMETERS_", l_val=do_ri_hfx)
     965              :       CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%HF%RI%_SECTION_PARAMETERS_", &
     966         7444 :                                 l_val=do_rpa_ri_exx)
     967         7444 :       IF (do_ri_hfx .OR. do_rpa_ri_exx) THEN
     968          108 :          CALL get_qs_env(qs_env, nkind=nkind)
     969          108 :          CALL section_vals_val_get(qs_env%input, "DFT%SORT_BASIS", i_val=sort_basis)
     970          290 :          DO ikind = 1, nkind
     971          182 :             NULLIFY (ri_hfx_basis)
     972          182 :             qs_kind => qs_kind_set(ikind)
     973              :             CALL get_qs_kind(qs_kind=qs_kind, basis_set=ri_hfx_basis, &
     974          182 :                              basis_type="RI_HFX")
     975         7626 :             IF (.NOT. (ASSOCIATED(ri_hfx_basis))) THEN
     976          178 :                CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto)
     977          178 :                IF (dft_control%do_admm) THEN
     978              :                   CALL create_ri_aux_basis_set(ri_hfx_basis, qs_kind, dft_control%auto_basis_ri_hfx, &
     979           58 :                                                basis_type="AUX_FIT", basis_sort=sort_basis)
     980              :                ELSE
     981              :                   CALL create_ri_aux_basis_set(ri_hfx_basis, qs_kind, dft_control%auto_basis_ri_hfx, &
     982          120 :                                                basis_sort=sort_basis)
     983              :                END IF
     984          178 :                CALL add_basis_set_to_container(qs_kind%basis_sets, ri_hfx_basis, "RI_HFX")
     985              :             END IF
     986              :          END DO
     987              :       END IF
     988              : 
     989         7444 :       IF (dft_control%qs_control%method_id == do_method_rigpw) THEN
     990              :          ! Check if RI_HXC basis is available, auto-generate if needed
     991            0 :          CALL get_qs_env(qs_env, nkind=nkind)
     992            0 :          DO ikind = 1, nkind
     993            0 :             NULLIFY (ri_hfx_basis)
     994            0 :             qs_kind => qs_kind_set(ikind)
     995            0 :             CALL get_qs_kind(qs_kind, basis_set=ri_hfx_basis, basis_type="RI_HXC")
     996            0 :             IF (.NOT. (ASSOCIATED(ri_hfx_basis))) THEN
     997              :                ! Generate a default basis
     998            0 :                CALL create_ri_aux_basis_set(ri_hfx_basis, qs_kind, dft_control%auto_basis_ri_hxc)
     999            0 :                CALL add_basis_set_to_container(qs_kind%basis_sets, ri_hfx_basis, "RI_HXC")
    1000              :             END IF
    1001              :          END DO
    1002              :       END IF
    1003              : 
    1004              :       ! Harris method
    1005         7444 :       NULLIFY (harris_env)
    1006              :       CALL section_vals_val_get(dft_section, "HARRIS_METHOD%_SECTION_PARAMETERS_", &
    1007         7444 :                                 l_val=qs_env%harris_method)
    1008         7444 :       harris_section => section_vals_get_subs_vals(dft_section, "HARRIS_METHOD")
    1009         7444 :       CALL harris_env_create(qs_env, harris_env, harris_section)
    1010         7444 :       CALL set_qs_env(qs_env, harris_env=harris_env)
    1011              :       !
    1012         7444 :       IF (qs_env%harris_method) THEN
    1013            6 :          CALL get_qs_env(qs_env, nkind=nkind)
    1014              :          ! Check if RI_HXC basis is available, auto-generate if needed
    1015           22 :          DO ikind = 1, nkind
    1016           16 :             NULLIFY (tmp_basis_set)
    1017           16 :             qs_kind => qs_kind_set(ikind)
    1018           16 :             CALL get_qs_kind(qs_kind, basis_set=rhoin_basis, basis_type="RHOIN")
    1019           22 :             IF (.NOT. (ASSOCIATED(rhoin_basis))) THEN
    1020              :                ! Generate a default basis
    1021           16 :                CALL create_ri_aux_basis_set(tmp_basis_set, qs_kind, dft_control%auto_basis_ri_hxc)
    1022           16 :                IF (qs_env%harris_env%density_source == hden_atomic) THEN
    1023           16 :                   CALL create_primitive_basis_set(tmp_basis_set, rhoin_basis, lmax=0)
    1024           16 :                   CALL deallocate_gto_basis_set(tmp_basis_set)
    1025              :                ELSE
    1026            0 :                   rhoin_basis => tmp_basis_set
    1027              :                END IF
    1028           16 :                CALL add_basis_set_to_container(qs_kind%basis_sets, rhoin_basis, "RHOIN")
    1029              :             END IF
    1030              :          END DO
    1031              :       END IF
    1032              : 
    1033         7444 :       mp2_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC%WF_CORRELATION")
    1034         7444 :       CALL section_vals_get(mp2_section, explicit=mp2_present)
    1035         7444 :       IF (mp2_present) THEN
    1036              : 
    1037              :          ! basis should be sorted for imaginary time RPA/GW
    1038          470 :          CALL section_vals_val_get(qs_env%input, "DFT%SORT_BASIS", i_val=sort_basis)
    1039              :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%LOW_SCALING%_SECTION_PARAMETERS_", &
    1040          470 :                                    l_val=do_wfc_im_time)
    1041              : 
    1042          470 :          IF (do_wfc_im_time .AND. sort_basis /= basis_sort_zet) THEN
    1043              :             CALL cp_warn(__LOCATION__, &
    1044           10 :                          "Low-scaling RPA requires SORT_BASIS EXP keyword (in DFT input section) for good performance")
    1045              :          END IF
    1046              : 
    1047              :          ! Check if RI_AUX basis (for MP2/RPA) is given, auto-generate if not
    1048          470 :          CALL mp2_env_create(qs_env%mp2_env)
    1049          470 :          CALL get_qs_env(qs_env, mp2_env=mp2_env, nkind=nkind)
    1050          470 :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_MP2%_SECTION_PARAMETERS_", l_val=do_ri_mp2)
    1051          470 :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_SOS_MP2%_SECTION_PARAMETERS_", l_val=do_ri_sos_mp2)
    1052          470 :          CALL section_vals_val_get(qs_env%input, "DFT%XC%WF_CORRELATION%RI_RPA%_SECTION_PARAMETERS_", l_val=do_ri_rpa)
    1053          470 :          IF (do_ri_mp2 .OR. do_ri_sos_mp2 .OR. do_ri_rpa) THEN
    1054         1264 :             DO ikind = 1, nkind
    1055          832 :                NULLIFY (ri_aux_basis_set)
    1056          832 :                qs_kind => qs_kind_set(ikind)
    1057              :                CALL get_qs_kind(qs_kind=qs_kind, basis_set=ri_aux_basis_set, &
    1058          832 :                                 basis_type="RI_AUX")
    1059         1302 :                IF (.NOT. (ASSOCIATED(ri_aux_basis_set))) THEN
    1060              :                   ! RI_AUX basis set is not yet loaded
    1061              :                   ! Generate a default basis
    1062            8 :                   CALL create_ri_aux_basis_set(ri_aux_basis_set, qs_kind, dft_control%auto_basis_ri_aux, basis_sort=sort_basis)
    1063            8 :                   CALL add_basis_set_to_container(qs_kind%basis_sets, ri_aux_basis_set, "RI_AUX")
    1064              :                   ! Add a flag, which allows to check if the basis was generated
    1065              :                   !  when applying ERI_METHOD OS to mp2, ri-rpa, gw etc
    1066            8 :                   qs_env%mp2_env%ri_aux_auto_generated = .TRUE.
    1067              :                END IF
    1068              :             END DO
    1069              :          END IF
    1070              : 
    1071              :       END IF
    1072              : 
    1073         7444 :       IF (dft_control%do_xas_tdp_calculation .OR. qs_env%do_rixs) THEN
    1074              :          ! Check if RI_XAS basis is given, auto-generate if not
    1075           64 :          CALL get_qs_env(qs_env, nkind=nkind)
    1076          166 :          DO ikind = 1, nkind
    1077          102 :             NULLIFY (ri_xas_basis)
    1078          102 :             qs_kind => qs_kind_set(ikind)
    1079          102 :             CALL get_qs_kind(qs_kind, basis_Set=ri_xas_basis, basis_type="RI_XAS")
    1080         7546 :             IF (.NOT. ASSOCIATED(ri_xas_basis)) THEN
    1081              :                ! Generate a default basis
    1082           98 :                CALL create_ri_aux_basis_set(ri_xas_basis, qs_kind, dft_control%auto_basis_ri_xas)
    1083           98 :                CALL add_basis_set_to_container(qs_kind%basis_sets, ri_xas_basis, "RI_XAS")
    1084              :             END IF
    1085              :          END DO
    1086              :       END IF
    1087              : 
    1088              :       !   *** Initialize the spherical harmonics and ***
    1089              :       !   *** the orbital transformation matrices    ***
    1090         7444 :       CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto, maxlppl=maxlppl, maxlppnl=maxlppnl)
    1091              : 
    1092              :       ! CNEO nuclear basis contributes to GAPW rho0
    1093         7444 :       IF (cneo_potential_present) THEN
    1094            8 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_nuc, basis_type="NUC")
    1095            8 :          maxlgto = MAX(maxlgto, maxlgto_nuc)
    1096              :       END IF
    1097         7444 :       lmax_sphere = dft_control%qs_control%gapw_control%lmax_sphere
    1098         7444 :       IF (lmax_sphere < 0) THEN
    1099         7320 :          lmax_sphere = 2*maxlgto
    1100         7320 :          dft_control%qs_control%gapw_control%lmax_sphere = lmax_sphere
    1101              :       END IF
    1102         7444 :       IF (dft_control%qs_control%method_id == do_method_lrigpw .OR. dft_control%qs_control%lri_optbas) THEN
    1103           46 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_lri, basis_type="LRI_AUX")
    1104              :          !take maxlgto from lri basis if larger (usually)
    1105           46 :          maxlgto = MAX(maxlgto, maxlgto_lri)
    1106         7398 :       ELSE IF (dft_control%qs_control%method_id == do_method_rigpw) THEN
    1107            0 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_lri, basis_type="RI_HXC")
    1108            0 :          maxlgto = MAX(maxlgto, maxlgto_lri)
    1109              :       END IF
    1110         7444 :       IF (dft_control%do_xas_tdp_calculation .OR. qs_env%do_rixs) THEN
    1111              :          !done as a precaution
    1112           64 :          CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto_lri, basis_type="RI_XAS")
    1113           64 :          maxlgto = MAX(maxlgto, maxlgto_lri)
    1114              :       END IF
    1115         7444 :       maxl = MAX(2*maxlgto, maxlppl, maxlppnl, lmax_sphere) + 1
    1116              : 
    1117         7444 :       CALL init_orbital_pointers(maxl)
    1118              : 
    1119         7444 :       CALL init_spherical_harmonics(maxl, 0)
    1120              : 
    1121              :       !   *** Initialise the qs_kind_set ***
    1122         7444 :       CALL init_qs_kind_set(qs_kind_set)
    1123              : 
    1124              :       !   *** Initialise GAPW soft basis and projectors
    1125         7444 :       IF (dft_control%qs_control%method_id == do_method_gapw .OR. &
    1126              :           dft_control%qs_control%method_id == do_method_gapw_xc) THEN
    1127          996 :          qs_control => dft_control%qs_control
    1128          996 :          CALL init_gapw_basis_set(qs_kind_set, qs_control, qs_env%input)
    1129              :       END IF
    1130              : 
    1131              :       !   *** Initialise CNEO nuclear soft basis
    1132         7444 :       IF (cneo_potential_present) THEN
    1133            8 :          CALL init_cneo_basis_set(qs_kind_set, qs_control)
    1134              :       END IF
    1135              : 
    1136              :       !   *** Initialize the pretabulation for the calculation of the   ***
    1137              :       !   *** incomplete Gamma function F_n(t) after McMurchie-Davidson ***
    1138         7444 :       CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto)
    1139         7444 :       maxl = MAX(3*maxlgto + 1, 0)
    1140         7444 :       CALL init_md_ftable(maxl)
    1141              : 
    1142              :       !   *** Initialize the atomic interaction radii ***
    1143         7444 :       CALL init_interaction_radii(dft_control%qs_control, qs_kind_set)
    1144              :       !
    1145         7444 :       IF (dft_control%qs_control%method_id == do_method_xtb) THEN
    1146          944 :          IF (.NOT. dft_control%qs_control%xtb_control%do_tblite) THEN
    1147              :             ! cutoff radius
    1148         3040 :             DO ikind = 1, nkind
    1149         2096 :                qs_kind => qs_kind_set(ikind)
    1150         3040 :                IF (qs_kind%xtb_parameter%defined) THEN
    1151         2094 :                   CALL get_qs_kind(qs_kind, basis_set=tmp_basis_set)
    1152         2094 :                   rcut = xtb_control%coulomb_sr_cut
    1153         2094 :                   fxx = 2.0_dp*xtb_control%coulomb_sr_eps*qs_kind%xtb_parameter%eta**2
    1154         2094 :                   fxx = 0.80_dp*(1.0_dp/fxx)**0.3333_dp
    1155         2094 :                   rcut = MIN(rcut, xtb_control%coulomb_sr_cut)
    1156         2094 :                   qs_kind%xtb_parameter%rcut = MIN(rcut, fxx)
    1157              :                ELSE
    1158            2 :                   qs_kind%xtb_parameter%rcut = 0.0_dp
    1159              :                END IF
    1160              :             END DO
    1161              :          END IF
    1162              :       END IF
    1163              : 
    1164         7444 :       IF (.NOT. be_silent) THEN
    1165         7438 :          CALL write_pgf_orb_radii("orb", atomic_kind_set, qs_kind_set, subsys_section)
    1166         7438 :          CALL write_pgf_orb_radii("aux", atomic_kind_set, qs_kind_set, subsys_section)
    1167         7438 :          CALL write_pgf_orb_radii("lri", atomic_kind_set, qs_kind_set, subsys_section)
    1168         7438 :          CALL write_pgf_orb_radii("nuc", atomic_kind_set, qs_kind_set, subsys_section)
    1169         7438 :          CALL write_core_charge_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1170         7438 :          CALL write_ppl_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1171         7438 :          CALL write_ppnl_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1172         7438 :          CALL write_paw_radii(atomic_kind_set, qs_kind_set, subsys_section)
    1173              :       END IF
    1174              : 
    1175              :       !   *** Distribute molecules and atoms using the new data structures ***
    1176              :       CALL distribute_molecules_1d(atomic_kind_set=atomic_kind_set, &
    1177              :                                    particle_set=particle_set, &
    1178              :                                    local_particles=local_particles, &
    1179              :                                    molecule_kind_set=molecule_kind_set, &
    1180              :                                    molecule_set=molecule_set, &
    1181              :                                    local_molecules=local_molecules, &
    1182         7444 :                                    force_env_section=qs_env%input)
    1183              : 
    1184              :       !   *** SCF parameters ***
    1185       215876 :       ALLOCATE (scf_control)
    1186              :       ! set (non)-self consistency
    1187         7444 :       IF (dft_control%qs_control%dftb) THEN
    1188          222 :          scf_control%non_selfconsistent = .NOT. dft_control%qs_control%dftb_control%self_consistent
    1189              :       END IF
    1190         7444 :       IF (dft_control%qs_control%xtb) THEN
    1191          944 :          IF (dft_control%qs_control%xtb_control%do_tblite) THEN
    1192            0 :             scf_control%non_selfconsistent = .FALSE.
    1193              :          ELSE
    1194          944 :             scf_control%non_selfconsistent = (dft_control%qs_control%xtb_control%gfn_type == 0)
    1195              :          END IF
    1196              :       END IF
    1197         7444 :       IF (qs_env%harris_method) THEN
    1198            6 :          scf_control%non_selfconsistent = .TRUE.
    1199              :       END IF
    1200         7444 :       CALL scf_c_create(scf_control)
    1201         7444 :       CALL scf_c_read_parameters(scf_control, dft_section)
    1202              :       !   *** Allocate the data structure for Quickstep energies ***
    1203         7444 :       CALL allocate_qs_energy(energy)
    1204              : 
    1205              :       ! check for orthogonal basis
    1206         7444 :       has_unit_metric = .FALSE.
    1207         7444 :       IF (dft_control%qs_control%semi_empirical) THEN
    1208          998 :          IF (dft_control%qs_control%se_control%orthogonal_basis) has_unit_metric = .TRUE.
    1209              :       END IF
    1210         7444 :       IF (dft_control%qs_control%dftb) THEN
    1211          222 :          IF (dft_control%qs_control%dftb_control%orthogonal_basis) has_unit_metric = .TRUE.
    1212              :       END IF
    1213         7444 :       CALL set_qs_env(qs_env, has_unit_metric=has_unit_metric)
    1214              : 
    1215              :       !   *** Activate the interpolation ***
    1216              :       CALL wfi_create(wf_history, &
    1217              :                       interpolation_method_nr= &
    1218              :                       dft_control%qs_control%wf_interpolation_method_nr, &
    1219              :                       extrapolation_order=dft_control%qs_control%wf_extrapolation_order, &
    1220         7444 :                       has_unit_metric=has_unit_metric)
    1221              : 
    1222              :       !   *** Set the current Quickstep environment ***
    1223              :       CALL set_qs_env(qs_env=qs_env, &
    1224              :                       scf_control=scf_control, &
    1225         7444 :                       wf_history=wf_history)
    1226              : 
    1227              :       CALL qs_subsys_set(subsys, &
    1228              :                          cell_ref=cell_ref, &
    1229              :                          use_ref_cell=use_ref_cell, &
    1230              :                          energy=energy, &
    1231         7444 :                          force=force)
    1232              : 
    1233         7444 :       CALL get_qs_env(qs_env, ks_env=ks_env)
    1234         7444 :       CALL set_ks_env(ks_env, dft_control=dft_control)
    1235              : 
    1236              :       CALL qs_subsys_set(subsys, local_molecules=local_molecules, &
    1237         7444 :                          local_particles=local_particles, cell=cell)
    1238              : 
    1239         7444 :       CALL distribution_1d_release(local_particles)
    1240         7444 :       CALL distribution_1d_release(local_molecules)
    1241         7444 :       CALL wfi_release(wf_history)
    1242              : 
    1243              :       CALL get_qs_env(qs_env=qs_env, &
    1244              :                       atomic_kind_set=atomic_kind_set, &
    1245              :                       dft_control=dft_control, &
    1246         7444 :                       scf_control=scf_control)
    1247              : 
    1248              :       ! decide what conditions need mo_derivs
    1249              :       ! right now, this only appears to be OT
    1250         7444 :       IF (dft_control%qs_control%do_ls_scf .OR. &
    1251              :           dft_control%qs_control%do_almo_scf) THEN
    1252          404 :          CALL set_qs_env(qs_env=qs_env, requires_mo_derivs=.FALSE.)
    1253              :       ELSE
    1254         7040 :          IF (scf_control%use_ot) THEN
    1255         2052 :             CALL set_qs_env(qs_env=qs_env, requires_mo_derivs=.TRUE.)
    1256              :          ELSE
    1257         4988 :             CALL set_qs_env(qs_env=qs_env, requires_mo_derivs=.FALSE.)
    1258              :          END IF
    1259              :       END IF
    1260              : 
    1261              :       ! XXXXXXX this is backwards XXXXXXXX
    1262         7444 :       dft_control%smear = scf_control%smear%do_smear
    1263              : 
    1264              :       ! Periodic efield needs equal occupation and orbital gradients
    1265         7444 :       IF (.NOT. (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb)) THEN
    1266         6278 :          IF (dft_control%apply_period_efield) THEN
    1267           30 :             CALL get_qs_env(qs_env=qs_env, requires_mo_derivs=orb_gradient)
    1268           30 :             IF (.NOT. orb_gradient) THEN
    1269              :                CALL cp_abort(__LOCATION__, "Periodic Efield needs orbital gradient and direct optimization."// &
    1270            0 :                              " Use the OT optimization method.")
    1271              :             END IF
    1272           30 :             IF (dft_control%smear) THEN
    1273              :                CALL cp_abort(__LOCATION__, "Periodic Efield needs equal occupation numbers."// &
    1274            0 :                              " Smearing option is not possible.")
    1275              :             END IF
    1276              :          END IF
    1277              :       END IF
    1278              : 
    1279              :       !   Initialize the GAPW local densities and potentials
    1280         7444 :       IF (dft_control%qs_control%method_id == do_method_gapw .OR. &
    1281              :           dft_control%qs_control%method_id == do_method_gapw_xc) THEN
    1282              :          !     *** Allocate and initialize the set of atomic densities ***
    1283          996 :          NULLIFY (rho_atom_set)
    1284          996 :          gapw_control => dft_control%qs_control%gapw_control
    1285          996 :          CALL init_rho_atom(rho_atom_set, atomic_kind_set, qs_kind_set, dft_control, para_env)
    1286          996 :          CALL set_qs_env(qs_env=qs_env, rho_atom_set=rho_atom_set)
    1287          996 :          IF (dft_control%qs_control%method_id /= do_method_gapw_xc) THEN
    1288          872 :             CALL get_qs_env(qs_env=qs_env, local_rho_set=local_rho_set, natom=natom)
    1289              :             !       *** Allocate and initialize the compensation density rho0 ***
    1290          872 :             CALL init_rho0(local_rho_set, qs_env, gapw_control)
    1291              :             !       *** Allocate and Initialize the local coulomb term ***
    1292          872 :             CALL init_coulomb_local(qs_env%hartree_local, natom)
    1293              :          END IF
    1294              :          ! NLCC
    1295          996 :          CALL init_gapw_nlcc(qs_kind_set)
    1296         6448 :       ELSE IF (dft_control%qs_control%method_id == do_method_lrigpw) THEN
    1297              :          ! allocate local ri environment
    1298              :          ! nothing to do here?
    1299         6408 :       ELSE IF (dft_control%qs_control%method_id == do_method_rigpw) THEN
    1300              :          ! allocate ri environment
    1301              :          ! nothing to do here?
    1302         6408 :       ELSE IF (dft_control%qs_control%semi_empirical) THEN
    1303          998 :          NULLIFY (se_store_int_env, se_nddo_mpole, se_nonbond_env)
    1304          998 :          natom = SIZE(particle_set)
    1305          998 :          se_section => section_vals_get_subs_vals(qs_section, "SE")
    1306          998 :          se_control => dft_control%qs_control%se_control
    1307              : 
    1308              :          ! Make the cutoff radii choice a bit smarter
    1309          998 :          CALL se_cutoff_compatible(se_control, se_section, cell, output_unit)
    1310              : 
    1311         1994 :          SELECT CASE (dft_control%qs_control%method_id)
    1312              :          CASE DEFAULT
    1313              :          CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pm3, &
    1314              :                do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
    1315              :             ! Neighbor lists have to be MAX(interaction range, orbital range)
    1316              :             ! set new kind radius
    1317          998 :             CALL init_se_nlradius(se_control, atomic_kind_set, qs_kind_set, subsys_section)
    1318              :          END SELECT
    1319              :          ! Initialize to zero the max multipole to treat in the EWALD scheme..
    1320          998 :          se_control%max_multipole = do_multipole_none
    1321              :          ! check for Ewald
    1322          998 :          IF (se_control%do_ewald .OR. se_control%do_ewald_gks) THEN
    1323          512 :             ALLOCATE (ewald_env)
    1324           32 :             CALL ewald_env_create(ewald_env, para_env)
    1325           32 :             poisson_section => section_vals_get_subs_vals(dft_section, "POISSON")
    1326           32 :             CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
    1327           32 :             ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
    1328              :             print_section => section_vals_get_subs_vals(qs_env%input, &
    1329           32 :                                                         "PRINT%GRID_INFORMATION")
    1330           32 :             CALL read_ewald_section(ewald_env, ewald_section)
    1331              :             ! Create ewald grids
    1332           32 :             ALLOCATE (ewald_pw)
    1333              :             CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, &
    1334           32 :                                  print_section=print_section)
    1335              :             ! Initialize ewald grids
    1336           32 :             CALL ewald_pw_grid_update(ewald_pw, ewald_env, cell%hmat)
    1337              :             ! Setup the nonbond environment (real space part of Ewald)
    1338           32 :             CALL ewald_env_get(ewald_env, rcut=ewald_rcut)
    1339              :             ! Setup the maximum level of multipoles to be treated in the periodic SE scheme
    1340           32 :             IF (se_control%do_ewald) THEN
    1341           30 :                CALL ewald_env_get(ewald_env, max_multipole=se_control%max_multipole)
    1342              :             END IF
    1343              :             CALL section_vals_val_get(se_section, "NEIGHBOR_LISTS%VERLET_SKIN", &
    1344           32 :                                       r_val=verlet_skin)
    1345           32 :             ALLOCATE (se_nonbond_env)
    1346              :             CALL fist_nonbond_env_create(se_nonbond_env, atomic_kind_set, do_nonbonded=.TRUE., &
    1347              :                                          do_electrostatics=.TRUE., verlet_skin=verlet_skin, ewald_rcut=ewald_rcut, &
    1348           32 :                                          ei_scale14=0.0_dp, vdw_scale14=0.0_dp, shift_cutoff=.FALSE.)
    1349              :             ! Create and Setup NDDO multipole environment
    1350           32 :             CALL nddo_mpole_setup(se_nddo_mpole, natom)
    1351              :             CALL set_qs_env(qs_env, ewald_env=ewald_env, ewald_pw=ewald_pw, &
    1352           32 :                             se_nonbond_env=se_nonbond_env, se_nddo_mpole=se_nddo_mpole)
    1353              :             ! Handle the residual integral part 1/R^3
    1354              :             CALL semi_empirical_expns3_setup(qs_kind_set, se_control, &
    1355           32 :                                              dft_control%qs_control%method_id)
    1356              :          END IF
    1357              :          ! Taper function
    1358              :          CALL se_taper_create(se_taper, se_control%integral_screening, se_control%do_ewald, &
    1359              :                               se_control%taper_cou, se_control%range_cou, &
    1360              :                               se_control%taper_exc, se_control%range_exc, &
    1361              :                               se_control%taper_scr, se_control%range_scr, &
    1362          998 :                               se_control%taper_lrc, se_control%range_lrc)
    1363          998 :          CALL set_qs_env(qs_env, se_taper=se_taper)
    1364              :          ! Store integral environment
    1365          998 :          CALL semi_empirical_si_create(se_store_int_env, se_section)
    1366          998 :          CALL set_qs_env(qs_env, se_store_int_env=se_store_int_env)
    1367              :       END IF
    1368              : 
    1369              :       !   Initialize possible dispersion parameters
    1370              :       IF (dft_control%qs_control%method_id == do_method_gpw .OR. &
    1371              :           dft_control%qs_control%method_id == do_method_gapw .OR. &
    1372              :           dft_control%qs_control%method_id == do_method_gapw_xc .OR. &
    1373              :           dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    1374         7444 :           dft_control%qs_control%method_id == do_method_rigpw .OR. &
    1375              :           dft_control%qs_control%method_id == do_method_ofgpw) THEN
    1376        26400 :          ALLOCATE (dispersion_env)
    1377         5280 :          NULLIFY (xc_section)
    1378         5280 :          xc_section => section_vals_get_subs_vals(dft_section, "XC")
    1379         5280 :          CALL qs_dispersion_env_set(dispersion_env, xc_section)
    1380         5280 :          IF (dispersion_env%type == xc_vdw_fun_pairpot) THEN
    1381          112 :             NULLIFY (pp_section)
    1382          112 :             pp_section => section_vals_get_subs_vals(xc_section, "VDW_POTENTIAL%PAIR_POTENTIAL")
    1383          112 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, pp_section, para_env)
    1384         5168 :          ELSE IF (dispersion_env%type == xc_vdw_fun_nonloc) THEN
    1385           46 :             NULLIFY (nl_section)
    1386           46 :             nl_section => section_vals_get_subs_vals(xc_section, "VDW_POTENTIAL%NON_LOCAL")
    1387           46 :             CALL qs_dispersion_nonloc_init(dispersion_env, para_env)
    1388              :          END IF
    1389         5280 :          CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1390         2164 :       ELSE IF (dft_control%qs_control%method_id == do_method_dftb) THEN
    1391         1110 :          ALLOCATE (dispersion_env)
    1392              :          ! set general defaults
    1393              :          dispersion_env%doabc = .FALSE.
    1394              :          dispersion_env%c9cnst = .FALSE.
    1395              :          dispersion_env%lrc = .FALSE.
    1396              :          dispersion_env%srb = .FALSE.
    1397              :          dispersion_env%verbose = .FALSE.
    1398              :          NULLIFY (dispersion_env%c6ab, dispersion_env%maxci, dispersion_env%r0ab, dispersion_env%rcov, &
    1399              :                   dispersion_env%r2r4, dispersion_env%cn, dispersion_env%cnkind, dispersion_env%cnlist, &
    1400              :                   dispersion_env%d3_exclude_pair)
    1401              :          NULLIFY (dispersion_env%q_mesh, dispersion_env%kernel, dispersion_env%d2phi_dk2, &
    1402              :                   dispersion_env%d2y_dx2, dispersion_env%dftd_section)
    1403              :          NULLIFY (dispersion_env%sab_vdw, dispersion_env%sab_cn)
    1404          222 :          IF (dftb_control%dispersion .AND. dftb_control%dispersion_type == dispersion_d3) THEN
    1405           14 :             dispersion_env%type = xc_vdw_fun_pairpot
    1406           14 :             dispersion_env%pp_type = vdw_pairpot_dftd3
    1407           14 :             dispersion_env%eps_cn = dftb_control%epscn
    1408           14 :             dispersion_env%s6 = dftb_control%sd3(1)
    1409           14 :             dispersion_env%sr6 = dftb_control%sd3(2)
    1410           14 :             dispersion_env%s8 = dftb_control%sd3(3)
    1411              :             dispersion_env%domol = .FALSE.
    1412           14 :             dispersion_env%kgc8 = 0._dp
    1413           14 :             dispersion_env%rc_disp = dftb_control%rcdisp
    1414           14 :             dispersion_env%exp_pre = 0._dp
    1415           14 :             dispersion_env%scaling = 0._dp
    1416           14 :             dispersion_env%nd3_exclude_pair = 0
    1417           14 :             dispersion_env%parameter_file_name = dftb_control%dispersion_parameter_file
    1418           14 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1419          208 :          ELSEIF (dftb_control%dispersion .AND. dftb_control%dispersion_type == dispersion_d3bj) THEN
    1420            2 :             dispersion_env%type = xc_vdw_fun_pairpot
    1421            2 :             dispersion_env%pp_type = vdw_pairpot_dftd3bj
    1422            2 :             dispersion_env%eps_cn = dftb_control%epscn
    1423            2 :             dispersion_env%s6 = dftb_control%sd3bj(1)
    1424            2 :             dispersion_env%a1 = dftb_control%sd3bj(2)
    1425            2 :             dispersion_env%s8 = dftb_control%sd3bj(3)
    1426            2 :             dispersion_env%a2 = dftb_control%sd3bj(4)
    1427              :             dispersion_env%domol = .FALSE.
    1428            2 :             dispersion_env%kgc8 = 0._dp
    1429            2 :             dispersion_env%rc_disp = dftb_control%rcdisp
    1430            2 :             dispersion_env%exp_pre = 0._dp
    1431            2 :             dispersion_env%scaling = 0._dp
    1432            2 :             dispersion_env%nd3_exclude_pair = 0
    1433            2 :             dispersion_env%parameter_file_name = dftb_control%dispersion_parameter_file
    1434            2 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1435          206 :          ELSEIF (dftb_control%dispersion .AND. dftb_control%dispersion_type == dispersion_d2) THEN
    1436            2 :             dispersion_env%type = xc_vdw_fun_pairpot
    1437            2 :             dispersion_env%pp_type = vdw_pairpot_dftd2
    1438            2 :             dispersion_env%exp_pre = dftb_control%exp_pre
    1439            2 :             dispersion_env%scaling = dftb_control%scaling
    1440            2 :             dispersion_env%parameter_file_name = dftb_control%dispersion_parameter_file
    1441            2 :             dispersion_env%rc_disp = dftb_control%rcdisp
    1442            2 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1443              :          ELSE
    1444          204 :             dispersion_env%type = xc_vdw_fun_none
    1445              :          END IF
    1446          222 :          CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1447         1942 :       ELSE IF (dft_control%qs_control%method_id == do_method_xtb) THEN
    1448          944 :          IF (.NOT. (dft_control%qs_control%xtb_control%do_tblite)) THEN
    1449         4720 :             ALLOCATE (dispersion_env)
    1450              :             ! set general defaults
    1451              :             dispersion_env%doabc = .FALSE.
    1452              :             dispersion_env%c9cnst = .FALSE.
    1453              :             dispersion_env%lrc = .FALSE.
    1454              :             dispersion_env%srb = .FALSE.
    1455              :             dispersion_env%verbose = .FALSE.
    1456              :             NULLIFY (dispersion_env%c6ab, dispersion_env%maxci, &
    1457              :                      dispersion_env%r0ab, dispersion_env%rcov, &
    1458              :                      dispersion_env%r2r4, dispersion_env%cn, &
    1459              :                      dispersion_env%cnkind, dispersion_env%cnlist, &
    1460              :                      dispersion_env%d3_exclude_pair)
    1461              :             NULLIFY (dispersion_env%q_mesh, dispersion_env%kernel, dispersion_env%d2phi_dk2, &
    1462              :                      dispersion_env%d2y_dx2, dispersion_env%dftd_section)
    1463              :             NULLIFY (dispersion_env%sab_vdw, dispersion_env%sab_cn)
    1464          944 :             dispersion_env%type = xc_vdw_fun_pairpot
    1465          944 :             dispersion_env%eps_cn = xtb_control%epscn
    1466          944 :             dispersion_env%s6 = xtb_control%s6
    1467          944 :             dispersion_env%s8 = xtb_control%s8
    1468          944 :             dispersion_env%a1 = xtb_control%a1
    1469          944 :             dispersion_env%a2 = xtb_control%a2
    1470              :             dispersion_env%domol = .FALSE.
    1471          944 :             dispersion_env%kgc8 = 0._dp
    1472          944 :             dispersion_env%rc_disp = xtb_control%rcdisp
    1473          944 :             dispersion_env%rc_d4 = xtb_control%rcdisp
    1474          944 :             dispersion_env%exp_pre = 0._dp
    1475          944 :             dispersion_env%scaling = 0._dp
    1476          944 :             dispersion_env%nd3_exclude_pair = 0
    1477          944 :             dispersion_env%parameter_file_name = xtb_control%dispersion_parameter_file
    1478              :             !
    1479         1248 :             SELECT CASE (xtb_control%vdw_type)
    1480              :             CASE (xtb_vdw_type_none, xtb_vdw_type_d3)
    1481          304 :                dispersion_env%pp_type = vdw_pairpot_dftd3bj
    1482          304 :                CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1483          304 :                IF (xtb_control%vdw_type == xtb_vdw_type_none) dispersion_env%type = xc_vdw_fun_none
    1484              :             CASE (xtb_vdw_type_d4)
    1485          640 :                dispersion_env%pp_type = vdw_pairpot_dftd4
    1486          640 :                dispersion_env%ref_functional = "none"
    1487              :                CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, &
    1488          640 :                                                dispersion_env, para_env=para_env)
    1489          640 :                dispersion_env%cnfun = 2
    1490              :             CASE DEFAULT
    1491          944 :                CPABORT("vdw type")
    1492              :             END SELECT
    1493          944 :             CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1494              :          END IF
    1495          998 :       ELSE IF (dft_control%qs_control%semi_empirical) THEN
    1496         4990 :          ALLOCATE (dispersion_env)
    1497              :          ! set general defaults
    1498              :          dispersion_env%doabc = .FALSE.
    1499              :          dispersion_env%c9cnst = .FALSE.
    1500              :          dispersion_env%lrc = .FALSE.
    1501              :          dispersion_env%srb = .FALSE.
    1502              :          dispersion_env%verbose = .FALSE.
    1503              :          NULLIFY (dispersion_env%c6ab, dispersion_env%maxci, dispersion_env%r0ab, dispersion_env%rcov, &
    1504              :                   dispersion_env%r2r4, dispersion_env%cn, dispersion_env%cnkind, dispersion_env%cnlist, &
    1505              :                   dispersion_env%d3_exclude_pair)
    1506              :          NULLIFY (dispersion_env%q_mesh, dispersion_env%kernel, dispersion_env%d2phi_dk2, &
    1507              :                   dispersion_env%d2y_dx2, dispersion_env%dftd_section)
    1508              :          NULLIFY (dispersion_env%sab_vdw, dispersion_env%sab_cn)
    1509          998 :          IF (se_control%dispersion) THEN
    1510            6 :             dispersion_env%type = xc_vdw_fun_pairpot
    1511            6 :             dispersion_env%pp_type = vdw_pairpot_dftd3
    1512            6 :             dispersion_env%eps_cn = se_control%epscn
    1513            6 :             dispersion_env%s6 = se_control%sd3(1)
    1514            6 :             dispersion_env%sr6 = se_control%sd3(2)
    1515            6 :             dispersion_env%s8 = se_control%sd3(3)
    1516              :             dispersion_env%domol = .FALSE.
    1517            6 :             dispersion_env%kgc8 = 0._dp
    1518            6 :             dispersion_env%rc_disp = se_control%rcdisp
    1519            6 :             dispersion_env%exp_pre = 0._dp
    1520            6 :             dispersion_env%scaling = 0._dp
    1521            6 :             dispersion_env%nd3_exclude_pair = 0
    1522            6 :             dispersion_env%parameter_file_name = se_control%dispersion_parameter_file
    1523            6 :             CALL qs_dispersion_pairpot_init(atomic_kind_set, qs_kind_set, dispersion_env, para_env=para_env)
    1524              :          ELSE
    1525          992 :             dispersion_env%type = xc_vdw_fun_none
    1526              :          END IF
    1527          998 :          CALL set_qs_env(qs_env, dispersion_env=dispersion_env)
    1528              :       END IF
    1529              : 
    1530              :       ! Initialize possible geomertical counterpoise correction potential
    1531              :       IF (dft_control%qs_control%method_id == do_method_gpw .OR. &
    1532              :           dft_control%qs_control%method_id == do_method_gapw .OR. &
    1533              :           dft_control%qs_control%method_id == do_method_gapw_xc .OR. &
    1534              :           dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    1535         7444 :           dft_control%qs_control%method_id == do_method_rigpw .OR. &
    1536              :           dft_control%qs_control%method_id == do_method_ofgpw) THEN
    1537         5280 :          ALLOCATE (gcp_env)
    1538         5280 :          NULLIFY (xc_section)
    1539         5280 :          xc_section => section_vals_get_subs_vals(dft_section, "XC")
    1540         5280 :          CALL qs_gcp_env_set(gcp_env, xc_section)
    1541         5280 :          CALL qs_gcp_init(qs_env, gcp_env)
    1542         5280 :          CALL set_qs_env(qs_env, gcp_env=gcp_env)
    1543              :       END IF
    1544              : 
    1545              :       !   *** Allocate the MO data types ***
    1546         7444 :       CALL get_qs_kind_set(qs_kind_set, nsgf=n_ao, nelectron=nelectron)
    1547              : 
    1548              :       ! the total number of electrons
    1549         7444 :       nelectron = nelectron - dft_control%charge
    1550              : 
    1551         7444 :       IF (dft_control%multiplicity == 0) THEN
    1552         6222 :          IF (MODULO(nelectron, 2) == 0) THEN
    1553         5745 :             dft_control%multiplicity = 1
    1554              :          ELSE
    1555          477 :             dft_control%multiplicity = 2
    1556              :          END IF
    1557              :       END IF
    1558              : 
    1559         7444 :       multiplicity = dft_control%multiplicity
    1560              : 
    1561         7444 :       IF ((dft_control%nspins < 1) .OR. (dft_control%nspins > 2)) THEN
    1562            0 :          CPABORT("nspins should be 1 or 2 for the time being ...")
    1563              :       END IF
    1564              : 
    1565         7444 :       IF ((MODULO(nelectron, 2) /= 0) .AND. (dft_control%nspins == 1)) THEN
    1566           12 :          IF (.NOT. dft_control%qs_control%ofgpw .AND. .NOT. dft_control%smear) THEN
    1567            0 :             CPABORT("Use the LSD option for an odd number of electrons")
    1568              :          END IF
    1569              :       END IF
    1570              : 
    1571              :       ! The transition potential method to calculate XAS needs LSD
    1572         7444 :       IF (dft_control%do_xas_calculation) THEN
    1573           42 :          IF (dft_control%nspins == 1) THEN
    1574            0 :             CPABORT("Use the LSD option for XAS with transition potential")
    1575              :          END IF
    1576              :       END IF
    1577              : 
    1578              :       !   assigning the number of states per spin initial version, not yet very
    1579              :       !   general. Should work for an even number of electrons and a single
    1580              :       !   additional electron this set of options that requires full matrices,
    1581              :       !   however, makes things a bit ugly right now.... we try to make a
    1582              :       !   distinction between the number of electrons per spin and the number of
    1583              :       !   MOs per spin this should allow the use of fractional occupations later
    1584              :       !   on
    1585         7444 :       IF (dft_control%qs_control%ofgpw) THEN
    1586              : 
    1587            0 :          IF (dft_control%nspins == 1) THEN
    1588            0 :             maxocc = nelectron
    1589            0 :             nelectron_spin(1) = nelectron
    1590            0 :             nelectron_spin(2) = 0
    1591            0 :             n_mo(1) = 1
    1592            0 :             n_mo(2) = 0
    1593              :          ELSE
    1594            0 :             IF (MODULO(nelectron + multiplicity - 1, 2) /= 0) THEN
    1595            0 :                CPABORT("LSD: try to use a different multiplicity")
    1596              :             END IF
    1597            0 :             nelectron_spin(1) = (nelectron + multiplicity - 1)/2
    1598            0 :             nelectron_spin(2) = (nelectron - multiplicity + 1)/2
    1599            0 :             IF (nelectron_spin(1) < 0) THEN
    1600            0 :                CPABORT("LSD: too few electrons for this multiplicity")
    1601              :             END IF
    1602            0 :             maxocc = MAXVAL(nelectron_spin)
    1603            0 :             n_mo(1) = MIN(nelectron_spin(1), 1)
    1604            0 :             n_mo(2) = MIN(nelectron_spin(2), 1)
    1605              :          END IF
    1606              : 
    1607              :       ELSE
    1608              : 
    1609         7444 :          IF (dft_control%nspins == 1) THEN
    1610         5815 :             maxocc = 2.0_dp
    1611         5815 :             nelectron_spin(1) = nelectron
    1612         5815 :             nelectron_spin(2) = 0
    1613         5815 :             IF (MODULO(nelectron, 2) == 0) THEN
    1614         5803 :                n_mo(1) = nelectron/2
    1615              :             ELSE
    1616           12 :                n_mo(1) = INT(nelectron/2._dp) + 1
    1617              :             END IF
    1618         5815 :             n_mo(2) = 0
    1619              :          ELSE
    1620         1629 :             maxocc = 1.0_dp
    1621              : 
    1622              :             ! The simplist spin distribution is written here. Special cases will
    1623              :             ! need additional user input
    1624         1629 :             IF (MODULO(nelectron + multiplicity - 1, 2) /= 0) THEN
    1625            0 :                CPABORT("LSD: try to use a different multiplicity")
    1626              :             END IF
    1627              : 
    1628         1629 :             nelectron_spin(1) = (nelectron + multiplicity - 1)/2
    1629         1629 :             nelectron_spin(2) = (nelectron - multiplicity + 1)/2
    1630              : 
    1631         1629 :             IF (nelectron_spin(2) < 0) THEN
    1632            0 :                CPABORT("LSD: too few electrons for this multiplicity")
    1633              :             END IF
    1634              : 
    1635         1629 :             n_mo(1) = nelectron_spin(1)
    1636         1629 :             n_mo(2) = nelectron_spin(2)
    1637              : 
    1638              :          END IF
    1639              : 
    1640              :       END IF
    1641              : 
    1642              :       ! Read the total_zeff_corr here [SGh]
    1643         7444 :       CALL get_qs_kind_set(qs_kind_set, total_zeff_corr=total_zeff_corr)
    1644              :       ! store it in qs_env
    1645         7444 :       qs_env%total_zeff_corr = total_zeff_corr
    1646              : 
    1647              :       ! store the number of electrons once an for all
    1648              :       CALL qs_subsys_set(subsys, &
    1649              :                          nelectron_total=nelectron, &
    1650         7444 :                          nelectron_spin=nelectron_spin)
    1651              : 
    1652              :       ! Check and set number of added (unoccupied) MOs
    1653         7444 :       IF (dft_control%nspins == 2) THEN
    1654         1629 :          IF (scf_control%added_mos(2) < 0) THEN
    1655          128 :             n_mo_add = n_ao - n_mo(2)  ! use all available MOs
    1656         1501 :          ELSEIF (scf_control%added_mos(2) > 0) THEN
    1657              :             n_mo_add = scf_control%added_mos(2)
    1658              :          ELSE
    1659         1355 :             n_mo_add = scf_control%added_mos(1)
    1660              :          END IF
    1661         1629 :          IF (n_mo_add > n_ao - n_mo(2)) THEN
    1662           18 :             CPWARN("More ADDED_MOs requested for beta spin than available.")
    1663              :          END IF
    1664         1629 :          scf_control%added_mos(2) = MIN(n_mo_add, n_ao - n_mo(2))
    1665         1629 :          n_mo(2) = n_mo(2) + scf_control%added_mos(2)
    1666              :       END IF
    1667              : 
    1668              :       ! proceed alpha orbitals after the beta orbitals; this is essential to avoid
    1669              :       ! reduction in the number of available unoccupied molecular orbitals.
    1670              :       ! E.g. n_ao = 10, nelectrons = 10, multiplicity = 3 implies n_mo(1) = 6, n_mo(2) = 4;
    1671              :       ! added_mos(1:2) = (6,undef) should increase the number of molecular orbitals as
    1672              :       ! n_mo(1) = min(n_ao, n_mo(1) + added_mos(1)) = 10, n_mo(2) = 10.
    1673              :       ! However, if we try to proceed alpha orbitals first, this leads us n_mo(1:2) = (10,8)
    1674              :       ! due to the following assignment instruction above:
    1675              :       !   IF (scf_control%added_mos(2) > 0) THEN ... ELSE; n_mo_add = scf_control%added_mos(1); END IF
    1676         7444 :       IF (scf_control%added_mos(1) < 0) THEN
    1677          678 :          scf_control%added_mos(1) = n_ao - n_mo(1)  ! use all available MOs
    1678         6766 :       ELSEIF (scf_control%added_mos(1) > n_ao - n_mo(1)) THEN
    1679              :          CALL cp_warn(__LOCATION__, &
    1680              :                       "More added MOs requested than available. "// &
    1681              :                       "The full set of unoccupied MOs will be used. "// &
    1682              :                       "Use 'ADDED_MOS -1' to always use all available MOs "// &
    1683           90 :                       "and to get rid of this warning.")
    1684              :       END IF
    1685         7444 :       scf_control%added_mos(1) = MIN(scf_control%added_mos(1), n_ao - n_mo(1))
    1686         7444 :       n_mo(1) = n_mo(1) + scf_control%added_mos(1)
    1687              : 
    1688         7444 :       IF (dft_control%nspins == 2) THEN
    1689         1629 :          IF (n_mo(2) > n_mo(1)) &
    1690              :             CALL cp_warn(__LOCATION__, &
    1691              :                          "More beta than alpha MOs requested. "// &
    1692            0 :                          "The number of beta MOs will be reduced to the number alpha MOs.")
    1693         1629 :          n_mo(2) = MIN(n_mo(1), n_mo(2))
    1694         1629 :          CPASSERT(n_mo(1) >= nelectron_spin(1))
    1695         1629 :          CPASSERT(n_mo(2) >= nelectron_spin(2))
    1696              :       END IF
    1697              : 
    1698              :       ! kpoints
    1699         7444 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
    1700         7444 :       IF (do_kpoints .AND. dft_control%nspins == 2) THEN
    1701              :          ! we need equal number of calculated states
    1702           22 :          IF (n_mo(2) /= n_mo(1)) &
    1703              :             CALL cp_warn(__LOCATION__, &
    1704              :                          "Kpoints: Different number of MOs requested. "// &
    1705            2 :                          "The number of beta MOs will be set to the number alpha MOs.")
    1706           22 :          n_mo(2) = n_mo(1)
    1707           22 :          CPASSERT(n_mo(1) >= nelectron_spin(1))
    1708           22 :          CPASSERT(n_mo(2) >= nelectron_spin(2))
    1709              :       END IF
    1710              : 
    1711              :       ! Compatibility checks for smearing
    1712         7444 :       IF (scf_control%smear%do_smear) THEN
    1713          902 :          IF (scf_control%added_mos(1) == 0) THEN
    1714            0 :             CPABORT("Extra MOs (ADDED_MOS) are required for smearing")
    1715              :          END IF
    1716              :       END IF
    1717              : 
    1718              :       !   *** Some options require that all MOs are computed ... ***
    1719              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
    1720              :                                            "PRINT%MO/CARTESIAN"), &
    1721              :                 cp_p_file) .OR. &
    1722              :           (scf_control%level_shift /= 0.0_dp) .OR. &
    1723         7444 :           (scf_control%diagonalization%eps_jacobi /= 0.0_dp) .OR. &
    1724              :           (dft_control%roks .AND. (.NOT. scf_control%use_ot))) THEN
    1725         7596 :          n_mo(:) = n_ao
    1726              :       END IF
    1727              : 
    1728              :       ! Compatibility checks for ROKS
    1729         7444 :       IF (dft_control%roks .AND. (.NOT. scf_control%use_ot)) THEN
    1730           40 :          IF (scf_control%roks_scheme == general_roks) THEN
    1731            0 :             CPWARN("General ROKS scheme is not yet tested!")
    1732              :          END IF
    1733           40 :          IF (scf_control%smear%do_smear) THEN
    1734              :             CALL cp_abort(__LOCATION__, &
    1735              :                           "The options ROKS and SMEAR are not compatible. "// &
    1736            0 :                           "Try UKS instead of ROKS")
    1737              :          END IF
    1738              :       END IF
    1739         7444 :       IF (dft_control%low_spin_roks) THEN
    1740            8 :          SELECT CASE (dft_control%qs_control%method_id)
    1741              :          CASE DEFAULT
    1742              :          CASE (do_method_xtb, do_method_dftb)
    1743              :             CALL cp_abort(__LOCATION__, &
    1744            0 :                           "xTB/DFTB methods are not compatible with low spin ROKS.")
    1745              :          CASE (do_method_rm1, do_method_am1, do_method_mndo, do_method_pm3, &
    1746              :                do_method_pm6, do_method_pm6fm, do_method_mndod, do_method_pnnl)
    1747              :             CALL cp_abort(__LOCATION__, &
    1748            8 :                           "SE methods are not compatible with low spin ROKS.")
    1749              :          END SELECT
    1750              :       END IF
    1751              : 
    1752              :       ! in principle the restricted calculation could be performed
    1753              :       ! using just one set of MOs and special casing most of the code
    1754              :       ! right now we'll just take care of what is effectively an additional constraint
    1755              :       ! at as few places as possible, just duplicating the beta orbitals
    1756         7444 :       IF (dft_control%restricted .AND. (output_unit > 0)) THEN
    1757              :          ! it is really not yet tested till the end ! Joost
    1758           23 :          WRITE (output_unit, *) ""
    1759           23 :          WRITE (output_unit, *) " **************************************"
    1760           23 :          WRITE (output_unit, *) " restricted calculation cutting corners"
    1761           23 :          WRITE (output_unit, *) " experimental feature, check code      "
    1762           23 :          WRITE (output_unit, *) " **************************************"
    1763              :       END IF
    1764              : 
    1765              :       ! no point in allocating these things here ?
    1766         7444 :       IF (dft_control%qs_control%do_ls_scf) THEN
    1767          338 :          NULLIFY (mos)
    1768              :       ELSE
    1769        30039 :          ALLOCATE (mos(dft_control%nspins))
    1770        15827 :          DO ispin = 1, dft_control%nspins
    1771              :             CALL allocate_mo_set(mo_set=mos(ispin), &
    1772              :                                  nao=n_ao, &
    1773              :                                  nmo=n_mo(ispin), &
    1774              :                                  nelectron=nelectron_spin(ispin), &
    1775              :                                  n_el_f=REAL(nelectron_spin(ispin), dp), &
    1776              :                                  maxocc=maxocc, &
    1777        15827 :                                  flexible_electron_count=dft_control%relax_multiplicity)
    1778              :          END DO
    1779              :       END IF
    1780              : 
    1781         7444 :       CALL set_qs_env(qs_env, mos=mos)
    1782              : 
    1783              :       ! allocate mos when switch_surf_dip is triggered [SGh]
    1784         7444 :       IF (dft_control%switch_surf_dip) THEN
    1785            8 :          ALLOCATE (mos_last_converged(dft_control%nspins))
    1786            4 :          DO ispin = 1, dft_control%nspins
    1787              :             CALL allocate_mo_set(mo_set=mos_last_converged(ispin), &
    1788              :                                  nao=n_ao, &
    1789              :                                  nmo=n_mo(ispin), &
    1790              :                                  nelectron=nelectron_spin(ispin), &
    1791              :                                  n_el_f=REAL(nelectron_spin(ispin), dp), &
    1792              :                                  maxocc=maxocc, &
    1793            4 :                                  flexible_electron_count=dft_control%relax_multiplicity)
    1794              :          END DO
    1795            2 :          CALL set_qs_env(qs_env, mos_last_converged=mos_last_converged)
    1796              :       END IF
    1797              : 
    1798         7444 :       IF (.NOT. be_silent) THEN
    1799              :          ! Print the DFT control parameters
    1800         7438 :          CALL write_dft_control(dft_control, dft_section)
    1801              : 
    1802              :          ! Print the vdW control parameters
    1803              :          IF (dft_control%qs_control%method_id == do_method_gpw .OR. &
    1804              :              dft_control%qs_control%method_id == do_method_gapw .OR. &
    1805              :              dft_control%qs_control%method_id == do_method_gapw_xc .OR. &
    1806              :              dft_control%qs_control%method_id == do_method_lrigpw .OR. &
    1807              :              dft_control%qs_control%method_id == do_method_rigpw .OR. &
    1808              :              dft_control%qs_control%method_id == do_method_dftb .OR. &
    1809              :              (dft_control%qs_control%method_id == do_method_xtb .AND. &
    1810         7438 :               (.NOT. dft_control%qs_control%xtb_control%do_tblite)) .OR. &
    1811              :              dft_control%qs_control%method_id == do_method_ofgpw) THEN
    1812         6440 :             CALL get_qs_env(qs_env, dispersion_env=dispersion_env)
    1813         6440 :             CALL qs_write_dispersion(qs_env, dispersion_env)
    1814              :          END IF
    1815              : 
    1816              :          ! Print the Quickstep control parameters
    1817         7438 :          CALL write_qs_control(dft_control%qs_control, dft_section)
    1818              : 
    1819              :          ! Print the ADMM control parameters
    1820         7438 :          IF (dft_control%do_admm) THEN
    1821          464 :             CALL write_admm_control(dft_control%admm_control, dft_section)
    1822              :          END IF
    1823              : 
    1824              :          ! Print XES/XAS control parameters
    1825         7438 :          IF (dft_control%do_xas_calculation) THEN
    1826           42 :             CALL cite_reference(Iannuzzi2007)
    1827              :             !CALL write_xas_control(dft_control%xas_control,dft_section)
    1828              :          END IF
    1829              : 
    1830              :          ! Print the unnormalized basis set information (input data)
    1831         7438 :          CALL write_gto_basis_sets(qs_kind_set, subsys_section)
    1832              : 
    1833              :          ! Print the atomic kind set
    1834         7438 :          CALL write_qs_kind_set(qs_kind_set, subsys_section)
    1835              : 
    1836              :          ! Print the molecule kind set
    1837         7438 :          CALL write_molecule_kind_set(molecule_kind_set, subsys_section)
    1838              : 
    1839              :          ! Print the total number of kinds, atoms, basis functions etc.
    1840         7438 :          CALL write_total_numbers(qs_kind_set, particle_set, qs_env%input)
    1841              : 
    1842              :          ! Print the atomic coordinates
    1843         7438 :          CALL write_qs_particle_coordinates(particle_set, qs_kind_set, subsys_section, label="QUICKSTEP")
    1844              : 
    1845              :          ! Print the interatomic distances
    1846         7438 :          CALL write_particle_distances(particle_set, cell, subsys_section)
    1847              : 
    1848              :          ! Print the requested structure data
    1849         7438 :          CALL write_structure_data(particle_set, cell, subsys_section)
    1850              : 
    1851              :          ! Print symmetry information
    1852         7438 :          CALL write_symmetry(particle_set, cell, subsys_section)
    1853              : 
    1854              :          ! Print the SCF parameters
    1855         7438 :          IF ((.NOT. dft_control%qs_control%do_ls_scf) .AND. &
    1856              :              (.NOT. dft_control%qs_control%do_almo_scf)) THEN
    1857         7034 :             CALL scf_c_write_parameters(scf_control, dft_section)
    1858              :          END IF
    1859              :       END IF
    1860              : 
    1861              :       ! Sets up pw_env, qs_charges, mpools ...
    1862         7444 :       CALL qs_env_setup(qs_env)
    1863              : 
    1864              :       ! Allocate and initialise rho0 soft on the global grid
    1865         7444 :       IF (dft_control%qs_control%method_id == do_method_gapw) THEN
    1866          872 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, rho0_mpole=rho0_mpole)
    1867          872 :          CALL rho0_s_grid_create(pw_env, rho0_mpole)
    1868              :       END IF
    1869              : 
    1870         7444 :       IF (output_unit > 0) CALL m_flush(output_unit)
    1871         7444 :       CALL timestop(handle)
    1872              : 
    1873        81884 :    END SUBROUTINE qs_init_subsys
    1874              : 
    1875              : ! **************************************************************************************************
    1876              : !> \brief Write the total number of kinds, atoms, etc. to the logical unit
    1877              : !>      number lunit.
    1878              : !> \param qs_kind_set ...
    1879              : !> \param particle_set ...
    1880              : !> \param force_env_section ...
    1881              : !> \author Creation (06.10.2000)
    1882              : ! **************************************************************************************************
    1883         7438 :    SUBROUTINE write_total_numbers(qs_kind_set, particle_set, force_env_section)
    1884              : 
    1885              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1886              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1887              :       TYPE(section_vals_type), POINTER                   :: force_env_section
    1888              : 
    1889              :       INTEGER                                            :: maxlgto, maxlppl, maxlppnl, natom, &
    1890              :                                                             natom_q, ncgf, nkind, nkind_q, npgf, &
    1891              :                                                             nset, nsgf, nshell, output_unit
    1892              :       TYPE(cp_logger_type), POINTER                      :: logger
    1893              : 
    1894         7438 :       NULLIFY (logger)
    1895         7438 :       logger => cp_get_default_logger()
    1896              :       output_unit = cp_print_key_unit_nr(logger, force_env_section, "PRINT%TOTAL_NUMBERS", &
    1897         7438 :                                          extension=".Log")
    1898              : 
    1899         7438 :       IF (output_unit > 0) THEN
    1900         3743 :          natom = SIZE(particle_set)
    1901         3743 :          nkind = SIZE(qs_kind_set)
    1902              : 
    1903              :          CALL get_qs_kind_set(qs_kind_set, &
    1904              :                               maxlgto=maxlgto, &
    1905              :                               ncgf=ncgf, &
    1906              :                               npgf=npgf, &
    1907              :                               nset=nset, &
    1908              :                               nsgf=nsgf, &
    1909              :                               nshell=nshell, &
    1910              :                               maxlppl=maxlppl, &
    1911         3743 :                               maxlppnl=maxlppnl)
    1912              : 
    1913              :          WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
    1914         3743 :             "TOTAL NUMBERS AND MAXIMUM NUMBERS"
    1915              : 
    1916         3743 :          IF (nset + npgf + ncgf > 0) THEN
    1917              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T71,I10))") &
    1918         3743 :                "Total number of", &
    1919         3743 :                "- Atomic kinds:                  ", nkind, &
    1920         3743 :                "- Atoms:                         ", natom, &
    1921         3743 :                "- Shell sets:                    ", nset, &
    1922         3743 :                "- Shells:                        ", nshell, &
    1923         3743 :                "- Primitive Cartesian functions: ", npgf, &
    1924         3743 :                "- Cartesian basis functions:     ", ncgf, &
    1925         7486 :                "- Spherical basis functions:     ", nsgf
    1926            0 :          ELSE IF (nshell + nsgf > 0) THEN
    1927              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T71,I10))") &
    1928            0 :                "Total number of", &
    1929            0 :                "- Atomic kinds:                  ", nkind, &
    1930            0 :                "- Atoms:                         ", natom, &
    1931            0 :                "- Shells:                        ", nshell, &
    1932            0 :                "- Spherical basis functions:     ", nsgf
    1933              :          ELSE
    1934              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T71,I10))") &
    1935            0 :                "Total number of", &
    1936            0 :                "- Atomic kinds:                  ", nkind, &
    1937            0 :                "- Atoms:                         ", natom
    1938              :          END IF
    1939              : 
    1940         3743 :          IF ((maxlppl > -1) .AND. (maxlppnl > -1)) THEN
    1941              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T75,I6))") &
    1942         1915 :                "Maximum angular momentum of the", &
    1943         1915 :                "- Orbital basis functions:                   ", maxlgto, &
    1944         1915 :                "- Local part of the GTH pseudopotential:     ", maxlppl, &
    1945         3830 :                "- Non-local part of the GTH pseudopotential: ", maxlppnl
    1946         1828 :          ELSEIF (maxlppl > -1) THEN
    1947              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,(T30,A,T75,I6))") &
    1948          454 :                "Maximum angular momentum of the", &
    1949          454 :                "- Orbital basis functions:                   ", maxlgto, &
    1950          908 :                "- Local part of the GTH pseudopotential:     ", maxlppl
    1951              :          ELSE
    1952              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,T75,I6)") &
    1953         1374 :                "Maximum angular momentum of the orbital basis functions: ", maxlgto
    1954              :          END IF
    1955              : 
    1956              :          ! LRI_AUX BASIS
    1957              :          CALL get_qs_kind_set(qs_kind_set, &
    1958              :                               maxlgto=maxlgto, &
    1959              :                               ncgf=ncgf, &
    1960              :                               npgf=npgf, &
    1961              :                               nset=nset, &
    1962              :                               nsgf=nsgf, &
    1963              :                               nshell=nshell, &
    1964         3743 :                               basis_type="LRI_AUX")
    1965         3743 :          IF (nset + npgf + ncgf > 0) THEN
    1966              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    1967          135 :                "LRI_AUX Basis: ", &
    1968          135 :                "Total number of", &
    1969          135 :                "- Shell sets:                    ", nset, &
    1970          135 :                "- Shells:                        ", nshell, &
    1971          135 :                "- Primitive Cartesian functions: ", npgf, &
    1972          135 :                "- Cartesian basis functions:     ", ncgf, &
    1973          270 :                "- Spherical basis functions:     ", nsgf
    1974              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    1975          135 :                "  Maximum angular momentum ", maxlgto
    1976              :          END IF
    1977              : 
    1978              :          ! RI_HXC BASIS
    1979              :          CALL get_qs_kind_set(qs_kind_set, &
    1980              :                               maxlgto=maxlgto, &
    1981              :                               ncgf=ncgf, &
    1982              :                               npgf=npgf, &
    1983              :                               nset=nset, &
    1984              :                               nsgf=nsgf, &
    1985              :                               nshell=nshell, &
    1986         3743 :                               basis_type="RI_HXC")
    1987         3743 :          IF (nset + npgf + ncgf > 0) THEN
    1988              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    1989          111 :                "RI_HXC Basis: ", &
    1990          111 :                "Total number of", &
    1991          111 :                "- Shell sets:                    ", nset, &
    1992          111 :                "- Shells:                        ", nshell, &
    1993          111 :                "- Primitive Cartesian functions: ", npgf, &
    1994          111 :                "- Cartesian basis functions:     ", ncgf, &
    1995          222 :                "- Spherical basis functions:     ", nsgf
    1996              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    1997          111 :                "  Maximum angular momentum ", maxlgto
    1998              :          END IF
    1999              : 
    2000              :          ! AUX_FIT BASIS
    2001              :          CALL get_qs_kind_set(qs_kind_set, &
    2002              :                               maxlgto=maxlgto, &
    2003              :                               ncgf=ncgf, &
    2004              :                               npgf=npgf, &
    2005              :                               nset=nset, &
    2006              :                               nsgf=nsgf, &
    2007              :                               nshell=nshell, &
    2008         3743 :                               basis_type="AUX_FIT")
    2009         3743 :          IF (nset + npgf + ncgf > 0) THEN
    2010              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    2011          343 :                "AUX_FIT ADMM-Basis: ", &
    2012          343 :                "Total number of", &
    2013          343 :                "- Shell sets:                    ", nset, &
    2014          343 :                "- Shells:                        ", nshell, &
    2015          343 :                "- Primitive Cartesian functions: ", npgf, &
    2016          343 :                "- Cartesian basis functions:     ", ncgf, &
    2017          686 :                "- Spherical basis functions:     ", nsgf
    2018              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    2019          343 :                "  Maximum angular momentum ", maxlgto
    2020              :          END IF
    2021              : 
    2022              :          ! NUCLEAR BASIS
    2023              :          CALL get_qs_kind_set(qs_kind_set, &
    2024              :                               nkind_q=nkind_q, &
    2025              :                               natom_q=natom_q, &
    2026              :                               maxlgto=maxlgto, &
    2027              :                               ncgf=ncgf, &
    2028              :                               npgf=npgf, &
    2029              :                               nset=nset, &
    2030              :                               nsgf=nsgf, &
    2031              :                               nshell=nshell, &
    2032         3743 :                               basis_type="NUC")
    2033         3743 :          IF (nset + npgf + ncgf > 0) THEN
    2034              :             WRITE (UNIT=output_unit, FMT="(/,T3,A,/,T3,A,(T30,A,T71,I10))") &
    2035          115 :                "Nuclear Basis: ", &
    2036          115 :                "Total number of", &
    2037          115 :                "- Quantum atomic kinds:          ", nkind_q, &
    2038          115 :                "- Quantum atoms:                 ", natom_q, &
    2039          115 :                "- Shell sets:                    ", nset, &
    2040          115 :                "- Shells:                        ", nshell, &
    2041          115 :                "- Primitive Cartesian functions: ", npgf, &
    2042          115 :                "- Cartesian basis functions:     ", ncgf, &
    2043          230 :                "- Spherical basis functions:     ", nsgf
    2044              :             WRITE (UNIT=output_unit, FMT="(T30,A,T75,I6)") &
    2045          115 :                "  Maximum angular momentum ", maxlgto
    2046              :          END IF
    2047              : 
    2048              :       END IF
    2049              :       CALL cp_print_key_finished_output(output_unit, logger, force_env_section, &
    2050         7438 :                                         "PRINT%TOTAL_NUMBERS")
    2051              : 
    2052         7438 :    END SUBROUTINE write_total_numbers
    2053              : 
    2054              : END MODULE qs_environment
        

Generated by: LCOV version 2.0-1