Skip to content

N-Body simulations of Galaxy dinamics using python with two diferent algorthms, brute force and Barnes-Hut

License

Notifications You must be signed in to change notification settings

DiegoViillalba/N-Body-Python-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

N-Body Simulation using the Barnes-Hut Algorithm

This repository contains an implementation of the Barnes-Hut algorithm for simulating gravitational interactions between multiple bodies. The method optimizes calculations by dividing space into a quadtree (or octree in 3D) and approximating forces using the center of mass, significantly reducing computational complexity.

Project Description

This Python program simulates and visualizes the gravitational dynamics of an (N)-body system using object-oriented programming and numerical methods.

Key Features:

  • Particle Initialization:
    • Bodies are initialized with random positions and velocities.
    • Includes particles and a massive central object representing a black hole.
  • Gravitational Force Calculation:
    • Uses the Barnes-Hut algorithm with a quadtree to divide space and approximate forces in distant regions.
  • Dynamic Simulation:
    • Integrates equations of motion using a time step (( \Delta t )).
    • Visualizes the system's evolution with animations.

Requirements

The following Python libraries are required:

  • numpy: For efficient numerical calculations.
  • matplotlib: For generating visualizations and animations.
  • tqdm: For displaying progress bars.

Install them with:

pip install numpy matplotlib tqdm

Main Files

  • galaxy.py: The core implementation of the Barnes-Hut algorithm, including:
    • vector class: Handles vector operations such as dot product and magnitude.
    • Particle class: Represents the system's bodies.
    • Node and QuadTree classes: Implement the quadtree for space division and optimization.
    • Simulation function: Executes system dynamics and generates animations.

Usage Instructions

  1. Clone this repository:
    git clone https://github.com/your-username/galaxy-simulation.git
    cd galaxy-simulation
  2. Run the script:
    python galaxy.py
  3. View the generated animation in a new window.

Configurable Parameters

Inside the galaxy.py file, you can adjust the following parameters:

  • N: Number of particles in the system.
  • dt: Time step for integration.
  • M_bh: Mass of the central massive object (e.g., a black hole).
  • t: Total simulation duration.
  • G: Universal gravitational constant.

Example Output

The program generates a simulation where bodies orbit a central massive object, illustrating how gravitational forces shape their trajectories.

Example Animation:

The simulation displays real-time evolution of particle trajectories computed using the Barnes-Hut algorithm.

Contributions

Contributions are welcome! Feel free to open an issue or submit a pull request if you have ideas for improvement or encounter any issues.

Author

Diego Antonio Villalba González
Developed as part of an Astrophysics Computational project at Facultad de Ciencias, UNAM. plot

About

N-Body simulations of Galaxy dinamics using python with two diferent algorthms, brute force and Barnes-Hut

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages