Take the 2-minute tour ×
Physics Stack Exchange is a question and answer site for active researchers, academics and students of physics. It's 100% free, no registration required.

I want to simulate an impact between two bodies according to gravity, and eventually considering other forces to stick matter together. I'd like to use python to do this, but I am open to alternatives. What kind of tools or libraries can I use to perform this task ?

share|improve this question
    
Something as game physics engine? (Newton laws+collision detection with friction) –  mbq Nov 17 '10 at 12:39
    
@mbq: do you know a good and easy one, possibly usable via python ? –  Stefano Borini Nov 17 '10 at 12:40
    
I have to say that the phrase "particle physics" in your title is confusing. I was wondering what model you were going to use for pion production... –  dmckee Nov 17 '10 at 18:57
    
@dmckee : you are absolutely right –  Stefano Borini Nov 18 '10 at 23:38
    
Long time ago I was doing some programming with ODE. There is also Bullet engine which I only heard about. I guess both of them might have python bindings. But certainly do use some tools and forget about writing a reasonable (in the sense of capable of simulating anything resembling reality) engine yourself, it's not worth it. Just google for engines I am sure you'll find even more of them. And also try asking at StackOverflow, as programmers use these engines much more often than physicists, I'd think (e.g. in games). –  Marek Nov 19 '10 at 0:37

3 Answers 3

up vote 3 down vote accepted

I recently did something like this, in order to simulate a system of two masses connected by a spring. Those masses lay horizontally on a frictionless plane. One of these masses got an initial impulse and thereafter the system was left alone. While the entire system (the controid to be precies) moves with constant velocity, the two masses are oscillating, while moving forward. Here is a short ASCII drawing of the system

 Initial Impulse     ______              ______ 
 ---->               | m1 |/\/\/\/\/\/\/\| m2 |
_____________________|____|______________|____|______________________

After writing down the differential equations, I wrote a small python programm simulating the problem. This programm relies on the method of small steps (also called the Eueler Method). Here is the correspondig wikipedia article:

http://en.wikipedia.org/wiki/Euler_method

I implemented this alogorithm for the problem described above and plotted the results using gnuplot:

gnuplot.info (I am only allowed to add one hyperlink, so please add www)

But you are free to use any tool you like for this purpose. Here comes the sourcecode of my small programm:

#!/usr/bin/python
import os

steps = 100000
time = 100.

# Initial conditions
D = 0.9
m1 = 1.2
m2 = 0.4
v1 = 1.3
v2 = 0.
x1 = 0.
x2 = 1.
l = 1.

#Since I also tried to implement other algorithmus i specify which one to use
Euler = 1


#Euler 

if Euler == 1:
    timesteps = time / steps
    # Open the files for writing the results to
    f = open('results_x1', 'w')
    f2 = open('results_x2', 'w')
    f3 = open('results_com', 'w')

    # The real calculation   
    for i in range(0,steps):
        x1 = x1 + (D * (x2 - x1 -l) / m1)* (timesteps**2) + v1 * timesteps
        x2 = x2 - (D * (x2 - x1 -l) / m2)* (timesteps**2) + v2 * timesteps
        v1 = v1 + (D * (x2 - x1 -l) / m1)* (timesteps)
        v2 = v2 - (D * (x2 - x1 -l) / m2)* (timesteps)  
        f.write(str(i*timesteps) + " " + str(x1) + "\n")
        f2.write(str(i*timesteps) + " " + str(x2) + "\n")
        f3.write(str(i*timesteps) + " " + str((x1*m1 + x2*m2)/(m1+m2)) + "\n")


    f.close()
    f2.close()
    f3.close()

Of course there are better alogorithmus than the euler one, but this one is definitly the easiest to implement (I failed implementing more advanced algorithms ;-)).

So these are the steps you should probably follow:

  • Write down the differential equations for you problem
  • Understand the Euler Method
  • Take my code as a reference point and modify it for your problem

I know that this is quite an extensive topic and that my answer is therefore just superficial. Just tell what you want to know more about, and I will try to add corresponding comments ;-)

share|improve this answer
    
A nice alternative to the Euler Method is Verlet Integration, which tends to be more stable and accurate than the Euler method. –  Justin L. Dec 26 '10 at 9:12
    
wow, jeah, hadn't heard of that. Seems really nice. If I have some spare time, I will have a look into it, thanks. –  ftiaronsem Dec 26 '10 at 11:43

Check out the site of Ron Fedkiw; it is a good starting point with comprehensive set of keywords.

share|improve this answer

it depends on what kind of simulation you trying to build:

if your simulation has the purpose build a simulative model, that, for example, avoids the experimental noise, maybe with a complex dynamics algorithm and so on, i think C or C++ are the best choices..

If on the other hand you want to create a quick simulation with graphical output and built-in analysis tools (maybe even for didactic purpose), python is your choice! in this case I suggest you check out the Enthought Python Distribution.. for accademic use it is freeware and it has a built-in release of scipy.

share|improve this answer
    
ok, but I'm not asking about a scientific distribution of python. I don't want to reimplement body dynamics from scratch. –  Stefano Borini Nov 17 '10 at 12:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.