Simulating the Outer Solar System
Data
The chosen units are masses relative to the sun, meaning the sun has mass $1$. We have taken $m_0 = 1.00000597682$ to take account of the inner planets. Distances are in astronomical units, times in earth days, and the gravitational constant is thus $G = 2.95912208286 \cdot 10^{-4}$.
| planet | mass | initial position | initial velocity |
|---|---|---|---|
| Jupiter | $m_1 = 0.000954786104043$ | [-3.5023653, -3.8169847, -1.5507963] | [0.00565429, -0.00412490, -0.00190589] |
| Saturn | $m_2 = 0.000285583733151$ | [9.0755314, -3.0458353, -1.6483708] | [0.00168318, 0.00483525, 0.00192462] |
| Uranus | $m_3 = 0.0000437273164546$ | [8.3101420, -16.2901086, -7.2521278] | [0.00354178, 0.00137102, 0.00055029] |
| Neptune | $m_4 = 0.0000517759138449$ | [11.4707666, -25.7294829, -10.8169456] | [0.00288930, 0.00114527, 0.00039677] |
| Pluto | $m_5 = 1/(1.3 \cdot 10^8 )$ | [-15.5387357, -25.2225594, -3.1902382] | [0.00276725, -0.00170702, -0.00136504] |
The data is taken from the book “Geometric Numerical Integration” by E. Hairer, C. Lubich and G. Wanner.
import Plots, OrdinaryDiffEq as ODE
import ModelingToolkit as MTK
using ModelingToolkit: t_nounits as t, D_nounits as D, @mtkbuild, @variables
Plots.gr()
G = 2.95912208286e-4
M = [
1.00000597682,
0.000954786104043,
0.000285583733151,
0.0000437273164546,
0.0000517759138449,
1 / 1.3e8
]
planets = ["Sun", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
pos = [0.0 -3.5023653 9.0755314 8.310142 11.4707666 -15.5387357
0.0 -3.8169847 -3.0458353 -16.2901086 -25.7294829 -25.2225594
0.0 -1.5507963 -1.6483708 -7.2521278 -10.8169456 -3.1902382]
vel = [0.0 0.00565429 0.00168318 0.00354178 0.0028893 0.00276725
0.0 -0.0041249 0.00483525 0.00137102 0.00114527 -0.00170702
0.0 -0.00190589 0.00192462 0.00055029 0.00039677 -0.00136504]
tspan = (0.0, 200_000.0)(0.0, 200000.0)The N-body problem's Hamiltonian is
\[H(p,q) = \frac{1}{2}\sum_{i=0}^{N}\frac{p_i^T p_i}{m_i} - G\sum_{i=1}^N \sum_{j=0}^{i-1}\frac{m_i m_j}{\left\lVert q_i - q_j \right\rVert}\]
where each $p_i$ and $q_i$ is a 3-dimensional vector describing the planet's position and momentum, respectively.
Here, we want to solve for the motion of the five outer planets relative to the sun, namely, Jupiter, Saturn, Uranus, Neptune, and Pluto.
const ∑ = sum
const N = 6
@variables u(t)[1:3, 1:N]
u = collect(u)
potential = -G *
∑(
i -> ∑(j -> (M[i] * M[j]) / √(∑(k -> (u[k, i] - u[k, j])^2, 1:3)), 1:(i - 1)),
2:N)\[ \begin{equation} - 0.00029591 ~ \left( \frac{4.3728 \cdot 10^{-5}}{\sqrt{\left( - u\_{1,1}\left( t \right) + u\_{1,4}\left( t \right) \right)^{2} + \left( - u\_{2,1}\left( t \right) + u\_{2,4}\left( t \right) \right)^{2} + \left( - u\_{3,1}\left( t \right) + u\_{3,4}\left( t \right) \right)^{2}}} + \frac{0.00095479}{\sqrt{\left( - u\_{1,1}\left( t \right) + u\_{1,2}\left( t \right) \right)^{2} + \left( - u\_{2,1}\left( t \right) + u\_{2,2}\left( t \right) \right)^{2} + \left( - u\_{3,1}\left( t \right) + u\_{3,2}\left( t \right) \right)^{2}}} + \frac{1.4786 \cdot 10^{-8}}{\sqrt{\left( - u\_{1,3}\left( t \right) + u\_{1,5}\left( t \right) \right)^{2} + \left( - u\_{2,3}\left( t \right) + u\_{2,5}\left( t \right) \right)^{2} + \left( - u\_{3,3}\left( t \right) + u\_{3,5}\left( t \right) \right)^{2}}} + \frac{2.1968 \cdot 10^{-12}}{\sqrt{\left( - u\_{1,3}\left( t \right) + u\_{1,6}\left( t \right) \right)^{2} + \left( - u\_{2,3}\left( t \right) + u\_{2,6}\left( t \right) \right)^{2} + \left( - u\_{3,3}\left( t \right) + u\_{3,6}\left( t \right) \right)^{2}}} + \frac{2.264 \cdot 10^{-9}}{\sqrt{\left( - u\_{1,4}\left( t \right) + u\_{1,5}\left( t \right) \right)^{2} + \left( - u\_{2,4}\left( t \right) + u\_{2,5}\left( t \right) \right)^{2} + \left( - u\_{3,4}\left( t \right) + u\_{3,5}\left( t \right) \right)^{2}}} + \frac{3.9828 \cdot 10^{-13}}{\sqrt{\left( - u\_{1,5}\left( t \right) + u\_{1,6}\left( t \right) \right)^{2} + \left( - u\_{2,5}\left( t \right) + u\_{2,6}\left( t \right) \right)^{2} + \left( - u\_{3,5}\left( t \right) + u\_{3,6}\left( t \right) \right)^{2}}} + \frac{5.1776 \cdot 10^{-5}}{\sqrt{\left( - u\_{1,1}\left( t \right) + u\_{1,5}\left( t \right) \right)^{2} + \left( - u\_{2,1}\left( t \right) + u\_{2,5}\left( t \right) \right)^{2} + \left( - u\_{3,1}\left( t \right) + u\_{3,5}\left( t \right) \right)^{2}}} + \frac{1.2488 \cdot 10^{-8}}{\sqrt{\left( - u\_{1,3}\left( t \right) + u\_{1,4}\left( t \right) \right)^{2} + \left( - u\_{2,3}\left( t \right) + u\_{2,4}\left( t \right) \right)^{2} + \left( - u\_{3,3}\left( t \right) + u\_{3,4}\left( t \right) \right)^{2}}} + \frac{3.3636 \cdot 10^{-13}}{\sqrt{\left( - u\_{1,4}\left( t \right) + u\_{1,6}\left( t \right) \right)^{2} + \left( - u\_{2,4}\left( t \right) + u\_{2,6}\left( t \right) \right)^{2} + \left( - u\_{3,4}\left( t \right) + u\_{3,6}\left( t \right) \right)^{2}}} + \frac{7.6924 \cdot 10^{-9}}{\sqrt{\left( - u\_{1,1}\left( t \right) + u\_{1,6}\left( t \right) \right)^{2} + \left( - u\_{2,1}\left( t \right) + u\_{2,6}\left( t \right) \right)^{2} + \left( - u\_{3,1}\left( t \right) + u\_{3,6}\left( t \right) \right)^{2}}} + \frac{4.9435 \cdot 10^{-8}}{\sqrt{\left( - u\_{1,2}\left( t \right) + u\_{1,5}\left( t \right) \right)^{2} + \left( - u\_{2,2}\left( t \right) + u\_{2,5}\left( t \right) \right)^{2} + \left( - u\_{3,2}\left( t \right) + u\_{3,5}\left( t \right) \right)^{2}}} + \frac{0.00028559}{\sqrt{\left( - u\_{1,1}\left( t \right) + u\_{1,3}\left( t \right) \right)^{2} + \left( - u\_{2,1}\left( t \right) + u\_{2,3}\left( t \right) \right)^{2} + \left( - u\_{3,1}\left( t \right) + u\_{3,3}\left( t \right) \right)^{2}}} + \frac{4.175 \cdot 10^{-8}}{\sqrt{\left( - u\_{1,2}\left( t \right) + u\_{1,4}\left( t \right) \right)^{2} + \left( - u\_{2,2}\left( t \right) + u\_{2,4}\left( t \right) \right)^{2} + \left( - u\_{3,2}\left( t \right) + u\_{3,4}\left( t \right) \right)^{2}}} + \frac{7.3445 \cdot 10^{-12}}{\sqrt{\left( - u\_{1,2}\left( t \right) + u\_{1,6}\left( t \right) \right)^{2} + \left( - u\_{2,2}\left( t \right) + u\_{2,6}\left( t \right) \right)^{2} + \left( - u\_{3,2}\left( t \right) + u\_{3,6}\left( t \right) \right)^{2}}} + \frac{2.7267 \cdot 10^{-7}}{\sqrt{\left( - u\_{1,2}\left( t \right) + u\_{1,3}\left( t \right) \right)^{2} + \left( - u\_{2,2}\left( t \right) + u\_{2,3}\left( t \right) \right)^{2} + \left( - u\_{3,2}\left( t \right) + u\_{3,3}\left( t \right) \right)^{2}}} \right) \end{equation} \]
Hamiltonian System
NBodyProblem constructs a second order ODE problem under the hood. We know that a Hamiltonian system has the form of
\[\dot{p} = -\frac{\partial H}{\partial q}, \quad \dot{q} = \frac{\partial H}{\partial p}\]
For an N-body system, we can simplify this as:
\[\dot{p} = -\nabla V(q), \quad \dot{q} = M^{-1} p.\]
Thus, $\dot{q}$ is defined by the masses. We only need to define $\dot{p}$, and this is done internally by taking the gradient of $V$. Therefore, we only need to pass the potential function and the rest is taken care of.
eqs = vec(@. D(D(u))) .~ .-MTK.gradient(potential, vec(u)) ./
repeat(M, inner = 3)
@mtkbuild sys = MTK.System(eqs, t)
prob = ODE.ODEProblem(sys, [vec(u .=> pos); vec(D.(u) .=> vel)], tspan)
sol = ODE.solve(prob, ODE.Tsit5());retcode: Success
Interpolation: specialized 4th order "free" interpolation
t: 862-element Vector{Float64}:
0.0
0.4345623854592078
35.99898429859733
174.61856264192386
380.59547356844905
611.8666224958926
881.0171294254685
1216.5292888230308
1576.6203478300724
1950.5247369459682
⋮
199464.1763426005
199535.15658191757
199610.59925970965
199680.2127842468
199755.12591135743
199821.69348971432
199894.37470391282
199963.8563633727
200000.0
u: 862-element Vector{Vector{Float64}}:
[-3.1902382, -25.2225594, -15.5387357, -10.8169456, -25.7294829, 11.4707666, -7.2521278, -16.2901086, 8.310142, -1.6483708 … 0.00354178, 0.00192462, 0.00483525, 0.00168318, -0.00190589, -0.0041249, 0.00565429, 0.0, 0.0, 0.0]
[-3.190831391666314, -25.223301179998256, -15.537533140796315, -10.816773167663484, -25.728985182525822, 11.472022169413352, -7.251888638015288, -16.289512746418094, 8.31168109382013, -1.6475343823037847 … 0.0035416394156575887, 0.0019248511933338966, 0.004835677090231295, 0.0016819059109041002, -0.0019046284357956216, -0.004121794983806699, 0.005657137663985553, -1.2785303085738884e-9, -3.1048711440018464e-9, -2.3461051313643876e-9]
[-3.2393549980716214, -25.28382714926436, -15.439004932934363, -10.802586698126767, -25.68807456291928, 11.574698023221423, -7.232135255028369, -16.240343014505505, 8.437431867001305, -1.5787461360440151 … 0.003530050705299505, 0.0019434002837321665, 0.004869678113502438, 0.0015771360623620687, -0.0017989198926536812, -0.003862300332365893, 0.005883789353087844, -1.0815714572233175e-7, -2.6207016211975343e-7, -1.8809265799702778e-7]
[-3.4280420079701845, -25.516315174827085, -15.052897344777248, -10.74588663345915, -25.525275821246, 11.973377190937034, -7.151759201209805, -16.04109905292252, 8.92355578462731, -1.3047059800667917 … 0.0034833220382023663, 0.0020085086352349597, 0.004983872848475712, 0.001159925238119635, -0.0013434734330002656, -0.0027569266671721732, 0.006637565644492371, -5.641293116780171e-7, -1.355853291430368e-6, -7.856804265309118e-7]
[-3.707045233737575, -25.851643637032474, -14.473230154989114, -10.657522537472943, -25.27360304517124, 12.561169018902879, -7.022513730851573, -15.723033045969192, 9.633516314698607, -0.8828569747252994 … 0.0034093846352291437, 0.0020830012488561394, 0.005097364894807607, 0.0005177706296843234, -0.0005615639064162919, -0.0008938228560599242, 0.00732295326725339, -1.3356559430885386e-6, -3.1755768738352596e-6, -1.2520419390932113e-6]
[-4.018219555698541, -26.213593809252725, -13.814242309814366, -10.55249040041501, -24.977230530048878, 13.214270892868017, -6.863712622101781, -15.335273269499737, 10.411813081950893, -0.3946555485512191 … 0.003320134583039653, 0.0021327085030388186, 0.005140272710915692, -0.00022593519308113902, 0.0004056976190662129, 0.0013657697967930863, 0.00737629704852887, -2.2774483152877735e-6, -5.354540757180861e-6, -1.0850151360140068e-6]
[-4.377351821263963, -26.615251742595444, -13.037031515584674, -10.4225811285836, -24.61415232483528, 13.964702392241312, -6.661187598566764, -14.844430810695425, 11.29056443664343, 0.18184560197133734 … 0.0032082766977699646, 0.0021422344170847346, 0.005071760741873903, -0.0011053612366158495, 0.001524119679033659, 0.00391913317522403, 0.006393763850831102, -3.352643743731525e-6, -7.783412619735192e-6, 1.1117424004516135e-7]
[-4.820111737629151, -27.086169758809156, -12.053684429277054, -10.249222799506164, -24.134590564238305, 14.88478091799421, -6.383017215336513, -14.175283374934832, 12.342012873833715, 0.8921234781572502 … 0.003057381822894924, 0.002077267176953791, 0.004801498391555867, -0.00219007808374467, 0.0026373811216874425, 0.006361810268772439, 0.0036755225624795053, -4.4025951139865464e-6, -1.005113500232717e-5, 3.0255810736516264e-6]
[-5.288698031255902, -27.55448768068402, -10.981811680435065, -10.049281927244104, -23.587206884911865, 15.8520692715453, -6.054200506543434, -13.389902886563227, 13.411790811130421, 1.6130640693535738 … 0.002882098483398166, 0.001910140608322444, 0.004282315243518839, -0.0032893506670255575, 0.0031164430292441457, 0.007245658434674586, -0.00043809969483555244, -4.818016508531506e-6, -1.0759768964913463e-5, 7.277884644905893e-6]
[-5.767395463053767, -27.999807278078084, -9.852496757125376, -9.826742706901163, -22.983785100386633, 16.83289924854452, -5.681515563669695, -12.50527369793824, 14.453259951043403, 2.2785511623098795 … 0.0026864469850750635, 0.0016324385357110599, 0.0035049450533046413, -0.004295404494929296, 0.0026116141120660847, 0.005830469215816499, -0.004617706489447968, -4.262396980911033e-6, -9.199407080435744e-6, 1.1567784087392676e-5]
⋮
[-15.12428321145426, -14.894874081334379, 35.667880311593166, 7.4315776951389605, 19.527694829589173, 21.925029902362844, 7.078852434351263, 16.078827530312687, -3.779058341746134, -0.31137881425065644 … -0.0038077595252227576, -0.0021362445252223108, -0.005131207063513987, 0.00033960493122794493, 0.0057634393409658315, 0.013411117084152619, -0.000724469903621151, -6.144047199049043e-6, -1.3831359131893112e-5, 7.062596806457794e-6]
[-15.114710858659436, -14.74747139770558, 35.789181791619086, 7.493906799414991, 19.670140847882386, 21.763222135660016, 7.045893479723227, 15.99492077658409, -4.04876070501613, -0.46291236561743043 … -0.0037914569940202987, -0.002132891443935548, -0.0050992330755615265, 0.0005673012351435305, 0.00415292593475153, 0.009088380276305609, -0.010694180169522076, -4.605988344596329e-6, -9.710109400497054e-6, 1.6516590126110436e-5]
[-15.104163016575788, -14.590443479633441, 35.917240726127105, 7.559675650564673, 19.820295168829617, 21.58997228355702, 7.009062917501749, 15.901670718128706, -4.334114311936603, -0.6235657813361021 … -0.0037731551668865955, -0.0021253907795129433, -0.005056012249711443, 0.0008065985304772452, -0.00012125544499117648, -0.0011150912483529805, -0.014716418089412023, -5.2581250719376e-7, 2.293297831075599e-8, 2.028869895561162e-5]
[-15.094090024875962, -14.445225638598082, 36.03461109434366, 7.619921572132155, 19.95769957058487, 21.42895771930952, 6.973437719918136, 15.811919443793055, -4.596163132017202, -0.7711770863362468 … -0.003755378273250854, -0.002114927659561817, -0.005007861075214955, 0.001024577622422391, -0.004174989546655308, -0.01030655218793932, -0.009941094064845496, 3.3429289853874026e-6, 8.788061526939474e-6, 1.5667103277921365e-5]
[-15.082887077037949, -14.2886113729952, 36.160065534762005, 7.684277012530359, 20.10432466176581, 21.254461476528693, 6.933347106815876, 15.71137646548158, -4.876744204094014, -0.9290754611443719 … -0.0037352958226249144, -0.002099941274711382, -0.004947384959897934, 0.0012557684635037913, -0.005914457227321276, -0.013714384274526491, 0.0015911414045346097, 5.000861017418266e-6, 1.2027804229068342e-5, 4.590322135364936e-6]
[-15.072617637514437, -14.149151810999962, 36.270803622342584, 7.741044998762698, 20.233529580763403, 21.098348757614914, 6.896202570000974, 15.61860374414486, -5.124776605861796, -1.0683309691256593 … -0.00371662258487358, -0.002083450192965714, -0.004886307957185618, 0.0014579657718091072, -0.004127768088198349, -0.008995278383096824, 0.011298737607877113, 3.291500821785404e-6, 7.507565382821609e-6, -4.736091242059667e-6]
[-15.06106848238177, -13.996574213275812, 36.39091653815536, 7.802573909148575, 20.37342626252894, 20.926773328706137, 6.854018931379957, 15.513639585596032, -5.394137651459188, -1.2190038670401349 … -0.0036953459140358926, -0.0020621049845998825, -0.00481194394771111, 0.001674927483288424, 0.0002411778246242779, 0.0014023657596519635, 0.014843420826708972, -8.846206401029478e-7, -2.437961485763195e-6, -8.182542643787225e-6]
[-15.049700635517883, -13.850415133881445, 36.504965631009156, 7.860949121362133, 20.50601071106621, 20.761661940075516, 6.81210963146918, 15.409728745894729, -5.650164631691982, -1.3614804101314608 … -0.003674141068707149, -0.0020385077879030607, -0.004733552691032501, 0.0018783500766333657, 0.004227914291723056, 0.010411150574796236, 0.009603033468161, -4.696520395831749e-6, -1.10587272355188e-5, -3.2373546988628376e-6]
[-15.043661226907014, -13.774270843429269, 36.563992817014835, 7.891142208622381, 20.574531427123674, 20.675354847003064, 6.789699247604722, 15.354302034897824, -5.782756769087059, -1.4349182809487384 … -0.0036627770095602153, -0.0020250238346580327, -0.004690020656762545, 0.001982543863559828, 0.0054353243107729396, 0.01293953488877421, 0.004479354345387266, -5.852508555675117e-6, -1.3483635473863919e-5, 1.6247891406544974e-6]plt = Plots.plot()
for i in 1:N
Plots.plot!(plt, sol, idxs = (u[:, i]...,), lab = planets[i])
end
Plots.plot!(plt; xlab = "x", ylab = "y", zlab = "z", title = "Outer solar system")