(git:27c966b)
Loading...
Searching...
No Matches
response_solver.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 Calculate the CPKS equation and the resulting forces
10!> \par History
11!> 03.2014 created
12!> 09.2019 Moved from KG to Kohn-Sham
13!> 11.2019 Moved from energy_correction
14!> 08.2020 AO linear response solver [fbelle]
15!> \author JGH
16! **************************************************************************************************
20 USE admm_types, ONLY: admm_type,&
24 USE cell_types, ONLY: cell_type
27 USE cp_dbcsr_api, ONLY: &
39 USE cp_fm_types, ONLY: cp_fm_create,&
49 USE ec_methods, ONLY: ec_mos_init
59 USE hfx_ri, ONLY: hfx_ri_update_forces,&
61 USE hfx_types, ONLY: hfx_type
62 USE input_constants, ONLY: &
74 USE kinds, ONLY: default_string_length,&
75 dp
76 USE machine, ONLY: m_flush
77 USE mathlib, ONLY: det_3x3
79 USE mulliken, ONLY: ao_charges
82 USE physcon, ONLY: pascal
83 USE pw_env_types, ONLY: pw_env_get,&
85 USE pw_methods, ONLY: pw_axpy,&
86 pw_copy,&
88 pw_scale,&
94 USE pw_types, ONLY: pw_c1d_gs_type,&
106 USE qs_force_types, ONLY: qs_force_type,&
108 USE qs_fxc, ONLY: qs_fxc_create
110 USE qs_integrate_potential, ONLY: integrate_rho_nlcc,&
111 integrate_v_core_rspace,&
112 integrate_v_rspace
113 USE qs_kind_types, ONLY: get_qs_kind,&
116 USE qs_ks_atom, ONLY: update_ks_atom
118 USE qs_ks_types, ONLY: qs_ks_env_type
126 USE qs_mo_types, ONLY: deallocate_mo_set,&
127 get_mo_set,&
132 USE qs_p_env_methods, ONLY: p_env_create,&
134 USE qs_p_env_types, ONLY: p_env_release,&
138 USE qs_rho0_methods, ONLY: init_rho0
142 USE qs_rho_types, ONLY: qs_rho_create,&
143 qs_rho_get,&
144 qs_rho_set,&
149 USE virial_types, ONLY: virial_type
155 USE xtb_types, ONLY: get_xtb_atom_param,&
157#include "./base/base_uses.f90"
158
159 IMPLICIT NONE
160
161 PRIVATE
162
163 ! Global parameters
164
165 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'response_solver'
166
169
170! **************************************************************************************************
171
172CONTAINS
173
174! **************************************************************************************************
175!> \brief Initializes solver of linear response equation for energy correction
176!> \brief Call AO or MO based linear response solver for energy correction
177!>
178!> \param qs_env The quickstep environment
179!> \param ec_env The energy correction environment
180!> \param silent ...
181!> \date 01.2020
182!> \author Fabian Belleflamme
183! **************************************************************************************************
184 SUBROUTINE response_calculation(qs_env, ec_env, silent)
185 TYPE(qs_environment_type), POINTER :: qs_env
186 TYPE(energy_correction_type), POINTER :: ec_env
187 LOGICAL, INTENT(IN), OPTIONAL :: silent
188
189 CHARACTER(LEN=*), PARAMETER :: routinen = 'response_calculation'
190
191 INTEGER :: handle, homo, ispin, nao, nao_aux, nmo, &
192 nocc, nspins, solver_method, unit_nr
193 LOGICAL :: should_stop
194 REAL(kind=dp) :: focc
195 TYPE(admm_type), POINTER :: admm_env
196 TYPE(cp_blacs_env_type), POINTER :: blacs_env
197 TYPE(cp_fm_struct_type), POINTER :: fm_struct
198 TYPE(cp_fm_type) :: sv
199 TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: cpmos, mo_occ
200 TYPE(cp_fm_type), POINTER :: mo_coeff
201 TYPE(cp_logger_type), POINTER :: logger
202 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, matrix_s_aux, rho_ao
203 TYPE(dft_control_type), POINTER :: dft_control
204 TYPE(linres_control_type), POINTER :: linres_control
205 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
206 TYPE(mp_para_env_type), POINTER :: para_env
207 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
208 POINTER :: sab_orb
209 TYPE(qs_energy_type), POINTER :: energy
210 TYPE(qs_p_env_type), POINTER :: p_env
211 TYPE(qs_rho_type), POINTER :: rho
212 TYPE(section_vals_type), POINTER :: input, solver_section
213
214 CALL timeset(routinen, handle)
215
216 NULLIFY (admm_env, dft_control, energy, logger, matrix_s, matrix_s_aux, mo_coeff, mos, para_env, &
217 rho_ao, sab_orb, solver_section)
218
219 ! Get useful output unit
220 logger => cp_get_default_logger()
221 IF (logger%para_env%is_source()) THEN
222 unit_nr = cp_logger_get_default_unit_nr(logger, local=.true.)
223 ELSE
224 unit_nr = -1
225 END IF
226
227 CALL get_qs_env(qs_env, &
228 dft_control=dft_control, &
229 input=input, &
230 matrix_s=matrix_s, &
231 para_env=para_env, &
232 sab_orb=sab_orb)
233 nspins = dft_control%nspins
234
235 ! initialize linres_control
236 NULLIFY (linres_control)
237 ALLOCATE (linres_control)
238 linres_control%do_kernel = .true.
239 linres_control%lr_triplet = .false.
240 linres_control%converged = .false.
241 linres_control%energy_gap = 0.02_dp
242
243 ! Read input
244 solver_section => section_vals_get_subs_vals(input, "DFT%ENERGY_CORRECTION%RESPONSE_SOLVER")
245 CALL section_vals_val_get(solver_section, "EPS", r_val=linres_control%eps)
246 CALL section_vals_val_get(solver_section, "EPS_FILTER", r_val=linres_control%eps_filter)
247 CALL section_vals_val_get(solver_section, "MAX_ITER", i_val=linres_control%max_iter)
248 CALL section_vals_val_get(solver_section, "METHOD", i_val=solver_method)
249 CALL section_vals_val_get(solver_section, "PRECONDITIONER", i_val=linres_control%preconditioner_type)
250 CALL section_vals_val_get(solver_section, "RESTART", l_val=linres_control%linres_restart)
251 CALL section_vals_val_get(solver_section, "RESTART_EVERY", i_val=linres_control%restart_every)
252 CALL set_qs_env(qs_env, linres_control=linres_control)
253
254 ! Write input section of response solver
255 CALL response_solver_write_input(solver_section, linres_control, unit_nr, silent=silent)
256
257 ! Allocate and initialize response density matrix Z,
258 ! and the energy weighted response density matrix
259 ! Template is the ground-state overlap matrix
260 CALL dbcsr_allocate_matrix_set(ec_env%matrix_wz, nspins)
261 CALL dbcsr_allocate_matrix_set(ec_env%matrix_z, nspins)
262 DO ispin = 1, nspins
263 ALLOCATE (ec_env%matrix_wz(ispin)%matrix)
264 ALLOCATE (ec_env%matrix_z(ispin)%matrix)
265 CALL dbcsr_create(ec_env%matrix_wz(ispin)%matrix, name="Wz MATRIX", &
266 template=matrix_s(1)%matrix)
267 CALL dbcsr_create(ec_env%matrix_z(ispin)%matrix, name="Z MATRIX", &
268 template=matrix_s(1)%matrix)
269 CALL cp_dbcsr_alloc_block_from_nbl(ec_env%matrix_wz(ispin)%matrix, sab_orb)
270 CALL cp_dbcsr_alloc_block_from_nbl(ec_env%matrix_z(ispin)%matrix, sab_orb)
271 CALL dbcsr_set(ec_env%matrix_wz(ispin)%matrix, 0.0_dp)
272 CALL dbcsr_set(ec_env%matrix_z(ispin)%matrix, 0.0_dp)
273 END DO
274
275 ! MO solver requires MO's of the ground-state calculation,
276 ! The MOs environment is not allocated if LS-DFT has been used.
277 ! Introduce MOs here
278 ! Remark: MOS environment also required for creation of p_env
279 IF (dft_control%qs_control%do_ls_scf) THEN
280
281 ! Allocate and initialize MO environment
282 CALL ec_mos_init(qs_env, matrix_s(1)%matrix)
283 CALL get_qs_env(qs_env, mos=mos, rho=rho)
284
285 ! Get ground-state density matrix
286 CALL qs_rho_get(rho, rho_ao=rho_ao)
287
288 DO ispin = 1, nspins
289 CALL get_mo_set(mo_set=mos(ispin), &
290 mo_coeff=mo_coeff, &
291 nmo=nmo, nao=nao, homo=homo)
292
293 CALL cp_fm_set_all(mo_coeff, 0.0_dp)
294 CALL cp_fm_init_random(mo_coeff, nmo)
295
296 CALL cp_fm_create(sv, mo_coeff%matrix_struct, "SV")
297 ! multiply times PS
298 ! PS*C(:,1:nomo)+C(:,nomo+1:nmo) (nomo=NINT(nelectron/maxocc))
299 CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, mo_coeff, sv, nmo)
300 CALL cp_dbcsr_sm_fm_multiply(rho_ao(ispin)%matrix, sv, mo_coeff, homo)
301 CALL cp_fm_release(sv)
302 ! and ortho the result
303 CALL make_basis_sm(mo_coeff, nmo, matrix_s(1)%matrix)
304
305 ! rebuilds fm_pools
306 ! originally done in qs_env_setup, only when mos associated
307 NULLIFY (blacs_env)
308 CALL get_qs_env(qs_env, blacs_env=blacs_env)
309 CALL mpools_rebuild_fm_pools(qs_env%mpools, mos=mos, &
310 blacs_env=blacs_env, para_env=para_env)
311 END DO
312 END IF
313
314 ! initialize p_env
315 ! Remark: mos environment is needed for this
316 IF (ASSOCIATED(ec_env%p_env)) THEN
317 CALL p_env_release(ec_env%p_env)
318 DEALLOCATE (ec_env%p_env)
319 NULLIFY (ec_env%p_env)
320 END IF
321 ALLOCATE (ec_env%p_env)
322 CALL p_env_create(ec_env%p_env, qs_env, orthogonal_orbitals=.true., &
323 linres_control=linres_control)
324 CALL p_env_psi0_changed(ec_env%p_env, qs_env)
325 ! Total energy overwritten, replace with Etot from energy correction
326 CALL get_qs_env(qs_env, energy=energy)
327 energy%total = ec_env%etotal
328 !
329 p_env => ec_env%p_env
330 !
331 CALL dbcsr_allocate_matrix_set(p_env%p1, nspins)
332 CALL dbcsr_allocate_matrix_set(p_env%w1, nspins)
333 DO ispin = 1, nspins
334 ALLOCATE (p_env%p1(ispin)%matrix, p_env%w1(ispin)%matrix)
335 CALL dbcsr_create(matrix=p_env%p1(ispin)%matrix, template=matrix_s(1)%matrix)
336 CALL dbcsr_create(matrix=p_env%w1(ispin)%matrix, template=matrix_s(1)%matrix)
337 CALL cp_dbcsr_alloc_block_from_nbl(p_env%p1(ispin)%matrix, sab_orb)
338 CALL cp_dbcsr_alloc_block_from_nbl(p_env%w1(ispin)%matrix, sab_orb)
339 END DO
340 IF (dft_control%do_admm) THEN
341 CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux)
342 CALL dbcsr_allocate_matrix_set(p_env%p1_admm, nspins)
343 DO ispin = 1, nspins
344 ALLOCATE (p_env%p1_admm(ispin)%matrix)
345 CALL dbcsr_create(p_env%p1_admm(ispin)%matrix, &
346 template=matrix_s_aux(1)%matrix)
347 CALL dbcsr_copy(p_env%p1_admm(ispin)%matrix, matrix_s_aux(1)%matrix)
348 CALL dbcsr_set(p_env%p1_admm(ispin)%matrix, 0.0_dp)
349 END DO
350 END IF
351
352 ! Choose between MO-solver and AO-solver
353 SELECT CASE (solver_method)
354 CASE (ec_mo_solver)
355
356 ! CPKS vector cpmos - RHS of response equation as Ax + b = 0 (sign of b)
357 ! Sign is changed in linres_solver!
358 ! Projector Q applied in linres_solver!
359 IF (ASSOCIATED(ec_env%cpmos)) THEN
360
361 CALL response_equation_new(qs_env, p_env, ec_env%cpmos, unit_nr, silent=silent)
362
363 ELSE
364 CALL get_qs_env(qs_env, mos=mos)
365 ALLOCATE (cpmos(nspins), mo_occ(nspins))
366 DO ispin = 1, nspins
367 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, homo=nocc)
368 NULLIFY (fm_struct)
369 CALL cp_fm_struct_create(fm_struct, ncol_global=nocc, &
370 template_fmstruct=mo_coeff%matrix_struct)
371 CALL cp_fm_create(cpmos(ispin), fm_struct)
372 CALL cp_fm_set_all(cpmos(ispin), 0.0_dp)
373 CALL cp_fm_create(mo_occ(ispin), fm_struct)
374 CALL cp_fm_to_fm(mo_coeff, mo_occ(ispin), nocc)
375 CALL cp_fm_struct_release(fm_struct)
376 END DO
377
378 focc = 2.0_dp
379 IF (nspins == 1) focc = 4.0_dp
380 DO ispin = 1, nspins
381 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, homo=nocc)
382 CALL cp_dbcsr_sm_fm_multiply(ec_env%matrix_hz(ispin)%matrix, mo_occ(ispin), &
383 cpmos(ispin), nocc, &
384 alpha=focc, beta=0.0_dp)
385 END DO
386 CALL cp_fm_release(mo_occ)
387
388 CALL response_equation_new(qs_env, p_env, cpmos, unit_nr, silent=silent)
389
390 CALL cp_fm_release(cpmos)
391 END IF
392
393 ! Get the response density matrix,
394 ! and energy-weighted response density matrix
395 DO ispin = 1, nspins
396 CALL dbcsr_copy(ec_env%matrix_z(ispin)%matrix, p_env%p1(ispin)%matrix)
397 CALL dbcsr_copy(ec_env%matrix_wz(ispin)%matrix, p_env%w1(ispin)%matrix)
398 END DO
399
400 CASE (ec_ls_solver)
401
402 IF (ec_env%energy_functional == ec_functional_ext) THEN
403 cpabort("AO Response Solver NYA for External Functional")
404 END IF
405
406 ! AO ortho solver
407 CALL ec_response_ao(qs_env=qs_env, &
408 p_env=p_env, &
409 matrix_hz=ec_env%matrix_hz, &
410 matrix_pz=ec_env%matrix_z, &
411 matrix_wz=ec_env%matrix_wz, &
412 iounit=unit_nr, &
413 should_stop=should_stop, &
414 silent=silent)
415
416 IF (dft_control%do_admm) THEN
417 CALL get_qs_env(qs_env, admm_env=admm_env)
418 cpassert(ASSOCIATED(admm_env%work_orb_orb))
419 cpassert(ASSOCIATED(admm_env%work_aux_orb))
420 cpassert(ASSOCIATED(admm_env%work_aux_aux))
421 nao = admm_env%nao_orb
422 nao_aux = admm_env%nao_aux_fit
423 DO ispin = 1, nspins
424 CALL copy_dbcsr_to_fm(ec_env%matrix_z(ispin)%matrix, admm_env%work_orb_orb)
425 CALL parallel_gemm('N', 'N', nao_aux, nao, nao, &
426 1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
427 admm_env%work_aux_orb)
428 CALL parallel_gemm('N', 'T', nao_aux, nao_aux, nao, &
429 1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
430 admm_env%work_aux_aux)
431 CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, p_env%p1_admm(ispin)%matrix, &
432 keep_sparsity=.true.)
433 END DO
434 END IF
435
436 CASE DEFAULT
437 cpabort("Unknown solver for response equation requested")
438 END SELECT
439
440 IF (dft_control%do_admm) THEN
441 CALL dbcsr_allocate_matrix_set(ec_env%z_admm, nspins)
442 DO ispin = 1, nspins
443 ALLOCATE (ec_env%z_admm(ispin)%matrix)
444 CALL dbcsr_create(matrix=ec_env%z_admm(ispin)%matrix, template=matrix_s_aux(1)%matrix)
445 CALL get_qs_env(qs_env, admm_env=admm_env)
446 CALL dbcsr_copy(ec_env%z_admm(ispin)%matrix, p_env%p1_admm(ispin)%matrix)
447 END DO
448 END IF
449
450 ! Get rid of MO environment again
451 IF (dft_control%qs_control%do_ls_scf) THEN
452 DO ispin = 1, nspins
453 CALL deallocate_mo_set(mos(ispin))
454 END DO
455 IF (ASSOCIATED(qs_env%mos)) THEN
456 DO ispin = 1, SIZE(qs_env%mos)
457 CALL deallocate_mo_set(qs_env%mos(ispin))
458 END DO
459 DEALLOCATE (qs_env%mos)
460 END IF
461 END IF
462
463 CALL timestop(handle)
464
465 END SUBROUTINE response_calculation
466
467! **************************************************************************************************
468!> \brief Parse the input section of the response solver
469!> \param input Input section which controls response solver parameters
470!> \param linres_control Environment for general setting of linear response calculation
471!> \param unit_nr ...
472!> \param silent ...
473!> \par History
474!> 2020.05 created [Fabian Belleflamme]
475!> \author Fabian Belleflamme
476! **************************************************************************************************
477 SUBROUTINE response_solver_write_input(input, linres_control, unit_nr, silent)
478 TYPE(section_vals_type), POINTER :: input
479 TYPE(linres_control_type), POINTER :: linres_control
480 INTEGER, INTENT(IN) :: unit_nr
481 LOGICAL, INTENT(IN), OPTIONAL :: silent
482
483 CHARACTER(len=*), PARAMETER :: routinen = 'response_solver_write_input'
484
485 INTEGER :: handle, max_iter_lanczos, s_sqrt_method, &
486 s_sqrt_order, solver_method
487 LOGICAL :: my_silent
488 REAL(kind=dp) :: eps_lanczos
489
490 CALL timeset(routinen, handle)
491
492 my_silent = .false.
493 IF (PRESENT(silent)) my_silent = silent
494
495 IF (unit_nr > 0) THEN
496
497 ! linres_control
498 WRITE (unit_nr, '(/,T2,A)') &
499 repeat("-", 30)//" Linear Response Solver "//repeat("-", 25)
500
501 IF (.NOT. my_silent) THEN
502 ! Which type of solver is used
503 CALL section_vals_val_get(input, "METHOD", i_val=solver_method)
504
505 SELECT CASE (solver_method)
506 CASE (ec_ls_solver)
507 WRITE (unit_nr, '(T2,A,T61,A20)') "Solver: ", "AO-based CG-solver"
508 CASE (ec_mo_solver)
509 WRITE (unit_nr, '(T2,A,T61,A20)') "Solver: ", "MO-based CG-solver"
510 END SELECT
511
512 WRITE (unit_nr, '(T2,A,T61,E20.3)') "eps:", linres_control%eps
513 WRITE (unit_nr, '(T2,A,T61,E20.3)') "eps_filter:", linres_control%eps_filter
514 WRITE (unit_nr, '(T2,A,T61,I20)') "Max iter:", linres_control%max_iter
515
516 SELECT CASE (linres_control%preconditioner_type)
518 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "FULL_ALL"
520 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "FULL_SINGLE_INVERSE"
522 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "FULL_SINGLE"
524 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "FULL_KINETIC"
526 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "FULL_S_INVERSE"
527 CASE (precond_mlp)
528 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "MULTI_LEVEL"
529 CASE (ot_precond_none)
530 WRITE (unit_nr, '(T2,A,T61,A20)') "Preconditioner: ", "NONE"
531 END SELECT
532
533 SELECT CASE (solver_method)
534 CASE (ec_ls_solver)
535
536 CALL section_vals_val_get(input, "S_SQRT_METHOD", i_val=s_sqrt_method)
537 CALL section_vals_val_get(input, "S_SQRT_ORDER", i_val=s_sqrt_order)
538 CALL section_vals_val_get(input, "EPS_LANCZOS", r_val=eps_lanczos)
539 CALL section_vals_val_get(input, "MAX_ITER_LANCZOS", i_val=max_iter_lanczos)
540
541 ! Response solver transforms P and KS into orthonormal basis,
542 ! reuires matrx S sqrt and its inverse
543 SELECT CASE (s_sqrt_method)
544 CASE (ls_s_sqrt_ns)
545 WRITE (unit_nr, '(T2,A,T61,A20)') "S sqrt method:", "NEWTONSCHULZ"
546 CASE (ls_s_sqrt_proot)
547 WRITE (unit_nr, '(T2,A,T61,A20)') "S sqrt method:", "PROOT"
548 CASE DEFAULT
549 cpabort("Unknown sqrt method.")
550 END SELECT
551 WRITE (unit_nr, '(T2,A,T61,I20)') "S sqrt order:", s_sqrt_order
552
553 CASE (ec_mo_solver)
554 END SELECT
555
556 WRITE (unit_nr, '(T2,A)') repeat("-", 79)
557
558 END IF
559
560 CALL m_flush(unit_nr)
561 END IF
562
563 CALL timestop(handle)
564
565 END SUBROUTINE response_solver_write_input
566
567! **************************************************************************************************
568!> \brief Initializes vectors for MO-coefficient based linear response solver
569!> and calculates response density, and energy-weighted response density matrix
570!>
571!> \param qs_env ...
572!> \param p_env ...
573!> \param cpmos ...
574!> \param iounit ...
575!> \param silent ...
576! **************************************************************************************************
577 SUBROUTINE response_equation_new(qs_env, p_env, cpmos, iounit, silent)
578 TYPE(qs_environment_type), POINTER :: qs_env
579 TYPE(qs_p_env_type) :: p_env
580 TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT) :: cpmos
581 INTEGER, INTENT(IN) :: iounit
582 LOGICAL, INTENT(IN), OPTIONAL :: silent
583
584 CHARACTER(LEN=*), PARAMETER :: routinen = 'response_equation_new'
585
586 INTEGER :: handle, ispin, nao, nao_aux, nocc, nspins
587 LOGICAL :: should_stop, uniform_occupation
588 REAL(kind=dp), DIMENSION(:), POINTER :: occupation
589 TYPE(admm_type), POINTER :: admm_env
590 TYPE(cp_fm_struct_type), POINTER :: fm_struct
591 TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: psi0, psi1
592 TYPE(cp_fm_type), POINTER :: mo_coeff
593 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
594 TYPE(dft_control_type), POINTER :: dft_control
595 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
596
597 CALL timeset(routinen, handle)
598
599 NULLIFY (dft_control, matrix_ks, mo_coeff, mos)
600
601 CALL get_qs_env(qs_env, dft_control=dft_control, matrix_ks=matrix_ks, &
602 matrix_s=matrix_s, mos=mos)
603 nspins = dft_control%nspins
604
605 ! Initialize vectors:
606 ! psi0 : The ground-state MO-coefficients
607 ! psi1 : The "perturbed" linear response orbitals
608 ALLOCATE (psi0(nspins), psi1(nspins))
609 DO ispin = 1, nspins
610 CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, homo=nocc, &
611 uniform_occupation=uniform_occupation)
612 IF (.NOT. uniform_occupation) THEN
613 CALL get_mo_set(mos(ispin), occupation_numbers=occupation)
614 cpassert(all(occupation(1:nocc) == occupation(1)))
615 END IF
616 NULLIFY (fm_struct)
617 CALL cp_fm_struct_create(fm_struct, ncol_global=nocc, &
618 template_fmstruct=mo_coeff%matrix_struct)
619 CALL cp_fm_create(psi0(ispin), fm_struct)
620 CALL cp_fm_to_fm(mo_coeff, psi0(ispin), nocc)
621 CALL cp_fm_create(psi1(ispin), fm_struct)
622 CALL cp_fm_set_all(psi1(ispin), 0.0_dp)
623 CALL cp_fm_struct_release(fm_struct)
624 END DO
625
626 should_stop = .false.
627 ! The response solver
628 CALL linres_solver(p_env, qs_env, psi1, cpmos, psi0, iounit, &
629 should_stop, silent=silent)
630
631 ! Building the response density matrix
632 DO ispin = 1, nspins
633 CALL dbcsr_copy(p_env%p1(ispin)%matrix, matrix_s(1)%matrix)
634 END DO
635 CALL build_dm_response(psi0, psi1, p_env%p1)
636 DO ispin = 1, nspins
637 CALL dbcsr_scale(p_env%p1(ispin)%matrix, 0.5_dp)
638 END DO
639
640 IF (dft_control%do_admm) THEN
641 CALL get_qs_env(qs_env, admm_env=admm_env)
642 cpassert(ASSOCIATED(admm_env%work_orb_orb))
643 cpassert(ASSOCIATED(admm_env%work_aux_orb))
644 cpassert(ASSOCIATED(admm_env%work_aux_aux))
645 nao = admm_env%nao_orb
646 nao_aux = admm_env%nao_aux_fit
647 DO ispin = 1, nspins
648 CALL copy_dbcsr_to_fm(p_env%p1(ispin)%matrix, admm_env%work_orb_orb)
649 CALL parallel_gemm('N', 'N', nao_aux, nao, nao, &
650 1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
651 admm_env%work_aux_orb)
652 CALL parallel_gemm('N', 'T', nao_aux, nao_aux, nao, &
653 1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
654 admm_env%work_aux_aux)
655 CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, p_env%p1_admm(ispin)%matrix, &
656 keep_sparsity=.true.)
657 END DO
658 END IF
659
660 ! Calculate Wz = 0.5*(psi1*eps*psi0^T + psi0*eps*psi1^T)
661 DO ispin = 1, nspins
662 CALL calculate_wz_matrix(mos(ispin), psi1(ispin), matrix_ks(ispin)%matrix, &
663 p_env%w1(ispin)%matrix)
664 END DO
665 DO ispin = 1, nspins
666 CALL cp_fm_release(cpmos(ispin))
667 END DO
668 CALL cp_fm_release(psi1)
669 CALL cp_fm_release(psi0)
670
671 CALL timestop(handle)
672
673 END SUBROUTINE response_equation_new
674
675! **************************************************************************************************
676!> \brief Initializes vectors for MO-coefficient based linear response solver
677!> and calculates response density, and energy-weighted response density matrix
678!> J. Chem. Theory Comput. 2022, 18, 4186−4202 (https://doi.org/10.1021/acs.jctc.2c00144)
679!>
680!> \param qs_env ...
681!> \param p_env Holds the two results of this routine, p_env%p1 = CZ^T + ZC^T,
682!> p_env%w1 = 0.5\sum_i(C_i*\epsilon_i*Z_i^T + Z_i*\epsilon_i*C_i^T)
683!> \param cpmos RHS of equation as Ax + b = 0 (sign of b)
684!> \param iounit ...
685!> \param lr_section ...
686!> \param silent ...
687! **************************************************************************************************
688 SUBROUTINE response_equation(qs_env, p_env, cpmos, iounit, lr_section, silent)
689 TYPE(qs_environment_type), POINTER :: qs_env
690 TYPE(qs_p_env_type) :: p_env
691 TYPE(cp_fm_type), DIMENSION(:), POINTER :: cpmos
692 INTEGER, INTENT(IN) :: iounit
693 TYPE(section_vals_type), OPTIONAL, POINTER :: lr_section
694 LOGICAL, INTENT(IN), OPTIONAL :: silent
695
696 CHARACTER(LEN=*), PARAMETER :: routinen = 'response_equation'
697
698 INTEGER :: handle, ispin, nao, nao_aux, nocc, nspins
699 LOGICAL :: should_stop
700 TYPE(admm_type), POINTER :: admm_env
701 TYPE(cp_fm_struct_type), POINTER :: fm_struct
702 TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: psi0, psi1
703 TYPE(cp_fm_type), POINTER :: mo_coeff
704 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s, matrix_s_aux
705 TYPE(dft_control_type), POINTER :: dft_control
706 TYPE(linres_control_type), POINTER :: linres_control
707 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
708 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
709 POINTER :: sab_orb
710
711 CALL timeset(routinen, handle)
712
713 ! initialized linres_control
714 NULLIFY (linres_control)
715 ALLOCATE (linres_control)
716 linres_control%do_kernel = .true.
717 linres_control%lr_triplet = .false.
718 IF (PRESENT(lr_section)) THEN
719 CALL section_vals_val_get(lr_section, "RESTART", l_val=linres_control%linres_restart)
720 CALL section_vals_val_get(lr_section, "MAX_ITER", i_val=linres_control%max_iter)
721 CALL section_vals_val_get(lr_section, "EPS", r_val=linres_control%eps)
722 CALL section_vals_val_get(lr_section, "EPS_FILTER", r_val=linres_control%eps_filter)
723 CALL section_vals_val_get(lr_section, "RESTART_EVERY", i_val=linres_control%restart_every)
724 CALL section_vals_val_get(lr_section, "PRECONDITIONER", i_val=linres_control%preconditioner_type)
725 CALL section_vals_val_get(lr_section, "ENERGY_GAP", r_val=linres_control%energy_gap)
726 ELSE
727 linres_control%linres_restart = .false.
728 linres_control%max_iter = 100
729 linres_control%eps = 1.0e-10_dp
730 linres_control%eps_filter = 1.0e-15_dp
731 linres_control%restart_every = 50
732 linres_control%preconditioner_type = ot_precond_full_single_inverse
733 linres_control%energy_gap = 0.02_dp
734 END IF
735
736 ! initialized p_env
737 CALL p_env_create(p_env, qs_env, orthogonal_orbitals=.true., &
738 linres_control=linres_control)
739 CALL set_qs_env(qs_env, linres_control=linres_control)
740 CALL p_env_psi0_changed(p_env, qs_env)
741 p_env%new_preconditioner = .true.
742
743 CALL get_qs_env(qs_env, dft_control=dft_control, mos=mos)
744 !
745 nspins = dft_control%nspins
746
747 ! Initialize vectors:
748 ! psi0 : The ground-state MO-coefficients
749 ! psi1 : The "perturbed" linear response orbitals
750 ALLOCATE (psi0(nspins), psi1(nspins))
751 DO ispin = 1, nspins
752 CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, homo=nocc)
753 NULLIFY (fm_struct)
754 CALL cp_fm_struct_create(fm_struct, ncol_global=nocc, &
755 template_fmstruct=mo_coeff%matrix_struct)
756 CALL cp_fm_create(psi0(ispin), fm_struct)
757 CALL cp_fm_to_fm(mo_coeff, psi0(ispin), nocc)
758 CALL cp_fm_create(psi1(ispin), fm_struct)
759 CALL cp_fm_set_all(psi1(ispin), 0.0_dp)
760 CALL cp_fm_struct_release(fm_struct)
761 END DO
762
763 should_stop = .false.
764 ! The response solver
765 CALL get_qs_env(qs_env, matrix_s=matrix_s, sab_orb=sab_orb)
766 CALL dbcsr_allocate_matrix_set(p_env%p1, nspins)
767 CALL dbcsr_allocate_matrix_set(p_env%w1, nspins)
768 DO ispin = 1, nspins
769 ALLOCATE (p_env%p1(ispin)%matrix, p_env%w1(ispin)%matrix)
770 CALL dbcsr_create(matrix=p_env%p1(ispin)%matrix, template=matrix_s(1)%matrix)
771 CALL dbcsr_create(matrix=p_env%w1(ispin)%matrix, template=matrix_s(1)%matrix)
772 CALL cp_dbcsr_alloc_block_from_nbl(p_env%p1(ispin)%matrix, sab_orb)
773 CALL cp_dbcsr_alloc_block_from_nbl(p_env%w1(ispin)%matrix, sab_orb)
774 END DO
775 IF (dft_control%do_admm) THEN
776 CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux)
777 CALL dbcsr_allocate_matrix_set(p_env%p1_admm, nspins)
778 DO ispin = 1, nspins
779 ALLOCATE (p_env%p1_admm(ispin)%matrix)
780 CALL dbcsr_create(p_env%p1_admm(ispin)%matrix, &
781 template=matrix_s_aux(1)%matrix)
782 CALL dbcsr_copy(p_env%p1_admm(ispin)%matrix, matrix_s_aux(1)%matrix)
783 CALL dbcsr_set(p_env%p1_admm(ispin)%matrix, 0.0_dp)
784 END DO
785 END IF
786
787 CALL linres_solver(p_env, qs_env, psi1, cpmos, psi0, iounit, &
788 should_stop, silent=silent)
789
790 ! Building the response density matrix
791 DO ispin = 1, nspins
792 CALL dbcsr_copy(p_env%p1(ispin)%matrix, matrix_s(1)%matrix)
793 END DO
794 CALL build_dm_response(psi0, psi1, p_env%p1)
795 DO ispin = 1, nspins
796 CALL dbcsr_scale(p_env%p1(ispin)%matrix, 0.5_dp)
797 END DO
798 IF (dft_control%do_admm) THEN
799 CALL get_qs_env(qs_env, admm_env=admm_env)
800 cpassert(ASSOCIATED(admm_env%work_orb_orb))
801 cpassert(ASSOCIATED(admm_env%work_aux_orb))
802 cpassert(ASSOCIATED(admm_env%work_aux_aux))
803 nao = admm_env%nao_orb
804 nao_aux = admm_env%nao_aux_fit
805 DO ispin = 1, nspins
806 CALL copy_dbcsr_to_fm(p_env%p1(ispin)%matrix, admm_env%work_orb_orb)
807 CALL parallel_gemm('N', 'N', nao_aux, nao, nao, &
808 1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
809 admm_env%work_aux_orb)
810 CALL parallel_gemm('N', 'T', nao_aux, nao_aux, nao, &
811 1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
812 admm_env%work_aux_aux)
813 CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, p_env%p1_admm(ispin)%matrix, &
814 keep_sparsity=.true.)
815 END DO
816 END IF
817
818 ! Calculate the second term of Eq. 51 Wz = 0.5*(psi1*eps*psi0^T + psi0*eps*psi1^T)
819 CALL get_qs_env(qs_env, matrix_ks=matrix_ks)
820 DO ispin = 1, nspins
821 CALL calculate_wz_matrix(mos(ispin), psi1(ispin), matrix_ks(ispin)%matrix, &
822 p_env%w1(ispin)%matrix)
823 END DO
824 CALL cp_fm_release(psi0)
825 CALL cp_fm_release(psi1)
826
827 CALL timestop(handle)
828
829 END SUBROUTINE response_equation
830
831! **************************************************************************************************
832!> \brief ...
833!> \param qs_env ...
834!> \param vh_rspace ...
835!> \param vxc_rspace ...
836!> \param vtau_rspace ...
837!> \param vadmm_rspace ...
838!> \param vadmm_tau_rspace ...
839!> \param matrix_hz Right-hand-side of linear response equation
840!> \param matrix_pz Linear response density matrix
841!> \param matrix_pz_admm Linear response density matrix in ADMM basis
842!> \param matrix_wz Energy-weighted linear response density
843!> \param zehartree Hartree volume response contribution to stress tensor
844!> \param zexc XC volume response contribution to stress tensor
845!> \param zexc_aux_fit ADMM XC volume response contribution to stress tensor
846!> \param rhopz_r Response density on real space grid
847!> \param p_env ...
848!> \param ex_env ...
849!> \param debug ...
850! **************************************************************************************************
851 SUBROUTINE response_force(qs_env, vh_rspace, vxc_rspace, vtau_rspace, vadmm_rspace, &
852 vadmm_tau_rspace, matrix_hz, matrix_pz, matrix_pz_admm, matrix_wz, &
853 zehartree, zexc, zexc_aux_fit, rhopz_r, p_env, ex_env, debug)
854 TYPE(qs_environment_type), POINTER :: qs_env
855 TYPE(pw_r3d_rs_type), INTENT(IN) :: vh_rspace
856 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: vxc_rspace, vtau_rspace, vadmm_rspace, &
857 vadmm_tau_rspace
858 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_hz, matrix_pz, matrix_pz_admm, &
859 matrix_wz
860 REAL(kind=dp), OPTIONAL :: zehartree, zexc, zexc_aux_fit
861 TYPE(pw_r3d_rs_type), DIMENSION(:), &
862 INTENT(INOUT), OPTIONAL :: rhopz_r
863 TYPE(qs_p_env_type), OPTIONAL :: p_env
864 TYPE(excited_energy_type), OPTIONAL, POINTER :: ex_env
865 LOGICAL, INTENT(IN), OPTIONAL :: debug
866
867 CHARACTER(LEN=*), PARAMETER :: routinen = 'response_force'
868
869 CHARACTER(LEN=default_string_length) :: basis_type, unitstr
870 INTEGER :: handle, iounit, ispin, mspin, myfun, &
871 n_rep_hf, nao, nao_aux, natom, nder, &
872 nocc, nspins
873 LOGICAL :: debug_forces, debug_stress, distribute_fock_matrix, do_ex, do_hfx, do_onecenter, &
874 gapw, gapw_xc, hfx_treat_lsd_in_core, needs_tau_response, needs_tau_response_aux, &
875 resp_only, s_mstruct_changed, use_virial
876 REAL(kind=dp) :: eh1, ehartree, ekin_mol, eps_filter, &
877 exc, exc_aux_fit, fconv, focc, &
878 hartree_gs, hartree_t
879 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: ftot1, ftot2, ftot3
880 REAL(kind=dp), DIMENSION(2) :: total_rho_gs, total_rho_t
881 REAL(kind=dp), DIMENSION(3) :: fodeb
882 REAL(kind=dp), DIMENSION(3, 3) :: h_stress, pv_loc, stdeb, sttot, sttot2
883 TYPE(admm_type), POINTER :: admm_env
884 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
885 TYPE(cell_type), POINTER :: cell
886 TYPE(cp_logger_type), POINTER :: logger
887 TYPE(dbcsr_distribution_type), POINTER :: dbcsr_dist
888 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ht, matrix_pd, matrix_pza, &
889 matrix_s, mpa, scrm
890 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_h, matrix_p, mhd, mhx, mhy, mhz, &
891 mpa2, mpd, mpz, scrm2
892 TYPE(dbcsr_type), POINTER :: dbwork
893 TYPE(dft_control_type), POINTER :: dft_control
894 TYPE(hartree_local_type), POINTER :: hartree_local_gs, hartree_local_t
895 TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data
896 TYPE(kg_environment_type), POINTER :: kg_env
897 TYPE(local_rho_type), POINTER :: local_rho_set_f, local_rho_set_gs, &
898 local_rho_set_t, local_rho_set_vxc, &
899 local_rhoz_set_admm
900 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
901 TYPE(mp_para_env_type), POINTER :: para_env
902 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
903 POINTER :: sab_aux_fit, sab_orb
904 TYPE(oce_matrix_type), POINTER :: oce
905 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
906 TYPE(pw_c1d_gs_type) :: rho_tot_gspace, rho_tot_gspace_gs, rho_tot_gspace_t, &
907 rhoz_tot_gspace, v_hartree_gspace_gs, v_hartree_gspace_t, zv_hartree_gspace
908 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_gs, rho_g_t, rhoz_g, rhoz_g_aux, &
909 rhoz_g_xc
910 TYPE(pw_c1d_gs_type), POINTER :: rho_core
911 TYPE(pw_env_type), POINTER :: pw_env
912 TYPE(pw_poisson_type), POINTER :: poisson_env
913 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
914 TYPE(pw_r3d_rs_type) :: v_hartree_rspace_gs, v_hartree_rspace_t, &
915 vhxc_rspace, zv_hartree_rspace
916 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r_gs, rho_r_t, rhoz_r, rhoz_r_aux, &
917 rhoz_r_xc, rhoz_tau_r_aux, tauz_r, &
918 tauz_r_xc, v_xc, v_xc_tau
919 TYPE(pw_r3d_rs_type), POINTER :: rho_nlcc
920 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
921 TYPE(qs_kind_type), DIMENSION(:), POINTER :: kind_set, qs_kind_set
922 TYPE(qs_ks_env_type), POINTER :: ks_env
923 TYPE(qs_rho_type), POINTER :: rho, rho0, rho1, rho_aux_fit, rho_xc
924 TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho0_atom_set, rho1_atom_set
925 TYPE(section_vals_type), POINTER :: hfx_section, xc_fun_section, xc_section
926 TYPE(task_list_type), POINTER :: task_list, task_list_aux_fit
927 TYPE(virial_type), POINTER :: virial
928 TYPE(xc_rho_cflags_type) :: needs
929
930 CALL timeset(routinen, handle)
931
932 IF (PRESENT(debug)) THEN
933 debug_forces = debug
934 debug_stress = debug
935 ELSE
936 debug_forces = .false.
937 debug_stress = .false.
938 END IF
939
940 logger => cp_get_default_logger()
941 IF (logger%para_env%is_source()) THEN
942 iounit = cp_logger_get_default_unit_nr(logger, local=.true.)
943 ELSE
944 iounit = -1
945 END IF
946
947 do_ex = .false.
948 IF (PRESENT(ex_env)) do_ex = .true.
949 IF (do_ex) THEN
950 cpassert(PRESENT(p_env))
951 END IF
952
953 NULLIFY (ks_env, sab_orb, virial)
954 CALL get_qs_env(qs_env=qs_env, &
955 cell=cell, &
956 force=force, &
957 ks_env=ks_env, &
958 dft_control=dft_control, &
959 para_env=para_env, &
960 sab_orb=sab_orb, &
961 virial=virial)
962 nspins = dft_control%nspins
963 gapw = dft_control%qs_control%gapw
964 gapw_xc = dft_control%qs_control%gapw_xc
965
966 ! NLCC
967 NULLIFY (rho_nlcc)
968 CALL get_qs_env(qs_env=qs_env, rho_nlcc=rho_nlcc)
969
970 IF (debug_forces) THEN
971 CALL get_qs_env(qs_env, natom=natom, atomic_kind_set=atomic_kind_set)
972 ALLOCATE (ftot1(3, natom))
973 CALL total_qs_force(ftot1, force, atomic_kind_set)
974 END IF
975
976 ! check for virial
977 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
978
979 IF (use_virial .AND. do_ex) THEN
980 CALL cp_abort(__location__, "Stress Tensor not available for TDDFT calculations.")
981 END IF
982
983 fconv = 1.0e-9_dp*pascal/cell%deth
984 IF (debug_stress .AND. use_virial) THEN
985 sttot = virial%pv_virial
986 END IF
987
988 ! *** If LSD, then combine alpha density and beta density to
989 ! *** total density: alpha <- alpha + beta and
990 NULLIFY (mpa)
991 NULLIFY (matrix_ht)
992 IF (do_ex) THEN
993 CALL dbcsr_allocate_matrix_set(mpa, nspins)
994 DO ispin = 1, nspins
995 ALLOCATE (mpa(ispin)%matrix)
996 CALL dbcsr_create(mpa(ispin)%matrix, template=p_env%p1(ispin)%matrix)
997 CALL dbcsr_copy(mpa(ispin)%matrix, p_env%p1(ispin)%matrix)
998 CALL dbcsr_add(mpa(ispin)%matrix, ex_env%matrix_pe(ispin)%matrix, 1.0_dp, 1.0_dp)
999 CALL dbcsr_set(matrix_hz(ispin)%matrix, 0.0_dp)
1000 END DO
1001 ELSE
1002 mpa => matrix_pz
1003 END IF
1004 !
1005 IF (do_ex .OR. (gapw .OR. gapw_xc)) THEN
1006 CALL dbcsr_allocate_matrix_set(matrix_ht, nspins)
1007 DO ispin = 1, nspins
1008 ALLOCATE (matrix_ht(ispin)%matrix)
1009 CALL dbcsr_create(matrix_ht(ispin)%matrix, template=matrix_hz(ispin)%matrix)
1010 CALL dbcsr_copy(matrix_ht(ispin)%matrix, matrix_hz(ispin)%matrix)
1011 CALL dbcsr_set(matrix_ht(ispin)%matrix, 0.0_dp)
1012 END DO
1013 END IF
1014 !
1015 ! START OF Tr[(P+Z)Hcore]
1016 !
1017
1018 ! Kinetic energy matrix
1019 NULLIFY (scrm2)
1020 mpa2(1:nspins, 1:1) => mpa(1:nspins)
1021 CALL kinetic_energy_matrix(qs_env, matrixkp_t=scrm2, matrix_p=mpa2, &
1022 matrix_name="KINETIC ENERGY MATRIX", &
1023 basis_type="ORB", &
1024 sab_orb=sab_orb, calculate_forces=.true., &
1025 debug_forces=debug_forces, debug_stress=debug_stress)
1026 CALL dbcsr_deallocate_matrix_set(scrm2)
1027
1028 ! Initialize a matrix scrm, later used for scratch purposes
1029 CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s)
1030 NULLIFY (scrm)
1031 CALL dbcsr_allocate_matrix_set(scrm, nspins)
1032 DO ispin = 1, nspins
1033 ALLOCATE (scrm(ispin)%matrix)
1034 CALL dbcsr_create(scrm(ispin)%matrix, template=matrix_s(1)%matrix)
1035 CALL dbcsr_copy(scrm(ispin)%matrix, matrix_s(1)%matrix)
1036 CALL dbcsr_set(scrm(ispin)%matrix, 0.0_dp)
1037 END DO
1038
1039 CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, particle_set=particle_set, &
1040 atomic_kind_set=atomic_kind_set)
1041
1042 ALLOCATE (matrix_p(nspins, 1), matrix_h(nspins, 1))
1043 DO ispin = 1, nspins
1044 matrix_p(ispin, 1)%matrix => mpa(ispin)%matrix
1045 matrix_h(ispin, 1)%matrix => scrm(ispin)%matrix
1046 END DO
1047 matrix_h(1, 1)%matrix => scrm(1)%matrix
1048
1049 nder = 1
1050 CALL core_matrices(qs_env, matrix_h, matrix_p, .true., nder, &
1051 debug_forces=debug_forces, debug_stress=debug_stress)
1052
1053 ! Kim-Gordon subsystem DFT
1054 ! Atomic potential for nonadditive kinetic energy contribution
1055 IF (dft_control%qs_control%do_kg) THEN
1056 IF (qs_env%kg_env%tnadd_method == kg_tnadd_atomic) THEN
1057 CALL get_qs_env(qs_env=qs_env, kg_env=kg_env, dbcsr_dist=dbcsr_dist)
1058
1059 IF (use_virial) THEN
1060 pv_loc = virial%pv_virial
1061 END IF
1062
1063 IF (debug_forces) fodeb(1:3) = force(1)%kinetic(1:3, 1)
1064 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
1065 CALL build_tnadd_mat(kg_env=kg_env, matrix_p=matrix_p, force=force, virial=virial, &
1066 calculate_forces=.true., use_virial=use_virial, &
1067 qs_kind_set=qs_kind_set, atomic_kind_set=atomic_kind_set, &
1068 particle_set=particle_set, sab_orb=sab_orb, dbcsr_dist=dbcsr_dist)
1069 IF (debug_forces) THEN
1070 fodeb(1:3) = force(1)%kinetic(1:3, 1) - fodeb(1:3)
1071 CALL para_env%sum(fodeb)
1072 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*dTnadd ", fodeb
1073 END IF
1074 IF (debug_stress .AND. use_virial) THEN
1075 stdeb = fconv*(virial%pv_virial - stdeb)
1076 CALL para_env%sum(stdeb)
1077 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1078 'STRESS| Pz*dTnadd ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1079 END IF
1080
1081 ! Stress-tensor update components
1082 IF (use_virial) THEN
1083 virial%pv_ekinetic = virial%pv_ekinetic + (virial%pv_virial - pv_loc)
1084 END IF
1085
1086 END IF
1087 END IF
1088
1089 DEALLOCATE (matrix_h)
1090 DEALLOCATE (matrix_p)
1092
1093 ! initialize src matrix
1094 ! Necessary as build_kinetic_matrix will only allocate scrm(1)
1095 ! and not scrm(2) in open-shell case
1096 NULLIFY (scrm)
1097 CALL dbcsr_allocate_matrix_set(scrm, nspins)
1098 DO ispin = 1, nspins
1099 ALLOCATE (scrm(ispin)%matrix)
1100 CALL dbcsr_create(scrm(ispin)%matrix, template=matrix_pz(1)%matrix)
1101 CALL dbcsr_copy(scrm(ispin)%matrix, matrix_pz(ispin)%matrix)
1102 CALL dbcsr_set(scrm(ispin)%matrix, 0.0_dp)
1103 END DO
1104
1105 IF (debug_forces) THEN
1106 ALLOCATE (ftot2(3, natom))
1107 CALL total_qs_force(ftot2, force, atomic_kind_set)
1108 fodeb(1:3) = ftot2(1:3, 1) - ftot1(1:3, 1)
1109 CALL para_env%sum(fodeb)
1110 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: (T+Dz)*dHcore", fodeb
1111 END IF
1112 IF (debug_stress .AND. use_virial) THEN
1113 stdeb = fconv*(virial%pv_virial - sttot)
1114 CALL para_env%sum(stdeb)
1115 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1116 'STRESS| Stress Pz*dHcore ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1117 ! save current total viral, does not contain volume terms yet
1118 sttot2 = virial%pv_virial
1119 END IF
1120 !
1121 ! END OF Tr(P+Z)Hcore
1122 !
1123 !
1124 ! Vhxc (KS potentials calculated externally)
1125 CALL get_qs_env(qs_env, pw_env=pw_env)
1126 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
1127 !
1128 IF (dft_control%do_admm) THEN
1129 CALL get_qs_env(qs_env, admm_env=admm_env)
1130 xc_section => admm_env%xc_section_primary
1131 ELSE
1132 xc_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC")
1133 END IF
1134 xc_fun_section => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
1135 CALL section_vals_val_get(xc_fun_section, "_SECTION_PARAMETERS_", i_val=myfun)
1136 needs = xc_functionals_get_needs(xc_fun_section, (nspins == 2), .true.)
1137 needs_tau_response = needs%tau .OR. needs%tau_spin
1138 !
1139 IF (gapw .OR. gapw_xc) THEN
1140 NULLIFY (oce, sab_orb)
1141 CALL get_qs_env(qs_env=qs_env, oce=oce, sab_orb=sab_orb)
1142 ! set up local_rho_set for GS density
1143 NULLIFY (local_rho_set_gs)
1144 CALL get_qs_env(qs_env=qs_env, rho=rho)
1145 CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
1146 CALL local_rho_set_create(local_rho_set_gs)
1147 CALL allocate_rho_atom_internals(local_rho_set_gs%rho_atom_set, atomic_kind_set, &
1148 qs_kind_set, dft_control, para_env)
1149 CALL init_rho0(local_rho_set_gs, qs_env, dft_control%qs_control%gapw_control)
1150 CALL rho0_s_grid_create(pw_env, local_rho_set_gs%rho0_mpole)
1151 CALL calculate_rho_atom_coeff(qs_env, matrix_p(:, 1), local_rho_set_gs%rho_atom_set, &
1152 qs_kind_set, oce, sab_orb, para_env)
1153 CALL prepare_gapw_den(qs_env, local_rho_set_gs, do_rho0=gapw)
1154 ! set up local_rho_set for response density
1155 NULLIFY (local_rho_set_t)
1156 CALL local_rho_set_create(local_rho_set_t)
1157 CALL allocate_rho_atom_internals(local_rho_set_t%rho_atom_set, atomic_kind_set, &
1158 qs_kind_set, dft_control, para_env)
1159 CALL init_rho0(local_rho_set_t, qs_env, dft_control%qs_control%gapw_control, &
1160 zcore=0.0_dp)
1161 CALL rho0_s_grid_create(pw_env, local_rho_set_t%rho0_mpole)
1162 CALL calculate_rho_atom_coeff(qs_env, mpa(:), local_rho_set_t%rho_atom_set, &
1163 qs_kind_set, oce, sab_orb, para_env)
1164 CALL prepare_gapw_den(qs_env, local_rho_set_t, do_rho0=gapw)
1165
1166 ! compute soft GS potential
1167 ALLOCATE (rho_r_gs(nspins), rho_g_gs(nspins))
1168 DO ispin = 1, nspins
1169 CALL auxbas_pw_pool%create_pw(rho_r_gs(ispin))
1170 CALL auxbas_pw_pool%create_pw(rho_g_gs(ispin))
1171 END DO
1172 CALL auxbas_pw_pool%create_pw(rho_tot_gspace_gs)
1173 ! compute soft GS density
1174 total_rho_gs = 0.0_dp
1175 CALL pw_zero(rho_tot_gspace_gs)
1176 DO ispin = 1, nspins
1177 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=matrix_p(ispin, 1)%matrix, &
1178 rho=rho_r_gs(ispin), &
1179 rho_gspace=rho_g_gs(ispin), &
1180 soft_valid=(gapw .OR. gapw_xc), &
1181 total_rho=total_rho_gs(ispin))
1182 CALL pw_axpy(rho_g_gs(ispin), rho_tot_gspace_gs)
1183 END DO
1184 IF (gapw) THEN
1185 CALL get_qs_env(qs_env, natom=natom)
1186 ! add rho0 contributions to GS density (only for Coulomb) only for gapw
1187 CALL pw_axpy(local_rho_set_gs%rho0_mpole%rho0_s_gs, rho_tot_gspace_gs)
1188 IF (ASSOCIATED(local_rho_set_gs%rho0_mpole%rhoz_cneo_s_gs)) THEN
1189 CALL pw_axpy(local_rho_set_gs%rho0_mpole%rhoz_cneo_s_gs, rho_tot_gspace_gs)
1190 END IF
1191 IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
1192 CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
1193 CALL pw_axpy(rho_core, rho_tot_gspace_gs)
1194 END IF
1195 ! compute GS potential
1196 CALL auxbas_pw_pool%create_pw(v_hartree_gspace_gs)
1197 CALL auxbas_pw_pool%create_pw(v_hartree_rspace_gs)
1198 NULLIFY (hartree_local_gs)
1199 CALL hartree_local_create(hartree_local_gs)
1200 CALL init_coulomb_local(hartree_local_gs, natom)
1201 CALL pw_poisson_solve(poisson_env, rho_tot_gspace_gs, hartree_gs, v_hartree_gspace_gs)
1202 CALL pw_transfer(v_hartree_gspace_gs, v_hartree_rspace_gs)
1203 CALL pw_scale(v_hartree_rspace_gs, v_hartree_rspace_gs%pw_grid%dvol)
1204 END IF
1205 END IF
1206
1207 IF (gapw) THEN
1208 ! Hartree grid PAW term
1209 cpassert(.NOT. use_virial)
1210 IF (debug_forces) fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1)
1211 CALL vh_1c_gg_integrals(qs_env, hartree_gs, hartree_local_gs%ecoul_1c, local_rho_set_t, para_env, tddft=.true., &
1212 local_rho_set_2nd=local_rho_set_gs, core_2nd=.false.) ! n^core for GS potential
1213 ! 1st to define integral space, 2nd for potential, integral contributions stored on local_rho_set_gs
1214 CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace_gs, para_env, calculate_forces=.true., &
1215 local_rho_set=local_rho_set_t, local_rho_set_2nd=local_rho_set_gs)
1216 IF (debug_forces) THEN
1217 fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1) - fodeb(1:3)
1218 CALL para_env%sum(fodeb)
1219 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: (T+Dz)*dVh[D^GS]PAWg0", fodeb
1220 END IF
1221 END IF
1222 IF (gapw .OR. gapw_xc) THEN
1223 IF (myfun /= xc_none) THEN
1224 ! add 1c hard and soft XC contributions
1225 NULLIFY (local_rho_set_vxc)
1226 CALL local_rho_set_create(local_rho_set_vxc)
1227 CALL allocate_rho_atom_internals(local_rho_set_vxc%rho_atom_set, atomic_kind_set, &
1228 qs_kind_set, dft_control, para_env)
1229 CALL calculate_rho_atom_coeff(qs_env, matrix_p(:, 1), local_rho_set_vxc%rho_atom_set, &
1230 qs_kind_set, oce, sab_orb, para_env)
1231 CALL prepare_gapw_den(qs_env, local_rho_set_vxc, do_rho0=.false.)
1232 ! compute hard and soft atomic contributions
1233 CALL calculate_vxc_atom(qs_env, .false., exc1=hartree_gs, xc_section_external=xc_section, &
1234 rho_atom_set_external=local_rho_set_vxc%rho_atom_set)
1235 END IF ! myfun
1236 END IF ! gapw
1237
1238 CALL auxbas_pw_pool%create_pw(vhxc_rspace)
1239 !
1240 ! Stress-tensor: integration contribution direct term
1241 ! int v_Hxc[n^in]*n^z
1242 IF (use_virial) THEN
1243 pv_loc = virial%pv_virial
1244 END IF
1245
1246 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1247 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
1248 IF (gapw .OR. gapw_xc) THEN
1249 ! vtot = v_xc + v_hartree
1250 DO ispin = 1, nspins
1251 CALL pw_zero(vhxc_rspace)
1252 IF (gapw) THEN
1253 CALL pw_transfer(v_hartree_rspace_gs, vhxc_rspace)
1254 ELSE IF (gapw_xc) THEN
1255 CALL pw_transfer(vh_rspace, vhxc_rspace)
1256 END IF
1257 CALL integrate_v_rspace(v_rspace=vhxc_rspace, &
1258 hmat=scrm(ispin), pmat=mpa(ispin), &
1259 qs_env=qs_env, gapw=gapw, &
1260 calculate_forces=.true.)
1261 END DO
1262 IF (myfun /= xc_none) THEN
1263 DO ispin = 1, nspins
1264 CALL pw_zero(vhxc_rspace)
1265 CALL pw_axpy(vxc_rspace(ispin), vhxc_rspace)
1266 CALL integrate_v_rspace(v_rspace=vhxc_rspace, &
1267 hmat=scrm(ispin), pmat=mpa(ispin), &
1268 qs_env=qs_env, gapw=(gapw .OR. gapw_xc), &
1269 calculate_forces=.true.)
1270 END DO
1271 END IF
1272 ELSE ! original GPW with Standard Hartree as Potential
1273 DO ispin = 1, nspins
1274 CALL pw_transfer(vh_rspace, vhxc_rspace)
1275 CALL pw_axpy(vxc_rspace(ispin), vhxc_rspace)
1276 CALL integrate_v_rspace(v_rspace=vhxc_rspace, &
1277 hmat=scrm(ispin), pmat=mpa(ispin), &
1278 qs_env=qs_env, gapw=gapw, calculate_forces=.true.)
1279 END DO
1280 END IF
1281
1282 IF (debug_forces) THEN
1283 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1284 CALL para_env%sum(fodeb)
1285 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: (T+Dz)*dVhxc[D^GS] ", fodeb
1286 END IF
1287 IF (debug_stress .AND. use_virial) THEN
1288 stdeb = fconv*(virial%pv_virial - pv_loc)
1289 CALL para_env%sum(stdeb)
1290 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1291 'STRESS| INT Pz*dVhxc ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1292 END IF
1293
1294 IF (gapw .OR. gapw_xc) THEN
1295 ! HXC term
1296 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
1297 IF (gapw) CALL update_ks_atom(qs_env, scrm, mpa, forces=.true., tddft=.false., &
1298 rho_atom_external=local_rho_set_gs%rho_atom_set)
1299 IF (myfun /= xc_none) CALL update_ks_atom(qs_env, scrm, mpa, forces=.true., tddft=.false., &
1300 rho_atom_external=local_rho_set_vxc%rho_atom_set)
1301 IF (debug_forces) THEN
1302 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
1303 CALL para_env%sum(fodeb)
1304 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: (T+Dz)*dVhxc[D^GS]PAW ", fodeb
1305 END IF
1306 ! release local environments for GAPW
1307 IF (myfun /= xc_none) THEN
1308 IF (ASSOCIATED(local_rho_set_vxc)) CALL local_rho_set_release(local_rho_set_vxc)
1309 END IF
1310 IF (ASSOCIATED(local_rho_set_gs)) CALL local_rho_set_release(local_rho_set_gs)
1311 IF (gapw) THEN
1312 IF (ASSOCIATED(hartree_local_gs)) CALL hartree_local_release(hartree_local_gs)
1313 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace_gs)
1314 CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace_gs)
1315 END IF
1316 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace_gs)
1317 IF (ASSOCIATED(rho_r_gs)) THEN
1318 DO ispin = 1, nspins
1319 CALL auxbas_pw_pool%give_back_pw(rho_r_gs(ispin))
1320 END DO
1321 DEALLOCATE (rho_r_gs)
1322 END IF
1323 IF (ASSOCIATED(rho_g_gs)) THEN
1324 DO ispin = 1, nspins
1325 CALL auxbas_pw_pool%give_back_pw(rho_g_gs(ispin))
1326 END DO
1327 DEALLOCATE (rho_g_gs)
1328 END IF
1329 END IF !gapw
1330
1331 IF (ASSOCIATED(vtau_rspace)) THEN
1332 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1333 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
1334 DO ispin = 1, nspins
1335 CALL integrate_v_rspace(v_rspace=vtau_rspace(ispin), &
1336 hmat=scrm(ispin), pmat=mpa(ispin), &
1337 qs_env=qs_env, gapw=(gapw .OR. gapw_xc), &
1338 calculate_forces=.true., compute_tau=.true.)
1339 END DO
1340 IF (debug_forces) THEN
1341 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1342 CALL para_env%sum(fodeb)
1343 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*dVxc_tau ", fodeb
1344 END IF
1345 IF (debug_stress .AND. use_virial) THEN
1346 stdeb = fconv*(virial%pv_virial - pv_loc)
1347 CALL para_env%sum(stdeb)
1348 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1349 'STRESS| INT Pz*dVxc_tau ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1350 END IF
1351 END IF
1352 CALL auxbas_pw_pool%give_back_pw(vhxc_rspace)
1353
1354 ! Stress-tensor Pz*v_Hxc[Pin]
1355 IF (use_virial) THEN
1356 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1357 END IF
1358
1359 ! KG Embedding
1360 ! calculate kinetic energy potential and integrate with response density
1361 IF (dft_control%qs_control%do_kg) THEN
1362 IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed .OR. &
1363 qs_env%kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
1364
1365 ekin_mol = 0.0_dp
1366 IF (use_virial) THEN
1367 pv_loc = virial%pv_virial
1368 END IF
1369
1370 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1371 CALL kg_ekin_subset(qs_env=qs_env, &
1372 ks_matrix=scrm, &
1373 ekin_mol=ekin_mol, &
1374 calc_force=.true., &
1375 do_kernel=.false., &
1376 pmat_ext=mpa)
1377 IF (debug_forces) THEN
1378 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1379 CALL para_env%sum(fodeb)
1380 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*dVkg ", fodeb
1381 END IF
1382 IF (debug_stress .AND. use_virial) THEN
1383 !IF (iounit > 0) WRITE(iounit, *) &
1384 ! "response_force | VOL 1st KG - v_KG[n_in]*n_z: ", ekin_mol
1385 stdeb = 1.0_dp*fconv*ekin_mol
1386 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1387 'STRESS| VOL KG Pz*dVKG ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1388
1389 stdeb = fconv*(virial%pv_virial - pv_loc)
1390 CALL para_env%sum(stdeb)
1391 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1392 'STRESS| INT KG Pz*dVKG ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1393
1394 stdeb = fconv*virial%pv_xc
1395 CALL para_env%sum(stdeb)
1396 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1397 'STRESS| GGA KG Pz*dVKG ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1398 END IF
1399 IF (use_virial) THEN
1400 ! Direct integral contribution
1401 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1402 END IF
1403
1404 END IF ! tnadd_method
1405 END IF ! do_kg
1406
1408
1409 !
1410 ! Hartree potential of response density
1411 !
1412 ALLOCATE (rhoz_r(nspins), rhoz_g(nspins))
1413 DO ispin = 1, nspins
1414 CALL auxbas_pw_pool%create_pw(rhoz_r(ispin))
1415 CALL auxbas_pw_pool%create_pw(rhoz_g(ispin))
1416 END DO
1417 CALL auxbas_pw_pool%create_pw(rhoz_tot_gspace)
1418 CALL auxbas_pw_pool%create_pw(zv_hartree_rspace)
1419 CALL auxbas_pw_pool%create_pw(zv_hartree_gspace)
1420
1421 CALL pw_zero(rhoz_tot_gspace)
1422 DO ispin = 1, nspins
1423 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpa(ispin)%matrix, &
1424 rho=rhoz_r(ispin), rho_gspace=rhoz_g(ispin), &
1425 soft_valid=gapw)
1426 CALL pw_axpy(rhoz_g(ispin), rhoz_tot_gspace)
1427 END DO
1428 NULLIFY (tauz_r, tauz_r_xc)
1429 IF (gapw_xc) THEN
1430 ALLOCATE (rhoz_r_xc(nspins), rhoz_g_xc(nspins))
1431 DO ispin = 1, nspins
1432 CALL auxbas_pw_pool%create_pw(rhoz_r_xc(ispin))
1433 CALL auxbas_pw_pool%create_pw(rhoz_g_xc(ispin))
1434 END DO
1435 DO ispin = 1, nspins
1436 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpa(ispin)%matrix, &
1437 rho=rhoz_r_xc(ispin), rho_gspace=rhoz_g_xc(ispin), &
1438 soft_valid=gapw_xc)
1439 END DO
1440 END IF
1441
1442 IF (needs_tau_response) THEN
1443 block
1444 TYPE(pw_c1d_gs_type) :: work_g
1445 ALLOCATE (tauz_r(nspins))
1446 CALL auxbas_pw_pool%create_pw(work_g)
1447 DO ispin = 1, nspins
1448 CALL auxbas_pw_pool%create_pw(tauz_r(ispin))
1449 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpa(ispin)%matrix, &
1450 rho=tauz_r(ispin), rho_gspace=work_g, &
1451 soft_valid=gapw, compute_tau=.true.)
1452 END DO
1453 CALL auxbas_pw_pool%give_back_pw(work_g)
1454 END block
1455 IF (gapw_xc) THEN
1456 block
1457 TYPE(pw_c1d_gs_type) :: work_g
1458 ALLOCATE (tauz_r_xc(nspins))
1459 CALL auxbas_pw_pool%create_pw(work_g)
1460 DO ispin = 1, nspins
1461 CALL auxbas_pw_pool%create_pw(tauz_r_xc(ispin))
1462 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpa(ispin)%matrix, &
1463 rho=tauz_r_xc(ispin), rho_gspace=work_g, &
1464 soft_valid=gapw_xc, compute_tau=.true.)
1465 END DO
1466 CALL auxbas_pw_pool%give_back_pw(work_g)
1467 END block
1468 END IF
1469 END IF
1470
1471 !
1472 IF (PRESENT(rhopz_r)) THEN
1473 DO ispin = 1, nspins
1474 CALL pw_copy(rhoz_r(ispin), rhopz_r(ispin))
1475 END DO
1476 END IF
1477
1478 ALLOCATE (rho1)
1479 CALL qs_rho_create(rho1)
1480 IF (gapw_xc) THEN
1481 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_xc)
1482 rho0 => rho_xc
1483 IF (ASSOCIATED(tauz_r_xc)) THEN
1484 CALL qs_rho_set(rho1, rho_r=rhoz_r_xc, rho_g=rhoz_g_xc, tau_r=tauz_r_xc, &
1485 rho_r_valid=.true., rho_g_valid=.true., tau_r_valid=.true.)
1486 ELSE
1487 CALL qs_rho_set(rho1, rho_r=rhoz_r_xc, rho_g=rhoz_g_xc, &
1488 rho_r_valid=.true., rho_g_valid=.true.)
1489 END IF
1490 ELSE
1491 CALL get_qs_env(qs_env=qs_env, rho=rho)
1492 rho0 => rho
1493 IF (ASSOCIATED(tauz_r)) THEN
1494 CALL qs_rho_set(rho1, rho_r=rhoz_r, rho_g=rhoz_g, tau_r=tauz_r, &
1495 rho_r_valid=.true., rho_g_valid=.true., tau_r_valid=.true.)
1496 ELSE
1497 CALL qs_rho_set(rho1, rho_r=rhoz_r, rho_g=rhoz_g, &
1498 rho_r_valid=.true., rho_g_valid=.true.)
1499 END IF
1500 END IF
1501
1502 IF (dft_control%qs_control%gapw_control%accurate_xcint) THEN
1503 ! GAPW Accurate integration
1504 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1505 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
1506 !
1507 CALL accint_weight_force(qs_env, rho0, rho1, 1, xc_section)
1508 !
1509 IF (debug_forces) THEN
1510 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1511 CALL para_env%sum(fodeb)
1512 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*Vxc*dw ", fodeb
1513 END IF
1514 IF (debug_stress .AND. use_virial) THEN
1515 stdeb = fconv*(virial%pv_virial - stdeb)
1516 CALL para_env%sum(stdeb)
1517 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1518 'STRESS| INT Pz*dVxc*dw ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1519 END IF
1520 END IF
1521
1522 ! Stress-tensor contribution second derivative
1523 ! Volume : int v_H[n^z]*n_in
1524 ! Volume : int epsilon_xc*n_z
1525 IF (use_virial) THEN
1526
1527 CALL get_qs_env(qs_env, rho=rho)
1528 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
1529
1530 ! Get the total input density in g-space [ions + electrons]
1531 CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
1532
1533 h_stress(:, :) = 0.0_dp
1534 ! calculate associated hartree potential
1535 ! This term appears twice in the derivation of the equations
1536 ! v_H[n_in]*n_z and v_H[n_z]*n_in
1537 ! due to symmetry we only need to call this routine once,
1538 ! and count the Volume and Green function contribution
1539 ! which is stored in h_stress twice
1540 CALL pw_poisson_solve(poisson_env, &
1541 density=rhoz_tot_gspace, & ! n_z
1542 ehartree=ehartree, &
1543 vhartree=zv_hartree_gspace, & ! v_H[n_z]
1544 h_stress=h_stress, &
1545 aux_density=rho_tot_gspace) ! n_in
1546
1547 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
1548
1549 ! Stress tensor Green function contribution
1550 virial%pv_ehartree = virial%pv_ehartree + 2.0_dp*h_stress/real(para_env%num_pe, dp)
1551 virial%pv_virial = virial%pv_virial + 2.0_dp*h_stress/real(para_env%num_pe, dp)
1552
1553 IF (debug_stress) THEN
1554 stdeb = -1.0_dp*fconv*ehartree
1555 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1556 'STRESS| VOL 1st v_H[n_z]*n_in ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1557 stdeb = -1.0_dp*fconv*ehartree
1558 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1559 'STRESS| VOL 2nd v_H[n_in]*n_z ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1560 stdeb = fconv*(h_stress/real(para_env%num_pe, dp))
1561 CALL para_env%sum(stdeb)
1562 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1563 'STRESS| GREEN 1st v_H[n_z]*n_in ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1564 stdeb = fconv*(h_stress/real(para_env%num_pe, dp))
1565 CALL para_env%sum(stdeb)
1566 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1567 'STRESS| GREEN 2nd v_H[n_in]*n_z ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1568 END IF
1569
1570 ! Stress tensor volume term: \int v_xc[n_in]*n_z
1571 ! vxc_rspace already scaled, we need to unscale it!
1572 exc = 0.0_dp
1573 DO ispin = 1, nspins
1574 exc = exc + pw_integral_ab(rhoz_r(ispin), vxc_rspace(ispin))/ &
1575 vxc_rspace(ispin)%pw_grid%dvol
1576 END DO
1577 IF (ASSOCIATED(vtau_rspace)) THEN
1578 DO ispin = 1, nspins
1579 exc = exc + pw_integral_ab(tauz_r(ispin), vtau_rspace(ispin))/ &
1580 vtau_rspace(ispin)%pw_grid%dvol
1581 END DO
1582 END IF
1583
1584 ! Add KG embedding correction
1585 IF (dft_control%qs_control%do_kg) THEN
1586 IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed .OR. &
1587 qs_env%kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
1588 exc = exc - ekin_mol
1589 END IF
1590 END IF
1591
1592 IF (debug_stress) THEN
1593 stdeb = -1.0_dp*fconv*exc
1594 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1595 'STRESS| VOL 1st eps_XC[n_in]*n_z', one_third_sum_diag(stdeb), det_3x3(stdeb)
1596 END IF
1597
1598 ELSE ! use_virial
1599
1600 ! calculate associated hartree potential
1601 ! contribution for both T and D^Z
1602 IF (gapw) THEN
1603 CALL pw_axpy(local_rho_set_t%rho0_mpole%rho0_s_gs, rhoz_tot_gspace)
1604 IF (ASSOCIATED(local_rho_set_t%rho0_mpole%rhoz_cneo_s_gs)) THEN
1605 CALL pw_axpy(local_rho_set_t%rho0_mpole%rhoz_cneo_s_gs, rhoz_tot_gspace)
1606 END IF
1607 END IF
1608 CALL pw_poisson_solve(poisson_env, rhoz_tot_gspace, ehartree, zv_hartree_gspace)
1609
1610 END IF ! use virial
1611 IF (gapw .OR. gapw_xc) THEN
1612 IF (ASSOCIATED(local_rho_set_t)) CALL local_rho_set_release(local_rho_set_t)
1613 END IF
1614
1615 IF (debug_forces) fodeb(1:3) = force(1)%rho_core(1:3, 1)
1616 IF (debug_stress .AND. use_virial) stdeb = virial%pv_ehartree
1617 CALL pw_transfer(zv_hartree_gspace, zv_hartree_rspace)
1618 CALL pw_scale(zv_hartree_rspace, zv_hartree_rspace%pw_grid%dvol)
1619 ! Getting nuclear force contribution from the core charge density (not for GAPW)
1620 CALL integrate_v_core_rspace(zv_hartree_rspace, qs_env)
1621 IF (debug_forces) THEN
1622 fodeb(1:3) = force(1)%rho_core(1:3, 1) - fodeb(1:3)
1623 CALL para_env%sum(fodeb)
1624 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Vh(rhoz)*dncore ", fodeb
1625 END IF
1626 IF (debug_stress .AND. use_virial) THEN
1627 stdeb = fconv*(virial%pv_ehartree - stdeb)
1628 CALL para_env%sum(stdeb)
1629 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1630 'STRESS| INT Vh(rhoz)*dncore ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1631 END IF
1632
1633 !
1634 IF (gapw_xc) THEN
1635 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_xc)
1636 ELSE
1637 CALL get_qs_env(qs_env=qs_env, rho=rho)
1638 END IF
1639 IF (dft_control%do_admm) THEN
1640 CALL get_qs_env(qs_env, admm_env=admm_env)
1641 xc_section => admm_env%xc_section_primary
1642 ELSE
1643 xc_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC")
1644 END IF
1645
1646 IF (use_virial) THEN
1647 virial%pv_xc = 0.0_dp
1648 END IF
1649
1650 IF (gapw .OR. gapw_xc) THEN
1651 !get local_rho_set for GS density and response potential / density
1652 NULLIFY (local_rho_set_t)
1653 CALL local_rho_set_create(local_rho_set_t)
1654 CALL allocate_rho_atom_internals(local_rho_set_t%rho_atom_set, atomic_kind_set, &
1655 qs_kind_set, dft_control, para_env)
1656 CALL init_rho0(local_rho_set_t, qs_env, dft_control%qs_control%gapw_control, &
1657 zcore=0.0_dp)
1658 CALL rho0_s_grid_create(pw_env, local_rho_set_t%rho0_mpole)
1659 CALL calculate_rho_atom_coeff(qs_env, mpa(:), local_rho_set_t%rho_atom_set, &
1660 qs_kind_set, oce, sab_orb, para_env)
1661 CALL prepare_gapw_den(qs_env, local_rho_set_t, do_rho0=gapw)
1662 NULLIFY (local_rho_set_gs)
1663 CALL local_rho_set_create(local_rho_set_gs)
1664 CALL allocate_rho_atom_internals(local_rho_set_gs%rho_atom_set, atomic_kind_set, &
1665 qs_kind_set, dft_control, para_env)
1666 CALL init_rho0(local_rho_set_gs, qs_env, dft_control%qs_control%gapw_control)
1667 CALL rho0_s_grid_create(pw_env, local_rho_set_gs%rho0_mpole)
1668 CALL calculate_rho_atom_coeff(qs_env, matrix_p(:, 1), local_rho_set_gs%rho_atom_set, &
1669 qs_kind_set, oce, sab_orb, para_env)
1670 CALL prepare_gapw_den(qs_env, local_rho_set_gs, do_rho0=gapw)
1671 ! compute response potential
1672 ALLOCATE (rho_r_t(nspins), rho_g_t(nspins))
1673 DO ispin = 1, nspins
1674 CALL auxbas_pw_pool%create_pw(rho_r_t(ispin))
1675 CALL auxbas_pw_pool%create_pw(rho_g_t(ispin))
1676 END DO
1677 CALL auxbas_pw_pool%create_pw(rho_tot_gspace_t)
1678 total_rho_t = 0.0_dp
1679 CALL pw_zero(rho_tot_gspace_t)
1680 DO ispin = 1, nspins
1681 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpa(ispin)%matrix, &
1682 rho=rho_r_t(ispin), &
1683 rho_gspace=rho_g_t(ispin), &
1684 soft_valid=gapw, &
1685 total_rho=total_rho_t(ispin))
1686 CALL pw_axpy(rho_g_t(ispin), rho_tot_gspace_t)
1687 END DO
1688 ! add rho0 contributions to response density (only for Coulomb) only for gapw
1689 IF (gapw) THEN
1690 CALL pw_axpy(local_rho_set_t%rho0_mpole%rho0_s_gs, rho_tot_gspace_t)
1691 IF (ASSOCIATED(local_rho_set_t%rho0_mpole%rhoz_cneo_s_gs)) THEN
1692 CALL pw_axpy(local_rho_set_t%rho0_mpole%rhoz_cneo_s_gs, rho_tot_gspace_t)
1693 END IF
1694 ! compute response Coulomb potential
1695 CALL auxbas_pw_pool%create_pw(v_hartree_gspace_t)
1696 CALL auxbas_pw_pool%create_pw(v_hartree_rspace_t)
1697 NULLIFY (hartree_local_t)
1698 CALL hartree_local_create(hartree_local_t)
1699 CALL init_coulomb_local(hartree_local_t, natom)
1700 CALL pw_poisson_solve(poisson_env, rho_tot_gspace_t, hartree_t, v_hartree_gspace_t)
1701 CALL pw_transfer(v_hartree_gspace_t, v_hartree_rspace_t)
1702 CALL pw_scale(v_hartree_rspace_t, v_hartree_rspace_t%pw_grid%dvol)
1703 !
1704 IF (debug_forces) fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1)
1705 CALL vh_1c_gg_integrals(qs_env, hartree_t, hartree_local_t%ecoul_1c, local_rho_set_gs, para_env, tddft=.false., &
1706 local_rho_set_2nd=local_rho_set_t, core_2nd=.true.) ! n^core for GS potential
1707 CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace_t, para_env, calculate_forces=.true., &
1708 local_rho_set=local_rho_set_gs, local_rho_set_2nd=local_rho_set_t)
1709 IF (debug_forces) THEN
1710 fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1) - fodeb(1:3)
1711 CALL para_env%sum(fodeb)
1712 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Vh(T)*dncore PAWg0", fodeb
1713 END IF
1714 END IF !gapw
1715 END IF !gapw
1716
1717 do_onecenter = .false.
1718 NULLIFY (rho0_atom_set, rho1_atom_set)
1719 IF (gapw .OR. gapw_xc) THEN
1720 !GAPW compute atomic fxc contributions
1721 IF (myfun /= xc_none) THEN
1722 ! local_rho_set_f
1723 NULLIFY (local_rho_set_f)
1724 CALL local_rho_set_create(local_rho_set_f)
1725 CALL allocate_rho_atom_internals(local_rho_set_f%rho_atom_set, atomic_kind_set, &
1726 qs_kind_set, dft_control, para_env)
1727 CALL calculate_rho_atom_coeff(qs_env, mpa, local_rho_set_f%rho_atom_set, &
1728 qs_kind_set, oce, sab_orb, para_env)
1729 CALL prepare_gapw_den(qs_env, local_rho_set_f, do_rho0=.false.)
1730 rho0_atom_set => local_rho_set_gs%rho_atom_set
1731 rho1_atom_set => local_rho_set_f%rho_atom_set
1732 do_onecenter = .true.
1733 END IF ! myfun
1734 END IF
1735
1736 NULLIFY (v_xc, v_xc_tau)
1737 CALL qs_fxc_create(qs_env, rho0, rho1, rho0_atom_set, xc_section, do_onecenter, &
1738 v_xc, v_xc_tau, rho1_atom_set, &
1739 dispersion_env=qs_env%dispersion_env, &
1740 compute_virial=use_virial, virial_xc=virial%pv_xc)
1741 DEALLOCATE (rho1)
1742
1743 ! Stress-tensor XC-kernel GGA contribution
1744 IF (use_virial) THEN
1745 virial%pv_exc = virial%pv_exc + virial%pv_xc
1746 virial%pv_virial = virial%pv_virial + virial%pv_xc
1747 END IF
1748
1749 IF (debug_stress .AND. use_virial) THEN
1750 stdeb = 1.0_dp*fconv*virial%pv_xc
1751 CALL para_env%sum(stdeb)
1752 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1753 'STRESS| GGA 2nd Pin*dK*rhoz', one_third_sum_diag(stdeb), det_3x3(stdeb)
1754 END IF
1755
1756 ! Stress-tensor integral contribution of 2nd derivative terms
1757 IF (use_virial) THEN
1758 pv_loc = virial%pv_virial
1759 END IF
1760
1761 CALL get_qs_env(qs_env=qs_env, rho=rho)
1762 CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
1763 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
1764
1765 IF (ASSOCIATED(rho_nlcc)) THEN
1766 DO ispin = 1, nspins
1767 CALL integrate_rho_nlcc(v_xc(ispin), qs_env, &
1768 debug_forces, debug_stress, "dnlcc*dVxc")
1769 END DO
1770 END IF
1771
1772 DO ispin = 1, nspins
1773 CALL pw_scale(v_xc(ispin), v_xc(ispin)%pw_grid%dvol)
1774 END DO
1775
1776 IF ((.NOT. (gapw)) .AND. (.NOT. gapw_xc)) THEN
1777 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1778 DO ispin = 1, nspins
1779 CALL pw_axpy(zv_hartree_rspace, v_xc(ispin)) ! Hartree potential of response density
1780 CALL integrate_v_rspace(qs_env=qs_env, &
1781 v_rspace=v_xc(ispin), &
1782 hmat=matrix_hz(ispin), &
1783 pmat=matrix_p(ispin, 1), &
1784 gapw=.false., &
1785 calculate_forces=.true.)
1786 END DO
1787 IF (debug_forces) THEN
1788 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1789 CALL para_env%sum(fodeb)
1790 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*dKhxc*rhoz ", fodeb
1791 END IF
1792 ELSE
1793 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1794 IF (myfun /= xc_none) THEN
1795 DO ispin = 1, nspins
1796 CALL integrate_v_rspace(qs_env=qs_env, &
1797 v_rspace=v_xc(ispin), &
1798 hmat=matrix_hz(ispin), &
1799 pmat=matrix_p(ispin, 1), &
1800 gapw=.true., &
1801 calculate_forces=.true.)
1802 END DO
1803 END IF ! my_fun
1804 ! Coulomb T+Dz
1805 DO ispin = 1, nspins
1806 CALL pw_zero(v_xc(ispin))
1807 IF (gapw) THEN ! Hartree potential of response density
1808 CALL pw_axpy(v_hartree_rspace_t, v_xc(ispin))
1809 ELSE IF (gapw_xc) THEN
1810 CALL pw_axpy(zv_hartree_rspace, v_xc(ispin))
1811 END IF
1812 CALL integrate_v_rspace(qs_env=qs_env, &
1813 v_rspace=v_xc(ispin), &
1814 hmat=matrix_ht(ispin), &
1815 pmat=matrix_p(ispin, 1), &
1816 gapw=gapw, &
1817 calculate_forces=.true.)
1818 END DO
1819 IF (debug_forces) THEN
1820 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1821 CALL para_env%sum(fodeb)
1822 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*dKhxc*rhoz ", fodeb
1823 END IF
1824 END IF
1825
1826 IF (gapw .OR. gapw_xc) THEN
1827 ! compute hard and soft atomic contributions
1828 IF (myfun /= xc_none) THEN
1829 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
1830 CALL update_ks_atom(qs_env, matrix_hz, matrix_p, forces=.true., tddft=.false., &
1831 rho_atom_external=local_rho_set_f%rho_atom_set)
1832 IF (debug_forces) THEN
1833 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
1834 CALL para_env%sum(fodeb)
1835 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P^GS*dKxc*(Dz+T) PAW", fodeb
1836 END IF
1837 END IF !myfun
1838 ! Coulomb contributions
1839 IF (gapw) THEN
1840 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
1841 CALL update_ks_atom(qs_env, matrix_ht, matrix_p, forces=.true., tddft=.false., &
1842 rho_atom_external=local_rho_set_t%rho_atom_set)
1843 IF (debug_forces) THEN
1844 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
1845 CALL para_env%sum(fodeb)
1846 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P^GS*dKh*(Dz+T) PAW", fodeb
1847 END IF
1848 END IF
1849 ! add Coulomb and XC
1850 DO ispin = 1, nspins
1851 CALL dbcsr_add(matrix_hz(ispin)%matrix, matrix_ht(ispin)%matrix, 1.0_dp, 1.0_dp)
1852 END DO
1853
1854 ! release
1855 IF (myfun /= xc_none) THEN
1856 IF (ASSOCIATED(local_rho_set_f)) CALL local_rho_set_release(local_rho_set_f)
1857 END IF
1858 IF (ASSOCIATED(local_rho_set_t)) CALL local_rho_set_release(local_rho_set_t)
1859 IF (ASSOCIATED(local_rho_set_gs)) CALL local_rho_set_release(local_rho_set_gs)
1860 IF (gapw) THEN
1861 IF (ASSOCIATED(hartree_local_t)) CALL hartree_local_release(hartree_local_t)
1862 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace_t)
1863 CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace_t)
1864 END IF
1865 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace_t)
1866 DO ispin = 1, nspins
1867 CALL auxbas_pw_pool%give_back_pw(rho_r_t(ispin))
1868 CALL auxbas_pw_pool%give_back_pw(rho_g_t(ispin))
1869 END DO
1870 DEALLOCATE (rho_r_t, rho_g_t)
1871 END IF ! gapw
1872
1873 IF (debug_stress .AND. use_virial) THEN
1874 stdeb = fconv*(virial%pv_virial - stdeb)
1875 CALL para_env%sum(stdeb)
1876 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1877 'STRESS| INT 2nd f_Hxc[Pz]*Pin', one_third_sum_diag(stdeb), det_3x3(stdeb)
1878 END IF
1879 !
1880 IF (ASSOCIATED(v_xc_tau)) THEN
1881 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1882 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
1883 DO ispin = 1, nspins
1884 CALL pw_scale(v_xc_tau(ispin), v_xc_tau(ispin)%pw_grid%dvol)
1885 CALL integrate_v_rspace(qs_env=qs_env, &
1886 v_rspace=v_xc_tau(ispin), &
1887 hmat=matrix_hz(ispin), &
1888 pmat=matrix_p(ispin, 1), &
1889 compute_tau=.true., &
1890 gapw=(gapw .OR. gapw_xc), &
1891 calculate_forces=.true.)
1892 END DO
1893 IF (debug_forces) THEN
1894 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1895 CALL para_env%sum(fodeb)
1896 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*dKtau*tauz ", fodeb
1897 END IF
1898 END IF
1899 IF (debug_stress .AND. use_virial) THEN
1900 stdeb = fconv*(virial%pv_virial - stdeb)
1901 CALL para_env%sum(stdeb)
1902 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1903 'STRESS| INT 2nd f_xctau[Pz]*Pin', one_third_sum_diag(stdeb), det_3x3(stdeb)
1904 END IF
1905 ! Stress-tensor integral contribution of 2nd derivative terms
1906 IF (use_virial) THEN
1907 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1908 END IF
1909
1910 ! KG Embedding
1911 ! calculate kinetic energy kernel, folded with response density for partial integration
1912 IF (dft_control%qs_control%do_kg) THEN
1913 IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed) THEN
1914 ekin_mol = 0.0_dp
1915 IF (use_virial) THEN
1916 pv_loc = virial%pv_virial
1917 END IF
1918
1919 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1920 IF (use_virial) virial%pv_xc = 0.0_dp
1921 CALL kg_ekin_subset(qs_env=qs_env, &
1922 ks_matrix=matrix_hz, &
1923 ekin_mol=ekin_mol, &
1924 calc_force=.true., &
1925 do_kernel=.true., &
1926 pmat_ext=matrix_pz)
1927
1928 IF (debug_forces) THEN
1929 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1930 CALL para_env%sum(fodeb)
1931 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*d(Kkg)*rhoz ", fodeb
1932 END IF
1933 IF (debug_stress .AND. use_virial) THEN
1934 stdeb = fconv*(virial%pv_virial - pv_loc)
1935 CALL para_env%sum(stdeb)
1936 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1937 'STRESS| INT KG Pin*d(KKG)*rhoz ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1938
1939 stdeb = fconv*(virial%pv_xc)
1940 CALL para_env%sum(stdeb)
1941 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
1942 'STRESS| GGA KG Pin*d(KKG)*rhoz ', one_third_sum_diag(stdeb), det_3x3(stdeb)
1943 END IF
1944
1945 ! Stress tensor
1946 IF (use_virial) THEN
1947 ! XC-kernel Integral contribution
1948 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1949
1950 ! XC-kernel GGA contribution
1951 virial%pv_exc = virial%pv_exc - virial%pv_xc
1952 virial%pv_virial = virial%pv_virial - virial%pv_xc
1953 virial%pv_xc = 0.0_dp
1954 END IF
1955 END IF
1956 END IF
1957 CALL auxbas_pw_pool%give_back_pw(rhoz_tot_gspace)
1958 CALL auxbas_pw_pool%give_back_pw(zv_hartree_gspace)
1959 CALL auxbas_pw_pool%give_back_pw(zv_hartree_rspace)
1960 DO ispin = 1, nspins
1961 CALL auxbas_pw_pool%give_back_pw(rhoz_r(ispin))
1962 CALL auxbas_pw_pool%give_back_pw(rhoz_g(ispin))
1963 CALL auxbas_pw_pool%give_back_pw(v_xc(ispin))
1964 END DO
1965 DEALLOCATE (rhoz_r, rhoz_g, v_xc)
1966 IF (gapw_xc) THEN
1967 DO ispin = 1, nspins
1968 CALL auxbas_pw_pool%give_back_pw(rhoz_r_xc(ispin))
1969 CALL auxbas_pw_pool%give_back_pw(rhoz_g_xc(ispin))
1970 END DO
1971 DEALLOCATE (rhoz_r_xc, rhoz_g_xc)
1972 END IF
1973 IF (ASSOCIATED(v_xc_tau)) THEN
1974 DO ispin = 1, nspins
1975 CALL auxbas_pw_pool%give_back_pw(tauz_r(ispin))
1976 CALL auxbas_pw_pool%give_back_pw(v_xc_tau(ispin))
1977 END DO
1978 DEALLOCATE (tauz_r, v_xc_tau)
1979 IF (ASSOCIATED(tauz_r_xc)) THEN
1980 DO ispin = 1, nspins
1981 CALL auxbas_pw_pool%give_back_pw(tauz_r_xc(ispin))
1982 END DO
1983 DEALLOCATE (tauz_r_xc)
1984 END IF
1985 END IF
1986 IF (debug_forces) THEN
1987 ALLOCATE (ftot3(3, natom))
1988 CALL total_qs_force(ftot3, force, atomic_kind_set)
1989 fodeb(1:3) = ftot3(1:3, 1) - ftot2(1:3, 1)
1990 CALL para_env%sum(fodeb)
1991 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*V(rhoz)", fodeb
1992 END IF
1993 CALL dbcsr_deallocate_matrix_set(scrm)
1994 CALL dbcsr_deallocate_matrix_set(matrix_ht)
1995
1996 ! -----------------------------------------
1997 ! Apply ADMM exchange correction
1998 ! -----------------------------------------
1999
2000 IF (dft_control%do_admm) THEN
2001 ! volume term
2002 exc_aux_fit = 0.0_dp
2003
2004 IF (qs_env%admm_env%aux_exch_func == do_admm_aux_exch_func_none) THEN
2005 ! nothing to do
2006 NULLIFY (mpz, mhz, mhx, mhy)
2007 ELSE
2008 ! add ADMM xc_section_aux terms: Pz*Vxc + P0*K0[rhoz]
2009 CALL get_qs_env(qs_env, admm_env=admm_env)
2010 CALL get_admm_env(admm_env, rho_aux_fit=rho_aux_fit, matrix_s_aux_fit=scrm, &
2011 task_list_aux_fit=task_list_aux_fit)
2012 !
2013 NULLIFY (mpz, mhz, mhx, mhy)
2014 CALL dbcsr_allocate_matrix_set(mhx, nspins, 1)
2015 CALL dbcsr_allocate_matrix_set(mhy, nspins, 1)
2016 CALL dbcsr_allocate_matrix_set(mpz, nspins, 1)
2017 DO ispin = 1, nspins
2018 ALLOCATE (mhx(ispin, 1)%matrix)
2019 CALL dbcsr_create(mhx(ispin, 1)%matrix, template=scrm(1)%matrix)
2020 CALL dbcsr_copy(mhx(ispin, 1)%matrix, scrm(1)%matrix)
2021 CALL dbcsr_set(mhx(ispin, 1)%matrix, 0.0_dp)
2022 ALLOCATE (mhy(ispin, 1)%matrix)
2023 CALL dbcsr_create(mhy(ispin, 1)%matrix, template=scrm(1)%matrix)
2024 CALL dbcsr_copy(mhy(ispin, 1)%matrix, scrm(1)%matrix)
2025 CALL dbcsr_set(mhy(ispin, 1)%matrix, 0.0_dp)
2026 ALLOCATE (mpz(ispin, 1)%matrix)
2027 IF (do_ex) THEN
2028 CALL dbcsr_create(mpz(ispin, 1)%matrix, template=p_env%p1_admm(ispin)%matrix)
2029 CALL dbcsr_copy(mpz(ispin, 1)%matrix, p_env%p1_admm(ispin)%matrix)
2030 CALL dbcsr_add(mpz(ispin, 1)%matrix, ex_env%matrix_pe_admm(ispin)%matrix, &
2031 1.0_dp, 1.0_dp)
2032 ELSE
2033 CALL dbcsr_create(mpz(ispin, 1)%matrix, template=matrix_pz_admm(ispin)%matrix)
2034 CALL dbcsr_copy(mpz(ispin, 1)%matrix, matrix_pz_admm(ispin)%matrix)
2035 END IF
2036 END DO
2037 !
2038 xc_section => admm_env%xc_section_aux
2039 xc_fun_section => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
2040 needs = xc_functionals_get_needs(xc_fun_section, (nspins == 2), .true.)
2041 needs_tau_response_aux = needs%tau .OR. needs%tau_spin
2042 ! Stress-tensor: integration contribution direct term
2043 ! int Pz*v_xc[rho_admm]
2044 IF (use_virial) THEN
2045 pv_loc = virial%pv_virial
2046 END IF
2047
2048 basis_type = "AUX_FIT"
2049 task_list => task_list_aux_fit
2050 IF (admm_env%do_gapw) THEN
2051 basis_type = "AUX_FIT_SOFT"
2052 task_list => admm_env%admm_gapw_env%task_list
2053 END IF
2054 !
2055 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
2056 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
2057 DO ispin = 1, nspins
2058 CALL integrate_v_rspace(v_rspace=vadmm_rspace(ispin), &
2059 hmat=mhx(ispin, 1), pmat=mpz(ispin, 1), &
2060 qs_env=qs_env, calculate_forces=.true., &
2061 basis_type=basis_type, task_list_external=task_list)
2062 IF (ASSOCIATED(vadmm_tau_rspace)) THEN
2063 CALL integrate_v_rspace(v_rspace=vadmm_tau_rspace(ispin), &
2064 hmat=mhx(ispin, 1), pmat=mpz(ispin, 1), &
2065 qs_env=qs_env, calculate_forces=.true., compute_tau=.true., &
2066 basis_type=basis_type, task_list_external=task_list)
2067 END IF
2068 END DO
2069 IF (debug_forces) THEN
2070 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
2071 CALL para_env%sum(fodeb)
2072 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*Vxc(rho_admm)", fodeb
2073 END IF
2074 IF (debug_stress .AND. use_virial) THEN
2075 stdeb = fconv*(virial%pv_virial - pv_loc)
2076 CALL para_env%sum(stdeb)
2077 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2078 'STRESS| INT 1st Pz*dVxc(rho_admm) ', one_third_sum_diag(stdeb), det_3x3(stdeb)
2079 END IF
2080 ! Stress-tensor Pz_admm*v_xc[rho_admm]
2081 IF (use_virial) THEN
2082 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
2083 END IF
2084 !
2085 IF (admm_env%do_gapw) THEN
2086 CALL get_admm_env(admm_env, sab_aux_fit=sab_aux_fit)
2087 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
2088 CALL update_ks_atom(qs_env, mhx(:, 1), mpz(:, 1), forces=.true., tddft=.false., &
2089 rho_atom_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
2090 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
2091 oce_external=admm_env%admm_gapw_env%oce, &
2092 sab_external=sab_aux_fit)
2093 IF (debug_forces) THEN
2094 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
2095 CALL para_env%sum(fodeb)
2096 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*Vxc(rho_admm)PAW", fodeb
2097 END IF
2098 END IF
2099 !
2100 ! rhoz_aux
2101 NULLIFY (rhoz_g_aux, rhoz_r_aux, rhoz_tau_r_aux)
2102 ALLOCATE (rhoz_r_aux(nspins), rhoz_g_aux(nspins))
2103 DO ispin = 1, nspins
2104 CALL auxbas_pw_pool%create_pw(rhoz_r_aux(ispin))
2105 CALL auxbas_pw_pool%create_pw(rhoz_g_aux(ispin))
2106 END DO
2107 DO ispin = 1, nspins
2108 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpz(ispin, 1)%matrix, &
2109 rho=rhoz_r_aux(ispin), rho_gspace=rhoz_g_aux(ispin), &
2110 basis_type=basis_type, task_list_external=task_list)
2111 END DO
2112 IF (needs_tau_response_aux .OR. ASSOCIATED(vadmm_tau_rspace)) THEN
2113 block
2114 TYPE(pw_c1d_gs_type) :: work_g
2115 ALLOCATE (rhoz_tau_r_aux(nspins))
2116 CALL auxbas_pw_pool%create_pw(work_g)
2117 DO ispin = 1, nspins
2118 CALL auxbas_pw_pool%create_pw(rhoz_tau_r_aux(ispin))
2119 CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpz(ispin, 1)%matrix, &
2120 rho=rhoz_tau_r_aux(ispin), rho_gspace=work_g, &
2121 basis_type=basis_type, task_list_external=task_list, &
2122 compute_tau=.true.)
2123 END DO
2124 CALL auxbas_pw_pool%give_back_pw(work_g)
2125 END block
2126 END IF
2127 !
2128 ! Add ADMM volume contribution to stress tensor
2129 IF (use_virial) THEN
2130
2131 ! Stress tensor volume term: \int v_xc[n_in_admm]*n_z_admm
2132 ! vadmm_rspace already scaled, we need to unscale it!
2133 DO ispin = 1, nspins
2134 exc_aux_fit = exc_aux_fit + pw_integral_ab(rhoz_r_aux(ispin), vadmm_rspace(ispin))/ &
2135 vadmm_rspace(ispin)%pw_grid%dvol
2136 END DO
2137 IF (ASSOCIATED(vadmm_tau_rspace) .AND. ASSOCIATED(rhoz_tau_r_aux)) THEN
2138 DO ispin = 1, nspins
2139 exc_aux_fit = exc_aux_fit + pw_integral_ab(rhoz_tau_r_aux(ispin), vadmm_tau_rspace(ispin))/ &
2140 vadmm_tau_rspace(ispin)%pw_grid%dvol
2141 END DO
2142 END IF
2143
2144 IF (debug_stress) THEN
2145 stdeb = -1.0_dp*fconv*exc_aux_fit
2146 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T43,2(1X,ES19.11))") &
2147 'STRESS| VOL 1st eps_XC[n_in_admm]*n_z_admm', one_third_sum_diag(stdeb), det_3x3(stdeb)
2148 END IF
2149
2150 END IF
2151 !
2152 NULLIFY (v_xc, v_xc_tau)
2153
2154 IF (use_virial) virial%pv_xc = 0.0_dp
2155
2156 NULLIFY (rho0_atom_set, rho1_atom_set)
2157 kind_set => qs_kind_set
2158 IF (admm_env%do_gapw) THEN
2159 kind_set => admm_env%admm_gapw_env%admm_kind_set
2160 CALL local_rho_set_create(local_rhoz_set_admm)
2161 CALL allocate_rho_atom_internals(local_rhoz_set_admm%rho_atom_set, atomic_kind_set, &
2162 kind_set, dft_control, para_env)
2163 CALL calculate_rho_atom_coeff(qs_env, mpz(:, 1), local_rhoz_set_admm%rho_atom_set, &
2164 kind_set, admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
2165 CALL prepare_gapw_den(qs_env, local_rho_set=local_rhoz_set_admm, &
2166 do_rho0=.false., kind_set_external=kind_set)
2167 rho0_atom_set => admm_env%admm_gapw_env%local_rho_set%rho_atom_set
2168 rho1_atom_set => local_rhoz_set_admm%rho_atom_set
2169 do_onecenter = .true.
2170 END IF
2171
2172 ALLOCATE (rho1)
2173 CALL qs_rho_create(rho1)
2174 IF (ASSOCIATED(rhoz_tau_r_aux)) THEN
2175 CALL qs_rho_set(rho1, rho_r=rhoz_r_aux, rho_g=rhoz_g_aux, tau_r=rhoz_tau_r_aux, &
2176 rho_r_valid=.true., rho_g_valid=.true., tau_r_valid=.true.)
2177 ELSE
2178 CALL qs_rho_set(rho1, rho_r=rhoz_r_aux, rho_g=rhoz_g_aux, &
2179 rho_r_valid=.true., rho_g_valid=.true.)
2180 END IF
2181 CALL qs_fxc_create(qs_env, rho_aux_fit, rho1, rho0_atom_set, xc_section, do_onecenter, &
2182 v_xc, v_xc_tau, rho1_atom_set, &
2183 kind_set_external=kind_set, &
2184 compute_virial=use_virial, virial_xc=virial%pv_xc)
2185
2186 ! Stress-tensor ADMM-kernel GGA contribution
2187 IF (use_virial) THEN
2188 virial%pv_exc = virial%pv_exc + virial%pv_xc
2189 virial%pv_virial = virial%pv_virial + virial%pv_xc
2190 END IF
2191
2192 IF (debug_stress .AND. use_virial) THEN
2193 stdeb = 1.0_dp*fconv*virial%pv_xc
2194 CALL para_env%sum(stdeb)
2195 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2196 'STRESS| GGA 2nd Pin_admm*dK*rhoz_admm', one_third_sum_diag(stdeb), det_3x3(stdeb)
2197 END IF
2198 !
2199 IF (ASSOCIATED(rho_nlcc)) THEN
2200 DO ispin = 1, nspins
2201 CALL integrate_rho_nlcc(v_xc(ispin), qs_env, &
2202 debug_forces, debug_stress, "dnlcc*dVxc(ADMM)")
2203 END DO
2204 END IF
2205 !
2206 CALL qs_rho_get(rho_aux_fit, rho_ao_kp=matrix_p)
2207 ! Stress-tensor Pin*dK*rhoz_admm
2208 IF (use_virial) THEN
2209 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
2210 END IF
2211 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
2212 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
2213 DO ispin = 1, nspins
2214 CALL dbcsr_set(mhy(ispin, 1)%matrix, 0.0_dp)
2215 CALL pw_scale(v_xc(ispin), v_xc(ispin)%pw_grid%dvol)
2216 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_xc(ispin), &
2217 hmat=mhy(ispin, 1), pmat=matrix_p(ispin, 1), &
2218 calculate_forces=.true., &
2219 basis_type=basis_type, task_list_external=task_list)
2220 END DO
2221 IF (ASSOCIATED(v_xc_tau)) THEN
2222 DO ispin = 1, nspins
2223 CALL pw_scale(v_xc_tau(ispin), v_xc_tau(ispin)%pw_grid%dvol)
2224 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_xc_tau(ispin), &
2225 hmat=mhy(ispin, 1), pmat=matrix_p(ispin, 1), &
2226 calculate_forces=.true., compute_tau=.true., &
2227 basis_type=basis_type, task_list_external=task_list)
2228 END DO
2229 END IF
2230 IF (debug_forces) THEN
2231 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
2232 CALL para_env%sum(fodeb)
2233 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*dK*rhoz_admm ", fodeb
2234 END IF
2235 IF (debug_stress .AND. use_virial) THEN
2236 stdeb = fconv*(virial%pv_virial - pv_loc)
2237 CALL para_env%sum(stdeb)
2238 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2239 'STRESS| INT 2nd Pin*dK*rhoz_admm ', one_third_sum_diag(stdeb), det_3x3(stdeb)
2240 END IF
2241 ! Stress-tensor Pin*dK*rhoz_admm
2242 IF (use_virial) THEN
2243 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
2244 END IF
2245
2246 ! GAPW ADMM XC correction integrate weight contribution to force
2247 IF (admm_env%do_gapw) THEN
2248 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
2249 IF (debug_stress .AND. use_virial) stdeb = virial%pv_virial
2250 !
2251 CALL accint_weight_force(qs_env, rho_aux_fit, rho1, 1, xc_section)
2252 !
2253 IF (debug_forces) THEN
2254 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
2255 CALL para_env%sum(fodeb)
2256 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: dKxc*rhoz_admm*dw ", fodeb
2257 END IF
2258 IF (debug_stress .AND. use_virial) THEN
2259 stdeb = fconv*(virial%pv_virial - stdeb)
2260 CALL para_env%sum(stdeb)
2261 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2262 'STRESS| dKxc*rhoz_admm*dw', one_third_sum_diag(stdeb), det_3x3(stdeb)
2263 END IF
2264 END IF
2265 ! return ADMM response densities and potentials
2266 DO ispin = 1, nspins
2267 CALL auxbas_pw_pool%give_back_pw(v_xc(ispin))
2268 IF (ASSOCIATED(v_xc_tau)) CALL auxbas_pw_pool%give_back_pw(v_xc_tau(ispin))
2269 CALL auxbas_pw_pool%give_back_pw(rhoz_r_aux(ispin))
2270 CALL auxbas_pw_pool%give_back_pw(rhoz_g_aux(ispin))
2271 IF (ASSOCIATED(rhoz_tau_r_aux)) CALL auxbas_pw_pool%give_back_pw(rhoz_tau_r_aux(ispin))
2272 END DO
2273 DEALLOCATE (v_xc, rhoz_r_aux, rhoz_g_aux)
2274 IF (ASSOCIATED(v_xc_tau)) DEALLOCATE (v_xc_tau)
2275 IF (ASSOCIATED(rhoz_tau_r_aux)) DEALLOCATE (rhoz_tau_r_aux)
2276 DEALLOCATE (rho1)
2277 !
2278 IF (admm_env%do_gapw) THEN
2279 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
2280 CALL update_ks_atom(qs_env, mhy(:, 1), matrix_p(:, 1), forces=.true., tddft=.false., &
2281 rho_atom_external=local_rhoz_set_admm%rho_atom_set, &
2282 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
2283 oce_external=admm_env%admm_gapw_env%oce, &
2284 sab_external=sab_aux_fit)
2285 IF (debug_forces) THEN
2286 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
2287 CALL para_env%sum(fodeb)
2288 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pin*dK*rhoz_admm[PAW] ", fodeb
2289 END IF
2290 CALL local_rho_set_release(local_rhoz_set_admm)
2291 END IF
2292 !
2293 nao = admm_env%nao_orb
2294 nao_aux = admm_env%nao_aux_fit
2295 ALLOCATE (dbwork)
2296 CALL dbcsr_create(dbwork, template=matrix_hz(1)%matrix)
2297 DO ispin = 1, nspins
2298 CALL cp_dbcsr_sm_fm_multiply(mhy(ispin, 1)%matrix, admm_env%A, &
2299 admm_env%work_aux_orb, nao)
2300 CALL parallel_gemm('T', 'N', nao, nao, nao_aux, &
2301 1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
2302 admm_env%work_orb_orb)
2303 CALL dbcsr_copy(dbwork, matrix_hz(ispin)%matrix)
2304 CALL dbcsr_set(dbwork, 0.0_dp)
2305 CALL copy_fm_to_dbcsr(admm_env%work_orb_orb, dbwork, keep_sparsity=.true.)
2306 CALL dbcsr_add(matrix_hz(ispin)%matrix, dbwork, 1.0_dp, 1.0_dp)
2307 END DO
2308 CALL dbcsr_release(dbwork)
2309 DEALLOCATE (dbwork)
2310 CALL dbcsr_deallocate_matrix_set(mpz)
2311 END IF ! qs_env%admm_env%aux_exch_func == do_admm_aux_exch_func_none
2312 END IF ! do_admm
2313
2314 ! -----------------------------------------
2315 ! HFX
2316 ! -----------------------------------------
2317
2318 ! HFX
2319 hfx_section => section_vals_get_subs_vals(xc_section, "HF")
2320 CALL section_vals_get(hfx_section, explicit=do_hfx)
2321 IF (do_hfx) THEN
2322 CALL section_vals_get(hfx_section, n_repetition=n_rep_hf)
2323 cpassert(n_rep_hf == 1)
2324 CALL section_vals_val_get(hfx_section, "TREAT_LSD_IN_CORE", l_val=hfx_treat_lsd_in_core, &
2325 i_rep_section=1)
2326 mspin = 1
2327 IF (hfx_treat_lsd_in_core) mspin = nspins
2328 IF (use_virial) virial%pv_fock_4c = 0.0_dp
2329 !
2330 CALL get_qs_env(qs_env=qs_env, rho=rho, x_data=x_data, &
2331 s_mstruct_changed=s_mstruct_changed)
2332 distribute_fock_matrix = .true.
2333
2334 ! -----------------------------------------
2335 ! HFX-ADMM
2336 ! -----------------------------------------
2337 IF (dft_control%do_admm) THEN
2338 CALL get_qs_env(qs_env=qs_env, admm_env=admm_env)
2339 CALL get_admm_env(admm_env, matrix_s_aux_fit=scrm, rho_aux_fit=rho_aux_fit)
2340 CALL qs_rho_get(rho_aux_fit, rho_ao_kp=matrix_p)
2341 NULLIFY (mpz, mhz, mpd, mhd)
2342 CALL dbcsr_allocate_matrix_set(mpz, nspins, 1)
2343 CALL dbcsr_allocate_matrix_set(mhz, nspins, 1)
2344 CALL dbcsr_allocate_matrix_set(mpd, nspins, 1)
2345 CALL dbcsr_allocate_matrix_set(mhd, nspins, 1)
2346 DO ispin = 1, nspins
2347 ALLOCATE (mhz(ispin, 1)%matrix, mhd(ispin, 1)%matrix)
2348 CALL dbcsr_create(mhz(ispin, 1)%matrix, template=scrm(1)%matrix)
2349 CALL dbcsr_create(mhd(ispin, 1)%matrix, template=scrm(1)%matrix)
2350 CALL dbcsr_copy(mhz(ispin, 1)%matrix, scrm(1)%matrix)
2351 CALL dbcsr_copy(mhd(ispin, 1)%matrix, scrm(1)%matrix)
2352 CALL dbcsr_set(mhz(ispin, 1)%matrix, 0.0_dp)
2353 CALL dbcsr_set(mhd(ispin, 1)%matrix, 0.0_dp)
2354 ALLOCATE (mpz(ispin, 1)%matrix)
2355 IF (do_ex) THEN
2356 CALL dbcsr_create(mpz(ispin, 1)%matrix, template=scrm(1)%matrix)
2357 CALL dbcsr_copy(mpz(ispin, 1)%matrix, p_env%p1_admm(ispin)%matrix)
2358 CALL dbcsr_add(mpz(ispin, 1)%matrix, ex_env%matrix_pe_admm(ispin)%matrix, &
2359 1.0_dp, 1.0_dp)
2360 ELSE
2361 CALL dbcsr_create(mpz(ispin, 1)%matrix, template=scrm(1)%matrix)
2362 CALL dbcsr_copy(mpz(ispin, 1)%matrix, matrix_pz_admm(ispin)%matrix)
2363 END IF
2364 mpd(ispin, 1)%matrix => matrix_p(ispin, 1)%matrix
2365 END DO
2366 !
2367 IF (x_data(1, 1)%do_hfx_ri) THEN
2368
2369 eh1 = 0.0_dp
2370 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhz, eh1, rho_ao=mpz, &
2371 geometry_did_change=s_mstruct_changed, nspins=nspins, &
2372 hf_fraction=x_data(1, 1)%general_parameter%fraction)
2373
2374 eh1 = 0.0_dp
2375 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhd, eh1, rho_ao=mpd, &
2376 geometry_did_change=s_mstruct_changed, nspins=nspins, &
2377 hf_fraction=x_data(1, 1)%general_parameter%fraction)
2378
2379 ELSE
2380 DO ispin = 1, mspin
2381 eh1 = 0.0_dp
2382 CALL integrate_four_center(qs_env, x_data, mhz, eh1, mpz, hfx_section, &
2383 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
2384 ispin=ispin)
2385 END DO
2386 DO ispin = 1, mspin
2387 eh1 = 0.0_dp
2388 CALL integrate_four_center(qs_env, x_data, mhd, eh1, mpd, hfx_section, &
2389 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
2390 ispin=ispin)
2391 END DO
2392 END IF
2393 !
2394 CALL get_qs_env(qs_env, admm_env=admm_env)
2395 cpassert(ASSOCIATED(admm_env%work_aux_orb))
2396 cpassert(ASSOCIATED(admm_env%work_orb_orb))
2397 nao = admm_env%nao_orb
2398 nao_aux = admm_env%nao_aux_fit
2399 ALLOCATE (dbwork)
2400 CALL dbcsr_create(dbwork, template=matrix_hz(1)%matrix)
2401 DO ispin = 1, nspins
2402 CALL cp_dbcsr_sm_fm_multiply(mhz(ispin, 1)%matrix, admm_env%A, &
2403 admm_env%work_aux_orb, nao)
2404 CALL parallel_gemm('T', 'N', nao, nao, nao_aux, &
2405 1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
2406 admm_env%work_orb_orb)
2407 CALL dbcsr_copy(dbwork, matrix_hz(ispin)%matrix)
2408 CALL dbcsr_set(dbwork, 0.0_dp)
2409 CALL copy_fm_to_dbcsr(admm_env%work_orb_orb, dbwork, keep_sparsity=.true.)
2410 CALL dbcsr_add(matrix_hz(ispin)%matrix, dbwork, 1.0_dp, 1.0_dp)
2411 END DO
2412 CALL dbcsr_release(dbwork)
2413 DEALLOCATE (dbwork)
2414 ! derivatives Tr (Pz [A(T)H dA/dR])
2415 IF (debug_forces) fodeb(1:3) = force(1)%overlap_admm(1:3, 1)
2416 IF (ASSOCIATED(mhx) .AND. ASSOCIATED(mhy)) THEN
2417 DO ispin = 1, nspins
2418 CALL dbcsr_add(mhd(ispin, 1)%matrix, mhx(ispin, 1)%matrix, 1.0_dp, 1.0_dp)
2419 CALL dbcsr_add(mhz(ispin, 1)%matrix, mhy(ispin, 1)%matrix, 1.0_dp, 1.0_dp)
2420 END DO
2421 END IF
2422 CALL qs_rho_get(rho, rho_ao=matrix_pd)
2423 CALL admm_projection_derivative(qs_env, mhd(:, 1), mpa)
2424 CALL admm_projection_derivative(qs_env, mhz(:, 1), matrix_pd)
2425 IF (debug_forces) THEN
2426 fodeb(1:3) = force(1)%overlap_admm(1:3, 1) - fodeb(1:3)
2427 CALL para_env%sum(fodeb)
2428 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*hfx*S' ", fodeb
2429 END IF
2430 CALL dbcsr_deallocate_matrix_set(mpz)
2431 CALL dbcsr_deallocate_matrix_set(mhz)
2432 CALL dbcsr_deallocate_matrix_set(mhd)
2433 IF (ASSOCIATED(mhx) .AND. ASSOCIATED(mhy)) THEN
2434 CALL dbcsr_deallocate_matrix_set(mhx)
2435 CALL dbcsr_deallocate_matrix_set(mhy)
2436 END IF
2437 DEALLOCATE (mpd)
2438 ELSE
2439 ! -----------------------------------------
2440 ! conventional HFX
2441 ! -----------------------------------------
2442 ALLOCATE (mpz(nspins, 1), mhz(nspins, 1))
2443 DO ispin = 1, nspins
2444 mhz(ispin, 1)%matrix => matrix_hz(ispin)%matrix
2445 mpz(ispin, 1)%matrix => mpa(ispin)%matrix
2446 END DO
2447
2448 IF (x_data(1, 1)%do_hfx_ri) THEN
2449
2450 eh1 = 0.0_dp
2451 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhz, eh1, rho_ao=mpz, &
2452 geometry_did_change=s_mstruct_changed, nspins=nspins, &
2453 hf_fraction=x_data(1, 1)%general_parameter%fraction)
2454 ELSE
2455 DO ispin = 1, mspin
2456 eh1 = 0.0_dp
2457 CALL integrate_four_center(qs_env, x_data, mhz, eh1, mpz, hfx_section, &
2458 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
2459 ispin=ispin)
2460 END DO
2461 END IF
2462 DEALLOCATE (mhz, mpz)
2463 END IF
2464
2465 ! -----------------------------------------
2466 ! HFX FORCES
2467 ! -----------------------------------------
2468
2469 resp_only = .true.
2470 IF (debug_forces) fodeb(1:3) = force(1)%fock_4c(1:3, 1)
2471 IF (dft_control%do_admm) THEN
2472 ! -----------------------------------------
2473 ! HFX-ADMM FORCES
2474 ! -----------------------------------------
2475 CALL qs_rho_get(rho_aux_fit, rho_ao_kp=matrix_p)
2476 NULLIFY (matrix_pza)
2477 CALL dbcsr_allocate_matrix_set(matrix_pza, nspins)
2478 DO ispin = 1, nspins
2479 ALLOCATE (matrix_pza(ispin)%matrix)
2480 IF (do_ex) THEN
2481 CALL dbcsr_create(matrix_pza(ispin)%matrix, template=p_env%p1_admm(ispin)%matrix)
2482 CALL dbcsr_copy(matrix_pza(ispin)%matrix, p_env%p1_admm(ispin)%matrix)
2483 CALL dbcsr_add(matrix_pza(ispin)%matrix, ex_env%matrix_pe_admm(ispin)%matrix, &
2484 1.0_dp, 1.0_dp)
2485 ELSE
2486 CALL dbcsr_create(matrix_pza(ispin)%matrix, template=matrix_pz_admm(ispin)%matrix)
2487 CALL dbcsr_copy(matrix_pza(ispin)%matrix, matrix_pz_admm(ispin)%matrix)
2488 END IF
2489 END DO
2490 IF (x_data(1, 1)%do_hfx_ri) THEN
2491
2492 CALL hfx_ri_update_forces(qs_env, x_data(1, 1)%ri_data, nspins, &
2493 x_data(1, 1)%general_parameter%fraction, &
2494 rho_ao=matrix_p, rho_ao_resp=matrix_pza, &
2495 use_virial=use_virial, resp_only=resp_only)
2496 ELSE
2497 CALL derivatives_four_center(qs_env, matrix_p, matrix_pza, hfx_section, para_env, &
2498 1, use_virial, resp_only=resp_only)
2499 END IF
2500 CALL dbcsr_deallocate_matrix_set(matrix_pza)
2501 ELSE
2502 ! -----------------------------------------
2503 ! conventional HFX FORCES
2504 ! -----------------------------------------
2505 CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
2506 IF (x_data(1, 1)%do_hfx_ri) THEN
2507
2508 CALL hfx_ri_update_forces(qs_env, x_data(1, 1)%ri_data, nspins, &
2509 x_data(1, 1)%general_parameter%fraction, &
2510 rho_ao=matrix_p, rho_ao_resp=mpa, &
2511 use_virial=use_virial, resp_only=resp_only)
2512 ELSE
2513 CALL derivatives_four_center(qs_env, matrix_p, mpa, hfx_section, para_env, &
2514 1, use_virial, resp_only=resp_only)
2515 END IF
2516 END IF ! do_admm
2517
2518 IF (use_virial) THEN
2519 virial%pv_exx = virial%pv_exx - virial%pv_fock_4c
2520 virial%pv_virial = virial%pv_virial - virial%pv_fock_4c
2521 virial%pv_calculate = .false.
2522 END IF
2523
2524 IF (debug_forces) THEN
2525 fodeb(1:3) = force(1)%fock_4c(1:3, 1) - fodeb(1:3)
2526 CALL para_env%sum(fodeb)
2527 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*hfx ", fodeb
2528 END IF
2529 IF (debug_stress .AND. use_virial) THEN
2530 stdeb = -1.0_dp*fconv*virial%pv_fock_4c
2531 CALL para_env%sum(stdeb)
2532 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2533 'STRESS| Pz*hfx ', one_third_sum_diag(stdeb), det_3x3(stdeb)
2534 END IF
2535 END IF ! do_hfx
2536
2537 ! Stress-tensor volume contributions
2538 ! These need to be applied at the end of qs_force
2539 IF (use_virial) THEN
2540 ! Adding mixed Hartree energy twice, due to symmetry
2541 zehartree = zehartree + 2.0_dp*ehartree
2542 zexc = zexc + exc
2543 ! ADMM contribution handled differently in qs_force
2544 IF (dft_control%do_admm) THEN
2545 zexc_aux_fit = zexc_aux_fit + exc_aux_fit
2546 END IF
2547 END IF
2548
2549 ! Overlap matrix
2550 ! H(drho+dz) + Wz
2551 ! If ground-state density matrix solved by diagonalization, then use this
2552 IF (dft_control%qs_control%do_ls_scf) THEN
2553 ! Ground-state density has been calculated by LS
2554 eps_filter = dft_control%qs_control%eps_filter_matrix
2555 CALL calculate_whz_ao_matrix(qs_env, matrix_hz, matrix_wz, eps_filter)
2556 ELSE
2557 IF (do_ex) THEN
2558 matrix_wz => p_env%w1
2559 END IF
2560 focc = 1.0_dp
2561 IF (nspins == 1) focc = 2.0_dp
2562 CALL get_qs_env(qs_env, mos=mos)
2563 DO ispin = 1, nspins
2564 CALL get_mo_set(mo_set=mos(ispin), homo=nocc)
2565 CALL calculate_whz_matrix(mos(ispin)%mo_coeff, matrix_hz(ispin)%matrix, &
2566 matrix_wz(ispin)%matrix, focc, nocc)
2567 END DO
2568 END IF
2569 IF (nspins == 2) THEN
2570 CALL dbcsr_add(matrix_wz(1)%matrix, matrix_wz(2)%matrix, &
2571 alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
2572 END IF
2573
2574 IF (debug_forces) fodeb(1:3) = force(1)%overlap(1:3, 1)
2575 IF (debug_stress .AND. use_virial) stdeb = virial%pv_overlap
2576 NULLIFY (scrm)
2577 CALL build_overlap_matrix(ks_env, matrix_s=scrm, &
2578 matrix_name="OVERLAP MATRIX", &
2579 basis_type_a="ORB", basis_type_b="ORB", &
2580 sab_nl=sab_orb, calculate_forces=.true., &
2581 matrix_p=matrix_wz(1)%matrix)
2582
2583 IF (SIZE(matrix_wz, 1) == 2) THEN
2584 CALL dbcsr_add(matrix_wz(1)%matrix, matrix_wz(2)%matrix, &
2585 alpha_scalar=1.0_dp, beta_scalar=-1.0_dp)
2586 END IF
2587
2588 IF (debug_forces) THEN
2589 fodeb(1:3) = force(1)%overlap(1:3, 1) - fodeb(1:3)
2590 CALL para_env%sum(fodeb)
2591 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Wz*dS ", fodeb
2592 END IF
2593 IF (debug_stress .AND. use_virial) THEN
2594 stdeb = fconv*(virial%pv_overlap - stdeb)
2595 CALL para_env%sum(stdeb)
2596 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2597 'STRESS| WHz ', one_third_sum_diag(stdeb), det_3x3(stdeb)
2598 END IF
2599 CALL dbcsr_deallocate_matrix_set(scrm)
2600
2601 IF (debug_forces) THEN
2602 CALL total_qs_force(ftot2, force, atomic_kind_set)
2603 fodeb(1:3) = ftot2(1:3, 1) - ftot1(1:3, 1)
2604 CALL para_env%sum(fodeb)
2605 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Response Force", fodeb
2606 fodeb(1:3) = ftot2(1:3, 1)
2607 CALL para_env%sum(fodeb)
2608 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Total Force ", fodeb
2609 DEALLOCATE (ftot1, ftot2, ftot3)
2610 END IF
2611 IF (debug_stress .AND. use_virial) THEN
2612 stdeb = fconv*(virial%pv_virial - sttot)
2613 CALL para_env%sum(stdeb)
2614 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2615 'STRESS| Stress Response ', one_third_sum_diag(stdeb), det_3x3(stdeb)
2616 stdeb = fconv*(virial%pv_virial)
2617 CALL para_env%sum(stdeb)
2618 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,A,T41,2(1X,ES19.11))") &
2619 'STRESS| Total Stress ', one_third_sum_diag(stdeb), det_3x3(stdeb)
2620 IF (iounit > 0) WRITE (unit=iounit, fmt="(T2,3(1X,ES19.11))") &
2621 stdeb(1, 1), stdeb(2, 2), stdeb(3, 3)
2622 unitstr = "bar"
2623 END IF
2624
2625 IF (do_ex) THEN
2626 CALL dbcsr_deallocate_matrix_set(mpa)
2627 CALL dbcsr_deallocate_matrix_set(matrix_hz)
2628 END IF
2629
2630 CALL timestop(handle)
2631
2632 END SUBROUTINE response_force
2633
2634! **************************************************************************************************
2635!> \brief ...
2636!> \param qs_env ...
2637!> \param p_env ...
2638!> \param matrix_hz ...
2639!> \param ex_env ...
2640!> \param debug ...
2641! **************************************************************************************************
2642 SUBROUTINE response_force_xtb(qs_env, p_env, matrix_hz, ex_env, debug)
2643 TYPE(qs_environment_type), POINTER :: qs_env
2644 TYPE(qs_p_env_type) :: p_env
2645 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_hz
2646 TYPE(excited_energy_type), OPTIONAL, POINTER :: ex_env
2647 LOGICAL, INTENT(IN), OPTIONAL :: debug
2648
2649 CHARACTER(LEN=*), PARAMETER :: routinen = 'response_force_xtb'
2650
2651 INTEGER :: atom_a, handle, iatom, ikind, iounit, &
2652 is, ispin, na, natom, natorb, nimages, &
2653 nkind, nocc, ns, nsgf, nspins
2654 INTEGER, DIMENSION(25) :: lao
2655 INTEGER, DIMENSION(5) :: occ
2656 LOGICAL :: debug_forces, do_ex, use_virial
2657 REAL(kind=dp) :: focc
2658 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: mcharge, mcharge1
2659 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: aocg, aocg1, charges, charges1, ftot1, &
2660 ftot2
2661 REAL(kind=dp), DIMENSION(3) :: fodeb
2662 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
2663 TYPE(cp_logger_type), POINTER :: logger
2664 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_pz, matrix_wz, mpa, p_matrix, scrm
2665 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_p, matrix_s
2666 TYPE(dbcsr_type), POINTER :: s_matrix
2667 TYPE(dft_control_type), POINTER :: dft_control
2668 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
2669 TYPE(mp_para_env_type), POINTER :: para_env
2670 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2671 POINTER :: sab_orb
2672 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2673 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
2674 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
2675 TYPE(qs_ks_env_type), POINTER :: ks_env
2676 TYPE(qs_rho_type), POINTER :: rho
2677 TYPE(xtb_atom_type), POINTER :: xtb_kind
2678
2679 CALL timeset(routinen, handle)
2680
2681 IF (PRESENT(debug)) THEN
2682 debug_forces = debug
2683 ELSE
2684 debug_forces = .false.
2685 END IF
2686
2687 logger => cp_get_default_logger()
2688 IF (logger%para_env%is_source()) THEN
2689 iounit = cp_logger_get_default_unit_nr(logger, local=.true.)
2690 ELSE
2691 iounit = -1
2692 END IF
2693
2694 do_ex = .false.
2695 IF (PRESENT(ex_env)) do_ex = .true.
2696
2697 NULLIFY (ks_env, sab_orb)
2698 CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, dft_control=dft_control, &
2699 sab_orb=sab_orb)
2700 CALL get_qs_env(qs_env=qs_env, para_env=para_env, force=force)
2701 nspins = dft_control%nspins
2702
2703 IF (debug_forces) THEN
2704 CALL get_qs_env(qs_env, natom=natom, atomic_kind_set=atomic_kind_set)
2705 ALLOCATE (ftot1(3, natom))
2706 ALLOCATE (ftot2(3, natom))
2707 CALL total_qs_force(ftot1, force, atomic_kind_set)
2708 END IF
2709
2710 matrix_pz => p_env%p1
2711 NULLIFY (mpa)
2712 IF (do_ex) THEN
2713 CALL dbcsr_allocate_matrix_set(mpa, nspins)
2714 DO ispin = 1, nspins
2715 ALLOCATE (mpa(ispin)%matrix)
2716 CALL dbcsr_create(mpa(ispin)%matrix, template=matrix_pz(ispin)%matrix)
2717 CALL dbcsr_copy(mpa(ispin)%matrix, matrix_pz(ispin)%matrix)
2718 CALL dbcsr_add(mpa(ispin)%matrix, ex_env%matrix_pe(ispin)%matrix, 1.0_dp, 1.0_dp)
2719 CALL dbcsr_set(matrix_hz(ispin)%matrix, 0.0_dp)
2720 END DO
2721 ELSE
2722 mpa => p_env%p1
2723 END IF
2724 !
2725 ! START OF Tr(P+Z)Hcore
2726 !
2727 IF (nspins == 2) THEN
2728 CALL dbcsr_add(mpa(1)%matrix, mpa(2)%matrix, 1.0_dp, 1.0_dp)
2729 END IF
2730 ! Hcore matrix
2731 IF (debug_forces) fodeb(1:3) = force(1)%all_potential(1:3, 1)
2732 CALL build_xtb_hab_force(qs_env, mpa(1)%matrix)
2733 IF (debug_forces) THEN
2734 fodeb(1:3) = force(1)%all_potential(1:3, 1) - fodeb(1:3)
2735 CALL para_env%sum(fodeb)
2736 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Pz*dHcore ", fodeb
2737 END IF
2738 IF (nspins == 2) THEN
2739 CALL dbcsr_add(mpa(1)%matrix, mpa(2)%matrix, 1.0_dp, -1.0_dp)
2740 END IF
2741 !
2742 ! END OF Tr(P+Z)Hcore
2743 !
2744 use_virial = .false.
2745 nimages = 1
2746 !
2747 ! Hartree potential of response density
2748 !
2749 IF (dft_control%qs_control%xtb_control%coulomb_interaction) THEN
2750 ! Mulliken charges
2751 CALL get_qs_env(qs_env, rho=rho, particle_set=particle_set, matrix_s_kp=matrix_s)
2752 natom = SIZE(particle_set)
2753 CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
2754 ALLOCATE (mcharge(natom), charges(natom, 5))
2755 ALLOCATE (mcharge1(natom), charges1(natom, 5))
2756 charges = 0.0_dp
2757 charges1 = 0.0_dp
2758 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
2759 nkind = SIZE(atomic_kind_set)
2760 CALL get_qs_kind_set(qs_kind_set, maxsgf=nsgf)
2761 ALLOCATE (aocg(nsgf, natom))
2762 aocg = 0.0_dp
2763 ALLOCATE (aocg1(nsgf, natom))
2764 aocg1 = 0.0_dp
2765 p_matrix => matrix_p(:, 1)
2766 s_matrix => matrix_s(1, 1)%matrix
2767 CALL ao_charges(p_matrix, s_matrix, aocg, para_env)
2768 CALL ao_charges(mpa, s_matrix, aocg1, para_env)
2769 DO ikind = 1, nkind
2770 CALL get_atomic_kind(atomic_kind_set(ikind), natom=na)
2771 CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
2772 CALL get_xtb_atom_param(xtb_kind, natorb=natorb, lao=lao, occupation=occ)
2773 DO iatom = 1, na
2774 atom_a = atomic_kind_set(ikind)%atom_list(iatom)
2775 charges(atom_a, :) = real(occ(:), kind=dp)
2776 DO is = 1, natorb
2777 ns = lao(is) + 1
2778 charges(atom_a, ns) = charges(atom_a, ns) - aocg(is, atom_a)
2779 charges1(atom_a, ns) = charges1(atom_a, ns) - aocg1(is, atom_a)
2780 END DO
2781 END DO
2782 END DO
2783 DEALLOCATE (aocg, aocg1)
2784 DO iatom = 1, natom
2785 mcharge(iatom) = sum(charges(iatom, :))
2786 mcharge1(iatom) = sum(charges1(iatom, :))
2787 END DO
2788 ! Coulomb Kernel
2789 CALL xtb_coulomb_hessian(qs_env, matrix_hz, charges1, mcharge1, mcharge, mpa)
2790 CALL calc_xtb_ehess_force(qs_env, p_matrix, mpa, charges, mcharge, charges1, &
2791 mcharge1, debug_forces)
2792 !
2793 DEALLOCATE (charges, mcharge, charges1, mcharge1)
2794 END IF
2795 ! Overlap matrix
2796 ! H(drho+dz) + Wz
2797 matrix_wz => p_env%w1
2798 focc = 0.5_dp
2799 IF (nspins == 1) focc = 1.0_dp
2800 CALL get_qs_env(qs_env, mos=mos)
2801 DO ispin = 1, nspins
2802 CALL get_mo_set(mo_set=mos(ispin), homo=nocc)
2803 CALL calculate_whz_matrix(mos(ispin)%mo_coeff, matrix_hz(ispin)%matrix, &
2804 matrix_wz(ispin)%matrix, focc, nocc)
2805 END DO
2806 IF (nspins == 2) THEN
2807 CALL dbcsr_add(matrix_wz(1)%matrix, matrix_wz(2)%matrix, &
2808 alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
2809 END IF
2810 IF (debug_forces) fodeb(1:3) = force(1)%overlap(1:3, 1)
2811 NULLIFY (scrm)
2812 CALL build_overlap_matrix(ks_env, matrix_s=scrm, &
2813 matrix_name="OVERLAP MATRIX", &
2814 basis_type_a="ORB", basis_type_b="ORB", &
2815 sab_nl=sab_orb, calculate_forces=.true., &
2816 matrix_p=matrix_wz(1)%matrix)
2817 IF (debug_forces) THEN
2818 fodeb(1:3) = force(1)%overlap(1:3, 1) - fodeb(1:3)
2819 CALL para_env%sum(fodeb)
2820 IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Wz*dS ", fodeb
2821 END IF
2822 CALL dbcsr_deallocate_matrix_set(scrm)
2823
2824 IF (debug_forces) THEN
2825 CALL total_qs_force(ftot2, force, atomic_kind_set)
2826 fodeb(1:3) = ftot2(1:3, 1) - ftot1(1:3, 1)
2827 CALL para_env%sum(fodeb)
2828 IF (iounit > 0) WRITE (iounit, "(T3,A,T30,3F16.8)") "DEBUG:: Response Force", fodeb
2829 DEALLOCATE (ftot1, ftot2)
2830 END IF
2831
2832 IF (do_ex) THEN
2833 CALL dbcsr_deallocate_matrix_set(mpa)
2834 END IF
2835
2836 CALL timestop(handle)
2837
2838 END SUBROUTINE response_force_xtb
2839
2840! **************************************************************************************************
2841!> \brief Win = focc*(P*(H[P_out - P_in] + H[Z] )*P)
2842!> Langrange multiplier matrix with response and perturbation (Harris) kernel matrices
2843!>
2844!> \param qs_env ...
2845!> \param matrix_hz ...
2846!> \param matrix_whz ...
2847!> \param eps_filter ...
2848!> \param
2849!> \par History
2850!> 2020.2 created [Fabian Belleflamme]
2851!> \author Fabian Belleflamme
2852! **************************************************************************************************
2853 SUBROUTINE calculate_whz_ao_matrix(qs_env, matrix_hz, matrix_whz, eps_filter)
2854
2855 TYPE(qs_environment_type), POINTER :: qs_env
2856 TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN), &
2857 POINTER :: matrix_hz
2858 TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT), &
2859 POINTER :: matrix_whz
2860 REAL(kind=dp), INTENT(IN) :: eps_filter
2861
2862 CHARACTER(len=*), PARAMETER :: routinen = 'calculate_whz_ao_matrix'
2863
2864 INTEGER :: handle, ispin, nspins
2865 REAL(kind=dp) :: scaling
2866 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
2867 TYPE(dbcsr_type) :: matrix_tmp
2868 TYPE(dft_control_type), POINTER :: dft_control
2869 TYPE(mp_para_env_type), POINTER :: para_env
2870 TYPE(qs_rho_type), POINTER :: rho
2871
2872 CALL timeset(routinen, handle)
2873
2874 cpassert(ASSOCIATED(qs_env))
2875 cpassert(ASSOCIATED(matrix_hz))
2876 cpassert(ASSOCIATED(matrix_whz))
2877
2878 CALL get_qs_env(qs_env=qs_env, &
2879 dft_control=dft_control, &
2880 rho=rho, &
2881 para_env=para_env)
2882 nspins = dft_control%nspins
2883 CALL qs_rho_get(rho, rho_ao=rho_ao)
2884
2885 ! init temp matrix
2886 CALL dbcsr_create(matrix_tmp, template=matrix_hz(1)%matrix, &
2887 matrix_type=dbcsr_type_no_symmetry)
2888
2889 !Spin factors simplify to
2890 scaling = 1.0_dp
2891 IF (nspins == 1) scaling = 0.5_dp
2892
2893 ! Operation in MO-solver :
2894 ! Whz = focc*(CC^T*Hz*CC^T)
2895 ! focc = 2.0_dp Closed-shell
2896 ! focc = 1.0_dp Open-shell
2897
2898 ! Operation in AO-solver :
2899 ! Whz = (scaling*P)*(focc*Hz)*(scaling*P)
2900 ! focc see above
2901 ! scaling = 0.5_dp Closed-shell (P = 2*CC^T), WHz = (0.5*P)*(2*Hz)*(0.5*P)
2902 ! scaling = 1.0_dp Open-shell, WHz = P*Hz*P
2903
2904 ! Spin factors from Hz and P simplify to
2905 scaling = 1.0_dp
2906 IF (nspins == 1) scaling = 0.5_dp
2907
2908 DO ispin = 1, nspins
2909
2910 ! tmp = H*CC^T
2911 CALL dbcsr_multiply("N", "N", scaling, matrix_hz(ispin)%matrix, rho_ao(ispin)%matrix, &
2912 0.0_dp, matrix_tmp, filter_eps=eps_filter)
2913 ! WHz = CC^T*tmp
2914 ! WHz = Wz + (scaling*P)*(focc*Hz)*(scaling*P)
2915 ! WHz = Wz + scaling*(P*Hz*P)
2916 CALL dbcsr_multiply("N", "N", 1.0_dp, rho_ao(ispin)%matrix, matrix_tmp, &
2917 1.0_dp, matrix_whz(ispin)%matrix, filter_eps=eps_filter, &
2918 retain_sparsity=.true.)
2919
2920 END DO
2921
2922 CALL dbcsr_release(matrix_tmp)
2923
2924 CALL timestop(handle)
2925
2926 END SUBROUTINE calculate_whz_ao_matrix
2927
2928! **************************************************************************************************
2929
2930END MODULE response_solver
subroutine, public accint_weight_force(qs_env, rho, rho1, order, xc_section, triplet, force_scale)
...
Contains ADMM methods which require molecular orbitals.
subroutine, public admm_projection_derivative(qs_env, matrix_hz, matrix_pz, fval)
Calculate derivatives terms from overlap matrices.
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.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
Handles all functions related to the CELL.
Definition cell_types.F:15
methods related to the blacs parallel environment
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_multiply(transa, transb, alpha, matrix_a, matrix_b, beta, matrix_c, first_row, last_row, first_column, last_column, first_k, last_k, retain_sparsity, filter_eps, flop)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
DBCSR operations in CP2K.
subroutine, public cp_dbcsr_sm_fm_multiply(matrix, fm_in, fm_out, ncol, alpha, beta)
multiply a dbcsr with a fm matrix
subroutine, public copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
Copy a DBCSR matrix to a BLACS matrix.
subroutine, public copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
Copy a BLACS matrix to a dbcsr matrix.
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_set_all(matrix, alpha, beta)
set all elements of a matrix to the same value, and optionally the diagonal to a different one
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
subroutine, public cp_fm_init_random(matrix, ncol, start_col)
fills a matrix with random numbers
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
Types needed for a for a Energy Correction.
Routines used for Harris functional Kohn-Sham calculation.
Definition ec_methods.F:15
subroutine, public ec_mos_init(qs_env, matrix_s)
Allocate and initiate molecular orbitals environment.
Definition ec_methods.F:67
AO-based conjugate-gradient response solver routines.
subroutine, public ec_response_ao(qs_env, p_env, matrix_hz, matrix_pz, matrix_wz, iounit, should_stop, silent)
AO-based conjugate gradient linear response solver. In goes the right hand side B of the equation AZ=...
Types for excited states potential energies.
subroutine, public init_coulomb_local(hartree_local, natom)
...
subroutine, public vh_1c_gg_integrals(qs_env, energy_hartree_1c, ecoul_1c, local_rho_set, para_env, tddft, local_rho_set_2nd, core_2nd)
Calculates one center GAPW Hartree energies and matrix elements Hartree potentials are input Takes po...
subroutine, public hartree_local_release(hartree_local)
...
subroutine, public hartree_local_create(hartree_local)
...
Routines to calculate derivatives with respect to basis function origin.
subroutine, public derivatives_four_center(qs_env, rho_ao, rho_ao_resp, hfx_section, para_env, irep, use_virial, adiabatic_rescale_factor, resp_only, external_x_data, nspins)
computes four center derivatives for a full basis set and updates the forcesfock_4c arrays....
Routines to calculate HFX energy and potential.
subroutine, public integrate_four_center(qs_env, x_data, ks_matrix, ehfx, rho_ao, hfx_section, para_env, geometry_did_change, irep, distribute_fock_matrix, ispin, nspins)
computes four center integrals for a full basis set and updates the Kohn-Sham-Matrix and energy....
RI-methods for HFX.
Definition hfx_ri.F:12
subroutine, public hfx_ri_update_ks(qs_env, ri_data, ks_matrix, ehfx, mos, rho_ao, geometry_did_change, nspins, hf_fraction)
...
Definition hfx_ri.F:1041
subroutine, public hfx_ri_update_forces(qs_env, ri_data, nspins, hf_fraction, rho_ao, rho_ao_resp, mos, use_virial, resp_only, rescale_factor)
the general routine that calls the relevant force code
Definition hfx_ri.F:3046
Types and set/get functions for HFX.
Definition hfx_types.F:16
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public precond_mlp
integer, parameter, public kg_tnadd_embed_ri
integer, parameter, public kg_tnadd_embed
integer, parameter, public ec_mo_solver
integer, parameter, public ot_precond_full_kinetic
integer, parameter, public kg_tnadd_atomic
integer, parameter, public do_admm_aux_exch_func_none
integer, parameter, public ls_s_sqrt_proot
integer, parameter, public ot_precond_full_single
integer, parameter, public ls_s_sqrt_ns
integer, parameter, public ot_precond_none
integer, parameter, public ot_precond_full_single_inverse
integer, parameter, public ec_functional_ext
integer, parameter, public xc_none
integer, parameter, public ot_precond_s_inverse
integer, parameter, public ot_precond_full_all
integer, parameter, public ec_ls_solver
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
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.
Calculation of the local potential contribution of the nonadditive kinetic energy <a|V(local)|b> = <a...
subroutine, public build_tnadd_mat(kg_env, matrix_p, force, virial, calculate_forces, use_virial, qs_kind_set, atomic_kind_set, particle_set, sab_orb, dbcsr_dist)
...
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
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:127
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
Interface to the message passing library MPI.
compute mulliken charges we (currently) define them as c_i = 1/2 [ (PS)_{ii} + (SP)_{ii}...
Definition mulliken.F:13
basic linear algebra operations for full matrixes
Define the data structure for the particle information.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public pascal
Definition physcon.F:174
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
functions related to the poisson solver on regular grids
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Routines to calculate 2nd order kernels from a given response density in ao basis linear response scf...
subroutine, public build_dm_response(c0, c1, dm)
This routine builds response density in dbcsr format.
Calculate the plane wave density by collocating the primitive Gaussian functions (pgf).
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
Calculation of the core Hamiltonian integral matrix <a|H|b> over Cartesian Gaussian-type functions.
subroutine, public core_matrices(qs_env, matrix_h, matrix_p, calculate_forces, nder, ec_env, dcdr_env, ec_env_matrices, ext_kpoints, basis_type, debug_forces, debug_stress, atcore)
...
subroutine, public kinetic_energy_matrix(qs_env, matrixkp_t, matrix_t, matrix_p, ext_kpoints, matrix_name, calculate_forces, nderivative, sab_orb, eps_filter, basis_type, debug_forces, debug_stress)
Calculate kinetic energy matrix and possible relativistic correction.
collects routines that calculate density matrices
subroutine, public calculate_whz_matrix(c0vec, hzm, w_matrix, focc, nocc)
Calculate the Wz matrix from the MO eigenvectors, MO eigenvalues, and the MO occupation numbers....
subroutine, public calculate_wz_matrix(mo_set, psi1, ks_matrix, w_matrix)
Calculate the response W matrix from the MO eigenvectors, MO eigenvalues, and the MO occupation numbe...
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.
subroutine, public total_qs_force(force, qs_force, atomic_kind_set)
Get current total force.
Setup Routine for Fxc Potentials.
Definition qs_fxc.F:15
subroutine, public qs_fxc_create(qs_env, rho0_struct, rho1_struct, rho0_atom_set, xc_section, do_onecenter, fxc_rho, fxc_tau, rho1_atom_set, do_scale, is_triplet, spinflip, no_weights, uf_grid_results, pw_env_ext, kind_set_external, para_env_external, dispersion_env, compute_virial, virial_xc)
...
Definition qs_fxc.F:116
subroutine, public prepare_gapw_den(qs_env, local_rho_set, do_rho0, kind_set_external, pw_env_sub)
...
Integrate single or product functions over a potential on a RS grid.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
subroutine, public get_qs_kind_set(qs_kind_set, all_potential_present, tnadd_potential_present, gth_potential_present, sgp_potential_present, paw_atom_present, dft_plus_u_atom_present, maxcgf, maxsgf, maxco, maxco_proj, maxgtops, maxlgto, maxlprj, maxnset, maxsgf_set, ncgf, npgf, nset, nsgf, nshell, maxpol, maxlppl, maxlppnl, maxppnl, nelectron, maxder, max_ngrid_rad, max_sph_harm, maxg_iso_not0, lmax_rho0, basis_rcut, do_mtlr_present, basis_type, total_zeff_corr, npgf_seg, cneo_potential_present, nkind_q, natom_q)
Get attributes of an atomic kind set.
routines that build the Kohn-Sham matrix contributions coming from local atomic densities
Definition qs_ks_atom.F:12
subroutine, public update_ks_atom(qs_env, ksmat, pmat, forces, tddft, rho_atom_external, kind_set_external, oce_external, sab_external, kscale, kintegral, kforce, fscale)
The correction to the KS matrix due to the GAPW local terms to the hartree and XC contributions is he...
Definition qs_ks_atom.F:144
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
subroutine, public calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
...
localize wavefunctions linear response scf
subroutine, public linres_solver(p_env, qs_env, psi1, h1_psi0, psi0_order, iounit, should_stop, silent)
scf loop to optimize the first order wavefunctions (psi1) given a perturbation as an operator applied...
Type definitiona for linear response calculations.
subroutine, public local_rho_set_create(local_rho_set)
...
subroutine, public local_rho_set_release(local_rho_set)
...
wrapper for the pools of matrixes
subroutine, public mpools_rebuild_fm_pools(mpools, mos, blacs_env, para_env, nmosub)
rebuilds the pools of the (ao x mo, ao x ao , mo x mo) full matrixes
collects routines that perform operations directly related to MOs
subroutine, public make_basis_sm(vmatrix, ncol, matrix_s)
returns an S-orthonormal basis v (v^T S v ==1)
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public deallocate_mo_set(mo_set)
Deallocate a wavefunction data structure.
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count, cmo_coeff)
Get the components of a MO set data structure.
Define the neighbor list data types and the corresponding functionality.
Calculation of overlap matrix, its derivatives and forces.
Definition qs_overlap.F:19
subroutine, public build_overlap_matrix(ks_env, matrix_s, matrixkp_s, matrix_name, nderivative, basis_type_a, basis_type_b, sab_nl, calculate_forces, matrix_p, matrixkp_p, ext_kpoints)
Calculation of the overlap matrix over Cartesian Gaussian functions.
Definition qs_overlap.F:121
Utility functions for the perturbation calculations.
subroutine, public p_env_psi0_changed(p_env, qs_env)
To be called after the value of psi0 has changed. Recalculates the quantities S_psi0 and m_epsilon.
subroutine, public p_env_create(p_env, qs_env, p1_option, p1_admm_option, orthogonal_orbitals, linres_control)
allocates and initializes the perturbation environment (no setup)
basis types for the calculation of the perturbation of density theory.
subroutine, public p_env_release(p_env)
relases the given p_env (see doc/ReferenceCounting.html)
subroutine, public rho0_s_grid_create(pw_env, rho0_mpole)
...
subroutine, public integrate_vhg0_rspace(qs_env, v_rspace, para_env, calculate_forces, local_rho_set, local_rho_set_2nd, atener, kforce, my_pools, my_rs_descs)
...
subroutine, public init_rho0(local_rho_set, qs_env, gapw_control, zcore)
...
subroutine, public allocate_rho_atom_internals(rho_atom_set, atomic_kind_set, qs_kind_set, dft_control, para_env)
...
subroutine, public calculate_rho_atom_coeff(qs_env, rho_ao, rho_atom_set, qs_kind_set, oce, sab, para_env)
...
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_create(rho)
Allocates a new instance of rho.
routines that build the integrals of the Vxc potential calculated for the atomic density in the basis...
Definition qs_vxc_atom.F:12
subroutine, public calculate_vxc_atom(qs_env, energy_only, exc1, adiabatic_rescale_factor, kind_set_external, rho_atom_set_external, xc_section_external, calculate_forces, composite_vxc_rho, composite_vxc_tau, composite_reference_active, direct_valence_atom_grid, atom_composite_grid)
...
Calculate the CPKS equation and the resulting forces.
subroutine, public response_force_xtb(qs_env, p_env, matrix_hz, ex_env, debug)
...
subroutine, public response_force(qs_env, vh_rspace, vxc_rspace, vtau_rspace, vadmm_rspace, vadmm_tau_rspace, matrix_hz, matrix_pz, matrix_pz_admm, matrix_wz, zehartree, zexc, zexc_aux_fit, rhopz_r, p_env, ex_env, debug)
...
subroutine, public response_calculation(qs_env, ec_env, silent)
Initializes solver of linear response equation for energy correction.
subroutine, public response_equation(qs_env, p_env, cpmos, iounit, lr_section, silent)
Initializes vectors for MO-coefficient based linear response solver and calculates response density,...
subroutine, public response_equation_new(qs_env, p_env, cpmos, iounit, silent)
Initializes vectors for MO-coefficient based linear response solver and calculates response density,...
types for task lists
pure real(kind=dp) function, public one_third_sum_diag(a)
...
type(xc_rho_cflags_type) function, public xc_functionals_get_needs(functionals, lsd, calc_potential)
...
contains the structure
Calculation of forces for Coulomb contributions in response xTB.
subroutine, public calc_xtb_ehess_force(qs_env, matrix_p0, matrix_p1, charges0, mcharge0, charges1, mcharge1, debug_forces)
...
Calculation of Coulomb Hessian contributions in xTB.
Definition xtb_ehess.F:12
subroutine, public xtb_coulomb_hessian(qs_env, ks_matrix, charges1, mcharge1, mcharge, matrix_p1)
...
Definition xtb_ehess.F:79
Calculation of xTB Hamiltonian derivative Reference: Stefan Grimme, Christoph Bannwarth,...
subroutine, public build_xtb_hab_force(qs_env, p_matrix)
...
Definition of the xTB parameter types.
Definition xtb_types.F:20
subroutine, public get_xtb_atom_param(xtb_parameter, symbol, aname, typ, defined, z, zeff, natorb, lmax, nao, lao, rcut, rcov, kx, eta, xgamma, alpha, zneff, nshell, nval, lval, kpoly, kappa, wall, hen, zeta, xi, kappa0, alpg, occupation, ngauss, electronegativity, chmax, en, kqat2, kcn, kq)
...
Definition xtb_types.F:206
stores some data used in wavefunction fitting
Definition admm_types.F:120
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Contains information on the energy correction functional for KG.
Contains information on the excited states energy.
stores some data used in construction of Kohn-Sham matrix
Definition hfx_types.F:474
Contains all the info needed for KG runs...
stores all the informations relevant to an mpi environment
contained for different pw related things
environment for the poisson solver
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Provides all information about a quickstep kind.
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
General settings for linear response calculations.
Represent a qs system that is perturbed. Can calculate the linear operator and the rhs of the system ...
keeps the density in various representations, keeping track of which ones are valid.
contains a flag for each component of xc_rho_set, so that you can use it to tell which components you...