Ideal Gas Simulator: Help Solving Collision Detection Issue

In summary, the conversation is about a programmer who has written a program in Python to simulate an ideal gas in 2D. They are having issues with energy conservation during collisions between particles and are seeking help to fix the problem. The programmer's physics teacher suggests extrapolating backwards to determine the exact time of collision, but they have not seen this approach in other programs. They provide a code snippet and link to their code for reference. Another user suggests that discretization of time may be the cause of the issue and provides a link to a tutorial with incorrect equations. They also suggest looking at another source for correct equations. The conversation ends with the programmer thanking the user for their help and agreeing to look into the suggested source.
  • #1
JizzaDaMan
48
0
This one's aimed primarily at the programmers in this forum.

I've written a programme in python (Set as a long term project from my A-level physics teacher) which will hopefully end up being a scientifically accurate ideal gas simulator in 2D - I'll set up a bunch of particles with random velocities, then let it run indefinitely. I'll write the kinetic energy of each particle to a file every 100 'time steps' (or data to that effect) that I can then plot a histogram from in excel to analyse the distributions of the velocities.

At the moment I'm at a stage where I have multiple particles bouncing around a box, and I'm just showing it graphically so I can see what's happening.

Unfortunately at the moment I have a pretty serious problem with my code; at seemingly random collisions between particles, total kinetic energy is not conserved. When all the particles are the same mass and without gravity (which I've tried implementing in the past to see if it works) the increase in energy that happens isn't actually noticeable to the eye (but still not scientifically accurate!), but when I change some of the particles' masses or add in gravity then it all just goes totally crazy.

I've spoken to my physics teacher about it and he seems to think that when I detect a collision, it's never going be the exact moment of impact, so I need to extrapolate backwards to determine the exact time of the collision and work things out from there. I've looked at source code of other programmes achieving the same thing however, and I haven't seen anything so complicated.

So yeah this problem is totally confusing me, so some help would be great. This is the collision detection routine which then calculates the velocities after the collision:

Code:
	def CollideBall(self, ball, timeframe = 1):				
		Impulse = self.VectorToBall(Vector(ball._getX() + ball.Velocity._getX() * timeframe, 
			ball._getY() + ball.Velocity._getY() * timeframe))
		
		if Impulse.Length() <= self.Size + ball.Size:
			Impulse.Normalize()
			Impact = Vector(self.Velocity._getX() - ball.Velocity._getX(), self.Velocity._getY() - ball.Velocity._getY())
			ImpactVelocity = Impact.DotProduct(Impulse)
			
			Impulse.MultiplyScalar(math.sqrt(abs(ImpactVelocity**2 * self.Mass * ball.Mass)))
			
			self.Velocity.X += Impulse.X / self.Mass
			self.Velocity.Y += Impulse.Y / self.Mass

			ball.Velocity.X -= Impulse.X / ball.Mass
			ball.Velocity.Y -= Impulse.Y / ball.Mass

I'm afraid I'm no professional programmer so my code's a bit messy, I hope you can follow it. All the variable names should be fairly self explanatory.

I implemented that by following this semi-tutorial thingy:
http://freespace.virgin.net/hugo.elias/models/m_snokr.htm

I had to change one little bit; notice that I squared the impact velocity on the 'Impulse.MultiplyScalar()' line, whereas the tutorial doesn't mention anything like that. I did that because before kinetic energy wasn't being conserved in any collisions at all.

The rest of my code is attached if you want to read it. In 'Physics.py' are the methods and classes that deal with the motion and collisions etc. The 'Bouncing_Ball.py' file just contains the code that initialises the particles and draws them. The 'graphics.py' module is just one I found on line which uses tKinter to draw basic shapes. (Unfortunately I had to convert them to text files in order to upload them).

Thanks so much for any help in advance :)
 

Attachments

  • Bouncing_Ball.txt
    1.5 KB · Views: 392
  • Physics.txt
    5.5 KB · Views: 439
  • graphics.txt
    27.2 KB · Views: 464
Last edited:
Physics news on Phys.org
  • #2
Is the total energy increasing as a function of time or is it stable on averge? If it is the latter, then there is nothing to worry about: I've never seen such a simulation where this is not the case. Discretization of time is the culprit.
 
  • #3
It is stable on average, but it's not just changing by the odd joule it's in the order of a kJ the first time. Once it happens once, then it begins to increase at an increasing rate. I won't say exponentially though because it is still random. When I change the mass of one molecule then the kinetic energy increases very quickly, and the rate is somewhere in between when I try to implement gravity.
 
  • #4
What happens when you reduce the time step?
 
  • #5
It reduces the frequency of errors
 
  • #6
JizzaDaMan said:
I implemented that by following this semi-tutorial thingy:
http://freespace.virgin.net/hugo.elias/models/m_snokr.htm
I looked at that link, and the equations are incorrect. I don't understand why he takes the square root of the "impact speed", it doesn't make sense in terms of units. And ##\sqrt{m_1 m_2}## also doesn't make any sense, as you would rather expect the reduced mass ##m_1 m_2 / (m_1 + m_2)## to appear in there.

I looked on the internet, and found this page: http://introcs.cs.princeton.edu/java/assignments/collisions.html

which explains things correctly. In particular, scroll down to the section entitled "Collision resolution."
 
  • #7
ok thanks I'll have a look at that :)
 

1. What is the "Ideal Gas Simulator" and what does it simulate?

The "Ideal Gas Simulator" is a computer program that simulates the behavior of particles in an ideal gas. It takes into account variables such as temperature, pressure, and volume to calculate the behavior of the particles.

2. What is the collision detection issue in the "Ideal Gas Simulator"?

The collision detection issue in the "Ideal Gas Simulator" refers to the difficulty in accurately detecting and calculating collisions between particles in the simulation. This can lead to inaccurate results and affect the overall accuracy of the simulation.

3. Why is collision detection important in the "Ideal Gas Simulator"?

Collision detection is important in the "Ideal Gas Simulator" because it is essential for accurately representing the behavior of particles in an ideal gas. Without proper collision detection, the simulation may not accurately reflect the real-life behavior of particles in an ideal gas, leading to inaccurate results.

4. What are some potential solutions to the collision detection issue in the "Ideal Gas Simulator"?

Some potential solutions to the collision detection issue in the "Ideal Gas Simulator" include using more advanced algorithms for collision detection, increasing the number of particles in the simulation, and adjusting the parameters of the simulation to better reflect real-world conditions.

5. How can we improve the accuracy of collision detection in the "Ideal Gas Simulator"?

To improve the accuracy of collision detection in the "Ideal Gas Simulator", we can continuously test and refine the algorithms used for collision detection, adjust the parameters of the simulation based on experimental data, and compare the simulation results with real-world observations to identify and address any discrepancies.

Similar threads

  • Chemistry
Replies
1
Views
2K
Replies
4
Views
3K
Replies
22
Views
2K
Replies
2
Views
1K
Replies
4
Views
3K
Replies
4
Views
3K
  • Classical Physics
Replies
7
Views
3K
Replies
1
Views
3K
  • Advanced Physics Homework Help
Replies
27
Views
4K
Back
Top