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