Giter VIP home page Giter VIP logo

n-body-sim's Introduction

preview

About this project

This project showcases OpenCL/OpenGL interoperability through a simulation of gravitational forces acting on n particles. Employing a brute-force algorithm with O(nĀ²) complexity, it leverages the power of parallel computing to manage and render the simulation efficiently. The project is developed entirely in C++, utilizing GLFW 3, GLAD, and GLM for high-performance rendering with OpenGL, and features a user-friendly interface built with ImGui. Adhering to the OpenCL 1.2 specification, it demonstrates the seamless integration of compute and graphics processing for sophisticated simulations.

Note

This project is designed for use with a dedicated graphics card. While OpenCL and OpenGL can theoretically run without a GPU, this program assumes you have both a GPU and the necessary GPU drivers installed.

Usage

There are some pre-built executables for Windows x86-64/MacOS ARM64/Linux x86-64. These were all tested on an NVIDIA GPU but should work with other vendors, granted the OpenCL/OpenGL sharing extension is supported by the device. You can find the binaries here.

Note

The masses are pre-multiplied with the gravitational constant G to reduce the load on the GPU. If you want to change existing world generators or add new ones, you must edit the world generators source files in src/world_gens. The conversion from OpenGL units to real-life units is described in state.cpp. The color of a particle represents its speed.

Some initial parameters can also be modified in state.cpp and adjusted at runtime using the ImGui user interface.

Controls

  • Use w/a/s/d for up/left/down/right respectively
  • Hold Right Click and move the mouse to look around the scene
  • Scroll Mouse Wheel to change the camera speed

Building from source

Linux

Install dependencies

sudo apt-get update
sudo apt-get install -y \
    build-essential cmake pkg-config python3 git \
    libxkbcommon-dev xorg-dev libwayland-dev

MacOS

Install C++ compiler

xcode-select --install

Install Homebrew package manager

  1. Run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Follow installer instructions to add brew to PATH

Install dependencies

brew update
brew install cmake python3

Windows

Install a C++ compiler (e.g. MinGW-w64)

If you choose to install MinGW-w64, download and install MSYS2. Add the mingw-w64-x86_64-gcc and mingw-w64-x86_64-make package from within the MSYS console:

pacman -S mingw-w64-x86_64-gcc
pacman -S mingw-w64-x86_64-make

Make sure the binary directory is added to PATH before executing the CMake commands below. For MSYS2, the PATH should be C:\msys64\mingw64\bin by default.

Install CMake (3.10+)

Download and install CMake Make sure to set the correct PATH.

Install Git for Windows

Download and install Git Make sure to set the correct PATH.

Building

Clone this repository

git clone https://github.com/22ms/n-body-sim.git

Set up your build directory

mkdir build
cd build

Generate the buildfiles

On MacOS and Linux:

cmake ..

On Windows (given you installed MinGW-w64 with MSYS2):

cmake -G "MinGW Makefiles" -DCMAKE_CXX_COMPILER="C:/msys64/mingw64/bin/g++.exe" ..

Compile the project

cmake --build . -t n-body

You should now have an executable called n-body inside of the build directory.

Program architechture

---
title: N-body simulation architechture
---
classDiagram
note for State "These are not classes since they all manage one global state"
note for Camera "Actual classes used by the global state"
note for OpenCLWrapper "Acquires GL buffer and simulates timestep on it"
note for OpenGLWrapper "Manages host arrays and buffer, renders out the particles"
note for ImGuiWrapper "Frontend for parameters, updates WorldState"

OpenGLWrapper..>WorldGenerator
OpenCLWrapper..>Kernel
OpenGLWrapper..>Shader
OpenGLWrapper..>Camera

ImGuiWrapper-->OpenGLWrapper
OpenCLWrapper-->OpenGLWrapper
OpenGLWrapper-->OpenCLWrapper

ImGuiWrapper-->State
OpenCLWrapper-->State
OpenGLWrapper-->State
namespace Global {
  class State {
    +MAX_N
    +NPtr
    +TimeScalePtr
    +EpsilonPtr
    +WorldGeneratorPtr
    +...
  }
  class OpenGLWrapper {
    +ParticleArray
    +ParticleBuffer
    +...
    +Initialize()
    +Render()
    +...()
  }
  class OpenCLWrapper {
    -cmdQueue
    +Initialize()
    +Simulate()
    +...()
  }
  class ImGuiWrapper {
    +Initialize()
    +Display()
  }
}
namespace Classes {
  class Shader {
    +ID
    +Shader(filepath)
    +Use()
    +SetUniform()
    +...()
  }
  class Camera {
    +Position
    +...
    +ProcessInput()
    +...()
  }
  class Kernel {
    +Kernel(filepath)
    +GetKernel()
  }
  class WorldGenerator {
    +Generate(arrays) virtual void
    +ToString() virtual string
    +Clone()
    +...()
  }
}

Loading

n-body-sim's People

Contributors

22ms avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.