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.

Revisions

  1. pmocz revised this gist Sep 14, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions getAcc.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    def getAcc( pos, mass, G, softening ):
    """
    Calculate the acceleration on each particle due to Newton's Law
    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
    @@ -20,5 +20,5 @@ def getAcc( pos, mass, G, softening ):
    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
  2. pmocz created this gist Sep 14, 2020.
    24 changes: 24 additions & 0 deletions getAcc.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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