Interactive Newtonian N-body gravity simulator accelerated with C library.
Link: https://alvinng4.github.io/orbit_simulator_Web/
Click once after you see the green loading bar showing "Ready to start!". You should then see the main menu. From there, simply select a system to start. See controls for basic controls.
This online demo is built with the pygbag package.
Warning
In Safari, if you accidently selected/clicked the window, the FPS would drops significantly. To fix it, simply click on something else outside the game window.
Check out my other gravity simulator, with higher accuracy and ability to create 3D animated plots: https://github.com/alvinng4/Gravity-Simulator
- Quick Start
- Running the program
- Available systems
- Controls
- Available integrators
- Program keeps crashing. What should I do?
- Feedback and Bugs
- Data Sources
- References
- Acknowledgement
This program requires Python version 3.10 or higher.
Download the source files, or clone this repository by running the following command in terminal:
git clone https://github.com/alvinng4/OrbitSim
Install the required packages by
pip install .
Once you have downloaded the source files, navigate to the source directory in terminal and run
python orbit_sim [-n|--numpy] [-r|--resolution <width> <height>]
By default, the simulation is performed in C to improve performance. If you want to use numpy, run the program with
python orbit_sim [-n|--numpy]
The default resolution is set to the user's screen size. However, you can set your own resolution by the following command:
python3 orbit_sim [-r|--resolution <width> <height>]
System | Description |
---|---|
Void | Emptiness |
figure-8 | A "figure-8" orbit involving three stars |
pyth-3-body | Three stars arranged in a triangle with length ratios of 3, 4, and 5 |
solar_system | Solar System with the Sun and the planets |
Tip
Pythagorean three body orbit is a highly chaotic orbit with close encounters, which is useful to test the difference between fixed and variable step size integrators. For RK4, the largest dt to produce desired result is 2e-8 days.
Action | Control |
---|---|
Move camera | W A S D / ↑ ↓ ← → |
Menu | Esc |
Pause | P |
Toggle full-screen mode | F |
Hide user interface | H |
Reset parameters | R |
Create new star | Hold the right mouse button to create a star + drag the mouse to give it an initial boost. |
Adjust parameter values | Left-click the parameter on the parameters panel + scroll to change its value. |
Switch integrators | Left-click the integrator on the integrators panel. |
Warning
Switching integrators or changing dt in the middle of simulation may produce some numerical error.
Fixed step size integrators are simple methods to simulate the system with the given step size dt.
Fixed step size methods |
---|
Euler |
Euler Cromer |
Fourth Order Runge-Kutta (RK4) |
Leapfrog |
Embedded RK methods are adaptive methods that decides the step size automatically based on the estimated error. The system would adopt smaller step size for smaller tolerance.
Embdedded Runge-Kutta methods | Recommended tolerance* |
---|---|
Runge–Kutta–Fehlberg 4(5) | 1e-8 to 1e-14 |
Dormand–Prince method (DOPRI) 5(4) | 1e-8 to 1e-14 |
Verner's method (DVERK) 6(5) | 1e-8 to 1e-14 |
Runge–Kutta–Fehlberg 7(8) | 1e-4 to 1e-8 |
IAS15 (Implicit integrator with Adaptive time Stepping, 15th order) [1] is a highly optimized and efficient integrator.
Recommended tolerance*: 1e-9
*For reference only
If you find any bugs or want to leave some feedback, please feel free to let me know by sending an email to [email protected] or open an issue.
The solar system positions and velocities data at 1/Jan/2024 are collected from the Horizons System [1]. Gravitational constant, and masses of the solar system objects are calculated using the data from R.S. Park et. al. [2].
- H. Rein, and D. S. Spiegel, 2014, "IAS15: A fast, adaptive, high-order integrator for gravitational dynamics, accurate to machine precision over a billion orbits", Monthly Notices of the Royal Astronomical Society 446: 1424–1437.
- Horizons System, Jet Propulsion Laboratory, https://ssd.jpl.nasa.gov/horizons/
- R. S. Park, et al., 2021, “The JPL Planetary and Lunar Ephemerides DE440 and DE441”, https://ssd.jpl.nasa.gov/doc/Park.2021.AJ.DE440.pdf, Astronomical Journal, 161:105.
The integrators in this project were developed with great assistance from the following book:
- J. Roa, et al. Moving Planets Around: An Introduction to N-Body Simulations Applied to Exoplanetary Systems, MIT Press, 2020