14#include "../base/base_uses.f90"
18 INTEGER,
PARAMETER :: image_shell = 1
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(:)
42 REAL(
dp),
INTENT(IN) :: atom_coords(:, :)
44 INTEGER,
INTENT(IN) :: target_atom
45 INTEGER,
INTENT(IN),
OPTIONAL :: image_periodicity(3)
47 INTEGER,
INTENT(OUT) :: target_image
49 INTEGER :: a, b, base(3), extent(3), i, j, k, n, &
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
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)
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
87 layout%inverse_distance(n, a) = 0.0_dp
89 distance = norm2(layout%offset(:, n, a))
90 cpassert(distance > 1.e-10_dp)
91 layout%inverse_distance(n, a) = 1.0_dp/distance
98 target_image = target_atom
111 weight, dweight_datom, dweight_dstrain)
112 REAL(
dp),
INTENT(IN) :: grid_point(3)
114 INTEGER,
INTENT(IN) :: target_image
115 REAL(
dp),
INTENT(OUT) :: weight
116 REAL(
dp),
INTENT(OUT),
OPTIONAL :: dweight_datom(:, :), &
117 dweight_dstrain(:, :)
119 INTEGER :: a, base(3), extent(3), i, j, k, n, &
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)
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
139 CALL seed_value(layout, target_image, grid_point - layout%atoms(:, target_image), weight, zero, .false.)
141 extent = (image_shell + 1)*layout%periodicity
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)
150 shift = base + [i, j, k]
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
161 IF (maxval(q) < 1.e-200_dp)
THEN
162 DO n = 1, layout%nneighbor
164 IF (.NOT. active(n)) cycle
165 CALL seed_value(layout, owner(n), grid_point - centers(:, n), q(n), zero, .true.)
167 q = exp(q - maxval(q))
169 cpassert(sum(q) > 0.0_dp)
172 IF (.NOT.
PRESENT(dweight_datom))
RETURN
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)
181 dweight_datom = datom
182 dweight_dstrain = moment
183 DO a = 1, layout%natom
185 dweight_dstrain(:, j) = dweight_dstrain(:, j) - datom(:, a)*layout%atoms(j, a)
202 dweight_datom, dweight_dstrain, image_periodicity)
203 REAL(
dp),
INTENT(IN) :: grid_point(3), atom_coords(:, :)
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)
216 dweight_datom, dweight_dstrain)
226 PURE SUBROUTINE compact_window(layout, x, value, gradient)
228 REAL(
dp),
INTENT(IN) :: x(3)
229 REAL(
dp),
INTENT(OUT) ::
value, gradient(3)
232 REAL(
dp) :: dh(3), f(3), h(3), z
234 f = matmul(layout%inverse, x)
238 IF (layout%periodicity(d) == 0) cycle
239 z = abs(f(d)) - real(image_shell,
dp)
240 IF (z >= 1.0_dp)
THEN
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))
252 IF (j /= d) dh(d) = dh(d)*h(j)
255 gradient = matmul(transpose(layout%inverse), dh)
256 END SUBROUTINE compact_window
264 PURE SUBROUTINE pair_switch(mu, value, derivative)
265 REAL(
dp),
INTENT(IN) :: mu
266 REAL(
dp),
INTENT(OUT) ::
value, derivative
270 value = 0.5_dp*(1.0_dp - max(-1.0_dp, min(1.0_dp, mu)))
273 derivative = derivative*6.0_dp*
value*(1.0_dp -
value)
274 value =
value**2*(3.0_dp - 2.0_dp*
value)
276 END SUBROUTINE pair_switch
287 PURE SUBROUTINE seed_value(layout, atom, x, q, zero, logarithmic)
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
296 REAL(
dp) :: c, dc(3), ds, mu, ri, rj, s
298 CALL compact_window(layout, x, c, dc)
303 IF (logarithmic) q = log(c)
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
316 IF (logarithmic)
THEN
322 END SUBROUTINE seed_value
334 SUBROUTINE seed_reverse(layout, atom, center, point, coefficient, datom, moment)
336 INTEGER,
INTENT(IN) ::
atom
337 REAL(
dp),
INTENT(IN) :: center(3), point(3), coefficient
338 REAL(
dp),
INTENT(INOUT) :: datom(:, :), moment(3, 3)
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)
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
352 moment(:, d) = moment(:, d) + gi*point(d)
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)
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
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)
378 moment(:, d) = moment(:, d) + gi*center(d) + gj*neighbor(d)
381 END SUBROUTINE seed_reverse
Handles all functions related to the CELL.
Defines the basic variable types.
integer, parameter, public dp
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.