205 print_active, ext_ks_matrix, ext_xc_section)
207 LOGICAL,
INTENT(in) :: calculate_forces, just_energy
208 LOGICAL,
INTENT(IN),
OPTIONAL :: print_active
210 POINTER :: ext_ks_matrix
213 CHARACTER(LEN=*),
PARAMETER :: routinen =
'qs_ks_build_kohn_sham_matrix'
215 CHARACTER(len=default_string_length) :: gauxc_model_name, name
216 INTEGER :: ace_rebuild_frequency, atom_a, gapw_representation, handle, iatom, ikind, img, &
217 ispin, natom, nimages, nspins, output_unit
218 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_of_kind, kind_of
219 LOGICAL :: ace_active, do_adiabatic_rescaling, do_ddapc, do_hfx, do_kpoints, do_ppl, dokp, &
220 gapw, gapw_all_all_electron, gapw_all_pseudopotentials, gapw_mixed_core_representations, &
221 gapw_xc, gauxc_model_none, just_energy_xc, lrigpw, my_print, &
222 native_direct_valence_atom_composite, native_gapw_all_electron_atom_composite, &
223 native_gapw_atom_composite_reference, native_gapw_atom_composite_requested, &
224 native_gapw_composite_direct_ao, native_gapw_composite_reference, &
225 native_gapw_periodic_atom_composite, native_grid_diagnostics, native_grid_use_cuda, &
226 native_skala_evaluator, native_skala_restore_exc
227 LOGICAL :: paw_composite_representation, rigpw, use_gauxc_matrix, use_virial
228 LOGICAL,
SAVE :: native_grid_cpu_kpoints_warned = .false.
229 REAL(kind=
dp) :: ecore_ppl, edisp, ee_ener, ekin_mol, mulliken_order_p, &
230 native_skala_composite_contraction, native_skala_composite_rho_contraction, &
231 native_skala_composite_tau_contraction, native_skala_exc_scf, native_skala_total_scf, &
233 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: cdft_gapw_values
234 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: cdft_charge_correction, &
235 cdft_electronic_charge, &
236 native_skala_atom_force
237 REAL(kind=
dp),
DIMENSION(3, 3) :: h_stress, native_skala_composite_virial, &
244 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: ksmat, matrix_vhxc, mo_derivs
245 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: ks_matrix, ks_matrix_im, matrix_h, &
246 matrix_h_im, matrix_s, matrix_vxc, &
266 TYPE(
pw_r3d_rs_type),
DIMENSION(:),
POINTER :: rho_r, v_rspace_embed, v_rspace_new, &
267 v_rspace_new_aux_fit, v_tau_rspace, &
269 TYPE(
pw_r3d_rs_type),
POINTER :: rho0_s_rs, rho_nlcc, rhoz_cneo_s_rs, v_hartree_rspace, &
270 v_sccs_rspace, v_sic_rspace, v_spin_ddapc_rest_r, vee, vppl_rspace
273 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
275 TYPE(
qs_rho_type),
POINTER :: rho, rho1, rho_struct, rho_xc
277 adiabatic_rescaling_section, &
278 gauxc_section, hfx_sections, input, &
279 scf_section, xc_section
282 CALL timeset(routinen, handle)
283 NULLIFY (admm_env, atomic_kind_set, cell, dft_control, force, logger, mo_derivs, my_rho, &
284 rho_struct, para_env, pw_env, virial, vppl_rspace, &
286 adiabatic_rescaling_section, hfx_sections, input, scf_section, &
287 xc_section, gauxc_section, matrix_h, matrix_h_im, matrix_s, auxbas_pw_pool, poisson_env, &
288 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, &
290 vee, rho_nlcc, ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, &
291 rho_core, sab_orb, particle_set, qs_kind_set, kpoints)
293 cpassert(
ASSOCIATED(qs_env))
297 IF (
PRESENT(print_active)) my_print = print_active
298 use_gauxc_matrix = .false.
299 native_gapw_atom_composite_reference = .false.
300 native_gapw_atom_composite_requested = .false.
301 native_gapw_all_electron_atom_composite = .false.
302 native_direct_valence_atom_composite = .false.
303 gapw_all_all_electron = .false.
304 gapw_all_pseudopotentials = .false.
305 gapw_mixed_core_representations = .false.
306 native_gapw_composite_direct_ao = .false.
307 native_gapw_composite_reference = .false.
308 native_gapw_periodic_atom_composite = .false.
309 native_grid_diagnostics = .false.
310 native_skala_composite_contraction = 0.0_dp
311 native_skala_composite_rho_contraction = 0.0_dp
312 native_skala_composite_tau_contraction = 0.0_dp
313 native_skala_restore_exc = .false.
317 dft_control=dft_control, &
318 matrix_h_kp=matrix_h, &
319 matrix_h_im_kp=matrix_h_im, &
320 matrix_s_kp=matrix_s, &
321 matrix_ks_kp=ks_matrix, &
322 matrix_ks_im_kp=ks_matrix_im, &
323 matrix_vhxc=matrix_vhxc, &
324 matrix_vxc_kp=matrix_vxc, &
327 atomic_kind_set=atomic_kind_set, &
331 v_hartree_rspace=v_hartree_rspace, &
341 do_kpoints=do_kpoints, &
342 particle_set=particle_set, &
343 qs_kind_set=qs_kind_set, &
347 IF (dft_control%low_spin_roks .AND. dft_control%qs_control%gapw)
THEN
348 IF (.NOT. virial%pv_numer .AND. &
349 (virial%pv_calculate .OR. (virial%pv_availability .AND. calculate_forces)))
THEN
350 CALL cp_abort(__location__,
"GAPW low spin ROKS stress is not implemented.")
352 IF (calculate_forces)
THEN
353 CALL cp_abort(__location__,
"GAPW low spin ROKS forces are not implemented.")
357 CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
359 nimages = dft_control%nimages
360 nspins = dft_control%nspins
363 IF (
PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
365 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
368 CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
369 just_energy_xc = just_energy
370 IF (do_adiabatic_rescaling)
THEN
373 just_energy_xc = .true.
376 cpassert(
ASSOCIATED(matrix_h))
377 cpassert(
ASSOCIATED(matrix_s))
378 cpassert(
ASSOCIATED(rho))
379 cpassert(
ASSOCIATED(pw_env))
380 cpassert(
SIZE(ks_matrix, 1) > 0)
384 do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
385 qs_env%cp_ddapc_ewald%do_decoupling .OR. &
386 qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
387 qs_env%cp_ddapc_ewald%do_solvation
390 lrigpw = dft_control%qs_control%lrigpw
391 rigpw = dft_control%qs_control%rigpw
393 cpassert(nimages == 1)
395 IF (lrigpw .AND. rigpw)
THEN
396 cpabort(
" LRI and RI are not compatible")
400 gapw = dft_control%qs_control%gapw
401 gapw_xc = dft_control%qs_control%gapw_xc
402 IF (gapw_xc .AND. gapw)
THEN
403 cpabort(
" GAPW and GAPW_XC are not compatible")
405 IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw))
THEN
406 cpabort(
" GAPW/GAPW_XC and LRIGPW are not compatible")
408 IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw))
THEN
409 cpabort(
" GAPW/GAPW_XC and RIGPW are not compatible")
412 do_ppl = dft_control%qs_control%do_ppl_method ==
do_ppl_grid
415 CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
419 cpassert(
ASSOCIATED(rho_xc))
423 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
426 cpabort(
"The implicit Poisson solver cannot be used in conjunction with GAPW.")
430 IF (gapw .OR. gapw_xc)
THEN
435 CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
436 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
440 "PRINT%DETAILED_ENERGY"), &
442 (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
455 IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc)
THEN
456 cpabort(
"GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
460 IF (dft_control%do_pcc)
THEN
468 IF (dft_control%do_sccs)
THEN
470 NULLIFY (v_sccs_rspace)
471 ALLOCATE (v_sccs_rspace)
472 CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
475 cpabort(
"The implicit Poisson solver cannot be used together with SCCS.")
478 IF (use_virial .AND. calculate_forces)
THEN
479 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
481 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe,
dp)
482 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe,
dp)
484 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
489 IF (use_virial .AND. calculate_forces)
THEN
490 h_stress(:, :) = 0.0_dp
492 v_hartree_gspace, h_stress=h_stress, &
494 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe,
dp)
495 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe,
dp)
498 v_hartree_gspace, rho_core=rho_core)
502 IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce)
THEN
503 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
505 qs_env%scf_control%gce%ref_esp, para_env)
510 CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
511 v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
514 dft_control%qs_control%ddapc_explicit_potential = .false.
515 dft_control%qs_control%ddapc_restraint_is_spin = .false.
516 IF (.NOT. just_energy)
THEN
517 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
518 CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
521 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
523 IF (dft_control%correct_surf_dip)
THEN
524 IF (dft_control%surf_dip_correct_switch)
THEN
526 energy%hartree = energy%hartree + energy%surf_dipole
531 CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
532 just_energy, calculate_forces, auxbas_pw_pool)
538 IF (dft_control%apply_external_potential)
THEN
544 IF (.NOT. just_energy)
THEN
547 rho0_s_rs=rho0_s_rs, &
548 rhoz_cneo_s_rs=rhoz_cneo_s_rs)
549 cpassert(
ASSOCIATED(rho0_s_rs))
550 IF (
ASSOCIATED(rhoz_cneo_s_rs))
THEN
551 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
554 IF (
ASSOCIATED(rhoz_cneo_s_rs))
THEN
555 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
564 IF (qs_env%qmmm)
THEN
567 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
568 qmmm_energy=energy%qmmm_el)
569 IF (qs_env%qmmm_env_qm%image_charge)
THEN
571 rho_hartree_gspace=rho_tot_gspace, &
573 qmmm_env=qs_env%qmmm_env_qm, &
575 IF (.NOT. just_energy)
THEN
577 v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
579 IF (calculate_forces)
THEN
581 potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
582 forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
583 qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
586 CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
587 DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
589 IF (.NOT. just_energy)
THEN
591 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
594 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
597 IF (dft_control%smeagol_control%smeagol_enabled .AND. &
599 cpassert(
ASSOCIATED(dft_control%smeagol_control%aux))
601 dft_control%smeagol_control%aux%HartreeLeadsLeft, &
602 dft_control%smeagol_control%aux%HartreeLeadsRight, &
603 dft_control%smeagol_control%aux%HartreeLeadsBottom, &
604 dft_control%smeagol_control%aux%VBias, &
605 dft_control%smeagol_control%aux%minL, &
606 dft_control%smeagol_control%aux%maxR, &
607 dft_control%smeagol_control%aux%isexplicit_maxR, &
608 dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
612 IF (dft_control%do_admm)
THEN
613 IF (
PRESENT(ext_xc_section))
THEN
619 IF (dft_control%do_admm_mo)
THEN
620 IF (qs_env%run_rtp)
THEN
629 ELSE IF (dft_control%do_admm_dm)
THEN
635 IF (use_virial .AND. calculate_forces) virial%pv_calculate = .true.
640 IF (dft_control%do_admm)
THEN
642 xc_section => admm_env%xc_section_aux
646 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
647 vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
648 just_energy=just_energy_xc)
650 IF (admm_env%do_gapw)
THEN
652 CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
653 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
654 xc_section_external=xc_section, &
655 rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
656 calculate_forces=calculate_forces)
662 IF (use_virial .AND. calculate_forces)
THEN
665 IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
666 IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
667 virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
668 virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
671 xc_section => admm_env%xc_section_primary
675 IF (
PRESENT(ext_xc_section)) xc_section => ext_xc_section
679 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
681 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
685 IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc)
THEN
690 IF (dft_control%apply_embed_pot)
THEN
691 NULLIFY (v_rspace_embed)
692 energy%embed_corr = 0.0_dp
694 energy%embed_corr, just_energy)
699 paw_composite_representation = .false.
700 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc))
THEN
704 native_skala_evaluator = native_skala_evaluator .OR. &
705 paw_composite_representation
707 IF (dft_control%use_gauxc)
THEN
708 IF (native_skala_evaluator)
THEN
709 IF (gapw .AND. dft_control%low_spin_roks)
THEN
710 CALL cp_abort(__location__, &
711 "GAPW low spin ROKS requires the molecular GauXC grid, not native Skala grids.")
715 IF (gapw .OR. gapw_xc)
THEN
718 gapw_mixed_core_representations = &
719 .NOT. gapw_all_all_electron .AND. .NOT. gapw_all_pseudopotentials
721 native_direct_valence_atom_composite = &
724 ((.NOT. gapw .AND. .NOT. gapw_xc) .OR. &
726 gapw_all_pseudopotentials))
727 native_gapw_composite_reference = &
729 native_gapw_atom_composite_requested = &
732 native_gapw_atom_composite_reference = &
733 native_gapw_atom_composite_requested .OR. paw_composite_representation
734 native_gapw_periodic_atom_composite = &
736 IF (gapw_mixed_core_representations .AND. &
737 .NOT. native_gapw_periodic_atom_composite)
THEN
738 CALL cp_abort(__location__, &
739 "Mixed all-electron and pseudopotential GAPW kinds require "// &
740 "NATIVE_GRID_LAYOUT ATOM_COMPOSITE so that their primitive "// &
741 "fields are combined before Skala feature construction.")
743 native_gapw_atom_composite_reference = &
744 native_gapw_atom_composite_reference .OR. &
745 (gapw_mixed_core_representations .AND. &
746 native_gapw_periodic_atom_composite)
747 native_gapw_all_electron_atom_composite = &
748 (gapw .OR. gapw_xc) .AND. &
749 gapw_all_all_electron .AND. &
750 (.NOT. native_gapw_composite_reference) .AND. &
751 native_gapw_periodic_atom_composite
752 native_gapw_atom_composite_reference = &
753 native_gapw_atom_composite_reference .OR. &
754 native_gapw_all_electron_atom_composite
755 IF (paw_composite_representation)
THEN
756 IF (any(cell%perd /= 0))
THEN
757 native_gapw_composite_reference = &
758 .NOT. native_gapw_periodic_atom_composite
759 native_gapw_atom_composite_reference = &
760 native_gapw_periodic_atom_composite
762 native_gapw_composite_reference = .false.
763 native_gapw_atom_composite_reference = .true.
766 IF (native_gapw_composite_reference .AND. native_gapw_atom_composite_reference)
THEN
767 CALL cp_abort(__location__, &
768 "Select only one native-grid GAPW composite reference route.")
770 native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
773 IF (
ASSOCIATED(gauxc_section))
THEN
775 l_val=native_grid_diagnostics)
777 IF ((.NOT. do_kpoints) .AND. nimages /= 1)
THEN
778 CALL cp_abort(__location__, &
779 "Native SKALA grid evaluation supports multiple images only "// &
780 "for k-point calculations.")
783 cpassert(
ASSOCIATED(kpoints))
785 cpassert(
ASSOCIATED(gauxc_section))
787 IF (.NOT. native_grid_use_cuda)
THEN
788 IF (para_env%mepos == 0 .AND. .NOT. native_grid_cpu_kpoints_warned)
THEN
789 CALL cp_warn(__location__, &
790 "Native SKALA grid evaluation with k-points is using the CPU TorchScript "// &
791 "path. Use a mutually compatible BLAS, ScaLAPACK, OpenMP, and LibTorch "// &
792 "runtime stack, or select NATIVE_GRID_USE_CUDA T.")
793 native_grid_cpu_kpoints_warned = .true.
797 IF (dft_control%roks)
THEN
798 cpabort(
"Native SKALA grid evaluation does not support ROKS.")
800 IF (dft_control%do_admm)
THEN
801 cpabort(
"Native SKALA grid evaluation does not support ADMM.")
806 native_skala_restore_exc = calculate_forces
807 IF (native_skala_restore_exc)
THEN
808 native_skala_exc_scf = energy%exc
809 native_skala_total_scf = energy%total
811 IF (calculate_forces)
THEN
812 ALLOCATE (native_skala_atom_force(3, natom))
813 native_skala_atom_force = 0.0_dp
814 IF (native_gapw_atom_composite_reference .OR. &
815 native_direct_valence_atom_composite)
THEN
816 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
817 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
818 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
819 just_energy=just_energy_xc, qs_env_external=qs_env, &
820 native_gapw_composite_override=native_gapw_composite_reference, &
821 native_skala_defer_to_atom_composite=.true.)
823 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
824 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
825 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
826 just_energy=just_energy_xc, &
827 native_skala_atom_force=native_skala_atom_force, &
828 qs_env_external=qs_env, &
829 native_gapw_composite_override=native_gapw_composite_reference)
831 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
832 ASSOCIATED(v_rspace_new) .AND.
ASSOCIATED(v_tau_rspace))
THEN
833 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
835 IF (output_unit > 0)
THEN
837 WRITE (unit=output_unit, fmt=
"(T2,A,1X,I0,3(1X,ES20.12))") &
838 "SKALA_GPW| Native regular-grid atom force", iatom, &
839 native_skala_atom_force(:, iatom)
844 native_skala_composite_virial = 0.0_dp
846 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
847 atom_force=native_skala_atom_force, &
848 strain_virial=native_skala_composite_virial, &
849 one_center_contraction=native_skala_composite_contraction, &
850 one_center_rho_contraction=native_skala_composite_rho_contraction, &
851 one_center_tau_contraction=native_skala_composite_tau_contraction)
852 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
854 IF (output_unit > 0)
THEN
855 WRITE (unit=output_unit, fmt=
"(T2,A)") &
856 "SKALA_GPW| Composite-direction one-center virial"
858 WRITE (unit=output_unit, fmt=
"(T2,A,1X,3ES20.10)") &
859 "SKALA_GPW|", native_skala_composite_virial(iatom, :)
863 virial%pv_xc = virial%pv_xc + native_skala_composite_virial
866 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
867 atom_force=native_skala_atom_force, &
868 one_center_contraction=native_skala_composite_contraction, &
869 one_center_rho_contraction=native_skala_composite_rho_contraction, &
870 one_center_tau_contraction=native_skala_composite_tau_contraction)
873 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
875 IF (output_unit > 0)
THEN
876 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
877 "SKALA_GPW| Composite-direction one-center VXC contraction", &
878 native_skala_composite_contraction
879 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
880 "SKALA_GPW| Composite-direction one-center rho contraction", &
881 native_skala_composite_rho_contraction
882 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
883 "SKALA_GPW| Composite-direction one-center tau contraction", &
884 native_skala_composite_tau_contraction
886 WRITE (unit=output_unit, fmt=
"(T2,A,1X,I0,3(1X,ES20.12))") &
887 "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
891 cpassert(
ASSOCIATED(force))
892 cpassert(
ASSOCIATED(atomic_kind_set))
895 ikind = kind_of(iatom)
896 atom_a = atom_of_kind(iatom)
897 force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
898 native_skala_atom_force(:, iatom)
900 DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
902 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
903 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
904 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
905 just_energy=just_energy_xc, qs_env_external=qs_env, &
906 native_gapw_composite_override=native_gapw_composite_reference, &
907 native_skala_defer_to_atom_composite= &
908 (native_gapw_atom_composite_reference .OR. &
909 native_direct_valence_atom_composite))
910 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
911 ASSOCIATED(v_rspace_new) .AND.
ASSOCIATED(v_tau_rspace))
THEN
913 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
914 one_center_contraction=native_skala_composite_contraction, &
915 one_center_rho_contraction=native_skala_composite_rho_contraction, &
916 one_center_tau_contraction=native_skala_composite_tau_contraction)
917 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
919 IF (output_unit > 0)
THEN
920 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
921 "SKALA_GPW| Composite-direction one-center VXC contraction", &
922 native_skala_composite_contraction
923 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
924 "SKALA_GPW| Composite-direction one-center rho contraction", &
925 native_skala_composite_rho_contraction
926 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
927 "SKALA_GPW| Composite-direction one-center tau contraction", &
928 native_skala_composite_tau_contraction
933 IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
934 IF (native_direct_valence_atom_composite)
THEN
935 cpassert(
ASSOCIATED(v_rspace_new))
936 cpassert(
ASSOCIATED(v_tau_rspace))
938 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
939 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
940 composite_vxc_tau=v_tau_rspace, direct_valence_atom_grid=.true.)
942 ELSE IF ((gapw .OR. gapw_xc) .AND. .NOT. native_gapw_composite_reference)
THEN
943 IF (native_gapw_atom_composite_reference)
THEN
944 cpassert(
ASSOCIATED(v_rspace_new))
945 cpassert(
ASSOCIATED(v_tau_rspace))
947 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
948 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
949 composite_vxc_tau=v_tau_rspace, atom_composite_grid=.true.)
953 xc_section_external=xc_section, &
954 calculate_forces=calculate_forces)
957 IF (edisp /= 0.0_dp) energy%dispersion = edisp
958 IF (qs_env%requires_matrix_vxc .AND.
ASSOCIATED(v_rspace_new))
THEN
960 matrix_vxc=matrix_vxc, &
961 gapw_full_basis=native_gapw_composite_direct_ao)
962 CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc)
965 use_gauxc_matrix = .true.
966 CALL apply_gauxc(qs_env, xc_section, calculate_forces)
969 xc_section_external=xc_section, &
970 calculate_forces=calculate_forces)
974 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
975 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
976 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
977 just_energy=just_energy_xc)
978 IF (edisp /= 0.0_dp) energy%dispersion = edisp
979 IF (qs_env%requires_matrix_vxc .AND.
ASSOCIATED(v_rspace_new))
THEN
981 CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc)
984 IF (gapw .OR. gapw_xc)
THEN
986 xc_section_external=xc_section, &
987 calculate_forces=calculate_forces)
992 IF (gapw .AND. dft_control%qs_control%cdft)
THEN
993 ALLOCATE (cdft_gapw_values(
SIZE(cdft_control%group)))
994 IF (cdft_control%atomic_charges)
THEN
995 ALLOCATE (cdft_charge_correction(natom, nspins), &
996 cdft_electronic_charge(cdft_control%natoms, nspins))
998 cdft_gapw_values, cdft_charge_correction)
1000 DO iatom = 1, cdft_control%natoms
1001 cdft_electronic_charge(iatom, ispin) = &
1002 pw_integral_ab(cdft_control%charge(iatom), rho_r(ispin), local_only=.true.)
1005 CALL para_env%sum(cdft_electronic_charge)
1006 DO ispin = 1, nspins
1007 DO iatom = 1, cdft_control%natoms
1008 cdft_electronic_charge(iatom, ispin) = &
1009 cdft_electronic_charge(iatom, ispin) + &
1010 cdft_charge_correction(cdft_control%atoms(iatom), ispin)
1016 cdft_control%value = cdft_control%value + cdft_gapw_values
1017 energy%cdft = energy%cdft + dot_product(cdft_control%strength, cdft_gapw_values)
1018 IF (cdft_control%atomic_charges)
THEN
1020 DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
1024 DEALLOCATE (cdft_gapw_values)
1027 IF (dft_control%mtlr_dft_with_perturbation .AND. .NOT. just_energy)
THEN
1028 IF (do_adiabatic_rescaling .OR. use_gauxc_matrix)
THEN
1029 cpabort(
"MTLR requires an XC potential on the real-space grid.")
1031 IF (.NOT.
ASSOCIATED(matrix_vhxc))
THEN
1032 ALLOCATE (matrix_vhxc(nspins))
1033 DO ispin = 1, nspins
1034 NULLIFY (matrix_vhxc(ispin)%matrix)
1035 ALLOCATE (matrix_vhxc(ispin)%matrix)
1037 template=ks_matrix(1, 1)%matrix)
1039 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1041 CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
1043 cpassert(
SIZE(matrix_vhxc) == nspins)
1044 DO ispin = 1, nspins
1045 cpassert(
ASSOCIATED(matrix_vhxc(ispin)%matrix))
1048 DO ispin = 1, nspins
1049 CALL auxbas_pw_pool%create_pw(v_hxc_rspace(ispin))
1050 CALL pw_zero(v_hxc_rspace(ispin))
1051 CALL pw_axpy(v_hartree_rspace, v_hxc_rspace(ispin))
1052 IF (
ASSOCIATED(v_rspace_new))
THEN
1053 CALL pw_axpy(v_rspace_new(ispin), &
1054 v_hxc_rspace(ispin), &
1055 v_rspace_new(ispin)%pw_grid%dvol)
1057 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1058 CALL integrate_v_rspace(v_hxc_rspace(ispin), &
1059 hmat=matrix_vhxc(ispin), &
1061 calculate_forces=.false.)
1062 CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
1067 IF (qs_env%harris_method)
THEN
1068 CALL get_qs_env(qs_env, harris_env=harris_env)
1072 NULLIFY (rho_struct)
1073 IF (use_virial .AND. calculate_forces)
THEN
1074 virial%pv_exc = virial%pv_exc - virial%pv_xc
1075 virial%pv_virial = virial%pv_virial - virial%pv_xc
1082 ace_active = .false.
1083 ace_rebuild_frequency = 1
1087 IF (
ASSOCIATED(ace_section))
THEN
1095 IF (ace_active)
THEN
1096 cpabort(
"ACE-HFX for k-points is not implemented yet")
1103 IF (ace_active)
THEN
1105 just_energy, v_rspace_new, v_tau_rspace, &
1106 ace_rebuild_frequency, ext_xc_section=xc_section)
1108 CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1109 just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
1114 IF (do_ppl .AND. calculate_forces)
THEN
1115 cpassert(.NOT. gapw)
1116 DO ispin = 1, nspins
1117 CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
1122 IF (
ASSOCIATED(rho_nlcc) .AND. calculate_forces .AND. &
1123 .NOT. native_gapw_atom_composite_reference .AND. &
1124 .NOT. native_direct_valence_atom_composite)
THEN
1125 DO ispin = 1, nspins
1126 CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
1127 IF (dft_control%do_admm)
CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
1132 IF (dft_control%qs_control%do_kg .AND. just_energy)
THEN
1134 cpassert(nimages == 1)
1135 ksmat => ks_matrix(:, 1)
1136 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1139 energy%exc = energy%exc - ekin_mol
1144 IF (.NOT. just_energy)
THEN
1145 IF (calculate_forces)
THEN
1148 (poisson_env%parameters%dielectric_params%dielec_core_correction))
THEN
1151 CALL auxbas_pw_pool%create_pw(v_minus_veps)
1152 CALL pw_copy(v_hartree_rspace, v_minus_veps)
1153 CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
1154 CALL integrate_v_core_rspace(v_minus_veps, qs_env)
1155 CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
1158 CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
1164 IF (dft_control%do_sccs)
THEN
1165 IF (dft_control%sccs_control%core_fill)
THEN
1167 REAL(kind=
dp),
DIMENSION(:),
ALLOCATABLE :: calpha, ccore
1170 CALL auxbas_pw_pool%create_pw(v_minus_vsccs)
1171 CALL pw_copy(v_sccs_rspace, v_minus_vsccs)
1172 CALL pw_scale(v_minus_vsccs, -1.0_dp)
1173 CALL integrate_v_core_rspace(v_minus_vsccs, qs_env, calpha=calpha, ccore=ccore)
1174 CALL auxbas_pw_pool%give_back_pw(v_minus_vsccs)
1175 DEALLOCATE (calpha, ccore)
1181 IF (.NOT. do_hfx)
THEN
1184 DO ispin = 1, nspins
1186 CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name)
1187 CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
1191 IF (qs_env%run_rtp)
THEN
1192 IF (dft_control%rtp_control%velocity_gauge)
THEN
1193 cpassert(
ASSOCIATED(matrix_h_im))
1194 cpassert(
ASSOCIATED(ks_matrix_im))
1195 DO ispin = 1, nspins
1197 CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name)
1198 CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
1205 IF (use_virial .AND. calculate_forces)
THEN
1206 pv_loc = virial%pv_virial
1212 CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
1213 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
1214 v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
1215 cdft_control, calculate_forces)
1217 IF (use_gauxc_matrix)
THEN
1218 CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc)
1219 cpassert(
ASSOCIATED(matrix_vxc))
1220 DO ispin = 1, nspins
1222 CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc(ispin, img)%matrix, &
1228 IF (gapw .OR. gapw_xc)
THEN
1229 IF (calculate_forces)
THEN
1231 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
1233 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
1236 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
1237 .NOT. native_skala_evaluator)
THEN
1238 gauxc_model_none = .false.
1239 gauxc_section => get_gauxc_section(xc_section)
1240 IF (
ASSOCIATED(gauxc_section))
THEN
1241 CALL section_vals_val_get(gauxc_section,
"MODEL", c_val=gauxc_model_name)
1242 gauxc_model_name = adjustl(gauxc_model_name)
1243 CALL uppercase(gauxc_model_name)
1244 gauxc_model_none = (trim(gauxc_model_name) ==
"" .OR. &
1245 trim(gauxc_model_name) ==
"NONE")
1247 IF (gauxc_model_none .AND. &
1248 (gapw_xc .OR. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set)))
THEN
1249 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1252 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1255 IF (dft_control%do_admm)
THEN
1256 CALL get_qs_env(qs_env, admm_env=admm_env)
1257 xc_section => admm_env%xc_section_aux
1258 CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
1260 IF (admm_env%do_admmp)
THEN
1261 vscale = admm_env%gsi(1)**2
1262 ELSE IF (admm_env%do_admms)
THEN
1263 vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
1265 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
1270 IF (use_virial .AND. calculate_forces)
THEN
1271 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1273 IF (dft_control%qs_control%do_kg)
THEN
1274 cpassert(nimages == 1)
1275 ksmat => ks_matrix(:, 1)
1277 IF (use_virial .AND. calculate_forces)
THEN
1278 pv_loc = virial%pv_virial
1281 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1283 energy%exc = energy%exc - ekin_mol
1286 IF (use_virial .AND. calculate_forces)
THEN
1289 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1292 virial%pv_exc = virial%pv_exc + virial%pv_xc
1293 virial%pv_virial = virial%pv_virial + virial%pv_xc
1294 virial%pv_xc = 0.0_dp
1301 IF (
ASSOCIATED(v_rspace_new))
THEN
1302 DO ispin = 1,
SIZE(v_rspace_new)
1303 CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
1305 DEALLOCATE (v_rspace_new)
1307 IF (
ASSOCIATED(v_tau_rspace))
THEN
1308 DO ispin = 1,
SIZE(v_tau_rspace)
1309 CALL auxbas_pw_pool%give_back_pw(v_tau_rspace(ispin))
1311 DEALLOCATE (v_tau_rspace)
1315 cpwarn(
"KS matrix no longer correct. Check possible problems with property calculations!")
1319 IF (dft_control%qs_control%ddapc_explicit_potential)
THEN
1320 CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
1321 DEALLOCATE (v_spin_ddapc_rest_r)
1324 IF (calculate_forces .AND. dft_control%qs_control%cdft)
THEN
1325 IF (.NOT. cdft_control%transfer_pot)
THEN
1326 DO iatom = 1,
SIZE(cdft_control%group)
1327 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1328 DEALLOCATE (cdft_control%group(iatom)%weight)
1330 IF (cdft_control%atomic_charges)
THEN
1331 DO iatom = 1, cdft_control%natoms
1332 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1334 DEALLOCATE (cdft_control%charge)
1336 IF (cdft_control%type == outer_scf_becke_constraint .AND. &
1337 cdft_control%becke_control%cavity_confine)
THEN
1338 IF (.NOT.
ASSOCIATED(cdft_control%becke_control%cavity_mat))
THEN
1339 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1341 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1343 ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint)
THEN
1344 IF (
ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm))
THEN
1345 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1348 IF (
ASSOCIATED(cdft_control%charges_fragment))
DEALLOCATE (cdft_control%charges_fragment)
1349 cdft_control%save_pot = .false.
1350 cdft_control%need_pot = .true.
1351 cdft_control%external_control = .false.
1355 IF (dft_control%do_sccs)
THEN
1356 CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
1357 DEALLOCATE (v_sccs_rspace)
1361 IF (dft_control%apply_external_potential)
THEN
1363 CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
1364 v_qmmm=vee, scale=-1.0_dp)
1366 CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
1369 CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
1370 CALL vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.false., &
1373 energy%core_cneo = 0.0_dp
1374 IF (
ASSOCIATED(local_rho_set%rhoz_cneo_set))
THEN
1375 DO iatom = 1,
SIZE(local_rho_set%rhoz_cneo_set)
1376 energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
1381 IF (gapw .OR. gapw_xc)
THEN
1383 CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
1384 IF (dft_control%do_admm)
THEN
1387 CALL admm_update_ks_atom(qs_env, calculate_forces)
1392 CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
1393 ks_matrix, matrix_s, rho, mulliken_order_p)
1396 IF (dft_control%dft_plus_u)
THEN
1397 IF (just_energy)
THEN
1398 CALL plus_u(qs_env=qs_env)
1400 CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
1403 energy%dft_plus_u = 0.0_dp
1407 DO ispin = 1, nspins
1409 CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
1410 dft_control%qs_control%eps_filter_matrix)
1415 IF (dft_control%do_admm_mo)
THEN
1416 IF (qs_env%run_rtp)
THEN
1417 CALL rtp_admm_merge_ks_matrix(qs_env)
1419 CALL admm_mo_merge_ks_matrix(qs_env)
1421 ELSE IF (dft_control%do_admm_dm)
THEN
1422 CALL admm_dm_merge_ks_matrix(qs_env)
1426 CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
1432 IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp)
THEN
1433 CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
1434 cpassert(nimages == 1)
1435 ksmat => ks_matrix(:, 1)
1436 CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
1440 IF (calculate_forces .AND. dft_control%do_admm)
THEN
1442 CALL calc_admm_ovlp_forces_kp(qs_env)
1444 CALL calc_admm_ovlp_forces(qs_env)
1449 CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
1450 calculate_forces, auxbas_pw_pool)
1453 CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
1454 calculate_forces, auxbas_pw_pool)
1457 CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
1460 CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
1461 energy, calculate_forces, just_energy)
1466 DO ispin = 1, nspins
1467 ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
1469 energy%core = energy%core + ecore_ppl
1474 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
1475 IF (lri_env%ppl_ri)
THEN
1477 DO ispin = 1, nspins
1478 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
1479 CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
1481 energy%core = energy%core + ecore_ppl
1486 IF (native_skala_restore_exc)
THEN
1487 energy%total = native_skala_total_scf
1488 IF (abnormal_value(energy%total))
THEN
1489 CALL cp_abort(__location__, &
1490 "SCF energy restored by native Skala is an abnormal value (NaN/Inf).")
1493 CALL check_sum_energies(energy)
1498 CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
1501 CALL timestop(handle)