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