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