This project is a Python simulation of the Three-Body Problem with real-time visualization using matplotlib and scipy. The simulation models three stars and a habitable planet, showing their gravitational interactions and dynamic orbits. You can restart the simulation with randomized initial conditions by pressing Ctrl-R.
- Real-time simulation and visualization of the Three-Body Problem.
- Adaptive time-stepping with improved solver accuracy.
- Dynamic plot centering to follow the stars and planet during their motion.
- Supports restarting the simulation with new random initial conditions using a keypress (
Ctrl-R).
The three-body problem is a classic physics problem where the motion of three masses is governed by Newton's laws of motion and gravitation. Due to the chaotic nature of the system, there is no general analytical solution, and it must be solved numerically.
In this simulation:
- Three stars of equal mass interact gravitationally.
- A habitable planet orbits within the system, also subject to the gravitational forces of the stars.
- The simulation provides a real-time display of the stars and the planet as they move.
- Python 3.8+
- pip (Python package installer)
To install the required libraries, run:
pip install numpy scipy matplotlibTo get started, clone the repository to your local machine:
git clone https://github.com/yourusername/three-body-simulation.git
cd three-body-simulationTo run the simulation, simply execute the Python script:
python ThreeBody.pyYou will see a dynamic visualization of the stars and the planet. The plot will auto-center to follow the motion of the celestial bodies.
- Ctrl-R: Restart the simulation with randomized initial conditions.
This is the main script that:
- Sets up the simulation using
scipy.integrate.solve_ivpfor solving the equations of motion. - Handles real-time plotting and visualization using
matplotlib.animation. - Implements gravitational interactions between the stars and the planet, with adaptive solver tolerances for numerical stability.
equations_of_motion: Defines the accelerations due to gravitational forces for each body.solve_ivp: Solves the system of differential equations numerically.FuncAnimation: Handles the real-time update of the plot.- Key Bindings: Detects the
Ctrl-Rkey combination to restart the simulation.
We use an adaptive Runge-Kutta method (RK45) with tight tolerance settings (rtol=1e-8 and atol=1e-10) to ensure numerical stability during close encounters between stars.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to fork the project and submit pull requests if you have improvements, bug fixes, or new features you'd like to add.
This project was built using the following libraries:
- NumPy - For numerical operations.
- SciPy - For solving differential equations.
- Matplotlib - For real-time visualization.