pyLEAFS.grid module

Shared spatial grid.

The domain is a D-dimensional box partitioned into a regular array of cubical regions of side length L, with periodic (toroidal) boundaries. Every field and population in a simulation references one Grid, so geometry, neighbour lookups, and coordinate conventions are defined in exactly one place.

The grid is dimension-agnostic: D = 2 or D = 3 is chosen by the length of the shape argument. Positions are always (n, D) arrays in physical units; region indices are (n, D) integer arrays.

class pyLEAFS.grid.Grid(shape, L=1.0)[source]

Bases: object

A toroidal box partitioned into cubical regions.

Parameters:
  • shape (tuple of int) – Number of regions along each axis, e.g. (5, 5) for a 2d grid or (5, 5, 5) for 3d. Its length sets the dimension D.

  • L (float, optional) – Side length of one region in physical units (default 1.0).

shape

Regions per axis.

Type:

tuple of int

D

Spatial dimension.

Type:

int

L

Region side length.

Type:

float

n_regions

Total number of regions, prod(shape).

Type:

int

extent

Physical size of the domain along each axis (shape * L).

Type:

ndarray of float, shape (D,)

offsets

Moore-neighbourhood offsets, the centre cell (0, ..., 0) first.

Type:

ndarray of int, shape (3**D, D)

Examples

>>> g = Grid((5, 5), L=10.0)
>>> g.D, g.n_regions
(2, 25)
>>> g.region_of(np.array([[12.0, 3.0]]))
array([[1, 0]])
displacement(a, b)[source]

Minimum-image displacement b - a under periodic boundaries.

Parameters:
  • a (ndarray, shape (n, D)) – Physical positions.

  • b (ndarray, shape (n, D)) – Physical positions.

Returns:

The shortest displacement from a to b on the torus, each component in [-extent/2, extent/2).

Return type:

ndarray, shape (n, D)

neighbour_ids(region_idx)[source]

Linear ids of the Moore neighbourhood of each region.

Parameters:

region_idx (ndarray of int, shape (n, D)) – Per-axis region indices.

Returns:

Linear region ids of each region’s Moore neighbourhood, the region itself in column 0, wrapped toroidally.

Return type:

ndarray of int, shape (n, 3**D)

region_id(region_idx)[source]

Flatten (n, D) region indices to linear ids (n,).

Uses row-major (C) order consistent with numpy.ravel_multi_index.

region_of(pos)[source]

Return the region index (n, D) containing each position.

Parameters:

pos (ndarray, shape (n, D)) – Physical positions (wrapped internally).

Returns:

Per-axis region indices in [0, shape[axis]).

Return type:

ndarray of int, shape (n, D)

wrap(pos)[source]

Wrap physical positions into the domain by the toroidal boundary.

Parameters:

pos (ndarray, shape (n, D)) – Physical positions.

Returns:

Positions wrapped into [0, extent) along each axis.

Return type:

ndarray, shape (n, D)