https://github.com/rohithgowdam/n-body-simulation
https://github.com/rohithgowdam/n-body-simulation
opencl opencl-python padp-lab parallel-computing
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rohithgowdam/n-body-simulation
- Owner: RohithgowdaM
- License: apache-2.0
- Created: 2025-02-02T17:53:37.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-02-16T11:11:29.000Z (4 months ago)
- Last Synced: 2025-02-16T12:19:18.371Z (4 months ago)
- Topics: opencl, opencl-python, padp-lab, parallel-computing
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# 🚀 N-Body Simulation with OpenCL
This repository contains an **OpenCL-accelerated N-body simulation**, which simulates the gravitational interactions between celestial bodies in space. The simulation features **25 small bodies** and **1 massive central body**, with **randomized initial positions, velocities, and colors**.
## ✨ Features
- Uses **OpenCL** to run computations on the **GPU** for faster performance.
- Simulates **gravity-based motion** using **Newton’s laws**.
- **Parallel execution** ensures **speedup over CPU-based computation**.
- Outputs a **visual simulation** stored as an `.mp4` file.
- Displays the **first and last frames** for quick visualization.---
## 📜 How It Works
1. **Bodies are initialized** with random positions, velocities, and masses.
2. **The OpenCL kernel computes gravitational forces** in parallel.
3. **Positions and velocities are updated** using Euler’s method.
4. **Frames are generated using OpenCV** and saved as a video.
5. The simulation **runs for 10 seconds**, generating a `.mp4` output.---
## ⚡ Performance Comparison
| Feature | CPU Version | GPU Version (OpenCL) |
|---------|------------|----------------------|
| Force Computation | **Nested loops** (slow) | **Parallelized on GPU** (fast) |
| Execution Time | **Several seconds/minutes** | **10×–100× faster** |
| Visualization | **Matplotlib** | **OpenCV (efficient)** |
| Scalability | **Limited by CPU cores** | **Can handle thousands of bodies** |---
## 🛠️ Requirements
- **Python 3.x**
- `numpy`
- `pyopencl`
- `opencv-python`
- `os` (Built-in)
- `time` (Built-in)### 📌 Install Dependencies
Run the following command to install the required packages:
```bash
pip install numpy pyopencl opencv-python
git clone https://github.com/your-username/nbody-opencl.git
cd nbody-opencl
python nbody_opencl.py
```
If you want to run the simlation without opencl, use the command
```bash
python cpu_nbody.py
```