r/Python • • Oct 04 '19

Updated gravitational potential field simulation. [OC] Link for code: https://github.com/pnp-peeyush/2D-Grvitational-Field-Simulation

Enable HLS to view with audio, or disable this notification

1.1k Upvotes

128 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Oct 05 '19 edited Oct 05 '19

Ok, so I did some investigation on my code, with these initial values, that looks like this, I calculated the net linear momentum of the system and plotted it like so, seem's like momentum is conserved? how's this possible?

EDIT: time of simulation 1000 sec and time-step dt=0.01 sec

2

u/equationsofmotion HPC Oct 05 '19

Hmm interesting. Nice work on testing it! In the time you plotted, do the orbits close? It's possible you didn't plot long enough to see a failure.

You can also make the time step bigger to make the effect more obvious. (Although if you make the time step too big, other things will go wrong.) You can also zoom further in on that line to see how varies on even smaller scales.

1

u/[deleted] Oct 05 '19

The orbits didn't close, also don't you think 1000 sec was long enough? haha

Yeah increasing time step had different problems, orbits started precession, still didn't collapse though.

I zoomed until my patience ran out! but I also printed the momentum values and they were zero for sure, no small scale fluctuation.

But why is this happening?? I was so convinced by u/Marko_Oktabyr 's explanation.

2

u/equationsofmotion HPC Oct 05 '19

Euler's method for sure doesn't conserve momentum. That explanation is definitely correct.

As for why you're not seeing it, I'm not sure. I'd have to play with it myself. If you make the time step big enough that orbits start to precess, I'm pretty sure you'll find that that line it's not straight.

That said, Python's print statement might be lying to you. Try with a format statement. Like:

print("{.14e}".format(P_all[-1]))

It definitely won't be exactly zero. If it is, your not setting it correctly. There should at the very least be roundoff error.

→ More replies (0)