Giter Club home page Giter Club logo

barnes-hut-rs's Introduction

barnes-hut-rs

Implementation of the Barnes-Hut and direct algorithms in Rust. Web visualization at galaxy-sim.github.io.

Running the project

Assuming you have cargo installed along with the rest of the rustup toolchain, run the following to run the simulation locally.

cargo run

Note that the local OpenGL frontend is very slow compared to the web frontend.

The algorithm

The direct algorithm for solving the N-body problem involves the quadratic operation of computing the force between all pairs of bodies in the simulation. The Barnes-Hut algorithm notices that while the exact solution is expensive to compute, we can receive a decent approximation by "grouping" faraway bodies together.

This grouping is achieved by a quadtree (works for 2D, an octree is required for 3D) that maintains the center of mass for each node. When computing the force on each body, the tree is traversed from the root, only taking into account child nodes that are within a threshold distance.

For full details on the Barnes-Hut algorithm, see the wikipedia article.

Efficient quadtree implementation

This quadtree implementation has the following advantages compared to other existing quadtrees:

  1. Maintains center of mass for each node (required for Barnes-Hut algorithm).
  2. Does not explicity store a bounding box per node (this is inferred during iteration)
  3. Iterative insertion of new bodies (avoids overhead of recursing, which would limit the tree size by the call stack limit).

See src/quadtree/tree.rs for the implementation.

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.