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.
Call Contract¶
Neighbor lists are called as neighborlist(keys, systems, *, queries=None,
queried_keys=None). keys is the self-graph/output table. Use keyword-only
queries only for true bipartite queries. Use keyword-only queried_keys
only for self-graph updates; it is mutually exclusive with queries and names
affected keys ids after the caller has already written updated particle data
into keys.
Core Components¶
- Edges: Represents connections between particles with periodic shifts
- NeighborList: Protocol for neighbor search implementations
- Pipeline: Selector → mask sequence → compactor → postprocessors
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)
- Honors the cell's per-axis
periodicmask (bulk and bounded non-periodic)
-
- O(N²/K) complexity (K = number of systems)
- Best when cutoff / box_size ~ 1 (cutoff comparable to box)
-
- O(N²) complexity across all systems
- Only for single-system simulations or testing
- Crosses system boundaries (use with caution!)
Refinement Implementations¶
These let one expensive base neighbor list be shared across multiple potentials.
- RefineMaskNeighborList: apply different inclusion/exclusion masks to precomputed edges.
- RefineCutoffNeighborList: refine precomputed edges with new cutoff distances.
Cutoff-Free Implementations¶
These cover non-cutoff cases under the same NeighborList[D] protocol.
- EmptyNeighborList: emits a
zero-row
Edges[D]for point-cloud constructions. - FixedEdgesNeighborList:
stores fixed edge topology for bonded edge sets supplied by the state and
computes current periodic shifts during calls. Affected self-graph calls use
queried_keysand return only rows touched by those affectedkeysids.
Pipeline Primitives¶
Every neighbor list above is a Pipeline
of a CandidateSelector, a
tuple of Mask criteria, a
Compactor, and zero or more
Postprocessor transforms. Users
wanting custom behavior can compose their own pipeline directly.
AdaptiveNeighborList
¶
Bases: NeighborList[Literal[2]]
Neighbor list that dispatches to the cheapest backing implementation.
Holds (implementation, cost) pairs and, on each call, picks the
implementation whose :class:NeighborListCost is smallest for that call's
counts. Augment by appending pairs to :attr:implementations; the seeded
implementations share the UniversalNeighborlistParameters capacities, so
growing one grows the shared state.
Attributes:
| Name | Type | Description |
|---|---|---|
implementations |
tuple[NeighborListCandidate, ...]
|
Candidate :class: |
Source code in src/kups/core/neighborlist/adaptive.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 | |
from_state(state, cutoffs)
classmethod
¶
Seed from a state exposing neighborlist_params.
Source code in src/kups/core/neighborlist/adaptive.py
new(state, lens, cutoffs)
classmethod
¶
Seed the library implementations with their default cost guesses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
S
|
Object exposing |
required |
lens
|
Lens[S, IsUniversalNeighborlistParams]
|
Lens focusing the shared |
required |
cutoffs
|
Table[SystemId, Array]
|
Per-system cutoffs bound onto each implementation. |
required |
Returns:
| Type | Description |
|---|---|
AdaptiveNeighborList
|
An |
Source code in src/kups/core/neighborlist/adaptive.py
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. |
cutoffs |
Table[SystemId, Array]
|
Per-system cutoff distances used by this neighbor list. |
Example
Source code in src/kups/core/neighborlist/all_dense.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 | |
AllDenseSelector
¶
Selector that emits every (i, j) pair across all systems.
Source code in src/kups/core/neighborlist/all_dense.py
CandidateBatch
¶
Candidate set of degree D carried through the pipeline.
Reuses Edges[D] for the
(indices, shifts) layout (indices shape (n, D),
shifts shape (n, D-1, 3)); adds the
is_minimum_image flag that
ExclusionMask needs to
keep non-minimum periodic copies of excluded pairs.
Attributes:
| Name | Type | Description |
|---|---|---|
edges |
Edges[D]
|
Candidate edges (indices + fractional shifts). The first column
is keyed by |
is_minimum_image |
Array
|
|
query_keys |
tuple[ParticleId, ...] | None
|
Pair-specific key vocabulary for the second edge column.
|
Source code in src/kups/core/neighborlist/types.py
CandidateSelector
¶
Bases: Protocol
Produces a CandidateBatch[D] from the pipeline context.
Owns all candidate-set construction, including any PBC image
replication required when max(cutoff/perp_axis) > 0.5.
Source code in src/kups/core/neighborlist/types.py
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.
Honors the cell's per-axis periodic mask: stencil offsets that cross a
non-periodic face are routed to an out-of-bounds bin (no key matches), and
minimum-image shifts are zero on non-periodic axes. The fully-periodic path
is byte-identical to the original (gated at trace time on all(periodic))
so PBC kernels see no overhead.
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. |
cutoffs |
Table[SystemId, Array]
|
Per-system cutoff distances used by this neighbor list. |
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
- On non-periodic axes positions must lie inside
[0, L)in real coordinates (the caller's invariant; out-of-range positions are silently routed to the OOB bin)
Example
Source code in src/kups/core/neighborlist/cell_list.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
CellListSelector
¶
Selector for the cell-list algorithm.
Calls the raw spatial-hash candidate emission, then replicates per image
multiplicity when max(cutoff/perp) > 0.5.
Source code in src/kups/core/neighborlist/cell_list.py
Compactor
¶
Bases: Protocol
Produces compacted Edges[D] from the accumulated keep mask.
Source code in src/kups/core/neighborlist/types.py
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. |
cutoffs |
Table[SystemId, Array]
|
Per-system cutoff distances used by this neighbor list. |
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/dense.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 | |
DenseSelector
¶
Selector for the per-system dense O(N²/K²) algorithm.
Source code in src/kups/core/neighborlist/dense.py
DistanceCutoffMask
¶
Drops candidates whose squared real-space distance exceeds cutoff².
Source code in src/kups/core/neighborlist/masks.py
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 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/edges.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
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, HasCell[AnyPeriodicity]]
|
System data with cell for periodic boundary conditions. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Source code in src/kups/core/neighborlist/edges.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, HasCell[AnyPeriodicity]]
|
System data with cell for periodic boundary conditions. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Source code in src/kups/core/neighborlist/edges.py
EmptyNeighborList
¶
Neighbor list that emits an :class:Edges[D] with zero rows.
The degree field is the runtime arity carried by the emitted edges;
it must match the type parameter D.
Attributes:
| Name | Type | Description |
|---|---|---|
degree |
int
|
Edge arity ( |
Source code in src/kups/core/neighborlist/fixed.py
ExclusionMask
¶
Drops minimum-image pairs that share an exclusion segment.
Non-minimum-image periodic copies of excluded pairs survive (allowed when
batch.is_minimum_image is False for that copy).
Source code in src/kups/core/neighborlist/masks.py
FixedEdgesNeighborList
¶
Neighbor list for a fixed topology edge set.
Full self-graph calls return all fixed topology rows with shifts computed
from the current particle positions. Affected self-graph calls pass
keyword-only queried_keys after updated particle data has been written
into keys; the neighbor list returns only fixed rows touched by those
affected keys ids. queries is reserved for true bipartite
neighbor-list implementations and is not a fixed-edge update mechanism.
Attributes:
| Name | Type | Description |
|---|---|---|
indices |
Index[ParticleId]
|
Fixed edge topology. Shifts are intentionally not stored; they are computed from the call's current particle positions. |
avg_edges |
Capacity[int] | None
|
Update-only average affected-edge capacity per affected
|
Source code in src/kups/core/neighborlist/fixed.py
InBoundsMask
¶
Drops candidates whose key/query indices fall outside the valid inclusion-segment range.
Implements the per-side inclusion.indices < num_labels check used to
guard scatter/gather lookups when the candidate buffer is padded.
Source code in src/kups/core/neighborlist/masks.py
InclusionGroupSelector
¶
Pairs every particle with every other in the same inclusion segment.
Ignores the cutoff entirely. Shifts are int-typed minimum-image
fractional rounds — matches today's all_connected_neighborlist
(which is Ewald-only and assumed fully periodic).
Source code in src/kups/core/neighborlist/all_connected.py
InclusionMatchMask
¶
Drops candidates whose key/query inclusion segments differ.
Source code in src/kups/core/neighborlist/masks.py
IsAllDenseNeighborListParams
¶
Bases: Protocol
Protocol for parameters required by AllDenseNearestNeighborList.
Source code in src/kups/core/neighborlist/all_dense.py
IsCellListParams
¶
Bases: Protocol
Protocol for parameters required by CellListNeighborList.
Source code in src/kups/core/neighborlist/cell_list.py
IsDenseNeighborlistParams
¶
Bases: Protocol
Protocol for parameters required by DenseNearestNeighborList.
Source code in src/kups/core/neighborlist/dense.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/types.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/types.py
Mask
¶
Bases: Protocol
Returns this criterion's bool array; pipeline conjuncts the results.
The degree parameter tracks which candidate arity the mask accepts. Pair
masks implement Mask[Literal[2]] by annotating their batch argument
as CandidateBatch[Literal[2]]; degree-agnostic masks use a generic
__call__ method.
Cannot change batch.edges, batch.is_minimum_image, or the
candidate count. Pure (batch, ctx) -> Array.
Source code in src/kups/core/neighborlist/types.py
MaskOnlyCompactor
¶
Bases: Compactor[D]
In-place compaction: failing entries become OOB indices and zero shifts.
No size change; preserves the candidate count from the selector. Pair candidates are already in their output index space.
Source code in src/kups/core/neighborlist/compact.py
MirrorPairEdges
¶
Bases: Postprocessor[Literal[2]]
Append reversed pair edges for undirected graph outputs.
The default mirrors only self-graph update calls selected by
ctx.queried_keys. Full self-neighbor calls already emit both directions
before compaction, while queried_keys calls operate on affected ids in
the already-updated keys table and are deduplicated by
QueriedKeysDedupMask. Their reverse edges are restored after compaction.
Attributes:
| Name | Type | Description |
|---|---|---|
only_when_queried_keys |
bool
|
When |
Source code in src/kups/core/neighborlist/postprocess.py
NeighborList
¶
Bases: Protocol
Protocol for neighbor list construction algorithms.
Implementations find groups of particles within a cutoff distance, handling periodic boundary conditions and inclusion/exclusion masks. The degree parameter tracks the arity of the emitted edge tuples.
Source code in src/kups/core/neighborlist/types.py
__call__(keys, systems, *, queries=None, queried_keys=None)
¶
Find particle groups for a self-graph or bipartite query.
queries and queried_keys are mutually exclusive; the overloads
above reject calls that pass both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Table[ParticleId, P]
|
Particle table that the returned |
required |
systems
|
Table[SystemId, NeighborListSystems]
|
Indexed system data with cell information. |
required |
queries
|
Table[ParticleId, P] | None
|
Optional bipartite query table. Each edge then connects a
|
None
|
queried_keys
|
Index[ParticleId] | None
|
Optional subset of |
None
|
Returns:
| Type | Description |
|---|---|
Edges[D]
|
Edges whose columns index |
Source code in src/kups/core/neighborlist/types.py
NeighborListCandidate
¶
A backing neighbor list paired with its cost estimator.
Attributes:
| Name | Type | Description |
|---|---|---|
neighborlist |
NeighborList[Literal[2]]
|
The candidate implementation. |
cost |
NeighborListCost
|
Estimates the implementation's cost for a call's counts. |
Source code in src/kups/core/neighborlist/adaptive.py
NeighborListCost
¶
Bases: Protocol
Estimates the relative runtime cost of a neighbor list for one call.
Lower is cheaper; :class:AdaptiveNeighborList dispatches to the minimum.
Return math.inf to mark an implementation invalid for the given shape.
Source code in src/kups/core/neighborlist/adaptive.py
NeighborListFactory
¶
Bases: Protocol
Constructs a pair :class:NeighborList for a given state and cutoffs.
Used by radius-based potential factories so the construction strategy
can be swapped without coupling the potential to a concrete
neighbor-list class. The library default is
:meth:kups.core.neighborlist.AdaptiveNeighborList.from_state,
which is contravariant-compatible with any state satisfying
:class:IsNeighborListState.
The State type parameter is contravariant (it appears only in
input position in __call__), so a factory written against a
broader state protocol can be passed where a narrower one is expected.
Source code in src/kups/core/neighborlist/types.py
Pipeline
¶
Selector → mask sequence → compactor → postprocessors.
Attributes:
| Name | Type | Description |
|---|---|---|
selector |
CandidateSelector[D]
|
Produces a |
masks |
tuple[Mask[D], ...]
|
Tuple of mask criteria over |
compactor |
Compactor[D]
|
Produces compacted |
postprocessors |
tuple[Postprocessor[D], ...]
|
Edge transforms applied sequentially after compaction. |
Source code in src/kups/core/neighborlist/pipeline.py
PipelineContext
¶
Read-only inputs shared by every mask and the compactor.
Positions in keys and optional queries are in fractional
coordinates (transformed by
[_prepare][kups.core.neighborlist.pipeline._prepare]). There is no
out_of_bounds field — masks/compactors that need an OOB sentinel resolve
the query table first.
Attributes:
| Name | Type | Description |
|---|---|---|
keys |
Table[ParticleId, NeighborListPoints]
|
Key particle table in fractional coords (the table the returned
|
queries |
Table[ParticleId, NeighborListPoints] | None
|
Query particle table in fractional coords for true bipartite
queries. |
systems |
Table[SystemId, NeighborListSystems]
|
Indexed system data with cell information. |
queried_keys |
Array | None
|
Raw |
Source code in src/kups/core/neighborlist/types.py
edge_query_table
property
¶
Table addressed by the second edge column.
query_table
property
¶
Table used to enumerate query candidates.
queries is reserved for true bipartite calls. queried_keys
selects a self-graph update subset from keys and is lifted back to
keys index space before masks and compaction see the batch.
Postprocessor
¶
Bases: Protocol
Transforms compacted edges using the pipeline context.
Postprocessors run sequentially after compaction. They may change the
number of rows, but must preserve the edge degree D.
Source code in src/kups/core/neighborlist/types.py
PrecomputedEdgesSelector
¶
Selector that wraps precomputed Edges for both refine variants.
Precomputed self-graph edges are already in keys space. A disjoint
bipartite queries call may still use query positions for the second
column, matching the edge convention of the original candidate set.
Attributes:
| Name | Type | Description |
|---|---|---|
candidates |
Edges[Literal[2]]
|
Precomputed edges (indices in keys-space). |
recompute_mic_shifts |
bool
|
When |
Source code in src/kups/core/neighborlist/refine.py
QueriedKeysDedupMask
¶
Deduplicate self-graph update candidates.
Pair selectors emit candidates in keys space. When ctx.queried_keys
is set, the query side was restricted to those affected keys rows.
We keep edges whose key endpoint is unaffected, plus one orientation for
edges where both endpoints are affected. MirrorPairEdges restores the
reverse orientation after compaction.
Returns all-True for full self-graphs and bipartite queries.
Source code in src/kups/core/neighborlist/masks.py
ReduceCompactor
¶
Bases: Compactor[D]
Compact surviving candidates to a size-bounded Edges[D].
Compacts whole candidate rows, so pair neighbor lists and fixed higher-degree topology share the same implementation.
Source code in src/kups/core/neighborlist/compact.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. |
cutoffs |
Table[SystemId, Array]
|
Per-system cutoff distances used by this refinement. |
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, cells)
# Share across potentials with different cutoffs
lj_nl = RefineCutoffNeighborList(
candidates=base_edges, avg_edges=cap1, cutoffs=lj_cutoffs
)
lj_edges = lj_nl(particles, cells) # LJ cutoff
coulomb_nl = RefineCutoffNeighborList(
candidates=base_edges, avg_edges=cap2, cutoffs=coulomb_cutoffs
)
coulomb_edges = coulomb_nl(particles, cells) # Coulomb cutoff
Source code in src/kups/core/neighborlist/refine.py
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
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, cells)
# Share across potentials with different masks
lj_nl = RefineMaskNeighborList(candidates=base_edges)
lj_edges = lj_nl(lj_particles, cells) # 1-4 exclusions
coulomb_nl = RefineMaskNeighborList(candidates=base_edges)
coulomb_edges = coulomb_nl(coulomb_particles, cells) # 1-2 exclusions only
Source code in src/kups/core/neighborlist/refine.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 | |
TouchesQueriedKeysMask
¶
Keep fixed-topology rows touched by ctx.queried_keys.
When no affected-index subset is active, every row is kept. This lets fixed topology use the same pipeline for full and patch-shaped calls.
Source code in src/kups/core/neighborlist/masks.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 candidate pairs per particle after
periodic-image replication (equals |
cells |
int
|
Maximum number of spatial hash cells across all systems. |
Source code in src/kups/core/neighborlist/parameters.py
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 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/parameters.py
all_connected_neighborlist(keys, systems, *, queries=None, queried_keys=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 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/all_connected.py
all_dense_cost(num_particles, num_systems)
¶
Default cost for :class:AllDenseNearestNeighborList.
O(N^2) across all particles, so it ties dense for a single system and is
invalid (inf) for multiple systems, which it would incorrectly merge.
Source code in src/kups/core/neighborlist/adaptive.py
cell_list_cost(num_particles, num_systems)
¶
Default cost for :class:CellListNeighborList (O(N) with a large
constant, crossing dense at _CELL_LIST_CROSSOVER particles per system).
Source code in src/kups/core/neighborlist/adaptive.py
dense_cost(num_particles, num_systems)
¶
Default cost for :class:DenseNearestNeighborList (O(N^2/K)).
neighborlist_changes(neighborlist, lh, rh, systems, 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
|
NeighborList[Literal[2]]
|
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 (cells, etc.). |
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
|
|