29#include "./base/base_uses.f90"
38 REAL(KIND=
dp),
PRIVATE,
PARAMETER :: default_verlet_skin = 0.5_dp
39 REAL(KIND=
dp),
PRIVATE,
PARAMETER :: rebuild_eps = 1.0e-12_dp
57 TYPE(
nnp_type),
INTENT(INOUT),
POINTER :: nnp
61 REAL(kind=
dp) :: dr2_max, exact_cutoff, list_cutoff, skin
62 REAL(kind=
dp),
DIMENSION(3) :: dr
65 CALL timeset(
'nnp_acsf_cell_list_prepare', handle)
70 exact_cutoff = nnp%max_cut
71 IF (exact_cutoff > 0.0_dp)
THEN
72 IF (nnp%verlet_skin >= 0.0_dp)
THEN
74 skin = nnp%verlet_skin
77 skin = min(default_verlet_skin, 0.1_dp*exact_cutoff)
82 list_cutoff = exact_cutoff + skin
84 CALL nnp_cell_list_ensure_coord_buffers(nnp%cell_list_cache, nnp%num_atoms)
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.)
90 cache%coord_primary(:, i), cell)
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)
103 IF ((.NOT. rebuild) .AND. (skin > 0.0_dp))
THEN
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))
110 IF (dr2_max > 0.25_dp*skin**2) rebuild = .true.
114 CALL nnp_build_cell_list_cache(cache, cell, exact_cutoff, list_cutoff, skin)
121 cache%rebuild_count = cache%rebuild_count + 1
122 ELSE IF (cache%initialized)
THEN
126 CALL nnp_cell_list_rebin_chain(cache)
130 CALL timestop(handle)
140 SUBROUTINE nnp_cell_list_ensure_coord_buffers(cache, num_atoms)
142 TYPE(nnp_cell_list_cache_type),
INTENT(INOUT) :: cache
143 INTEGER,
INTENT(IN) :: num_atoms
145 IF (
ALLOCATED(cache%coord_primary))
THEN
146 IF (
SIZE(cache%coord_primary, 2) == num_atoms)
RETURN
147 DEALLOCATE (cache%coord_primary)
149 IF (
ALLOCATED(cache%coord_scaled))
DEALLOCATE (cache%coord_scaled)
150 IF (
ALLOCATED(cache%ref_coord_primary))
DEALLOCATE (cache%ref_coord_primary)
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))
157 cache%initialized = .false.
158 cache%num_atoms = num_atoms
160 END SUBROUTINE nnp_cell_list_ensure_coord_buffers
174 SUBROUTINE nnp_build_cell_list_cache(cache, cell, exact_cutoff, list_cutoff, skin)
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
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
185 CALL nnp_cell_list_release_cell_structure(cache)
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
195 cache%ref_coord_primary(:, :) = cache%coord_primary(:, :)
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)
203 cache%image_copies = max(cache%list_pbc_copies, cell%perd)
206 cpassert(all(cache%exact_pbc_copies <= cache%image_copies))
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
214 ALLOCATE (cache%image_atom(n_images))
215 ALLOCATE (cache%image_shift(3, n_images))
216 ALLOCATE (cache%image_translation(3, n_images))
218 min_ref(:) = huge(1.0_dp)
219 max_ref(:) = -huge(1.0_dp)
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)
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(:))
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(:)
246 safe_cutoff = max(list_cutoff, rebuild_eps)
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)))
255 cache%bin_width(i) = 1.0_dp
256 cache%bin_span(i) = 0
260 cache%n_cells = product(cache%nbin)
261 ALLOCATE (cache%head(cache%n_cells))
262 ALLOCATE (cache%next(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
274 cache%initialized = .true.
276 END SUBROUTINE nnp_build_cell_list_cache
284 SUBROUTINE nnp_cell_list_rebin_chain(cache)
286 TYPE(nnp_cell_list_cache_type),
INTENT(INOUT) :: cache
288 INTEGER :: i, img, lin_idx
289 INTEGER,
DIMENSION(3) :: bin_index
290 REAL(kind=dp),
DIMENSION(3) :: ref_pos
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
304 END SUBROUTINE nnp_cell_list_rebin_chain
312 SUBROUTINE nnp_cell_list_release_cell_structure(cache)
314 TYPE(nnp_cell_list_cache_type),
INTENT(INOUT) :: cache
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)
324 cache%exact_pbc_copies = 0
325 cache%list_pbc_copies = 0
326 cache%image_copies = 0
331 cache%bin_width = 1.0_dp
333 END SUBROUTINE nnp_cell_list_release_cell_structure
348 TYPE(nnp_type),
INTENT(INOUT),
POINTER :: nnp
349 TYPE(nnp_neighbor_type),
INTENT(INOUT) :: neighbor
350 INTEGER,
INTENT(IN) :: i
352 INTEGER :: bx, by, bz, img, ind, j, neighbor_ind, &
354 INTEGER,
DIMENSION(3) :: bin_index, current_shift
355 REAL(kind=dp) :: norm
356 REAL(kind=dp),
DIMENSION(3) :: center, dr, image_pos
358 associate(cache => nnp%cell_list_cache, &
359 state => nnp%neighbor_interface_state)
362 center(:) = cache%coord_primary(:, i)
363 CALL nnp_cell_list_bin_from_position(cache, center, bin_index)
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]))
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)
379 IF (.NOT. nnp_cell_list_exact_image_match(cache, i, j, current_shift))
THEN
380 img = cache%next(img)
384 image_pos(:) = cache%coord_primary(:, j) + cache%image_translation(:, img)
385 dr(:) = center(:) - image_pos(:)
387 neighbor_ind = nnp%ele_ind(j)
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))
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
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))
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
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))
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
436 img = cache%next(img)
454 LOGICAL FUNCTION nnp_cell_list_exact_image_match(cache, i, j, image_shift)
456 TYPE(nnp_cell_list_cache_type),
INTENT(IN) :: cache
457 INTEGER,
INTENT(IN) :: i, j
458 INTEGER,
DIMENSION(3),
INTENT(IN) :: image_shift
460 INTEGER :: d, minimal_shift
462 nnp_cell_list_exact_image_match = .true.
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.
470 ELSE IF (image_shift(d) /= 0)
THEN
471 nnp_cell_list_exact_image_match = .false.
476 END FUNCTION nnp_cell_list_exact_image_match
484 SUBROUTINE nnp_cell_list_bin_from_position(cache, pos, bin_index)
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
491 REAL(kind=dp) :: scaled
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)))
499 END SUBROUTINE nnp_cell_list_bin_from_position
507 INTEGER FUNCTION nnp_cell_list_linear_index(cache, bin_index)
509 TYPE(nnp_cell_list_cache_type),
INTENT(IN) :: cache
510 INTEGER,
DIMENSION(3),
INTENT(IN) :: bin_index
512 nnp_cell_list_linear_index = bin_index(1) + cache%nbin(1)* &
513 ((bin_index(2) - 1) + cache%nbin(2)*(bin_index(3) - 1))
515 END FUNCTION nnp_cell_list_linear_index
527 SUBROUTINE nnp_compute_pbc_copies(pbc_copies, cell, cutoff)
529 INTEGER,
DIMENSION(3),
INTENT(OUT) :: pbc_copies
530 TYPE(cell_type),
INTENT(IN),
POINTER :: cell
531 REAL(kind=dp),
INTENT(IN) :: cutoff
533 REAL(kind=dp) :: proja, projb, projc
534 REAL(kind=dp),
DIMENSION(3) :: axb, axc, bxc
538 cpassert(cell%deth > 0.0_dp)
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(:))
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(:))
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(:))
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
560 DO WHILE ((pbc_copies(1) + 1)*proja <= cutoff)
561 pbc_copies(1) = pbc_copies(1) + 1
563 DO WHILE ((pbc_copies(2) + 1)*projb <= cutoff)
564 pbc_copies(2) = pbc_copies(2) + 1
566 DO WHILE ((pbc_copies(3) + 1)*projc <= cutoff)
567 pbc_copies(3) = pbc_copies(3) + 1
569 pbc_copies(:) = pbc_copies(:)*cell%perd(:)
571 END SUBROUTINE nnp_compute_pbc_copies
Handles all functions related to the CELL.
subroutine, public real_to_scaled(s, r, cell)
Transform real to scaled cell coordinates. s=h_inv*r.
Defines the basic variable types.
integer, parameter, public dp
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.
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.