LCOV - code coverage report
Current view: top level - src - qs_environment_types.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:2fce0f8) Lines: 509 628 81.1 %
Date: 2024-12-21 06:28:57 Functions: 6 7 85.7 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \par History
      10             : !>      - mo_set_p_type added to qs_env (23.04.02,MK)
      11             : !>      - qs_force_type added to qs_env (05.06.02,MK)
      12             : !> \author MK (23.01.2002)
      13             : ! **************************************************************************************************
      14             : MODULE qs_environment_types
      15             :    USE admm_types,                      ONLY: admm_env_release,&
      16             :                                               admm_type
      17             :    USE almo_scf_types,                  ONLY: almo_scf_env_release,&
      18             :                                               almo_scf_env_type
      19             :    USE atomic_kind_types,               ONLY: atomic_kind_type
      20             :    USE atprop_types,                    ONLY: atprop_type
      21             :    USE cell_types,                      ONLY: cell_release,&
      22             :                                               cell_retain,&
      23             :                                               cell_type
      24             :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      25             :    USE cp_control_types,                ONLY: dft_control_type
      26             :    USE cp_dbcsr_api,                    ONLY: dbcsr_distribution_type,&
      27             :                                               dbcsr_p_type,&
      28             :                                               dbcsr_release_p
      29             :    USE cp_ddapc_types,                  ONLY: cp_ddapc_ewald_release,&
      30             :                                               cp_ddapc_ewald_type,&
      31             :                                               cp_ddapc_release,&
      32             :                                               cp_ddapc_type
      33             :    USE cp_fm_types,                     ONLY: cp_fm_release,&
      34             :                                               cp_fm_type
      35             :    USE cp_result_types,                 ONLY: cp_result_type
      36             :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      37             :    USE distribution_1d_types,           ONLY: distribution_1d_type
      38             :    USE distribution_2d_types,           ONLY: distribution_2d_type
      39             :    USE dm_ls_scf_types,                 ONLY: ls_scf_env_type,&
      40             :                                               ls_scf_release
      41             :    USE ec_env_types,                    ONLY: ec_env_release,&
      42             :                                               energy_correction_type
      43             :    USE et_coupling_types,               ONLY: et_coupling_release,&
      44             :                                               et_coupling_type
      45             :    USE ewald_environment_types,         ONLY: ewald_env_release,&
      46             :                                               ewald_environment_type
      47             :    USE ewald_pw_types,                  ONLY: ewald_pw_release,&
      48             :                                               ewald_pw_type
      49             :    USE exstates_types,                  ONLY: excited_energy_type,&
      50             :                                               exstate_release
      51             :    USE fist_nonbond_env_types,          ONLY: fist_nonbond_env_release,&
      52             :                                               fist_nonbond_env_type
      53             :    USE global_types,                    ONLY: global_environment_type
      54             :    USE hartree_local_types,             ONLY: ecoul_1center_type,&
      55             :                                               get_hartree_local,&
      56             :                                               hartree_local_create,&
      57             :                                               hartree_local_release,&
      58             :                                               hartree_local_type,&
      59             :                                               set_hartree_local
      60             :    USE hfx_types,                       ONLY: hfx_release,&
      61             :                                               hfx_type
      62             :    USE input_constants,                 ONLY: energy_force_run,&
      63             :                                               energy_run
      64             :    USE input_section_types,             ONLY: section_vals_release,&
      65             :                                               section_vals_retain,&
      66             :                                               section_vals_type
      67             :    USE kg_environment_types,            ONLY: kg_env_release,&
      68             :                                               kg_environment_type
      69             :    USE kinds,                           ONLY: dp
      70             :    USE kpoint_types,                    ONLY: kpoint_type
      71             :    USE lri_environment_types,           ONLY: lri_density_release,&
      72             :                                               lri_density_type,&
      73             :                                               lri_env_release,&
      74             :                                               lri_environment_type
      75             :    USE message_passing,                 ONLY: mp_para_env_type
      76             :    USE molecule_kind_types,             ONLY: molecule_kind_type
      77             :    USE molecule_types,                  ONLY: molecule_type
      78             :    USE mp2_types,                       ONLY: mp2_env_release,&
      79             :                                               mp2_type
      80             :    USE mscfg_types,                     ONLY: molecular_scf_guess_env_destroy,&
      81             :                                               molecular_scf_guess_env_type
      82             :    USE particle_types,                  ONLY: particle_type
      83             :    USE post_scf_bandstructure_types,    ONLY: bs_env_release,&
      84             :                                               post_scf_bandstructure_type
      85             :    USE pw_env_types,                    ONLY: pw_env_type
      86             :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      87             :                                               pw_r3d_rs_type
      88             :    USE qmmm_types_low,                  ONLY: qmmm_env_qm_type
      89             :    USE qs_active_space_types,           ONLY: active_space_type,&
      90             :                                               release_active_space_type
      91             :    USE qs_charges_types,                ONLY: qs_charges_release,&
      92             :                                               qs_charges_type
      93             :    USE qs_dftb_types,                   ONLY: qs_dftb_pairpot_release,&
      94             :                                               qs_dftb_pairpot_type
      95             :    USE qs_dispersion_types,             ONLY: qs_dispersion_release,&
      96             :                                               qs_dispersion_type
      97             :    USE qs_energy_types,                 ONLY: qs_energy_type
      98             :    USE qs_force_types,                  ONLY: qs_force_type
      99             :    USE qs_gcp_types,                    ONLY: qs_gcp_release,&
     100             :                                               qs_gcp_type
     101             :    USE qs_harris_types,                 ONLY: harris_env_release,&
     102             :                                               harris_type
     103             :    USE qs_kind_types,                   ONLY: qs_kind_type
     104             :    USE qs_ks_qmmm_types,                ONLY: qs_ks_qmmm_env_type,&
     105             :                                               qs_ks_qmmm_release
     106             :    USE qs_ks_types,                     ONLY: get_ks_env,&
     107             :                                               qs_ks_env_type,&
     108             :                                               qs_ks_part_release,&
     109             :                                               qs_ks_release,&
     110             :                                               set_ks_env
     111             :    USE qs_linres_types,                 ONLY: linres_control_release,&
     112             :                                               linres_control_type,&
     113             :                                               polar_env_release,&
     114             :                                               polar_env_type
     115             :    USE qs_local_rho_types,              ONLY: get_local_rho,&
     116             :                                               local_rho_set_create,&
     117             :                                               local_rho_set_release,&
     118             :                                               local_rho_type,&
     119             :                                               rhoz_type,&
     120             :                                               set_local_rho
     121             :    USE qs_matrix_pools,                 ONLY: mpools_release,&
     122             :                                               mpools_retain,&
     123             :                                               qs_matrix_pools_type
     124             :    USE qs_mo_types,                     ONLY: deallocate_mo_set,&
     125             :                                               mo_set_type
     126             :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
     127             :    USE qs_oce_types,                    ONLY: deallocate_oce_set,&
     128             :                                               oce_matrix_type
     129             :    USE qs_period_efield_types,          ONLY: efield_berry_release,&
     130             :                                               efield_berry_type
     131             :    USE qs_rho0_types,                   ONLY: rho0_atom_type,&
     132             :                                               rho0_mpole_type
     133             :    USE qs_rho_atom_types,               ONLY: rho_atom_type
     134             :    USE qs_rho_types,                    ONLY: qs_rho_p_type,&
     135             :                                               qs_rho_release,&
     136             :                                               qs_rho_type
     137             :    USE qs_scf_types,                    ONLY: qs_scf_env_type,&
     138             :                                               scf_env_release
     139             :    USE qs_subsys_types,                 ONLY: qs_subsys_set,&
     140             :                                               qs_subsys_type
     141             :    USE qs_wf_history_types,             ONLY: qs_wf_history_type,&
     142             :                                               wfi_release,&
     143             :                                               wfi_retain
     144             :    USE rel_control_types,               ONLY: rel_c_release,&
     145             :                                               rel_control_type
     146             :    USE rt_propagation_types,            ONLY: rt_prop_release,&
     147             :                                               rt_prop_type
     148             :    USE scf_control_types,               ONLY: scf_c_release,&
     149             :                                               scf_control_type
     150             :    USE semi_empirical_mpole_types,      ONLY: nddo_mpole_release,&
     151             :                                               nddo_mpole_type
     152             :    USE semi_empirical_store_int_types,  ONLY: semi_empirical_si_release,&
     153             :                                               semi_empirical_si_type
     154             :    USE semi_empirical_types,            ONLY: se_taper_release,&
     155             :                                               se_taper_type
     156             :    USE task_list_types,                 ONLY: task_list_type
     157             :    USE transport_env_types,             ONLY: transport_env_release,&
     158             :                                               transport_env_type
     159             :    USE virial_types,                    ONLY: virial_type
     160             :    USE wannier_states_types,            ONLY: wannier_centres_type
     161             :    USE xas_env_types,                   ONLY: xas_env_release,&
     162             :                                               xas_environment_type
     163             : #include "./base/base_uses.f90"
     164             : 
     165             :    IMPLICIT NONE
     166             : 
     167             :    PRIVATE
     168             : 
     169             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_environment_types'
     170             : 
     171             : ! *** Public data types ***
     172             : 
     173             :    PUBLIC :: qs_environment_type
     174             : 
     175             : ! *** Public subroutines ***
     176             : 
     177             :    PUBLIC :: get_qs_env, &
     178             :              qs_env_create, &
     179             :              qs_env_release, &
     180             :              qs_env_part_release, &
     181             :              set_qs_env
     182             : 
     183             : ! **************************************************************************************************
     184             : !> \param local_rho_set contains the atomic, compensations and core densities
     185             : !>                       and the local parts of the xc terms
     186             : !> \param hartree_local contains the 1, 2 and 3 centers coulomb terms
     187             : !> \param requires_mo_derivs logical, true if dE/dC is required (e.g. OT)
     188             : !> \param has_unit_metric logical, true if the S matrix is considered unity for the SCF
     189             : !> \param mo_derivs the actual derivatives of the total energy wrt to MO coeffs (divided by 2*f_i)
     190             : !> \param xas_env temporary information for xas calculation
     191             : !> \param dftb_potential pair potentials for use with DFTB
     192             : !> \param dispersion_env environment for use with QS dispersion
     193             : !>
     194             : !>      compatibility get (things that you should get from the subsys):
     195             : !> \param atomic_kind_set array with infos about the species (atomic_kinds)
     196             : !>        present in the system
     197             : !> \param particle_set info on the atoms you simulate, pos,...
     198             : !> \param local_particles which particles ar local to this processor
     199             : !>      new:
     200             : !> \param local_molecules which molecules are local to this processor
     201             : !> \param molecule_kind_set description of the molecule kinds
     202             : !> \param molecule_set all the molecule description
     203             : !> \param rtp all data needed for real time propagation
     204             : !> \param x contains data used in Hartree-Fock-Exchange calculations
     205             : !> \param task_list the list of tasks used in collocate and integrate
     206             : !> \param task_list_soft the list of tasks used in collocate and integrate in case of soft basis functions
     207             : !> \param mo_loc_history if a history of localized wfn is kept, they are stored here.
     208             : !> \param molecular_scf_guess_env contains inforamation about and results of claculations
     209             : !>          on separate molecules
     210             : !> \par History
     211             : !>      11.2002 added doc and attribute description [fawzi]
     212             : !>      08.2004 renamed some of the very short names (s,c,k,h) for easier grepping
     213             : !>      06.2018 polar_env added (MK)
     214             : !> \author Matthias Krack & fawzi
     215             : ! **************************************************************************************************
     216             : 
     217             :    TYPE qs_environment_type
     218             :       LOGICAL :: qmmm = .FALSE., qmmm_periodic = .FALSE.
     219             :       LOGICAL :: requires_mo_derivs = .FALSE.
     220             :       LOGICAL :: requires_matrix_vxc = .FALSE.
     221             :       LOGICAL :: has_unit_metric = .FALSE.
     222             :       LOGICAL :: run_rtp = .FALSE.
     223             :       LOGICAL :: linres_run = .FALSE.
     224             :       LOGICAL :: calc_image_preconditioner = .FALSE.
     225             :       LOGICAL :: do_transport = .FALSE.
     226             :       LOGICAL :: single_point_run = .FALSE.
     227             :       LOGICAL :: given_embed_pot = .FALSE.
     228             :       LOGICAL :: energy_correction = .FALSE.
     229             :       LOGICAL :: harris_method = .FALSE.
     230             :       REAL(KIND=dp)                                         :: sim_time = -1.0_dp
     231             :       REAL(KIND=dp) :: start_time = -1.0_dp, target_time = -1.0_dp
     232             :       REAL(KIND=dp), DIMENSION(:, :), POINTER               :: image_matrix => NULL()
     233             :       REAL(KIND=dp), DIMENSION(:), POINTER                  :: image_coeff => NULL()
     234             :       INTEGER, DIMENSION(:), POINTER                        :: ipiv => NULL()
     235             :       INTEGER                                               :: sim_step = -1
     236             :       TYPE(ls_scf_env_type), POINTER                        :: ls_scf_env => NULL()
     237             :       TYPE(almo_scf_env_type), POINTER                      :: almo_scf_env => NULL()
     238             :       TYPE(transport_env_type), POINTER                     :: transport_env => NULL()
     239             :       TYPE(cell_type), POINTER                              :: super_cell => NULL()
     240             :       TYPE(mo_set_type), DIMENSION(:), POINTER              :: mos => NULL()
     241             :       TYPE(cp_fm_type), DIMENSION(:), POINTER               :: mo_loc_history => NULL()
     242             :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER             :: mo_derivs => NULL()
     243             :       TYPE(scf_control_type), POINTER                       :: scf_control => NULL()
     244             :       TYPE(rel_control_type), POINTER                       :: rel_control => NULL()
     245             :       ! ZMP adding variables
     246             :       TYPE(qs_rho_type), POINTER                            :: rho_external => NULL()
     247             :       TYPE(pw_r3d_rs_type), POINTER                         :: external_vxc => NULL()
     248             :       TYPE(pw_r3d_rs_type), POINTER                         :: mask => NULL()
     249             :       TYPE(qs_charges_type), POINTER                        :: qs_charges => NULL()
     250             :       TYPE(qs_ks_env_type), POINTER                         :: ks_env => NULL()
     251             :       TYPE(qs_ks_qmmm_env_type), POINTER                    :: ks_qmmm_env => NULL()
     252             :       TYPE(qmmm_env_qm_type), POINTER                       :: qmmm_env_qm => NULL()
     253             :       TYPE(qs_wf_history_type), POINTER                     :: wf_history => NULL()
     254             :       TYPE(qs_scf_env_type), POINTER                        :: scf_env => NULL()
     255             :       TYPE(qs_matrix_pools_type), POINTER                   :: mpools => NULL()
     256             :       TYPE(oce_matrix_type), POINTER                        :: oce => NULL()
     257             :       TYPE(local_rho_type), POINTER                         :: local_rho_set => NULL()
     258             :       TYPE(hartree_local_type), POINTER                     :: hartree_local => NULL()
     259             :       TYPE(section_vals_type), POINTER                      :: input => NULL()
     260             :       TYPE(linres_control_type), POINTER                    :: linres_control => NULL()
     261             :       TYPE(xas_environment_type), POINTER                   :: xas_env => NULL()
     262             :       TYPE(cp_ddapc_type), POINTER                          :: cp_ddapc_env => NULL()
     263             :       TYPE(cp_ddapc_ewald_type), POINTER                    :: cp_ddapc_ewald => NULL()
     264             :       REAL(KIND=dp), DIMENSION(:, :), POINTER               :: outer_scf_history => NULL()
     265             :       INTEGER                                               :: outer_scf_ihistory = -1
     266             :       REAL(KIND=dp), DIMENSION(:, :), POINTER               :: gradient_history => NULL(), &
     267             :                                                                variable_history => NULL()
     268             :       TYPE(hfx_type), DIMENSION(:, :), POINTER              :: x_data => NULL()
     269             :       TYPE(et_coupling_type), POINTER                       :: et_coupling => NULL()
     270             :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), POINTER  :: dftb_potential => NULL()
     271             :       TYPE(admm_type), POINTER                              :: admm_env => NULL()
     272             :       TYPE(active_space_type), POINTER                      :: active_space => NULL()
     273             :       ! LRI
     274             :       TYPE(lri_environment_type), POINTER                   :: lri_env => NULL()
     275             :       TYPE(lri_density_type), POINTER                       :: lri_density => NULL()
     276             :       ! Harris model
     277             :       TYPE(harris_type), POINTER                            :: harris_env => NULL()
     278             :       ! Energy correction
     279             :       TYPE(energy_correction_type), POINTER                 :: ec_env => NULL()
     280             :       ! Excited States
     281             :       LOGICAL                                               :: excited_state = .FALSE.
     282             :       TYPE(excited_energy_type), POINTER                    :: exstate_env => NULL()
     283             :       ! Empirical dispersion
     284             :       TYPE(qs_dispersion_type), POINTER                     :: dispersion_env => NULL()
     285             :       ! Empirical geometrical BSSE correction
     286             :       TYPE(qs_gcp_type), POINTER                            :: gcp_env => NULL()
     287             :       ! Semi-empirical and DFTB types
     288             :       TYPE(ewald_environment_type), POINTER                 :: ewald_env => NULL()
     289             :       TYPE(ewald_pw_type), POINTER                          :: ewald_pw => NULL()
     290             :       ! Semi-empirical types
     291             :       TYPE(se_taper_type), POINTER                          :: se_taper => NULL()
     292             :       TYPE(semi_empirical_si_type), POINTER                 :: se_store_int_env => NULL()
     293             :       TYPE(nddo_mpole_type), POINTER                        :: se_nddo_mpole => NULL()
     294             :       TYPE(fist_nonbond_env_type), POINTER                  :: se_nonbond_env => NULL()
     295             :       TYPE(rt_prop_type), POINTER                           :: rtp => NULL()
     296             :       TYPE(efield_berry_type), POINTER                      :: efield => NULL()
     297             :       ! a history for the broyden ot
     298             :       REAL(KIND=dp)                                         :: broyden_adaptive_sigma = -1.0_dp
     299             :       TYPE(mp2_type), POINTER                               :: mp2_env => NULL()
     300             :       TYPE(post_scf_bandstructure_type), POINTER                                :: bs_env => NULL()
     301             :       TYPE(kg_environment_type), POINTER                    :: kg_env => NULL()
     302             :       TYPE(wannier_centres_type), POINTER, DIMENSION(:)     :: WannierCentres => NULL()
     303             :       TYPE(molecular_scf_guess_env_type), POINTER           :: molecular_scf_guess_env => NULL()
     304             :       ! Subsystem densities
     305             :       TYPE(qs_rho_p_type), DIMENSION(:), POINTER            :: subsys_dens => NULL()
     306             :       ! Embedding potential
     307             :       TYPE(pw_r3d_rs_type), POINTER                         :: embed_pot => NULL()
     308             :       TYPE(pw_r3d_rs_type), POINTER                         :: spin_embed_pot => NULL()
     309             :       ! Polarizability tensor
     310             :       TYPE(polar_env_type), POINTER                         :: polar_env => NULL()
     311             :       ! Resp charges
     312             :       REAL(KIND=dp), DIMENSION(:), POINTER                  :: rhs => NULL()
     313             :       REAL(KIND=dp)                                         :: total_zeff_corr = -1.0_dp, surface_dipole_moment = -1.0_dp
     314             :       LOGICAL                                               :: surface_dipole_switch_off = .FALSE.
     315             :       TYPE(mo_set_type), DIMENSION(:), POINTER              :: mos_last_converged => NULL()
     316             :    END TYPE qs_environment_type
     317             : 
     318             : CONTAINS
     319             : 
     320             : ! **************************************************************************************************
     321             : !> \brief   Get the QUICKSTEP environment.
     322             : !> \param qs_env ...
     323             : !> \param atomic_kind_set ...
     324             : !> \param qs_kind_set ...
     325             : !> \param cell ...
     326             : !> \param super_cell ...
     327             : !> \param cell_ref ...
     328             : !> \param use_ref_cell ...
     329             : !> \param kpoints ...
     330             : !> \param dft_control ...
     331             : !> \param mos ...
     332             : !> \param sab_orb ...
     333             : !> \param sab_all ...
     334             : !> \param qmmm ...
     335             : !> \param qmmm_periodic ...
     336             : !> \param sac_ae ...
     337             : !> \param sac_ppl ...
     338             : !> \param sac_lri ...
     339             : !> \param sap_ppnl ...
     340             : !> \param sab_vdw ...
     341             : !> \param sab_scp ...
     342             : !> \param sap_oce ...
     343             : !> \param sab_lrc ...
     344             : !> \param sab_se ...
     345             : !> \param sab_xtbe ...
     346             : !> \param sab_tbe ...
     347             : !> \param sab_core ...
     348             : !> \param sab_xb ...
     349             : !> \param sab_xtb_pp ...
     350             : !> \param sab_xtb_nonbond ...
     351             : !> \param sab_almo ...
     352             : !> \param sab_kp ...
     353             : !> \param sab_kp_nosym ...
     354             : !> \param particle_set ...
     355             : !> \param energy ...
     356             : !> \param force ...
     357             : !> \param matrix_h ...
     358             : !> \param matrix_h_im ...
     359             : !> \param matrix_ks ...
     360             : !> \param matrix_ks_im ...
     361             : !> \param matrix_vxc ...
     362             : !> \param run_rtp ...
     363             : !> \param rtp ...
     364             : !> \param matrix_h_kp ...
     365             : !> \param matrix_h_im_kp ...
     366             : !> \param matrix_ks_kp ...
     367             : !> \param matrix_ks_im_kp ...
     368             : !> \param matrix_vxc_kp ...
     369             : !> \param kinetic_kp ...
     370             : !> \param matrix_s_kp ...
     371             : !> \param matrix_w_kp ...
     372             : !> \param matrix_s_RI_aux_kp ...
     373             : !> \param matrix_s ...
     374             : !> \param matrix_s_RI_aux ...
     375             : !> \param matrix_w ...
     376             : !> \param matrix_p_mp2 ...
     377             : !> \param matrix_p_mp2_admm ...
     378             : !> \param rho ...
     379             : !> \param rho_xc ...
     380             : !> \param pw_env ...
     381             : !> \param ewald_env ...
     382             : !> \param ewald_pw ...
     383             : !> \param active_space ...
     384             : !> \param mpools ...
     385             : !> \param input ...
     386             : !> \param para_env ...
     387             : !> \param blacs_env ...
     388             : !> \param scf_control ...
     389             : !> \param rel_control ...
     390             : !> \param kinetic ...
     391             : !> \param qs_charges ...
     392             : !> \param vppl ...
     393             : !> \param rho_core ...
     394             : !> \param rho_nlcc ...
     395             : !> \param rho_nlcc_g ...
     396             : !> \param ks_env ...
     397             : !> \param ks_qmmm_env ...
     398             : !> \param wf_history ...
     399             : !> \param scf_env ...
     400             : !> \param local_particles ...
     401             : !> \param local_molecules ...
     402             : !> \param distribution_2d ...
     403             : !> \param dbcsr_dist ...
     404             : !> \param molecule_kind_set ...
     405             : !> \param molecule_set ...
     406             : !> \param subsys ...
     407             : !> \param cp_subsys ...
     408             : !> \param oce ...
     409             : !> \param local_rho_set ...
     410             : !> \param rho_atom_set ...
     411             : !> \param task_list ...
     412             : !> \param task_list_soft ...
     413             : !> \param rho0_atom_set ...
     414             : !> \param rho0_mpole ...
     415             : !> \param rhoz_set ...
     416             : !> \param ecoul_1c ...
     417             : !> \param rho0_s_rs ...
     418             : !> \param rho0_s_gs ...
     419             : !> \param do_kpoints ...
     420             : !> \param has_unit_metric ...
     421             : !> \param requires_mo_derivs ...
     422             : !> \param mo_derivs ...
     423             : !> \param mo_loc_history ...
     424             : !> \param nkind ...
     425             : !> \param natom ...
     426             : !> \param nelectron_total ...
     427             : !> \param nelectron_spin ...
     428             : !> \param efield ...
     429             : !> \param neighbor_list_id ...
     430             : !> \param linres_control ...
     431             : !> \param xas_env ...
     432             : !> \param virial ...
     433             : !> \param cp_ddapc_env ...
     434             : !> \param cp_ddapc_ewald ...
     435             : !> \param outer_scf_history ...
     436             : !> \param outer_scf_ihistory ...
     437             : !> \param x_data ...
     438             : !> \param et_coupling ...
     439             : !> \param dftb_potential ...
     440             : !> \param results ...
     441             : !> \param se_taper ...
     442             : !> \param se_store_int_env ...
     443             : !> \param se_nddo_mpole ...
     444             : !> \param se_nonbond_env ...
     445             : !> \param admm_env ...
     446             : !> \param lri_env ...
     447             : !> \param lri_density ...
     448             : !> \param exstate_env ...
     449             : !> \param ec_env ...
     450             : !> \param harris_env ...
     451             : !> \param dispersion_env ...
     452             : !> \param gcp_env ...
     453             : !> \param vee ...
     454             : !> \param rho_external ...
     455             : !> \param external_vxc ...
     456             : !> \param mask ...
     457             : !> \param mp2_env ...
     458             : !> \param bs_env ...
     459             : !> \param kg_env ...
     460             : !> \param WannierCentres ...
     461             : !> \param atprop ...
     462             : !> \param ls_scf_env ...
     463             : !> \param do_transport ...
     464             : !> \param transport_env ...
     465             : !> \param v_hartree_rspace ...
     466             : !> \param s_mstruct_changed ...
     467             : !> \param rho_changed ...
     468             : !> \param potential_changed ...
     469             : !> \param forces_up_to_date ...
     470             : !> \param mscfg_env ...
     471             : !> \param almo_scf_env ...
     472             : !> \param gradient_history ...
     473             : !> \param variable_history ...
     474             : !> \param embed_pot ...
     475             : !> \param spin_embed_pot ...
     476             : !> \param polar_env ...
     477             : !> \param mos_last_converged ... [SGh]
     478             : !> \param rhs ...
     479             : !> \date    23.01.2002
     480             : !> \author  MK
     481             : !> \version 1.0
     482             : ! **************************************************************************************************
     483     8460064 :    SUBROUTINE get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, &
     484             :                          dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, sac_ae, sac_ppl, sac_lri, &
     485             :                          sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, &
     486             :                          sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, &
     487             :                          sab_kp, sab_kp_nosym, particle_set, energy, force, &
     488             :                          matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, &
     489             :                          matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, &
     490             :                          matrix_w_kp, matrix_s_RI_aux_kp, matrix_s, matrix_s_RI_aux, matrix_w, &
     491             :                          matrix_p_mp2, matrix_p_mp2_admm, rho, &
     492             :                          rho_xc, pw_env, ewald_env, ewald_pw, active_space, &
     493             :                          mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, &
     494             :                          vppl, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, &
     495             :                          local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, &
     496             :                          molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, &
     497             :                          task_list, &
     498             :                          task_list_soft, &
     499             :                          rho0_atom_set, rho0_mpole, rhoz_set, ecoul_1c, &
     500             :                          rho0_s_rs, rho0_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, &
     501             :                          mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, &
     502             :                          neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, &
     503             :                          outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, &
     504             :                          se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, &
     505             :                          lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, &
     506             :                          rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, &
     507             :                          WannierCentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, &
     508             :                          s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, &
     509             :                          gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, rhs)
     510             :       TYPE(qs_environment_type), INTENT(IN)              :: qs_env
     511             :       TYPE(atomic_kind_type), DIMENSION(:), OPTIONAL, &
     512             :          POINTER                                         :: atomic_kind_set
     513             :       TYPE(qs_kind_type), DIMENSION(:), OPTIONAL, &
     514             :          POINTER                                         :: qs_kind_set
     515             :       TYPE(cell_type), OPTIONAL, POINTER                 :: cell, super_cell, cell_ref
     516             :       LOGICAL, OPTIONAL                                  :: use_ref_cell
     517             :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoints
     518             :       TYPE(dft_control_type), OPTIONAL, POINTER          :: dft_control
     519             :       TYPE(mo_set_type), DIMENSION(:), OPTIONAL, POINTER :: mos
     520             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     521             :          OPTIONAL, POINTER                               :: sab_orb, sab_all
     522             :       LOGICAL, OPTIONAL                                  :: qmmm, qmmm_periodic
     523             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), OPTIONAL, POINTER :: sac_ae, sac_ppl, sac_lri, &
     524             :          sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, &
     525             :          sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym
     526             :       TYPE(particle_type), DIMENSION(:), OPTIONAL, &
     527             :          POINTER                                         :: particle_set
     528             :       TYPE(qs_energy_type), OPTIONAL, POINTER            :: energy
     529             :       TYPE(qs_force_type), DIMENSION(:), OPTIONAL, &
     530             :          POINTER                                         :: force
     531             :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
     532             :          POINTER                                         :: matrix_h, matrix_h_im, matrix_ks, &
     533             :                                                             matrix_ks_im, matrix_vxc
     534             :       LOGICAL, OPTIONAL                                  :: run_rtp
     535             :       TYPE(rt_prop_type), OPTIONAL, POINTER              :: rtp
     536             :       TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, POINTER :: matrix_h_kp, matrix_h_im_kp, &
     537             :          matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, &
     538             :          matrix_s_RI_aux_kp
     539             :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
     540             :          POINTER                                         :: matrix_s, matrix_s_RI_aux, matrix_w, &
     541             :                                                             matrix_p_mp2, matrix_p_mp2_admm
     542             :       TYPE(qs_rho_type), OPTIONAL, POINTER               :: rho, rho_xc
     543             :       TYPE(pw_env_type), OPTIONAL, POINTER               :: pw_env
     544             :       TYPE(ewald_environment_type), OPTIONAL, POINTER    :: ewald_env
     545             :       TYPE(ewald_pw_type), OPTIONAL, POINTER             :: ewald_pw
     546             :       TYPE(active_space_type), OPTIONAL, POINTER         :: active_space
     547             :       TYPE(qs_matrix_pools_type), OPTIONAL, POINTER      :: mpools
     548             :       TYPE(section_vals_type), OPTIONAL, POINTER         :: input
     549             :       TYPE(mp_para_env_type), OPTIONAL, POINTER          :: para_env
     550             :       TYPE(cp_blacs_env_type), OPTIONAL, POINTER         :: blacs_env
     551             :       TYPE(scf_control_type), OPTIONAL, POINTER          :: scf_control
     552             :       TYPE(rel_control_type), OPTIONAL, POINTER          :: rel_control
     553             :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
     554             :          POINTER                                         :: kinetic
     555             :       TYPE(qs_charges_type), OPTIONAL, POINTER           :: qs_charges
     556             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: vppl
     557             :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho_core
     558             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: rho_nlcc
     559             :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho_nlcc_g
     560             :       TYPE(qs_ks_env_type), OPTIONAL, POINTER            :: ks_env
     561             :       TYPE(qs_ks_qmmm_env_type), OPTIONAL, POINTER       :: ks_qmmm_env
     562             :       TYPE(qs_wf_history_type), OPTIONAL, POINTER        :: wf_history
     563             :       TYPE(qs_scf_env_type), OPTIONAL, POINTER           :: scf_env
     564             :       TYPE(distribution_1d_type), OPTIONAL, POINTER      :: local_particles, local_molecules
     565             :       TYPE(distribution_2d_type), OPTIONAL, POINTER      :: distribution_2d
     566             :       TYPE(dbcsr_distribution_type), OPTIONAL, POINTER   :: dbcsr_dist
     567             :       TYPE(molecule_kind_type), DIMENSION(:), OPTIONAL, &
     568             :          POINTER                                         :: molecule_kind_set
     569             :       TYPE(molecule_type), DIMENSION(:), OPTIONAL, &
     570             :          POINTER                                         :: molecule_set
     571             :       TYPE(qs_subsys_type), OPTIONAL, POINTER            :: subsys
     572             :       TYPE(cp_subsys_type), OPTIONAL, POINTER            :: cp_subsys
     573             :       TYPE(oce_matrix_type), OPTIONAL, POINTER           :: oce
     574             :       TYPE(local_rho_type), OPTIONAL, POINTER            :: local_rho_set
     575             :       TYPE(rho_atom_type), DIMENSION(:), OPTIONAL, &
     576             :          POINTER                                         :: rho_atom_set
     577             :       TYPE(task_list_type), OPTIONAL, POINTER            :: task_list, task_list_soft
     578             :       TYPE(rho0_atom_type), DIMENSION(:), OPTIONAL, &
     579             :          POINTER                                         :: rho0_atom_set
     580             :       TYPE(rho0_mpole_type), OPTIONAL, POINTER           :: rho0_mpole
     581             :       TYPE(rhoz_type), DIMENSION(:), OPTIONAL, POINTER   :: rhoz_set
     582             :       TYPE(ecoul_1center_type), DIMENSION(:), OPTIONAL, &
     583             :          POINTER                                         :: ecoul_1c
     584             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: rho0_s_rs
     585             :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho0_s_gs
     586             :       LOGICAL, OPTIONAL                                  :: do_kpoints, has_unit_metric, &
     587             :                                                             requires_mo_derivs
     588             :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
     589             :          POINTER                                         :: mo_derivs
     590             :       TYPE(cp_fm_type), DIMENSION(:), OPTIONAL, POINTER  :: mo_loc_history
     591             :       INTEGER, OPTIONAL                                  :: nkind, natom, nelectron_total
     592             :       INTEGER, DIMENSION(2), OPTIONAL                    :: nelectron_spin
     593             :       TYPE(efield_berry_type), OPTIONAL, POINTER         :: efield
     594             :       INTEGER, OPTIONAL                                  :: neighbor_list_id
     595             :       TYPE(linres_control_type), OPTIONAL, POINTER       :: linres_control
     596             :       TYPE(xas_environment_type), OPTIONAL, POINTER      :: xas_env
     597             :       TYPE(virial_type), OPTIONAL, POINTER               :: virial
     598             :       TYPE(cp_ddapc_type), OPTIONAL, POINTER             :: cp_ddapc_env
     599             :       TYPE(cp_ddapc_ewald_type), OPTIONAL, POINTER       :: cp_ddapc_ewald
     600             :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: outer_scf_history
     601             :       INTEGER, INTENT(out), OPTIONAL                     :: outer_scf_ihistory
     602             :       TYPE(hfx_type), DIMENSION(:, :), OPTIONAL, POINTER :: x_data
     603             :       TYPE(et_coupling_type), OPTIONAL, POINTER          :: et_coupling
     604             :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), &
     605             :          OPTIONAL, POINTER                               :: dftb_potential
     606             :       TYPE(cp_result_type), OPTIONAL, POINTER            :: results
     607             :       TYPE(se_taper_type), OPTIONAL, POINTER             :: se_taper
     608             :       TYPE(semi_empirical_si_type), OPTIONAL, POINTER    :: se_store_int_env
     609             :       TYPE(nddo_mpole_type), OPTIONAL, POINTER           :: se_nddo_mpole
     610             :       TYPE(fist_nonbond_env_type), OPTIONAL, POINTER     :: se_nonbond_env
     611             :       TYPE(admm_type), OPTIONAL, POINTER                 :: admm_env
     612             :       TYPE(lri_environment_type), OPTIONAL, POINTER      :: lri_env
     613             :       TYPE(lri_density_type), OPTIONAL, POINTER          :: lri_density
     614             :       TYPE(excited_energy_type), OPTIONAL, POINTER       :: exstate_env
     615             :       TYPE(energy_correction_type), OPTIONAL, POINTER    :: ec_env
     616             :       TYPE(harris_type), OPTIONAL, POINTER               :: harris_env
     617             :       TYPE(qs_dispersion_type), OPTIONAL, POINTER        :: dispersion_env
     618             :       TYPE(qs_gcp_type), OPTIONAL, POINTER               :: gcp_env
     619             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: vee
     620             :       TYPE(qs_rho_type), OPTIONAL, POINTER               :: rho_external
     621             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: external_vxc, mask
     622             :       TYPE(mp2_type), OPTIONAL, POINTER                  :: mp2_env
     623             :       TYPE(post_scf_bandstructure_type), OPTIONAL, &
     624             :          POINTER                                         :: bs_env
     625             :       TYPE(kg_environment_type), OPTIONAL, POINTER       :: kg_env
     626             :       TYPE(wannier_centres_type), DIMENSION(:), &
     627             :          OPTIONAL, POINTER                               :: WannierCentres
     628             :       TYPE(atprop_type), OPTIONAL, POINTER               :: atprop
     629             :       TYPE(ls_scf_env_type), OPTIONAL, POINTER           :: ls_scf_env
     630             :       LOGICAL, OPTIONAL                                  :: do_transport
     631             :       TYPE(transport_env_type), OPTIONAL, POINTER        :: transport_env
     632             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: v_hartree_rspace
     633             :       LOGICAL, OPTIONAL                                  :: s_mstruct_changed, rho_changed, &
     634             :                                                             potential_changed, forces_up_to_date
     635             :       TYPE(molecular_scf_guess_env_type), OPTIONAL, &
     636             :          POINTER                                         :: mscfg_env
     637             :       TYPE(almo_scf_env_type), OPTIONAL, POINTER         :: almo_scf_env
     638             :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: gradient_history, variable_history
     639             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: embed_pot, spin_embed_pot
     640             :       TYPE(polar_env_type), OPTIONAL, POINTER            :: polar_env
     641             :       TYPE(mo_set_type), DIMENSION(:), OPTIONAL, POINTER :: mos_last_converged
     642             :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: rhs
     643             : 
     644             :       TYPE(rho0_mpole_type), POINTER                     :: rho0_m
     645             : 
     646     8460064 :       NULLIFY (rho0_m)
     647     8460064 :       CPASSERT(ASSOCIATED(qs_env%ks_env))
     648             : 
     649     8460064 :       IF (PRESENT(outer_scf_history)) outer_scf_history => qs_env%outer_scf_history
     650     8460064 :       IF (PRESENT(outer_scf_ihistory)) outer_scf_ihistory = qs_env%outer_scf_ihistory
     651     8460064 :       IF (PRESENT(gradient_history)) gradient_history => qs_env%gradient_history
     652     8460064 :       IF (PRESENT(variable_history)) variable_history => qs_env%variable_history
     653     8460064 :       IF (PRESENT(mp2_env)) mp2_env => qs_env%mp2_env
     654     8460064 :       IF (PRESENT(bs_env)) bs_env => qs_env%bs_env
     655     8460064 :       IF (PRESENT(kg_env)) kg_env => qs_env%kg_env
     656     8460064 :       IF (PRESENT(super_cell)) super_cell => qs_env%super_cell
     657     8460064 :       IF (PRESENT(qmmm)) qmmm = qs_env%qmmm
     658     8460064 :       IF (PRESENT(qmmm_periodic)) qmmm_periodic = qs_env%qmmm_periodic
     659     8460064 :       IF (PRESENT(mos)) mos => qs_env%mos
     660     8460064 :       IF (PRESENT(mos_last_converged)) mos_last_converged => qs_env%mos_last_converged
     661     8460064 :       IF (PRESENT(ewald_env)) ewald_env => qs_env%ewald_env
     662     8460064 :       IF (PRESENT(ewald_pw)) ewald_pw => qs_env%ewald_pw
     663     8460064 :       IF (PRESENT(mpools)) mpools => qs_env%mpools
     664     8460064 :       IF (PRESENT(scf_control)) scf_control => qs_env%scf_control
     665     8460064 :       IF (PRESENT(rel_control)) rel_control => qs_env%rel_control
     666             :       ! ZMP pointing vectors
     667     8460064 :       IF (PRESENT(rho_external)) rho_external => qs_env%rho_external
     668     8460064 :       IF (PRESENT(external_vxc)) external_vxc => qs_env%external_vxc
     669     8460064 :       IF (PRESENT(mask)) mask => qs_env%mask
     670     8460064 :       IF (PRESENT(qs_charges)) qs_charges => qs_env%qs_charges
     671     8460064 :       IF (PRESENT(ks_env)) ks_env => qs_env%ks_env
     672     8460064 :       IF (PRESENT(ks_qmmm_env)) ks_qmmm_env => qs_env%ks_qmmm_env
     673     8460064 :       IF (PRESENT(wf_history)) wf_history => qs_env%wf_history
     674     8460064 :       IF (PRESENT(scf_env)) scf_env => qs_env%scf_env
     675     8460064 :       IF (PRESENT(oce)) oce => qs_env%oce
     676     8460064 :       IF (PRESENT(requires_mo_derivs)) requires_mo_derivs = qs_env%requires_mo_derivs
     677     8460064 :       IF (PRESENT(has_unit_metric)) has_unit_metric = qs_env%has_unit_metric
     678     8460064 :       IF (PRESENT(mo_derivs)) mo_derivs => qs_env%mo_derivs
     679     8460064 :       IF (PRESENT(mo_loc_history)) mo_loc_history => qs_env%mo_loc_history
     680     8460064 :       IF (PRESENT(linres_control)) linres_control => qs_env%linres_control
     681     8460064 :       IF (PRESENT(se_taper)) se_taper => qs_env%se_taper
     682     8460064 :       IF (PRESENT(se_store_int_env)) se_store_int_env => qs_env%se_store_int_env
     683     8460064 :       IF (PRESENT(se_nddo_mpole)) se_nddo_mpole => qs_env%se_nddo_mpole
     684     8460064 :       IF (PRESENT(se_nonbond_env)) se_nonbond_env => qs_env%se_nonbond_env
     685     8460064 :       IF (PRESENT(lri_env)) lri_env => qs_env%lri_env
     686     8460064 :       IF (PRESENT(lri_density)) lri_density => qs_env%lri_density
     687     8460064 :       IF (PRESENT(harris_env)) harris_env => qs_env%harris_env
     688     8460064 :       IF (PRESENT(ec_env)) ec_env => qs_env%ec_env
     689     8460064 :       IF (PRESENT(exstate_env)) exstate_env => qs_env%exstate_env
     690     8460064 :       IF (PRESENT(dispersion_env)) dispersion_env => qs_env%dispersion_env
     691     8460064 :       IF (PRESENT(gcp_env)) gcp_env => qs_env%gcp_env
     692     8460064 :       IF (PRESENT(run_rtp)) run_rtp = qs_env%run_rtp
     693     8460064 :       IF (PRESENT(rtp)) rtp => qs_env%rtp
     694     8460064 :       IF (PRESENT(ls_scf_env)) ls_scf_env => qs_env%ls_scf_env
     695     8460064 :       IF (PRESENT(almo_scf_env)) almo_scf_env => qs_env%almo_scf_env
     696     8460064 :       IF (PRESENT(do_transport)) do_transport = qs_env%do_transport
     697     8460064 :       IF (PRESENT(transport_env)) transport_env => qs_env%transport_env
     698     8460064 :       IF (PRESENT(mscfg_env)) mscfg_env => qs_env%molecular_scf_guess_env
     699     8460064 :       IF (PRESENT(active_space)) active_space => qs_env%active_space
     700     8460064 :       IF (PRESENT(admm_env)) admm_env => qs_env%admm_env
     701             : 
     702             :       ! Embedding potential
     703     8460064 :       IF (PRESENT(embed_pot)) embed_pot => qs_env%embed_pot
     704     8460064 :       IF (PRESENT(spin_embed_pot)) spin_embed_pot => qs_env%spin_embed_pot
     705             : 
     706             :       ! Polarisability tensor
     707     8460064 :       IF (PRESENT(polar_env)) polar_env => qs_env%polar_env
     708             : 
     709             :       ! Resp charges
     710     8460064 :       IF (PRESENT(rhs)) rhs => qs_env%rhs
     711             : 
     712     8460064 :       IF (PRESENT(local_rho_set)) &
     713       13930 :          local_rho_set => qs_env%local_rho_set
     714     8460064 :       IF (PRESENT(rho_atom_set)) &
     715       98886 :          CALL get_local_rho(qs_env%local_rho_set, rho_atom_set=rho_atom_set)
     716     8460064 :       IF (PRESENT(rho0_atom_set)) &
     717       22800 :          CALL get_local_rho(qs_env%local_rho_set, rho0_atom_set=rho0_atom_set)
     718     8460064 :       IF (PRESENT(rho0_mpole)) &
     719       40984 :          CALL get_local_rho(qs_env%local_rho_set, rho0_mpole=rho0_mpole)
     720     8460064 :       IF (PRESENT(rhoz_set)) &
     721          14 :          CALL get_local_rho(qs_env%local_rho_set, rhoz_set=rhoz_set)
     722     8460064 :       IF (PRESENT(ecoul_1c)) &
     723       13122 :          CALL get_hartree_local(qs_env%hartree_local, ecoul_1c=ecoul_1c)
     724     8460064 :       IF (PRESENT(rho0_s_rs)) THEN
     725        1488 :          CALL get_local_rho(qs_env%local_rho_set, rho0_mpole=rho0_m)
     726        1488 :          IF (ASSOCIATED(rho0_m)) THEN
     727        1488 :             rho0_s_rs => rho0_m%rho0_s_rs
     728             :          END IF
     729             :       END IF
     730     8460064 :       IF (PRESENT(rho0_s_gs)) THEN
     731       18490 :          CALL get_local_rho(qs_env%local_rho_set, rho0_mpole=rho0_m)
     732       18490 :          IF (ASSOCIATED(rho0_m)) THEN
     733       13336 :             rho0_s_gs => rho0_m%rho0_s_gs
     734             :          END IF
     735             :       END IF
     736             : 
     737     8460064 :       IF (PRESENT(xas_env)) xas_env => qs_env%xas_env
     738     8460064 :       IF (PRESENT(input)) input => qs_env%input
     739     8460064 :       IF (PRESENT(cp_ddapc_env)) cp_ddapc_env => qs_env%cp_ddapc_env
     740     8460064 :       IF (PRESENT(cp_ddapc_ewald)) cp_ddapc_ewald => qs_env%cp_ddapc_ewald
     741     8460064 :       IF (PRESENT(x_data)) x_data => qs_env%x_data
     742     8460064 :       IF (PRESENT(et_coupling)) et_coupling => qs_env%et_coupling
     743     8460064 :       IF (PRESENT(dftb_potential)) dftb_potential => qs_env%dftb_potential
     744     8460064 :       IF (PRESENT(efield)) efield => qs_env%efield
     745     8460064 :       IF (PRESENT(WannierCentres)) WannierCentres => qs_env%WannierCentres
     746             : 
     747             :       CALL get_ks_env(qs_env%ks_env, &
     748             :                       v_hartree_rspace=v_hartree_rspace, &
     749             :                       s_mstruct_changed=s_mstruct_changed, &
     750             :                       rho_changed=rho_changed, &
     751             :                       potential_changed=potential_changed, &
     752             :                       forces_up_to_date=forces_up_to_date, &
     753             :                       matrix_h=matrix_h, &
     754             :                       matrix_h_im=matrix_h_im, &
     755             :                       matrix_ks=matrix_ks, &
     756             :                       matrix_ks_im=matrix_ks_im, &
     757             :                       matrix_vxc=matrix_vxc, &
     758             :                       kinetic=kinetic, &
     759             :                       matrix_s=matrix_s, &
     760             :                       matrix_s_RI_aux=matrix_s_RI_aux, &
     761             :                       matrix_ks_im_kp=matrix_ks_im_kp, &
     762             :                       matrix_w=matrix_w, &
     763             :                       matrix_p_mp2=matrix_p_mp2, &
     764             :                       matrix_p_mp2_admm=matrix_p_mp2_admm, &
     765             :                       matrix_h_kp=matrix_h_kp, &
     766             :                       matrix_h_im_kp=matrix_h_im_kp, &
     767             :                       matrix_ks_kp=matrix_ks_kp, &
     768             :                       matrix_vxc_kp=matrix_vxc_kp, &
     769             :                       kinetic_kp=kinetic_kp, &
     770             :                       matrix_s_kp=matrix_s_kp, &
     771             :                       matrix_w_kp=matrix_w_kp, &
     772             :                       matrix_s_RI_aux_kp=matrix_s_RI_aux_kp, &
     773             :                       rho=rho, &
     774             :                       rho_xc=rho_xc, &
     775             :                       rho_core=rho_core, &
     776             :                       rho_nlcc=rho_nlcc, &
     777             :                       rho_nlcc_g=rho_nlcc_g, &
     778             :                       vppl=vppl, &
     779             :                       vee=vee, &
     780             :                       neighbor_list_id=neighbor_list_id, &
     781             :                       sab_orb=sab_orb, &
     782             :                       sab_all=sab_all, &
     783             :                       sab_scp=sab_scp, &
     784             :                       sab_vdw=sab_vdw, &
     785             :                       sac_ae=sac_ae, &
     786             :                       sac_ppl=sac_ppl, &
     787             :                       sac_lri=sac_lri, &
     788             :                       sap_ppnl=sap_ppnl, &
     789             :                       sap_oce=sap_oce, &
     790             :                       sab_se=sab_se, &
     791             :                       sab_lrc=sab_lrc, &
     792             :                       sab_tbe=sab_tbe, &
     793             :                       sab_xtbe=sab_xtbe, &
     794             :                       sab_core=sab_core, &
     795             :                       sab_xb=sab_xb, &
     796             :                       sab_xtb_pp=sab_xtb_pp, &
     797             :                       sab_xtb_nonbond=sab_xtb_nonbond, &
     798             :                       sab_almo=sab_almo, &
     799             :                       sab_kp=sab_kp, &
     800             :                       sab_kp_nosym=sab_kp_nosym, &
     801             :                       task_list=task_list, &
     802             :                       task_list_soft=task_list_soft, &
     803             :                       kpoints=kpoints, &
     804             :                       do_kpoints=do_kpoints, &
     805             :                       local_molecules=local_molecules, &
     806             :                       local_particles=local_particles, &
     807             :                       atprop=atprop, &
     808             :                       virial=virial, &
     809             :                       results=results, &
     810             :                       cell=cell, &
     811             :                       cell_ref=cell_ref, &
     812             :                       use_ref_cell=use_ref_cell, &
     813             :                       energy=energy, &
     814             :                       force=force, &
     815             :                       qs_kind_set=qs_kind_set, &
     816             :                       subsys=subsys, &
     817             :                       cp_subsys=cp_subsys, &
     818             :                       atomic_kind_set=atomic_kind_set, &
     819             :                       particle_set=particle_set, &
     820             :                       molecule_kind_set=molecule_kind_set, &
     821             :                       molecule_set=molecule_set, &
     822             :                       natom=natom, &
     823             :                       nkind=nkind, &
     824             :                       dft_control=dft_control, &
     825             :                       dbcsr_dist=dbcsr_dist, &
     826             :                       distribution_2d=distribution_2d, &
     827             :                       pw_env=pw_env, &
     828             :                       para_env=para_env, &
     829             :                       blacs_env=blacs_env, &
     830             :                       nelectron_total=nelectron_total, &
     831     8460064 :                       nelectron_spin=nelectron_spin)
     832             : 
     833     8460064 :    END SUBROUTINE get_qs_env
     834             : 
     835             : ! **************************************************************************************************
     836             : !> \brief  Initialise the QUICKSTEP environment.
     837             : !> \param qs_env ...
     838             : !> \param globenv ...
     839             : !> \date    25.01.2002
     840             : !> \author  MK
     841             : !> \version 1.0
     842             : ! **************************************************************************************************
     843        7334 :    SUBROUTINE init_qs_env(qs_env, globenv)
     844             : 
     845             :       TYPE(qs_environment_type), INTENT(INOUT)           :: qs_env
     846             :       TYPE(global_environment_type), OPTIONAL, POINTER   :: globenv
     847             : 
     848        7334 :       NULLIFY (qs_env%ls_scf_env)
     849        7334 :       NULLIFY (qs_env%almo_scf_env)
     850        7334 :       NULLIFY (qs_env%transport_env)
     851        7334 :       NULLIFY (qs_env%image_matrix)
     852        7334 :       NULLIFY (qs_env%ipiv)
     853        7334 :       NULLIFY (qs_env%image_coeff)
     854        7334 :       NULLIFY (qs_env%super_cell)
     855        7334 :       NULLIFY (qs_env%mos)
     856        7334 :       NULLIFY (qs_env%mos_last_converged)
     857        7334 :       NULLIFY (qs_env%mpools)
     858        7334 :       NULLIFY (qs_env%ewald_env)
     859        7334 :       NULLIFY (qs_env%ewald_pw)
     860        7334 :       NULLIFY (qs_env%scf_control)
     861        7334 :       NULLIFY (qs_env%rel_control)
     862        7334 :       NULLIFY (qs_env%qs_charges)
     863             :       ! ZMP initializing arrays
     864        7334 :       NULLIFY (qs_env%rho_external)
     865        7334 :       NULLIFY (qs_env%external_vxc)
     866        7334 :       NULLIFY (qs_env%mask)
     867             :       ! Embedding potential
     868        7334 :       NULLIFY (qs_env%embed_pot)
     869        7334 :       NULLIFY (qs_env%spin_embed_pot)
     870             : 
     871             :       ! Polarisability tensor
     872        7334 :       NULLIFY (qs_env%polar_env)
     873             : 
     874        7334 :       NULLIFY (qs_env%ks_env)
     875        7334 :       NULLIFY (qs_env%ks_qmmm_env)
     876        7334 :       NULLIFY (qs_env%wf_history)
     877        7334 :       NULLIFY (qs_env%scf_env)
     878        7334 :       NULLIFY (qs_env%oce)
     879        7334 :       NULLIFY (qs_env%local_rho_set)
     880        7334 :       NULLIFY (qs_env%hartree_local)
     881        7334 :       NULLIFY (qs_env%input)
     882        7334 :       NULLIFY (qs_env%linres_control)
     883        7334 :       NULLIFY (qs_env%xas_env)
     884        7334 :       NULLIFY (qs_env%cp_ddapc_env)
     885        7334 :       NULLIFY (qs_env%cp_ddapc_ewald)
     886        7334 :       NULLIFY (qs_env%outer_scf_history)
     887        7334 :       NULLIFY (qs_env%gradient_history)
     888        7334 :       NULLIFY (qs_env%variable_history)
     889        7334 :       NULLIFY (qs_env%x_data)
     890        7334 :       NULLIFY (qs_env%et_coupling)
     891        7334 :       NULLIFY (qs_env%dftb_potential)
     892        7334 :       NULLIFY (qs_env%active_space)
     893             : 
     894        7334 :       NULLIFY (qs_env%se_taper)
     895        7334 :       NULLIFY (qs_env%se_store_int_env)
     896        7334 :       NULLIFY (qs_env%se_nddo_mpole)
     897        7334 :       NULLIFY (qs_env%se_nonbond_env)
     898        7334 :       NULLIFY (qs_env%admm_env)
     899        7334 :       NULLIFY (qs_env%efield)
     900        7334 :       NULLIFY (qs_env%lri_env)
     901        7334 :       NULLIFY (qs_env%harris_env)
     902        7334 :       NULLIFY (qs_env%ec_env)
     903        7334 :       NULLIFY (qs_env%exstate_env)
     904        7334 :       NULLIFY (qs_env%lri_density)
     905        7334 :       NULLIFY (qs_env%gcp_env)
     906        7334 :       NULLIFY (qs_env%rtp)
     907        7334 :       NULLIFY (qs_env%mp2_env)
     908        7334 :       NULLIFY (qs_env%bs_env)
     909        7334 :       NULLIFY (qs_env%kg_env)
     910        7334 :       NULLIFY (qs_env%WannierCentres)
     911             : 
     912        7334 :       qs_env%outer_scf_ihistory = 0
     913        7334 :       qs_env%broyden_adaptive_sigma = -1.0_dp
     914             : 
     915        7334 :       CALL local_rho_set_create(qs_env%local_rho_set)
     916        7334 :       CALL hartree_local_create(qs_env%hartree_local)
     917        7334 :       qs_env%run_rtp = .FALSE.
     918        7334 :       qs_env%linres_run = .FALSE.
     919        7334 :       qs_env%single_point_run = .FALSE.
     920        7334 :       qs_env%qmmm = .FALSE.
     921        7334 :       qs_env%qmmm_periodic = .FALSE.
     922        7334 :       qs_env%requires_mo_derivs = .FALSE.
     923        7334 :       qs_env%requires_matrix_vxc = .FALSE.
     924        7334 :       qs_env%has_unit_metric = .FALSE.
     925        7334 :       qs_env%calc_image_preconditioner = .TRUE.
     926        7334 :       qs_env%do_transport = .FALSE.
     927        7334 :       qs_env%given_embed_pot = .FALSE.
     928        7334 :       IF (PRESENT(globenv)) THEN
     929        7328 :          qs_env%target_time = globenv%cp2k_target_time
     930        7328 :          qs_env%start_time = globenv%cp2k_start_time
     931             :          qs_env%single_point_run = (globenv%run_type_id == energy_run .OR. &
     932        7328 :                                     globenv%run_type_id == energy_force_run)
     933             :       ELSE
     934           6 :          qs_env%target_time = 0.0_dp
     935           6 :          qs_env%start_time = 0.0_dp
     936             :       END IF
     937             : 
     938        7334 :       qs_env%sim_time = 0._dp
     939        7334 :       qs_env%sim_step = 0
     940             : 
     941        7334 :       qs_env%total_zeff_corr = 0.0_dp
     942        7334 :       qs_env%surface_dipole_moment = 0.0_dp
     943        7334 :       qs_env%surface_dipole_switch_off = .FALSE.
     944             : 
     945             :       ! Zero all variables containing results
     946        7334 :       NULLIFY (qs_env%mo_derivs)
     947        7334 :       NULLIFY (qs_env%mo_loc_history)
     948             : 
     949        7334 :       IF (.NOT. ASSOCIATED(qs_env%molecular_scf_guess_env)) ALLOCATE (qs_env%molecular_scf_guess_env)
     950             : 
     951        7334 :    END SUBROUTINE init_qs_env
     952             : 
     953             : ! **************************************************************************************************
     954             : !> \brief   Set the QUICKSTEP environment.
     955             : !> \param qs_env ...
     956             : !> \param super_cell ...
     957             : !> \param mos ...
     958             : !> \param qmmm ...
     959             : !> \param qmmm_periodic ...
     960             : !> \param ewald_env ...
     961             : !> \param ewald_pw ...
     962             : !> \param mpools ...
     963             : !> \param rho_external ...
     964             : !> \param external_vxc ...
     965             : !> \param mask ...
     966             : !> \param scf_control ...
     967             : !> \param rel_control ...
     968             : !> \param qs_charges ...
     969             : !> \param ks_env ...
     970             : !> \param ks_qmmm_env ...
     971             : !> \param wf_history ...
     972             : !> \param scf_env ...
     973             : !> \param active_space ...
     974             : !> \param input ...
     975             : !> \param oce ...
     976             : !> \param rho_atom_set ...
     977             : !> \param rho0_atom_set ...
     978             : !> \param rho0_mpole ...
     979             : !> \param run_rtp ...
     980             : !> \param rtp ...
     981             : !> \param rhoz_set ...
     982             : !> \param rhoz_tot ...
     983             : !> \param ecoul_1c ...
     984             : !> \param has_unit_metric ...
     985             : !> \param requires_mo_derivs ...
     986             : !> \param mo_derivs ...
     987             : !> \param mo_loc_history ...
     988             : !> \param efield ...
     989             : !> \param linres_control ...
     990             : !> \param xas_env ...
     991             : !> \param cp_ddapc_env ...
     992             : !> \param cp_ddapc_ewald ...
     993             : !> \param outer_scf_history ...
     994             : !> \param outer_scf_ihistory ...
     995             : !> \param x_data ...
     996             : !> \param et_coupling ...
     997             : !> \param dftb_potential ...
     998             : !> \param se_taper ...
     999             : !> \param se_store_int_env ...
    1000             : !> \param se_nddo_mpole ...
    1001             : !> \param se_nonbond_env ...
    1002             : !> \param admm_env ...
    1003             : !> \param ls_scf_env ...
    1004             : !> \param do_transport ...
    1005             : !> \param transport_env ...
    1006             : !> \param lri_env ...
    1007             : !> \param lri_density ...
    1008             : !> \param exstate_env ...
    1009             : !> \param ec_env ...
    1010             : !> \param dispersion_env ...
    1011             : !> \param harris_env ...
    1012             : !> \param gcp_env ...
    1013             : !> \param mp2_env ...
    1014             : !> \param bs_env ...
    1015             : !> \param kg_env ...
    1016             : !> \param force ...
    1017             : !> \param kpoints ...
    1018             : !> \param WannierCentres ...
    1019             : !> \param almo_scf_env ...
    1020             : !> \param gradient_history ...
    1021             : !> \param variable_history ...
    1022             : !> \param embed_pot ...
    1023             : !> \param spin_embed_pot ...
    1024             : !> \param polar_env ...
    1025             : !> \param mos_last_converged ... [SGh]
    1026             : !> \param rhs ...
    1027             : !> \date    23.01.2002
    1028             : !> \author  MK
    1029             : !> \version 1.0
    1030             : ! **************************************************************************************************
    1031      150298 :    SUBROUTINE set_qs_env(qs_env, super_cell, &
    1032             :                          mos, qmmm, qmmm_periodic, &
    1033             :                          ewald_env, ewald_pw, mpools, &
    1034             :                          rho_external, external_vxc, mask, &
    1035             :                          scf_control, rel_control, qs_charges, ks_env, &
    1036             :                          ks_qmmm_env, wf_history, scf_env, active_space, &
    1037             :                          input, oce, rho_atom_set, rho0_atom_set, rho0_mpole, run_rtp, rtp, &
    1038             :                          rhoz_set, rhoz_tot, ecoul_1c, has_unit_metric, requires_mo_derivs, mo_derivs, &
    1039             :                          mo_loc_history, efield, &
    1040             :                          linres_control, xas_env, cp_ddapc_env, cp_ddapc_ewald, &
    1041             :                          outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, &
    1042             :                          se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, ls_scf_env, &
    1043             :                          do_transport, transport_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, &
    1044             :                          harris_env, gcp_env, mp2_env, bs_env, kg_env, force, &
    1045             :                          kpoints, WannierCentres, almo_scf_env, gradient_history, variable_history, embed_pot, &
    1046             :                          spin_embed_pot, polar_env, mos_last_converged, rhs)
    1047             : 
    1048             :       TYPE(qs_environment_type), INTENT(INOUT)           :: qs_env
    1049             :       TYPE(cell_type), OPTIONAL, POINTER                 :: super_cell
    1050             :       TYPE(mo_set_type), DIMENSION(:), OPTIONAL, POINTER :: mos
    1051             :       LOGICAL, OPTIONAL                                  :: qmmm, qmmm_periodic
    1052             :       TYPE(ewald_environment_type), OPTIONAL, POINTER    :: ewald_env
    1053             :       TYPE(ewald_pw_type), OPTIONAL, POINTER             :: ewald_pw
    1054             :       TYPE(qs_matrix_pools_type), OPTIONAL, POINTER      :: mpools
    1055             :       TYPE(qs_rho_type), OPTIONAL, POINTER               :: rho_external
    1056             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: external_vxc, mask
    1057             :       TYPE(scf_control_type), OPTIONAL, POINTER          :: scf_control
    1058             :       TYPE(rel_control_type), OPTIONAL, POINTER          :: rel_control
    1059             :       TYPE(qs_charges_type), OPTIONAL, POINTER           :: qs_charges
    1060             :       TYPE(qs_ks_env_type), OPTIONAL, POINTER            :: ks_env
    1061             :       TYPE(qs_ks_qmmm_env_type), OPTIONAL, POINTER       :: ks_qmmm_env
    1062             :       TYPE(qs_wf_history_type), OPTIONAL, POINTER        :: wf_history
    1063             :       TYPE(qs_scf_env_type), OPTIONAL, POINTER           :: scf_env
    1064             :       TYPE(active_space_type), OPTIONAL, POINTER         :: active_space
    1065             :       TYPE(section_vals_type), OPTIONAL, POINTER         :: input
    1066             :       TYPE(oce_matrix_type), OPTIONAL, POINTER           :: oce
    1067             :       TYPE(rho_atom_type), DIMENSION(:), OPTIONAL, &
    1068             :          POINTER                                         :: rho_atom_set
    1069             :       TYPE(rho0_atom_type), DIMENSION(:), OPTIONAL, &
    1070             :          POINTER                                         :: rho0_atom_set
    1071             :       TYPE(rho0_mpole_type), OPTIONAL, POINTER           :: rho0_mpole
    1072             :       LOGICAL, OPTIONAL                                  :: run_rtp
    1073             :       TYPE(rt_prop_type), OPTIONAL, POINTER              :: rtp
    1074             :       TYPE(rhoz_type), DIMENSION(:), OPTIONAL, POINTER   :: rhoz_set
    1075             :       REAL(dp), OPTIONAL                                 :: rhoz_tot
    1076             :       TYPE(ecoul_1center_type), DIMENSION(:), OPTIONAL, &
    1077             :          POINTER                                         :: ecoul_1c
    1078             :       LOGICAL, OPTIONAL                                  :: has_unit_metric, requires_mo_derivs
    1079             :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
    1080             :          POINTER                                         :: mo_derivs
    1081             :       TYPE(cp_fm_type), DIMENSION(:), OPTIONAL, POINTER  :: mo_loc_history
    1082             :       TYPE(efield_berry_type), OPTIONAL, POINTER         :: efield
    1083             :       TYPE(linres_control_type), OPTIONAL, POINTER       :: linres_control
    1084             :       TYPE(xas_environment_type), OPTIONAL, POINTER      :: xas_env
    1085             :       TYPE(cp_ddapc_type), OPTIONAL, POINTER             :: cp_ddapc_env
    1086             :       TYPE(cp_ddapc_ewald_type), OPTIONAL, POINTER       :: cp_ddapc_ewald
    1087             :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: outer_scf_history
    1088             :       INTEGER, INTENT(IN), OPTIONAL                      :: outer_scf_ihistory
    1089             :       TYPE(hfx_type), DIMENSION(:, :), OPTIONAL, POINTER :: x_data
    1090             :       TYPE(et_coupling_type), OPTIONAL, POINTER          :: et_coupling
    1091             :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), &
    1092             :          OPTIONAL, POINTER                               :: dftb_potential
    1093             :       TYPE(se_taper_type), OPTIONAL, POINTER             :: se_taper
    1094             :       TYPE(semi_empirical_si_type), OPTIONAL, POINTER    :: se_store_int_env
    1095             :       TYPE(nddo_mpole_type), OPTIONAL, POINTER           :: se_nddo_mpole
    1096             :       TYPE(fist_nonbond_env_type), OPTIONAL, POINTER     :: se_nonbond_env
    1097             :       TYPE(admm_type), OPTIONAL, POINTER                 :: admm_env
    1098             :       TYPE(ls_scf_env_type), OPTIONAL, POINTER           :: ls_scf_env
    1099             :       LOGICAL, OPTIONAL                                  :: do_transport
    1100             :       TYPE(transport_env_type), OPTIONAL, POINTER        :: transport_env
    1101             :       TYPE(lri_environment_type), OPTIONAL, POINTER      :: lri_env
    1102             :       TYPE(lri_density_type), OPTIONAL, POINTER          :: lri_density
    1103             :       TYPE(excited_energy_type), OPTIONAL, POINTER       :: exstate_env
    1104             :       TYPE(energy_correction_type), OPTIONAL, POINTER    :: ec_env
    1105             :       TYPE(qs_dispersion_type), OPTIONAL, POINTER        :: dispersion_env
    1106             :       TYPE(harris_type), OPTIONAL, POINTER               :: harris_env
    1107             :       TYPE(qs_gcp_type), OPTIONAL, POINTER               :: gcp_env
    1108             :       TYPE(mp2_type), OPTIONAL, POINTER                  :: mp2_env
    1109             :       TYPE(post_scf_bandstructure_type), OPTIONAL, &
    1110             :          POINTER                                         :: bs_env
    1111             :       TYPE(kg_environment_type), OPTIONAL, POINTER       :: kg_env
    1112             :       TYPE(qs_force_type), DIMENSION(:), OPTIONAL, &
    1113             :          POINTER                                         :: force
    1114             :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoints
    1115             :       TYPE(wannier_centres_type), DIMENSION(:), &
    1116             :          OPTIONAL, POINTER                               :: WannierCentres
    1117             :       TYPE(almo_scf_env_type), OPTIONAL, POINTER         :: almo_scf_env
    1118             :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: gradient_history, variable_history
    1119             :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: embed_pot, spin_embed_pot
    1120             :       TYPE(polar_env_type), OPTIONAL, POINTER            :: polar_env
    1121             :       TYPE(mo_set_type), DIMENSION(:), OPTIONAL, POINTER :: mos_last_converged
    1122             :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: rhs
    1123             : 
    1124             :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1125             : 
    1126      150298 :       IF (PRESENT(mp2_env)) qs_env%mp2_env => mp2_env
    1127      150298 :       IF (PRESENT(bs_env)) qs_env%bs_env => bs_env
    1128      150298 :       IF (PRESENT(kg_env)) qs_env%kg_env => kg_env
    1129      150298 :       IF (PRESENT(super_cell)) THEN
    1130          46 :          CALL cell_retain(super_cell)
    1131          46 :          CALL cell_release(qs_env%super_cell)
    1132          46 :          qs_env%super_cell => super_cell
    1133             :       END IF
    1134             :       !
    1135      150298 :       IF (PRESENT(qmmm)) qs_env%qmmm = qmmm
    1136      150298 :       IF (PRESENT(qmmm_periodic)) qs_env%qmmm_periodic = qmmm_periodic
    1137      150298 :       IF (PRESENT(mos)) qs_env%mos => mos
    1138      150298 :       IF (PRESENT(mos_last_converged)) qs_env%mos_last_converged => mos_last_converged
    1139      150298 :       IF (PRESENT(ls_scf_env)) qs_env%ls_scf_env => ls_scf_env
    1140      150298 :       IF (PRESENT(almo_scf_env)) qs_env%almo_scf_env => almo_scf_env
    1141      150298 :       IF (PRESENT(do_transport)) qs_env%do_transport = do_transport
    1142      150298 :       IF (PRESENT(transport_env)) qs_env%transport_env => transport_env
    1143             :       ! if intels checking (-C) complains here, you have rediscovered a bug in the intel
    1144             :       ! compiler (present in at least 10.0.025). A testcase has been submitted to intel.
    1145      150298 :       IF (PRESENT(oce)) qs_env%oce => oce
    1146      150298 :       IF (PRESENT(outer_scf_history)) qs_env%outer_scf_history => outer_scf_history
    1147      150298 :       IF (PRESENT(gradient_history)) qs_env%gradient_history => gradient_history
    1148      150298 :       IF (PRESENT(variable_history)) qs_env%variable_history => variable_history
    1149      150298 :       IF (PRESENT(outer_scf_ihistory)) qs_env%outer_scf_ihistory = outer_scf_ihistory
    1150      150298 :       IF (PRESENT(requires_mo_derivs)) qs_env%requires_mo_derivs = requires_mo_derivs
    1151      150298 :       IF (PRESENT(has_unit_metric)) qs_env%has_unit_metric = has_unit_metric
    1152      150298 :       IF (PRESENT(mo_derivs)) qs_env%mo_derivs => mo_derivs
    1153      150298 :       IF (PRESENT(mo_loc_history)) qs_env%mo_loc_history => mo_loc_history
    1154      150298 :       IF (PRESENT(run_rtp)) qs_env%run_rtp = run_rtp
    1155      150298 :       IF (PRESENT(rtp)) qs_env%rtp => rtp
    1156      150298 :       IF (PRESENT(efield)) qs_env%efield => efield
    1157      150298 :       IF (PRESENT(active_space)) qs_env%active_space => active_space
    1158             : 
    1159      150298 :       IF (PRESENT(ewald_env)) THEN ! accept also null pointers?
    1160         302 :          IF (ASSOCIATED(qs_env%ewald_env)) THEN
    1161           0 :          IF (.NOT. ASSOCIATED(qs_env%ewald_env, ewald_env)) THEN
    1162           0 :             CALL ewald_env_release(qs_env%ewald_env)
    1163           0 :             DEALLOCATE (qs_env%ewald_env)
    1164             :          END IF
    1165             :          END IF
    1166         302 :          qs_env%ewald_env => ewald_env
    1167             :       END IF
    1168      150298 :       IF (PRESENT(ewald_pw)) THEN ! accept also null pointers?
    1169         302 :          IF (ASSOCIATED(qs_env%ewald_pw)) THEN
    1170           0 :             IF (.NOT. ASSOCIATED(ewald_pw, qs_env%ewald_pw)) THEN
    1171           0 :                CALL ewald_pw_release(qs_env%ewald_pw)
    1172           0 :                DEALLOCATE (qs_env%ewald_pw)
    1173             :             END IF
    1174             :          END IF
    1175         302 :          qs_env%ewald_pw => ewald_pw
    1176             :       END IF
    1177      150298 :       IF (PRESENT(scf_control)) THEN ! accept also null pointers?
    1178        7334 :          IF (ASSOCIATED(qs_env%scf_control)) THEN
    1179           0 :             IF (.NOT. ASSOCIATED(qs_env%scf_control, scf_control)) THEN
    1180           0 :                CALL scf_c_release(qs_env%scf_control)
    1181           0 :                DEALLOCATE (qs_env%scf_control)
    1182             :             END IF
    1183             :          END IF
    1184        7334 :          qs_env%scf_control => scf_control
    1185             :       END IF
    1186      150298 :       IF (PRESENT(rel_control)) THEN ! accept also null pointers?
    1187        7334 :          IF (ASSOCIATED(qs_env%rel_control)) THEN
    1188           0 :             IF (.NOT. ASSOCIATED(qs_env%rel_control, rel_control)) THEN
    1189           0 :                CALL rel_c_release(qs_env%rel_control)
    1190           0 :                DEALLOCATE (qs_env%rel_control)
    1191             :             END IF
    1192             :          END IF
    1193        7334 :          qs_env%rel_control => rel_control
    1194             :       END IF
    1195      150298 :       IF (PRESENT(linres_control)) THEN ! accept also null pointers?
    1196        1634 :          IF (ASSOCIATED(qs_env%linres_control)) THEN
    1197         614 :          IF (.NOT. ASSOCIATED(qs_env%linres_control, linres_control)) THEN
    1198         614 :             CALL linres_control_release(qs_env%linres_control)
    1199         614 :             DEALLOCATE (qs_env%linres_control)
    1200             :          END IF
    1201             :          END IF
    1202        1634 :          qs_env%linres_control => linres_control
    1203             :       END IF
    1204             :       ! ZMP associating variables
    1205      150298 :       IF (PRESENT(rho_external)) THEN
    1206           0 :          IF (ASSOCIATED(qs_env%rho_external)) THEN
    1207           0 :          IF (.NOT. ASSOCIATED(qs_env%rho_external, rho_external)) THEN
    1208           0 :             CALL qs_rho_release(qs_env%rho_external)
    1209           0 :             DEALLOCATE (qs_env%rho_external)
    1210             :          END IF
    1211             :          END IF
    1212           0 :          qs_env%rho_external => rho_external
    1213             :       END IF
    1214      150298 :       IF (PRESENT(external_vxc)) qs_env%external_vxc => external_vxc
    1215      150298 :       IF (PRESENT(mask)) qs_env%mask => mask
    1216             :       ! Embedding potential
    1217      150298 :       IF (PRESENT(embed_pot)) qs_env%embed_pot => embed_pot
    1218      150298 :       IF (PRESENT(spin_embed_pot)) qs_env%spin_embed_pot => spin_embed_pot
    1219             : 
    1220             :       ! Polarisability tensor
    1221      150298 :       IF (PRESENT(polar_env)) qs_env%polar_env => polar_env
    1222             : 
    1223      150298 :       IF (PRESENT(qs_charges)) THEN
    1224        7334 :          IF (ASSOCIATED(qs_env%qs_charges)) THEN
    1225           0 :             IF (.NOT. ASSOCIATED(qs_env%qs_charges, qs_charges)) THEN
    1226           0 :                CALL qs_charges_release(qs_env%qs_charges)
    1227           0 :                DEALLOCATE (qs_env%qs_charges)
    1228             :             END IF
    1229             :          END IF
    1230        7334 :          qs_env%qs_charges => qs_charges
    1231             :       END IF
    1232      150298 :       IF (PRESENT(ks_qmmm_env)) THEN
    1233         378 :          IF (ASSOCIATED(qs_env%ks_qmmm_env)) THEN
    1234           0 :             IF (.NOT. ASSOCIATED(qs_env%ks_qmmm_env, ks_qmmm_env)) THEN
    1235           0 :                CALL qs_ks_qmmm_release(qs_env%ks_qmmm_env)
    1236           0 :                DEALLOCATE (qs_env%ks_qmmm_env)
    1237             :             END IF
    1238             :          END IF
    1239         378 :          qs_env%ks_qmmm_env => ks_qmmm_env
    1240             :       END IF
    1241      150298 :       IF (PRESENT(ks_env)) THEN ! accept also null pointers?
    1242        7334 :          IF (ASSOCIATED(qs_env%ks_env)) THEN
    1243           0 :             IF (.NOT. ASSOCIATED(qs_env%ks_env, ks_env)) THEN
    1244           0 :                CALL qs_ks_release(qs_env%ks_env)
    1245           0 :                DEALLOCATE (qs_env%ks_env)
    1246             :             END IF
    1247             :          END IF
    1248        7334 :          qs_env%ks_env => ks_env
    1249             :       END IF
    1250      150298 :       IF (PRESENT(wf_history)) THEN ! accept also null pointers ?
    1251        7706 :          CALL wfi_retain(wf_history)
    1252        7706 :          CALL wfi_release(qs_env%wf_history)
    1253        7706 :          qs_env%wf_history => wf_history
    1254             :       END IF
    1255      150298 :       IF (PRESENT(scf_env)) THEN ! accept also null pointers ?
    1256        6289 :          IF (ASSOCIATED(qs_env%scf_env)) THEN
    1257          50 :          IF (.NOT. ASSOCIATED(qs_env%scf_env, scf_env)) THEN
    1258          50 :             CALL scf_env_release(qs_env%scf_env)
    1259          50 :             DEALLOCATE (qs_env%scf_env)
    1260             :          END IF
    1261             :          END IF
    1262        6289 :          qs_env%scf_env => scf_env
    1263             :       END IF
    1264      150298 :       IF (PRESENT(xas_env)) THEN ! accept also null pointers?
    1265          42 :          IF (ASSOCIATED(qs_env%xas_env)) THEN
    1266           0 :             IF (.NOT. ASSOCIATED(qs_env%xas_env, xas_env)) THEN
    1267           0 :                CALL xas_env_release(qs_env%xas_env)
    1268           0 :                DEALLOCATE (qs_env%xas_env)
    1269             :             END IF
    1270             :          END IF
    1271          42 :          qs_env%xas_env => xas_env
    1272             :       END IF
    1273      150298 :       IF (PRESENT(mpools)) THEN
    1274           0 :          CALL mpools_retain(mpools)
    1275           0 :          CALL mpools_release(qs_env%mpools)
    1276           0 :          qs_env%mpools => mpools
    1277             :       END IF
    1278      150298 :       IF (PRESENT(rho_atom_set)) THEN
    1279         922 :          CALL set_local_rho(qs_env%local_rho_set, rho_atom_set=rho_atom_set)
    1280             :       END IF
    1281      150298 :       IF (PRESENT(rho0_atom_set)) THEN
    1282           0 :          CALL set_local_rho(qs_env%local_rho_set, rho0_atom_set=rho0_atom_set)
    1283             :       END IF
    1284      150298 :       IF (PRESENT(rho0_mpole)) THEN
    1285           0 :          CALL set_local_rho(qs_env%local_rho_set, rho0_mpole=rho0_mpole)
    1286             :       END IF
    1287      150298 :       IF (PRESENT(rhoz_set)) THEN
    1288           0 :          CALL set_local_rho(qs_env%local_rho_set, rhoz_set=rhoz_set)
    1289             :       END IF
    1290      150298 :       IF (PRESENT(rhoz_tot)) qs_env%local_rho_set%rhoz_tot = rhoz_tot
    1291      150298 :       IF (PRESENT(ecoul_1c)) THEN
    1292           0 :          CALL set_hartree_local(qs_env%hartree_local, ecoul_1c=ecoul_1c)
    1293             :       END IF
    1294      150298 :       IF (PRESENT(input)) THEN
    1295        7334 :          CALL section_vals_retain(input)
    1296        7334 :          CALL section_vals_release(qs_env%input)
    1297        7334 :          qs_env%input => input
    1298             :       END IF
    1299      150298 :       IF (PRESENT(cp_ddapc_env)) THEN
    1300           0 :          IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
    1301           0 :             IF (.NOT. ASSOCIATED(qs_env%cp_ddapc_env, cp_ddapc_env)) THEN
    1302           0 :                CALL cp_ddapc_release(qs_env%cp_ddapc_env)
    1303           0 :                DEALLOCATE (qs_env%cp_ddapc_env)
    1304             :             END IF
    1305             :          END IF
    1306           0 :          qs_env%cp_ddapc_env => cp_ddapc_env
    1307             :       END IF
    1308      150298 :       IF (PRESENT(cp_ddapc_ewald)) THEN
    1309           0 :          qs_env%cp_ddapc_ewald => cp_ddapc_ewald
    1310             :       END IF
    1311      150298 :       IF (PRESENT(x_data)) qs_env%x_data => x_data
    1312      150298 :       IF (PRESENT(et_coupling)) qs_env%et_coupling => et_coupling
    1313      150298 :       IF (PRESENT(dftb_potential)) qs_env%dftb_potential => dftb_potential
    1314      150298 :       IF (PRESENT(se_taper)) THEN
    1315         998 :          CALL se_taper_release(qs_env%se_taper)
    1316         998 :          qs_env%se_taper => se_taper
    1317             :       END IF
    1318      150298 :       IF (PRESENT(se_store_int_env)) THEN
    1319         998 :          CALL semi_empirical_si_release(qs_env%se_store_int_env)
    1320         998 :          qs_env%se_store_int_env => se_store_int_env
    1321             :       END IF
    1322      150298 :       IF (PRESENT(se_nddo_mpole)) THEN
    1323          32 :          CALL nddo_mpole_release(qs_env%se_nddo_mpole)
    1324          32 :          qs_env%se_nddo_mpole => se_nddo_mpole
    1325             :       END IF
    1326      150298 :       IF (PRESENT(se_nonbond_env)) THEN
    1327          32 :          IF (ASSOCIATED(qs_env%se_nonbond_env)) THEN
    1328           0 :          IF (.NOT. ASSOCIATED(qs_env%se_nonbond_env, se_nonbond_env)) THEN
    1329           0 :             CALL fist_nonbond_env_release(qs_env%se_nonbond_env)
    1330           0 :             DEALLOCATE (qs_env%se_nonbond_env)
    1331             :          END IF
    1332             :          END IF
    1333          32 :          qs_env%se_nonbond_env => se_nonbond_env
    1334             :       END IF
    1335      150298 :       IF (PRESENT(admm_env)) qs_env%admm_env => admm_env
    1336      150298 :       IF (PRESENT(lri_env)) qs_env%lri_env => lri_env
    1337      150298 :       IF (PRESENT(lri_density)) qs_env%lri_density => lri_density
    1338      150298 :       IF (PRESENT(harris_env)) qs_env%harris_env => harris_env
    1339      150298 :       IF (PRESENT(ec_env)) qs_env%ec_env => ec_env
    1340      150298 :       IF (PRESENT(exstate_env)) qs_env%exstate_env => exstate_env
    1341      150298 :       IF (PRESENT(dispersion_env)) qs_env%dispersion_env => dispersion_env
    1342      150298 :       IF (PRESENT(gcp_env)) qs_env%gcp_env => gcp_env
    1343      150298 :       IF (PRESENT(WannierCentres)) qs_env%WannierCentres => WannierCentres
    1344      150298 :       IF (PRESENT(kpoints)) CALL set_ks_env(qs_env%ks_env, kpoints=kpoints)
    1345             : 
    1346             :       ! Resp charges
    1347      150298 :       IF (PRESENT(rhs)) qs_env%rhs => rhs
    1348             : 
    1349      150298 :       IF (PRESENT(force)) THEN
    1350        2248 :          CALL get_qs_env(qs_env, subsys=subsys)
    1351        2248 :          CALL qs_subsys_set(subsys, force=force)
    1352             :       END IF
    1353             : 
    1354      150298 :    END SUBROUTINE set_qs_env
    1355             : 
    1356             : ! **************************************************************************************************
    1357             : !> \brief allocates and intitializes a qs_env
    1358             : !> \param qs_env the object to create
    1359             : !> \param globenv ...
    1360             : !> \par History
    1361             : !>      12.2002 created [fawzi]
    1362             : !> \author Fawzi Mohamed
    1363             : ! **************************************************************************************************
    1364        7334 :    SUBROUTINE qs_env_create(qs_env, globenv)
    1365             :       TYPE(qs_environment_type), INTENT(OUT)             :: qs_env
    1366             :       TYPE(global_environment_type), OPTIONAL, POINTER   :: globenv
    1367             : 
    1368        7334 :       CALL init_qs_env(qs_env, globenv=globenv)
    1369        7334 :    END SUBROUTINE qs_env_create
    1370             : 
    1371             : ! **************************************************************************************************
    1372             : !> \brief releases the given qs_env (see doc/ReferenceCounting.html)
    1373             : !> \param qs_env the object to release
    1374             : !> \par History
    1375             : !>      12.2002 created [fawzi]
    1376             : !>      06.2018 polar_env added (MK)
    1377             : !> \author Fawzi Mohamed
    1378             : ! **************************************************************************************************
    1379        7334 :    SUBROUTINE qs_env_release(qs_env)
    1380             :       TYPE(qs_environment_type), INTENT(INOUT)           :: qs_env
    1381             : 
    1382             :       INTEGER                                            :: i
    1383             : 
    1384        7334 :       CALL cell_release(qs_env%super_cell)
    1385        7334 :       IF (ASSOCIATED(qs_env%mos)) THEN
    1386       15579 :          DO i = 1, SIZE(qs_env%mos)
    1387       15579 :             CALL deallocate_mo_set(qs_env%mos(i))
    1388             :          END DO
    1389        6990 :          DEALLOCATE (qs_env%mos)
    1390             :       END IF
    1391        7334 :       IF (ASSOCIATED(qs_env%mos_last_converged)) THEN
    1392           4 :          DO i = 1, SIZE(qs_env%mos_last_converged)
    1393           4 :             CALL deallocate_mo_set(qs_env%mos_last_converged(i))
    1394             :          END DO
    1395           2 :          DEALLOCATE (qs_env%mos_last_converged)
    1396             :       END IF
    1397             : 
    1398        7334 :       IF (ASSOCIATED(qs_env%mo_derivs)) THEN
    1399        4491 :          DO I = 1, SIZE(qs_env%mo_derivs)
    1400        4491 :             CALL dbcsr_release_p(qs_env%mo_derivs(I)%matrix)
    1401             :          END DO
    1402        1951 :          DEALLOCATE (qs_env%mo_derivs)
    1403             :       END IF
    1404             : 
    1405        7334 :       CALL cp_fm_release(qs_env%mo_loc_history)
    1406             : 
    1407        7334 :       IF (ASSOCIATED(qs_env%rtp)) THEN
    1408         198 :          CALL rt_prop_release(qs_env%rtp)
    1409         198 :          DEALLOCATE (qs_env%rtp)
    1410             :       END IF
    1411        7334 :       IF (ASSOCIATED(qs_env%outer_scf_history)) THEN
    1412        1291 :          DEALLOCATE (qs_env%outer_scf_history)
    1413        1291 :          qs_env%outer_scf_ihistory = 0
    1414             :       END IF
    1415        7334 :       IF (ASSOCIATED(qs_env%gradient_history)) &
    1416        1291 :          DEALLOCATE (qs_env%gradient_history)
    1417        7334 :       IF (ASSOCIATED(qs_env%variable_history)) &
    1418        1291 :          DEALLOCATE (qs_env%variable_history)
    1419        7334 :       IF (ASSOCIATED(qs_env%oce)) CALL deallocate_oce_set(qs_env%oce)
    1420        7334 :       IF (ASSOCIATED(qs_env%local_rho_set)) THEN
    1421        7325 :          CALL local_rho_set_release(qs_env%local_rho_set)
    1422             :       END IF
    1423        7334 :       IF (ASSOCIATED(qs_env%hartree_local)) THEN
    1424        7325 :          CALL hartree_local_release(qs_env%hartree_local)
    1425             :       END IF
    1426        7334 :       IF (ASSOCIATED(qs_env%scf_control)) THEN
    1427        7325 :          CALL scf_c_release(qs_env%scf_control)
    1428        7325 :          DEALLOCATE (qs_env%scf_control)
    1429             :       END IF
    1430        7334 :       IF (ASSOCIATED(qs_env%rel_control)) THEN
    1431        7325 :          CALL rel_c_release(qs_env%rel_control)
    1432        7325 :          DEALLOCATE (qs_env%rel_control)
    1433             :       END IF
    1434             : 
    1435        7334 :       IF (ASSOCIATED(qs_env%linres_control)) THEN
    1436        1020 :          CALL linres_control_release(qs_env%linres_control)
    1437        1020 :          DEALLOCATE (qs_env%linres_control)
    1438             :       END IF
    1439             : 
    1440        7334 :       IF (ASSOCIATED(qs_env%almo_scf_env)) THEN
    1441          66 :          CALL almo_scf_env_release(qs_env%almo_scf_env)
    1442             :       END IF
    1443             : 
    1444        7334 :       IF (ASSOCIATED(qs_env%ls_scf_env)) THEN
    1445         342 :          CALL ls_scf_release(qs_env%ls_scf_env)
    1446             :       END IF
    1447        7334 :       IF (ASSOCIATED(qs_env%molecular_scf_guess_env)) THEN
    1448        7325 :          CALL molecular_scf_guess_env_destroy(qs_env%molecular_scf_guess_env)
    1449        7325 :          DEALLOCATE (qs_env%molecular_scf_guess_env)
    1450             :       END IF
    1451             : 
    1452        7334 :       IF (ASSOCIATED(qs_env%transport_env)) THEN
    1453           0 :          CALL transport_env_release(qs_env%transport_env)
    1454             :       END IF
    1455             : 
    1456             :       !Only if do_xas_calculation
    1457        7334 :       IF (ASSOCIATED(qs_env%xas_env)) THEN
    1458           0 :          CALL xas_env_release(qs_env%xas_env)
    1459           0 :          DEALLOCATE (qs_env%xas_env)
    1460             :       END IF
    1461        7334 :       IF (ASSOCIATED(qs_env%ewald_env)) THEN
    1462         302 :          CALL ewald_env_release(qs_env%ewald_env)
    1463         302 :          DEALLOCATE (qs_env%ewald_env)
    1464             :       END IF
    1465        7334 :       IF (ASSOCIATED(qs_env%ewald_pw)) THEN
    1466         302 :          CALL ewald_pw_release(qs_env%ewald_pw)
    1467         302 :          DEALLOCATE (qs_env%ewald_pw)
    1468             :       END IF
    1469        7334 :       IF (ASSOCIATED(qs_env%image_matrix)) THEN
    1470          10 :          DEALLOCATE (qs_env%image_matrix)
    1471             :       END IF
    1472        7334 :       IF (ASSOCIATED(qs_env%ipiv)) THEN
    1473           8 :          DEALLOCATE (qs_env%ipiv)
    1474             :       END IF
    1475        7334 :       IF (ASSOCIATED(qs_env%image_coeff)) THEN
    1476          10 :          DEALLOCATE (qs_env%image_coeff)
    1477             :       END IF
    1478             :       ! ZMP
    1479        7334 :       IF (ASSOCIATED(qs_env%rho_external)) THEN
    1480           0 :          CALL qs_rho_release(qs_env%rho_external)
    1481           0 :          DEALLOCATE (qs_env%rho_external)
    1482             :       END IF
    1483        7334 :       IF (ASSOCIATED(qs_env%external_vxc)) THEN
    1484           0 :          CALL qs_env%external_vxc%release()
    1485           0 :          DEALLOCATE (qs_env%external_vxc)
    1486             :       END IF
    1487        7334 :       IF (ASSOCIATED(qs_env%mask)) THEN
    1488           0 :          CALL qs_env%mask%release()
    1489           0 :          DEALLOCATE (qs_env%mask)
    1490             :       END IF
    1491        7334 :       IF (ASSOCIATED(qs_env%active_space)) THEN
    1492         106 :          CALL release_active_space_type(qs_env%active_space)
    1493             :       END IF
    1494             :       ! Embedding potentials if provided as input
    1495        7334 :       IF (qs_env%given_embed_pot) THEN
    1496           2 :          CALL qs_env%embed_pot%release()
    1497           2 :          DEALLOCATE (qs_env%embed_pot)
    1498           2 :          IF (ASSOCIATED(qs_env%spin_embed_pot)) THEN
    1499           2 :             CALL qs_env%spin_embed_pot%release()
    1500           2 :             DEALLOCATE (qs_env%spin_embed_pot)
    1501             :          END IF
    1502             :       END IF
    1503             : 
    1504             :       ! Polarisability tensor
    1505        7334 :       CALL polar_env_release(qs_env%polar_env)
    1506             : 
    1507        7334 :       IF (ASSOCIATED(qs_env%qs_charges)) THEN
    1508        7325 :          CALL qs_charges_release(qs_env%qs_charges)
    1509        7325 :          DEALLOCATE (qs_env%qs_charges)
    1510             :       END IF
    1511        7334 :       IF (ASSOCIATED(qs_env%ks_env)) THEN
    1512        7334 :          CALL qs_ks_release(qs_env%ks_env)
    1513        7334 :          DEALLOCATE (qs_env%ks_env)
    1514             :       END IF
    1515        7334 :       IF (ASSOCIATED(qs_env%ks_qmmm_env)) THEN
    1516         378 :          CALL qs_ks_qmmm_release(qs_env%ks_qmmm_env)
    1517         378 :          DEALLOCATE (qs_env%ks_qmmm_env)
    1518             :       END IF
    1519        7334 :       CALL wfi_release(qs_env%wf_history)
    1520        7334 :       IF (ASSOCIATED(qs_env%scf_env)) THEN
    1521        6239 :          CALL scf_env_release(qs_env%scf_env)
    1522        6239 :          DEALLOCATE (qs_env%scf_env)
    1523             :       END IF
    1524        7334 :       CALL mpools_release(qs_env%mpools)
    1525        7334 :       CALL section_vals_release(qs_env%input)
    1526        7334 :       IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
    1527         118 :          CALL cp_ddapc_release(qs_env%cp_ddapc_env)
    1528         118 :          DEALLOCATE (qs_env%cp_ddapc_env)
    1529             :       END IF
    1530        7334 :       CALL cp_ddapc_ewald_release(qs_env%cp_ddapc_ewald)
    1531        7334 :       CALL efield_berry_release(qs_env%efield)
    1532        7334 :       IF (ASSOCIATED(qs_env%x_data)) THEN
    1533        1182 :          CALL hfx_release(qs_env%x_data)
    1534             :       END IF
    1535        7334 :       IF (ASSOCIATED(qs_env%et_coupling)) THEN
    1536          10 :          CALL et_coupling_release(qs_env%et_coupling)
    1537             :       END IF
    1538        7334 :       IF (ASSOCIATED(qs_env%dftb_potential)) THEN
    1539         222 :          CALL qs_dftb_pairpot_release(qs_env%dftb_potential)
    1540             :       END IF
    1541        7334 :       IF (ASSOCIATED(qs_env%se_taper)) THEN
    1542         998 :          CALL se_taper_release(qs_env%se_taper)
    1543             :       END IF
    1544        7334 :       IF (ASSOCIATED(qs_env%se_store_int_env)) THEN
    1545         998 :          CALL semi_empirical_si_release(qs_env%se_store_int_env)
    1546             :       END IF
    1547        7334 :       IF (ASSOCIATED(qs_env%se_nddo_mpole)) THEN
    1548          32 :          CALL nddo_mpole_release(qs_env%se_nddo_mpole)
    1549             :       END IF
    1550        7334 :       IF (ASSOCIATED(qs_env%se_nonbond_env)) THEN
    1551          32 :          CALL fist_nonbond_env_release(qs_env%se_nonbond_env)
    1552          32 :          DEALLOCATE (qs_env%se_nonbond_env)
    1553             :       END IF
    1554        7334 :       IF (ASSOCIATED(qs_env%admm_env)) THEN
    1555         442 :          CALL admm_env_release(qs_env%admm_env)
    1556             :       END IF
    1557        7334 :       IF (ASSOCIATED(qs_env%lri_env)) THEN
    1558          46 :          CALL lri_env_release(qs_env%lri_env)
    1559          46 :          DEALLOCATE (qs_env%lri_env)
    1560             :       END IF
    1561        7334 :       IF (ASSOCIATED(qs_env%lri_density)) THEN
    1562          46 :          CALL lri_density_release(qs_env%lri_density)
    1563          46 :          DEALLOCATE (qs_env%lri_density)
    1564             :       END IF
    1565        7334 :       IF (ASSOCIATED(qs_env%harris_env)) THEN
    1566        7325 :          CALL harris_env_release(qs_env%harris_env)
    1567             :       END IF
    1568        7334 :       IF (ASSOCIATED(qs_env%ec_env)) THEN
    1569        7325 :          CALL ec_env_release(qs_env%ec_env)
    1570             :       END IF
    1571        7334 :       IF (ASSOCIATED(qs_env%exstate_env)) THEN
    1572        7325 :          CALL exstate_release(qs_env%exstate_env)
    1573             :       END IF
    1574        7334 :       IF (ASSOCIATED(qs_env%mp2_env)) THEN
    1575         476 :          CALL mp2_env_release(qs_env%mp2_env)
    1576        6188 :          DEALLOCATE (qs_env%mp2_env)
    1577             :          NULLIFY (qs_env%mp2_env)
    1578             :       END IF
    1579        7334 :       IF (ASSOCIATED(qs_env%bs_env)) THEN
    1580          34 :          CALL bs_env_release(qs_env%bs_env)
    1581             :       END IF
    1582        7334 :       IF (ASSOCIATED(qs_env%kg_env)) THEN
    1583          66 :          CALL kg_env_release(qs_env%kg_env)
    1584             :       END IF
    1585             : 
    1586             :       ! dispersion
    1587        7334 :       CALL qs_dispersion_release(qs_env%dispersion_env)
    1588             :       ! gCP
    1589        7334 :       IF (ASSOCIATED(qs_env%gcp_env)) THEN
    1590        5195 :          CALL qs_gcp_release(qs_env%gcp_env)
    1591             :       END IF
    1592             : 
    1593        7334 :       IF (ASSOCIATED(qs_env%WannierCentres)) THEN
    1594          12 :          DO i = 1, SIZE(qs_env%WannierCentres)
    1595           6 :             DEALLOCATE (qs_env%WannierCentres(i)%WannierHamDiag)
    1596          12 :             DEALLOCATE (qs_env%WannierCentres(i)%centres)
    1597             :          END DO
    1598           6 :          DEALLOCATE (qs_env%WannierCentres)
    1599             :       END IF
    1600             :       ! Resp charges
    1601        7334 :       IF (ASSOCIATED(qs_env%rhs)) DEALLOCATE (qs_env%rhs)
    1602             : 
    1603        7334 :    END SUBROUTINE qs_env_release
    1604             : 
    1605             : ! **************************************************************************************************
    1606             : !> \brief releases part of the given qs_env in order to save memory
    1607             : !> \param qs_env the object to release
    1608             : !> \par History
    1609             : !>      04.2022 created [JGH]
    1610             : ! **************************************************************************************************
    1611           9 :    SUBROUTINE qs_env_part_release(qs_env)
    1612             :       TYPE(qs_environment_type), INTENT(INOUT)           :: qs_env
    1613             : 
    1614             :       INTEGER                                            :: i
    1615             : 
    1616           9 :       IF (ASSOCIATED(qs_env%mos_last_converged)) THEN
    1617           0 :          DO i = 1, SIZE(qs_env%mos_last_converged)
    1618           0 :             CALL deallocate_mo_set(qs_env%mos_last_converged(i))
    1619             :          END DO
    1620           0 :          DEALLOCATE (qs_env%mos_last_converged)
    1621             :       END IF
    1622             : 
    1623           9 :       IF (ASSOCIATED(qs_env%mo_derivs)) THEN
    1624           0 :          DO I = 1, SIZE(qs_env%mo_derivs)
    1625           0 :             CALL dbcsr_release_p(qs_env%mo_derivs(I)%matrix)
    1626             :          END DO
    1627           0 :          DEALLOCATE (qs_env%mo_derivs)
    1628             :       END IF
    1629             : 
    1630           9 :       CALL cp_fm_release(qs_env%mo_loc_history)
    1631             : 
    1632           9 :       IF (ASSOCIATED(qs_env%rtp)) THEN
    1633           0 :          CALL rt_prop_release(qs_env%rtp)
    1634           0 :          DEALLOCATE (qs_env%rtp)
    1635             :       END IF
    1636           9 :       IF (ASSOCIATED(qs_env%outer_scf_history)) THEN
    1637           0 :          DEALLOCATE (qs_env%outer_scf_history)
    1638           0 :          qs_env%outer_scf_ihistory = 0
    1639             :       END IF
    1640           9 :       IF (ASSOCIATED(qs_env%gradient_history)) &
    1641           0 :          DEALLOCATE (qs_env%gradient_history)
    1642           9 :       IF (ASSOCIATED(qs_env%variable_history)) &
    1643           0 :          DEALLOCATE (qs_env%variable_history)
    1644           9 :       IF (ASSOCIATED(qs_env%oce)) CALL deallocate_oce_set(qs_env%oce)
    1645           9 :       IF (ASSOCIATED(qs_env%local_rho_set)) THEN
    1646           9 :          CALL local_rho_set_release(qs_env%local_rho_set)
    1647             :       END IF
    1648           9 :       IF (ASSOCIATED(qs_env%hartree_local)) THEN
    1649           9 :          CALL hartree_local_release(qs_env%hartree_local)
    1650             :       END IF
    1651           9 :       IF (ASSOCIATED(qs_env%scf_control)) THEN
    1652           9 :          CALL scf_c_release(qs_env%scf_control)
    1653           9 :          DEALLOCATE (qs_env%scf_control)
    1654             :       END IF
    1655           9 :       IF (ASSOCIATED(qs_env%rel_control)) THEN
    1656           9 :          CALL rel_c_release(qs_env%rel_control)
    1657           9 :          DEALLOCATE (qs_env%rel_control)
    1658             :       END IF
    1659             : 
    1660           9 :       IF (ASSOCIATED(qs_env%linres_control)) THEN
    1661           0 :          CALL linres_control_release(qs_env%linres_control)
    1662           0 :          DEALLOCATE (qs_env%linres_control)
    1663             :       END IF
    1664             : 
    1665           9 :       IF (ASSOCIATED(qs_env%almo_scf_env)) THEN
    1666           0 :          CALL almo_scf_env_release(qs_env%almo_scf_env)
    1667             :       END IF
    1668             : 
    1669           9 :       IF (ASSOCIATED(qs_env%ls_scf_env)) THEN
    1670           0 :          CALL ls_scf_release(qs_env%ls_scf_env)
    1671             :       END IF
    1672           9 :       IF (ASSOCIATED(qs_env%molecular_scf_guess_env)) THEN
    1673           9 :          CALL molecular_scf_guess_env_destroy(qs_env%molecular_scf_guess_env)
    1674           9 :          DEALLOCATE (qs_env%molecular_scf_guess_env)
    1675             :       END IF
    1676             : 
    1677           9 :       IF (ASSOCIATED(qs_env%transport_env)) THEN
    1678           0 :          CALL transport_env_release(qs_env%transport_env)
    1679             :       END IF
    1680             : 
    1681             :       !Only if do_xas_calculation
    1682           9 :       IF (ASSOCIATED(qs_env%xas_env)) THEN
    1683           0 :          CALL xas_env_release(qs_env%xas_env)
    1684           0 :          DEALLOCATE (qs_env%xas_env)
    1685             :       END IF
    1686           9 :       IF (ASSOCIATED(qs_env%ewald_env)) THEN
    1687           0 :          CALL ewald_env_release(qs_env%ewald_env)
    1688           0 :          DEALLOCATE (qs_env%ewald_env)
    1689             :       END IF
    1690           9 :       IF (ASSOCIATED(qs_env%ewald_pw)) THEN
    1691           0 :          CALL ewald_pw_release(qs_env%ewald_pw)
    1692           0 :          DEALLOCATE (qs_env%ewald_pw)
    1693             :       END IF
    1694           9 :       IF (ASSOCIATED(qs_env%image_matrix)) THEN
    1695           0 :          DEALLOCATE (qs_env%image_matrix)
    1696             :       END IF
    1697           9 :       IF (ASSOCIATED(qs_env%ipiv)) THEN
    1698           0 :          DEALLOCATE (qs_env%ipiv)
    1699             :       END IF
    1700           9 :       IF (ASSOCIATED(qs_env%image_coeff)) THEN
    1701           0 :          DEALLOCATE (qs_env%image_coeff)
    1702             :       END IF
    1703             :       ! ZMP
    1704           9 :       IF (ASSOCIATED(qs_env%rho_external)) THEN
    1705           0 :          CALL qs_rho_release(qs_env%rho_external)
    1706           0 :          DEALLOCATE (qs_env%rho_external)
    1707             :       END IF
    1708           9 :       IF (ASSOCIATED(qs_env%external_vxc)) THEN
    1709           0 :          CALL qs_env%external_vxc%release()
    1710           0 :          DEALLOCATE (qs_env%external_vxc)
    1711             :       END IF
    1712           9 :       IF (ASSOCIATED(qs_env%mask)) THEN
    1713           0 :          CALL qs_env%mask%release()
    1714           0 :          DEALLOCATE (qs_env%mask)
    1715             :       END IF
    1716           9 :       IF (ASSOCIATED(qs_env%active_space)) THEN
    1717           0 :          CALL release_active_space_type(qs_env%active_space)
    1718             :       END IF
    1719             :       ! Embedding potentials if provided as input
    1720           9 :       IF (qs_env%given_embed_pot) THEN
    1721           0 :          CALL qs_env%embed_pot%release()
    1722           0 :          DEALLOCATE (qs_env%embed_pot)
    1723           0 :          IF (ASSOCIATED(qs_env%spin_embed_pot)) THEN
    1724           0 :             CALL qs_env%spin_embed_pot%release()
    1725           0 :             DEALLOCATE (qs_env%spin_embed_pot)
    1726             :          END IF
    1727             :       END IF
    1728             : 
    1729             :       ! Polarisability tensor
    1730           9 :       CALL polar_env_release(qs_env%polar_env)
    1731             : 
    1732           9 :       IF (ASSOCIATED(qs_env%qs_charges)) THEN
    1733           9 :          CALL qs_charges_release(qs_env%qs_charges)
    1734           9 :          DEALLOCATE (qs_env%qs_charges)
    1735             :       END IF
    1736           9 :       CALL qs_ks_part_release(qs_env%ks_env)
    1737           9 :       IF (ASSOCIATED(qs_env%ks_qmmm_env)) THEN
    1738           0 :          CALL qs_ks_qmmm_release(qs_env%ks_qmmm_env)
    1739           0 :          DEALLOCATE (qs_env%ks_qmmm_env)
    1740             :       END IF
    1741           9 :       CALL wfi_release(qs_env%wf_history)
    1742           9 :       IF (ASSOCIATED(qs_env%scf_env)) THEN
    1743           0 :          CALL scf_env_release(qs_env%scf_env)
    1744           0 :          DEALLOCATE (qs_env%scf_env)
    1745             :       END IF
    1746           9 :       IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
    1747           0 :          CALL cp_ddapc_release(qs_env%cp_ddapc_env)
    1748           0 :          DEALLOCATE (qs_env%cp_ddapc_env)
    1749             :       END IF
    1750           9 :       CALL cp_ddapc_ewald_release(qs_env%cp_ddapc_ewald)
    1751           9 :       CALL efield_berry_release(qs_env%efield)
    1752           9 :       IF (ASSOCIATED(qs_env%x_data)) THEN
    1753           0 :          CALL hfx_release(qs_env%x_data)
    1754             :       END IF
    1755           9 :       IF (ASSOCIATED(qs_env%et_coupling)) THEN
    1756           0 :          CALL et_coupling_release(qs_env%et_coupling)
    1757             :       END IF
    1758           9 :       IF (ASSOCIATED(qs_env%dftb_potential)) THEN
    1759           0 :          CALL qs_dftb_pairpot_release(qs_env%dftb_potential)
    1760             :       END IF
    1761           9 :       IF (ASSOCIATED(qs_env%se_taper)) THEN
    1762           0 :          CALL se_taper_release(qs_env%se_taper)
    1763             :       END IF
    1764           9 :       IF (ASSOCIATED(qs_env%se_store_int_env)) THEN
    1765           0 :          CALL semi_empirical_si_release(qs_env%se_store_int_env)
    1766             :       END IF
    1767           9 :       IF (ASSOCIATED(qs_env%se_nddo_mpole)) THEN
    1768           0 :          CALL nddo_mpole_release(qs_env%se_nddo_mpole)
    1769             :       END IF
    1770           9 :       IF (ASSOCIATED(qs_env%se_nonbond_env)) THEN
    1771           0 :          CALL fist_nonbond_env_release(qs_env%se_nonbond_env)
    1772           0 :          DEALLOCATE (qs_env%se_nonbond_env)
    1773             :       END IF
    1774           9 :       IF (ASSOCIATED(qs_env%admm_env)) THEN
    1775           0 :          CALL admm_env_release(qs_env%admm_env)
    1776             :       END IF
    1777           9 :       IF (ASSOCIATED(qs_env%lri_env)) THEN
    1778           0 :          CALL lri_env_release(qs_env%lri_env)
    1779           0 :          DEALLOCATE (qs_env%lri_env)
    1780             :       END IF
    1781           9 :       IF (ASSOCIATED(qs_env%lri_density)) THEN
    1782           0 :          CALL lri_density_release(qs_env%lri_density)
    1783           0 :          DEALLOCATE (qs_env%lri_density)
    1784             :       END IF
    1785           9 :       IF (ASSOCIATED(qs_env%harris_env)) THEN
    1786           9 :          CALL harris_env_release(qs_env%harris_env)
    1787             :       END IF
    1788           9 :       IF (ASSOCIATED(qs_env%ec_env)) THEN
    1789           9 :          CALL ec_env_release(qs_env%ec_env)
    1790             :       END IF
    1791           9 :       IF (ASSOCIATED(qs_env%exstate_env)) THEN
    1792           9 :          CALL exstate_release(qs_env%exstate_env)
    1793             :       END IF
    1794           9 :       IF (ASSOCIATED(qs_env%mp2_env)) THEN
    1795           0 :          CALL mp2_env_release(qs_env%mp2_env)
    1796           0 :          DEALLOCATE (qs_env%mp2_env)
    1797             :          NULLIFY (qs_env%mp2_env)
    1798             :       END IF
    1799           9 :       IF (ASSOCIATED(qs_env%kg_env)) THEN
    1800           0 :          CALL kg_env_release(qs_env%kg_env)
    1801             :       END IF
    1802             : 
    1803             :       ! dispersion
    1804           9 :       CALL qs_dispersion_release(qs_env%dispersion_env)
    1805             :       ! gCP
    1806           9 :       IF (ASSOCIATED(qs_env%gcp_env)) THEN
    1807           9 :          CALL qs_gcp_release(qs_env%gcp_env)
    1808             :       END IF
    1809             : 
    1810           9 :       IF (ASSOCIATED(qs_env%WannierCentres)) THEN
    1811           0 :          DO i = 1, SIZE(qs_env%WannierCentres)
    1812           0 :             DEALLOCATE (qs_env%WannierCentres(i)%WannierHamDiag)
    1813           0 :             DEALLOCATE (qs_env%WannierCentres(i)%centres)
    1814             :          END DO
    1815           0 :          DEALLOCATE (qs_env%WannierCentres)
    1816             :       END IF
    1817             :       ! Resp charges
    1818           9 :       IF (ASSOCIATED(qs_env%rhs)) DEALLOCATE (qs_env%rhs)
    1819             : 
    1820           9 :    END SUBROUTINE qs_env_part_release
    1821             : 
    1822           0 : END MODULE qs_environment_types

Generated by: LCOV version 1.15