This Python script simulates the motion and interaction of celestial bodies in a dynamical system. It models gravitational forces and inelastic collisions between celestial bodies.
The simulation consists of three classes:
-
DynamicalSystem:
- Represents the overall dynamical system containing celestial bodies.
- Allows for the addition of celestial bodies, updates their motion, and displays their positions.
- Calculates gravitational interactions between celestial bodies.
-
NBodies:
- Represents a celestial body in the system.
- Defines parameters such as mass, position, velocity, and density.
- Updates the body's motion, displays it in the 3D plot, and calculates gravitational interactions with other bodies.
-
CelestialBody(NBodies):
- Inherits from NBodies and adds properties like color to represent a celestial body with additional visual features.
# Example usage:
system = DynamicalSystem(size=400, projection2D=True , view = (10,0))
# Sun
sun = CelestialBody(system, mass=1000, position=np.array([0.0, 0.0, 0.0]), velocity=np.array([0.0, 0.0, 0.0]), color="yellow")
# Planets
mercury = CelestialBody(system, mass=1, position=np.array([30.0, 0.0, 0.0]), velocity=np.array([0.0, 1.3, 0.0]), color="grey")
venus = CelestialBody(system, mass=3, position=np.array([50.0, 0.0, 0.0]), velocity=np.array([0.0, 2.5, 0.0]), color="orange")
earth = CelestialBody(system, mass=5, position=np.array([80.0, 0.0, 0.0]), velocity=np.array([0.0, 2.0, 0.0]), color="blue")
mars = CelestialBody(system, mass=2, position=np.array([110.0, 0.0, 0.0]), velocity=np.array([0.0, 1.5, 0.0]), color="red")
# Simulate the solar system for several steps
for _ in range(500):
system.dynamical_interaction()
system.update_all()
system.display_all()Note: Ensure that the required libraries (numpy and matplotlib) are installed before running the code.
-
Clone the repository:
git clone https://github.com/Amirsamandar/N-Body-Simulation.git
-
Navigate to the project directory:
cd n-body-simulation -
Install the required dependencies:
pip install -r requirements.txt
-
Run the simulation:
python solar_system.py
numpy: For numerical operations.matplotlib: For 3D plotting.
Contributions are welcome! If you find any issues or have improvements, feel free to open an issue or submit a pull request.