# 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. ```{literalinclude} ../examples/example_periodic.py :language: python :start-at: "# Two objects on opposite sides of the x faces" :end-at: "limits=[[0, 1], [0, 1], [0, 1]], periodic=True)" ``` ```{literalinclude} ../examples/example_periodic.cpp :language: cpp :start-at: "// Two objects on opposite sides of the x faces" :end-at: "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: ```{literalinclude} ../examples/example_periodic.py :language: python :start-at: "# Positions from a simulation can drift" :end-at: "inside = grid.contains(wrapped, middle, middle)" ``` ```{literalinclude} ../examples/example_periodic.cpp :language: cpp :start-at: "// Positions from a simulation can drift" :end-at: "const std::vector 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. ```{literalinclude} ../examples/example_periodic.py :language: python :start-at: "nearest = grid.nearest_object(left)" :end-at: "corner = grid.close_objects(0.0, 0.0, 0.0, 0.1)" ``` ```{literalinclude} ../examples/example_periodic.cpp :language: cpp :start-at: "const unsigned int nearest = grid.nearestObject(left);" :end-at: "const std::vector 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](benchmark.md#periodic-boxes) gives the figures. A grid with no periodic axis does none of this work.