(git:374b731)
Loading...
Searching...
No Matches
kg_correction.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Routines for a Kim-Gordon-like partitioning into molecular subunits
10!> \par History
11!> 2012.06 created [Martin Haeufel]
12!> \author Martin Haeufel and Florian Schiffmann
13! **************************************************************************************************
20 USE dbcsr_api, ONLY: dbcsr_add,&
21 dbcsr_dot,&
22 dbcsr_p_type
23 USE ec_methods, ONLY: create_kernel
30 USE kinds, ONLY: dp
39 USE pw_env_types, ONLY: pw_env_get,&
41 USE pw_methods, ONLY: pw_integral_ab,&
44 USE pw_types, ONLY: pw_c1d_gs_type,&
48 USE qs_integrate_potential, ONLY: integrate_v_rspace,&
49 integrate_v_rspace_one_center
53 USE qs_rho_types, ONLY: qs_rho_create,&
59 USE qs_vxc, ONLY: qs_vxc_create
60 USE virial_types, ONLY: virial_type
61#include "./base/base_uses.f90"
62
63 IMPLICIT NONE
64
65 PRIVATE
66
67 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'kg_correction'
68
69 PUBLIC :: kg_ekin_subset
70
71CONTAINS
72
73! **************************************************************************************************
74!> \brief Calculates the subsystem Hohenberg-Kohn kinetic energy and the forces
75!> \param qs_env ...
76!> \param ks_matrix ...
77!> \param ekin_mol ...
78!> \param calc_force ...
79!> \param do_kernel Contribution of kinetic energy functional to kernel in response calculation
80!> \param pmat_ext Response density used to fold 2nd deriv or to integrate kinetic energy functional
81!> \par History
82!> 2012.06 created [Martin Haeufel]
83!> 2014.01 added atomic potential option [JGH]
84!> 2020.01 Added KG contribution to linear response [fbelle]
85!> \author Martin Haeufel and Florian Schiffmann
86! **************************************************************************************************
87 SUBROUTINE kg_ekin_subset(qs_env, ks_matrix, ekin_mol, calc_force, do_kernel, pmat_ext)
88 TYPE(qs_environment_type), POINTER :: qs_env
89 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix
90 REAL(kind=dp), INTENT(out) :: ekin_mol
91 LOGICAL, INTENT(IN) :: calc_force, do_kernel
92 TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
93 POINTER :: pmat_ext
94
95 LOGICAL :: lrigpw
96 TYPE(dft_control_type), POINTER :: dft_control
97 TYPE(kg_environment_type), POINTER :: kg_env
98
99 CALL get_qs_env(qs_env, kg_env=kg_env, dft_control=dft_control)
100 lrigpw = dft_control%qs_control%lrigpw
101
102 IF (kg_env%tnadd_method == kg_tnadd_embed) THEN
103 IF (lrigpw) THEN
104 CALL kg_ekin_embed_lri(qs_env, kg_env, ks_matrix, ekin_mol, calc_force)
105 ELSE
106 CALL kg_ekin_embed(qs_env, kg_env, ks_matrix, ekin_mol, calc_force, &
107 do_kernel, pmat_ext)
108 END IF
109 ELSE IF (kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
110 CALL kg_ekin_ri_embed(qs_env, kg_env, ks_matrix, ekin_mol, calc_force, &
111 do_kernel, pmat_ext)
112 ELSE IF (kg_env%tnadd_method == kg_tnadd_atomic) THEN
113 CALL kg_ekin_atomic(qs_env, ks_matrix, ekin_mol)
114 ELSE IF (kg_env%tnadd_method == kg_tnadd_none) THEN
115 ekin_mol = 0.0_dp
116 ELSE
117 cpabort("Unknown KG embedding method")
118 END IF
119
120 END SUBROUTINE kg_ekin_subset
121
122! **************************************************************************************************
123!> \brief ...
124!> \param qs_env ...
125!> \param kg_env ...
126!> \param ks_matrix ...
127!> \param ekin_mol ...
128!> \param calc_force ...
129!> \param do_kernel Contribution of kinetic energy functional to kernel in response calculation
130!> \param pmat_ext Response density used to fold 2nd deriv or to integrate kinetic energy functional
131! **************************************************************************************************
132 SUBROUTINE kg_ekin_embed(qs_env, kg_env, ks_matrix, ekin_mol, calc_force, do_kernel, pmat_ext)
133 TYPE(qs_environment_type), POINTER :: qs_env
134 TYPE(kg_environment_type), POINTER :: kg_env
135 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix
136 REAL(kind=dp), INTENT(out) :: ekin_mol
137 LOGICAL, INTENT(IN) :: calc_force, do_kernel
138 TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
139 POINTER :: pmat_ext
140
141 CHARACTER(LEN=*), PARAMETER :: routinen = 'kg_ekin_embed'
142
143 INTEGER :: handle, iounit, ispin, isub, nspins
144 LOGICAL :: use_virial
145 REAL(kind=dp) :: alpha, ekin_imol
146 REAL(kind=dp), DIMENSION(3, 3) :: xcvirial
147 TYPE(cp_logger_type), POINTER :: logger
148 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: density_matrix
149 TYPE(dft_control_type), POINTER :: dft_control
150 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho1_g
151 TYPE(pw_env_type), POINTER :: pw_env
152 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
153 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho1_r, rho_r, tau1_r, vxc_rho, vxc_tau
154 TYPE(qs_ks_env_type), POINTER :: ks_env
155 TYPE(qs_rho_type), POINTER :: old_rho, rho1, rho_struct
156 TYPE(section_vals_type), POINTER :: xc_section
157 TYPE(virial_type), POINTER :: virial
158
159 CALL timeset(routinen, handle)
160
161 logger => cp_get_default_logger()
162 iounit = cp_logger_get_default_unit_nr(logger)
163
164 NULLIFY (ks_env, dft_control, old_rho, pw_env, rho_struct, virial, vxc_rho, vxc_tau)
165
166 CALL get_qs_env(qs_env, &
167 ks_env=ks_env, &
168 rho=old_rho, &
169 dft_control=dft_control, &
170 virial=virial, &
171 pw_env=pw_env)
172 nspins = dft_control%nspins
173 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
174 use_virial = use_virial .AND. calc_force
175
176 ! Kernel potential in response calculation (no forces calculated at this point)
177 ! requires spin-factor
178 ! alpha = 2 closed-shell
179 ! alpha = 1 open-shell
180 alpha = 1.0_dp
181 IF (do_kernel .AND. .NOT. calc_force .AND. nspins == 1) alpha = 2.0_dp
182
183 NULLIFY (auxbas_pw_pool)
184 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
185
186 ! get the density matrix
187 CALL qs_rho_get(old_rho, rho_ao=density_matrix)
188 ! allocate and initialize the density
189 ALLOCATE (rho_struct)
190 CALL qs_rho_create(rho_struct)
191 ! set the density matrix to the blocked matrix
192 CALL qs_rho_set(rho_struct, rho_ao=density_matrix) ! blocked_matrix
193 CALL qs_rho_rebuild(rho_struct, qs_env, rebuild_ao=.false., rebuild_grids=.true.)
194 ! full density kinetic energy term
195 CALL qs_rho_update_rho(rho_struct, qs_env)
196 ! get blocked density that has been put on grid
197 CALL qs_rho_get(rho_struct, rho_r=rho_r)
198
199 ! If external density associated then it is needed either for
200 ! 1) folding of second derivative while partially integrating, or
201 ! 2) integration of response forces
202 NULLIFY (rho1)
203 IF (PRESENT(pmat_ext)) THEN
204 ALLOCATE (rho1)
205 CALL qs_rho_create(rho1)
206 CALL qs_rho_set(rho1, rho_ao=pmat_ext)
207 CALL qs_rho_rebuild(rho1, qs_env, rebuild_ao=.false., rebuild_grids=.true.)
208 CALL qs_rho_update_rho(rho1, qs_env)
209 END IF
210
211 ! XC-section pointing to kinetic energy functional in KG environment
212 NULLIFY (xc_section)
213 xc_section => kg_env%xc_section_kg
214
215 ekin_imol = 0.0_dp
216
217 ! calculate xc potential or kernel
218 IF (do_kernel) THEN
219 ! derivation wrt to rho_struct and evaluation at rho_struct
220 IF (use_virial) virial%pv_xc = 0.0_dp
221 CALL qs_rho_get(rho1, rho_r=rho1_r, rho_g=rho1_g, tau_r=tau1_r)
222 CALL create_kernel(qs_env, &
223 vxc=vxc_rho, &
224 vxc_tau=vxc_tau, &
225 rho=rho_struct, &
226 rho1_r=rho1_r, &
227 rho1_g=rho1_g, &
228 tau1_r=tau1_r, &
229 xc_section=xc_section, &
230 compute_virial=use_virial, &
231 virial_xc=virial%pv_xc)
232 ELSE
233 CALL qs_vxc_create(ks_env=ks_env, &
234 rho_struct=rho_struct, &
235 xc_section=xc_section, &
236 vxc_rho=vxc_rho, &
237 vxc_tau=vxc_tau, &
238 exc=ekin_imol)
239 END IF
240
241 IF (ASSOCIATED(vxc_tau)) THEN
242 cpabort(" KG with meta-kinetic energy functionals not implemented")
243 END IF
244
245 ! Integrate xc-potential with external density for outer response forces
246 IF (PRESENT(pmat_ext) .AND. .NOT. do_kernel) THEN
247 CALL qs_rho_get(rho1, rho_ao=density_matrix, rho_r=rho1_r)
248 ! Direct volume term of virial
249 ! xc-potential is unscaled
250 IF (use_virial) THEN
251 ekin_imol = 0.0_dp
252 DO ispin = 1, nspins
253 ekin_imol = ekin_imol + pw_integral_ab(rho1_r(ispin), vxc_rho(ispin))
254 END DO
255 END IF
256 END IF
257
258 DO ispin = 1, nspins
259 CALL pw_scale(vxc_rho(ispin), alpha*vxc_rho(ispin)%pw_grid%dvol)
260 END DO
261
262 DO ispin = 1, nspins
263 CALL integrate_v_rspace(v_rspace=vxc_rho(ispin), &
264 pmat=density_matrix(ispin), hmat=ks_matrix(ispin), &
265 qs_env=qs_env, calculate_forces=calc_force)
266 CALL auxbas_pw_pool%give_back_pw(vxc_rho(ispin))
267 END DO
268 DEALLOCATE (vxc_rho)
269 ekin_mol = -ekin_imol
270 xcvirial(1:3, 1:3) = 0.0_dp
271 IF (use_virial) THEN
272 xcvirial(1:3, 1:3) = xcvirial(1:3, 1:3) - virial%pv_xc(1:3, 1:3)
273 END IF
274
275 ! loop over all subsets
276 DO isub = 1, kg_env%nsubsets
277 ! calculate the densities for the given blocked density matrix
278 ! pass the subset task_list
279 CALL qs_rho_update_rho(rho_struct, qs_env, &
280 task_list_external=kg_env%subset(isub)%task_list)
281 ! Same for external (response) density if present
282 IF (PRESENT(pmat_ext)) THEN
283 CALL qs_rho_update_rho(rho1, qs_env, &
284 task_list_external=kg_env%subset(isub)%task_list)
285 END IF
286
287 ekin_imol = 0.0_dp
288 NULLIFY (vxc_rho)
289
290 ! calculate Hohenberg-Kohn kinetic energy of the density
291 ! corresponding to the remaining molecular block(s)
292 ! info per block in rho_struct now
293
294 ! calculate xc-potential or kernel
295 IF (do_kernel) THEN
296 IF (use_virial) virial%pv_xc = 0.0_dp
297 CALL qs_rho_get(rho1, rho_r=rho1_r, rho_g=rho1_g, tau_r=tau1_r)
298 CALL create_kernel(qs_env, &
299 vxc=vxc_rho, &
300 vxc_tau=vxc_tau, &
301 rho=rho_struct, &
302 rho1_r=rho1_r, &
303 rho1_g=rho1_g, &
304 tau1_r=tau1_r, &
305 xc_section=xc_section, &
306 compute_virial=use_virial, &
307 virial_xc=virial%pv_xc)
308 ELSE
309 CALL qs_vxc_create(ks_env=ks_env, &
310 rho_struct=rho_struct, &
311 xc_section=xc_section, &
312 vxc_rho=vxc_rho, &
313 vxc_tau=vxc_tau, &
314 exc=ekin_imol)
315 END IF
316
317 ! Integrate with response density for outer response forces
318 IF (PRESENT(pmat_ext) .AND. .NOT. do_kernel) THEN
319 CALL qs_rho_get(rho1, rho_ao=density_matrix)
320 ! Direct volume term of virial
321 ! xc-potential is unscaled
322 IF (use_virial) THEN
323 ekin_imol = 0.0_dp
324 DO ispin = 1, nspins
325 ekin_imol = ekin_imol + pw_integral_ab(rho1_r(ispin), vxc_rho(ispin))
326 END DO
327 END IF
328 END IF
329
330 DO ispin = 1, nspins
331 CALL pw_scale(vxc_rho(ispin), -alpha*vxc_rho(ispin)%pw_grid%dvol)
332
333 CALL integrate_v_rspace(v_rspace=vxc_rho(ispin), &
334 pmat=density_matrix(ispin), &
335 hmat=ks_matrix(ispin), &
336 qs_env=qs_env, &
337 calculate_forces=calc_force, &
338 task_list_external=kg_env%subset(isub)%task_list)
339 ! clean up vxc_rho
340 CALL auxbas_pw_pool%give_back_pw(vxc_rho(ispin))
341 IF (ASSOCIATED(vxc_tau)) THEN
342 CALL pw_scale(vxc_tau(ispin), -alpha*vxc_tau(ispin)%pw_grid%dvol)
343 CALL integrate_v_rspace(v_rspace=vxc_tau(ispin), &
344 pmat=density_matrix(ispin), &
345 hmat=ks_matrix(ispin), &
346 qs_env=qs_env, &
347 compute_tau=.true., &
348 calculate_forces=calc_force, &
349 task_list_external=kg_env%subset(isub)%task_list)
350 ! clean up vxc_rho
351 CALL auxbas_pw_pool%give_back_pw(vxc_tau(ispin))
352 END IF
353 END DO
354 DEALLOCATE (vxc_rho)
355
356 ekin_mol = ekin_mol + ekin_imol
357
358 IF (use_virial) THEN
359 xcvirial(1:3, 1:3) = xcvirial(1:3, 1:3) + virial%pv_xc(1:3, 1:3)
360 END IF
361
362 END DO
363
364 IF (use_virial) THEN
365 virial%pv_xc(1:3, 1:3) = xcvirial(1:3, 1:3)
366 END IF
367
368 ! clean up rho_struct
369 CALL qs_rho_unset_rho_ao(rho_struct)
370 CALL qs_rho_release(rho_struct)
371 DEALLOCATE (rho_struct)
372 IF (PRESENT(pmat_ext)) THEN
373 CALL qs_rho_unset_rho_ao(rho1)
374 CALL qs_rho_release(rho1)
375 DEALLOCATE (rho1)
376 END IF
377
378 CALL timestop(handle)
379
380 END SUBROUTINE kg_ekin_embed
381
382! **************************************************************************************************
383!> \brief ...
384!> \param qs_env ...
385!> \param kg_env ...
386!> \param ks_matrix ...
387!> \param ekin_mol ...
388!> \param calc_force ...
389! **************************************************************************************************
390 SUBROUTINE kg_ekin_embed_lri(qs_env, kg_env, ks_matrix, ekin_mol, calc_force)
391 TYPE(qs_environment_type), POINTER :: qs_env
392 TYPE(kg_environment_type), POINTER :: kg_env
393 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix
394 REAL(kind=dp), INTENT(out) :: ekin_mol
395 LOGICAL :: calc_force
396
397 CHARACTER(LEN=*), PARAMETER :: routinen = 'kg_ekin_embed_lri'
398
399 INTEGER :: color, handle, iatom, ikind, imol, &
400 ispin, isub, natom, nkind, nspins
401 INTEGER, ALLOCATABLE, DIMENSION(:) :: atomlist
402 LOGICAL :: use_virial
403 REAL(kind=dp) :: ekin_imol
404 REAL(kind=dp), DIMENSION(3, 3) :: xcvirial
405 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
406 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: density_matrix, ksmat
407 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: pmat
408 TYPE(dft_control_type), POINTER :: dft_control
409 TYPE(lri_density_type), POINTER :: lri_density
410 TYPE(lri_environment_type), POINTER :: lri_env
411 TYPE(lri_kind_type), DIMENSION(:), POINTER :: lri_v_int
412 TYPE(mp_para_env_type), POINTER :: para_env
413 TYPE(pw_env_type), POINTER :: pw_env
414 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
415 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: vxc_rho, vxc_tau
416 TYPE(qs_ks_env_type), POINTER :: ks_env
417 TYPE(qs_rho_type), POINTER :: old_rho, rho_struct
418 TYPE(virial_type), POINTER :: virial
419
420 CALL timeset(routinen, handle)
421
422 NULLIFY (vxc_rho, vxc_tau, old_rho, rho_struct, ks_env)
423
424 CALL get_qs_env(qs_env, dft_control=dft_control)
425
426 ! get set of molecules, natom, dft_control, pw_env
427 CALL get_qs_env(qs_env, &
428 ks_env=ks_env, &
429 rho=old_rho, &
430 natom=natom, &
431 dft_control=dft_control, &
432 virial=virial, &
433 para_env=para_env, &
434 pw_env=pw_env)
435
436 nspins = dft_control%nspins
437 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
438 use_virial = use_virial .AND. calc_force
439
440 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
441
442 ! get the density matrix
443 CALL qs_rho_get(old_rho, rho_ao=density_matrix)
444 ! allocate and initialize the density
445 ALLOCATE (rho_struct)
446 CALL qs_rho_create(rho_struct)
447 ! set the density matrix to the blocked matrix
448 CALL qs_rho_set(rho_struct, rho_ao=density_matrix) ! blocked_matrix
449 CALL qs_rho_rebuild(rho_struct, qs_env, rebuild_ao=.false., rebuild_grids=.true.)
450
451 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density, nkind=nkind)
452 IF (lri_env%exact_1c_terms) THEN
453 cpabort(" KG with LRI and exact one-center terms not implemented")
454 END IF
455 ALLOCATE (atomlist(natom))
456 DO ispin = 1, nspins
457 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
458 DO ikind = 1, nkind
459 lri_v_int(ikind)%v_int = 0.0_dp
460 IF (calc_force) THEN
461 lri_v_int(ikind)%v_dadr = 0.0_dp
462 lri_v_int(ikind)%v_dfdr = 0.0_dp
463 END IF
464 END DO
465 END DO
466
467 ! full density kinetic energy term
468 atomlist = 1
469 CALL lri_kg_rho_update(rho_struct, qs_env, lri_env, lri_density, atomlist)
470 ekin_imol = 0.0_dp
471 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=kg_env%xc_section_kg, &
472 vxc_rho=vxc_rho, vxc_tau=vxc_tau, exc=ekin_imol)
473 IF (ASSOCIATED(vxc_tau)) THEN
474 cpabort(" KG with meta-kinetic energy functionals not implemented")
475 END IF
476 DO ispin = 1, nspins
477 CALL pw_scale(vxc_rho(ispin), vxc_rho(ispin)%pw_grid%dvol)
478 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
479 CALL integrate_v_rspace_one_center(vxc_rho(ispin), qs_env, lri_v_int, calc_force, "LRI_AUX")
480 CALL auxbas_pw_pool%give_back_pw(vxc_rho(ispin))
481 END DO
482 DEALLOCATE (vxc_rho)
483 ekin_mol = -ekin_imol
484 xcvirial(1:3, 1:3) = 0.0_dp
485 IF (use_virial) xcvirial(1:3, 1:3) = xcvirial(1:3, 1:3) - virial%pv_xc(1:3, 1:3)
486
487 ! loop over all subsets
488 DO isub = 1, kg_env%nsubsets
489 atomlist = 0
490 DO iatom = 1, natom
491 imol = kg_env%atom_to_molecule(iatom)
492 color = kg_env%subset_of_mol(imol)
493 IF (color == isub) atomlist(iatom) = 1
494 END DO
495 CALL lri_kg_rho_update(rho_struct, qs_env, lri_env, lri_density, atomlist)
496
497 ekin_imol = 0.0_dp
498 ! calc Hohenberg-Kohn kin. energy of the density corresp. to the remaining molecular block(s)
499 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=kg_env%xc_section_kg, &
500 vxc_rho=vxc_rho, vxc_tau=vxc_tau, exc=ekin_imol)
501 ekin_mol = ekin_mol + ekin_imol
502
503 DO ispin = 1, nspins
504 CALL pw_scale(vxc_rho(ispin), -vxc_rho(ispin)%pw_grid%dvol)
505 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
506 CALL integrate_v_rspace_one_center(vxc_rho(ispin), qs_env, &
507 lri_v_int, calc_force, &
508 "LRI_AUX", atomlist=atomlist)
509 ! clean up vxc_rho
510 CALL auxbas_pw_pool%give_back_pw(vxc_rho(ispin))
511 END DO
512 DEALLOCATE (vxc_rho)
513
514 IF (use_virial) THEN
515 xcvirial(1:3, 1:3) = xcvirial(1:3, 1:3) + virial%pv_xc(1:3, 1:3)
516 END IF
517
518 END DO
519
520 IF (use_virial) THEN
521 virial%pv_xc(1:3, 1:3) = xcvirial(1:3, 1:3)
522 END IF
523
524 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
525 ALLOCATE (ksmat(1))
526 DO ispin = 1, nspins
527 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
528 DO ikind = 1, nkind
529 CALL para_env%sum(lri_v_int(ikind)%v_int)
530 END DO
531 ksmat(1)%matrix => ks_matrix(ispin)%matrix
532 CALL calculate_lri_ks_matrix(lri_env, lri_v_int, ksmat, atomic_kind_set)
533 END DO
534 IF (calc_force) THEN
535 pmat(1:nspins, 1:1) => density_matrix(1:nspins)
536 CALL calculate_lri_forces(lri_env, lri_density, qs_env, pmat, atomic_kind_set)
537 END IF
538 DEALLOCATE (atomlist, ksmat)
539
540 ! clean up rho_struct
541 CALL qs_rho_unset_rho_ao(rho_struct)
542 CALL qs_rho_release(rho_struct)
543 DEALLOCATE (rho_struct)
544
545 CALL timestop(handle)
546
547 END SUBROUTINE kg_ekin_embed_lri
548
549! **************************************************************************************************
550!> \brief ...
551!> \param qs_env ...
552!> \param kg_env ...
553!> \param ks_matrix ...
554!> \param ekin_mol ...
555!> \param calc_force ...
556!> \param do_kernel Contribution of kinetic energy functional to kernel in response calculation
557!> \param pmat_ext Response density used to fold 2nd deriv or to integrate kinetic energy functional
558! **************************************************************************************************
559 SUBROUTINE kg_ekin_ri_embed(qs_env, kg_env, ks_matrix, ekin_mol, calc_force, &
560 do_kernel, pmat_ext)
561 TYPE(qs_environment_type), POINTER :: qs_env
562 TYPE(kg_environment_type), POINTER :: kg_env
563 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix
564 REAL(kind=dp), INTENT(out) :: ekin_mol
565 LOGICAL :: calc_force, do_kernel
566 TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
567 POINTER :: pmat_ext
568
569 CHARACTER(LEN=*), PARAMETER :: routinen = 'kg_ekin_ri_embed'
570
571 INTEGER :: color, handle, iatom, ikind, imol, &
572 iounit, ispin, isub, natom, nkind, &
573 nspins
574 INTEGER, ALLOCATABLE, DIMENSION(:) :: atomlist
575 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
576 LOGICAL :: use_virial
577 REAL(kind=dp) :: alpha, ekin_imol
578 REAL(kind=dp), DIMENSION(3, 3) :: xcvirial
579 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
580 TYPE(cp_logger_type), POINTER :: logger
581 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: density_matrix, ksmat
582 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: pmat
583 TYPE(dft_control_type), POINTER :: dft_control
584 TYPE(lri_density_type), POINTER :: lri_density, lri_rho1
585 TYPE(lri_environment_type), POINTER :: lri_env, lri_env1
586 TYPE(lri_kind_type), DIMENSION(:), POINTER :: lri_v_int
587 TYPE(mp_para_env_type), POINTER :: para_env
588 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho1_g
589 TYPE(pw_env_type), POINTER :: pw_env
590 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
591 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho1_r, tau1_r, vxc_rho, vxc_tau
592 TYPE(qs_ks_env_type), POINTER :: ks_env
593 TYPE(qs_rho_type), POINTER :: rho, rho1, rho_struct
594 TYPE(section_vals_type), POINTER :: xc_section
595 TYPE(virial_type), POINTER :: virial
596
597 CALL timeset(routinen, handle)
598
599 logger => cp_get_default_logger()
600 iounit = cp_logger_get_default_unit_nr(logger)
601
602 CALL get_qs_env(qs_env, &
603 ks_env=ks_env, &
604 rho=rho, &
605 natom=natom, &
606 nkind=nkind, &
607 dft_control=dft_control, &
608 virial=virial, &
609 para_env=para_env, &
610 pw_env=pw_env)
611
612 nspins = dft_control%nspins
613 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
614 use_virial = use_virial .AND. calc_force
615
616 ! Kernel potential in response calculation (no forces calculated at this point)
617 ! requires spin-factor
618 ! alpha = 2 closed-shell
619 ! alpha = 1 open-shell
620 alpha = 1.0_dp
621 IF (do_kernel .AND. .NOT. calc_force .AND. nspins == 1) alpha = 2.0_dp
622
623 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
624
625 ! get the density matrix
626 CALL qs_rho_get(rho, rho_ao=density_matrix)
627 ! allocate and initialize the density
628 NULLIFY (rho_struct)
629 ALLOCATE (rho_struct)
630 CALL qs_rho_create(rho_struct)
631 ! set the density matrix to the blocked matrix
632 CALL qs_rho_set(rho_struct, rho_ao=density_matrix)
633 CALL qs_rho_rebuild(rho_struct, qs_env, rebuild_ao=.false., rebuild_grids=.true.)
634
635 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
636 ALLOCATE (cell_to_index(1, 1, 1))
637 cell_to_index(1, 1, 1) = 1
638 lri_env => kg_env%lri_env
639 lri_density => kg_env%lri_density
640
641 NULLIFY (pmat)
642 ALLOCATE (pmat(nspins, 1))
643 DO ispin = 1, nspins
644 pmat(ispin, 1)%matrix => density_matrix(ispin)%matrix
645 END DO
646 CALL calculate_lri_densities(lri_env, lri_density, qs_env, pmat, cell_to_index, &
647 rho_struct, atomic_kind_set, para_env, response_density=.false.)
648 kg_env%lri_density => lri_density
649
650 DEALLOCATE (pmat)
651
652 IF (PRESENT(pmat_ext)) THEN
653 ! If external density associated then it is needed either for
654 ! 1) folding of second derivative while partially integrating, or
655 ! 2) integration of response forces
656 NULLIFY (rho1)
657 ALLOCATE (rho1)
658 CALL qs_rho_create(rho1)
659 CALL qs_rho_set(rho1, rho_ao=pmat_ext)
660 CALL qs_rho_rebuild(rho1, qs_env, rebuild_ao=.false., rebuild_grids=.true.)
661
662 lri_env1 => kg_env%lri_env1
663 lri_rho1 => kg_env%lri_rho1
664 ! calculate external density as LRI-densities
665 NULLIFY (pmat)
666 ALLOCATE (pmat(nspins, 1))
667 DO ispin = 1, nspins
668 pmat(ispin, 1)%matrix => pmat_ext(ispin)%matrix
669 END DO
670 CALL calculate_lri_densities(lri_env1, lri_rho1, qs_env, pmat, cell_to_index, &
671 rho1, atomic_kind_set, para_env, response_density=.false.)
672 kg_env%lri_rho1 => lri_rho1
673 DEALLOCATE (pmat)
674
675 END IF
676
677 ! XC-section pointing to kinetic energy functional in KG environment
678 NULLIFY (xc_section)
679 xc_section => kg_env%xc_section_kg
680
681 ! full density kinetic energy term
682 ekin_imol = 0.0_dp
683 NULLIFY (vxc_rho, vxc_tau)
684
685 ! calculate xc potential or kernel
686 IF (do_kernel) THEN
687 ! kernel total
688 ! derivation wrt to rho_struct and evaluation at rho_struct
689 CALL qs_rho_get(rho1, rho_r=rho1_r, rho_g=rho1_g, tau_r=tau1_r)
690 CALL create_kernel(qs_env, &
691 vxc=vxc_rho, &
692 vxc_tau=vxc_tau, &
693 rho=rho_struct, &
694 rho1_r=rho1_r, &
695 rho1_g=rho1_g, &
696 tau1_r=tau1_r, &
697 xc_section=xc_section)
698 ELSE
699 ! vxc total
700 CALL qs_vxc_create(ks_env=ks_env, &
701 rho_struct=rho_struct, &
702 xc_section=xc_section, &
703 vxc_rho=vxc_rho, &
704 vxc_tau=vxc_tau, &
705 exc=ekin_imol)
706
707 END IF
708
709 IF (ASSOCIATED(vxc_tau)) THEN
710 cpabort(" KG with meta-kinetic energy functionals not implemented")
711 END IF
712
713 DO ispin = 1, nspins
714 CALL pw_scale(vxc_rho(ispin), alpha*vxc_rho(ispin)%pw_grid%dvol)
715
716 IF (PRESENT(pmat_ext) .AND. .NOT. do_kernel) THEN
717 ! int w/ pmat_ext
718 lri_v_int => lri_rho1%lri_coefs(ispin)%lri_kinds
719 ELSE
720 ! int w/ rho_ao
721 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
722 END IF
723 CALL integrate_v_rspace_one_center(vxc_rho(ispin), qs_env, lri_v_int, calc_force, "LRI_AUX")
724 CALL auxbas_pw_pool%give_back_pw(vxc_rho(ispin))
725 END DO
726
727 DEALLOCATE (vxc_rho)
728 ekin_mol = -ekin_imol
729 xcvirial(1:3, 1:3) = 0.0_dp
730 IF (use_virial) xcvirial(1:3, 1:3) = xcvirial(1:3, 1:3) - virial%pv_xc(1:3, 1:3)
731
732 ! loop over all subsets
733 ALLOCATE (atomlist(natom))
734 DO isub = 1, kg_env%nsubsets
735 atomlist = 0
736 DO iatom = 1, natom
737 imol = kg_env%atom_to_molecule(iatom)
738 color = kg_env%subset_of_mol(imol)
739 IF (color == isub) atomlist(iatom) = 1
740 END DO
741 ! update ground-state density
742 CALL lri_kg_rho_update(rho_struct, qs_env, lri_env, lri_density, atomlist)
743
744 ! Same for external (response) density if present
745 IF (PRESENT(pmat_ext)) THEN
746 ! update response density
747 CALL lri_kg_rho_update(rho1, qs_env, lri_env1, lri_rho1, atomlist)
748 END IF
749
750 ekin_imol = 0.0_dp
751 ! calc Hohenberg-Kohn kin. energy of the density corresp. to the remaining molecular block(s)
752 NULLIFY (vxc_rho, vxc_tau)
753
754 ! calculate xc potential or kernel
755 IF (do_kernel) THEN
756 ! subsys kernel
757 CALL qs_rho_get(rho1, rho_r=rho1_r, rho_g=rho1_g, tau_r=tau1_r)
758 CALL create_kernel(qs_env, &
759 vxc=vxc_rho, &
760 vxc_tau=vxc_tau, &
761 rho=rho_struct, &
762 rho1_r=rho1_r, &
763 rho1_g=rho1_g, &
764 tau1_r=tau1_r, &
765 xc_section=xc_section)
766 ELSE
767
768 ! subsys xc-potential
769 CALL qs_vxc_create(ks_env=ks_env, &
770 rho_struct=rho_struct, &
771 xc_section=xc_section, &
772 vxc_rho=vxc_rho, &
773 vxc_tau=vxc_tau, &
774 exc=ekin_imol)
775 END IF
776 ekin_mol = ekin_mol + ekin_imol
777
778 DO ispin = 1, nspins
779 CALL pw_scale(vxc_rho(ispin), -alpha*vxc_rho(ispin)%pw_grid%dvol)
780
781 IF (PRESENT(pmat_ext) .AND. .NOT. do_kernel) THEN
782 ! int w/ pmat_ext
783 lri_v_int => lri_rho1%lri_coefs(ispin)%lri_kinds
784 ELSE
785 ! int w/ rho_ao
786 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
787 END IF
788
789 CALL integrate_v_rspace_one_center(vxc_rho(ispin), qs_env, &
790 lri_v_int, calc_force, &
791 "LRI_AUX", atomlist=atomlist)
792 ! clean up vxc_rho
793 CALL auxbas_pw_pool%give_back_pw(vxc_rho(ispin))
794 END DO
795 DEALLOCATE (vxc_rho)
796
797 IF (use_virial) THEN
798 xcvirial(1:3, 1:3) = xcvirial(1:3, 1:3) + virial%pv_xc(1:3, 1:3)
799 END IF
800
801 END DO
802
803 IF (use_virial) THEN
804 virial%pv_xc(1:3, 1:3) = xcvirial(1:3, 1:3)
805 END IF
806
807 ALLOCATE (ksmat(1))
808 DO ispin = 1, nspins
809 ksmat(1)%matrix => ks_matrix(ispin)%matrix
810 IF (PRESENT(pmat_ext) .AND. .NOT. do_kernel) THEN
811 ! KS int with rho_ext"
812 lri_v_int => lri_rho1%lri_coefs(ispin)%lri_kinds
813 DO ikind = 1, nkind
814 CALL para_env%sum(lri_v_int(ikind)%v_int)
815 END DO
816 CALL calculate_lri_ks_matrix(lri_env1, lri_v_int, ksmat, atomic_kind_set)
817 ELSE
818 ! KS int with rho_ao"
819 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
820 DO ikind = 1, nkind
821 CALL para_env%sum(lri_v_int(ikind)%v_int)
822 END DO
823 CALL calculate_lri_ks_matrix(lri_env, lri_v_int, ksmat, atomic_kind_set)
824 END IF
825
826 END DO
827 IF (calc_force) THEN
828
829 NULLIFY (pmat)
830 ALLOCATE (pmat(nspins, 1))
831
832 IF (PRESENT(pmat_ext) .AND. .NOT. do_kernel) THEN
833 ! Forces with rho_ext
834 DO ispin = 1, nspins
835 pmat(ispin, 1)%matrix => pmat_ext(ispin)%matrix
836 END DO
837 CALL calculate_lri_forces(lri_env1, lri_rho1, qs_env, pmat, atomic_kind_set)
838 ELSE
839 ! Forces with rho_ao
840 DO ispin = 1, nspins
841 pmat(ispin, 1)%matrix => density_matrix(ispin)%matrix
842 END DO
843 CALL calculate_lri_forces(lri_env, lri_density, qs_env, pmat, atomic_kind_set)
844 END IF
845
846 DEALLOCATE (pmat)
847
848 END IF
849 DEALLOCATE (atomlist, ksmat)
850
851 ! clean up rho_struct
852 CALL qs_rho_unset_rho_ao(rho_struct)
853 CALL qs_rho_release(rho_struct)
854 DEALLOCATE (rho_struct)
855 IF (PRESENT(pmat_ext)) THEN
856 CALL qs_rho_unset_rho_ao(rho1)
857 CALL qs_rho_release(rho1)
858 DEALLOCATE (rho1)
859 END IF
860 DEALLOCATE (cell_to_index)
861
862 CALL timestop(handle)
863
864 END SUBROUTINE kg_ekin_ri_embed
865
866! **************************************************************************************************
867!> \brief ...
868!> \param qs_env ...
869!> \param ks_matrix ...
870!> \param ekin_mol ...
871! **************************************************************************************************
872 SUBROUTINE kg_ekin_atomic(qs_env, ks_matrix, ekin_mol)
873 TYPE(qs_environment_type), POINTER :: qs_env
874 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix
875 REAL(kind=dp), INTENT(out) :: ekin_mol
876
877 CHARACTER(LEN=*), PARAMETER :: routinen = 'kg_ekin_atomic'
878
879 INTEGER :: handle, ispin, nspins
880 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: density_matrix, tnadd_matrix
881 TYPE(kg_environment_type), POINTER :: kg_env
882 TYPE(qs_rho_type), POINTER :: rho
883
884 NULLIFY (rho, kg_env, density_matrix, tnadd_matrix)
885
886 CALL timeset(routinen, handle)
887 CALL get_qs_env(qs_env, kg_env=kg_env, rho=rho)
888
889 nspins = SIZE(ks_matrix)
890 ! get the density matrix
891 CALL qs_rho_get(rho, rho_ao=density_matrix)
892 ! get the tnadd matrix
893 tnadd_matrix => kg_env%tnadd_mat
894
895 ekin_mol = 0.0_dp
896 DO ispin = 1, nspins
897 CALL dbcsr_dot(tnadd_matrix(1)%matrix, density_matrix(ispin)%matrix, ekin_mol)
898 CALL dbcsr_add(ks_matrix(ispin)%matrix, tnadd_matrix(1)%matrix, &
899 alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
900 END DO
901 ! definition is inverted (see qs_ks_methods)
902 ekin_mol = -ekin_mol
903
904 CALL timestop(handle)
905
906 END SUBROUTINE kg_ekin_atomic
907
908END MODULE kg_correction
Define the atomic kind types and their sub types.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
various routines to log and control the output. The idea is that decisions about where to log should ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Routines used for Harris functional Kohn-Sham calculation.
Definition ec_methods.F:15
subroutine, public create_kernel(qs_env, vxc, vxc_tau, rho, rho1_r, rho1_g, tau1_r, xc_section, compute_virial, virial_xc)
Creation of second derivative xc-potential.
Definition ec_methods.F:88
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public kg_tnadd_none
integer, parameter, public kg_tnadd_embed_ri
integer, parameter, public kg_tnadd_embed
integer, parameter, public kg_tnadd_atomic
objects that represent the structure of input sections and the data contained in an input section
Routines for a Kim-Gordon-like partitioning into molecular subunits.
subroutine, public kg_ekin_subset(qs_env, ks_matrix, ekin_mol, calc_force, do_kernel, pmat_ext)
Calculates the subsystem Hohenberg-Kohn kinetic energy and the forces.
Types needed for a Kim-Gordon-like partitioning into molecular subunits.
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Calculates integral matrices for LRIGPW method lri : local resolution of the identity.
subroutine, public lri_kg_rho_update(rho_struct, qs_env, lri_env, lri_density, atomlist)
...
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...
Calculates forces for LRIGPW method lri : local resolution of the identity.
Definition lri_forces.F:16
subroutine, public calculate_lri_forces(lri_env, lri_density, qs_env, pmatrix, atomic_kind_set)
calculates the lri forces
Definition lri_forces.F:81
routines that build the Kohn-Sham matrix for the LRIGPW and xc parts
subroutine, public calculate_lri_ks_matrix(lri_env, lri_v_int, h_matrix, atomic_kind_set, cell_to_index)
update of LRIGPW KS matrix
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 ...
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, 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_nonbond, sab_almo, sab_kp, sab_kp_nosym, 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, 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, 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, ecoul_1c, rho0_s_rs, rho0_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, 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, rhs)
Get the QUICKSTEP environment.
Integrate single or product functions over a potential on a RS grid.
methods of the rho structure (defined in qs_rho_types)
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 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_unset_rho_ao(rho_struct)
Unsets the rho_ao / rho_ao_kp field without calling kpoint_transitional_release().
subroutine, public qs_rho_create(rho)
Allocates a new instance of rho.
subroutine, public qs_rho_release(rho_struct)
releases a rho_struct by decreasing the reference count by one and deallocating if it reaches 0 (to b...
subroutine, public qs_vxc_create(ks_env, rho_struct, xc_section, vxc_rho, vxc_tau, exc, just_energy, edisp, dispersion_env, adiabatic_rescale_factor, pw_env_external)
calculates and allocates the xc potential, already reducing it to the dependence on rho and the one o...
Definition qs_vxc.F:98
Provides all information about an atomic kind.
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Contains all the info needed for KG runs...
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 ...
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.