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 ?
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:
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 ;-)
Check out the site of Ron Fedkiw; it is a good starting point with comprehensive set of keywords.
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.