Physics and numerical methods ============================= Equations of motion ------------------- Each body :math:`i` has mass :math:`m_i`, position :math:`\mathbf r_i` and velocity :math:`\mathbf v_i`. Newton's law of gravitation gives .. math:: \dot{\mathbf r}_i = \mathbf v_i, \qquad \dot{\mathbf v}_i = -G \sum_{k \neq i} m_k \frac{\mathbf r_i - \mathbf r_k}{\left(|\mathbf r_i - \mathbf r_k|^2 + \epsilon^2\right)^{3/2}} where :math:`\epsilon` is the optional softening length (``0`` by default). Softening keeps the force finite when two bodies pass very close to each other. That avoids huge velocity kicks, at the cost of slightly changing the physics at distances below :math:`\epsilon`. The accelerations of all bodies are computed together with NumPy array operations. The cost grows as :math:`N^2`, which is fine for up to a few hundred bodies. Integrators ----------- The time step :math:`h` is ``dt``. The last step is shortened so the run ends exactly at ``final_time``. **Euler** (``--method euler``, 1st order): :math:`\mathbf r \leftarrow \mathbf r + h\mathbf v`, :math:`\mathbf v \leftarrow \mathbf v + h\mathbf a(\mathbf r)`. It is the simplest scheme, but energy drifts quickly and orbits spiral out. It is mainly for comparison. **RK2 / midpoint** (``--method rk2``, 2nd order): evaluates the derivative at the middle of the step. **RK4** (``--method rk4``, 4th order, default): the classic Runge-Kutta method with four evaluations per step. The error per unit time scales as :math:`h^4`, so halving ``dt`` makes it about 16 times smaller. Checking accuracy ----------------- The total energy .. math:: E = \sum_i \tfrac12 m_i |\mathbf v_i|^2 - G \sum_{i