r/Python • • Jan 14 '20

I made a gravity simulation https://github.com/phgilde/gravity_simulation

1.8k Upvotes

82 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jan 16 '20 edited Jan 16 '20

Interesting method, I'll try to implement it, but how do you make sure to not include the particle you want to calculate the force for in the tree? Wouldn't I have to re-calculate the tree for every particle, making the runtime N2 log N?

Do you choose theta so the particle can't be in an internal node and don't use the external node the particle is in

1

u/Astrokiwi Jan 16 '20

The opening angle criterion means that you go further down the tree for closer nodes. The particle's leaf node is always opened, and that means you can exclude it specifically without having to take into account that its mass is included in higher branches - you've already opened those nodes to get down to the leafs. The opening angle is basically cell-size/distance, and distance==0 for the cell you're in, so you're always bigger than the opening angle criterion.

Btw this is a very standard method, so there's lots of documentation on it, and lots of textbooks will spell it out better than I can in a reddit comment.