ngraph.quadtreebh
Implementation of Barnes-Hut N-Body simulation in Javascript.
Reference
- Fast Hierarchical Methods for the N-body Problem - one of the best explanations of Barnes-Hut method by James Demmel.
- Wikipedia article - general introduction into the problem
Usage
I'm still working on API and it might be changed in future.
var Body = Body;// Create new bodies at (1, 0) and (2, 0):var bodies = ;bodies;bodies;/* ... create more as you need ... */// build quad tree:var createQuadTree = ;var quadTree = ;// insert bodies into the quad treequadTree; // performance: O(n * log n)// calculate forces acting on each body in the tree O(n * log n):bodies;// At this point every body object has valid 2d force vecorconsoledirbodies0force;
Note: You don't necessary have to use ngraph.physics.primitives. That package merely defines an interface for a physical Body, which is expected by quad tree. As long as your Body object implements this interface (mass
, pos
and force
) - you can use current quad tree structure to calculate forces.
Configuring quad tree
Quad tree allows to change two global options:
gravity
- Gravitational constant, used in force value calculation. Defaults to-1
;theta
- a threshold which determines when group of bodies is considered distant enough to be approximated as a single body. The value should be> 0
, and usually little less than1
. It defaults to0.8
if not suplied. This argument affects accuracy and speed of simulation. Please refer to [1] for more details.
You can pass these setting to quad tree as follows:
var createQuadTree = ;var quadTree =;
To query current options of the tree use:
var createQuadTree = ;var quadTree = ;consoledirquadTreeoptions; // prints { theta: 0.8, gravity: -1};
To change options at run time:
var createQuadTree = ;var quadTree = ;quadTreeoptionstheta: 05gravity: -42;
Mass of each body affects overall result of computation. You can tweak it when creating new bodies:
var Body = Body;var earth = ; earthmass = 5972;var sun = ; sunmass = 1989000;
Install
npm install ngraph.quadtreebh
License
BSD 3-Clause