(git:fd7f302)
Loading...
Searching...
No Matches
qs_ks_methods.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief routines that build the Kohn-Sham matrix (i.e calculate the coulomb
10!> and xc parts
11!> \author Fawzi Mohamed
12!> \par History
13!> - 05.2002 moved from qs_scf (see there the history) [fawzi]
14!> - JGH [30.08.02] multi-grid arrays independent from density and potential
15!> - 10.2002 introduced pools, uses updated rho as input,
16!> removed most temporary variables, renamed may vars,
17!> began conversion to LSD [fawzi]
18!> - 10.2004 moved calculate_w_matrix here [Joost VandeVondele]
19!> introduced energy derivative wrt MOs [Joost VandeVondele]
20!> - SCCS implementation (16.10.2013,MK)
21! **************************************************************************************************
33 USE admm_types, ONLY: admm_type,&
37 USE cell_types, ONLY: cell_type
39 USE cp_dbcsr_api, ONLY: &
41 dbcsr_p_type, dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
42 dbcsr_type_symmetric
46 USE cp_ddapc, ONLY: qs_ks_ddapc
47 USE cp_fm_types, ONLY: cp_fm_type
51 USE cp_output_handling, ONLY: cp_p_file,&
53 USE dft_plus_u, ONLY: plus_u
60 USE hfx_admm_utils, ONLY: hfx_admm_init,&
63 USE input_constants, ONLY: do_ppl_grid,&
72 USE kinds, ONLY: default_string_length,&
73 dp
74 USE kpoint_types, ONLY: get_kpoint_info,&
80 USE mathlib, ONLY: abnormal_value
83 USE pw_env_types, ONLY: pw_env_get,&
85 USE pw_methods, ONLY: pw_axpy,&
86 pw_copy,&
89 pw_scale,&
96 USE pw_types, ONLY: pw_c1d_gs_type,&
115 USE qs_integrate_potential, ONLY: integrate_ppl_rspace,&
116 integrate_rho_nlcc,&
117 integrate_v_core_rspace,&
118 integrate_v_rspace
119 USE qs_kind_types, ONLY: qs_kind_type
123 USE qs_ks_atom, ONLY: update_ks_atom
126 USE qs_ks_types, ONLY: qs_ks_env_type,&
128 USE qs_ks_utils, ONLY: &
133 USE qs_mo_types, ONLY: get_mo_set,&
137 USE qs_rho_types, ONLY: qs_rho_get,&
139 USE qs_sccs, ONLY: sccs
140 USE qs_vxc, ONLY: qs_vxc_create
146 USE skala_gpw_functional, ONLY: &
152 USE string_utilities, ONLY: uppercase
155 USE virial_types, ONLY: virial_type
163#include "./base/base_uses.f90"
164
165 IMPLICIT NONE
166
167 PRIVATE
168
169 LOGICAL, PARAMETER :: debug_this_module = .true.
170 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_methods'
171
174
175CONTAINS
176
177! **************************************************************************************************
178!> \brief routine where the real calculations are made: the
179!> KS matrix is calculated
180!> \param qs_env the qs_env to update
181!> \param calculate_forces if true calculate the quantities needed
182!> to calculate the forces. Defaults to false.
183!> \param just_energy if true updates the energies but not the
184!> ks matrix. Defaults to false
185!> \param print_active ...
186!> \param ext_ks_matrix ...
187!> \param ext_xc_section ...
188!> \par History
189!> 06.2002 moved from qs_scf to qs_ks_methods, use of ks_env
190!> new did_change scheme [fawzi]
191!> 10.2002 introduced pools, uses updated rho as input, LSD [fawzi]
192!> 10.2004 build_kohn_sham matrix now also computes the derivatives
193!> of the total energy wrt to the MO coefs, if instructed to
194!> do so. This appears useful for orbital dependent functionals
195!> where the KS matrix alone (however this might be defined)
196!> does not contain the info to construct this derivative.
197!> \author Matthias Krack
198!> \note
199!> make rho, energy and qs_charges optional, defaulting
200!> to qs_env components?
201! **************************************************************************************************
202 SUBROUTINE qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, &
203 print_active, ext_ks_matrix, ext_xc_section)
204 TYPE(qs_environment_type), POINTER :: qs_env
205 LOGICAL, INTENT(in) :: calculate_forces, just_energy
206 LOGICAL, INTENT(IN), OPTIONAL :: print_active
207 TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
208 POINTER :: ext_ks_matrix
209 TYPE(section_vals_type), OPTIONAL, POINTER :: ext_xc_section
210
211 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_ks_build_kohn_sham_matrix'
212
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, &
230 vscale
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, &
236 pv_loc
237 TYPE(admm_type), POINTER :: admm_env
238 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
239 TYPE(cdft_control_type), POINTER :: cdft_control
240 TYPE(cell_type), POINTER :: cell
241 TYPE(cp_logger_type), POINTER :: logger
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, &
245 my_rho, rho_ao
246 TYPE(dft_control_type), POINTER :: dft_control
247 TYPE(ecoul_1center_type), DIMENSION(:), POINTER :: ecoul_1c
248 TYPE(harris_type), POINTER :: harris_env
249 TYPE(kpoint_type), POINTER :: kpoints
250 TYPE(local_rho_type), POINTER :: local_rho_set
251 TYPE(lri_density_type), POINTER :: lri_density
252 TYPE(lri_environment_type), POINTER :: lri_env
253 TYPE(lri_kind_type), DIMENSION(:), POINTER :: lri_v_int
254 TYPE(mp_para_env_type), POINTER :: para_env
255 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
256 POINTER :: sab_orb
257 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
258 TYPE(pw_c1d_gs_type) :: rho_tot_gspace, v_hartree_gspace
259 TYPE(pw_c1d_gs_type), POINTER :: rho_core
260 TYPE(pw_env_type), POINTER :: pw_env
261 TYPE(pw_poisson_type), POINTER :: poisson_env
262 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
263 TYPE(pw_r3d_rs_type), DIMENSION(2) :: v_hxc_rspace
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, &
266 v_tau_rspace_aux_fit
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
269 TYPE(qs_energy_type), POINTER :: energy
270 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
271 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
272 TYPE(qs_ks_env_type), POINTER :: ks_env
273 TYPE(qs_rho_type), POINTER :: rho, rho1, rho_struct, rho_xc
274 TYPE(section_vals_type), POINTER :: ace_section, &
275 adiabatic_rescaling_section, &
276 gauxc_section, hfx_sections, input, &
277 scf_section, xc_section
278 TYPE(virial_type), POINTER :: virial
279
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, &
283 ace_section, &
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)
290
291 cpassert(ASSOCIATED(qs_env))
292
293 logger => cp_get_default_logger()
294 my_print = .true.
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.
312
313 CALL get_qs_env(qs_env, &
314 ks_env=ks_env, &
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, &
324 pw_env=pw_env, &
325 cell=cell, &
326 atomic_kind_set=atomic_kind_set, &
327 para_env=para_env, &
328 input=input, &
329 virial=virial, &
330 v_hartree_rspace=v_hartree_rspace, &
331 vee=vee, &
332 rho_nlcc=rho_nlcc, &
333 rho=rho, &
334 rho_core=rho_core, &
335 rho_xc=rho_xc, &
336 sab_orb=sab_orb, &
337 energy=energy, &
338 force=force, &
339 kpoints=kpoints, &
340 do_kpoints=do_kpoints, &
341 particle_set=particle_set, &
342 qs_kind_set=qs_kind_set, &
343 natom=natom)
344
345 CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
346
347 nimages = dft_control%nimages
348 nspins = dft_control%nspins
349
350 ! remap pointer to allow for non-kpoint external ks matrix
351 IF (PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
352
353 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
354
355 adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
356 CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
357 just_energy_xc = just_energy
358 IF (do_adiabatic_rescaling) THEN
359 !! If we perform adiabatic rescaling, the xc potential has to be scaled by the xc- and
360 !! HFX-energy. Thus, let us first calculate the energy
361 just_energy_xc = .true.
362 END IF
363
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)
369 dokp = (nimages > 1)
370
371 ! Setup the possible usage of DDAPC charges
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
376
377 ! Check if LRIGPW is used
378 lrigpw = dft_control%qs_control%lrigpw
379 rigpw = dft_control%qs_control%rigpw
380 IF (rigpw) THEN
381 cpassert(nimages == 1)
382 END IF
383 IF (lrigpw .AND. rigpw) THEN
384 cpabort(" LRI and RI are not compatible")
385 END IF
386
387 ! Check for GAPW method : additional terms for local densities
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")
392 END IF
393 IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw)) THEN
394 cpabort(" GAPW/GAPW_XC and LRIGPW are not compatible")
395 END IF
396 IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw)) THEN
397 cpabort(" GAPW/GAPW_XC and RIGPW are not compatible")
398 END IF
399
400 do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
401 IF (do_ppl) THEN
402 cpassert(.NOT. gapw)
403 CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
404 END IF
405
406 IF (gapw_xc) THEN
407 cpassert(ASSOCIATED(rho_xc))
408 END IF
409
410 ! gets the tmp grids
411 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
412
413 IF (gapw .AND. (poisson_env%parameters%solver == pw_poisson_implicit)) THEN
414 cpabort("The implicit Poisson solver cannot be used in conjunction with GAPW.")
415 END IF
416
417 ! *** Prepare densities for gapw ***
418 IF (gapw .OR. gapw_xc) THEN
419 CALL prepare_gapw_den(qs_env, do_rho0=(.NOT. gapw_xc))
420 END IF
421
422 ! Calculate the Hartree potential
423 CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
424 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
425
426 scf_section => section_vals_get_subs_vals(input, "DFT%SCF")
427 IF (btest(cp_print_key_should_output(logger%iter_info, scf_section, &
428 "PRINT%DETAILED_ENERGY"), &
429 cp_p_file) .AND. &
430 (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
431 (.NOT. (poisson_env%parameters%solver == pw_poisson_implicit))) THEN
432 CALL pw_zero(rho_tot_gspace)
433 CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density=.true.)
434 CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%e_hartree, &
435 v_hartree_gspace)
436 CALL pw_zero(rho_tot_gspace)
437 CALL pw_zero(v_hartree_gspace)
438 END IF
439
440 ! Get the total density in g-space [ions + electrons]
441 CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
442
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.")
445 END IF
446
447 ! Add the planar counter charge density
448 IF (dft_control%do_pcc) THEN
449 CALL planar_counter_charge(rho_tot_gspace, dft_control%pcc_control, auxbas_pw_pool)
450 END IF
451
452 IF (my_print) THEN
453 CALL print_densities(qs_env, rho)
454 END IF
455
456 IF (dft_control%do_sccs) THEN
457 ! Self-consistent continuum solvation (SCCS) model
458 NULLIFY (v_sccs_rspace)
459 ALLOCATE (v_sccs_rspace)
460 CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
461
462 IF (poisson_env%parameters%solver == pw_poisson_implicit) THEN
463 cpabort("The implicit Poisson solver cannot be used together with SCCS.")
464 END IF
465
466 IF (use_virial .AND. calculate_forces) THEN
467 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
468 h_stress=h_stress)
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)
471 ELSE
472 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
473 END IF
474 ELSE
475 ! Getting the Hartree energy and Hartree potential. Also getting the stress tensor
476 ! from the Hartree term if needed. No nuclear force information here
477 IF (use_virial .AND. calculate_forces) THEN
478 h_stress(:, :) = 0.0_dp
479 CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
480 v_hartree_gspace, h_stress=h_stress, &
481 rho_core=rho_core)
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)
484 ELSE
485 CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
486 v_hartree_gspace, rho_core=rho_core)
487 END IF
488 END IF
489
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)
492 CALL planar_averaged_v_hartree_3d(v_hartree_rspace, dft_control, qs_env%scf_control%gce%do_gce, &
493 qs_env%scf_control%gce%ref_esp, para_env)
494 END IF
495
496 ! In case decouple periodic images and/or apply restraints to charges
497 IF (do_ddapc) THEN
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, &
500 just_energy)
501 ELSE
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)
507 END IF
508 END IF
509 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
510
511 IF (dft_control%correct_surf_dip) THEN
512 IF (dft_control%surf_dip_correct_switch) THEN
513 CALL calc_dipsurf_potential(qs_env, energy)
514 energy%hartree = energy%hartree + energy%surf_dipole
515 END IF
516 END IF
517
518 ! SIC
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)
521
522 ! Check if CDFT constraint is needed
523 CALL qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
524
525 ! Adds the External Potential if requested
526 IF (dft_control%apply_external_potential) THEN
527 ! Compute the energy due to the external potential
528 ee_ener = 0.0_dp
529 DO ispin = 1, nspins
530 ee_ener = ee_ener + pw_integral_ab(rho_r(ispin), vee)
531 END DO
532 IF (.NOT. just_energy) THEN
533 IF (gapw) THEN
534 CALL get_qs_env(qs_env=qs_env, &
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)
540 END IF
541 ee_ener = ee_ener + pw_integral_ab(rho0_s_rs, vee)
542 IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
543 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
544 END IF
545 END IF
546 END IF
547 ! the sign accounts for the charge of the electrons
548 energy%ee = -ee_ener
549 END IF
550
551 ! Adds the QM/MM potential
552 IF (qs_env%qmmm) THEN
553 CALL qmmm_calculate_energy(qs_env=qs_env, &
554 rho=rho_r, &
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
558 CALL calculate_image_pot(v_hartree_rspace=v_hartree_rspace, &
559 rho_hartree_gspace=rho_tot_gspace, &
560 energy=energy, &
561 qmmm_env=qs_env%qmmm_env_qm, &
562 qs_env=qs_env)
563 IF (.NOT. just_energy) THEN
564 CALL add_image_pot_to_hartree_pot(v_hartree=v_hartree_rspace, &
565 v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
566 qs_env=qs_env)
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)
572 END IF
573 END IF
574 CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
575 DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
576 END IF
577 IF (.NOT. just_energy) THEN
578 CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
579 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
580 END IF
581 END IF
582 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
583
584 ! SMEAGOL interface
585 IF (dft_control%smeagol_control%smeagol_enabled .AND. &
586 dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
587 cpassert(ASSOCIATED(dft_control%smeagol_control%aux))
588 CALL smeagol_shift_v_hartree(v_hartree_rspace, cell, &
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)
597 END IF
598
599 ! calculate the density matrix for the fitted mo_coeffs
600 IF (dft_control%do_admm) THEN
601 IF (PRESENT(ext_xc_section)) THEN
602 CALL hfx_admm_init(qs_env, calculate_forces, ext_xc_section)
603 ELSE
604 CALL hfx_admm_init(qs_env, calculate_forces)
605 END IF
606
607 IF (dft_control%do_admm_mo) THEN
608 IF (qs_env%run_rtp) THEN
609 CALL rtp_admm_calc_rho_aux(qs_env)
610 ELSE
611 IF (dokp) THEN
612 CALL admm_mo_calc_rho_aux_kp(qs_env)
613 ELSE
614 CALL admm_mo_calc_rho_aux(qs_env)
615 END IF
616 END IF
617 ELSE IF (dft_control%do_admm_dm) THEN
618 CALL admm_dm_calc_rho_aux(qs_env)
619 END IF
620 END IF
621
622 ! only activate stress calculation if
623 IF (use_virial .AND. calculate_forces) virial%pv_calculate = .true.
624
625 ! *** calculate the xc potential on the pw density ***
626 ! *** associates v_rspace_new if the xc potential needs to be computed.
627 ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
628 IF (dft_control%do_admm) THEN
629 CALL get_qs_env(qs_env, admm_env=admm_env)
630 xc_section => admm_env%xc_section_aux
631 CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
632
633 ! here we ignore a possible vdW section in 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)
637
638 IF (admm_env%do_gapw) THEN
639 !compute the potential due to atomic densities
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)
645
646 END IF
647
648 NULLIFY (rho_struct)
649
650 IF (use_virial .AND. calculate_forces) THEN
651 vscale = 1.0_dp
652 !Note: ADMMS and ADMMP stress tensor only for closed-shell calculations
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
657 ! virial%pv_xc will be zeroed in the xc routines
658 END IF
659 xc_section => admm_env%xc_section_primary
660 ELSE
661 xc_section => section_vals_get_subs_vals(input, "DFT%XC")
662 ! build ks matrix with an xc section potentially different from the one defined in input
663 IF (PRESENT(ext_xc_section)) xc_section => ext_xc_section
664 END IF
665
666 IF (gapw_xc) THEN
667 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
668 ELSE
669 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
670 END IF
671
672 ! zmp
673 IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc) THEN
674 energy%exc = 0.0_dp
675 CALL calculate_zmp_potential(qs_env, v_rspace_new, rho, exc=energy%exc)
676 ELSE
677 ! Embedding potential (runs regardless of XC method)
678 IF (dft_control%apply_embed_pot) THEN
679 NULLIFY (v_rspace_embed)
680 energy%embed_corr = 0.0_dp
681 CALL get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, &
682 energy%embed_corr, just_energy)
683 END IF
684
685 ! Everything else, either via GauXC or manual XC computation
686 native_skala_evaluator = xc_section_uses_native_skala_grid(xc_section)
687 paw_composite_representation = .false.
688 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc)) THEN
689 paw_composite_representation = xc_section_uses_gauxc_model(xc_section) .AND. &
692 native_skala_evaluator = native_skala_evaluator .OR. &
693 paw_composite_representation
694 END IF
695 IF (dft_control%use_gauxc) THEN
696 IF (native_skala_evaluator) THEN
697 CALL ensure_native_skala_grid_scope(xc_section)
698 gapw_representation = skala_gapw_representation(xc_section)
699 IF (gapw .OR. gapw_xc) THEN
700 gapw_all_all_electron = gauxc_gapw_all_all_electron(qs_kind_set)
701 gapw_all_pseudopotentials = gauxc_gapw_all_pseudopotentials(qs_kind_set)
702 gapw_mixed_core_representations = &
703 .NOT. gapw_all_all_electron .AND. .NOT. gapw_all_pseudopotentials
704 END IF
705 native_direct_valence_atom_composite = &
706 xc_section_uses_native_skala_grid(xc_section) .AND. &
707 native_skala_uses_atom_composite_grid(xc_section) .AND. &
708 ((.NOT. gapw .AND. .NOT. gapw_xc) .OR. &
709 (gapw_representation == skala_gapw_direct_valence .AND. &
710 gapw_all_pseudopotentials))
711 native_gapw_composite_reference = &
712 native_skala_gapw_composite_reference(xc_section) .AND. (gapw .OR. gapw_xc)
713 native_gapw_atom_composite_requested = &
715 (gapw .OR. gapw_xc)
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.")
726 END IF
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
745 ELSE
746 native_gapw_composite_reference = .false.
747 native_gapw_atom_composite_reference = .true.
748 END IF
749 END IF
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.")
753 END IF
754 native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
756 gauxc_section => get_gauxc_section(xc_section)
757 IF (ASSOCIATED(gauxc_section)) THEN
758 CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_DIAGNOSTICS", &
759 l_val=native_grid_diagnostics)
760 END IF
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.")
765 END IF
766 IF (do_kpoints) THEN
767 cpassert(ASSOCIATED(kpoints))
768 gauxc_section => get_gauxc_section(xc_section)
769 cpassert(ASSOCIATED(gauxc_section))
770 CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_USE_CUDA", l_val=native_grid_use_cuda)
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.
778 END IF
779 END IF
780 END IF
781 IF (dft_control%roks) THEN
782 cpabort("Native SKALA grid evaluation does not support ROKS.")
783 END IF
784 IF (dft_control%do_admm) THEN
785 cpabort("Native SKALA grid evaluation does not support ADMM.")
786 END IF
787 ! Force and stress rebuilds re-enter this path for derivatives and VXC only. The
788 ! autograd virial already contains the rebuilt XC volume term when qs_vxc_create
789 ! returns, so the externally reported energy must be restored to its SCF value.
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
794 END IF
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.)
806 ELSE
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)
814 END IF
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
818 output_unit = cp_logger_get_default_io_unit()
819 IF (output_unit > 0) THEN
820 DO iatom = 1, natom
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)
824 END DO
825 END IF
826 END IF
827 IF (use_virial) THEN
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
837 output_unit = cp_logger_get_default_io_unit()
838 IF (output_unit > 0) THEN
839 WRITE (unit=output_unit, fmt="(T2,A)") &
840 "SKALA_GPW| Composite-direction one-center virial"
841 DO iatom = 1, 3
842 WRITE (unit=output_unit, fmt="(T2,A,1X,3ES20.10)") &
843 "SKALA_GPW|", native_skala_composite_virial(iatom, :)
844 END DO
845 END IF
846 END IF
847 virial%pv_xc = virial%pv_xc + native_skala_composite_virial
848 ELSE
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)
855 END IF
856 END IF
857 IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
858 output_unit = cp_logger_get_default_io_unit()
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
869 DO iatom = 1, natom
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)
872 END DO
873 END IF
874 END IF
875 cpassert(ASSOCIATED(force))
876 cpassert(ASSOCIATED(atomic_kind_set))
877 CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
878 DO iatom = 1, natom
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)
883 END DO
884 DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
885 ELSE
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
902 output_unit = cp_logger_get_default_io_unit()
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
913 END IF
914 END IF
915 END IF
916 END IF
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))
921 CALL calculate_vxc_atom( &
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.)
925 energy%exc = 0.0_dp
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))
930 CALL calculate_vxc_atom( &
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.)
934 energy%exc = 0.0_dp
935 ELSE
936 CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
937 xc_section_external=xc_section, &
938 calculate_forces=calculate_forces)
939 END IF
940 END IF
941 IF (edisp /= 0.0_dp) energy%dispersion = edisp
942 IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
943 IF (do_kpoints) THEN
944 CALL compute_matrix_vxc_kp(qs_env=qs_env, v_rspace=v_rspace_new, &
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)
948 ELSE
949 CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, &
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)
953 END IF
954 END IF
955 ELSE
956 use_gauxc_matrix = .true.
957 CALL apply_gauxc(qs_env, xc_section, calculate_forces)
958 IF (gapw_xc .OR. (gapw .AND. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
959 CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
960 xc_section_external=xc_section, &
961 calculate_forces=calculate_forces)
962 END IF
963 END IF
964 ELSE
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
971 CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
972 CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
973 END IF
974
975 IF (gapw .OR. gapw_xc) THEN
976 CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
977 xc_section_external=xc_section, &
978 calculate_forces=calculate_forces)
979 END IF
980 END IF
981 END IF
982
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))
988 CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, &
989 cdft_gapw_values, cdft_charge_correction)
990 DO ispin = 1, nspins
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.)
994 END DO
995 END DO
996 CALL para_env%sum(cdft_electronic_charge)
997 DO ispin = 1, nspins
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)
1002 END DO
1003 END DO
1004 ELSE
1005 CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, cdft_gapw_values)
1006 END IF
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
1010 CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
1011 DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
1012 ELSE
1013 CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
1014 END IF
1015 DEALLOCATE (cdft_gapw_values)
1016 END IF
1017
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.")
1021 END IF
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)
1027 CALL dbcsr_create(matrix_vhxc(ispin)%matrix, &
1028 template=ks_matrix(1, 1)%matrix)
1029 CALL cp_dbcsr_alloc_block_from_nbl(matrix_vhxc(ispin)%matrix, sab_orb)
1030 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1031 END DO
1032 CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
1033 ELSE
1034 cpassert(SIZE(matrix_vhxc) == nspins)
1035 DO ispin = 1, nspins
1036 cpassert(ASSOCIATED(matrix_vhxc(ispin)%matrix))
1037 END DO
1038 END IF
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)
1047 END IF
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), &
1051 qs_env=qs_env, &
1052 calculate_forces=.false.)
1053 CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
1054 END DO
1055 END IF
1056
1057 ! set hartree and xc potentials for use in Harris method
1058 IF (qs_env%harris_method) THEN
1059 CALL get_qs_env(qs_env, harris_env=harris_env)
1060 CALL harris_set_potentials(harris_env, v_hartree_rspace, v_rspace_new)
1061 END IF
1062
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
1067 END IF
1068
1069 ! *** Add Hartree-Fock contribution if required ***
1070 hfx_sections => section_vals_get_subs_vals(xc_section, "HF")
1071 CALL section_vals_get(hfx_sections, explicit=do_hfx)
1072
1073 ace_active = .false.
1074 ace_rebuild_frequency = 1
1075
1076 IF (do_hfx) THEN
1077 ace_section => section_vals_get_subs_vals(hfx_sections, "ACE")
1078 IF (ASSOCIATED(ace_section)) THEN
1079 CALL section_vals_val_get(ace_section, "ACTIVE", l_val=ace_active)
1080 CALL section_vals_val_get(ace_section, "REBUILD_FREQUENCY", i_val=ace_rebuild_frequency)
1081 END IF
1082 END IF
1083
1084 IF (do_hfx) THEN
1085 IF (dokp) THEN
1086 IF (ace_active) THEN
1087 cpabort("ACE-HFX for k-points is not implemented yet")
1088 ELSE
1089 CALL hfx_ks_matrix_kp(qs_env, ks_matrix, energy, calculate_forces)
1090 END IF
1091
1092 ELSE
1093 ! ext_xc_section may contain a hfx section
1094 IF (ace_active) THEN
1095 CALL hfx_ace_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1096 just_energy, v_rspace_new, v_tau_rspace, &
1097 ace_rebuild_frequency, ext_xc_section=xc_section)
1098 ELSE
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)
1101 END IF
1102 END IF
1103 END IF !do_hfx
1104
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)
1109 END DO
1110 END IF
1111
1112 ! The atom-composite evaluator differentiates its atom-centered NLCC field directly.
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)
1119 END DO
1120 END IF
1121
1122 ! calculate KG correction
1123 IF (dft_control%qs_control%do_kg .AND. just_energy) THEN
1124
1125 cpassert(nimages == 1)
1126 ksmat => ks_matrix(:, 1)
1127 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1128
1129 ! subtract kg corr from the total energy
1130 energy%exc = energy%exc - ekin_mol
1131
1132 END IF
1133
1134 ! *** Single atom contributions ***
1135 IF (.NOT. just_energy) THEN
1136 IF (calculate_forces) THEN
1137 ! Getting nuclear force contribution from the core charge density
1138 IF ((poisson_env%parameters%solver == pw_poisson_implicit) .AND. &
1139 (poisson_env%parameters%dielectric_params%dielec_core_correction)) THEN
1140 block
1141 TYPE(pw_r3d_rs_type) :: v_minus_veps
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)
1147 END block
1148 ELSE
1149 CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
1150 END IF
1151 END IF
1152
1153 IF (.NOT. do_hfx) THEN
1154 ! Initialize the Kohn-Sham matrix with the core Hamiltonian matrix
1155 ! (sets ks sparsity equal to matrix_h sparsity)
1156 DO ispin = 1, nspins
1157 DO img = 1, nimages
1158 CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name) ! keep the name
1159 CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
1160 END DO
1161 END DO
1162 ! imaginary part if required
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
1168 DO img = 1, nimages
1169 CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name) ! keep the name
1170 CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
1171 END DO
1172 END DO
1173 END IF
1174 END IF
1175 END IF
1176
1177 IF (use_virial .AND. calculate_forces) THEN
1178 pv_loc = virial%pv_virial
1179 END IF
1180 ! sum up potentials and integrate
1181 ! Pointing my_rho to the density matrix rho_ao
1182 my_rho => rho_ao
1183
1184 CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
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)
1188
1189 IF (use_gauxc_matrix) THEN
1190 IF (dokp) 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
1194 DO img = 1, nimages
1195 CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc_kp(ispin, img)%matrix, &
1196 1.0_dp, 1.0_dp)
1197 END DO
1198 END DO
1199 ELSE
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)
1205 END DO
1206 END IF
1207 END IF
1208
1209 IF (gapw .OR. gapw_xc) THEN
1210 IF (calculate_forces) THEN
1211 IF (gapw_xc) THEN
1212 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
1213 ELSE
1214 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
1215 END IF
1216 NULLIFY (rho1)
1217 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
1218 .NOT. native_skala_evaluator) THEN
1219 gauxc_model_none = .false.
1220 gauxc_section => get_gauxc_section(xc_section)
1221 IF (ASSOCIATED(gauxc_section)) THEN
1222 CALL section_vals_val_get(gauxc_section, "MODEL", c_val=gauxc_model_name)
1223 gauxc_model_name = adjustl(gauxc_model_name)
1224 CALL uppercase(gauxc_model_name)
1225 gauxc_model_none = (trim(gauxc_model_name) == "" .OR. &
1226 trim(gauxc_model_name) == "NONE")
1227 END IF
1228 IF (gauxc_model_none .AND. &
1229 (gapw_xc .OR. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
1230 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1231 END IF
1232 ELSE
1233 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1234 END IF
1235 !
1236 IF (dft_control%do_admm) THEN
1237 CALL get_qs_env(qs_env, admm_env=admm_env)
1238 xc_section => admm_env%xc_section_aux
1239 CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
1240 vscale = 1.0_dp
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)
1245 END IF
1246 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
1247 END IF
1248 END IF
1249 END IF
1250
1251 IF (use_virial .AND. calculate_forces) THEN
1252 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1253 END IF
1254 IF (dft_control%qs_control%do_kg) THEN
1255 cpassert(nimages == 1)
1256 ksmat => ks_matrix(:, 1)
1257
1258 IF (use_virial .AND. calculate_forces) THEN
1259 pv_loc = virial%pv_virial
1260 END IF
1261
1262 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1263 ! subtract kg corr from the total energy
1264 energy%exc = energy%exc - ekin_mol
1265
1266 ! virial corrections
1267 IF (use_virial .AND. calculate_forces) THEN
1268
1269 ! Integral contribution
1270 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1271
1272 ! GGA contribution
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
1276 END IF
1277 END IF
1278
1279 ELSE
1280 IF (do_hfx) THEN
1281 IF (.false.) THEN
1282 cpwarn("KS matrix no longer correct. Check possible problems with property calculations!")
1283 END IF
1284 END IF
1285 END IF ! .NOT. just energy
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)
1289 END IF
1290
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)
1296 END DO
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))
1300 END DO
1301 DEALLOCATE (cdft_control%charge)
1302 END IF
1303 IF (cdft_control%type == outer_scf_becke_constraint .AND. &
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)
1307 ELSE
1308 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1309 END IF
1310 ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
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)
1313 END IF
1314 END IF
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.
1319 END IF
1320 END IF
1321
1322 IF (dft_control%do_sccs) THEN
1323 CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
1324 DEALLOCATE (v_sccs_rspace)
1325 END IF
1326
1327 IF (gapw) THEN
1328 IF (dft_control%apply_external_potential) THEN
1329 ! Integrals of the Hartree potential with g0_soft
1330 CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
1331 v_qmmm=vee, scale=-1.0_dp)
1332 END IF
1333 CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
1334 ! Place Vh_1c_gg_integrals after integrate_vhg0_rspace for CNEO calculations
1335 ! because vhg0 integral is needed to build the complete nuclear equation
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., &
1338 core_2nd=.false.)
1339 ! CNEO quantum nuclear core energy (kinetic + Z*erfc(r)/r potential from classical nuclei)
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
1344 END DO
1345 END IF
1346 END IF
1347
1348 IF (gapw .OR. gapw_xc) THEN
1349 ! Single atom contributions in the KS matrix ***
1350 CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
1351 IF (dft_control%do_admm) THEN
1352 !Single atom contribution to the AUX matrices
1353 !Note: also update ks_aux_fit matrix in case of rtp
1354 CALL admm_update_ks_atom(qs_env, calculate_forces)
1355 END IF
1356 END IF
1357
1358 !Calculation of Mulliken restraint, if requested
1359 CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
1360 ks_matrix, matrix_s, rho, mulliken_order_p)
1361
1362 ! Add DFT+U contribution, if requested
1363 IF (dft_control%dft_plus_u) THEN
1364 IF (just_energy) THEN
1365 CALL plus_u(qs_env=qs_env)
1366 ELSE
1367 CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
1368 END IF
1369 ELSE
1370 energy%dft_plus_u = 0.0_dp
1371 END IF
1372
1373 ! At this point the ks matrix should be up to date, filter it if requested
1374 DO ispin = 1, nspins
1375 DO img = 1, nimages
1376 CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
1377 dft_control%qs_control%eps_filter_matrix)
1378 END DO
1379 END DO
1380
1381 !** merge the auxiliary KS matrix and the primary one
1382 IF (dft_control%do_admm_mo) THEN
1383 IF (qs_env%run_rtp) THEN
1384 CALL rtp_admm_merge_ks_matrix(qs_env)
1385 ELSE
1386 CALL admm_mo_merge_ks_matrix(qs_env)
1387 END IF
1388 ELSE IF (dft_control%do_admm_dm) THEN
1389 CALL admm_dm_merge_ks_matrix(qs_env)
1390 END IF
1391
1392 ! External field (nonperiodic case)
1393 CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
1394
1395 ! Right now we can compute the orbital derivative here, as it depends currently only on the available
1396 ! Kohn-Sham matrix. This might change in the future, in which case more pieces might need to be assembled
1397 ! from this routine, notice that this part of the calculation in not linear scaling
1398 ! right now this operation is only non-trivial because of occupation numbers and the restricted keyword
1399 IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp) THEN
1400 CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
1401 cpassert(nimages == 1)
1402 ksmat => ks_matrix(:, 1)
1403 CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
1404 END IF
1405
1406 ! ADMM overlap forces
1407 IF (calculate_forces .AND. dft_control%do_admm) THEN
1408 IF (dokp) THEN
1409 CALL calc_admm_ovlp_forces_kp(qs_env)
1410 ELSE
1411 CALL calc_admm_ovlp_forces(qs_env)
1412 END IF
1413 END IF
1414
1415 ! deal with low spin roks
1416 CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
1417 calculate_forces, auxbas_pw_pool)
1418
1419 ! deal with sic on explicit orbitals
1420 CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
1421 calculate_forces, auxbas_pw_pool)
1422
1423 ! Periodic external field
1424 CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
1425
1426 ! adds s2_restraint energy and orbital derivatives
1427 CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
1428 energy, calculate_forces, just_energy)
1429
1430 IF (do_ppl) THEN
1431 ! update core energy for grid based local pseudopotential
1432 ecore_ppl = 0._dp
1433 DO ispin = 1, nspins
1434 ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
1435 END DO
1436 energy%core = energy%core + ecore_ppl
1437 END IF
1438
1439 IF (lrigpw) THEN
1440 ! update core energy for ppl_ri method
1441 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
1442 IF (lri_env%ppl_ri) THEN
1443 ecore_ppl = 0._dp
1444 DO ispin = 1, nspins
1445 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
1446 CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
1447 END DO
1448 energy%core = energy%core + ecore_ppl
1449 END IF
1450 END IF
1451
1452 ! Sum all energy terms to obtain the total energy
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
1461
1462 IF (dft_control%apply_embed_pot) energy%total = energy%total + energy%embed_corr
1463
1464 IF (native_skala_restore_exc) energy%total = native_skala_total_scf
1465
1466 IF (abnormal_value(energy%total)) THEN
1467 cpabort("KS energy is an abnormal value (NaN/Inf).")
1468 END IF
1469
1470 ! Print detailed energy
1471 IF (my_print) THEN
1472 CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
1473 END IF
1474
1475 CALL timestop(handle)
1476
1477 END SUBROUTINE qs_ks_build_kohn_sham_matrix
1478
1479! **************************************************************************************************
1480!> \brief ...
1481!> \param rho_tot_gspace ...
1482!> \param qs_env ...
1483!> \param rho ...
1484!> \param skip_nuclear_density ...
1485! **************************************************************************************************
1486 SUBROUTINE calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
1487 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: rho_tot_gspace
1488 TYPE(qs_environment_type), POINTER :: qs_env
1489 TYPE(qs_rho_type), POINTER :: rho
1490 LOGICAL, INTENT(IN), OPTIONAL :: skip_nuclear_density
1491
1492 INTEGER :: ispin
1493 LOGICAL :: my_skip
1494 TYPE(dft_control_type), POINTER :: dft_control
1495 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
1496 TYPE(pw_c1d_gs_type), POINTER :: rho0_s_gs, rho_core, rhoz_cneo_s_gs
1497 TYPE(qs_charges_type), POINTER :: qs_charges
1498
1499 my_skip = .false.
1500 IF (PRESENT(skip_nuclear_density)) my_skip = skip_nuclear_density
1501
1502 CALL qs_rho_get(rho, rho_g=rho_g)
1503 CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
1504
1505 IF (.NOT. my_skip) THEN
1506 NULLIFY (rho_core)
1507 CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
1508 IF (dft_control%qs_control%gapw) THEN
1509 NULLIFY (rho0_s_gs, rhoz_cneo_s_gs)
1510 CALL get_qs_env(qs_env=qs_env, rho0_s_gs=rho0_s_gs, rhoz_cneo_s_gs=rhoz_cneo_s_gs)
1511 cpassert(ASSOCIATED(rho0_s_gs))
1512 CALL pw_copy(rho0_s_gs, rho_tot_gspace)
1513 IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
1514 CALL pw_axpy(rhoz_cneo_s_gs, rho_tot_gspace)
1515 END IF
1516 IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
1517 CALL pw_axpy(rho_core, rho_tot_gspace)
1518 END IF
1519 ELSE
1520 CALL pw_copy(rho_core, rho_tot_gspace)
1521 END IF
1522 DO ispin = 1, dft_control%nspins
1523 CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
1524 END DO
1525 CALL get_qs_env(qs_env=qs_env, qs_charges=qs_charges)
1526 qs_charges%total_rho_gspace = pw_integrate_function(rho_tot_gspace, isign=-1)
1527 ELSE
1528 DO ispin = 1, dft_control%nspins
1529 CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
1530 END DO
1531 END IF
1532
1533 END SUBROUTINE calc_rho_tot_gspace
1534
1535! **************************************************************************************************
1536!> \brief compute MO derivatives
1537!> \param qs_env the qs_env to update
1538!> \param ks_matrix ...
1539!> \param mo_derivs ...
1540!> \par History
1541!> 01.2014 created, transferred from qs_ks_build_kohn_sham_matrix in
1542!> separate subroutine
1543!> \author Dorothea Golze
1544! **************************************************************************************************
1545 SUBROUTINE calc_mo_derivatives(qs_env, ks_matrix, mo_derivs)
1546 TYPE(qs_environment_type), POINTER :: qs_env
1547 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix, mo_derivs
1548
1549 INTEGER :: ispin
1550 LOGICAL :: uniform_occupation
1551 REAL(kind=dp), DIMENSION(:), POINTER :: occupation_numbers
1552 TYPE(cp_fm_type), POINTER :: mo_coeff
1553 TYPE(dbcsr_type) :: mo_derivs2_tmp1, mo_derivs2_tmp2
1554 TYPE(dbcsr_type), POINTER :: mo_coeff_b
1555 TYPE(dft_control_type), POINTER :: dft_control
1556 TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
1557
1558 NULLIFY (dft_control, mo_array, mo_coeff, mo_coeff_b, occupation_numbers)
1559
1560 CALL get_qs_env(qs_env, &
1561 dft_control=dft_control, &
1562 mos=mo_array)
1563
1564 DO ispin = 1, SIZE(mo_derivs)
1565
1566 CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, &
1567 mo_coeff_b=mo_coeff_b, occupation_numbers=occupation_numbers)
1568 CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin)%matrix, mo_coeff_b, &
1569 0.0_dp, mo_derivs(ispin)%matrix)
1570
1571 IF (dft_control%restricted) THEN
1572 ! only the first mo_set are actual variables, but we still need both
1573 cpassert(ispin == 1)
1574 cpassert(SIZE(mo_array) == 2)
1575 ! use a temporary array with the same size as the first spin for the second spin
1576
1577 ! uniform_occupation is needed for this case, otherwise we can not
1578 ! reconstruct things in ot, since we irreversibly sum
1579 CALL get_mo_set(mo_set=mo_array(1), uniform_occupation=uniform_occupation)
1580 cpassert(uniform_occupation)
1581 CALL get_mo_set(mo_set=mo_array(2), uniform_occupation=uniform_occupation)
1582 cpassert(uniform_occupation)
1583
1584 ! The beta-spin might have fewer orbitals than alpa-spin...
1585 ! create temporary matrices with beta_nmo columns
1586 CALL get_mo_set(mo_set=mo_array(2), mo_coeff_b=mo_coeff_b)
1587 CALL dbcsr_create(mo_derivs2_tmp1, template=mo_coeff_b)
1588
1589 ! calculate beta derivatives
1590 CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(2)%matrix, mo_coeff_b, 0.0_dp, mo_derivs2_tmp1)
1591
1592 ! create larger matrix with alpha_nmo columns
1593 CALL dbcsr_create(mo_derivs2_tmp2, template=mo_derivs(1)%matrix)
1594 CALL dbcsr_set(mo_derivs2_tmp2, 0.0_dp)
1595
1596 ! copy into larger matrix, fills the first beta_nmo columns
1597 CALL dbcsr_copy_columns_hack(mo_derivs2_tmp2, mo_derivs2_tmp1, &
1598 mo_array(2)%nmo, 1, 1, &
1599 para_env=mo_array(1)%mo_coeff%matrix_struct%para_env, &
1600 blacs_env=mo_array(1)%mo_coeff%matrix_struct%context)
1601
1602 ! add beta contribution to alpa mo_derivs
1603 CALL dbcsr_add(mo_derivs(1)%matrix, mo_derivs2_tmp2, 1.0_dp, 1.0_dp)
1604 CALL dbcsr_release(mo_derivs2_tmp1)
1605 CALL dbcsr_release(mo_derivs2_tmp2)
1606 END IF
1607 END DO
1608
1609 IF (dft_control%do_admm_mo) THEN
1610 CALL calc_admm_mo_derivatives(qs_env, mo_derivs)
1611 END IF
1612
1613 END SUBROUTINE calc_mo_derivatives
1614
1615! **************************************************************************************************
1616!> \brief updates the Kohn Sham matrix of the given qs_env (facility method)
1617!> \param qs_env the qs_env to update
1618!> \param calculate_forces if true calculate the quantities needed
1619!> to calculate the forces. Defaults to false.
1620!> \param just_energy if true updates the energies but not the
1621!> ks matrix. Defaults to false
1622!> \param print_active ...
1623!> \par History
1624!> 4.2002 created [fawzi]
1625!> 8.2014 kpoints [JGH]
1626!> 10.2014 refractored [Ole Schuett]
1627!> \author Fawzi Mohamed
1628! **************************************************************************************************
1629 SUBROUTINE qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, &
1630 print_active)
1631 TYPE(qs_environment_type), POINTER :: qs_env
1632 LOGICAL, INTENT(IN), OPTIONAL :: calculate_forces, just_energy, &
1633 print_active
1634
1635 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_ks_update_qs_env'
1636
1637 INTEGER :: handle, unit_nr
1638 LOGICAL :: c_forces, do_rebuild, energy_only, &
1639 forces_up_to_date, potential_changed, &
1640 rho_changed, s_mstruct_changed
1641 TYPE(qs_ks_env_type), POINTER :: ks_env
1642
1643 NULLIFY (ks_env)
1644 unit_nr = cp_logger_get_default_io_unit()
1645
1646 c_forces = .false.
1647 energy_only = .false.
1648 IF (PRESENT(just_energy)) energy_only = just_energy
1649 IF (PRESENT(calculate_forces)) c_forces = calculate_forces
1650
1651 IF (c_forces) THEN
1652 CALL timeset(routinen//'_forces', handle)
1653 ELSE
1654 CALL timeset(routinen, handle)
1655 END IF
1656
1657 cpassert(ASSOCIATED(qs_env))
1658
1659 CALL get_qs_env(qs_env, &
1660 ks_env=ks_env, &
1661 rho_changed=rho_changed, &
1662 s_mstruct_changed=s_mstruct_changed, &
1663 potential_changed=potential_changed, &
1664 forces_up_to_date=forces_up_to_date)
1665
1666 do_rebuild = .false.
1667 do_rebuild = do_rebuild .OR. rho_changed
1668 do_rebuild = do_rebuild .OR. s_mstruct_changed
1669 do_rebuild = do_rebuild .OR. potential_changed
1670 do_rebuild = do_rebuild .OR. (c_forces .AND. .NOT. forces_up_to_date)
1671
1672 IF (do_rebuild) THEN
1673 CALL evaluate_core_matrix_traces(qs_env)
1674
1675 ! the ks matrix will be rebuilt so this is fine now
1676 CALL set_ks_env(ks_env, potential_changed=.false.)
1677
1678 CALL rebuild_ks_matrix(qs_env, &
1679 calculate_forces=c_forces, &
1680 just_energy=energy_only, &
1681 print_active=print_active)
1682
1683 IF (.NOT. energy_only) THEN
1684 CALL set_ks_env(ks_env, &
1685 rho_changed=.false., &
1686 s_mstruct_changed=.false., &
1687 forces_up_to_date=forces_up_to_date .OR. c_forces)
1688 END IF
1689 END IF
1690
1691 CALL timestop(handle)
1692
1693 END SUBROUTINE qs_ks_update_qs_env
1694
1695! **************************************************************************************************
1696!> \brief Calculates the traces of the core matrices and the density matrix.
1697!> \param qs_env ...
1698!> \param rho_ao_ext ...
1699!> \author Ole Schuett
1700! **************************************************************************************************
1701 SUBROUTINE evaluate_core_matrix_traces(qs_env, rho_ao_ext)
1702 TYPE(qs_environment_type), POINTER :: qs_env
1703 TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
1704 POINTER :: rho_ao_ext
1705
1706 CHARACTER(LEN=*), PARAMETER :: routinen = 'evaluate_core_matrix_traces'
1707
1708 INTEGER :: handle
1709 REAL(kind=dp) :: energy_core_im
1710 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrixkp_h, matrixkp_t, rho_ao_kp
1711 TYPE(dft_control_type), POINTER :: dft_control
1712 TYPE(qs_energy_type), POINTER :: energy
1713 TYPE(qs_rho_type), POINTER :: rho
1714
1715 CALL timeset(routinen, handle)
1716 NULLIFY (energy, rho, dft_control, rho_ao_kp, matrixkp_t, matrixkp_h)
1717
1718 CALL get_qs_env(qs_env, &
1719 rho=rho, &
1720 energy=energy, &
1721 dft_control=dft_control, &
1722 kinetic_kp=matrixkp_t, &
1723 matrix_h_kp=matrixkp_h)
1724
1725 IF (PRESENT(rho_ao_ext)) THEN
1726 rho_ao_kp => rho_ao_ext
1727 ELSE
1728 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
1729 END IF
1730
1731 CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy%core, dft_control%nspins)
1732
1733 ! Add the imaginary part in the RTP case
1734 IF (qs_env%run_rtp) THEN
1735 IF (dft_control%rtp_control%velocity_gauge) THEN
1736 CALL get_qs_env(qs_env, matrix_h_im_kp=matrixkp_h)
1737 CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_kp)
1738 CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy_core_im, dft_control%nspins)
1739 energy%core = energy%core - energy_core_im
1740 END IF
1741 END IF
1742
1743 ! kinetic energy
1744 IF (ASSOCIATED(matrixkp_t)) THEN
1745 CALL calculate_ptrace(matrixkp_t, rho_ao_kp, energy%kinetic, dft_control%nspins)
1746 END IF
1747
1748 CALL timestop(handle)
1749 END SUBROUTINE evaluate_core_matrix_traces
1750
1751! **************************************************************************************************
1752!> \brief Constructs a new Khon-Sham matrix
1753!> \param qs_env ...
1754!> \param calculate_forces ...
1755!> \param just_energy ...
1756!> \param print_active ...
1757!> \author Ole Schuett
1758! **************************************************************************************************
1759 SUBROUTINE rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
1760 TYPE(qs_environment_type), POINTER :: qs_env
1761 LOGICAL, INTENT(IN) :: calculate_forces, just_energy
1762 LOGICAL, INTENT(IN), OPTIONAL :: print_active
1763
1764 CHARACTER(LEN=*), PARAMETER :: routinen = 'rebuild_ks_matrix'
1765
1766 INTEGER :: handle
1767 TYPE(dft_control_type), POINTER :: dft_control
1768
1769 CALL timeset(routinen, handle)
1770 NULLIFY (dft_control)
1771
1772 CALL get_qs_env(qs_env, dft_control=dft_control)
1773
1774 IF (dft_control%qs_control%semi_empirical) THEN
1775 CALL build_se_fock_matrix(qs_env, &
1776 calculate_forces=calculate_forces, &
1777 just_energy=just_energy)
1778
1779 ELSE IF (dft_control%qs_control%dftb) THEN
1780 CALL build_dftb_ks_matrix(qs_env, &
1781 calculate_forces=calculate_forces, &
1782 just_energy=just_energy)
1783
1784 ELSE IF (dft_control%qs_control%xtb) THEN
1785 IF (dft_control%qs_control%xtb_control%do_tblite) THEN
1786 CALL build_tblite_ks_matrix(qs_env, &
1787 calculate_forces=calculate_forces, &
1788 just_energy=just_energy)
1789 ELSE
1790 CALL build_xtb_ks_matrix(qs_env, &
1791 calculate_forces=calculate_forces, &
1792 just_energy=just_energy)
1793 END IF
1794 ELSE
1795 CALL qs_ks_build_kohn_sham_matrix(qs_env, &
1796 calculate_forces=calculate_forces, &
1797 just_energy=just_energy, &
1798 print_active=print_active)
1799 END IF
1800
1801 CALL timestop(handle)
1802
1803 END SUBROUTINE rebuild_ks_matrix
1804
1805! **************************************************************************************************
1806!> \brief Allocate ks_matrix if necessary, take current overlap matrix as template
1807!> \param qs_env ...
1808!> \param is_complex ...
1809!> \par History
1810!> refactoring 04.03.2011 [MI]
1811!> \author
1812! **************************************************************************************************
1813
1814 SUBROUTINE qs_ks_allocate_basics(qs_env, is_complex)
1815 TYPE(qs_environment_type), POINTER :: qs_env
1816 LOGICAL, INTENT(in) :: is_complex
1817
1818 CHARACTER(LEN=default_string_length) :: headline
1819 INTEGER :: ic, ispin, nimages, nspins
1820 LOGICAL :: do_kpoints
1821 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, matrixkp_im_ks, matrixkp_ks
1822 TYPE(dbcsr_type), POINTER :: refmatrix
1823 TYPE(dft_control_type), POINTER :: dft_control
1824 TYPE(kpoint_type), POINTER :: kpoints
1825 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1826 POINTER :: sab_orb
1827 TYPE(qs_ks_env_type), POINTER :: ks_env
1828
1829 NULLIFY (dft_control, ks_env, matrix_s_kp, sab_orb, matrixkp_ks, refmatrix, matrixkp_im_ks, kpoints)
1830
1831 CALL get_qs_env(qs_env, &
1832 dft_control=dft_control, &
1833 matrix_s_kp=matrix_s_kp, &
1834 ks_env=ks_env, &
1835 kpoints=kpoints, &
1836 do_kpoints=do_kpoints, &
1837 matrix_ks_kp=matrixkp_ks, &
1838 matrix_ks_im_kp=matrixkp_im_ks)
1839
1840 IF (do_kpoints) THEN
1841 CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
1842 ELSE
1843 CALL get_qs_env(qs_env, sab_orb=sab_orb)
1844 END IF
1845
1846 nspins = dft_control%nspins
1847 nimages = dft_control%nimages
1848
1849 IF (.NOT. ASSOCIATED(matrixkp_ks)) THEN
1850 CALL dbcsr_allocate_matrix_set(matrixkp_ks, nspins, nimages)
1851 refmatrix => matrix_s_kp(1, 1)%matrix
1852 DO ispin = 1, nspins
1853 DO ic = 1, nimages
1854 IF (nspins > 1) THEN
1855 IF (ispin == 1) THEN
1856 headline = "KOHN-SHAM MATRIX FOR ALPHA SPIN"
1857 ELSE
1858 headline = "KOHN-SHAM MATRIX FOR BETA SPIN"
1859 END IF
1860 ELSE
1861 headline = "KOHN-SHAM MATRIX"
1862 END IF
1863 ALLOCATE (matrixkp_ks(ispin, ic)%matrix)
1864 CALL dbcsr_create(matrix=matrixkp_ks(ispin, ic)%matrix, template=refmatrix, &
1865 name=trim(headline), matrix_type=dbcsr_type_symmetric)
1866 CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_ks(ispin, ic)%matrix, sab_orb)
1867 CALL dbcsr_set(matrixkp_ks(ispin, ic)%matrix, 0.0_dp)
1868 END DO
1869 END DO
1870 CALL set_ks_env(ks_env, matrix_ks_kp=matrixkp_ks)
1871 END IF
1872
1873 IF (is_complex) THEN
1874 IF (.NOT. ASSOCIATED(matrixkp_im_ks)) THEN
1875 cpassert(nspins == SIZE(matrixkp_ks, 1))
1876 cpassert(nimages == SIZE(matrixkp_ks, 2))
1877 CALL dbcsr_allocate_matrix_set(matrixkp_im_ks, nspins, nimages)
1878 DO ispin = 1, nspins
1879 DO ic = 1, nimages
1880 IF (nspins > 1) THEN
1881 IF (ispin == 1) THEN
1882 headline = "IMAGINARY KOHN-SHAM MATRIX FOR ALPHA SPIN"
1883 ELSE
1884 headline = "IMAGINARY KOHN-SHAM MATRIX FOR BETA SPIN"
1885 END IF
1886 ELSE
1887 headline = "IMAGINARY KOHN-SHAM MATRIX"
1888 END IF
1889 ALLOCATE (matrixkp_im_ks(ispin, ic)%matrix)
1890 refmatrix => matrixkp_ks(ispin, ic)%matrix ! base on real part, but anti-symmetric
1891 CALL dbcsr_create(matrix=matrixkp_im_ks(ispin, ic)%matrix, template=refmatrix, &
1892 name=trim(headline), matrix_type=dbcsr_type_antisymmetric)
1893 CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_im_ks(ispin, ic)%matrix, sab_orb)
1894 CALL dbcsr_set(matrixkp_im_ks(ispin, ic)%matrix, 0.0_dp)
1895 END DO
1896 END DO
1897 CALL set_ks_env(ks_env, matrix_ks_im_kp=matrixkp_im_ks)
1898 END IF
1899 END IF
1900
1901 END SUBROUTINE qs_ks_allocate_basics
1902
1903END MODULE qs_ks_methods
subroutine, public accint_weight_force(qs_env, rho, rho1, order, xc_section, triplet, force_scale)
...
Contains ADMM methods which only require the density matrix.
subroutine, public admm_dm_merge_ks_matrix(qs_env)
Entry methods: Merges auxiliary Kohn-Sham matrix into primary one.
subroutine, public admm_dm_calc_rho_aux(qs_env)
Entry methods: Calculates auxiliary density matrix from primary one.
Contains ADMM methods which require molecular orbitals.
subroutine, public admm_mo_calc_rho_aux_kp(qs_env)
...
subroutine, public admm_mo_merge_ks_matrix(qs_env)
...
subroutine, public admm_update_ks_atom(qs_env, calculate_forces)
Adds the GAPW exchange contribution to the aux_fit ks matrices.
subroutine, public calc_admm_ovlp_forces_kp(qs_env)
Calculate the forces due to the AUX/ORB basis overlap in ADMM, in the KP case.
subroutine, public admm_mo_calc_rho_aux(qs_env)
...
subroutine, public calc_admm_ovlp_forces(qs_env)
Calculate the forces due to the AUX/ORB basis overlap in ADMM.
subroutine, public calc_admm_mo_derivatives(qs_env, mo_derivs)
Calculate the derivative of the AUX_FIT mo, based on the ORB mo_derivs.
Types and set/get functions for auxiliary density matrix methods.
Definition admm_types.F:15
subroutine, public get_admm_env(admm_env, mo_derivs_aux_fit, mos_aux_fit, sab_aux_fit, sab_aux_fit_asymm, sab_aux_fit_vs_orb, matrix_s_aux_fit, matrix_s_aux_fit_kp, matrix_s_aux_fit_vs_orb, matrix_s_aux_fit_vs_orb_kp, task_list_aux_fit, matrix_ks_aux_fit, matrix_ks_aux_fit_kp, matrix_ks_aux_fit_im, matrix_ks_aux_fit_dft, matrix_ks_aux_fit_hfx, matrix_ks_aux_fit_dft_kp, matrix_ks_aux_fit_hfx_kp, rho_aux_fit, rho_aux_fit_buffer, admm_dm)
Get routine for the ADMM env.
Definition admm_types.F:599
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_multiply(transa, transb, alpha, matrix_a, matrix_b, beta, matrix_c, first_row, last_row, first_column, last_column, first_k, last_k, retain_sparsity, filter_eps, flop)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_filter(matrix, eps)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
DBCSR operations in CP2K.
subroutine, public dbcsr_copy_columns_hack(matrix_b, matrix_a, ncol, source_start, target_start, para_env, blacs_env)
hack for dbcsr_copy_columns
Density Derived atomic point charges from a QM calculation (see Bloechl, J. Chem. Phys....
Definition cp_ddapc.F:15
subroutine, public qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, just_energy)
Set of methods using DDAPC charges.
Definition cp_ddapc.F:81
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer, parameter, public cp_p_file
integer function, public cp_print_key_should_output(iteration_info, basis_section, print_key_path, used_print_key, first_time)
returns what should be done with the given property if btest(res,cp_p_store) then the property should...
Add the DFT+U contribution to the Hamiltonian matrix.
Definition dft_plus_u.F:18
subroutine, public plus_u(qs_env, matrix_h, matrix_w)
Add the DFT+U contribution to the Hamiltonian matrix. Wrapper routine for all "+U" methods.
Definition dft_plus_u.F:107
GAPW reciprocal-space reconstruction and its discrete adjoint.
subroutine, public integrate_gapw_composite_vxc_gspace(qs_env, auxbas_pw_pool, vxc_rho, vxc_tau, atom_force, strain_virial, one_center_contraction, one_center_rho_contraction, one_center_tau_contraction)
Apply the discrete adjoint of the common-grid GAPW hard-minus-soft reconstruction.
subroutine, public planar_averaged_v_hartree_3d(v_rspace, dft_control, do_gce, ref_esp, para_env)
calculate the planar averaged real space potential (e.g. Hartree potential) along the surface normal ...
Definition gce_methods.F:82
subroutine, public planar_counter_charge(rho_tot_gspace, pcc_env, auxbas_pw_pool)
add the planar counter charge density to the total charge density
subroutine, public vh_1c_gg_integrals(qs_env, energy_hartree_1c, ecoul_1c, local_rho_set, para_env, tddft, local_rho_set_2nd, core_2nd)
Calculates one center GAPW Hartree energies and matrix elements Hartree potentials are input Takes po...
Adaptively Compressed Exchange (ACE) operator for HFX. Reference: Lin, J. Chem. Theory Comput....
subroutine, public hfx_ace_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, just_energy, v_rspace_new, v_tau_rspace, ace_rebuild_frequency, ext_xc_section)
Main ACE entry point, replacing hfx_ks_matrix in qs_ks_methods.
Utilities for hfx and admm methods.
subroutine, public hfx_admm_init(qs_env, calculate_forces, ext_xc_section)
...
subroutine, public hfx_ks_matrix(qs_env, matrix_ks, rho, energy, calculate_forces, just_energy, v_rspace_new, v_tau_rspace, ext_xc_section)
Add the hfx contributions to the Hamiltonian.
subroutine, public hfx_ks_matrix_kp(qs_env, matrix_ks, energy, calculate_forces)
Add the HFX K-point contribution to the real-space Hamiltonians.
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public smeagol_runtype_emtransport
integer, parameter, public do_ppl_grid
integer, parameter, public outer_scf_becke_constraint
integer, parameter, public outer_scf_hirshfeld_constraint
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Routines for a Kim-Gordon-like partitioning into molecular subunits.
subroutine, public kg_ekin_subset(qs_env, ks_matrix, ekin_mol, calc_force, do_kernel, pmat_ext)
Calculates the subsystem Hohenberg-Kohn kinetic energy and the forces.
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Types and basic routines needed for a kpoint calculation.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered, lattice_fft)
Retrieve information from a kpoint environment.
Calculates integral matrices for LRIGPW method lri : local resolution of the identity.
subroutine, public v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl_ri)
...
contains the types and subroutines for dealing with the lri_env lri : local resolution of the identit...
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
logical function, public abnormal_value(a)
determines if a value is not normal (e.g. for Inf and Nan) based on IO to work also under optimizatio...
Definition mathlib.F:159
Interface to the message passing library MPI.
Define the data structure for the particle information.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
functions related to the poisson solver on regular grids
integer, parameter, public pw_poisson_implicit
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Routines for image charge calculation within QM/MM.
subroutine, public calculate_image_pot(v_hartree_rspace, rho_hartree_gspace, energy, qmmm_env, qs_env)
determines coefficients by solving image_matrix*coeff=-pot_const by Gaussian elimination or in an ite...
subroutine, public integrate_potential_devga_rspace(potential, coeff, forces, qmmm_env, qs_env)
calculates the image forces on the MM atoms
subroutine, public add_image_pot_to_hartree_pot(v_hartree, v_metal, qs_env)
Add potential of metal (image charge pot) to Hartree Potential.
Defines CDFT control structures.
Utility subroutines for CDFT calculations.
subroutine, public cdft_constraint_print(qs_env, electronic_charge)
Prints information about CDFT constraints.
container for information about total charges on the grids
Calculation of the energies concerning the core charge distribution.
Calculation of Overlap and Hamiltonian matrices in DFTB.
subroutine, public build_dftb_ks_matrix(qs_env, calculate_forces, just_energy)
...
Calculates the energy contribution and the mo_derivative of a static periodic electric field.
subroutine, public qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
...
Calculates the energy contribution and the mo_derivative of a static electric field (nonperiodic)
subroutine, public qs_efield_local_operator(qs_env, just_energy, calculate_forces)
...
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.
subroutine, public prepare_gapw_den(qs_env, local_rho_set, do_rho0, kind_set_external, pw_env_sub)
...
Types needed for a for a Harris model calculation.
Harris method environment setup and handling.
subroutine, public harris_set_potentials(harris_env, vh_rspace, vxc_rspace)
...
Integrate single or product functions over a potential on a RS grid.
Define the quickstep kind type and their sub types.
Set of routines to apply restraints to the KS hamiltonian.
subroutine, public qs_ks_s2_restraint(dft_control, qs_env, matrix_s, energy, calculate_forces, just_energy)
...
subroutine, public qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, ks_matrix, matrix_s, rho, mulliken_order_p)
...
subroutine, public qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
Apply a CDFT constraint.
routines that build the Kohn-Sham matrix contributions coming from local atomic densities
Definition qs_ks_atom.F:12
subroutine, public update_ks_atom(qs_env, ksmat, pmat, forces, tddft, rho_atom_external, kind_set_external, oce_external, sab_external, kscale, kintegral, kforce, fscale)
The correction to the KS matrix due to the GAPW local terms to the hartree and XC contributions is he...
Definition qs_ks_atom.F:110
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
subroutine, public rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
Constructs a new Khon-Sham matrix.
subroutine, public evaluate_core_matrix_traces(qs_env, rho_ao_ext)
Calculates the traces of the core matrices and the density matrix.
subroutine, public qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, print_active)
updates the Kohn Sham matrix of the given qs_env (facility method)
subroutine, public qs_ks_allocate_basics(qs_env, is_complex)
Allocate ks_matrix if necessary, take current overlap matrix as template.
subroutine, public calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
...
subroutine, public qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, print_active, ext_ks_matrix, ext_xc_section)
routine where the real calculations are made: the KS matrix is calculated
subroutine, public qmmm_calculate_energy(qs_env, rho, v_qmmm, qmmm_energy)
Computes the contribution to the total energy of the QM/MM electrostatic coupling.
subroutine, public qmmm_modify_hartree_pot(v_hartree, v_qmmm, scale)
Modify the hartree potential in order to include the QM/MM correction.
subroutine, public set_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, kpoints, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env)
...
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
Definition qs_ks_utils.F:22
subroutine, public print_densities(qs_env, rho)
...
subroutine, public get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, embed_corr, just_energy)
...
subroutine, public compute_matrix_vxc_kp(qs_env, v_rspace, matrix_vxc_kp, gapw_full_basis)
Build the XC potential matrix for k-point/image-resolved KS matrices.
subroutine, public low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, calculate_forces, auxbas_pw_pool)
do ROKS calculations yielding low spin states
subroutine, public compute_matrix_vxc(qs_env, v_rspace, matrix_vxc, gapw_full_basis)
compute matrix_vxc, defined via the potential created by qs_vxc_create ignores things like tau functi...
subroutine, public sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, cdft_control, calculate_forces)
Sum up all potentials defined on the grid and integrate.
subroutine, public print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
Print detailed energies.
subroutine, public calculate_zmp_potential(qs_env, v_rspace_new, rho, exc)
Calculate the ZMP potential and energy as in Zhao, Morrison Parr PRA 50i, 2138 (1994) V_c^\lambda def...
subroutine, public calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, just_energy, calculate_forces, auxbas_pw_pool)
do sic calculations on the spin density
subroutine, public sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, calculate_forces, auxbas_pw_pool)
do sic calculations on explicit orbitals
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count)
Get the components of a MO set data structure.
Define the neighbor list data types and the corresponding functionality.
subroutine, public integrate_vhg0_rspace(qs_env, v_rspace, para_env, calculate_forces, local_rho_set, local_rho_set_2nd, atener, kforce, my_pools, my_rs_descs)
...
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
Self-consistent continuum solvation (SCCS) model implementation.
Definition qs_sccs.F:29
subroutine, public sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs, h_stress)
Self-consistent continuum solvation (SCCS) model implementation.
Definition qs_sccs.F:126
routines that build the integrals of the Vxc potential calculated for the atomic density in the basis...
Definition qs_vxc_atom.F:12
subroutine, public gapw_cdft_one_center(qs_env, energy_only, calculate_forces, values, electronic_charge, operator_group, rho_atom_operator_set)
Add the GAPW one-center correction to CDFT values and operators.
subroutine, public calculate_vxc_atom(qs_env, energy_only, exc1, adiabatic_rescale_factor, kind_set_external, rho_atom_set_external, xc_section_external, calculate_forces, composite_vxc_rho, composite_vxc_tau, composite_reference_active, direct_valence_atom_grid, atom_composite_grid)
...
subroutine, public qs_vxc_create(ks_env, rho_struct, xc_section, vxc_rho, vxc_tau, exc, just_energy, edisp, dispersion_env, adiabatic_rescale_factor, pw_env_external, native_skala_atom_force, qs_env_external, native_gapw_composite_override, native_skala_defer_to_atom_composite)
calculates and allocates the xc potential, already reducing it to the dependence on rho and the one o...
Definition qs_vxc.F:118
Utilities for rtp in combination with admm methods adapted routines from admm_method (author Manuel G...
subroutine, public rtp_admm_merge_ks_matrix(qs_env)
...
subroutine, public rtp_admm_calc_rho_aux(qs_env)
Compute the ADMM density matrix in case of rtp (complex MO's)
Calculation of the Fock matrix for SE methods.
subroutine, public build_se_fock_matrix(qs_env, calculate_forces, just_energy)
Construction of the Fock matrix for NDDO methods.
Experimental CP2K-native GPW real-space-grid path for SKALA TorchScript models.
subroutine, public ensure_native_skala_grid_scope(xc_section)
Enforce the currently implemented native SKALA GPW input scope.
logical function, public native_skala_gapw_composite_direct_ao(xc_section)
Return true if the GAPW composite reference uses direct full-ORB collocation.
logical function, public xc_section_uses_gauxc_model(xc_section)
Return true if the GAUXC subsection requests a model evaluation.
logical function, public native_skala_uses_atom_composite_grid(xc_section)
Return true when native Skala uses atom-centered grids.
integer function, public skala_gapw_representation(xc_section)
Return the pseudopotential GAPW representation selected for an active model.
type(section_vals_type) function, pointer, public get_gauxc_section(xc_section)
Return the first GAUXC functional subsection, if present.
logical function, public native_skala_gapw_atom_composite_requested(xc_section)
Return true when the explicit atom-centered composite reference is requested.
logical function, public native_skala_gapw_composite_reference(xc_section)
Return true if native SKALA should use the full GAPW ORB density on one common grid.
logical function, public xc_section_uses_native_skala_grid(xc_section)
Return true if the GAUXC subsection requests the CP2K-native GPW grid path.
CP2K+SMEAGOL interface.
subroutine, public smeagol_shift_v_hartree(v_hartree_rspace, cell, hartreeleadsleft, hartreeleadsright, hartreeleadsbottom, vbias, zleft, zright, isexplicit_zright, isexplicit_bottom)
Align Hatree potential of semi-infinite leads to match bulk-transport calculation and apply external ...
Utilities for string manipulations.
elemental subroutine, public uppercase(string)
Convert all lower case characters in a string to upper case.
subroutine, public calc_dipsurf_potential(qs_env, energy)
compute the surface dipole and the correction to the hartree potential
tblite matrix build
subroutine, public build_tblite_ks_matrix(qs_env, calculate_forces, just_energy, ext_ks_matrix)
...
logical function, public gauxc_gapw_has_paw_pseudopotentials(qs_kind_set)
Return whether GauXC GAPW mode sees pseudopotential one-center GAPW kinds.
logical function, public gauxc_gapw_all_all_electron(qs_kind_set)
Return whether every GAPW kind uses an all-electron potential.
logical function, public gauxc_gapw_all_pseudopotentials(qs_kind_set)
Return whether every GAPW kind uses a pseudopotential.
subroutine, public apply_gauxc(qs_env, xc_section, calculate_forces)
...
input constants for xc
integer, parameter, public skala_gapw_direct_valence
integer, parameter, public skala_gapw_paw_one_center
Calculation of KS matrix in xTB Reference: Stefan Grimme, Christoph Bannwarth, Philip Shushkov JCTC 1...
subroutine, public build_xtb_ks_matrix(qs_env, calculate_forces, just_energy, ext_ks_matrix)
...
stores some data used in wavefunction fitting
Definition admm_types.F:120
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Contains information about kpoints.
stores all the informations relevant to an mpi environment
contained for different pw related things
environment for the poisson solver
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Container for information about total charges on the grids.
Contains information on the Harris method.
Provides all information about a quickstep kind.
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.