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 Initial paths from an image-dependent pair potential (IDPP).
10 : ! **************************************************************************************************
11 : MODULE neb_idpp
12 : USE cell_types, ONLY: cell_type,&
13 : pbc
14 : USE kinds, ONLY: dp
15 : USE physcon, ONLY: bohr
16 : #include "../base/base_uses.f90"
17 :
18 : IMPLICIT NONE
19 : PRIVATE
20 :
21 : PUBLIC :: neb_initialize_idpp
22 :
23 : REAL(KIND=dp), PARAMETER :: soft_radius = 0.01_dp ! Angstrom
24 :
25 : CONTAINS
26 :
27 : ! **************************************************************************************************
28 : !> \brief Relax generated images on the IDPP surface with weak elastic coupling.
29 : !> Pair distances are interpolated separately between consecutive anchors.
30 : !> \param coords Cartesian coordinates in atomic units, indexed by component and image
31 : !> \param anchor true for user-supplied images, which are left unchanged
32 : !> \param cell fixed simulation cell
33 : !> \param iw initialization output unit
34 : !> \param sequential grow images from the anchors instead of using the linear guess
35 : ! **************************************************************************************************
36 4 : SUBROUTINE neb_initialize_idpp(coords, anchor, cell, iw, sequential)
37 : REAL(KIND=dp), INTENT(INOUT) :: coords(:, :)
38 : LOGICAL, INTENT(IN) :: anchor(:)
39 : TYPE(cell_type), POINTER :: cell
40 : INTEGER, INTENT(IN) :: iw
41 : LOGICAL, INTENT(IN) :: sequential
42 :
43 : INTEGER :: first, last
44 4 : REAL(KIND=dp), ALLOCATABLE :: path(:, :)
45 :
46 4 : IF (SIZE(coords, 1) <= 3) RETURN
47 4 : first = 1
48 22 : DO last = 2, SIZE(anchor)
49 18 : IF (.NOT. anchor(last)) CYCLE
50 4 : IF (last - first > 1) THEN
51 16 : ALLOCATE (path(SIZE(coords, 1), last - first + 1))
52 : ! Both IDPP methods use angstrom internally, including the reported pair distances.
53 158 : path(:, :) = coords(:, first:last)/bohr
54 4 : CALL relax_segment(path, cell, iw, sequential)
55 102 : coords(:, first + 1:last - 1) = path(:, 2:last - first)*bohr
56 4 : DEALLOCATE (path)
57 : END IF
58 18 : first = last
59 : END DO
60 4 : END SUBROUTINE neb_initialize_idpp
61 :
62 : ! **************************************************************************************************
63 : !> \brief Minimize the sum of regularized IDPP image energies and harmonic spring energies.
64 : !> \param path path in Angstrom, including its fixed endpoints
65 : !> \param cell fixed simulation cell
66 : !> \param iw initialization output unit
67 : !> \param sequential grow the segment using S-IDPP
68 : ! **************************************************************************************************
69 4 : SUBROUTINE relax_segment(path, cell, iw, sequential)
70 : REAL(KIND=dp), INTENT(INOUT) :: path(:, :)
71 : TYPE(cell_type), POINTER :: cell
72 : INTEGER, INTENT(IN) :: iw
73 : LOGICAL, INTENT(IN) :: sequential
74 :
75 : INTEGER, PARAMETER :: max_iterations = 250, max_backtrack = 25
76 : REAL(KIND=dp), PARAMETER :: spring = 0.02_dp, tolerance = 1.0E-4_dp
77 : INTEGER :: a, backtrack, b, image, it, k, ni, ns
78 : REAL(KIND=dp) :: alpha, e, e_trial, min_distance, min_initial, norm, slope
79 4 : REAL(KIND=dp), ALLOCATABLE :: anchor_distances(:, :), gradient(:, :), trial(:, :), trial_g(:, :)
80 :
81 4 : ns = SIZE(path, 1)
82 4 : ni = SIZE(path, 2)
83 16 : ALLOCATE (anchor_distances(ns/3*(ns/3 - 1)/2, 2))
84 4 : k = 0
85 8 : DO a = 1, ns - 3, 3
86 8 : DO b = a + 3, ns, 3
87 4 : k = k + 1
88 28 : anchor_distances(k, 1) = NORM2(pair_delta(path(a:a + 2, 1) - path(b:b + 2, 1)))
89 28 : anchor_distances(k, 2) = NORM2(pair_delta(path(a:a + 2, ni) - path(b:b + 2, ni)))
90 : END DO
91 : END DO
92 4 : IF (sequential) THEN
93 2 : CALL grow_segment()
94 2 : RETURN
95 : END IF
96 16 : ALLOCATE (gradient(ns, ni), trial(ns, ni), trial_g(ns, ni))
97 2 : CALL evaluate(path, e, gradient, min_initial)
98 4 : DO image = 2, ni - 1
99 4 : CALL perturb_short_contacts(path(:, image))
100 : END DO
101 2 : CALL evaluate(path, e, gradient, min_distance)
102 2 : alpha = 1.0_dp
103 106 : DO it = 1, max_iterations
104 2332 : norm = MAXVAL(ABS(gradient))
105 106 : IF (norm < tolerance) EXIT
106 104 : alpha = MIN(alpha, 0.1_dp/norm)
107 2288 : slope = SUM(gradient**2)
108 104 : DO backtrack = 1, max_backtrack
109 2288 : trial(:, :) = path - alpha*gradient
110 104 : CALL evaluate(trial, e_trial, trial_g, min_distance)
111 104 : IF (e_trial <= e - 1.0E-4_dp*alpha*slope) EXIT
112 208 : alpha = 0.5_dp*alpha
113 : END DO
114 : IF (backtrack > max_backtrack) EXIT
115 2288 : path(:, :) = trial
116 2288 : gradient(:, :) = trial_g
117 104 : e = e_trial
118 106 : alpha = MIN(1.5_dp*alpha, 20.0_dp)
119 : END DO
120 5 : IF (iw > 0) THEN
121 1 : CALL evaluate(path, e, gradient, min_distance)
122 : WRITE (iw, '(T2,A,F14.8,A,F14.8)') &
123 1 : "NEB| IDPP minimum pair distance [angstrom]: ", min_initial, " -> ", min_distance
124 22 : IF (MAXVAL(ABS(gradient)) >= tolerance) THEN
125 0 : WRITE (iw, '(T2,A)') "NEB| IDPP reached its stopping limit. Using the last relaxed images."
126 : END IF
127 : END IF
128 :
129 : CONTAINS
130 :
131 : ! **************************************************************************************************
132 : !> \brief Evaluate the auxiliary elastic-band objective and gradient.
133 : !> \param coords path at which to evaluate the objective, in Angstrom
134 : !> \param energy value of the objective
135 : !> \param grad Cartesian gradient, zero at both anchors
136 : !> \param shortest smallest unregularized pair distance in any generated image
137 : ! **************************************************************************************************
138 109 : SUBROUTINE evaluate(coords, energy, grad, shortest)
139 : REAL(KIND=dp), INTENT(IN) :: coords(:, :)
140 : REAL(KIND=dp), INTENT(OUT) :: energy, grad(:, :), shortest
141 :
142 : INTEGER :: i
143 : REAL(KIND=dp) :: fraction
144 :
145 1635 : energy = 0.5_dp*spring*SUM((coords(:, 2:ni) - coords(:, 1:ni - 1))**2)
146 2398 : grad = 0.0_dp
147 109 : shortest = HUGE(1.0_dp)
148 218 : DO i = 2, ni - 1
149 763 : grad(:, i) = spring*(2.0_dp*coords(:, i) - coords(:, i - 1) - coords(:, i + 1))
150 109 : fraction = REAL(i - 1, dp)/REAL(ni - 1, dp)
151 218 : CALL evaluate_image(coords(:, i), fraction, energy, grad(:, i), shortest)
152 : END DO
153 109 : END SUBROUTINE evaluate
154 :
155 : ! **************************************************************************************************
156 : !> \brief Accumulate the regularized pair objective and its gradient for one image.
157 : !> \param xyz image coordinates in Angstrom
158 : !> \param fraction fractional position of the target distances between the anchors
159 : !> \param energy objective accumulator
160 : !> \param grad Cartesian gradient accumulator
161 : !> \param shortest unregularized minimum pair distance accumulator
162 : ! **************************************************************************************************
163 1369 : SUBROUTINE evaluate_image(xyz, fraction, energy, grad, shortest)
164 : REAL(KIND=dp), INTENT(IN) :: xyz(:), fraction
165 : REAL(KIND=dp), INTENT(INOUT) :: energy, grad(:), shortest
166 :
167 : INTEGER :: ia, ib, pair
168 : REAL(KIND=dp) :: d, dg(3), dx(3), r, r2, weight
169 :
170 1369 : pair = 0
171 2738 : DO ia = 1, ns - 3, 3
172 4107 : DO ib = ia + 3, ns, 3
173 1369 : pair = pair + 1
174 5476 : dx = pair_delta(xyz(ia:ia + 2) - xyz(ib:ib + 2))
175 5476 : r2 = DOT_PRODUCT(dx, dx)
176 1369 : shortest = MIN(shortest, SQRT(r2))
177 1369 : r2 = r2 + soft_radius**2
178 1369 : r = SQRT(r2)
179 1369 : d = (1.0_dp - fraction)*anchor_distances(pair, 1) + fraction*anchor_distances(pair, 2)
180 1369 : d = SQRT(d*d + soft_radius**2)
181 1369 : weight = 1.0_dp/(r2*r2)
182 1369 : energy = energy + weight*(r - d)**2
183 5476 : dg = (2.0_dp*weight*(r - d)*(2.0_dp*d - r)/r2)*dx
184 5476 : grad(ia:ia + 2) = grad(ia:ia + 2) + dg
185 6845 : grad(ib:ib + 2) = grad(ib:ib + 2) - dg
186 : END DO
187 : END DO
188 1369 : END SUBROUTINE evaluate_image
189 :
190 : ! **************************************************************************************************
191 : !> \brief Break the symmetry of overlapping or collinearly exchanging pairs.
192 : !> \param xyz image coordinates in Angstrom
193 : ! **************************************************************************************************
194 14 : SUBROUTINE perturb_short_contacts(xyz)
195 : REAL(KIND=dp), INTENT(INOUT) :: xyz(:)
196 :
197 : INTEGER :: dim_min, ia, ib, pair
198 : REAL(KIND=dp) :: delta(3), direction(3), other(3), r
199 :
200 14 : pair = 0
201 28 : DO ia = 1, ns - 3, 3
202 42 : DO ib = ia + 3, ns, 3
203 14 : pair = pair + 1
204 56 : delta = pair_delta(xyz(ia:ia + 2) - xyz(ib:ib + 2))
205 56 : r = NORM2(delta)
206 14 : IF (r >= soft_radius) THEN
207 12 : IF (.NOT. sequential) CYCLE
208 36 : IF (r >= 0.5_dp*MINVAL(anchor_distances(pair, :))) CYCLE
209 : ! A collinear exchange can put the collision between images.
210 : ! The nudged force then removes the repulsion along the tangent.
211 16 : delta = pair_delta(path(ia:ia + 2, 1) - path(ib:ib + 2, 1))
212 16 : other = pair_delta(path(ia:ia + 2, ni) - path(ib:ib + 2, ni))
213 24 : IF (DOT_PRODUCT(delta, other) > -0.999_dp*PRODUCT(anchor_distances(pair, :))) CYCLE
214 : END IF
215 24 : delta = pair_delta(path(ia:ia + 2, 1) - path(ib:ib + 2, 1))
216 24 : dim_min = MINLOC(ABS(delta), 1)
217 6 : direction = 0.0_dp
218 6 : direction(dim_min) = 1.0_dp
219 24 : IF (NORM2(delta) > soft_radius) THEN
220 42 : delta = delta/NORM2(delta)
221 42 : direction = direction - DOT_PRODUCT(direction, delta)*delta
222 : END IF
223 42 : direction = 3.0_dp*soft_radius*direction/NORM2(direction)
224 24 : xyz(ia:ia + 2) = xyz(ia:ia + 2) + direction
225 46 : xyz(ib:ib + 2) = xyz(ib:ib + 2) - direction
226 : END DO
227 : END DO
228 14 : END SUBROUTINE perturb_short_contacts
229 :
230 : ! **************************************************************************************************
231 : !> \brief Grow an S-IDPP segment inward and relax it with uniform springs once full.
232 : ! **************************************************************************************************
233 2 : SUBROUTINE grow_segment()
234 : INTEGER :: front, i, left, m, right
235 : LOGICAL :: converged, hit_limit, incomplete
236 4 : REAL(KIND=dp) :: gap, shortest, spacing, step, tangent(ns)
237 2 : REAL(KIND=dp), ALLOCATABLE :: band(:, :), energies(:), fractions(:), &
238 2 : springs(:)
239 :
240 18 : ALLOCATE (band(ns, ni), energies(ni), fractions(ni), springs(ni - 1))
241 2 : IF (ni == 3) THEN
242 0 : left = 2
243 0 : right = 3
244 0 : path(:, 2) = 0.5_dp*(path(:, 1) + path(:, ni))
245 0 : CALL perturb_short_contacts(path(:, 2))
246 : ELSE
247 2 : left = 2
248 2 : right = ni - 1
249 14 : path(:, left) = path(:, 1) + (path(:, ni) - path(:, 1))/REAL(ni - 1, dp)
250 14 : path(:, right) = path(:, ni) - (path(:, ni) - path(:, 1))/REAL(ni - 1, dp)
251 2 : CALL perturb_short_contacts(path(:, left))
252 2 : CALL perturb_short_contacts(path(:, right))
253 : END IF
254 : hit_limit = .FALSE.
255 : DO
256 10 : m = left + ni - right + 1
257 10 : incomplete = m < ni
258 234 : band(:, 1:left) = path(:, 1:left)
259 206 : band(:, left + 1:m) = path(:, right:ni)
260 42 : DO i = 1, left
261 42 : fractions(i) = REAL(i - 1, dp)/REAL(ni - 1, dp)
262 : END DO
263 38 : DO i = left + 1, m
264 38 : fractions(i) = REAL(i - left + right - 2, dp)/REAL(ni - 1, dp)
265 : END DO
266 410 : spacing = SUM(NORM2(band(:, 2:m) - band(:, 1:m - 1), DIM=1))/REAL(ni - 1, dp)
267 60 : springs(1:m - 1) = 1.0_dp
268 10 : IF (incomplete) THEN
269 56 : gap = NORM2(band(:, left + 1) - band(:, left))
270 : ! Scale the spring across the unfilled interval to maintain the target tension.
271 8 : springs(left) = spacing/MAX(soft_radius, gap, spacing)
272 : END IF
273 : CALL relax_band(band(:, 1:m), fractions(1:m), springs(1:m - 1), left, incomplete, &
274 10 : energies(1:m), front, shortest, converged)
275 10 : hit_limit = hit_limit .OR. .NOT. converged
276 234 : path(:, 1:left) = band(:, 1:left)
277 206 : path(:, right:ni) = band(:, left + 1:m)
278 10 : IF (.NOT. incomplete) EXIT
279 :
280 56 : gap = NORM2(band(:, left + 1) - band(:, left))
281 296 : spacing = SUM(NORM2(band(:, 2:m) - band(:, 1:m - 1), DIM=1))/REAL(ni - 1, dp)
282 8 : step = MIN(spacing, 0.5_dp*gap)
283 : CALL band_tangent(band(:, front - 1:front + 1), energies(front - 1:front + 1), &
284 8 : tangent, weighted=.TRUE.)
285 10 : IF (front == left) THEN
286 4 : left = left + 1
287 28 : path(:, left) = band(:, front) + step*tangent
288 4 : CALL perturb_short_contacts(path(:, left))
289 : ELSE
290 4 : right = right - 1
291 28 : path(:, right) = band(:, front) - step*tangent
292 4 : CALL perturb_short_contacts(path(:, right))
293 : END IF
294 : END DO
295 2 : IF (iw > 0) THEN
296 1 : WRITE (iw, '(T2,A,I0,A,F14.8)') "NEB| S-IDPP ", ni, " images; minimum pair distance [angstrom]: ", shortest
297 1 : IF (hit_limit) WRITE (iw, '(T2,A)') "NEB| An S-IDPP stage reached its stopping limit. Using the last relaxed images."
298 : END IF
299 2 : END SUBROUTINE grow_segment
300 :
301 : ! **************************************************************************************************
302 : !> \brief Relax an active band with velocity projection on the nudged IDPP force.
303 : !> \param band active images in Angstrom
304 : !> \param fractions fractional position of each active image in the final segment
305 : !> \param springs spring constants of the active intervals
306 : !> \param left index of the growing front on the left
307 : !> \param incomplete whether the band is still growing
308 : !> \param energies objective values at the final step
309 : !> \param front active image to grow from when the band is incomplete
310 : !> \param shortest minimum pair distance among the active interior images
311 : !> \param converged whether the stage reached its force criterion
312 : ! **************************************************************************************************
313 10 : SUBROUTINE relax_band(band, fractions, springs, left, incomplete, energies, front, shortest, converged)
314 : REAL(KIND=dp), INTENT(INOUT) :: band(:, :)
315 : REAL(KIND=dp), INTENT(IN) :: fractions(:), springs(:)
316 : INTEGER, INTENT(IN) :: left
317 : LOGICAL, INTENT(IN) :: incomplete
318 : REAL(KIND=dp), INTENT(OUT) :: energies(:)
319 : INTEGER, INTENT(OUT) :: front
320 : REAL(KIND=dp), INTENT(OUT) :: shortest
321 : LOGICAL, INTENT(OUT) :: converged
322 :
323 : INTEGER, PARAMETER :: max_steps = 1200
324 : REAL(KIND=dp), PARAMETER :: max_step = 0.04_dp, max_tol = 0.01_dp, &
325 : rms_tol = 0.005_dp, timestep = 0.10_dp
326 :
327 : INTEGER :: i, it, m
328 : REAL(KIND=dp) :: backward, forward, min_image, power, &
329 20 : scale, tangent(ns)
330 10 : REAL(KIND=dp), ALLOCATABLE :: force(:, :), residual(:), velocity(:, :)
331 :
332 10 : m = SIZE(band, 2)
333 80 : ALLOCATE (force(ns, m), residual(m), velocity(ns, m))
334 10 : velocity = 0.0_dp
335 188 : DO it = 0, max_steps
336 188 : shortest = HUGE(1.0_dp)
337 188 : force = 0.0_dp
338 1448 : energies = 0.0_dp
339 1448 : DO i = 1, m
340 1260 : min_image = HUGE(1.0_dp)
341 1260 : CALL evaluate_image(band(:, i), fractions(i), energies(i), force(:, i), min_image)
342 1448 : IF (i > 1 .AND. i < m) shortest = MIN(shortest, min_image)
343 : END DO
344 188 : residual = 0.0_dp
345 1072 : DO i = 2, m - 1
346 884 : CALL band_tangent(band(:, i - 1:i + 1), energies(i - 1:i + 1), tangent, weighted=.FALSE.)
347 6188 : backward = NORM2(band(:, i) - band(:, i - 1))
348 6188 : forward = NORM2(band(:, i + 1) - band(:, i))
349 : force(:, i) = -force(:, i) + (DOT_PRODUCT(force(:, i), tangent) + &
350 11492 : springs(i)*forward - springs(i - 1)*backward)*tangent
351 11680 : residual(i) = MAX(MAXVAL(ABS(force(:, i)))/max_tol, NORM2(force(:, i))/(SQRT(REAL(ns, dp))*rms_tol))
352 : END DO
353 188 : front = left
354 188 : IF (incomplete) THEN
355 142 : IF (residual(left + 1) < residual(left)) front = left + 1
356 142 : converged = residual(front) <= 1.0_dp
357 : ELSE
358 414 : converged = MAXVAL(residual) <= 1.0_dp
359 : END IF
360 188 : IF (converged .OR. it == max_steps) EXIT
361 1032 : DO i = 2, m - 1
362 5908 : velocity(:, i) = velocity(:, i) + timestep*force(:, i)
363 5908 : power = DOT_PRODUCT(velocity(:, i), force(:, i))
364 1022 : IF (power > 0.0_dp) THEN
365 10946 : velocity(:, i) = (power/DOT_PRODUCT(force(:, i), force(:, i)))*force(:, i)
366 5894 : scale = MIN(timestep, max_step/MAX(MAXVAL(ABS(velocity(:, i))), TINY(1.0_dp)))
367 5894 : band(:, i) = band(:, i) + scale*velocity(:, i)
368 5894 : velocity(:, i) = (scale/timestep)*velocity(:, i)
369 : ELSE
370 14 : velocity(:, i) = 0.0_dp
371 : END IF
372 : END DO
373 : END DO
374 10 : END SUBROUTINE relax_band
375 :
376 : ! **************************************************************************************************
377 : !> \brief Estimate the tangent using normalized neighboring segments as in S-IDPP.
378 : !> \param band three consecutive images
379 : !> \param energies their IDPP objective values
380 : !> \param tangent normalized tangent at the middle image
381 : !> \param weighted always use both sides for seeding new images
382 : ! **************************************************************************************************
383 892 : SUBROUTINE band_tangent(band, energies, tangent, weighted)
384 : REAL(KIND=dp), INTENT(IN) :: band(:, :), energies(3)
385 : REAL(KIND=dp), INTENT(OUT) :: tangent(:)
386 : LOGICAL, INTENT(IN) :: weighted
387 :
388 1784 : REAL(KIND=dp) :: backward(ns), forward(ns), norm, wback, &
389 : wfwd
390 :
391 6244 : backward = band(:, 2) - band(:, 1)
392 6244 : forward = band(:, 3) - band(:, 2)
393 11596 : backward = backward/MAX(NORM2(backward), TINY(1.0_dp))
394 11596 : forward = forward/MAX(NORM2(forward), TINY(1.0_dp))
395 892 : IF (.NOT. weighted .AND. energies(3) > energies(2) .AND. energies(2) > energies(1)) THEN
396 1918 : tangent = forward
397 618 : ELSE IF (.NOT. weighted .AND. energies(3) < energies(2) .AND. energies(2) < energies(1)) THEN
398 2422 : tangent = backward
399 : ELSE
400 272 : wfwd = ABS(energies(3) - energies(2))
401 272 : wback = ABS(energies(2) - energies(1))
402 272 : IF (energies(3) > energies(1)) THEN
403 1372 : tangent = MAX(wfwd, wback)*forward + MIN(wfwd, wback)*backward
404 76 : ELSE IF (energies(3) < energies(1)) THEN
405 532 : tangent = MIN(wfwd, wback)*forward + MAX(wfwd, wback)*backward
406 : ELSE
407 0 : tangent = forward + backward
408 : END IF
409 : END IF
410 6244 : norm = NORM2(tangent)
411 892 : IF (norm <= TINY(1.0_dp)) THEN
412 0 : tangent = forward
413 0 : IF (NORM2(tangent) <= TINY(1.0_dp)) tangent = backward
414 : ELSE
415 6244 : tangent = tangent/norm
416 : END IF
417 892 : END SUBROUTINE band_tangent
418 :
419 : ! **************************************************************************************************
420 : !> \brief Cartesian pair displacement in Angstrom, folded for periodic directions.
421 : !> \param dx unreduced pair displacement
422 : !> \return nearest-image pair displacement
423 : ! **************************************************************************************************
424 1405 : FUNCTION pair_delta(dx) RESULT(dr)
425 : REAL(KIND=dp), INTENT(IN) :: dx(3)
426 : REAL(KIND=dp) :: dr(3)
427 :
428 5620 : dr = dx
429 11240 : IF (ANY(cell%perd /= 0)) dr = pbc(dx*bohr, cell)/bohr
430 1405 : END FUNCTION pair_delta
431 : END SUBROUTINE relax_segment
432 : END MODULE neb_idpp
|