Skip to content

Instantly share code, notes, and snippets.

@pmocz
Last active November 28, 2023 08:15
Show Gist options
  • Save pmocz/bd2257fcddb64aa52e555d67a5557a18 to your computer and use it in GitHub Desktop.
Save pmocz/bd2257fcddb64aa52e555d67a5557a18 to your computer and use it in GitHub Desktop.
Create your own N-body simulation (with Python): acceleration
def getAcc( pos, mass, G, softening ):
"""
Calculate the acceleration on each particle due to Newton's Law
pos is an N x 3 matrix of positions
mass is an N x 1 vector of masses
G is Newton's Gravitational constant
softening is the softening length
a is N x 3 matrix of accelerations
"""
N = pos.shape[0];
a = np.zeros((N,3));
for i in range(N):
for j in range(N):
dx = pos[j,0] - pos[i,0];
dy = pos[j,1] - pos[i,1];
dz = pos[j,2] - pos[i,2];
inv_r3 = (dx**2 + dy**2 + dz**2 + softening**2)**(-1.5);
a[i,0] += G * (dx * inv_r3) * mass[j];
a[i,1] += G * (dy * inv_r3) * mass[j];
a[i,2] += G * (dz * inv_r3) * mass[j];
return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment