kups.core.cell
¶
Simulation cell representations.
This module separates two concepts that other simulation codes usually conflate into one "cell" object:
-
Frame — pure geometry. A 3D parallelepiped defined by three basis vectors. No periodicity, no boundary semantics. What ASE calls
cell, OpenMM callsboxVectors, LAMMPS calls the simulation box; in crystallography the basis vectors of a periodic structure are conventionally called lattice vectors.Framesubsumes all of these — a Frame just describes a parallelepiped, and the meaning (periodic-translation vector vs bounding-box edge) is supplied by the cell type that wraps it. -
Cell — frame plus per-axis boundary semantics. Decides whether the frame is interpreted as a periodic unit cell or a bounding domain on each axis.
Why split: the same parallelepiped means different things in different
contexts. A 30 Å cubic frame inside a PeriodicCell
is a periodic unit cell — particles wrap, neighbor searches honour minimum
image. The same frame inside a VacuumCell is
the bounding box of a finite domain — no wrapping, neighbor searches treat
it as a spatial-partitioning hint. Naming the geometry "Lattice" or "Cell"
would prejudice the reading toward the periodic case; Frame is
boundary-condition-agnostic and reads equally honestly in both.
Frame implementations¶
- OrthogonalFrame — 3 DOF, axes-aligned parallelepiped parameterized by side lengths. Diagonal fast paths for volume, inverse, and coordinate transforms.
- TriclinicFrame — 6 DOF, general parallelepiped parameterized by the lower-triangular elements of the basis matrix.
- MaterializedFrame — caches
vectors,inverse_vectors, andvolumeas concrete arrays. Produced byFrame.materialize; useful when the same frame is queried many times or when the inverse needs to be cached across a JIT boundary.
Both expose vectors (the basis matrix —
this is what crystallography calls lattice vectors),
inverse_vectors,
volume,
perpendicular_lengths,
to_fractional /
to_real coordinate transforms, plus
tile for per-axis multiplicity tiling and
__mul__ for uniform scaling.
Cell implementations¶
- PeriodicCell — all three axes periodic
(literal
(True, True, True)). The frame is the unit cell of a periodic crystal or fluid. - VacuumCell — all three axes open (literal
(False, False, False)). The frame is the bounding parallelepiped of a finite simulation domain.
Cell is generic over the periodicity literal P
so consumers can narrow statically on the boundary axis. An Ewald path
that requires periodic boundaries declares Cell[Periodic3D] and
pyright rejects VacuumCell at the call site:
def ewald(cell: Cell[Periodic3D]) -> Energy: ...
ewald(PeriodicCell(frame)) # OK
ewald(VacuumCell(frame)) # pyright: "Vacuum is not assignable to Periodic3D"
Cell re-exposes the frame's geometric properties as passthrough so callers
can write cell.volume, cell.vectors etc. without going through
cell.frame. For frame-specific fields (OrthogonalFrame.lengths,
TriclinicFrame.tril / angles), narrow with isinstance:
The same Frame instance can be wrapped in either cell type:
frame = OrthogonalFrame(lengths=jnp.array([20., 20., 20.]))
PeriodicCell(frame) # 20 Å cubic crystal — particles wrap
VacuumCell(frame) # 20 Å bounding box of a cluster — no wrap
Convention¶
Frame vectors follow the row convention: r_real = r_frac @ frame.vectors.
Cell
¶
Bases: Sliceable
A Frame plus per-axis boundary semantics.
Generic over the periodicity literal P (a length-3 tuple of
booleans). PeriodicCell and
VacuumCell are subclasses that pin P
to a literal-typed default; for slab and wire geometries, construct
Cell(frame, periodic=mask) directly — the literal tuple narrows
P to the corresponding [Slab2D][kups.core.cell.Slab2D] or
[Wire1D][kups.core.cell.Wire1D] alias.
The cell delegates geometry queries (volume, vectors, etc.) to
its frame. Periodic-mask-aware operations
([wrap][kups.core.cell.Cell.wrap], fold,
minimum_image_shifts)
live on the cell so callers don't need to access cell.periodic
directly.
Source code in src/kups/core/cell.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | |
fold(r_frac)
¶
Fold fractional coords into [0, 1) on periodic axes; non-periodic
axes pass through unchanged.
Returns (folded, in_cell) where in_cell is a per-particle
mask, True where the folded coords lie in [0, 1) on every
axis. For fully-periodic cells, in_cell is trivially True
after folding; for cells with non-periodic axes, particles that
leaked out of the box on those axes are flagged False.
Used by neighbor-list spatial hashing; complementary to
[wrap][kups.core.cell.Cell.wrap] which uses the [-0.5, 0.5)
convention.
Source code in src/kups/core/cell.py
from_pbc(frame, pbc)
staticmethod
¶
Construct the cell flavor matching pbc.
Returns PeriodicCell for
(True, True, True), VacuumCell
for (False, False, False), and a generic Cell[P] carrying
the runtime mask for slab and wire geometries (P is inferred
from the literal tuple).
Source code in src/kups/core/cell.py
minimum_image_shifts(deltas)
¶
Per-axis minimum-image shifts for fractional separations.
Returns round(deltas) on periodic axes (the closest integer
cell offset that wraps the separation to its minimum image) and
0 on non-periodic axes.
Source code in src/kups/core/cell.py
CoordinateSpace
¶
Bases: Enum
Enumeration for coordinate systems.
Attributes:
| Name | Type | Description |
|---|---|---|
REAL |
Cartesian coordinates in Angstroms. |
|
FRACTIONAL |
Scaled coordinates in [0, 1) relative to frame vectors. |
Source code in src/kups/core/cell.py
Frame
¶
Bases: Protocol
3D parallelepiped geometry, no periodicity attached.
A Frame is a pure geometric container — three basis vectors that span a parallelepiped in 3D space. It does not commit to whether those vectors represent periodic translations or just bounding-box edges; that distinction is supplied by the Cell type that wraps a Frame.
In crystallography these basis vectors are conventionally called
"lattice vectors". They are exposed here under the name
vectors because the crystallographic
label implies a periodic interpretation that does not apply to all
Frame uses (e.g. the bounding box of a vacuum simulation).
Concrete implementations:
- OrthogonalFrame: 3 DOF (lengths).
- TriclinicFrame: 6 DOF (lower-triangular).
Source code in src/kups/core/cell.py
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 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 | |
inverse_vectors
property
¶
Matrix inverse of vectors,
used to convert real-space coordinates to fractional, shape
(..., 3, 3).
perpendicular_lengths
property
¶
Perpendicular distance between opposing faces, per axis,
shape (..., 3). Used by neighbor-list cutoff checks and by
min_multiplicity.
vectors
property
¶
Basis vectors of the parallelepiped, shape (..., 3, 3).
Rows are the basis vectors. Lower-triangular by convention so that
v[0] lies along x, v[1] in the xy-plane, v[2] general.
Crystallography calls this matrix the lattice vectors.
volume
property
¶
Volume of the parallelepiped, shape (...).
__mul__(other)
¶
from_matrix(vecs)
classmethod
¶
Construct from a (..., 3, 3) basis matrix.
Projects the input onto the frame's parameter space — entries
not represented by the parameterisation are discarded
(OrthogonalFrame keeps the diagonal; TriclinicFrame
keeps the lower-triangular block). Used to wrap a generic
∂E/∂h matrix back into the same frame type as an input cell.
Source code in src/kups/core/cell.py
materialize()
¶
Return a MaterializedFrame
with vectors, inverse_vectors and volume evaluated
and stored as concrete arrays.
Use this to avoid recomputing the inverse and determinant when the same frame is queried many times, or to lift these arrays across a JIT boundary so downstream callers don't need to know the frame's parametrisation.
Requires at least one leading batch dim so that vectors
(B, ..., 3, 3), inverse_vectors (B, ..., 3, 3) and
volume (B, ...) share a leading axis (see
MaterializedFrame).
Source code in src/kups/core/cell.py
tile(multiplicities)
¶
Per-axis integer scaling. Used by make_supercell to build supercells.
to_fractional(r)
¶
MaterializedFrame
¶
Bases: Sliceable
Frame that stores its basis matrix, inverse, and volume as concrete arrays.
Produced by Frame.materialize.
Useful when the same frame is queried repeatedly (no recomputation
of the inverse or determinant) or when the arrays need to cross a
JIT boundary independently of the frame's original parametrisation.
The stored matrices are assumed to follow the lower-triangular convention shared by every other Frame implementation; coordinate transforms use triangular_3x3_matmul accordingly. Manually constructing this frame with non-triangular vectors violates the convention.
All three fields are pytree leaves and must share a leading batch
dim B to satisfy the Sliceable
contract — unbatched single-frame inputs must be wrapped with an
explicit [None] axis before being passed in.
Attributes:
| Name | Type | Description |
|---|---|---|
vectors |
Array
|
Basis matrix, shape |
inverse_vectors |
Array
|
Matrix inverse of |
volume |
Array
|
Absolute determinant of |
Source code in src/kups/core/cell.py
OrthogonalFrame
¶
Bases: Sliceable
Axis-aligned Frame with 3 degrees of freedom.
Parameterized by the three side lengths. Exploits the diagonal structure for cheaper volume, inverse, and coordinate-transform operations than the general triclinic path. Use this when the simulation domain has perpendicular axes (cubic, tetragonal, or orthorhombic crystals; standard rectangular MD boxes).
Attributes:
| Name | Type | Description |
|---|---|---|
lengths |
Array
|
Box side lengths |
Source code in src/kups/core/cell.py
from_matrix(vecs)
classmethod
¶
Construct from a diagonal basis matrix, shape (..., 3, 3).
Projects the input matrix onto the orthogonal subspace by taking
its diagonal — off-diagonal entries are discarded, matching the
3-parameter (Lx, Ly, Lz) representation.
Source code in src/kups/core/cell.py
PeriodicCell
¶
Bases: Cell[Periodic3D]
Cell that is periodic along all three axes.
The frame is interpreted as a unit cell — a tile of a periodic
crystal or fluid. wrap folds coordinates into the primary unit
cell; the neighbor list applies the minimum-image convention; the
Ewald summation requires this cell type.
Source code in src/kups/core/cell.py
TriclinicFrame
¶
Bases: Sliceable
General triclinic Frame with 6 degrees of freedom.
Stores the 6 independent elements of the lower-triangular basis matrix. Vectors are a linear function of these parameters, making them suitable for gradient-based optimization (e.g. NPT cell-vector relaxation).
Use this when the simulation domain has non-orthogonal axes (monoclinic / triclinic crystals, sheared MD boxes). For axis-aligned domains, OrthogonalFrame is cheaper.
Attributes:
| Name | Type | Description |
|---|---|---|
tril |
Array
|
Lower-triangular elements |
Source code in src/kups/core/cell.py
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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
from_lengths_and_angles(lengths, angles)
classmethod
¶
Construct from crystallographic parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lengths
|
Array
|
Lattice lengths |
required |
angles
|
Array
|
Lattice angles |
required |
Source code in src/kups/core/cell.py
from_matrix(vecs)
classmethod
¶
Construct from a lower-triangular basis matrix, shape (..., 3, 3).
TriclinicMap
¶
Bases: Protocol
Mapping that takes positions in an arbitrary frame and returns them in the lower-triangular triclinic frame produced by to_lower_triangular.
Source code in src/kups/core/cell.py
VacuumCell
¶
Bases: Cell[Vacuum]
Cell with all three axes open.
The frame is interpreted as the bounding parallelepiped of a finite
simulation domain — a cluster, an isolated molecule, a gas-phase
sample. wrap is a no-op; long-range electrostatics use direct
pairwise sums (no Ewald). The frame is still required because the
neighbor-list machinery needs a spatial-partitioning hint.
Source code in src/kups/core/cell.py
is_3d_periodic(cell)
¶
is_vacuum(cell)
¶
make_supercell(cell, multiplicities, to_replicate, to_shift)
¶
Replicate a cell along each periodic axis.
Tiles the cell according to multiplicities (clamped to 1 on
non-periodic axes), replicates the data, and shifts coordinates into
the expanded cell using periodic wrapping. The returned cell has the
same concrete type as the input.
Source code in src/kups/core/cell.py
min_multiplicity(cell, cutoff)
¶
Minimum supercell replication per axis for a given cutoff.
Returns 1 for non-periodic axes (no replication needed).
Source code in src/kups/core/cell.py
to_lower_triangular(vecs)
¶
Convert arbitrary basis vectors to lower-triangular form via QR.
The returned basis has a positive diagonal. The coordinate mapper applies the same rigid rotation to positions, preserving fractional coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vecs
|
Array
|
Basis vectors as rows of a 3x3 matrix, shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, TriclinicMap]
|
Tuple of (lower_triangular_vectors, coordinate_rotation_fn). |