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 Linked-cell neighbour finder with a Verlet skin for the NNP descriptor.
10 : !> Owns the per-nnp cell-list cache (positions, image pool, head/next
11 : !> chain, bin geometry), rebuilds or rebins it as atoms move, and walks
12 : !> it to fill the per-element neighbour buffers. Each &NNP force_eval
13 : !> owns its cache, so the lifetime tracks nnp_env_release.
14 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
15 : !> \author Christoph Schran (christoph.schran@rub.de)
16 : !> \date 2026-05-21
17 : ! **************************************************************************************************
18 : MODULE nnp_cell_list
19 :
20 : USE cell_types, ONLY: cell_type,&
21 : pbc,&
22 : real_to_scaled
23 : USE kinds, ONLY: dp
24 : USE nnp_environment_types, ONLY: nnp_cell_list_cache_type,&
25 : nnp_env_get,&
26 : nnp_neighbor_type,&
27 : nnp_type
28 : USE nnp_neighbor_interface, ONLY: nnp_neigh_grp_grow
29 : #include "./base/base_uses.f90"
30 :
31 : IMPLICIT NONE
32 :
33 : PRIVATE
34 :
35 : ! Verlet skin: defaults to MIN(0.5 bohr, 0.1*cutoff), or the VERLET_SKIN
36 : ! keyword value (nnp%verlet_skin). The chain rebuilds once an atom drifts
37 : ! more than skin/2 from its rebuild-time position.
38 : REAL(KIND=dp), PRIVATE, PARAMETER :: default_verlet_skin = 0.5_dp
39 : REAL(KIND=dp), PRIVATE, PARAMETER :: rebuild_eps = 1.0E-12_dp
40 :
41 : PUBLIC :: nnp_prepare_cell_list_cache, &
42 : nnp_compute_neighbors_cell_list
43 :
44 : CONTAINS
45 :
46 : ! **************************************************************************************************
47 : !> \brief Prepare the cell-list cache for the current force evaluation: wrap
48 : !> positions into the primary cell, then either rebuild the bin geometry
49 : !> (cell, atom-count, cutoff or large-drift change) or rebin the chain in
50 : !> place while the Verlet skin still holds. Call once per force evaluation,
51 : !> before any nnp_compute_neighbors_cell_list call.
52 : !> \param nnp NNP environment whose cell_list_cache is prepared from current positions.
53 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
54 : ! **************************************************************************************************
55 55599 : SUBROUTINE nnp_prepare_cell_list_cache(nnp)
56 :
57 : TYPE(nnp_type), INTENT(INOUT), POINTER :: nnp
58 :
59 : INTEGER :: handle, i
60 : LOGICAL :: rebuild
61 : REAL(KIND=dp) :: dr2_max, exact_cutoff, list_cutoff, skin
62 : REAL(KIND=dp), DIMENSION(3) :: dr
63 : TYPE(cell_type), POINTER :: cell
64 :
65 55599 : CALL timeset('nnp_acsf_cell_list_prepare', handle)
66 :
67 55599 : NULLIFY (cell)
68 55599 : CALL nnp_env_get(nnp_env=nnp, cell=cell)
69 :
70 55599 : exact_cutoff = nnp%max_cut
71 55599 : IF (exact_cutoff > 0.0_dp) THEN
72 55599 : IF (nnp%verlet_skin >= 0.0_dp) THEN
73 : ! VERLET_SKIN keyword value (bohr).
74 0 : skin = nnp%verlet_skin
75 : ELSE
76 : ! Auto heuristic: cap at the default, scale down for small cutoffs.
77 55599 : skin = MIN(default_verlet_skin, 0.1_dp*exact_cutoff)
78 : END IF
79 : ELSE
80 0 : skin = 0.0_dp
81 : END IF
82 55599 : list_cutoff = exact_cutoff + skin
83 :
84 55599 : CALL nnp_cell_list_ensure_coord_buffers(nnp%cell_list_cache, nnp%num_atoms)
85 :
86 : ASSOCIATE (cache => nnp%cell_list_cache)
87 360961 : DO i = 1, nnp%num_atoms
88 1221448 : cache%coord_primary(:, i) = pbc(nnp%coord(:, i), cell, .TRUE.)
89 : CALL real_to_scaled(cache%coord_scaled(:, i), &
90 360961 : cache%coord_primary(:, i), cell)
91 : END DO
92 :
93 55599 : rebuild = .NOT. cache%initialized
94 55599 : IF (.NOT. rebuild) rebuild = (cache%num_atoms /= nnp%num_atoms)
95 55599 : IF (.NOT. rebuild) rebuild = (ABS(cache%exact_cutoff - exact_cutoff) > rebuild_eps)
96 55569 : IF (.NOT. rebuild) rebuild = (ABS(cache%list_cutoff - list_cutoff) > rebuild_eps)
97 55599 : IF (.NOT. rebuild) rebuild = (ABS(cache%verlet_skin - skin) > rebuild_eps)
98 222276 : IF (.NOT. rebuild) rebuild = ANY(cache%perd /= cell%perd)
99 55569 : IF (.NOT. rebuild) rebuild = (cache%orthorhombic .NEQV. cell%orthorhombic)
100 776029 : IF (.NOT. rebuild) rebuild = ANY(ABS(cache%hmat - cell%hmat) > rebuild_eps)
101 775153 : IF (.NOT. rebuild) rebuild = ANY(ABS(cache%h_inv - cell%h_inv) > rebuild_eps)
102 :
103 55353 : IF ((.NOT. rebuild) .AND. (skin > 0.0_dp)) THEN
104 55353 : dr2_max = 0.0_dp
105 312913 : DO i = 1, nnp%num_atoms
106 1030240 : dr(:) = cache%coord_primary(:, i) - cache%ref_coord_primary(:, i)
107 1030240 : dr(:) = pbc(dr, cell)
108 1085593 : dr2_max = MAX(dr2_max, DOT_PRODUCT(dr, dr))
109 : END DO
110 55353 : IF (dr2_max > 0.25_dp*skin**2) rebuild = .TRUE.
111 : END IF
112 :
113 55599 : IF (rebuild) THEN
114 54365 : CALL nnp_build_cell_list_cache(cache, cell, exact_cutoff, list_cutoff, skin)
115 : ! Count the rebuild so the GPU path can tell whether the image
116 : ! translations it last uploaded are still the current ones. A
117 : ! counter rather than a per-step flag, because this routine runs on
118 : ! every force evaluation while the device is only reached on some of
119 : ! them: a flag set on an energy-only step would be cleared before
120 : ! the next step that could act on it.
121 54365 : cache%rebuild_count = cache%rebuild_count + 1
122 1234 : ELSE IF (cache%initialized) THEN
123 : ! On reuse the bin geometry is frozen, but the head/next chain
124 : ! is rebinned from current positions so the walk still finds every
125 : ! in-cutoff pair.
126 1234 : CALL nnp_cell_list_rebin_chain(cache)
127 : END IF
128 : END ASSOCIATE
129 :
130 55599 : CALL timestop(handle)
131 :
132 55599 : END SUBROUTINE nnp_prepare_cell_list_cache
133 :
134 : ! **************************************************************************************************
135 : !> \brief Ensure the persistent coord buffers match num_atoms.
136 : !> Only (re-)allocates on first call or when num_atoms changes.
137 : !> \param cache cell-list cache whose coordinate buffers will be (re-)sized
138 : !> \param num_atoms number of primary-cell atoms the buffers must hold
139 : ! **************************************************************************************************
140 55599 : SUBROUTINE nnp_cell_list_ensure_coord_buffers(cache, num_atoms)
141 :
142 : TYPE(nnp_cell_list_cache_type), INTENT(INOUT) :: cache
143 : INTEGER, INTENT(IN) :: num_atoms
144 :
145 55599 : IF (ALLOCATED(cache%coord_primary)) THEN
146 55569 : IF (SIZE(cache%coord_primary, 2) == num_atoms) RETURN
147 0 : DEALLOCATE (cache%coord_primary)
148 : END IF
149 30 : IF (ALLOCATED(cache%coord_scaled)) DEALLOCATE (cache%coord_scaled)
150 30 : IF (ALLOCATED(cache%ref_coord_primary)) DEALLOCATE (cache%ref_coord_primary)
151 :
152 90 : ALLOCATE (cache%coord_primary(3, num_atoms))
153 60 : ALLOCATE (cache%coord_scaled(3, num_atoms))
154 60 : ALLOCATE (cache%ref_coord_primary(3, num_atoms))
155 :
156 : ! Resizing implies stale bin geometry; force the next prepare to rebuild.
157 30 : cache%initialized = .FALSE.
158 30 : cache%num_atoms = num_atoms
159 :
160 : END SUBROUTINE nnp_cell_list_ensure_coord_buffers
161 :
162 : ! **************************************************************************************************
163 : !> \brief Build the Cartesian linked-cell cache from the current positions.
164 : !>
165 : !> Positions must already live in cache%coord_primary / coord_scaled --
166 : !> this routine reads them in place and resets ref_coord_primary for
167 : !> the Verlet skin check.
168 : !> \param cache cell-list cache to populate; coord_primary / coord_scaled must already be filled
169 : !> \param cell simulation cell providing hmat, h_inv and per-axis periodicity
170 : !> \param exact_cutoff largest per-pair SF cutoff used to size the exact image ring
171 : !> \param list_cutoff cell-list bin width (exact_cutoff + Verlet skin)
172 : !> \param skin Verlet skin allowance used by the bin-reuse displacement check
173 : ! **************************************************************************************************
174 54365 : SUBROUTINE nnp_build_cell_list_cache(cache, cell, exact_cutoff, list_cutoff, skin)
175 :
176 : TYPE(nnp_cell_list_cache_type), INTENT(INOUT) :: cache
177 : TYPE(cell_type), INTENT(IN), POINTER :: cell
178 : REAL(KIND=dp), INTENT(IN) :: exact_cutoff, list_cutoff, skin
179 :
180 : INTEGER :: i, img, n_images, nx, ny, nz, tx, ty, tz
181 : INTEGER, DIMENSION(3) :: bin_index, shift
182 : REAL(KIND=dp) :: padding, safe_cutoff
183 : REAL(KIND=dp), DIMENSION(3) :: max_ref, min_ref, range_xyz, ref_pos
184 :
185 54365 : CALL nnp_cell_list_release_cell_structure(cache)
186 :
187 54365 : cache%exact_cutoff = exact_cutoff
188 54365 : cache%list_cutoff = list_cutoff
189 54365 : cache%verlet_skin = skin
190 217460 : cache%perd = cell%perd
191 54365 : cache%orthorhombic = cell%orthorhombic
192 706745 : cache%hmat = cell%hmat
193 706745 : cache%h_inv = cell%h_inv
194 :
195 1137141 : cache%ref_coord_primary(:, :) = cache%coord_primary(:, :)
196 :
197 54365 : CALL nnp_compute_pbc_copies(cache%exact_pbc_copies, cell, exact_cutoff)
198 54365 : CALL nnp_compute_pbc_copies(cache%list_pbc_copies, cell, list_cutoff)
199 : ! The bin stencil clips out-of-range bins rather than wrapping, so periodic
200 : ! neighbours come only from pre-replicated images. Force at least one ring
201 : ! on each periodic axis (MAX, not +) so boxes with L > 2*cutoff keep their
202 : ! wrap-around images.
203 217460 : cache%image_copies = MAX(cache%list_pbc_copies, cell%perd)
204 : ! The exact image ring must fit inside the replicated pool; otherwise the
205 : ! matcher would accept a ghost that was never created and drop a real pair.
206 217460 : CPASSERT(ALL(cache%exact_pbc_copies <= cache%image_copies))
207 :
208 54365 : nx = 2*cache%image_copies(1) + 1
209 54365 : ny = 2*cache%image_copies(2) + 1
210 54365 : nz = 2*cache%image_copies(3) + 1
211 54365 : n_images = cache%num_atoms*nx*ny*nz
212 54365 : cache%n_images = n_images
213 :
214 163095 : ALLOCATE (cache%image_atom(n_images))
215 163095 : ALLOCATE (cache%image_shift(3, n_images))
216 163095 : ALLOCATE (cache%image_translation(3, n_images))
217 :
218 217460 : min_ref(:) = HUGE(1.0_dp)
219 217460 : max_ref(:) = -HUGE(1.0_dp)
220 : img = 0
221 325059 : DO i = 1, cache%num_atoms
222 704393 : DO tx = -cache%image_copies(1), cache%image_copies(1)
223 1355282 : DO ty = -cache%image_copies(2), cache%image_copies(2)
224 2767602 : DO tz = -cache%image_copies(3), cache%image_copies(3)
225 1683014 : img = img + 1
226 6732056 : shift = [tx, ty, tz]
227 1683014 : cache%image_atom(img) = i
228 6732056 : cache%image_shift(:, img) = shift(:)
229 31977266 : cache%image_translation(:, img) = MATMUL(cell%hmat, REAL(shift, KIND=dp))
230 6732056 : ref_pos(:) = cache%coord_primary(:, i) + cache%image_translation(:, img)
231 6732056 : min_ref(:) = MIN(min_ref(:), ref_pos(:))
232 7437310 : max_ref(:) = MAX(max_ref(:), ref_pos(:))
233 : END DO
234 : END DO
235 : END DO
236 : END DO
237 :
238 54365 : padding = 0.5_dp*skin + rebuild_eps
239 217460 : cache%lower(:) = min_ref(:) - padding
240 217460 : cache%upper(:) = max_ref(:) + padding
241 217460 : range_xyz(:) = cache%upper(:) - cache%lower(:)
242 :
243 : ! Coarse grid: bin width = list_cutoff (bin_span 1, 27-bin walk). A finer
244 : ! grid changes the order in which neighbours are appended, which reorders
245 : ! the descriptor summation, so it is not used.
246 54365 : safe_cutoff = MAX(list_cutoff, rebuild_eps)
247 217460 : DO i = 1, 3
248 217460 : IF (range_xyz(i) > rebuild_eps) THEN
249 163095 : cache%nbin(i) = MAX(1, CEILING(range_xyz(i)/safe_cutoff))
250 163095 : cache%bin_width(i) = range_xyz(i)/REAL(cache%nbin(i), KIND=dp)
251 : cache%bin_span(i) = MIN(cache%nbin(i) - 1, &
252 163095 : CEILING(list_cutoff/MAX(cache%bin_width(i), rebuild_eps)))
253 : ELSE
254 0 : cache%nbin(i) = 1
255 0 : cache%bin_width(i) = 1.0_dp
256 0 : cache%bin_span(i) = 0
257 : END IF
258 : END DO
259 :
260 217460 : cache%n_cells = PRODUCT(cache%nbin)
261 163095 : ALLOCATE (cache%head(cache%n_cells))
262 108730 : ALLOCATE (cache%next(n_images))
263 225533 : cache%head(:) = 0
264 1737379 : cache%next(:) = 0
265 :
266 1737379 : DO img = 1, n_images
267 1683014 : i = cache%image_atom(img)
268 6732056 : ref_pos(:) = cache%coord_primary(:, i) + cache%image_translation(:, img)
269 1683014 : CALL nnp_cell_list_bin_from_position(cache, ref_pos, bin_index)
270 1683014 : cache%next(img) = cache%head(nnp_cell_list_linear_index(cache, bin_index))
271 1737379 : cache%head(nnp_cell_list_linear_index(cache, bin_index)) = img
272 : END DO
273 :
274 54365 : cache%initialized = .TRUE.
275 :
276 54365 : END SUBROUTINE nnp_build_cell_list_cache
277 :
278 : ! **************************************************************************************************
279 : !> \brief Rebin the head/next chain from current coord_primary while keeping the
280 : !> image pool, bin geometry and ref_coord_primary frozen. Used on Verlet
281 : !> skin reuse so the chain reflects atoms that crossed bin boundaries.
282 : !> \param cache cell-list cache whose head/next chain is rebuilt from current coord_primary
283 : ! **************************************************************************************************
284 1234 : SUBROUTINE nnp_cell_list_rebin_chain(cache)
285 :
286 : TYPE(nnp_cell_list_cache_type), INTENT(INOUT) :: cache
287 :
288 : INTEGER :: i, img, lin_idx
289 : INTEGER, DIMENSION(3) :: bin_index
290 : REAL(KIND=dp), DIMENSION(3) :: ref_pos
291 :
292 37406 : cache%head(:) = 0
293 823390 : cache%next(:) = 0
294 :
295 823390 : DO img = 1, cache%n_images
296 822156 : i = cache%image_atom(img)
297 3288624 : ref_pos(:) = cache%coord_primary(:, i) + cache%image_translation(:, img)
298 822156 : CALL nnp_cell_list_bin_from_position(cache, ref_pos, bin_index)
299 822156 : lin_idx = nnp_cell_list_linear_index(cache, bin_index)
300 822156 : cache%next(img) = cache%head(lin_idx)
301 823390 : cache%head(lin_idx) = img
302 : END DO
303 :
304 1234 : END SUBROUTINE nnp_cell_list_rebin_chain
305 :
306 : ! **************************************************************************************************
307 : !> \brief Free only the cell-structure pieces of the cache (bins and image
308 : !> metadata). The coordinate buffers are persistent and kept, so a
309 : !> rebuild can still read the current positions.
310 : !> \param cache cell-list cache whose bin geometry and image metadata are freed (coord buffers kept)
311 : ! **************************************************************************************************
312 54365 : SUBROUTINE nnp_cell_list_release_cell_structure(cache)
313 :
314 : TYPE(nnp_cell_list_cache_type), INTENT(INOUT) :: cache
315 :
316 54365 : IF (ALLOCATED(cache%image_atom)) DEALLOCATE (cache%image_atom)
317 54365 : IF (ALLOCATED(cache%head)) DEALLOCATE (cache%head)
318 54365 : IF (ALLOCATED(cache%next)) DEALLOCATE (cache%next)
319 54365 : IF (ALLOCATED(cache%image_shift)) DEALLOCATE (cache%image_shift)
320 54365 : IF (ALLOCATED(cache%image_translation)) DEALLOCATE (cache%image_translation)
321 :
322 54365 : cache%n_images = 0
323 54365 : cache%n_cells = 1
324 217460 : cache%exact_pbc_copies = 0
325 217460 : cache%list_pbc_copies = 0
326 217460 : cache%image_copies = 0
327 217460 : cache%nbin = 1
328 217460 : cache%bin_span = 0
329 217460 : cache%lower = 0.0_dp
330 217460 : cache%upper = 0.0_dp
331 217460 : cache%bin_width = 1.0_dp
332 :
333 54365 : END SUBROUTINE nnp_cell_list_release_cell_structure
334 :
335 : ! **************************************************************************************************
336 : !> \brief Fill the ACSF neighbour buffers for one central atom by walking the
337 : !> linked-cell stencil around its bin, pushing (j, dr, r) into the
338 : !> per-species-pair slabs for entries inside each pair cutoff. The own-bin
339 : !> self image (i == j, zero shift) is skipped.
340 : !> \param nnp NNP environment with cell_list_cache and neighbor_interface_state ready
341 : !> \param neighbor per-atom neighbour view filled in place (counters zeroed before this call)
342 : !> \param i central-atom index in the global ordering
343 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
344 : !> \author Christoph Schran (christoph.schran@rub.de)
345 : ! **************************************************************************************************
346 263313 : SUBROUTINE nnp_compute_neighbors_cell_list(nnp, neighbor, i)
347 :
348 : TYPE(nnp_type), INTENT(INOUT), POINTER :: nnp
349 : TYPE(nnp_neighbor_type), INTENT(INOUT) :: neighbor
350 : INTEGER, INTENT(IN) :: i
351 :
352 : INTEGER :: bx, by, bz, img, ind, j, neighbor_ind, &
353 : pair_slot, s
354 : INTEGER, DIMENSION(3) :: bin_index, current_shift
355 : REAL(KIND=dp) :: norm
356 : REAL(KIND=dp), DIMENSION(3) :: center, dr, image_pos
357 :
358 : ASSOCIATE (cache => nnp%cell_list_cache, &
359 : state => nnp%neighbor_interface_state)
360 :
361 263313 : ind = nnp%ele_ind(i)
362 1053252 : center(:) = cache%coord_primary(:, i)
363 263313 : CALL nnp_cell_list_bin_from_position(cache, center, bin_index)
364 :
365 747494 : DO bx = MAX(1, bin_index(1) - cache%bin_span(1)), &
366 526626 : MIN(cache%nbin(1), bin_index(1) + cache%bin_span(1))
367 1956034 : DO by = MAX(1, bin_index(2) - cache%bin_span(2)), &
368 747494 : MIN(cache%nbin(2), bin_index(2) + cache%bin_span(2))
369 7243509 : DO bz = MAX(1, bin_index(3) - cache%bin_span(3)), &
370 1956034 : MIN(cache%nbin(3), bin_index(3) + cache%bin_span(3))
371 23086624 : img = cache%head(nnp_cell_list_linear_index(cache, [bx, by, bz]))
372 136653804 : DO WHILE (img > 0)
373 129410295 : j = cache%image_atom(img)
374 517641180 : current_shift(:) = cache%image_shift(:, img)
375 222560560 : IF (j == i .AND. ALL(current_shift == 0)) THEN
376 263313 : img = cache%next(img)
377 263313 : CYCLE
378 : END IF
379 129146982 : IF (.NOT. nnp_cell_list_exact_image_match(cache, i, j, current_shift)) THEN
380 5103599 : img = cache%next(img)
381 5103599 : CYCLE
382 : END IF
383 :
384 496173532 : image_pos(:) = cache%coord_primary(:, j) + cache%image_translation(:, img)
385 496173532 : dr(:) = center(:) - image_pos(:)
386 496173532 : norm = NORM2(dr(:))
387 124043383 : neighbor_ind = nnp%ele_ind(j)
388 :
389 124043383 : IF (norm < state%pair_map(ind, neighbor_ind)%max_relevant_cutoff) THEN
390 9796172 : DO pair_slot = 1, state%pair_map(ind, neighbor_ind)%n_rad
391 4898086 : s = state%pair_map(ind, neighbor_ind)%rad_groups(pair_slot)
392 9796172 : IF (norm < nnp%rad(ind)%symfgrp(s)%cutoff) THEN
393 4898086 : neighbor%n_rad(s) = neighbor%n_rad(s) + 1
394 4898086 : IF (neighbor%n_rad(s) > neighbor%rad(s)%cap) THEN
395 423 : CALL nnp_neigh_grp_grow(neighbor%rad(s), neighbor%n_rad(s))
396 : END IF
397 4898086 : neighbor%rad(s)%ind(neighbor%n_rad(s)) = j
398 4898086 : neighbor%rad(s)%dist(1, neighbor%n_rad(s)) = dr(1)
399 4898086 : neighbor%rad(s)%dist(2, neighbor%n_rad(s)) = dr(2)
400 4898086 : neighbor%rad(s)%dist(3, neighbor%n_rad(s)) = dr(3)
401 4898086 : neighbor%rad(s)%dist(4, neighbor%n_rad(s)) = norm
402 : END IF
403 : END DO
404 :
405 11287857 : DO pair_slot = 1, state%pair_map(ind, neighbor_ind)%n_ang1
406 6389771 : s = state%pair_map(ind, neighbor_ind)%ang1_groups(pair_slot)
407 11287857 : IF (norm < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
408 6389771 : neighbor%n_ang1(s) = neighbor%n_ang1(s) + 1
409 6389771 : IF (neighbor%n_ang1(s) > neighbor%ang1(s)%cap) THEN
410 711 : CALL nnp_neigh_grp_grow(neighbor%ang1(s), neighbor%n_ang1(s))
411 : END IF
412 6389771 : neighbor%ang1(s)%ind(neighbor%n_ang1(s)) = j
413 6389771 : neighbor%ang1(s)%dist(1, neighbor%n_ang1(s)) = dr(1)
414 6389771 : neighbor%ang1(s)%dist(2, neighbor%n_ang1(s)) = dr(2)
415 6389771 : neighbor%ang1(s)%dist(3, neighbor%n_ang1(s)) = dr(3)
416 6389771 : neighbor%ang1(s)%dist(4, neighbor%n_ang1(s)) = norm
417 : END IF
418 : END DO
419 :
420 9255093 : DO pair_slot = 1, state%pair_map(ind, neighbor_ind)%n_ang2
421 4357007 : s = state%pair_map(ind, neighbor_ind)%ang2_groups(pair_slot)
422 9255093 : IF (norm < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
423 4357007 : neighbor%n_ang2(s) = neighbor%n_ang2(s) + 1
424 4357007 : IF (neighbor%n_ang2(s) > neighbor%ang2(s)%cap) THEN
425 513 : CALL nnp_neigh_grp_grow(neighbor%ang2(s), neighbor%n_ang2(s))
426 : END IF
427 4357007 : neighbor%ang2(s)%ind(neighbor%n_ang2(s)) = j
428 4357007 : neighbor%ang2(s)%dist(1, neighbor%n_ang2(s)) = dr(1)
429 4357007 : neighbor%ang2(s)%dist(2, neighbor%n_ang2(s)) = dr(2)
430 4357007 : neighbor%ang2(s)%dist(3, neighbor%n_ang2(s)) = dr(3)
431 4357007 : neighbor%ang2(s)%dist(4, neighbor%n_ang2(s)) = norm
432 : END IF
433 : END DO
434 : END IF
435 :
436 124043383 : img = cache%next(img)
437 : END DO
438 : END DO
439 : END DO
440 : END DO
441 :
442 : END ASSOCIATE
443 :
444 263313 : END SUBROUTINE nnp_compute_neighbors_cell_list
445 :
446 : ! **************************************************************************************************
447 : !> \brief Check whether a ghost image corresponds to one of the exact pbc copies.
448 : !> \param cache cell-list cache providing per-axis periodicity and scaled coordinates
449 : !> \param i central-atom index in the primary cell
450 : !> \param j candidate neighbour atom index in the primary cell
451 : !> \param image_shift 3-vector of integer cell translations identifying the ghost image
452 : !> \return .TRUE. if the (i, j, image_shift) image falls inside the exact_pbc_copies ring
453 : ! **************************************************************************************************
454 129146982 : LOGICAL FUNCTION nnp_cell_list_exact_image_match(cache, i, j, image_shift)
455 :
456 : TYPE(nnp_cell_list_cache_type), INTENT(IN) :: cache
457 : INTEGER, INTENT(IN) :: i, j
458 : INTEGER, DIMENSION(3), INTENT(IN) :: image_shift
459 :
460 : INTEGER :: d, minimal_shift
461 :
462 129146982 : nnp_cell_list_exact_image_match = .TRUE.
463 503502987 : DO d = 1, 3
464 503502987 : IF (cache%perd(d) == 1) THEN
465 377473214 : minimal_shift = NINT(cache%coord_scaled(d, i) - cache%coord_scaled(d, j))
466 377473214 : IF (ABS(minimal_shift - image_shift(d)) > cache%exact_pbc_copies(d)) THEN
467 129146982 : nnp_cell_list_exact_image_match = .FALSE.
468 : RETURN
469 : END IF
470 1986390 : ELSE IF (image_shift(d) /= 0) THEN
471 129146982 : nnp_cell_list_exact_image_match = .FALSE.
472 : RETURN
473 : END IF
474 : END DO
475 :
476 : END FUNCTION nnp_cell_list_exact_image_match
477 :
478 : ! **************************************************************************************************
479 : !> \brief Convert Cartesian position to linked-cell bin coordinates.
480 : !> \param cache cell-list cache providing the bin origin and bin widths
481 : !> \param pos Cartesian position (Bohr) to bin
482 : !> \param bin_index (out) 1-based bin indices clipped to [1, nbin] on each axis
483 : ! **************************************************************************************************
484 2768483 : SUBROUTINE nnp_cell_list_bin_from_position(cache, pos, bin_index)
485 :
486 : TYPE(nnp_cell_list_cache_type), INTENT(IN) :: cache
487 : REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: pos
488 : INTEGER, DIMENSION(3), INTENT(OUT) :: bin_index
489 :
490 : INTEGER :: d
491 : REAL(KIND=dp) :: scaled
492 :
493 11073932 : DO d = 1, 3
494 8305449 : scaled = (pos(d) - cache%lower(d))/MAX(cache%bin_width(d), rebuild_eps)
495 8305449 : bin_index(d) = INT(scaled) + 1
496 11073932 : bin_index(d) = MAX(1, MIN(cache%nbin(d), bin_index(d)))
497 : END DO
498 :
499 2768483 : END SUBROUTINE nnp_cell_list_bin_from_position
500 :
501 : ! **************************************************************************************************
502 : !> \brief Flatten 3D bin indices into the head/next storage index.
503 : !> \param cache cell-list cache providing the per-axis bin counts
504 : !> \param bin_index 1-based 3D bin indices to flatten
505 : !> \return linear index into cache%head
506 : ! **************************************************************************************************
507 8276826 : INTEGER FUNCTION nnp_cell_list_linear_index(cache, bin_index)
508 :
509 : TYPE(nnp_cell_list_cache_type), INTENT(IN) :: cache
510 : INTEGER, DIMENSION(3), INTENT(IN) :: bin_index
511 :
512 : nnp_cell_list_linear_index = bin_index(1) + cache%nbin(1)* &
513 8276826 : ((bin_index(2) - 1) + cache%nbin(2)*(bin_index(3) - 1))
514 :
515 8276826 : END FUNCTION nnp_cell_list_linear_index
516 :
517 : ! **************************************************************************************************
518 : !> \brief Number of PBC image rings along each lattice axis needed to cover a
519 : !> sphere of radius `cutoff` from any primary-cell atom. Uses the
520 : !> cell-vector projections onto the plane normals (correct for triclinic
521 : !> cells) and multiplies by cell%perd so non-periodic axes return 0.
522 : !> \param pbc_copies output: number of image rings on each of the 3 lattice axes
523 : !> \param cell cell whose hmat/h_inv/perd/deth are read
524 : !> \param cutoff sphere radius (Bohr) to cover
525 : !> \author Christoph Schran (christoph.schran@rub.de)
526 : ! **************************************************************************************************
527 108730 : SUBROUTINE nnp_compute_pbc_copies(pbc_copies, cell, cutoff)
528 :
529 : INTEGER, DIMENSION(3), INTENT(OUT) :: pbc_copies
530 : TYPE(cell_type), INTENT(IN), POINTER :: cell
531 : REAL(KIND=dp), INTENT(IN) :: cutoff
532 :
533 : REAL(KIND=dp) :: proja, projb, projc
534 : REAL(KIND=dp), DIMENSION(3) :: axb, axc, bxc
535 :
536 : ! A degenerate cell (deth <= 0) makes the NORM2 normalisations below
537 : ! divide by zero and the DO WHILE projection loops never terminate.
538 108730 : CPASSERT(cell%deth > 0.0_dp)
539 :
540 108730 : axb(1) = cell%hmat(2, 1)*cell%hmat(3, 2) - cell%hmat(3, 1)*cell%hmat(2, 2)
541 108730 : axb(2) = cell%hmat(3, 1)*cell%hmat(1, 2) - cell%hmat(1, 1)*cell%hmat(3, 2)
542 108730 : axb(3) = cell%hmat(1, 1)*cell%hmat(2, 2) - cell%hmat(2, 1)*cell%hmat(1, 2)
543 761110 : axb(:) = axb(:)/NORM2(axb(:))
544 :
545 108730 : axc(1) = cell%hmat(2, 1)*cell%hmat(3, 3) - cell%hmat(3, 1)*cell%hmat(2, 3)
546 108730 : axc(2) = cell%hmat(3, 1)*cell%hmat(1, 3) - cell%hmat(1, 1)*cell%hmat(3, 3)
547 108730 : axc(3) = cell%hmat(1, 1)*cell%hmat(2, 3) - cell%hmat(2, 1)*cell%hmat(1, 3)
548 761110 : axc(:) = axc(:)/NORM2(axc(:))
549 :
550 108730 : bxc(1) = cell%hmat(2, 2)*cell%hmat(3, 3) - cell%hmat(3, 2)*cell%hmat(2, 3)
551 108730 : bxc(2) = cell%hmat(3, 2)*cell%hmat(1, 3) - cell%hmat(1, 2)*cell%hmat(3, 3)
552 108730 : bxc(3) = cell%hmat(1, 2)*cell%hmat(2, 3) - cell%hmat(2, 2)*cell%hmat(1, 3)
553 761110 : bxc(:) = bxc(:)/NORM2(bxc(:))
554 :
555 434920 : proja = ABS(SUM(cell%hmat(:, 1)*bxc(:)))*0.5_dp
556 434920 : projb = ABS(SUM(cell%hmat(:, 2)*axc(:)))*0.5_dp
557 434920 : projc = ABS(SUM(cell%hmat(:, 3)*axb(:)))*0.5_dp
558 :
559 108730 : pbc_copies(:) = 0
560 433820 : DO WHILE ((pbc_copies(1) + 1)*proja <= cutoff)
561 325090 : pbc_copies(1) = pbc_copies(1) + 1
562 : END DO
563 433820 : DO WHILE ((pbc_copies(2) + 1)*projb <= cutoff)
564 325090 : pbc_copies(2) = pbc_copies(2) + 1
565 : END DO
566 433828 : DO WHILE ((pbc_copies(3) + 1)*projc <= cutoff)
567 325098 : pbc_copies(3) = pbc_copies(3) + 1
568 : END DO
569 434920 : pbc_copies(:) = pbc_copies(:)*cell%perd(:)
570 :
571 108730 : END SUBROUTINE nnp_compute_pbc_copies
572 :
573 : END MODULE nnp_cell_list
|