(git:c641773)
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,&
142 USE qs_vxc, ONLY: qs_vxc_create
148 USE skala_gpw_functional, ONLY: &
154 USE string_utilities, ONLY: uppercase
157 USE virial_types, ONLY: virial_type
165#include "./base/base_uses.f90"
166
167 IMPLICIT NONE
168
169 PRIVATE
170
171 LOGICAL, PARAMETER :: debug_this_module = .true.
172 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_methods'
173
176
177CONTAINS
178
179! **************************************************************************************************
180!> \brief routine where the real calculations are made: the
181!> KS matrix is calculated
182!> \param qs_env the qs_env to update
183!> \param calculate_forces if true calculate the quantities needed
184!> to calculate the forces. Defaults to false.
185!> \param just_energy if true updates the energies but not the
186!> ks matrix. Defaults to false
187!> \param print_active ...
188!> \param ext_ks_matrix ...
189!> \param ext_xc_section ...
190!> \par History
191!> 06.2002 moved from qs_scf to qs_ks_methods, use of ks_env
192!> new did_change scheme [fawzi]
193!> 10.2002 introduced pools, uses updated rho as input, LSD [fawzi]
194!> 10.2004 build_kohn_sham matrix now also computes the derivatives
195!> of the total energy wrt to the MO coefs, if instructed to
196!> do so. This appears useful for orbital dependent functionals
197!> where the KS matrix alone (however this might be defined)
198!> does not contain the info to construct this derivative.
199!> \author Matthias Krack
200!> \note
201!> make rho, energy and qs_charges optional, defaulting
202!> to qs_env components?
203! **************************************************************************************************
204 SUBROUTINE qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, &
205 print_active, ext_ks_matrix, ext_xc_section)
206 TYPE(qs_environment_type), POINTER :: qs_env
207 LOGICAL, INTENT(in) :: calculate_forces, just_energy
208 LOGICAL, INTENT(IN), OPTIONAL :: print_active
209 TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
210 POINTER :: ext_ks_matrix
211 TYPE(section_vals_type), OPTIONAL, POINTER :: ext_xc_section
212
213 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_ks_build_kohn_sham_matrix'
214
215 CHARACTER(len=default_string_length) :: gauxc_model_name, name
216 INTEGER :: ace_rebuild_frequency, atom_a, gapw_representation, handle, iatom, ikind, img, &
217 ispin, natom, nimages, nspins, output_unit
218 INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, kind_of
219 LOGICAL :: ace_active, do_adiabatic_rescaling, do_ddapc, do_hfx, do_kpoints, do_ppl, dokp, &
220 gapw, gapw_all_all_electron, gapw_all_pseudopotentials, gapw_mixed_core_representations, &
221 gapw_xc, gauxc_model_none, just_energy_xc, lrigpw, my_print, &
222 native_direct_valence_atom_composite, native_gapw_all_electron_atom_composite, &
223 native_gapw_atom_composite_reference, native_gapw_atom_composite_requested, &
224 native_gapw_composite_direct_ao, native_gapw_composite_reference, &
225 native_gapw_periodic_atom_composite, native_grid_diagnostics, native_grid_use_cuda, &
226 native_skala_evaluator, native_skala_restore_exc
227 LOGICAL :: paw_composite_representation, rigpw, use_gauxc_matrix, use_virial
228 LOGICAL, SAVE :: native_grid_cpu_kpoints_warned = .false.
229 REAL(kind=dp) :: ecore_ppl, edisp, ee_ener, ekin_mol, mulliken_order_p, &
230 native_skala_composite_contraction, native_skala_composite_rho_contraction, &
231 native_skala_composite_tau_contraction, native_skala_exc_scf, native_skala_total_scf, &
232 vscale
233 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: cdft_gapw_values
234 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: cdft_charge_correction, &
235 cdft_electronic_charge, &
236 native_skala_atom_force
237 REAL(kind=dp), DIMENSION(3, 3) :: h_stress, native_skala_composite_virial, &
238 pv_loc
239 TYPE(admm_type), POINTER :: admm_env
240 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
241 TYPE(cdft_control_type), POINTER :: cdft_control
242 TYPE(cell_type), POINTER :: cell
243 TYPE(cp_logger_type), POINTER :: logger
244 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ksmat, matrix_vhxc, mo_derivs
245 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: ks_matrix, ks_matrix_im, matrix_h, &
246 matrix_h_im, matrix_s, matrix_vxc, &
247 my_rho, rho_ao
248 TYPE(dft_control_type), POINTER :: dft_control
249 TYPE(ecoul_1center_type), DIMENSION(:), POINTER :: ecoul_1c
250 TYPE(harris_type), POINTER :: harris_env
251 TYPE(kpoint_type), POINTER :: kpoints
252 TYPE(local_rho_type), POINTER :: local_rho_set
253 TYPE(lri_density_type), POINTER :: lri_density
254 TYPE(lri_environment_type), POINTER :: lri_env
255 TYPE(lri_kind_type), DIMENSION(:), POINTER :: lri_v_int
256 TYPE(mp_para_env_type), POINTER :: para_env
257 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
258 POINTER :: sab_orb
259 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
260 TYPE(pw_c1d_gs_type) :: rho_tot_gspace, v_hartree_gspace
261 TYPE(pw_c1d_gs_type), POINTER :: rho_core
262 TYPE(pw_env_type), POINTER :: pw_env
263 TYPE(pw_poisson_type), POINTER :: poisson_env
264 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
265 TYPE(pw_r3d_rs_type), DIMENSION(2) :: v_hxc_rspace
266 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, v_rspace_embed, v_rspace_new, &
267 v_rspace_new_aux_fit, v_tau_rspace, &
268 v_tau_rspace_aux_fit
269 TYPE(pw_r3d_rs_type), POINTER :: rho0_s_rs, rho_nlcc, rhoz_cneo_s_rs, v_hartree_rspace, &
270 v_sccs_rspace, v_sic_rspace, v_spin_ddapc_rest_r, vee, vppl_rspace
271 TYPE(qs_energy_type), POINTER :: energy
272 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
273 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
274 TYPE(qs_ks_env_type), POINTER :: ks_env
275 TYPE(qs_rho_type), POINTER :: rho, rho1, rho_struct, rho_xc
276 TYPE(section_vals_type), POINTER :: ace_section, &
277 adiabatic_rescaling_section, &
278 gauxc_section, hfx_sections, input, &
279 scf_section, xc_section
280 TYPE(virial_type), POINTER :: virial
281
282 CALL timeset(routinen, handle)
283 NULLIFY (admm_env, atomic_kind_set, cell, dft_control, force, logger, mo_derivs, my_rho, &
284 rho_struct, para_env, pw_env, virial, vppl_rspace, &
285 ace_section, &
286 adiabatic_rescaling_section, hfx_sections, input, scf_section, &
287 xc_section, gauxc_section, matrix_h, matrix_h_im, matrix_s, auxbas_pw_pool, poisson_env, &
288 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, &
289 matrix_vhxc, &
290 vee, rho_nlcc, ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, &
291 rho_core, sab_orb, particle_set, qs_kind_set, kpoints)
292
293 cpassert(ASSOCIATED(qs_env))
294
295 logger => cp_get_default_logger()
296 my_print = .true.
297 IF (PRESENT(print_active)) my_print = print_active
298 use_gauxc_matrix = .false.
299 native_gapw_atom_composite_reference = .false.
300 native_gapw_atom_composite_requested = .false.
301 native_gapw_all_electron_atom_composite = .false.
302 native_direct_valence_atom_composite = .false.
303 gapw_all_all_electron = .false.
304 gapw_all_pseudopotentials = .false.
305 gapw_mixed_core_representations = .false.
306 native_gapw_composite_direct_ao = .false.
307 native_gapw_composite_reference = .false.
308 native_gapw_periodic_atom_composite = .false.
309 native_grid_diagnostics = .false.
310 native_skala_composite_contraction = 0.0_dp
311 native_skala_composite_rho_contraction = 0.0_dp
312 native_skala_composite_tau_contraction = 0.0_dp
313 native_skala_restore_exc = .false.
314
315 CALL get_qs_env(qs_env, &
316 ks_env=ks_env, &
317 dft_control=dft_control, &
318 matrix_h_kp=matrix_h, &
319 matrix_h_im_kp=matrix_h_im, &
320 matrix_s_kp=matrix_s, &
321 matrix_ks_kp=ks_matrix, &
322 matrix_ks_im_kp=ks_matrix_im, &
323 matrix_vhxc=matrix_vhxc, &
324 matrix_vxc_kp=matrix_vxc, &
325 pw_env=pw_env, &
326 cell=cell, &
327 atomic_kind_set=atomic_kind_set, &
328 para_env=para_env, &
329 input=input, &
330 virial=virial, &
331 v_hartree_rspace=v_hartree_rspace, &
332 vee=vee, &
333 rho_nlcc=rho_nlcc, &
334 rho=rho, &
335 rho_core=rho_core, &
336 rho_xc=rho_xc, &
337 sab_orb=sab_orb, &
338 energy=energy, &
339 force=force, &
340 kpoints=kpoints, &
341 do_kpoints=do_kpoints, &
342 particle_set=particle_set, &
343 qs_kind_set=qs_kind_set, &
344 natom=natom)
345
346 ! Reject force/stress requests before assembling even the baseline KS matrix.
347 IF (dft_control%low_spin_roks .AND. dft_control%qs_control%gapw) THEN
348 IF (.NOT. virial%pv_numer .AND. &
349 (virial%pv_calculate .OR. (virial%pv_availability .AND. calculate_forces))) THEN
350 CALL cp_abort(__location__, "GAPW low spin ROKS stress is not implemented.")
351 END IF
352 IF (calculate_forces) THEN
353 CALL cp_abort(__location__, "GAPW low spin ROKS forces are not implemented.")
354 END IF
355 END IF
356
357 CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
358
359 nimages = dft_control%nimages
360 nspins = dft_control%nspins
361
362 ! remap pointer to allow for non-kpoint external ks matrix
363 IF (PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
364
365 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
366
367 adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
368 CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
369 just_energy_xc = just_energy
370 IF (do_adiabatic_rescaling) THEN
371 !! If we perform adiabatic rescaling, the xc potential has to be scaled by the xc- and
372 !! HFX-energy. Thus, let us first calculate the energy
373 just_energy_xc = .true.
374 END IF
375
376 cpassert(ASSOCIATED(matrix_h))
377 cpassert(ASSOCIATED(matrix_s))
378 cpassert(ASSOCIATED(rho))
379 cpassert(ASSOCIATED(pw_env))
380 cpassert(SIZE(ks_matrix, 1) > 0)
381 dokp = (nimages > 1)
382
383 ! Setup the possible usage of DDAPC charges
384 do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
385 qs_env%cp_ddapc_ewald%do_decoupling .OR. &
386 qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
387 qs_env%cp_ddapc_ewald%do_solvation
388
389 ! Check if LRIGPW is used
390 lrigpw = dft_control%qs_control%lrigpw
391 rigpw = dft_control%qs_control%rigpw
392 IF (rigpw) THEN
393 cpassert(nimages == 1)
394 END IF
395 IF (lrigpw .AND. rigpw) THEN
396 cpabort(" LRI and RI are not compatible")
397 END IF
398
399 ! Check for GAPW method : additional terms for local densities
400 gapw = dft_control%qs_control%gapw
401 gapw_xc = dft_control%qs_control%gapw_xc
402 IF (gapw_xc .AND. gapw) THEN
403 cpabort(" GAPW and GAPW_XC are not compatible")
404 END IF
405 IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw)) THEN
406 cpabort(" GAPW/GAPW_XC and LRIGPW are not compatible")
407 END IF
408 IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw)) THEN
409 cpabort(" GAPW/GAPW_XC and RIGPW are not compatible")
410 END IF
411
412 do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
413 IF (do_ppl) THEN
414 cpassert(.NOT. gapw)
415 CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
416 END IF
417
418 IF (gapw_xc) THEN
419 cpassert(ASSOCIATED(rho_xc))
420 END IF
421
422 ! gets the tmp grids
423 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
424
425 IF (gapw .AND. (poisson_env%parameters%solver == pw_poisson_implicit)) THEN
426 cpabort("The implicit Poisson solver cannot be used in conjunction with GAPW.")
427 END IF
428
429 ! *** Prepare densities for gapw ***
430 IF (gapw .OR. gapw_xc) THEN
431 CALL prepare_gapw_den(qs_env, do_rho0=(.NOT. gapw_xc))
432 END IF
433
434 ! Calculate the Hartree potential
435 CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
436 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
437
438 scf_section => section_vals_get_subs_vals(input, "DFT%SCF")
439 IF (btest(cp_print_key_should_output(logger%iter_info, scf_section, &
440 "PRINT%DETAILED_ENERGY"), &
441 cp_p_file) .AND. &
442 (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
443 (.NOT. (poisson_env%parameters%solver == pw_poisson_implicit))) THEN
444 CALL pw_zero(rho_tot_gspace)
445 CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density=.true.)
446 CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%e_hartree, &
447 v_hartree_gspace)
448 CALL pw_zero(rho_tot_gspace)
449 CALL pw_zero(v_hartree_gspace)
450 END IF
451
452 ! Get the total density in g-space [ions + electrons]
453 CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
454
455 IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc) THEN
456 cpabort("GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
457 END IF
458
459 ! Add the planar counter charge density
460 IF (dft_control%do_pcc) THEN
461 CALL planar_counter_charge(rho_tot_gspace, dft_control%pcc_control, auxbas_pw_pool)
462 END IF
463
464 IF (my_print) THEN
465 CALL print_densities(qs_env, rho)
466 END IF
467
468 IF (dft_control%do_sccs) THEN
469 ! Self-consistent continuum solvation (SCCS) model
470 NULLIFY (v_sccs_rspace)
471 ALLOCATE (v_sccs_rspace)
472 CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
473
474 IF (poisson_env%parameters%solver == pw_poisson_implicit) THEN
475 cpabort("The implicit Poisson solver cannot be used together with SCCS.")
476 END IF
477
478 IF (use_virial .AND. calculate_forces) THEN
479 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
480 h_stress=h_stress)
481 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe, dp)
482 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe, dp)
483 ELSE
484 CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
485 END IF
486 ELSE
487 ! Getting the Hartree energy and Hartree potential. Also getting the stress tensor
488 ! from the Hartree term if needed. No nuclear force information here
489 IF (use_virial .AND. calculate_forces) THEN
490 h_stress(:, :) = 0.0_dp
491 CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
492 v_hartree_gspace, h_stress=h_stress, &
493 rho_core=rho_core)
494 virial%pv_ehartree = virial%pv_ehartree + h_stress/real(para_env%num_pe, dp)
495 virial%pv_virial = virial%pv_virial + h_stress/real(para_env%num_pe, dp)
496 ELSE
497 CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
498 v_hartree_gspace, rho_core=rho_core)
499 END IF
500 END IF
501
502 IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce) THEN
503 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
504 CALL planar_averaged_v_hartree_3d(v_hartree_rspace, dft_control, qs_env%scf_control%gce%do_gce, &
505 qs_env%scf_control%gce%ref_esp, para_env)
506 END IF
507
508 ! In case decouple periodic images and/or apply restraints to charges
509 IF (do_ddapc) THEN
510 CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
511 v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
512 just_energy)
513 ELSE
514 dft_control%qs_control%ddapc_explicit_potential = .false.
515 dft_control%qs_control%ddapc_restraint_is_spin = .false.
516 IF (.NOT. just_energy) THEN
517 CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
518 CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
519 END IF
520 END IF
521 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
522
523 IF (dft_control%correct_surf_dip) THEN
524 IF (dft_control%surf_dip_correct_switch) THEN
525 CALL calc_dipsurf_potential(qs_env, energy)
526 energy%hartree = energy%hartree + energy%surf_dipole
527 END IF
528 END IF
529
530 ! SIC
531 CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
532 just_energy, calculate_forces, auxbas_pw_pool)
533
534 ! Check if CDFT constraint is needed
535 CALL qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
536
537 ! Adds the External Potential if requested
538 IF (dft_control%apply_external_potential) THEN
539 ! Compute the energy due to the external potential
540 ee_ener = 0.0_dp
541 DO ispin = 1, nspins
542 ee_ener = ee_ener + pw_integral_ab(rho_r(ispin), vee)
543 END DO
544 IF (.NOT. just_energy) THEN
545 IF (gapw) THEN
546 CALL get_qs_env(qs_env=qs_env, &
547 rho0_s_rs=rho0_s_rs, &
548 rhoz_cneo_s_rs=rhoz_cneo_s_rs)
549 cpassert(ASSOCIATED(rho0_s_rs))
550 IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
551 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
552 END IF
553 ee_ener = ee_ener + pw_integral_ab(rho0_s_rs, vee)
554 IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
555 CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs, -1.0_dp)
556 END IF
557 END IF
558 END IF
559 ! the sign accounts for the charge of the electrons
560 energy%ee = -ee_ener
561 END IF
562
563 ! Adds the QM/MM potential
564 IF (qs_env%qmmm) THEN
565 CALL qmmm_calculate_energy(qs_env=qs_env, &
566 rho=rho_r, &
567 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
568 qmmm_energy=energy%qmmm_el)
569 IF (qs_env%qmmm_env_qm%image_charge) THEN
570 CALL calculate_image_pot(v_hartree_rspace=v_hartree_rspace, &
571 rho_hartree_gspace=rho_tot_gspace, &
572 energy=energy, &
573 qmmm_env=qs_env%qmmm_env_qm, &
574 qs_env=qs_env)
575 IF (.NOT. just_energy) THEN
576 CALL add_image_pot_to_hartree_pot(v_hartree=v_hartree_rspace, &
577 v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
578 qs_env=qs_env)
579 IF (calculate_forces) THEN
581 potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
582 forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
583 qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
584 END IF
585 END IF
586 CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
587 DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
588 END IF
589 IF (.NOT. just_energy) THEN
590 CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
591 v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
592 END IF
593 END IF
594 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
595
596 ! SMEAGOL interface
597 IF (dft_control%smeagol_control%smeagol_enabled .AND. &
598 dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
599 cpassert(ASSOCIATED(dft_control%smeagol_control%aux))
600 CALL smeagol_shift_v_hartree(v_hartree_rspace, cell, &
601 dft_control%smeagol_control%aux%HartreeLeadsLeft, &
602 dft_control%smeagol_control%aux%HartreeLeadsRight, &
603 dft_control%smeagol_control%aux%HartreeLeadsBottom, &
604 dft_control%smeagol_control%aux%VBias, &
605 dft_control%smeagol_control%aux%minL, &
606 dft_control%smeagol_control%aux%maxR, &
607 dft_control%smeagol_control%aux%isexplicit_maxR, &
608 dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
609 END IF
610
611 ! calculate the density matrix for the fitted mo_coeffs
612 IF (dft_control%do_admm) THEN
613 IF (PRESENT(ext_xc_section)) THEN
614 CALL hfx_admm_init(qs_env, calculate_forces, ext_xc_section)
615 ELSE
616 CALL hfx_admm_init(qs_env, calculate_forces)
617 END IF
618
619 IF (dft_control%do_admm_mo) THEN
620 IF (qs_env%run_rtp) THEN
621 CALL rtp_admm_calc_rho_aux(qs_env)
622 ELSE
623 IF (dokp) THEN
624 CALL admm_mo_calc_rho_aux_kp(qs_env)
625 ELSE
626 CALL admm_mo_calc_rho_aux(qs_env)
627 END IF
628 END IF
629 ELSE IF (dft_control%do_admm_dm) THEN
630 CALL admm_dm_calc_rho_aux(qs_env)
631 END IF
632 END IF
633
634 ! only activate stress calculation if
635 IF (use_virial .AND. calculate_forces) virial%pv_calculate = .true.
636
637 ! *** calculate the xc potential on the pw density ***
638 ! *** associates v_rspace_new if the xc potential needs to be computed.
639 ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
640 IF (dft_control%do_admm) THEN
641 CALL get_qs_env(qs_env, admm_env=admm_env)
642 xc_section => admm_env%xc_section_aux
643 CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
644
645 ! here we ignore a possible vdW section in admm_env%xc_section_aux
646 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
647 vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
648 just_energy=just_energy_xc)
649
650 IF (admm_env%do_gapw) THEN
651 !compute the potential due to atomic densities
652 CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
653 kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
654 xc_section_external=xc_section, &
655 rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
656 calculate_forces=calculate_forces)
657
658 END IF
659
660 NULLIFY (rho_struct)
661
662 IF (use_virial .AND. calculate_forces) THEN
663 vscale = 1.0_dp
664 !Note: ADMMS and ADMMP stress tensor only for closed-shell calculations
665 IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
666 IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
667 virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
668 virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
669 ! virial%pv_xc will be zeroed in the xc routines
670 END IF
671 xc_section => admm_env%xc_section_primary
672 ELSE
673 xc_section => section_vals_get_subs_vals(input, "DFT%XC")
674 ! build ks matrix with an xc section potentially different from the one defined in input
675 IF (PRESENT(ext_xc_section)) xc_section => ext_xc_section
676 END IF
677
678 IF (gapw_xc) THEN
679 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
680 ELSE
681 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
682 END IF
683
684 ! zmp
685 IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc) THEN
686 energy%exc = 0.0_dp
687 CALL calculate_zmp_potential(qs_env, v_rspace_new, rho, exc=energy%exc)
688 ELSE
689 ! Embedding potential (runs regardless of XC method)
690 IF (dft_control%apply_embed_pot) THEN
691 NULLIFY (v_rspace_embed)
692 energy%embed_corr = 0.0_dp
693 CALL get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, &
694 energy%embed_corr, just_energy)
695 END IF
696
697 ! Everything else, either via GauXC or manual XC computation
698 native_skala_evaluator = xc_section_uses_native_skala_grid(xc_section)
699 paw_composite_representation = .false.
700 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc)) THEN
701 paw_composite_representation = xc_section_uses_gauxc_model(xc_section) .AND. &
704 native_skala_evaluator = native_skala_evaluator .OR. &
705 paw_composite_representation
706 END IF
707 IF (dft_control%use_gauxc) THEN
708 IF (native_skala_evaluator) THEN
709 IF (gapw .AND. dft_control%low_spin_roks) THEN
710 CALL cp_abort(__location__, &
711 "GAPW low spin ROKS requires the molecular GauXC grid, not native Skala grids.")
712 END IF
713 CALL ensure_native_skala_grid_scope(xc_section)
714 gapw_representation = skala_gapw_representation(xc_section)
715 IF (gapw .OR. gapw_xc) THEN
716 gapw_all_all_electron = gauxc_gapw_all_all_electron(qs_kind_set)
717 gapw_all_pseudopotentials = gauxc_gapw_all_pseudopotentials(qs_kind_set)
718 gapw_mixed_core_representations = &
719 .NOT. gapw_all_all_electron .AND. .NOT. gapw_all_pseudopotentials
720 END IF
721 native_direct_valence_atom_composite = &
722 xc_section_uses_native_skala_grid(xc_section) .AND. &
723 native_skala_uses_atom_composite_grid(xc_section) .AND. &
724 ((.NOT. gapw .AND. .NOT. gapw_xc) .OR. &
725 (gapw_representation == skala_gapw_direct_valence .AND. &
726 gapw_all_pseudopotentials))
727 native_gapw_composite_reference = &
728 native_skala_gapw_composite_reference(xc_section) .AND. (gapw .OR. gapw_xc)
729 native_gapw_atom_composite_requested = &
731 (gapw .OR. gapw_xc)
732 native_gapw_atom_composite_reference = &
733 native_gapw_atom_composite_requested .OR. paw_composite_representation
734 native_gapw_periodic_atom_composite = &
736 IF (gapw_mixed_core_representations .AND. &
737 .NOT. native_gapw_periodic_atom_composite) THEN
738 CALL cp_abort(__location__, &
739 "Mixed all-electron and pseudopotential GAPW kinds require "// &
740 "NATIVE_GRID_LAYOUT ATOM_COMPOSITE so that their primitive "// &
741 "fields are combined before Skala feature construction.")
742 END IF
743 native_gapw_atom_composite_reference = &
744 native_gapw_atom_composite_reference .OR. &
745 (gapw_mixed_core_representations .AND. &
746 native_gapw_periodic_atom_composite)
747 native_gapw_all_electron_atom_composite = &
748 (gapw .OR. gapw_xc) .AND. &
749 gapw_all_all_electron .AND. &
750 (.NOT. native_gapw_composite_reference) .AND. &
751 native_gapw_periodic_atom_composite
752 native_gapw_atom_composite_reference = &
753 native_gapw_atom_composite_reference .OR. &
754 native_gapw_all_electron_atom_composite
755 IF (paw_composite_representation) THEN
756 IF (any(cell%perd /= 0)) THEN
757 native_gapw_composite_reference = &
758 .NOT. native_gapw_periodic_atom_composite
759 native_gapw_atom_composite_reference = &
760 native_gapw_periodic_atom_composite
761 ELSE
762 native_gapw_composite_reference = .false.
763 native_gapw_atom_composite_reference = .true.
764 END IF
765 END IF
766 IF (native_gapw_composite_reference .AND. native_gapw_atom_composite_reference) THEN
767 CALL cp_abort(__location__, &
768 "Select only one native-grid GAPW composite reference route.")
769 END IF
770 native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
772 gauxc_section => get_gauxc_section(xc_section)
773 IF (ASSOCIATED(gauxc_section)) THEN
774 CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_DIAGNOSTICS", &
775 l_val=native_grid_diagnostics)
776 END IF
777 IF ((.NOT. do_kpoints) .AND. nimages /= 1) THEN
778 CALL cp_abort(__location__, &
779 "Native SKALA grid evaluation supports multiple images only "// &
780 "for k-point calculations.")
781 END IF
782 IF (do_kpoints) THEN
783 cpassert(ASSOCIATED(kpoints))
784 gauxc_section => get_gauxc_section(xc_section)
785 cpassert(ASSOCIATED(gauxc_section))
786 CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_USE_CUDA", l_val=native_grid_use_cuda)
787 IF (.NOT. native_grid_use_cuda) THEN
788 IF (para_env%mepos == 0 .AND. .NOT. native_grid_cpu_kpoints_warned) THEN
789 CALL cp_warn(__location__, &
790 "Native SKALA grid evaluation with k-points is using the CPU TorchScript "// &
791 "path. Use a mutually compatible BLAS, ScaLAPACK, OpenMP, and LibTorch "// &
792 "runtime stack, or select NATIVE_GRID_USE_CUDA T.")
793 native_grid_cpu_kpoints_warned = .true.
794 END IF
795 END IF
796 END IF
797 IF (dft_control%roks) THEN
798 cpabort("Native SKALA grid evaluation does not support ROKS.")
799 END IF
800 IF (dft_control%do_admm) THEN
801 cpabort("Native SKALA grid evaluation does not support ADMM.")
802 END IF
803 ! Force and stress rebuilds re-enter this path for derivatives and VXC only. The
804 ! autograd virial already contains the rebuilt XC volume term when qs_vxc_create
805 ! returns, so the externally reported energy must be restored to its SCF value.
806 native_skala_restore_exc = calculate_forces
807 IF (native_skala_restore_exc) THEN
808 native_skala_exc_scf = energy%exc
809 native_skala_total_scf = energy%total
810 END IF
811 IF (calculate_forces) THEN
812 ALLOCATE (native_skala_atom_force(3, natom))
813 native_skala_atom_force = 0.0_dp
814 IF (native_gapw_atom_composite_reference .OR. &
815 native_direct_valence_atom_composite) THEN
816 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
817 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
818 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
819 just_energy=just_energy_xc, qs_env_external=qs_env, &
820 native_gapw_composite_override=native_gapw_composite_reference, &
821 native_skala_defer_to_atom_composite=.true.)
822 ELSE
823 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
824 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
825 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
826 just_energy=just_energy_xc, &
827 native_skala_atom_force=native_skala_atom_force, &
828 qs_env_external=qs_env, &
829 native_gapw_composite_override=native_gapw_composite_reference)
830 END IF
831 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
832 ASSOCIATED(v_rspace_new) .AND. ASSOCIATED(v_tau_rspace)) THEN
833 IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
834 output_unit = cp_logger_get_default_io_unit()
835 IF (output_unit > 0) THEN
836 DO iatom = 1, natom
837 WRITE (unit=output_unit, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
838 "SKALA_GPW| Native regular-grid atom force", iatom, &
839 native_skala_atom_force(:, iatom)
840 END DO
841 END IF
842 END IF
843 IF (use_virial) THEN
844 native_skala_composite_virial = 0.0_dp
846 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
847 atom_force=native_skala_atom_force, &
848 strain_virial=native_skala_composite_virial, &
849 one_center_contraction=native_skala_composite_contraction, &
850 one_center_rho_contraction=native_skala_composite_rho_contraction, &
851 one_center_tau_contraction=native_skala_composite_tau_contraction)
852 IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
853 output_unit = cp_logger_get_default_io_unit()
854 IF (output_unit > 0) THEN
855 WRITE (unit=output_unit, fmt="(T2,A)") &
856 "SKALA_GPW| Composite-direction one-center virial"
857 DO iatom = 1, 3
858 WRITE (unit=output_unit, fmt="(T2,A,1X,3ES20.10)") &
859 "SKALA_GPW|", native_skala_composite_virial(iatom, :)
860 END DO
861 END IF
862 END IF
863 virial%pv_xc = virial%pv_xc + native_skala_composite_virial
864 ELSE
866 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
867 atom_force=native_skala_atom_force, &
868 one_center_contraction=native_skala_composite_contraction, &
869 one_center_rho_contraction=native_skala_composite_rho_contraction, &
870 one_center_tau_contraction=native_skala_composite_tau_contraction)
871 END IF
872 END IF
873 IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
874 output_unit = cp_logger_get_default_io_unit()
875 IF (output_unit > 0) THEN
876 WRITE (unit=output_unit, fmt="(T2,A,1X,ES20.10)") &
877 "SKALA_GPW| Composite-direction one-center VXC contraction", &
878 native_skala_composite_contraction
879 WRITE (unit=output_unit, fmt="(T2,A,1X,ES20.10)") &
880 "SKALA_GPW| Composite-direction one-center rho contraction", &
881 native_skala_composite_rho_contraction
882 WRITE (unit=output_unit, fmt="(T2,A,1X,ES20.10)") &
883 "SKALA_GPW| Composite-direction one-center tau contraction", &
884 native_skala_composite_tau_contraction
885 DO iatom = 1, natom
886 WRITE (unit=output_unit, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
887 "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
888 END DO
889 END IF
890 END IF
891 cpassert(ASSOCIATED(force))
892 cpassert(ASSOCIATED(atomic_kind_set))
893 CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
894 DO iatom = 1, natom
895 ikind = kind_of(iatom)
896 atom_a = atom_of_kind(iatom)
897 force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
898 native_skala_atom_force(:, iatom)
899 END DO
900 DEALLOCATE (atom_of_kind, kind_of, native_skala_atom_force)
901 ELSE
902 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
903 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
904 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
905 just_energy=just_energy_xc, qs_env_external=qs_env, &
906 native_gapw_composite_override=native_gapw_composite_reference, &
907 native_skala_defer_to_atom_composite= &
908 (native_gapw_atom_composite_reference .OR. &
909 native_direct_valence_atom_composite))
910 IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
911 ASSOCIATED(v_rspace_new) .AND. ASSOCIATED(v_tau_rspace)) THEN
913 qs_env, auxbas_pw_pool, v_rspace_new, v_tau_rspace, &
914 one_center_contraction=native_skala_composite_contraction, &
915 one_center_rho_contraction=native_skala_composite_rho_contraction, &
916 one_center_tau_contraction=native_skala_composite_tau_contraction)
917 IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
918 output_unit = cp_logger_get_default_io_unit()
919 IF (output_unit > 0) THEN
920 WRITE (unit=output_unit, fmt="(T2,A,1X,ES20.10)") &
921 "SKALA_GPW| Composite-direction one-center VXC contraction", &
922 native_skala_composite_contraction
923 WRITE (unit=output_unit, fmt="(T2,A,1X,ES20.10)") &
924 "SKALA_GPW| Composite-direction one-center rho contraction", &
925 native_skala_composite_rho_contraction
926 WRITE (unit=output_unit, fmt="(T2,A,1X,ES20.10)") &
927 "SKALA_GPW| Composite-direction one-center tau contraction", &
928 native_skala_composite_tau_contraction
929 END IF
930 END IF
931 END IF
932 END IF
933 IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
934 IF (native_direct_valence_atom_composite) THEN
935 cpassert(ASSOCIATED(v_rspace_new))
936 cpassert(ASSOCIATED(v_tau_rspace))
937 CALL calculate_vxc_atom( &
938 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
939 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
940 composite_vxc_tau=v_tau_rspace, direct_valence_atom_grid=.true.)
941 energy%exc = 0.0_dp
942 ELSE IF ((gapw .OR. gapw_xc) .AND. .NOT. native_gapw_composite_reference) THEN
943 IF (native_gapw_atom_composite_reference) THEN
944 cpassert(ASSOCIATED(v_rspace_new))
945 cpassert(ASSOCIATED(v_tau_rspace))
946 CALL calculate_vxc_atom( &
947 qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section, &
948 calculate_forces=calculate_forces, composite_vxc_rho=v_rspace_new, &
949 composite_vxc_tau=v_tau_rspace, atom_composite_grid=.true.)
950 energy%exc = 0.0_dp
951 ELSE
952 CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
953 xc_section_external=xc_section, &
954 calculate_forces=calculate_forces)
955 END IF
956 END IF
957 IF (edisp /= 0.0_dp) energy%dispersion = edisp
958 IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
959 CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, &
960 matrix_vxc=matrix_vxc, &
961 gapw_full_basis=native_gapw_composite_direct_ao)
962 CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc)
963 END IF
964 ELSE
965 use_gauxc_matrix = .true.
966 CALL apply_gauxc(qs_env, xc_section, calculate_forces)
967 IF (gapw_xc .OR. (gapw .AND. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
968 CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
969 xc_section_external=xc_section, &
970 calculate_forces=calculate_forces)
971 END IF
972 END IF
973 ELSE
974 CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
975 vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
976 edisp=edisp, dispersion_env=qs_env%dispersion_env, &
977 just_energy=just_energy_xc)
978 IF (edisp /= 0.0_dp) energy%dispersion = edisp
979 IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
980 CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
981 CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc)
982 END IF
983
984 IF (gapw .OR. gapw_xc) THEN
985 CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
986 xc_section_external=xc_section, &
987 calculate_forces=calculate_forces)
988 END IF
989 END IF
990 END IF
991
992 IF (gapw .AND. dft_control%qs_control%cdft) THEN
993 ALLOCATE (cdft_gapw_values(SIZE(cdft_control%group)))
994 IF (cdft_control%atomic_charges) THEN
995 ALLOCATE (cdft_charge_correction(natom, nspins), &
996 cdft_electronic_charge(cdft_control%natoms, nspins))
997 CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, &
998 cdft_gapw_values, cdft_charge_correction)
999 DO ispin = 1, nspins
1000 DO iatom = 1, cdft_control%natoms
1001 cdft_electronic_charge(iatom, ispin) = &
1002 pw_integral_ab(cdft_control%charge(iatom), rho_r(ispin), local_only=.true.)
1003 END DO
1004 END DO
1005 CALL para_env%sum(cdft_electronic_charge)
1006 DO ispin = 1, nspins
1007 DO iatom = 1, cdft_control%natoms
1008 cdft_electronic_charge(iatom, ispin) = &
1009 cdft_electronic_charge(iatom, ispin) + &
1010 cdft_charge_correction(cdft_control%atoms(iatom), ispin)
1011 END DO
1012 END DO
1013 ELSE
1014 CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, cdft_gapw_values)
1015 END IF
1016 cdft_control%value = cdft_control%value + cdft_gapw_values
1017 energy%cdft = energy%cdft + dot_product(cdft_control%strength, cdft_gapw_values)
1018 IF (cdft_control%atomic_charges) THEN
1019 CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
1020 DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
1021 ELSE
1022 CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
1023 END IF
1024 DEALLOCATE (cdft_gapw_values)
1025 END IF
1026
1027 IF (dft_control%mtlr_dft_with_perturbation .AND. .NOT. just_energy) THEN
1028 IF (do_adiabatic_rescaling .OR. use_gauxc_matrix) THEN
1029 cpabort("MTLR requires an XC potential on the real-space grid.")
1030 END IF
1031 IF (.NOT. ASSOCIATED(matrix_vhxc)) THEN
1032 ALLOCATE (matrix_vhxc(nspins))
1033 DO ispin = 1, nspins
1034 NULLIFY (matrix_vhxc(ispin)%matrix)
1035 ALLOCATE (matrix_vhxc(ispin)%matrix)
1036 CALL dbcsr_create(matrix_vhxc(ispin)%matrix, &
1037 template=ks_matrix(1, 1)%matrix)
1038 CALL cp_dbcsr_alloc_block_from_nbl(matrix_vhxc(ispin)%matrix, sab_orb)
1039 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1040 END DO
1041 CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
1042 ELSE
1043 cpassert(SIZE(matrix_vhxc) == nspins)
1044 DO ispin = 1, nspins
1045 cpassert(ASSOCIATED(matrix_vhxc(ispin)%matrix))
1046 END DO
1047 END IF
1048 DO ispin = 1, nspins
1049 CALL auxbas_pw_pool%create_pw(v_hxc_rspace(ispin))
1050 CALL pw_zero(v_hxc_rspace(ispin))
1051 CALL pw_axpy(v_hartree_rspace, v_hxc_rspace(ispin))
1052 IF (ASSOCIATED(v_rspace_new)) THEN
1053 CALL pw_axpy(v_rspace_new(ispin), &
1054 v_hxc_rspace(ispin), &
1055 v_rspace_new(ispin)%pw_grid%dvol)
1056 END IF
1057 CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1058 CALL integrate_v_rspace(v_hxc_rspace(ispin), &
1059 hmat=matrix_vhxc(ispin), &
1060 qs_env=qs_env, &
1061 calculate_forces=.false.)
1062 CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
1063 END DO
1064 END IF
1065
1066 ! set hartree and xc potentials for use in Harris method
1067 IF (qs_env%harris_method) THEN
1068 CALL get_qs_env(qs_env, harris_env=harris_env)
1069 CALL harris_set_potentials(harris_env, v_hartree_rspace, v_rspace_new)
1070 END IF
1071
1072 NULLIFY (rho_struct)
1073 IF (use_virial .AND. calculate_forces) THEN
1074 virial%pv_exc = virial%pv_exc - virial%pv_xc
1075 virial%pv_virial = virial%pv_virial - virial%pv_xc
1076 END IF
1077
1078 ! *** Add Hartree-Fock contribution if required ***
1079 hfx_sections => section_vals_get_subs_vals(xc_section, "HF")
1080 CALL section_vals_get(hfx_sections, explicit=do_hfx)
1081
1082 ace_active = .false.
1083 ace_rebuild_frequency = 1
1084
1085 IF (do_hfx) THEN
1086 ace_section => section_vals_get_subs_vals(hfx_sections, "ACE")
1087 IF (ASSOCIATED(ace_section)) THEN
1088 CALL section_vals_val_get(ace_section, "ACTIVE", l_val=ace_active)
1089 CALL section_vals_val_get(ace_section, "REBUILD_FREQUENCY", i_val=ace_rebuild_frequency)
1090 END IF
1091 END IF
1092
1093 IF (do_hfx) THEN
1094 IF (dokp) THEN
1095 IF (ace_active) THEN
1096 cpabort("ACE-HFX for k-points is not implemented yet")
1097 ELSE
1098 CALL hfx_ks_matrix_kp(qs_env, ks_matrix, energy, calculate_forces)
1099 END IF
1100
1101 ELSE
1102 ! ext_xc_section may contain a hfx section
1103 IF (ace_active) THEN
1104 CALL hfx_ace_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1105 just_energy, v_rspace_new, v_tau_rspace, &
1106 ace_rebuild_frequency, ext_xc_section=xc_section)
1107 ELSE
1108 CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1109 just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
1110 END IF
1111 END IF
1112 END IF !do_hfx
1113
1114 IF (do_ppl .AND. calculate_forces) THEN
1115 cpassert(.NOT. gapw)
1116 DO ispin = 1, nspins
1117 CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
1118 END DO
1119 END IF
1120
1121 ! The atom-composite evaluator differentiates its atom-centered NLCC field directly.
1122 IF (ASSOCIATED(rho_nlcc) .AND. calculate_forces .AND. &
1123 .NOT. native_gapw_atom_composite_reference .AND. &
1124 .NOT. native_direct_valence_atom_composite) THEN
1125 DO ispin = 1, nspins
1126 CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
1127 IF (dft_control%do_admm) CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
1128 END DO
1129 END IF
1130
1131 ! calculate KG correction
1132 IF (dft_control%qs_control%do_kg .AND. just_energy) THEN
1133
1134 cpassert(nimages == 1)
1135 ksmat => ks_matrix(:, 1)
1136 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1137
1138 ! subtract kg corr from the total energy
1139 energy%exc = energy%exc - ekin_mol
1140
1141 END IF
1142
1143 ! *** Single atom contributions ***
1144 IF (.NOT. just_energy) THEN
1145 IF (calculate_forces) THEN
1146 ! Getting nuclear force contribution from the core charge density
1147 IF ((poisson_env%parameters%solver == pw_poisson_implicit) .AND. &
1148 (poisson_env%parameters%dielectric_params%dielec_core_correction)) THEN
1149 block
1150 TYPE(pw_r3d_rs_type) :: v_minus_veps
1151 CALL auxbas_pw_pool%create_pw(v_minus_veps)
1152 CALL pw_copy(v_hartree_rspace, v_minus_veps)
1153 CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
1154 CALL integrate_v_core_rspace(v_minus_veps, qs_env)
1155 CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
1156 END block
1157 ELSE
1158 CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
1159 END IF
1160 ! SCCS with core fill: the dielectric function depends on rho_elec + rho_fill(R),
1161 ! which yields an additional nuclear force contribution from v_sccs. The fill
1162 ! Gaussians enter integrate_v_core_rspace with the sign convention of rho_core
1163 ! (-ccore*exp(-calpha*r**2)), hence the negated potential
1164 IF (dft_control%do_sccs) THEN
1165 IF (dft_control%sccs_control%core_fill) THEN
1166 block
1167 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: calpha, ccore
1168 TYPE(pw_r3d_rs_type) :: v_minus_vsccs
1169 CALL sccs_core_fill_coefficients(qs_env, calpha, ccore)
1170 CALL auxbas_pw_pool%create_pw(v_minus_vsccs)
1171 CALL pw_copy(v_sccs_rspace, v_minus_vsccs)
1172 CALL pw_scale(v_minus_vsccs, -1.0_dp)
1173 CALL integrate_v_core_rspace(v_minus_vsccs, qs_env, calpha=calpha, ccore=ccore)
1174 CALL auxbas_pw_pool%give_back_pw(v_minus_vsccs)
1175 DEALLOCATE (calpha, ccore)
1176 END block
1177 END IF
1178 END IF
1179 END IF
1180
1181 IF (.NOT. do_hfx) THEN
1182 ! Initialize the Kohn-Sham matrix with the core Hamiltonian matrix
1183 ! (sets ks sparsity equal to matrix_h sparsity)
1184 DO ispin = 1, nspins
1185 DO img = 1, nimages
1186 CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name) ! keep the name
1187 CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
1188 END DO
1189 END DO
1190 ! imaginary part if required
1191 IF (qs_env%run_rtp) THEN
1192 IF (dft_control%rtp_control%velocity_gauge) THEN
1193 cpassert(ASSOCIATED(matrix_h_im))
1194 cpassert(ASSOCIATED(ks_matrix_im))
1195 DO ispin = 1, nspins
1196 DO img = 1, nimages
1197 CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name) ! keep the name
1198 CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
1199 END DO
1200 END DO
1201 END IF
1202 END IF
1203 END IF
1204
1205 IF (use_virial .AND. calculate_forces) THEN
1206 pv_loc = virial%pv_virial
1207 END IF
1208 ! sum up potentials and integrate
1209 ! Pointing my_rho to the density matrix rho_ao
1210 my_rho => rho_ao
1211
1212 CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
1213 v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
1214 v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
1215 cdft_control, calculate_forces)
1216
1217 IF (use_gauxc_matrix) THEN
1218 CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc)
1219 cpassert(ASSOCIATED(matrix_vxc))
1220 DO ispin = 1, nspins
1221 DO img = 1, nimages
1222 CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc(ispin, img)%matrix, &
1223 1.0_dp, 1.0_dp)
1224 END DO
1225 END DO
1226 END IF
1227
1228 IF (gapw .OR. gapw_xc) THEN
1229 IF (calculate_forces) THEN
1230 IF (gapw_xc) THEN
1231 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
1232 ELSE
1233 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
1234 END IF
1235 NULLIFY (rho1)
1236 IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
1237 .NOT. native_skala_evaluator) THEN
1238 gauxc_model_none = .false.
1239 gauxc_section => get_gauxc_section(xc_section)
1240 IF (ASSOCIATED(gauxc_section)) THEN
1241 CALL section_vals_val_get(gauxc_section, "MODEL", c_val=gauxc_model_name)
1242 gauxc_model_name = adjustl(gauxc_model_name)
1243 CALL uppercase(gauxc_model_name)
1244 gauxc_model_none = (trim(gauxc_model_name) == "" .OR. &
1245 trim(gauxc_model_name) == "NONE")
1246 END IF
1247 IF (gauxc_model_none .AND. &
1248 (gapw_xc .OR. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
1249 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1250 END IF
1251 ELSE
1252 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1253 END IF
1254 !
1255 IF (dft_control%do_admm) THEN
1256 CALL get_qs_env(qs_env, admm_env=admm_env)
1257 xc_section => admm_env%xc_section_aux
1258 CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
1259 vscale = 1.0_dp
1260 IF (admm_env%do_admmp) THEN
1261 vscale = admm_env%gsi(1)**2
1262 ELSE IF (admm_env%do_admms) THEN
1263 vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
1264 END IF
1265 CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
1266 END IF
1267 END IF
1268 END IF
1269
1270 IF (use_virial .AND. calculate_forces) THEN
1271 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1272 END IF
1273 IF (dft_control%qs_control%do_kg) THEN
1274 cpassert(nimages == 1)
1275 ksmat => ks_matrix(:, 1)
1276
1277 IF (use_virial .AND. calculate_forces) THEN
1278 pv_loc = virial%pv_virial
1279 END IF
1280
1281 CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.false.)
1282 ! subtract kg corr from the total energy
1283 energy%exc = energy%exc - ekin_mol
1284
1285 ! virial corrections
1286 IF (use_virial .AND. calculate_forces) THEN
1287
1288 ! Integral contribution
1289 virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1290
1291 ! GGA contribution
1292 virial%pv_exc = virial%pv_exc + virial%pv_xc
1293 virial%pv_virial = virial%pv_virial + virial%pv_xc
1294 virial%pv_xc = 0.0_dp
1295 END IF
1296 END IF
1297
1298 ELSE
1299 ! Atom-composite XC also creates potential buffers for energy-only calls.
1300 ! These calls skip sum_up_and_integrate, which normally returns the buffers.
1301 IF (ASSOCIATED(v_rspace_new)) THEN
1302 DO ispin = 1, SIZE(v_rspace_new)
1303 CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
1304 END DO
1305 DEALLOCATE (v_rspace_new)
1306 END IF
1307 IF (ASSOCIATED(v_tau_rspace)) THEN
1308 DO ispin = 1, SIZE(v_tau_rspace)
1309 CALL auxbas_pw_pool%give_back_pw(v_tau_rspace(ispin))
1310 END DO
1311 DEALLOCATE (v_tau_rspace)
1312 END IF
1313 IF (do_hfx) THEN
1314 IF (.false.) THEN
1315 cpwarn("KS matrix no longer correct. Check possible problems with property calculations!")
1316 END IF
1317 END IF
1318 END IF ! .NOT. just energy
1319 IF (dft_control%qs_control%ddapc_explicit_potential) THEN
1320 CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
1321 DEALLOCATE (v_spin_ddapc_rest_r)
1322 END IF
1323
1324 IF (calculate_forces .AND. dft_control%qs_control%cdft) THEN
1325 IF (.NOT. cdft_control%transfer_pot) THEN
1326 DO iatom = 1, SIZE(cdft_control%group)
1327 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1328 DEALLOCATE (cdft_control%group(iatom)%weight)
1329 END DO
1330 IF (cdft_control%atomic_charges) THEN
1331 DO iatom = 1, cdft_control%natoms
1332 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1333 END DO
1334 DEALLOCATE (cdft_control%charge)
1335 END IF
1336 IF (cdft_control%type == outer_scf_becke_constraint .AND. &
1337 cdft_control%becke_control%cavity_confine) THEN
1338 IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
1339 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1340 ELSE
1341 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1342 END IF
1343 ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
1344 IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
1345 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1346 END IF
1347 END IF
1348 IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
1349 cdft_control%save_pot = .false.
1350 cdft_control%need_pot = .true.
1351 cdft_control%external_control = .false.
1352 END IF
1353 END IF
1354
1355 IF (dft_control%do_sccs) THEN
1356 CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
1357 DEALLOCATE (v_sccs_rspace)
1358 END IF
1359
1360 IF (gapw) THEN
1361 IF (dft_control%apply_external_potential) THEN
1362 ! Integrals of the Hartree potential with g0_soft
1363 CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
1364 v_qmmm=vee, scale=-1.0_dp)
1365 END IF
1366 CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
1367 ! Place Vh_1c_gg_integrals after integrate_vhg0_rspace for CNEO calculations
1368 ! because vhg0 integral is needed to build the complete nuclear equation
1369 CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
1370 CALL vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.false., &
1371 core_2nd=.false.)
1372 ! CNEO quantum nuclear core energy (kinetic + Z*erfc(r)/r potential from classical nuclei)
1373 energy%core_cneo = 0.0_dp
1374 IF (ASSOCIATED(local_rho_set%rhoz_cneo_set)) THEN
1375 DO iatom = 1, SIZE(local_rho_set%rhoz_cneo_set)
1376 energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
1377 END DO
1378 END IF
1379 END IF
1380
1381 IF (gapw .OR. gapw_xc) THEN
1382 ! Single atom contributions in the KS matrix ***
1383 CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
1384 IF (dft_control%do_admm) THEN
1385 !Single atom contribution to the AUX matrices
1386 !Note: also update ks_aux_fit matrix in case of rtp
1387 CALL admm_update_ks_atom(qs_env, calculate_forces)
1388 END IF
1389 END IF
1390
1391 !Calculation of Mulliken restraint, if requested
1392 CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
1393 ks_matrix, matrix_s, rho, mulliken_order_p)
1394
1395 ! Add DFT+U contribution, if requested
1396 IF (dft_control%dft_plus_u) THEN
1397 IF (just_energy) THEN
1398 CALL plus_u(qs_env=qs_env)
1399 ELSE
1400 CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
1401 END IF
1402 ELSE
1403 energy%dft_plus_u = 0.0_dp
1404 END IF
1405
1406 ! At this point the ks matrix should be up to date, filter it if requested
1407 DO ispin = 1, nspins
1408 DO img = 1, nimages
1409 CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
1410 dft_control%qs_control%eps_filter_matrix)
1411 END DO
1412 END DO
1413
1414 !** merge the auxiliary KS matrix and the primary one
1415 IF (dft_control%do_admm_mo) THEN
1416 IF (qs_env%run_rtp) THEN
1417 CALL rtp_admm_merge_ks_matrix(qs_env)
1418 ELSE
1419 CALL admm_mo_merge_ks_matrix(qs_env)
1420 END IF
1421 ELSE IF (dft_control%do_admm_dm) THEN
1422 CALL admm_dm_merge_ks_matrix(qs_env)
1423 END IF
1424
1425 ! External field (nonperiodic case)
1426 CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
1427
1428 ! Right now we can compute the orbital derivative here, as it depends currently only on the available
1429 ! Kohn-Sham matrix. This might change in the future, in which case more pieces might need to be assembled
1430 ! from this routine, notice that this part of the calculation in not linear scaling
1431 ! right now this operation is only non-trivial because of occupation numbers and the restricted keyword
1432 IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp) THEN
1433 CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
1434 cpassert(nimages == 1)
1435 ksmat => ks_matrix(:, 1)
1436 CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
1437 END IF
1438
1439 ! ADMM overlap forces
1440 IF (calculate_forces .AND. dft_control%do_admm) THEN
1441 IF (dokp) THEN
1442 CALL calc_admm_ovlp_forces_kp(qs_env)
1443 ELSE
1444 CALL calc_admm_ovlp_forces(qs_env)
1445 END IF
1446 END IF
1447
1448 ! deal with low spin roks
1449 CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
1450 calculate_forces, auxbas_pw_pool)
1451
1452 ! deal with sic on explicit orbitals
1453 CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
1454 calculate_forces, auxbas_pw_pool)
1455
1456 ! Periodic external field
1457 CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
1458
1459 ! adds s2_restraint energy and orbital derivatives
1460 CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
1461 energy, calculate_forces, just_energy)
1462
1463 IF (do_ppl) THEN
1464 ! update core energy for grid based local pseudopotential
1465 ecore_ppl = 0._dp
1466 DO ispin = 1, nspins
1467 ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
1468 END DO
1469 energy%core = energy%core + ecore_ppl
1470 END IF
1471
1472 IF (lrigpw) THEN
1473 ! update core energy for ppl_ri method
1474 CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
1475 IF (lri_env%ppl_ri) THEN
1476 ecore_ppl = 0._dp
1477 DO ispin = 1, nspins
1478 lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
1479 CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
1480 END DO
1481 energy%core = energy%core + ecore_ppl
1482 END IF
1483 END IF
1484
1485 ! Sum all energy terms to obtain the total energy
1486 IF (native_skala_restore_exc) THEN
1487 energy%total = native_skala_total_scf
1488 IF (abnormal_value(energy%total)) THEN
1489 CALL cp_abort(__location__, &
1490 "SCF energy restored by native Skala is an abnormal value (NaN/Inf).")
1491 END IF
1492 ELSE
1493 CALL check_sum_energies(energy)
1494 END IF
1495
1496 ! Print detailed energy
1497 IF (my_print) THEN
1498 CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
1499 END IF
1500
1501 CALL timestop(handle)
1502
1503 END SUBROUTINE qs_ks_build_kohn_sham_matrix
1504
1505! **************************************************************************************************
1506!> \brief ...
1507!> \param rho_tot_gspace ...
1508!> \param qs_env ...
1509!> \param rho ...
1510!> \param skip_nuclear_density ...
1511! **************************************************************************************************
1512 SUBROUTINE calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
1513 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: rho_tot_gspace
1514 TYPE(qs_environment_type), POINTER :: qs_env
1515 TYPE(qs_rho_type), POINTER :: rho
1516 LOGICAL, INTENT(IN), OPTIONAL :: skip_nuclear_density
1517
1518 INTEGER :: ispin
1519 LOGICAL :: my_skip
1520 TYPE(dft_control_type), POINTER :: dft_control
1521 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
1522 TYPE(pw_c1d_gs_type), POINTER :: rho0_s_gs, rho_core, rhoz_cneo_s_gs
1523 TYPE(qs_charges_type), POINTER :: qs_charges
1524
1525 my_skip = .false.
1526 IF (PRESENT(skip_nuclear_density)) my_skip = skip_nuclear_density
1527
1528 CALL qs_rho_get(rho, rho_g=rho_g)
1529 CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
1530
1531 IF (.NOT. my_skip) THEN
1532 NULLIFY (rho_core)
1533 CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
1534 IF (dft_control%qs_control%gapw) THEN
1535 NULLIFY (rho0_s_gs, rhoz_cneo_s_gs)
1536 CALL get_qs_env(qs_env=qs_env, rho0_s_gs=rho0_s_gs, rhoz_cneo_s_gs=rhoz_cneo_s_gs)
1537 cpassert(ASSOCIATED(rho0_s_gs))
1538 CALL pw_copy(rho0_s_gs, rho_tot_gspace)
1539 IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
1540 CALL pw_axpy(rhoz_cneo_s_gs, rho_tot_gspace)
1541 END IF
1542 IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
1543 CALL pw_axpy(rho_core, rho_tot_gspace)
1544 END IF
1545 ELSE
1546 CALL pw_copy(rho_core, rho_tot_gspace)
1547 END IF
1548 DO ispin = 1, dft_control%nspins
1549 CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
1550 END DO
1551 CALL get_qs_env(qs_env=qs_env, qs_charges=qs_charges)
1552 qs_charges%total_rho_gspace = pw_integrate_function(rho_tot_gspace, isign=-1)
1553 ELSE
1554 DO ispin = 1, dft_control%nspins
1555 CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
1556 END DO
1557 END IF
1558
1559 END SUBROUTINE calc_rho_tot_gspace
1560
1561! **************************************************************************************************
1562!> \brief compute MO derivatives
1563!> \param qs_env the qs_env to update
1564!> \param ks_matrix ...
1565!> \param mo_derivs ...
1566!> \par History
1567!> 01.2014 created, transferred from qs_ks_build_kohn_sham_matrix in
1568!> separate subroutine
1569!> \author Dorothea Golze
1570! **************************************************************************************************
1571 SUBROUTINE calc_mo_derivatives(qs_env, ks_matrix, mo_derivs)
1572 TYPE(qs_environment_type), POINTER :: qs_env
1573 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix, mo_derivs
1574
1575 INTEGER :: ispin
1576 LOGICAL :: uniform_occupation
1577 REAL(kind=dp), DIMENSION(:), POINTER :: occupation_numbers
1578 TYPE(cp_fm_type), POINTER :: mo_coeff
1579 TYPE(dbcsr_type) :: mo_derivs2_tmp1, mo_derivs2_tmp2
1580 TYPE(dbcsr_type), POINTER :: mo_coeff_b
1581 TYPE(dft_control_type), POINTER :: dft_control
1582 TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
1583
1584 NULLIFY (dft_control, mo_array, mo_coeff, mo_coeff_b, occupation_numbers)
1585
1586 CALL get_qs_env(qs_env, &
1587 dft_control=dft_control, &
1588 mos=mo_array)
1589
1590 DO ispin = 1, SIZE(mo_derivs)
1591
1592 CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, &
1593 mo_coeff_b=mo_coeff_b, occupation_numbers=occupation_numbers)
1594 CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin)%matrix, mo_coeff_b, &
1595 0.0_dp, mo_derivs(ispin)%matrix)
1596
1597 IF (dft_control%restricted) THEN
1598 ! only the first mo_set are actual variables, but we still need both
1599 cpassert(ispin == 1)
1600 cpassert(SIZE(mo_array) == 2)
1601 ! use a temporary array with the same size as the first spin for the second spin
1602
1603 ! uniform_occupation is needed for this case, otherwise we can not
1604 ! reconstruct things in ot, since we irreversibly sum
1605 CALL get_mo_set(mo_set=mo_array(1), uniform_occupation=uniform_occupation)
1606 cpassert(uniform_occupation)
1607 CALL get_mo_set(mo_set=mo_array(2), uniform_occupation=uniform_occupation)
1608 cpassert(uniform_occupation)
1609
1610 ! The beta-spin might have fewer orbitals than alpa-spin...
1611 ! create temporary matrices with beta_nmo columns
1612 CALL get_mo_set(mo_set=mo_array(2), mo_coeff_b=mo_coeff_b)
1613 CALL dbcsr_create(mo_derivs2_tmp1, template=mo_coeff_b)
1614
1615 ! calculate beta derivatives
1616 CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(2)%matrix, mo_coeff_b, 0.0_dp, mo_derivs2_tmp1)
1617
1618 ! create larger matrix with alpha_nmo columns
1619 CALL dbcsr_create(mo_derivs2_tmp2, template=mo_derivs(1)%matrix)
1620 CALL dbcsr_set(mo_derivs2_tmp2, 0.0_dp)
1621
1622 ! copy into larger matrix, fills the first beta_nmo columns
1623 CALL dbcsr_copy_columns_hack(mo_derivs2_tmp2, mo_derivs2_tmp1, &
1624 mo_array(2)%nmo, 1, 1, &
1625 para_env=mo_array(1)%mo_coeff%matrix_struct%para_env, &
1626 blacs_env=mo_array(1)%mo_coeff%matrix_struct%context)
1627
1628 ! add beta contribution to alpa mo_derivs
1629 CALL dbcsr_add(mo_derivs(1)%matrix, mo_derivs2_tmp2, 1.0_dp, 1.0_dp)
1630 CALL dbcsr_release(mo_derivs2_tmp1)
1631 CALL dbcsr_release(mo_derivs2_tmp2)
1632 END IF
1633 END DO
1634
1635 IF (dft_control%do_admm_mo) THEN
1636 CALL calc_admm_mo_derivatives(qs_env, mo_derivs)
1637 END IF
1638
1639 END SUBROUTINE calc_mo_derivatives
1640
1641! **************************************************************************************************
1642!> \brief updates the Kohn Sham matrix of the given qs_env (facility method)
1643!> \param qs_env the qs_env to update
1644!> \param calculate_forces if true calculate the quantities needed
1645!> to calculate the forces. Defaults to false.
1646!> \param just_energy if true updates the energies but not the
1647!> ks matrix. Defaults to false
1648!> \param print_active ...
1649!> \par History
1650!> 4.2002 created [fawzi]
1651!> 8.2014 kpoints [JGH]
1652!> 10.2014 refractored [Ole Schuett]
1653!> \author Fawzi Mohamed
1654! **************************************************************************************************
1655 SUBROUTINE qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, &
1656 print_active)
1657 TYPE(qs_environment_type), POINTER :: qs_env
1658 LOGICAL, INTENT(IN), OPTIONAL :: calculate_forces, just_energy, &
1659 print_active
1660
1661 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_ks_update_qs_env'
1662
1663 INTEGER :: handle, unit_nr
1664 LOGICAL :: c_forces, do_rebuild, energy_only, &
1665 forces_up_to_date, potential_changed, &
1666 rho_changed, s_mstruct_changed
1667 TYPE(qs_ks_env_type), POINTER :: ks_env
1668
1669 NULLIFY (ks_env)
1670 unit_nr = cp_logger_get_default_io_unit()
1671
1672 c_forces = .false.
1673 energy_only = .false.
1674 IF (PRESENT(just_energy)) energy_only = just_energy
1675 IF (PRESENT(calculate_forces)) c_forces = calculate_forces
1676
1677 IF (c_forces) THEN
1678 CALL timeset(routinen//'_forces', handle)
1679 ELSE
1680 CALL timeset(routinen, handle)
1681 END IF
1682
1683 cpassert(ASSOCIATED(qs_env))
1684
1685 CALL get_qs_env(qs_env, &
1686 ks_env=ks_env, &
1687 rho_changed=rho_changed, &
1688 s_mstruct_changed=s_mstruct_changed, &
1689 potential_changed=potential_changed, &
1690 forces_up_to_date=forces_up_to_date)
1691
1692 do_rebuild = .false.
1693 do_rebuild = do_rebuild .OR. rho_changed
1694 do_rebuild = do_rebuild .OR. s_mstruct_changed
1695 do_rebuild = do_rebuild .OR. potential_changed
1696 do_rebuild = do_rebuild .OR. (c_forces .AND. .NOT. forces_up_to_date)
1697
1698 IF (do_rebuild) THEN
1699 CALL evaluate_core_matrix_traces(qs_env)
1700
1701 ! the ks matrix will be rebuilt so this is fine now
1702 CALL set_ks_env(ks_env, potential_changed=.false.)
1703
1704 CALL rebuild_ks_matrix(qs_env, &
1705 calculate_forces=c_forces, &
1706 just_energy=energy_only, &
1707 print_active=print_active)
1708
1709 IF (.NOT. energy_only) THEN
1710 CALL set_ks_env(ks_env, &
1711 rho_changed=.false., &
1712 s_mstruct_changed=.false., &
1713 forces_up_to_date=forces_up_to_date .OR. c_forces)
1714 END IF
1715 END IF
1716
1717 CALL timestop(handle)
1718
1719 END SUBROUTINE qs_ks_update_qs_env
1720
1721! **************************************************************************************************
1722!> \brief Calculates the traces of the core matrices and the density matrix.
1723!> \param qs_env ...
1724!> \param rho_ao_ext ...
1725!> \author Ole Schuett
1726! **************************************************************************************************
1727 SUBROUTINE evaluate_core_matrix_traces(qs_env, rho_ao_ext)
1728 TYPE(qs_environment_type), POINTER :: qs_env
1729 TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
1730 POINTER :: rho_ao_ext
1731
1732 CHARACTER(LEN=*), PARAMETER :: routinen = 'evaluate_core_matrix_traces'
1733
1734 INTEGER :: handle
1735 REAL(kind=dp) :: energy_core_im
1736 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrixkp_h, matrixkp_t, rho_ao_kp
1737 TYPE(dft_control_type), POINTER :: dft_control
1738 TYPE(qs_energy_type), POINTER :: energy
1739 TYPE(qs_rho_type), POINTER :: rho
1740
1741 CALL timeset(routinen, handle)
1742 NULLIFY (energy, rho, dft_control, rho_ao_kp, matrixkp_t, matrixkp_h)
1743
1744 CALL get_qs_env(qs_env, &
1745 rho=rho, &
1746 energy=energy, &
1747 dft_control=dft_control, &
1748 kinetic_kp=matrixkp_t, &
1749 matrix_h_kp=matrixkp_h)
1750
1751 IF (PRESENT(rho_ao_ext)) THEN
1752 rho_ao_kp => rho_ao_ext
1753 ELSE
1754 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
1755 END IF
1756
1757 CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy%core, dft_control%nspins)
1758
1759 ! Add the imaginary part in the RTP case
1760 IF (qs_env%run_rtp) THEN
1761 IF (dft_control%rtp_control%velocity_gauge) THEN
1762 CALL get_qs_env(qs_env, matrix_h_im_kp=matrixkp_h)
1763 CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_kp)
1764 CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy_core_im, dft_control%nspins)
1765 energy%core = energy%core - energy_core_im
1766 END IF
1767 END IF
1768
1769 ! kinetic energy
1770 IF (ASSOCIATED(matrixkp_t)) THEN
1771 CALL calculate_ptrace(matrixkp_t, rho_ao_kp, energy%kinetic, dft_control%nspins)
1772 END IF
1773
1774 CALL timestop(handle)
1775 END SUBROUTINE evaluate_core_matrix_traces
1776
1777! **************************************************************************************************
1778!> \brief Constructs a new Khon-Sham matrix
1779!> \param qs_env ...
1780!> \param calculate_forces ...
1781!> \param just_energy ...
1782!> \param print_active ...
1783!> \author Ole Schuett
1784! **************************************************************************************************
1785 SUBROUTINE rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
1786 TYPE(qs_environment_type), POINTER :: qs_env
1787 LOGICAL, INTENT(IN) :: calculate_forces, just_energy
1788 LOGICAL, INTENT(IN), OPTIONAL :: print_active
1789
1790 CHARACTER(LEN=*), PARAMETER :: routinen = 'rebuild_ks_matrix'
1791
1792 INTEGER :: handle
1793 TYPE(dft_control_type), POINTER :: dft_control
1794 TYPE(qs_scf_env_type), POINTER :: scf_env
1795
1796 CALL timeset(routinen, handle)
1797 NULLIFY (dft_control, scf_env)
1798
1799 CALL get_qs_env(qs_env, dft_control=dft_control, scf_env=scf_env)
1800 IF (.NOT. just_energy .AND. ASSOCIATED(scf_env)) scf_env%ot_ks_valid = .false.
1801
1802 IF (dft_control%qs_control%semi_empirical) THEN
1803 CALL build_se_fock_matrix(qs_env, &
1804 calculate_forces=calculate_forces, &
1805 just_energy=just_energy)
1806
1807 ELSE IF (dft_control%qs_control%dftb) THEN
1808 CALL build_dftb_ks_matrix(qs_env, &
1809 calculate_forces=calculate_forces, &
1810 just_energy=just_energy)
1811
1812 ELSE IF (dft_control%qs_control%xtb) THEN
1813 IF (dft_control%qs_control%xtb_control%do_tblite) THEN
1814 CALL build_tblite_ks_matrix(qs_env, &
1815 calculate_forces=calculate_forces, &
1816 just_energy=just_energy)
1817 ELSE
1818 CALL build_xtb_ks_matrix(qs_env, &
1819 calculate_forces=calculate_forces, &
1820 just_energy=just_energy)
1821 END IF
1822 ELSE
1823 CALL qs_ks_build_kohn_sham_matrix(qs_env, &
1824 calculate_forces=calculate_forces, &
1825 just_energy=just_energy, &
1826 print_active=print_active)
1827 END IF
1828
1829 CALL timestop(handle)
1830
1831 END SUBROUTINE rebuild_ks_matrix
1832
1833! **************************************************************************************************
1834!> \brief Allocate ks_matrix if necessary, take current overlap matrix as template
1835!> \param qs_env ...
1836!> \param is_complex ...
1837!> \par History
1838!> refactoring 04.03.2011 [MI]
1839!> \author
1840! **************************************************************************************************
1841
1842 SUBROUTINE qs_ks_allocate_basics(qs_env, is_complex)
1843 TYPE(qs_environment_type), POINTER :: qs_env
1844 LOGICAL, INTENT(in) :: is_complex
1845
1846 CHARACTER(LEN=default_string_length) :: headline
1847 INTEGER :: ic, ispin, nimages, nspins
1848 LOGICAL :: do_kpoints
1849 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, matrixkp_im_ks, matrixkp_ks
1850 TYPE(dbcsr_type), POINTER :: refmatrix
1851 TYPE(dft_control_type), POINTER :: dft_control
1852 TYPE(kpoint_type), POINTER :: kpoints
1853 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1854 POINTER :: sab_orb
1855 TYPE(qs_ks_env_type), POINTER :: ks_env
1856
1857 NULLIFY (dft_control, ks_env, matrix_s_kp, sab_orb, matrixkp_ks, refmatrix, matrixkp_im_ks, kpoints)
1858
1859 CALL get_qs_env(qs_env, &
1860 dft_control=dft_control, &
1861 matrix_s_kp=matrix_s_kp, &
1862 ks_env=ks_env, &
1863 kpoints=kpoints, &
1864 do_kpoints=do_kpoints, &
1865 matrix_ks_kp=matrixkp_ks, &
1866 matrix_ks_im_kp=matrixkp_im_ks)
1867
1868 IF (do_kpoints) THEN
1869 CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
1870 ELSE
1871 CALL get_qs_env(qs_env, sab_orb=sab_orb)
1872 END IF
1873
1874 nspins = dft_control%nspins
1875 nimages = dft_control%nimages
1876
1877 IF (.NOT. ASSOCIATED(matrixkp_ks)) THEN
1878 CALL dbcsr_allocate_matrix_set(matrixkp_ks, nspins, nimages)
1879 refmatrix => matrix_s_kp(1, 1)%matrix
1880 DO ispin = 1, nspins
1881 DO ic = 1, nimages
1882 IF (nspins > 1) THEN
1883 IF (ispin == 1) THEN
1884 headline = "KOHN-SHAM MATRIX FOR ALPHA SPIN"
1885 ELSE
1886 headline = "KOHN-SHAM MATRIX FOR BETA SPIN"
1887 END IF
1888 ELSE
1889 headline = "KOHN-SHAM MATRIX"
1890 END IF
1891 ALLOCATE (matrixkp_ks(ispin, ic)%matrix)
1892 CALL dbcsr_create(matrix=matrixkp_ks(ispin, ic)%matrix, template=refmatrix, &
1893 name=trim(headline), matrix_type=dbcsr_type_symmetric)
1894 CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_ks(ispin, ic)%matrix, sab_orb)
1895 CALL dbcsr_set(matrixkp_ks(ispin, ic)%matrix, 0.0_dp)
1896 END DO
1897 END DO
1898 CALL set_ks_env(ks_env, matrix_ks_kp=matrixkp_ks)
1899 END IF
1900
1901 IF (is_complex) THEN
1902 IF (.NOT. ASSOCIATED(matrixkp_im_ks)) THEN
1903 cpassert(nspins == SIZE(matrixkp_ks, 1))
1904 cpassert(nimages == SIZE(matrixkp_ks, 2))
1905 CALL dbcsr_allocate_matrix_set(matrixkp_im_ks, nspins, nimages)
1906 DO ispin = 1, nspins
1907 DO ic = 1, nimages
1908 IF (nspins > 1) THEN
1909 IF (ispin == 1) THEN
1910 headline = "IMAGINARY KOHN-SHAM MATRIX FOR ALPHA SPIN"
1911 ELSE
1912 headline = "IMAGINARY KOHN-SHAM MATRIX FOR BETA SPIN"
1913 END IF
1914 ELSE
1915 headline = "IMAGINARY KOHN-SHAM MATRIX"
1916 END IF
1917 ALLOCATE (matrixkp_im_ks(ispin, ic)%matrix)
1918 refmatrix => matrixkp_ks(ispin, ic)%matrix ! base on real part, but anti-symmetric
1919 CALL dbcsr_create(matrix=matrixkp_im_ks(ispin, ic)%matrix, template=refmatrix, &
1920 name=trim(headline), matrix_type=dbcsr_type_antisymmetric)
1921 CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_im_ks(ispin, ic)%matrix, sab_orb)
1922 CALL dbcsr_set(matrixkp_im_ks(ispin, ic)%matrix, 0.0_dp)
1923 END DO
1924 END DO
1925 CALL set_ks_env(ks_env, matrix_ks_im_kp=matrixkp_im_ks)
1926 END IF
1927 END IF
1928
1929 END SUBROUTINE qs_ks_allocate_basics
1930
1931END 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:134
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:161
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 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 check_sum_energies(energy)
Check each term of the energy and sum to total.
subroutine, public compute_matrix_vxc(qs_env, v_rspace, matrix_vxc, gapw_full_basis)
Integrate the scalar XC potential into spin/image-resolved AO matrices. Gamma-only calculations have ...
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, cmo_coeff)
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:134
subroutine, public sccs_core_fill_coefficients(qs_env, calpha, ccore, output_unit)
Exponents and coefficients of the SCCS core fill Gaussians per atomic kind, such that the fill densit...
Definition qs_sccs.F:1569
module that contains the definitions of the scf types
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:120
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, rho_ao_external, exc_external, matrix_vxc_external)
Evaluate molecular GauXC, optionally for an explicit configuration density.
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.