r/programming • u/ttsiodras • Dec 23 '12
Simulating a solar system with Python
http://users.softlab.ntua.gr/~ttsiod/gravityRK4.html
244
Upvotes
r/programming • u/ttsiodras • Dec 23 '12
39
u/AsterJ Dec 23 '12
My favorite line of code:
sun = Planet()
Spoken like a true programmer!
13
u/quizzle Dec 23 '12
If the planets are sticking together you're simulating perfect inelastic collisions, not elastic. Elastic collisions would be cool though.
9
u/mk_gecko Dec 23 '12
How do you get code highlighting in your webpage?
4
u/Bob_goes_up Dec 23 '12
When you use Runge Kutta, you are not supposed to handle one planet at a time. The function named nextderivative should return a vector containing accellerations of all the planets, and you should only calculate the constants a,b,c and d once for each timestep. The function named updatePlanet should be deleted.
7
u/question_all_the_thi Dec 23 '12
For those interested, here's an adaptive step Runge-Kutta-Cash-Karp differential equation solver in Python:
2
u/pururin Dec 23 '12
Why are all the fun programming projects in python, and not in, say, ruby or perl?
4
u/protocol_7 Dec 23 '12
I did something like this as my final project for a physics class. I made two simulations with it: The first used the actual data for planets and dwarf planets in the solar system, and it showed some cool things like the perihelion precession of Mercury (except the part due to general relativity, since I only simulated a limited subset of special relativity) and the Sun's small orbit around the center of mass of the solar system.
The second simulation showed how the Trojan asteroids cluster around the Lagrange points 60° to each side of Jupiter. It worked by randomly scattering a bunch of asteroids in plausible positions and orbits around Jupiter, after which they naturally tend to cluster around the two stable (L4 and L5) Lagrange points. (Due to computational limitations, collisions and the asteroids' gravitational pull on each other was neglected, making it a restricted three-body problem.)
3
u/quizzle Dec 23 '12
I made this once using Pyglet to get some nice openGL graphics in it too. Was the most fun coding project I ever did.
3
u/sontek Dec 23 '12
Have you seen http://rhodesmill.org/pyephem/ ?
3
u/Jasper1984 Dec 23 '12
Libnova is a useful library for planetary orbits and orientations.
3
u/bamdastard Dec 23 '12
I built one of these using pyopencl that does 12000 particles and another piece of it uses a genetic algorithm to find stable orbits for planets.
http://www.youtube.com/watch?v=XCvRBHtPbzE
http://www.youtube.com/watch?v=lnOmy1ly6M0
http://code.google.com/p/stableorbit
5
u/jminuse Dec 23 '12
A similar simulation in JavaScript: http://comicjk.com/gravity.html.
2
u/ipki Dec 23 '12
I had to write a very similar program, but in javascript through google docs, for my high school physics class this year.
2
u/sifodeas Dec 24 '12
Here is mine that I made. I have not put out an update in forever, though. https://github.com/Sifodeas/N-Body
4
u/Peaker Dec 24 '12 edited Dec 24 '12
I have a habit of transliterating Python code to Haskell.
The Haskell code.
And due to a bit of silliness in Haskell's GL library, here's an extra tiny wrapper it depends on, Vector2.hs.
I think the Haskell code turns out cleaner than the Python code.
But most importantly - it gets static type checking without additional costs!
To reiterate, the Python/Haskell versions are roughly the same size, and despite this, the Haskell code is fully statically typed.
One nice thing is the Haskell one takes 10% CPU here and the Python one takes 50% (without psyco). Haven't profiled to figure out why.
EDIT: Here are some comparisons of the length of both:
If you remove the silly Vector2 wrapper that exists in libraries anyway (wanted light-weight dependencies):
Haskell wc: 231 1161 8612
If you ignore all the imports which don't have the ordinary cost of other code, you get:
I find it pretty amazing that you get more safety and less code at the same time. This in a simulation, which is often cited as appropriate for OO programming, and not FP.
2
u/FinallyGotAccount Dec 23 '12
Is it just me or do those ellipses seem far too exaggerated in the width-dimension? I thought that the orbits of the planets around the sun were much closer to proper circles and only slightly "elliptical" (sidenote: what is the correct term for this? it's been a while since I took that astro-physics course)
1
u/Coffee2theorems Dec 23 '12
This Sim is missing a button that will produce a Godzilla that wreaks havoc on the Solar System.
1
u/rtywrenx Dec 24 '12
This is a great post, and simulations of this type interest me. Any recommendations for introductions to the material?
1
u/[deleted] Dec 25 '12
I am just amazed at how smart some people are. I my self would never be able to implement this I am just to sucky at algorithms and math.
1
u/igouy Dec 27 '12
Using various programming languages -- model the orbits of Jovian planets.
-2
u/KamiCrit Dec 23 '12
I was thinking solar system as a solar array.. Hoping for random days of cloud then sun and the kWh sky rockets. Non the less very cool program!
0
u/[deleted] Dec 23 '12
[deleted] — view deleted comment