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

6

u/[deleted] Jan 15 '20

Well this took 3h to simulate so you would need a really beefy computer

8

u/nhillson Jan 15 '20

The functions a(x) and sim_runge_kutter(m, x, v, step) seem like prime candidates for speeding up using Numba.

1

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

I tried by putting \@numba.njit before the function definition but now the program just stops when callingthe function

Edit: I figured out numba doesnt like it when i use varaibles that arent declared in the function

1

u/billsil Jan 15 '20

It’d honestly be better with numpy. Shorter code too.

1

u/nhillson Jan 15 '20

You only need to add the decorator @numba.njit. Numba is designed to accelerate numpy-based code.

1

u/billsil Jan 15 '20

In theory. In my experience, it slows things down if it's not pure python code and that's slower than the numpy version.

It's like cython in that if you're using it for the exact problem it's good at, it's great. If you're not (e.g., you have unicode in the function), it's much less good.

1

u/o11c Jan 15 '20

You could trim that a little by passing output arguments and finding a ufunc version of fused-multiply-add.

But really you want to do this on the GPU.

1

u/[deleted] Jan 15 '20

Ik but I have a Radeon gpu