Skip to content

Repository files navigation

N-Body Simulation

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.

Overview

The simulation consists of three classes:

  1. 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.
  2. 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.
  3. CelestialBody(NBodies):

    • Inherits from NBodies and adds properties like color to represent a celestial body with additional visual features.

Usage

# 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.

Getting Started

  1. Clone the repository:

    git clone https://github.com/Amirsamandar/N-Body-Simulation.git
  2. Navigate to the project directory:

    cd n-body-simulation
  3. Install the required dependencies:

    pip install -r requirements.txt
  4. Run the simulation:

    python solar_system.py

Dependencies

  • numpy: For numerical operations.
  • matplotlib: For 3D plotting.

Contributing

Contributions are welcome! If you find any issues or have improvements, feel free to open an issue or submit a pull request.

License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages