(git:3d5d2e5)
Loading...
Searching...
No Matches
uspp_augmentation.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Direct reciprocal-space reference for scalar UPF augmentation.
10!> Uses the complete tabulated radial mesh and CP2K real spherical harmonics.
11!> The overlap correction is the zero-frequency limit of this same transform.
12! **************************************************************************************************
14 USE atom_upf, ONLY: atom_upfpot_type,&
15 upf_radial_weights
16 USE bessel_lib, ONLY: bessel0
18 USE kinds, ONLY: dp,&
19 int_8
20 USE mathconstants, ONLY: fourpi
21 USE orbital_pointers, ONLY: indco,&
23 nco,&
24 ncoset
27 USE spherical_harmonics, ONLY: y_lm
28 USE splines, ONLY: spline3ders
30#include "../base/base_uses.f90"
31
32 IMPLICIT NONE
33 PRIVATE
34 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'uspp_augmentation'
35
37 INTEGER :: nproj = 0, lmax = -1
38 INTEGER, ALLOCATABLE :: radial_index(:), angular_index(:), lbeta(:), radial_end(:, :)
39 REAL(dp) :: realspace_radius = 0.0_dp
40 ! weighted_q can omit an exact zero suffix; r retains the full mesh for real-space splines.
41 REAL(kind=dp), ALLOCATABLE :: r(:), weighted_q(:, :, :), gaunt(:, :, :), q(:, :)
42 REAL(kind=dp), ALLOCATABLE :: radial_x(:), radial_value(:, :, :), radial_slope(:, :, :)
43 INTEGER(int_8) :: fourier_budget = -1_int_8
44 INTEGER, ALLOCATABLE :: fourier_map(:)
45 REAL(dp), ALLOCATABLE :: fourier_norm(:), fourier_radial(:, :, :), fourier_next(:, :, :)
47
53
54CONTAINS
55
56! **************************************************************************************************
57!> \brief Prepare radial quadrature and Gaunt coefficients in the native beta convention.
58!> \param pot Original scalar USPP with angular-resolved augmentation.
59!> \param projectors Prepared native projectors, with Cartesian degree at least max(lbeta).
60!> \param augmentation Immutable augmentation data, in the same magnetic-projector ordering.
61! **************************************************************************************************
62 SUBROUTINE uspp_augmentation_init(pot, projectors, augmentation)
63 TYPE(atom_upfpot_type), INTENT(IN) :: pot
64 TYPE(upf_projector_type), INTENT(IN) :: projectors
65 TYPE(uspp_augmentation_type), INTENT(OUT) :: augmentation
66
67 INTEGER :: i, j, l, m, n, nc, p, pair, q, s
68 REAL(kind=dp), ALLOCATABLE :: weights(:)
69
70 cpassert(.NOT. pot%has_so .AND. .NOT. pot%is_paw)
71 cpassert(ALLOCATED(pot%qijl))
72 cpassert(projectors%degree >= maxval(pot%lbeta))
73 cpassert(all(projectors%lbeta == pot%lbeta))
74 n = SIZE(pot%r)
75 cpassert(n >= 3 .AND. SIZE(pot%rab) == n)
76 cpassert(all(pot%r >= 0.0_dp) .AND. all(pot%rab > 0.0_dp))
77 cpassert(all(pot%r(2:) > pot%r(:n - 1)))
78 nc = pot%number_of_proj*(pot%number_of_proj + 1)/2
79 augmentation%nproj = projectors%nproj
80 augmentation%lmax = 2*maxval(pot%lbeta)
81 CALL init_orbital_pointers(augmentation%lmax)
82 CALL init_spherical_harmonics(augmentation%lmax, -1)
83 cpassert(all(shape(pot%qijl) == [n, nc, augmentation%lmax + 1]))
84 cpassert(lbound(pot%qijl, 3) == 0)
85 ALLOCATE (augmentation%r(n), weights(n))
86 augmentation%r(:) = pot%r
87 weights(:) = upf_radial_weights(pot)
88 ! Keep the quadrature weights of the original mesh. Only an exact zero
89 ! suffix can be omitted; shortening the mesh before forming its weights
90 ! would change the endpoint rule and hence the augmentation metric.
91 DO WHILE (n > 0)
92 IF (any(pot%qijl(n, :, :) /= 0.0_dp)) EXIT
93 n = n - 1
94 END DO
95 ALLOCATE (augmentation%weighted_q(n, nc, 0:augmentation%lmax))
96 DO l = 0, augmentation%lmax
97 DO pair = 1, nc
98 augmentation%weighted_q(:, pair, l) = weights(:n)*pot%qijl(:n, pair, l)
99 END DO
100 END DO
101 n = augmentation%nproj
102 ALLOCATE (augmentation%radial_index(n), augmentation%angular_index(n), &
103 augmentation%lbeta(pot%number_of_proj))
104 augmentation%radial_index(:) = projectors%radial_index
105 augmentation%angular_index(:) = projectors%angular_index
106 augmentation%lbeta(:) = pot%lbeta
107 ALLOCATE (augmentation%gaunt(n, n, (augmentation%lmax + 1)**2), augmentation%q(n, n))
108 augmentation%gaunt = 0.0_dp
109 augmentation%q = 0.0_dp
110 DO q = 1, n
111 j = projectors%radial_index(q)
112 l = pot%lbeta(j)
113 m = projectors%angular_index(q) - l*l
114 DO p = 1, n
115 i = projectors%radial_index(p)
116 ! Contract the existing analytic monomial/Y/Y integrals with Y_j's polynomial.
117 DO s = 1, nco(l)
118 augmentation%gaunt(p, q, :) = augmentation%gaunt(p, q, :) + &
119 orbtramat(l)%slm(m, s)*projectors%gaunt(ncoset(l - 1) + s, &
120 projectors%angular_index(p), :(augmentation%lmax + 1)**2)
121 END DO
122 pair = max(i, j)*(max(i, j) - 1)/2 + min(i, j)
123 IF (projectors%angular_index(p) == projectors%angular_index(q)) THEN
124 augmentation%q(p, q) = sum(augmentation%weighted_q(:, pair, 0))
125 END IF
126 END DO
127 END DO
128 END SUBROUTINE uspp_augmentation_init
129
130! **************************************************************************************************
131!> \brief Prepare regular radial splines for atom-grid augmentation quadrature.
132!> UPF stores r**2 Q_ij^L(r). Interpolate Q_ij^L(r)/r**L in r**2 and multiply
133!> by solid harmonics, so values and Cartesian derivatives remain regular at the origin.
134!> The existing reciprocal-space quadrature and its overlap correction are unchanged.
135!> \param pot Original scalar USPP used to initialize augmentation.
136!> \param augmentation Prepared angular data; owns the optional real-space cache on return.
137! **************************************************************************************************
138 SUBROUTINE uspp_augmentation_prepare_realspace(pot, augmentation)
139 TYPE(atom_upfpot_type), INTENT(IN) :: pot
140 TYPE(uspp_augmentation_type), INTENT(INOUT) :: augmentation
141
142 INTEGER :: first, l, last, n, nc, pair
143 REAL(kind=dp), ALLOCATABLE :: values(:), x(:)
144
145 cpassert(ALLOCATED(pot%qijl) .AND. ALLOCATED(augmentation%r))
146 cpassert(SIZE(pot%r) == SIZE(augmentation%r))
147 cpassert(all(pot%r == augmentation%r))
148 cpassert(all(pot%lbeta == augmentation%lbeta))
149 IF (ALLOCATED(augmentation%radial_x)) RETURN
150 first = 1
151 IF (pot%r(1) == 0.0_dp) first = 2
152 n = SIZE(pot%r) - first + 1
153 cpassert(n >= 4)
154 nc = SIZE(pot%qijl, 2)
155 ALLOCATE (x(n), values(n), augmentation%radial_x(n + 1), &
156 augmentation%radial_value(n + 1, nc, 0:augmentation%lmax), &
157 augmentation%radial_slope(n + 1, nc, 0:augmentation%lmax), &
158 augmentation%radial_end(nc, 0:augmentation%lmax))
159 x(:) = pot%r(first:)**2
160 augmentation%radial_x(1) = 0.0_dp
161 augmentation%radial_x(2:) = x
162 augmentation%radial_value = 0.0_dp
163 augmentation%radial_slope = 0.0_dp
164 augmentation%radial_end = 1
165 augmentation%realspace_radius = 0.0_dp
166 DO l = 0, augmentation%lmax
167 DO pair = 1, nc
168 IF (all(pot%qijl(:, pair, l) == 0.0_dp)) cycle
169 ! Preserve the exact zero suffix of each supplied augmentation channel.
170 ! No numerical threshold is used, and a nonzero last node keeps the full mesh.
171 last = SIZE(pot%r)
172 DO WHILE (pot%qijl(last, pair, l) == 0.0_dp)
173 last = last - 1
174 END DO
175 augmentation%radial_end(pair, l) = last - first + 3
176 augmentation%realspace_radius = max(augmentation%realspace_radius, pot%r(min(last + 1, SIZE(pot%r))))
177 values(:) = pot%qijl(first:, pair, l)/pot%r(first:)**(l + 2)
178 ! The first interval continues the endpoint cubic to x=0. A tabulated
179 ! r**2 Q value at r=0 alone does not determine the regular radial limit.
180 CALL spline3ders(x, values, augmentation%radial_x, &
181 ynew=augmentation%radial_value(:, pair, l), &
182 dynew=augmentation%radial_slope(:, pair, l))
183 END DO
184 END DO
186
187! **************************************************************************************************
188!> \brief Evaluate the augmentation matrix and derivatives at a Cartesian displacement.
189!> Uses cubic splines with a C2 quintic closure into an exact tabulated zero suffix.
190!> Values, gradients and Hessians share the same polynomial. No angular division
191!> by radius is needed, including at the center and on coordinate axes.
192!> \param augmentation Data with an initialized real-space cache.
193!> \param displacement Evaluation point minus augmentation center, in bohr.
194!> \param qr Symmetric Q_ij(r), without radial Jacobian or quadrature weights.
195!> \param gradient Optional Cartesian gradient of qr with respect to the evaluation point.
196!> \param hessian Optional Cartesian Hessian of qr, also needed for GGA geometry derivatives.
197! **************************************************************************************************
198 SUBROUTINE uspp_augmentation_realspace(augmentation, displacement, qr, gradient, hessian)
199 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
200 REAL(kind=dp), INTENT(IN) :: displacement(3)
201 REAL(kind=dp), INTENT(OUT) :: qr(:, :)
202 REAL(kind=dp), INTENT(OUT), OPTIONAL :: gradient(:, :, :), hessian(:, :, :, :)
203
204 REAL(dp), ALLOCATABLE :: ddharmonics(:, :, :), dharmonics(:, :), &
205 harmonics(:)
206
207 ALLOCATE (harmonics((augmentation%lmax + 1)**2), &
208 dharmonics(3, (augmentation%lmax + 1)**2), &
209 ddharmonics(3, 3, (augmentation%lmax + 1)**2))
210 CALL realspace_with_workspace(augmentation, displacement, qr, gradient, hessian, &
211 harmonics, dharmonics, ddharmonics)
212 END SUBROUTINE uspp_augmentation_realspace
213
214! **************************************************************************************************
215!> \brief Point kernel with reusable angular scratch space for atom-grid batches.
216!> \param augmentation Initialized radial and angular data
217!> \param displacement Point relative to augmentation center
218!> \param qr Augmentation matrix
219!> \param gradient Optional Cartesian gradient
220!> \param hessian Optional Cartesian Hessian
221!> \param harmonics Scratch values of solid harmonics
222!> \param dharmonics Scratch gradients of solid harmonics
223!> \param ddharmonics Scratch Hessians of solid harmonics
224! **************************************************************************************************
225 SUBROUTINE realspace_with_workspace(augmentation, displacement, qr, gradient, hessian, &
226 harmonics, dharmonics, ddharmonics)
227 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
228 REAL(kind=dp), INTENT(IN) :: displacement(3)
229 REAL(kind=dp), INTENT(OUT) :: qr(:, :)
230 REAL(kind=dp), INTENT(OUT), OPTIONAL :: gradient(:, :, :), hessian(:, :, :, :)
231 REAL(kind=dp), INTENT(OUT) :: harmonics(:), dharmonics(:, :), &
232 ddharmonics(:, :, :)
233
234 INTEGER :: a, axis, axis2, hi, i, j, l, lm, lo, m, &
235 mid, p, pair, powers(3), q, reduced(3)
236 REAL(kind=dp) :: angular, angular_gradient(3), angular_hessian(3, 3), c0, c1, c2, c3, c4, &
237 c5, factor, h, radial, radial_d, radial_dd, s, t, term, value, value_gradient(3), &
238 value_hessian(3, 3)
239
240 cpassert(ALLOCATED(augmentation%radial_x))
241 cpassert(all(shape(qr) == augmentation%nproj))
242 qr = 0.0_dp
243 IF (PRESENT(gradient)) THEN
244 IF (.NOT. (all(shape(gradient) == [augmentation%nproj, augmentation%nproj, 3]))) THEN
245 cpabort("Invalid USPP augmentation array dimensions")
246 END IF
247 gradient = 0.0_dp
248 END IF
249 IF (PRESENT(hessian)) THEN
250 IF (.NOT. (all(shape(hessian) == [augmentation%nproj, augmentation%nproj, 3, 3]))) THEN
251 cpabort("Invalid USPP augmentation array dimensions")
252 END IF
253 hessian = 0.0_dp
254 END IF
255 s = sum(displacement**2)
256 hi = SIZE(augmentation%radial_x)
257 IF (s > augmentation%realspace_radius**2) RETURN
258 lo = 1
259 DO WHILE (hi - lo > 1)
260 mid = (lo + hi)/2
261 IF (s < augmentation%radial_x(mid)) THEN
262 hi = mid
263 ELSE
264 lo = mid
265 END IF
266 END DO
267 h = augmentation%radial_x(hi) - augmentation%radial_x(lo)
268 t = (s - augmentation%radial_x(lo))/h
269 harmonics = 0.0_dp
270 dharmonics = 0.0_dp
271 ddharmonics = 0.0_dp
272 DO l = 0, augmentation%lmax
273 DO m = -l, l
274 lm = l*l + l + m + 1
275 DO a = 1, nco(l)
276 powers = indco(:, ncoset(l - 1) + a)
277 factor = orbtramat(l)%slm(l + m + 1, a)
278 harmonics(lm) = harmonics(lm) + factor*product(displacement**powers)
279 DO axis = 1, 3
280 IF (powers(axis) == 0) cycle
281 reduced = powers
282 reduced(axis) = reduced(axis) - 1
283 term = factor*real(powers(axis), dp)
284 dharmonics(axis, lm) = dharmonics(axis, lm) + term*product(displacement**reduced)
285 IF (PRESENT(hessian)) THEN
286 DO axis2 = 1, 3
287 IF (reduced(axis2) == 0) cycle
288 reduced(axis2) = reduced(axis2) - 1
289 ddharmonics(axis, axis2, lm) = ddharmonics(axis, axis2, lm) + &
290 term*real(reduced(axis2) + 1, dp)*product(displacement**reduced)
291 reduced(axis2) = reduced(axis2) + 1
292 END DO
293 END IF
294 END DO
295 END DO
296 END DO
297 END DO
298 DO q = 1, augmentation%nproj
299 j = augmentation%radial_index(q)
300 DO p = 1, q
301 i = augmentation%radial_index(p)
302 pair = max(i, j)*(max(i, j) - 1)/2 + min(i, j)
303 DO l = abs(augmentation%lbeta(i) - augmentation%lbeta(j)), &
304 augmentation%lbeta(i) + augmentation%lbeta(j), 2
305 IF (lo >= augmentation%radial_end(pair, l)) cycle
306 c0 = augmentation%radial_value(lo, pair, l)
307 c1 = h*augmentation%radial_slope(lo, pair, l)
308 c2 = 3.0_dp*(augmentation%radial_value(hi, pair, l) - c0) - &
309 2.0_dp*c1 - h*augmentation%radial_slope(hi, pair, l)
310 c3 = 2.0_dp*(c0 - augmentation%radial_value(hi, pair, l)) + &
311 c1 + h*augmentation%radial_slope(hi, pair, l)
312 c4 = 0.0_dp
313 c5 = 0.0_dp
314 IF (hi == augmentation%radial_end(pair, l)) THEN
315 ! Match value and first/second x derivatives at the last nonzero node;
316 ! all three vanish at the first node of the exact zero suffix.
317 c3 = -10.0_dp*c0 - 6.0_dp*c1 - 3.0_dp*c2
318 c4 = 15.0_dp*c0 + 8.0_dp*c1 + 3.0_dp*c2
319 c5 = -6.0_dp*c0 - 3.0_dp*c1 - c2
320 END IF
321 radial = c0 + t*(c1 + t*(c2 + t*(c3 + t*(c4 + t*c5))))
322 radial_d = (c1 + t*(2.0_dp*c2 + t*(3.0_dp*c3 + t*(4.0_dp*c4 + t*5.0_dp*c5))))/h
323 IF (PRESENT(hessian)) radial_dd = (2.0_dp*c2 + t*(6.0_dp*c3 + t*(12.0_dp*c4 + t*20.0_dp*c5)))/h**2
324 angular = 0.0_dp
325 angular_gradient = 0.0_dp
326 angular_hessian = 0.0_dp
327 DO lm = l*l + 1, (l + 1)**2
328 factor = augmentation%gaunt(p, q, lm)
329 angular = angular + factor*harmonics(lm)
330 angular_gradient = angular_gradient + factor*dharmonics(:, lm)
331 IF (PRESENT(hessian)) angular_hessian = angular_hessian + factor*ddharmonics(:, :, lm)
332 END DO
333 value = radial*angular
334 qr(p, q) = qr(p, q) + value
335 IF (PRESENT(gradient)) THEN
336 value_gradient = 2.0_dp*displacement*radial_d*angular + radial*angular_gradient
337 gradient(p, q, :) = gradient(p, q, :) + value_gradient
338 END IF
339 IF (PRESENT(hessian)) THEN
340 DO axis2 = 1, 3
341 DO axis = 1, 3
342 value_hessian(axis, axis2) = &
343 4.0_dp*displacement(axis)*displacement(axis2)*radial_dd*angular + &
344 2.0_dp*radial_d*(displacement(axis)*angular_gradient(axis2) + &
345 displacement(axis2)*angular_gradient(axis)) + &
346 radial*angular_hessian(axis, axis2)
347 END DO
348 value_hessian(axis2, axis2) = value_hessian(axis2, axis2) + 2.0_dp*radial_d*angular
349 END DO
350 hessian(p, q, :, :) = hessian(p, q, :, :) + value_hessian
351 END IF
352 END DO
353 qr(q, p) = qr(p, q)
354 IF (PRESENT(gradient)) gradient(q, p, :) = gradient(p, q, :)
355 IF (PRESENT(hessian)) hessian(q, p, :, :) = hessian(p, q, :, :)
356 END DO
357 END DO
358 END SUBROUTINE realspace_with_workspace
359
360! **************************************************************************************************
361!> \brief Add one center's augmentation density and gradient to arbitrary atom-grid points.
362!> \param augmentation Data with an initialized real-space cache.
363!> \param displacements Cartesian quadrature points relative to this center, in bohr.
364!> \param density Real symmetric on-site density matrix, including off-diagonal entries.
365!> \param rho Accumulated density values; no quadrature weights included.
366!> \param gradient Accumulated Cartesian density gradients.
367! **************************************************************************************************
368 SUBROUTINE uspp_augmentation_collocate_atom(augmentation, displacements, density, rho, gradient)
369 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
370 REAL(kind=dp), INTENT(IN) :: displacements(:, :), density(:, :)
371 REAL(kind=dp), INTENT(INOUT) :: rho(:), gradient(:, :)
372
373 INTEGER :: axis, n, point
374 REAL(dp), ALLOCATABLE :: ddharmonics(:, :, :), dharmonics(:, :), &
375 harmonics(:)
376 REAL(kind=dp), ALLOCATABLE :: dqr(:, :, :), qr(:, :)
377
378 n = augmentation%nproj
379 cpassert(all(shape(density) == n))
380 cpassert(all(shape(displacements) == [3, SIZE(rho)]))
381 cpassert(all(shape(gradient) == shape(displacements)))
382 IF (SIZE(rho) > 0) THEN
383 cpassert(ALLOCATED(augmentation%radial_x))
384 END IF
385 ALLOCATE (qr(n, n), dqr(n, n, 3))
386 ALLOCATE (harmonics((augmentation%lmax + 1)**2), &
387 dharmonics(3, (augmentation%lmax + 1)**2), &
388 ddharmonics(3, 3, (augmentation%lmax + 1)**2))
389 DO point = 1, SIZE(rho)
390 ! The point kernel is exactly zero outside the original radial support.
391 IF (sum(displacements(:, point)**2) > augmentation%realspace_radius**2) cycle
392 CALL realspace_with_workspace(augmentation, displacements(:, point), qr, dqr, &
393 harmonics=harmonics, dharmonics=dharmonics, ddharmonics=ddharmonics)
394 rho(point) = rho(point) + sum(density*qr)
395 DO axis = 1, 3
396 gradient(axis, point) = gradient(axis, point) + sum(density*dqr(:, :, axis))
397 END DO
398 END DO
400
401! **************************************************************************************************
402!> \brief Adjoint of the atom-grid density map, including the GGA gradient term.
403!> \param augmentation Data with an initialized real-space cache.
404!> \param displacements Cartesian quadrature points relative to this center, in bohr.
405!> \param weights Quadrature weights, optionally signed for a hard/soft energy difference.
406!> \param v_rho Derivative of the energy density with respect to density, without weights.
407!> \param v_gradient Derivative with respect to the Cartesian density gradient, without weights.
408!> \param matrix Resulting symmetric on-site operator.
409!> \param density Optional on-site density, required for center_gradient.
410!> \param center_gradient Optional energy gradient from translating this center at fixed grid points.
411!> Projector, moving-grid and partition-weight derivatives are separate contributions.
412!> \param point_gradient Optional energy gradient of each quadrature point at fixed center and weights.
413! **************************************************************************************************
414 SUBROUTINE uspp_augmentation_integrate_atom(augmentation, displacements, weights, v_rho, v_gradient, &
415 matrix, density, center_gradient, point_gradient)
416 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
417 REAL(kind=dp), INTENT(IN) :: displacements(:, :), weights(:), &
418 v_rho(:), v_gradient(:, :)
419 REAL(kind=dp), INTENT(OUT) :: matrix(:, :)
420 REAL(kind=dp), INTENT(IN), OPTIONAL :: density(:, :)
421 REAL(kind=dp), INTENT(OUT), OPTIONAL :: center_gradient(3), point_gradient(:, :)
422
423 INTEGER :: axis, axis2, n, point
424 REAL(dp), ALLOCATABLE :: ddharmonics(:, :, :), dharmonics(:, :), &
425 harmonics(:)
426 REAL(kind=dp) :: derivative
427 REAL(kind=dp), ALLOCATABLE :: ddqr(:, :, :, :), dqr(:, :, :), qr(:, :)
428
429 n = augmentation%nproj
430 cpassert(all(shape(matrix) == n))
431 cpassert(all(shape(displacements) == [3, SIZE(weights)]))
432 cpassert(SIZE(v_rho) == SIZE(weights))
433 cpassert(all(shape(v_gradient) == shape(displacements)))
434 ALLOCATE (qr(n, n), dqr(n, n, 3))
435 ALLOCATE (harmonics((augmentation%lmax + 1)**2), &
436 dharmonics(3, (augmentation%lmax + 1)**2), &
437 ddharmonics(3, 3, (augmentation%lmax + 1)**2))
438 matrix = 0.0_dp
439 IF (PRESENT(center_gradient) .OR. PRESENT(point_gradient)) THEN
440 cpassert(PRESENT(density))
441 cpassert(all(shape(density) == n))
442 ALLOCATE (ddqr(n, n, 3, 3))
443 END IF
444 IF (PRESENT(center_gradient)) center_gradient = 0.0_dp
445 IF (PRESENT(point_gradient)) THEN
446 cpassert(all(shape(point_gradient) == shape(displacements)))
447 point_gradient = 0.0_dp
448 END IF
449 DO point = 1, SIZE(weights)
450 IF (weights(point) == 0.0_dp) cycle
451 IF (v_rho(point) == 0.0_dp .AND. all(v_gradient(:, point) == 0.0_dp)) cycle
452 cpassert(ALLOCATED(augmentation%radial_x))
453 IF (sum(displacements(:, point)**2) > augmentation%realspace_radius**2) cycle
454 IF (ALLOCATED(ddqr)) THEN
455 CALL realspace_with_workspace(augmentation, displacements(:, point), qr, dqr, ddqr, &
456 harmonics, dharmonics, ddharmonics)
457 ELSE
458 CALL realspace_with_workspace(augmentation, displacements(:, point), qr, dqr, &
459 harmonics=harmonics, dharmonics=dharmonics, ddharmonics=ddharmonics)
460 END IF
461 matrix(:, :) = matrix + weights(point)*v_rho(point)*qr
462 DO axis = 1, 3
463 matrix(:, :) = matrix + weights(point)*v_gradient(axis, point)*dqr(:, :, axis)
464 END DO
465 IF (ALLOCATED(ddqr)) THEN
466 DO axis = 1, 3
467 derivative = v_rho(point)*sum(density*dqr(:, :, axis))
468 DO axis2 = 1, 3
469 derivative = derivative + v_gradient(axis2, point)*sum(density*ddqr(:, :, axis2, axis))
470 END DO
471 IF (PRESENT(center_gradient)) center_gradient(axis) = center_gradient(axis) - weights(point)*derivative
472 IF (PRESENT(point_gradient)) point_gradient(axis, point) = weights(point)*derivative
473 END DO
474 END IF
475 END DO
477
478! **************************************************************************************************
479!> \brief Cache exact radial transforms for repeated reciprocal-vector lengths.
480!> The retained arrays are bounded; uncached lengths use direct quadrature.
481!> Initialization of a new augmentation object also releases this cache.
482!> \param augmentation Prepared radial data, with the cache updated in place.
483!> \param gvecs Local Cartesian reciprocal vectors, in their current ordering.
484!> \param max_bytes Optional retained-memory limit per kind and rank (default 64 MiB).
485! **************************************************************************************************
486 SUBROUTINE uspp_augmentation_prepare_fourier(augmentation, gvecs, max_bytes)
487 TYPE(uspp_augmentation_type), INTENT(INOUT) :: augmentation
488 REAL(dp), INTENT(IN) :: gvecs(:, :)
489 INTEGER(int_8), INTENT(IN), OPTIONAL :: max_bytes
490
491 INTEGER :: capacity, ig, index, lmax, nc, ng, unique
492 INTEGER(int_8) :: budget, entry_bytes, index_bytes
493 INTEGER, ALLOCATABLE :: order(:)
494 REAL(dp) :: previous
495 REAL(dp), ALLOCATABLE :: norms(:), sorted(:)
496
497 cpassert(SIZE(gvecs, 1) == 3)
498 budget = 64_int_8*1024*1024
499 IF (PRESENT(max_bytes)) budget = max_bytes
500 cpassert(budget >= 0_int_8)
501 ng = SIZE(gvecs, 2)
502 nc = SIZE(augmentation%weighted_q, 2)
503 lmax = augmentation%lmax
504 cpassert(nc > 0 .AND. lmax >= 0)
505 index_bytes = int(ng, int_8)*(storage_size(0.0_dp) + storage_size(0))/8
506 entry_bytes = 2_int_8*nc*(lmax + 1)*storage_size(0.0_dp)/8
507 capacity = int(min(int(ng, int_8), max(0_int_8, (budget - index_bytes)/entry_bytes)))
508 IF (capacity > 0) THEN
509 ALLOCATE (norms(ng))
510 DO ig = 1, ng
511 norms(ig) = sqrt(sum(gvecs(:, ig)*gvecs(:, ig)))
512 END DO
513 IF (ALLOCATED(augmentation%fourier_norm) .AND. augmentation%fourier_budget == budget) THEN
514 IF (SIZE(augmentation%fourier_norm) == ng) THEN
515 IF (all(augmentation%fourier_norm == norms)) RETURN
516 END IF
517 END IF
518 END IF
519 IF (ALLOCATED(augmentation%fourier_norm)) THEN
520 DEALLOCATE (augmentation%fourier_norm, augmentation%fourier_map, &
521 augmentation%fourier_radial, augmentation%fourier_next)
522 END IF
523 augmentation%fourier_budget = budget
524 IF (capacity == 0) RETURN
525 ALLOCATE (augmentation%fourier_norm, source=norms)
526 ALLOCATE (augmentation%fourier_map(ng), order(ng), sorted(ng))
527 augmentation%fourier_map = 0
528 sorted(:) = norms
529 CALL cp_1d_r_sort(sorted, ng, order)
530 unique = 0
531 previous = -1.0_dp
532 DO ig = 1, ng
533 IF (sorted(ig) == 0.0_dp) cycle
534 IF (sorted(ig) /= previous) unique = unique + 1
535 previous = sorted(ig)
536 END DO
537 capacity = min(capacity, unique)
538 ALLOCATE (augmentation%fourier_radial(nc, 0:lmax, capacity), &
539 augmentation%fourier_next(nc, 0:lmax, capacity))
540 index = 0
541 previous = -1.0_dp
542 DO ig = 1, ng
543 IF (sorted(ig) == 0.0_dp) cycle
544 IF (sorted(ig) /= previous) THEN
545 IF (index == capacity) EXIT
546 index = index + 1
547 previous = sorted(ig)
548 CALL augmentation_radial(augmentation%r, augmentation%weighted_q, previous, &
549 augmentation%fourier_radial(:, :, index), augmentation%fourier_next(:, :, index))
550 END IF
551 augmentation%fourier_map(order(ig)) = index
552 END DO
554
555! **************************************************************************************************
556!> \brief Direct radial quadrature shared by cached and uncached Fourier evaluations.
557!> \param r Original radial mesh.
558!> \param weighted_q Weighted radial augmentation.
559!> \param norm_g Reciprocal-vector length.
560!> \param radial Integrals against j_l(Gr).
561!> \param radial_next Optional integrals against r*j_(l+1)(Gr), for strain derivatives.
562! **************************************************************************************************
563 SUBROUTINE augmentation_radial(r, weighted_q, norm_g, radial, radial_next)
564 REAL(dp), INTENT(IN) :: r(:), weighted_q(:, :, 0:), norm_g
565 REAL(dp), INTENT(OUT) :: radial(:, 0:)
566 REAL(dp), INTENT(OUT), OPTIONAL :: radial_next(:, 0:)
567
568 INTEGER :: l, nr
569 REAL(dp), ALLOCATABLE :: work(:)
570
571 nr = SIZE(weighted_q, 1)
572 ALLOCATE (work(nr))
573 DO l = 0, ubound(weighted_q, 3)
574 IF (PRESENT(radial_next)) THEN
575 work(:) = r(:nr)*bessel0(norm_g*r(:nr), l + 1)
576 radial_next(:, l) = matmul(work, weighted_q(:, :, l))
577 END IF
578 work(:) = bessel0(norm_g*r(:nr), l)
579 radial(:, l) = matmul(work, weighted_q(:, :, l))
580 END DO
581 END SUBROUTINE augmentation_radial
582
583! **************************************************************************************************
584!> \brief Evaluate Q_ij(G) = integral Q_ij(r-R) exp(-i G.r) dr / volume.
585!> Direct radial quadrature is a reference without reciprocal interpolation.
586!> \param augmentation Prepared data.
587!> \param g Cartesian reciprocal vector in inverse bohr.
588!> \param position Augmentation center in bohr.
589!> \param volume Cell volume in bohr cubed.
590!> \param qg Complex symmetric form-factor matrix. Q(-G) = conjugate(Q(G)).
591!> \param strain Optional dQ/d epsilon_ab at fixed fractional center and reciprocal index.
592!> Includes the inverse-volume derivative; the center phase is strain invariant.
593!> \param grid_index Optional index in the prepared Fourier cache; absent entries use direct quadrature.
594! **************************************************************************************************
595 SUBROUTINE uspp_augmentation_fourier(augmentation, g, position, volume, qg, strain, grid_index)
596 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
597 REAL(kind=dp), INTENT(IN) :: g(3), position(3), volume
598 COMPLEX(KIND=dp), INTENT(OUT) :: qg(:, :)
599 COMPLEX(KIND=dp), INTENT(OUT), OPTIONAL :: strain(:, :, :, :)
600 INTEGER, INTENT(IN), OPTIONAL :: grid_index
601
602 COMPLEX(KIND=dp) :: phase, phase_l
603 INTEGER :: a, axis, cached, i, j, l, lm, m, p, &
604 pair, powers(3), q
605 REAL(kind=dp) :: angular, angular_derivative(3), &
606 direction(3, 1), monomial, norm_g, &
607 value(1)
608 REAL(kind=dp), ALLOCATABLE :: harmonics(:), radial(:, :), &
609 radial_next(:, :), solid_gradient(:, :)
610
611 cpassert(volume > 0.0_dp)
612 cpassert(all(shape(qg) == augmentation%nproj))
613 IF (PRESENT(strain)) THEN
614 IF (.NOT. (all(shape(strain) == [augmentation%nproj, augmentation%nproj, 3, 3]))) THEN
615 cpabort("Invalid USPP augmentation array dimensions")
616 END IF
617 strain = cmplx(0.0_dp, 0.0_dp, dp)
618 END IF
619 norm_g = sqrt(sum(g*g))
620 IF (norm_g == 0.0_dp) THEN
621 qg(:, :) = cmplx(augmentation%q/volume, 0.0_dp, kind=dp)
622 IF (PRESENT(strain)) THEN
623 DO axis = 1, 3
624 strain(:, :, axis, axis) = -qg
625 END DO
626 END IF
627 RETURN
628 END IF
629 direction(:, 1) = g/norm_g
630 ALLOCATE (harmonics((augmentation%lmax + 1)**2))
631 ALLOCATE (radial(SIZE(augmentation%weighted_q, 2), 0:augmentation%lmax))
632 IF (PRESENT(strain)) THEN
633 ALLOCATE (solid_gradient(3, (augmentation%lmax + 1)**2), &
634 radial_next(SIZE(radial, 1), 0:augmentation%lmax))
635 solid_gradient = 0.0_dp
636 END IF
637 cached = 0
638 IF (PRESENT(grid_index) .AND. ALLOCATED(augmentation%fourier_map)) THEN
639 ! Independent callers can supply a changed or reordered list without preparing it.
640 ! Reuse only a matching length; the direct path remains valid for every other vector.
641 IF (grid_index >= 1 .AND. grid_index <= SIZE(augmentation%fourier_map)) THEN
642 IF (augmentation%fourier_norm(grid_index) == norm_g) cached = augmentation%fourier_map(grid_index)
643 END IF
644 END IF
645 IF (cached > 0) THEN
646 radial(:, :) = augmentation%fourier_radial(:, :, cached)
647 IF (PRESENT(strain)) radial_next(:, :) = augmentation%fourier_next(:, :, cached)
648 ELSE
649 CALL augmentation_radial(augmentation%r, augmentation%weighted_q, norm_g, radial, radial_next)
650 END IF
651 DO l = 0, augmentation%lmax
652 DO m = -l, l
653 lm = l*l + l + m + 1
654 CALL y_lm(direction, value, l, m)
655 harmonics(lm) = value(1)
656 IF (PRESENT(strain)) THEN
657 ! Cartesian solid-harmonic derivatives avoid polar-axis singularities.
658 DO a = 1, nco(l)
659 DO axis = 1, 3
660 powers = indco(:, ncoset(l - 1) + a)
661 IF (powers(axis) == 0) cycle
662 monomial = real(powers(axis), dp)
663 powers(axis) = powers(axis) - 1
664 solid_gradient(axis, lm) = solid_gradient(axis, lm) + &
665 orbtramat(l)%slm(l + m + 1, a)*monomial*product(direction(:, 1)**powers)
666 END DO
667 END DO
668 END IF
669 END DO
670 END DO
671 phase = exp(cmplx(0.0_dp, -dot_product(g, position), kind=dp))*fourpi/volume
672 qg = cmplx(0.0_dp, 0.0_dp, kind=dp)
673 DO q = 1, augmentation%nproj
674 j = augmentation%radial_index(q)
675 DO p = 1, q
676 i = augmentation%radial_index(p)
677 pair = max(i, j)*(max(i, j) - 1)/2 + min(i, j)
678 DO l = abs(augmentation%lbeta(i) - augmentation%lbeta(j)), &
679 augmentation%lbeta(i) + augmentation%lbeta(j), 2
680 angular = dot_product(augmentation%gaunt(p, q, l*l + 1:(l + 1)**2), &
681 harmonics(l*l + 1:(l + 1)**2))
682 phase_l = cmplx(0.0_dp, -1.0_dp, kind=dp)**l
683 qg(p, q) = qg(p, q) + phase_l*angular*radial(pair, l)
684 IF (PRESENT(strain)) THEN
685 angular_derivative = matmul(solid_gradient(:, l*l + 1:(l + 1)**2), &
686 augmentation%gaunt(p, q, l*l + 1:(l + 1)**2))
687 ! d[j_l(Gr)Y_l(G)]/dG combines the l/G terms analytically.
688 angular_derivative = radial(pair, l)/norm_g*angular_derivative &
689 - radial_next(pair, l)*angular*direction(:, 1)
690 DO axis = 1, 3
691 strain(p, q, :, axis) = strain(p, q, :, axis) - phase*phase_l*g*angular_derivative(axis)
692 END DO
693 END IF
694 END DO
695 qg(p, q) = phase*qg(p, q)
696 qg(q, p) = qg(p, q)
697 IF (PRESENT(strain)) THEN
698 DO axis = 1, 3
699 strain(p, q, axis, axis) = strain(p, q, axis, axis) - qg(p, q)
700 END DO
701 strain(q, p, :, :) = strain(p, q, :, :)
702 END IF
703 END DO
704 END DO
705 END SUBROUTINE uspp_augmentation_fourier
706
707! **************************************************************************************************
708!> \brief Add the augmentation density on a list of reciprocal vectors.
709!> \param augmentation Prepared data.
710!> \param gvecs Cartesian reciprocal vectors (3,ng).
711!> \param position Center in bohr.
712!> \param volume Cell volume.
713!> \param density_matrix Real symmetric projector density matrix, with occupations included.
714!> \param rho Fourier density coefficients (integral divided by volume), accumulated in place.
715! **************************************************************************************************
716 SUBROUTINE uspp_augmentation_collocate(augmentation, gvecs, position, volume, density_matrix, rho)
717 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
718 REAL(kind=dp), INTENT(IN) :: gvecs(:, :), position(3), volume, &
719 density_matrix(:, :)
720 COMPLEX(KIND=dp), INTENT(INOUT) :: rho(:)
721
722 COMPLEX(KIND=dp), ALLOCATABLE :: qg(:, :)
723 INTEGER :: ig
724
725 cpassert(SIZE(gvecs, 1) == 3 .AND. SIZE(gvecs, 2) == SIZE(rho))
726 cpassert(all(shape(density_matrix) == augmentation%nproj))
727 ALLOCATE (qg(augmentation%nproj, augmentation%nproj))
728 DO ig = 1, SIZE(rho)
729 CALL uspp_augmentation_fourier(augmentation, gvecs(:, ig), position, volume, qg, grid_index=ig)
730 rho(ig) = rho(ig) + sum(density_matrix*qg)
731 END DO
732 END SUBROUTINE uspp_augmentation_collocate
733
734! **************************************************************************************************
735!> \brief Adjoint of augmentation collocation: integrate a real local potential against Q.
736!> \param augmentation Prepared data.
737!> \param gvecs Cartesian reciprocal vectors (3,ng).
738!> \param position Center in bohr.
739!> \param volume Cell volume.
740!> \param vg Fourier potential coefficients (integral divided by volume).
741!> \param multiplicity One for a complete reciprocal list; two for a retained +/-G pair.
742!> \param matrix Real symmetric screened projector contribution, overwritten.
743!> \param derivative Optional derivatives of matrix with respect to the augmentation center.
744!> \param strain Optional density strain contribution at fixed Fourier potential and volume weight.
745! **************************************************************************************************
746 SUBROUTINE uspp_augmentation_integrate(augmentation, gvecs, position, volume, vg, multiplicity, matrix, derivative, strain)
747 TYPE(uspp_augmentation_type), INTENT(IN) :: augmentation
748 REAL(kind=dp), INTENT(IN) :: gvecs(:, :), position(3), volume
749 COMPLEX(KIND=dp), INTENT(IN) :: vg(:)
750 REAL(kind=dp), INTENT(IN) :: multiplicity(:)
751 REAL(kind=dp), INTENT(OUT) :: matrix(:, :)
752 REAL(kind=dp), INTENT(OUT), OPTIONAL :: derivative(:, :, :), strain(:, :, :, :)
753
754 COMPLEX(KIND=dp), ALLOCATABLE :: dq(:, :, :, :), qg(:, :)
755 INTEGER :: idir, ig, jdir
756
757 cpassert(SIZE(gvecs, 1) == 3 .AND. SIZE(gvecs, 2) == SIZE(vg))
758 cpassert(SIZE(multiplicity) == SIZE(vg))
759 cpassert(all(shape(matrix) == augmentation%nproj))
760 ALLOCATE (qg(augmentation%nproj, augmentation%nproj))
761 matrix = 0.0_dp
762 IF (PRESENT(derivative)) THEN
763 IF (.NOT. (all(shape(derivative) == [augmentation%nproj, augmentation%nproj, 3]))) THEN
764 cpabort("Invalid USPP augmentation array dimensions")
765 END IF
766 derivative = 0.0_dp
767 END IF
768 IF (PRESENT(strain)) THEN
769 IF (.NOT. (all(shape(strain) == [augmentation%nproj, augmentation%nproj, 3, 3]))) THEN
770 cpabort("Invalid USPP augmentation array dimensions")
771 END IF
772 ALLOCATE (dq(augmentation%nproj, augmentation%nproj, 3, 3))
773 strain = 0.0_dp
774 END IF
775 DO ig = 1, SIZE(vg)
776 CALL uspp_augmentation_fourier(augmentation, gvecs(:, ig), position, volume, qg, dq, grid_index=ig)
777 IF (PRESENT(strain)) THEN
778 DO jdir = 1, 3
779 DO idir = 1, 3
780 strain(:, :, idir, jdir) = strain(:, :, idir, jdir) + volume*multiplicity(ig)* &
781 REAL(conjg(dq(:, :, idir, jdir))*vg(ig), kind=dp)
782 END DO
783 END DO
784 END IF
785 matrix(:, :) = matrix + volume*multiplicity(ig)*real(conjg(qg)*vg(ig), kind=dp)
786 IF (PRESENT(derivative)) THEN
787 DO idir = 1, 3
788 derivative(:, :, idir) = derivative(:, :, idir) + volume*multiplicity(ig)* &
789 REAL(cmplx(0.0_dp, gvecs(idir, ig), dp)*conjg(qg)*vg(ig), kind=dp)
790 END DO
791 END IF
792 END DO
793 END SUBROUTINE uspp_augmentation_integrate
794
795END MODULE uspp_augmentation
Routines that process Quantum Espresso UPF files.
Definition atom_upf.F:14
Calculates Bessel functions.
Definition bessel_lib.F:16
elemental impure real(kind=dp) function, public bessel0(x, l)
...
Definition bessel_lib.F:161
Routine for sorting an array.
subroutine, public cp_1d_r_sort(arr, n, indices)
Sorts an array inplace using a combination of merge- and bubble-sort. It also returns the indices,...
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public fourpi
Provides Cartesian and spherical orbital pointers and indices.
subroutine, public init_orbital_pointers(maxl)
Initialize or update the orbital pointers.
integer, dimension(:), allocatable, public nco
integer, dimension(:), allocatable, public ncoset
integer, dimension(:, :), allocatable, public indco
Calculation of the spherical harmonics and the corresponding orbital transformation matrices.
subroutine, public init_spherical_harmonics(maxl, output_unit)
Initialize or update the orbital transformation matrices.
type(orbtramat_type), dimension(:), pointer, public orbtramat
Calculate spherical harmonics.
Simple splines Splines are fully specified by the interpolation points, except that at the ends,...
Definition splines.F:28
subroutine, public spline3ders(x, y, xnew, ynew, dynew, d2ynew)
...
Definition splines.F:81
Half-numerical overlaps with the original scalar UPF radial projectors. Cartesian polynomials and ang...
Direct reciprocal-space reference for scalar UPF augmentation. Uses the complete tabulated radial mes...
subroutine, public uspp_augmentation_collocate_atom(augmentation, displacements, density, rho, gradient)
Add one center's augmentation density and gradient to arbitrary atom-grid points.
subroutine, public uspp_augmentation_collocate(augmentation, gvecs, position, volume, density_matrix, rho)
Add the augmentation density on a list of reciprocal vectors.
subroutine, public uspp_augmentation_integrate(augmentation, gvecs, position, volume, vg, multiplicity, matrix, derivative, strain)
Adjoint of augmentation collocation: integrate a real local potential against Q.
subroutine, public uspp_augmentation_prepare_fourier(augmentation, gvecs, max_bytes)
Cache exact radial transforms for repeated reciprocal-vector lengths. The retained arrays are bounded...
subroutine, public uspp_augmentation_prepare_realspace(pot, augmentation)
Prepare regular radial splines for atom-grid augmentation quadrature. UPF stores r**2 Q_ij^L(r)....
subroutine, public uspp_augmentation_realspace(augmentation, displacement, qr, gradient, hessian)
Evaluate the augmentation matrix and derivatives at a Cartesian displacement. Uses cubic splines with...
subroutine, public uspp_augmentation_init(pot, projectors, augmentation)
Prepare radial quadrature and Gaunt coefficients in the native beta convention.
subroutine, public uspp_augmentation_fourier(augmentation, g, position, volume, qg, strain, grid_index)
Evaluate Q_ij(G) = integral Q_ij(r-R) exp(-i G.r) dr / volume. Direct radial quadrature is a referenc...
subroutine, public uspp_augmentation_integrate_atom(augmentation, displacements, weights, v_rho, v_gradient, matrix, density, center_gradient, point_gradient)
Adjoint of the atom-grid density map, including the GGA gradient term.