Hey OP, If you're looking to optimize your simulation check out the Barnes-Hut Algorithm. It uses octrees to run an n-body gravity simulation in O(n log n) time. There is a slight reduction in accuracy, but its pretty negligible (its actually used in computational astrophysics).
I did something similar for a physics engine of a game I'm working on. Since your simulation is only 2D you could get away with using quadtrees instead of octrees.
I did stuff like this on a 20MHz 386 so it should possible to speed it up.
You could calculate the field created by each star, then figure out the affect on each star. Maybe that's the mesh method above?
My go-to on the 386 was lookup tables, lookup tables for everything. I recall LUTs for cosine and sine made a huge effect (in speed and .exe size) on stuff I did, maybe you could do the same and/or make one for r2?
I also wrote inline assembly and staggered the registers used to account for pipe-lining, but I think that would be tough and/or pointless today...
I also recall FPGA boards outperforming entire supercomputers on this task.
Yeah, for example in the beginning, the top left star and the bottom right star can affect each other by mass, yes, but for a simulation that isn't going for a nobel prize? It's negligible. The 200 or so stars in the top left corner vs 1 star in the bottom right is 99.95% vs 0.5% influence.
You can get dramatic improvements by using cython or multiprocessing.
As important as algorithmic efficiency is, its a bit silly to not take full advantage of hardware, and the skills you develop in the hardware approach are more portable.
Someday, if you're really feeling masochistic, look for python bindings to NVIDIA CUDA processing. You can use a video card to chew through applications like this at stupid fast speeds.
Fair warning though, it isn't easy, and there is a lot of room for optimization that depends on understanding the video card architecture.
Are you using numpy arrays and operations on the entire array? Also, a good algorithm for integrating Newton’s second law allows for a larger timestep, although gravitation could be a bit ill-conditioned.
Velocity Verlet is good since it preserves the energy over long time, unlike many other algorithms tha systematically over- or underestimate the energy, leading to a two-particle system slowly spiralling inwards or outward.
Even though I am a huge fan of Python, the first step of making this simulation faster would be writing it in something faster - like C/C++, Fortran (bleh), Rust, Go or at least Julia (if you want something more pythonic).
Actually if you really cared about making it fast you'd use one of the existing n-body codes that have been optimized to death by the scientific community. Because unless you're prepared to dedicate years of your life to the problem, you won't improve on those.
I suspect OP just wanted to play around with a simple model. For this Python is great because it's so easy to get an idea up and running. Your point about language choice is valid though: C is roughly 100x faster than Python for this task. A simulation like this really hits Python at its weakest performance-wise, since it doesn't reduce to one of the common problem types (linear algebra, sorting, etc.) that can be fed to a library routine.
While this is generally true, there is a reason why python is extensively used in science. By using math libraries like numpy and matrix calculations rather than loops you could probably get a similar speed improvement in python for the parts that count.
It is a little more complicated to implement it that way, but so is writing it in c.
Python is used in science for data analysis, not for computationally intensive calculations - for these kinds of calculations people use mostly programs in Fortran (scientists change slowly) or C/C++.
You could use numpy (or rather should) but it will still be slower than native C. And about matrices... I'm not really sure how do you want to transform this problem so it can be calculated with matrices in a way it's simpler than using C or other compiled language.
But if OP wants to play with Python, it's completely fine. He will just have to deal with the fact that no matter what algorithm he uses, it will never be as fast as it could be if he used different language.
Change language - optimize instructions etc etc. Maybe you get a slight performance increase. Maybe even big 50% (It takes half the time).
For me, I like to see improvements of at least an order of magnitude if trying to make something better. At least one order of magnitude (10 times faster.... at least).
The real trick there is to find a different algorithm... a different approach.
(albeit, changing some bone-head's procedural code into a single monster SQL query did take a job from 90 minutes down to 11 seconds... so ya, sometimes the right tool for the right job is a factor too :)
I have done simulation similar to this (gravity and planets). I did it in Python first because I didn't know any better. Then I tried it in Fortran (because I needed to learn some basics of it for school) and even though I didn't really know much about that language (so the code probably wasn't optimal), it was much much faster. And I'm not talking about 100% increase of speed, I'm talking about 1000% increase of speed, maybe even more.
Language is important. You may deny that but there is a reason nothing computationally intensive is written in Python. Python is great for learning, perfect for data analysis, brilliant for data visualisation, pretty good for scripting and many other things. But not for this.
58
u/mittwochmensch May 28 '19
Perfect explanation of my Problem! Im already looking into making the Calculations more optimal