What's the average position of oscillating particles in a box with periodic boundary conditions? Imagine an open box repeating itself in a way that a if a particle crossing one of the box boundary is "teleported" on the opposite boundary (typical periodic boundary position in 3D).
Now put a hundred of oscillating particles inside. Each particle position is identified in a uniq fashion through time.
I want to get the average position of each of these particles.
EDIT:
Thanks for your input. But I think I did not explain myself correctly. Please forgive me for that. That's why I removed most of my wordy description and created the following picture as a 1D reduction of my problem instead.

Both pictures:


*

*This is a 1D projection of the 3D problem.

*The axis is infinite.

*The blue line represents the 1D unit cell repeated by translation along the axis without overlapping.

*The red dot represents the position of one unique and identified particle. 

*The green dots represents the equivalent particles to the red one obtained by translation. These are really equivalent to the red one: same nature, velocity, force field, etc.

*As an input, I only have particle positions inside of the 1D unit cell (the blue line).


Top picture:


*

*The red particle is present in the unit cell for each time step.

*I take the particle position $x(t)$ and average it over $t$ to get $\bar{x} = \frac{1}{4}$ of the unit cell in this case (black arrow).


Bottom picture:


*

*The red particle is crossing the boundary of the unit cell.

*However, $x(t)$ is only defined in the unit cell, therefore in this case at:


*

*$t=0$, the position of the green particle (on the right) is taken into account ;

*$t=1$, the position of the red particle (on the left) is taken into account

*and so on;


*If the average $\bar{x}$ is calculated without much care, it is false since this average is done between two different particles (red arrow instead of black arrow).


I developed a small algorithm that tries to correct this assuming that $x(t+\Delta t) - x(t) < \frac{\Delta x}{2}$, with $\Delta x$, the size of the unit cell.
However this algorithm is quite slow. I know I can improve the algorithm itself and my implementation of it. But I also know that this is a trivial (and inefficient) way to do it. I also hate to reinvent the wheel again and again...
Do you know any smart implementation or algorithm able to calculate this kind of "periodic" average?
 A: Just to be clear, it sounds like your system is a 3-torus, $$\mathbb{T}^3 = S^1\times S^1\times S^1$$
Position should then be modulo the length of the relevant dimension. The trick is an understanding that any particle can never be more than $\frac{L}{2}$ distance from the center point of the cube in any one direction (although total distance might be greater as calculated with Pythagorean Theorem, but this can never exceed $D = \sqrt{{(0.5L_x)}^2+{(0.5L_y)}^2+{(0.5L_z)}^2}$).
I would try something like this; define the position of the particles relative to the center of the cube and give the particle a sign flip each time it crosses a boundary.  So if velocity is positive along a given axis, and the distance from the center reference goes above $\frac{L}{2}$ then multiply by $-1$.  You should then be able to calculate and average position of particles relative to the center of the box.
I would also add that you would have to code so that the final instantaneous velocity when you reach a boundary becomes the initial velocity on the other side of the boundary, this would be dependent on how you are calculating velocities with your script.
