# How can I draw a box with moving particles inside and a hole in one side? [closed]

The idea is to create a simulation for ideal gas motion in a container that have a hole and then find the speed distribution for the gas particles that leave the box through the hole. I have a seminar work tomorrow and any help would be very appreciated.

• Is this question about visualizing the phenomenon, or also simulating it? Also, are you after a three-dimensional, or two-dimensional diagram? What have you tried this far? – kirma Jun 22 '15 at 16:26
• …and can you show us anything you've attempted? – J. M.'s ennui Jun 22 '15 at 16:26
• It is a 3D animation. Up to now i have drawn only a container with some random points in it. Im trying to figure out how to make them move. I haven't used mathematica before for such seminar works and I new in this stuffs – BIA NKA Jun 22 '15 at 16:30
• This kind of problem is not trivial by most standards -- it goes well beyond the scope of this Q/A. However if you encounter any specific problems along the way feel free to ask. – Sektor Jun 22 '15 at 16:34
• – Jens Jun 22 '15 at 17:20

I'll provide an starting point for 2D case with single particle. Collisions with other particles are likely to be hard to model (or at least require adding an massive amount of WhenEvent rules if implemented this way), since NDSolve and WhenEvent tend to miss discrete events. Also, 3D case would be considerably more complicated to build; likely to take more time than you have.

In this case improvements, or rather, improvisation is literally left as an exercise to the reader. This code requires v10 for its use of RegionMember.

Module[{line, eventactions, sol},
(* the box boundary *)
line = {{-1, 1/4}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}, {-1, -1/4}};
(* actions at the boundary built from box *)
eventactions =
WhenEvent[
Evaluate[
(* BooleanMinimize somehow compensates flimsiness of WhenEvent.
also, evaluate and simplify equations here before run of NDSolve. *)
BooleanMinimize[
FullSimplify[
(* is the particle on the line segment? *)
RegionMember[
Line[{##}], {x[t], y[t]}], (x[t] | y[t]) \[Element] Reals],
"CNF"]],
(* action at boundary for each line segment: bounce *)
Evaluate[
{a[t], b[t]} ->
ReflectionTransform[RotationTransform[\[Pi]/2][#1 - #2]][{a[t],
b[t]}]]] & @@@ Partition[line, 2, 1];
sol = NDSolve[{
(* model of motion *)
x'[t] == a[t], y'[t] == b[t],
(* initial parameters *)
x == 0, y == 1/2,
a == 1, b == Sqrt,
(* actions at boundary *)
Sequence @@ eventactions},
{x, y}, {t, 0, 12}, DiscreteVariables -> {a, b}];
Animate[Graphics[{Line@line, Point[{x[t], y[t]} /. sol]}],
{t, 0, 12}, AnimationRate -> 1/2]] (It seems GIF export has some issues in this case in the end of the animation...)