Periodic boxes¶
Any axis of the grid can be periodic, as in a simulation box: distances
along it are measured the short way round, as if the box repeated without end
along that axis. This is the minimum image convention: of all the periodic
images of an object, a query considers only the nearest, and so finds each
object at most once. Below, x, y, z are n = 5,000 uniform points in the
unit cube.
# Two objects on opposite sides of the x faces, 0.004 apart through the boundary
# and 0.996 apart across the box.
left, right = n, n + 1
x = np.append(x, [0.002, 0.998])
y = np.append(y, [0.5, 0.5])
z = np.append(z, [0.5, 0.5])
# A periodic axis needs its limits: they are the box, and the period is hi-lo.
# Coordinates are not wrapped, so every point must already lie within them.
# The periods must be whole multiples of the shortest one. periodic is a bool
# for all three axes or one bool per axis, so a slab periodic in x and y only
# would be periodic=(True, True, False). Both are keyword-only.
cellsize = 2 * (1.0 / n) ** (1 / 3)
grid = meshsearch.MeshGrid(x, y, z, cellsize,
limits=[[0, 1], [0, 1], [0, 1]], periodic=True)
// Two objects on opposite sides of the x faces, 0.004 apart through the
// boundary and 0.996 apart across the box.
const unsigned int left = n;
const unsigned int right = n+1;
X.push_back(0.002); Y.push_back(0.5); Z.push_back(0.5);
X.push_back(0.998); Y.push_back(0.5); Z.push_back(0.5);
// A periodic axis needs its limits: they are the box, and the period is
// hi-lo. Coordinates are not wrapped, so every point must already lie
// within them. The periods must be whole multiples of the shortest one. The
// flags are per axis, so a slab periodic in x and y only would be
// {true, true, false}.
const double cellsize = 2.*std::cbrt(1./double(n));
meshsearch::MeshGrid::Options options;
options.limits = std::array<std::array<double,2>,3>{{{0.,1.}, {0.,1.}, {0.,1.}}};
options.periodic = {true, true, true};
meshsearch::MeshGrid grid(X, Y, Z, cellsize, options);
In Python, periodic is one bool for all three axes or a triple, one per
axis, and grid.periodic returns the flags; in C++ they are
Options::periodic and get_periodic(). Periodic and non-periodic axes can
be mixed, for instance in a slab periodic in two directions.
The limits are the box. A periodic axis requires limits, and its period is
hi - lo. Its box is exactly the limits, neither widened nor extended to
whole cells. Coordinates are never wrapped: a point outside the limits is an
error, so coordinates must be wrapped into the box before the grid is built.
Query points and new objects must also lie within the limits; nothing is
wrapped. Points at lo and at hi are both accepted, and along that axis
their distance is 0. An axis of zero extent cannot be periodic. Reducing a
coordinate modulo the period brings it into the box:
# Positions from a simulation can drift just outside the box, and nothing is
# wrapped: a query point, like an object, must lie within the limits. Reduce
# each coordinate modulo the period first; one that lands on hi is accepted.
lo, hi = 0.0, 1.0
drifted = np.array([-0.01, 0.5, 1.01])
wrapped = lo + np.mod(drifted - lo, hi - lo)
middle = np.full(3, 0.5)
outside = grid.contains(drifted, middle, middle)
inside = grid.contains(wrapped, middle, middle)
// Positions from a simulation can drift just outside the box, and nothing
// is wrapped: a query point, like an object, must lie within the limits.
// Reduce each coordinate modulo the period first; one that lands on hi is
// accepted.
const double lo = 0., hi = 1.;
const std::vector<double> drifted = {-0.01, 0.5, 1.01};
std::vector<double> wrapped;
for (const double x : drifted) {
const double w = std::fmod(x-lo, hi-lo);
wrapped.push_back(w < 0. ? lo+w+(hi-lo) : lo+w);
}
const std::vector<double> middle(3, 0.5);
const std::vector<std::uint8_t> outside = grid.contains(drifted, middle, middle);
const std::vector<std::uint8_t> inside = grid.contains(wrapped, middle, middle);
The periods must share one cell side. All cells are cubes of one side, so
every period must be an integer multiple of the shortest: a box of 1 by 1 by 2
is accepted, 1 by 1 by 1.5 is not. Only the periodic axes count, so the
second box is accepted with its third axis not periodic. The match is checked
to a relative 10⁻¹², which absorbs the rounding of limits such as 0.1 * 3.
The cell side used can be smaller than the one requested. The grid uses
the largest side that divides the shortest period without exceeding the
requested one: with a period of 1 and a request of 0.234, five cells of 0.2;
a request at or above the shortest period gives cells as long as that period.
get_cellsize() (cellsize in Python) returns the side used. Non-periodic
axes use the same side and are extended to whole cells as usual. With no
periodic axis, the side is exactly the one requested.
Any radius is valid. A ball may be larger than the box, and Rmax may be
+inf: since each object is found once, at its nearest image, a ball of
infinite radius returns every object exactly once, and nearestObjects can
request as many objects as the grid holds. Along a periodic axis of period
L the displacement is taken in [−L/2, L/2), so the search covers half a
period on either side of the query point, and an object exactly half a period
away is found once.
nearest = grid.nearest_object(left)
print(f"nearest to object {left}: object {nearest} (expected {right})")
# Radii are not limited by the box: +inf returns every object once, each at its
# nearest image.
everything = grid.close_objects(0.5, 0.5, 0.5, np.inf)
print(f"objects within +inf of the centre: {everything.size} of {len(grid)}")
# A ball around a point at the corner takes in objects from all eight corners
# of the box.
corner = grid.close_objects(0.0, 0.0, 0.0, 0.1)
const unsigned int nearest = grid.nearestObject(left);
std::printf("nearest to object %u: object %u (expected %u)\n", left, nearest, right);
// Radii are not limited by the box: +inf returns every object once, each
// at its nearest image.
const double inf = std::numeric_limits<double>::infinity();
const std::vector<unsigned int> all = grid.closeObjects(0.5, 0.5, 0.5, inf);
std::printf("objects within +inf of the centre: %zu of %u\n", all.size(), grid.get_nObjects());
// A ball around a point at the corner takes in objects from all eight
// corners of the box.
const std::vector<unsigned int> corner = grid.closeObjects(0., 0., 0., 0.1);
The cost. A periodic query also visits the cells across the faces, and tests the wrap in the end cells of each periodic axis, so it is slower than on the same points without periodicity; the performance page gives the figures. A grid with no periodic axis does none of this work.