DEV Community

Cover image for Performance benefits of using Octrees and Barnes-Hut Approximation in Space Simulation
Gage Howe
Gage Howe

Posted on

Performance benefits of using Octrees and Barnes-Hut Approximation in Space Simulation

Image description
~ Matplotlib didn't want to render all 10000, apparently ~

This is chapter one in my public logs for my galaxy simulation project. I'm building an app that will simulate a large number of celestial bodies, and I needed a method of simulating the forces between all of them. Obviously, calculating gravity by directly comparing the distance and mass between two objects would be incredibly expensive as that would be O(n²). For 1000+ celestial bodies, this isn't practical.

The Challenge of N-body Simulations

In a naive approach to n-body gravitational simulations, we would calculate the force between every pair of bodies. This method, while accurate, quickly becomes computationally infeasible as the number of bodies increases. For n bodies, we need to perform n(n-1)/2 calculations per time step, leading to an O(n²) time complexity.

Enter the Barnes-Hut Algorithm

My research led me to a method that uses Octrees, specifically the Barnes-Hut algorithm, to dramatically reduce the number of calculations needed. The Barnes-Hut algorithm is a clever approximation method that treats clusters of distant bodies as single, massive bodies.
Here's how it works:

The algorithm divides the simulation space into a hierarchical tree structure (an octree in 3D simulations).
Each node in the tree represents a cubic region of space.
The tree is constructed recursively, with each node either containing a single body or subdividing into eight child nodes.
When calculating the force on a body, the algorithm traverses the tree:

If a node is sufficiently far away (determined by a threshold θ), it's treated as a single body at its center of mass.
If a node is close, the algorithm recurses into its children.

The key to the Barnes-Hut algorithm's efficiency is its approximation of distant forces. It introduces a parameter θ (theta), which determines when a group of bodies can be approximated as a single body. If the ratio of the width of a node to the distance from the body is less than θ, that node's bodies are treated as one.

Results

When I implemented the Barnes-Hut Tree (BHT) in my simulation, I saw an exponential performance boost. In terms of Big-O notation, the direct-comparison method cost O(n²), where the BHT method reduced this to O(n log n).

This improvement means that as the number of bodies in the simulation increases, the computational cost grows much more slowly with the Barnes-Hut algorithm than with the direct method. For large simulations with thousands or millions of bodies, this makes the difference between a simulation that takes days to run and one that completes in minutes or hours.

Conclusion and Next Steps
Implementing the Barnes-Hut algorithm has been a crucial step in making my galaxy simulation project feasible for large numbers of celestial bodies. The dramatic performance improvement allows me to simulate more complex and realistic galaxy models.

In future updates, I plan to discuss and implement a few things:

  • Fine-tuning the θ parameter for the optimal balance between speed and accuracy
  • Integrating the simulation into my OpenGL project
  • Parallelizing the Barnes-Hut algorithm for even greater performance

I'll be posting updates as they come.

https://en.wikipedia.org/wiki/Barnes%E2%80%93Hut_simulation

Top comments (0)