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{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{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{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{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{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{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{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{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}}} + \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.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{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{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{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.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{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}}} \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.43456238545920783
4.780186240051286
48.23642478597206
195.27337773985937
404.37880043365203
641.2461951827266
917.4024243418721
1259.375074434556
1620.723519074113
⋮
199469.90015912452
199550.2939495557
199620.51345781717
199697.88015266228
199765.84252853855
199832.36322265412
199887.0449782007
199959.30871681948
200000.0
u: 862-element Vector{Vector{Float64}}:
[-3.1902382, -0.00136504, -25.2225594, -0.00170702, -15.5387357, 0.00276725, -10.8169456, 0.00039677, -25.7294829, 0.00114527 … -3.8169847, -0.0041249, -3.5023653, 0.00565429, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[-3.190831391666314, -0.0013650244789412218, -25.223301179998256, -0.001706897186454997, -15.537533140796315, 0.002767325672882192, -10.816773167663484, 0.0003968207122091521, -25.728985182525822, 0.0011453906312636451 … -3.818776551780467, -0.004121794983806699, -3.4999075394383845, 0.005657137663985553, -2.777762988623254e-10, -1.2785303085738865e-9, -6.74577349634531e-10, -3.104871144001842e-9, -5.098303779711273e-10, -2.346105131364384e-9]
[-3.196762937128868, -0.0013648691152799539, -25.230716044387762, -0.0017056688965533717, -15.52550574063629, 0.002768082053772159, -10.815047632292558, 0.0003973277927111847, -25.724005124769256, 0.0011465968224111933 … -3.8366207222927584, -0.004090656421676488, -3.4752620277231188, 0.005685511868486027, -3.366964879118852e-8, -1.4100666088910384e-8, -8.175135524038135e-8, -3.4233554324164556e-8, -6.15298818910492e-8, -2.5706941518695228e-8]
[-3.2560410257920296, -0.0013633001902609797, -25.304570881838067, -0.0016933706555559782, -15.405051486626984, 0.0027756110286434707, -10.797671145111675, 0.0004023944185903396, -25.673916441625973, 0.0011586465368538337 … -4.007487516495138, -0.003770599600091882, -3.2221835308829867, 0.0059587932915218115, -3.4874659022100094e-6, -1.4594000158085e-7, -8.452307004570987e-6, -3.53353735173139e-7, -6.100402511077378e-6, -2.4909025937027336e-7]
[-3.4560950438194866, -0.001357786536892723, -25.55048864881513, -0.001651557531681458, -14.995086973641925, 0.002800613918439817, -10.737247074610087, 0.00041948022029475646, -25.500564258550423, 0.0011992504322559396 … -4.476328274083103, -0.002580656082263763, -2.286242708424173, 0.006730897201673274, -6.024100672070627e-5, -6.369559649765012e-7, -0.00014514825020175705, -1.529138371536941e-6, -9.029656259744761e-5, -8.562666292142245e-7]
[-3.7391503253370364, -0.0013494060257915718, -25.88957839011003, -0.001591589478503688, -14.405852122492028, 0.002834905191574055, -10.647004315296812, 0.000443619661740527, -25.24379557801365, 0.001256535830385216 … -4.819472301720941, -0.0006664593990847965, -0.8022848285526653, 0.0073650062276909385, -0.0002747692410211364, -1.4300961982037631e-6, -0.0006569678433674013, -3.3960859156204777e-6, -0.00032246353219088787, -1.2700600202222377e-6]
[-4.057582856734454, -0.0013391609150743518, -26.25846463383041, -0.001523008173343417, -13.729932034469996, 0.002871931193437516, -10.53870984206983, 0.00047072594647005735, -24.938543951977763, 0.0013207457633296043 … -4.703474908002007, 0.001654859960900065, 0.9542708808238614, 0.007324487906032095, -0.0007276022576520437, -2.3985121859603296e-6, -0.0017256604570546575, -5.631384329495244e-6, -0.0006083763727699905, -1.0075133892324636e-6]
[-4.425639265202051, -0.0013262311425467392, -26.667923865275117, -0.0014422802284886843, -12.931149248426918, 0.0029126298323277314, -10.404390682372131, 0.0005019874376483291, -24.563583836576818, 0.001394641388147916 … -3.882175023379044, 0.004236424139516342, 2.845652151911801, 0.006174492073878072, -0.0015432638606036715, -3.487397690399135e-6, -0.0036276110616956887, -8.082240979433919e-6, -0.0007246641716726974, 3.5546718543275755e-7]
[-4.876236753538832, -0.001308794260336332, -27.14391262024817, -0.001341340187806172, -11.927010904521099, 0.002959304010544335, -10.226181232787367, 0.0005401516040701683, -24.071220358929434, 0.0014846157954746795 … -1.9952056202291168, 0.006575790764247322, 4.495930608252939, 0.0032307257053529235, -0.002923904354474398, -4.497046006563033e-6, -0.006799227714296786, -1.0242905047747264e-5, -0.00010701718486927391, 3.4901821399911105e-6]
[-5.345590814017202, -0.0012887220283255018, -27.609190764455516, -0.0012337563665169732, -10.849435759690564, 0.0030041096081688755, -10.023818065895448, 0.0005797663178001922, -23.517872437082897, 0.0015777192883279381 … 0.5606535208229478, 0.007201496082832906, 4.920174440735772, -0.0009678270857685025, -0.004632370364695391, -4.8051349879131565e-6, -0.010648108542765492, -1.069645302652261e-5, 0.001922857878051442, 7.821335996287818e-6]
⋮
[-15.123523880073716, 0.00013284032063680582, -14.882994657378239, 0.002074724802454574, 35.67769472308447, 0.001713628210345406, 7.436621644547421, 0.0008806862614728671, 19.539227497268236, 0.0020135310906586766 … -1.1643981895619295, 0.011342882694096609, 2.373090420825739, 0.008108459845755193, -0.24733895576641196, -5.0936020424311874e-6, -0.4930482161662788, -1.1856992687665248e-5, 1.2328033718798685, -1.3756763976225038e-6]
[-15.112625185782635, 0.00013828809834930464, -14.715988438783155, 0.0020799659130910295, 35.8149515584494, 0.001700984773655596, 7.507144477365168, 0.0008737379661163307, 19.70037613155878, 0.0019954268202432295 … -0.12493114606224337, 0.012846267147291228, 2.541229017109021, -0.004187624400617065, -0.2478118051969559, -5.99276600243798e-6, -0.49412351429213364, -1.3299592555491402e-5, 1.2331516774802105, 1.0290986775687222e-5]
[-15.102748228612011, 0.0001430252212320266, -14.56977552156746, 0.002084476570867666, 35.93400616551369, 0.0016899421899713256, 7.568283311017907, 0.00086761721770305, 19.8399353176335, 0.0019794954653617053 … 0.5915801232007423, 0.006546786805771362, 1.91603017444622, -0.012770346826891264, -0.2481643961652316, -3.6152110604820575e-6, -0.49488054381184726, -7.2937782817482535e-6, 1.2341882469382734, 1.8422199884253616e-5]
[-15.091481670674318, 0.00014822164206314498, -14.408316515174947, 0.002089374222536334, 36.064280799247534, 0.0016777767590994447, 7.635145346524086, 0.0008608180610764067, 19.992399295658046, 0.001961816178807624 … 0.6750327459390678, -0.004589218834277804, 0.8132249189685125, -0.014001166798708714, -0.2482737634964013, 9.09663375507148e-7, -0.4950413792875162, 3.3263000211009072e-6, 1.2357204515313662, 1.9528391518336594e-5]
[-15.08125362551423, 0.00015276667910690201, -14.266173261088861, 0.0020936145177145155, 36.17794338259126, 0.001667091313662994, 7.693444262111297, 0.0008547977779873281, 20.12519813456571, 0.0019461774704407691 … 0.0753380566727822, -0.01224547580947474, 0.06405183992755256, -0.0069426827267978965, -0.24808961811610408, 4.2021007868685925e-6, -0.49454096345180587, 1.062323578450124e-5, 1.2368523849369706, 1.272935770623535e-5]
[-15.070944013333705, 0.00015719744089874889, -14.126768080934582, 0.0020977089884715236, 36.2884916289678, 0.001656633916118789, 7.750108823872253, 0.0008488623664954016, 20.254147385647624, 0.0019307730108758074 … -0.8170119372293512, -0.013316857213057923, -0.043716320947437236, 0.0038863401998018364, -0.24777020174710365, 4.887271934758809e-6, -0.4937605249123162, 1.1631352118957386e-5, 1.2373591762423182, 2.332405034635597e-6]
[-15.062248907760994, 0.00016082640711299163, -14.011970605776282, 0.0021010335309454044, 36.378844278062445, 0.0016480388543747007, 7.796391977934791, 0.0008439517926315404, 20.35937756370975, 0.0019180384881624787 … -1.4404526081184275, -0.008759825053230294, 0.3913845836404639, 0.011515697617529243, -0.24754075317027113, 3.196022137281285e-6, -0.4932248803922952, 7.266770609283673e-6, 1.237272913491196, -4.998634249787092e-6]
[-15.050454245073123, 0.00016560390422353373, -13.859984986507277, 0.0021053702245809737, 36.49752737090725, 0.001636682038410312, 7.8571433449071755, 0.0008374190480838476, 20.49737124963343, 0.0019011111362364555 … -1.7118857098472784, 0.0016317486025170817, 1.398692836747585, 0.014801735498347658, -0.24745385494724664, -9.844362330944119e-7, -0.4930456973524687, -2.6748331574582576e-6, 1.2367422730794975, -8.196749217624223e-6]
[-15.043661040106313, 0.00016828493415680293, -13.774265594520848, 0.0021077838588411286, 36.56399597152567, 0.0016302880834577558, 7.891143772372193, 0.0008337189626938671, 20.57453509756263, 0.0018915305767049102 … -1.5256262784807415, 0.0073521407702557716, 1.9645692422512997, 0.012544147979040984, -0.24754385873275253, -3.3802622714674945e-6, -0.4932692265497591, -8.148726010549189e-6, 1.2364428294256808, -6.07487446336594e-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")