Line data Source code
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 : ! **************************************************************************************************
22 : MODULE qs_ks_methods
23 : USE accint_weights_forces, ONLY: accint_weight_force
24 : USE admm_dm_methods, ONLY: admm_dm_calc_rho_aux,&
25 : admm_dm_merge_ks_matrix
26 : USE admm_methods, ONLY: admm_mo_calc_rho_aux,&
27 : admm_mo_calc_rho_aux_kp,&
28 : admm_mo_merge_ks_matrix,&
29 : admm_update_ks_atom,&
30 : calc_admm_mo_derivatives,&
31 : calc_admm_ovlp_forces,&
32 : calc_admm_ovlp_forces_kp
33 : USE admm_types, ONLY: admm_type,&
34 : get_admm_env
35 : USE atomic_kind_types, ONLY: atomic_kind_type,&
36 : get_atomic_kind_set
37 : USE cell_types, ONLY: cell_type
38 : USE cp_control_types, ONLY: dft_control_type
39 : USE cp_dbcsr_api, ONLY: &
40 : dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_filter, dbcsr_get_info, dbcsr_multiply, &
41 : dbcsr_p_type, dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
42 : dbcsr_type_symmetric
43 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
44 : USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set,&
45 : dbcsr_copy_columns_hack
46 : USE cp_ddapc, ONLY: qs_ks_ddapc
47 : USE cp_fm_types, ONLY: cp_fm_type
48 : USE cp_log_handling, ONLY: cp_get_default_logger,&
49 : cp_logger_get_default_io_unit,&
50 : cp_logger_type
51 : USE cp_output_handling, ONLY: cp_p_file,&
52 : cp_print_key_should_output
53 : USE dft_plus_u, ONLY: plus_u
54 : USE gapw_gspace_reconstruction, ONLY: integrate_gapw_composite_vxc_gspace
55 : USE gce_methods, ONLY: planar_averaged_v_hartree_3d,&
56 : planar_counter_charge
57 : USE hartree_local_methods, ONLY: Vh_1c_gg_integrals
58 : USE hartree_local_types, ONLY: ecoul_1center_type
59 : USE hfx_ace_methods, ONLY: hfx_ace_ks_matrix
60 : USE hfx_admm_utils, ONLY: hfx_admm_init,&
61 : hfx_ks_matrix,&
62 : hfx_ks_matrix_kp
63 : USE input_constants, ONLY: do_ppl_grid,&
64 : outer_scf_becke_constraint,&
65 : outer_scf_hirshfeld_constraint,&
66 : smeagol_runtype_emtransport
67 : USE input_section_types, ONLY: section_vals_get,&
68 : section_vals_get_subs_vals,&
69 : section_vals_type,&
70 : section_vals_val_get
71 : USE kg_correction, ONLY: kg_ekin_subset
72 : USE kinds, ONLY: default_string_length,&
73 : dp
74 : USE kpoint_types, ONLY: get_kpoint_info,&
75 : kpoint_type
76 : USE lri_environment_methods, ONLY: v_int_ppl_energy
77 : USE lri_environment_types, ONLY: lri_density_type,&
78 : lri_environment_type,&
79 : lri_kind_type
80 : USE mathlib, ONLY: abnormal_value
81 : USE message_passing, ONLY: mp_para_env_type
82 : USE particle_types, ONLY: particle_type
83 : USE pw_env_types, ONLY: pw_env_get,&
84 : pw_env_type
85 : USE pw_methods, ONLY: pw_axpy,&
86 : pw_copy,&
87 : pw_integral_ab,&
88 : pw_integrate_function,&
89 : pw_scale,&
90 : pw_transfer,&
91 : pw_zero
92 : USE pw_poisson_methods, ONLY: pw_poisson_solve
93 : USE pw_poisson_types, ONLY: pw_poisson_implicit,&
94 : pw_poisson_type
95 : USE pw_pool_types, ONLY: pw_pool_type
96 : USE pw_types, ONLY: pw_c1d_gs_type,&
97 : pw_r3d_rs_type
98 : USE qmmm_image_charge, ONLY: add_image_pot_to_hartree_pot,&
99 : calculate_image_pot,&
100 : integrate_potential_devga_rspace
101 : USE qs_cdft_types, ONLY: cdft_control_type
102 : USE qs_cdft_utils, ONLY: cdft_constraint_print
103 : USE qs_charges_types, ONLY: qs_charges_type
104 : USE qs_core_energies, ONLY: calculate_ptrace
105 : USE qs_dftb_matrices, ONLY: build_dftb_ks_matrix
106 : USE qs_efield_berry, ONLY: qs_efield_berry_phase
107 : USE qs_efield_local, ONLY: qs_efield_local_operator
108 : USE qs_energy_types, ONLY: qs_energy_type
109 : USE qs_environment_types, ONLY: get_qs_env,&
110 : qs_environment_type
111 : USE qs_force_types, ONLY: qs_force_type
112 : USE qs_gapw_densities, ONLY: prepare_gapw_den
113 : USE qs_harris_types, ONLY: harris_type
114 : USE qs_harris_utils, ONLY: harris_set_potentials
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
120 : USE qs_ks_apply_restraints, ONLY: qs_ks_cdft_constraint,&
121 : qs_ks_mulliken_restraint,&
122 : qs_ks_s2_restraint
123 : USE qs_ks_atom, ONLY: update_ks_atom
124 : USE qs_ks_qmmm_methods, ONLY: qmmm_calculate_energy,&
125 : qmmm_modify_hartree_pot
126 : USE qs_ks_types, ONLY: qs_ks_env_type,&
127 : set_ks_env
128 : USE qs_ks_utils, ONLY: &
129 : calc_v_sic_rspace, calculate_zmp_potential, check_sum_energies, compute_matrix_vxc, &
130 : compute_matrix_vxc_kp, get_embed_potential_energy, low_spin_roks, print_densities, &
131 : print_detailed_energy, sic_explicit_orbitals, sum_up_and_integrate
132 : USE qs_local_rho_types, ONLY: local_rho_type
133 : USE qs_mo_types, ONLY: get_mo_set,&
134 : mo_set_type
135 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
136 : USE qs_rho0_ggrid, ONLY: integrate_vhg0_rspace
137 : USE qs_rho_types, ONLY: qs_rho_get,&
138 : qs_rho_type
139 : USE qs_sccs, ONLY: sccs,&
140 : sccs_core_fill_coefficients
141 : USE qs_vxc, ONLY: qs_vxc_create
142 : USE qs_vxc_atom, ONLY: calculate_vxc_atom,&
143 : gapw_cdft_one_center
144 : USE rtp_admm_methods, ONLY: rtp_admm_calc_rho_aux,&
145 : rtp_admm_merge_ks_matrix
146 : USE se_fock_matrix, ONLY: build_se_fock_matrix
147 : USE skala_gpw_functional, ONLY: &
148 : ensure_native_skala_grid_scope, get_gauxc_section, &
149 : native_skala_gapw_atom_composite_requested, native_skala_gapw_composite_direct_ao, &
150 : native_skala_gapw_composite_reference, native_skala_uses_atom_composite_grid, &
151 : skala_gapw_representation, xc_section_uses_gauxc_model, xc_section_uses_native_skala_grid
152 : USE smeagol_interface, ONLY: smeagol_shift_v_hartree
153 : USE string_utilities, ONLY: uppercase
154 : USE surface_dipole, ONLY: calc_dipsurf_potential
155 : USE tblite_ks_matrix, ONLY: build_tblite_ks_matrix
156 : USE virial_types, ONLY: virial_type
157 : USE xc_gauxc_functional, ONLY: apply_gauxc,&
158 : gauxc_gapw_all_all_electron,&
159 : gauxc_gapw_all_pseudopotentials,&
160 : gauxc_gapw_has_paw_pseudopotentials
161 : USE xc_input_constants, ONLY: skala_gapw_direct_valence,&
162 : skala_gapw_paw_one_center
163 : USE xtb_ks_matrix, ONLY: build_xtb_ks_matrix
164 : #include "./base/base_uses.f90"
165 :
166 : IMPLICIT NONE
167 :
168 : PRIVATE
169 :
170 : LOGICAL, PARAMETER :: debug_this_module = .TRUE.
171 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_methods'
172 :
173 : PUBLIC :: calc_rho_tot_gspace, qs_ks_update_qs_env, qs_ks_build_kohn_sham_matrix, &
174 : qs_ks_allocate_basics, evaluate_core_matrix_traces, rebuild_ks_matrix
175 :
176 : CONTAINS
177 :
178 : ! **************************************************************************************************
179 : !> \brief routine where the real calculations are made: the
180 : !> KS matrix is calculated
181 : !> \param qs_env the qs_env to update
182 : !> \param calculate_forces if true calculate the quantities needed
183 : !> to calculate the forces. Defaults to false.
184 : !> \param just_energy if true updates the energies but not the
185 : !> ks matrix. Defaults to false
186 : !> \param print_active ...
187 : !> \param ext_ks_matrix ...
188 : !> \param ext_xc_section ...
189 : !> \par History
190 : !> 06.2002 moved from qs_scf to qs_ks_methods, use of ks_env
191 : !> new did_change scheme [fawzi]
192 : !> 10.2002 introduced pools, uses updated rho as input, LSD [fawzi]
193 : !> 10.2004 build_kohn_sham matrix now also computes the derivatives
194 : !> of the total energy wrt to the MO coefs, if instructed to
195 : !> do so. This appears useful for orbital dependent functionals
196 : !> where the KS matrix alone (however this might be defined)
197 : !> does not contain the info to construct this derivative.
198 : !> \author Matthias Krack
199 : !> \note
200 : !> make rho, energy and qs_charges optional, defaulting
201 : !> to qs_env components?
202 : ! **************************************************************************************************
203 136161 : SUBROUTINE qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, &
204 : print_active, ext_ks_matrix, ext_xc_section)
205 : TYPE(qs_environment_type), POINTER :: qs_env
206 : LOGICAL, INTENT(in) :: calculate_forces, just_energy
207 : LOGICAL, INTENT(IN), OPTIONAL :: print_active
208 : TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
209 : POINTER :: ext_ks_matrix
210 : TYPE(section_vals_type), OPTIONAL, POINTER :: ext_xc_section
211 :
212 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_build_kohn_sham_matrix'
213 :
214 : CHARACTER(len=default_string_length) :: gauxc_model_name, name
215 : INTEGER :: ace_rebuild_frequency, atom_a, gapw_representation, handle, iatom, ikind, img, &
216 : ispin, natom, nimages, nspins, output_unit
217 136161 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, kind_of
218 : LOGICAL :: ace_active, do_adiabatic_rescaling, do_ddapc, do_hfx, do_kpoints, do_ppl, dokp, &
219 : gapw, gapw_all_all_electron, gapw_all_pseudopotentials, gapw_mixed_core_representations, &
220 : gapw_xc, gauxc_model_none, just_energy_xc, lrigpw, my_print, &
221 : native_direct_valence_atom_composite, native_gapw_all_electron_atom_composite, &
222 : native_gapw_atom_composite_reference, native_gapw_atom_composite_requested, &
223 : native_gapw_composite_direct_ao, native_gapw_composite_reference, &
224 : native_gapw_periodic_atom_composite, native_grid_diagnostics, native_grid_use_cuda, &
225 : native_skala_evaluator, native_skala_restore_exc
226 : LOGICAL :: paw_composite_representation, rigpw, use_gauxc_matrix, use_virial
227 : LOGICAL, SAVE :: native_grid_cpu_kpoints_warned = .FALSE.
228 : REAL(KIND=dp) :: ecore_ppl, edisp, ee_ener, ekin_mol, mulliken_order_p, &
229 : native_skala_composite_contraction, native_skala_composite_rho_contraction, &
230 : native_skala_composite_tau_contraction, native_skala_exc_scf, native_skala_total_scf, &
231 : vscale
232 136161 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: cdft_gapw_values
233 136161 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: cdft_charge_correction, &
234 136161 : cdft_electronic_charge, &
235 136161 : native_skala_atom_force
236 : REAL(KIND=dp), DIMENSION(3, 3) :: h_stress, native_skala_composite_virial, &
237 : pv_loc
238 : TYPE(admm_type), POINTER :: admm_env
239 136161 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
240 : TYPE(cdft_control_type), POINTER :: cdft_control
241 : TYPE(cell_type), POINTER :: cell
242 : TYPE(cp_logger_type), POINTER :: logger
243 136161 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ksmat, matrix_vhxc, matrix_vxc, mo_derivs
244 136161 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: ks_matrix, ks_matrix_im, matrix_h, &
245 136161 : matrix_h_im, matrix_s, matrix_vxc_kp, &
246 136161 : my_rho, rho_ao
247 : TYPE(dft_control_type), POINTER :: dft_control
248 136161 : TYPE(ecoul_1center_type), DIMENSION(:), POINTER :: ecoul_1c
249 : TYPE(harris_type), POINTER :: harris_env
250 : TYPE(kpoint_type), POINTER :: kpoints
251 : TYPE(local_rho_type), POINTER :: local_rho_set
252 : TYPE(lri_density_type), POINTER :: lri_density
253 : TYPE(lri_environment_type), POINTER :: lri_env
254 136161 : TYPE(lri_kind_type), DIMENSION(:), POINTER :: lri_v_int
255 : TYPE(mp_para_env_type), POINTER :: para_env
256 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
257 136161 : POINTER :: sab_orb
258 136161 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
259 : TYPE(pw_c1d_gs_type) :: rho_tot_gspace, v_hartree_gspace
260 : TYPE(pw_c1d_gs_type), POINTER :: rho_core
261 : TYPE(pw_env_type), POINTER :: pw_env
262 : TYPE(pw_poisson_type), POINTER :: poisson_env
263 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
264 408483 : TYPE(pw_r3d_rs_type), DIMENSION(2) :: v_hxc_rspace
265 272322 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, v_rspace_embed, v_rspace_new, &
266 136161 : v_rspace_new_aux_fit, v_tau_rspace, &
267 136161 : v_tau_rspace_aux_fit
268 : TYPE(pw_r3d_rs_type), POINTER :: rho0_s_rs, rho_nlcc, rhoz_cneo_s_rs, v_hartree_rspace, &
269 : v_sccs_rspace, v_sic_rspace, v_spin_ddapc_rest_r, vee, vppl_rspace
270 : TYPE(qs_energy_type), POINTER :: energy
271 136161 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
272 136161 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
273 : TYPE(qs_ks_env_type), POINTER :: ks_env
274 : TYPE(qs_rho_type), POINTER :: rho, rho1, rho_struct, rho_xc
275 : TYPE(section_vals_type), POINTER :: ace_section, &
276 : adiabatic_rescaling_section, &
277 : gauxc_section, hfx_sections, input, &
278 : scf_section, xc_section
279 : TYPE(virial_type), POINTER :: virial
280 :
281 136161 : CALL timeset(routineN, handle)
282 136161 : NULLIFY (admm_env, atomic_kind_set, cell, dft_control, force, logger, mo_derivs, my_rho, &
283 136161 : rho_struct, para_env, pw_env, virial, vppl_rspace, &
284 136161 : ace_section, &
285 136161 : adiabatic_rescaling_section, hfx_sections, input, scf_section, &
286 136161 : xc_section, gauxc_section, matrix_h, matrix_h_im, matrix_s, auxbas_pw_pool, poisson_env, &
287 136161 : v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, &
288 136161 : matrix_vxc_kp, matrix_vhxc, &
289 136161 : vee, rho_nlcc, ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, &
290 136161 : rho_core, sab_orb, particle_set, qs_kind_set, kpoints)
291 :
292 136161 : CPASSERT(ASSOCIATED(qs_env))
293 :
294 136161 : logger => cp_get_default_logger()
295 136161 : my_print = .TRUE.
296 136161 : IF (PRESENT(print_active)) my_print = print_active
297 136161 : use_gauxc_matrix = .FALSE.
298 136161 : native_gapw_atom_composite_reference = .FALSE.
299 136161 : native_gapw_atom_composite_requested = .FALSE.
300 136161 : native_gapw_all_electron_atom_composite = .FALSE.
301 136161 : native_direct_valence_atom_composite = .FALSE.
302 136161 : gapw_all_all_electron = .FALSE.
303 136161 : gapw_all_pseudopotentials = .FALSE.
304 136161 : gapw_mixed_core_representations = .FALSE.
305 136161 : native_gapw_composite_direct_ao = .FALSE.
306 136161 : native_gapw_composite_reference = .FALSE.
307 136161 : native_gapw_periodic_atom_composite = .FALSE.
308 136161 : native_grid_diagnostics = .FALSE.
309 136161 : native_skala_composite_contraction = 0.0_dp
310 136161 : native_skala_composite_rho_contraction = 0.0_dp
311 136161 : native_skala_composite_tau_contraction = 0.0_dp
312 136161 : native_skala_restore_exc = .FALSE.
313 :
314 : CALL get_qs_env(qs_env, &
315 : ks_env=ks_env, &
316 : dft_control=dft_control, &
317 : matrix_h_kp=matrix_h, &
318 : matrix_h_im_kp=matrix_h_im, &
319 : matrix_s_kp=matrix_s, &
320 : matrix_ks_kp=ks_matrix, &
321 : matrix_ks_im_kp=ks_matrix_im, &
322 : matrix_vxc=matrix_vxc, &
323 : matrix_vhxc=matrix_vhxc, &
324 : matrix_vxc_kp=matrix_vxc_kp, &
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 136161 : natom=natom)
345 :
346 : ! Reject force/stress requests before assembling even the baseline KS matrix.
347 136161 : IF (dft_control%low_spin_roks .AND. dft_control%qs_control%gapw) THEN
348 136 : IF (.NOT. virial%pv_numer .AND. &
349 : (virial%pv_calculate .OR. (virial%pv_availability .AND. calculate_forces))) THEN
350 0 : CALL cp_abort(__LOCATION__, "GAPW low spin ROKS stress is not implemented.")
351 : END IF
352 136 : IF (calculate_forces) THEN
353 0 : CALL cp_abort(__LOCATION__, "GAPW low spin ROKS forces are not implemented.")
354 : END IF
355 : END IF
356 :
357 136161 : CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
358 :
359 136161 : nimages = dft_control%nimages
360 136161 : nspins = dft_control%nspins
361 :
362 : ! remap pointer to allow for non-kpoint external ks matrix
363 136161 : IF (PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
364 :
365 136161 : use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
366 :
367 136161 : adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
368 136161 : CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
369 136161 : just_energy_xc = just_energy
370 136161 : 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 44 : just_energy_xc = .TRUE.
374 : END IF
375 :
376 136161 : CPASSERT(ASSOCIATED(matrix_h))
377 136161 : CPASSERT(ASSOCIATED(matrix_s))
378 136161 : CPASSERT(ASSOCIATED(rho))
379 136161 : CPASSERT(ASSOCIATED(pw_env))
380 136161 : CPASSERT(SIZE(ks_matrix, 1) > 0)
381 136161 : 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 136161 : qs_env%cp_ddapc_ewald%do_solvation
388 :
389 : ! Check if LRIGPW is used
390 136161 : lrigpw = dft_control%qs_control%lrigpw
391 136161 : rigpw = dft_control%qs_control%rigpw
392 136161 : IF (rigpw) THEN
393 26 : CPASSERT(nimages == 1)
394 : END IF
395 26 : IF (lrigpw .AND. rigpw) THEN
396 0 : CPABORT(" LRI and RI are not compatible")
397 : END IF
398 :
399 : ! Check for GAPW method : additional terms for local densities
400 136161 : gapw = dft_control%qs_control%gapw
401 136161 : gapw_xc = dft_control%qs_control%gapw_xc
402 136161 : IF (gapw_xc .AND. gapw) THEN
403 0 : CPABORT(" GAPW and GAPW_XC are not compatible")
404 : END IF
405 136161 : IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw)) THEN
406 0 : CPABORT(" GAPW/GAPW_XC and LRIGPW are not compatible")
407 : END IF
408 136161 : IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw)) THEN
409 0 : CPABORT(" GAPW/GAPW_XC and RIGPW are not compatible")
410 : END IF
411 :
412 136161 : do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
413 136161 : IF (do_ppl) THEN
414 60 : CPASSERT(.NOT. gapw)
415 60 : CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
416 : END IF
417 :
418 136161 : IF (gapw_xc) THEN
419 4450 : CPASSERT(ASSOCIATED(rho_xc))
420 : END IF
421 :
422 : ! gets the tmp grids
423 136161 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
424 :
425 136161 : IF (gapw .AND. (poisson_env%parameters%solver == pw_poisson_implicit)) THEN
426 0 : CPABORT("The implicit Poisson solver cannot be used in conjunction with GAPW.")
427 : END IF
428 :
429 : ! *** Prepare densities for gapw ***
430 136161 : IF (gapw .OR. gapw_xc) THEN
431 28376 : CALL prepare_gapw_den(qs_env, do_rho0=(.NOT. gapw_xc))
432 : END IF
433 :
434 : ! Calculate the Hartree potential
435 136161 : CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
436 136161 : CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
437 :
438 136161 : 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 136161 : (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
443 : (.NOT. (poisson_env%parameters%solver == pw_poisson_implicit))) THEN
444 920 : CALL pw_zero(rho_tot_gspace)
445 920 : 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 920 : v_hartree_gspace)
448 920 : CALL pw_zero(rho_tot_gspace)
449 920 : CALL pw_zero(v_hartree_gspace)
450 : END IF
451 :
452 : ! Get the total density in g-space [ions + electrons]
453 136161 : CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
454 :
455 136161 : IF (qs_env%scf_control%gce%do_gce .AND. .NOT. dft_control%do_pcc) THEN
456 0 : CPABORT("GCE requires DFT%PLANAR_COUNTER_CHARGE to define the countercharge plane.")
457 : END IF
458 :
459 : ! Add the planar counter charge density
460 136161 : IF (dft_control%do_pcc) THEN
461 412 : CALL planar_counter_charge(rho_tot_gspace, dft_control%pcc_control, auxbas_pw_pool)
462 : END IF
463 :
464 136161 : IF (my_print) THEN
465 136105 : CALL print_densities(qs_env, rho)
466 : END IF
467 :
468 136161 : IF (dft_control%do_sccs) THEN
469 : ! Self-consistent continuum solvation (SCCS) model
470 : NULLIFY (v_sccs_rspace)
471 260 : ALLOCATE (v_sccs_rspace)
472 260 : CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
473 :
474 260 : IF (poisson_env%parameters%solver == pw_poisson_implicit) THEN
475 0 : CPABORT("The implicit Poisson solver cannot be used together with SCCS.")
476 : END IF
477 :
478 260 : IF (use_virial .AND. calculate_forces) THEN
479 : CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
480 0 : h_stress=h_stress)
481 0 : virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
482 0 : virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
483 : ELSE
484 260 : 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 135901 : IF (use_virial .AND. calculate_forces) THEN
490 540 : 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 540 : rho_core=rho_core)
494 7020 : virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
495 7020 : 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 135361 : v_hartree_gspace, rho_core=rho_core)
499 : END IF
500 : END IF
501 :
502 136161 : IF (dft_control%do_paep .OR. qs_env%scf_control%gce%do_gce) THEN
503 390 : 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 390 : 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 136161 : 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 2340 : just_energy)
513 : ELSE
514 133821 : dft_control%qs_control%ddapc_explicit_potential = .FALSE.
515 133821 : dft_control%qs_control%ddapc_restraint_is_spin = .FALSE.
516 133821 : IF (.NOT. just_energy) THEN
517 123483 : CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
518 123483 : CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
519 : END IF
520 : END IF
521 136161 : CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
522 :
523 136161 : IF (dft_control%correct_surf_dip) THEN
524 110 : IF (dft_control%surf_dip_correct_switch) THEN
525 110 : CALL calc_dipsurf_potential(qs_env, energy)
526 110 : 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 136161 : just_energy, calculate_forces, auxbas_pw_pool)
533 :
534 : ! Check if CDFT constraint is needed
535 136161 : CALL qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
536 :
537 : ! Adds the External Potential if requested
538 136161 : IF (dft_control%apply_external_potential) THEN
539 : ! Compute the energy due to the external potential
540 : ee_ener = 0.0_dp
541 728 : DO ispin = 1, nspins
542 728 : ee_ener = ee_ener + pw_integral_ab(rho_r(ispin), vee)
543 : END DO
544 364 : IF (.NOT. just_energy) THEN
545 364 : IF (gapw) THEN
546 : CALL get_qs_env(qs_env=qs_env, &
547 : rho0_s_rs=rho0_s_rs, &
548 42 : rhoz_cneo_s_rs=rhoz_cneo_s_rs)
549 42 : CPASSERT(ASSOCIATED(rho0_s_rs))
550 42 : IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
551 0 : CALL pw_axpy(rhoz_cneo_s_rs, rho0_s_rs)
552 : END IF
553 42 : ee_ener = ee_ener + pw_integral_ab(rho0_s_rs, vee)
554 42 : IF (ASSOCIATED(rhoz_cneo_s_rs)) THEN
555 0 : 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 364 : energy%ee = -ee_ener
561 : END IF
562 :
563 : ! Adds the QM/MM potential
564 136161 : 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 6334 : qmmm_energy=energy%qmmm_el)
569 6334 : 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 60 : qs_env=qs_env)
575 60 : 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 60 : qs_env=qs_env)
579 60 : IF (calculate_forces) THEN
580 : CALL integrate_potential_devga_rspace( &
581 : potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
582 : forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
583 20 : qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
584 : END IF
585 : END IF
586 60 : CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
587 60 : DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
588 : END IF
589 6334 : IF (.NOT. just_energy) THEN
590 : CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
591 6244 : v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
592 : END IF
593 : END IF
594 136161 : CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
595 :
596 : ! SMEAGOL interface
597 136161 : IF (dft_control%smeagol_control%smeagol_enabled .AND. &
598 : dft_control%smeagol_control%run_type == smeagol_runtype_emtransport) THEN
599 0 : 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 0 : dft_control%smeagol_control%aux%isexplicit_HartreeLeadsBottom)
609 : END IF
610 :
611 : ! calculate the density matrix for the fitted mo_coeffs
612 136161 : IF (dft_control%do_admm) THEN
613 13626 : IF (PRESENT(ext_xc_section)) THEN
614 0 : CALL hfx_admm_init(qs_env, calculate_forces, ext_xc_section)
615 : ELSE
616 13626 : CALL hfx_admm_init(qs_env, calculate_forces)
617 : END IF
618 :
619 13626 : IF (dft_control%do_admm_mo) THEN
620 13412 : IF (qs_env%run_rtp) THEN
621 114 : CALL rtp_admm_calc_rho_aux(qs_env)
622 : ELSE
623 13298 : IF (dokp) THEN
624 154 : CALL admm_mo_calc_rho_aux_kp(qs_env)
625 : ELSE
626 13144 : CALL admm_mo_calc_rho_aux(qs_env)
627 : END IF
628 : END IF
629 214 : ELSE IF (dft_control%do_admm_dm) THEN
630 214 : CALL admm_dm_calc_rho_aux(qs_env)
631 : END IF
632 : END IF
633 :
634 : ! only activate stress calculation if
635 136161 : 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 136161 : IF (dft_control%do_admm) THEN
641 13626 : CALL get_qs_env(qs_env, admm_env=admm_env)
642 13626 : xc_section => admm_env%xc_section_aux
643 13626 : 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 13626 : just_energy=just_energy_xc)
649 :
650 13626 : 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 4974 : calculate_forces=calculate_forces)
657 :
658 : END IF
659 :
660 13626 : NULLIFY (rho_struct)
661 :
662 13626 : IF (use_virial .AND. calculate_forces) THEN
663 20 : vscale = 1.0_dp
664 : !Note: ADMMS and ADMMP stress tensor only for closed-shell calculations
665 20 : IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
666 20 : IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
667 260 : virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
668 260 : 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 13626 : xc_section => admm_env%xc_section_primary
672 : ELSE
673 122535 : 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 122535 : IF (PRESENT(ext_xc_section)) xc_section => ext_xc_section
676 : END IF
677 :
678 136161 : IF (gapw_xc) THEN
679 4450 : CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
680 : ELSE
681 131711 : CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
682 : END IF
683 :
684 : ! zmp
685 136161 : IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc) THEN
686 106 : energy%exc = 0.0_dp
687 106 : CALL calculate_zmp_potential(qs_env, v_rspace_new, rho, exc=energy%exc)
688 : ELSE
689 : ! Embedding potential (runs regardless of XC method)
690 136055 : IF (dft_control%apply_embed_pot) THEN
691 822 : NULLIFY (v_rspace_embed)
692 822 : energy%embed_corr = 0.0_dp
693 : CALL get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, &
694 822 : energy%embed_corr, just_energy)
695 : END IF
696 :
697 : ! Everything else, either via GauXC or manual XC computation
698 136055 : native_skala_evaluator = xc_section_uses_native_skala_grid(xc_section)
699 136055 : paw_composite_representation = .FALSE.
700 136055 : IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc)) THEN
701 : paw_composite_representation = xc_section_uses_gauxc_model(xc_section) .AND. &
702 : skala_gapw_representation(xc_section) == skala_gapw_paw_one_center .AND. &
703 310 : gauxc_gapw_has_paw_pseudopotentials(qs_kind_set)
704 : native_skala_evaluator = native_skala_evaluator .OR. &
705 310 : paw_composite_representation
706 : END IF
707 136055 : IF (dft_control%use_gauxc) THEN
708 904 : IF (native_skala_evaluator) THEN
709 300 : IF (gapw .AND. dft_control%low_spin_roks) THEN
710 : CALL cp_abort(__LOCATION__, &
711 0 : "GAPW low spin ROKS requires the molecular GauXC grid, not native Skala grids.")
712 : END IF
713 300 : CALL ensure_native_skala_grid_scope(xc_section)
714 300 : gapw_representation = skala_gapw_representation(xc_section)
715 300 : IF (gapw .OR. gapw_xc) THEN
716 168 : gapw_all_all_electron = gauxc_gapw_all_all_electron(qs_kind_set)
717 168 : gapw_all_pseudopotentials = gauxc_gapw_all_pseudopotentials(qs_kind_set)
718 : gapw_mixed_core_representations = &
719 168 : .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 300 : gapw_all_pseudopotentials))
727 : native_gapw_composite_reference = &
728 300 : native_skala_gapw_composite_reference(xc_section) .AND. (gapw .OR. gapw_xc)
729 : native_gapw_atom_composite_requested = &
730 : native_skala_gapw_atom_composite_requested(xc_section) .AND. &
731 300 : (gapw .OR. gapw_xc)
732 : native_gapw_atom_composite_reference = &
733 300 : native_gapw_atom_composite_requested .OR. paw_composite_representation
734 : native_gapw_periodic_atom_composite = &
735 300 : native_skala_uses_atom_composite_grid(xc_section)
736 300 : 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 0 : "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 300 : 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 300 : native_gapw_periodic_atom_composite
752 : native_gapw_atom_composite_reference = &
753 : native_gapw_atom_composite_reference .OR. &
754 300 : native_gapw_all_electron_atom_composite
755 300 : IF (paw_composite_representation) THEN
756 136 : IF (ANY(cell%perd /= 0)) THEN
757 : native_gapw_composite_reference = &
758 104 : .NOT. native_gapw_periodic_atom_composite
759 : native_gapw_atom_composite_reference = &
760 104 : native_gapw_periodic_atom_composite
761 : ELSE
762 8 : native_gapw_composite_reference = .FALSE.
763 8 : native_gapw_atom_composite_reference = .TRUE.
764 : END IF
765 : END IF
766 300 : IF (native_gapw_composite_reference .AND. native_gapw_atom_composite_reference) THEN
767 : CALL cp_abort(__LOCATION__, &
768 0 : "Select only one native-grid GAPW composite reference route.")
769 : END IF
770 : native_gapw_composite_direct_ao = native_gapw_composite_reference .AND. &
771 300 : native_skala_gapw_composite_direct_ao(xc_section)
772 300 : gauxc_section => get_gauxc_section(xc_section)
773 300 : IF (ASSOCIATED(gauxc_section)) THEN
774 : CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_DIAGNOSTICS", &
775 300 : l_val=native_grid_diagnostics)
776 : END IF
777 300 : IF ((.NOT. do_kpoints) .AND. nimages /= 1) THEN
778 : CALL cp_abort(__LOCATION__, &
779 : "Native SKALA grid evaluation supports multiple images only "// &
780 0 : "for k-point calculations.")
781 : END IF
782 300 : IF (do_kpoints) THEN
783 36 : CPASSERT(ASSOCIATED(kpoints))
784 36 : gauxc_section => get_gauxc_section(xc_section)
785 36 : CPASSERT(ASSOCIATED(gauxc_section))
786 36 : CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_USE_CUDA", l_val=native_grid_use_cuda)
787 36 : IF (.NOT. native_grid_use_cuda) THEN
788 36 : 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 3 : "runtime stack, or select NATIVE_GRID_USE_CUDA T.")
793 3 : native_grid_cpu_kpoints_warned = .TRUE.
794 : END IF
795 : END IF
796 : END IF
797 300 : IF (dft_control%roks) THEN
798 0 : CPABORT("Native SKALA grid evaluation does not support ROKS.")
799 : END IF
800 300 : IF (dft_control%do_admm) THEN
801 0 : 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 300 : native_skala_restore_exc = calculate_forces
807 300 : IF (native_skala_restore_exc) THEN
808 66 : native_skala_exc_scf = energy%exc
809 66 : native_skala_total_scf = energy%total
810 : END IF
811 300 : IF (calculate_forces) THEN
812 198 : ALLOCATE (native_skala_atom_force(3, natom))
813 66 : native_skala_atom_force = 0.0_dp
814 66 : 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 58 : 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 8 : 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 66 : ASSOCIATED(v_rspace_new) .AND. ASSOCIATED(v_tau_rspace)) THEN
833 2 : IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
834 0 : output_unit = cp_logger_get_default_io_unit()
835 0 : IF (output_unit > 0) THEN
836 0 : DO iatom = 1, natom
837 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,I0,3(1X,ES20.12))") &
838 0 : "SKALA_GPW| Native regular-grid atom force", iatom, &
839 0 : native_skala_atom_force(:, iatom)
840 : END DO
841 : END IF
842 : END IF
843 2 : IF (use_virial) THEN
844 2 : native_skala_composite_virial = 0.0_dp
845 : CALL integrate_gapw_composite_vxc_gspace( &
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 2 : one_center_tau_contraction=native_skala_composite_tau_contraction)
852 2 : IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
853 0 : output_unit = cp_logger_get_default_io_unit()
854 0 : IF (output_unit > 0) THEN
855 : WRITE (UNIT=output_unit, FMT="(T2,A)") &
856 0 : "SKALA_GPW| Composite-direction one-center virial"
857 0 : DO iatom = 1, 3
858 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,3ES20.10)") &
859 0 : "SKALA_GPW|", native_skala_composite_virial(iatom, :)
860 : END DO
861 : END IF
862 : END IF
863 26 : virial%pv_xc = virial%pv_xc + native_skala_composite_virial
864 : ELSE
865 : CALL integrate_gapw_composite_vxc_gspace( &
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 0 : one_center_tau_contraction=native_skala_composite_tau_contraction)
871 : END IF
872 : END IF
873 66 : IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
874 7 : output_unit = cp_logger_get_default_io_unit()
875 7 : IF (output_unit > 0) THEN
876 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
877 7 : "SKALA_GPW| Composite-direction one-center VXC contraction", &
878 14 : native_skala_composite_contraction
879 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
880 7 : "SKALA_GPW| Composite-direction one-center rho contraction", &
881 14 : native_skala_composite_rho_contraction
882 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
883 7 : "SKALA_GPW| Composite-direction one-center tau contraction", &
884 14 : native_skala_composite_tau_contraction
885 21 : DO iatom = 1, natom
886 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,I0,3(1X,ES20.12))") &
887 21 : "SKALA_GPW| Native atom force", iatom, native_skala_atom_force(:, iatom)
888 : END DO
889 : END IF
890 : END IF
891 66 : CPASSERT(ASSOCIATED(force))
892 66 : CPASSERT(ASSOCIATED(atomic_kind_set))
893 66 : CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
894 198 : DO iatom = 1, natom
895 132 : ikind = kind_of(iatom)
896 132 : atom_a = atom_of_kind(iatom)
897 : force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + &
898 594 : native_skala_atom_force(:, iatom)
899 : END DO
900 66 : 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 234 : native_direct_valence_atom_composite))
910 : IF (native_gapw_composite_reference .AND. .NOT. native_gapw_composite_direct_ao .AND. &
911 234 : ASSOCIATED(v_rspace_new) .AND. ASSOCIATED(v_tau_rspace)) THEN
912 : CALL integrate_gapw_composite_vxc_gspace( &
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 2 : one_center_tau_contraction=native_skala_composite_tau_contraction)
917 2 : IF (native_grid_diagnostics .AND. para_env%mepos == 0) THEN
918 0 : output_unit = cp_logger_get_default_io_unit()
919 0 : IF (output_unit > 0) THEN
920 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
921 0 : "SKALA_GPW| Composite-direction one-center VXC contraction", &
922 0 : native_skala_composite_contraction
923 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
924 0 : "SKALA_GPW| Composite-direction one-center rho contraction", &
925 0 : native_skala_composite_rho_contraction
926 : WRITE (UNIT=output_unit, FMT="(T2,A,1X,ES20.10)") &
927 0 : "SKALA_GPW| Composite-direction one-center tau contraction", &
928 0 : native_skala_composite_tau_contraction
929 : END IF
930 : END IF
931 : END IF
932 : END IF
933 300 : IF (native_skala_restore_exc) energy%exc = native_skala_exc_scf
934 300 : IF (native_direct_valence_atom_composite) THEN
935 124 : CPASSERT(ASSOCIATED(v_rspace_new))
936 124 : 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 124 : composite_vxc_tau=v_tau_rspace, direct_valence_atom_grid=.TRUE.)
941 124 : energy%exc = 0.0_dp
942 176 : ELSE IF ((gapw .OR. gapw_xc) .AND. .NOT. native_gapw_composite_reference) THEN
943 144 : IF (native_gapw_atom_composite_reference) THEN
944 128 : CPASSERT(ASSOCIATED(v_rspace_new))
945 128 : 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 128 : composite_vxc_tau=v_tau_rspace, atom_composite_grid=.TRUE.)
950 128 : 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 16 : calculate_forces=calculate_forces)
955 : END IF
956 : END IF
957 300 : IF (edisp /= 0.0_dp) energy%dispersion = edisp
958 300 : IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
959 0 : IF (do_kpoints) THEN
960 : CALL compute_matrix_vxc_kp(qs_env=qs_env, v_rspace=v_rspace_new, &
961 : matrix_vxc_kp=matrix_vxc_kp, &
962 0 : gapw_full_basis=native_gapw_composite_direct_ao)
963 0 : CALL set_ks_env(ks_env, matrix_vxc_kp=matrix_vxc_kp)
964 : ELSE
965 : CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, &
966 : matrix_vxc=matrix_vxc, &
967 0 : gapw_full_basis=native_gapw_composite_direct_ao)
968 0 : CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
969 : END IF
970 : END IF
971 : ELSE
972 604 : use_gauxc_matrix = .TRUE.
973 604 : CALL apply_gauxc(qs_env, xc_section, calculate_forces)
974 604 : IF (gapw_xc .OR. (gapw .AND. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
975 : CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
976 : xc_section_external=xc_section, &
977 2 : calculate_forces=calculate_forces)
978 : END IF
979 : END IF
980 : ELSE
981 : CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
982 : vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
983 : edisp=edisp, dispersion_env=qs_env%dispersion_env, &
984 135151 : just_energy=just_energy_xc)
985 135151 : IF (edisp /= 0.0_dp) energy%dispersion = edisp
986 135151 : IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
987 2 : CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
988 2 : CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
989 : END IF
990 :
991 135151 : IF (gapw .OR. gapw_xc) THEN
992 : CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, &
993 : xc_section_external=xc_section, &
994 28066 : calculate_forces=calculate_forces)
995 : END IF
996 : END IF
997 : END IF
998 :
999 28614 : IF (gapw .AND. dft_control%qs_control%cdft) THEN
1000 1260 : ALLOCATE (cdft_gapw_values(SIZE(cdft_control%group)))
1001 420 : IF (cdft_control%atomic_charges) THEN
1002 : ALLOCATE (cdft_charge_correction(natom, nspins), &
1003 532 : cdft_electronic_charge(cdft_control%natoms, nspins))
1004 : CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, &
1005 76 : cdft_gapw_values, cdft_charge_correction)
1006 228 : DO ispin = 1, nspins
1007 624 : DO iatom = 1, cdft_control%natoms
1008 : cdft_electronic_charge(iatom, ispin) = &
1009 548 : pw_integral_ab(cdft_control%charge(iatom), rho_r(ispin), local_only=.TRUE.)
1010 : END DO
1011 : END DO
1012 76 : CALL para_env%sum(cdft_electronic_charge)
1013 228 : DO ispin = 1, nspins
1014 624 : DO iatom = 1, cdft_control%natoms
1015 : cdft_electronic_charge(iatom, ispin) = &
1016 : cdft_electronic_charge(iatom, ispin) + &
1017 548 : cdft_charge_correction(cdft_control%atoms(iatom), ispin)
1018 : END DO
1019 : END DO
1020 : ELSE
1021 344 : CALL gapw_cdft_one_center(qs_env, just_energy, calculate_forces, cdft_gapw_values)
1022 : END IF
1023 908 : cdft_control%value = cdft_control%value + cdft_gapw_values
1024 908 : energy%cdft = energy%cdft + DOT_PRODUCT(cdft_control%strength, cdft_gapw_values)
1025 420 : IF (cdft_control%atomic_charges) THEN
1026 76 : CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
1027 76 : DEALLOCATE (cdft_charge_correction, cdft_electronic_charge)
1028 : ELSE
1029 344 : CALL cdft_constraint_print(qs_env, cdft_electronic_charge)
1030 : END IF
1031 420 : DEALLOCATE (cdft_gapw_values)
1032 : END IF
1033 :
1034 136161 : IF (dft_control%mtlr_dft_with_perturbation .AND. .NOT. just_energy) THEN
1035 1844 : IF (do_adiabatic_rescaling .OR. use_gauxc_matrix) THEN
1036 0 : CPABORT("MTLR requires an XC potential on the real-space grid.")
1037 : END IF
1038 1844 : IF (.NOT. ASSOCIATED(matrix_vhxc)) THEN
1039 80 : ALLOCATE (matrix_vhxc(nspins))
1040 48 : DO ispin = 1, nspins
1041 32 : NULLIFY (matrix_vhxc(ispin)%matrix)
1042 32 : ALLOCATE (matrix_vhxc(ispin)%matrix)
1043 : CALL dbcsr_create(matrix_vhxc(ispin)%matrix, &
1044 32 : template=ks_matrix(1, 1)%matrix)
1045 32 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_vhxc(ispin)%matrix, sab_orb)
1046 48 : CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1047 : END DO
1048 16 : CALL set_ks_env(ks_env, matrix_vhxc=matrix_vhxc)
1049 : ELSE
1050 1828 : CPASSERT(SIZE(matrix_vhxc) == nspins)
1051 5484 : DO ispin = 1, nspins
1052 5484 : CPASSERT(ASSOCIATED(matrix_vhxc(ispin)%matrix))
1053 : END DO
1054 : END IF
1055 5532 : DO ispin = 1, nspins
1056 3688 : CALL auxbas_pw_pool%create_pw(v_hxc_rspace(ispin))
1057 3688 : CALL pw_zero(v_hxc_rspace(ispin))
1058 3688 : CALL pw_axpy(v_hartree_rspace, v_hxc_rspace(ispin))
1059 3688 : IF (ASSOCIATED(v_rspace_new)) THEN
1060 : CALL pw_axpy(v_rspace_new(ispin), &
1061 : v_hxc_rspace(ispin), &
1062 3688 : v_rspace_new(ispin)%pw_grid%dvol)
1063 : END IF
1064 3688 : CALL dbcsr_set(matrix_vhxc(ispin)%matrix, 0.0_dp)
1065 : CALL integrate_v_rspace(v_hxc_rspace(ispin), &
1066 : hmat=matrix_vhxc(ispin), &
1067 : qs_env=qs_env, &
1068 3688 : calculate_forces=.FALSE.)
1069 5532 : CALL auxbas_pw_pool%give_back_pw(v_hxc_rspace(ispin))
1070 : END DO
1071 : END IF
1072 :
1073 : ! set hartree and xc potentials for use in Harris method
1074 136161 : IF (qs_env%harris_method) THEN
1075 116 : CALL get_qs_env(qs_env, harris_env=harris_env)
1076 116 : CALL harris_set_potentials(harris_env, v_hartree_rspace, v_rspace_new)
1077 : END IF
1078 :
1079 136161 : NULLIFY (rho_struct)
1080 136161 : IF (use_virial .AND. calculate_forces) THEN
1081 7020 : virial%pv_exc = virial%pv_exc - virial%pv_xc
1082 7020 : virial%pv_virial = virial%pv_virial - virial%pv_xc
1083 : END IF
1084 :
1085 : ! *** Add Hartree-Fock contribution if required ***
1086 136161 : hfx_sections => section_vals_get_subs_vals(xc_section, "HF")
1087 136161 : CALL section_vals_get(hfx_sections, explicit=do_hfx)
1088 :
1089 136161 : ace_active = .FALSE.
1090 136161 : ace_rebuild_frequency = 1
1091 :
1092 136161 : IF (do_hfx) THEN
1093 31040 : ace_section => section_vals_get_subs_vals(hfx_sections, "ACE")
1094 31040 : IF (ASSOCIATED(ace_section)) THEN
1095 31040 : CALL section_vals_val_get(ace_section, "ACTIVE", l_val=ace_active)
1096 31040 : CALL section_vals_val_get(ace_section, "REBUILD_FREQUENCY", i_val=ace_rebuild_frequency)
1097 : END IF
1098 : END IF
1099 :
1100 136161 : IF (do_hfx) THEN
1101 31040 : IF (dokp) THEN
1102 276 : IF (ace_active) THEN
1103 0 : CPABORT("ACE-HFX for k-points is not implemented yet")
1104 : ELSE
1105 276 : CALL hfx_ks_matrix_kp(qs_env, ks_matrix, energy, calculate_forces)
1106 : END IF
1107 :
1108 : ELSE
1109 : ! ext_xc_section may contain a hfx section
1110 30764 : IF (ace_active) THEN
1111 : CALL hfx_ace_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1112 : just_energy, v_rspace_new, v_tau_rspace, &
1113 48 : ace_rebuild_frequency, ext_xc_section=xc_section)
1114 : ELSE
1115 : CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
1116 30716 : just_energy, v_rspace_new, v_tau_rspace, ext_xc_section=xc_section)
1117 : END IF
1118 : END IF
1119 : END IF !do_hfx
1120 :
1121 136161 : IF (do_ppl .AND. calculate_forces) THEN
1122 12 : CPASSERT(.NOT. gapw)
1123 26 : DO ispin = 1, nspins
1124 26 : CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
1125 : END DO
1126 : END IF
1127 :
1128 : ! The atom-composite evaluator differentiates its atom-centered NLCC field directly.
1129 : IF (ASSOCIATED(rho_nlcc) .AND. calculate_forces .AND. &
1130 136161 : .NOT. native_gapw_atom_composite_reference .AND. &
1131 : .NOT. native_direct_valence_atom_composite) THEN
1132 92 : DO ispin = 1, nspins
1133 46 : CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
1134 92 : IF (dft_control%do_admm) CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
1135 : END DO
1136 : END IF
1137 :
1138 : ! calculate KG correction
1139 136161 : IF (dft_control%qs_control%do_kg .AND. just_energy) THEN
1140 :
1141 12 : CPASSERT(nimages == 1)
1142 12 : ksmat => ks_matrix(:, 1)
1143 12 : CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
1144 :
1145 : ! subtract kg corr from the total energy
1146 12 : energy%exc = energy%exc - ekin_mol
1147 :
1148 : END IF
1149 :
1150 : ! *** Single atom contributions ***
1151 136161 : IF (.NOT. just_energy) THEN
1152 125433 : IF (calculate_forces) THEN
1153 : ! Getting nuclear force contribution from the core charge density
1154 6023 : IF ((poisson_env%parameters%solver == pw_poisson_implicit) .AND. &
1155 : (poisson_env%parameters%dielectric_params%dielec_core_correction)) THEN
1156 28 : BLOCK
1157 : TYPE(pw_r3d_rs_type) :: v_minus_veps
1158 28 : CALL auxbas_pw_pool%create_pw(v_minus_veps)
1159 28 : CALL pw_copy(v_hartree_rspace, v_minus_veps)
1160 28 : CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
1161 28 : CALL integrate_v_core_rspace(v_minus_veps, qs_env)
1162 28 : CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
1163 : END BLOCK
1164 : ELSE
1165 5995 : CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
1166 : END IF
1167 : ! SCCS with core fill: the dielectric function depends on rho_elec + rho_fill(R),
1168 : ! which yields an additional nuclear force contribution from v_sccs. The fill
1169 : ! Gaussians enter integrate_v_core_rspace with the sign convention of rho_core
1170 : ! (-ccore*exp(-calpha*r**2)), hence the negated potential
1171 6023 : IF (dft_control%do_sccs) THEN
1172 6 : IF (dft_control%sccs_control%core_fill) THEN
1173 : BLOCK
1174 2 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: calpha, ccore
1175 : TYPE(pw_r3d_rs_type) :: v_minus_vsccs
1176 2 : CALL sccs_core_fill_coefficients(qs_env, calpha, ccore)
1177 2 : CALL auxbas_pw_pool%create_pw(v_minus_vsccs)
1178 2 : CALL pw_copy(v_sccs_rspace, v_minus_vsccs)
1179 2 : CALL pw_scale(v_minus_vsccs, -1.0_dp)
1180 2 : CALL integrate_v_core_rspace(v_minus_vsccs, qs_env, calpha=calpha, ccore=ccore)
1181 2 : CALL auxbas_pw_pool%give_back_pw(v_minus_vsccs)
1182 4 : DEALLOCATE (calpha, ccore)
1183 : END BLOCK
1184 : END IF
1185 : END IF
1186 : END IF
1187 :
1188 125433 : IF (.NOT. do_hfx) THEN
1189 : ! Initialize the Kohn-Sham matrix with the core Hamiltonian matrix
1190 : ! (sets ks sparsity equal to matrix_h sparsity)
1191 213317 : DO ispin = 1, nspins
1192 1355089 : DO img = 1, nimages
1193 1141772 : CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name) ! keep the name
1194 1258278 : CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
1195 : END DO
1196 : END DO
1197 : ! imaginary part if required
1198 96811 : IF (qs_env%run_rtp) THEN
1199 2088 : IF (dft_control%rtp_control%velocity_gauge) THEN
1200 182 : CPASSERT(ASSOCIATED(matrix_h_im))
1201 182 : CPASSERT(ASSOCIATED(ks_matrix_im))
1202 364 : DO ispin = 1, nspins
1203 546 : DO img = 1, nimages
1204 182 : CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name) ! keep the name
1205 364 : CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
1206 : END DO
1207 : END DO
1208 : END IF
1209 : END IF
1210 : END IF
1211 :
1212 125433 : IF (use_virial .AND. calculate_forces) THEN
1213 7020 : pv_loc = virial%pv_virial
1214 : END IF
1215 : ! sum up potentials and integrate
1216 : ! Pointing my_rho to the density matrix rho_ao
1217 125433 : my_rho => rho_ao
1218 :
1219 : CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
1220 : v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
1221 : v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
1222 125433 : cdft_control, calculate_forces)
1223 :
1224 125433 : IF (use_gauxc_matrix) THEN
1225 592 : IF (dokp) THEN
1226 0 : CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc_kp)
1227 0 : CPASSERT(ASSOCIATED(matrix_vxc_kp))
1228 0 : DO ispin = 1, nspins
1229 0 : DO img = 1, nimages
1230 : CALL dbcsr_add(ks_matrix(ispin, img)%matrix, matrix_vxc_kp(ispin, img)%matrix, &
1231 0 : 1.0_dp, 1.0_dp)
1232 : END DO
1233 : END DO
1234 : ELSE
1235 592 : CALL get_qs_env(qs_env=qs_env, matrix_vxc=matrix_vxc)
1236 592 : CPASSERT(ASSOCIATED(matrix_vxc))
1237 592 : CPASSERT(nimages == 1)
1238 1224 : DO ispin = 1, nspins
1239 1224 : CALL dbcsr_add(ks_matrix(ispin, 1)%matrix, matrix_vxc(ispin)%matrix, 1.0_dp, 1.0_dp)
1240 : END DO
1241 : END IF
1242 : END IF
1243 :
1244 125433 : IF (gapw .OR. gapw_xc) THEN
1245 27428 : IF (calculate_forces) THEN
1246 790 : IF (gapw_xc) THEN
1247 124 : CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
1248 : ELSE
1249 666 : CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
1250 : END IF
1251 790 : NULLIFY (rho1)
1252 790 : IF (dft_control%use_gauxc .AND. (gapw .OR. gapw_xc) .AND. &
1253 : .NOT. native_skala_evaluator) THEN
1254 2 : gauxc_model_none = .FALSE.
1255 2 : gauxc_section => get_gauxc_section(xc_section)
1256 2 : IF (ASSOCIATED(gauxc_section)) THEN
1257 2 : CALL section_vals_val_get(gauxc_section, "MODEL", c_val=gauxc_model_name)
1258 2 : gauxc_model_name = ADJUSTL(gauxc_model_name)
1259 2 : CALL uppercase(gauxc_model_name)
1260 : gauxc_model_none = (TRIM(gauxc_model_name) == "" .OR. &
1261 2 : TRIM(gauxc_model_name) == "NONE")
1262 : END IF
1263 2 : IF (gauxc_model_none .AND. &
1264 : (gapw_xc .OR. gauxc_gapw_has_paw_pseudopotentials(qs_kind_set))) THEN
1265 0 : CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1266 : END IF
1267 : ELSE
1268 788 : CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section)
1269 : END IF
1270 : !
1271 790 : IF (dft_control%do_admm) THEN
1272 92 : CALL get_qs_env(qs_env, admm_env=admm_env)
1273 92 : xc_section => admm_env%xc_section_aux
1274 92 : CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
1275 92 : vscale = 1.0_dp
1276 92 : IF (admm_env%do_admmp) THEN
1277 8 : vscale = admm_env%gsi(1)**2
1278 84 : ELSE IF (admm_env%do_admms) THEN
1279 6 : vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
1280 : END IF
1281 92 : CALL accint_weight_force(qs_env, rho_struct, rho1, 0, xc_section, force_scale=vscale)
1282 : END IF
1283 : END IF
1284 : END IF
1285 :
1286 125433 : IF (use_virial .AND. calculate_forces) THEN
1287 7020 : virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1288 : END IF
1289 125433 : IF (dft_control%qs_control%do_kg) THEN
1290 900 : CPASSERT(nimages == 1)
1291 900 : ksmat => ks_matrix(:, 1)
1292 :
1293 900 : IF (use_virial .AND. calculate_forces) THEN
1294 208 : pv_loc = virial%pv_virial
1295 : END IF
1296 :
1297 900 : CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
1298 : ! subtract kg corr from the total energy
1299 900 : energy%exc = energy%exc - ekin_mol
1300 :
1301 : ! virial corrections
1302 900 : IF (use_virial .AND. calculate_forces) THEN
1303 :
1304 : ! Integral contribution
1305 208 : virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
1306 :
1307 : ! GGA contribution
1308 208 : virial%pv_exc = virial%pv_exc + virial%pv_xc
1309 208 : virial%pv_virial = virial%pv_virial + virial%pv_xc
1310 208 : virial%pv_xc = 0.0_dp
1311 : END IF
1312 : END IF
1313 :
1314 : ELSE
1315 : ! Atom-composite XC also creates potential buffers for energy-only calls.
1316 : ! These calls skip sum_up_and_integrate, which normally returns the buffers.
1317 10728 : IF (ASSOCIATED(v_rspace_new)) THEN
1318 0 : DO ispin = 1, SIZE(v_rspace_new)
1319 0 : CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
1320 : END DO
1321 0 : DEALLOCATE (v_rspace_new)
1322 : END IF
1323 10728 : IF (ASSOCIATED(v_tau_rspace)) THEN
1324 0 : DO ispin = 1, SIZE(v_tau_rspace)
1325 0 : CALL auxbas_pw_pool%give_back_pw(v_tau_rspace(ispin))
1326 : END DO
1327 0 : DEALLOCATE (v_tau_rspace)
1328 : END IF
1329 : IF (do_hfx) THEN
1330 : IF (.FALSE.) THEN
1331 : CPWARN("KS matrix no longer correct. Check possible problems with property calculations!")
1332 : END IF
1333 : END IF
1334 : END IF ! .NOT. just energy
1335 136161 : IF (dft_control%qs_control%ddapc_explicit_potential) THEN
1336 164 : CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
1337 164 : DEALLOCATE (v_spin_ddapc_rest_r)
1338 : END IF
1339 :
1340 136161 : IF (calculate_forces .AND. dft_control%qs_control%cdft) THEN
1341 126 : IF (.NOT. cdft_control%transfer_pot) THEN
1342 230 : DO iatom = 1, SIZE(cdft_control%group)
1343 124 : CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1344 230 : DEALLOCATE (cdft_control%group(iatom)%weight)
1345 : END DO
1346 106 : IF (cdft_control%atomic_charges) THEN
1347 78 : DO iatom = 1, cdft_control%natoms
1348 78 : CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1349 : END DO
1350 26 : DEALLOCATE (cdft_control%charge)
1351 : END IF
1352 106 : IF (cdft_control%type == outer_scf_becke_constraint .AND. &
1353 : cdft_control%becke_control%cavity_confine) THEN
1354 88 : IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
1355 64 : CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1356 : ELSE
1357 24 : DEALLOCATE (cdft_control%becke_control%cavity_mat)
1358 : END IF
1359 18 : ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
1360 6 : IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
1361 0 : CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1362 : END IF
1363 : END IF
1364 106 : IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
1365 106 : cdft_control%save_pot = .FALSE.
1366 106 : cdft_control%need_pot = .TRUE.
1367 106 : cdft_control%external_control = .FALSE.
1368 : END IF
1369 : END IF
1370 :
1371 136161 : IF (dft_control%do_sccs) THEN
1372 260 : CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
1373 260 : DEALLOCATE (v_sccs_rspace)
1374 : END IF
1375 :
1376 136161 : IF (gapw) THEN
1377 23926 : IF (dft_control%apply_external_potential) THEN
1378 : ! Integrals of the Hartree potential with g0_soft
1379 : CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
1380 42 : v_qmmm=vee, scale=-1.0_dp)
1381 : END IF
1382 23926 : CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
1383 : ! Place Vh_1c_gg_integrals after integrate_vhg0_rspace for CNEO calculations
1384 : ! because vhg0 integral is needed to build the complete nuclear equation
1385 23926 : CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
1386 : CALL Vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.FALSE., &
1387 23926 : core_2nd=.FALSE.)
1388 : ! CNEO quantum nuclear core energy (kinetic + Z*erfc(r)/r potential from classical nuclei)
1389 23926 : energy%core_cneo = 0.0_dp
1390 23926 : IF (ASSOCIATED(local_rho_set%rhoz_cneo_set)) THEN
1391 184 : DO iatom = 1, SIZE(local_rho_set%rhoz_cneo_set)
1392 184 : energy%core_cneo = energy%core_cneo + local_rho_set%rhoz_cneo_set(iatom)%e_core
1393 : END DO
1394 : END IF
1395 : END IF
1396 :
1397 136161 : IF (gapw .OR. gapw_xc) THEN
1398 : ! Single atom contributions in the KS matrix ***
1399 28376 : CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
1400 28376 : IF (dft_control%do_admm) THEN
1401 : !Single atom contribution to the AUX matrices
1402 : !Note: also update ks_aux_fit matrix in case of rtp
1403 4974 : CALL admm_update_ks_atom(qs_env, calculate_forces)
1404 : END IF
1405 : END IF
1406 :
1407 : !Calculation of Mulliken restraint, if requested
1408 : CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
1409 136161 : ks_matrix, matrix_s, rho, mulliken_order_p)
1410 :
1411 : ! Add DFT+U contribution, if requested
1412 136161 : IF (dft_control%dft_plus_u) THEN
1413 6282 : IF (just_energy) THEN
1414 746 : CALL plus_u(qs_env=qs_env)
1415 : ELSE
1416 5536 : CALL plus_u(qs_env=qs_env, matrix_h=ks_matrix)
1417 : END IF
1418 : ELSE
1419 129879 : energy%dft_plus_u = 0.0_dp
1420 : END IF
1421 :
1422 : ! At this point the ks matrix should be up to date, filter it if requested
1423 301395 : DO ispin = 1, nspins
1424 1705511 : DO img = 1, nimages
1425 : CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
1426 1569350 : dft_control%qs_control%eps_filter_matrix)
1427 : END DO
1428 : END DO
1429 :
1430 : !** merge the auxiliary KS matrix and the primary one
1431 136161 : IF (dft_control%do_admm_mo) THEN
1432 13412 : IF (qs_env%run_rtp) THEN
1433 114 : CALL rtp_admm_merge_ks_matrix(qs_env)
1434 : ELSE
1435 13298 : CALL admm_mo_merge_ks_matrix(qs_env)
1436 : END IF
1437 122749 : ELSE IF (dft_control%do_admm_dm) THEN
1438 214 : CALL admm_dm_merge_ks_matrix(qs_env)
1439 : END IF
1440 :
1441 : ! External field (nonperiodic case)
1442 136161 : CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
1443 :
1444 : ! Right now we can compute the orbital derivative here, as it depends currently only on the available
1445 : ! Kohn-Sham matrix. This might change in the future, in which case more pieces might need to be assembled
1446 : ! from this routine, notice that this part of the calculation in not linear scaling
1447 : ! right now this operation is only non-trivial because of occupation numbers and the restricted keyword
1448 136161 : IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp) THEN
1449 46567 : CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
1450 46567 : CPASSERT(nimages == 1)
1451 46567 : ksmat => ks_matrix(:, 1)
1452 46567 : CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
1453 : END IF
1454 :
1455 : ! ADMM overlap forces
1456 136161 : IF (calculate_forces .AND. dft_control%do_admm) THEN
1457 318 : IF (dokp) THEN
1458 30 : CALL calc_admm_ovlp_forces_kp(qs_env)
1459 : ELSE
1460 288 : CALL calc_admm_ovlp_forces(qs_env)
1461 : END IF
1462 : END IF
1463 :
1464 : ! deal with low spin roks
1465 : CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
1466 136161 : calculate_forces, auxbas_pw_pool)
1467 :
1468 : ! deal with sic on explicit orbitals
1469 : CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
1470 136161 : calculate_forces, auxbas_pw_pool)
1471 :
1472 : ! Periodic external field
1473 136161 : CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
1474 :
1475 : ! adds s2_restraint energy and orbital derivatives
1476 : CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
1477 136161 : energy, calculate_forces, just_energy)
1478 :
1479 136161 : IF (do_ppl) THEN
1480 : ! update core energy for grid based local pseudopotential
1481 60 : ecore_ppl = 0._dp
1482 126 : DO ispin = 1, nspins
1483 126 : ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
1484 : END DO
1485 60 : energy%core = energy%core + ecore_ppl
1486 : END IF
1487 :
1488 136161 : IF (lrigpw) THEN
1489 : ! update core energy for ppl_ri method
1490 470 : CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
1491 470 : IF (lri_env%ppl_ri) THEN
1492 8 : ecore_ppl = 0._dp
1493 16 : DO ispin = 1, nspins
1494 8 : lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
1495 16 : CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
1496 : END DO
1497 8 : energy%core = energy%core + ecore_ppl
1498 : END IF
1499 : END IF
1500 :
1501 : ! Sum all energy terms to obtain the total energy
1502 136161 : IF (native_skala_restore_exc) THEN
1503 66 : energy%total = native_skala_total_scf
1504 66 : IF (abnormal_value(energy%total)) THEN
1505 : CALL cp_abort(__LOCATION__, &
1506 0 : "SCF energy restored by native Skala is an abnormal value (NaN/Inf).")
1507 : END IF
1508 : ELSE
1509 136095 : CALL check_sum_energies(energy)
1510 : END IF
1511 :
1512 : ! Print detailed energy
1513 136161 : IF (my_print) THEN
1514 136105 : CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
1515 : END IF
1516 :
1517 136161 : CALL timestop(handle)
1518 :
1519 408483 : END SUBROUTINE qs_ks_build_kohn_sham_matrix
1520 :
1521 : ! **************************************************************************************************
1522 : !> \brief ...
1523 : !> \param rho_tot_gspace ...
1524 : !> \param qs_env ...
1525 : !> \param rho ...
1526 : !> \param skip_nuclear_density ...
1527 : ! **************************************************************************************************
1528 139951 : SUBROUTINE calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
1529 : TYPE(pw_c1d_gs_type), INTENT(INOUT) :: rho_tot_gspace
1530 : TYPE(qs_environment_type), POINTER :: qs_env
1531 : TYPE(qs_rho_type), POINTER :: rho
1532 : LOGICAL, INTENT(IN), OPTIONAL :: skip_nuclear_density
1533 :
1534 : INTEGER :: ispin
1535 : LOGICAL :: my_skip
1536 : TYPE(dft_control_type), POINTER :: dft_control
1537 139951 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
1538 : TYPE(pw_c1d_gs_type), POINTER :: rho0_s_gs, rho_core, rhoz_cneo_s_gs
1539 : TYPE(qs_charges_type), POINTER :: qs_charges
1540 :
1541 139951 : my_skip = .FALSE.
1542 934 : IF (PRESENT(skip_nuclear_density)) my_skip = skip_nuclear_density
1543 :
1544 139951 : CALL qs_rho_get(rho, rho_g=rho_g)
1545 139951 : CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
1546 :
1547 139951 : IF (.NOT. my_skip) THEN
1548 139027 : NULLIFY (rho_core)
1549 139027 : CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
1550 139027 : IF (dft_control%qs_control%gapw) THEN
1551 24244 : NULLIFY (rho0_s_gs, rhoz_cneo_s_gs)
1552 24244 : CALL get_qs_env(qs_env=qs_env, rho0_s_gs=rho0_s_gs, rhoz_cneo_s_gs=rhoz_cneo_s_gs)
1553 24244 : CPASSERT(ASSOCIATED(rho0_s_gs))
1554 24244 : CALL pw_copy(rho0_s_gs, rho_tot_gspace)
1555 24244 : IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
1556 48 : CALL pw_axpy(rhoz_cneo_s_gs, rho_tot_gspace)
1557 : END IF
1558 24244 : IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
1559 2876 : CALL pw_axpy(rho_core, rho_tot_gspace)
1560 : END IF
1561 : ELSE
1562 114783 : CALL pw_copy(rho_core, rho_tot_gspace)
1563 : END IF
1564 307453 : DO ispin = 1, dft_control%nspins
1565 307453 : CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
1566 : END DO
1567 139027 : CALL get_qs_env(qs_env=qs_env, qs_charges=qs_charges)
1568 139027 : qs_charges%total_rho_gspace = pw_integrate_function(rho_tot_gspace, isign=-1)
1569 : ELSE
1570 1852 : DO ispin = 1, dft_control%nspins
1571 1852 : CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
1572 : END DO
1573 : END IF
1574 :
1575 139951 : END SUBROUTINE calc_rho_tot_gspace
1576 :
1577 : ! **************************************************************************************************
1578 : !> \brief compute MO derivatives
1579 : !> \param qs_env the qs_env to update
1580 : !> \param ks_matrix ...
1581 : !> \param mo_derivs ...
1582 : !> \par History
1583 : !> 01.2014 created, transferred from qs_ks_build_kohn_sham_matrix in
1584 : !> separate subroutine
1585 : !> \author Dorothea Golze
1586 : ! **************************************************************************************************
1587 46567 : SUBROUTINE calc_mo_derivatives(qs_env, ks_matrix, mo_derivs)
1588 : TYPE(qs_environment_type), POINTER :: qs_env
1589 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix, mo_derivs
1590 :
1591 : INTEGER :: ispin
1592 : LOGICAL :: uniform_occupation
1593 46567 : REAL(KIND=dp), DIMENSION(:), POINTER :: occupation_numbers
1594 : TYPE(cp_fm_type), POINTER :: mo_coeff
1595 : TYPE(dbcsr_type) :: mo_derivs2_tmp1, mo_derivs2_tmp2
1596 : TYPE(dbcsr_type), POINTER :: mo_coeff_b
1597 : TYPE(dft_control_type), POINTER :: dft_control
1598 46567 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
1599 :
1600 46567 : NULLIFY (dft_control, mo_array, mo_coeff, mo_coeff_b, occupation_numbers)
1601 :
1602 : CALL get_qs_env(qs_env, &
1603 : dft_control=dft_control, &
1604 46567 : mos=mo_array)
1605 :
1606 103295 : DO ispin = 1, SIZE(mo_derivs)
1607 :
1608 : CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, &
1609 56728 : mo_coeff_b=mo_coeff_b, occupation_numbers=occupation_numbers)
1610 : CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin)%matrix, mo_coeff_b, &
1611 56728 : 0.0_dp, mo_derivs(ispin)%matrix)
1612 :
1613 103295 : IF (dft_control%restricted) THEN
1614 : ! only the first mo_set are actual variables, but we still need both
1615 564 : CPASSERT(ispin == 1)
1616 564 : CPASSERT(SIZE(mo_array) == 2)
1617 : ! use a temporary array with the same size as the first spin for the second spin
1618 :
1619 : ! uniform_occupation is needed for this case, otherwise we can not
1620 : ! reconstruct things in ot, since we irreversibly sum
1621 564 : CALL get_mo_set(mo_set=mo_array(1), uniform_occupation=uniform_occupation)
1622 564 : CPASSERT(uniform_occupation)
1623 564 : CALL get_mo_set(mo_set=mo_array(2), uniform_occupation=uniform_occupation)
1624 564 : CPASSERT(uniform_occupation)
1625 :
1626 : ! The beta-spin might have fewer orbitals than alpa-spin...
1627 : ! create temporary matrices with beta_nmo columns
1628 564 : CALL get_mo_set(mo_set=mo_array(2), mo_coeff_b=mo_coeff_b)
1629 564 : CALL dbcsr_create(mo_derivs2_tmp1, template=mo_coeff_b)
1630 :
1631 : ! calculate beta derivatives
1632 564 : CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(2)%matrix, mo_coeff_b, 0.0_dp, mo_derivs2_tmp1)
1633 :
1634 : ! create larger matrix with alpha_nmo columns
1635 564 : CALL dbcsr_create(mo_derivs2_tmp2, template=mo_derivs(1)%matrix)
1636 564 : CALL dbcsr_set(mo_derivs2_tmp2, 0.0_dp)
1637 :
1638 : ! copy into larger matrix, fills the first beta_nmo columns
1639 : CALL dbcsr_copy_columns_hack(mo_derivs2_tmp2, mo_derivs2_tmp1, &
1640 : mo_array(2)%nmo, 1, 1, &
1641 : para_env=mo_array(1)%mo_coeff%matrix_struct%para_env, &
1642 564 : blacs_env=mo_array(1)%mo_coeff%matrix_struct%context)
1643 :
1644 : ! add beta contribution to alpa mo_derivs
1645 564 : CALL dbcsr_add(mo_derivs(1)%matrix, mo_derivs2_tmp2, 1.0_dp, 1.0_dp)
1646 564 : CALL dbcsr_release(mo_derivs2_tmp1)
1647 564 : CALL dbcsr_release(mo_derivs2_tmp2)
1648 : END IF
1649 : END DO
1650 :
1651 46567 : IF (dft_control%do_admm_mo) THEN
1652 6868 : CALL calc_admm_mo_derivatives(qs_env, mo_derivs)
1653 : END IF
1654 :
1655 46567 : END SUBROUTINE calc_mo_derivatives
1656 :
1657 : ! **************************************************************************************************
1658 : !> \brief updates the Kohn Sham matrix of the given qs_env (facility method)
1659 : !> \param qs_env the qs_env to update
1660 : !> \param calculate_forces if true calculate the quantities needed
1661 : !> to calculate the forces. Defaults to false.
1662 : !> \param just_energy if true updates the energies but not the
1663 : !> ks matrix. Defaults to false
1664 : !> \param print_active ...
1665 : !> \par History
1666 : !> 4.2002 created [fawzi]
1667 : !> 8.2014 kpoints [JGH]
1668 : !> 10.2014 refractored [Ole Schuett]
1669 : !> \author Fawzi Mohamed
1670 : ! **************************************************************************************************
1671 282828 : SUBROUTINE qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, &
1672 : print_active)
1673 : TYPE(qs_environment_type), POINTER :: qs_env
1674 : LOGICAL, INTENT(IN), OPTIONAL :: calculate_forces, just_energy, &
1675 : print_active
1676 :
1677 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_update_qs_env'
1678 :
1679 : INTEGER :: handle, unit_nr
1680 : LOGICAL :: c_forces, do_rebuild, energy_only, &
1681 : forces_up_to_date, potential_changed, &
1682 : rho_changed, s_mstruct_changed
1683 : TYPE(qs_ks_env_type), POINTER :: ks_env
1684 :
1685 282828 : NULLIFY (ks_env)
1686 282828 : unit_nr = cp_logger_get_default_io_unit()
1687 :
1688 282828 : c_forces = .FALSE.
1689 282828 : energy_only = .FALSE.
1690 282828 : IF (PRESENT(just_energy)) energy_only = just_energy
1691 282828 : IF (PRESENT(calculate_forces)) c_forces = calculate_forces
1692 :
1693 282828 : IF (c_forces) THEN
1694 10613 : CALL timeset(routineN//'_forces', handle)
1695 : ELSE
1696 272215 : CALL timeset(routineN, handle)
1697 : END IF
1698 :
1699 282828 : CPASSERT(ASSOCIATED(qs_env))
1700 :
1701 : CALL get_qs_env(qs_env, &
1702 : ks_env=ks_env, &
1703 : rho_changed=rho_changed, &
1704 : s_mstruct_changed=s_mstruct_changed, &
1705 : potential_changed=potential_changed, &
1706 282828 : forces_up_to_date=forces_up_to_date)
1707 :
1708 282828 : do_rebuild = .FALSE.
1709 282828 : do_rebuild = do_rebuild .OR. rho_changed
1710 9095 : do_rebuild = do_rebuild .OR. s_mstruct_changed
1711 9095 : do_rebuild = do_rebuild .OR. potential_changed
1712 9095 : do_rebuild = do_rebuild .OR. (c_forces .AND. .NOT. forces_up_to_date)
1713 :
1714 : IF (do_rebuild) THEN
1715 274105 : CALL evaluate_core_matrix_traces(qs_env)
1716 :
1717 : ! the ks matrix will be rebuilt so this is fine now
1718 274105 : CALL set_ks_env(ks_env, potential_changed=.FALSE.)
1719 :
1720 : CALL rebuild_ks_matrix(qs_env, &
1721 : calculate_forces=c_forces, &
1722 : just_energy=energy_only, &
1723 274105 : print_active=print_active)
1724 :
1725 274105 : IF (.NOT. energy_only) THEN
1726 : CALL set_ks_env(ks_env, &
1727 : rho_changed=.FALSE., &
1728 : s_mstruct_changed=.FALSE., &
1729 505077 : forces_up_to_date=forces_up_to_date .OR. c_forces)
1730 : END IF
1731 : END IF
1732 :
1733 282828 : CALL timestop(handle)
1734 :
1735 282828 : END SUBROUTINE qs_ks_update_qs_env
1736 :
1737 : ! **************************************************************************************************
1738 : !> \brief Calculates the traces of the core matrices and the density matrix.
1739 : !> \param qs_env ...
1740 : !> \param rho_ao_ext ...
1741 : !> \author Ole Schuett
1742 : ! **************************************************************************************************
1743 299713 : SUBROUTINE evaluate_core_matrix_traces(qs_env, rho_ao_ext)
1744 : TYPE(qs_environment_type), POINTER :: qs_env
1745 : TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
1746 : POINTER :: rho_ao_ext
1747 :
1748 : CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_core_matrix_traces'
1749 :
1750 : INTEGER :: handle
1751 : REAL(KIND=dp) :: energy_core_im
1752 299713 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrixkp_h, matrixkp_t, rho_ao_kp
1753 : TYPE(dft_control_type), POINTER :: dft_control
1754 : TYPE(qs_energy_type), POINTER :: energy
1755 : TYPE(qs_rho_type), POINTER :: rho
1756 :
1757 299713 : CALL timeset(routineN, handle)
1758 299713 : NULLIFY (energy, rho, dft_control, rho_ao_kp, matrixkp_t, matrixkp_h)
1759 :
1760 : CALL get_qs_env(qs_env, &
1761 : rho=rho, &
1762 : energy=energy, &
1763 : dft_control=dft_control, &
1764 : kinetic_kp=matrixkp_t, &
1765 299713 : matrix_h_kp=matrixkp_h)
1766 :
1767 299713 : IF (PRESENT(rho_ao_ext)) THEN
1768 24766 : rho_ao_kp => rho_ao_ext
1769 : ELSE
1770 274947 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
1771 : END IF
1772 :
1773 299713 : CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy%core, dft_control%nspins)
1774 :
1775 : ! Add the imaginary part in the RTP case
1776 299713 : IF (qs_env%run_rtp) THEN
1777 3350 : IF (dft_control%rtp_control%velocity_gauge) THEN
1778 182 : CALL get_qs_env(qs_env, matrix_h_im_kp=matrixkp_h)
1779 182 : CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_kp)
1780 182 : CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy_core_im, dft_control%nspins)
1781 182 : energy%core = energy%core - energy_core_im
1782 : END IF
1783 : END IF
1784 :
1785 : ! kinetic energy
1786 299713 : IF (ASSOCIATED(matrixkp_t)) THEN
1787 135801 : CALL calculate_ptrace(matrixkp_t, rho_ao_kp, energy%kinetic, dft_control%nspins)
1788 : END IF
1789 :
1790 299713 : CALL timestop(handle)
1791 299713 : END SUBROUTINE evaluate_core_matrix_traces
1792 :
1793 : ! **************************************************************************************************
1794 : !> \brief Constructs a new Khon-Sham matrix
1795 : !> \param qs_env ...
1796 : !> \param calculate_forces ...
1797 : !> \param just_energy ...
1798 : !> \param print_active ...
1799 : !> \author Ole Schuett
1800 : ! **************************************************************************************************
1801 274125 : SUBROUTINE rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
1802 : TYPE(qs_environment_type), POINTER :: qs_env
1803 : LOGICAL, INTENT(IN) :: calculate_forces, just_energy
1804 : LOGICAL, INTENT(IN), OPTIONAL :: print_active
1805 :
1806 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rebuild_ks_matrix'
1807 :
1808 : INTEGER :: handle
1809 : TYPE(dft_control_type), POINTER :: dft_control
1810 :
1811 274125 : CALL timeset(routineN, handle)
1812 274125 : NULLIFY (dft_control)
1813 :
1814 274125 : CALL get_qs_env(qs_env, dft_control=dft_control)
1815 :
1816 274125 : IF (dft_control%qs_control%semi_empirical) THEN
1817 : CALL build_se_fock_matrix(qs_env, &
1818 : calculate_forces=calculate_forces, &
1819 41338 : just_energy=just_energy)
1820 :
1821 232787 : ELSE IF (dft_control%qs_control%dftb) THEN
1822 : CALL build_dftb_ks_matrix(qs_env, &
1823 : calculate_forces=calculate_forces, &
1824 31494 : just_energy=just_energy)
1825 :
1826 201293 : ELSE IF (dft_control%qs_control%xtb) THEN
1827 65480 : IF (dft_control%qs_control%xtb_control%do_tblite) THEN
1828 : CALL build_tblite_ks_matrix(qs_env, &
1829 : calculate_forces=calculate_forces, &
1830 28206 : just_energy=just_energy)
1831 : ELSE
1832 : CALL build_xtb_ks_matrix(qs_env, &
1833 : calculate_forces=calculate_forces, &
1834 37274 : just_energy=just_energy)
1835 : END IF
1836 : ELSE
1837 : CALL qs_ks_build_kohn_sham_matrix(qs_env, &
1838 : calculate_forces=calculate_forces, &
1839 : just_energy=just_energy, &
1840 135813 : print_active=print_active)
1841 : END IF
1842 :
1843 274125 : CALL timestop(handle)
1844 :
1845 274125 : END SUBROUTINE rebuild_ks_matrix
1846 :
1847 : ! **************************************************************************************************
1848 : !> \brief Allocate ks_matrix if necessary, take current overlap matrix as template
1849 : !> \param qs_env ...
1850 : !> \param is_complex ...
1851 : !> \par History
1852 : !> refactoring 04.03.2011 [MI]
1853 : !> \author
1854 : ! **************************************************************************************************
1855 :
1856 29727 : SUBROUTINE qs_ks_allocate_basics(qs_env, is_complex)
1857 : TYPE(qs_environment_type), POINTER :: qs_env
1858 : LOGICAL, INTENT(in) :: is_complex
1859 :
1860 : CHARACTER(LEN=default_string_length) :: headline
1861 : INTEGER :: ic, ispin, nimages, nspins
1862 : LOGICAL :: do_kpoints
1863 29727 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, matrixkp_im_ks, matrixkp_ks
1864 : TYPE(dbcsr_type), POINTER :: refmatrix
1865 : TYPE(dft_control_type), POINTER :: dft_control
1866 : TYPE(kpoint_type), POINTER :: kpoints
1867 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1868 29727 : POINTER :: sab_orb
1869 : TYPE(qs_ks_env_type), POINTER :: ks_env
1870 :
1871 29727 : NULLIFY (dft_control, ks_env, matrix_s_kp, sab_orb, matrixkp_ks, refmatrix, matrixkp_im_ks, kpoints)
1872 :
1873 : CALL get_qs_env(qs_env, &
1874 : dft_control=dft_control, &
1875 : matrix_s_kp=matrix_s_kp, &
1876 : ks_env=ks_env, &
1877 : kpoints=kpoints, &
1878 : do_kpoints=do_kpoints, &
1879 : matrix_ks_kp=matrixkp_ks, &
1880 29727 : matrix_ks_im_kp=matrixkp_im_ks)
1881 :
1882 29727 : IF (do_kpoints) THEN
1883 4054 : CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
1884 : ELSE
1885 25673 : CALL get_qs_env(qs_env, sab_orb=sab_orb)
1886 : END IF
1887 :
1888 29727 : nspins = dft_control%nspins
1889 29727 : nimages = dft_control%nimages
1890 :
1891 29727 : IF (.NOT. ASSOCIATED(matrixkp_ks)) THEN
1892 29677 : CALL dbcsr_allocate_matrix_set(matrixkp_ks, nspins, nimages)
1893 29677 : refmatrix => matrix_s_kp(1, 1)%matrix
1894 63619 : DO ispin = 1, nspins
1895 410465 : DO ic = 1, nimages
1896 346846 : IF (nspins > 1) THEN
1897 74306 : IF (ispin == 1) THEN
1898 37153 : headline = "KOHN-SHAM MATRIX FOR ALPHA SPIN"
1899 : ELSE
1900 37153 : headline = "KOHN-SHAM MATRIX FOR BETA SPIN"
1901 : END IF
1902 : ELSE
1903 272540 : headline = "KOHN-SHAM MATRIX"
1904 : END IF
1905 346846 : ALLOCATE (matrixkp_ks(ispin, ic)%matrix)
1906 : CALL dbcsr_create(matrix=matrixkp_ks(ispin, ic)%matrix, template=refmatrix, &
1907 346846 : name=TRIM(headline), matrix_type=dbcsr_type_symmetric)
1908 346846 : CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_ks(ispin, ic)%matrix, sab_orb)
1909 380788 : CALL dbcsr_set(matrixkp_ks(ispin, ic)%matrix, 0.0_dp)
1910 : END DO
1911 : END DO
1912 29677 : CALL set_ks_env(ks_env, matrix_ks_kp=matrixkp_ks)
1913 : END IF
1914 :
1915 29727 : IF (is_complex) THEN
1916 174 : IF (.NOT. ASSOCIATED(matrixkp_im_ks)) THEN
1917 174 : CPASSERT(nspins == SIZE(matrixkp_ks, 1))
1918 174 : CPASSERT(nimages == SIZE(matrixkp_ks, 2))
1919 174 : CALL dbcsr_allocate_matrix_set(matrixkp_im_ks, nspins, nimages)
1920 372 : DO ispin = 1, nspins
1921 570 : DO ic = 1, nimages
1922 198 : IF (nspins > 1) THEN
1923 48 : IF (ispin == 1) THEN
1924 24 : headline = "IMAGINARY KOHN-SHAM MATRIX FOR ALPHA SPIN"
1925 : ELSE
1926 24 : headline = "IMAGINARY KOHN-SHAM MATRIX FOR BETA SPIN"
1927 : END IF
1928 : ELSE
1929 150 : headline = "IMAGINARY KOHN-SHAM MATRIX"
1930 : END IF
1931 198 : ALLOCATE (matrixkp_im_ks(ispin, ic)%matrix)
1932 198 : refmatrix => matrixkp_ks(ispin, ic)%matrix ! base on real part, but anti-symmetric
1933 : CALL dbcsr_create(matrix=matrixkp_im_ks(ispin, ic)%matrix, template=refmatrix, &
1934 198 : name=TRIM(headline), matrix_type=dbcsr_type_antisymmetric)
1935 198 : CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_im_ks(ispin, ic)%matrix, sab_orb)
1936 396 : CALL dbcsr_set(matrixkp_im_ks(ispin, ic)%matrix, 0.0_dp)
1937 : END DO
1938 : END DO
1939 174 : CALL set_ks_env(ks_env, matrix_ks_im_kp=matrixkp_im_ks)
1940 : END IF
1941 : END IF
1942 :
1943 29727 : END SUBROUTINE qs_ks_allocate_basics
1944 :
1945 : END MODULE qs_ks_methods
|