(git:3d5d2e5)
Loading...
Searching...
No Matches
upf_radial_fields.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 Original scalar radial fields in the native real-harmonic projector order.
10!> Regular radial factors are splined in r**2, with Cartesian solid harmonics.
11!> An exact zero suffix supplies a C2 closure; no numerical cutoff is introduced.
12! **************************************************************************************************
14 USE ieee_arithmetic, ONLY: ieee_is_finite
15 USE kinds, ONLY: dp
16 USE mathconstants, ONLY: twopi
17 USE orbital_pointers, ONLY: indco,&
19 nco,&
20 ncoset
23 USE splines, ONLY: spline3ders
24#include "../base/base_uses.f90"
25
26 IMPLICIT NONE
27 PRIVATE
28
30 INTEGER :: nproj = 0, lmax = -1
31 LOGICAL :: finite_table = .false.
32 INTEGER, ALLOCATABLE :: radial_index(:), angular_index(:), lbeta(:), radial_end(:)
33 REAL(dp) :: radius = 0.0_dp
34 REAL(dp), ALLOCATABLE :: x(:), coefficients(:, :, :)
36
39
40CONTAINS
41
42! **************************************************************************************************
43!> \brief Cache original f_l(r)Y_lm, without weights, normalization changes or channel rotations.
44!> f_l/r**l is continued from the first positive-radius cubic to the origin.
45!> The final interval into the exact zero suffix matches value and two derivatives.
46!> Noncompact tables require an explicit boundary policy and are rejected here.
47!> \param r Original strictly increasing nonnegative radial mesh.
48!> \param radial Original f_l(r), including its full exact zero suffix.
49!> \param lbeta Angular momentum of each original radial channel.
50!> \param fields Immutable cache, initialized outside threaded regions.
51!> \param finite_table Retain a noncompact reference table. Queries beyond its last node are invalid.
52! **************************************************************************************************
53 SUBROUTINE upf_radial_fields_init(r, radial, lbeta, fields, finite_table)
54 REAL(dp), INTENT(IN) :: r(:), radial(:, :)
55 INTEGER, INTENT(IN) :: lbeta(:)
56 TYPE(upf_radial_fields_type), INTENT(OUT) :: fields
57 LOGICAL, INTENT(IN), OPTIONAL :: finite_table
58
59 INTEGER :: first, i, j, l, last, m, n, p
60 REAL(dp) :: h
61 REAL(dp), ALLOCATABLE :: slope(:), value(:), y(:)
62
63 n = SIZE(r)
64 cpassert(n >= 4 .AND. SIZE(lbeta) > 0)
65 cpassert(all(shape(radial) == [n, SIZE(lbeta)]))
66 cpassert(all(ieee_is_finite(r)) .AND. all(ieee_is_finite(radial)))
67 cpassert(all(r >= 0.0_dp) .AND. all(r(2:) > r(:n - 1)))
68 cpassert(all(lbeta >= 0) .AND. all(lbeta <= 20))
69 first = 1
70 IF (r(1) == 0.0_dp) first = 2
71 n = SIZE(r) - first + 1
72 cpassert(n >= 4)
73 fields%lmax = maxval(lbeta)
74 fields%nproj = sum(2*lbeta + 1)
75 IF (PRESENT(finite_table)) fields%finite_table = finite_table
76 IF (fields%finite_table) fields%radius = r(SIZE(r))
77 CALL init_orbital_pointers(fields%lmax)
78 CALL init_spherical_harmonics(fields%lmax, -1)
79 ALLOCATE (fields%lbeta, source=lbeta)
80 ALLOCATE (fields%x(n + 1), fields%coefficients(0:5, n, SIZE(lbeta)), &
81 fields%radial_end(SIZE(lbeta)), y(n), value(n + 1), slope(n + 1))
82 fields%x(1) = 0.0_dp
83 fields%x(2:) = r(first:)**2
84 fields%coefficients = 0.0_dp
85 fields%radial_end = 1
86 DO i = 1, SIZE(lbeta)
87 IF (all(radial(:, i) == 0.0_dp)) cycle
88 last = SIZE(r)
89 IF (fields%finite_table) THEN
90 fields%radial_end(i) = SIZE(fields%x)
91 ELSE
92 DO WHILE (radial(last, i) == 0.0_dp)
93 last = last - 1
94 END DO
95 IF (last == SIZE(r)) THEN
96 cpabort("Original radial field requires an exact zero suffix")
97 END IF
98 fields%radial_end(i) = last - first + 3
99 fields%radius = max(fields%radius, r(last + 1))
100 END IF
101 cpassert(last >= first)
102 y(:) = radial(first:, i)/r(first:)**lbeta(i)
103 cpassert(all(ieee_is_finite(y)))
104 CALL spline3ders(fields%x(2:), y, fields%x, ynew=value, dynew=slope)
105 DO j = 1, fields%radial_end(i) - 1
106 h = fields%x(j + 1) - fields%x(j)
107 associate(c => fields%coefficients(:, j, i))
108 c(1) = value(j)
109 c(2) = h*slope(j)
110 c(3) = 3.0_dp*(value(j + 1) - c(1)) - 2.0_dp*c(2) - h*slope(j + 1)
111 c(4) = 2.0_dp*(c(1) - value(j + 1)) + c(2) + h*slope(j + 1)
112 IF (j + 1 == fields%radial_end(i) .AND. .NOT. fields%finite_table) THEN
113 c(4) = -10.0_dp*c(1) - 6.0_dp*c(2) - 3.0_dp*c(3)
114 c(5) = 15.0_dp*c(1) + 8.0_dp*c(2) + 3.0_dp*c(3)
115 c(6) = -6.0_dp*c(1) - 3.0_dp*c(2) - c(3)
116 END IF
117 END associate
118 END DO
119 END DO
120 ALLOCATE (fields%radial_index(fields%nproj), fields%angular_index(fields%nproj))
121 p = 0
122 DO i = 1, SIZE(lbeta)
123 l = lbeta(i)
124 DO m = 1, 2*l + 1
125 p = p + 1
126 fields%radial_index(p) = i
127 fields%angular_index(p) = l*l + m
128 END DO
129 END DO
130 END SUBROUTINE upf_radial_fields_init
131
132! **************************************************************************************************
133!> \brief Evaluate values and Cartesian derivatives from the same radial/harmonic polynomial.
134!> \param fields Initialized original radial fields.
135!> \param displacement Point minus field center, in bohr.
136!> \param value Values in the native magnetic-projector order.
137!> \param gradient Optional Cartesian point gradients; center gradients have opposite sign.
138!> \param hessian Optional Cartesian Hessians, including origin and coordinate axes.
139! **************************************************************************************************
140 SUBROUTINE upf_radial_fields_evaluate(fields, displacement, value, gradient, hessian)
141 TYPE(upf_radial_fields_type), INTENT(IN) :: fields
142 REAL(dp), INTENT(IN) :: displacement(3)
143 REAL(dp), INTENT(OUT) :: value(:)
144 REAL(dp), INTENT(OUT), OPTIONAL :: gradient(:, :), hessian(:, :, :)
145
146 INTEGER :: a, axis, axis2, hi, i, l, lo, m, mid, p, &
147 powers(3), reduced(3)
148 REAL(dp) :: angular, da(3), dda(3, 3), factor, h, &
149 radial, rd, rdd, s, t, term
150
151 cpassert(ALLOCATED(fields%x) .AND. SIZE(value) == fields%nproj)
152 value = 0.0_dp
153 IF (PRESENT(gradient)) THEN
154 cpassert(all(shape(gradient) == [3, fields%nproj]))
155 gradient = 0.0_dp
156 END IF
157 IF (PRESENT(hessian)) THEN
158 cpassert(all(shape(hessian) == [3, 3, fields%nproj]))
159 hessian = 0.0_dp
160 END IF
161 s = sum(displacement**2)
162 IF (fields%finite_table) THEN
163 cpassert(s <= fields%radius**2)
164 ELSE IF (s >= fields%radius**2) THEN
165 RETURN
166 END IF
167 lo = 1
168 hi = SIZE(fields%x)
169 DO WHILE (hi - lo > 1)
170 mid = (lo + hi)/2
171 IF (s < fields%x(mid)) THEN
172 hi = mid
173 ELSE
174 lo = mid
175 END IF
176 END DO
177 h = fields%x(hi) - fields%x(lo)
178 t = (s - fields%x(lo))/h
179 DO p = 1, fields%nproj
180 i = fields%radial_index(p)
181 IF (lo >= fields%radial_end(i)) cycle
182 l = fields%lbeta(i)
183 m = fields%angular_index(p) - l*l
184 associate(c => fields%coefficients(:, lo, i))
185 radial = c(1) + t*(c(2) + t*(c(3) + t*(c(4) + t*(c(5) + t*c(6)))))
186 rd = (c(2) + t*(2.0_dp*c(3) + t*(3.0_dp*c(4) + t*(4.0_dp*c(5) + t*5.0_dp*c(6)))))/h
187 rdd = (2.0_dp*c(3) + t*(6.0_dp*c(4) + t*(12.0_dp*c(5) + t*20.0_dp*c(6))))/h**2
188 END associate
189 angular = 0.0_dp
190 da = 0.0_dp
191 dda = 0.0_dp
192 DO a = 1, nco(l)
193 powers = indco(:, ncoset(l - 1) + a)
194 factor = orbtramat(l)%slm(m, a)
195 angular = angular + factor*product(displacement**powers)
196 DO axis = 1, 3
197 IF (powers(axis) == 0) cycle
198 reduced = powers
199 reduced(axis) = reduced(axis) - 1
200 term = factor*real(powers(axis), dp)
201 da(axis) = da(axis) + term*product(displacement**reduced)
202 DO axis2 = 1, 3
203 IF (reduced(axis2) == 0) cycle
204 reduced(axis2) = reduced(axis2) - 1
205 dda(axis, axis2) = dda(axis, axis2) + &
206 term*real(reduced(axis2) + 1, dp)*product(displacement**reduced)
207 reduced(axis2) = reduced(axis2) + 1
208 END DO
209 END DO
210 END DO
211 value(p) = radial*angular
212 IF (PRESENT(gradient)) gradient(:, p) = 2.0_dp*displacement*rd*angular + radial*da
213 IF (PRESENT(hessian)) THEN
214 DO axis2 = 1, 3
215 DO axis = 1, 3
216 hessian(axis, axis2, p) = 4.0_dp*displacement(axis)*displacement(axis2)*rdd*angular + &
217 2.0_dp*rd*(displacement(axis)*da(axis2) + displacement(axis2)*da(axis)) + &
218 radial*dda(axis, axis2)
219 END DO
220 hessian(axis2, axis2, p) = hessian(axis2, axis2, p) + 2.0_dp*rd*angular
221 END DO
222 END IF
223 END DO
224 END SUBROUTINE upf_radial_fields_evaluate
225
226! **************************************************************************************************
227!> \brief Sum supplied Bloch images on common Cartesian quadrature points, including responses.
228!> Caller supplies complete image bounds; no minimum-image approximation is made.
229!> Cell responses use Cartesian cell columns, with reduced k fixed unless differentiated.
230!> \param fields Original radial cache.
231!> \param points Cartesian quadrature points, one per column.
232!> \param center Cartesian original center.
233!> \param cell Cartesian cell vectors, one per column.
234!> \param images Integer image vectors, one per column.
235!> \param reduced_k Reduced Bloch wavevector, in reciprocal lattice units.
236!> \param value Bloch values, point-major and native projector-minor.
237!> \param gradient Optional spatial gradients, Cartesian axis last.
238!> \param response Optional geometry/Bloch response, including image phase derivatives.
239!> \param gradient_response Optional joint spatial/geometry response, requiring Hessians.
240!> \param points_response Optional Cartesian point displacements.
241!> \param center_response Optional Cartesian center displacement.
242!> \param cell_response Optional cell-vector response.
243!> \param reduced_k_response Optional reduced Bloch-wavevector response.
244! **************************************************************************************************
245 SUBROUTINE upf_radial_fields_periodic(fields, points, center, cell, images, reduced_k, value, &
246 gradient, response, gradient_response, points_response, &
247 center_response, cell_response, reduced_k_response)
248 TYPE(upf_radial_fields_type), INTENT(IN) :: fields
249 REAL(dp), INTENT(IN) :: points(:, :), center(3), cell(3, 3)
250 INTEGER, INTENT(IN) :: images(:, :)
251 REAL(dp), INTENT(IN) :: reduced_k(3)
252 COMPLEX(dp), INTENT(OUT) :: value(:, :)
253 COMPLEX(dp), INTENT(OUT), OPTIONAL :: gradient(:, :, :), response(:, :), &
254 gradient_response(:, :, :)
255 REAL(dp), INTENT(IN), OPTIONAL :: points_response(:, :), &
256 center_response(3), &
257 cell_response(3, 3), &
258 reduced_k_response(3)
259
260 COMPLEX(dp) :: dphase, phase
261 INTEGER :: axis, image, point
262 REAL(dp) :: displacement(3), dr(3), dshift(3), &
263 shift(3), v(fields%nproj), &
264 hh(3, 3, fields%nproj), &
265 g(3, fields%nproj)
266
267 cpassert(SIZE(points, 1) == 3 .AND. SIZE(images, 1) == 3)
268 cpassert(all(shape(value) == [SIZE(points, 2), fields%nproj]))
269 value = cmplx(0.0_dp, 0.0_dp, dp)
270 IF (PRESENT(gradient)) THEN
271 cpassert(all(shape(gradient) == [SIZE(points, 2), fields%nproj, 3]))
272 gradient = cmplx(0.0_dp, 0.0_dp, dp)
273 END IF
274 IF (PRESENT(response)) THEN
275 cpassert(all(shape(response) == shape(value)))
276 response = cmplx(0.0_dp, 0.0_dp, dp)
277 END IF
278 IF (PRESENT(gradient_response)) THEN
279 IF (.NOT. (all(shape(gradient_response) == [SIZE(points, 2), fields%nproj, 3]))) THEN
280 cpabort("Invalid radial field response dimensions")
281 END IF
282 gradient_response = cmplx(0.0_dp, 0.0_dp, dp)
283 END IF
284 IF (PRESENT(points_response)) THEN
285 cpassert(all(shape(points_response) == shape(points)))
286 END IF
287 DO image = 1, SIZE(images, 2)
288 shift = center + matmul(cell, real(images(:, image), dp))
289 dshift = 0.0_dp
290 IF (PRESENT(center_response)) dshift = center_response
291 IF (PRESENT(cell_response)) dshift = dshift + matmul(cell_response, real(images(:, image), dp))
292 phase = exp(cmplx(0.0_dp, twopi*dot_product(reduced_k, real(images(:, image), dp)), dp))
293 dphase = cmplx(0.0_dp, 0.0_dp, dp)
294 IF (PRESENT(reduced_k_response)) THEN
295 dphase = phase*cmplx(0.0_dp, twopi*dot_product(reduced_k_response, real(images(:, image), dp)), dp)
296 END IF
297 DO point = 1, SIZE(points, 2)
298 displacement = points(:, point) - shift
299 dr = -dshift
300 IF (PRESENT(points_response)) dr = dr + points_response(:, point)
301 IF (sum(displacement**2) >= fields%radius**2) cycle
302 IF (PRESENT(gradient_response)) THEN
303 CALL upf_radial_fields_evaluate(fields, displacement, v, g, hh)
304 ELSE IF (PRESENT(gradient) .OR. PRESENT(response)) THEN
305 CALL upf_radial_fields_evaluate(fields, displacement, v, g)
306 ELSE
307 CALL upf_radial_fields_evaluate(fields, displacement, v)
308 END IF
309 value(point, :) = value(point, :) + phase*v
310 IF (PRESENT(response)) response(point, :) = response(point, :) + &
311 phase*matmul(dr, g) + dphase*v
312 DO axis = 1, 3
313 IF (PRESENT(gradient)) gradient(point, :, axis) = gradient(point, :, axis) + phase*g(axis, :)
314 IF (PRESENT(gradient_response)) THEN
315 gradient_response(point, :, axis) = gradient_response(point, :, axis) + &
316 phase*matmul(dr, hh(axis, :, :)) + dphase*g(axis, :)
317 END IF
318 END DO
319 END DO
320 END DO
321 END SUBROUTINE upf_radial_fields_periodic
322
323END MODULE upf_radial_fields
static GRID_HOST_DEVICE int ncoset(const int l)
Number of Cartesian orbitals up to given angular momentum quantum.
Definition grid_common.h:82
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public twopi
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
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
Original scalar radial fields in the native real-harmonic projector order. Regular radial factors are...
subroutine, public upf_radial_fields_evaluate(fields, displacement, value, gradient, hessian)
Evaluate values and Cartesian derivatives from the same radial/harmonic polynomial.
subroutine, public upf_radial_fields_init(r, radial, lbeta, fields, finite_table)
Cache original f_l(r)Y_lm, without weights, normalization changes or channel rotations....
subroutine, public upf_radial_fields_periodic(fields, points, center, cell, images, reduced_k, value, gradient, response, gradient_response, points_response, center_response, cell_response, reduced_k_response)
Sum supplied Bloch images on common Cartesian quadrature points, including responses....