Skip to content

AICL-Lab/n-body

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

n-body

Version License CUDA C++ Build Docs

Million-Particle GPU Physics Engine — High-performance N-body simulation with CUDA acceleration, real-time OpenGL visualization, and three force calculation algorithms.

GitHub Pages · Getting Started · Examples · OpenSpec

Why this project

This project combines three force-computation strategies with a single simulation/runtime API:

  • Direct N² for exact pairwise reference results
  • Barnes-Hut for scalable long-range approximation
  • Spatial Hash for efficient short-range interaction
  • Velocity Verlet for stable symplectic integration
  • CUDA/OpenGL interop for zero-copy visualization

The goal is not just to render particles quickly, but to keep the simulation architecture understandable, testable, and easy to compare across algorithms.

Performance Highlights

Particles Direct N² Barnes-Hut Spatial Hash
10K 60 FPS 120 FPS 120 FPS
100K 10 FPS 60 FPS 90 FPS
1M 1 FPS 25 FPS 60 FPS

Benchmarks on NVIDIA RTX 3080

Architecture

graph TB
    subgraph CPU["CPU (Host)"]
        Config[SimulationConfig]
        PS[ParticleSystem]
        RL[Render Loop]
    end
    
    subgraph GPU["GPU (Device)"]
        FK[Force Kernel<br/>N²/Barnes-Hut/Hash]
        INT[Integration<br/>Velocity Verlet]
        GL[OpenGL<br/>Zero-Copy]
    end
    
    Config --> PS
    PS --> FK
    FK --> INT
    INT --> GL
    GL --> RL
Loading

Technical Highlights

Area What it provides
Compute CUDA kernels for force evaluation and integration
Algorithms Direct N², Barnes-Hut, Spatial Hash
Rendering OpenGL renderer with CUDA/OpenGL interop
Architecture ParticleSystem facade + ForceCalculator strategy
Quality GoogleTest + RapidCheck, OpenSpec-driven workflow

Algorithm Guide

Algorithm Complexity Best fit
Direct N² O(N²) Small systems, reference validation
Barnes-Hut O(N log N) Large gravitational systems
Spatial Hash O(N) Short-range interaction workloads

Quick Start

Requirements

  • NVIDIA GPU with CUDA support
  • CUDA Toolkit 11+
  • CMake 3.18+
  • OpenGL, GLFW, GLEW, GLM
  • For a headless core-only validation path, disable CUDA/rendering; headless observability tests and benchmarks still work.

Build

./scripts/build.sh

When CUDA is unavailable, the script now falls back to a headless core-only build and still produces the core library, headless observability tests, and the benchmark executable. Rendered app surfaces and examples remain disabled.

Manual path:

mkdir -p build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j"$(nproc)"

Run

./build/nbody_sim
./build/nbody_sim 100000

Test

./scripts/test.sh

Benchmark

./scripts/benchmark.sh
./scripts/benchmark.sh serialization.round_trip build/benchmark-results.json

Project Layout

Path Purpose
include/nbody/ Public headers
src/ Core, CUDA, rendering, utilities
tests/ Unit and property-based tests
examples/ Example programs and usage patterns
docs/ Canonical repository-local documentation
site/ GitHub Pages showcase
openspec/specs/ Active specifications
openspec/changes/ Active proposals and implementation tasks

Canonical Documentation

Examples

Development Notes

Citation

If you use this project in your research, please cite:

@software{nbody2026,
  title = {N-Body: Million-Particle GPU Physics Engine},
  author = {AICL-Lab},
  year = {2026},
  url = {https://github.com/AICL-Lab/n-body},
  version = {2.1.0},
  note = {CUDA-accelerated N-body simulation with real-time visualization}
}

License

MIT