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/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?