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, matrix_vxc, mo_derivs
245 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: ks_matrix, ks_matrix_im, matrix_h, &
246 matrix_h_im, matrix_s, matrix_vxc_kp, &
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, &
289 matrix_vxc_kp, matrix_vhxc, &
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_vxc=matrix_vxc, &
324 matrix_vhxc=matrix_vhxc, &
325 matrix_vxc_kp=matrix_vxc_kp, &
328 atomic_kind_set=atomic_kind_set, &
332 v_hartree_rspace=v_hartree_rspace, &
342 do_kpoints=do_kpoints, &
343 particle_set=particle_set, &
344 qs_kind_set=qs_kind_set, &
348 IF (dft_control%low_spin_roks .AND. dft_control%qs_control%gapw)
THEN
349 IF (.NOT. virial%pv_numer .AND. &
350 (virial%pv_calculate .OR. (virial%pv_availability .AND. calculate_forces)))
THEN
351 CALL cp_abort(__location__,
"GAPW low spin ROKS stress is not implemented.")
353 IF (calculate_forces)
THEN
354 CALL cp_abort(__location__,
"GAPW low spin ROKS forces are not implemented.")
358 CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
360 nimages = dft_control%nimages
361 nspins = dft_control%nspins
364 IF (
PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
366 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
369 CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
370 just_energy_xc = just_energy
371 IF (do_adiabatic_rescaling)
THEN
374 just_energy_xc = .true.
377 cpassert(
ASSOCIATED(matrix_h))
378 cpassert(
ASSOCIATED(matrix_s))
379 cpassert(
ASSOCIATED(rho))
380 cpassert(
ASSOCIATED(pw_env))
381 cpassert(
SIZE(ks_matrix, 1) > 0)
385 do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
386 qs_env%cp_ddapc_ewald%do_decoupling .OR. &
387 qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
388 qs_env%cp_ddapc_ewald%do_solvation
391 lrigpw = dft_control%qs_control%lrigpw
392 rigpw = dft_control%qs_control%rigpw
394 cpassert(nimages == 1)
396 IF (lrigpw .AND. rigpw)
THEN
397 cpabort(
" LRI and RI are not compatible")
401 gapw = dft_control%qs_control%gapw
402 gapw_xc = dft_control%qs_control%gapw_xc
403 IF (gapw_xc .AND. gapw)
THEN
404 cpabort(
" GAPW and GAPW_XC are not compatible")
406 IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw))
THEN
407 cpabort(
" GAPW/GAPW_XC and LRIGPW are not compatible")
409 IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw))
THEN
410 cpabort(
" GAPW/GAPW_XC and RIGPW are not compatible")
413 do_ppl = dft_control%qs_control%do_ppl_method ==
do_ppl_grid
416 CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
420 cpassert(
ASSOCIATED(rho_xc))
424 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
427 cpabort(
"The implicit Poisson solver cannot be used in conjunction with GAPW.")
431 IF (gapw .OR. gapw_xc)
THEN
436 CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
437 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
441 "PRINT%DETAILED_ENERGY"), &
443 (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
456 IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc)
THEN
457 cpabort(
"GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
461 IF (dft_control%do_pcc)
THEN
469 IF (dft_control%do_sccs)
THEN
471 NULLIFY (v_sccs_rspace)
472 ALLOCATE (v_sccs_rspace)
473 CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
476 cpabort(
"The implicit Poisson solver cannot be used together with SCCS.")
479 IF (use_virial .AND. calculate_forces)
THEN
480 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
482 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe,
dp)
483 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe,
dp)
485 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
490 IF (use_virial .AND. calculate_forces)
THEN
491 h_stress(:, :) = 0.0_dp
493 v_hartree_gspace, h_stress=h_stress, &
495 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe,
dp)
496 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe,
dp)
499 v_hartree_gspace, rho_core=rho_core)
503 IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce)
THEN
504 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
506 qs_env%scf_control%gce%ref_esp, para_env)
511 CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
512 v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
515 dft_control%qs_control%ddapc_explicit_potential = .false.
516 dft_control%qs_control%ddapc_restraint_is_spin = .false.
517 IF (.NOT. just_energy)
THEN
518 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
519 CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
522 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
524 IF (dft_control%correct_surf_dip)
THEN
525 IF (dft_control%surf_dip_correct_switch)
THEN
527 energy%hartree = energy%hartree + energy%surf_dipole
532 CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
533 just_energy, calculate_forces, auxbas_pw_pool)
539 IF (dft_control%apply_external_potential)
THEN
545 IF (.NOT. just_energy)
THEN
548 rho0_s_rs=rho0_s_rs, &
549 rhoz_cneo_s_rs=rhoz_cneo_s_rs)
550 cpassert(
ASSOCIATED(rho0_s_rs))
551 IF (
ASSOCIATED(rhoz_cneo_s_rs))
THEN
552 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
555 IF (
ASSOCIATED(rhoz_cneo_s_rs))
THEN
556 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
565 IF (qs_env%qmmm)
THEN
568 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
569 qmmm_energy=energy%qmmm_el)
570 IF (qs_env%qmmm_env_qm%image_charge)
THEN
572 rho_hartree_gspace=rho_tot_gspace, &
574 qmmm_env=qs_env%qmmm_env_qm, &
576 IF (.NOT. just_energy)
THEN
578 v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
580 IF (calculate_forces)
THEN
582 potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
583 forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
584 qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
587 CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
588 DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
590 IF (.NOT. just_energy)
THEN
592 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
595 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
598 IF (dft_control%smeagol_control%smeagol_enabled .AND. &
600 cpassert(
ASSOCIATED(dft_control%smeagol_control%aux))
602 dft_control%smeagol_control%aux%HartreeLeadsLeft, &
603 dft_control%smeagol_control%aux%HartreeLeadsRight, &
604 dft_control%smeagol_control%aux%HartreeLeadsBottom, &
605 dft_control%smeagol_control%aux%VBias, &
606 dft_control%smeagol_control%aux%minL, &
607 dft_control%smeagol_control%aux%maxR, &
608 dft_control%smeagol_control%aux%isexplicit_maxR, &
609 dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
613 IF (dft_control%do_admm)
THEN
614 IF (
PRESENT(ext_xc_section))
THEN
620 IF (dft_control%do_admm_mo)
THEN
621 IF (qs_env%run_rtp)
THEN
630 ELSE IF (dft_control%do_admm_dm)
THEN
636 IF (use_virial .AND. calculate_forces) virial%pv_calculate = .true.
641 IF (dft_control%do_admm)
THEN
643 xc_section => admm_env%xc_section_aux
647 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
648 vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
649 just_energy=just_energy_xc)
651 IF (admm_env%do_gapw)
THEN
653 CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
654 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
655 xc_section_external=xc_section, &
656 rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
657 calculate_forces=calculate_forces)
663 IF (use_virial .AND. calculate_forces)
THEN
666 IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
667 IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
668 virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
669 virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
672 xc_section => admm_env%xc_section_primary
676 IF (
PRESENT(ext_xc_section)) xc_section => ext_xc_section
680 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
682 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
686 IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc)
THEN
691 IF (dft_control%apply_embed_pot)
THEN
692 NULLIFY (v_rspace_embed)
693 energy%embed_corr = 0.0_dp
695 energy%embed_corr, just_energy)
700 paw_composite_representation = .false.
701 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc))
THEN
705 native_skala_evaluator = native_skala_evaluator .OR. &
706 paw_composite_representation
708 IF (dft_control%use_gauxc)
THEN
709 IF (native_skala_evaluator)
THEN
710 IF (gapw .AND. dft_control%low_spin_roks)
THEN
711 CALL cp_abort(__location__, &
712 "GAPW low spin ROKS requires the molecular GauXC grid, not native Skala grids.")
716 IF (gapw .OR. gapw_xc)
THEN
719 gapw_mixed_core_representations = &
720 .NOT. gapw_all_all_electron .AND. .NOT. gapw_all_pseudopotentials
722 native_direct_valence_atom_composite = &
725 ((.NOT. gapw .AND. .NOT. gapw_xc) .OR. &
727 gapw_all_pseudopotentials))
728 native_gapw_composite_reference = &
730 native_gapw_atom_composite_requested = &
733 native_gapw_atom_composite_reference = &
734 native_gapw_atom_composite_requested .OR. paw_composite_representation
735 native_gapw_periodic_atom_composite = &
737 IF (gapw_mixed_core_representations .AND. &
738 .NOT. native_gapw_periodic_atom_composite)
THEN
739 CALL cp_abort(__location__, &
740 "Mixed all-electron and pseudopotential GAPW kinds require "// &
741 "NATIVE_GRID_LAYOUT ATOM_COMPOSITE so that their primitive "// &
742 "fields are combined before Skala feature construction.")
744 native_gapw_atom_composite_reference = &
745 native_gapw_atom_composite_reference .OR. &
746 (gapw_mixed_core_representations .AND. &
747 native_gapw_periodic_atom_composite)
748 native_gapw_all_electron_atom_composite = &
749 (gapw .OR. gapw_xc) .AND. &
750 gapw_all_all_electron .AND. &
751 (.NOT. native_gapw_composite_reference) .AND. &
752 native_gapw_periodic_atom_composite
753 native_gapw_atom_composite_reference = &
754 native_gapw_atom_composite_reference .OR. &
755 native_gapw_all_electron_atom_composite
756 IF (paw_composite_representation)
THEN
757 IF (any(cell%perd /= 0))
THEN
758 native_gapw_composite_reference = &
759 .NOT. native_gapw_periodic_atom_composite
760 native_gapw_atom_composite_reference = &
761 native_gapw_periodic_atom_composite
763 native_gapw_composite_reference = .false.
764 native_gapw_atom_composite_reference = .true.
767 IF (native_gapw_composite_reference .AND. native_gapw_atom_composite_reference)
THEN
768 CALL cp_abort(__location__, &
769 "Select only one native-grid GAPW composite reference route.")
771 native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
774 IF (
ASSOCIATED(gauxc_section))
THEN
776 l_val=native_grid_diagnostics)
778 IF ((.NOT. do_kpoints) .AND. nimages /= 1)
THEN
779 CALL cp_abort(__location__, &
780 "Native SKALA grid evaluation supports multiple images only "// &
781 "for k-point calculations.")
784 cpassert(
ASSOCIATED(kpoints))
786 cpassert(
ASSOCIATED(gauxc_section))
788 IF (.NOT. native_grid_use_cuda)
THEN
789 IF (para_env%mepos == 0 .AND. .NOT. native_grid_cpu_kpoints_warned)
THEN
790 CALL cp_warn(__location__, &
791 "Native SKALA grid evaluation with k-points is using the CPU TorchScript "// &
792 "path. Use a mutually compatible BLAS, ScaLAPACK, OpenMP, and LibTorch "// &
793 "runtime stack, or select NATIVE_GRID_USE_CUDA T.")
794 native_grid_cpu_kpoints_warned = .true.
798 IF (dft_control%roks)
THEN
799 cpabort(
"Native SKALA grid evaluation does not support ROKS.")
801 IF (dft_control%do_admm)
THEN
802 cpabort(
"Native SKALA grid evaluation does not support ADMM.")
807 native_skala_restore_exc = calculate_forces
808 IF (native_skala_restore_exc)
THEN
809 native_skala_exc_scf = energy%exc
810 native_skala_total_scf = energy%total
812 IF (calculate_forces)
THEN
813 ALLOCATE (native_skala_atom_force(3, natom))
814 native_skala_atom_force = 0.0_dp
815 IF (native_gapw_atom_composite_reference .OR. &
816 native_direct_valence_atom_composite)
THEN
817 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
818 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
819 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
820 just_energy=just_energy_xc, qs_env_external=qs_env, &
821 native_gapw_composite_override=native_gapw_composite_reference, &
822 native_skala_defer_to_atom_composite=.true.)
824 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
825 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
826 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
827 just_energy=just_energy_xc, &
828 native_skala_atom_force=native_skala_atom_force, &
829 qs_env_external=qs_env, &
830 native_gapw_composite_override=native_gapw_composite_reference)
832 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
833 ASSOCIATED(v_rspace_new) .AND.
ASSOCIATED(v_tau_rspace))
THEN
834 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
836 IF (output_unit > 0)
THEN
838 WRITE (unit=output_unit, fmt=
"(T2,A,1X,I0,3(1X,ES20.12))") &
839 "SKALA_GPW| Native regular-grid atom force", iatom, &
840 native_skala_atom_force(:, iatom)
845 native_skala_composite_virial = 0.0_dp
847 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
848 atom_force=native_skala_atom_force, &
849 strain_virial=native_skala_composite_virial, &
850 one_center_contraction=native_skala_composite_contraction, &
851 one_center_rho_contraction=native_skala_composite_rho_contraction, &
852 one_center_tau_contraction=native_skala_composite_tau_contraction)
853 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
855 IF (output_unit > 0)
THEN
856 WRITE (unit=output_unit, fmt=
"(T2,A)") &
857 "SKALA_GPW| Composite-direction one-center virial"
859 WRITE (unit=output_unit, fmt=
"(T2,A,1X,3ES20.10)") &
860 "SKALA_GPW|", native_skala_composite_virial(iatom, :)
864 virial%pv_xc = virial%pv_xc + native_skala_composite_virial
867 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
868 atom_force=native_skala_atom_force, &
869 one_center_contraction=native_skala_composite_contraction, &
870 one_center_rho_contraction=native_skala_composite_rho_contraction, &
871 one_center_tau_contraction=native_skala_composite_tau_contraction)
874 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
876 IF (output_unit > 0)
THEN
877 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
878 "SKALA_GPW| Composite-direction one-center VXC contraction", &
879 native_skala_composite_contraction
880 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
881 "SKALA_GPW| Composite-direction one-center rho contraction", &
882 native_skala_composite_rho_contraction
883 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
884 "SKALA_GPW| Composite-direction one-center tau contraction", &
885 native_skala_composite_tau_contraction
887 WRITE (unit=output_unit, fmt=
"(T2,A,1X,I0,3(1X,ES20.12))") &
888 "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
892 cpassert(
ASSOCIATED(force))
893 cpassert(
ASSOCIATED(atomic_kind_set))
896 ikind = kind_of(iatom)
897 atom_a = atom_of_kind(iatom)
898 force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
899 native_skala_atom_force(:, iatom)
901 DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
903 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
904 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
905 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
906 just_energy=just_energy_xc, qs_env_external=qs_env, &
907 native_gapw_composite_override=native_gapw_composite_reference, &
908 native_skala_defer_to_atom_composite= &
909 (native_gapw_atom_composite_reference .OR. &
910 native_direct_valence_atom_composite))
911 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
912 ASSOCIATED(v_rspace_new) .AND.
ASSOCIATED(v_tau_rspace))
THEN
914 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
915 one_center_contraction=native_skala_composite_contraction, &
916 one_center_rho_contraction=native_skala_composite_rho_contraction, &
917 one_center_tau_contraction=native_skala_composite_tau_contraction)
918 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
920 IF (output_unit > 0)
THEN
921 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
922 "SKALA_GPW| Composite-direction one-center VXC contraction", &
923 native_skala_composite_contraction
924 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
925 "SKALA_GPW| Composite-direction one-center rho contraction", &
926 native_skala_composite_rho_contraction
927 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
928 "SKALA_GPW| Composite-direction one-center tau contraction", &
929 native_skala_composite_tau_contraction
934 IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
935 IF (native_direct_valence_atom_composite)
THEN
936 cpassert(
ASSOCIATED(v_rspace_new))
937 cpassert(
ASSOCIATED(v_tau_rspace))
939 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
940 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
941 composite_vxc_tau=v_tau_rspace, direct_valence_atom_grid=.true.)
943 ELSE IF ((gapw .OR. gapw_xc) .AND. .NOT. native_gapw_composite_reference)
THEN
944 IF (native_gapw_atom_composite_reference)
THEN
945 cpassert(
ASSOCIATED(v_rspace_new))
946 cpassert(
ASSOCIATED(v_tau_rspace))
948 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
949 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
950 composite_vxc_tau=v_tau_rspace, atom_composite_grid=.true.)
954 xc_section_external=xc_section, &
955 calculate_forces=calculate_forces)
958 IF (edisp /= 0.0_dp) energy%dispersion = edisp
959 IF (qs_env%requires_matrix_vxc .AND.
ASSOCIATED(v_rspace_new))
THEN
962 matrix_vxc_kp=matrix_vxc_kp, &
963 gapw_full_basis=native_gapw_composite_direct_ao)
964 CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc_kp)
967 matrix_vxc=matrix_vxc, &
968 gapw_full_basis=native_gapw_composite_direct_ao)
969 CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
973 use_gauxc_matrix = .true.
974 CALL apply_gauxc(qs_env, xc_section, calculate_forces)
977 xc_section_external=xc_section, &
978 calculate_forces=calculate_forces)
982 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
983 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
984 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
985 just_energy=just_energy_xc)
986 IF (edisp /= 0.0_dp) energy%dispersion = edisp
987 IF (qs_env%requires_matrix_vxc .AND.
ASSOCIATED(v_rspace_new))
THEN
989 CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
992 IF (gapw .OR. gapw_xc)
THEN
994 xc_section_external=xc_section, &
995 calculate_forces=calculate_forces)
1000 IF (gapw .AND. dft_control%qs_control%cdft)
THEN
1001 ALLOCATE (cdft_gapw_values(
SIZE(cdft_control%group)))
1002 IF (cdft_control%atomic_charges)
THEN
1003 ALLOCATE (cdft_charge_correction(natom, nspins), &
1004 cdft_electronic_charge(cdft_control%natoms, nspins))
1006 cdft_gapw_values, cdft_charge_correction)
1007 DO ispin = 1, nspins
1008 DO iatom = 1, cdft_control%natoms
1009 cdft_electronic_charge(iatom, ispin) = &
1010 pw_integral_ab(cdft_control%charge(iatom), rho_r(ispin), local_only=.true.)
1013 CALL para_env%sum(cdft_electronic_charge)
1014 DO ispin = 1, nspins
1015 DO iatom = 1, cdft_control%natoms
1016 cdft_electronic_charge(iatom, ispin) = &
1017 cdft_electronic_charge(iatom, ispin) + &
1018 cdft_charge_correction(cdft_control%atoms(iatom), ispin)
1024 cdft_control%value = cdft_control%value + cdft_gapw_values
1025 energy%cdft = energy%cdft + dot_product(cdft_control%strength, cdft_gapw_values)
1026 IF (cdft_control%atomic_charges)
THEN
1028 DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
1032 DEALLOCATE (cdft_gapw_values)
1035 IF (dft_control%mtlr_dft_with_perturbation .AND. .NOT. just_energy)
THEN
1036 IF (do_adiabatic_rescaling .OR. use_gauxc_matrix)
THEN
1037 cpabort(
"MTLR requires an XC potential on the real-space grid.")
1039 IF (.NOT.
ASSOCIATED(matrix_vhxc))
THEN
1040 ALLOCATE (matrix_vhxc(nspins))
1041 DO ispin = 1, nspins
1042 NULLIFY (matrix_vhxc(ispin)%matrix)
1043 ALLOCATE (matrix_vhxc(ispin)%matrix)
1045 template=ks_matrix(1, 1)%matrix)
1047 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1049 CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
1051 cpassert(
SIZE(matrix_vhxc) == nspins)
1052 DO ispin = 1, nspins
1053 cpassert(
ASSOCIATED(matrix_vhxc(ispin)%matrix))
1056 DO ispin = 1, nspins
1057 CALL auxbas_pw_pool%create_pw(v_hxc_rspace(ispin))
1058 CALL pw_zero(v_hxc_rspace(ispin))
1059 CALL pw_axpy(v_hartree_rspace, v_hxc_rspace(ispin))
1060 IF (
ASSOCIATED(v_rspace_new))
THEN
1061 CALL pw_axpy(v_rspace_new(ispin), &
1062 v_hxc_rspace(ispin), &
1063 v_rspace_new(ispin)%pw_grid%dvol)
1065 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1066 CALL integrate_v_rspace(v_hxc_rspace(ispin), &
1067 hmat=matrix_vhxc(ispin), &
1069 calculate_forces=.false.)
1070 CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
1075 IF (qs_env%harris_method)
THEN
1076 CALL get_qs_env(qs_env, harris_env=harris_env)
1080 NULLIFY (rho_struct)
1081 IF (use_virial .AND. calculate_forces)
THEN
1082 virial%pv_exc = virial%pv_exc - virial%pv_xc
1083 virial%pv_virial = virial%pv_virial - virial%pv_xc
1090 ace_active = .false.
1091 ace_rebuild_frequency = 1
1095 IF (
ASSOCIATED(ace_section))
THEN
1103 IF (ace_active)
THEN
1104 cpabort(
"ACE-HFX for k-points is not implemented yet")
1111 IF (ace_active)
THEN
1113 just_energy, v_rspace_new, v_tau_rspace, &
1114 ace_rebuild_frequency, ext_xc_section=xc_section)
1116 CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1117 just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
1122 IF (do_ppl .AND. calculate_forces)
THEN
1123 cpassert(.NOT. gapw)
1124 DO ispin = 1, nspins
1125 CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
1130 IF (
ASSOCIATED(rho_nlcc) .AND. calculate_forces .AND. &
1131 .NOT. native_gapw_atom_composite_reference .AND. &
1132 .NOT. native_direct_valence_atom_composite)
THEN
1133 DO ispin = 1, nspins
1134 CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
1135 IF (dft_control%do_admm)
CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
1140 IF (dft_control%qs_control%do_kg .AND. just_energy)
THEN
1142 cpassert(nimages == 1)
1143 ksmat => ks_matrix(:, 1)
1144 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1147 energy%exc = energy%exc - ekin_mol
1152 IF (.NOT. just_energy)
THEN
1153 IF (calculate_forces)
THEN
1156 (poisson_env%parameters%dielectric_params%dielec_core_correction))
THEN
1159 CALL auxbas_pw_pool%create_pw(v_minus_veps)
1160 CALL pw_copy(v_hartree_rspace, v_minus_veps)
1161 CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
1162 CALL integrate_v_core_rspace(v_minus_veps, qs_env)
1163 CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
1166 CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
1172 IF (dft_control%do_sccs)
THEN
1173 IF (dft_control%sccs_control%core_fill)
THEN
1175 REAL(kind=
dp),
DIMENSION(:),
ALLOCATABLE :: calpha, ccore
1178 CALL auxbas_pw_pool%create_pw(v_minus_vsccs)
1179 CALL pw_copy(v_sccs_rspace, v_minus_vsccs)
1180 CALL pw_scale(v_minus_vsccs, -1.0_dp)
1181 CALL integrate_v_core_rspace(v_minus_vsccs, qs_env, calpha=calpha, ccore=ccore)
1182 CALL auxbas_pw_pool%give_back_pw(v_minus_vsccs)
1183 DEALLOCATE (calpha, ccore)
1189 IF (.NOT. do_hfx)
THEN
1192 DO ispin = 1, nspins
1194 CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name)
1195 CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
1199 IF (qs_env%run_rtp)
THEN
1200 IF (dft_control%rtp_control%velocity_gauge)
THEN
1201 cpassert(
ASSOCIATED(matrix_h_im))
1202 cpassert(
ASSOCIATED(ks_matrix_im))
1203 DO ispin = 1, nspins
1205 CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name)
1206 CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
1213 IF (use_virial .AND. calculate_forces)
THEN
1214 pv_loc = virial%pv_virial
1220 CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
1221 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
1222 v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
1223 cdft_control, calculate_forces)
1225 IF (use_gauxc_matrix)
THEN
1227 CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc_kp)
1228 cpassert(
ASSOCIATED(matrix_vxc_kp))
1229 DO ispin = 1, nspins
1231 CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc_kp(ispin, img)%matrix, &
1236 CALL get_qs_env(qs_env=qs_env, matrix_vxc=matrix_vxc)
1237 cpassert(
ASSOCIATED(matrix_vxc))
1238 cpassert(nimages == 1)
1239 DO ispin = 1, nspins
1240 CALL dbcsr_add(ks_matrix(ispin, 1)%matrix, matrix_vxc(ispin)%matrix, 1.0_dp, 1.0_dp)
1245 IF (gapw .OR. gapw_xc)
THEN
1246 IF (calculate_forces)
THEN
1248 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
1250 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
1253 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
1254 .NOT. native_skala_evaluator)
THEN
1255 gauxc_model_none = .false.
1256 gauxc_section => get_gauxc_section(xc_section)
1257 IF (
ASSOCIATED(gauxc_section))
THEN
1258 CALL section_vals_val_get(gauxc_section,
"MODEL", c_val=gauxc_model_name)
1259 gauxc_model_name = adjustl(gauxc_model_name)
1260 CALL uppercase(gauxc_model_name)
1261 gauxc_model_none = (trim(gauxc_model_name) ==
"" .OR. &
1262 trim(gauxc_model_name) ==
"NONE")
1264 IF (gauxc_model_none .AND. &
1265 (gapw_xc .OR. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set)))
THEN
1266 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1269 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1272 IF (dft_control%do_admm)
THEN
1273 CALL get_qs_env(qs_env, admm_env=admm_env)
1274 xc_section => admm_env%xc_section_aux
1275 CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
1277 IF (admm_env%do_admmp)
THEN
1278 vscale = admm_env%gsi(1)**2
1279 ELSE IF (admm_env%do_admms)
THEN
1280 vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
1282 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
1287 IF (use_virial .AND. calculate_forces)
THEN
1288 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1290 IF (dft_control%qs_control%do_kg)
THEN
1291 cpassert(nimages == 1)
1292 ksmat => ks_matrix(:, 1)
1294 IF (use_virial .AND. calculate_forces)
THEN
1295 pv_loc = virial%pv_virial
1298 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1300 energy%exc = energy%exc - ekin_mol
1303 IF (use_virial .AND. calculate_forces)
THEN
1306 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1309 virial%pv_exc = virial%pv_exc + virial%pv_xc
1310 virial%pv_virial = virial%pv_virial + virial%pv_xc
1311 virial%pv_xc = 0.0_dp
1318 IF (
ASSOCIATED(v_rspace_new))
THEN
1319 DO ispin = 1,
SIZE(v_rspace_new)
1320 CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
1322 DEALLOCATE (v_rspace_new)
1324 IF (
ASSOCIATED(v_tau_rspace))
THEN
1325 DO ispin = 1,
SIZE(v_tau_rspace)
1326 CALL auxbas_pw_pool%give_back_pw(v_tau_rspace(ispin))
1328 DEALLOCATE (v_tau_rspace)
1332 cpwarn(
"KS matrix no longer correct. Check possible problems with property calculations!")
1336 IF (dft_control%qs_control%ddapc_explicit_potential)
THEN
1337 CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
1338 DEALLOCATE (v_spin_ddapc_rest_r)
1341 IF (calculate_forces .AND. dft_control%qs_control%cdft)
THEN
1342 IF (.NOT. cdft_control%transfer_pot)
THEN
1343 DO iatom = 1,
SIZE(cdft_control%group)
1344 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1345 DEALLOCATE (cdft_control%group(iatom)%weight)
1347 IF (cdft_control%atomic_charges)
THEN
1348 DO iatom = 1, cdft_control%natoms
1349 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1351 DEALLOCATE (cdft_control%charge)
1353 IF (cdft_control%type == outer_scf_becke_constraint .AND. &
1354 cdft_control%becke_control%cavity_confine)
THEN
1355 IF (.NOT.
ASSOCIATED(cdft_control%becke_control%cavity_mat))
THEN
1356 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1358 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1360 ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint)
THEN
1361 IF (
ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm))
THEN
1362 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1365 IF (
ASSOCIATED(cdft_control%charges_fragment))
DEALLOCATE (cdft_control%charges_fragment)
1366 cdft_control%save_pot = .false.
1367 cdft_control%need_pot = .true.
1368 cdft_control%external_control = .false.
1372 IF (dft_control%do_sccs)
THEN
1373 CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
1374 DEALLOCATE (v_sccs_rspace)
1378 IF (dft_control%apply_external_potential)
THEN
1380 CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
1381 v_qmmm=vee, scale=-1.0_dp)
1383 CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
1386 CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
1387 CALL vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.false., &
1390 energy%core_cneo = 0.0_dp
1391 IF (
ASSOCIATED(local_rho_set%rhoz_cneo_set))
THEN
1392 DO iatom = 1,
SIZE(local_rho_set%rhoz_cneo_set)
1393 energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
1398 IF (gapw .OR. gapw_xc)
THEN
1400 CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
1401 IF (dft_control%do_admm)
THEN
1404 CALL admm_update_ks_atom(qs_env, calculate_forces)
1409 CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
1410 ks_matrix, matrix_s, rho, mulliken_order_p)
1413 IF (dft_control%dft_plus_u)
THEN
1414 IF (just_energy)
THEN
1415 CALL plus_u(qs_env=qs_env)
1417 CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
1420 energy%dft_plus_u = 0.0_dp
1424 DO ispin = 1, nspins
1426 CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
1427 dft_control%qs_control%eps_filter_matrix)
1432 IF (dft_control%do_admm_mo)
THEN
1433 IF (qs_env%run_rtp)
THEN
1434 CALL rtp_admm_merge_ks_matrix(qs_env)
1436 CALL admm_mo_merge_ks_matrix(qs_env)
1438 ELSE IF (dft_control%do_admm_dm)
THEN
1439 CALL admm_dm_merge_ks_matrix(qs_env)
1443 CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
1449 IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp)
THEN
1450 CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
1451 cpassert(nimages == 1)
1452 ksmat => ks_matrix(:, 1)
1453 CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
1457 IF (calculate_forces .AND. dft_control%do_admm)
THEN
1459 CALL calc_admm_ovlp_forces_kp(qs_env)
1461 CALL calc_admm_ovlp_forces(qs_env)
1466 CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
1467 calculate_forces, auxbas_pw_pool)
1470 CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
1471 calculate_forces, auxbas_pw_pool)
1474 CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
1477 CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
1478 energy, calculate_forces, just_energy)
1483 DO ispin = 1, nspins
1484 ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
1486 energy%core = energy%core + ecore_ppl
1491 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
1492 IF (lri_env%ppl_ri)
THEN
1494 DO ispin = 1, nspins
1495 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
1496 CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
1498 energy%core = energy%core + ecore_ppl
1503 IF (native_skala_restore_exc)
THEN
1504 energy%total = native_skala_total_scf
1505 IF (abnormal_value(energy%total))
THEN
1506 CALL cp_abort(__location__, &
1507 "SCF energy restored by native Skala is an abnormal value (NaN/Inf).")
1510 CALL check_sum_energies(energy)
1515 CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
1518 CALL timestop(handle)