167 SUBROUTINE fhxc_force(qs_env, ex_env, gs_mos, full_kernel, debug_forces)
174 LOGICAL,
INTENT(IN) :: debug_forces
176 CHARACTER(LEN=*),
PARAMETER :: routinen =
'fhxc_force'
178 CHARACTER(LEN=default_string_length) :: basis_type
179 INTEGER :: handle, ia, ib, iounit, ispin, mspin, myfun, n_rep_hf, nactive(2), nao, nao_aux, &
180 natom, nkind, norb(2), nsev, nspins, nsteps, order, spin
181 LOGICAL :: analytic_2nd_deriv, analytic_3rd_deriv, distribute_fock_matrix, do_admm, do_hfx, &
182 do_hfxlr, do_hfxsr, do_onecenter, do_res, do_sf, gapw, gapw_xc, hfx_treat_lsd_in_core, &
183 is_rks_triplets, needs_tau_response, needs_tau_response_aux, s_mstruct_changed, use_virial
184 REAL(kind=
dp) :: eh1, eh1c, eps_delta, eps_fit, focc, &
185 fscal, fval, kval, xehartree
186 REAL(kind=
dp),
DIMENSION(3) :: fodeb
190 TYPE(
cp_fm_type) :: avamat, avcmat, cpscr, cvcmat, vavec, &
192 TYPE(
cp_fm_type),
DIMENSION(:),
POINTER :: cpmos, evect
193 TYPE(
cp_fm_type),
POINTER :: mos, mos2, mosa, mosa2
195 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: matrix_fx, matrix_gx, matrix_hfx, &
196 matrix_hfx_admm, matrix_hfx_admm_asymm, matrix_hfx_asymm, matrix_hx, matrix_p, &
197 matrix_p_admm, matrix_px1, matrix_px1_admm, matrix_px1_admm_asymm, matrix_px1_asymm, &
198 matrix_s, matrix_s_aux_fit, matrix_wx1, mdum, mfx, mgx
199 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: mhe, mpe, mpga
200 TYPE(
dbcsr_type),
POINTER :: dbwork, dbwork_asymm
203 TYPE(
hfx_type),
DIMENSION(:, :),
POINTER :: x_data
204 TYPE(
local_rho_type),
POINTER :: local_rho_set, local_rho_set_admm, local_rho_set_f, &
205 local_rho_set_f_admm, local_rho_set_g, local_rho_set_g_admm
208 POINTER :: sab, sab_aux_fit, sab_orb, sap_oce
212 TYPE(
pw_c1d_gs_type),
DIMENSION(:),
POINTER :: rho_g_aux, rhox_g, rhox_g_aux, &
213 rhox_tau_g, rhox_tau_g_aux, rhoxx_g, &
219 TYPE(
pw_r3d_rs_type),
DIMENSION(:),
POINTER :: fxc_rho, fxc_tau, gxc_rho, gxc_tau, &
220 rho_r_aux, rhox_r, rhox_r_aux, &
221 rhox_tau_r, rhox_tau_r_aux, rhoxx_r, &
225 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
227 TYPE(
qs_rho_type),
POINTER :: rho, rho_aux_fit, rhox, rhox_aux
228 TYPE(
rho_atom_type),
DIMENSION(:),
POINTER :: rho_atom_set, rho_atom_set_f, &
235 CALL timeset(routinen, handle)
238 IF (logger%para_env%is_source())
THEN
244 CALL get_qs_env(qs_env, dft_control=dft_control)
245 tddfpt_control => dft_control%tddfpt2_control
246 nspins = dft_control%nspins
247 is_rks_triplets = tddfpt_control%rks_triplets .AND. (nspins == 1)
254 do_hfx = tddfpt_control%do_hfx
255 do_hfxsr = tddfpt_control%do_hfxsr
256 do_hfxlr = tddfpt_control%do_hfxlr
257 do_admm = tddfpt_control%do_admm
258 gapw = dft_control%qs_control%gapw
259 gapw_xc = dft_control%qs_control%gapw_xc
260 xc_section => full_kernel%xc_section
263 needs_tau_response = needs%tau .OR. needs%tau_spin
264 needs_tau_response_aux = .false.
266 evect => ex_env%evect
267 matrix_px1 => ex_env%matrix_px1
268 matrix_px1_admm => ex_env%matrix_px1_admm
269 matrix_px1_asymm => ex_env%matrix_px1_asymm
270 matrix_px1_admm_asymm => ex_env%matrix_px1_admm_asymm
273 IF (nspins == 2) focc = 0.5_dp
274 nsev =
SIZE(evect, 1)
278 CALL dbcsr_set(matrix_px1(ispin)%matrix, 0.0_dp)
280 matrix_v=evect(ispin), &
281 matrix_g=gs_mos(ispin)%mos_active, &
282 ncol=nactive(ispin), alpha=2.0_dp*focc, symmetry_mode=1)
285 CALL dbcsr_set(matrix_px1_asymm(ispin)%matrix, 0.0_dp)
287 matrix_v=gs_mos(ispin)%mos_active, &
288 matrix_g=evect(ispin), &
289 ncol=nactive(ispin), alpha=2.0_dp*focc, &
293 CALL get_qs_env(qs_env, ks_env=ks_env, pw_env=pw_env, para_env=para_env)
294 CALL get_qs_env(qs_env, xcint_weights=weights)
298 NULLIFY (hartree_local, local_rho_set, local_rho_set_admm)
299 IF (gapw .OR. gapw_xc)
THEN
300 IF (nspins == 2)
THEN
306 atomic_kind_set=atomic_kind_set, &
307 qs_kind_set=qs_kind_set)
310 qs_kind_set, dft_control, para_env)
313 CALL init_rho0(local_rho_set, qs_env, dft_control%qs_control%gapw_control, &
320 CALL get_qs_env(qs_env=qs_env, oce=oce, sap_oce=sap_oce, sab_orb=sab)
322 CALL get_qs_env(qs_env=qs_env, nkind=nkind, particle_set=particle_set)
324 eps_fit = dft_control%qs_control%gapw_control%eps_fit
329 mpga(1:nsev, 1:1) => matrix_px1(1:nsev)
331 qs_kind_set, oce, sab, para_env)
336 qs_kind_set, dft_control, para_env)
338 qs_kind_set, oce, sab, para_env)
343 qs_kind_set, dft_control, para_env)
345 qs_kind_set, oce, sab, para_env)
347 IF (nspins == 2)
THEN
356 nao_aux = admm_env%nao_aux_fit
357 nao = admm_env%nao_orb
363 1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
364 admm_env%work_aux_orb)
366 1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
367 admm_env%work_aux_aux)
368 CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, matrix_px1_admm(ispin)%matrix, &
369 keep_sparsity=.true.)
371 CALL copy_dbcsr_to_fm(matrix_px1_asymm(ispin)%matrix, admm_env%work_orb_orb)
373 1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
374 admm_env%work_aux_orb)
376 1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
377 admm_env%work_aux_aux)
378 CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, matrix_px1_admm_asymm(ispin)%matrix, &
379 keep_sparsity=.true.)
382 IF (admm_env%do_gapw)
THEN
383 IF (do_admm .AND. tddfpt_control%admm_xc_correction)
THEN
387 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
390 admm_env%admm_gapw_env%admm_kind_set, dft_control, para_env)
391 mpga(1:nsev, 1:1) => matrix_px1_admm(1:nsev)
394 admm_env%admm_gapw_env%admm_kind_set, &
395 admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
397 kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
401 admm_env%admm_gapw_env%admm_kind_set, dft_control, para_env)
403 admm_env%admm_gapw_env%admm_kind_set, &
404 admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
406 kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
410 admm_env%admm_gapw_env%admm_kind_set, dft_control, para_env)
412 admm_env%admm_gapw_env%admm_kind_set, &
413 admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
415 kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
421 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
422 poisson_env=poisson_env)
424 NULLIFY (rhox_tau_g, rhox_tau_r, rhoxx_tau_g, rhoxx_tau_r)
425 ALLOCATE (rhox_r(nsev), rhox_g(nsev))
426 DO ispin = 1,
SIZE(evect, 1)
427 CALL auxbas_pw_pool%create_pw(rhox_r(ispin))
428 CALL auxbas_pw_pool%create_pw(rhox_g(ispin))
430 IF (needs_tau_response)
THEN
431 ALLOCATE (rhox_tau_r(nsev), rhox_tau_g(nsev))
432 DO ispin = 1,
SIZE(evect, 1)
433 CALL auxbas_pw_pool%create_pw(rhox_tau_r(ispin))
434 CALL auxbas_pw_pool%create_pw(rhox_tau_g(ispin))
437 CALL auxbas_pw_pool%create_pw(rhox_tot_gspace)
442 IF (nspins == 2)
CALL dbcsr_scale(matrix_px1(ispin)%matrix, 2.0_dp)
444 rho=rhox_r(ispin), rho_gspace=rhox_g(ispin), &
446 IF (needs_tau_response)
THEN
448 rho=rhox_tau_r(ispin), rho_gspace=rhox_tau_g(ispin), &
449 soft_valid=gapw, compute_tau=.true.)
452 CALL pw_axpy(rhox_g(ispin), rhox_tot_gspace)
454 IF (nspins == 2)
CALL dbcsr_scale(matrix_px1(ispin)%matrix, 0.5_dp)
458 ALLOCATE (rhoxx_r(nsev), rhoxx_g(nsev))
460 CALL auxbas_pw_pool%create_pw(rhoxx_r(ispin))
461 CALL auxbas_pw_pool%create_pw(rhoxx_g(ispin))
463 IF (needs_tau_response)
THEN
464 ALLOCATE (rhoxx_tau_r(nsev), rhoxx_tau_g(nsev))
466 CALL auxbas_pw_pool%create_pw(rhoxx_tau_r(ispin))
467 CALL auxbas_pw_pool%create_pw(rhoxx_tau_g(ispin))
471 IF (nspins == 2)
CALL dbcsr_scale(matrix_px1(ispin)%matrix, 2.0_dp)
473 rho=rhoxx_r(ispin), rho_gspace=rhoxx_g(ispin), &
475 IF (needs_tau_response)
THEN
477 rho=rhoxx_tau_r(ispin), rho_gspace=rhoxx_tau_g(ispin), &
478 soft_valid=gapw_xc, compute_tau=.true.)
480 IF (nspins == 2)
CALL dbcsr_scale(matrix_px1(ispin)%matrix, 0.5_dp)
484 CALL get_qs_env(qs_env, matrix_s=matrix_s, force=force)
486 IF (.NOT. (is_rks_triplets .OR. do_sf))
THEN
487 CALL auxbas_pw_pool%create_pw(xv_hartree_rspace)
488 CALL auxbas_pw_pool%create_pw(xv_hartree_gspace)
491 CALL pw_axpy(local_rho_set%rho0_mpole%rho0_s_gs, rhox_tot_gspace)
492 IF (
ASSOCIATED(local_rho_set%rho0_mpole%rhoz_cneo_s_gs))
THEN
493 CALL pw_axpy(local_rho_set%rho0_mpole%rhoz_cneo_s_gs, rhox_tot_gspace)
498 CALL pw_transfer(xv_hartree_gspace, xv_hartree_rspace)
499 CALL pw_scale(xv_hartree_rspace, xv_hartree_rspace%pw_grid%dvol)
501 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
505 ALLOCATE (matrix_hx(ispin)%matrix)
506 CALL dbcsr_create(matrix_hx(ispin)%matrix, template=matrix_s(1)%matrix)
507 CALL dbcsr_copy(matrix_hx(ispin)%matrix, matrix_s(1)%matrix)
508 CALL dbcsr_set(matrix_hx(ispin)%matrix, 0.0_dp)
509 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=xv_hartree_rspace, &
510 pmat=matrix_px1(ispin), hmat=matrix_hx(ispin), &
511 gapw=gapw, calculate_forces=.true.)
513 IF (debug_forces)
THEN
514 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
515 CALL para_env%sum(fodeb)
516 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dKh[Dx] ", fodeb
519 IF (debug_forces) fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1)
520 CALL vh_1c_gg_integrals(qs_env, eh1c, hartree_local%ecoul_1c, local_rho_set, para_env, tddft=.true., &
522 IF (nspins == 1)
THEN
528 local_rho_set=local_rho_set, kforce=kval)
529 IF (debug_forces)
THEN
530 fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1) - fodeb(1:3)
531 CALL para_env%sum(fodeb)
532 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dKh[Dx]PAWg0", fodeb
534 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
535 CALL update_ks_atom(qs_env, matrix_hx, matrix_px1, forces=.true., &
536 rho_atom_external=local_rho_set%rho_atom_set)
537 IF (debug_forces)
THEN
538 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
539 CALL para_env%sum(fodeb)
540 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dKh[Dx]PAW", fodeb
546 IF (full_kernel%do_exck)
THEN
549 NULLIFY (fxc_rho, fxc_tau, gxc_rho, gxc_tau)
550 xc_section => full_kernel%xc_section
561 CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, rho_xc=rho)
563 CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, rho=rho)
572 IF (needs_tau_response)
THEN
573 CALL qs_rho_set(rho_struct=rhox, rho_ao=matrix_px1, rho_r=rhoxx_r, rho_g=rhoxx_g, &
574 tau_r=rhoxx_tau_r, tau_g=rhoxx_tau_g, &
575 rho_r_valid=.true., rho_g_valid=.true., &
576 tau_r_valid=.true., tau_g_valid=.true.)
578 CALL qs_rho_set(rho_struct=rhox, rho_ao=matrix_px1, rho_r=rhoxx_r, rho_g=rhoxx_g, &
579 rho_r_valid=.true., rho_g_valid=.true.)
582 IF (needs_tau_response)
THEN
583 CALL qs_rho_set(rho_struct=rhox, rho_ao=matrix_px1, rho_r=rhox_r, rho_g=rhox_g, &
584 tau_r=rhox_tau_r, tau_g=rhox_tau_g, &
585 rho_r_valid=.true., rho_g_valid=.true., &
586 tau_r_valid=.true., tau_g_valid=.true.)
588 CALL qs_rho_set(rho_struct=rhox, rho_ao=matrix_px1, rho_r=rhox_r, rho_g=rhox_g, &
589 rho_r_valid=.true., rho_g_valid=.true.)
594 IF (gapw .OR. gapw_xc)
THEN
595 do_onecenter = .true.
596 rho_atom_set => ex_env%local_rho_set%rho_atom_set
597 rho_atom_set_f => local_rho_set_f%rho_atom_set
598 rho_atom_set_g => local_rho_set_g%rho_atom_set
599 CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set)
601 do_onecenter = .false.
602 NULLIFY (rho_atom_set, rho_atom_set_f, rho_atom_set_g, qs_kind_set)
605 xc_section, do_onecenter, is_rks_triplets, do_sf, &
606 fxc_rho, fxc_tau, gxc_rho, gxc_tau, &
607 rho_atom_set_f, rho_atom_set_g, qs_kind_set)
609 IF (nspins == 2)
THEN
611 CALL pw_scale(gxc_rho(ispin), 0.5_dp)
612 IF (
ASSOCIATED(gxc_tau))
CALL pw_scale(gxc_tau(ispin), 0.5_dp)
616 IF (
ASSOCIATED(rho_nlcc))
THEN
618 CALL integrate_rho_nlcc(gxc_rho(ispin), qs_env, &
619 debug_forces, .false.,
"dnlcc*dFxc")
623 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
626 DO ispin = 1,
SIZE(fxc_rho, 1)
627 ALLOCATE (matrix_fx(ispin)%matrix)
628 CALL dbcsr_create(matrix_fx(ispin)%matrix, template=matrix_s(1)%matrix)
629 CALL dbcsr_copy(matrix_fx(ispin)%matrix, matrix_s(1)%matrix)
630 CALL dbcsr_set(matrix_fx(ispin)%matrix, 0.0_dp)
631 CALL pw_scale(fxc_rho(ispin), fxc_rho(ispin)%pw_grid%dvol)
634 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=fxc_rho(ispin), &
635 pmat=matrix_px1(ispin), hmat=matrix_fx(ispin), &
636 gapw=(gapw .OR. gapw_xc), calculate_forces=.true.)
637 IF (
ASSOCIATED(fxc_tau))
THEN
639 CALL pw_scale(fxc_tau(ispin), fxc_tau(ispin)%pw_grid%dvol)
640 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=fxc_tau(ispin), &
641 pmat=matrix_px1(ispin), hmat=matrix_fx(ispin), &
642 gapw=(gapw .OR. gapw_xc), calculate_forces=.true., compute_tau=.true.)
645 IF (debug_forces)
THEN
646 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
647 CALL para_env%sum(fodeb)
648 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dfxc[Dx] ", fodeb
651 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
657 ALLOCATE (matrix_gx(ispin)%matrix)
658 CALL dbcsr_create(matrix_gx(ispin)%matrix, template=matrix_s(1)%matrix)
659 CALL dbcsr_copy(matrix_gx(ispin)%matrix, matrix_s(1)%matrix)
660 CALL dbcsr_set(matrix_gx(ispin)%matrix, 0.0_dp)
661 CALL pw_scale(gxc_rho(ispin), 0.5_dp*gxc_rho(ispin)%pw_grid%dvol)
662 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=gxc_rho(ispin), &
663 pmat=matrix_p(ispin), hmat=matrix_gx(ispin), &
664 gapw=(gapw .OR. gapw_xc), calculate_forces=.true.)
665 IF (
ASSOCIATED(gxc_tau))
THEN
666 CALL pw_scale(gxc_tau(ispin), 0.5_dp*gxc_tau(ispin)%pw_grid%dvol)
667 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=gxc_tau(ispin), &
668 pmat=matrix_p(ispin), hmat=matrix_gx(ispin), &
669 gapw=(gapw .OR. gapw_xc), calculate_forces=.true., compute_tau=.true.)
673 IF (debug_forces)
THEN
674 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
675 CALL para_env%sum(fodeb)
676 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dgxc[Dx]", fodeb
678 CALL qs_fgxc_release(fxc_rho, fxc_tau, gxc_rho, gxc_tau, auxbas_pw_pool)
681 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
683 IF (debug_forces)
THEN
684 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
685 CALL para_env%sum(fodeb)
686 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*fxc[Dx]*dw", fodeb
695 IF (gapw .OR. gapw_xc)
THEN
696 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
697 CALL update_ks_atom(qs_env, matrix_fx, matrix_px1, forces=.true., tddft=.true., &
698 rho_atom_external=local_rho_set_f%rho_atom_set, &
699 kintegral=1.0_dp, kforce=1.0_dp)
700 IF (debug_forces)
THEN
701 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
702 CALL para_env%sum(fodeb)
703 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dfxc[Dx]PAW ", fodeb
705 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
706 IF (nspins == 1)
THEN
707 CALL update_ks_atom(qs_env, matrix_gx, matrix_p, forces=.true., tddft=.true., &
708 rho_atom_external=local_rho_set_g%rho_atom_set, &
712 rho_atom_external=local_rho_set_g%rho_atom_set, &
713 kintegral=0.5_dp, kforce=0.25_dp)
715 IF (debug_forces)
THEN
716 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
717 CALL para_env%sum(fodeb)
718 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dgxc[Dx]PAW ", fodeb
724 IF (do_admm .AND. tddfpt_control%admm_xc_correction .AND. (tddfpt_control%spinflip /=
tddfpt_sf_col))
THEN
728 IF (.NOT. tddfpt_control%admm_symm)
THEN
729 CALL cp_warn(__location__,
"Forces need symmetric ADMM kernel corrections")
730 cpabort(
"ADMM KERNEL CORRECTION")
732 xc_section => admm_env%xc_section_aux
735 needs_tau_response_aux = needs%tau .OR. needs%tau_spin
736 CALL get_admm_env(admm_env, rho_aux_fit=rho_aux_fit, matrix_s_aux_fit=matrix_s_aux_fit, &
737 task_list_aux_fit=task_list)
738 basis_type =
"AUX_FIT"
739 IF (admm_env%do_gapw)
THEN
740 basis_type =
"AUX_FIT_SOFT"
741 task_list => admm_env%admm_gapw_env%task_list
748 ALLOCATE (mfx(ispin)%matrix)
749 CALL dbcsr_create(mfx(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
750 CALL dbcsr_copy(mfx(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
751 CALL dbcsr_set(mfx(ispin)%matrix, 0.0_dp)
754 ALLOCATE (mgx(ispin)%matrix)
755 CALL dbcsr_create(mgx(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
756 CALL dbcsr_copy(mgx(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
757 CALL dbcsr_set(mgx(ispin)%matrix, 0.0_dp)
761 NULLIFY (rho_g_aux, rho_r_aux, rhox_g_aux, rhox_r_aux, rhox_tau_g_aux, rhox_tau_r_aux)
762 CALL qs_rho_get(rho_aux_fit, rho_r=rho_r_aux, rho_g=rho_g_aux)
763 CALL qs_rho_get(rho_aux_fit, rho_ao=matrix_p_admm)
765 ALLOCATE (rhox_r_aux(nsev), rhox_g_aux(nsev))
767 CALL auxbas_pw_pool%create_pw(rhox_r_aux(ispin))
768 CALL auxbas_pw_pool%create_pw(rhox_g_aux(ispin))
770 IF (needs_tau_response_aux)
THEN
771 ALLOCATE (rhox_tau_r_aux(nsev), rhox_tau_g_aux(nsev))
773 CALL auxbas_pw_pool%create_pw(rhox_tau_r_aux(ispin))
774 CALL auxbas_pw_pool%create_pw(rhox_tau_g_aux(ispin))
779 rho=rhox_r_aux(ispin), rho_gspace=rhox_g_aux(ispin), &
780 basis_type=basis_type, &
781 task_list_external=task_list)
782 IF (needs_tau_response_aux)
THEN
784 rho=rhox_tau_r_aux(ispin), &
785 rho_gspace=rhox_tau_g_aux(ispin), &
786 basis_type=basis_type, task_list_external=task_list, &
794 IF (needs_tau_response_aux)
THEN
795 CALL qs_rho_set(rho_struct=rhox_aux, rho_ao=matrix_px1_admm, &
796 rho_r=rhox_r_aux, rho_g=rhox_g_aux, &
797 tau_r=rhox_tau_r_aux, tau_g=rhox_tau_g_aux, &
798 rho_r_valid=.true., rho_g_valid=.true., &
799 tau_r_valid=.true., tau_g_valid=.true.)
801 CALL qs_rho_set(rho_struct=rhox_aux, rho_ao=matrix_px1_admm, &
802 rho_r=rhox_r_aux, rho_g=rhox_g_aux, &
803 rho_r_valid=.true., rho_g_valid=.true.)
806 IF (admm_env%do_gapw)
THEN
807 do_onecenter = .true.
808 rho_atom_set => admm_env%admm_gapw_env%local_rho_set%rho_atom_set
809 rho_atom_set_f => local_rho_set_f_admm%rho_atom_set
810 rho_atom_set_g => local_rho_set_g_admm%rho_atom_set
811 qs_kind_set => admm_env%admm_gapw_env%admm_kind_set
813 do_onecenter = .false.
814 NULLIFY (rho_atom_set, rho_atom_set_f, rho_atom_set_g, qs_kind_set)
816 CALL qs_fgxc_create(qs_env, rho_aux_fit, rhox_aux, rho_atom_set, &
817 xc_section, do_onecenter, is_rks_triplets, do_sf, &
818 fxc_rho, fxc_tau, gxc_rho, gxc_tau, &
819 rho_atom_set_f, rho_atom_set_g, qs_kind_set)
821 IF (
ASSOCIATED(rho_nlcc))
THEN
823 CALL integrate_rho_nlcc(gxc_rho(ispin), qs_env, &
824 debug_forces, .false.,
"dnlcc*dFxc(ADMM)")
829 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
833 IF (debug_forces)
THEN
834 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
835 CALL para_env%sum(fodeb)
836 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx_admm*fxc[Dx_admm]*dw", fodeb
843 DEALLOCATE (rhox_aux)
846 CALL auxbas_pw_pool%give_back_pw(rhox_r_aux(ispin))
847 CALL auxbas_pw_pool%give_back_pw(rhox_g_aux(ispin))
849 DEALLOCATE (rhox_r_aux, rhox_g_aux)
850 IF (needs_tau_response_aux)
THEN
852 CALL auxbas_pw_pool%give_back_pw(rhox_tau_r_aux(ispin))
853 CALL auxbas_pw_pool%give_back_pw(rhox_tau_g_aux(ispin))
855 DEALLOCATE (rhox_tau_r_aux, rhox_tau_g_aux)
858 IF (nspins == 2) fscal = 2.0_dp
860 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
862 CALL pw_scale(fxc_rho(ispin), fxc_rho(ispin)%pw_grid%dvol)
863 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=fxc_rho(ispin), &
865 pmat=matrix_px1_admm(ispin), &
866 basis_type=basis_type, &
867 calculate_forces=.true., &
869 task_list_external=task_list)
871 IF (debug_forces)
THEN
872 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
873 CALL para_env%sum(fodeb)
874 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dfxc[Dx]ADMM", fodeb
877 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
879 CALL pw_scale(gxc_rho(ispin), gxc_rho(ispin)%pw_grid%dvol)
880 CALL pw_scale(gxc_rho(ispin), 0.5_dp)
881 CALL integrate_v_rspace(qs_env=qs_env, v_rspace=gxc_rho(ispin), &
883 pmat=matrix_p_admm(ispin), &
884 basis_type=basis_type, &
885 calculate_forces=.true., &
887 task_list_external=task_list)
890 IF (debug_forces)
THEN
891 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
892 CALL para_env%sum(fodeb)
893 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dgxc[Dx]ADMM", fodeb
895 CALL qs_fgxc_release(fxc_rho, fxc_tau, gxc_rho, gxc_tau, auxbas_pw_pool)
897 IF (admm_env%do_gapw)
THEN
899 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
900 IF (nspins == 1)
THEN
901 CALL update_ks_atom(qs_env, mfx, matrix_px1_admm, forces=.true., &
902 rho_atom_external=rho_atom_set_f, &
903 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
904 oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
905 kintegral=2.0_dp, kforce=0.5_dp)
907 CALL update_ks_atom(qs_env, mfx, matrix_px1_admm, forces=.true., &
908 rho_atom_external=rho_atom_set_f, &
909 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
910 oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
911 kintegral=2.0_dp, kforce=1.0_dp)
913 IF (debug_forces)
THEN
914 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
915 CALL para_env%sum(fodeb)
916 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dfxc[Dx]ADMM-PAW ", fodeb
918 IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
919 IF (nspins == 1)
THEN
921 rho_atom_external=rho_atom_set_g, &
922 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
923 oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
924 kintegral=1.0_dp, kforce=0.5_dp)
927 rho_atom_external=rho_atom_set_g, &
928 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
929 oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
930 kintegral=1.0_dp, kforce=1.0_dp)
932 IF (debug_forces)
THEN
933 fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
934 CALL para_env%sum(fodeb)
935 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dgxc[Dx]ADMM-PAW ", fodeb
941 IF (debug_forces) fodeb(1:3) = force(1)%overlap_admm(1:3, 1)
942 fval = 2.0_dp*real(nspins, kind=
dp)
944 IF (debug_forces)
THEN
945 fodeb(1:3) = force(1)%overlap_admm(1:3, 1) - fodeb(1:3)
946 CALL para_env%sum(fodeb)
947 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: P*dfXC(P)*S' ", fodeb
949 IF (debug_forces) fodeb(1:3) = force(1)%overlap_admm(1:3, 1)
950 fval = 2.0_dp*real(nspins, kind=
dp)
952 IF (debug_forces)
THEN
953 fodeb(1:3) = force(1)%overlap_admm(1:3, 1) - fodeb(1:3)
954 CALL para_env%sum(fodeb)
955 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: P*dgXC(P)*S' ", fodeb
960 IF (nspins == 2) fscal = 2.0_dp
961 nao = admm_env%nao_orb
962 nao_aux = admm_env%nao_aux_fit
968 admm_env%work_aux_orb, nao)
970 1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
971 admm_env%work_orb_orb)
975 CALL dbcsr_add(matrix_fx(ispin)%matrix, dbwork, 1.0_dp, fscal)
978 admm_env%work_aux_orb, nao)
980 1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
981 admm_env%work_orb_orb)
984 CALL dbcsr_add(matrix_gx(ispin)%matrix, dbwork, 1.0_dp, fscal)
995 CALL auxbas_pw_pool%give_back_pw(rhox_r(ispin))
996 CALL auxbas_pw_pool%give_back_pw(rhox_g(ispin))
998 DEALLOCATE (rhox_r, rhox_g)
999 IF (needs_tau_response)
THEN
1001 CALL auxbas_pw_pool%give_back_pw(rhox_tau_r(ispin))
1002 CALL auxbas_pw_pool%give_back_pw(rhox_tau_g(ispin))
1004 DEALLOCATE (rhox_tau_r, rhox_tau_g)
1006 CALL auxbas_pw_pool%give_back_pw(rhox_tot_gspace)
1009 CALL auxbas_pw_pool%give_back_pw(rhoxx_r(ispin))
1010 CALL auxbas_pw_pool%give_back_pw(rhoxx_g(ispin))
1012 DEALLOCATE (rhoxx_r, rhoxx_g)
1013 IF (needs_tau_response)
THEN
1015 CALL auxbas_pw_pool%give_back_pw(rhoxx_tau_r(ispin))
1016 CALL auxbas_pw_pool%give_back_pw(rhoxx_tau_g(ispin))
1018 DEALLOCATE (rhoxx_tau_r, rhoxx_tau_g)
1021 IF (.NOT. (is_rks_triplets .OR. do_sf))
THEN
1022 CALL auxbas_pw_pool%give_back_pw(xv_hartree_rspace)
1023 CALL auxbas_pw_pool%give_back_pw(xv_hartree_gspace)
1028 NULLIFY (matrix_hfx, matrix_hfx_asymm)
1032 ALLOCATE (matrix_hfx(ispin)%matrix)
1033 CALL dbcsr_create(matrix_hfx(ispin)%matrix, template=matrix_s(1)%matrix)
1034 CALL dbcsr_copy(matrix_hfx(ispin)%matrix, matrix_s(1)%matrix)
1035 CALL dbcsr_set(matrix_hfx(ispin)%matrix, 0.0_dp)
1037 ALLOCATE (matrix_hfx_asymm(ispin)%matrix)
1038 CALL dbcsr_create(matrix_hfx_asymm(ispin)%matrix, template=matrix_s(1)%matrix, &
1039 matrix_type=dbcsr_type_antisymmetric)
1043 xc_section => full_kernel%xc_section
1046 cpassert(n_rep_hf == 1)
1050 IF (hfx_treat_lsd_in_core) mspin = nsev
1052 CALL get_qs_env(qs_env=qs_env, x_data=x_data, s_mstruct_changed=s_mstruct_changed)
1053 distribute_fock_matrix = .true.
1056 CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux_fit)
1057 NULLIFY (matrix_hfx_admm, matrix_hfx_admm_asymm)
1061 ALLOCATE (matrix_hfx_admm(ispin)%matrix)
1062 CALL dbcsr_create(matrix_hfx_admm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
1063 CALL dbcsr_copy(matrix_hfx_admm(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
1064 CALL dbcsr_set(matrix_hfx_admm(ispin)%matrix, 0.0_dp)
1066 ALLOCATE (matrix_hfx_admm_asymm(ispin)%matrix)
1067 CALL dbcsr_create(matrix_hfx_admm_asymm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix, &
1068 matrix_type=dbcsr_type_antisymmetric)
1073 ALLOCATE (mpe(nsev, 1), mhe(nsev, 1))
1075 mhe(ispin, 1)%matrix => matrix_hfx_admm(ispin)%matrix
1076 mpe(ispin, 1)%matrix => matrix_px1_admm(ispin)%matrix
1078 IF (x_data(1, 1)%do_hfx_ri)
THEN
1080 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhe, eh1, rho_ao=mpe, &
1081 geometry_did_change=s_mstruct_changed, nspins=nspins, &
1082 hf_fraction=x_data(1, 1)%general_parameter%fraction)
1087 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
1088 ispin=ispin, nspins=nsev)
1093 mhe(ispin, 1)%matrix => matrix_hfx_admm_asymm(ispin)%matrix
1094 mpe(ispin, 1)%matrix => matrix_px1_admm_asymm(ispin)%matrix
1096 IF (x_data(1, 1)%do_hfx_ri)
THEN
1098 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhe, eh1, rho_ao=mpe, &
1099 geometry_did_change=s_mstruct_changed, nspins=nspins, &
1100 hf_fraction=x_data(1, 1)%general_parameter%fraction)
1105 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
1106 ispin=ispin, nspins=nsev)
1110 nao = admm_env%nao_orb
1111 nao_aux = admm_env%nao_aux_fit
1112 ALLOCATE (dbwork, dbwork_asymm)
1113 CALL dbcsr_create(dbwork, template=matrix_hfx(1)%matrix)
1114 CALL dbcsr_create(dbwork_asymm, template=matrix_hfx(1)%matrix, matrix_type=dbcsr_type_antisymmetric)
1117 admm_env%work_aux_orb, nao)
1119 1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
1120 admm_env%work_orb_orb)
1121 CALL dbcsr_copy(dbwork, matrix_hfx(1)%matrix)
1124 CALL dbcsr_add(matrix_hfx(ispin)%matrix, dbwork, 1.0_dp, 1.0_dp)
1127 admm_env%work_aux_orb, nao)
1129 1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
1130 admm_env%work_orb_orb)
1131 CALL dbcsr_copy(dbwork_asymm, matrix_hfx_asymm(1)%matrix)
1133 CALL copy_fm_to_dbcsr(admm_env%work_orb_orb, dbwork_asymm, keep_sparsity=.true.)
1134 CALL dbcsr_add(matrix_hfx_asymm(ispin)%matrix, dbwork_asymm, 1.0_dp, 1.0_dp)
1138 DEALLOCATE (dbwork, dbwork_asymm)
1141 IF (debug_forces) fodeb(1:3) = force(1)%overlap_admm(1:3, 1)
1142 fval = 4.0_dp*real(nspins, kind=
dp)*0.5_dp
1145 IF (debug_forces)
THEN
1146 fodeb(1:3) = force(1)%overlap_admm(1:3, 1) - fodeb(1:3)
1147 CALL para_env%sum(fodeb)
1148 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: P*Hx(P)*S' ", fodeb
1151 use_virial = .false.
1153 fval = 2.0_dp*real(nspins, kind=
dp)*0.5_dp
1155 IF (do_sf) fval = fval*2.0_dp
1156 IF (debug_forces) fodeb(1:3) = force(1)%fock_4c(1:3, 1)
1158 mpe(ispin, 1)%matrix => matrix_px1_admm(ispin)%matrix
1160 IF (x_data(1, 1)%do_hfx_ri)
THEN
1162 x_data(1, 1)%general_parameter%fraction, &
1163 rho_ao=mpe, rho_ao_resp=mdum, &
1164 use_virial=use_virial, rescale_factor=fval)
1167 adiabatic_rescale_factor=fval, nspins=nsev)
1170 mpe(ispin, 1)%matrix => matrix_px1_admm_asymm(ispin)%matrix
1172 IF (x_data(1, 1)%do_hfx_ri)
THEN
1174 x_data(1, 1)%general_parameter%fraction, &
1175 rho_ao=mpe, rho_ao_resp=mdum, &
1176 use_virial=use_virial, rescale_factor=fval)
1179 adiabatic_rescale_factor=fval, nspins=
SIZE(mpe, 1))
1181 IF (debug_forces)
THEN
1182 fodeb(1:3) = force(1)%fock_4c(1:3, 1) - fodeb(1:3)
1183 CALL para_env%sum(fodeb)
1184 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dhfx'*Dx ", fodeb
1187 DEALLOCATE (mpe, mhe)
1193 ALLOCATE (mpe(nsev, 1), mhe(nsev, 1))
1195 mhe(ispin, 1)%matrix => matrix_hfx(ispin)%matrix
1196 mpe(ispin, 1)%matrix => matrix_px1(ispin)%matrix
1198 IF (x_data(1, 1)%do_hfx_ri)
THEN
1200 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhe, eh1, rho_ao=mpe, &
1201 geometry_did_change=s_mstruct_changed, nspins=nspins, &
1202 hf_fraction=x_data(1, 1)%general_parameter%fraction)
1207 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
1208 ispin=ispin, nspins=
SIZE(mpe, 1))
1214 mhe(ispin, 1)%matrix => matrix_hfx_asymm(ispin)%matrix
1215 mpe(ispin, 1)%matrix => matrix_px1_asymm(ispin)%matrix
1217 IF (x_data(1, 1)%do_hfx_ri)
THEN
1219 CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhe, eh1, rho_ao=mpe, &
1220 geometry_did_change=s_mstruct_changed, nspins=nspins, &
1221 hf_fraction=x_data(1, 1)%general_parameter%fraction)
1226 para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
1227 ispin=ispin, nspins=
SIZE(mpe, 1))
1231 use_virial = .false.
1233 fval = 2.0_dp*real(nspins, kind=
dp)*0.5_dp
1235 IF (do_sf) fval = fval*2.0_dp
1236 IF (debug_forces) fodeb(1:3) = force(1)%fock_4c(1:3, 1)
1238 mpe(ispin, 1)%matrix => matrix_px1(ispin)%matrix
1240 IF (x_data(1, 1)%do_hfx_ri)
THEN
1242 x_data(1, 1)%general_parameter%fraction, &
1243 rho_ao=mpe, rho_ao_resp=mdum, &
1244 use_virial=use_virial, rescale_factor=fval)
1247 adiabatic_rescale_factor=fval, nspins=
SIZE(mpe, 1))
1250 mpe(ispin, 1)%matrix => matrix_px1_asymm(ispin)%matrix
1252 IF (x_data(1, 1)%do_hfx_ri)
THEN
1254 x_data(1, 1)%general_parameter%fraction, &
1255 rho_ao=mpe, rho_ao_resp=mdum, &
1256 use_virial=use_virial, rescale_factor=fval)
1259 adiabatic_rescale_factor=fval, nspins=
SIZE(mpe, 1))
1261 IF (debug_forces)
THEN
1262 fodeb(1:3) = force(1)%fock_4c(1:3, 1) - fodeb(1:3)
1263 CALL para_env%sum(fodeb)
1264 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Dx*dhfx*Dx ", fodeb
1267 DEALLOCATE (mpe, mhe)
1269 fval = 2.0_dp*real(nspins, kind=
dp)*0.5_dp
1271 IF (do_sf) fval = fval*2.0_dp
1274 CALL dbcsr_scale(matrix_hfx_asymm(ispin)%matrix, fval)
1278 IF (gapw .OR. gapw_xc)
THEN
1287 IF (admm_env%do_gapw)
THEN
1288 IF (tddfpt_control%admm_xc_correction)
THEN
1300 cpabort(
"HFXSR not implemented")
1304 cpabort(
"HFXLR not implemented")
1308 NULLIFY (matrix_wx1)
1310 cpmos => ex_env%cpmos
1312 IF (nspins == 2) focc = 1.0_dp
1317 mos => gs_mos(1)%mos_occ
1318 mosa => gs_mos(1)%mos_active
1319 norb(1) = gs_mos(1)%nmo_occ
1320 nactive(1) = gs_mos(1)%nmo_active
1321 IF (nspins == 2)
THEN
1322 mos2 => gs_mos(2)%mos_occ
1323 mosa2 => gs_mos(2)%mos_active
1324 norb(2) = gs_mos(2)%nmo_occ
1325 nactive(2) = gs_mos(2)%nmo_active
1328 DO ispin = 1, nspins
1330 IF (nactive(ispin) == norb(ispin))
THEN
1332 DO ia = 1, nactive(ispin)
1333 cpassert(ia == gs_mos(ispin)%index_active(ia))
1340 IF (.NOT. do_sf)
THEN
1342 mos => gs_mos(ispin)%mos_occ
1343 mos2 => gs_mos(ispin)%mos_occ
1344 mosa => gs_mos(ispin)%mos_active
1345 mosa2 => gs_mos(ispin)%mos_active
1349 CALL cp_fm_create(cpscr, gs_mos(ispin)%mos_active%matrix_struct,
"cpscr", set_zero=.true.)
1351 CALL cp_fm_get_info(gs_mos(ispin)%mos_occ, matrix_struct=fm_struct, nrow_global=nao)
1353 CALL cp_fm_create(avamat, fm_struct, nrow=nactive(spin), ncol=nactive(spin))
1354 CALL cp_fm_create(avcmat, fm_struct, nrow=nactive(spin), ncol=norb(spin))
1355 CALL cp_fm_create(cvcmat, fm_struct, nrow=norb(spin), ncol=norb(spin))
1357 CALL cp_fm_create(vcvec, gs_mos(ispin)%mos_occ%matrix_struct,
"vcvec")
1358 CALL cp_fm_create(vavec, gs_mos(ispin)%mos_active%matrix_struct,
"vavec")
1361 ALLOCATE (matrix_wx1(ispin)%matrix)
1362 CALL dbcsr_create(matrix=matrix_wx1(ispin)%matrix, template=matrix_s(1)%matrix)
1364 CALL dbcsr_set(matrix_wx1(ispin)%matrix, 0.0_dp)
1367 IF (.NOT. (is_rks_triplets .OR. do_sf))
THEN
1369 cpscr, nactive(ispin), alpha=focc, beta=1.0_dp)
1371 alpha=1.0_dp, beta=0.0_dp)
1372 CALL parallel_gemm(
"T",
"N", nactive(ispin), norb(ispin), nao, 1.0_dp, &
1373 mosa, vcvec, 0.0_dp, avcmat)
1374 CALL parallel_gemm(
"N",
"N", nao, norb(ispin), nactive(ispin), 1.0_dp, &
1375 evect(ispin), avcmat, 0.0_dp, vcvec)
1379 ncol=norb(ispin), alpha=2.0_dp, symmetry_mode=1)
1386 IF (.NOT. (do_sf .AND. (ispin == 2)))
THEN
1389 cpscr, nactive(ispin), alpha=focc, beta=1.0_dp)
1392 IF (.NOT. (do_sf .AND. (ispin == 1)))
THEN
1395 norb(ispin), alpha=1.0_dp, beta=0.0_dp)
1397 CALL parallel_gemm(
"T",
"N", nactive(spin), norb(ispin), nao, 1.0_dp, &
1398 mosa, vcvec, 0.0_dp, avcmat)
1400 CALL parallel_gemm(
"N",
"N", nao, norb(ispin), nactive(spin), 1.0_dp, &
1401 evect(spin), avcmat, 0.0_dp, vcvec)
1404 norb(ispin), alpha=-focc, beta=1.0_dp)
1407 CALL cp_dbcsr_plus_fm_fm_t(matrix_wx1(ispin)%matrix, matrix_v=vcvec, matrix_g=gs_mos(ispin)%mos_occ, &
1408 ncol=norb(ispin), alpha=2.0_dp, symmetry_mode=1)
1415 cpmos(ispin), norb(ispin), alpha=focc, beta=1.0_dp)
1419 alpha=1.0_dp, beta=0.0_dp)
1421 CALL parallel_gemm(
"T",
"N", norb(ispin), norb(ispin), nao, 1.0_dp, gs_mos(ispin)%mos_occ, vcvec, 0.0_dp, cvcmat)
1423 CALL parallel_gemm(
"N",
"N", nao, norb(ispin), norb(ispin), 1.0_dp, gs_mos(ispin)%mos_occ, cvcmat, 0.0_dp, vcvec)
1425 CALL cp_dbcsr_plus_fm_fm_t(matrix_wx1(ispin)%matrix, matrix_v=vcvec, matrix_g=gs_mos(ispin)%mos_occ, &
1426 ncol=norb(ispin), alpha=1.0_dp, symmetry_mode=1)
1432 IF (.NOT. (do_sf .AND. (ispin == 2)))
THEN
1435 cpscr, nactive(spin), alpha=focc, beta=1.0_dp)
1438 cpscr, nactive(spin), alpha=focc, beta=1.0_dp)
1442 IF (.NOT. (do_sf .AND. (ispin == 1)))
THEN
1445 alpha=1.0_dp, beta=0.0_dp)
1448 alpha=1.0_dp, beta=1.0_dp)
1450 CALL parallel_gemm(
"T",
"N", nactive(spin), norb(ispin), nao, 1.0_dp, &
1451 mosa, vcvec, 0.0_dp, avcmat)
1453 CALL parallel_gemm(
"N",
"N", nao, norb(ispin), nactive(spin), 1.0_dp, &
1454 evect(spin), avcmat, 0.0_dp, vcvec)
1457 norb(ispin), alpha=-focc, beta=1.0_dp)
1461 ncol=norb(ispin), alpha=2.0_dp, symmetry_mode=1)
1466 DO ia = 1, nactive(ispin)
1467 ib = gs_mos(ispin)%index_active(ia)
1471 CALL cp_fm_geadd(1.0_dp,
"N", cpscr, 1.0_dp, cpmos(ispin))
1482 IF (.NOT. (is_rks_triplets .OR. do_sf))
THEN
1486 ex_env%matrix_wx1 => matrix_wx1
1496 CALL timestop(handle)
1510 SUBROUTINE stda_force(qs_env, ex_env, gs_mos, stda_env, sub_env, work, debug_forces)
1519 LOGICAL,
INTENT(IN) :: debug_forces
1521 CHARACTER(len=*),
PARAMETER :: routinen =
'stda_force'
1523 INTEGER :: atom_i, atom_j, ewald_type, handle, i, &
1524 ia, iatom, idimk, ikind, iounit, is, &
1525 ispin, jatom, jkind, jspin, nao, &
1526 natom, norb, nsgf, nspins
1527 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_of_kind, first_sgf, kind_of, &
1529 INTEGER,
DIMENSION(2) :: nactive, nlim
1530 LOGICAL :: calculate_forces, do_coulomb, do_ewald, &
1531 found, is_rks_triplets, use_virial
1532 REAL(kind=
dp) :: alpha, bp, dgabr, dr, eta, fdim, gabr, &
1533 hfx, rbeta, spinfac, xfac
1534 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: tcharge, tv
1535 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: gtcharge
1536 REAL(kind=
dp),
DIMENSION(3) :: fij, focoul, fodeb, foexch, rij
1537 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gab, pblock
1541 TYPE(
cp_fm_type) :: cvcmat, cvec, cvecjspin, t0matrix, &
1542 t1matrix, vcvec, xvec
1543 TYPE(
cp_fm_type),
ALLOCATABLE,
DIMENSION(:) :: xtransformed
1544 TYPE(
cp_fm_type),
DIMENSION(:),
POINTER :: cpmos, x
1545 TYPE(
cp_fm_type),
POINTER :: ct, ctjspin, ucmatrix, uxmatrix
1548 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: gamma_matrix, matrix_plo, matrix_s, &
1557 POINTER :: n_list, sab_orb
1560 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
1566 CALL timeset(routinen, handle)
1568 cpassert(
ASSOCIATED(ex_env))
1569 cpassert(
ASSOCIATED(gs_mos))
1572 IF (logger%para_env%is_source())
THEN
1578 CALL get_qs_env(qs_env, dft_control=dft_control)
1579 tddfpt_control => dft_control%tddfpt2_control
1580 stda_control => tddfpt_control%stda_control
1581 nspins = dft_control%nspins
1582 is_rks_triplets = tddfpt_control%rks_triplets .AND. (nspins == 1)
1586 nactive(:) = stda_env%nactive(:)
1589 IF (nspins == 2) spinfac = 1.0_dp
1590 NULLIFY (matrix_wx1)
1592 NULLIFY (matrix_plo)
1595 IF (nspins == 1 .AND. is_rks_triplets)
THEN
1596 do_coulomb = .false.
1600 do_ewald = stda_control%do_ewald
1602 CALL get_qs_env(qs_env, para_env=para_env, force=force)
1604 CALL get_qs_env(qs_env, natom=natom, cell=cell, &
1605 particle_set=particle_set, qs_kind_set=qs_kind_set)
1606 ALLOCATE (first_sgf(natom))
1607 ALLOCATE (last_sgf(natom))
1608 CALL get_particle_set(particle_set, qs_kind_set, first_sgf=first_sgf, last_sgf=last_sgf)
1610 CALL get_qs_env(qs_env, ks_env=ks_env, matrix_s=matrix_s, sab_orb=sab_orb, atomic_kind_set=atomic_kind_set)
1611 CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of, atom_of_kind=atom_of_kind)
1615 ALLOCATE (xtransformed(nspins))
1616 DO ispin = 1, nspins
1618 ct => work%ctransformed(ispin)
1620 CALL cp_fm_create(matrix=xtransformed(ispin), matrix_struct=fmstruct, name=
"XTRANSFORMED")
1624 ALLOCATE (tcharge(natom), gtcharge(natom, 4))
1626 cpmos => ex_env%cpmos
1631 DO ispin = 1, nspins
1632 ct => work%ctransformed(ispin)
1633 CALL cp_fm_get_info(ct, matrix_struct=fmstruct, nrow_global=nsgf)
1638 ALLOCATE (matrix_wx1(ispin)%matrix)
1639 CALL dbcsr_create(matrix=matrix_wx1(ispin)%matrix, template=matrix_s(1)%matrix)
1641 CALL dbcsr_set(matrix_wx1(ispin)%matrix, 0.0_dp)
1642 ALLOCATE (matrix_plo(ispin)%matrix)
1643 CALL dbcsr_create(matrix=matrix_plo(ispin)%matrix, template=matrix_s(1)%matrix)
1645 CALL dbcsr_set(matrix_plo(ispin)%matrix, 0.0_dp)
1649 IF (do_coulomb)
THEN
1651 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1654 DO jspin = 1, nspins
1655 ctjspin => work%ctransformed(jspin)
1657 CALL cp_fm_get_info(ctjspin, matrix_struct=fmstructjspin, nrow_global=nsgf)
1666 DO is = first_sgf(ia), last_sgf(ia)
1667 tcharge(ia) = tcharge(ia) + tv(is)
1677 NULLIFY (gamma_matrix)
1678 CALL setup_gamma(qs_env, stda_env, sub_env, gamma_matrix, ndim=4)
1679 tempmat => gamma_matrix(1)%matrix
1683 gtcharge(iatom, 1) = gtcharge(iatom, 1) + gab(1, 1)*tcharge(jatom)
1684 IF (iatom /= jatom)
THEN
1685 gtcharge(jatom, 1) = gtcharge(jatom, 1) + gab(1, 1)*tcharge(iatom)
1690 row=iatom, col=jatom, block=gab, found=found)
1692 gtcharge(iatom, idimk) = gtcharge(iatom, idimk) + gab(1, 1)*tcharge(jatom)
1693 IF (iatom /= jatom)
THEN
1694 gtcharge(jatom, idimk) = gtcharge(jatom, idimk) + fdim*gab(1, 1)*tcharge(iatom)
1703 ewald_env => work%ewald_env
1704 ewald_pw => work%ewald_pw
1705 CALL ewald_env_get(ewald_env, alpha=alpha, ewald_type=ewald_type)
1706 CALL get_qs_env(qs_env=qs_env, sab_orb=n_list, virial=virial)
1707 use_virial = .false.
1708 calculate_forces = .false.
1709 CALL tb_ewald_overlap(gtcharge, tcharge, alpha, n_list, virial, use_virial)
1711 gtcharge, tcharge, calculate_forces, virial, use_virial)
1713 IF (para_env%is_source())
THEN
1714 gtcharge(:, 1) = gtcharge(:, 1) - 2._dp*alpha*
oorootpi*tcharge(:)
1717 nlim =
get_limit(natom, para_env%num_pe, para_env%mepos)
1718 DO iatom = nlim(1), nlim(2)
1719 DO jatom = 1, iatom - 1
1720 rij = particle_set(iatom)%r - particle_set(jatom)%r
1721 rij =
pbc(rij, cell)
1722 dr = sqrt(sum(rij(:)**2))
1723 IF (dr > 1.e-6_dp)
THEN
1724 gtcharge(iatom, 1) = gtcharge(iatom, 1) + tcharge(jatom)/dr
1725 gtcharge(jatom, 1) = gtcharge(jatom, 1) + tcharge(iatom)/dr
1727 gtcharge(iatom, idimk) = gtcharge(iatom, idimk) + rij(idimk - 1)*tcharge(jatom)/dr**3
1728 gtcharge(jatom, idimk) = gtcharge(jatom, idimk) - rij(idimk - 1)*tcharge(iatom)/dr**3
1734 CALL para_env%sum(gtcharge(:, 1))
1739 DO is = first_sgf(ia), last_sgf(ia)
1740 tv(is) = gtcharge(ia, 1)
1745 ikind = kind_of(iatom)
1746 atom_i = atom_of_kind(iatom)
1748 fij(i) = spinfac*spinfac*gtcharge(iatom, i + 1)*tcharge(iatom)
1750 force(ikind)%rho_elec(1, atom_i) = force(ikind)%rho_elec(1, atom_i) - fij(1)
1751 force(ikind)%rho_elec(2, atom_i) = force(ikind)%rho_elec(2, atom_i) - fij(2)
1752 force(ikind)%rho_elec(3, atom_i) = force(ikind)%rho_elec(3, atom_i) - fij(3)
1755 IF (debug_forces)
THEN
1756 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1757 CALL para_env%sum(fodeb)
1758 focoul(1:3) = focoul(1:3) + fodeb(1:3)
1760 norb = nactive(ispin)
1762 CALL cp_fm_get_info(work%S_C0_C0T(ispin), matrix_struct=fmstruct)
1763 CALL cp_fm_create(t0matrix, matrix_struct=fmstruct, name=
"T0 SCRATCH")
1764 CALL cp_fm_create(t1matrix, matrix_struct=fmstruct, name=
"T1 SCRATCH")
1769 ct => work%ctransformed(ispin)
1772 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1773 cvec, 0.0_dp, ucmatrix)
1774 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1775 x(ispin), 0.0_dp, uxmatrix)
1776 CALL parallel_gemm(
'N',
'T', nsgf, nsgf, norb, 1.0_dp, uxmatrix, ucmatrix, 0.0_dp, t0matrix)
1779 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1780 cvec, 0.0_dp, uxmatrix)
1781 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1782 gs_mos(ispin)%mos_occ, 0.0_dp, ucmatrix)
1783 CALL parallel_gemm(
'N',
'T', nsgf, nsgf, norb, 1.0_dp, ucmatrix, uxmatrix, 1.0_dp, t0matrix)
1786 CALL parallel_gemm(
'N',
'N', nsgf, nsgf, nsgf, spinfac, work%S_eigenvectors, t1matrix, &
1789 matrix_g=work%S_eigenvectors, ncol=nsgf, alpha=2.0_dp, symmetry_mode=1)
1791 DEALLOCATE (ucmatrix)
1793 DEALLOCATE (uxmatrix)
1802 ct => work%ctransformed(ispin)
1806 CALL cp_fm_get_info(cvec, matrix_struct=fmstruct, nrow_global=nao)
1810 ncol_global=norb, para_env=fmstruct%para_env)
1813 CALL parallel_gemm(
"T",
"N", norb, norb, nao, 1.0_dp, gs_mos(ispin)%mos_occ, vcvec, 0.0_dp, cvcmat)
1814 CALL parallel_gemm(
"N",
"N", nao, norb, norb, 1.0_dp, x(ispin), cvcmat, 0.0_dp, vcvec)
1816 nactive(ispin), alpha=-2.0_dp*spinfac, beta=1.0_dp)
1820 matrix_g=vcvec, ncol=norb, alpha=2.0_dp*alpha, symmetry_mode=1)
1827 IF (stda_env%do_exchange)
THEN
1829 IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
1831 norb = nactive(ispin)
1833 tempmat => work%shalf
1834 CALL dbcsr_create(pdens, template=tempmat, matrix_type=dbcsr_type_no_symmetry)
1836 ct => work%ctransformed(ispin)
1839 1.0_dp, keep_sparsity=.false.)
1843 bp = stda_env%beta_param
1844 hfx = stda_env%hfx_fraction
1848 rij = particle_set(iatom)%r - particle_set(jatom)%r
1849 rij =
pbc(rij, cell)
1850 dr = sqrt(sum(rij(:)**2))
1851 ikind = kind_of(iatom)
1852 jkind = kind_of(jatom)
1853 eta = (stda_env%kind_param_set(ikind)%kind_param%hardness_param + &
1854 stda_env%kind_param_set(jkind)%kind_param%hardness_param)/2.0_dp
1856 IF (hfx > 0.0_dp)
THEN
1857 gabr = (1._dp/(rbeta + (hfx*eta)**(-bp)))**(1._dp/bp)
1859 IF (dr < 1.0e-6_dp)
THEN
1867 IF (dr > 1.0e-6_dp)
THEN
1868 IF (hfx > 0.0_dp)
THEN
1869 dgabr = -(1._dp/bp)*(1._dp/(rbeta + (hfx*eta)**(-bp)))**(1._dp/bp + 1._dp)
1870 dgabr = bp*rbeta/dr**2*dgabr
1871 dgabr = sum(pblock**2)*dgabr
1873 dgabr = -1.0_dp/dr**3
1874 dgabr = sum(pblock**2)*dgabr
1876 atom_i = atom_of_kind(iatom)
1877 atom_j = atom_of_kind(jatom)
1879 fij(i) = dgabr*rij(i)
1881 force(ikind)%rho_elec(1, atom_i) = force(ikind)%rho_elec(1, atom_i) - fij(1)
1882 force(ikind)%rho_elec(2, atom_i) = force(ikind)%rho_elec(2, atom_i) - fij(2)
1883 force(ikind)%rho_elec(3, atom_i) = force(ikind)%rho_elec(3, atom_i) - fij(3)
1884 force(jkind)%rho_elec(1, atom_j) = force(jkind)%rho_elec(1, atom_j) + fij(1)
1885 force(jkind)%rho_elec(2, atom_j) = force(jkind)%rho_elec(2, atom_j) + fij(2)
1886 force(jkind)%rho_elec(3, atom_j) = force(jkind)%rho_elec(3, atom_j) + fij(3)
1889 pblock = gabr*pblock
1898 CALL cp_fm_get_info(work%S_C0_C0T(ispin), matrix_struct=fmstruct)
1899 CALL cp_fm_create(t0matrix, matrix_struct=fmstruct, name=
"T0 SCRATCH")
1900 CALL cp_fm_create(t1matrix, matrix_struct=fmstruct, name=
"T1 SCRATCH")
1905 ct => work%ctransformed(ispin)
1907 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1908 cvec, 0.0_dp, ucmatrix)
1909 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1910 x(ispin), 0.0_dp, uxmatrix)
1911 CALL parallel_gemm(
'N',
'T', nsgf, nsgf, norb, 1.0_dp, uxmatrix, ucmatrix, 0.0_dp, t0matrix)
1913 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1914 cvec, 0.0_dp, uxmatrix)
1915 CALL parallel_gemm(
'T',
'N', nsgf, norb, nsgf, 1.0_dp, work%S_eigenvectors, &
1916 gs_mos(ispin)%mos_occ, 0.0_dp, ucmatrix)
1917 CALL parallel_gemm(
'N',
'T', nsgf, nsgf, norb, 1.0_dp, ucmatrix, uxmatrix, 1.0_dp, t0matrix)
1919 CALL parallel_gemm(
'N',
'N', nsgf, nsgf, nsgf, -1.0_dp, work%S_eigenvectors, t1matrix, &
1922 matrix_g=work%S_eigenvectors, ncol=nsgf, alpha=2.0_dp, symmetry_mode=1)
1924 DEALLOCATE (ucmatrix)
1926 DEALLOCATE (uxmatrix)
1934 alpha=-xfac, beta=1.0_dp)
1936 CALL cp_fm_get_info(cvec, matrix_struct=fmstruct, nrow_global=nao)
1942 ncol_global=norb, para_env=fmstruct%para_env)
1945 CALL parallel_gemm(
"T",
"N", norb, norb, nao, 1.0_dp, gs_mos(ispin)%mos_occ, vcvec, 0.0_dp, cvcmat)
1946 CALL parallel_gemm(
"N",
"N", nao, norb, norb, 1.0_dp, x(ispin), cvcmat, 0.0_dp, vcvec)
1948 norb, alpha=xfac, beta=1.0_dp)
1950 IF (nspins == 2)
THEN
1957 ncol=norb, alpha=2.0_dp*alpha, symmetry_mode=1)
1964 IF (debug_forces)
THEN
1965 fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
1966 CALL para_env%sum(fodeb)
1967 foexch(1:3) = foexch(1:3) + fodeb(1:3)
1976 IF (debug_forces)
THEN
1977 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Coul[X] ", focoul
1978 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Exch[X] ", foexch
1982 DEALLOCATE (tcharge, gtcharge)
1983 DEALLOCATE (first_sgf, last_sgf)
1986 IF (nspins == 2)
THEN
1987 CALL dbcsr_add(matrix_plo(1)%matrix, matrix_plo(2)%matrix, &
1988 alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
1992 IF (debug_forces) fodeb(1:3) = force(1)%overlap(1:3, 1)
1994 matrix_name=
"OVERLAP MATRIX", &
1995 basis_type_a=
"ORB", basis_type_b=
"ORB", &
1996 sab_nl=sab_orb, calculate_forces=.true., &
1997 matrix_p=matrix_plo(1)%matrix)
2000 IF (debug_forces)
THEN
2001 fodeb(1:3) = force(1)%overlap(1:3, 1) - fodeb(1:3)
2002 CALL para_env%sum(fodeb)
2003 IF (iounit > 0)
WRITE (iounit,
"(T3,A,T33,3F16.8)")
"DEBUG:: Lowdin ", fodeb
2007 ex_env%matrix_wx1 => matrix_wx1
2009 CALL timestop(handle)