(git:8917686)
Loading...
Searching...
No Matches
qs_rho_methods.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief methods of the rho structure (defined in qs_rho_types)
10!> \par History
11!> 08.2002 created [fawzi]
12!> 08.2014 kpoints [JGH]
13!> \author Fawzi Mohamed
14! **************************************************************************************************
16 USE admm_types, ONLY: get_admm_env
19 USE cp_dbcsr_api, ONLY: &
21 dbcsr_type_antisymmetric, dbcsr_type_symmetric
26 USE kinds, ONLY: default_string_length,&
27 dp
28 USE kpoint_types, ONLY: get_kpoint_info,&
34 USE pw_env_types, ONLY: pw_env_get,&
36 USE pw_methods, ONLY: pw_axpy,&
37 pw_copy,&
38 pw_scale,&
41 USE pw_types, ONLY: pw_c1d_gs_type,&
51 USE qs_ks_types, ONLY: get_ks_env,&
58 USE qs_rho_types, ONLY: qs_rho_clear,&
64#include "./base/base_uses.f90"
65
66 IMPLICIT NONE
67 PRIVATE
68
69 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
70 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_rho_methods'
71
76
77CONTAINS
78
79! **************************************************************************************************
80!> \brief rebuilds rho (if necessary allocating and initializing it)
81!> \param rho the rho type to rebuild (defaults to qs_env%rho)
82!> \param qs_env the environment to which rho belongs
83!> \param rebuild_ao if it is necessary to rebuild rho_ao. Defaults to true.
84!> \param rebuild_grids if it in necessary to rebuild rho_r and rho_g.
85!> Defaults to false.
86!> \param admm (use aux_fit basis)
87!> \param pw_env_external external plane wave environment
88!> \par History
89!> 11.2002 created replacing qs_rho_create and qs_env_rebuild_rho[fawzi]
90!> \author Fawzi Mohamed
91!> \note
92!> needs updated pw pools, s, s_mstruct and h in qs_env.
93!> The use of p to keep the structure of h (needed for the forces)
94!> is ugly and should be removed.
95!> Change so that it does not allocate a subcomponent if it is not
96!> associated and not requested?
97! **************************************************************************************************
98 SUBROUTINE qs_rho_rebuild(rho, qs_env, rebuild_ao, rebuild_grids, admm, pw_env_external)
99 TYPE(qs_rho_type), INTENT(INOUT) :: rho
100 TYPE(qs_environment_type), POINTER :: qs_env
101 LOGICAL, INTENT(in), OPTIONAL :: rebuild_ao, rebuild_grids, admm
102 TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
103
104 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_rho_rebuild'
105
106 CHARACTER(LEN=default_string_length) :: headline
107 INTEGER :: handle, i, ic, j, nimg, nspins
108 LOGICAL :: do_kpoints, my_admm, my_rebuild_ao, &
109 my_rebuild_grids, rho_ao_is_complex
110 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_r
111 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, rho_ao_im_kp, rho_ao_kp
112 TYPE(dbcsr_type), POINTER :: refmatrix, tmatrix
113 TYPE(dft_control_type), POINTER :: dft_control
114 TYPE(kpoint_type), POINTER :: kpoints
115 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
116 POINTER :: sab_orb
117 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g, tau_g
118 TYPE(pw_c1d_gs_type), DIMENSION(:, :), POINTER :: drho_g
119 TYPE(pw_env_type), POINTER :: pw_env
120 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
121 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, tau_r
122 TYPE(pw_r3d_rs_type), DIMENSION(:, :), POINTER :: drho_r
123 TYPE(pw_r3d_rs_type), POINTER :: rho_r_sccs
124
125 CALL timeset(routinen, handle)
126
127 NULLIFY (pw_env, auxbas_pw_pool, matrix_s_kp, dft_control)
128 NULLIFY (tot_rho_r, rho_ao_kp, rho_r, rho_g, drho_r, drho_g, tau_r, tau_g, rho_ao_im_kp)
129 NULLIFY (rho_r_sccs)
130 NULLIFY (sab_orb)
131 my_rebuild_ao = .true.
132 my_rebuild_grids = .true.
133 my_admm = .false.
134 IF (PRESENT(rebuild_ao)) my_rebuild_ao = rebuild_ao
135 IF (PRESENT(rebuild_grids)) my_rebuild_grids = rebuild_grids
136 IF (PRESENT(admm)) my_admm = admm
137
138 CALL get_qs_env(qs_env, &
139 kpoints=kpoints, &
140 do_kpoints=do_kpoints, &
141 pw_env=pw_env, &
142 dft_control=dft_control)
143 IF (PRESENT(pw_env_external)) THEN
144 pw_env => pw_env_external
145 END IF
146
147 nimg = dft_control%nimages
148
149 IF (my_admm) THEN
150 CALL get_admm_env(qs_env%admm_env, sab_aux_fit=sab_orb, matrix_s_aux_fit_kp=matrix_s_kp)
151 ELSE
152 CALL get_qs_env(qs_env, matrix_s_kp=matrix_s_kp)
153
154 IF (do_kpoints) THEN
155 CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
156 ELSE
157 CALL get_qs_env(qs_env, sab_orb=sab_orb)
158 END IF
159 END IF
160 refmatrix => matrix_s_kp(1, 1)%matrix
161
162 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
163 nspins = dft_control%nspins
164
165 CALL qs_rho_get(rho, &
166 tot_rho_r=tot_rho_r, &
167 rho_ao_kp=rho_ao_kp, &
168 rho_ao_im_kp=rho_ao_im_kp, &
169 rho_r=rho_r, &
170 rho_g=rho_g, &
171 drho_r=drho_r, &
172 drho_g=drho_g, &
173 tau_r=tau_r, &
174 tau_g=tau_g, &
175 rho_r_sccs=rho_r_sccs, &
176 complex_rho_ao=rho_ao_is_complex)
177
178 IF (.NOT. ASSOCIATED(tot_rho_r)) THEN
179 ALLOCATE (tot_rho_r(nspins))
180 tot_rho_r = 0.0_dp
181 CALL qs_rho_set(rho, tot_rho_r=tot_rho_r)
182 END IF
183
184 ! rho_ao
185 IF (my_rebuild_ao .OR. (.NOT. ASSOCIATED(rho_ao_kp))) THEN
186 IF (ASSOCIATED(rho_ao_kp)) THEN
187 CALL dbcsr_deallocate_matrix_set(rho_ao_kp)
188 END IF
189 ! Create a new density matrix set
190 CALL dbcsr_allocate_matrix_set(rho_ao_kp, nspins, nimg)
191 CALL qs_rho_set(rho, rho_ao_kp=rho_ao_kp)
192 DO i = 1, nspins
193 DO ic = 1, nimg
194 IF (nspins > 1) THEN
195 IF (i == 1) THEN
196 headline = "DENSITY MATRIX FOR ALPHA SPIN"
197 ELSE
198 headline = "DENSITY MATRIX FOR BETA SPIN"
199 END IF
200 ELSE
201 headline = "DENSITY MATRIX"
202 END IF
203 ALLOCATE (rho_ao_kp(i, ic)%matrix)
204 tmatrix => rho_ao_kp(i, ic)%matrix
205 CALL dbcsr_create(matrix=tmatrix, template=refmatrix, name=trim(headline), &
206 matrix_type=dbcsr_type_symmetric)
207 CALL cp_dbcsr_alloc_block_from_nbl(tmatrix, sab_orb)
208 CALL dbcsr_set(tmatrix, 0.0_dp)
209 END DO
210 END DO
211 IF (rho_ao_is_complex) THEN
212 IF (ASSOCIATED(rho_ao_im_kp)) THEN
213 CALL dbcsr_deallocate_matrix_set(rho_ao_im_kp)
214 END IF
215 CALL dbcsr_allocate_matrix_set(rho_ao_im_kp, nspins, nimg)
216 CALL qs_rho_set(rho, rho_ao_im_kp=rho_ao_im_kp)
217 DO i = 1, nspins
218 DO ic = 1, nimg
219 IF (nspins > 1) THEN
220 IF (i == 1) THEN
221 headline = "IMAGINARY PART OF DENSITY MATRIX FOR ALPHA SPIN"
222 ELSE
223 headline = "IMAGINARY PART OF DENSITY MATRIX FOR BETA SPIN"
224 END IF
225 ELSE
226 headline = "IMAGINARY PART OF DENSITY MATRIX"
227 END IF
228 ALLOCATE (rho_ao_im_kp(i, ic)%matrix)
229 tmatrix => rho_ao_im_kp(i, ic)%matrix
230 CALL dbcsr_create(matrix=tmatrix, template=refmatrix, name=trim(headline), &
231 matrix_type=dbcsr_type_antisymmetric)
232 CALL cp_dbcsr_alloc_block_from_nbl(tmatrix, sab_orb)
233 CALL dbcsr_set(tmatrix, 0.0_dp)
234 END DO
235 END DO
236 END IF
237 END IF
238
239 ! rho_r
240 IF (my_rebuild_grids .OR. .NOT. ASSOCIATED(rho_r)) THEN
241 IF (ASSOCIATED(rho_r)) THEN
242 DO i = 1, SIZE(rho_r)
243 CALL rho_r(i)%release()
244 END DO
245 DEALLOCATE (rho_r)
246 END IF
247 ALLOCATE (rho_r(nspins))
248 CALL qs_rho_set(rho, rho_r=rho_r)
249 DO i = 1, nspins
250 CALL auxbas_pw_pool%create_pw(rho_r(i))
251 END DO
252 END IF
253
254 ! rho_g
255 IF (my_rebuild_grids .OR. .NOT. ASSOCIATED(rho_g)) THEN
256 IF (ASSOCIATED(rho_g)) THEN
257 DO i = 1, SIZE(rho_g)
258 CALL rho_g(i)%release()
259 END DO
260 DEALLOCATE (rho_g)
261 END IF
262 ALLOCATE (rho_g(nspins))
263 CALL qs_rho_set(rho, rho_g=rho_g)
264 DO i = 1, nspins
265 CALL auxbas_pw_pool%create_pw(rho_g(i))
266 END DO
267 END IF
268
269 ! SCCS
270 IF (dft_control%do_sccs) THEN
271 IF (my_rebuild_grids .OR. (.NOT. ASSOCIATED(rho_r_sccs))) THEN
272 IF (ASSOCIATED(rho_r_sccs)) THEN
273 CALL rho_r_sccs%release()
274 DEALLOCATE (rho_r_sccs)
275 END IF
276 ALLOCATE (rho_r_sccs)
277 CALL qs_rho_set(rho, rho_r_sccs=rho_r_sccs)
278 CALL auxbas_pw_pool%create_pw(rho_r_sccs)
279 CALL pw_zero(rho_r_sccs)
280 END IF
281 END IF
282
283 ! allocate drho_r and drho_g if xc_deriv_collocate
284 IF (dft_control%drho_by_collocation) THEN
285 ! drho_r
286 IF (my_rebuild_grids .OR. .NOT. ASSOCIATED(drho_r)) THEN
287 IF (ASSOCIATED(drho_r)) THEN
288 DO j = 1, SIZE(drho_r, 2)
289 DO i = 1, SIZE(drho_r, 1)
290 CALL drho_r(i, j)%release()
291 END DO
292 END DO
293 DEALLOCATE (drho_r)
294 END IF
295 ALLOCATE (drho_r(3, nspins))
296 CALL qs_rho_set(rho, drho_r=drho_r)
297 DO j = 1, nspins
298 DO i = 1, 3
299 CALL auxbas_pw_pool%create_pw(drho_r(i, j))
300 END DO
301 END DO
302 END IF
303 ! drho_g
304 IF (my_rebuild_grids .OR. .NOT. ASSOCIATED(drho_g)) THEN
305 IF (ASSOCIATED(drho_g)) THEN
306 DO j = 1, SIZE(drho_g, 2)
307 DO i = 1, SIZE(drho_r, 1)
308 CALL drho_g(i, j)%release()
309 END DO
310 END DO
311 DEALLOCATE (drho_g)
312 END IF
313 ALLOCATE (drho_g(3, nspins))
314 CALL qs_rho_set(rho, drho_g=drho_g)
315 DO j = 1, nspins
316 DO i = 1, 3
317 CALL auxbas_pw_pool%create_pw(drho_g(i, j))
318 END DO
319 END DO
320 END IF
321 END IF
322
323 ! allocate tau_r and tau_g if use_kinetic_energy_density
324 IF (dft_control%use_kinetic_energy_density) THEN
325 ! tau_r
326 IF (my_rebuild_grids .OR. .NOT. ASSOCIATED(tau_r)) THEN
327 IF (ASSOCIATED(tau_r)) THEN
328 DO i = 1, SIZE(tau_r)
329 CALL tau_r(i)%release()
330 END DO
331 DEALLOCATE (tau_r)
332 END IF
333 ALLOCATE (tau_r(nspins))
334 CALL qs_rho_set(rho, tau_r=tau_r)
335 DO i = 1, nspins
336 CALL auxbas_pw_pool%create_pw(tau_r(i))
337 END DO
338 END IF
339
340 ! tau_g
341 IF (my_rebuild_grids .OR. .NOT. ASSOCIATED(tau_g)) THEN
342 IF (ASSOCIATED(tau_g)) THEN
343 DO i = 1, SIZE(tau_g)
344 CALL tau_g(i)%release()
345 END DO
346 DEALLOCATE (tau_g)
347 END IF
348 ALLOCATE (tau_g(nspins))
349 CALL qs_rho_set(rho, tau_g=tau_g)
350 DO i = 1, nspins
351 CALL auxbas_pw_pool%create_pw(tau_g(i))
352 END DO
353 END IF
354 END IF ! use_kinetic_energy_density
355
356 CALL timestop(handle)
357
358 END SUBROUTINE qs_rho_rebuild
359
360! **************************************************************************************************
361!> \brief updates rho_r and rho_g to the rho%rho_ao.
362!> if use_kinetic_energy_density also computes tau_r and tau_g
363!> this works for all ground state and ground state response methods
364!> \param rho_struct the rho structure that should be updated
365!> \param qs_env the qs_env rho_struct refers to
366!> the integrated charge in r space
367!> \param rho_xc_external ...
368!> \param local_rho_set ...
369!> \param task_list_external external task list
370!> \param task_list_external_soft external task list (soft_version)
371!> \param pw_env_external external plane wave environment
372!> \param para_env_external external MPI environment
373!> \par History
374!> 08.2002 created [fawzi]
375!> \author Fawzi Mohamed
376! **************************************************************************************************
377 SUBROUTINE qs_rho_update_rho(rho_struct, qs_env, rho_xc_external, local_rho_set, &
378 task_list_external, task_list_external_soft, &
379 pw_env_external, para_env_external)
380 TYPE(qs_rho_type), INTENT(INOUT) :: rho_struct
381 TYPE(qs_environment_type), POINTER :: qs_env
382 TYPE(qs_rho_type), OPTIONAL, POINTER :: rho_xc_external
383 TYPE(local_rho_type), OPTIONAL, POINTER :: local_rho_set
384 TYPE(task_list_type), OPTIONAL, POINTER :: task_list_external, &
385 task_list_external_soft
386 TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
387 TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_external
388
389 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
390 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
391 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
392 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
393 TYPE(dft_control_type), POINTER :: dft_control
394 TYPE(harris_type), POINTER :: harris_env
395 TYPE(kpoint_type), POINTER :: kpoints
396 TYPE(lri_density_type), POINTER :: lri_density
397 TYPE(lri_environment_type), POINTER :: lri_env
398 TYPE(mp_para_env_type), POINTER :: para_env
399 TYPE(qs_ks_env_type), POINTER :: ks_env
400
401 CALL get_qs_env(qs_env, dft_control=dft_control, &
402 atomic_kind_set=atomic_kind_set, &
403 para_env=para_env)
404 IF (PRESENT(para_env_external)) para_env => para_env_external
405
406 IF (qs_env%harris_method) THEN
407 CALL get_qs_env(qs_env, harris_env=harris_env)
408 CALL calculate_harris_density(qs_env, harris_env%rhoin, rho_struct)
409 CALL qs_rho_set(rho_struct, rho_r_valid=.true., rho_g_valid=.true.)
410
411 ELSE IF (dft_control%qs_control%semi_empirical .OR. &
412 dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb) THEN
413
414 CALL qs_rho_set(rho_struct, rho_r_valid=.false., rho_g_valid=.false.)
415
416 ELSE IF (dft_control%qs_control%lrigpw) THEN
417 cpassert(.NOT. dft_control%use_kinetic_energy_density)
418 cpassert(.NOT. dft_control%drho_by_collocation)
419 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
420 CALL get_qs_env(qs_env, ks_env=ks_env)
421 CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
422 CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
423 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
424 CALL calculate_lri_densities(lri_env, lri_density, qs_env, rho_ao_kp, cell_to_index, &
425 lri_rho_struct=rho_struct, &
426 atomic_kind_set=atomic_kind_set, &
427 para_env=para_env, &
428 response_density=.false.)
429 CALL set_qs_env(qs_env, lri_density=lri_density)
430 CALL qs_rho_set(rho_struct, rho_r_valid=.true., rho_g_valid=.true.)
431
432 ELSE IF (dft_control%qs_control%rigpw) THEN
433 cpassert(.NOT. dft_control%use_kinetic_energy_density)
434 cpassert(.NOT. dft_control%drho_by_collocation)
435 CALL get_qs_env(qs_env, lri_env=lri_env)
436 CALL qs_rho_get(rho_struct, rho_ao=rho_ao)
437 CALL calculate_ri_densities(lri_env, qs_env, rho_ao, &
438 lri_rho_struct=rho_struct, &
439 atomic_kind_set=atomic_kind_set, &
440 para_env=para_env)
441 CALL qs_rho_set(rho_struct, rho_r_valid=.true., rho_g_valid=.true.)
442
443 ELSE
444 CALL qs_rho_update_rho_low(rho_struct=rho_struct, qs_env=qs_env, &
445 rho_xc_external=rho_xc_external, &
446 local_rho_set=local_rho_set, &
447 task_list_external=task_list_external, &
448 task_list_external_soft=task_list_external_soft, &
449 pw_env_external=pw_env_external, &
450 para_env_external=para_env_external)
451
452 END IF
453
454 END SUBROUTINE qs_rho_update_rho
455
456! **************************************************************************************************
457!> \brief updates rho_r and rho_g to the rho%rho_ao.
458!> if use_kinetic_energy_density also computes tau_r and tau_g
459!> \param rho_struct the rho structure that should be updated
460!> \param qs_env the qs_env rho_struct refers to
461!> the integrated charge in r space
462!> \param rho_xc_external rho structure for GAPW_XC
463!> \param local_rho_set ...
464!> \param pw_env_external external plane wave environment
465!> \param task_list_external external task list (use for default and GAPW)
466!> \param task_list_external_soft external task list (soft density for GAPW_XC)
467!> \param para_env_external ...
468!> \par History
469!> 08.2002 created [fawzi]
470!> \author Fawzi Mohamed
471! **************************************************************************************************
472 SUBROUTINE qs_rho_update_rho_low(rho_struct, qs_env, rho_xc_external, &
473 local_rho_set, pw_env_external, &
474 task_list_external, task_list_external_soft, &
475 para_env_external)
476 TYPE(qs_rho_type), INTENT(INOUT) :: rho_struct
477 TYPE(qs_environment_type), POINTER :: qs_env
478 TYPE(qs_rho_type), OPTIONAL, POINTER :: rho_xc_external
479 TYPE(local_rho_type), OPTIONAL, POINTER :: local_rho_set
480 TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
481 TYPE(task_list_type), OPTIONAL, POINTER :: task_list_external, &
482 task_list_external_soft
483 TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_external
484
485 CHARACTER(len=*), PARAMETER :: routinen = 'qs_rho_update_rho_low'
486
487 INTEGER :: handle, img, ispin, nimg, nspins
488 LOGICAL :: gapw, gapw_xc
489 REAL(kind=dp) :: dum
490 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_r, tot_rho_r_xc
491 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
492 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
493 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp, rho_xc_ao
494 TYPE(dft_control_type), POINTER :: dft_control
495 TYPE(mp_para_env_type), POINTER :: para_env
496 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
497 POINTER :: sab
498 TYPE(oce_matrix_type), POINTER :: oce
499 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g, rho_xc_g, tau_g, tau_xc_g
500 TYPE(pw_c1d_gs_type), DIMENSION(:, :), POINTER :: drho_g, drho_xc_g
501 TYPE(pw_env_type), POINTER :: pw_env
502 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, rho_xc_r, tau_r, tau_xc_r
503 TYPE(pw_r3d_rs_type), DIMENSION(:, :), POINTER :: drho_r, drho_xc_r
504 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
505 TYPE(qs_ks_env_type), POINTER :: ks_env
506 TYPE(qs_rho_type), POINTER :: rho_xc
507 TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho_atom_set
508 TYPE(task_list_type), POINTER :: task_list
509
510 CALL timeset(routinen, handle)
511
512 NULLIFY (dft_control, rho_xc, ks_env, rho_ao, rho_r, rho_g, drho_r, drho_g, tau_r, tau_g)
513 NULLIFY (rho_xc_ao, rho_xc_g, rho_xc_r, drho_xc_g, tau_xc_r, tau_xc_g, tot_rho_r, tot_rho_r_xc)
514 NULLIFY (para_env, pw_env, atomic_kind_set)
515
516 CALL get_qs_env(qs_env, &
517 ks_env=ks_env, &
518 dft_control=dft_control, &
519 atomic_kind_set=atomic_kind_set)
520
521 CALL qs_rho_get(rho_struct, &
522 rho_r=rho_r, &
523 rho_g=rho_g, &
524 tot_rho_r=tot_rho_r, &
525 drho_r=drho_r, &
526 drho_g=drho_g, &
527 tau_r=tau_r, &
528 tau_g=tau_g)
529
530 CALL get_qs_env(qs_env, task_list=task_list, &
531 para_env=para_env, pw_env=pw_env)
532 IF (PRESENT(pw_env_external)) pw_env => pw_env_external
533 IF (PRESENT(task_list_external)) task_list => task_list_external
534 IF (PRESENT(para_env_external)) para_env => para_env_external
535
536 nspins = dft_control%nspins
537 nimg = dft_control%nimages
538 gapw = dft_control%qs_control%gapw
539 gapw_xc = dft_control%qs_control%gapw_xc
540
541 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
542 DO ispin = 1, nspins
543 rho_ao => rho_ao_kp(ispin, :)
544 CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
545 rho=rho_r(ispin), &
546 rho_gspace=rho_g(ispin), &
547 total_rho=tot_rho_r(ispin), &
548 ks_env=ks_env, soft_valid=gapw, &
549 task_list_external=task_list_external, &
550 pw_env_external=pw_env_external)
551 END DO
552 CALL qs_rho_set(rho_struct, rho_r_valid=.true., rho_g_valid=.true.)
553
554 IF (gapw_xc) THEN
555 IF (PRESENT(rho_xc_external)) THEN
556 rho_xc => rho_xc_external
557 ELSE
558 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_xc)
559 END IF
560 CALL qs_rho_get(rho_xc, &
561 rho_ao_kp=rho_xc_ao, &
562 rho_r=rho_xc_r, &
563 rho_g=rho_xc_g, &
564 tot_rho_r=tot_rho_r_xc)
565 ! copy rho_ao into rho_xc_ao
566 DO ispin = 1, nspins
567 DO img = 1, nimg
568 CALL dbcsr_copy(rho_xc_ao(ispin, img)%matrix, rho_ao_kp(ispin, img)%matrix)
569 END DO
570 END DO
571 DO ispin = 1, nspins
572 rho_ao => rho_xc_ao(ispin, :)
573 CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
574 rho=rho_xc_r(ispin), &
575 rho_gspace=rho_xc_g(ispin), &
576 total_rho=tot_rho_r_xc(ispin), &
577 ks_env=ks_env, soft_valid=gapw_xc, &
578 task_list_external=task_list_external_soft, &
579 pw_env_external=pw_env_external)
580 END DO
581 CALL qs_rho_set(rho_xc, rho_r_valid=.true., rho_g_valid=.true.)
582 END IF
583
584 ! GAPW o GAPW_XC require the calculation of hard and soft local densities
585 IF (gapw .OR. gapw_xc) THEN
586 CALL get_qs_env(qs_env=qs_env, &
587 rho_atom_set=rho_atom_set, &
588 qs_kind_set=qs_kind_set, &
589 oce=oce, sab_orb=sab)
590 IF (PRESENT(local_rho_set)) rho_atom_set => local_rho_set%rho_atom_set
591 cpassert(ASSOCIATED(rho_atom_set))
592 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
593 CALL calculate_rho_atom_coeff(qs_env, rho_ao_kp, rho_atom_set, qs_kind_set, oce, sab, para_env)
594 END IF
595
596 IF (.NOT. gapw_xc) THEN
597 ! if needed compute also the gradient of the density
598 IF (dft_control%drho_by_collocation) THEN
599 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
600 cpassert(.NOT. PRESENT(task_list_external))
601 DO ispin = 1, nspins
602 rho_ao => rho_ao_kp(ispin, :)
603 CALL calculate_drho_elec(matrix_p_kp=rho_ao, &
604 drho=drho_r(:, ispin), &
605 drho_gspace=drho_g(:, ispin), &
606 qs_env=qs_env, soft_valid=gapw)
607 END DO
608 CALL qs_rho_set(rho_struct, drho_r_valid=.true., drho_g_valid=.true.)
609 END IF
610 ! if needed compute also the kinetic energy density
611 IF (dft_control%use_kinetic_energy_density) THEN
612 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
613 DO ispin = 1, nspins
614 rho_ao => rho_ao_kp(ispin, :)
615 CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
616 rho=tau_r(ispin), &
617 rho_gspace=tau_g(ispin), &
618 total_rho=dum, & ! presumably not meaningful
619 ks_env=ks_env, soft_valid=gapw, &
620 compute_tau=.true., &
621 task_list_external=task_list_external, &
622 pw_env_external=pw_env_external)
623 END DO
624 CALL qs_rho_set(rho_struct, tau_r_valid=.true., tau_g_valid=.true.)
625 END IF
626 ELSE
627 CALL qs_rho_get(rho_xc, &
628 drho_r=drho_xc_r, &
629 drho_g=drho_xc_g, &
630 tau_r=tau_xc_r, &
631 tau_g=tau_xc_g)
632 ! if needed compute also the gradient of the density
633 IF (dft_control%drho_by_collocation) THEN
634 cpassert(.NOT. PRESENT(task_list_external))
635 DO ispin = 1, nspins
636 rho_ao => rho_xc_ao(ispin, :)
637 CALL calculate_drho_elec(matrix_p_kp=rho_ao, &
638 drho=drho_xc_r(:, ispin), &
639 drho_gspace=drho_xc_g(:, ispin), &
640 qs_env=qs_env, soft_valid=gapw_xc)
641 END DO
642 CALL qs_rho_set(rho_xc, drho_r_valid=.true., drho_g_valid=.true.)
643 END IF
644 ! if needed compute also the kinetic energy density
645 IF (dft_control%use_kinetic_energy_density) THEN
646 DO ispin = 1, nspins
647 rho_ao => rho_xc_ao(ispin, :)
648 CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
649 rho=tau_xc_r(ispin), &
650 rho_gspace=tau_xc_g(ispin), &
651 ks_env=ks_env, soft_valid=gapw_xc, &
652 compute_tau=.true., &
653 task_list_external=task_list_external_soft, &
654 pw_env_external=pw_env_external)
655 END DO
656 CALL qs_rho_set(rho_xc, tau_r_valid=.true., tau_g_valid=.true.)
657 END IF
658 END IF
659
660 CALL timestop(handle)
661
662 END SUBROUTINE qs_rho_update_rho_low
663
664! **************************************************************************************************
665!> \brief updates rho_r and rho_g to the rho%rho_ao.
666!> if use_kinetic_energy_density also computes tau_r and tau_g
667!> \param rho_struct the rho structure that should be updated
668!> \param qs_env the qs_env rho_struct refers to
669!> the integrated charge in r space
670!> \param pw_env_external external plane wave environment
671!> \param task_list_external external task list
672!> \param para_env_external ...
673!> \param tddfpt_lri_env ...
674!> \param tddfpt_lri_density ...
675!> \par History
676!> 08.2002 created [fawzi]
677!> \author Fawzi Mohamed
678! **************************************************************************************************
679 SUBROUTINE qs_rho_update_tddfpt(rho_struct, qs_env, pw_env_external, task_list_external, &
680 para_env_external, tddfpt_lri_env, tddfpt_lri_density)
681 TYPE(qs_rho_type), INTENT(INOUT) :: rho_struct
682 TYPE(qs_environment_type), POINTER :: qs_env
683 TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
684 TYPE(task_list_type), OPTIONAL, POINTER :: task_list_external
685 TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_external
686 TYPE(lri_environment_type), OPTIONAL, POINTER :: tddfpt_lri_env
687 TYPE(lri_density_type), OPTIONAL, POINTER :: tddfpt_lri_density
688
689 CHARACTER(len=*), PARAMETER :: routinen = 'qs_rho_update_tddfpt'
690
691 INTEGER :: handle, ispin, nspins
692 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
693 LOGICAL :: lri_response
694 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_r
695 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
696 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
697 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
698 TYPE(dft_control_type), POINTER :: dft_control
699 TYPE(kpoint_type), POINTER :: kpoints
700 TYPE(mp_para_env_type), POINTER :: para_env
701 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
702 TYPE(pw_env_type), POINTER :: pw_env
703 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
704 TYPE(qs_ks_env_type), POINTER :: ks_env
705 TYPE(task_list_type), POINTER :: task_list
706
707 CALL timeset(routinen, handle)
708
709 CALL get_qs_env(qs_env, &
710 ks_env=ks_env, &
711 dft_control=dft_control, &
712 atomic_kind_set=atomic_kind_set, &
713 task_list=task_list, &
714 para_env=para_env, &
715 pw_env=pw_env)
716 IF (PRESENT(pw_env_external)) pw_env => pw_env_external
717 IF (PRESENT(task_list_external)) task_list => task_list_external
718 IF (PRESENT(para_env_external)) para_env => para_env_external
719
720 CALL qs_rho_get(rho_struct, &
721 rho_r=rho_r, &
722 rho_g=rho_g, &
723 tot_rho_r=tot_rho_r)
724
725 nspins = dft_control%nspins
726
727 lri_response = PRESENT(tddfpt_lri_env)
728 IF (lri_response) THEN
729 cpassert(PRESENT(tddfpt_lri_density))
730 END IF
731
732 cpassert(.NOT. dft_control%drho_by_collocation)
733 cpassert(.NOT. dft_control%use_kinetic_energy_density)
734 cpassert(.NOT. dft_control%qs_control%gapw)
735 cpassert(.NOT. dft_control%qs_control%gapw_xc)
736
737 IF (lri_response) THEN
738 CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
739 CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
740 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
741 CALL calculate_lri_densities(tddfpt_lri_env, tddfpt_lri_density, qs_env, rho_ao_kp, cell_to_index, &
742 lri_rho_struct=rho_struct, &
743 atomic_kind_set=atomic_kind_set, &
744 para_env=para_env, &
745 response_density=lri_response)
746 CALL qs_rho_set(rho_struct, rho_r_valid=.true., rho_g_valid=.true.)
747 ELSE
748 CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
749 DO ispin = 1, nspins
750 rho_ao => rho_ao_kp(ispin, :)
751 CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
752 rho=rho_r(ispin), &
753 rho_gspace=rho_g(ispin), &
754 total_rho=tot_rho_r(ispin), &
755 ks_env=ks_env, &
756 task_list_external=task_list_external, &
757 pw_env_external=pw_env_external)
758 END DO
759 CALL qs_rho_set(rho_struct, rho_r_valid=.true., rho_g_valid=.true.)
760 END IF
761
762 CALL timestop(handle)
763
764 END SUBROUTINE qs_rho_update_tddfpt
765
766! **************************************************************************************************
767!> \brief Allocate a density structure and fill it with data from an input structure
768!> SIZE(rho_input) == mspin == 1 direct copy
769!> SIZE(rho_input) == mspin == 2 direct copy of alpha and beta spin
770!> SIZE(rho_input) == 1 AND mspin == 2 copy rho/2 into alpha and beta spin
771!> \param rho_input ...
772!> \param rho_output ...
773!> \param auxbas_pw_pool ...
774!> \param mspin ...
775! **************************************************************************************************
776 SUBROUTINE qs_rho_copy(rho_input, rho_output, auxbas_pw_pool, mspin)
777
778 TYPE(qs_rho_type), INTENT(IN) :: rho_input
779 TYPE(qs_rho_type), INTENT(INOUT) :: rho_output
780 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
781 INTEGER, INTENT(IN) :: mspin
782
783 CHARACTER(len=*), PARAMETER :: routinen = 'qs_rho_copy'
784
785 INTEGER :: handle, i, j, nspins
786 LOGICAL :: complex_rho_ao, drho_g_valid_in, drho_r_valid_in, rho_g_valid_in, rho_r_valid_in, &
787 soft_valid_in, tau_g_valid_in, tau_r_valid_in
788 REAL(kind=dp) :: ospin
789 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_g_in, tot_rho_g_out, &
790 tot_rho_r_in, tot_rho_r_out
791 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao_im_in, rho_ao_im_out, rho_ao_in, &
792 rho_ao_out
793 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp_in
794 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_in, rho_g_out, tau_g_in, tau_g_out
795 TYPE(pw_c1d_gs_type), DIMENSION(:, :), POINTER :: drho_g_in, drho_g_out
796 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r_in, rho_r_out, tau_r_in, tau_r_out
797 TYPE(pw_r3d_rs_type), DIMENSION(:, :), POINTER :: drho_r_in, drho_r_out
798 TYPE(pw_r3d_rs_type), POINTER :: rho_r_sccs_in, rho_r_sccs_out
799
800 CALL timeset(routinen, handle)
801
802 cpassert(mspin == 1 .OR. mspin == 2)
803 ospin = 1._dp/real(mspin, kind=dp)
804
805 CALL qs_rho_clear(rho_output)
806
807 NULLIFY (rho_ao_in, rho_ao_kp_in, rho_ao_im_in, rho_r_in, rho_g_in, drho_r_in, &
808 drho_g_in, tau_r_in, tau_g_in, tot_rho_r_in, tot_rho_g_in, rho_r_sccs_in)
809
810 CALL qs_rho_get(rho_input, &
811 rho_ao=rho_ao_in, &
812 rho_ao_kp=rho_ao_kp_in, &
813 rho_ao_im=rho_ao_im_in, &
814 rho_r=rho_r_in, &
815 rho_g=rho_g_in, &
816 drho_r=drho_r_in, &
817 drho_g=drho_g_in, &
818 tau_r=tau_r_in, &
819 tau_g=tau_g_in, &
820 tot_rho_r=tot_rho_r_in, &
821 tot_rho_g=tot_rho_g_in, &
822 rho_g_valid=rho_g_valid_in, &
823 rho_r_valid=rho_r_valid_in, &
824 drho_g_valid=drho_g_valid_in, &
825 drho_r_valid=drho_r_valid_in, &
826 tau_r_valid=tau_r_valid_in, &
827 tau_g_valid=tau_g_valid_in, &
828 rho_r_sccs=rho_r_sccs_in, &
829 soft_valid=soft_valid_in, &
830 complex_rho_ao=complex_rho_ao)
831
832 NULLIFY (rho_ao_out, rho_ao_im_out, rho_r_out, rho_g_out, drho_r_out, &
833 drho_g_out, tau_r_out, tau_g_out, tot_rho_r_out, tot_rho_g_out, rho_r_sccs_out)
834 ! rho_ao
835 IF (ASSOCIATED(rho_ao_in)) THEN
836 nspins = SIZE(rho_ao_in)
837 cpassert(mspin >= nspins)
838 CALL dbcsr_allocate_matrix_set(rho_ao_out, mspin)
839 CALL qs_rho_set(rho_output, rho_ao=rho_ao_out)
840 IF (mspin > nspins) THEN
841 DO i = 1, mspin
842 ALLOCATE (rho_ao_out(i)%matrix)
843 CALL dbcsr_copy(rho_ao_out(i)%matrix, rho_ao_in(1)%matrix, name="RHO copy")
844 CALL dbcsr_scale(rho_ao_out(i)%matrix, ospin)
845 END DO
846 ELSE
847 DO i = 1, nspins
848 ALLOCATE (rho_ao_out(i)%matrix)
849 CALL dbcsr_copy(rho_ao_out(i)%matrix, rho_ao_in(i)%matrix, name="RHO copy")
850 END DO
851 END IF
852 END IF
853
854 ! rho_ao_kp
855 ! only for non-kp, we could probably just copy this pointer, should work also for non-kp?
856 !IF (ASSOCIATED(rho_ao_kp_in)) THEN
857 ! CPABORT("Copy not available")
858 !END IF
859
860 ! rho_ao_im
861 IF (ASSOCIATED(rho_ao_im_in)) THEN
862 nspins = SIZE(rho_ao_im_in)
863 cpassert(mspin >= nspins)
864 CALL dbcsr_allocate_matrix_set(rho_ao_im_out, mspin)
865 CALL qs_rho_set(rho_output, rho_ao_im=rho_ao_im_out)
866 IF (mspin > nspins) THEN
867 DO i = 1, mspin
868 ALLOCATE (rho_ao_im_out(i)%matrix)
869 CALL dbcsr_copy(rho_ao_im_out(i)%matrix, rho_ao_im_in(1)%matrix, name="RHO copy")
870 CALL dbcsr_scale(rho_ao_im_out(i)%matrix, ospin)
871 END DO
872 ELSE
873 DO i = 1, nspins
874 ALLOCATE (rho_ao_im_out(i)%matrix)
875 CALL dbcsr_copy(rho_ao_im_out(i)%matrix, rho_ao_im_in(i)%matrix, name="RHO copy")
876 END DO
877 END IF
878 END IF
879
880 ! rho_r
881 IF (ASSOCIATED(rho_r_in)) THEN
882 nspins = SIZE(rho_r_in)
883 cpassert(mspin >= nspins)
884 ALLOCATE (rho_r_out(mspin))
885 CALL qs_rho_set(rho_output, rho_r=rho_r_out)
886 IF (mspin > nspins) THEN
887 DO i = 1, mspin
888 CALL auxbas_pw_pool%create_pw(rho_r_out(i))
889 CALL pw_copy(rho_r_in(1), rho_r_out(i))
890 CALL pw_scale(rho_r_out(i), ospin)
891 END DO
892 ELSE
893 DO i = 1, nspins
894 CALL auxbas_pw_pool%create_pw(rho_r_out(i))
895 CALL pw_copy(rho_r_in(i), rho_r_out(i))
896 END DO
897 END IF
898 END IF
899
900 ! rho_g
901 IF (ASSOCIATED(rho_g_in)) THEN
902 nspins = SIZE(rho_g_in)
903 cpassert(mspin >= nspins)
904 ALLOCATE (rho_g_out(mspin))
905 CALL qs_rho_set(rho_output, rho_g=rho_g_out)
906 IF (mspin > nspins) THEN
907 DO i = 1, mspin
908 CALL auxbas_pw_pool%create_pw(rho_g_out(i))
909 CALL pw_copy(rho_g_in(1), rho_g_out(i))
910 CALL pw_scale(rho_g_out(i), ospin)
911 END DO
912 ELSE
913 DO i = 1, nspins
914 CALL auxbas_pw_pool%create_pw(rho_g_out(i))
915 CALL pw_copy(rho_g_in(i), rho_g_out(i))
916 END DO
917 END IF
918 END IF
919
920 ! SCCS
921 IF (ASSOCIATED(rho_r_sccs_in)) THEN
922 CALL qs_rho_set(rho_output, rho_r_sccs=rho_r_sccs_out)
923 CALL auxbas_pw_pool%create_pw(rho_r_sccs_out)
924 CALL pw_copy(rho_r_sccs_in, rho_r_sccs_out)
925 END IF
926
927 ! drho_r
928 IF (ASSOCIATED(drho_r_in)) THEN
929 nspins = SIZE(drho_r_in)
930 cpassert(mspin >= nspins)
931 ALLOCATE (drho_r_out(3, mspin))
932 CALL qs_rho_set(rho_output, drho_r=drho_r_out)
933 IF (mspin > nspins) THEN
934 DO j = 1, mspin
935 DO i = 1, 3
936 CALL auxbas_pw_pool%create_pw(drho_r_out(i, j))
937 CALL pw_copy(drho_r_in(i, 1), drho_r_out(i, j))
938 CALL pw_scale(drho_r_out(i, j), ospin)
939 END DO
940 END DO
941 ELSE
942 DO j = 1, nspins
943 DO i = 1, 3
944 CALL auxbas_pw_pool%create_pw(drho_r_out(i, j))
945 CALL pw_copy(drho_r_in(i, j), drho_r_out(i, j))
946 END DO
947 END DO
948 END IF
949 END IF
950
951 ! drho_g
952 IF (ASSOCIATED(drho_g_in)) THEN
953 nspins = SIZE(drho_g_in)
954 cpassert(mspin >= nspins)
955 ALLOCATE (drho_g_out(3, mspin))
956 CALL qs_rho_set(rho_output, drho_g=drho_g_out)
957 IF (mspin > nspins) THEN
958 DO j = 1, mspin
959 DO i = 1, 3
960 CALL auxbas_pw_pool%create_pw(drho_g_out(i, j))
961 CALL pw_copy(drho_g_in(i, 1), drho_g_out(i, j))
962 CALL pw_scale(drho_g_out(i, j), ospin)
963 END DO
964 END DO
965 ELSE
966 DO j = 1, nspins
967 DO i = 1, 3
968 CALL auxbas_pw_pool%create_pw(drho_g_out(i, j))
969 CALL pw_copy(drho_g_in(i, j), drho_g_out(i, j))
970 END DO
971 END DO
972 END IF
973 END IF
974
975 ! tau_r
976 IF (ASSOCIATED(tau_r_in)) THEN
977 nspins = SIZE(tau_r_in)
978 cpassert(mspin >= nspins)
979 ALLOCATE (tau_r_out(mspin))
980 CALL qs_rho_set(rho_output, tau_r=tau_r_out)
981 IF (mspin > nspins) THEN
982 DO i = 1, mspin
983 CALL auxbas_pw_pool%create_pw(tau_r_out(i))
984 CALL pw_copy(tau_r_in(1), tau_r_out(i))
985 CALL pw_scale(tau_r_out(i), ospin)
986 END DO
987 ELSE
988 DO i = 1, nspins
989 CALL auxbas_pw_pool%create_pw(tau_r_out(i))
990 CALL pw_copy(tau_r_in(i), tau_r_out(i))
991 END DO
992 END IF
993 END IF
994
995 ! tau_g
996 IF (ASSOCIATED(tau_g_in)) THEN
997 nspins = SIZE(tau_g_in)
998 cpassert(mspin >= nspins)
999 ALLOCATE (tau_g_out(mspin))
1000 CALL qs_rho_set(rho_output, tau_g=tau_g_out)
1001 IF (mspin > nspins) THEN
1002 DO i = 1, mspin
1003 CALL auxbas_pw_pool%create_pw(tau_g_out(i))
1004 CALL pw_copy(tau_g_in(1), tau_g_out(i))
1005 CALL pw_scale(tau_g_out(i), ospin)
1006 END DO
1007 ELSE
1008 DO i = 1, nspins
1009 CALL auxbas_pw_pool%create_pw(tau_g_out(i))
1010 CALL pw_copy(tau_g_in(i), tau_g_out(i))
1011 END DO
1012 END IF
1013 END IF
1014
1015 ! tot_rho_r
1016 IF (ASSOCIATED(tot_rho_r_in)) THEN
1017 nspins = SIZE(tot_rho_r_in)
1018 cpassert(mspin >= nspins)
1019 ALLOCATE (tot_rho_r_out(mspin))
1020 CALL qs_rho_set(rho_output, tot_rho_r=tot_rho_r_out)
1021 IF (mspin > nspins) THEN
1022 DO i = 1, mspin
1023 tot_rho_r_out(i) = tot_rho_r_in(1)*ospin
1024 END DO
1025 ELSE
1026 DO i = 1, nspins
1027 tot_rho_r_out(i) = tot_rho_r_in(i)
1028 END DO
1029 END IF
1030 END IF
1031
1032 ! tot_rho_g
1033 IF (ASSOCIATED(tot_rho_g_in)) THEN
1034 nspins = SIZE(tot_rho_g_in)
1035 cpassert(mspin >= nspins)
1036 ALLOCATE (tot_rho_g_out(mspin))
1037 CALL qs_rho_set(rho_output, tot_rho_g=tot_rho_g_out)
1038 IF (mspin > nspins) THEN
1039 DO i = 1, mspin
1040 tot_rho_g_out(i) = tot_rho_g_in(1)*ospin
1041 END DO
1042 ELSE
1043 DO i = 1, nspins
1044 tot_rho_g_out(i) = tot_rho_g_in(i)
1045 END DO
1046 END IF
1047 END IF
1048
1049 CALL qs_rho_set(rho_output, &
1050 rho_g_valid=rho_g_valid_in, &
1051 rho_r_valid=rho_r_valid_in, &
1052 drho_g_valid=drho_g_valid_in, &
1053 drho_r_valid=drho_r_valid_in, &
1054 tau_r_valid=tau_r_valid_in, &
1055 tau_g_valid=tau_g_valid_in, &
1056 soft_valid=soft_valid_in, &
1057 complex_rho_ao=complex_rho_ao)
1058
1059 CALL timestop(handle)
1060
1061 END SUBROUTINE qs_rho_copy
1062
1063! **************************************************************************************************
1064!> \brief rhoa(2) = alpha*rhoa(2)+beta*rhob(1)
1065!> \param rhoa ...
1066!> \param rhob ...
1067!> \param alpha ...
1068!> \param beta ...
1069! **************************************************************************************************
1070 SUBROUTINE qs_rho_scale_and_add_b(rhoa, rhob, alpha, beta)
1071
1072 TYPE(qs_rho_type), INTENT(IN) :: rhoa, rhob
1073 REAL(kind=dp), INTENT(IN) :: alpha, beta
1074
1075 CHARACTER(len=*), PARAMETER :: routinen = 'qs_rho_scale_and_add_b'
1076
1077 INTEGER :: handle
1078 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_g_a, tot_rho_g_b, tot_rho_r_a, &
1079 tot_rho_r_b
1080 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao_a, rho_ao_b, rho_ao_im_a, &
1081 rho_ao_im_b
1082 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_a, rho_g_b, tau_g_a, tau_g_b
1083 TYPE(pw_c1d_gs_type), DIMENSION(:, :), POINTER :: drho_g_a, drho_g_b
1084 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r_a, rho_r_b, tau_r_a, tau_r_b
1085 TYPE(pw_r3d_rs_type), DIMENSION(:, :), POINTER :: drho_r_a, drho_r_b
1086 TYPE(pw_r3d_rs_type), POINTER :: rho_r_sccs_a, rho_r_sccs_b
1087
1088 CALL timeset(routinen, handle)
1089
1090 NULLIFY (rho_ao_a, rho_ao_im_a, rho_r_a, rho_g_a, drho_r_a, &
1091 drho_g_a, tau_r_a, tau_g_a, tot_rho_r_a, tot_rho_g_a, rho_r_sccs_a)
1092
1093 CALL qs_rho_get(rhoa, &
1094 rho_ao=rho_ao_a, &
1095 rho_ao_im=rho_ao_im_a, &
1096 rho_r=rho_r_a, &
1097 rho_g=rho_g_a, &
1098 drho_r=drho_r_a, &
1099 drho_g=drho_g_a, &
1100 tau_r=tau_r_a, &
1101 tau_g=tau_g_a, &
1102 tot_rho_r=tot_rho_r_a, &
1103 tot_rho_g=tot_rho_g_a, &
1104 rho_r_sccs=rho_r_sccs_a)
1105
1106 NULLIFY (rho_ao_b, rho_ao_im_b, rho_r_b, rho_g_b, drho_r_b, &
1107 drho_g_b, tau_r_b, tau_g_b, tot_rho_r_b, tot_rho_g_b, rho_r_sccs_b)
1108
1109 CALL qs_rho_get(rhob, &
1110 rho_ao=rho_ao_b, &
1111 rho_ao_im=rho_ao_im_b, &
1112 rho_r=rho_r_b, &
1113 rho_g=rho_g_b, &
1114 drho_r=drho_r_b, &
1115 drho_g=drho_g_b, &
1116 tau_r=tau_r_b, &
1117 tau_g=tau_g_b, &
1118 tot_rho_r=tot_rho_r_b, &
1119 tot_rho_g=tot_rho_g_b, &
1120 rho_r_sccs=rho_r_sccs_b)
1121 ! rho_ao
1122 IF (ASSOCIATED(rho_ao_a) .AND. ASSOCIATED(rho_ao_b)) THEN
1123 CALL dbcsr_add(rho_ao_a(2)%matrix, rho_ao_b(1)%matrix, alpha, beta)
1124 END IF
1125
1126 ! rho_ao_im
1127 IF (ASSOCIATED(rho_ao_im_a) .AND. ASSOCIATED(rho_ao_im_b)) THEN
1128 CALL dbcsr_add(rho_ao_im_a(2)%matrix, rho_ao_im_b(1)%matrix, alpha, beta)
1129 END IF
1130
1131 ! rho_r
1132 IF (ASSOCIATED(rho_r_a) .AND. ASSOCIATED(rho_r_b)) THEN
1133 CALL pw_axpy(rho_r_b(1), rho_r_a(2), beta, alpha)
1134 END IF
1135
1136 ! rho_g
1137 IF (ASSOCIATED(rho_g_a) .AND. ASSOCIATED(rho_g_b)) THEN
1138 CALL pw_axpy(rho_g_b(1), rho_g_a(2), beta, alpha)
1139 END IF
1140
1141 ! SCCS
1142 IF (ASSOCIATED(rho_r_sccs_a) .AND. ASSOCIATED(rho_r_sccs_b)) THEN
1143 CALL pw_axpy(rho_r_sccs_b, rho_r_sccs_a, beta, alpha)
1144 END IF
1145
1146 ! drho_r
1147 IF (ASSOCIATED(drho_r_a) .AND. ASSOCIATED(drho_r_b)) THEN
1148 cpassert(ASSOCIATED(drho_r_a) .AND. ASSOCIATED(drho_r_b)) ! not implemented
1149 END IF
1150
1151 ! drho_g
1152 IF (ASSOCIATED(drho_g_a) .AND. ASSOCIATED(drho_g_b)) THEN
1153 cpassert(ASSOCIATED(drho_g_a) .AND. ASSOCIATED(drho_g_b)) ! not implemented
1154 END IF
1155
1156 ! tau_r
1157 IF (ASSOCIATED(tau_r_a) .AND. ASSOCIATED(tau_r_b)) THEN
1158 CALL pw_axpy(tau_r_b(1), tau_r_a(2), beta, alpha)
1159 END IF
1160
1161 ! tau_g
1162 IF (ASSOCIATED(tau_g_a) .AND. ASSOCIATED(tau_g_b)) THEN
1163 CALL pw_axpy(tau_g_b(1), tau_g_a(2), beta, alpha)
1164 END IF
1165
1166 ! tot_rho_r
1167 IF (ASSOCIATED(tot_rho_r_a) .AND. ASSOCIATED(tot_rho_r_b)) THEN
1168 tot_rho_r_a(2) = alpha*tot_rho_r_a(2) + beta*tot_rho_r_b(1)
1169 END IF
1170
1171 ! tot_rho_g
1172 IF (ASSOCIATED(tot_rho_g_a) .AND. ASSOCIATED(tot_rho_g_b)) THEN
1173 tot_rho_g_a(2) = alpha*tot_rho_g_a(2) + beta*tot_rho_g_b(1)
1174 END IF
1175
1176 CALL timestop(handle)
1177
1178 END SUBROUTINE qs_rho_scale_and_add_b
1179
1180! **************************************************************************************************
1181!> \brief rhoa = alpha*rhoa+beta*rhob
1182!> \param rhoa ...
1183!> \param rhob ...
1184!> \param alpha ...
1185!> \param beta ...
1186! **************************************************************************************************
1187 SUBROUTINE qs_rho_scale_and_add(rhoa, rhob, alpha, beta)
1188
1189 TYPE(qs_rho_type), INTENT(IN) :: rhoa, rhob
1190 REAL(kind=dp), INTENT(IN) :: alpha, beta
1191
1192 CHARACTER(len=*), PARAMETER :: routinen = 'qs_rho_scale_and_add'
1193
1194 INTEGER :: handle, i, j, nspina, nspinb, nspins
1195 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_g_a, tot_rho_g_b, tot_rho_r_a, &
1196 tot_rho_r_b
1197 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao_a, rho_ao_b, rho_ao_im_a, &
1198 rho_ao_im_b
1199 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_a, rho_g_b, tau_g_a, tau_g_b
1200 TYPE(pw_c1d_gs_type), DIMENSION(:, :), POINTER :: drho_g_a, drho_g_b
1201 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r_a, rho_r_b, tau_r_a, tau_r_b
1202 TYPE(pw_r3d_rs_type), DIMENSION(:, :), POINTER :: drho_r_a, drho_r_b
1203 TYPE(pw_r3d_rs_type), POINTER :: rho_r_sccs_a, rho_r_sccs_b
1204
1205 CALL timeset(routinen, handle)
1206
1207 NULLIFY (rho_ao_a, rho_ao_im_a, rho_r_a, rho_g_a, drho_r_a, &
1208 drho_g_a, tau_r_a, tau_g_a, tot_rho_r_a, tot_rho_g_a, rho_r_sccs_a)
1209
1210 CALL qs_rho_get(rhoa, &
1211 rho_ao=rho_ao_a, &
1212 rho_ao_im=rho_ao_im_a, &
1213 rho_r=rho_r_a, &
1214 rho_g=rho_g_a, &
1215 drho_r=drho_r_a, &
1216 drho_g=drho_g_a, &
1217 tau_r=tau_r_a, &
1218 tau_g=tau_g_a, &
1219 tot_rho_r=tot_rho_r_a, &
1220 tot_rho_g=tot_rho_g_a, &
1221 rho_r_sccs=rho_r_sccs_a)
1222
1223 NULLIFY (rho_ao_b, rho_ao_im_b, rho_r_b, rho_g_b, drho_r_b, &
1224 drho_g_b, tau_r_b, tau_g_b, tot_rho_r_b, tot_rho_g_b, rho_r_sccs_b)
1225
1226 CALL qs_rho_get(rhob, &
1227 rho_ao=rho_ao_b, &
1228 rho_ao_im=rho_ao_im_b, &
1229 rho_r=rho_r_b, &
1230 rho_g=rho_g_b, &
1231 drho_r=drho_r_b, &
1232 drho_g=drho_g_b, &
1233 tau_r=tau_r_b, &
1234 tau_g=tau_g_b, &
1235 tot_rho_r=tot_rho_r_b, &
1236 tot_rho_g=tot_rho_g_b, &
1237 rho_r_sccs=rho_r_sccs_b)
1238 ! rho_ao
1239 IF (ASSOCIATED(rho_ao_a) .AND. ASSOCIATED(rho_ao_b)) THEN
1240 nspina = SIZE(rho_ao_a)
1241 nspinb = SIZE(rho_ao_b)
1242 nspins = min(nspina, nspinb)
1243 DO i = 1, nspins
1244 CALL dbcsr_add(rho_ao_a(i)%matrix, rho_ao_b(i)%matrix, alpha, beta)
1245 END DO
1246 END IF
1247
1248 ! rho_ao_im
1249 IF (ASSOCIATED(rho_ao_im_a) .AND. ASSOCIATED(rho_ao_im_b)) THEN
1250 nspina = SIZE(rho_ao_im_a)
1251 nspinb = SIZE(rho_ao_im_b)
1252 nspins = min(nspina, nspinb)
1253 DO i = 1, nspins
1254 CALL dbcsr_add(rho_ao_im_a(i)%matrix, rho_ao_im_b(i)%matrix, alpha, beta)
1255 END DO
1256 END IF
1257
1258 ! rho_r
1259 IF (ASSOCIATED(rho_r_a) .AND. ASSOCIATED(rho_r_b)) THEN
1260 nspina = SIZE(rho_ao_a)
1261 nspinb = SIZE(rho_ao_b)
1262 nspins = min(nspina, nspinb)
1263 DO i = 1, nspins
1264 CALL pw_axpy(rho_r_b(i), rho_r_a(i), beta, alpha)
1265 END DO
1266 END IF
1267
1268 ! rho_g
1269 IF (ASSOCIATED(rho_g_a) .AND. ASSOCIATED(rho_g_b)) THEN
1270 nspina = SIZE(rho_ao_a)
1271 nspinb = SIZE(rho_ao_b)
1272 nspins = min(nspina, nspinb)
1273 DO i = 1, nspins
1274 CALL pw_axpy(rho_g_b(i), rho_g_a(i), beta, alpha)
1275 END DO
1276 END IF
1277
1278 ! SCCS
1279 IF (ASSOCIATED(rho_r_sccs_a) .AND. ASSOCIATED(rho_r_sccs_b)) THEN
1280 CALL pw_axpy(rho_r_sccs_b, rho_r_sccs_a, beta, alpha)
1281 END IF
1282
1283 ! drho_r
1284 IF (ASSOCIATED(drho_r_a) .AND. ASSOCIATED(drho_r_b)) THEN
1285 cpassert(all(shape(drho_r_a) == shape(drho_r_b))) ! not implemented
1286 DO j = 1, SIZE(drho_r_a, 2)
1287 DO i = 1, SIZE(drho_r_a, 1)
1288 CALL pw_axpy(drho_r_b(i, j), drho_r_a(i, j), beta, alpha)
1289 END DO
1290 END DO
1291 END IF
1292
1293 ! drho_g
1294 IF (ASSOCIATED(drho_g_a) .AND. ASSOCIATED(drho_g_b)) THEN
1295 cpassert(all(shape(drho_g_a) == shape(drho_g_b))) ! not implemented
1296 DO j = 1, SIZE(drho_g_a, 2)
1297 DO i = 1, SIZE(drho_g_a, 1)
1298 CALL pw_axpy(drho_g_b(i, j), drho_g_a(i, j), beta, alpha)
1299 END DO
1300 END DO
1301 END IF
1302
1303 ! tau_r
1304 IF (ASSOCIATED(tau_r_a) .AND. ASSOCIATED(tau_r_b)) THEN
1305 nspina = SIZE(rho_ao_a)
1306 nspinb = SIZE(rho_ao_b)
1307 nspins = min(nspina, nspinb)
1308 DO i = 1, nspins
1309 CALL pw_axpy(tau_r_b(i), tau_r_a(i), beta, alpha)
1310 END DO
1311 END IF
1312
1313 ! tau_g
1314 IF (ASSOCIATED(tau_g_a) .AND. ASSOCIATED(tau_g_b)) THEN
1315 nspina = SIZE(rho_ao_a)
1316 nspinb = SIZE(rho_ao_b)
1317 nspins = min(nspina, nspinb)
1318 DO i = 1, nspins
1319 CALL pw_axpy(tau_g_b(i), tau_g_a(i), beta, alpha)
1320 END DO
1321 END IF
1322
1323 ! tot_rho_r
1324 IF (ASSOCIATED(tot_rho_r_a) .AND. ASSOCIATED(tot_rho_r_b)) THEN
1325 nspina = SIZE(rho_ao_a)
1326 nspinb = SIZE(rho_ao_b)
1327 nspins = min(nspina, nspinb)
1328 DO i = 1, nspins
1329 tot_rho_r_a(i) = alpha*tot_rho_r_a(i) + beta*tot_rho_r_b(i)
1330 END DO
1331 END IF
1332
1333 ! tot_rho_g
1334 IF (ASSOCIATED(tot_rho_g_a) .AND. ASSOCIATED(tot_rho_g_b)) THEN
1335 nspina = SIZE(rho_ao_a)
1336 nspinb = SIZE(rho_ao_b)
1337 nspins = min(nspina, nspinb)
1338 DO i = 1, nspins
1339 tot_rho_g_a(i) = alpha*tot_rho_g_a(i) + beta*tot_rho_g_b(i)
1340 END DO
1341 END IF
1342
1343 CALL timestop(handle)
1344
1345 END SUBROUTINE qs_rho_scale_and_add
1346
1347! **************************************************************************************************
1348!> \brief Duplicates a pointer physically
1349!> \param rho_input The rho structure to be duplicated
1350!> \param rho_output The duplicate rho structure
1351!> \param qs_env The QS environment from which the auxiliary PW basis-set
1352!> pool is taken
1353!> \par History
1354!> 07.2005 initial create [tdk]
1355!> \author Thomas D. Kuehne (tkuehne@phys.chem.ethz.ch)
1356!> \note
1357!> Associated pointers are deallocated, nullified pointers are NOT accepted!
1358! **************************************************************************************************
1359 SUBROUTINE duplicate_rho_type(rho_input, rho_output, qs_env)
1360
1361 TYPE(qs_rho_type), INTENT(INOUT) :: rho_input, rho_output
1362 TYPE(qs_environment_type), POINTER :: qs_env
1363
1364 CHARACTER(len=*), PARAMETER :: routinen = 'duplicate_rho_type'
1365
1366 INTEGER :: handle, i, j, nspins
1367 LOGICAL :: complex_rho_ao_in, drho_g_valid_in, drho_r_valid_in, rho_g_valid_in, &
1368 rho_r_valid_in, soft_valid_in, tau_g_valid_in, tau_r_valid_in
1369 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_g_in, tot_rho_g_out, &
1370 tot_rho_r_in, tot_rho_r_out
1371 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao_im_in, rho_ao_im_out, rho_ao_in, &
1372 rho_ao_out
1373 TYPE(dft_control_type), POINTER :: dft_control
1374 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_in, rho_g_out, tau_g_in, tau_g_out
1375 TYPE(pw_c1d_gs_type), DIMENSION(:, :), POINTER :: drho_g_in, drho_g_out
1376 TYPE(pw_env_type), POINTER :: pw_env
1377 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
1378 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r_in, rho_r_out, tau_r_in, tau_r_out
1379 TYPE(pw_r3d_rs_type), DIMENSION(:, :), POINTER :: drho_r_in, drho_r_out
1380 TYPE(pw_r3d_rs_type), POINTER :: rho_r_sccs_in, rho_r_sccs_out
1381
1382 CALL timeset(routinen, handle)
1383
1384 NULLIFY (dft_control, pw_env, auxbas_pw_pool)
1385 NULLIFY (rho_ao_in, rho_ao_out, rho_ao_im_in, rho_ao_im_out)
1386 NULLIFY (rho_r_in, rho_r_out, rho_g_in, rho_g_out, drho_r_in, drho_r_out)
1387 NULLIFY (drho_g_in, drho_g_out, tau_r_in, tau_r_out, tau_g_in, tau_g_out)
1388 NULLIFY (tot_rho_r_in, tot_rho_r_out, tot_rho_g_in, tot_rho_g_out)
1389 NULLIFY (rho_r_sccs_in, rho_r_sccs_out)
1390
1391 cpassert(ASSOCIATED(qs_env))
1392
1393 CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, dft_control=dft_control)
1394 CALL pw_env_get(pw_env=pw_env, auxbas_pw_pool=auxbas_pw_pool)
1395 nspins = dft_control%nspins
1396
1397 CALL qs_rho_clear(rho_output)
1398
1399 CALL qs_rho_get(rho_input, &
1400 rho_ao=rho_ao_in, &
1401 rho_ao_im=rho_ao_im_in, &
1402 rho_r=rho_r_in, &
1403 rho_g=rho_g_in, &
1404 drho_r=drho_r_in, &
1405 drho_g=drho_g_in, &
1406 tau_r=tau_r_in, &
1407 tau_g=tau_g_in, &
1408 tot_rho_r=tot_rho_r_in, &
1409 tot_rho_g=tot_rho_g_in, &
1410 rho_g_valid=rho_g_valid_in, &
1411 rho_r_valid=rho_r_valid_in, &
1412 drho_g_valid=drho_g_valid_in, &
1413 drho_r_valid=drho_r_valid_in, &
1414 tau_r_valid=tau_r_valid_in, &
1415 tau_g_valid=tau_g_valid_in, &
1416 rho_r_sccs=rho_r_sccs_in, &
1417 soft_valid=soft_valid_in, &
1418 complex_rho_ao=complex_rho_ao_in)
1419
1420 ! rho_ao
1421 IF (ASSOCIATED(rho_ao_in)) THEN
1422 CALL dbcsr_allocate_matrix_set(rho_ao_out, nspins)
1423 CALL qs_rho_set(rho_output, rho_ao=rho_ao_out)
1424 DO i = 1, nspins
1425 ALLOCATE (rho_ao_out(i)%matrix)
1426 CALL dbcsr_copy(rho_ao_out(i)%matrix, rho_ao_in(i)%matrix, &
1427 name="myDensityMatrix_for_Spin_"//trim(adjustl(cp_to_string(i))))
1428 CALL dbcsr_set(rho_ao_out(i)%matrix, 0.0_dp)
1429 END DO
1430 END IF
1431
1432 ! rho_ao_im
1433 IF (ASSOCIATED(rho_ao_im_in)) THEN
1434 CALL dbcsr_allocate_matrix_set(rho_ao_im_out, nspins)
1435 CALL qs_rho_set(rho_output, rho_ao=rho_ao_im_out)
1436 DO i = 1, nspins
1437 ALLOCATE (rho_ao_im_out(i)%matrix)
1438 CALL dbcsr_copy(rho_ao_im_out(i)%matrix, rho_ao_im_in(i)%matrix, &
1439 name="myImagDensityMatrix_for_Spin_"//trim(adjustl(cp_to_string(i))))
1440 CALL dbcsr_set(rho_ao_im_out(i)%matrix, 0.0_dp)
1441 END DO
1442 END IF
1443
1444 ! rho_r
1445 IF (ASSOCIATED(rho_r_in)) THEN
1446 ALLOCATE (rho_r_out(nspins))
1447 CALL qs_rho_set(rho_output, rho_r=rho_r_out)
1448 DO i = 1, nspins
1449 CALL auxbas_pw_pool%create_pw(rho_r_out(i))
1450 CALL pw_copy(rho_r_in(i), rho_r_out(i))
1451 END DO
1452 END IF
1453
1454 ! rho_g
1455 IF (ASSOCIATED(rho_g_in)) THEN
1456 ALLOCATE (rho_g_out(nspins))
1457 CALL qs_rho_set(rho_output, rho_g=rho_g_out)
1458 DO i = 1, nspins
1459 CALL auxbas_pw_pool%create_pw(rho_g_out(i))
1460 CALL pw_copy(rho_g_in(i), rho_g_out(i))
1461 END DO
1462 END IF
1463
1464 ! SCCS
1465 IF (ASSOCIATED(rho_r_sccs_in)) THEN
1466 CALL qs_rho_set(rho_output, rho_r_sccs=rho_r_sccs_out)
1467 CALL auxbas_pw_pool%create_pw(rho_r_sccs_out)
1468 CALL pw_copy(rho_r_sccs_in, rho_r_sccs_out)
1469 END IF
1470
1471 ! drho_r and drho_g are only needed if calculated by collocation
1472 IF (dft_control%drho_by_collocation) THEN
1473 ! drho_r
1474 IF (ASSOCIATED(drho_r_in)) THEN
1475 ALLOCATE (drho_r_out(3, nspins))
1476 CALL qs_rho_set(rho_output, drho_r=drho_r_out)
1477 DO j = 1, nspins
1478 DO i = 1, 3
1479 CALL auxbas_pw_pool%create_pw(drho_r_out(i, j))
1480 CALL pw_copy(drho_r_in(i, j), drho_r_out(i, j))
1481 END DO
1482 END DO
1483 END IF
1484
1485 ! drho_g
1486 IF (ASSOCIATED(drho_g_in)) THEN
1487 ALLOCATE (drho_g_out(3, nspins))
1488 CALL qs_rho_set(rho_output, drho_g=drho_g_out)
1489 DO j = 1, nspins
1490 DO i = 1, 3
1491 CALL auxbas_pw_pool%create_pw(drho_g_out(i, j))
1492 CALL pw_copy(drho_g_in(i, j), drho_g_out(i, j))
1493 END DO
1494 END DO
1495 END IF
1496 END IF
1497
1498 ! tau_r and tau_g are only needed in the case of Meta-GGA XC-functionals
1499 ! are used. Therefore they are only allocated if
1500 ! dft_control%use_kinetic_energy_density is true
1501 IF (dft_control%use_kinetic_energy_density) THEN
1502 ! tau_r
1503 IF (ASSOCIATED(tau_r_in)) THEN
1504 ALLOCATE (tau_r_out(nspins))
1505 CALL qs_rho_set(rho_output, tau_r=tau_r_out)
1506 DO i = 1, nspins
1507 CALL auxbas_pw_pool%create_pw(tau_r_out(i))
1508 CALL pw_copy(tau_r_in(i), tau_r_out(i))
1509 END DO
1510 END IF
1511
1512 ! tau_g
1513 IF (ASSOCIATED(tau_g_in)) THEN
1514 ALLOCATE (tau_g_out(nspins))
1515 CALL qs_rho_set(rho_output, tau_g=tau_g_out)
1516 DO i = 1, nspins
1517 CALL auxbas_pw_pool%create_pw(tau_g_out(i))
1518 CALL pw_copy(tau_g_in(i), tau_g_out(i))
1519 END DO
1520 END IF
1521 END IF
1522
1523 CALL qs_rho_set(rho_output, &
1524 rho_g_valid=rho_g_valid_in, &
1525 rho_r_valid=rho_r_valid_in, &
1526 drho_g_valid=drho_g_valid_in, &
1527 drho_r_valid=drho_r_valid_in, &
1528 tau_r_valid=tau_r_valid_in, &
1529 tau_g_valid=tau_g_valid_in, &
1530 soft_valid=soft_valid_in, &
1531 complex_rho_ao=complex_rho_ao_in)
1532
1533 ! tot_rho_r
1534 IF (ASSOCIATED(tot_rho_r_in)) THEN
1535 ALLOCATE (tot_rho_r_out(nspins))
1536 CALL qs_rho_set(rho_output, tot_rho_r=tot_rho_r_out)
1537 DO i = 1, nspins
1538 tot_rho_r_out(i) = tot_rho_r_in(i)
1539 END DO
1540 END IF
1541
1542 ! tot_rho_g
1543 IF (ASSOCIATED(tot_rho_g_in)) THEN
1544 ALLOCATE (tot_rho_g_out(nspins))
1545 CALL qs_rho_set(rho_output, tot_rho_g=tot_rho_g_out)
1546 DO i = 1, nspins
1547 tot_rho_g_out(i) = tot_rho_g_in(i)
1548 END DO
1549
1550 END IF
1551
1552 CALL timestop(handle)
1553
1554 END SUBROUTINE duplicate_rho_type
1555
1556! **************************************************************************************************
1557!> \brief (Re-)allocates rho_ao_im from real part rho_ao
1558!> \param rho ...
1559!> \param qs_env ...
1560! **************************************************************************************************
1561 SUBROUTINE allocate_rho_ao_imag_from_real(rho, qs_env)
1562 TYPE(qs_rho_type), POINTER :: rho
1563 TYPE(qs_environment_type), POINTER :: qs_env
1564
1565 CHARACTER(LEN=default_string_length) :: headline
1566 INTEGER :: i, ic, nimages, nspins
1567 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_im_kp, rho_ao_kp
1568 TYPE(dbcsr_type), POINTER :: template
1569 TYPE(dft_control_type), POINTER :: dft_control
1570 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1571 POINTER :: sab_orb
1572
1573 NULLIFY (rho_ao_im_kp, rho_ao_kp, dft_control, template, sab_orb)
1574
1575 CALL get_qs_env(qs_env, &
1576 dft_control=dft_control, &
1577 sab_orb=sab_orb)
1578
1579 CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_im_kp, rho_ao_kp=rho_ao_kp)
1580
1581 nspins = dft_control%nspins
1582 nimages = dft_control%nimages
1583
1584 cpassert(nspins == SIZE(rho_ao_kp, 1))
1585 cpassert(nimages == SIZE(rho_ao_kp, 2))
1586
1587 CALL dbcsr_allocate_matrix_set(rho_ao_im_kp, nspins, nimages)
1588 CALL qs_rho_set(rho, rho_ao_im_kp=rho_ao_im_kp)
1589 DO i = 1, nspins
1590 DO ic = 1, nimages
1591 IF (nspins > 1) THEN
1592 IF (i == 1) THEN
1593 headline = "IMAGINARY PART OF DENSITY MATRIX FOR ALPHA SPIN"
1594 ELSE
1595 headline = "IMAGINARY PART OF DENSITY MATRIX FOR BETA SPIN"
1596 END IF
1597 ELSE
1598 headline = "IMAGINARY PART OF DENSITY MATRIX"
1599 END IF
1600 ALLOCATE (rho_ao_im_kp(i, ic)%matrix)
1601 template => rho_ao_kp(i, ic)%matrix ! base on real part, but anti-symmetric
1602 CALL dbcsr_create(matrix=rho_ao_im_kp(i, ic)%matrix, template=template, &
1603 name=trim(headline), matrix_type=dbcsr_type_antisymmetric)
1604 CALL cp_dbcsr_alloc_block_from_nbl(rho_ao_im_kp(i, ic)%matrix, sab_orb)
1605 CALL dbcsr_set(rho_ao_im_kp(i, ic)%matrix, 0.0_dp)
1606 END DO
1607 END DO
1608
1609 END SUBROUTINE allocate_rho_ao_imag_from_real
1610
1611END MODULE qs_rho_methods
Types and set/get functions for auxiliary density matrix methods.
Definition admm_types.F:15
subroutine, public get_admm_env(admm_env, mo_derivs_aux_fit, mos_aux_fit, sab_aux_fit, sab_aux_fit_asymm, sab_aux_fit_vs_orb, matrix_s_aux_fit, matrix_s_aux_fit_kp, matrix_s_aux_fit_vs_orb, matrix_s_aux_fit_vs_orb_kp, task_list_aux_fit, matrix_ks_aux_fit, matrix_ks_aux_fit_kp, matrix_ks_aux_fit_im, matrix_ks_aux_fit_dft, matrix_ks_aux_fit_hfx, matrix_ks_aux_fit_dft_kp, matrix_ks_aux_fit_hfx_kp, rho_aux_fit, rho_aux_fit_buffer, admm_dm)
Get routine for the ADMM env.
Definition admm_types.F:599
Define the atomic kind types and their sub types.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_scale(matrix, alpha_scalar)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
DBCSR operations in CP2K.
various routines to log and control the output. The idea is that decisions about where to log should ...
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Types and basic routines needed for a kpoint calculation.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered)
Retrieve information from a kpoint environment.
Calculates integral matrices for LRIGPW method lri : local resolution of the identity.
subroutine, public calculate_lri_densities(lri_env, lri_density, qs_env, pmatrix, cell_to_index, lri_rho_struct, atomic_kind_set, para_env, response_density)
performs the fitting of the density and distributes the fitted density on the grid
contains the types and subroutines for dealing with the lri_env lri : local resolution of the identit...
Interface to the message passing library MPI.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Calculate the plane wave density by collocating the primitive Gaussian functions (pgf).
subroutine, public calculate_drho_elec(matrix_p, matrix_p_kp, drho, drho_gspace, qs_env, soft_valid, basis_type)
computes the gradient of the density corresponding to a given density matrix on the grid
subroutine, public calculate_rho_elec(matrix_p, matrix_p_kp, rho, rho_gspace, total_rho, ks_env, soft_valid, compute_tau, compute_grad, basis_type, der_type, idir, task_list_external, pw_env_external)
computes the density corresponding to a given density matrix on the grid
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
subroutine, public set_qs_env(qs_env, super_cell, mos, qmmm, qmmm_periodic, mimic, ewald_env, ewald_pw, mpools, rho_external, external_vxc, mask, scf_control, rel_control, qs_charges, ks_env, ks_qmmm_env, wf_history, scf_env, active_space, input, oce, rho_atom_set, rho0_atom_set, rho0_mpole, run_rtp, rtp, rhoz_set, rhoz_tot, ecoul_1c, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, efield, rhoz_cneo_set, linres_control, xas_env, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, ls_scf_env, do_transport, transport_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, harris_env, gcp_env, mp2_env, bs_env, kg_env, force, kpoints, wanniercentres, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Set the QUICKSTEP environment.
Types needed for a for a Harris model calculation.
Harris method environment setup and handling.
subroutine, public calculate_harris_density(qs_env, rhoin, rho_struct)
...
Define the quickstep kind type and their sub types.
subroutine, public get_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, rho, rho_xc, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, kpoints, do_kpoints, atomic_kind_set, qs_kind_set, cell, cell_ref, use_ref_cell, particle_set, energy, force, local_particles, local_molecules, molecule_kind_set, molecule_set, subsys, cp_subsys, virial, results, atprop, nkind, natom, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env, nelectron_total, nelectron_spin)
...
Define the neighbor list data types and the corresponding functionality.
subroutine, public calculate_rho_atom_coeff(qs_env, rho_ao, rho_atom_set, qs_kind_set, oce, sab, para_env)
...
methods of the rho structure (defined in qs_rho_types)
subroutine, public qs_rho_update_tddfpt(rho_struct, qs_env, pw_env_external, task_list_external, para_env_external, tddfpt_lri_env, tddfpt_lri_density)
updates rho_r and rho_g to the rhorho_ao. if use_kinetic_energy_density also computes tau_r and tau_g
subroutine, public allocate_rho_ao_imag_from_real(rho, qs_env)
(Re-)allocates rho_ao_im from real part rho_ao
subroutine, public qs_rho_update_rho(rho_struct, qs_env, rho_xc_external, local_rho_set, task_list_external, task_list_external_soft, pw_env_external, para_env_external)
updates rho_r and rho_g to the rhorho_ao. if use_kinetic_energy_density also computes tau_r and tau_g...
subroutine, public duplicate_rho_type(rho_input, rho_output, qs_env)
Duplicates a pointer physically.
subroutine, public qs_rho_scale_and_add_b(rhoa, rhob, alpha, beta)
rhoa(2) = alpha*rhoa(2)+beta*rhob(1)
subroutine, public qs_rho_copy(rho_input, rho_output, auxbas_pw_pool, mspin)
Allocate a density structure and fill it with data from an input structure SIZE(rho_input) == mspin =...
subroutine, public qs_rho_scale_and_add(rhoa, rhob, alpha, beta)
rhoa = alpha*rhoa+beta*rhob
subroutine, public qs_rho_rebuild(rho, qs_env, rebuild_ao, rebuild_grids, admm, pw_env_external)
rebuilds rho (if necessary allocating and initializing it)
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_set(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
subroutine, public qs_rho_clear(rho_struct)
Deallocates all components, without deallocating rho_struct itself.
Calculates integral matrices for RIGPW method.
subroutine, public calculate_ri_densities(lri_env, qs_env, pmatrix, lri_rho_struct, atomic_kind_set, para_env)
performs the fitting of the density and distributes the fitted density on the grid
types for task lists
Provides all information about an atomic kind.
Contains information about kpoints.
stores all the informations relevant to an mpi environment
contained for different pw related things
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Contains information on the Harris method.
Provides all information about a quickstep kind.
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.