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 Smooth atom-image weights normalized over translated, equivalent environments.
10 : ! **************************************************************************************************
11 : MODULE skala_periodic_partition
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
19 : TYPE, PUBLIC :: skala_periodic_partition_type
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(:)
26 : END TYPE skala_periodic_partition_type
27 : PUBLIC :: build_periodic_atom_image_layout, periodic_atom_image_partition
28 : PUBLIC :: periodic_atom_image_partition_from_layout
29 : CONTAINS
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 232261 : 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 8009 : CPASSERT(ASSOCIATED(cell))
54 8009 : layout%natom = SIZE(atom_coords, 2)
55 8009 : CPASSERT(SIZE(atom_coords, 1) == 3 .AND. layout%natom > 0)
56 8009 : CPASSERT(target_atom >= 1 .AND. target_atom <= layout%natom)
57 32036 : layout%periodicity = cell%perd
58 32036 : IF (PRESENT(image_periodicity)) layout%periodicity = image_periodicity
59 32036 : CPASSERT(ALL(layout%periodicity == 0 .OR. layout%periodicity == 1))
60 104117 : layout%cell = cell%hmat
61 104117 : layout%inverse = cell%h_inv
62 24027 : ALLOCATE (layout%atoms(3, layout%natom))
63 72093 : layout%atoms(:, :) = atom_coords
64 32036 : extent = (image_shell + 1)*layout%periodicity
65 32036 : layout%nneighbor = layout%natom*PRODUCT(2*extent + 1)
66 0 : ALLOCATE (layout%offset(3, layout%nneighbor, layout%natom), &
67 0 : layout%inverse_distance(layout%nneighbor, layout%natom), &
68 0 : layout%cutoff(layout%nneighbor, layout%natom), &
69 0 : layout%cutoff_gradient(3, layout%nneighbor, layout%natom), &
70 128144 : layout%owner(layout%nneighbor, layout%natom), layout%self(layout%natom))
71 24030 : DO a = 1, layout%natom
72 16021 : n = 0
73 56081 : DO b = 1, layout%natom
74 641020 : base = NINT(MATMUL(layout%inverse, atom_coords(:, a) - atom_coords(:, b)))*layout%periodicity
75 183623 : DO k = -extent(3), extent(3)
76 752813 : DO j = -extent(2), extent(2)
77 3286241 : DO i = -extent(1), extent(1)
78 2565479 : n = n + 1
79 10261916 : shift = base + [i, j, k]
80 2565479 : layout%owner(n, a) = b
81 : layout%offset(:, n, a) = atom_coords(:, b) - atom_coords(:, a) + &
82 56440538 : MATMUL(layout%cell, REAL(shift, dp))
83 : CALL compact_window(layout, layout%offset(:, n, a), &
84 2565479 : layout%cutoff(n, a), layout%cutoff_gradient(:, n, a))
85 3903503 : IF (a == b .AND. ALL(shift == 0)) THEN
86 16021 : layout%self(a) = n
87 16021 : layout%inverse_distance(n, a) = 0.0_dp
88 : ELSE
89 10197832 : distance = NORM2(layout%offset(:, n, a))
90 2549458 : CPASSERT(distance > 1.e-10_dp)
91 2549458 : 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 8009 : target_image = target_atom
99 8009 : END SUBROUTINE build_periodic_atom_image_layout
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 380940 : SUBROUTINE periodic_atom_image_partition_from_layout(grid_point, layout, target_image, &
111 380940 : 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 761880 : q(layout%nneighbor), &
124 761880 : datom(3, layout%natom), &
125 761880 : centers(3, layout%nneighbor)
126 761880 : LOGICAL :: active(layout%nneighbor)
127 380940 : INTEGER :: owner(layout%nneighbor)
128 :
129 380940 : CPASSERT(target_image >= 1 .AND. target_image <= layout%natom)
130 380940 : CPASSERT(PRESENT(dweight_datom) .EQV. PRESENT(dweight_dstrain))
131 380940 : IF (PRESENT(dweight_datom)) THEN
132 97584 : CPASSERT(SIZE(dweight_datom, 1) == 3)
133 97584 : CPASSERT(SIZE(dweight_datom, 2) == layout%natom)
134 292752 : CPASSERT(ALL(SHAPE(dweight_dstrain) == [3, 3]))
135 878256 : dweight_datom = 0.0_dp
136 1268592 : dweight_dstrain = 0.0_dp
137 : END IF
138 : weight = 0.0_dp
139 1523760 : CALL seed_value(layout, target_image, grid_point - layout%atoms(:, target_image), weight, zero, .FALSE.)
140 380940 : IF (zero) RETURN
141 1406556 : extent = (image_shell + 1)*layout%periodicity
142 351639 : n = 0
143 351639 : TARGET = 0
144 1062234 : DO a = 1, layout%natom
145 13501305 : base = NINT(MATMUL(layout%inverse, grid_point - layout%atoms(:, a)))*layout%periodicity
146 4606473 : DO k = -extent(3), extent(3)
147 21946941 : DO j = -extent(2), extent(2)
148 109597521 : DO i = -extent(1), extent(1)
149 88361175 : n = n + 1
150 353444700 : shift = base + [i, j, k]
151 88361175 : owner(n) = a
152 1943945850 : centers(:, n) = layout%atoms(:, a) + MATMUL(layout%cell, REAL(shift, dp))
153 353444700 : CALL seed_value(layout, a, grid_point - centers(:, n), q(n), zero, .FALSE.)
154 88361175 : active(n) = .NOT. zero
155 128000223 : IF (a == target_image .AND. ALL(shift == 0)) TARGET = n
156 : END DO
157 : END DO
158 : END DO
159 : END DO
160 351639 : CPASSERT(TARGET > 0)
161 88712814 : IF (MAXVAL(q) < 1.e-200_dp) THEN
162 0 : DO n = 1, layout%nneighbor
163 0 : q(n) = -HUGE(1.0_dp)
164 0 : IF (.NOT. active(n)) CYCLE
165 0 : CALL seed_value(layout, owner(n), grid_point - centers(:, n), q(n), zero, .TRUE.)
166 : END DO
167 0 : q = EXP(q - MAXVAL(q))
168 : END IF
169 88712814 : CPASSERT(SUM(q) > 0.0_dp)
170 177073989 : q = q/SUM(q)
171 351639 : weight = q(TARGET)
172 351639 : IF (.NOT. PRESENT(dweight_datom)) RETURN
173 821385 : datom = 0.0_dp
174 91265 : moment = 0.0_dp
175 22898107 : DO n = 1, layout%nneighbor
176 22806842 : coefficient = -weight*q(n)
177 22806842 : IF (n == TARGET) coefficient = coefficient + weight
178 22806842 : IF (coefficient == 0.0_dp) CYCLE
179 22898107 : CALL seed_reverse(layout, owner(n), centers(:, n), grid_point, coefficient, datom, moment)
180 : END DO
181 821385 : dweight_datom = datom
182 1186445 : dweight_dstrain = moment
183 273795 : DO a = 1, layout%natom
184 821385 : DO j = 1, 3
185 2372890 : dweight_dstrain(:, j) = dweight_dstrain(:, j) - datom(:, a)*layout%atoms(j, a)
186 : END DO
187 : END DO
188 : END SUBROUTINE periodic_atom_image_partition_from_layout
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 7680 : SUBROUTINE periodic_atom_image_partition(grid_point, atom_coords, cell, target_atom, weight, &
202 7680 : 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 230400 : TYPE(skala_periodic_partition_type) :: layout
213 :
214 7680 : 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 23040 : dweight_datom, dweight_dstrain)
217 7680 : 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 101785664 : 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 1323213632 : f = MATMUL(layout%inverse, x)
235 407142656 : h = 1.0_dp
236 101785664 : dh = 0.0_dp
237 298509369 : DO d = 1, 3
238 246034744 : IF (layout%periodicity(d) == 0) CYCLE
239 245763440 : z = ABS(f(d)) - REAL(image_shell, dp)
240 245763440 : IF (z >= 1.0_dp) THEN
241 49311039 : value = 0.0_dp
242 49311039 : gradient = 0.0_dp
243 49311039 : RETURN
244 : END IF
245 196452401 : IF (z <= 0.0_dp) CYCLE
246 88341641 : h(d) = (1.0_dp - z)**3*(1.0_dp + 3.0_dp*z + 6.0_dp*z**2)
247 249198330 : dh(d) = -30.0_dp*z**2*(1.0_dp - z)**2*SIGN(1.0_dp, f(d))
248 : END DO
249 209898500 : value = PRODUCT(h)
250 209898500 : DO d = 1, 3
251 682170125 : DO j = 1, 3
252 629695500 : IF (j /= d) dh(d) = dh(d)*h(j)
253 : END DO
254 : END DO
255 52474625 : 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 3244961072 : 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 3244961072 : value = 0.5_dp*(1.0_dp - MAX(-1.0_dp, MIN(1.0_dp, mu)))
271 3244961072 : derivative = -0.5_dp
272 12979844288 : DO i = 1, 3
273 9734883216 : derivative = derivative*6.0_dp*value*(1.0_dp - value)
274 12979844288 : value = value**2*(3.0_dp - 2.0_dp*value)
275 : END DO
276 3244961072 : 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 88742115 : 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 88742115 : CALL compact_window(layout, x, c, dc)
299 88742115 : zero = c <= 0.0_dp
300 88742115 : q = 0.0_dp
301 89122154 : IF (zero) RETURN
302 40889328 : q = c
303 40889328 : IF (logarithmic) q = LOG(c)
304 163557312 : ri = NORM2(x)
305 10368944552 : DO k = 1, layout%nneighbor
306 10328435263 : IF (k == layout%self(atom) .OR. layout%cutoff(k, atom) == 0.0_dp) CYCLE
307 10381263968 : rj = NORM2(x - layout%offset(:, k, atom))
308 2595315992 : mu = (ri - rj)*layout%inverse_distance(k, atom)
309 2595315992 : CALL pair_switch(mu, s, ds)
310 2595315992 : s = (1.0_dp - layout%cutoff(k, atom)) + layout%cutoff(k, atom)*s
311 2595315992 : IF (s <= 0.0_dp) THEN
312 380039 : q = 0.0_dp
313 380039 : zero = .TRUE.
314 380039 : RETURN
315 : END IF
316 5230761234 : IF (logarithmic) THEN
317 0 : q = q + LOG(s)
318 : ELSE
319 2594935953 : 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 10478070 : 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 41912280 : x = point - center
346 10478070 : CALL compact_window(layout, x, c, dc)
347 10478070 : IF (c <= 0.0_dp .OR. coefficient == 0.0_dp) RETURN
348 41912280 : gi = -coefficient*dc/c
349 41912280 : datom(:, atom) = datom(:, atom) + gi
350 : ! Include the explicit inverse-cell response of the compact window.
351 41912280 : DO d = 1, 3
352 136214910 : moment(:, d) = moment(:, d) + gi*point(d)
353 : END DO
354 41912280 : ri = NORM2(x)
355 41912280 : u = x/MAX(ri, 1.e-30_dp)
356 2629842834 : DO k = 1, layout%nneighbor
357 2619364764 : IF (k == layout%self(atom) .OR. layout%cutoff(k, atom) == 0.0_dp) CYCLE
358 649645080 : b = layout%owner(k, atom)
359 2598580320 : y = x - layout%offset(:, k, atom)
360 2598580320 : rj = NORM2(y)
361 2598580320 : v = y/MAX(rj, 1.e-30_dp)
362 649645080 : mu = (ri - rj)*layout%inverse_distance(k, atom)
363 649645080 : CALL pair_switch(mu, s, ds)
364 649645080 : pair = (1.0_dp - layout%cutoff(k, atom)) + layout%cutoff(k, atom)*s
365 649645080 : IF (pair <= 0.0_dp) CYCLE
366 : ! The neighbor taper's affine cell response cancels its center moment.
367 2598580320 : gi = coefficient*(s - 1.0_dp)/pair*layout%cutoff_gradient(:, k, atom)
368 2598580320 : datom(:, atom) = datom(:, atom) - gi
369 2598580320 : datom(:, b) = datom(:, b) + gi
370 649645080 : factor = coefficient*layout%cutoff(k, atom)*ds/pair*layout%inverse_distance(k, atom)
371 2598580320 : e = -layout%offset(:, k, atom)*layout%inverse_distance(k, atom)
372 2598580320 : gi = factor*(-u - mu*e)
373 2598580320 : gj = factor*(v + mu*e)
374 2598580320 : datom(:, atom) = datom(:, atom) + gi
375 2598580320 : datom(:, b) = datom(:, b) + gj
376 2598580320 : neighbor = center + layout%offset(:, k, atom)
377 3258703470 : DO d = 1, 3
378 10415105724 : moment(:, d) = moment(:, d) + gi*center(d) + gj*neighbor(d)
379 : END DO
380 : END DO
381 : END SUBROUTINE seed_reverse
382 32051 : END MODULE skala_periodic_partition
|