Skip to main content

calc_acceleration

calc_acceleration(pos, mass, G, softening=0.0)

Gravitational acceleration of every body due to every other body.

This is the force calculation at the heart of the engine. simulate_system calls it once at the start of a run and once per time step after that. It is exported so you can inspect the accelerations in a configuration, or write an integrator of your own on top of it.

Parameters

Name Type Description Default
pos numpy.ndarray Positions in meters, as an array of shape (n, 3) with one row per body and columns x, y, z. required
mass numpy.ndarray Masses in kilograms, as an array of length n in the same order as the rows of pos. required
G float Gravitational constant in m^3 kg^-1 s^-2. Use orb.gravitational_constant for real gravity. G = 0 gives zero acceleration everywhere. required
softening float Softening length in meters. Every squared separation r^2 is replaced by r^2 + softening^2, which keeps the acceleration finite when two bodies pass very close to each other. The default 0 means no softening. 0.0

Returns

Name Type Description
numpy.ndarray Accelerations in m/s^2, as an array of shape (n, 3) with one row per body and columns x, y, z. The result is all zeros when G is 0 or there are fewer than two bodies.

See Also

  • simulate_system: Integrate a system forward in time with these accelerations.
  • get_energy: Kinetic and potential energy of a simulation.

Notes

The acceleration of body \(j\) is the sum of Newtonian pulls from every other body \(k\):

\[ \mathbf{a}_j = G \sum_{k \neq j} m_k \frac{\mathbf{r}_k - \mathbf{r}_j} {\left(|\mathbf{r}_k - \mathbf{r}_j|^2 + \epsilon^2\right)^{3/2}} \]

where \(\epsilon\) is the softening length. A body does not pull on itself, and two bodies at exactly the same position (with no softening) exert no force on each other, so the result never contains infinities from a zero separation. All pairs are computed at once with numpy, so the cost grows as \(n^2\).

Examples

The Earth and the Moon, one mean lunar distance apart on the x axis. The Moon is pulled toward the Earth about 81 times harder than the Earth is pulled toward the Moon, the ratio of their masses.

import numpy as np
import pyorbiter as orb

pos = np.array([[0.0, 0.0, 0.0],
                [orb.distance_earth_moon, 0.0, 0.0]])
mass = np.array([orb.mass_earth, orb.mass_moon])

orb.calc_acceleration(pos, mass, orb.gravitational_constant)
array([[ 3.31629645e-05,  0.00000000e+00,  0.00000000e+00],
       [-2.69748330e-03,  0.00000000e+00,  0.00000000e+00]])

Softening weakens the pull at separations comparable to the softening length.

import numpy as np
import pyorbiter as orb

pos = np.array([[0.0, 0.0, 0.0],
                [orb.distance_earth_moon, 0.0, 0.0]])
mass = np.array([orb.mass_earth, orb.mass_moon])

orb.calc_acceleration(pos, mass, orb.gravitational_constant,
                      softening=orb.distance_earth_moon)
array([[ 1.17248785e-05,  0.00000000e+00,  0.00000000e+00],
       [-9.53704367e-04,  0.00000000e+00,  0.00000000e+00]])