PBC Cheat Sheet This cheat sheet is based on the article A Practical Guide to Periodic Boundary Conditions.
This cheat sheet works for vectors with any number of dimensions. It's sufficient to treat each dimension individually. Each dimension can have different boundaries. A boundary is a periodic interval $[a, b)$ with an inclusive lower bound and an exclusive upper bound.

Wrap Positions

Ensures that positions stay inside the periodic boundaries.

On Generic Boundaries $[a, b)$:

$$x \mathrel{{-}{=}} \text{floor}\left(\frac{x - a}{b - a}\right) \cdot (b - a)$$

Special Cases

On Boundaries $[0, 1)$:

$$x \mathrel{{-}{=}} \text{floor}(x)$$

On Boundaries $[0, w)$:

$$x \mathrel{{-}{=}} \text{floor}(x/w) \cdot w$$

On Boundaries $[-v, v)$:

$$x \mathrel{{-}{=}} \text{round}\left(\frac{x}{2v}\right) \cdot 2v$$

Shortest Connection

Determine the shortest connection of two positions in your periodic space. The connection can wrap around boundaries. First, compute the connection in the usual way:

$$dx = x_2 - x_1$$

Then, wrap the connection:

On Generic Boundaries $[a, b)$:

$$dx \mathrel{{-}{=}} \text{round}\left(\frac{dx}{b - a}\right) \cdot (b - a)$$

Special Cases:

On Boundaries $[0, 1)$:

$$dx \mathrel{{-}{=}} \text{round}(dx)$$