# A simple Gas simulation

Up a level : Themodynamics
Next page : A simulation of an Atmosphere In this page you will find a simple gas simulation. It is 1000 “single atom molecules” 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 in consideration.

In the simulation you may “heat up” or “cool down” the gas. You may also move the left wall to compress or expand the gas.

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 approach 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 basically 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 since no internal energy is stored as potential energy. The average kinetic energy per atom is this 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.

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.

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

The “box” that the molecules move in are 600 by 600 pixels large, and it contains 1000 molecules with the radii 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.

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.

The simulation is updated to be quite much faster + I have added a restart button. Up a level : Themodynamics
Next page : A simulation of an Atmosphere Last modified: Dec 16, 2021 @ 19:37