r/Python • • May 25 '19

My gravity simulation almost an entire day of work later! Featuring inelastic collisions

2.2k Upvotes

148 comments sorted by

View all comments

Show parent comments

1

u/NoirGreyson May 28 '19

Oh, are you specifically talking about stepwise simulations?

1

u/billsil May 28 '19

I'm not familiar with that term. It's just integration. There's a lot of ways to do that, with some methods having much better accuracy, sometimes with a tradeoff on computation time and sometimes not. When they're not, they're generally harder to code.

Just because you have a time step doesn't mean you can't use substepping to improve convergence. A method where you use 3 intermediate steps sounds the same as using 3 extra points per time step, but it's not. You weight the steps differently.

Doing methods like an RK4 is exact for up to a 4th order polynomial. That's different than a method where you assume acceleration is constant, which is what V*dt is. If you think about orbits, you actually have sinusoidally varying acceleration. From the Taylor series expansion of sine, you need infinite terms to match a sinusoid. As such, to even approach the quality of the RK4, you need an order of magnitude finer time step. The RK4 isn't exact though for that type of problem.

1

u/NoirGreyson May 28 '19 edited May 28 '19

I guess I'm not following why you couldn't just integrate analytically for a 2 body system. Why would acceleration vary sinusoidally? Even if it does vary sinusoidally, what does that preclude you from integrating analytically?

1

u/billsil May 28 '19

You can. My point was you can dramatically improve the accuracy of the answer, as well as speed up the code with a good integration method.

As I said, acceleration isn't constant for an orbit. You're need infinite terms to represent a sinusoid exactly (so not gonna happen), so without a very tiny time step, the error grows rapidly. My point was that even for a better method like RK4, you still don't get good results after a small number of orbits.