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.
- 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
.mp4file. - Displays the first and last frames for quick visualization.
- Bodies are initialized with random positions, velocities, and masses.
- The OpenCL kernel computes gravitational forces in parallel.
- Positions and velocities are updated using Eulerโs method.
- Frames are generated using OpenCV and saved as a video.
- The simulation runs for 10 seconds, generating a
.mp4output.
| 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 |
- Python 3.x
numpypyopenclopencv-pythonos(Built-in)time(Built-in)
Run the following command to install the required packages:
pip install numpy pyopencl opencv-python
git clone https://github.com/your-username/nbody-opencl.git
cd nbody-opencl
python nbody_opencl.pyIf you want to run the simlation without opencl, use the command
python cpu_nbody.py