1

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.

BIA NKA
  • 11
  • 1

1 Answers1

6

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] == 0, y[0] == 1/2,
    a[0] == 1, b[0] == Sqrt[2],
    (* 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]]

enter image description here

(It seems GIF export has some issues in this case in the end of the animation...)

kirma
  • 19,056
  • 1
  • 51
  • 93