Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2024 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 admm_dm_methods, ONLY: admm_dm_calc_rho_aux,&
24 : admm_dm_merge_ks_matrix
25 : USE admm_methods, ONLY: admm_mo_calc_rho_aux,&
26 : admm_mo_calc_rho_aux_kp,&
27 : admm_mo_merge_ks_matrix,&
28 : admm_update_ks_atom,&
29 : calc_admm_mo_derivatives,&
30 : calc_admm_ovlp_forces,&
31 : calc_admm_ovlp_forces_kp
32 : USE admm_types, ONLY: admm_type,&
33 : get_admm_env
34 : USE cell_types, ONLY: cell_type
35 : USE cp_control_types, ONLY: dft_control_type
36 : USE cp_dbcsr_api, ONLY: &
37 : dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_filter, dbcsr_get_info, dbcsr_multiply, &
38 : dbcsr_p_type, dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
39 : dbcsr_type_symmetric
40 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
41 : USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set,&
42 : dbcsr_copy_columns_hack
43 : USE cp_ddapc, ONLY: qs_ks_ddapc
44 : USE cp_fm_types, ONLY: cp_fm_type
45 : USE cp_log_handling, ONLY: cp_get_default_logger,&
46 : cp_logger_get_default_io_unit,&
47 : cp_logger_type
48 : USE cp_output_handling, ONLY: cp_p_file,&
49 : cp_print_key_should_output
50 : USE dft_plus_u, ONLY: plus_u
51 : USE hartree_local_methods, ONLY: Vh_1c_gg_integrals
52 : USE hartree_local_types, ONLY: ecoul_1center_type
53 : USE hfx_admm_utils, ONLY: hfx_admm_init,&
54 : hfx_ks_matrix,&
55 : hfx_ks_matrix_kp
56 : USE input_constants, ONLY: do_ppl_grid,&
57 : outer_scf_becke_constraint,&
58 : outer_scf_hirshfeld_constraint
59 : USE input_section_types, ONLY: section_vals_get,&
60 : section_vals_get_subs_vals,&
61 : section_vals_type,&
62 : section_vals_val_get
63 : USE kg_correction, ONLY: kg_ekin_subset
64 : USE kinds, ONLY: default_string_length,&
65 : dp
66 : USE kpoint_types, ONLY: get_kpoint_info,&
67 : kpoint_type
68 : USE lri_environment_methods, ONLY: v_int_ppl_energy
69 : USE lri_environment_types, ONLY: lri_density_type,&
70 : lri_environment_type,&
71 : lri_kind_type
72 : USE mathlib, ONLY: abnormal_value
73 : USE message_passing, ONLY: mp_para_env_type
74 : USE pw_env_types, ONLY: pw_env_get,&
75 : pw_env_type
76 : USE pw_methods, ONLY: pw_axpy,&
77 : pw_copy,&
78 : pw_integral_ab,&
79 : pw_integrate_function,&
80 : pw_scale,&
81 : pw_transfer,&
82 : pw_zero
83 : USE pw_poisson_methods, ONLY: pw_poisson_solve
84 : USE pw_poisson_types, ONLY: pw_poisson_implicit,&
85 : pw_poisson_type
86 : USE pw_pool_types, ONLY: pw_pool_type
87 : USE pw_types, ONLY: pw_c1d_gs_type,&
88 : pw_r3d_rs_type
89 : USE qmmm_image_charge, ONLY: add_image_pot_to_hartree_pot,&
90 : calculate_image_pot,&
91 : integrate_potential_devga_rspace
92 : USE qs_cdft_types, ONLY: cdft_control_type
93 : USE qs_charges_types, ONLY: qs_charges_type
94 : USE qs_core_energies, ONLY: calculate_ptrace
95 : USE qs_dftb_matrices, ONLY: build_dftb_ks_matrix
96 : USE qs_efield_berry, ONLY: qs_efield_berry_phase
97 : USE qs_efield_local, ONLY: qs_efield_local_operator
98 : USE qs_energy_types, ONLY: qs_energy_type
99 : USE qs_environment_types, ONLY: get_qs_env,&
100 : qs_environment_type
101 : USE qs_gapw_densities, ONLY: prepare_gapw_den
102 : USE qs_integrate_potential, ONLY: integrate_ppl_rspace,&
103 : integrate_rho_nlcc,&
104 : integrate_v_core_rspace
105 : USE qs_ks_apply_restraints, ONLY: qs_ks_cdft_constraint,&
106 : qs_ks_mulliken_restraint,&
107 : qs_ks_s2_restraint
108 : USE qs_ks_atom, ONLY: update_ks_atom
109 : USE qs_ks_qmmm_methods, ONLY: qmmm_calculate_energy,&
110 : qmmm_modify_hartree_pot
111 : USE qs_ks_types, ONLY: qs_ks_env_type,&
112 : set_ks_env
113 : USE qs_ks_utils, ONLY: &
114 : calc_v_sic_rspace, calculate_zmp_potential, compute_matrix_vxc, &
115 : get_embed_potential_energy, low_spin_roks, print_densities, print_detailed_energy, &
116 : sic_explicit_orbitals, sum_up_and_integrate
117 : USE qs_local_rho_types, ONLY: local_rho_type
118 : USE qs_mo_types, ONLY: get_mo_set,&
119 : mo_set_type
120 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
121 : USE qs_rho0_ggrid, ONLY: integrate_vhg0_rspace
122 : USE qs_rho_types, ONLY: qs_rho_get,&
123 : qs_rho_type
124 : USE qs_sccs, ONLY: sccs
125 : USE qs_vxc, ONLY: qs_vxc_create
126 : USE qs_vxc_atom, ONLY: calculate_vxc_atom
127 : USE rtp_admm_methods, ONLY: rtp_admm_calc_rho_aux,&
128 : rtp_admm_merge_ks_matrix
129 : USE se_fock_matrix, ONLY: build_se_fock_matrix
130 : USE surface_dipole, ONLY: calc_dipsurf_potential
131 : USE virial_types, ONLY: virial_type
132 : USE xtb_matrices, ONLY: build_xtb_ks_matrix
133 : #include "./base/base_uses.f90"
134 :
135 : IMPLICIT NONE
136 :
137 : PRIVATE
138 :
139 : LOGICAL, PARAMETER :: debug_this_module = .TRUE.
140 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_methods'
141 :
142 : PUBLIC :: calc_rho_tot_gspace, qs_ks_update_qs_env, qs_ks_build_kohn_sham_matrix, &
143 : qs_ks_allocate_basics
144 :
145 : CONTAINS
146 :
147 : ! **************************************************************************************************
148 : !> \brief routine where the real calculations are made: the
149 : !> KS matrix is calculated
150 : !> \param qs_env the qs_env to update
151 : !> \param calculate_forces if true calculate the quantities needed
152 : !> to calculate the forces. Defaults to false.
153 : !> \param just_energy if true updates the energies but not the
154 : !> ks matrix. Defaults to false
155 : !> \param print_active ...
156 : !> \param ext_ks_matrix ...
157 : !> \par History
158 : !> 06.2002 moved from qs_scf to qs_ks_methods, use of ks_env
159 : !> new did_change scheme [fawzi]
160 : !> 10.2002 introduced pools, uses updated rho as input, LSD [fawzi]
161 : !> 10.2004 build_kohn_sham matrix now also computes the derivatives
162 : !> of the total energy wrt to the MO coefs, if instructed to
163 : !> do so. This appears useful for orbital dependent functionals
164 : !> where the KS matrix alone (however this might be defined)
165 : !> does not contain the info to construct this derivative.
166 : !> \author Matthias Krack
167 : !> \note
168 : !> make rho, energy and qs_charges optional, defaulting
169 : !> to qs_env components?
170 : ! **************************************************************************************************
171 290847 : SUBROUTINE qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces, just_energy, &
172 : print_active, ext_ks_matrix)
173 : TYPE(qs_environment_type), POINTER :: qs_env
174 : LOGICAL, INTENT(in) :: calculate_forces, just_energy
175 : LOGICAL, INTENT(IN), OPTIONAL :: print_active
176 : TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
177 : POINTER :: ext_ks_matrix
178 :
179 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_build_kohn_sham_matrix'
180 :
181 : CHARACTER(len=default_string_length) :: name
182 : INTEGER :: handle, iatom, img, ispin, nimages, &
183 : nspins
184 : LOGICAL :: do_adiabatic_rescaling, do_ddapc, do_hfx, do_ppl, dokp, gapw, gapw_xc, &
185 : hfx_treat_lsd_in_core, just_energy_xc, lrigpw, my_print, rigpw, use_virial
186 : REAL(KIND=dp) :: ecore_ppl, edisp, ee_ener, ekin_mol, &
187 : mulliken_order_p, vscale
188 : REAL(KIND=dp), DIMENSION(3, 3) :: h_stress, pv_loc
189 : TYPE(admm_type), POINTER :: admm_env
190 : TYPE(cdft_control_type), POINTER :: cdft_control
191 : TYPE(cell_type), POINTER :: cell
192 : TYPE(cp_logger_type), POINTER :: logger
193 96949 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ksmat, matrix_vxc, mo_derivs
194 96949 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: ks_matrix, ks_matrix_im, matrix_h, &
195 96949 : matrix_h_im, matrix_s, my_rho, rho_ao
196 : TYPE(dft_control_type), POINTER :: dft_control
197 96949 : TYPE(ecoul_1center_type), DIMENSION(:), POINTER :: ecoul_1c
198 : TYPE(local_rho_type), POINTER :: local_rho_set
199 : TYPE(lri_density_type), POINTER :: lri_density
200 : TYPE(lri_environment_type), POINTER :: lri_env
201 96949 : TYPE(lri_kind_type), DIMENSION(:), POINTER :: lri_v_int
202 : TYPE(mp_para_env_type), POINTER :: para_env
203 : TYPE(pw_c1d_gs_type) :: rho_tot_gspace, v_hartree_gspace
204 : TYPE(pw_c1d_gs_type), POINTER :: rho_core
205 : TYPE(pw_env_type), POINTER :: pw_env
206 : TYPE(pw_poisson_type), POINTER :: poisson_env
207 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
208 96949 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, v_rspace_embed, v_rspace_new, &
209 96949 : v_rspace_new_aux_fit, v_tau_rspace, &
210 96949 : v_tau_rspace_aux_fit
211 : TYPE(pw_r3d_rs_type), POINTER :: rho0_s_rs, rho_nlcc, v_hartree_rspace, &
212 : v_sccs_rspace, v_sic_rspace, &
213 : v_spin_ddapc_rest_r, vee, vppl_rspace
214 : TYPE(qs_energy_type), POINTER :: energy
215 : TYPE(qs_ks_env_type), POINTER :: ks_env
216 : TYPE(qs_rho_type), POINTER :: rho, rho_struct, rho_xc
217 : TYPE(section_vals_type), POINTER :: adiabatic_rescaling_section, &
218 : hfx_sections, input, scf_section, &
219 : xc_section
220 : TYPE(virial_type), POINTER :: virial
221 :
222 96949 : CALL timeset(routineN, handle)
223 96949 : NULLIFY (admm_env, cell, dft_control, logger, mo_derivs, my_rho, &
224 96949 : rho_struct, para_env, pw_env, virial, vppl_rspace, &
225 96949 : adiabatic_rescaling_section, hfx_sections, &
226 96949 : input, scf_section, xc_section, matrix_h, matrix_h_im, matrix_s, &
227 96949 : auxbas_pw_pool, poisson_env, v_rspace_new, v_rspace_new_aux_fit, &
228 96949 : v_tau_rspace, v_tau_rspace_aux_fit, matrix_vxc, vee, rho_nlcc, &
229 96949 : ks_env, ks_matrix, ks_matrix_im, rho, energy, rho_xc, rho_r, rho_ao, rho_core)
230 :
231 96949 : CPASSERT(ASSOCIATED(qs_env))
232 :
233 96949 : logger => cp_get_default_logger()
234 96949 : my_print = .TRUE.
235 96949 : IF (PRESENT(print_active)) my_print = print_active
236 :
237 : CALL get_qs_env(qs_env, &
238 : ks_env=ks_env, &
239 : dft_control=dft_control, &
240 : matrix_h_kp=matrix_h, &
241 : matrix_h_im_kp=matrix_h_im, &
242 : matrix_s_kp=matrix_s, &
243 : matrix_ks_kp=ks_matrix, &
244 : matrix_ks_im_kp=ks_matrix_im, &
245 : matrix_vxc=matrix_vxc, &
246 : pw_env=pw_env, &
247 : cell=cell, &
248 : para_env=para_env, &
249 : input=input, &
250 : virial=virial, &
251 : v_hartree_rspace=v_hartree_rspace, &
252 : vee=vee, &
253 : rho_nlcc=rho_nlcc, &
254 : rho=rho, &
255 : rho_core=rho_core, &
256 : rho_xc=rho_xc, &
257 96949 : energy=energy)
258 :
259 96949 : CALL qs_rho_get(rho, rho_r=rho_r, rho_ao_kp=rho_ao)
260 :
261 96949 : nimages = dft_control%nimages
262 96949 : nspins = dft_control%nspins
263 :
264 : ! remap pointer to allow for non-kpoint external ks matrix
265 96949 : IF (PRESENT(ext_ks_matrix)) ks_matrix(1:nspins, 1:1) => ext_ks_matrix(1:nspins)
266 :
267 96949 : use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
268 :
269 96949 : hfx_sections => section_vals_get_subs_vals(input, "DFT%XC%HF")
270 96949 : CALL section_vals_get(hfx_sections, explicit=do_hfx)
271 96949 : IF (do_hfx) THEN
272 : CALL section_vals_val_get(hfx_sections, "TREAT_LSD_IN_CORE", l_val=hfx_treat_lsd_in_core, &
273 23560 : i_rep_section=1)
274 : END IF
275 96949 : adiabatic_rescaling_section => section_vals_get_subs_vals(input, "DFT%XC%ADIABATIC_RESCALING")
276 96949 : CALL section_vals_get(adiabatic_rescaling_section, explicit=do_adiabatic_rescaling)
277 96949 : just_energy_xc = just_energy
278 96949 : IF (do_adiabatic_rescaling) THEN
279 : !! If we perform adiabatic rescaling, the xc potential has to be scaled by the xc- and
280 : !! HFX-energy. Thus, let us first calculate the energy
281 56 : just_energy_xc = .TRUE.
282 : END IF
283 :
284 96949 : CPASSERT(ASSOCIATED(matrix_h))
285 96949 : CPASSERT(ASSOCIATED(matrix_s))
286 96949 : CPASSERT(ASSOCIATED(rho))
287 96949 : CPASSERT(ASSOCIATED(pw_env))
288 96949 : CPASSERT(SIZE(ks_matrix, 1) > 0)
289 96949 : dokp = (nimages > 1)
290 :
291 : ! Setup the possible usage of DDAPC charges
292 : do_ddapc = dft_control%qs_control%ddapc_restraint .OR. &
293 : qs_env%cp_ddapc_ewald%do_decoupling .OR. &
294 : qs_env%cp_ddapc_ewald%do_qmmm_periodic_decpl .OR. &
295 96949 : qs_env%cp_ddapc_ewald%do_solvation
296 :
297 : ! Check if LRIGPW is used
298 96949 : lrigpw = dft_control%qs_control%lrigpw
299 96949 : rigpw = dft_control%qs_control%rigpw
300 96949 : IF (rigpw) THEN
301 0 : CPASSERT(nimages == 1)
302 : END IF
303 0 : IF (lrigpw .AND. rigpw) THEN
304 0 : CPABORT(" LRI and RI are not compatible")
305 : END IF
306 :
307 : ! Check for GAPW method : additional terms for local densities
308 96949 : gapw = dft_control%qs_control%gapw
309 96949 : gapw_xc = dft_control%qs_control%gapw_xc
310 96949 : IF (gapw_xc .AND. gapw) THEN
311 0 : CPABORT(" GAPW and GAPW_XC are not compatible")
312 : END IF
313 96949 : IF ((gapw .AND. lrigpw) .OR. (gapw_xc .AND. lrigpw)) THEN
314 0 : CPABORT(" GAPW/GAPW_XC and LRIGPW are not compatible")
315 : END IF
316 96949 : IF ((gapw .AND. rigpw) .OR. (gapw_xc .AND. rigpw)) THEN
317 0 : CPABORT(" GAPW/GAPW_XC and RIGPW are not compatible")
318 : END IF
319 :
320 96949 : do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
321 96949 : IF (do_ppl) THEN
322 60 : CPASSERT(.NOT. gapw)
323 60 : CALL get_qs_env(qs_env=qs_env, vppl=vppl_rspace)
324 : END IF
325 :
326 96949 : IF (gapw_xc) THEN
327 2534 : CPASSERT(ASSOCIATED(rho_xc))
328 : END IF
329 :
330 : ! gets the tmp grids
331 96949 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env)
332 :
333 96949 : IF (gapw .AND. (poisson_env%parameters%solver .EQ. pw_poisson_implicit)) THEN
334 0 : CPABORT("The implicit Poisson solver cannot be used in conjunction with GAPW.")
335 : END IF
336 :
337 : ! *** Prepare densities for gapw ***
338 96949 : IF (gapw .OR. gapw_xc) THEN
339 15572 : CALL prepare_gapw_den(qs_env, do_rho0=(.NOT. gapw_xc))
340 : END IF
341 :
342 : ! Calculate the Hartree potential
343 96949 : CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
344 96949 : CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
345 :
346 96949 : scf_section => section_vals_get_subs_vals(input, "DFT%SCF")
347 : IF (BTEST(cp_print_key_should_output(logger%iter_info, scf_section, &
348 : "PRINT%DETAILED_ENERGY"), &
349 : cp_p_file) .AND. &
350 96949 : (.NOT. gapw) .AND. (.NOT. gapw_xc) .AND. &
351 : (.NOT. (poisson_env%parameters%solver .EQ. pw_poisson_implicit))) THEN
352 912 : CALL pw_zero(rho_tot_gspace)
353 912 : CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density=.TRUE.)
354 : CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%e_hartree, &
355 912 : v_hartree_gspace)
356 912 : CALL pw_zero(rho_tot_gspace)
357 912 : CALL pw_zero(v_hartree_gspace)
358 : END IF
359 :
360 : ! Get the total density in g-space [ions + electrons]
361 96949 : CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
362 :
363 96949 : IF (my_print) THEN
364 96927 : CALL print_densities(qs_env, rho)
365 : END IF
366 :
367 96949 : IF (dft_control%do_sccs) THEN
368 : ! Self-consistent continuum solvation (SCCS) model
369 : NULLIFY (v_sccs_rspace)
370 132 : ALLOCATE (v_sccs_rspace)
371 132 : CALL auxbas_pw_pool%create_pw(v_sccs_rspace)
372 :
373 132 : IF (poisson_env%parameters%solver .EQ. pw_poisson_implicit) THEN
374 0 : CPABORT("The implicit Poisson solver cannot be used together with SCCS.")
375 : END IF
376 :
377 132 : IF (use_virial .AND. calculate_forces) THEN
378 : CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace, &
379 0 : h_stress=h_stress)
380 0 : virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
381 0 : virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
382 : ELSE
383 132 : CALL sccs(qs_env, rho_tot_gspace, v_hartree_gspace, v_sccs_rspace)
384 : END IF
385 : ELSE
386 : ! Getting the Hartree energy and Hartree potential. Also getting the stress tensor
387 : ! from the Hartree term if needed. No nuclear force information here
388 96817 : IF (use_virial .AND. calculate_forces) THEN
389 376 : h_stress(:, :) = 0.0_dp
390 : CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
391 : v_hartree_gspace, h_stress=h_stress, &
392 376 : rho_core=rho_core)
393 4888 : virial%pv_ehartree = virial%pv_ehartree + h_stress/REAL(para_env%num_pe, dp)
394 4888 : virial%pv_virial = virial%pv_virial + h_stress/REAL(para_env%num_pe, dp)
395 : ELSE
396 : CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
397 96441 : v_hartree_gspace, rho_core=rho_core)
398 : END IF
399 : END IF
400 :
401 : ! In case decouple periodic images and/or apply restraints to charges
402 96949 : IF (do_ddapc) THEN
403 : CALL qs_ks_ddapc(qs_env, auxbas_pw_pool, rho_tot_gspace, v_hartree_gspace, &
404 : v_spin_ddapc_rest_r, energy, calculate_forces, ks_matrix, &
405 1016 : just_energy)
406 : ELSE
407 95933 : dft_control%qs_control%ddapc_explicit_potential = .FALSE.
408 95933 : dft_control%qs_control%ddapc_restraint_is_spin = .FALSE.
409 95933 : IF (.NOT. just_energy) THEN
410 89264 : CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
411 89264 : CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
412 : END IF
413 : END IF
414 96949 : CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
415 :
416 96949 : IF (dft_control%correct_surf_dip) THEN
417 98 : IF (dft_control%surf_dip_correct_switch) THEN
418 98 : CALL calc_dipsurf_potential(qs_env, energy)
419 98 : energy%hartree = energy%hartree + energy%surf_dipole
420 : END IF
421 : END IF
422 :
423 : ! SIC
424 : CALL calc_v_sic_rspace(v_sic_rspace, energy, qs_env, dft_control, rho, poisson_env, &
425 96949 : just_energy, calculate_forces, auxbas_pw_pool)
426 :
427 96949 : IF (gapw) THEN
428 13038 : CALL get_qs_env(qs_env, ecoul_1c=ecoul_1c, local_rho_set=local_rho_set)
429 : CALL Vh_1c_gg_integrals(qs_env, energy%hartree_1c, ecoul_1c, local_rho_set, para_env, tddft=.FALSE., &
430 13038 : core_2nd=.FALSE.)
431 : END IF
432 :
433 : ! Check if CDFT constraint is needed
434 96949 : CALL qs_ks_cdft_constraint(qs_env, auxbas_pw_pool, calculate_forces, cdft_control)
435 :
436 : ! Adds the External Potential if requested
437 96949 : IF (dft_control%apply_external_potential) THEN
438 : ! Compute the energy due to the external potential
439 : ee_ener = 0.0_dp
440 728 : DO ispin = 1, nspins
441 728 : ee_ener = ee_ener + pw_integral_ab(rho_r(ispin), vee)
442 : END DO
443 364 : IF (.NOT. just_energy) THEN
444 364 : IF (gapw) THEN
445 : CALL get_qs_env(qs_env=qs_env, &
446 42 : rho0_s_rs=rho0_s_rs)
447 42 : CPASSERT(ASSOCIATED(rho0_s_rs))
448 42 : ee_ener = ee_ener + pw_integral_ab(rho0_s_rs, vee)
449 : END IF
450 : END IF
451 : ! the sign accounts for the charge of the electrons
452 364 : energy%ee = -ee_ener
453 : END IF
454 :
455 : ! Adds the QM/MM potential
456 96949 : IF (qs_env%qmmm) THEN
457 : CALL qmmm_calculate_energy(qs_env=qs_env, &
458 : rho=rho_r, &
459 : v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, &
460 6298 : qmmm_energy=energy%qmmm_el)
461 6298 : IF (qs_env%qmmm_env_qm%image_charge) THEN
462 : CALL calculate_image_pot(v_hartree_rspace=v_hartree_rspace, &
463 : rho_hartree_gspace=rho_tot_gspace, &
464 : energy=energy, &
465 : qmmm_env=qs_env%qmmm_env_qm, &
466 60 : qs_env=qs_env)
467 60 : IF (.NOT. just_energy) THEN
468 : CALL add_image_pot_to_hartree_pot(v_hartree=v_hartree_rspace, &
469 : v_metal=qs_env%ks_qmmm_env%v_metal_rspace, &
470 60 : qs_env=qs_env)
471 60 : IF (calculate_forces) THEN
472 : CALL integrate_potential_devga_rspace( &
473 : potential=v_hartree_rspace, coeff=qs_env%image_coeff, &
474 : forces=qs_env%qmmm_env_qm%image_charge_pot%image_forcesMM, &
475 20 : qmmm_env=qs_env%qmmm_env_qm, qs_env=qs_env)
476 : END IF
477 : END IF
478 60 : CALL qs_env%ks_qmmm_env%v_metal_rspace%release()
479 60 : DEALLOCATE (qs_env%ks_qmmm_env%v_metal_rspace)
480 : END IF
481 6298 : IF (.NOT. just_energy) THEN
482 : CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
483 6218 : v_qmmm=qs_env%ks_qmmm_env%v_qmmm_rspace, scale=1.0_dp)
484 : END IF
485 : END IF
486 96949 : CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
487 :
488 : ! calculate the density matrix for the fitted mo_coeffs
489 96949 : IF (dft_control%do_admm) THEN
490 10180 : CALL hfx_admm_init(qs_env, calculate_forces)
491 :
492 10180 : IF (dft_control%do_admm_mo) THEN
493 10040 : IF (qs_env%run_rtp) THEN
494 76 : CALL rtp_admm_calc_rho_aux(qs_env)
495 : ELSE
496 9964 : IF (dokp) THEN
497 114 : CALL admm_mo_calc_rho_aux_kp(qs_env)
498 : ELSE
499 9850 : CALL admm_mo_calc_rho_aux(qs_env)
500 : END IF
501 : END IF
502 140 : ELSEIF (dft_control%do_admm_dm) THEN
503 140 : CALL admm_dm_calc_rho_aux(qs_env)
504 : END IF
505 : END IF
506 :
507 : ! only activate stress calculation if
508 96949 : IF (use_virial .AND. calculate_forces) virial%pv_calculate = .TRUE.
509 :
510 : ! *** calculate the xc potential on the pw density ***
511 : ! *** associates v_rspace_new if the xc potential needs to be computed.
512 : ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
513 96949 : IF (dft_control%do_admm) THEN
514 10180 : CALL get_qs_env(qs_env, admm_env=admm_env)
515 10180 : xc_section => admm_env%xc_section_aux
516 10180 : CALL get_admm_env(admm_env, rho_aux_fit=rho_struct)
517 :
518 : ! here we ignore a possible vdW section in admm_env%xc_section_aux
519 : CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
520 : vxc_rho=v_rspace_new_aux_fit, vxc_tau=v_tau_rspace_aux_fit, exc=energy%exc_aux_fit, &
521 10180 : just_energy=just_energy_xc)
522 :
523 10180 : IF (admm_env%do_gapw) THEN
524 : !compute the potential due to atomic densities
525 : CALL calculate_vxc_atom(qs_env, energy_only=just_energy_xc, exc1=energy%exc1_aux_fit, &
526 : kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
527 : xc_section_external=xc_section, &
528 2350 : rho_atom_set_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set)
529 :
530 : END IF
531 :
532 10180 : NULLIFY (rho_struct)
533 :
534 10180 : IF (use_virial .AND. calculate_forces) THEN
535 12 : vscale = 1.0_dp
536 : !Note: ADMMS and ADMMP stress tensor only for closed-shell calculations
537 12 : IF (admm_env%do_admms) vscale = admm_env%gsi(1)**(2.0_dp/3.0_dp)
538 12 : IF (admm_env%do_admmp) vscale = admm_env%gsi(1)**2
539 156 : virial%pv_exc = virial%pv_exc - vscale*virial%pv_xc
540 156 : virial%pv_virial = virial%pv_virial - vscale*virial%pv_xc
541 : ! virial%pv_xc will be zeroed in the xc routines
542 : END IF
543 10180 : xc_section => admm_env%xc_section_primary
544 : ELSE
545 86769 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
546 : END IF
547 :
548 96949 : IF (gapw_xc) THEN
549 2534 : CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
550 : ELSE
551 94415 : CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
552 : END IF
553 :
554 : ! zmp
555 96949 : IF (dft_control%apply_external_density .OR. dft_control%apply_external_vxc) THEN
556 0 : energy%exc = 0.0_dp
557 0 : CALL calculate_zmp_potential(qs_env, v_rspace_new, rho, exc=energy%exc)
558 : ELSE
559 : ! Embedding potential
560 96949 : IF (dft_control%apply_embed_pot) THEN
561 868 : NULLIFY (v_rspace_embed)
562 868 : energy%embed_corr = 0.0_dp
563 : CALL get_embed_potential_energy(qs_env, rho, v_rspace_embed, dft_control, &
564 868 : energy%embed_corr, just_energy)
565 : END IF
566 : ! Everything else
567 : CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
568 : vxc_rho=v_rspace_new, vxc_tau=v_tau_rspace, exc=energy%exc, &
569 : edisp=edisp, dispersion_env=qs_env%dispersion_env, &
570 96949 : just_energy=just_energy_xc)
571 96949 : IF (edisp /= 0.0_dp) energy%dispersion = edisp
572 96949 : IF (qs_env%requires_matrix_vxc .AND. ASSOCIATED(v_rspace_new)) THEN
573 0 : CALL compute_matrix_vxc(qs_env=qs_env, v_rspace=v_rspace_new, matrix_vxc=matrix_vxc)
574 0 : CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
575 : END IF
576 :
577 96949 : IF (gapw .OR. gapw_xc) THEN
578 15572 : CALL calculate_vxc_atom(qs_env, just_energy_xc, energy%exc1, xc_section_external=xc_section)
579 : ! test for not implemented (bug) option
580 15572 : IF (use_virial .AND. calculate_forces) THEN
581 26 : IF (ASSOCIATED(v_tau_rspace)) THEN
582 0 : CPABORT("MGGA STRESS with GAPW/GAPW_XC not implemneted")
583 : END IF
584 : END IF
585 : END IF
586 :
587 : END IF
588 :
589 15390 : NULLIFY (rho_struct)
590 81559 : IF (use_virial .AND. calculate_forces) THEN
591 4888 : virial%pv_exc = virial%pv_exc - virial%pv_xc
592 4888 : virial%pv_virial = virial%pv_virial - virial%pv_xc
593 : END IF
594 :
595 : ! *** Add Hartree-Fock contribution if required ***
596 96949 : IF (do_hfx) THEN
597 23560 : IF (dokp) THEN
598 214 : CALL hfx_ks_matrix_kp(qs_env, ks_matrix, energy, calculate_forces)
599 : ELSE
600 : CALL hfx_ks_matrix(qs_env, ks_matrix, rho, energy, calculate_forces, &
601 23346 : just_energy, v_rspace_new, v_tau_rspace)
602 : END IF
603 : !! Adiabatic rescaling only if do_hfx; right?????
604 : END IF !do_hfx
605 :
606 96949 : IF (do_ppl .AND. calculate_forces) THEN
607 12 : CPASSERT(.NOT. gapw)
608 26 : DO ispin = 1, nspins
609 26 : CALL integrate_ppl_rspace(rho_r(ispin), qs_env)
610 : END DO
611 : END IF
612 :
613 96949 : IF (ASSOCIATED(rho_nlcc) .AND. calculate_forces) THEN
614 60 : DO ispin = 1, nspins
615 30 : CALL integrate_rho_nlcc(v_rspace_new(ispin), qs_env)
616 60 : IF (dft_control%do_admm) CALL integrate_rho_nlcc(v_rspace_new_aux_fit(ispin), qs_env)
617 : END DO
618 : END IF
619 :
620 : ! calculate KG correction
621 96949 : IF (dft_control%qs_control%do_kg .AND. just_energy) THEN
622 :
623 12 : CPASSERT(.NOT. (gapw .OR. gapw_xc))
624 12 : CPASSERT(nimages == 1)
625 12 : ksmat => ks_matrix(:, 1)
626 12 : CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
627 :
628 : ! subtract kg corr from the total energy
629 12 : energy%exc = energy%exc - ekin_mol
630 :
631 : END IF
632 :
633 : ! *** Single atom contributions ***
634 96949 : IF (.NOT. just_energy) THEN
635 90014 : IF (calculate_forces) THEN
636 : ! Getting nuclear force contribution from the core charge density
637 5339 : IF ((poisson_env%parameters%solver .EQ. pw_poisson_implicit) .AND. &
638 : (poisson_env%parameters%dielectric_params%dielec_core_correction)) THEN
639 30 : BLOCK
640 : TYPE(pw_r3d_rs_type) :: v_minus_veps
641 30 : CALL auxbas_pw_pool%create_pw(v_minus_veps)
642 30 : CALL pw_copy(v_hartree_rspace, v_minus_veps)
643 30 : CALL pw_axpy(poisson_env%implicit_env%v_eps, v_minus_veps, -v_hartree_rspace%pw_grid%dvol)
644 30 : CALL integrate_v_core_rspace(v_minus_veps, qs_env)
645 30 : CALL auxbas_pw_pool%give_back_pw(v_minus_veps)
646 : END BLOCK
647 : ELSE
648 5309 : CALL integrate_v_core_rspace(v_hartree_rspace, qs_env)
649 : END IF
650 : END IF
651 :
652 90014 : IF (.NOT. do_hfx) THEN
653 : ! Initialize the Kohn-Sham matrix with the core Hamiltonian matrix
654 : ! (sets ks sparsity equal to matrix_h sparsity)
655 149359 : DO ispin = 1, nspins
656 324582 : DO img = 1, nimages
657 175223 : CALL dbcsr_get_info(ks_matrix(ispin, img)%matrix, name=name) ! keep the name
658 256360 : CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix, name=name)
659 : END DO
660 : END DO
661 : ! imaginary part if required
662 68222 : IF (qs_env%run_rtp) THEN
663 2002 : IF (dft_control%rtp_control%velocity_gauge) THEN
664 150 : CPASSERT(ASSOCIATED(matrix_h_im))
665 150 : CPASSERT(ASSOCIATED(ks_matrix_im))
666 300 : DO ispin = 1, nspins
667 450 : DO img = 1, nimages
668 150 : CALL dbcsr_get_info(ks_matrix_im(ispin, img)%matrix, name=name) ! keep the name
669 300 : CALL dbcsr_copy(ks_matrix_im(ispin, img)%matrix, matrix_h_im(1, img)%matrix, name=name)
670 : END DO
671 : END DO
672 : END IF
673 : END IF
674 : END IF
675 :
676 90014 : IF (use_virial .AND. calculate_forces) THEN
677 4888 : pv_loc = virial%pv_virial
678 : END IF
679 : ! sum up potentials and integrate
680 : ! Pointing my_rho to the density matrix rho_ao
681 90014 : my_rho => rho_ao
682 :
683 : CALL sum_up_and_integrate(qs_env, ks_matrix, rho, my_rho, vppl_rspace, &
684 : v_rspace_new, v_rspace_new_aux_fit, v_tau_rspace, v_tau_rspace_aux_fit, &
685 : v_sic_rspace, v_spin_ddapc_rest_r, v_sccs_rspace, v_rspace_embed, &
686 90014 : cdft_control, calculate_forces)
687 :
688 90014 : IF (use_virial .AND. calculate_forces) THEN
689 4888 : virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
690 : END IF
691 90014 : IF (dft_control%qs_control%do_kg) THEN
692 776 : CPASSERT(.NOT. (gapw .OR. gapw_xc))
693 776 : CPASSERT(nimages == 1)
694 776 : ksmat => ks_matrix(:, 1)
695 :
696 776 : IF (use_virial .AND. calculate_forces) THEN
697 0 : pv_loc = virial%pv_virial
698 : END IF
699 :
700 776 : CALL kg_ekin_subset(qs_env, ksmat, ekin_mol, calculate_forces, do_kernel=.FALSE.)
701 : ! subtract kg corr from the total energy
702 776 : energy%exc = energy%exc - ekin_mol
703 :
704 : ! virial corrections
705 776 : IF (use_virial .AND. calculate_forces) THEN
706 :
707 : ! Integral contribution
708 0 : virial%pv_ehartree = virial%pv_ehartree + (virial%pv_virial - pv_loc)
709 :
710 : ! GGA contribution
711 0 : virial%pv_exc = virial%pv_exc + virial%pv_xc
712 0 : virial%pv_virial = virial%pv_virial + virial%pv_xc
713 0 : virial%pv_xc = 0.0_dp
714 : END IF
715 : END IF
716 :
717 : ELSE
718 : IF (do_hfx) THEN
719 : IF (.FALSE.) THEN
720 : CPWARN("KS matrix not longer correct. Check possible problems with property calculations!")
721 : END IF
722 : END IF
723 : END IF ! .NOT. just energy
724 :
725 96949 : IF (dft_control%qs_control%ddapc_explicit_potential) THEN
726 92 : CALL auxbas_pw_pool%give_back_pw(v_spin_ddapc_rest_r)
727 92 : DEALLOCATE (v_spin_ddapc_rest_r)
728 : END IF
729 :
730 96949 : IF (calculate_forces .AND. dft_control%qs_control%cdft) THEN
731 118 : IF (.NOT. cdft_control%transfer_pot) THEN
732 212 : DO iatom = 1, SIZE(cdft_control%group)
733 114 : CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
734 212 : DEALLOCATE (cdft_control%group(iatom)%weight)
735 : END DO
736 98 : IF (cdft_control%atomic_charges) THEN
737 78 : DO iatom = 1, cdft_control%natoms
738 78 : CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
739 : END DO
740 26 : DEALLOCATE (cdft_control%charge)
741 : END IF
742 98 : IF (cdft_control%type == outer_scf_becke_constraint .AND. &
743 : cdft_control%becke_control%cavity_confine) THEN
744 88 : IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
745 64 : CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
746 : ELSE
747 24 : DEALLOCATE (cdft_control%becke_control%cavity_mat)
748 : END IF
749 10 : ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
750 2 : IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
751 0 : CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
752 : END IF
753 : END IF
754 98 : IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
755 98 : cdft_control%save_pot = .FALSE.
756 98 : cdft_control%need_pot = .TRUE.
757 98 : cdft_control%external_control = .FALSE.
758 : END IF
759 : END IF
760 :
761 96949 : IF (dft_control%do_sccs) THEN
762 132 : CALL auxbas_pw_pool%give_back_pw(v_sccs_rspace)
763 132 : DEALLOCATE (v_sccs_rspace)
764 : END IF
765 :
766 96949 : IF (gapw) THEN
767 13038 : IF (dft_control%apply_external_potential) THEN
768 : ! Integrals of the Hartree potential with g0_soft
769 : CALL qmmm_modify_hartree_pot(v_hartree=v_hartree_rspace, &
770 42 : v_qmmm=vee, scale=-1.0_dp)
771 : END IF
772 13038 : CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, calculate_forces)
773 : END IF
774 :
775 96949 : IF (gapw .OR. gapw_xc) THEN
776 : ! Single atom contributions in the KS matrix ***
777 15572 : CALL update_ks_atom(qs_env, ks_matrix, rho_ao, calculate_forces)
778 15572 : IF (dft_control%do_admm) THEN
779 : !Single atom contribution to the AUX matrices
780 : !Note: also update ks_aux_fit matrix in case of rtp
781 2350 : CALL admm_update_ks_atom(qs_env, calculate_forces)
782 : END IF
783 : END IF
784 :
785 : !Calculation of Mulliken restraint, if requested
786 : CALL qs_ks_mulliken_restraint(energy, dft_control, just_energy, para_env, &
787 96949 : ks_matrix, matrix_s, rho, mulliken_order_p)
788 :
789 : ! Add DFT+U contribution, if requested
790 96949 : IF (dft_control%dft_plus_u) THEN
791 1552 : CPASSERT(nimages == 1)
792 1552 : IF (just_energy) THEN
793 588 : CALL plus_u(qs_env=qs_env)
794 : ELSE
795 964 : ksmat => ks_matrix(:, 1)
796 964 : CALL plus_u(qs_env=qs_env, matrix_h=ksmat)
797 : END IF
798 : ELSE
799 95397 : energy%dft_plus_u = 0.0_dp
800 : END IF
801 :
802 : ! At this point the ks matrix should be up to date, filter it if requested
803 213854 : DO ispin = 1, nspins
804 435919 : DO img = 1, nimages
805 : CALL dbcsr_filter(ks_matrix(ispin, img)%matrix, &
806 338970 : dft_control%qs_control%eps_filter_matrix)
807 : END DO
808 : END DO
809 :
810 : !** merge the auxiliary KS matrix and the primary one
811 96949 : IF (dft_control%do_admm_mo) THEN
812 10040 : IF (qs_env%run_rtp) THEN
813 76 : CALL rtp_admm_merge_ks_matrix(qs_env)
814 : ELSE
815 9964 : CALL admm_mo_merge_ks_matrix(qs_env)
816 : END IF
817 86909 : ELSEIF (dft_control%do_admm_dm) THEN
818 140 : CALL admm_dm_merge_ks_matrix(qs_env)
819 : END IF
820 :
821 : ! External field (nonperiodic case)
822 96949 : CALL qs_efield_local_operator(qs_env, just_energy, calculate_forces)
823 :
824 : ! Right now we can compute the orbital derivative here, as it depends currently only on the available
825 : ! Kohn-Sham matrix. This might change in the future, in which case more pieces might need to be assembled
826 : ! from this routine, notice that this part of the calculation in not linear scaling
827 : ! right now this operation is only non-trivial because of occupation numbers and the restricted keyword
828 96949 : IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy .AND. .NOT. qs_env%run_rtp) THEN
829 34478 : CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
830 34478 : CPASSERT(nimages == 1)
831 34478 : ksmat => ks_matrix(:, 1)
832 34478 : CALL calc_mo_derivatives(qs_env, ksmat, mo_derivs)
833 : END IF
834 :
835 : ! ADMM overlap forces
836 96949 : IF (calculate_forces .AND. dft_control%do_admm) THEN
837 262 : IF (dokp) THEN
838 24 : CALL calc_admm_ovlp_forces_kp(qs_env)
839 : ELSE
840 238 : CALL calc_admm_ovlp_forces(qs_env)
841 : END IF
842 : END IF
843 :
844 : ! deal with low spin roks
845 : CALL low_spin_roks(energy, qs_env, dft_control, do_hfx, just_energy, &
846 96949 : calculate_forces, auxbas_pw_pool)
847 :
848 : ! deal with sic on explicit orbitals
849 : CALL sic_explicit_orbitals(energy, qs_env, dft_control, poisson_env, just_energy, &
850 96949 : calculate_forces, auxbas_pw_pool)
851 :
852 : ! Periodic external field
853 96949 : CALL qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
854 :
855 : ! adds s2_restraint energy and orbital derivatives
856 : CALL qs_ks_s2_restraint(dft_control, qs_env, matrix_s, &
857 96949 : energy, calculate_forces, just_energy)
858 :
859 96949 : IF (do_ppl) THEN
860 : ! update core energy for grid based local pseudopotential
861 60 : ecore_ppl = 0._dp
862 126 : DO ispin = 1, nspins
863 126 : ecore_ppl = ecore_ppl + pw_integral_ab(vppl_rspace, rho_r(ispin))
864 : END DO
865 60 : energy%core = energy%core + ecore_ppl
866 : END IF
867 :
868 96949 : IF (lrigpw) THEN
869 : ! update core energy for ppl_ri method
870 430 : CALL get_qs_env(qs_env, lri_env=lri_env, lri_density=lri_density)
871 430 : IF (lri_env%ppl_ri) THEN
872 8 : ecore_ppl = 0._dp
873 16 : DO ispin = 1, nspins
874 8 : lri_v_int => lri_density%lri_coefs(ispin)%lri_kinds
875 16 : CALL v_int_ppl_energy(qs_env, lri_v_int, ecore_ppl)
876 : END DO
877 8 : energy%core = energy%core + ecore_ppl
878 : END IF
879 : END IF
880 :
881 : ! Sum all energy terms to obtain the total energy
882 : energy%total = energy%core_overlap + energy%core_self + energy%core + energy%hartree + &
883 : energy%hartree_1c + energy%exc + energy%exc1 + energy%ex + &
884 : energy%dispersion + energy%gcp + energy%qmmm_el + energy%mulliken + &
885 : SUM(energy%ddapc_restraint) + energy%s2_restraint + &
886 : energy%dft_plus_u + energy%kTS + &
887 : energy%efield + energy%efield_core + energy%ee + &
888 : energy%ee_core + energy%exc_aux_fit + energy%image_charge + &
889 193954 : energy%sccs_pol + energy%cdft + energy%exc1_aux_fit
890 :
891 96949 : IF (dft_control%apply_embed_pot) energy%total = energy%total + energy%embed_corr
892 :
893 96949 : IF (abnormal_value(energy%total)) &
894 0 : CPABORT("KS energy is an abnormal value (NaN/Inf).")
895 :
896 : ! Print detailed energy
897 96949 : IF (my_print) THEN
898 96927 : CALL print_detailed_energy(qs_env, dft_control, input, energy, mulliken_order_p)
899 : END IF
900 :
901 96949 : CALL timestop(handle)
902 :
903 96949 : END SUBROUTINE qs_ks_build_kohn_sham_matrix
904 :
905 : ! **************************************************************************************************
906 : !> \brief ...
907 : !> \param rho_tot_gspace ...
908 : !> \param qs_env ...
909 : !> \param rho ...
910 : !> \param skip_nuclear_density ...
911 : ! **************************************************************************************************
912 100265 : SUBROUTINE calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
913 : TYPE(pw_c1d_gs_type), INTENT(INOUT) :: rho_tot_gspace
914 : TYPE(qs_environment_type), POINTER :: qs_env
915 : TYPE(qs_rho_type), POINTER :: rho
916 : LOGICAL, INTENT(IN), OPTIONAL :: skip_nuclear_density
917 :
918 : INTEGER :: ispin
919 : LOGICAL :: my_skip
920 : TYPE(dft_control_type), POINTER :: dft_control
921 100265 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
922 : TYPE(pw_c1d_gs_type), POINTER :: rho0_s_gs, rho_core
923 : TYPE(qs_charges_type), POINTER :: qs_charges
924 :
925 100265 : my_skip = .FALSE.
926 926 : IF (PRESENT(skip_nuclear_density)) my_skip = skip_nuclear_density
927 :
928 100265 : CALL qs_rho_get(rho, rho_g=rho_g)
929 100265 : CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
930 :
931 100265 : IF (.NOT. my_skip) THEN
932 99349 : NULLIFY (rho_core)
933 99349 : CALL get_qs_env(qs_env=qs_env, rho_core=rho_core)
934 99349 : IF (dft_control%qs_control%gapw) THEN
935 13110 : NULLIFY (rho0_s_gs)
936 13110 : CALL get_qs_env(qs_env=qs_env, rho0_s_gs=rho0_s_gs)
937 13110 : CPASSERT(ASSOCIATED(rho0_s_gs))
938 13110 : CALL pw_copy(rho0_s_gs, rho_tot_gspace)
939 13110 : IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
940 1226 : CALL pw_axpy(rho_core, rho_tot_gspace)
941 : END IF
942 : ELSE
943 86239 : CALL pw_copy(rho_core, rho_tot_gspace)
944 : END IF
945 218940 : DO ispin = 1, dft_control%nspins
946 218940 : CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
947 : END DO
948 99349 : CALL get_qs_env(qs_env=qs_env, qs_charges=qs_charges)
949 99349 : qs_charges%total_rho_gspace = pw_integrate_function(rho_tot_gspace, isign=-1)
950 : ELSE
951 1836 : DO ispin = 1, dft_control%nspins
952 1836 : CALL pw_axpy(rho_g(ispin), rho_tot_gspace)
953 : END DO
954 : END IF
955 :
956 100265 : END SUBROUTINE calc_rho_tot_gspace
957 :
958 : ! **************************************************************************************************
959 : !> \brief compute MO derivatives
960 : !> \param qs_env the qs_env to update
961 : !> \param ks_matrix ...
962 : !> \param mo_derivs ...
963 : !> \par History
964 : !> 01.2014 created, transferred from qs_ks_build_kohn_sham_matrix in
965 : !> separate subroutine
966 : !> \author Dorothea Golze
967 : ! **************************************************************************************************
968 34478 : SUBROUTINE calc_mo_derivatives(qs_env, ks_matrix, mo_derivs)
969 : TYPE(qs_environment_type), POINTER :: qs_env
970 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ks_matrix, mo_derivs
971 :
972 : INTEGER :: ispin
973 : LOGICAL :: uniform_occupation
974 34478 : REAL(KIND=dp), DIMENSION(:), POINTER :: occupation_numbers
975 : TYPE(cp_fm_type), POINTER :: mo_coeff
976 : TYPE(dbcsr_type) :: mo_derivs2_tmp1, mo_derivs2_tmp2
977 : TYPE(dbcsr_type), POINTER :: mo_coeff_b
978 : TYPE(dft_control_type), POINTER :: dft_control
979 34478 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
980 :
981 34478 : NULLIFY (dft_control, mo_array, mo_coeff, mo_coeff_b, occupation_numbers)
982 :
983 : CALL get_qs_env(qs_env, &
984 : dft_control=dft_control, &
985 34478 : mos=mo_array)
986 :
987 75457 : DO ispin = 1, SIZE(mo_derivs)
988 :
989 : CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, &
990 40979 : mo_coeff_b=mo_coeff_b, occupation_numbers=occupation_numbers)
991 : CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin)%matrix, mo_coeff_b, &
992 40979 : 0.0_dp, mo_derivs(ispin)%matrix)
993 :
994 75457 : IF (dft_control%restricted) THEN
995 : ! only the first mo_set are actual variables, but we still need both
996 552 : CPASSERT(ispin == 1)
997 552 : CPASSERT(SIZE(mo_array) == 2)
998 : ! use a temporary array with the same size as the first spin for the second spin
999 :
1000 : ! uniform_occupation is needed for this case, otherwise we can no
1001 : ! reconstruct things in ot, since we irreversibly sum
1002 552 : CALL get_mo_set(mo_set=mo_array(1), uniform_occupation=uniform_occupation)
1003 552 : CPASSERT(uniform_occupation)
1004 552 : CALL get_mo_set(mo_set=mo_array(2), uniform_occupation=uniform_occupation)
1005 552 : CPASSERT(uniform_occupation)
1006 :
1007 : ! The beta-spin might have fewer orbitals than alpa-spin...
1008 : ! create tempoary matrices with beta_nmo columns
1009 552 : CALL get_mo_set(mo_set=mo_array(2), mo_coeff_b=mo_coeff_b)
1010 552 : CALL dbcsr_create(mo_derivs2_tmp1, template=mo_coeff_b)
1011 :
1012 : ! calculate beta derivatives
1013 552 : CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(2)%matrix, mo_coeff_b, 0.0_dp, mo_derivs2_tmp1)
1014 :
1015 : ! create larger matrix with alpha_nmo columns
1016 552 : CALL dbcsr_create(mo_derivs2_tmp2, template=mo_derivs(1)%matrix)
1017 552 : CALL dbcsr_set(mo_derivs2_tmp2, 0.0_dp)
1018 :
1019 : ! copy into larger matrix, fills the first beta_nmo columns
1020 : CALL dbcsr_copy_columns_hack(mo_derivs2_tmp2, mo_derivs2_tmp1, &
1021 : mo_array(2)%nmo, 1, 1, &
1022 : para_env=mo_array(1)%mo_coeff%matrix_struct%para_env, &
1023 552 : blacs_env=mo_array(1)%mo_coeff%matrix_struct%context)
1024 :
1025 : ! add beta contribution to alpa mo_derivs
1026 552 : CALL dbcsr_add(mo_derivs(1)%matrix, mo_derivs2_tmp2, 1.0_dp, 1.0_dp)
1027 552 : CALL dbcsr_release(mo_derivs2_tmp1)
1028 552 : CALL dbcsr_release(mo_derivs2_tmp2)
1029 : END IF
1030 : END DO
1031 :
1032 34478 : IF (dft_control%do_admm_mo) THEN
1033 5006 : CALL calc_admm_mo_derivatives(qs_env, mo_derivs)
1034 : END IF
1035 :
1036 34478 : END SUBROUTINE calc_mo_derivatives
1037 :
1038 : ! **************************************************************************************************
1039 : !> \brief updates the Kohn Sham matrix of the given qs_env (facility method)
1040 : !> \param qs_env the qs_env to update
1041 : !> \param calculate_forces if true calculate the quantities needed
1042 : !> to calculate the forces. Defaults to false.
1043 : !> \param just_energy if true updates the energies but not the
1044 : !> ks matrix. Defaults to false
1045 : !> \param print_active ...
1046 : !> \par History
1047 : !> 4.2002 created [fawzi]
1048 : !> 8.2014 kpoints [JGH]
1049 : !> 10.2014 refractored [Ole Schuett]
1050 : !> \author Fawzi Mohamed
1051 : ! **************************************************************************************************
1052 181235 : SUBROUTINE qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, &
1053 : print_active)
1054 : TYPE(qs_environment_type), POINTER :: qs_env
1055 : LOGICAL, INTENT(IN), OPTIONAL :: calculate_forces, just_energy, &
1056 : print_active
1057 :
1058 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_ks_update_qs_env'
1059 :
1060 : INTEGER :: handle, unit_nr
1061 : LOGICAL :: c_forces, do_rebuild, energy_only, &
1062 : forces_up_to_date, potential_changed, &
1063 : rho_changed, s_mstruct_changed
1064 : TYPE(qs_ks_env_type), POINTER :: ks_env
1065 :
1066 181235 : NULLIFY (ks_env)
1067 181235 : unit_nr = cp_logger_get_default_io_unit()
1068 :
1069 181235 : c_forces = .FALSE.
1070 181235 : energy_only = .FALSE.
1071 181235 : IF (PRESENT(just_energy)) energy_only = just_energy
1072 181235 : IF (PRESENT(calculate_forces)) c_forces = calculate_forces
1073 :
1074 181235 : IF (c_forces) THEN
1075 9489 : CALL timeset(routineN//'_forces', handle)
1076 : ELSE
1077 171746 : CALL timeset(routineN, handle)
1078 : END IF
1079 :
1080 181235 : CPASSERT(ASSOCIATED(qs_env))
1081 :
1082 : CALL get_qs_env(qs_env, &
1083 : ks_env=ks_env, &
1084 : rho_changed=rho_changed, &
1085 : s_mstruct_changed=s_mstruct_changed, &
1086 : potential_changed=potential_changed, &
1087 181235 : forces_up_to_date=forces_up_to_date)
1088 :
1089 181235 : do_rebuild = .FALSE.
1090 181235 : do_rebuild = do_rebuild .OR. rho_changed
1091 7740 : do_rebuild = do_rebuild .OR. s_mstruct_changed
1092 7732 : do_rebuild = do_rebuild .OR. potential_changed
1093 7732 : do_rebuild = do_rebuild .OR. (c_forces .AND. .NOT. forces_up_to_date)
1094 :
1095 : IF (do_rebuild) THEN
1096 173853 : CALL evaluate_core_matrix_traces(qs_env)
1097 :
1098 : ! the ks matrix will be rebuilt so this is fine now
1099 173853 : CALL set_ks_env(ks_env, potential_changed=.FALSE.)
1100 :
1101 : CALL rebuild_ks_matrix(qs_env, &
1102 : calculate_forces=c_forces, &
1103 : just_energy=energy_only, &
1104 173853 : print_active=print_active)
1105 :
1106 173853 : IF (.NOT. energy_only) THEN
1107 : CALL set_ks_env(ks_env, &
1108 : rho_changed=.FALSE., &
1109 : s_mstruct_changed=.FALSE., &
1110 317035 : forces_up_to_date=forces_up_to_date .OR. c_forces)
1111 : END IF
1112 : END IF
1113 :
1114 181235 : CALL timestop(handle)
1115 :
1116 181235 : END SUBROUTINE qs_ks_update_qs_env
1117 :
1118 : ! **************************************************************************************************
1119 : !> \brief Calculates the traces of the core matrices and the density matrix.
1120 : !> \param qs_env ...
1121 : !> \author Ole Schuett
1122 : ! **************************************************************************************************
1123 173853 : SUBROUTINE evaluate_core_matrix_traces(qs_env)
1124 : TYPE(qs_environment_type), POINTER :: qs_env
1125 :
1126 : CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_core_matrix_traces'
1127 :
1128 : INTEGER :: handle
1129 : REAL(KIND=dp) :: energy_core_im
1130 173853 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrixkp_h, matrixkp_t, rho_ao_kp
1131 : TYPE(dft_control_type), POINTER :: dft_control
1132 : TYPE(qs_energy_type), POINTER :: energy
1133 : TYPE(qs_rho_type), POINTER :: rho
1134 :
1135 173853 : CALL timeset(routineN, handle)
1136 173853 : NULLIFY (energy, rho, dft_control, rho_ao_kp, matrixkp_t, matrixkp_h)
1137 :
1138 : CALL get_qs_env(qs_env, &
1139 : rho=rho, &
1140 : energy=energy, &
1141 : dft_control=dft_control, &
1142 : kinetic_kp=matrixkp_t, &
1143 173853 : matrix_h_kp=matrixkp_h)
1144 :
1145 173853 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
1146 :
1147 173853 : CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy%core, dft_control%nspins)
1148 :
1149 : ! Add the imaginary part in the RTP case
1150 173853 : IF (qs_env%run_rtp) THEN
1151 3178 : IF (dft_control%rtp_control%velocity_gauge) THEN
1152 150 : CALL get_qs_env(qs_env, matrix_h_im_kp=matrixkp_h)
1153 150 : CALL qs_rho_get(rho, rho_ao_im_kp=rho_ao_kp)
1154 150 : CALL calculate_ptrace(matrixkp_h, rho_ao_kp, energy_core_im, dft_control%nspins)
1155 150 : energy%core = energy%core - energy_core_im
1156 : END IF
1157 : END IF
1158 :
1159 : ! kinetic energy
1160 173853 : IF (ASSOCIATED(matrixkp_t)) &
1161 96785 : CALL calculate_ptrace(matrixkp_t, rho_ao_kp, energy%kinetic, dft_control%nspins)
1162 :
1163 173853 : CALL timestop(handle)
1164 173853 : END SUBROUTINE evaluate_core_matrix_traces
1165 :
1166 : ! **************************************************************************************************
1167 : !> \brief Constructs a new Khon-Sham matrix
1168 : !> \param qs_env ...
1169 : !> \param calculate_forces ...
1170 : !> \param just_energy ...
1171 : !> \param print_active ...
1172 : !> \author Ole Schuett
1173 : ! **************************************************************************************************
1174 173853 : SUBROUTINE rebuild_ks_matrix(qs_env, calculate_forces, just_energy, print_active)
1175 : TYPE(qs_environment_type), POINTER :: qs_env
1176 : LOGICAL, INTENT(IN) :: calculate_forces, just_energy
1177 : LOGICAL, INTENT(IN), OPTIONAL :: print_active
1178 :
1179 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rebuild_ks_matrix'
1180 :
1181 : INTEGER :: handle
1182 : TYPE(dft_control_type), POINTER :: dft_control
1183 :
1184 173853 : CALL timeset(routineN, handle)
1185 173853 : NULLIFY (dft_control)
1186 :
1187 173853 : CALL get_qs_env(qs_env, dft_control=dft_control)
1188 :
1189 173853 : IF (dft_control%qs_control%semi_empirical) THEN
1190 : CALL build_se_fock_matrix(qs_env, &
1191 : calculate_forces=calculate_forces, &
1192 41012 : just_energy=just_energy)
1193 :
1194 132841 : ELSEIF (dft_control%qs_control%dftb) THEN
1195 : CALL build_dftb_ks_matrix(qs_env, &
1196 : calculate_forces=calculate_forces, &
1197 13050 : just_energy=just_energy)
1198 :
1199 119791 : ELSEIF (dft_control%qs_control%xtb) THEN
1200 : CALL build_xtb_ks_matrix(qs_env, &
1201 : calculate_forces=calculate_forces, &
1202 23006 : just_energy=just_energy)
1203 :
1204 : ELSE
1205 : CALL qs_ks_build_kohn_sham_matrix(qs_env, &
1206 : calculate_forces=calculate_forces, &
1207 : just_energy=just_energy, &
1208 96785 : print_active=print_active)
1209 : END IF
1210 :
1211 173853 : CALL timestop(handle)
1212 :
1213 173853 : END SUBROUTINE rebuild_ks_matrix
1214 :
1215 : ! **************************************************************************************************
1216 : !> \brief Allocate ks_matrix if necessary, take current overlap matrix as template
1217 : !> \param qs_env ...
1218 : !> \param is_complex ...
1219 : !> \par History
1220 : !> refactoring 04.03.2011 [MI]
1221 : !> \author
1222 : ! **************************************************************************************************
1223 :
1224 19588 : SUBROUTINE qs_ks_allocate_basics(qs_env, is_complex)
1225 : TYPE(qs_environment_type), POINTER :: qs_env
1226 : LOGICAL, INTENT(in) :: is_complex
1227 :
1228 : CHARACTER(LEN=default_string_length) :: headline
1229 : INTEGER :: ic, ispin, nimages, nspins
1230 : LOGICAL :: do_kpoints
1231 19588 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, matrixkp_im_ks, matrixkp_ks
1232 : TYPE(dbcsr_type), POINTER :: refmatrix
1233 : TYPE(dft_control_type), POINTER :: dft_control
1234 : TYPE(kpoint_type), POINTER :: kpoints
1235 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1236 19588 : POINTER :: sab_orb
1237 : TYPE(qs_ks_env_type), POINTER :: ks_env
1238 :
1239 19588 : NULLIFY (dft_control, ks_env, matrix_s_kp, sab_orb, matrixkp_ks, refmatrix, matrixkp_im_ks, kpoints)
1240 :
1241 : CALL get_qs_env(qs_env, &
1242 : dft_control=dft_control, &
1243 : matrix_s_kp=matrix_s_kp, &
1244 : ks_env=ks_env, &
1245 : kpoints=kpoints, &
1246 : do_kpoints=do_kpoints, &
1247 : matrix_ks_kp=matrixkp_ks, &
1248 19588 : matrix_ks_im_kp=matrixkp_im_ks)
1249 :
1250 19588 : IF (do_kpoints) THEN
1251 770 : CALL get_kpoint_info(kpoints, sab_nl=sab_orb)
1252 : ELSE
1253 18818 : CALL get_qs_env(qs_env, sab_orb=sab_orb)
1254 : END IF
1255 :
1256 19588 : nspins = dft_control%nspins
1257 19588 : nimages = dft_control%nimages
1258 :
1259 19588 : IF (.NOT. ASSOCIATED(matrixkp_ks)) THEN
1260 19548 : CALL dbcsr_allocate_matrix_set(matrixkp_ks, nspins, nimages)
1261 19548 : refmatrix => matrix_s_kp(1, 1)%matrix
1262 41580 : DO ispin = 1, nspins
1263 132940 : DO ic = 1, nimages
1264 91360 : IF (nspins > 1) THEN
1265 24680 : IF (ispin == 1) THEN
1266 12340 : headline = "KOHN-SHAM MATRIX FOR ALPHA SPIN"
1267 : ELSE
1268 12340 : headline = "KOHN-SHAM MATRIX FOR BETA SPIN"
1269 : END IF
1270 : ELSE
1271 66680 : headline = "KOHN-SHAM MATRIX"
1272 : END IF
1273 91360 : ALLOCATE (matrixkp_ks(ispin, ic)%matrix)
1274 : CALL dbcsr_create(matrix=matrixkp_ks(ispin, ic)%matrix, template=refmatrix, &
1275 91360 : name=TRIM(headline), matrix_type=dbcsr_type_symmetric, nze=0)
1276 91360 : CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_ks(ispin, ic)%matrix, sab_orb)
1277 113392 : CALL dbcsr_set(matrixkp_ks(ispin, ic)%matrix, 0.0_dp)
1278 : END DO
1279 : END DO
1280 19548 : CALL set_ks_env(ks_env, matrix_ks_kp=matrixkp_ks)
1281 : END IF
1282 :
1283 19588 : IF (is_complex) THEN
1284 138 : IF (.NOT. ASSOCIATED(matrixkp_im_ks)) THEN
1285 138 : CPASSERT(nspins .EQ. SIZE(matrixkp_ks, 1))
1286 138 : CPASSERT(nimages .EQ. SIZE(matrixkp_ks, 2))
1287 138 : CALL dbcsr_allocate_matrix_set(matrixkp_im_ks, nspins, nimages)
1288 288 : DO ispin = 1, nspins
1289 438 : DO ic = 1, nimages
1290 150 : IF (nspins > 1) THEN
1291 24 : IF (ispin == 1) THEN
1292 12 : headline = "IMAGINARY KOHN-SHAM MATRIX FOR ALPHA SPIN"
1293 : ELSE
1294 12 : headline = "IMAGINARY KOHN-SHAM MATRIX FOR BETA SPIN"
1295 : END IF
1296 : ELSE
1297 126 : headline = "IMAGINARY KOHN-SHAM MATRIX"
1298 : END IF
1299 150 : ALLOCATE (matrixkp_im_ks(ispin, ic)%matrix)
1300 150 : refmatrix => matrixkp_ks(ispin, ic)%matrix ! base on real part, but anti-symmetric
1301 : CALL dbcsr_create(matrix=matrixkp_im_ks(ispin, ic)%matrix, template=refmatrix, &
1302 150 : name=TRIM(headline), matrix_type=dbcsr_type_antisymmetric, nze=0)
1303 150 : CALL cp_dbcsr_alloc_block_from_nbl(matrixkp_im_ks(ispin, ic)%matrix, sab_orb)
1304 300 : CALL dbcsr_set(matrixkp_im_ks(ispin, ic)%matrix, 0.0_dp)
1305 : END DO
1306 : END DO
1307 138 : CALL set_ks_env(ks_env, matrix_ks_im_kp=matrixkp_im_ks)
1308 : END IF
1309 : END IF
1310 :
1311 19588 : END SUBROUTINE qs_ks_allocate_basics
1312 :
1313 : END MODULE qs_ks_methods
|