kups.core.neighborlist
¶
Neighbor list construction and edge representations for molecular systems.
This module provides multiple neighbor list algorithms for finding interacting pairs of particles within cutoff distances, with different performance and accuracy trade-offs.
Core Components¶
- Edges: Represents connections between particles with periodic shifts
- NearestNeighborList: Protocol for neighbor search implementations
- RefineMaskNeighborList: Applies inclusion/exclusion masks for selective interactions
Neighbor List Implementations¶
Primary Implementations¶
-
CellListNeighborList (Recommended when cutoff << box size)
- O(N) complexity using spatial hashing
- Best when cutoff / box_size < 0.3 (cutoff much smaller than box)
- Requires periodic boundary conditions
- Efficiency improves as cutoff/box ratio decreases
-
- O(N²/K) complexity (K = number of systems)
- Best when cutoff / box_size ~ 1 (cutoff comparable to box)
- Works with or without periodic boundaries
- More efficient when few cells would fit in box
-
- O(N²) complexity across all systems
- Only for single-system simulations or testing
- Crosses system boundaries (use with caution!)
Refinement Implementations¶
These allow sharing a single base neighbor list across multiple potentials with different cutoffs or interaction rules (e.g., Lennard-Jones and Coulomb).
-
- Applies inclusion/exclusion masks to precomputed edges
- Use for bonded exclusions or group-specific interactions
- No distance recalculation
- Share one neighbor list, apply different masks per potential
-
- Refines precomputed edges with new cutoff distances
- Use for multi-stage construction or adaptive cutoffs
- Recalculates distances
- Share one conservative neighbor list, apply different cutoffs per potential
Features¶
All neighbor lists handle: - Periodic boundary conditions via shift vectors - Multiple systems in parallel with segmentation - Automatic capacity management for variable neighbor counts - Integration with JAX transformations (JIT, vmap, etc.)
Choosing an Implementation¶
# When cutoff << box size (cutoff/box < 0.3)
# Example: 10 Å cutoff, 50 Å box → use CellList
nl = CellListNeighborList.new(state, lens=lens(lambda s: s.nl_params))
# When cutoff ~ box size (cutoff/box ~ 1)
# Example: 15 Å cutoff, 20 Å box → use Dense
nl = DenseNearestNeighborList.new(state, lens=lens(lambda s: s.nl_params))
# Share one neighbor list across multiple potentials with different masks
base_edges = base_nl(particles, None, cells, cutoffs, None)
lj_nl = RefineMaskNeighborList(candidates=base_edges) # Exclude 1-4 interactions
coulomb_nl = RefineMaskNeighborList(candidates=base_edges) # Different exclusions
# Share one neighbor list across potentials with different cutoffs
base_edges = base_nl(particles, None, cells, max_cutoff, None)
lj_nl = RefineCutoffNeighborList(candidates=base_edges, avg_edges=cap1) # r_cut = 10 Å
coulomb_nl = RefineCutoffNeighborList(candidates=base_edges, avg_edges=cap2) # r_cut = 15 Å
AllDenseNearestNeighborList
¶
Dense O(N²) neighbor list considering all pairs across all systems.
This implementation generates all possible particle pairs without spatial optimization. It is only suitable for very small systems or testing.
Warning: This crosses system boundaries! Only use for single-system simulations. For multiple systems, use DenseNearestNeighborList instead.
Complexity: O(N²) where N is the total number of particles across all systems.
Attributes:
| Name | Type | Description |
|---|---|---|
avg_edges |
Capacity[int]
|
Capacity manager for edge array. |
avg_image_candidates |
Capacity[int]
|
Capacity manager for image candidate pairs. |
Example
Source code in src/kups/core/neighborlist.py
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
CellListNeighborList
¶
Efficient O(N) neighbor list using spatial hashing with cell lists.
This is the recommended implementation when the cutoff is much smaller than the box size. It divides space into a grid of cells and only checks pairs in neighboring cells, achieving linear scaling with system size.
Requires periodic boundary conditions (UnitCell).
Complexity: O(N) for well-distributed particles where cutoff << box size. Efficiency improves as cutoff/box ratio decreases.
Attributes:
| Name | Type | Description |
|---|---|---|
avg_candidates |
Capacity[int]
|
Capacity for candidate pair storage (from cell list). |
avg_edges |
Capacity[int]
|
Capacity for final edge array. |
cells |
Capacity[int]
|
Capacity for cell hash table (grows with box_size³/cutoff³). |
avg_image_candidates |
Capacity[int]
|
Capacity for image candidate pairs. |
Algorithm
- Partition space into grid cells of size ~cutoff
- Hash each particle to its cell
- For each particle, check only neighboring 27 cells (3D)
- Filter candidates by actual distance
When to use
- When cutoff/box_size << 1 (cutoff much smaller than box)
- Typically cutoff/box < 0.3 for good efficiency
- Periodic boundary conditions required
Example
Source code in src/kups/core/neighborlist.py
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 | |
DenseNearestNeighborList
¶
Dense O(N²) neighbor list respecting system boundaries.
This implementation generates all particle pairs within each system separately, avoiding cross-system interactions. Efficient when the cutoff is comparable to the box size (cutoff/box ~ 1).
Complexity: O(N² / K²) where N is total particles and K is number of systems.
Attributes:
| Name | Type | Description |
|---|---|---|
avg_candidates |
Capacity[int]
|
Capacity for candidate pair storage. |
avg_edges |
Capacity[int]
|
Capacity for final edge array. |
avg_image_candidates |
Capacity[int]
|
Capacity for image candidate pairs. |
When to use
- When cutoff/box_size ~ 1 (cutoff comparable to box dimensions)
- Small box relative to cutoff (few cells would fit)
- Non-periodic systems
Example
Source code in src/kups/core/neighborlist.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 | |
Edges
¶
Bases: Sliceable
Represents edges (connections) between particles in a molecular system.
An edge connects Degree particles, where degree=2 represents pairwise
interactions (bonds), degree=3 represents three-body interactions (angles), etc.
For periodic systems, edges include shift vectors that indicate how many unit cells to traverse when computing distances between connected particles.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Degree
|
int
|
Number of particles connected by each edge (static type check) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
indices |
Index[ParticleId]
|
Particle indices for each edge, shape |
shifts |
Array
|
Periodic shift vectors, shape |
Example
Source code in src/kups/core/neighborlist.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
absolute_shifts(particles, systems)
¶
Compute absolute shift vectors for all particles in each edge.
Converts relative shifts to absolute Cartesian shift vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Table[ParticleId, HasPositionsAndSystemIndex]
|
Particle data with system index information. |
required |
systems
|
Table[SystemId, HasUnitCell]
|
System data with unit cell for periodic boundary conditions. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Source code in src/kups/core/neighborlist.py
difference_vectors(particles, systems)
¶
Compute difference vectors between connected particles.
For each edge, computes the vector from the first particle to each subsequent particle, accounting for periodic boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Table[ParticleId, HasPositionsAndSystemIndex]
|
Particle positions with system index information. |
required |
systems
|
Table[SystemId, HasUnitCell]
|
System data with unit cell for periodic boundary conditions. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Source code in src/kups/core/neighborlist.py
IsAllDenseNeighborListParams
¶
Bases: Protocol
Protocol for parameters required by AllDenseNearestNeighborList.
Source code in src/kups/core/neighborlist.py
IsCellListParams
¶
Bases: Protocol
Protocol for parameters required by CellListNeighborList.
Source code in src/kups/core/neighborlist.py
IsDenseNeighborlistParams
¶
Bases: Protocol
Protocol for parameters required by DenseNearestNeighborList.
Source code in src/kups/core/neighborlist.py
IsNeighborListState
¶
Bases: Protocol
Protocol for states that expose neighbor list parameters.
A state satisfying this protocol can be passed to from_state() on any
neighbor list class. The type parameter P determines which neighbor
list types the state can construct (e.g., IsAllDenseNeighborListParams,
IsDenseNeighborlistParams, IsCellListParams, or
IsUniversalNeighborlistParams).
Source code in src/kups/core/neighborlist.py
IsUniversalNeighborlistParams
¶
Bases: Protocol
Protocol for parameters required by any neighbor list implementation.
A superset of IsAllDenseNeighborListParams, IsDenseNeighborlistParams,
and IsCellListParams. Satisfying this protocol allows constructing any
of the three neighbor list types.
Source code in src/kups/core/neighborlist.py
NearestNeighborList
¶
Bases: Protocol
Protocol for neighbor list construction algorithms.
Implementations find pairs of particles within a cutoff distance, handling periodic boundary conditions and inclusion/exclusion masks.
Source code in src/kups/core/neighborlist.py
__call__(lh, rh, systems, cutoffs, rh_index_remap=None)
¶
Find all particle pairs within the cutoff distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lh
|
Table[ParticleId, P]
|
Left-hand particles to find neighbors for |
required |
rh
|
Table[ParticleId, P] | None
|
Right-hand particles to search within (or None for self-neighbors) |
required |
systems
|
Table[SystemId, NeighborListSystems]
|
Indexed system data with unit cell information |
required |
cutoffs
|
Table[SystemId, Array]
|
Indexed cutoff data per system |
required |
rh_index_remap
|
Index[ParticleId] | None
|
Optional index mapping rh particles back to lh
particle IDs for self-interaction exclusion. When |
None
|
Returns:
| Type | Description |
|---|---|
Edges[Literal[2]]
|
Edges connecting particle pairs within cutoff |
Source code in src/kups/core/neighborlist.py
RefineCutoffNeighborList
¶
Refine precomputed edges by re-checking distances with new cutoffs.
This neighbor list takes an existing set of candidate edges and filters them by computing actual distances and comparing to cutoffs. Enables sharing a single conservative neighbor list across multiple potentials with different cutoff distances.
Key benefit: Compute expensive neighbor list once with maximum cutoff, then refine for each potential with its specific cutoff (e.g., Lennard-Jones at 10 Å, Coulomb at 15 Å).
Attributes:
| Name | Type | Description |
|---|---|---|
candidates |
Edges[Literal[2]]
|
Precomputed edges to refine (should be conservative/over-inclusive). |
avg_edges |
Capacity[int]
|
Capacity for output edge array. |
Use cases
- Multiple potentials sharing one neighbor list with different cutoffs
- Multi-stage neighbor list construction (coarse then fine)
- Adaptive cutoffs that change during simulation
- Using a static "super" neighbor list with varying actual cutoffs
Example
# Compute base neighbor list once with maximum cutoff
max_cutoff = 15.0 # Maximum of all potential cutoffs
base_edges = base_nl(particles, None, cells, max_cutoff, None)
# Share across potentials with different cutoffs
lj_nl = RefineCutoffNeighborList(candidates=base_edges, avg_edges=cap1)
lj_edges = lj_nl(particles, None, cells, cutoff=10.0, None) # LJ cutoff
coulomb_nl = RefineCutoffNeighborList(candidates=base_edges, avg_edges=cap2)
coulomb_edges = coulomb_nl(particles, None, cells, cutoff=15.0, None) # Coulomb cutoff
Source code in src/kups/core/neighborlist.py
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 | |
RefineMaskNeighborList
¶
Refine a precomputed neighbor list by applying inclusion/exclusion masks.
This neighbor list takes an existing set of candidate edges and filters them based on segmentation masks, without recomputing distances. Enables sharing a single base neighbor list across multiple potentials with different interaction rules.
Key benefit: Compute expensive neighbor list once, apply different masks for different potentials (e.g., Lennard-Jones excludes 1-4 interactions, Coulomb has different exclusions).
Attributes:
| Name | Type | Description |
|---|---|---|
candidates |
Edges[Literal[2]]
|
Precomputed edges to refine |
Use cases
- Multiple potentials sharing one neighbor list with different exclusions
- Excluding bonded pairs (1-2, 1-3, 1-4) from non-bonded interactions
- Applying group-specific interaction rules
- Multi-scale simulations with different interaction levels
Example
# Compute base neighbor list once
base_edges = base_nl(particles, None, cells, cutoffs, None)
# Share across potentials with different masks
lj_nl = RefineMaskNeighborList(candidates=base_edges)
lj_edges = lj_nl(lj_particles, None, cells, cutoffs, None) # 1-4 exclusions
coulomb_nl = RefineMaskNeighborList(candidates=base_edges)
coulomb_edges = coulomb_nl(coulomb_particles, None, cells, cutoffs, None) # 1-2 exclusions only
Source code in src/kups/core/neighborlist.py
UniversalNeighborlistParameters
¶
Concrete parameter dataclass satisfying IsUniversalNeighborlistParams.
Holds the capacity hints needed by every neighbor list implementation.
Use the estimate() classmethod to compute reasonable initial values
from system geometry rather than guessing manually.
Attributes:
| Name | Type | Description |
|---|---|---|
avg_edges |
int
|
Average number of edges per particle (for edge capacity). |
avg_candidates |
int
|
Average number of candidate pairs per particle. |
avg_image_candidates |
int
|
Average number of image candidate pairs per particle. |
cells |
int
|
Maximum number of spatial hash cells across all systems. |
Source code in src/kups/core/neighborlist.py
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 | |
estimate(particles_per_system, systems, cutoffs, *, base=2, multiplier=1.0)
classmethod
¶
Estimate parameters for all neighbor list types from system geometry.
Computes conservative initial capacities based on particle density
and cutoff radii. The estimates are rounded up to the next power of
base to amortize future resizing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles_per_system
|
Table[SystemId, Array]
|
Number of particles per system. |
required |
systems
|
Table[SystemId, NeighborListSystems]
|
System data with unit cell information. |
required |
cutoffs
|
Table[SystemId, Array]
|
Cutoff distance per system. |
required |
base
|
float
|
Base for power-of rounding (default 2). |
2
|
multiplier
|
float
|
Safety factor applied to the estimate (default 1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
UniversalNeighborlistParameters
|
A |
Source code in src/kups/core/neighborlist.py
all_connected_neighborlist(lh, rh, systems, cutoffs, rh_index_remap=None)
¶
Neighbor list connecting all pairs sharing the same inclusion segment, ignoring distance.
Connects every particle pair that belongs to the same inclusion segment and has differing exclusion segment IDs. The cutoff is ignored for neighbor selection; the unit cell is used only to compute minimum-image shifts.
Requires max_count to be set on the inclusion Index.
Source code in src/kups/core/neighborlist.py
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 | |
basic_neighborlist(lh, rh, systems, cutoffs, rh_index_remap, *, candidate_selector, max_num_edges, max_image_candidates=None, consider_images=True)
¶
Core neighbor list construction algorithm with pluggable candidate selection.
Source code in src/kups/core/neighborlist.py
neighborlist_changes(neighborlist, lh, rh, systems, cutoffs, compaction=0.5)
¶
Compute added/removed edges from a particle change in a single call.
Appends proposed positions to the particle array and queries both old
and new interactions at once, then splits the result by filtering
edge indices into removed (before) and added (after) sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neighborlist
|
NearestNeighborList
|
Neighbor list implementation. |
required |
lh
|
Table[ParticleId, NeighborListPoints]
|
Full original particle table. |
required |
rh
|
WithIndices[ParticleId, Table[ParticleId, NeighborListPoints]]
|
Proposed changes — |
required |
systems
|
Table[SystemId, NeighborListSystems]
|
Per-system data (unit cells, etc.). |
required |
cutoffs
|
Table[SystemId, Array]
|
Per-system cutoff distances. |
required |
compaction
|
float
|
Fraction of total edges allocated per output (0–1). 0.5 means each of added/removed gets half the buffer. 1.0 means no compaction — full buffer with masking only. |
0.5
|
Returns:
| Type | Description |
|---|---|
NeighborListChangesResult
|
|
Source code in src/kups/core/neighborlist.py
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 | |