LCOV - code coverage report
Current view: top level - src - qs_ks_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:42dac4a) Lines: 93.0 % 329 306
Test Date: 2025-07-25 12:55:17 Functions: 85.7 % 7 6

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \par History
      10              : !>      11.2002 created [fawzi]
      11              : !>      10.2014 Moved many components from qs_env to here [Ole Schuett]
      12              : !>      11.2014 unified k-point and gamma-point code [Ole Schuett]
      13              : !> \author Fawzi Mohamed
      14              : ! **************************************************************************************************
      15              : MODULE qs_ks_types
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      17              :    USE atprop_types,                    ONLY: atprop_type
      18              :    USE cell_types,                      ONLY: cell_type
      19              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_release,&
      20              :                                               cp_blacs_env_type
      21              :    USE cp_control_types,                ONLY: dft_control_release,&
      22              :                                               dft_control_type
      23              :    USE cp_dbcsr_api,                    ONLY: dbcsr_distribution_release,&
      24              :                                               dbcsr_distribution_type,&
      25              :                                               dbcsr_p_type
      26              :    USE cp_dbcsr_operations,             ONLY: dbcsr_deallocate_matrix_set
      27              :    USE cp_result_types,                 ONLY: cp_result_type
      28              :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      29              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      30              :    USE distribution_2d_types,           ONLY: distribution_2d_release,&
      31              :                                               distribution_2d_retain,&
      32              :                                               distribution_2d_type
      33              :    USE kpoint_transitional,             ONLY: get_1d_pointer,&
      34              :                                               get_2d_pointer,&
      35              :                                               kpoint_transitional_release,&
      36              :                                               kpoint_transitional_type,&
      37              :                                               set_1d_pointer,&
      38              :                                               set_2d_pointer
      39              :    USE kpoint_types,                    ONLY: kpoint_release,&
      40              :                                               kpoint_type
      41              :    USE message_passing,                 ONLY: mp_para_env_release,&
      42              :                                               mp_para_env_type
      43              :    USE molecule_kind_types,             ONLY: molecule_kind_type
      44              :    USE molecule_types,                  ONLY: molecule_type
      45              :    USE particle_types,                  ONLY: particle_type
      46              :    USE pw_env_types,                    ONLY: pw_env_release,&
      47              :                                               pw_env_retain,&
      48              :                                               pw_env_type
      49              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      50              :                                               pw_r3d_rs_type
      51              :    USE qs_energy_types,                 ONLY: qs_energy_type
      52              :    USE qs_force_types,                  ONLY: qs_force_type
      53              :    USE qs_kind_types,                   ONLY: qs_kind_type
      54              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type,&
      55              :                                               release_neighbor_list_sets
      56              :    USE qs_rho_types,                    ONLY: qs_rho_create,&
      57              :                                               qs_rho_release,&
      58              :                                               qs_rho_type
      59              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
      60              :                                               qs_subsys_release,&
      61              :                                               qs_subsys_type
      62              :    USE task_list_types,                 ONLY: deallocate_task_list,&
      63              :                                               task_list_type
      64              :    USE virial_types,                    ONLY: virial_type
      65              : #include "./base/base_uses.f90"
      66              : 
      67              :    IMPLICIT NONE
      68              :    PRIVATE
      69              : 
      70              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      71              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_types'
      72              : 
      73              :    PUBLIC :: qs_ks_env_type, qs_ks_env_create, qs_ks_did_change
      74              :    PUBLIC :: qs_ks_release, qs_ks_part_release, get_ks_env, set_ks_env
      75              : 
      76              : ! **************************************************************************************************
      77              : !> \brief calculation environment to calculate the ks matrix,
      78              : !>      holds all the needed vars.
      79              : !>      assumes that the core hamiltonian and energy are up to date.
      80              : !> \param s_mstruct_changed if the sparsity structure of s in qs_env where this
      81              : !>        ks_env lives has changed
      82              : !> \param rho_changed if the rho stored in the qs_env has changed
      83              : !> \param forces_up_to_date if the forces are up to date
      84              : !> \param potential_changed if some external potential changed (such as due to the constraint)
      85              : !>
      86              : !> \param matrix_h core hamiltonian (matrix_h(1)), if needed by the calculation also
      87              : !>        its derivatives wrt. x,y, and z (matrix_h(2:4))
      88              : !> \param matrix_ks Khon-Sham matrix (filtered by the structure of S).
      89              : !>        with LSD an array with the different orbitals
      90              : !> \param matrix_s the overlap matrix (matrix_s(1)), if needed by the calculation also
      91              : !>        its derivatives wrt. x,y, and z (matrix_s(2:4))
      92              : !> \param kinetic kinetic part of h
      93              : !>
      94              : !> \param rho the (old) density, in various representations (ao+grid)
      95              : !> \param rho_xc the (old) soft density, used for the GAPW_XC method only
      96              : !> \param rho_core a grid with the charges of the cores of the atoms in the
      97              : !>        reciprocal (g) space
      98              : !> \param vppl a realspace grid with the local pseudopotential
      99              : !> \param vee a realspace grid with the external electrostatic potential
     100              : !>
     101              : !> \param neighbor_list_id the current version of the neighbor_list
     102              : !> \param sab_orb ,sac_ae, sac_ppl, sac_lri, sap_ppnl:
     103              : !>        neighbor lists for the calculation of the core Hamiltonian matrix
     104              : !> \param sap_oce: neighbor lists for the calculation of the expansion
     105              : !>        coefficients of the local atomic densities rho1_hard and rho1_soft
     106              : !> \param sab_se: neighbor lists for the calculation of the 2 centers
     107              : !>        hartree term in semi-empirical methods
     108              : !> \param sac_tbe: neighbor lists for the calculation of the tight binding
     109              : !>        Ewald terms (DFTB)
     110              : !> \param sab_core neighbor lists for the calculation of the core interactions
     111              : !> \param sab_xb neighbor lists for the calculation of the XB interactions in xTB
     112              : !> \param sab_xtb_pp neighbor lists for the calculation of the repulsive interactions in xTB
     113              : !> \param sab_xtb_nonbond neighbor lists for the calculation of the nonbonded interactions in xTB
     114              : !> \param sab_all neighbor lists for the calculation of the  matrix element of
     115              : !>        non-symmetric operators
     116              : !> \param sab_vdw: neighbor lists for the calculation of dispersion interactions
     117              : !> \param sab_scp: neighbor lists for the calculation of self-consistent polarization
     118              : !> \param sab_almo: neighbor lists to create ALMO delocalization template
     119              : !> \param sab_kp: neighbor lists to create kp image cell lists
     120              : !> \param sab_kp_nosym: neighbor lists to create kp image cell lists, non-symmetric
     121              : !>
     122              : !> \param kpoints information on the kpoints used
     123              : !> \param subsys the particles, molecules,... of this environment
     124              : !> \param dft_control object that contains the values of various parameters
     125              : !>        relevant to a dft calculation
     126              : !> \param distribution_2d: distribution of the atom pairs between the processors
     127              : !>
     128              : !> \par History
     129              : !>      04.2002 created [fawzi]
     130              : !>      10.2002 introduced pools, uses updated rho as input,
     131              : !>              removed most temporary variables, renamed may vars,
     132              : !>              began conversion to LSD [fawzi]
     133              : !>      10.2014 Moved many components from qs_env here [Ole Schuett]
     134              : !> \author Fawzi Mohamed
     135              : ! **************************************************************************************************
     136              :    TYPE qs_ks_env_type
     137              :       PRIVATE
     138              :       LOGICAL                                               :: s_mstruct_changed = .TRUE., &
     139              :                                                                rho_changed = .TRUE., &
     140              :                                                                potential_changed = .TRUE., &
     141              :                                                                forces_up_to_date = .FALSE., &
     142              :                                                                complex_ks = .FALSE.
     143              :       INTEGER                                               :: n_evals = 0
     144              : 
     145              :       ! hartree is supposed to contain the hartree potential (for cube output)
     146              :       ! ugly to keep it always around only for a cube output...
     147              :       TYPE(pw_r3d_rs_type), POINTER                         :: v_hartree_rspace => Null()
     148              : 
     149              :       TYPE(kpoint_transitional_type)                        :: matrix_ks, &
     150              :                                                                matrix_s, &
     151              :                                                                kinetic, &
     152              :                                                                matrix_h, &
     153              :                                                                matrix_w, &
     154              :                                                                matrix_vxc, &
     155              :                                                                matrix_s_RI_aux, &
     156              :                                                                matrix_ks_im, &
     157              :                                                                matrix_h_im
     158              : 
     159              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER             :: matrix_p_mp2 => Null(), &
     160              :                                                                matrix_p_mp2_admm => Null()
     161              : 
     162              :       TYPE(qs_rho_type), POINTER                            :: rho => Null(), &
     163              :                                                                rho_xc => Null()
     164              : 
     165              :       TYPE(pw_r3d_rs_type), POINTER                         :: vppl => Null(), &
     166              :                                                                rho_nlcc => Null()
     167              :       TYPE(pw_c1d_gs_type), POINTER                         :: rho_nlcc_g => Null()
     168              : 
     169              :       TYPE(pw_c1d_gs_type), POINTER                         :: rho_core => NULL()
     170              :       TYPE(pw_r3d_rs_type), POINTER                         :: vee => NULL()
     171              : 
     172              :       INTEGER                                               :: neighbor_list_id = -1
     173              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), POINTER :: sab_orb => Null(), &
     174              :                                                                sac_ae => Null(), &
     175              :                                                                sac_ppl => Null(), &
     176              :                                                                sac_lri => Null(), &
     177              :                                                                sap_ppnl => Null(), &
     178              :                                                                sap_oce => Null(), &
     179              :                                                                sab_se => Null(), &
     180              :                                                                sab_vdw => Null(), &
     181              :                                                                sab_scp => Null(), &
     182              :                                                                sab_tbe => Null(), &
     183              :                                                                sab_xtbe => Null(), &
     184              :                                                                sab_core => Null(), &
     185              :                                                                sab_xb => Null(), &
     186              :                                                                sab_xtb_pp => Null(), &
     187              :                                                                sab_xtb_nonbond => Null(), &
     188              :                                                                sab_all => Null(), &
     189              :                                                                sab_lrc => Null(), &
     190              :                                                                sab_almo => Null(), &
     191              :                                                                sab_kp => Null(), &
     192              :                                                                sab_kp_nosym => Null()
     193              : 
     194              :       TYPE(task_list_type), POINTER                         :: task_list => Null()
     195              :       TYPE(task_list_type), POINTER                         :: task_list_soft => Null()
     196              : 
     197              :       TYPE(kpoint_type), POINTER                            :: kpoints => Null()
     198              :       TYPE(qs_subsys_type), POINTER                         :: subsys => Null()
     199              :       TYPE(dft_control_type), POINTER                       :: dft_control => Null()
     200              :       TYPE(dbcsr_distribution_type), POINTER                :: dbcsr_dist => Null()
     201              :       TYPE(distribution_2d_type), POINTER                   :: distribution_2d => Null()
     202              :       TYPE(pw_env_type), POINTER                            :: pw_env => Null()
     203              :       TYPE(mp_para_env_type), POINTER                       :: para_env => Null()
     204              :       TYPE(cp_blacs_env_type), POINTER                      :: blacs_env => Null()
     205              :    END TYPE qs_ks_env_type
     206              : 
     207              : CONTAINS
     208              : 
     209              : ! **************************************************************************************************
     210              : !> \brief Allocates a new instance of ks_env.
     211              : !> \param ks_env ...
     212              : !> \author Ole Schuett
     213              : ! **************************************************************************************************
     214         7404 :    SUBROUTINE qs_ks_env_create(ks_env)
     215              :       TYPE(qs_ks_env_type), INTENT(OUT)                  :: ks_env
     216              : 
     217         7404 :       ALLOCATE (ks_env%rho)
     218         7404 :       CALL qs_rho_create(ks_env%rho)
     219         7404 :       ALLOCATE (ks_env%rho_xc)
     220         7404 :       CALL qs_rho_create(ks_env%rho_xc)
     221         7404 :    END SUBROUTINE qs_ks_env_create
     222              : 
     223              : ! **************************************************************************************************
     224              : !> \brief ...
     225              : !> \param ks_env ...
     226              : !> \param v_hartree_rspace ...
     227              : !> \param s_mstruct_changed ...
     228              : !> \param rho_changed ...
     229              : !> \param potential_changed ...
     230              : !> \param forces_up_to_date ...
     231              : !> \param complex_ks ...
     232              : !> \param matrix_h ...
     233              : !> \param matrix_h_im ...
     234              : !> \param matrix_ks ...
     235              : !> \param matrix_ks_im ...
     236              : !> \param matrix_vxc ...
     237              : !> \param kinetic ...
     238              : !> \param matrix_s ...
     239              : !> \param matrix_s_RI_aux ...
     240              : !> \param matrix_w ...
     241              : !> \param matrix_p_mp2 ...
     242              : !> \param matrix_p_mp2_admm ...
     243              : !> \param matrix_h_kp ...
     244              : !> \param matrix_h_im_kp ...
     245              : !> \param matrix_ks_kp ...
     246              : !> \param matrix_vxc_kp ...
     247              : !> \param kinetic_kp ...
     248              : !> \param matrix_s_kp ...
     249              : !> \param matrix_w_kp ...
     250              : !> \param matrix_s_RI_aux_kp ...
     251              : !> \param matrix_ks_im_kp ...
     252              : !> \param rho ...
     253              : !> \param rho_xc ...
     254              : !> \param vppl ...
     255              : !> \param rho_core ...
     256              : !> \param rho_nlcc ...
     257              : !> \param rho_nlcc_g ...
     258              : !> \param vee ...
     259              : !> \param neighbor_list_id ...
     260              : !> \param sab_orb ...
     261              : !> \param sab_all ...
     262              : !> \param sac_ae ...
     263              : !> \param sac_ppl ...
     264              : !> \param sac_lri ...
     265              : !> \param sap_ppnl ...
     266              : !> \param sap_oce ...
     267              : !> \param sab_lrc ...
     268              : !> \param sab_se ...
     269              : !> \param sab_xtbe ...
     270              : !> \param sab_tbe ...
     271              : !> \param sab_core ...
     272              : !> \param sab_xb ...
     273              : !> \param sab_xtb_pp ...
     274              : !> \param sab_xtb_nonbond ...
     275              : !> \param sab_vdw ...
     276              : !> \param sab_scp ...
     277              : !> \param sab_almo ...
     278              : !> \param sab_kp ...
     279              : !> \param sab_kp_nosym ...
     280              : !> \param task_list ...
     281              : !> \param task_list_soft ...
     282              : !> \param kpoints ...
     283              : !> \param do_kpoints ...
     284              : !> \param atomic_kind_set ...
     285              : !> \param qs_kind_set ...
     286              : !> \param cell ...
     287              : !> \param cell_ref ...
     288              : !> \param use_ref_cell ...
     289              : !> \param particle_set ...
     290              : !> \param energy ...
     291              : !> \param force ...
     292              : !> \param local_particles ...
     293              : !> \param local_molecules ...
     294              : !> \param molecule_kind_set ...
     295              : !> \param molecule_set ...
     296              : !> \param subsys ...
     297              : !> \param cp_subsys ...
     298              : !> \param virial ...
     299              : !> \param results ...
     300              : !> \param atprop ...
     301              : !> \param nkind ...
     302              : !> \param natom ...
     303              : !> \param dft_control ...
     304              : !> \param dbcsr_dist ...
     305              : !> \param distribution_2d ...
     306              : !> \param pw_env ...
     307              : !> \param para_env ...
     308              : !> \param blacs_env ...
     309              : !> \param nelectron_total ...
     310              : !> \param nelectron_spin ...
     311              : ! **************************************************************************************************
     312     10616943 :    SUBROUTINE get_ks_env(ks_env, v_hartree_rspace, &
     313              :                          s_mstruct_changed, rho_changed, &
     314              :                          potential_changed, forces_up_to_date, complex_ks, &
     315              :                          matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, &
     316              :                          kinetic, matrix_s, &
     317              :                          matrix_s_RI_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, &
     318              :                          matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, &
     319              :                          matrix_s_RI_aux_kp, matrix_ks_im_kp, &
     320              :                          rho, rho_xc, &
     321              :                          vppl, rho_core, rho_nlcc, rho_nlcc_g, vee, &
     322              :                          neighbor_list_id, &
     323              :                          sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, &
     324              :                          sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, &
     325              :                          sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, &
     326              :                          task_list, task_list_soft, &
     327              :                          kpoints, do_kpoints, &
     328              :                          atomic_kind_set, qs_kind_set, cell, cell_ref, use_ref_cell, &
     329              :                          particle_set, energy, force, local_particles, local_molecules, &
     330              :                          molecule_kind_set, molecule_set, subsys, cp_subsys, virial, results, atprop, &
     331              :                          nkind, natom, dft_control, dbcsr_dist, distribution_2d, pw_env, &
     332              :                          para_env, blacs_env, nelectron_total, nelectron_spin)
     333              : 
     334              :       TYPE(qs_ks_env_type), INTENT(IN)                   :: ks_env
     335              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: v_hartree_rspace
     336              :       LOGICAL, OPTIONAL                                  :: s_mstruct_changed, rho_changed, &
     337              :                                                             potential_changed, forces_up_to_date, &
     338              :                                                             complex_ks
     339              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, POINTER :: matrix_h, matrix_h_im, matrix_ks, &
     340              :          matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_RI_aux, matrix_w, matrix_p_mp2, &
     341              :          matrix_p_mp2_admm
     342              :       TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, POINTER :: matrix_h_kp, matrix_h_im_kp, &
     343              :          matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_RI_aux_kp, &
     344              :          matrix_ks_im_kp
     345              :       TYPE(qs_rho_type), OPTIONAL, POINTER               :: rho, rho_xc
     346              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: vppl
     347              :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho_core
     348              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: rho_nlcc
     349              :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho_nlcc_g
     350              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: vee
     351              :       INTEGER, OPTIONAL                                  :: neighbor_list_id
     352              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), OPTIONAL, POINTER :: sab_orb, sab_all, sac_ae, &
     353              :          sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, &
     354              :          sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym
     355              :       TYPE(task_list_type), OPTIONAL, POINTER            :: task_list, task_list_soft
     356              :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoints
     357              :       LOGICAL, OPTIONAL                                  :: do_kpoints
     358              :       TYPE(atomic_kind_type), DIMENSION(:), OPTIONAL, &
     359              :          POINTER                                         :: atomic_kind_set
     360              :       TYPE(qs_kind_type), DIMENSION(:), OPTIONAL, &
     361              :          POINTER                                         :: qs_kind_set
     362              :       TYPE(cell_type), OPTIONAL, POINTER                 :: cell, cell_ref
     363              :       LOGICAL, OPTIONAL                                  :: use_ref_cell
     364              :       TYPE(particle_type), DIMENSION(:), OPTIONAL, &
     365              :          POINTER                                         :: particle_set
     366              :       TYPE(qs_energy_type), OPTIONAL, POINTER            :: energy
     367              :       TYPE(qs_force_type), DIMENSION(:), OPTIONAL, &
     368              :          POINTER                                         :: force
     369              :       TYPE(distribution_1d_type), OPTIONAL, POINTER      :: local_particles, local_molecules
     370              :       TYPE(molecule_kind_type), DIMENSION(:), OPTIONAL, &
     371              :          POINTER                                         :: molecule_kind_set
     372              :       TYPE(molecule_type), DIMENSION(:), OPTIONAL, &
     373              :          POINTER                                         :: molecule_set
     374              :       TYPE(qs_subsys_type), OPTIONAL, POINTER            :: subsys
     375              :       TYPE(cp_subsys_type), OPTIONAL, POINTER            :: cp_subsys
     376              :       TYPE(virial_type), OPTIONAL, POINTER               :: virial
     377              :       TYPE(cp_result_type), OPTIONAL, POINTER            :: results
     378              :       TYPE(atprop_type), OPTIONAL, POINTER               :: atprop
     379              :       INTEGER, OPTIONAL                                  :: nkind, natom
     380              :       TYPE(dft_control_type), OPTIONAL, POINTER          :: dft_control
     381              :       TYPE(dbcsr_distribution_type), OPTIONAL, POINTER   :: dbcsr_dist
     382              :       TYPE(distribution_2d_type), OPTIONAL, POINTER      :: distribution_2d
     383              :       TYPE(pw_env_type), OPTIONAL, POINTER               :: pw_env
     384              :       TYPE(mp_para_env_type), OPTIONAL, POINTER          :: para_env
     385              :       TYPE(cp_blacs_env_type), OPTIONAL, POINTER         :: blacs_env
     386              :       INTEGER, OPTIONAL                                  :: nelectron_total
     387              :       INTEGER, DIMENSION(2), OPTIONAL                    :: nelectron_spin
     388              : 
     389     10616943 :       IF (.NOT. ASSOCIATED(ks_env%subsys)) CPABORT("get_ks_env: subsys not associated")
     390              : 
     391     10616943 :       IF (PRESENT(s_mstruct_changed)) s_mstruct_changed = ks_env%s_mstruct_changed
     392     10616943 :       IF (PRESENT(rho_changed)) rho_changed = ks_env%rho_changed
     393     10616943 :       IF (PRESENT(potential_changed)) potential_changed = ks_env%potential_changed
     394     10616943 :       IF (PRESENT(forces_up_to_date)) forces_up_to_date = ks_env%forces_up_to_date
     395     10616943 :       IF (PRESENT(complex_ks)) complex_ks = ks_env%complex_ks
     396     10616943 :       IF (PRESENT(v_hartree_rspace)) v_hartree_rspace => ks_env%v_hartree_rspace
     397              : 
     398     10616943 :       IF (PRESENT(matrix_ks)) matrix_ks => get_1d_pointer(ks_env%matrix_ks)
     399     10616943 :       IF (PRESENT(matrix_s)) matrix_s => get_1d_pointer(ks_env%matrix_s)
     400     10616943 :       IF (PRESENT(matrix_s_RI_aux)) matrix_s_RI_aux => get_1d_pointer(ks_env%matrix_s_RI_aux)
     401     10616943 :       IF (PRESENT(kinetic)) kinetic => get_1d_pointer(ks_env%kinetic)
     402     10616943 :       IF (PRESENT(matrix_h)) matrix_h => get_1d_pointer(ks_env%matrix_h)
     403     10616943 :       IF (PRESENT(matrix_h_im)) matrix_h_im => get_1d_pointer(ks_env%matrix_h_im)
     404     10616943 :       IF (PRESENT(matrix_w)) matrix_w => get_1d_pointer(ks_env%matrix_w)
     405     10616943 :       IF (PRESENT(matrix_vxc)) matrix_vxc => get_1d_pointer(ks_env%matrix_vxc)
     406              : 
     407     10616943 :       IF (PRESENT(matrix_ks_kp)) matrix_ks_kp => get_2d_pointer(ks_env%matrix_ks)
     408     10616943 :       IF (PRESENT(matrix_ks_im_kp)) matrix_ks_im_kp => get_2d_pointer(ks_env%matrix_ks_im)
     409     10616943 :       IF (PRESENT(matrix_s_kp)) matrix_s_kp => get_2d_pointer(ks_env%matrix_s)
     410     10616943 :       IF (PRESENT(matrix_s_RI_aux_kp)) matrix_s_RI_aux_kp => get_2d_pointer(ks_env%matrix_s_RI_aux)
     411     10616943 :       IF (PRESENT(matrix_w_kp)) matrix_w_kp => get_2d_pointer(ks_env%matrix_w)
     412     10616943 :       IF (PRESENT(kinetic_kp)) kinetic_kp => get_2d_pointer(ks_env%kinetic)
     413     10616943 :       IF (PRESENT(matrix_h_kp)) matrix_h_kp => get_2d_pointer(ks_env%matrix_h)
     414     10616943 :       IF (PRESENT(matrix_h_im_kp)) matrix_h_im_kp => get_2d_pointer(ks_env%matrix_h_im)
     415     10616943 :       IF (PRESENT(matrix_vxc_kp)) matrix_vxc_kp => get_2d_pointer(ks_env%matrix_vxc)
     416     10616943 :       IF (PRESENT(matrix_ks_im)) matrix_ks_im => get_1d_pointer(ks_env%matrix_ks_im)
     417              : 
     418     10616943 :       IF (PRESENT(matrix_p_mp2)) matrix_p_mp2 => ks_env%matrix_p_mp2
     419     10616943 :       IF (PRESENT(matrix_p_mp2_admm)) matrix_p_mp2_admm => ks_env%matrix_p_mp2_admm
     420     10616943 :       IF (PRESENT(rho)) rho => ks_env%rho
     421     10616943 :       IF (PRESENT(rho_xc)) rho_xc => ks_env%rho_xc
     422     10616943 :       IF (PRESENT(rho_core)) rho_core => ks_env%rho_core
     423     10616943 :       IF (PRESENT(rho_nlcc)) rho_nlcc => ks_env%rho_nlcc
     424     10616943 :       IF (PRESENT(rho_nlcc_g)) rho_nlcc_g => ks_env%rho_nlcc_g
     425     10616943 :       IF (PRESENT(vppl)) vppl => ks_env%vppl
     426     10616943 :       IF (PRESENT(vee)) vee => ks_env%vee
     427              : 
     428     10616943 :       IF (PRESENT(neighbor_list_id)) neighbor_list_id = ks_env%neighbor_list_id
     429     10616943 :       IF (PRESENT(sab_orb)) sab_orb => ks_env%sab_orb
     430     10616943 :       IF (PRESENT(sab_all)) sab_all => ks_env%sab_all
     431     10616943 :       IF (PRESENT(sab_vdw)) sab_vdw => ks_env%sab_vdw
     432     10616943 :       IF (PRESENT(sab_scp)) sab_scp => ks_env%sab_scp
     433     10616943 :       IF (PRESENT(sac_ae)) sac_ae => ks_env%sac_ae
     434     10616943 :       IF (PRESENT(sac_ppl)) sac_ppl => ks_env%sac_ppl
     435     10616943 :       IF (PRESENT(sac_lri)) sac_lri => ks_env%sac_lri
     436     10616943 :       IF (PRESENT(sap_ppnl)) sap_ppnl => ks_env%sap_ppnl
     437     10616943 :       IF (PRESENT(sap_oce)) sap_oce => ks_env%sap_oce
     438     10616943 :       IF (PRESENT(sab_se)) sab_se => ks_env%sab_se
     439     10616943 :       IF (PRESENT(sab_lrc)) sab_lrc => ks_env%sab_lrc
     440     10616943 :       IF (PRESENT(sab_tbe)) sab_tbe => ks_env%sab_tbe
     441     10616943 :       IF (PRESENT(sab_xtbe)) sab_xtbe => ks_env%sab_xtbe
     442     10616943 :       IF (PRESENT(sab_core)) sab_core => ks_env%sab_core
     443     10616943 :       IF (PRESENT(sab_xb)) sab_xb => ks_env%sab_xb
     444     10616943 :       IF (PRESENT(sab_xtb_pp)) sab_xtb_pp => ks_env%sab_xtb_pp
     445     10616943 :       IF (PRESENT(sab_xtb_nonbond)) sab_xtb_nonbond => ks_env%sab_xtb_nonbond
     446     10616943 :       IF (PRESENT(sab_almo)) sab_almo => ks_env%sab_almo
     447     10616943 :       IF (PRESENT(sab_kp)) sab_kp => ks_env%sab_kp
     448     10616943 :       IF (PRESENT(sab_kp_nosym)) sab_kp_nosym => ks_env%sab_kp_nosym
     449     10616943 :       IF (PRESENT(dft_control)) dft_control => ks_env%dft_control
     450     10616943 :       IF (PRESENT(dbcsr_dist)) dbcsr_dist => ks_env%dbcsr_dist
     451     10616943 :       IF (PRESENT(distribution_2d)) distribution_2d => ks_env%distribution_2d
     452     10616943 :       IF (PRESENT(pw_env)) pw_env => ks_env%pw_env
     453     10616943 :       IF (PRESENT(para_env)) para_env => ks_env%para_env
     454     10616943 :       IF (PRESENT(blacs_env)) blacs_env => ks_env%blacs_env
     455              : 
     456     10616943 :       IF (PRESENT(task_list)) task_list => ks_env%task_list
     457     10616943 :       IF (PRESENT(task_list_soft)) task_list_soft => ks_env%task_list_soft
     458              : 
     459     10616943 :       IF (PRESENT(kpoints)) kpoints => ks_env%kpoints
     460     10616943 :       IF (PRESENT(do_kpoints)) THEN
     461       704877 :          do_kpoints = (ks_env%kpoints%nkp > 0)
     462              :       END IF
     463              : 
     464     10616943 :       IF (PRESENT(subsys)) subsys => ks_env%subsys
     465              :       CALL qs_subsys_get(ks_env%subsys, &
     466              :                          local_molecules=local_molecules, &
     467              :                          local_particles=local_particles, &
     468              :                          atprop=atprop, &
     469              :                          virial=virial, &
     470              :                          results=results, &
     471              :                          cell=cell, &
     472              :                          cell_ref=cell_ref, &
     473              :                          use_ref_cell=use_ref_cell, &
     474              :                          energy=energy, &
     475              :                          force=force, &
     476              :                          qs_kind_set=qs_kind_set, &
     477              :                          cp_subsys=cp_subsys, &
     478              :                          atomic_kind_set=atomic_kind_set, &
     479              :                          particle_set=particle_set, &
     480              :                          molecule_kind_set=molecule_kind_set, &
     481              :                          molecule_set=molecule_set, &
     482              :                          natom=natom, &
     483              :                          nkind=nkind, &
     484              :                          nelectron_total=nelectron_total, &
     485     10616943 :                          nelectron_spin=nelectron_spin)
     486              : 
     487     10616943 :    END SUBROUTINE get_ks_env
     488              : 
     489              : ! **************************************************************************************************
     490              : !> \brief ...
     491              : !> \param ks_env ...
     492              : !> \param v_hartree_rspace ...
     493              : !> \param s_mstruct_changed ...
     494              : !> \param rho_changed ...
     495              : !> \param potential_changed ...
     496              : !> \param forces_up_to_date ...
     497              : !> \param complex_ks ...
     498              : !> \param matrix_h ...
     499              : !> \param matrix_h_im ...
     500              : !> \param matrix_ks ...
     501              : !> \param matrix_ks_im ...
     502              : !> \param matrix_vxc ...
     503              : !> \param kinetic ...
     504              : !> \param matrix_s ...
     505              : !> \param matrix_s_RI_aux ...
     506              : !> \param matrix_w ...
     507              : !> \param matrix_p_mp2 ...
     508              : !> \param matrix_p_mp2_admm ...
     509              : !> \param matrix_h_kp ...
     510              : !> \param matrix_h_im_kp ...
     511              : !> \param matrix_ks_kp ...
     512              : !> \param matrix_vxc_kp ...
     513              : !> \param kinetic_kp ...
     514              : !> \param matrix_s_kp ...
     515              : !> \param matrix_w_kp ...
     516              : !> \param matrix_s_RI_aux_kp ...
     517              : !> \param matrix_ks_im_kp ...
     518              : !> \param vppl ...
     519              : !> \param rho_core ...
     520              : !> \param rho_nlcc ...
     521              : !> \param rho_nlcc_g ...
     522              : !> \param vee ...
     523              : !> \param neighbor_list_id ...
     524              : !> \param kpoints ...
     525              : !> \param sab_orb ...
     526              : !> \param sab_all ...
     527              : !> \param sac_ae ...
     528              : !> \param sac_ppl ...
     529              : !> \param sac_lri ...
     530              : !> \param sap_ppnl ...
     531              : !> \param sap_oce ...
     532              : !> \param sab_lrc ...
     533              : !> \param sab_se ...
     534              : !> \param sab_xtbe ...
     535              : !> \param sab_tbe ...
     536              : !> \param sab_core ...
     537              : !> \param sab_xb ...
     538              : !> \param sab_xtb_pp ...
     539              : !> \param sab_xtb_nonbond ...
     540              : !> \param sab_vdw ...
     541              : !> \param sab_scp ...
     542              : !> \param sab_almo ...
     543              : !> \param sab_kp ...
     544              : !> \param sab_kp_nosym ...
     545              : !> \param task_list ...
     546              : !> \param task_list_soft ...
     547              : !> \param subsys ...
     548              : !> \param dft_control ...
     549              : !> \param dbcsr_dist ...
     550              : !> \param distribution_2d ...
     551              : !> \param pw_env ...
     552              : !> \param para_env ...
     553              : !> \param blacs_env ...
     554              : ! **************************************************************************************************
     555       681735 :    SUBROUTINE set_ks_env(ks_env, v_hartree_rspace, &
     556              :                          s_mstruct_changed, rho_changed, &
     557              :                          potential_changed, forces_up_to_date, complex_ks, &
     558              :                          matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, &
     559              :                          kinetic, matrix_s, &
     560              :                          matrix_s_RI_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, &
     561              :                          matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, &
     562              :                          matrix_s_RI_aux_kp, matrix_ks_im_kp, &
     563              :                          vppl, rho_core, rho_nlcc, rho_nlcc_g, vee, &
     564              :                          neighbor_list_id, &
     565              :                          kpoints, &
     566              :                          sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, &
     567              :                          sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, &
     568              :                          sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, &
     569              :                          task_list, task_list_soft, &
     570              :                          subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, &
     571              :                          para_env, blacs_env)
     572              : 
     573              :       TYPE(qs_ks_env_type), INTENT(INOUT)                :: ks_env
     574              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: v_hartree_rspace
     575              :       LOGICAL, OPTIONAL                                  :: s_mstruct_changed, rho_changed, &
     576              :                                                             potential_changed, forces_up_to_date, &
     577              :                                                             complex_ks
     578              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, POINTER :: matrix_h, matrix_h_im, matrix_ks, &
     579              :          matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_RI_aux, matrix_w, matrix_p_mp2, &
     580              :          matrix_p_mp2_admm
     581              :       TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, POINTER :: matrix_h_kp, matrix_h_im_kp, &
     582              :          matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_RI_aux_kp, &
     583              :          matrix_ks_im_kp
     584              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: vppl
     585              :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho_core
     586              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: rho_nlcc
     587              :       TYPE(pw_c1d_gs_type), OPTIONAL, POINTER            :: rho_nlcc_g
     588              :       TYPE(pw_r3d_rs_type), OPTIONAL, POINTER            :: vee
     589              :       INTEGER, OPTIONAL                                  :: neighbor_list_id
     590              :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoints
     591              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), OPTIONAL, POINTER :: sab_orb, sab_all, sac_ae, &
     592              :          sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, &
     593              :          sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym
     594              :       TYPE(task_list_type), OPTIONAL, POINTER            :: task_list, task_list_soft
     595              :       TYPE(qs_subsys_type), OPTIONAL, POINTER            :: subsys
     596              :       TYPE(dft_control_type), OPTIONAL, POINTER          :: dft_control
     597              :       TYPE(dbcsr_distribution_type), OPTIONAL, POINTER   :: dbcsr_dist
     598              :       TYPE(distribution_2d_type), OPTIONAL, POINTER      :: distribution_2d
     599              :       TYPE(pw_env_type), OPTIONAL, POINTER               :: pw_env
     600              :       TYPE(mp_para_env_type), OPTIONAL, POINTER          :: para_env
     601              :       TYPE(cp_blacs_env_type), OPTIONAL, POINTER         :: blacs_env
     602              : 
     603       681735 :       IF (PRESENT(s_mstruct_changed)) ks_env%s_mstruct_changed = s_mstruct_changed
     604       681735 :       IF (PRESENT(rho_changed)) ks_env%rho_changed = rho_changed
     605       681735 :       IF (PRESENT(potential_changed)) ks_env%potential_changed = potential_changed
     606       681735 :       IF (PRESENT(forces_up_to_date)) ks_env%forces_up_to_date = forces_up_to_date
     607       681735 :       IF (PRESENT(complex_ks)) ks_env%complex_ks = complex_ks
     608       681735 :       IF (ks_env%s_mstruct_changed .OR. ks_env%potential_changed .OR. ks_env%rho_changed) &
     609       383031 :          ks_env%forces_up_to_date = .FALSE.
     610       681735 :       IF (PRESENT(v_hartree_rspace)) ks_env%v_hartree_rspace => v_hartree_rspace
     611              : 
     612       681735 :       IF (PRESENT(matrix_h)) CALL set_1d_pointer(ks_env%matrix_h, matrix_h)
     613       681735 :       IF (PRESENT(matrix_h_kp)) CALL set_2d_pointer(ks_env%matrix_h, matrix_h_kp)
     614              : 
     615       681735 :       IF (PRESENT(matrix_h_im)) CALL set_1d_pointer(ks_env%matrix_h_im, matrix_h_im)
     616       681735 :       IF (PRESENT(matrix_h_im_kp)) CALL set_2d_pointer(ks_env%matrix_h_im, matrix_h_im_kp)
     617              : 
     618       681735 :       IF (PRESENT(matrix_ks)) CALL set_1d_pointer(ks_env%matrix_ks, matrix_ks)
     619       681735 :       IF (PRESENT(matrix_ks_kp)) CALL set_2d_pointer(ks_env%matrix_ks, matrix_ks_kp)
     620              : 
     621       681735 :       IF (PRESENT(matrix_vxc)) CALL set_1d_pointer(ks_env%matrix_vxc, matrix_vxc)
     622       681735 :       IF (PRESENT(matrix_vxc_kp)) CALL set_2d_pointer(ks_env%matrix_vxc, matrix_vxc_kp)
     623              : 
     624       681735 :       IF (PRESENT(matrix_s)) CALL set_1d_pointer(ks_env%matrix_s, matrix_s)
     625       681735 :       IF (PRESENT(matrix_s_kp)) CALL set_2d_pointer(ks_env%matrix_s, matrix_s_kp)
     626              : 
     627       681735 :       IF (PRESENT(kinetic)) CALL set_1d_pointer(ks_env%kinetic, kinetic)
     628       681735 :       IF (PRESENT(kinetic_kp)) CALL set_2d_pointer(ks_env%kinetic, kinetic_kp)
     629              : 
     630       681735 :       IF (PRESENT(matrix_w)) CALL set_1d_pointer(ks_env%matrix_w, matrix_w)
     631       681735 :       IF (PRESENT(matrix_w_kp)) CALL set_2d_pointer(ks_env%matrix_w, matrix_w_kp)
     632              : 
     633       681735 :       IF (PRESENT(matrix_s_RI_aux)) CALL set_1d_pointer(ks_env%matrix_s_RI_aux, matrix_s_RI_aux)
     634       681735 :       IF (PRESENT(matrix_s_RI_aux_kp)) CALL set_2d_pointer(ks_env%matrix_s_RI_aux, matrix_s_RI_aux_kp)
     635              : 
     636       681735 :       IF (PRESENT(matrix_ks_im)) CALL set_1d_pointer(ks_env%matrix_ks_im, matrix_ks_im)
     637       681735 :       IF (PRESENT(matrix_ks_im_kp)) CALL set_2d_pointer(ks_env%matrix_ks_im, matrix_ks_im_kp)
     638              : 
     639       681735 :       IF (PRESENT(matrix_p_mp2)) ks_env%matrix_p_mp2 => matrix_p_mp2
     640       681735 :       IF (PRESENT(matrix_p_mp2_admm)) ks_env%matrix_p_mp2_admm => matrix_p_mp2_admm
     641       681735 :       IF (PRESENT(rho_core)) ks_env%rho_core => rho_core
     642       681735 :       IF (PRESENT(rho_nlcc)) ks_env%rho_nlcc => rho_nlcc
     643       681735 :       IF (PRESENT(rho_nlcc_g)) ks_env%rho_nlcc_g => rho_nlcc_g
     644       681735 :       IF (PRESENT(vppl)) ks_env%vppl => vppl
     645       681735 :       IF (PRESENT(vee)) ks_env%vee => vee
     646              : 
     647       681735 :       IF (PRESENT(neighbor_list_id)) ks_env%neighbor_list_id = neighbor_list_id
     648       681735 :       IF (PRESENT(kpoints)) ks_env%kpoints => kpoints
     649       681735 :       IF (PRESENT(sab_orb)) ks_env%sab_orb => sab_orb
     650       681735 :       IF (PRESENT(sab_vdw)) ks_env%sab_vdw => sab_vdw
     651       681735 :       IF (PRESENT(sab_scp)) ks_env%sab_scp => sab_scp
     652       681735 :       IF (PRESENT(sab_all)) ks_env%sab_all => sab_all
     653       681735 :       IF (PRESENT(sac_ae)) ks_env%sac_ae => sac_ae
     654       681735 :       IF (PRESENT(sac_ppl)) ks_env%sac_ppl => sac_ppl
     655       681735 :       IF (PRESENT(sac_lri)) ks_env%sac_lri => sac_lri
     656       681735 :       IF (PRESENT(sap_ppnl)) ks_env%sap_ppnl => sap_ppnl
     657       681735 :       IF (PRESENT(sap_oce)) ks_env%sap_oce => sap_oce
     658       681735 :       IF (PRESENT(sab_se)) ks_env%sab_se => sab_se
     659       681735 :       IF (PRESENT(sab_lrc)) ks_env%sab_lrc => sab_lrc
     660       681735 :       IF (PRESENT(sab_tbe)) ks_env%sab_tbe => sab_tbe
     661       681735 :       IF (PRESENT(sab_xtbe)) ks_env%sab_xtbe => sab_xtbe
     662       681735 :       IF (PRESENT(sab_core)) ks_env%sab_core => sab_core
     663       681735 :       IF (PRESENT(sab_xb)) ks_env%sab_xb => sab_xb
     664       681735 :       IF (PRESENT(sab_xtb_pp)) ks_env%sab_xtb_pp => sab_xtb_pp
     665       681735 :       IF (PRESENT(sab_xtb_nonbond)) ks_env%sab_xtb_nonbond => sab_xtb_nonbond
     666       681735 :       IF (PRESENT(sab_almo)) ks_env%sab_almo => sab_almo
     667       681735 :       IF (PRESENT(sab_kp)) ks_env%sab_kp => sab_kp
     668       681735 :       IF (PRESENT(sab_kp_nosym)) ks_env%sab_kp_nosym => sab_kp_nosym
     669              : 
     670       681735 :       IF (PRESENT(task_list)) ks_env%task_list => task_list
     671       681735 :       IF (PRESENT(task_list_soft)) ks_env%task_list_soft => task_list_soft
     672              : 
     673       681735 :       IF (PRESENT(subsys)) THEN
     674         7404 :          IF (ASSOCIATED(ks_env%subsys)) THEN
     675            0 :          IF (ASSOCIATED(ks_env%subsys, subsys)) THEN
     676            0 :             CALL qs_subsys_release(ks_env%subsys)
     677            0 :             DEALLOCATE (ks_env%subsys)
     678              :          END IF
     679              :          END IF
     680         7404 :          ks_env%subsys => subsys
     681              :       END IF
     682       681735 :       IF (PRESENT(dft_control)) THEN
     683         7404 :          IF (ASSOCIATED(ks_env%dft_control)) THEN
     684            0 :          IF (.NOT. ASSOCIATED(ks_env%dft_control, dft_control)) THEN
     685            0 :             CALL dft_control_release(ks_env%dft_control)
     686            0 :             DEALLOCATE (ks_env%dft_control)
     687              :          END IF
     688              :          END IF
     689         7404 :          ks_env%dft_control => dft_control
     690              :       END IF
     691       681735 :       IF (PRESENT(dbcsr_dist)) THEN
     692         7404 :          IF (ASSOCIATED(ks_env%dbcsr_dist)) THEN
     693            0 :             IF (.NOT. ASSOCIATED(ks_env%dbcsr_dist, dbcsr_dist)) THEN
     694            0 :                CALL dbcsr_distribution_release(ks_env%dbcsr_dist)
     695            0 :                DEALLOCATE (ks_env%dbcsr_dist)
     696              :             END IF
     697              :          END IF
     698         7404 :          ks_env%dbcsr_dist => dbcsr_dist
     699              :       END IF
     700       681735 :       IF (PRESENT(distribution_2d)) THEN
     701         7404 :          CALL distribution_2d_retain(distribution_2d)
     702         7404 :          CALL distribution_2d_release(ks_env%distribution_2d)
     703         7404 :          ks_env%distribution_2d => distribution_2d
     704              :       END IF
     705       681735 :       IF (PRESENT(pw_env)) THEN
     706         7404 :          CALL pw_env_retain(pw_env)
     707         7404 :          CALL pw_env_release(ks_env%pw_env)
     708         7404 :          ks_env%pw_env => pw_env
     709              :       END IF
     710       681735 :       IF (PRESENT(para_env)) THEN
     711         7404 :          CALL para_env%retain()
     712         7404 :          CALL mp_para_env_release(ks_env%para_env)
     713         7404 :          ks_env%para_env => para_env
     714              :       END IF
     715       681735 :       IF (PRESENT(blacs_env)) THEN
     716         7404 :          CALL blacs_env%retain()
     717         7404 :          CALL cp_blacs_env_release(ks_env%blacs_env)
     718         7404 :          ks_env%blacs_env => blacs_env
     719              :       END IF
     720       681735 :    END SUBROUTINE set_ks_env
     721              : 
     722              : ! **************************************************************************************************
     723              : !> \brief releases the ks_env (see doc/ReferenceCounting.html)
     724              : !> \param ks_env the ks_env to be released
     725              : !> \par History
     726              : !>      05.2002 created [fawzi]
     727              : !> \author Fawzi Mohamed
     728              : ! **************************************************************************************************
     729         7404 :    SUBROUTINE qs_ks_release(ks_env)
     730              :       TYPE(qs_ks_env_type), INTENT(INOUT)                :: ks_env
     731              : 
     732         7404 :       IF (ASSOCIATED(ks_env%v_hartree_rspace)) THEN
     733         7395 :          CALL ks_env%v_hartree_rspace%release()
     734         7395 :          DEALLOCATE (ks_env%v_hartree_rspace)
     735              :       END IF
     736              : 
     737         7404 :       CALL kpoint_transitional_release(ks_env%matrix_ks)
     738         7404 :       CALL kpoint_transitional_release(ks_env%matrix_ks_im)
     739         7404 :       CALL kpoint_transitional_release(ks_env%matrix_h)
     740         7404 :       CALL kpoint_transitional_release(ks_env%matrix_h_im)
     741         7404 :       CALL kpoint_transitional_release(ks_env%matrix_vxc)
     742         7404 :       CALL kpoint_transitional_release(ks_env%matrix_s)
     743         7404 :       CALL kpoint_transitional_release(ks_env%matrix_w)
     744         7404 :       CALL kpoint_transitional_release(ks_env%kinetic)
     745         7404 :       CALL kpoint_transitional_release(ks_env%matrix_s_RI_aux)
     746              : 
     747         7404 :       IF (ASSOCIATED(ks_env%matrix_p_mp2)) &
     748          194 :          CALL dbcsr_deallocate_matrix_set(ks_env%matrix_p_mp2)
     749         7404 :       IF (ASSOCIATED(ks_env%matrix_p_mp2_admm)) &
     750           36 :          CALL dbcsr_deallocate_matrix_set(ks_env%matrix_p_mp2_admm)
     751         7404 :       IF (ASSOCIATED(ks_env%rho)) THEN
     752         7395 :          CALL qs_rho_release(ks_env%rho)
     753         7395 :          DEALLOCATE (ks_env%rho)
     754              :       END IF
     755         7404 :       IF (ASSOCIATED(ks_env%rho_xc)) THEN
     756         7395 :          CALL qs_rho_release(ks_env%rho_xc)
     757         7395 :          DEALLOCATE (ks_env%rho_xc)
     758              :       END IF
     759         7404 :       IF (ASSOCIATED(ks_env%distribution_2d)) &
     760         7404 :          CALL distribution_2d_release(ks_env%distribution_2d)
     761         7404 :       IF (ASSOCIATED(ks_env%task_list)) &
     762         4345 :          CALL deallocate_task_list(ks_env%task_list)
     763         7404 :       IF (ASSOCIATED(ks_env%task_list_soft)) &
     764          938 :          CALL deallocate_task_list(ks_env%task_list_soft)
     765              : 
     766         7404 :       IF (ASSOCIATED(ks_env%rho_nlcc_g)) THEN
     767           12 :          CALL ks_env%rho_nlcc_g%release()
     768           12 :          DEALLOCATE (ks_env%rho_nlcc_g)
     769              :       END IF
     770         7404 :       IF (ASSOCIATED(ks_env%rho_nlcc)) THEN
     771           12 :          CALL ks_env%rho_nlcc%release()
     772           12 :          DEALLOCATE (ks_env%rho_nlcc)
     773              :       END IF
     774         7404 :       IF (ASSOCIATED(ks_env%rho_core)) THEN
     775         4497 :          CALL ks_env%rho_core%release()
     776         4497 :          DEALLOCATE (ks_env%rho_core)
     777              :       END IF
     778         7404 :       IF (ASSOCIATED(ks_env%vppl)) THEN
     779            8 :          CALL ks_env%vppl%release()
     780            8 :          DEALLOCATE (ks_env%vppl)
     781              :       END IF
     782         7404 :       IF (ASSOCIATED(ks_env%vee)) THEN
     783           16 :          CALL ks_env%vee%release()
     784           16 :          DEALLOCATE (ks_env%vee)
     785              :       END IF
     786         7404 :       IF (ASSOCIATED(ks_env%dbcsr_dist)) THEN
     787         7404 :          CALL dbcsr_distribution_release(ks_env%dbcsr_dist)
     788         7404 :          DEALLOCATE (ks_env%dbcsr_dist)
     789              :       END IF
     790              : 
     791         7404 :       CALL release_neighbor_list_sets(ks_env%sab_orb)
     792         7404 :       CALL release_neighbor_list_sets(ks_env%sac_ae)
     793         7404 :       CALL release_neighbor_list_sets(ks_env%sac_ppl)
     794         7404 :       CALL release_neighbor_list_sets(ks_env%sac_lri)
     795         7404 :       CALL release_neighbor_list_sets(ks_env%sap_ppnl)
     796         7404 :       CALL release_neighbor_list_sets(ks_env%sap_oce)
     797         7404 :       CALL release_neighbor_list_sets(ks_env%sab_se)
     798         7404 :       CALL release_neighbor_list_sets(ks_env%sab_vdw)
     799         7404 :       CALL release_neighbor_list_sets(ks_env%sab_scp)
     800         7404 :       CALL release_neighbor_list_sets(ks_env%sab_tbe)
     801         7404 :       CALL release_neighbor_list_sets(ks_env%sab_xtbe)
     802         7404 :       CALL release_neighbor_list_sets(ks_env%sab_core)
     803         7404 :       CALL release_neighbor_list_sets(ks_env%sab_xb)
     804         7404 :       CALL release_neighbor_list_sets(ks_env%sab_xtb_pp)
     805         7404 :       CALL release_neighbor_list_sets(ks_env%sab_xtb_nonbond)
     806         7404 :       CALL release_neighbor_list_sets(ks_env%sab_all)
     807         7404 :       CALL release_neighbor_list_sets(ks_env%sab_lrc)
     808         7404 :       CALL release_neighbor_list_sets(ks_env%sab_almo)
     809         7404 :       CALL release_neighbor_list_sets(ks_env%sab_kp)
     810         7404 :       CALL release_neighbor_list_sets(ks_env%sab_kp_nosym)
     811         7404 :       IF (ASSOCIATED(ks_env%dft_control)) THEN
     812         7404 :          CALL dft_control_release(ks_env%dft_control)
     813         7404 :          DEALLOCATE (ks_env%dft_control)
     814              :       END IF
     815         7404 :       CALL kpoint_release(ks_env%kpoints)
     816         7404 :       IF (ASSOCIATED(ks_env%subsys)) THEN
     817         7404 :          CALL qs_subsys_release(ks_env%subsys)
     818         7404 :          DEALLOCATE (ks_env%subsys)
     819              :       END IF
     820         7404 :       CALL pw_env_release(ks_env%pw_env)
     821         7404 :       CALL mp_para_env_release(ks_env%para_env)
     822         7404 :       CALL cp_blacs_env_release(ks_env%blacs_env)
     823              : 
     824         7404 :    END SUBROUTINE qs_ks_release
     825              : 
     826              : ! **************************************************************************************************
     827              : !> \brief releases part of the ks_env
     828              : !> \param ks_env the ks_env to be released
     829              : !> \par History
     830              : !>      04.2022 created [JGH]
     831              : ! **************************************************************************************************
     832            9 :    SUBROUTINE qs_ks_part_release(ks_env)
     833              :       TYPE(qs_ks_env_type), INTENT(INOUT)                :: ks_env
     834              : 
     835            9 :       IF (ASSOCIATED(ks_env%v_hartree_rspace)) THEN
     836            9 :          CALL ks_env%v_hartree_rspace%release()
     837            9 :          DEALLOCATE (ks_env%v_hartree_rspace)
     838              :       END IF
     839              : 
     840            9 :       CALL kpoint_transitional_release(ks_env%matrix_h)
     841            9 :       CALL kpoint_transitional_release(ks_env%matrix_h_im)
     842            9 :       CALL kpoint_transitional_release(ks_env%matrix_ks_im)
     843            9 :       CALL kpoint_transitional_release(ks_env%matrix_vxc)
     844            9 :       CALL kpoint_transitional_release(ks_env%matrix_w)
     845            9 :       CALL kpoint_transitional_release(ks_env%kinetic)
     846            9 :       CALL kpoint_transitional_release(ks_env%matrix_s_RI_aux)
     847              : 
     848            9 :       IF (ASSOCIATED(ks_env%matrix_p_mp2)) &
     849            0 :          CALL dbcsr_deallocate_matrix_set(ks_env%matrix_p_mp2)
     850            9 :       IF (ASSOCIATED(ks_env%matrix_p_mp2_admm)) &
     851            0 :          CALL dbcsr_deallocate_matrix_set(ks_env%matrix_p_mp2_admm)
     852            9 :       IF (ASSOCIATED(ks_env%rho)) THEN
     853            9 :          CALL qs_rho_release(ks_env%rho)
     854            9 :          DEALLOCATE (ks_env%rho)
     855              :       END IF
     856            9 :       IF (ASSOCIATED(ks_env%rho_xc)) THEN
     857            9 :          CALL qs_rho_release(ks_env%rho_xc)
     858            9 :          DEALLOCATE (ks_env%rho_xc)
     859              :       END IF
     860            9 :       IF (ASSOCIATED(ks_env%task_list)) &
     861            9 :          CALL deallocate_task_list(ks_env%task_list)
     862            9 :       IF (ASSOCIATED(ks_env%task_list_soft)) &
     863            0 :          CALL deallocate_task_list(ks_env%task_list_soft)
     864              : 
     865            9 :       IF (ASSOCIATED(ks_env%rho_nlcc_g)) THEN
     866            0 :          CALL ks_env%rho_nlcc_g%release()
     867            0 :          DEALLOCATE (ks_env%rho_nlcc_g)
     868              :       END IF
     869            9 :       IF (ASSOCIATED(ks_env%rho_nlcc)) THEN
     870            0 :          CALL ks_env%rho_nlcc%release()
     871            0 :          DEALLOCATE (ks_env%rho_nlcc)
     872              :       END IF
     873            9 :       IF (ASSOCIATED(ks_env%rho_core)) THEN
     874            9 :          CALL ks_env%rho_core%release()
     875            9 :          DEALLOCATE (ks_env%rho_core)
     876              :       END IF
     877            9 :       IF (ASSOCIATED(ks_env%vppl)) THEN
     878            0 :          CALL ks_env%vppl%release()
     879            0 :          DEALLOCATE (ks_env%vppl)
     880              :       END IF
     881            9 :       IF (ASSOCIATED(ks_env%vee)) THEN
     882            0 :          CALL ks_env%vee%release()
     883            0 :          DEALLOCATE (ks_env%vee)
     884              :       END IF
     885              : 
     886            9 :       CALL release_neighbor_list_sets(ks_env%sac_ae)
     887            9 :       CALL release_neighbor_list_sets(ks_env%sac_ppl)
     888            9 :       CALL release_neighbor_list_sets(ks_env%sac_lri)
     889            9 :       CALL release_neighbor_list_sets(ks_env%sap_ppnl)
     890            9 :       CALL release_neighbor_list_sets(ks_env%sap_oce)
     891            9 :       CALL release_neighbor_list_sets(ks_env%sab_se)
     892            9 :       CALL release_neighbor_list_sets(ks_env%sab_vdw)
     893            9 :       CALL release_neighbor_list_sets(ks_env%sab_scp)
     894            9 :       CALL release_neighbor_list_sets(ks_env%sab_tbe)
     895            9 :       CALL release_neighbor_list_sets(ks_env%sab_xtbe)
     896            9 :       CALL release_neighbor_list_sets(ks_env%sab_core)
     897            9 :       CALL release_neighbor_list_sets(ks_env%sab_xb)
     898            9 :       CALL release_neighbor_list_sets(ks_env%sab_xtb_pp)
     899            9 :       CALL release_neighbor_list_sets(ks_env%sab_xtb_nonbond)
     900            9 :       CALL release_neighbor_list_sets(ks_env%sab_all)
     901            9 :       CALL release_neighbor_list_sets(ks_env%sab_lrc)
     902            9 :       CALL release_neighbor_list_sets(ks_env%sab_almo)
     903            9 :       CALL release_neighbor_list_sets(ks_env%sab_kp)
     904            9 :       CALL release_neighbor_list_sets(ks_env%sab_kp_nosym)
     905            9 :       CALL kpoint_release(ks_env%kpoints)
     906            9 :       CALL pw_env_release(ks_env%pw_env, ks_env%para_env)
     907            9 :    END SUBROUTINE qs_ks_part_release
     908              : 
     909              : ! **************************************************************************************************
     910              : !> \brief tells that some of the things relevant to the ks calculation
     911              : !>      did change. has to be called when changes happen otherwise
     912              : !>      the calculation will give wrong results.
     913              : !> \param ks_env the environment that is informed about the changes
     914              : !> \param s_mstruct_changed if true it means that the structure of the
     915              : !>        overlap matrix has changed
     916              : !>        (atoms have moved)
     917              : !> \param rho_changed if true it means that the density has changed
     918              : !> \param potential_changed ...
     919              : !> \param full_reset if true everything has changed
     920              : !> \par History
     921              : !>      4.2002 created [fawzi]
     922              : !>     12.2014 moved from qs_ks_methods, added deallocation of KS-matrices [Ole Schuett]
     923              : !> \author Fawzi Mohamed
     924              : ! **************************************************************************************************
     925       222981 :    SUBROUTINE qs_ks_did_change(ks_env, s_mstruct_changed, rho_changed, &
     926              :                                potential_changed, full_reset)
     927              :       TYPE(qs_ks_env_type), INTENT(INOUT)                :: ks_env
     928              :       LOGICAL, INTENT(in), OPTIONAL                      :: s_mstruct_changed, rho_changed, &
     929              :                                                             potential_changed, full_reset
     930              : 
     931              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_ks_did_change'
     932              : 
     933              :       INTEGER                                            :: handle
     934              :       LOGICAL                                            :: my_mstruct_chg
     935              : 
     936       222981 :       CALL timeset(routineN, handle)
     937       222981 :       my_mstruct_chg = .FALSE.
     938              : 
     939       222981 :       IF (PRESENT(rho_changed)) THEN
     940       199517 :          IF (rho_changed) ks_env%rho_changed = .TRUE.
     941              :       END IF
     942              : 
     943       222981 :       IF (PRESENT(potential_changed)) THEN
     944         3230 :          IF (potential_changed) ks_env%potential_changed = .TRUE.
     945              :       END IF
     946              : 
     947       222981 :       IF (PRESENT(s_mstruct_changed)) THEN
     948        22374 :          IF (s_mstruct_changed) my_mstruct_chg = .TRUE.
     949              :       END IF
     950              : 
     951       222981 :       IF (PRESENT(full_reset)) THEN
     952            0 :          IF (full_reset) THEN
     953            0 :             ks_env%potential_changed = .TRUE.
     954              :             my_mstruct_chg = .TRUE.
     955              :          END IF
     956              :       END IF
     957              : 
     958       222981 :       IF (my_mstruct_chg) THEN
     959        22374 :          ks_env%s_mstruct_changed = .TRUE.
     960              :          ! *** deallocate matrices that will have the wrong structure ***
     961        22374 :          CALL kpoint_transitional_release(ks_env%matrix_ks)
     962              :          !TODO: deallocate imaginary parts as well
     963        22374 :          IF (ks_env%complex_ks) THEN
     964           98 :             CALL kpoint_transitional_release(ks_env%matrix_ks_im)
     965              :          END IF
     966              :          !CALL dbcsr_deallocate_matrix_set(ks_env%matrix_ks_im)
     967              :          !CALL dbcsr_deallocate_matrix_set(ks_env%matrix_ks_aux_fit_im)
     968              :       END IF
     969              : 
     970       222981 :       CALL timestop(handle)
     971              : 
     972       222981 :    END SUBROUTINE qs_ks_did_change
     973              : 
     974            0 : END MODULE qs_ks_types
        

Generated by: LCOV version 2.0-1