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 Calculate the derivatives of the MO coefficients wrt nuclear coordinates
10 : !> \author Sandra Luber, Edward Ditler
11 : ! **************************************************************************************************
12 :
13 : MODULE qs_dcdr_ao
14 :
15 : USE basis_set_types, ONLY: gto_basis_set_p_type,&
16 : gto_basis_set_type
17 : USE cp_control_types, ONLY: dft_control_type
18 : USE cp_dbcsr_api, ONLY: dbcsr_copy,&
19 : dbcsr_get_block_p,&
20 : dbcsr_p_type,&
21 : dbcsr_set,&
22 : dbcsr_type
23 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
24 : copy_fm_to_dbcsr
25 : USE cp_fm_types, ONLY: cp_fm_create,&
26 : cp_fm_release,&
27 : cp_fm_type
28 : USE cp_log_handling, ONLY: cp_get_default_logger,&
29 : cp_logger_type
30 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
31 : section_vals_type
32 : USE kinds, ONLY: default_string_length,&
33 : dp
34 : USE parallel_gemm_api, ONLY: parallel_gemm
35 : USE pw_env_types, ONLY: pw_env_get,&
36 : pw_env_type
37 : USE pw_methods, ONLY: pw_axpy,&
38 : pw_copy,&
39 : pw_scale,&
40 : pw_transfer,&
41 : pw_zero
42 : USE pw_poisson_methods, ONLY: pw_poisson_solve
43 : USE pw_poisson_types, ONLY: pw_poisson_type
44 : USE pw_pool_types, ONLY: pw_pool_p_type,&
45 : pw_pool_type
46 : USE pw_types, ONLY: pw_c1d_gs_type,&
47 : pw_r3d_rs_type
48 : USE qs_collocate_density, ONLY: calculate_drho_core,&
49 : calculate_drho_elec_dR
50 : USE qs_core_matrices, ONLY: core_matrices
51 : USE qs_energy_types, ONLY: qs_energy_type
52 : USE qs_environment_types, ONLY: get_qs_env,&
53 : qs_environment_type
54 : USE qs_fxc, ONLY: qs_fxc_create
55 : USE qs_integral_utils, ONLY: basis_set_list_setup
56 : USE qs_integrate_potential, ONLY: integrate_v_dbasis,&
57 : integrate_v_rspace
58 : USE qs_kind_types, ONLY: qs_kind_type
59 : USE qs_ks_types, ONLY: get_ks_env,&
60 : qs_ks_env_type
61 : USE qs_linres_types, ONLY: dcdr_env_type
62 : USE qs_neighbor_list_types, ONLY: get_iterator_info,&
63 : get_neighbor_list_set_p,&
64 : neighbor_list_iterate,&
65 : neighbor_list_iterator_create,&
66 : neighbor_list_iterator_p_type,&
67 : neighbor_list_iterator_release,&
68 : neighbor_list_set_p_type
69 : USE qs_rho_atom_types, ONLY: rho_atom_type
70 : USE qs_rho_methods, ONLY: qs_rho_rebuild,&
71 : qs_rho_update_rho
72 : USE qs_rho_types, ONLY: qs_rho_create,&
73 : qs_rho_get,&
74 : qs_rho_release,&
75 : qs_rho_set,&
76 : qs_rho_type
77 : USE qs_vxc, ONLY: qs_vxc_create
78 :
79 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
80 : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
81 : !$ omp_init_lock, omp_set_lock, &
82 : !$ omp_unset_lock, omp_destroy_lock
83 :
84 : #include "./base/base_uses.f90"
85 :
86 : IMPLICIT NONE
87 :
88 : PRIVATE
89 : PUBLIC :: core_dR, d_vhxc_dR, d_core_charge_density_dR, apply_op_constant_term
90 : PUBLIC :: vhxc_R_perturbed_basis_functions
91 : PUBLIC :: hr_mult_by_delta_1d
92 :
93 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_dcdr_ao'
94 : CHARACTER(len=*), PARAMETER, PRIVATE :: dcdr_meta_gga_error = &
95 : "Analytical DCDR is not implemented for functionals that depend on the kinetic energy density. "// &
96 : "Use PROPERTIES%LINRES%DCDR%APT_FD T to calculate APTs by finite differences."
97 :
98 : CONTAINS
99 :
100 : ! **************************************************************************************************
101 : !> \brief Build the perturbed density matrix correction depending on the overlap derivative
102 : !> \param qs_env ...
103 : !> \param dcdr_env ...
104 : !> \param overlap1 Overlap derivative in AO basis
105 : !> \author Edward Ditler
106 : ! **************************************************************************************************
107 252 : SUBROUTINE apply_op_constant_term(qs_env, dcdr_env, overlap1)
108 : TYPE(qs_environment_type), POINTER :: qs_env
109 : TYPE(dcdr_env_type) :: dcdr_env
110 : TYPE(dbcsr_p_type), OPTIONAL :: overlap1
111 :
112 : CHARACTER(len=*), PARAMETER :: routineN = 'apply_op_constant_term'
113 :
114 : INTEGER :: handle, ispin
115 : REAL(KIND=dp) :: energy_hartree
116 : TYPE(cp_fm_type) :: rho_ao_fm, rho_ao_s1, rho_ao_s1_rho_ao, &
117 : s1_ao
118 252 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho1_ao, rho_ao
119 : TYPE(pw_c1d_gs_type) :: rho1_tot_gspace, v_hartree_gspace
120 252 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho1_g
121 : TYPE(pw_env_type), POINTER :: pw_env
122 : TYPE(pw_poisson_type), POINTER :: poisson_env
123 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
124 : TYPE(pw_r3d_rs_type) :: v_hartree_rspace
125 252 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, v_rspace_new, v_xc, v_xc_tau
126 : TYPE(qs_rho_type), POINTER :: perturbed_density, rho
127 252 : TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho0_atom_set, rho1_atom_set
128 : TYPE(section_vals_type), POINTER :: input, xc_section
129 :
130 : ! Build the perturbed density matrix correction depending on the overlap derivative
131 : ! P1 = C0 C1 + C1 C0
132 : ! - C0_(mu j) S1_(jk) C0_(k nu)
133 : ! This routine is adapted from apply_op_2_dft. There, build_dm_response builds
134 : ! C0 * dCR + dCR * C0.
135 : ! build_dm_response is computing $-1 * (C^0 C^1 + C^1 C^0)$ and later on in the
136 : ! integration the factor 2 is applied to account for the occupancy.
137 : ! The sign is negative because the kernel is on the RHS of the Sternheimer equation.
138 : !
139 : ! The correction factor in this routine needs to have
140 : ! the opposite sign mathematically as (C0 C1 + C1 C0)
141 : ! so the same sign in the code because of the $-1$ in dCR
142 : ! so the opposite sign in the code because we are on the LHS of the Sternheimer equation.
143 : !
144 : ! This term must not go into the kernel applied by the linear response solver, because
145 : ! for the (P)CG algorithm, all constant terms have to be on one side of the equations
146 : ! and all solution dependent terms must be on the other side.
147 :
148 252 : CALL timeset(routineN, handle)
149 :
150 252 : NULLIFY (auxbas_pw_pool, pw_env, v_xc, poisson_env, input, rho, &
151 252 : rho1_g, v_xc_tau)
152 :
153 252 : CALL cp_fm_create(rho_ao_fm, dcdr_env%aoao_fm_struct)
154 252 : CALL cp_fm_create(rho_ao_s1, dcdr_env%aoao_fm_struct)
155 252 : CALL cp_fm_create(rho_ao_s1_rho_ao, dcdr_env%aoao_fm_struct)
156 252 : CALL cp_fm_create(s1_ao, dcdr_env%aoao_fm_struct)
157 :
158 252 : IF (PRESENT(overlap1)) THEN
159 0 : CALL copy_dbcsr_to_fm(overlap1%matrix, s1_ao)
160 : ELSE
161 252 : CALL copy_dbcsr_to_fm(dcdr_env%matrix_s1(dcdr_env%beta + 1)%matrix, s1_ao)
162 : END IF
163 :
164 576 : DO ispin = 1, dcdr_env%nspins
165 324 : CALL dbcsr_set(dcdr_env%perturbed_dm_correction(ispin)%matrix, 0._dp)
166 324 : CALL dbcsr_set(dcdr_env%matrix_apply_op_constant(ispin)%matrix, 0.0_dp)
167 :
168 : CALL parallel_gemm('N', 'T', dcdr_env%nao, dcdr_env%nao, dcdr_env%nmo(ispin), &
169 : 1.0_dp, dcdr_env%mo_coeff(ispin), dcdr_env%mo_coeff(ispin), &
170 324 : 0.0_dp, rho_ao_fm)
171 :
172 : CALL parallel_gemm('N', 'N', dcdr_env%nao, dcdr_env%nao, dcdr_env%nao, &
173 : 1.0_dp, rho_ao_fm, s1_ao, &
174 324 : 0.0_dp, rho_ao_s1)
175 :
176 : CALL parallel_gemm('N', 'N', dcdr_env%nao, dcdr_env%nao, dcdr_env%nao, &
177 : -1._dp, rho_ao_s1, rho_ao_fm, & ! this is the sign mentioned above.
178 324 : 0.0_dp, rho_ao_s1_rho_ao)
179 :
180 576 : CALL copy_fm_to_dbcsr(rho_ao_s1_rho_ao, dcdr_env%perturbed_dm_correction(ispin)%matrix)
181 : END DO
182 :
183 252 : CALL cp_fm_release(rho_ao_fm)
184 252 : CALL cp_fm_release(rho_ao_s1)
185 252 : CALL cp_fm_release(rho_ao_s1_rho_ao)
186 252 : CALL cp_fm_release(s1_ao)
187 : ! Done building the density matrix correction
188 :
189 : ! Build the density struct from the environment
190 : NULLIFY (perturbed_density)
191 252 : ALLOCATE (perturbed_density)
192 252 : CALL qs_rho_create(perturbed_density)
193 252 : CALL qs_rho_rebuild(perturbed_density, qs_env=qs_env)
194 :
195 : ! ... set the density matrix to be the perturbed density matrix
196 252 : CALL qs_rho_get(perturbed_density, rho_ao=rho1_ao)
197 576 : DO ispin = 1, dcdr_env%nspins
198 576 : CALL dbcsr_copy(rho1_ao(ispin)%matrix, dcdr_env%perturbed_dm_correction(ispin)%matrix)
199 : END DO
200 :
201 : ! ... updates rho_r and rho_g to the rho%rho_ao.
202 : CALL qs_rho_update_rho(rho_struct=perturbed_density, &
203 252 : qs_env=qs_env)
204 :
205 : ! Also update the qs_env%rho
206 252 : CALL get_qs_env(qs_env, rho=rho)
207 252 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
208 252 : CALL qs_rho_get(rho, rho_ao=rho_ao, rho_r=rho_r)
209 :
210 : energy_hartree = 0.0_dp
211 :
212 : CALL get_qs_env(qs_env=qs_env, &
213 : pw_env=pw_env, &
214 252 : input=input)
215 :
216 : ! Create the temporary grids
217 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
218 252 : poisson_env=poisson_env)
219 :
220 252 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
221 :
222 1080 : ALLOCATE (v_rspace_new(dcdr_env%nspins))
223 252 : CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
224 252 : CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
225 :
226 : ! Calculate the Hartree potential on the total density
227 252 : CALL auxbas_pw_pool%create_pw(rho1_tot_gspace)
228 :
229 252 : CALL qs_rho_get(perturbed_density, rho_g=rho1_g)
230 252 : CALL pw_copy(rho1_g(1), rho1_tot_gspace)
231 324 : DO ispin = 2, dcdr_env%nspins
232 324 : CALL pw_axpy(rho1_g(ispin), rho1_tot_gspace)
233 : END DO
234 :
235 : CALL pw_poisson_solve(poisson_env, rho1_tot_gspace, &
236 : energy_hartree, &
237 252 : v_hartree_gspace)
238 252 : CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
239 :
240 252 : CALL auxbas_pw_pool%give_back_pw(rho1_tot_gspace)
241 :
242 : ! Calculate the second derivative of the exchange-correlation potential
243 : CALL qs_fxc_create(qs_env, rho, perturbed_density, rho0_atom_set, xc_section, &
244 252 : .FALSE., v_xc, v_xc_tau, rho1_atom_set)
245 : !
246 :
247 576 : DO ispin = 1, dcdr_env%nspins
248 576 : v_rspace_new(ispin) = v_xc(ispin)
249 : END DO
250 252 : DEALLOCATE (v_xc)
251 :
252 : ! Done calculating the potentials
253 :
254 : !-------------------------------!
255 : ! Add both hartree and xc terms !
256 : !-------------------------------!
257 252 : CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
258 576 : DO ispin = 1, dcdr_env%nspins
259 576 : CALL pw_scale(v_rspace_new(ispin), v_rspace_new(ispin)%pw_grid%dvol)
260 : END DO
261 :
262 576 : DO ispin = 1, dcdr_env%nspins
263 324 : CALL dbcsr_set(dcdr_env%matrix_apply_op_constant(ispin)%matrix, 0.0_dp)
264 324 : CALL pw_axpy(v_hartree_rspace, v_rspace_new(ispin))
265 324 : IF (dcdr_env%nspins == 1) THEN
266 180 : CALL pw_scale(v_rspace_new(1), 2.0_dp)
267 : END IF
268 :
269 : CALL integrate_v_rspace(v_rspace=v_rspace_new(ispin), &
270 : hmat=dcdr_env%matrix_apply_op_constant(ispin), &
271 : qs_env=qs_env, &
272 576 : calculate_forces=.FALSE.)
273 : END DO
274 :
275 252 : CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
276 252 : CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
277 576 : DO ispin = 1, dcdr_env%nspins
278 576 : CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
279 : END DO
280 252 : DEALLOCATE (v_rspace_new)
281 :
282 252 : IF (ASSOCIATED(v_xc_tau)) THEN
283 0 : CALL pw_scale(v_xc_tau(1), 2._dp*v_xc_tau(1)%pw_grid%dvol)
284 : CALL integrate_v_rspace(v_rspace=v_xc_tau(1), &
285 : hmat=dcdr_env%matrix_apply_op_constant(1), &
286 : qs_env=qs_env, &
287 : compute_tau=.TRUE., &
288 0 : calculate_forces=.FALSE.)
289 :
290 0 : CALL auxbas_pw_pool%give_back_pw(v_xc_tau(1))
291 0 : DEALLOCATE (v_xc_tau)
292 : END IF
293 :
294 252 : CALL qs_rho_release(perturbed_density)
295 252 : DEALLOCATE (perturbed_density)
296 :
297 252 : CALL timestop(handle)
298 :
299 756 : END SUBROUTINE apply_op_constant_term
300 :
301 : ! **************************************************************************************************
302 : !> \brief Calculate the derivative of the Hartree term due to the core charge density
303 : !> \param qs_env ...
304 : !> \param dcdr_env ...
305 : !> \author Edward Ditler
306 : ! **************************************************************************************************
307 72 : SUBROUTINE d_core_charge_density_dR(qs_env, dcdr_env)
308 : ! drho_core contribution
309 : ! sum over all directions
310 : ! output in ao x ao
311 : TYPE(qs_environment_type), POINTER :: qs_env
312 : TYPE(dcdr_env_type) :: dcdr_env
313 :
314 : CHARACTER(len=*), PARAMETER :: routineN = 'd_core_charge_density_dR'
315 :
316 : INTEGER :: beta, handle
317 : TYPE(cp_logger_type), POINTER :: logger
318 : TYPE(dft_control_type), POINTER :: dft_control
319 : TYPE(pw_c1d_gs_type) :: drho_g, v_hartree_gspace
320 : TYPE(pw_env_type), POINTER :: pw_env
321 : TYPE(pw_poisson_type), POINTER :: poisson_env
322 72 : TYPE(pw_pool_p_type), DIMENSION(:), POINTER :: pw_pools
323 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
324 : TYPE(pw_r3d_rs_type) :: v_hartree_rspace
325 : TYPE(qs_rho_type), POINTER :: rho
326 :
327 72 : CALL timeset(routineN, handle)
328 :
329 72 : logger => cp_get_default_logger()
330 :
331 72 : NULLIFY (pw_env, auxbas_pw_pool, pw_pools, poisson_env, dft_control, &
332 72 : rho)
333 :
334 : CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, rho=rho, &
335 72 : dft_control=dft_control)
336 :
337 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env, &
338 72 : pw_pools=pw_pools)
339 :
340 : ! Create the Hartree potential grids in real and reciprocal space.
341 72 : CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
342 72 : CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
343 : ! Create the grid for the derivative of the core potential
344 72 : CALL auxbas_pw_pool%create_pw(drho_g)
345 :
346 288 : DO beta = 1, 3
347 216 : CALL pw_zero(v_hartree_gspace)
348 216 : CALL pw_zero(v_hartree_rspace)
349 216 : CALL pw_zero(drho_g)
350 :
351 : ! Calculate the Hartree potential on the perturbed density and Poisson solve it
352 : CALL calculate_drho_core(drho_core=drho_g, qs_env=qs_env, &
353 216 : beta=beta, lambda=dcdr_env%lambda)
354 : CALL pw_poisson_solve(poisson_env, drho_g, &
355 216 : vhartree=v_hartree_gspace)
356 216 : CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
357 216 : CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
358 :
359 : ! Calculate the integrals
360 : CALL integrate_v_rspace(v_rspace=v_hartree_rspace, &
361 : hmat=dcdr_env%matrix_core_charge_1(beta), &
362 : qs_env=qs_env, &
363 288 : calculate_forces=.FALSE.)
364 : END DO
365 :
366 72 : CALL auxbas_pw_pool%give_back_pw(drho_g)
367 72 : CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
368 72 : CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
369 :
370 72 : CALL timestop(handle)
371 72 : END SUBROUTINE d_core_charge_density_dR
372 :
373 : ! **************************************************************************************************
374 : !> \brief Core Hamiltonian contributions to the operator (the pseudopotentials)
375 : !> \param qs_env ...
376 : !> \param dcdr_env ..
377 : !> \author Edward Ditler
378 : ! **************************************************************************************************
379 72 : SUBROUTINE core_dR(qs_env, dcdr_env)
380 : TYPE(qs_environment_type), POINTER :: qs_env
381 : TYPE(dcdr_env_type) :: dcdr_env
382 :
383 : CHARACTER(LEN=*), PARAMETER :: routineN = 'core_dR'
384 :
385 : CHARACTER(LEN=default_string_length) :: my_basis_type
386 : INTEGER :: handle, nder
387 : LOGICAL :: calculate_forces
388 72 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
389 72 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_h, matrix_p_pass
390 : TYPE(qs_ks_env_type), POINTER :: ks_env
391 : TYPE(qs_rho_type), POINTER :: rho
392 :
393 72 : CALL timeset(routineN, handle)
394 :
395 72 : CALL get_qs_env(qs_env=qs_env, ks_env=ks_env)
396 72 : CALL get_ks_env(ks_env=ks_env, rho=rho)
397 72 : CALL qs_rho_get(rho, rho_ao=rho_ao)
398 :
399 72 : nder = 1
400 72 : calculate_forces = .FALSE.
401 :
402 : my_basis_type = "ORB"
403 :
404 72 : NULLIFY (matrix_h)
405 72 : matrix_p_pass(1:1, 1:1) => rho_ao(1:1)
406 : CALL core_matrices(qs_env, matrix_h, matrix_p_pass, calculate_forces, nder, &
407 72 : dcdr_env=dcdr_env)
408 :
409 72 : CALL timestop(handle)
410 :
411 72 : END SUBROUTINE core_dR
412 :
413 : ! **************************************************************************************************
414 : !> \brief The derivatives of the basis functions going into the HXC potential wrt nuclear positions
415 : !> \param qs_env ...
416 : !> \param dcdr_env ...
417 : !> \author Edward Ditler
418 : ! **************************************************************************************************
419 72 : SUBROUTINE d_vhxc_dR(qs_env, dcdr_env)
420 : TYPE(qs_environment_type), POINTER :: qs_env
421 : TYPE(dcdr_env_type) :: dcdr_env
422 :
423 : CHARACTER(len=*), PARAMETER :: routineN = 'd_vhxc_dR'
424 :
425 : INTEGER :: handle, idir, ispin
426 72 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
427 : TYPE(pw_c1d_gs_type) :: drho_g_total, v_hartree_gspace
428 72 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: drho_g
429 : TYPE(pw_env_type), POINTER :: pw_env
430 : TYPE(pw_poisson_type), POINTER :: poisson_env
431 72 : TYPE(pw_pool_p_type), DIMENSION(:), POINTER :: pw_pools
432 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
433 : TYPE(pw_r3d_rs_type) :: drho_r_total, v_hartree_rspace
434 72 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: drho_r, dtau_r, rho_r, v_xc, v_xc_tau
435 : TYPE(qs_rho_type), POINTER :: drho, rho
436 72 : TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho0_atom_set, rho1_atom_set
437 : TYPE(section_vals_type), POINTER :: input, xc_section
438 :
439 72 : CALL timeset(routineN, handle)
440 :
441 : CALL get_qs_env(qs_env=qs_env, &
442 : pw_env=pw_env, &
443 : input=input, &
444 72 : rho=rho)
445 72 : CALL qs_rho_get(rho, rho_ao=rho_ao, rho_r=rho_r)
446 :
447 72 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
448 :
449 : ! get the tmp grids
450 300 : ALLOCATE (drho_r(dcdr_env%nspins))
451 300 : ALLOCATE (drho_g(dcdr_env%nspins))
452 :
453 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
454 72 : pw_pools=pw_pools, poisson_env=poisson_env)
455 72 : CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
456 72 : CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
457 :
458 156 : DO ispin = 1, dcdr_env%nspins
459 84 : CALL auxbas_pw_pool%create_pw(drho_r(ispin))
460 156 : CALL auxbas_pw_pool%create_pw(drho_g(ispin))
461 : END DO
462 72 : CALL auxbas_pw_pool%create_pw(drho_g_total)
463 72 : CALL auxbas_pw_pool%create_pw(drho_r_total)
464 :
465 288 : DO idir = 1, 3
466 216 : CALL pw_zero(v_hartree_gspace)
467 216 : CALL pw_zero(v_hartree_rspace)
468 216 : CALL pw_zero(drho_g_total)
469 216 : CALL pw_zero(drho_r_total)
470 :
471 468 : DO ispin = 1, dcdr_env%nspins
472 252 : CALL pw_zero(drho_r(ispin))
473 252 : CALL pw_zero(drho_g(ispin))
474 :
475 : ! Get the density
476 : CALL calculate_drho_elec_dR(matrix_p=rho_ao(ispin)%matrix, &
477 : drho=drho_r(ispin), &
478 : drho_gspace=drho_g(ispin), &
479 : qs_env=qs_env, &
480 252 : beta=idir, lambda=dcdr_env%lambda)
481 :
482 252 : CALL pw_axpy(drho_g(ispin), drho_g_total)
483 468 : CALL pw_axpy(drho_r(ispin), drho_r_total)
484 : END DO
485 : ! Get the Hartree potential corresponding to the perturbed density
486 : CALL pw_poisson_solve(poisson_env, drho_g_total, &
487 216 : vhartree=v_hartree_gspace)
488 216 : CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
489 :
490 216 : ALLOCATE (drho)
491 216 : CALL qs_rho_create(drho)
492 216 : IF (ASSOCIATED(drho_r)) THEN
493 216 : CALL qs_rho_set(drho, rho_r=drho_r, rho_r_valid=.TRUE.)
494 : END IF
495 216 : IF (ASSOCIATED(drho_g)) THEN
496 216 : CALL qs_rho_set(drho, rho_g=drho_g, rho_g_valid=.TRUE.)
497 : END IF
498 216 : IF (ASSOCIATED(dtau_r)) THEN
499 192 : CALL qs_rho_set(drho, tau_r=dtau_r, tau_r_valid=.TRUE.)
500 : END IF
501 : !
502 216 : NULLIFY (v_xc, v_xc_tau)
503 : CALL qs_fxc_create(qs_env, rho, drho, rho0_atom_set, xc_section, .FALSE., &
504 216 : v_xc, v_xc_tau, rho1_atom_set)
505 : !
506 216 : DEALLOCATE (drho)
507 :
508 216 : IF (ASSOCIATED(v_xc_tau)) THEN
509 0 : CPABORT(dcdr_meta_gga_error)
510 : END IF
511 :
512 : !-------------------------------!
513 : ! Add both hartree and xc terms !
514 : !-------------------------------!
515 468 : DO ispin = 1, dcdr_env%nspins
516 : ! Can the dvol be different?
517 252 : CALL pw_scale(v_xc(ispin), v_xc(ispin)%pw_grid%dvol)
518 252 : CALL pw_axpy(v_hartree_rspace, v_xc(ispin), v_hartree_rspace%pw_grid%dvol)
519 :
520 : CALL integrate_v_rspace(v_rspace=v_xc(ispin), &
521 : hmat=dcdr_env%matrix_d_vhxc_dR(idir, ispin), &
522 : qs_env=qs_env, &
523 252 : calculate_forces=.FALSE.)
524 :
525 : ! v_xc gets allocated again in xc_calc_2nd_deriv
526 468 : CALL auxbas_pw_pool%give_back_pw(v_xc(ispin))
527 : END DO ! ispin
528 504 : DEALLOCATE (v_xc)
529 : END DO ! idir
530 :
531 72 : CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
532 72 : CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
533 72 : CALL auxbas_pw_pool%give_back_pw(drho_g_total)
534 72 : CALL auxbas_pw_pool%give_back_pw(drho_r_total)
535 :
536 156 : DO ispin = 1, dcdr_env%nspins
537 84 : CALL auxbas_pw_pool%give_back_pw(drho_g(ispin))
538 156 : CALL auxbas_pw_pool%give_back_pw(drho_r(ispin))
539 : END DO
540 :
541 72 : DEALLOCATE (drho_g)
542 72 : DEALLOCATE (drho_r)
543 :
544 72 : CALL timestop(handle)
545 :
546 72 : END SUBROUTINE d_vhxc_dR
547 :
548 : ! **************************************************************************************************
549 : !> \brief The derivatives of the basis functions over which the HXC potential is integrated,
550 : !> so < da/dR | Vhxc | b >
551 : !> \param qs_env ...
552 : !> \param dcdr_env ...
553 : !> \author Edward Ditler
554 : ! **************************************************************************************************
555 72 : SUBROUTINE vhxc_R_perturbed_basis_functions(qs_env, dcdr_env)
556 : TYPE(qs_environment_type), POINTER :: qs_env
557 : TYPE(dcdr_env_type) :: dcdr_env
558 :
559 : CHARACTER(LEN=*), PARAMETER :: routineN = 'vhxc_R_perturbed_basis_functions'
560 :
561 : INTEGER :: handle, ispin
562 72 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_vhxc_dbasis
563 72 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_p
564 : TYPE(pw_env_type), POINTER :: pw_env
565 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
566 72 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: v_hxc_r, v_tau_rspace
567 : TYPE(pw_r3d_rs_type), POINTER :: v_hartree_r
568 : TYPE(qs_energy_type), POINTER :: energy
569 : TYPE(qs_ks_env_type), POINTER :: ks_env
570 : TYPE(qs_rho_type), POINTER :: rho_struct
571 : TYPE(section_vals_type), POINTER :: input, xc_section
572 :
573 72 : CALL timeset(routineN, handle)
574 :
575 72 : NULLIFY (rho_struct, energy, input, ks_env, pw_env, matrix_p)
576 : CALL get_qs_env(qs_env, &
577 : rho=rho_struct, &
578 : energy=energy, &
579 : input=input, &
580 : ks_env=ks_env, &
581 : pw_env=pw_env, &
582 72 : v_hartree_rspace=v_hartree_r)
583 72 : CALL qs_rho_get(rho_struct, rho_ao_kp=matrix_p)
584 72 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
585 :
586 72 : NULLIFY (auxbas_pw_pool)
587 72 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
588 :
589 : ! *** calculate the xc potential on the pw density ***
590 : ! *** associates v_hxc_r if the xc potential needs to be computed.
591 : ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
592 72 : NULLIFY (v_hxc_r, v_tau_rspace)
593 : CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
594 72 : vxc_rho=v_hxc_r, vxc_tau=v_tau_rspace, exc=energy%exc)
595 :
596 156 : DO ispin = 1, dcdr_env%nspins
597 84 : CALL pw_scale(v_hxc_r(ispin), v_hxc_r(ispin)%pw_grid%dvol)
598 :
599 : ! sum up potentials and integrate
600 84 : CALL pw_axpy(v_hartree_r, v_hxc_r(ispin), 1._dp)
601 :
602 84 : matrix_vhxc_dbasis => dcdr_env%matrix_vhxc_perturbed_basis(ispin, :)
603 : CALL integrate_v_dbasis(v_rspace=v_hxc_r(ispin), &
604 : matrix_p=matrix_p(ispin, 1)%matrix, &
605 : matrix_vhxc_dbasis=matrix_vhxc_dbasis, &
606 : qs_env=qs_env, &
607 84 : lambda=dcdr_env%lambda)
608 :
609 156 : CALL auxbas_pw_pool%give_back_pw(v_hxc_r(ispin))
610 : END DO
611 :
612 72 : DEALLOCATE (v_hxc_r)
613 :
614 72 : CALL timestop(handle)
615 72 : END SUBROUTINE vhxc_R_perturbed_basis_functions
616 :
617 : ! **************************************************************************************************
618 : !> \brief Enforce that one of the basis functions in < a | O | b > is centered on atom lambda.
619 : !> \param matrix ...
620 : !> \param qs_kind_set ...
621 : !> \param basis_type ...
622 : !> \param sab_nl ...
623 : !> \param lambda Atom index
624 : !> \param direction_Or True: < a | O | b==lambda >, False: < a==lambda | O | b >
625 : ! **************************************************************************************************
626 2610 : SUBROUTINE hr_mult_by_delta_1d(matrix, qs_kind_set, basis_type, sab_nl, lambda, direction_Or)
627 :
628 : TYPE(dbcsr_type), POINTER :: matrix
629 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
630 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
631 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
632 : POINTER :: sab_nl
633 : INTEGER, INTENT(IN) :: lambda
634 : LOGICAL, INTENT(IN) :: direction_Or
635 :
636 : CHARACTER(len=*), PARAMETER :: routineN = 'hr_mult_by_delta_1d'
637 :
638 : INTEGER :: handle, iatom, icol, ikind, irow, jatom, &
639 : jkind, mepos, nkind, nthread
640 : LOGICAL :: do_symmetric, found
641 2610 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: k_block
642 2610 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
643 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
644 : TYPE(neighbor_list_iterator_p_type), &
645 2610 : DIMENSION(:), POINTER :: nl_iterator
646 :
647 2610 : CALL timeset(routineN, handle)
648 :
649 2610 : nkind = SIZE(qs_kind_set)
650 :
651 : ! check for symmetry
652 2610 : CPASSERT(SIZE(sab_nl) > 0)
653 2610 : CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
654 :
655 : ! prepare basis set
656 13050 : ALLOCATE (basis_set_list(nkind))
657 2610 : CALL basis_set_list_setup(basis_set_list, basis_type, qs_kind_set)
658 :
659 : nthread = 1
660 2610 : !$ nthread = omp_get_max_threads()
661 : ! Iterate of neighbor list
662 2610 : CALL neighbor_list_iterator_create(nl_iterator, sab_nl, nthread=nthread)
663 :
664 : !$OMP PARALLEL DEFAULT(NONE) &
665 : !$OMP SHARED (nl_iterator, do_symmetric) &
666 : !$OMP SHARED (matrix,basis_set_list) &
667 : !$OMP SHARED (direction_or, lambda) &
668 : !$OMP PRIVATE (k_block,mepos,ikind,jkind,iatom,jatom) &
669 : !$OMP PRIVATE (basis_set_a,basis_set_b) &
670 2610 : !$OMP PRIVATE (irow, icol, found)
671 :
672 : mepos = 0
673 : !$ mepos = omp_get_thread_num()
674 :
675 : DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
676 : CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, &
677 : iatom=iatom, jatom=jatom)
678 : basis_set_a => basis_set_list(ikind)%gto_basis_set
679 : IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
680 : basis_set_b => basis_set_list(jkind)%gto_basis_set
681 : IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
682 : IF (do_symmetric) THEN
683 : IF (iatom <= jatom) THEN
684 : irow = iatom
685 : icol = jatom
686 : ELSE
687 : irow = jatom
688 : icol = iatom
689 : END IF
690 : ELSE
691 : irow = iatom
692 : icol = jatom
693 : END IF
694 :
695 : NULLIFY (k_block)
696 : CALL dbcsr_get_block_p(matrix, irow, icol, k_block, found)
697 : CPASSERT(found)
698 :
699 : IF (direction_Or) THEN
700 : IF (jatom /= lambda) k_block(:, :) = 0._dp
701 : ELSE IF (.NOT. direction_Or) THEN
702 : IF (iatom /= lambda) k_block(:, :) = 0._dp
703 : END IF
704 : END DO
705 : !$OMP END PARALLEL
706 2610 : CALL neighbor_list_iterator_release(nl_iterator)
707 :
708 : ! Release work storage
709 2610 : DEALLOCATE (basis_set_list)
710 :
711 2610 : CALL timestop(handle)
712 :
713 5220 : END SUBROUTINE hr_mult_by_delta_1d
714 :
715 : END MODULE qs_dcdr_ao
|