npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngraph.quadtreebh

v1.0.0

Published

Quad tree data structure for Barnes-Hut simulation

Downloads

5,271

Readme

ngraph.quadtreebh

Implementation of Barnes-Hut N-Body simulation in Javascript.

build status Reference

  1. Fast Hierarchical Methods for the N-body Problem - one of the best explanations of Barnes-Hut method by James Demmel.
  2. Wikipedia article - general introduction into the problem

Usage

I'm still working on API and it might be changed in future.

var Body = require('ngraph.physics.primitives').Body;
// Create new bodies at (1, 0) and (2, 0):
var bodies = [];
bodies.push(new Body(1, 0));
bodies.push(new Body(2, 0));
/* ... create more as you need ... */

// build quad tree:
var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree();

// insert bodies into the quad tree 
quadTree.insertBodies(bodies); // performance: O(n * log n)

// calculate forces acting on each body in the tree O(n * log n):
bodies.forEach(function(body) {
  quadTree.updateBodyForce(body);
});
// At this point every body object has valid 2d force vecor
console.dir(bodies[0].force);

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 than 1. It defaults to 0.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 = require('ngraph.quadtreebh');
var quadTree = createQuadTree({
  theta: 1.2,
  gravity: -10
});

To query current options of the tree use:

var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree();
console.dir(quadTree.options()); // prints { theta: 0.8, gravity: -1};

To change options at run time:

var createQuadTree = require('ngraph.quadtreebh');
var quadTree = createQuadTree();
quadTree.options({
  theta: 0.5,
  gravity: -42
});

Mass of each body affects overall result of computation. You can tweak it when creating new bodies:

var Body = require('ngraph.physics.primitives').Body;
var earth = new Body(); earth.mass = 5.972;
var sun   = new Body(); sun.mass = 1989000;

Install

npm install ngraph.quadtreebh

License

BSD 3-Clause