I want to set up a two dimensional box with length L
and periodic boundary conditions in Python. One way to proceed is to fix the reference frame at bottom left corner, then I have:
L = 5 # box length
x, y = 2, 3 # initial values
# step in my algorithm
x = (x + 4) % L
y = (y - 4) % L
And the output is, obviously, x, y = (1, 4)
. We can assure that for every step the positions of the particles will remain inside the box of length L.
Okay, but what if I want to set the reference frame in the center of the box? The following code, of course, doesn't work:
x = (x + 4) % L/2
y = (y - 4) % L/2
What I want to happen is that if a particle escapes from the x=+L/2 side then it appears by x=-L/2 (same for y), but the module operator cannot deal with this in the same way as in the previous case.