(git:27c966b)
Loading...
Searching...
No Matches
skala_periodic_partition.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 Smooth atom-image weights normalized over translated, equivalent environments.
10! **************************************************************************************************
12 USE cell_types, ONLY: cell_type
13 USE kinds, ONLY: dp
14#include "../base/base_uses.f90"
15
16 IMPLICIT NONE
17 PRIVATE
18 INTEGER, PARAMETER :: image_shell = 1
20 PRIVATE
21 INTEGER :: natom = 0, nneighbor = 0, periodicity(3) = 0
22 REAL(dp) :: cell(3, 3) = 0.0_dp, inverse(3, 3) = 0.0_dp
23 REAL(dp), ALLOCATABLE :: atoms(:, :), offset(:, :, :), inverse_distance(:, :)
24 REAL(dp), ALLOCATABLE :: cutoff(:, :), cutoff_gradient(:, :, :)
25 INTEGER, ALLOCATABLE :: owner(:, :), self(:)
29CONTAINS
30
31! **************************************************************************************************
32!> \brief Prepare geometry shared by the points of an atom block.
33!> \param atom_coords Base-atom coordinates.
34!> \param cell Simulation cell.
35!> \param target_atom Reference-cell atom whose weight will be evaluated.
36!> \param image_periodicity Optional periodic directions, including auxiliary-grid periodicity.
37!> \param layout Periodized compact Becke geometry.
38!> \param target_image Reference-atom index in the layout.
39! **************************************************************************************************
40 SUBROUTINE build_periodic_atom_image_layout(atom_coords, cell, target_atom, image_periodicity, &
41 layout, target_image)
42 REAL(dp), INTENT(IN) :: atom_coords(:, :)
43 TYPE(cell_type), POINTER :: cell
44 INTEGER, INTENT(IN) :: target_atom
45 INTEGER, INTENT(IN), OPTIONAL :: image_periodicity(3)
46 TYPE(skala_periodic_partition_type), INTENT(OUT) :: layout
47 INTEGER, INTENT(OUT) :: target_image
48
49 INTEGER :: a, b, base(3), extent(3), i, j, k, n, &
50 shift(3)
51 REAL(dp) :: distance
52
53 cpassert(ASSOCIATED(cell))
54 layout%natom = SIZE(atom_coords, 2)
55 cpassert(SIZE(atom_coords, 1) == 3 .AND. layout%natom > 0)
56 cpassert(target_atom >= 1 .AND. target_atom <= layout%natom)
57 layout%periodicity = cell%perd
58 IF (PRESENT(image_periodicity)) layout%periodicity = image_periodicity
59 cpassert(all(layout%periodicity == 0 .OR. layout%periodicity == 1))
60 layout%cell = cell%hmat
61 layout%inverse = cell%h_inv
62 ALLOCATE (layout%atoms(3, layout%natom))
63 layout%atoms(:, :) = atom_coords
64 extent = (image_shell + 1)*layout%periodicity
65 layout%nneighbor = layout%natom*product(2*extent + 1)
66 ALLOCATE (layout%offset(3, layout%nneighbor, layout%natom), &
67 layout%inverse_distance(layout%nneighbor, layout%natom), &
68 layout%cutoff(layout%nneighbor, layout%natom), &
69 layout%cutoff_gradient(3, layout%nneighbor, layout%natom), &
70 layout%owner(layout%nneighbor, layout%natom), layout%self(layout%natom))
71 DO a = 1, layout%natom
72 n = 0
73 DO b = 1, layout%natom
74 base = nint(matmul(layout%inverse, atom_coords(:, a) - atom_coords(:, b)))*layout%periodicity
75 DO k = -extent(3), extent(3)
76 DO j = -extent(2), extent(2)
77 DO i = -extent(1), extent(1)
78 n = n + 1
79 shift = base + [i, j, k]
80 layout%owner(n, a) = b
81 layout%offset(:, n, a) = atom_coords(:, b) - atom_coords(:, a) + &
82 matmul(layout%cell, real(shift, dp))
83 CALL compact_window(layout, layout%offset(:, n, a), &
84 layout%cutoff(n, a), layout%cutoff_gradient(:, n, a))
85 IF (a == b .AND. all(shift == 0)) THEN
86 layout%self(a) = n
87 layout%inverse_distance(n, a) = 0.0_dp
88 ELSE
89 distance = norm2(layout%offset(:, n, a))
90 cpassert(distance > 1.e-10_dp)
91 layout%inverse_distance(n, a) = 1.0_dp/distance
92 END IF
93 END DO
94 END DO
95 END DO
96 END DO
97 END DO
98 target_image = target_atom
100
101! **************************************************************************************************
102!> \brief Evaluate a reference-atom weight and optional base-atom and explicit image-strain responses.
103!> \param grid_point Cartesian point held fixed when taking the derivatives.
104!> \param layout Precomputed atom-image geometry.
105!> \param target_image Reference-atom index returned by the builder.
106!> \param weight Normalized reference-image weight.
107!> \param dweight_datom Optional base-atom derivative at fixed grid point and cell.
108!> \param dweight_dstrain Optional cell response at fixed grid point and base-atom coordinates.
109! **************************************************************************************************
110 SUBROUTINE periodic_atom_image_partition_from_layout(grid_point, layout, target_image, &
111 weight, dweight_datom, dweight_dstrain)
112 REAL(dp), INTENT(IN) :: grid_point(3)
113 TYPE(skala_periodic_partition_type), INTENT(IN) :: layout
114 INTEGER, INTENT(IN) :: target_image
115 REAL(dp), INTENT(OUT) :: weight
116 REAL(dp), INTENT(OUT), OPTIONAL :: dweight_datom(:, :), &
117 dweight_dstrain(:, :)
118
119 INTEGER :: a, base(3), extent(3), i, j, k, n, &
120 shift(3), target
121 LOGICAL :: zero
122 REAL(dp) :: coefficient, moment(3, 3), &
123 q(layout%nneighbor), &
124 datom(3, layout%natom), &
125 centers(3, layout%nneighbor)
126 LOGICAL :: active(layout%nneighbor)
127 INTEGER :: owner(layout%nneighbor)
128
129 cpassert(target_image >= 1 .AND. target_image <= layout%natom)
130 cpassert(PRESENT(dweight_datom) .EQV. PRESENT(dweight_dstrain))
131 IF (PRESENT(dweight_datom)) THEN
132 cpassert(SIZE(dweight_datom, 1) == 3)
133 cpassert(SIZE(dweight_datom, 2) == layout%natom)
134 cpassert(all(shape(dweight_dstrain) == [3, 3]))
135 dweight_datom = 0.0_dp
136 dweight_dstrain = 0.0_dp
137 END IF
138 weight = 0.0_dp
139 CALL seed_value(layout, target_image, grid_point - layout%atoms(:, target_image), weight, zero, .false.)
140 IF (zero) RETURN
141 extent = (image_shell + 1)*layout%periodicity
142 n = 0
143 TARGET = 0
144 DO a = 1, layout%natom
145 base = nint(matmul(layout%inverse, grid_point - layout%atoms(:, a)))*layout%periodicity
146 DO k = -extent(3), extent(3)
147 DO j = -extent(2), extent(2)
148 DO i = -extent(1), extent(1)
149 n = n + 1
150 shift = base + [i, j, k]
151 owner(n) = a
152 centers(:, n) = layout%atoms(:, a) + matmul(layout%cell, real(shift, dp))
153 CALL seed_value(layout, a, grid_point - centers(:, n), q(n), zero, .false.)
154 active(n) = .NOT. zero
155 IF (a == target_image .AND. all(shift == 0)) TARGET = n
156 END DO
157 END DO
158 END DO
159 END DO
160 cpassert(TARGET > 0)
161 IF (maxval(q) < 1.e-200_dp) THEN
162 DO n = 1, layout%nneighbor
163 q(n) = -huge(1.0_dp)
164 IF (.NOT. active(n)) cycle
165 CALL seed_value(layout, owner(n), grid_point - centers(:, n), q(n), zero, .true.)
166 END DO
167 q = exp(q - maxval(q))
168 END IF
169 cpassert(sum(q) > 0.0_dp)
170 q = q/sum(q)
171 weight = q(TARGET)
172 IF (.NOT. PRESENT(dweight_datom)) RETURN
173 datom = 0.0_dp
174 moment = 0.0_dp
175 DO n = 1, layout%nneighbor
176 coefficient = -weight*q(n)
177 IF (n == TARGET) coefficient = coefficient + weight
178 IF (coefficient == 0.0_dp) cycle
179 CALL seed_reverse(layout, owner(n), centers(:, n), grid_point, coefficient, datom, moment)
180 END DO
181 dweight_datom = datom
182 dweight_dstrain = moment
183 DO a = 1, layout%natom
184 DO j = 1, 3
185 dweight_dstrain(:, j) = dweight_dstrain(:, j) - datom(:, a)*layout%atoms(j, a)
186 END DO
187 END DO
189
190! **************************************************************************************************
191!> \brief Convenience evaluation without retaining a layout between grid points.
192!> \param grid_point Cartesian evaluation point.
193!> \param atom_coords Base-atom coordinates.
194!> \param cell Simulation cell.
195!> \param target_atom Reference-cell atom.
196!> \param weight Normalized reference-image weight.
197!> \param dweight_datom Optional base-atom derivative at fixed grid point and cell.
198!> \param dweight_dstrain Optional cell response at fixed grid point and base atoms.
199!> \param image_periodicity Optional periodic directions.
200! **************************************************************************************************
201 SUBROUTINE periodic_atom_image_partition(grid_point, atom_coords, cell, target_atom, weight, &
202 dweight_datom, dweight_dstrain, image_periodicity)
203 REAL(dp), INTENT(IN) :: grid_point(3), atom_coords(:, :)
204 TYPE(cell_type), POINTER :: cell
205 INTEGER, INTENT(IN) :: target_atom
206 REAL(dp), INTENT(OUT) :: weight
207 REAL(dp), INTENT(OUT), OPTIONAL :: dweight_datom(:, :), &
208 dweight_dstrain(:, :)
209 INTEGER, INTENT(IN), OPTIONAL :: image_periodicity(3)
210
211 INTEGER :: target
212 TYPE(skala_periodic_partition_type) :: layout
213
214 CALL build_periodic_atom_image_layout(atom_coords, cell, target_atom, image_periodicity, layout, TARGET)
215 CALL periodic_atom_image_partition_from_layout(grid_point, layout, TARGET, weight, &
216 dweight_datom, dweight_dstrain)
217 END SUBROUTINE periodic_atom_image_partition
218
219! **************************************************************************************************
220!> \brief Compact quintic window in fractional coordinates.
221!> \param layout Cell geometry and periodic directions.
222!> \param x Cartesian displacement from the window center.
223!> \param value Window value.
224!> \param gradient Cartesian displacement gradient.
225! **************************************************************************************************
226 PURE SUBROUTINE compact_window(layout, x, value, gradient)
227 TYPE(skala_periodic_partition_type), INTENT(IN) :: layout
228 REAL(dp), INTENT(IN) :: x(3)
229 REAL(dp), INTENT(OUT) :: value, gradient(3)
230
231 INTEGER :: d, j
232 REAL(dp) :: dh(3), f(3), h(3), z
233
234 f = matmul(layout%inverse, x)
235 h = 1.0_dp
236 dh = 0.0_dp
237 DO d = 1, 3
238 IF (layout%periodicity(d) == 0) cycle
239 z = abs(f(d)) - real(image_shell, dp)
240 IF (z >= 1.0_dp) THEN
241 value = 0.0_dp
242 gradient = 0.0_dp
243 RETURN
244 END IF
245 IF (z <= 0.0_dp) cycle
246 h(d) = (1.0_dp - z)**3*(1.0_dp + 3.0_dp*z + 6.0_dp*z**2)
247 dh(d) = -30.0_dp*z**2*(1.0_dp - z)**2*sign(1.0_dp, f(d))
248 END DO
249 value = product(h)
250 DO d = 1, 3
251 DO j = 1, 3
252 IF (j /= d) dh(d) = dh(d)*h(j)
253 END DO
254 END DO
255 gradient = matmul(transpose(layout%inverse), dh)
256 END SUBROUTINE compact_window
257
258! **************************************************************************************************
259!> \brief Threefold Becke polynomial and its derivative.
260!> \param mu Normalized difference of distances to the two centers.
261!> \param value Pair weight.
262!> \param derivative Derivative with respect to mu.
263! **************************************************************************************************
264 PURE SUBROUTINE pair_switch(mu, value, derivative)
265 REAL(dp), INTENT(IN) :: mu
266 REAL(dp), INTENT(OUT) :: value, derivative
267
268 INTEGER :: i
269
270 value = 0.5_dp*(1.0_dp - max(-1.0_dp, min(1.0_dp, mu)))
271 derivative = -0.5_dp
272 DO i = 1, 3
273 derivative = derivative*6.0_dp*value*(1.0_dp - value)
274 value = value**2*(3.0_dp - 2.0_dp*value)
275 END DO
276 END SUBROUTINE pair_switch
277
278! **************************************************************************************************
279!> \brief Evaluate one compact atom-image seed using its translated neighbor environment.
280!> \param layout Cached neighbor geometry.
281!> \param atom Base atom identifying the image family.
282!> \param x Evaluation point relative to the image center.
283!> \param q Seed value or logarithm.
284!> \param zero Whether the seed is identically zero at this point.
285!> \param logarithmic Return the logarithm to avoid product underflow.
286! **************************************************************************************************
287 PURE SUBROUTINE seed_value(layout, atom, x, q, zero, logarithmic)
288 TYPE(skala_periodic_partition_type), INTENT(IN) :: layout
289 INTEGER, INTENT(IN) :: atom
290 REAL(dp), INTENT(IN) :: x(3)
291 REAL(dp), INTENT(OUT) :: q
292 LOGICAL, INTENT(OUT) :: zero
293 LOGICAL, INTENT(IN) :: logarithmic
294
295 INTEGER :: k
296 REAL(dp) :: c, dc(3), ds, mu, ri, rj, s
297
298 CALL compact_window(layout, x, c, dc)
299 zero = c <= 0.0_dp
300 q = 0.0_dp
301 IF (zero) RETURN
302 q = c
303 IF (logarithmic) q = log(c)
304 ri = norm2(x)
305 DO k = 1, layout%nneighbor
306 IF (k == layout%self(atom) .OR. layout%cutoff(k, atom) == 0.0_dp) cycle
307 rj = norm2(x - layout%offset(:, k, atom))
308 mu = (ri - rj)*layout%inverse_distance(k, atom)
309 CALL pair_switch(mu, s, ds)
310 s = (1.0_dp - layout%cutoff(k, atom)) + layout%cutoff(k, atom)*s
311 IF (s <= 0.0_dp) THEN
312 q = 0.0_dp
313 zero = .true.
314 RETURN
315 END IF
316 IF (logarithmic) THEN
317 q = q + log(s)
318 ELSE
319 q = q*s
320 END IF
321 END DO
322 END SUBROUTINE seed_value
323
324! **************************************************************************************************
325!> \brief Reverse one log seed into base-atom derivatives and the affine strain moment.
326!> \param layout Cached neighbor geometry.
327!> \param atom Base atom identifying the image family.
328!> \param center Cartesian image center.
329!> \param point Cartesian evaluation point.
330!> \param coefficient Adjoint of the log seed after normalization.
331!> \param datom Accumulated base-atom derivatives.
332!> \param moment Accumulated center moment including explicit inverse-cell responses.
333! **************************************************************************************************
334 SUBROUTINE seed_reverse(layout, atom, center, point, coefficient, datom, moment)
335 TYPE(skala_periodic_partition_type), INTENT(IN) :: layout
336 INTEGER, INTENT(IN) :: atom
337 REAL(dp), INTENT(IN) :: center(3), point(3), coefficient
338 REAL(dp), INTENT(INOUT) :: datom(:, :), moment(3, 3)
339
340 INTEGER :: b, d, k
341 REAL(dp) :: c, dc(3), ds, e(3), factor, gi(3), &
342 gj(3), mu, neighbor(3), pair, ri, rj, &
343 s, u(3), v(3), x(3), y(3)
344
345 x = point - center
346 CALL compact_window(layout, x, c, dc)
347 IF (c <= 0.0_dp .OR. coefficient == 0.0_dp) RETURN
348 gi = -coefficient*dc/c
349 datom(:, atom) = datom(:, atom) + gi
350 ! Include the explicit inverse-cell response of the compact window.
351 DO d = 1, 3
352 moment(:, d) = moment(:, d) + gi*point(d)
353 END DO
354 ri = norm2(x)
355 u = x/max(ri, 1.e-30_dp)
356 DO k = 1, layout%nneighbor
357 IF (k == layout%self(atom) .OR. layout%cutoff(k, atom) == 0.0_dp) cycle
358 b = layout%owner(k, atom)
359 y = x - layout%offset(:, k, atom)
360 rj = norm2(y)
361 v = y/max(rj, 1.e-30_dp)
362 mu = (ri - rj)*layout%inverse_distance(k, atom)
363 CALL pair_switch(mu, s, ds)
364 pair = (1.0_dp - layout%cutoff(k, atom)) + layout%cutoff(k, atom)*s
365 IF (pair <= 0.0_dp) cycle
366 ! The neighbor taper's affine cell response cancels its center moment.
367 gi = coefficient*(s - 1.0_dp)/pair*layout%cutoff_gradient(:, k, atom)
368 datom(:, atom) = datom(:, atom) - gi
369 datom(:, b) = datom(:, b) + gi
370 factor = coefficient*layout%cutoff(k, atom)*ds/pair*layout%inverse_distance(k, atom)
371 e = -layout%offset(:, k, atom)*layout%inverse_distance(k, atom)
372 gi = factor*(-u - mu*e)
373 gj = factor*(v + mu*e)
374 datom(:, atom) = datom(:, atom) + gi
375 datom(:, b) = datom(:, b) + gj
376 neighbor = center + layout%offset(:, k, atom)
377 DO d = 1, 3
378 moment(:, d) = moment(:, d) + gi*center(d) + gj*neighbor(d)
379 END DO
380 END DO
381 END SUBROUTINE seed_reverse
Definition atom.F:9
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Smooth atom-image weights normalized over translated, equivalent environments.
subroutine, public build_periodic_atom_image_layout(atom_coords, cell, target_atom, image_periodicity, layout, target_image)
Prepare geometry shared by the points of an atom block.
subroutine, public periodic_atom_image_partition_from_layout(grid_point, layout, target_image, weight, dweight_datom, dweight_dstrain)
Evaluate a reference-atom weight and optional base-atom and explicit image-strain responses.
subroutine, public periodic_atom_image_partition(grid_point, atom_coords, cell, target_atom, weight, dweight_datom, dweight_dstrain, image_periodicity)
Convenience evaluation without retaining a layout between grid points.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60