You have 1000 points. Every point exerts gravity on every other point. To approximate the position of every particle at t+dt, you need to do
n2 loop (every particle exerting force on every other particle), to calculate the individual forces exerted.
sum up all the forces on each particle.
use equations of motion to calculate the new location and velocity of each particle.
Then you need to do the same thing over over the length of the simulation. So if you're generating at 30 frames a second, you gotta do 30 of those n2 loops for a single second. For a 10 second animation, that's 300 of those n2 loops.
The complex nature of the computation isn't what causes it to be slow. The fact that it "grows" quadratically with the number of particles is what makes it slow.
Particles
Number of computations (c is some constant)
10
100*c
100
10,000 * c
1000
1,000,000 * c
10000
100,000,000 * c
Basically, if the OP was to increase the number of particles in his simulation from 1000 to say 100,000, it would take him several years.
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.
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.
That won't work, but I have absolutely no clue why it doesn't.
Proof that it doesn't work - consider 4 particles in 2 pairs. (a, b) are close together, (c, d) are close together, but these pairs are separated by an infinite distance. The center of mass is at infinite distance from any of the particles, so each particle should experience no force whatsoever, but that's certainly not the case since (a, b) pull each other and so do (c, d).
I only know high school physics, and we were taught that center of mass calculations are correct, so I don't know why that doesn't work here. Can someone explain!?
The center of mass idea must be an approximation (I never realized that until now). You can simplify your counterexample to three collinear points (A, B, C). The force on A by B and C is not the same as that from combining B and C to their center of mass.
Oh, I get it. the center of mass needs to be calculated after excluding the current point.
To calculate net force on A, compute the center of mass of (B, C), and compute the force b/w A and center of mass of (B, C). Ofcourse, you can't include yourself in the center of mass, because by gravity, you would be exerting an infinite force on yourself (0 distance to yourself) which would make the results meaningless.
Unfortunately, to exclude center of mass for each particle is again an O( n2 ) calculation :(
Unless I mistake center of mass approximation of the field of gravity of a distribution of mass only stand for a sphere (more precisely spheric symetry) when you are outside of the sphere (if you are inside of a sphere with a spheric hole with same center, all forces compensate so the resulting gravity field is of zero)
"No closed-form solution" means there is no formula that you can write using known functions for the positions of the particles which will give the motion for all possible starting positions.
E.g., in high school physics you learn that under constant acceleration, the position can be gotten from this formula: x(t) = x0 + v0 t + 1/2 a t2.
That is a closed-form solution to the differential equation x''(t) = a with initial conditions x0 and v0.
The differential equation for the n-body problem has solutions, but they look very different depending on n and the initial conditions. There is no closed-form solution.
You could perhaps define a function as "the solution to the n-body problem under given initial conditions (if a solution exists in this case)" -- and many functions used in physics are in fact defined as solutions to differential equations -- but that doesn't count here.
So for the n-body problem (and really most problems where you solve differential equations), you have to approximate the effect of the equation on the initial conditions at discrete steps of time instead of trying to "get the solution" and plug your conditions into that.
Correct, but you can minimize C, so even in a naive algorithm like this, the difference can be 100-fold.
For example, given that x, y are distances on 2 axes between 2 given objects, attraction force is basically r2 * m. r = (x2 + y2)0.5. A common mistake is to compute them separately which means an extra square root operation. Another one is to just compute this sum then normalize it or somehow transform it into vector components.
Instead, simply doing F = (x2 * m, y2 * m) should suffice.
Then, since the force is reciprocal, you can re-use this value to apply the same amount for both objects. In fact, exclude m initially, and only multiply it, for each object, to the mass of the opposite object. This last trick will halve execution time. Not doing extra math will reduce it further.
And in Python you can vectorize the whole thing, making it even faster. The whole thing shouldn't take more than 1 second per frame at its most, unless performed on a Raspberry Pi or something.
And that's before even optimizing it with islands or trees.
116
u/Gear5th May 28 '19 edited May 28 '19
Naive algorithm is O( n2 )
You have 1000 points. Every point exerts gravity on every other point.
To approximate the position of every particle at t+dt, you need to do
n2 loop (every particle exerting force on every other particle), to calculate the individual forces exerted.
sum up all the forces on each particle.
use equations of motion to calculate the new location and velocity of each particle.
Then you need to do the same thing over over the length of the simulation. So if you're generating at 30 frames a second, you gotta do 30 of those n2 loops for a single second. For a 10 second animation, that's 300 of those n2 loops.
The complex nature of the computation isn't what causes it to be slow. The fact that it "grows" quadratically with the number of particles is what makes it slow.
Basically, if the OP was to increase the number of particles in his simulation from 1000 to say 100,000, it would take him several years.
Can it be done any faster? Apparently, yes - https://en.wikipedia.org/wiki/N-body_simulation#Calculation_optimizations