(git:374b731)
Loading...
Searching...
No Matches
qs_local_properties.F
Go to the documentation of this file.
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 for calculating local energy and stress tensor
10!> \author JGH
11!> \par History
12!> - 07.2019 created
13! **************************************************************************************************
15 USE bibliography, ONLY: cohen2000,&
18 cite_reference
24 USE dbcsr_api, ONLY: dbcsr_copy,&
25 dbcsr_p_type,&
26 dbcsr_set,&
27 dbcsr_type
30 USE kinds, ONLY: dp
31 USE mathlib, ONLY: det_3x3
32 USE pw_env_types, ONLY: pw_env_get,&
34 USE pw_methods, ONLY: pw_axpy,&
35 pw_copy,&
36 pw_derive,&
42 USE pw_types, ONLY: pw_c1d_gs_type,&
51 USE qs_ks_types, ONLY: qs_ks_env_type,&
53 USE qs_rho_types, ONLY: qs_rho_get,&
55 USE qs_vxc, ONLY: qs_xc_density
56 USE virial_types, ONLY: virial_type
57#include "./base/base_uses.f90"
58
59 IMPLICIT NONE
60
61 PRIVATE
62
63 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_local_properties'
64
66
67! **************************************************************************************************
68
69CONTAINS
70
71! **************************************************************************************************
72!> \brief Routine to calculate the local energy
73!> \param qs_env the qs_env to update
74!> \param energy_density ...
75!> \par History
76!> 07.2019 created
77!> \author JGH
78! **************************************************************************************************
79 SUBROUTINE qs_local_energy(qs_env, energy_density)
80 TYPE(qs_environment_type), POINTER :: qs_env
81 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: energy_density
82
83 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_local_energy'
84
85 INTEGER :: handle, img, iounit, ispin, nimages, &
86 nkind, nspins
87 LOGICAL :: gapw, gapw_xc
88 REAL(kind=dp) :: eban, eband, eh, exc, ovol
89 TYPE(cp_logger_type), POINTER :: logger
90 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
91 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s, matrix_w, rho_ao_kp
92 TYPE(dbcsr_type), POINTER :: matrix
93 TYPE(dft_control_type), POINTER :: dft_control
94 TYPE(pw_c1d_gs_type) :: edens_g
95 TYPE(pw_c1d_gs_type), POINTER :: rho_core, rho_tot_gspace
96 TYPE(pw_env_type), POINTER :: pw_env
97 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
98 TYPE(pw_r3d_rs_type) :: band_density, edens_r, hartree_density, &
99 xc_density
100 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
101 TYPE(pw_r3d_rs_type), POINTER :: rho_tot_rspace, v_hartree_rspace
102 TYPE(qs_energy_type), POINTER :: energy
103 TYPE(qs_ks_env_type), POINTER :: ks_env
104 TYPE(qs_rho_type), POINTER :: rho, rho_struct
105 TYPE(section_vals_type), POINTER :: input, xc_section
106
107 CALL timeset(routinen, handle)
108
109 CALL cite_reference(cohen2000)
110
111 cpassert(ASSOCIATED(qs_env))
112 logger => cp_get_default_logger()
114
115 ! Check for GAPW method : additional terms for local densities
116 CALL get_qs_env(qs_env, nkind=nkind, dft_control=dft_control)
117 gapw = dft_control%qs_control%gapw
118 gapw_xc = dft_control%qs_control%gapw_xc
119
120 nimages = dft_control%nimages
121 nspins = dft_control%nspins
122
123 ! get working arrays
124 CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
125 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
126 CALL auxbas_pw_pool%create_pw(band_density)
127 CALL auxbas_pw_pool%create_pw(hartree_density)
128 CALL auxbas_pw_pool%create_pw(xc_density)
129
130 ! w matrix
131 CALL get_qs_env(qs_env, matrix_w_kp=matrix_w)
132 IF (.NOT. ASSOCIATED(matrix_w)) THEN
133 CALL get_qs_env(qs_env, &
134 ks_env=ks_env, &
135 matrix_s_kp=matrix_s)
136 matrix => matrix_s(1, 1)%matrix
137 CALL dbcsr_allocate_matrix_set(matrix_w, nspins, nimages)
138 DO ispin = 1, nspins
139 DO img = 1, nimages
140 ALLOCATE (matrix_w(ispin, img)%matrix)
141 CALL dbcsr_copy(matrix_w(ispin, img)%matrix, matrix, name="W MATRIX")
142 CALL dbcsr_set(matrix_w(ispin, img)%matrix, 0.0_dp)
143 END DO
144 END DO
145 CALL set_ks_env(ks_env, matrix_w_kp=matrix_w)
146 END IF
147 ! band structure energy density
148 CALL qs_energies_compute_w(qs_env, .true.)
149 CALL get_qs_env(qs_env, ks_env=ks_env, matrix_w_kp=matrix_w)
150 CALL auxbas_pw_pool%create_pw(edens_r)
151 CALL auxbas_pw_pool%create_pw(edens_g)
152 CALL pw_zero(band_density)
153 DO ispin = 1, nspins
154 rho_ao => matrix_w(ispin, :)
155 CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
156 rho=edens_r, &
157 rho_gspace=edens_g, &
158 ks_env=ks_env, soft_valid=(gapw .OR. gapw_xc))
159 CALL pw_axpy(edens_r, band_density)
160 END DO
161 CALL auxbas_pw_pool%give_back_pw(edens_r)
162 CALL auxbas_pw_pool%give_back_pw(edens_g)
163
164 ! Hartree energy density correction = -0.5 * V_H(r) * [rho(r) - rho_core(r)]
165 ALLOCATE (rho_tot_gspace, rho_tot_rspace)
166 CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
167 CALL auxbas_pw_pool%create_pw(rho_tot_rspace)
168 NULLIFY (rho_core)
169 CALL get_qs_env(qs_env, &
170 v_hartree_rspace=v_hartree_rspace, &
171 rho_core=rho_core, rho=rho)
172 CALL qs_rho_get(rho, rho_r=rho_r)
173 IF (ASSOCIATED(rho_core)) THEN
174 CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
175 CALL pw_transfer(rho_core, rho_tot_rspace)
176 ELSE
177 CALL pw_zero(rho_tot_rspace)
178 END IF
179 DO ispin = 1, nspins
180 CALL pw_axpy(rho_r(ispin), rho_tot_rspace, alpha=-1.0_dp)
181 END DO
182 CALL pw_zero(hartree_density)
183 ovol = 0.5_dp/hartree_density%pw_grid%dvol
184 CALL pw_multiply(hartree_density, v_hartree_rspace, rho_tot_rspace, alpha=ovol)
185 CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
186 CALL auxbas_pw_pool%give_back_pw(rho_tot_rspace)
187 DEALLOCATE (rho_tot_gspace, rho_tot_rspace)
188
189 IF (dft_control%do_admm) THEN
190 CALL cp_warn(__location__, "ADMM not supported for local energy calculation")
191 END IF
192 IF (gapw_xc .OR. gapw) THEN
193 CALL cp_warn(__location__, "GAPW/GAPW_XC not supported for local energy calculation")
194 END IF
195 ! XC energy density correction = E_xc(r) - V_xc(r)*rho(r)
196 CALL get_qs_env(qs_env, input=input)
197 xc_section => section_vals_get_subs_vals(input, "DFT%XC")
198 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
199 !
200 CALL qs_xc_density(ks_env, rho_struct, xc_section, xc_ener=xc_density)
201 !
202 ! energies
203 CALL get_qs_env(qs_env=qs_env, energy=energy)
204 eban = pw_integrate_function(band_density)
205 eh = pw_integrate_function(hartree_density)
206 exc = pw_integrate_function(xc_density)
207
208 ! band energy
209 CALL get_qs_env(qs_env, matrix_ks_kp=matrix_ks)
210 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
211 CALL calculate_ptrace(matrix_ks, rho_ao_kp, eband, nspins)
212
213 ! get full density
214 CALL pw_copy(band_density, energy_density)
215 CALL pw_axpy(hartree_density, energy_density)
216 CALL pw_axpy(xc_density, energy_density)
217
218 IF (iounit > 0) THEN
219 WRITE (unit=iounit, fmt="(/,T3,A)") repeat("=", 78)
220 WRITE (unit=iounit, fmt="(T4,A,T52,A,T75,A)") "Local Energy Calculation", "GPW/GAPW", "Local"
221 WRITE (unit=iounit, fmt="(T4,A,T45,F15.8,T65,F15.8)") "Band Energy", eband, eban
222 WRITE (unit=iounit, fmt="(T4,A,T65,F15.8)") "Hartree Energy Correction", eh
223 WRITE (unit=iounit, fmt="(T4,A,T65,F15.8)") "XC Energy Correction", exc
224 WRITE (unit=iounit, fmt="(T4,A,T45,F15.8,T65,F15.8)") "Total Energy", &
225 energy%total, eban + eh + exc + energy%core_overlap + energy%core_self + energy%dispersion
226 WRITE (unit=iounit, fmt="(T3,A)") repeat("=", 78)
227 END IF
228
229 ! return temp arrays
230 CALL auxbas_pw_pool%give_back_pw(band_density)
231 CALL auxbas_pw_pool%give_back_pw(hartree_density)
232 CALL auxbas_pw_pool%give_back_pw(xc_density)
233
234 CALL timestop(handle)
235
236 END SUBROUTINE qs_local_energy
237
238! **************************************************************************************************
239!> \brief Routine to calculate the local stress
240!> \param qs_env the qs_env to update
241!> \param stress_tensor ...
242!> \param beta ...
243!> \par History
244!> 07.2019 created
245!> \author JGH
246! **************************************************************************************************
247 SUBROUTINE qs_local_stress(qs_env, stress_tensor, beta)
248 TYPE(qs_environment_type), POINTER :: qs_env
249 TYPE(pw_r3d_rs_type), DIMENSION(:, :), &
250 INTENT(INOUT), OPTIONAL :: stress_tensor
251 REAL(kind=dp), INTENT(IN), OPTIONAL :: beta
252
253 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_local_stress'
254
255 INTEGER :: handle, i, iounit, j, nimages, nkind, &
256 nspins
257 LOGICAL :: do_stress, gapw, gapw_xc, use_virial
258 REAL(kind=dp) :: my_beta
259 REAL(kind=dp), DIMENSION(3, 3) :: pv_loc
260 TYPE(cp_logger_type), POINTER :: logger
261 TYPE(dft_control_type), POINTER :: dft_control
262 TYPE(pw_c1d_gs_type) :: v_hartree_gspace
263 TYPE(pw_c1d_gs_type), DIMENSION(3) :: efield
264 TYPE(pw_env_type), POINTER :: pw_env
265 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
266 TYPE(pw_r3d_rs_type) :: xc_density
267 TYPE(pw_r3d_rs_type), POINTER :: v_hartree_rspace
268 TYPE(qs_ks_env_type), POINTER :: ks_env
269 TYPE(qs_rho_type), POINTER :: rho_struct
270 TYPE(section_vals_type), POINTER :: input, xc_section
271 TYPE(virial_type), POINTER :: virial
272
273 CALL cp_warn(__location__, "Local Stress Tensor code is not working, skipping")
274 RETURN
275
276 CALL timeset(routinen, handle)
277
278 CALL cite_reference(filippetti2000)
279 CALL cite_reference(rogers2002)
280
281 cpassert(ASSOCIATED(qs_env))
282
283 IF (PRESENT(stress_tensor)) THEN
284 do_stress = .true.
285 ELSE
286 do_stress = .false.
287 END IF
288 IF (PRESENT(beta)) THEN
289 my_beta = beta
290 ELSE
291 my_beta = 0.0_dp
292 END IF
293
294 logger => cp_get_default_logger()
296
297 !!!!!!
298 CALL cp_warn(__location__, "Local Stress Tensor code is not tested")
299 !!!!!!
300
301 ! Check for GAPW method : additional terms for local densities
302 CALL get_qs_env(qs_env, nkind=nkind, dft_control=dft_control)
303 gapw = dft_control%qs_control%gapw
304 gapw_xc = dft_control%qs_control%gapw_xc
305
306 nimages = dft_control%nimages
307 nspins = dft_control%nspins
308
309 ! get working arrays
310 CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
311 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
312 CALL auxbas_pw_pool%create_pw(xc_density)
313
314 ! init local stress tensor
315 IF (do_stress) THEN
316 DO i = 1, 3
317 DO j = 1, 3
318 CALL pw_zero(stress_tensor(i, j))
319 END DO
320 END DO
321 END IF
322
323 IF (dft_control%do_admm) THEN
324 CALL cp_warn(__location__, "ADMM not supported for local energy calculation")
325 END IF
326 IF (gapw_xc .OR. gapw) THEN
327 CALL cp_warn(__location__, "GAPW/GAPW_XC not supported for local energy calculation")
328 END IF
329 ! XC energy density correction = E_xc(r) - V_xc(r)*rho(r)
330 CALL get_qs_env(qs_env, ks_env=ks_env, input=input, rho=rho_struct)
331 xc_section => section_vals_get_subs_vals(input, "DFT%XC")
332 !
333 CALL qs_xc_density(ks_env, rho_struct, xc_section, xc_ener=xc_density)
334
335 ! Electrical field terms
336 CALL get_qs_env(qs_env, v_hartree_rspace=v_hartree_rspace)
337 CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
338 CALL pw_transfer(v_hartree_rspace, v_hartree_gspace)
339 DO i = 1, 3
340 CALL auxbas_pw_pool%create_pw(efield(i))
341 CALL pw_copy(v_hartree_gspace, efield(i))
342 END DO
343 CALL pw_derive(efield(1), (/1, 0, 0/))
344 CALL pw_derive(efield(2), (/0, 1, 0/))
345 CALL pw_derive(efield(3), (/0, 0, 1/))
346 CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
347 DO i = 1, 3
348 CALL auxbas_pw_pool%give_back_pw(efield(i))
349 END DO
350
351 pv_loc = 0.0_dp
352
353 CALL get_qs_env(qs_env=qs_env, virial=virial)
354 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
355 IF (.NOT. use_virial) THEN
356 CALL cp_warn(__location__, "Local stress should be used with standard stress calculation.")
357 END IF
358 IF (iounit > 0 .AND. use_virial) THEN
359 WRITE (unit=iounit, fmt="(/,T3,A)") repeat("=", 78)
360 WRITE (unit=iounit, fmt="(T4,A)") "Local Stress Calculation"
361 WRITE (unit=iounit, fmt="(T42,A,T64,A)") " 1/3 Trace", " Determinant"
362 WRITE (unit=iounit, fmt="(T4,A,T42,F16.8,T64,F16.8)") "Total Stress", &
363 (pv_loc(1, 1) + pv_loc(2, 2) + pv_loc(3, 3))/3.0_dp, det_3x3(pv_loc)
364 WRITE (unit=iounit, fmt="(T3,A)") repeat("=", 78)
365 END IF
366
367 CALL timestop(handle)
368
369 END SUBROUTINE qs_local_stress
370
371! **************************************************************************************************
372
373END MODULE qs_local_properties
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public filippetti2000
integer, save, public cohen2000
integer, save, public rogers2002
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
DBCSR operations in CP2K.
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
subroutine, public pw_derive(pw, n)
Calculate the derivative of a plane wave vector.
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Calculate the plane wave density by collocating the primitive Gaussian functions (pgf).
subroutine, public calculate_rho_elec(matrix_p, matrix_p_kp, rho, rho_gspace, total_rho, ks_env, soft_valid, compute_tau, compute_grad, basis_type, der_type, idir, task_list_external, pw_env_external)
computes the density corresponding to a given density matrix on the grid
Calculation of the energies concerning the core charge distribution.
Utility subroutine for qs energy calculation.
subroutine, public qs_energies_compute_w(qs_env, calc_forces)
Refactoring of qs_energies_scf. Moves computation of matrix_w into separate subroutine.
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, ecoul_1c, rho0_s_rs, rho0_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, rhs)
Get the QUICKSTEP environment.
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
subroutine, public calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho, skip_nuclear_density)
...
subroutine, public set_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, vppl, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, kpoints, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, task_list, task_list_soft, subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env)
...
Routines for calculating local energy and stress tensor.
subroutine, public qs_local_stress(qs_env, stress_tensor, beta)
Routine to calculate the local stress.
subroutine, public qs_local_energy(qs_env, energy_density)
Routine to calculate the local energy.
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
subroutine, public qs_xc_density(ks_env, rho_struct, xc_section, dispersion_env, xc_ener, xc_den, vxc, vtau)
calculates the XC density: E_xc(r) - V_xc(r)*rho(r) or E_xc(r)/rho(r)
Definition qs_vxc.F:573
type of a logger, at the moment it contains just a print level starting at which level it should be l...
contained for different pw related things
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.