Simulating Gas Molecules
Giving Gas molecules Boltzmann Velocities and then asking them to collide.
- We take two systems.
- We fill the systems with molecules based on the temperature of the systems.
- We give motion to the particles.
- We open the barrier.
- We observe the temperature change of the system.
- We observe the systems and we store the data.
We take two systems
Divide the whole screen into two equal parts: Box A and Box B.
The controls are on the left side which has Initialize Button (to fill the boxes with molecules), Open barrier Button (to open the barrier for heat exchange) and Pause/Resume Button (to pause or resume the animation).
They also contains the input for masses, number of particles and Initial Temperature of the systems.
The right side shows graph and have button to download data. The graphs are shown using chart.js (open source and very customizable).
We fill the systems with molecules based on the temperature of the systems
This one took lot of my thinking time. I needed to make sure that both system has Boltzmann velocity distribution.
However, there is no universal rule to distribute velocities based on such probability distribution (or any quantity based on such probability distributions). There were some based on generating some easier curve like normal distribution and then modify the distribution to generate the required distribution. But those involved very complex calculations.
Another possible solution was to give each particle equal velocities
such that the temperature of the system is equal to given one and then wait for some time for the system to evolve into Boltzmann, but you know waiting is boring and we don’t know for how long to wait.
So, how I did it?
Well, we know that probability distribution can be used to give number of particles with a particular velocity, if we know the total number of particles. Let Ntotal be the total number of particles in a system. Then,
But the problem with this is, the velocities are continuous (ah infinity and everything becomes so complex).
Can’t we make these velocities discrete, well do you remember something similar? Riemann integral in which we partition the interval into finite number of sub-intervals. And then we find the areas of the rectangles formed and then we sum them all and take the limit of thickness of sub-interval to be 0.
I am gonna do the same thing. But I don’t have a finite interval (v ranges from 0 to infinity). So I am just gonna take a finite thickness sub-intervals. Once the width of these sub-intervals is taken, let’s say dv, then the number of particles with velocity between v and v+dv is given by the area of the trapezoid,
Therefore, we get the number of particles with velocity between v and v + dv as:
So, now we can randomly N(v, v+dv) particles with velocities between v and v+dv. For this, we can use a uniform random number generator.
A small discussion then we can proceed to complete algorithm to generate a velocity distribution. How to choose dv?
We need to choose dv small for limiting case. But if we decrease the value of dv very low, then N(v, v+dv) < 1 for most of the cases and hence we will not get a single particle for most of the sub-intervals. Another problem with dv is that, as we increase the temperature the distribution widens and hence the dv that works for T = 200K will not work for T = 400K because for T = 400K, dv may become too small and will lead to case discussed above. One of the solution to this problem is to use most probable speed to determine dv because for higher temperature, most probable speed will be greater and we also need higher dv, direct proportionality.
Time for a complete algorithm:
- Choose a particular dv. I chose, dv = 0.1 * most probable speed.
- Start with v = 0 and find the number of particles (N(v, v+dv)) between v and v + dv.
- Generate N(v, v+dv) random numbers between v and v+dv and add them to your list of velocities.
- Make v = v + dv.
- Repeat 2, 3 and 4 until …
- You will need to break the loop using the condition when N(v, v +dv) will start coming as 0 because after that you will always get 0. Note that v > most probable speed or rms speed.
However, this is just an approximate way and doesn’t make sure that we get the required number of particles and we have required summation of velocities given by
So what we do is that, we generate remaining number of random numbers centered around most probable speed and then normalize them such that the sum of square of velocities is equal to remaining sum of square of velocities.
And so we generated a velocity distribution given the Temperature of system, Total Number of particles and mass of particles in the system.
So we can generate the two systems with given Temperature, mass of particles and total number number of particles in each box.
The simulation has been written in JavaScript. I have added the link to code at the end of the article.
We give motion to the particles
So, now we know the initial velocities that is to be given to the particles of each system. We use canvas and particles to design all this. We will then need to handle the collision between the particles assuming the collision to be elastic. I won’t go into detail of designing all this as excellent tutorials exist for how to design collision:
And the code for collision has been partially taken from above tutorial.
During the designing of the two systems, make sure that the particles are not moving from one system to another and the particles of different systems aren’t interacting otherwise it will cause some dramatic affect at the border of the two systems.
We open the barrier.
To open the barrier is equivalent to giving permission to particles to move in the entire screen and particles from different system can interact with each other.
We observe the temperature change of the system.
So when barrier is opened, particles from different boxes with mix and thus, a particle which was in box A can move to box B. So,we need to keep track of the box of the particles as well.
To calculate the temperature of the systems (boxes). We find the particles which are present in the system by filtering the particles based on boxes.
var particlesA = particles.filter(x => x.box == "A")
var particlesB = particles.filter(x => x.box == "B")
And then calculate the temperature of the system using the formula,
We observe the systems and we store the data.
Store the number of particles and temperature of both systems in an array and download them into a text file when “Download Data” button is clicked. printData() function is used to the above task.
And in this way, I designed the simulation. Note that I haven’t mentioned the JavaScript code for every part and only where it’s required. The master code can be found here.
Tools used for writing this article:
- Geogebra for graphs.
- Online Latex Editor for Latex images.
- Colab for implementing python codes.
Enjoy !!