203 print_active, ext_ks_matrix, ext_xc_section)
205 LOGICAL,
INTENT(in) :: calculate_forces, just_energy
206 LOGICAL,
INTENT(IN),
OPTIONAL :: print_active
208 POINTER :: ext_ks_matrix
211 CHARACTER(LEN=*),
PARAMETER :: routinen =
'qs_ks_build_kohn_sham_matrix'
213 CHARACTER(len=default_string_length) :: gauxc_model_name, name
214 INTEGER :: ace_rebuild_frequency, atom_a, gapw_representation, handle, iatom, ikind, img, &
215 ispin, natom, nimages, nspins, output_unit
216 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_of_kind, kind_of
217 LOGICAL :: ace_active, do_adiabatic_rescaling, do_ddapc, do_hfx, do_kpoints, do_ppl, dokp, &
218 gapw, gapw_all_all_electron, gapw_all_pseudopotentials, gapw_mixed_core_representations, &
219 gapw_xc, gauxc_model_none, just_energy_xc, lrigpw, my_print, &
220 native_direct_valence_atom_composite, native_gapw_all_electron_atom_composite, &
221 native_gapw_atom_composite_reference, native_gapw_atom_composite_requested, &
222 native_gapw_composite_direct_ao, native_gapw_composite_reference, &
223 native_gapw_periodic_atom_composite, native_grid_diagnostics, native_grid_use_cuda, &
224 native_skala_evaluator, native_skala_restore_exc
225 LOGICAL :: paw_composite_representation, rigpw, use_gauxc_matrix, use_virial
226 LOGICAL,
SAVE :: native_grid_cpu_kpoints_warned = .false.
227 REAL(kind=
dp) :: ecore_ppl, edisp, ee_ener, ekin_mol, mulliken_order_p, &
228 native_skala_composite_contraction, native_skala_composite_rho_contraction, &
229 native_skala_composite_tau_contraction, native_skala_exc_scf, native_skala_total_scf, &
231 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: cdft_gapw_values
232 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: cdft_charge_correction, &
233 cdft_electronic_charge, &
234 native_skala_atom_force
235 REAL(kind=
dp),
DIMENSION(3, 3) :: h_stress, native_skala_composite_virial, &
242 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: ksmat, matrix_vhxc, matrix_vxc, mo_derivs
243 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: ks_matrix, ks_matrix_im, matrix_h, &
244 matrix_h_im, matrix_s, matrix_vxc_kp, &
264 TYPE(
pw_r3d_rs_type),
DIMENSION(:),
POINTER :: rho_r, v_rspace_embed, v_rspace_new, &
265 v_rspace_new_aux_fit, v_tau_rspace, &
267 TYPE(
pw_r3d_rs_type),
POINTER :: rho0_s_rs, rho_nlcc, rhoz_cneo_s_rs, v_hartree_rspace, &
268 v_sccs_rspace, v_sic_rspace, v_spin_ddapc_rest_r, vee, vppl_rspace
271 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
273 TYPE(
qs_rho_type),
POINTER :: rho, rho1, rho_struct, rho_xc
275 adiabatic_rescaling_section, &
276 gauxc_section, hfx_sections, input, &
277 scf_section, xc_section
280 CALL timeset(routinen, handle)
281 NULLIFY (admm_env, atomic_kind_set, cell, dft_control, force, logger, mo_derivs, my_rho, &
282 rho_struct, para_env, pw_env, virial, vppl_rspace, &
284 adiabatic_rescaling_section, hfx_sections, input, scf_section, &
285 xc_section, gauxc_section, matrix_h, matrix_h_im, matrix_s, auxbas_pw_pool, poisson_env, &
286 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, &
287 matrix_vxc_kp, matrix_vhxc, &
288 vee, rho_nlcc, ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, &
289 rho_core, sab_orb, particle_set, qs_kind_set, kpoints)
291 cpassert(
ASSOCIATED(qs_env))
295 IF (
PRESENT(print_active)) my_print = print_active
296 use_gauxc_matrix = .false.
297 native_gapw_atom_composite_reference = .false.
298 native_gapw_atom_composite_requested = .false.
299 native_gapw_all_electron_atom_composite = .false.
300 native_direct_valence_atom_composite = .false.
301 gapw_all_all_electron = .false.
302 gapw_all_pseudopotentials = .false.
303 gapw_mixed_core_representations = .false.
304 native_gapw_composite_direct_ao = .false.
305 native_gapw_composite_reference = .false.
306 native_gapw_periodic_atom_composite = .false.
307 native_grid_diagnostics = .false.
308 native_skala_composite_contraction = 0.0_dp
309 native_skala_composite_rho_contraction = 0.0_dp
310 native_skala_composite_tau_contraction = 0.0_dp
311 native_skala_restore_exc = .false.
315 dft_control=dft_control, &
316 matrix_h_kp=matrix_h, &
317 matrix_h_im_kp=matrix_h_im, &
318 matrix_s_kp=matrix_s, &
319 matrix_ks_kp=ks_matrix, &
320 matrix_ks_im_kp=ks_matrix_im, &
321 matrix_vxc=matrix_vxc, &
322 matrix_vhxc=matrix_vhxc, &
323 matrix_vxc_kp=matrix_vxc_kp, &
326 atomic_kind_set=atomic_kind_set, &
330 v_hartree_rspace=v_hartree_rspace, &
340 do_kpoints=do_kpoints, &
341 particle_set=particle_set, &
342 qs_kind_set=qs_kind_set, &
345 CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
347 nimages = dft_control%nimages
348 nspins = dft_control%nspins
351 IF (
PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
353 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
356 CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
357 just_energy_xc = just_energy
358 IF (do_adiabatic_rescaling)
THEN
361 just_energy_xc = .true.
364 cpassert(
ASSOCIATED(matrix_h))
365 cpassert(
ASSOCIATED(matrix_s))
366 cpassert(
ASSOCIATED(rho))
367 cpassert(
ASSOCIATED(pw_env))
368 cpassert(
SIZE(ks_matrix, 1) > 0)
372 do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
373 qs_env%cp_ddapc_ewald%do_decoupling .OR. &
374 qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
375 qs_env%cp_ddapc_ewald%do_solvation
378 lrigpw = dft_control%qs_control%lrigpw
379 rigpw = dft_control%qs_control%rigpw
381 cpassert(nimages == 1)
383 IF (lrigpw .AND. rigpw)
THEN
384 cpabort(
" LRI and RI are not compatible")
388 gapw = dft_control%qs_control%gapw
389 gapw_xc = dft_control%qs_control%gapw_xc
390 IF (gapw_xc .AND. gapw)
THEN
391 cpabort(
" GAPW and GAPW_XC are not compatible")
393 IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw))
THEN
394 cpabort(
" GAPW/GAPW_XC and LRIGPW are not compatible")
396 IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw))
THEN
397 cpabort(
" GAPW/GAPW_XC and RIGPW are not compatible")
400 do_ppl = dft_control%qs_control%do_ppl_method ==
do_ppl_grid
403 CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
407 cpassert(
ASSOCIATED(rho_xc))
411 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
414 cpabort(
"The implicit Poisson solver cannot be used in conjunction with GAPW.")
418 IF (gapw .OR. gapw_xc)
THEN
423 CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
424 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
428 "PRINT%DETAILED_ENERGY"), &
430 (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
443 IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc)
THEN
444 cpabort(
"GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
448 IF (dft_control%do_pcc)
THEN
456 IF (dft_control%do_sccs)
THEN
458 NULLIFY (v_sccs_rspace)
459 ALLOCATE (v_sccs_rspace)
460 CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
463 cpabort(
"The implicit Poisson solver cannot be used together with SCCS.")
466 IF (use_virial .AND. calculate_forces)
THEN
467 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
469 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe,
dp)
470 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe,
dp)
472 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
477 IF (use_virial .AND. calculate_forces)
THEN
478 h_stress(:, :) = 0.0_dp
480 v_hartree_gspace, h_stress=h_stress, &
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)
486 v_hartree_gspace, rho_core=rho_core)
490 IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce)
THEN
491 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
493 qs_env%scf_control%gce%ref_esp, para_env)
498 CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
499 v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
502 dft_control%qs_control%ddapc_explicit_potential = .false.
503 dft_control%qs_control%ddapc_restraint_is_spin = .false.
504 IF (.NOT. just_energy)
THEN
505 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
506 CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
509 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
511 IF (dft_control%correct_surf_dip)
THEN
512 IF (dft_control%surf_dip_correct_switch)
THEN
514 energy%hartree = energy%hartree + energy%surf_dipole
519 CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
520 just_energy, calculate_forces, auxbas_pw_pool)
526 IF (dft_control%apply_external_potential)
THEN
532 IF (.NOT. just_energy)
THEN
535 rho0_s_rs=rho0_s_rs, &
536 rhoz_cneo_s_rs=rhoz_cneo_s_rs)
537 cpassert(
ASSOCIATED(rho0_s_rs))
538 IF (
ASSOCIATED(rhoz_cneo_s_rs))
THEN
539 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
542 IF (
ASSOCIATED(rhoz_cneo_s_rs))
THEN
543 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
552 IF (qs_env%qmmm)
THEN
555 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
556 qmmm_energy=energy%qmmm_el)
557 IF (qs_env%qmmm_env_qm%image_charge)
THEN
559 rho_hartree_gspace=rho_tot_gspace, &
561 qmmm_env=qs_env%qmmm_env_qm, &
563 IF (.NOT. just_energy)
THEN
565 v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
567 IF (calculate_forces)
THEN
569 potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
570 forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
571 qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
574 CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
575 DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
577 IF (.NOT. just_energy)
THEN
579 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
582 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
585 IF (dft_control%smeagol_control%smeagol_enabled .AND. &
587 cpassert(
ASSOCIATED(dft_control%smeagol_control%aux))
589 dft_control%smeagol_control%aux%HartreeLeadsLeft, &
590 dft_control%smeagol_control%aux%HartreeLeadsRight, &
591 dft_control%smeagol_control%aux%HartreeLeadsBottom, &
592 dft_control%smeagol_control%aux%VBias, &
593 dft_control%smeagol_control%aux%minL, &
594 dft_control%smeagol_control%aux%maxR, &
595 dft_control%smeagol_control%aux%isexplicit_maxR, &
596 dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
600 IF (dft_control%do_admm)
THEN
601 IF (
PRESENT(ext_xc_section))
THEN
607 IF (dft_control%do_admm_mo)
THEN
608 IF (qs_env%run_rtp)
THEN
617 ELSE IF (dft_control%do_admm_dm)
THEN
623 IF (use_virial .AND. calculate_forces) virial%pv_calculate = .true.
628 IF (dft_control%do_admm)
THEN
630 xc_section => admm_env%xc_section_aux
634 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
635 vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
636 just_energy=just_energy_xc)
638 IF (admm_env%do_gapw)
THEN
640 CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
641 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
642 xc_section_external=xc_section, &
643 rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
644 calculate_forces=calculate_forces)
650 IF (use_virial .AND. calculate_forces)
THEN
653 IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
654 IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
655 virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
656 virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
659 xc_section => admm_env%xc_section_primary
663 IF (
PRESENT(ext_xc_section)) xc_section => ext_xc_section
667 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
669 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
673 IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc)
THEN
678 IF (dft_control%apply_embed_pot)
THEN
679 NULLIFY (v_rspace_embed)
680 energy%embed_corr = 0.0_dp
682 energy%embed_corr, just_energy)
687 paw_composite_representation = .false.
688 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc))
THEN
692 native_skala_evaluator = native_skala_evaluator .OR. &
693 paw_composite_representation
695 IF (dft_control%use_gauxc)
THEN
696 IF (native_skala_evaluator)
THEN
699 IF (gapw .OR. gapw_xc)
THEN
702 gapw_mixed_core_representations = &
703 .NOT. gapw_all_all_electron .AND. .NOT. gapw_all_pseudopotentials
705 native_direct_valence_atom_composite = &
708 ((.NOT. gapw .AND. .NOT. gapw_xc) .OR. &
710 gapw_all_pseudopotentials))
711 native_gapw_composite_reference = &
713 native_gapw_atom_composite_requested = &
716 native_gapw_atom_composite_reference = &
717 native_gapw_atom_composite_requested .OR. paw_composite_representation
718 native_gapw_periodic_atom_composite = &
720 IF (gapw_mixed_core_representations .AND. &
721 .NOT. native_gapw_periodic_atom_composite)
THEN
722 CALL cp_abort(__location__, &
723 "Mixed all-electron and pseudopotential GAPW kinds require "// &
724 "NATIVE_GRID_LAYOUT ATOM_COMPOSITE so that their primitive "// &
725 "fields are combined before Skala feature construction.")
727 native_gapw_atom_composite_reference = &
728 native_gapw_atom_composite_reference .OR. &
729 (gapw_mixed_core_representations .AND. &
730 native_gapw_periodic_atom_composite)
731 native_gapw_all_electron_atom_composite = &
732 (gapw .OR. gapw_xc) .AND. &
733 gapw_all_all_electron .AND. &
734 (.NOT. native_gapw_composite_reference) .AND. &
735 native_gapw_periodic_atom_composite
736 native_gapw_atom_composite_reference = &
737 native_gapw_atom_composite_reference .OR. &
738 native_gapw_all_electron_atom_composite
739 IF (paw_composite_representation)
THEN
740 IF (any(cell%perd /= 0))
THEN
741 native_gapw_composite_reference = &
742 .NOT. native_gapw_periodic_atom_composite
743 native_gapw_atom_composite_reference = &
744 native_gapw_periodic_atom_composite
746 native_gapw_composite_reference = .false.
747 native_gapw_atom_composite_reference = .true.
750 IF (native_gapw_composite_reference .AND. native_gapw_atom_composite_reference)
THEN
751 CALL cp_abort(__location__, &
752 "Select only one native-grid GAPW composite reference route.")
754 native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
757 IF (
ASSOCIATED(gauxc_section))
THEN
759 l_val=native_grid_diagnostics)
761 IF ((.NOT. do_kpoints) .AND. nimages /= 1)
THEN
762 CALL cp_abort(__location__, &
763 "Native SKALA grid evaluation supports multiple images only "// &
764 "for k-point calculations.")
767 cpassert(
ASSOCIATED(kpoints))
769 cpassert(
ASSOCIATED(gauxc_section))
771 IF (.NOT. native_grid_use_cuda)
THEN
772 IF (para_env%mepos == 0 .AND. .NOT. native_grid_cpu_kpoints_warned)
THEN
773 CALL cp_warn(__location__, &
774 "Native SKALA grid evaluation with k-points is using the CPU TorchScript "// &
775 "path. Use a mutually compatible BLAS, ScaLAPACK, OpenMP, and LibTorch "// &
776 "runtime stack, or select NATIVE_GRID_USE_CUDA T.")
777 native_grid_cpu_kpoints_warned = .true.
781 IF (dft_control%roks)
THEN
782 cpabort(
"Native SKALA grid evaluation does not support ROKS.")
784 IF (dft_control%do_admm)
THEN
785 cpabort(
"Native SKALA grid evaluation does not support ADMM.")
790 native_skala_restore_exc = calculate_forces
791 IF (native_skala_restore_exc)
THEN
792 native_skala_exc_scf = energy%exc
793 native_skala_total_scf = energy%total
795 IF (calculate_forces)
THEN
796 ALLOCATE (native_skala_atom_force(3, natom))
797 native_skala_atom_force = 0.0_dp
798 IF (native_gapw_atom_composite_reference .OR. &
799 native_direct_valence_atom_composite)
THEN
800 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
801 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
802 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
803 just_energy=just_energy_xc, qs_env_external=qs_env, &
804 native_gapw_composite_override=native_gapw_composite_reference, &
805 native_skala_defer_to_atom_composite=.true.)
807 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
808 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
809 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
810 just_energy=just_energy_xc, &
811 native_skala_atom_force=native_skala_atom_force, &
812 qs_env_external=qs_env, &
813 native_gapw_composite_override=native_gapw_composite_reference)
815 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
816 ASSOCIATED(v_rspace_new) .AND.
ASSOCIATED(v_tau_rspace))
THEN
817 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
819 IF (output_unit > 0)
THEN
821 WRITE (unit=output_unit, fmt=
"(T2,A,1X,I0,3(1X,ES20.12))") &
822 "SKALA_GPW| Native regular-grid atom force", iatom, &
823 native_skala_atom_force(:, iatom)
828 native_skala_composite_virial = 0.0_dp
830 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
831 atom_force=native_skala_atom_force, &
832 strain_virial=native_skala_composite_virial, &
833 one_center_contraction=native_skala_composite_contraction, &
834 one_center_rho_contraction=native_skala_composite_rho_contraction, &
835 one_center_tau_contraction=native_skala_composite_tau_contraction)
836 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
838 IF (output_unit > 0)
THEN
839 WRITE (unit=output_unit, fmt=
"(T2,A)") &
840 "SKALA_GPW| Composite-direction one-center virial"
842 WRITE (unit=output_unit, fmt=
"(T2,A,1X,3ES20.10)") &
843 "SKALA_GPW|", native_skala_composite_virial(iatom, :)
847 virial%pv_xc = virial%pv_xc + native_skala_composite_virial
850 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
851 atom_force=native_skala_atom_force, &
852 one_center_contraction=native_skala_composite_contraction, &
853 one_center_rho_contraction=native_skala_composite_rho_contraction, &
854 one_center_tau_contraction=native_skala_composite_tau_contraction)
857 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
859 IF (output_unit > 0)
THEN
860 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
861 "SKALA_GPW| Composite-direction one-center VXC contraction", &
862 native_skala_composite_contraction
863 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
864 "SKALA_GPW| Composite-direction one-center rho contraction", &
865 native_skala_composite_rho_contraction
866 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
867 "SKALA_GPW| Composite-direction one-center tau contraction", &
868 native_skala_composite_tau_contraction
870 WRITE (unit=output_unit, fmt=
"(T2,A,1X,I0,3(1X,ES20.12))") &
871 "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
875 cpassert(
ASSOCIATED(force))
876 cpassert(
ASSOCIATED(atomic_kind_set))
879 ikind = kind_of(iatom)
880 atom_a = atom_of_kind(iatom)
881 force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
882 native_skala_atom_force(:, iatom)
884 DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
886 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
887 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
888 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
889 just_energy=just_energy_xc, qs_env_external=qs_env, &
890 native_gapw_composite_override=native_gapw_composite_reference, &
891 native_skala_defer_to_atom_composite= &
892 (native_gapw_atom_composite_reference .OR. &
893 native_direct_valence_atom_composite))
894 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
895 ASSOCIATED(v_rspace_new) .AND.
ASSOCIATED(v_tau_rspace))
THEN
897 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
898 one_center_contraction=native_skala_composite_contraction, &
899 one_center_rho_contraction=native_skala_composite_rho_contraction, &
900 one_center_tau_contraction=native_skala_composite_tau_contraction)
901 IF (native_grid_diagnostics .AND. para_env%mepos == 0)
THEN
903 IF (output_unit > 0)
THEN
904 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
905 "SKALA_GPW| Composite-direction one-center VXC contraction", &
906 native_skala_composite_contraction
907 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
908 "SKALA_GPW| Composite-direction one-center rho contraction", &
909 native_skala_composite_rho_contraction
910 WRITE (unit=output_unit, fmt=
"(T2,A,1X,ES20.10)") &
911 "SKALA_GPW| Composite-direction one-center tau contraction", &
912 native_skala_composite_tau_contraction
917 IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
918 IF (native_direct_valence_atom_composite)
THEN
919 cpassert(
ASSOCIATED(v_rspace_new))
920 cpassert(
ASSOCIATED(v_tau_rspace))
922 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
923 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
924 composite_vxc_tau=v_tau_rspace, direct_valence_atom_grid=.true.)
926 ELSE IF ((gapw .OR. gapw_xc) .AND. .NOT. native_gapw_composite_reference)
THEN
927 IF (native_gapw_atom_composite_reference)
THEN
928 cpassert(
ASSOCIATED(v_rspace_new))
929 cpassert(
ASSOCIATED(v_tau_rspace))
931 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
932 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
933 composite_vxc_tau=v_tau_rspace, atom_composite_grid=.true.)
937 xc_section_external=xc_section, &
938 calculate_forces=calculate_forces)
941 IF (edisp /= 0.0_dp) energy%dispersion = edisp
942 IF (qs_env%requires_matrix_vxc .AND.
ASSOCIATED(v_rspace_new))
THEN
945 matrix_vxc_kp=matrix_vxc_kp, &
946 gapw_full_basis=native_gapw_composite_direct_ao)
947 CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc_kp)
950 matrix_vxc=matrix_vxc, &
951 gapw_full_basis=native_gapw_composite_direct_ao)
952 CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
956 use_gauxc_matrix = .true.
957 CALL apply_gauxc(qs_env, xc_section, calculate_forces)
960 xc_section_external=xc_section, &
961 calculate_forces=calculate_forces)
965 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
966 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
967 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
968 just_energy=just_energy_xc)
969 IF (edisp /= 0.0_dp) energy%dispersion = edisp
970 IF (qs_env%requires_matrix_vxc .AND.
ASSOCIATED(v_rspace_new))
THEN
972 CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
975 IF (gapw .OR. gapw_xc)
THEN
977 xc_section_external=xc_section, &
978 calculate_forces=calculate_forces)
983 IF (gapw .AND. dft_control%qs_control%cdft)
THEN
984 ALLOCATE (cdft_gapw_values(
SIZE(cdft_control%group)))
985 IF (cdft_control%atomic_charges)
THEN
986 ALLOCATE (cdft_charge_correction(natom, nspins), &
987 cdft_electronic_charge(cdft_control%natoms, nspins))
989 cdft_gapw_values, cdft_charge_correction)
991 DO iatom = 1, cdft_control%natoms
992 cdft_electronic_charge(iatom, ispin) = &
993 pw_integral_ab(cdft_control%charge(iatom), rho_r(ispin), local_only=.true.)
996 CALL para_env%sum(cdft_electronic_charge)
998 DO iatom = 1, cdft_control%natoms
999 cdft_electronic_charge(iatom, ispin) = &
1000 cdft_electronic_charge(iatom, ispin) + &
1001 cdft_charge_correction(cdft_control%atoms(iatom), ispin)
1007 cdft_control%value = cdft_control%value + cdft_gapw_values
1008 energy%cdft = energy%cdft + dot_product(cdft_control%strength, cdft_gapw_values)
1009 IF (cdft_control%atomic_charges)
THEN
1011 DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
1015 DEALLOCATE (cdft_gapw_values)
1018 IF (dft_control%mtlr_dft_with_perturbation .AND. .NOT. just_energy)
THEN
1019 IF (do_adiabatic_rescaling .OR. use_gauxc_matrix)
THEN
1020 cpabort(
"MTLR requires an XC potential on the real-space grid.")
1022 IF (.NOT.
ASSOCIATED(matrix_vhxc))
THEN
1023 ALLOCATE (matrix_vhxc(nspins))
1024 DO ispin = 1, nspins
1025 NULLIFY (matrix_vhxc(ispin)%matrix)
1026 ALLOCATE (matrix_vhxc(ispin)%matrix)
1028 template=ks_matrix(1, 1)%matrix)
1030 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1032 CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
1034 cpassert(
SIZE(matrix_vhxc) == nspins)
1035 DO ispin = 1, nspins
1036 cpassert(
ASSOCIATED(matrix_vhxc(ispin)%matrix))
1039 DO ispin = 1, nspins
1040 CALL auxbas_pw_pool%create_pw(v_hxc_rspace(ispin))
1041 CALL pw_zero(v_hxc_rspace(ispin))
1042 CALL pw_axpy(v_hartree_rspace, v_hxc_rspace(ispin))
1043 IF (
ASSOCIATED(v_rspace_new))
THEN
1044 CALL pw_axpy(v_rspace_new(ispin), &
1045 v_hxc_rspace(ispin), &
1046 v_rspace_new(ispin)%pw_grid%dvol)
1048 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1049 CALL integrate_v_rspace(v_hxc_rspace(ispin), &
1050 hmat=matrix_vhxc(ispin), &
1052 calculate_forces=.false.)
1053 CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
1058 IF (qs_env%harris_method)
THEN
1059 CALL get_qs_env(qs_env, harris_env=harris_env)
1063 NULLIFY (rho_struct)
1064 IF (use_virial .AND. calculate_forces)
THEN
1065 virial%pv_exc = virial%pv_exc - virial%pv_xc
1066 virial%pv_virial = virial%pv_virial - virial%pv_xc
1073 ace_active = .false.
1074 ace_rebuild_frequency = 1
1078 IF (
ASSOCIATED(ace_section))
THEN
1086 IF (ace_active)
THEN
1087 cpabort(
"ACE-HFX for k-points is not implemented yet")
1094 IF (ace_active)
THEN
1096 just_energy, v_rspace_new, v_tau_rspace, &
1097 ace_rebuild_frequency, ext_xc_section=xc_section)
1099 CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1100 just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
1105 IF (do_ppl .AND. calculate_forces)
THEN
1106 cpassert(.NOT. gapw)
1107 DO ispin = 1, nspins
1108 CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
1113 IF (
ASSOCIATED(rho_nlcc) .AND. calculate_forces .AND. &
1114 .NOT. native_gapw_atom_composite_reference .AND. &
1115 .NOT. native_direct_valence_atom_composite)
THEN
1116 DO ispin = 1, nspins
1117 CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
1118 IF (dft_control%do_admm)
CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
1123 IF (dft_control%qs_control%do_kg .AND. just_energy)
THEN
1125 cpassert(nimages == 1)
1126 ksmat => ks_matrix(:, 1)
1127 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1130 energy%exc = energy%exc - ekin_mol
1135 IF (.NOT. just_energy)
THEN
1136 IF (calculate_forces)
THEN
1139 (poisson_env%parameters%dielectric_params%dielec_core_correction))
THEN
1142 CALL auxbas_pw_pool%create_pw(v_minus_veps)
1143 CALL pw_copy(v_hartree_rspace, v_minus_veps)
1144 CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
1145 CALL integrate_v_core_rspace(v_minus_veps, qs_env)
1146 CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
1149 CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
1153 IF (.NOT. do_hfx)
THEN
1156 DO ispin = 1, nspins
1159 CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
1163 IF (qs_env%run_rtp)
THEN
1164 IF (dft_control%rtp_control%velocity_gauge)
THEN
1165 cpassert(
ASSOCIATED(matrix_h_im))
1166 cpassert(
ASSOCIATED(ks_matrix_im))
1167 DO ispin = 1, nspins
1170 CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
1177 IF (use_virial .AND. calculate_forces)
THEN
1178 pv_loc = virial%pv_virial
1185 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
1186 v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
1187 cdft_control, calculate_forces)
1189 IF (use_gauxc_matrix)
THEN
1191 CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc_kp)
1192 cpassert(
ASSOCIATED(matrix_vxc_kp))
1193 DO ispin = 1, nspins
1195 CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc_kp(ispin, img)%matrix, &
1200 CALL get_qs_env(qs_env=qs_env, matrix_vxc=matrix_vxc)
1201 cpassert(
ASSOCIATED(matrix_vxc))
1202 cpassert(nimages == 1)
1203 DO ispin = 1, nspins
1204 CALL dbcsr_add(ks_matrix(ispin, 1)%matrix, matrix_vxc(ispin)%matrix, 1.0_dp, 1.0_dp)
1209 IF (gapw .OR. gapw_xc)
THEN
1210 IF (calculate_forces)
THEN
1212 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
1214 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
1217 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
1218 .NOT. native_skala_evaluator)
THEN
1219 gauxc_model_none = .false.
1221 IF (
ASSOCIATED(gauxc_section))
THEN
1223 gauxc_model_name = adjustl(gauxc_model_name)
1225 gauxc_model_none = (trim(gauxc_model_name) ==
"" .OR. &
1226 trim(gauxc_model_name) ==
"NONE")
1228 IF (gauxc_model_none .AND. &
1236 IF (dft_control%do_admm)
THEN
1238 xc_section => admm_env%xc_section_aux
1241 IF (admm_env%do_admmp)
THEN
1242 vscale = admm_env%gsi(1)**2
1243 ELSE IF (admm_env%do_admms)
THEN
1244 vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
1251 IF (use_virial .AND. calculate_forces)
THEN
1252 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1254 IF (dft_control%qs_control%do_kg)
THEN
1255 cpassert(nimages == 1)
1256 ksmat => ks_matrix(:, 1)
1258 IF (use_virial .AND. calculate_forces)
THEN
1259 pv_loc = virial%pv_virial
1262 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1264 energy%exc = energy%exc - ekin_mol
1267 IF (use_virial .AND. calculate_forces)
THEN
1270 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1273 virial%pv_exc = virial%pv_exc + virial%pv_xc
1274 virial%pv_virial = virial%pv_virial + virial%pv_xc
1275 virial%pv_xc = 0.0_dp
1282 cpwarn(
"KS matrix no longer correct. Check possible problems with property calculations!")
1286 IF (dft_control%qs_control%ddapc_explicit_potential)
THEN
1287 CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
1288 DEALLOCATE (v_spin_ddapc_rest_r)
1291 IF (calculate_forces .AND. dft_control%qs_control%cdft)
THEN
1292 IF (.NOT. cdft_control%transfer_pot)
THEN
1293 DO iatom = 1,
SIZE(cdft_control%group)
1294 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1295 DEALLOCATE (cdft_control%group(iatom)%weight)
1297 IF (cdft_control%atomic_charges)
THEN
1298 DO iatom = 1, cdft_control%natoms
1299 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1301 DEALLOCATE (cdft_control%charge)
1304 cdft_control%becke_control%cavity_confine)
THEN
1305 IF (.NOT.
ASSOCIATED(cdft_control%becke_control%cavity_mat))
THEN
1306 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1308 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1311 IF (
ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm))
THEN
1312 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1315 IF (
ASSOCIATED(cdft_control%charges_fragment))
DEALLOCATE (cdft_control%charges_fragment)
1316 cdft_control%save_pot = .false.
1317 cdft_control%need_pot = .true.
1318 cdft_control%external_control = .false.
1322 IF (dft_control%do_sccs)
THEN
1323 CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
1324 DEALLOCATE (v_sccs_rspace)
1328 IF (dft_control%apply_external_potential)
THEN
1331 v_qmmm=vee, scale=-1.0_dp)
1336 CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
1337 CALL vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.false., &
1340 energy%core_cneo = 0.0_dp
1341 IF (
ASSOCIATED(local_rho_set%rhoz_cneo_set))
THEN
1342 DO iatom = 1,
SIZE(local_rho_set%rhoz_cneo_set)
1343 energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
1348 IF (gapw .OR. gapw_xc)
THEN
1351 IF (dft_control%do_admm)
THEN
1360 ks_matrix, matrix_s, rho, mulliken_order_p)
1363 IF (dft_control%dft_plus_u)
THEN
1364 IF (just_energy)
THEN
1365 CALL plus_u(qs_env=qs_env)
1367 CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
1370 energy%dft_plus_u = 0.0_dp
1374 DO ispin = 1, nspins
1377 dft_control%qs_control%eps_filter_matrix)
1382 IF (dft_control%do_admm_mo)
THEN
1383 IF (qs_env%run_rtp)
THEN
1388 ELSE IF (dft_control%do_admm_dm)
THEN
1399 IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp)
THEN
1401 cpassert(nimages == 1)
1402 ksmat => ks_matrix(:, 1)
1403 CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
1407 IF (calculate_forces .AND. dft_control%do_admm)
THEN
1416 CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
1417 calculate_forces, auxbas_pw_pool)
1421 calculate_forces, auxbas_pw_pool)
1428 energy, calculate_forces, just_energy)
1433 DO ispin = 1, nspins
1434 ecore_ppl = ecore_ppl +
pw_integral_ab(vppl_rspace, rho_r(ispin))
1436 energy%core = energy%core + ecore_ppl
1441 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
1442 IF (lri_env%ppl_ri)
THEN
1444 DO ispin = 1, nspins
1445 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
1448 energy%core = energy%core + ecore_ppl
1453 energy%total = energy%core_overlap + energy%core_self + energy%core_cneo + energy%core + &
1454 energy%hartree + energy%hartree_1c + energy%exc + energy%exc1 + energy%ex + &
1455 energy%dispersion + energy%gcp + energy%qmmm_el + energy%mulliken + &
1456 sum(energy%ddapc_restraint) + energy%s2_restraint + &
1457 energy%dft_plus_u + energy%kTS + &
1458 energy%efield + energy%efield_core + energy%ee + &
1459 energy%ee_core + energy%exc_aux_fit + energy%image_charge + &
1460 energy%sccs_pol + energy%cdft + energy%exc1_aux_fit
1462 IF (dft_control%apply_embed_pot) energy%total = energy%total + energy%embed_corr
1464 IF (native_skala_restore_exc) energy%total = native_skala_total_scf
1467 cpabort(
"KS energy is an abnormal value (NaN/Inf).")
1475 CALL timestop(handle)
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.