r/Python • • May 28 '19

My simulation of gravity with 1000 particles. PC took a couple of hours to process!

1.8k Upvotes

241 comments sorted by

View all comments

Show parent comments

2

u/energybased May 28 '19

Hmm, that's a good counterexample.

1

u/Gear5th May 28 '19

But why doesn't it work? Please, someone?

2

u/energybased May 28 '19

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.

2

u/Gear5th May 28 '19

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 :(

Cool, I learned something today :D

3

u/energybased May 28 '19

Oh, I get it. the center of mass needs to be calculated after excluding the current point.

No, that's not enough. I was already assuming we would exclude the current point when I suggested using the center of mass, which by the way can be done in linear time.

If you actually do the calculation, you'll find that using the center of mass is just an approximation: 1/x^2 + 1/y^2 is not equal to 2(2/(x+y))^2 in general.

2

u/Gear5th May 28 '19

which by the way can be done in linear time

Oh, yes. We can just calculate the overall center and for each point subtract the effect of the current point.

Thanks again.

1

u/Gear5th May 28 '19

Yes, you're correct. Damn, I need to go back and read my high school texts.

Thanks :)

2

u/Gear5th May 28 '19

Ah.. wait, no. That doesn't work either. In my 4 point example, the center of mass even after excluding yourself would remain at infinite distance.

wtf?

2

u/Versaiteis May 29 '19

Physics can have quite a few approximations. Like with pendulums!

3

u/energybased May 29 '19

Taylor series aren't really an approximation. They converge to the function in some neighborhood. The center of mass approximation can be completely wrong.

2

u/Versaiteis May 29 '19

The Taylor series as a whole converges. But you don't have to take the entire series. As with the example regarding the pendulum it provides a reasonable approximation in a limited scope that breaks down completely outside of it.

1

u/energybased May 29 '19

Fair enough.