N-Body Gravitational Orbit Simulator

Computational Astrophysics · 2-to-4 Body System · Velocity Verlet / RK4 / Euler Student: Guest | SAPID: NA | Topic: N-Body Gravitational Dynamics
← PHYS4022P Hub
👤 Guest

🌌The N-Body Gravitational Problem

The N-body problem asks: given N point masses interacting solely through Newtonian gravity, how do their positions and velocities evolve with time? For N ≥ 3 no general closed-form solution exists — numerical integration is required. This simulator solves the equations of motion for up to 4 bodies in real time using three user-selectable integrators.

⚖️Newton's Law of Gravitation

The gravitational force on body i due to body j:

Fij = G · mi · mj / |rij

F⃗ij = G · mi · mj · (r⃗j − r⃗i) / |r⃗j − r⃗i

where G = 6.674 × 10¹¹ N m² kg² is the gravitational constant.

Equations of Motion

Summing over all pairs for body i:

mi · d²r⃗i/dt² = G · Σj≠i [ mi·mj · (r⃗j−r⃗i) / |r⃗j−r⃗i|³ ]

This yields 6N coupled first-order ODEs (3 position + 3 velocity per body).

🔢Conservation Laws

Total Mechanical Energy

E = K + U

K = ½ · Σi mi |v⃗i

U = −G · Σi<j mi·mj / |r⃗ij|

For an isolated system E = constant. Energy drift is a key diagnostic of integrator accuracy.

Angular Momentum (z-component)

Lz = Σi mi · (xi·vyi − yi·vxi)

Also conserved. Large drifts in Lz indicate numerical instability or a large timestep.

Centre-of-Mass Frame

The simulator shifts all bodies into the CoM frame at initialisation — ensuring zero net linear momentum.

🧮Numerical Integrators

1 · Euler Method (1st order)

vn+1 = vn + a(rn) · Δt
rn+1 = rn + vn · Δt

Simple but not symplectic — energy grows monotonically. Suitable only for qualitative demonstrations.

2 · Velocity Verlet (2nd order, symplectic)

rn+1 = rn + vn·Δt + ½·an·Δt²
vn+1 = vn + ½·(an + an+1)·Δt

Recommended for long integrations. Symplectic — exhibits bounded rather than drifting energy error.

3 · Runge–Kutta 4th Order (RK4)

yn+1 = yn + (Δt/6)·(k₁ + 2k₂ + 2k₃ + k₄)

4th-order accuracy per step. More accurate than Verlet for smooth orbits but not symplectic. 4 force evaluations per step.

🪐Physical Units & Scales

1 AU = 1.496 × 1011 m (Earth–Sun distance)
M☉ = 1.989 × 1030 kg (solar mass)
M⊕ = 5.972 × 1024 kg (Earth mass)
M♃ = 1.898 × 1027 kg (Jupiter mass)
G = 6.674 × 10−11 N m² kg−2

Circular Orbit Velocity

vcirc = √(G·M / r)

Initial velocities are set to vcirc around Body 1, producing approximately circular orbits.

Kepler's 3rd Law

T² = (4π² / G·M) · a³

Earth's period: T ≈ 365.25 days at a = 1 AU.

Gravitational Softening

F = G·mi·mj / (r² + ε²),  ε = 108 m

Prevents numerical divergence at very close separations.

🔍Key Simulation Scenarios

Sun–Earth (2-body)

Perfectly integrable. Earth orbits in an ellipse; period ≈ 365.25 days. Ideal for verifying energy and Lz conservation.

Sun–Earth–Moon (3-body)

Moon orbits Earth at ~0.00257 AU. Demonstrates hierarchical three-body stability; Moon period ≈ 27.3 days.

Adding a 4th Body

A 4th body near the inner system can cause orbital resonances, perturbations, or ejections — illustrating deterministic chaos sensitive to initial conditions.

Integrator Comparison

Run the same preset with all three integrators and compare energy drift over 10+ orbits to concretely see the impact of integrator choice.

Time: 0.00 days
Bodies: 0
Heaviest:
Zoom: 1.00×
How data logging works: When the Log data checkbox is enabled in the Simulation tab, the simulator records body positions and velocities at configurable intervals. Download the data in standard formats for analysis in Python, MATLAB, or Excel.
0
Recorded rows
0
Bodies tracked
0.0
Time span (days)
Every 1 day
Sampling interval

⚙️Logging Settings

💾Download Simulation Data

CSV columns: sapid, student_name, time_days, body_name, x_AU, y_AU, vx_ms, vy_ms, mass_kg, energy_j, lz_kgm2s. JSON includes full metadata (integrator, G scale, body names). The Python script uses matplotlib to reproduce the orbital plot from the exported CSV.

📋Live Data Preview (last 20 rows)

Time (days)Bodyx (AU)y (AU)vx (m/s)vy (m/s)Mass (kg)
No data yet — run the simulation with "Log data" checked.

🔒Student Login Audit

Total recorded logins: 0
Timestamp (ISO)NameSAPID
No login records yet.

🎯Module: Gravitational N-Body Dynamics

This interactive module is part of the Computational Astrophysics curriculum. Students explore Newton's law of gravitation through hands-on numerical simulation, compare integration algorithms, and analyse conservation laws in multi-body gravitational systems.

📌Learning Objectives

After completing this module, students will be able to:
  • State Newton's universal law of gravitation and write the N-body equations of motion.
  • Explain symplectic integrators and why they are preferred for long-term orbital integration.
  • Distinguish between Euler, Velocity Verlet, and RK4 in accuracy and energy conservation.
  • Identify conserved quantities (E, Lz) and use them to assess numerical accuracy.
  • Set up and simulate a Sun–Earth–Moon system with physically realistic parameters.
  • Investigate chaotic behaviour by adding a perturbing fourth body.
  • Export simulation data and perform basic analysis using Python/matplotlib.

📦Deliverables

Students are expected to submit:
  1. 1.Lab Report (PDF) — Screenshots of orbital traces for at least two integrators, energy/Lz plots, discussion.
  2. 2.Exported CSV file — Minimum 2 complete Earth orbits (~730 days) sampled at 0.5-day intervals.
  3. 3.Python analysis script — Orbital plot and energy-vs-time plot with annotations.
  4. 4.Comparison table — Energy drift (ΔE/E₀) for all three integrators over same duration.
  5. 5.Chaos investigation (bonus) — Document conditions under which the 4-body system becomes unstable.

📅Suggested Lab Workflow

Step-by-step guide
  1. 1.Read the Theory tab carefully. Note equations and physical units.
  2. 2.Load Sun–Earth preset; run ~5 orbits; record E and Lz.
  3. 3.Switch to Sun–Earth–Moon; observe the Moon's trajectory.
  4. 4.Switch integrator to Euler; reset and observe energy growth.
  5. 5.Compare with Velocity Verlet and RK4.
  6. 6.Enable Log data, run 730 days, download CSV.
  7. 7.Use the Python starter script to plot orbits and analyse conservation.
  8. 8.Add Body 4 (Jupiter mass) and explore chaotic behaviour.

📐Assessment Criteria

  • Understanding (30%) — Accuracy of theoretical explanation in report.
  • Simulation (25%) — Correct physical parameters and orbital period verification.
  • Data Analysis (25%) — Quality of Python plots and quantitative energy analysis.
  • Integrator comparison (15%) — Complete table with correct interpretation.
  • Bonus – Chaos (5%) — Evidence of genuine investigation of chaotic dynamics.
LMS Note: Submit your PDF, CSV, and Python script as a single ZIP to the course portal under Lab 4 — N-Body Dynamics.

🧠Concept Check Quiz

10 questions per round. Pass a round to unlock the next level with a stronger difficulty mix.
Adaptive mode: clear 6/10 or better to unlock the next level and a harder mixed set.
No attempts yet.

🎮Level Progress

Level 1/10
Mastery 0%
Questions 0/0
Answer 10 questions to complete a level. Passing scores unlock the next level with harder questions.

📝Quiz Attempt Log

Attempts: 0

📚Bibliography & Further Reading

References used to ensure scientific accuracy of this module. Open-access links are provided where available.

1
Murray, C. D. & Dermott, S. F. — Solar System Dynamics
Cambridge University Press, 1999. ISBN 978-0-521-57597-3.
The definitive graduate text on celestial mechanics, orbital resonances, and perturbation theory.
↗ Publisher page
2
Press, W. H. et al. — Numerical Recipes: The Art of Scientific Computing (3rd ed.)
Cambridge University Press, 2007. ISBN 978-0-521-88068-8.
Chapters 17–18 cover ODE integration (Euler, RK4, symplectic methods) with worked examples.
↗ numerical.recipes
3
Goldstein, H., Poole, C. & Safko, J. — Classical Mechanics (3rd ed.)
Addison-Wesley, 2002. ISBN 978-0-201-65702-9. Chapters 3 and 8: two-body problem and Hamiltonian mechanics.
4
Aarseth, S. J. — Gravitational N-Body Simulations: Tools and Algorithms
Cambridge University Press, 2003. ISBN 978-0-521-43272-6.
↗ Publisher page
5
Hairer, E., Lubich, C. & Wanner, G. — Geometric Numerical Integration (2nd ed.)
Springer, 2006. ISBN 978-3-540-30663-4. Authoritative treatment of symplectic integrators.
↗ Springer Link
6
NASA JPL — Solar System Ephemeris (DE440)
Mass values and orbital parameters for solar system bodies.
↗ JPL Solar System Dynamics
7
CODATA 2018 — NIST Reference on Constants, Units and Uncertainty
Newell, D. B. et al., Metrologia 56, 054001 (2019). Source for G = 6.67430 × 10−11 N m² kg−2.
↗ NIST CODATA G value

🛠Software & Tools Used

  • HTML5 Canvas API — Real-time 2D rendering of orbital trajectories.
  • Vanilla JavaScript (ES2020) — Simulation engine and UI logic. No external runtime dependencies.
  • Google Fonts (CDN) — Space Grotesk, IBM Plex Mono (falls back to system fonts offline).
  • Project Leadership — Created and maintained by Dr. Nitesh Kumar, UPES.