A simple Gas simulation

Up a level : Themodynamics
Next page : A simulation of an Atmosphere

On this page, you will find a simple gas simulation. There are 1000 “single atom molecules” that will bounce back and forth between the walls in a rectangular box. All collisions are perfectly elastic, i.e. both the linear moment and the kinetic energy are conserved. The collisions take angles and offsets between the particles into consideration.

One of the particles will be red to make it easier to follow.

Keys and buttons

  • Restart or “R” restarts the simulation.
  • Trace, “T” will toggle trace on or trace off, of the red particle. This will allow you to see the Brownian random walk of the particle.
  • Warmer, “W”, will cause the lower wall to be “warm”, thus heating the gas.
  • Colder, “C”,  will cause the lower wall to be “cold”, thus cooling the gas.
  • Left, “Left arrow”, will move the left wall to the left, if possible.
  • Right, “Right arrow”, will move the left wall to the right.

The displayed information

  • The box of particles: The “box” that the molecules move in an array, 600 by 600 pixels large, and it contains 1000 molecules with a radius of 2 pixels. That means that the total area the atoms cover is about 3.5% of the total area, which is quite much more than how big part of a volume ordinary gas molecules occupy.  Each molecule will have about 19 by 19 pixels of space to occupy.  If we, on the other hand, see this as a slice of a 3D space, 600 by 600 by 600 pixels large, and that each molecule will now have 19 by 19 by 19 pixels, then we would have about 31000 molecules, and they would occupy about 0.05% of the total volume, and that is quite much closer to the about 0.1% of the space the air molecules at room temperature and a pressure of one atmosphere does occupy, so this simulation would give you a hint of how close the molecules in ordinary are are to each other compared to in ordinary air.
  • The particles: They are shown as slightly elongated, with a narrow nose that is proportional to the speed. One of the particles is red.
  • A trace of the red particle, if that is set to on.
  • You can see the temperature above the graph. This is calculated as the total kinetic energy times a scale factor. This calculation corresponds to the equation E=(3/2)kT.
  • You can see the pressure both numerically and on a pressure gauge. The pressure is calculated as a moving average of the sum of the impulses on the right wall.
    As you can see, the pressure is a statistical phenomenon.
  • T/P: If you heat up the gas or cool it down you can see that the T/P will remain somewhat constant. This shows that this “gas” obeys Charles’s law, T/P=constant.
    You may have to wait a few seconds for the value to stabilize somewhat. As you “heat up” the gas the T/P will remain somewhat constant. It will deviate just as you change the temperature, but get somewhat back to the old value if you wait a few seconds.
  • A graph of the speed distribution.

The particles will initially all have the same speed, but they will move in random directions. It is interesting to see how quickly the distribution of speeds approaches the theoretical 2D Maxwell-Bolzmann distribution i.e., the Rayleigh distribution.

To check if this simple model would give you the theoretical distribution was one of the aims of doing this (the main one was that it was fun). I knew that it would work, but what surprised me was how fast it approached the theoretical distribution.

The temperature will stay constant in a closed container, given no external heat or work will enter or exit the container. This is because no internal energy is stored as potential energy. The average kinetic energy per atom is the average internal energy per atom.

In this case, the temperature would not be a statistical physical quantity. In reality, though it would be one.

The pressure is measured as a scaling factor times the sum of the impulses on the left wall per frame. This will vary slightly since the pressure is a statistical physical quantity.

When moving the wall you might change the temperature, even if you get back to the original size of the camber. This is because the wall, albeit moving slowly, still moves too fast compared to the average speed of the atoms – and this will make the behaviour of the gas deviate from the gas laws.

This is written in JavaScript and it is 410 lines long. Perhaps 300 lines if one would remove all comments. No external frameworks or modules are used.

You can find the simulation here.

Up a level : Themodynamics
Next page : A simulation of an AtmosphereLast modified: Feb 18, 2024 @ 20:19