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