# How to update particle location with reflection in a cuboid?

Let's consider Omega= [0, x_max]*[0, y_max]*[0, z_max] the domain in which some particles move. I have a question about the update of the particles locations if they cross the domain boundaries.

More precisely, I want the particles to be reflected inside the domain Omega if they across the limits of any axis. For example if the new location is {0.5, 0.2, 1.3} and $$x_{max} = y_{max}=z_{max}=1.0$$, then in this case the $$z$$ location is outside the limit, so how to update the particle location in that case with reflection?

• so you have to create Animation !? or table values for t then export as motion pic!? Please show us your code so we can help! Commented Mar 25, 2019 at 15:01
• no, I'm asking about the way to update the location in 3D environment if we use reflective boundaries. I have particles that move in Omega and sometimes the new locations go out from Omega so I'm asking how to reput them again inside Omega through reflecting boundaries mathematically before code? Commented Mar 25, 2019 at 15:13
• this can be useful Commented Mar 26, 2019 at 19:14

One method for solving this problem is to use NDSolve to solve the differential equations for free flight and use WhenEvent to enforce the reflections at the cube boundaries.

(* cube boundaries *)
{xmin, xmax} = {ymin, ymax} = {zmin, zmax} = {-1, 1};

(* ODE set *)
odeSet = {x''[t] == 0, y''[t] == 0, z''[t] == 0};

(* initial conditions *)
init = {
x[0] == .1, y[0] == .2, z[0] == .3,
x'[0] == 1, y'[0] == 1.1, z'[0] == .8
};

(* reflect when crossing boundaries *)
reflections = {
WhenEvent[x[t] > xmax || x[t] < xmin, x'[t] -> -x'[t]],
WhenEvent[y[t] > ymax || y[t] < ymin, y'[t] -> -y'[t]],
WhenEvent[z[t] > zmax || z[t] < zmin, z'[t] -> -z'[t]]
};

(* use NDSolve with WhenEvent to enforce reflections *)
sol = NDSolveValue[
Join[odeSet, init, reflections],
{x[t], y[t], z[t]},
{t, 0, 10}
];

ParametricPlot3D[sol, {t, 0, 10},
PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}]


Assuming the nature of the reflection that you want you might use TriangleWave.

reflectedWithin[m_, M_][x_] /; M > m := TriangleWave[{m, M}, (m + M - 2 x)/(4 m - 4 M)]


Usage for a value bounded between -3 and 5:

Plot[reflectedWithin[-3, 5][x], {x, 0, 25}
, AspectRatio -> Automatic
, GridLines -> {None, {-3, 5}}
]


• If we use specular reflexion, is that correct to consider for example: if x_(t+1)>x_max: x_(t+1)= 2x_max-x_(t+1) if x_(t+1)< 0 : x_(t+1)=-x_(t+1) samething for the two other axis? Commented Mar 26, 2019 at 14:11