Algorithm Search⌘K

Algorithmadvanced

Wisdom-Holman integrator

Also called mixed-variable symplectic integrator, Wisdom-Holman map.

A symplectic splitting for planetary systems that advances the Kepler motion of each planet exactly and adds the mutual perturbations as kicks.

Time
Θ(N^2) step
Space
Θ(N) extra
Where
N = the number of bodies
Properties
deterministicnot implicitone stepsymmetricsymplectic
Origin
Jack Wisdom, Matthew Holman, 1991

The Wisdom-Holman integrator advances a planetary system, a star of mass m0m_0 with planets of much smaller masses, by splitting the Hamiltonian into two parts that can each be solved exactly. The first part is a sum of independent Kepler problems, one per planet, in Jacobi coordinates; its flow moves each planet along its exact conic in closed form. The second part, the mutual pull of the planets, depends on the positions only; its flow is a kick of the velocities. The step is the symmetric composition kick-drift-kick of operator splitting, as in leapfrog, so the map is symplectic, time-symmetric and of second order. But because the large Kepler force is treated exactly, the error is of order εh2\varepsilon h^2 with ε\varepsilon the ratio of the planet masses to m0m_0, and steps of a twentieth of an orbit are common. It is the standard method for long integrations of planetary systems.

In plain words

In a planetary system, each planet mostly goes around the star on a fixed ellipse, and the planets only pull each other a little. The motion on an ellipse can be computed exactly for any time, with a formula.

So the method alternates two simple moves. First it gives each planet a small push from the other planets, for half a step. Then it moves each planet along its own ellipse around the star for a full step, exactly. Then it gives the second half push. The only error comes from doing the two moves one after the other instead of at the same time, and that error is small because the pushes are small. The steps can be long, and the errors in energy stay bounded over billions of orbits.

The problem

The input is a star (body 0) and planets 1,…,N−11, \ldots, N - 1 with masses mim_i, positions xi\mathbf{x}_i and velocities vi\mathbf{v}_i, G=1G = 1, and a time step hh. The output is an approximation of the state at time t+ht + h of the gravitational N-body problem, without softening.

Jacobi coordinates. Let ηi=m0+⋯+mi\eta_i = m_0 + \cdots + m_i and let Xi\mathbf{X}_i be the center of mass of bodies 0,…,i0, \ldots, i. Set xi′=xi−Xi−1\mathbf{x}'_i = \mathbf{x}_i - \mathbf{X}_{i-1} for i≥1i \ge 1 and x0′=XN−1\mathbf{x}'_0 = \mathbf{X}_{N-1}, and the same for velocities. With the masses mi′=miηi−1/ηim'_i = m_i \eta_{i-1} / \eta_i the kinetic energy is diagonal, and the Hamiltonian is H=HK+HIH = H_K + H_I with

HK=∣p0′∣22ηN−1+∑i≥1(∣pi′∣22mi′−Gmi′ηiri′),HI=−∑i<jGmimj∣xi−xj∣+∑i≥1Gmi′ηiri′.H_K = \frac{|\mathbf{p}'_0|^2}{2\eta_{N-1}} + \sum_{i \ge 1} \left( \frac{|\mathbf{p}'_i|^2}{2 m'_i} - \frac{G m'_i \eta_i}{r'_i} \right), \qquad H_I = -\sum_{i < j} \frac{G m_i m_j}{|\mathbf{x}_i - \mathbf{x}_j|} + \sum_{i \ge 1} \frac{G m'_i \eta_i}{r'_i}.

The flow of HKH_K is a free motion of the center of mass and, for each planet, a Kepler orbit x¨i′=−μixi′/ri′3\ddot{\mathbf{x}}'_i = -\mu_i \mathbf{x}'_i / r'^3_i with μi=Gηi\mu_i = G \eta_i. The part HIH_I depends only on positions, and it is of the size of the planet-planet terms, smaller than HKH_K by a factor of about ε\varepsilon.

The algorithm

  1. WISDOM-HOLMAN-STEP(x, v, h)
  2. 1(x', v') ← JACOBI(x, v)
  3. 2v' ← v' + (h/2) KICK(x')
  4. 3for i ← 1 to N-1: (x'_i, v'_i) ← KEPLER(x'_i, v'_i, G η_i, h)
  5. 4x'_0 ← x'_0 + h v'_0
  6. 5v' ← v' + (h/2) KICK(x')
  7. 6return CARTESIAN(x', v')
  8. KICK(x'):
  9. 7a' ← JACOBI(Newtonian accelerations at CARTESIAN(x')), a'_0 ← 0
  10. 8a'_i ← a'_i + G η_i x'_i / r'_i³ for i ≥ 1; return a'
  11. KEPLER(x, v, μ, Δt): a ← 1/(2/r - v²/μ), n ← sqrt(μ/a³), e_c ← 1 - r/a, e_s ← x·v / sqrt(μ a)
  12. 9solve n Δt = ΔE - e_c sin ΔE + e_s (1 - cos ΔE) for ΔE by safeguarded Newton
  13. 10return (f x + g v, ḟ x + ġ v)

Lines L2 and L5 are the two half kicks with the flow of HIH_I. Because x′=Jx\mathbf{x}' = J \mathbf{x} for a fixed linear map JJ, the Jacobi accelerations of the full potential are JJ applied to the Cartesian ones (line L7); line L8 removes the Kepler part that HKH_K already contains. Lines L3 and L4 are the exact flow of HKH_K for the time hh. Line L9 is Kepler's equation in differences of the eccentric anomaly EE, with ec=ecos⁡E0e_c = e \cos E_0 and es=esin⁡E0e_s = e \sin E_0; its left side is increasing in ΔE\Delta E, and its root lies within 2 of nΔtn \Delta t, so Newton's method with a bisection safeguard (Newton-Raphson) always converges. Line L10 uses the Gauss ff and gg functions,

f=1−ar0(1−cos⁡ΔE),g=Δt−ΔE−sin⁡ΔEn,f˙=−μa sin⁡ΔErr0,g˙=1−ar(1−cos⁡ΔE),f = 1 - \frac{a}{r_0}(1 - \cos \Delta E), \quad g = \Delta t - \frac{\Delta E - \sin \Delta E}{n}, \quad \dot f = -\frac{\sqrt{\mu a}\, \sin \Delta E}{r r_0}, \quad \dot g = 1 - \frac{a}{r}(1 - \cos \Delta E),

with r=a(1−eccos⁡ΔE+essin⁡ΔE)r = a(1 - e_c \cos \Delta E + e_s \sin \Delta E). The code is for bound orbits only; hyperbolic planets need the universal variables.

Example

Take the star m0=1m_0 = 1 at the origin, planet 1 with m1=10−3m_1 = 10^{-3} at (1,0,0)(1, 0, 0), and planet 2 with m2=3×10−4m_2 = 3 \times 10^{-4} at (0,1.6,0.05)(0, 1.6, 0.05), on near-circular orbits. The Jacobi vectors are x1′=(1,0,0)\mathbf{x}'_1 = (1, 0, 0) and x2′=(−0.000999,1.6,0.05)\mathbf{x}'_2 = (-0.000999, 1.6, 0.05), with μ1=1.001\mu_1 = 1.001 and μ2=1.0013\mu_2 = 1.0013. The Jacobi acceleration of planet 1 is (−1.001045,−0.0000456,−0.0000014)(-1.001045, -0.0000456, -0.0000014), and the kick of line L8 is (−0.0000446,−0.0000456,−0.0000014)(-0.0000446, -0.0000456, -0.0000014): the exact Kepler part takes away all but a part in 10410^4 of the force.

Over one orbit of planet 1 (T=2πT = 2\pi), compared with a classical Runge-Kutta integration with 6000 steps, the largest position error is (measurement with this implementation):

steps per orbit 20 40 80
error, planet masses 10−310^{-3} and 3×10−43 \times 10^{-4} 1.17×10−51.17 \times 10^{-5} 2.92×10−62.92 \times 10^{-6} 7.31×10−77.31 \times 10^{-7}
error, planet masses ten times larger 2.35×10−42.35 \times 10^{-4} 5.88×10−55.88 \times 10^{-5} 1.47×10−51.47 \times 10^{-5}

Halving the step divides the error by 4, as for a second-order method. Over 10 orbits with 40 steps per orbit, the largest relative energy error ∣E(t)−E(0)∣/∣E(0)∣|E(t) - E(0)| / |E(0)| is 1.9×10−61.9 \times 10^{-6}, against 2.1×10−42.1 \times 10^{-4} for Cartesian leapfrog with the same step (measurement).

Correctness

The Jacobi map is linear with x′=Jx\mathbf{x}' = J\mathbf{x}, and the kinetic energy in Jacobi velocities is ∑mi′∣vi′∣2/2\sum m'_i |\mathbf{v}'_i|^2 / 2 plus the term of the center of mass, so H=HK+HIH = H_K + H_I holds exactly with HIH_I a function of positions only (Lemma 1 of the proof file). The flow of HIH_I is the kick of lines L7 and L8 (Lemma 2), and lines L9 and L10 give the exact Kepler flow from the eccentric anomaly (Lemma 3). The step is the composition φh/2I∘φhK∘φh/2I\varphi^{I}_{h/2} \circ \varphi^{K}_h \circ \varphi^{I}_{h/2} of exact Hamiltonian flows. So it is symplectic, it is symmetric (Φ−h=Φh−1\Phi_{-h} = \Phi_h^{-1}), and it is of second order by the Baker-Campbell-Hausdorff formula (theorem). By backward error analysis, it is the exact flow of a modified Hamiltonian H~=H+h2H2+O(h4)\tilde H = H + h^2 H_2 + O(h^4), where H2H_2 is a combination of the Poisson brackets {HK,{HK,HI}}\{H_K, \{H_K, H_I\}\} and {HI,{HI,HK}}\{H_I, \{H_I, H_K\}\}. Both contain HIH_I, so H~−H=O(εh2)\tilde H - H = O(\varepsilon h^2), and the energy error stays bounded by O(εh2)O(\varepsilon h^2) over exponentially long times (theorem, under the assumptions of backward error analysis, see shadow Hamiltonian).

Proof: The Wisdom-Holman step is a composition of exact Hamiltonian flows, so it is symplectic, symmetric and of second orderfull

Let ηi=m0+⋯+mi\eta_i = m_0 + \cdots + m_i, let Xi=ηi−1∑k≤imkxk\mathbf{X}_i = \eta_i^{-1} \sum_{k \le i} m_k \mathbf{x}_k, and let xi′=xi−Xi−1\mathbf{x}'_i = \mathbf{x}_i - \mathbf{X}_{i-1} for i≥1i \ge 1 and x0′=XN−1\mathbf{x}'_0 = \mathbf{X}_{N-1}. Write JJ for this linear map, x′=Jx\mathbf{x}' = J \mathbf{x}, and mi′=miηi−1/ηim'_i = m_i \eta_{i-1} / \eta_i.

Lemma 1 (the splitting). With v′=Jv\mathbf{v}' = J \mathbf{v}, the kinetic energy is T=12ηN−1∣v0′∣2+12∑i≥1mi′∣vi′∣2T = \frac{1}{2} \eta_{N-1} |\mathbf{v}'_0|^2 + \frac{1}{2} \sum_{i \ge 1} m'_i |\mathbf{v}'_i|^2. So with p0′=ηN−1v0′\mathbf{p}'_0 = \eta_{N-1} \mathbf{v}'_0 and pi′=mi′vi′\mathbf{p}'_i = m'_i \mathbf{v}'_i, the Hamiltonian is H=HK+HIH = H_K + H_I with HKH_K and HIH_I as on the page, and HIH_I depends on x′\mathbf{x}' only.

Proof. It is enough to show for each i≥1i \ge 1 that ηi−1∣Vi−1∣2+mi∣vi∣2=ηi∣Vi∣2+mi′∣vi−Vi−1∣2\eta_{i-1} |\mathbf{V}_{i-1}|^2 + m_i |\mathbf{v}_i|^2 = \eta_i |\mathbf{V}_i|^2 + m'_i |\mathbf{v}_i - \mathbf{V}_{i-1}|^2, where Vi=X˙i\mathbf{V}_i = \dot{\mathbf{X}}_i. Write w=vi−Vi−1\mathbf{w} = \mathbf{v}_i - \mathbf{V}_{i-1}. Then Vi=Vi−1+(mi/ηi)w\mathbf{V}_i = \mathbf{V}_{i-1} + (m_i / \eta_i) \mathbf{w}, so ηi∣Vi∣2=ηi∣Vi−1∣2+2miVi−1⋅w+(mi2/ηi)∣w∣2\eta_i |\mathbf{V}_i|^2 = \eta_i |\mathbf{V}_{i-1}|^2 + 2 m_i \mathbf{V}_{i-1} \cdot \mathbf{w} + (m_i^2 / \eta_i) |\mathbf{w}|^2, and mi∣vi∣2=mi∣Vi−1∣2+2miVi−1⋅w+mi∣w∣2m_i |\mathbf{v}_i|^2 = m_i |\mathbf{V}_{i-1}|^2 + 2 m_i \mathbf{V}_{i-1} \cdot \mathbf{w} + m_i |\mathbf{w}|^2. Subtracting, the left side minus ηi∣Vi∣2\eta_i |\mathbf{V}_i|^2 is (mi−mi2/ηi)∣w∣2=mi′∣w∣2(m_i - m_i^2 / \eta_i) |\mathbf{w}|^2 = m'_i |\mathbf{w}|^2. Starting from m0∣v0∣2=η0∣V0∣2m_0 |\mathbf{v}_0|^2 = \eta_0 |\mathbf{V}_0|^2 and applying the identity for i=1,…,N−1i = 1, \ldots, N-1 gives ∑kmk∣vk∣2=ηN−1∣VN−1∣2+∑i≥1mi′∣vi′∣2\sum_k m_k |\mathbf{v}_k|^2 = \eta_{N-1} |\mathbf{V}_{N-1}|^2 + \sum_{i \ge 1} m'_i |\mathbf{v}'_i|^2. Since JJ is invertible and does not depend on time, the change x′=Jx\mathbf{x}' = J\mathbf{x}, p′=J−Tp\mathbf{p}' = J^{-T} \mathbf{p} is canonical, and pi′\mathbf{p}'_i is as stated. The potential is a function of x=J−1x′\mathbf{x} = J^{-1} \mathbf{x}', and HIH_I is the potential plus a function of x′\mathbf{x}', so it depends on positions only. □\square

Lemma 2 (the kick). The flow of HIH_I for a time τ\tau keeps x′\mathbf{x}' and adds τai′\tau \mathbf{a}'_i to vi′\mathbf{v}'_i, where a′\mathbf{a}' is computed by lines L7 and L8.

Proof. Since HIH_I does not depend on p′\mathbf{p}', its flow keeps x′\mathbf{x}', and p˙′\dot{\mathbf{p}}' is constant, so vi′\mathbf{v}'_i changes by τai′\tau \mathbf{a}'_i with ai′=−∂HI/∂xi′/mi′\mathbf{a}'_i = -\partial H_I / \partial \mathbf{x}'_i / m'_i (and a0′=0\mathbf{a}'_0 = 0, because the potential and the Kepler terms do not depend on x0′\mathbf{x}'_0). For the potential part, the full Newtonian motion satisfies x¨′=Jx¨\ddot{\mathbf{x}}' = J \ddot{\mathbf{x}}, and since the kinetic energy is diagonal in v′\mathbf{v}' with masses mi′m'_i (Lemma 1), −∂U/∂xi′/mi′=(Ja)i-\partial U / \partial \mathbf{x}'_i / m'_i = (J \mathbf{a})_i, with a\mathbf{a} the Newtonian accelerations. This is line L7. For the term Gmi′ηi/ri′G m'_i \eta_i / r'_i, the derivative is −Gmi′ηixi′/ri′3-G m'_i \eta_i \mathbf{x}'_i / r'^3_i, which gives +Gηixi′/ri′3+ G \eta_i \mathbf{x}'_i / r'^3_i after the division by −mi′-m'_i. This is line L8. □\square

Lemma 3 (the Kepler drift). For a bound orbit x¨=−μx/r3\ddot{\mathbf{x}} = -\mu \mathbf{x} / r^3, lines L9 and L10 give the exact state after time Δt\Delta t. The Newton iteration of line L9 has a unique root to converge to.

Proof. Put the orbit in its plane with the pericenter on the first axis, so x(E)=(a(cos⁡E−e),bsin⁡E)\mathbf{x}(E) = (a(\cos E - e), b \sin E) with b=a1−e2b = a\sqrt{1 - e^2}, r=a(1−ecos⁡E)r = a(1 - e \cos E), and E˙=na/r\dot E = n a / r with n=μ/a3n = \sqrt{\mu / a^3}. Then Kepler's equation nt=E−esin⁡E+constn t = E - e \sin E + \text{const} gives, with E=E0+ΔEE = E_0 + \Delta E,

nΔt=ΔE−ecos⁡E0sin⁡ΔE+esin⁡E0(1−cos⁡ΔE).n \Delta t = \Delta E - e \cos E_0 \sin \Delta E + e \sin E_0 (1 - \cos \Delta E).

From r0=a(1−ecos⁡E0)r_0 = a(1 - e \cos E_0) we get ecos⁡E0=1−r0/a=ece \cos E_0 = 1 - r_0 / a = e_c, and from rr˙=x⋅vr \dot r = \mathbf{x} \cdot \mathbf{v} and r˙=aesin⁡E E˙\dot r = a e \sin E\, \dot E we get x0⋅v0=na2esin⁡E0\mathbf{x}_0 \cdot \mathbf{v}_0 = n a^2 e \sin E_0, so esin⁡E0=x0⋅v0/μa=ese \sin E_0 = \mathbf{x}_0 \cdot \mathbf{v}_0 / \sqrt{\mu a} = e_s. The energy gives 1/a=2/r0−v02/μ1/a = 2/r_0 - v_0^2 / \mu. So line L9 solves Kepler's equation. Its left side F(ΔE)=ΔE−ecsin⁡ΔE+es(1−cos⁡ΔE)F(\Delta E) = \Delta E - e_c \sin \Delta E + e_s(1 - \cos \Delta E) has F′=1−ecos⁡E=r/a>0F' = 1 - e \cos E = r / a > 0, so it is increasing and has one root; and ∣F(ΔE)−ΔE∣≤e(sin⁡ΔE)2+(1−cos⁡ΔE)2≤2e<2|F(\Delta E) - \Delta E| \le e \sqrt{(\sin \Delta E)^2 + (1 - \cos \Delta E)^2} \le 2e < 2, so the root lies in [nΔt−2,nΔt+2][n\Delta t - 2, n\Delta t + 2]. Newton with bisection on this bracket converges.

The plane is two-dimensional and x0×v0=nab≠0\mathbf{x}_0 \times \mathbf{v}_0 = n a b \ne 0, so x=fx0+gv0\mathbf{x} = f \mathbf{x}_0 + g \mathbf{v}_0 with f=(x×v0)/(x0×v0)f = (\mathbf{x} \times \mathbf{v}_0) / (\mathbf{x}_0 \times \mathbf{v}_0) and g=(x0×x)/(x0×v0)g = (\mathbf{x}_0 \times \mathbf{x}) / (\mathbf{x}_0 \times \mathbf{v}_0), where ×\times is the scalar cross product in the plane. With v0=(na/r0)(−asin⁡E0,bcos⁡E0)\mathbf{v}_0 = (n a / r_0)(-a \sin E_0, b \cos E_0),

x×v0=na2br0((cos⁡E−e)cos⁡E0+sin⁡Esin⁡E0)=na2br0(cos⁡ΔE−ec),\mathbf{x} \times \mathbf{v}_0 = \frac{n a^2 b}{r_0} \left( (\cos E - e) \cos E_0 + \sin E \sin E_0 \right) = \frac{n a^2 b}{r_0} (\cos \Delta E - e_c),

so f=(a/r0)(cos⁡ΔE−1+r0/a)=1−(a/r0)(1−cos⁡ΔE)f = (a / r_0)(\cos \Delta E - 1 + r_0 / a) = 1 - (a / r_0)(1 - \cos \Delta E). And x0×x=ab((cos⁡E0−e)sin⁡E−sin⁡E0(cos⁡E−e))=ab(sin⁡ΔE−esin⁡E+esin⁡E0)\mathbf{x}_0 \times \mathbf{x} = a b \left( (\cos E_0 - e) \sin E - \sin E_0 (\cos E - e) \right) = a b \left( \sin \Delta E - e \sin E + e \sin E_0 \right). By Kepler's equation esin⁡E0−esin⁡E=nΔt−ΔEe \sin E_0 - e \sin E = n \Delta t - \Delta E, so g=(sin⁡ΔE+nΔt−ΔE)/n=Δt−(ΔE−sin⁡ΔE)/ng = (\sin \Delta E + n \Delta t - \Delta E) / n = \Delta t - (\Delta E - \sin \Delta E) / n. Differentiating in tt with dΔE/dt=na/rd \Delta E / dt = n a / r gives f˙=−na2sin⁡ΔE/(rr0)=−μasin⁡ΔE/(rr0)\dot f = -n a^2 \sin \Delta E / (r r_0) = -\sqrt{\mu a} \sin \Delta E / (r r_0) and g˙=1−(a/r)(1−cos⁡ΔE)\dot g = 1 - (a / r)(1 - \cos \Delta E), and v=f˙x0+g˙v0\mathbf{v} = \dot f \mathbf{x}_0 + \dot g \mathbf{v}_0. Finally r=a(1−ecos⁡(E0+ΔE))=a(1−eccos⁡ΔE+essin⁡ΔE)r = a(1 - e \cos(E_0 + \Delta E)) = a(1 - e_c \cos \Delta E + e_s \sin \Delta E). These are the formulas of line L10, and they do not depend on the choice of axes. □\square

Theorem. Let φτK\varphi^K_\tau and φτI\varphi^I_\tau be the flows of HKH_K and HIH_I. The step is Φh=φh/2I∘φhK∘φh/2I\Phi_h = \varphi^I_{h/2} \circ \varphi^K_h \circ \varphi^I_{h/2}. It is symplectic, it satisfies Φ−h=Φh−1\Phi_{-h} = \Phi_h^{-1}, and its local error is O(h3)O(h^3), so its global error over a fixed time is O(h2)O(h^2).

Proof. By Lemma 2, lines L2 and L5 are φh/2I\varphi^I_{h/2}. The flow of HKH_K is the free motion of x0′\mathbf{x}'_0 (line L4) and, for each i≥1i \ge 1, the Kepler flow with μi=Gηi\mu_i = G \eta_i, because ∂HK/∂pi′=pi′/mi′\partial H_K / \partial \mathbf{p}'_i = \mathbf{p}'_i / m'_i and −∂HK/∂xi′=−Gmi′ηixi′/ri′3-\partial H_K / \partial \mathbf{x}'_i = -G m'_i \eta_i \mathbf{x}'_i / r'^3_i; by Lemma 3, line L3 is this flow. Lines L1 and L6 are the canonical change of Lemma 1 and its inverse. The flow of a Hamiltonian is symplectic, and compositions of symplectic maps are symplectic. Since φ−τ=φτ−1\varphi_{-\tau} = \varphi_\tau^{-1} for each flow, Φ−h=φ−h/2I∘φ−hK∘φ−h/2I=(φh/2I∘φhK∘φh/2I)−1\Phi_{-h} = \varphi^I_{-h/2} \circ \varphi^K_{-h} \circ \varphi^I_{-h/2} = (\varphi^I_{h/2} \circ \varphi^K_h \circ \varphi^I_{h/2})^{-1}. For the order, write the flows as exponentials of the Lie derivatives AA and BB of HIH_I and HKH_K; the symmetric Baker-Campbell-Hausdorff formula gives ehA/2ehBehA/2=exp⁡(h(A+B)+h3C3+O(h5))e^{hA/2} e^{hB} e^{hA/2} = \exp\left(h(A + B) + h^3 C_3 + O(h^5)\right) with C3C_3 a combination of [A,[A,B]][A, [A, B]] and [B,[B,A]][B, [B, A]], so the local error is O(h3)O(h^3) and the step is consistent of order 2. The step map is Lipschitz with constant 1+O(h)1 + O(h) on a compact set away from collisions, and the standard argument for one-step methods turns this into a global error O(h2)O(h^2). □\square

Remark (the size of the error). Every bracket in C3C_3 contains AA, the Lie derivative of HIH_I, and HIH_I is smaller than HKH_K by a factor of order ε=max⁡i≥1mi/m0\varepsilon = \max_{i \ge 1} m_i / m_0. So the modified Hamiltonian of backward error analysis differs from HH by O(εh2)O(\varepsilon h^2). This is an estimate under the assumption that the orbits stay separated, not a sharp constant.

Complexity

In the real RAM model with a fixed number of Newton iterations, one step costs Θ(N2)\Theta(N^2) for the Newtonian accelerations of one kick (the second kick of a step is the first of the next, when steps are joined), Θ(N)\Theta(N) for the Kepler drifts and Θ(N)\Theta(N) for the coordinate changes. The extra space is Θ(N)\Theta(N). In practice the Newton solve converges in a few iterations from a good start, and for a few planets the Kepler drifts dominate the cost.

Properties

  • Symplectic, symmetric and second order. Like leapfrog, but with an error constant smaller by a factor of about ε\varepsilon.
  • Exact for one planet. With one planet HI=0H_I = 0, so the map gives the exact two-body motion for any step (tested).
  • Bounded energy error. For the example, the relative energy error over 50 orbits stays below 2×10−62 \times 10^{-6} and does not grow (measurement). The total momentum is conserved up to rounding, because the kick does not change v0′\mathbf{v}'_0.
  • Time-reversible. Running the map backward with reversed velocities returns to the start up to rounding.

Variants and improvements

  • Democratic heliocentric coordinates. Heliocentric positions with barycentric velocities, which split HH into Kepler, interaction and a "jump" part; used for close encounters in the code of Duncan, Levison and Lee.
  • Symplectic correctors. A near-identity change of variables at the output removes the O(εh2)O(\varepsilon h^2) error, leaving O(ε2h2+εh4)O(\varepsilon^2 h^2 + \varepsilon h^4) (Wisdom, Holman and Touma 1996).
  • Higher-order splittings. Compositions of the kick and drift with more stages, designed for ε≪1\varepsilon \ll 1, as by McLachlan and by Laskar and Robutel.
  • Close encounters. The hybrid method of Chambers switches a close pair of planets to an adaptive integrator, and the code of Duncan, Levison and Lee splits the interaction by distance.
  • Fast implementations. The WHFast code of Rein and Tamayo uses universal Stumpff functions for the Kepler step and compensated summation.

When to use it

Use it for long integrations of planetary systems, satellite systems and other systems dominated by one central mass, where the orbits stay well separated, as in the stability of the solar system or of exoplanet systems. Use Hermite integrators with block time steps for star clusters without a dominant mass, and Kustaanheimo-Stiefel regularization for close pairs.

Pitfalls

  • Close encounters. When two planets come close, HIH_I is no longer small and the fixed step is too long; the energy error jumps.
  • Pericenter resolution. For eccentric orbits, the step must resolve the pericenter passage even though the Kepler part is exact; otherwise the kick at a far point misses the fast change of HIH_I, and symplectic maps show step-size resonances.
  • Changing the step. A symplectic map with a variable step loses its bounded energy error, unless the step change is itself done symplectically.
  • Output at the wrong point. The state between the half kick and the drift is not the state at a full time; synchronize with a final half kick before output.
  • Unbound orbits. The ff and gg functions of the eccentric anomaly fail for 1/a≤01/a \le 0; ejected bodies need universal variables.
  • Cancellation in the kick. Line L8 subtracts two nearly equal terms of size μi/ri′2\mu_i / r'^2_i; for very small masses compute the planet-planet terms and the indirect term directly instead.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

wisdom_holman_integrator.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/wisdom-holman-integrator/. The authors are listed in prov/JQMB4C.jsonl.
"""Wisdom-Holman mixed-variable symplectic integrator for planetary systems.

Body 0 is the central star. The state is written in Jacobi coordinates: x'_i
is the position of body i relative to the center of mass of bodies 0..i-1.
The Hamiltonian splits into a sum of Kepler problems, one per planet with
mu_i = G eta_i (eta_i = m_0 + ... + m_i), and an interaction part that depends
on the positions only. A step is kick (h/2), Kepler drift (h), kick (h/2).
The Kepler drift is exact, with the f and g functions of the eccentric
anomaly, for bound orbits. G = 1 unless given.
"""

import math
from typing import Sequence

Vec = Sequence[float]
State = list[list[float]]


def to_jacobi(m: Sequence[float], x: Sequence[Vec]) -> State:
    """Jacobi vectors of positions (or velocities, or accelerations).
    Entry 0 is the center of mass of all bodies."""
    n, dim = len(m), len(x[0])
    out = [[0.0] * dim for _ in range(n)]
    com = list(x[0])
    eta = m[0]
    for i in range(1, n):
        out[i] = [x[i][k] - com[k] for k in range(dim)]
        eta_new = eta + m[i]
        com = [(eta * com[k] + m[i] * x[i][k]) / eta_new for k in range(dim)]
        eta = eta_new
    out[0] = com
    return out


def from_jacobi(m: Sequence[float], xj: Sequence[Vec]) -> State:
    """Inverse of to_jacobi."""
    n, dim = len(m), len(xj[0])
    eta = [0.0] * n
    eta[0] = m[0]
    for i in range(1, n):
        eta[i] = eta[i - 1] + m[i]
    out = [[0.0] * dim for _ in range(n)]
    com = list(xj[0])
    for i in range(n - 1, 0, -1):
        com_prev = [com[k] - m[i] / eta[i] * xj[i][k] for k in range(dim)]
        out[i] = [xj[i][k] + com_prev[k] for k in range(dim)]
        com = com_prev
    out[0] = com
    return out


def accelerations(m: Sequence[float], x: Sequence[Vec], G: float = 1.0) -> State:
    """Newtonian accelerations of all bodies by direct summation."""
    n, dim = len(m), len(x[0])
    a = [[0.0] * dim for _ in range(n)]
    for p in range(n - 1):
        for q in range(p + 1, n):
            d = [x[q][k] - x[p][k] for k in range(dim)]
            r2 = sum(c * c for c in d)
            f = G / (r2 * math.sqrt(r2))
            for k in range(dim):
                a[p][k] += m[q] * f * d[k]
                a[q][k] -= m[p] * f * d[k]
    return a


def kick_accel(m: Sequence[float], xj: Sequence[Vec], G: float = 1.0) -> State:
    """Jacobi accelerations of the interaction Hamiltonian: the full Newtonian
    acceleration in Jacobi form minus the Kepler part -G eta_i x'_i / r'_i^3."""
    n, dim = len(m), len(xj[0])
    aj = to_jacobi(m, accelerations(m, from_jacobi(m, xj), G))  # @line L7
    aj[0] = [0.0] * dim
    eta = m[0]
    for i in range(1, n):
        eta += m[i]
        r = math.sqrt(sum(c * c for c in xj[i]))
        f = G * eta / (r * r * r)
        aj[i] = [aj[i][k] + f * xj[i][k] for k in range(dim)]  # @line L8
    return aj


def kepler_drift(x: Vec, v: Vec, mu: float, dt: float) -> tuple[list[float], list[float]]:
    """Exact Kepler motion for time dt of a bound orbit with x'' = -mu x / |x|^3."""
    dim = len(x)
    r0 = math.sqrt(sum(c * c for c in x))
    v2 = sum(c * c for c in v)
    inv_a = 2.0 / r0 - v2 / mu
    if inv_a <= 0.0:
        raise ValueError("kepler_drift needs a bound orbit")
    a = 1.0 / inv_a
    n = math.sqrt(mu * inv_a ** 3)
    ec = 1.0 - r0 * inv_a
    es = sum(x[k] * v[k] for k in range(dim)) / math.sqrt(mu * a)
    mean = n * dt
    # F(dE) = dE - ec sin dE + es (1 - cos dE) - mean is increasing, with its root in [mean - 2, mean + 2].
    lo, hi = mean - 2.0, mean + 2.0
    de = mean
    for _ in range(100):
        s, c = math.sin(de), math.cos(de)
        fval = de - ec * s + es * (1.0 - c) - mean  # @line L9
        if fval > 0.0:
            hi = de
        else:
            lo = de
        step = fval / (1.0 - ec * c + es * s)
        new = de - step
        if not lo < new < hi:
            new = 0.5 * (lo + hi)
        if abs(new - de) <= 1e-15 * max(1.0, abs(de)):
            de = new
            break
        de = new
    s, c = math.sin(de), math.cos(de)
    r = a * (1.0 - ec * c + es * s)
    f = 1.0 - a / r0 * (1.0 - c)
    g = dt - (de - s) / n
    fdot = -math.sqrt(mu * a) * s / (r * r0)
    gdot = 1.0 - a / r * (1.0 - c)
    x1 = [f * x[k] + g * v[k] for k in range(dim)]  # @line L10
    v1 = [fdot * x[k] + gdot * v[k] for k in range(dim)]
    return x1, v1


def wh_step(m: Sequence[float], x: Sequence[Vec], v: Sequence[Vec], h: float,
            G: float = 1.0) -> tuple[State, State]:
    """One kick-drift-kick step of size h in Cartesian coordinates."""
    n, dim = len(m), len(x[0])
    xj, vj = to_jacobi(m, x), to_jacobi(m, v)  # @line L1
    aj = kick_accel(m, xj, G)
    vj = [[vj[i][k] + 0.5 * h * aj[i][k] for k in range(dim)] for i in range(n)]  # @line L2
    eta = m[0]
    for i in range(1, n):
        eta += m[i]
        xj[i], vj[i] = kepler_drift(xj[i], vj[i], G * eta, h)  # @line L3
    xj[0] = [xj[0][k] + h * vj[0][k] for k in range(dim)]  # @line L4
    aj = kick_accel(m, xj, G)
    vj = [[vj[i][k] + 0.5 * h * aj[i][k] for k in range(dim)] for i in range(n)]  # @line L5
    return from_jacobi(m, xj), from_jacobi(m, vj)  # @line L6


def integrate(m: Sequence[float], x: Sequence[Vec], v: Sequence[Vec], h: float, steps: int,
              G: float = 1.0) -> tuple[State, State]:
    x = [list(p) for p in x]
    v = [list(q) for q in v]
    for _ in range(steps):
        x, v = wh_step(m, x, v, h, G)
    return x, v


def energy(m: Sequence[float], x: Sequence[Vec], v: Sequence[Vec], G: float = 1.0) -> float:
    n = len(m)
    kin = 0.5 * sum(m[i] * sum(c * c for c in v[i]) for i in range(n))
    pot = -sum(G * m[i] * m[q] / math.dist(x[i], x[q]) for i in range(n) for q in range(i + 1, n))
    return kin + pot
test_wisdom_holman_integrator.py 138 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/wisdom-holman-integrator/. The authors are listed in prov/JQMB4C.jsonl.
import math
import random

import pytest

from wisdom_holman_integrator import (accelerations, energy, from_jacobi, integrate, kepler_drift,
                                      to_jacobi, wh_step)

P = 2 * math.pi


def planets(eps):
    """A star and two planets of masses eps and 0.3 eps, near circular orbits at radii 1 and 1.6."""
    m = [1.0, eps, 0.3 * eps]
    x = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.6, 0.05]]
    v = [[0.0, 0.0, 0.0], [0.0, 1.0, 0.02], [-1 / math.sqrt(1.6), 0.0, 0.0]]
    return m, x, v


def rk4_kepler(x, v, mu, dt, n):
    s = list(x) + list(v)

    def f(s):
        r = math.sqrt(sum(c * c for c in s[:3]))
        return s[3:] + [-mu * c / r ** 3 for c in s[:3]]

    for _ in range(n):
        k1 = f(s)
        k2 = f([s[i] + dt / 2 * k1[i] for i in range(6)])
        k3 = f([s[i] + dt / 2 * k2[i] for i in range(6)])
        k4 = f([s[i] + dt * k3[i] for i in range(6)])
        s = [s[i] + dt / 6 * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]) for i in range(6)]
    return s[:3], s[3:]


def rk4_nbody(m, x, v, dt, n):
    """Classical Runge-Kutta in Cartesian coordinates, the independent reference."""
    nb = len(m)

    def axpy(y, c, z):
        return [[y[i][k] + c * z[i][k] for k in range(3)] for i in range(nb)]

    for _ in range(n):
        a1 = accelerations(m, x)
        x2, v2 = axpy(x, dt / 2, v), axpy(v, dt / 2, a1)
        a2 = accelerations(m, x2)
        x3, v3 = axpy(x, dt / 2, v2), axpy(v, dt / 2, a2)
        a3 = accelerations(m, x3)
        x4, v4 = axpy(x, dt, v3), axpy(v, dt, a3)
        a4 = accelerations(m, x4)
        x = [[x[i][k] + dt / 6 * (v[i][k] + 2 * v2[i][k] + 2 * v3[i][k] + v4[i][k]) for k in range(3)]
             for i in range(nb)]
        v = [[v[i][k] + dt / 6 * (a1[i][k] + 2 * a2[i][k] + 2 * a3[i][k] + a4[i][k]) for k in range(3)]
             for i in range(nb)]
    return x, v


def test_jacobi_round_trip():
    rng = random.Random(1)
    m = [rng.uniform(0.1, 2.0) for _ in range(6)]
    x = [[rng.uniform(-1, 1) for _ in range(3)] for _ in range(6)]
    back = from_jacobi(m, to_jacobi(m, x))
    for p, q in zip(back, x):
        assert p == pytest.approx(q, abs=1e-14)
    # entry 0 is the center of mass
    com = [sum(m[i] * x[i][k] for i in range(6)) / sum(m) for k in range(3)]
    assert to_jacobi(m, x)[0] == pytest.approx(com, abs=1e-14)


def test_kepler_drift_circular_quarter_turn():
    x1, v1 = kepler_drift([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], 1.0, P / 4)
    assert x1 == pytest.approx([0.0, 1.0, 0.0], abs=1e-13)
    assert v1 == pytest.approx([-1.0, 0.0, 0.0], abs=1e-13)


def test_kepler_drift_against_runge_kutta():
    rng = random.Random(3)
    for _ in range(3):
        x = [rng.uniform(-1.5, 1.5) for _ in range(3)]
        r = math.sqrt(sum(c * c for c in x))
        speed = rng.uniform(0.5, 0.9) * math.sqrt(2 / r)  # bound orbit
        d = [rng.gauss(0, 1) for _ in range(3)]
        nd = math.sqrt(sum(c * c for c in d))
        v = [speed * c / nd for c in d]
        dt = rng.uniform(0.5, 4.0)
        xk, vk = kepler_drift(x, v, 1.0, dt)
        xr, vr = rk4_kepler(x, v, 1.0, dt / 6000, 6000)
        assert math.dist(xk, xr) < 1e-7
        assert math.dist(vk, vr) < 1e-7


def test_one_planet_is_exact_for_any_step():
    m = [1.0, 1e-3]
    x = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]
    v = [[0.0, 0.0, 0.0], [0.0, 1.1, 0.0]]
    xa, _ = integrate(m, x, v, 0.9, 7)
    xj, vj = to_jacobi(m, x), to_jacobi(m, v)
    xe, _ = kepler_drift(xj[1], vj[1], 1.001, 6.3)
    exact = from_jacobi(m, [[xj[0][k] + 6.3 * vj[0][k] for k in range(3)], xe])
    for p, q in zip(xa, exact):
        assert math.dist(p, q) < 1e-12


def test_time_reversible():
    m, x, v = planets(1e-3)
    x1, v1 = integrate(m, x, v, P / 40, 300)
    x2, v2 = integrate(m, x1, [[-c for c in q] for q in v1], P / 40, 300)
    for p, q in zip(x2, x):
        assert math.dist(p, q) < 1e-9
    for p, q in zip(v2, v):
        assert math.dist([-c for c in p], q) < 1e-9


def test_bounded_energy_error_and_momentum():
    m, x, v = planets(1e-3)
    e0 = energy(m, x, v)
    p0 = [sum(m[i] * v[i][k] for i in range(3)) for k in range(3)]
    errs = []
    for _ in range(2000):
        x, v = wh_step(m, x, v, P / 40)
        errs.append(abs(energy(m, x, v) - e0) / abs(e0))
    assert max(errs) < 5e-6
    assert max(errs[1000:]) < 1.2 * max(errs[:1000])  # no drift
    for k in range(3):
        assert sum(m[i] * v[i][k] for i in range(3)) == pytest.approx(p0[k], abs=1e-15)


def test_second_order_against_runge_kutta_reference():
    m, x, v = planets(1e-3)
    ref, _ = rk4_nbody(m, x, v, P / 6000, 6000)
    errs = []
    for steps in (20, 40):
        xw, _ = integrate(m, x, v, P / steps, steps)
        errs.append(max(math.dist(p, q) for p, q in zip(xw, ref)))
    assert errs[1] < 1e-5
    assert 3.5 < errs[0] / errs[1] < 4.5  # 2^2 = 4

History

Wisdom and Holman (1991) introduced the map, in Jacobi coordinates, and used it to integrate the outer planets of the solar system for a billion years. It extended Wisdom's earlier mapping methods for asteroids, which averaged the short-period terms. Kinoshita, Yoshida and Nakai proposed a related splitting the same year. The symplectic correctors, the democratic heliocentric form and the hybrid schemes followed in the 1990s, and the method is now the basis of planetary integrators such as the MERCURY, SWIFT and REBOUND packages.

References

  1. Jack Wisdom and Matthew Holman, Symplectic maps for the n-body problem, Astronomical Journal 102(4), 1991, 1528-1538, the splitting in Jacobi coordinates and the kick-drift-kick map
  2. Ernst Hairer, Christian Lubich and Gerhard Wanner, Geometric Numerical Integration, 2nd edition, Springer, 2006, Chapter II (splitting methods) and Chapter IX (backward error analysis)
  3. J. M. A. Danby, Fundamentals of Celestial Mechanics, 2nd edition, Willmann-Bell, 1988, the f and g functions and the solution of Kepler's equation

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.step
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example