(git:27c966b)
Loading...
Searching...
No Matches
nnp_cell_list.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 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! **************************************************************************************************
19
20 USE cell_types, ONLY: cell_type,&
21 pbc,&
23 USE kinds, ONLY: dp
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
43
44CONTAINS
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! **************************************************************************************************
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 CALL timeset('nnp_acsf_cell_list_prepare', handle)
66
67 NULLIFY (cell)
68 CALL nnp_env_get(nnp_env=nnp, cell=cell)
69
70 exact_cutoff = nnp%max_cut
71 IF (exact_cutoff > 0.0_dp) THEN
72 IF (nnp%verlet_skin >= 0.0_dp) THEN
73 ! VERLET_SKIN keyword value (bohr).
74 skin = nnp%verlet_skin
75 ELSE
76 ! Auto heuristic: cap at the default, scale down for small cutoffs.
77 skin = min(default_verlet_skin, 0.1_dp*exact_cutoff)
78 END IF
79 ELSE
80 skin = 0.0_dp
81 END IF
82 list_cutoff = exact_cutoff + skin
83
84 CALL nnp_cell_list_ensure_coord_buffers(nnp%cell_list_cache, nnp%num_atoms)
85
86 associate(cache => nnp%cell_list_cache)
87 DO i = 1, nnp%num_atoms
88 cache%coord_primary(:, i) = pbc(nnp%coord(:, i), cell, .true.)
89 CALL real_to_scaled(cache%coord_scaled(:, i), &
90 cache%coord_primary(:, i), cell)
91 END DO
92
93 rebuild = .NOT. cache%initialized
94 IF (.NOT. rebuild) rebuild = (cache%num_atoms /= nnp%num_atoms)
95 IF (.NOT. rebuild) rebuild = (abs(cache%exact_cutoff - exact_cutoff) > rebuild_eps)
96 IF (.NOT. rebuild) rebuild = (abs(cache%list_cutoff - list_cutoff) > rebuild_eps)
97 IF (.NOT. rebuild) rebuild = (abs(cache%verlet_skin - skin) > rebuild_eps)
98 IF (.NOT. rebuild) rebuild = any(cache%perd /= cell%perd)
99 IF (.NOT. rebuild) rebuild = (cache%orthorhombic .NEQV. cell%orthorhombic)
100 IF (.NOT. rebuild) rebuild = any(abs(cache%hmat - cell%hmat) > rebuild_eps)
101 IF (.NOT. rebuild) rebuild = any(abs(cache%h_inv - cell%h_inv) > rebuild_eps)
102
103 IF ((.NOT. rebuild) .AND. (skin > 0.0_dp)) THEN
104 dr2_max = 0.0_dp
105 DO i = 1, nnp%num_atoms
106 dr(:) = cache%coord_primary(:, i) - cache%ref_coord_primary(:, i)
107 dr(:) = pbc(dr, cell)
108 dr2_max = max(dr2_max, dot_product(dr, dr))
109 END DO
110 IF (dr2_max > 0.25_dp*skin**2) rebuild = .true.
111 END IF
112
113 IF (rebuild) THEN
114 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 cache%rebuild_count = cache%rebuild_count + 1
122 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 CALL nnp_cell_list_rebin_chain(cache)
127 END IF
128 END associate
129
130 CALL timestop(handle)
131
132 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 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 IF (ALLOCATED(cache%coord_primary)) THEN
146 IF (SIZE(cache%coord_primary, 2) == num_atoms) RETURN
147 DEALLOCATE (cache%coord_primary)
148 END IF
149 IF (ALLOCATED(cache%coord_scaled)) DEALLOCATE (cache%coord_scaled)
150 IF (ALLOCATED(cache%ref_coord_primary)) DEALLOCATE (cache%ref_coord_primary)
151
152 ALLOCATE (cache%coord_primary(3, num_atoms))
153 ALLOCATE (cache%coord_scaled(3, num_atoms))
154 ALLOCATE (cache%ref_coord_primary(3, num_atoms))
155
156 ! Resizing implies stale bin geometry; force the next prepare to rebuild.
157 cache%initialized = .false.
158 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 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 CALL nnp_cell_list_release_cell_structure(cache)
186
187 cache%exact_cutoff = exact_cutoff
188 cache%list_cutoff = list_cutoff
189 cache%verlet_skin = skin
190 cache%perd = cell%perd
191 cache%orthorhombic = cell%orthorhombic
192 cache%hmat = cell%hmat
193 cache%h_inv = cell%h_inv
194
195 cache%ref_coord_primary(:, :) = cache%coord_primary(:, :)
196
197 CALL nnp_compute_pbc_copies(cache%exact_pbc_copies, cell, exact_cutoff)
198 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 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 cpassert(all(cache%exact_pbc_copies <= cache%image_copies))
207
208 nx = 2*cache%image_copies(1) + 1
209 ny = 2*cache%image_copies(2) + 1
210 nz = 2*cache%image_copies(3) + 1
211 n_images = cache%num_atoms*nx*ny*nz
212 cache%n_images = n_images
213
214 ALLOCATE (cache%image_atom(n_images))
215 ALLOCATE (cache%image_shift(3, n_images))
216 ALLOCATE (cache%image_translation(3, n_images))
217
218 min_ref(:) = huge(1.0_dp)
219 max_ref(:) = -huge(1.0_dp)
220 img = 0
221 DO i = 1, cache%num_atoms
222 DO tx = -cache%image_copies(1), cache%image_copies(1)
223 DO ty = -cache%image_copies(2), cache%image_copies(2)
224 DO tz = -cache%image_copies(3), cache%image_copies(3)
225 img = img + 1
226 shift = [tx, ty, tz]
227 cache%image_atom(img) = i
228 cache%image_shift(:, img) = shift(:)
229 cache%image_translation(:, img) = matmul(cell%hmat, real(shift, kind=dp))
230 ref_pos(:) = cache%coord_primary(:, i) + cache%image_translation(:, img)
231 min_ref(:) = min(min_ref(:), ref_pos(:))
232 max_ref(:) = max(max_ref(:), ref_pos(:))
233 END DO
234 END DO
235 END DO
236 END DO
237
238 padding = 0.5_dp*skin + rebuild_eps
239 cache%lower(:) = min_ref(:) - padding
240 cache%upper(:) = max_ref(:) + padding
241 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 safe_cutoff = max(list_cutoff, rebuild_eps)
247 DO i = 1, 3
248 IF (range_xyz(i) > rebuild_eps) THEN
249 cache%nbin(i) = max(1, ceiling(range_xyz(i)/safe_cutoff))
250 cache%bin_width(i) = range_xyz(i)/real(cache%nbin(i), kind=dp)
251 cache%bin_span(i) = min(cache%nbin(i) - 1, &
252 ceiling(list_cutoff/max(cache%bin_width(i), rebuild_eps)))
253 ELSE
254 cache%nbin(i) = 1
255 cache%bin_width(i) = 1.0_dp
256 cache%bin_span(i) = 0
257 END IF
258 END DO
259
260 cache%n_cells = product(cache%nbin)
261 ALLOCATE (cache%head(cache%n_cells))
262 ALLOCATE (cache%next(n_images))
263 cache%head(:) = 0
264 cache%next(:) = 0
265
266 DO img = 1, n_images
267 i = cache%image_atom(img)
268 ref_pos(:) = cache%coord_primary(:, i) + cache%image_translation(:, img)
269 CALL nnp_cell_list_bin_from_position(cache, ref_pos, bin_index)
270 cache%next(img) = cache%head(nnp_cell_list_linear_index(cache, bin_index))
271 cache%head(nnp_cell_list_linear_index(cache, bin_index)) = img
272 END DO
273
274 cache%initialized = .true.
275
276 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 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 cache%head(:) = 0
293 cache%next(:) = 0
294
295 DO img = 1, cache%n_images
296 i = cache%image_atom(img)
297 ref_pos(:) = cache%coord_primary(:, i) + cache%image_translation(:, img)
298 CALL nnp_cell_list_bin_from_position(cache, ref_pos, bin_index)
299 lin_idx = nnp_cell_list_linear_index(cache, bin_index)
300 cache%next(img) = cache%head(lin_idx)
301 cache%head(lin_idx) = img
302 END DO
303
304 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 SUBROUTINE nnp_cell_list_release_cell_structure(cache)
313
314 TYPE(nnp_cell_list_cache_type), INTENT(INOUT) :: cache
315
316 IF (ALLOCATED(cache%image_atom)) DEALLOCATE (cache%image_atom)
317 IF (ALLOCATED(cache%head)) DEALLOCATE (cache%head)
318 IF (ALLOCATED(cache%next)) DEALLOCATE (cache%next)
319 IF (ALLOCATED(cache%image_shift)) DEALLOCATE (cache%image_shift)
320 IF (ALLOCATED(cache%image_translation)) DEALLOCATE (cache%image_translation)
321
322 cache%n_images = 0
323 cache%n_cells = 1
324 cache%exact_pbc_copies = 0
325 cache%list_pbc_copies = 0
326 cache%image_copies = 0
327 cache%nbin = 1
328 cache%bin_span = 0
329 cache%lower = 0.0_dp
330 cache%upper = 0.0_dp
331 cache%bin_width = 1.0_dp
332
333 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 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 ind = nnp%ele_ind(i)
362 center(:) = cache%coord_primary(:, i)
363 CALL nnp_cell_list_bin_from_position(cache, center, bin_index)
364
365 DO bx = max(1, bin_index(1) - cache%bin_span(1)), &
366 min(cache%nbin(1), bin_index(1) + cache%bin_span(1))
367 DO by = max(1, bin_index(2) - cache%bin_span(2)), &
368 min(cache%nbin(2), bin_index(2) + cache%bin_span(2))
369 DO bz = max(1, bin_index(3) - cache%bin_span(3)), &
370 min(cache%nbin(3), bin_index(3) + cache%bin_span(3))
371 img = cache%head(nnp_cell_list_linear_index(cache, [bx, by, bz]))
372 DO WHILE (img > 0)
373 j = cache%image_atom(img)
374 current_shift(:) = cache%image_shift(:, img)
375 IF (j == i .AND. all(current_shift == 0)) THEN
376 img = cache%next(img)
377 cycle
378 END IF
379 IF (.NOT. nnp_cell_list_exact_image_match(cache, i, j, current_shift)) THEN
380 img = cache%next(img)
381 cycle
382 END IF
383
384 image_pos(:) = cache%coord_primary(:, j) + cache%image_translation(:, img)
385 dr(:) = center(:) - image_pos(:)
386 norm = norm2(dr(:))
387 neighbor_ind = nnp%ele_ind(j)
388
389 IF (norm < state%pair_map(ind, neighbor_ind)%max_relevant_cutoff) THEN
390 DO pair_slot = 1, state%pair_map(ind, neighbor_ind)%n_rad
391 s = state%pair_map(ind, neighbor_ind)%rad_groups(pair_slot)
392 IF (norm < nnp%rad(ind)%symfgrp(s)%cutoff) THEN
393 neighbor%n_rad(s) = neighbor%n_rad(s) + 1
394 IF (neighbor%n_rad(s) > neighbor%rad(s)%cap) THEN
395 CALL nnp_neigh_grp_grow(neighbor%rad(s), neighbor%n_rad(s))
396 END IF
397 neighbor%rad(s)%ind(neighbor%n_rad(s)) = j
398 neighbor%rad(s)%dist(1, neighbor%n_rad(s)) = dr(1)
399 neighbor%rad(s)%dist(2, neighbor%n_rad(s)) = dr(2)
400 neighbor%rad(s)%dist(3, neighbor%n_rad(s)) = dr(3)
401 neighbor%rad(s)%dist(4, neighbor%n_rad(s)) = norm
402 END IF
403 END DO
404
405 DO pair_slot = 1, state%pair_map(ind, neighbor_ind)%n_ang1
406 s = state%pair_map(ind, neighbor_ind)%ang1_groups(pair_slot)
407 IF (norm < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
408 neighbor%n_ang1(s) = neighbor%n_ang1(s) + 1
409 IF (neighbor%n_ang1(s) > neighbor%ang1(s)%cap) THEN
410 CALL nnp_neigh_grp_grow(neighbor%ang1(s), neighbor%n_ang1(s))
411 END IF
412 neighbor%ang1(s)%ind(neighbor%n_ang1(s)) = j
413 neighbor%ang1(s)%dist(1, neighbor%n_ang1(s)) = dr(1)
414 neighbor%ang1(s)%dist(2, neighbor%n_ang1(s)) = dr(2)
415 neighbor%ang1(s)%dist(3, neighbor%n_ang1(s)) = dr(3)
416 neighbor%ang1(s)%dist(4, neighbor%n_ang1(s)) = norm
417 END IF
418 END DO
419
420 DO pair_slot = 1, state%pair_map(ind, neighbor_ind)%n_ang2
421 s = state%pair_map(ind, neighbor_ind)%ang2_groups(pair_slot)
422 IF (norm < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
423 neighbor%n_ang2(s) = neighbor%n_ang2(s) + 1
424 IF (neighbor%n_ang2(s) > neighbor%ang2(s)%cap) THEN
425 CALL nnp_neigh_grp_grow(neighbor%ang2(s), neighbor%n_ang2(s))
426 END IF
427 neighbor%ang2(s)%ind(neighbor%n_ang2(s)) = j
428 neighbor%ang2(s)%dist(1, neighbor%n_ang2(s)) = dr(1)
429 neighbor%ang2(s)%dist(2, neighbor%n_ang2(s)) = dr(2)
430 neighbor%ang2(s)%dist(3, neighbor%n_ang2(s)) = dr(3)
431 neighbor%ang2(s)%dist(4, neighbor%n_ang2(s)) = norm
432 END IF
433 END DO
434 END IF
435
436 img = cache%next(img)
437 END DO
438 END DO
439 END DO
440 END DO
441
442 END associate
443
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 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 nnp_cell_list_exact_image_match = .true.
463 DO d = 1, 3
464 IF (cache%perd(d) == 1) THEN
465 minimal_shift = nint(cache%coord_scaled(d, i) - cache%coord_scaled(d, j))
466 IF (abs(minimal_shift - image_shift(d)) > cache%exact_pbc_copies(d)) THEN
467 nnp_cell_list_exact_image_match = .false.
468 RETURN
469 END IF
470 ELSE IF (image_shift(d) /= 0) THEN
471 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 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 DO d = 1, 3
494 scaled = (pos(d) - cache%lower(d))/max(cache%bin_width(d), rebuild_eps)
495 bin_index(d) = int(scaled) + 1
496 bin_index(d) = max(1, min(cache%nbin(d), bin_index(d)))
497 END DO
498
499 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 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 ((bin_index(2) - 1) + cache%nbin(2)*(bin_index(3) - 1))
514
515 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 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 cpassert(cell%deth > 0.0_dp)
539
540 axb(1) = cell%hmat(2, 1)*cell%hmat(3, 2) - cell%hmat(3, 1)*cell%hmat(2, 2)
541 axb(2) = cell%hmat(3, 1)*cell%hmat(1, 2) - cell%hmat(1, 1)*cell%hmat(3, 2)
542 axb(3) = cell%hmat(1, 1)*cell%hmat(2, 2) - cell%hmat(2, 1)*cell%hmat(1, 2)
543 axb(:) = axb(:)/norm2(axb(:))
544
545 axc(1) = cell%hmat(2, 1)*cell%hmat(3, 3) - cell%hmat(3, 1)*cell%hmat(2, 3)
546 axc(2) = cell%hmat(3, 1)*cell%hmat(1, 3) - cell%hmat(1, 1)*cell%hmat(3, 3)
547 axc(3) = cell%hmat(1, 1)*cell%hmat(2, 3) - cell%hmat(2, 1)*cell%hmat(1, 3)
548 axc(:) = axc(:)/norm2(axc(:))
549
550 bxc(1) = cell%hmat(2, 2)*cell%hmat(3, 3) - cell%hmat(3, 2)*cell%hmat(2, 3)
551 bxc(2) = cell%hmat(3, 2)*cell%hmat(1, 3) - cell%hmat(1, 2)*cell%hmat(3, 3)
552 bxc(3) = cell%hmat(1, 2)*cell%hmat(2, 3) - cell%hmat(2, 2)*cell%hmat(1, 3)
553 bxc(:) = bxc(:)/norm2(bxc(:))
554
555 proja = abs(sum(cell%hmat(:, 1)*bxc(:)))*0.5_dp
556 projb = abs(sum(cell%hmat(:, 2)*axc(:)))*0.5_dp
557 projc = abs(sum(cell%hmat(:, 3)*axb(:)))*0.5_dp
558
559 pbc_copies(:) = 0
560 DO WHILE ((pbc_copies(1) + 1)*proja <= cutoff)
561 pbc_copies(1) = pbc_copies(1) + 1
562 END DO
563 DO WHILE ((pbc_copies(2) + 1)*projb <= cutoff)
564 pbc_copies(2) = pbc_copies(2) + 1
565 END DO
566 DO WHILE ((pbc_copies(3) + 1)*projc <= cutoff)
567 pbc_copies(3) = pbc_copies(3) + 1
568 END DO
569 pbc_copies(:) = pbc_copies(:)*cell%perd(:)
570
571 END SUBROUTINE nnp_compute_pbc_copies
572
573END MODULE nnp_cell_list
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public real_to_scaled(s, r, cell)
Transform real to scaled cell coordinates. s=h_inv*r.
Definition cell_types.F:631
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Linked-cell neighbour finder with a Verlet skin for the NNP descriptor. Owns the per-nnp cell-list ca...
subroutine, public nnp_prepare_cell_list_cache(nnp)
Prepare the cell-list cache for the current force evaluation: wrap positions into the primary cell,...
subroutine, public nnp_compute_neighbors_cell_list(nnp, neighbor, i)
Fill the ACSF neighbour buffers for one central atom by walking the linked-cell stencil around its bi...
Data types for neural network potentials.
subroutine, public nnp_env_get(nnp_env, nnp_forces, subsys, atomic_kind_set, particle_set, local_particles, molecule_kind_set, molecule_set, local_molecules, nnp_input, force_env_input, cell, cell_ref, use_ref_cell, nnp_potential_energy, virial)
Returns various attributes of the nnp environment.
Per-nnp persistent neighbour-interface state for the NNP hot path. Separates neighbour bookkeeping fr...
subroutine, public nnp_neigh_grp_grow(grp, n_needed)
Ensure a per-group (ind, dist) neighbour buffer holds n_needed entries. Called from inside the linked...
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
Linked-cell + Verlet-skin cache for the NNP descriptor neighbour walk. Persisted across MD steps so t...
Contains neighbors list of an atom (per-group dense layout).
Main data type collecting all relevant data for neural network potentials.