Skip to content
Projects

tree_d_3d.gravity

Barnes-Hut N-body solver for fast gravitational force calculations.

This module implements the Barnes-Hut algorithm for O(N log N) computation of gravitational forces in N-body simulations. It uses quadtree (2D) or octree (3D) spatial decomposition to approximate distant bodies.

Classes

NameDescription
BarnesHutSimulationN-body simulation using Barnes-Hut algorithm.
BarnesHutTreeQuadtree-based Barnes-Hut N-body tree.
BodyRepresents a body (particle) in N-body simulation.
NodeRepresents a node in the quadtree.

BarnesHutSimulationclass#

class BarnesHutSimulation(bodies: Sequence[Body], bounds: Quad, theta: float = 0.5, G: float = 1.0, softening: float = 0.1, dt: float = 0.01)

N-body simulation using Barnes-Hut algorithm.

This class manages particles and implements the main simulation loop using leapfrog integration with force calculation via the Barnes-Hut tree algorithm.

Gattributeinstance attribute#

G: float = G

bodiesattributeinstance attribute#

bodies: list[Body] = list(bodies)

boundsattributeinstance attribute#

bounds: Quad = bounds

dtattributeinstance attribute#

dt: float = dt

softeningattributeinstance attribute#

softening: float = softening

thetaattributeinstance attribute#

theta: float = theta

build_treemethod#

def build_tree() -> BarnesHutTree

Build a new Barnes-Hut tree from current particle positions.

Returns

BarnesHutTree
New BarnesHutTree

get_center_of_massmethod#

def get_center_of_mass() -> np.ndarray

Calculate center of mass of all bodies.

Returns

np.ndarray
Position vector of center of mass

get_kinetic_energymethod#

def get_kinetic_energy() -> float

Calculate total kinetic energy of all bodies.

Returns

float
Total kinetic energy

stepmethod#

def step() -> BarnesHutTree

Advance simulation by one timestep using leapfrog integration.

Returns

BarnesHutTree
The updated Barnes-Hut tree (for visualization/diagnostics)

BarnesHutTreeclass#

class BarnesHutTree(root_quad: Quad)

Quadtree-based Barnes-Hut N-body tree.

rootattributeinstance attribute#

root: Node = Node(root_quad)

collect_nodesmethod#

def collect_nodes() -> list[Node]

Collect all nodes in the tree (for visualization).

Returns

list[Node]
List of all non-empty nodes

compute_force_onmethod#

def compute_force_on(body: Body, theta: float = 0.5, G: float = 1.0, softening: float = 0.1) -> None

Compute force on a body from all other bodies in tree.

Parameters

NameTypeDefaultDescription
bodyBodyrequiredBody to compute force on
thetafloat0.5Opening angle (trade-off accuracy vs speed, typically 0.3-0.9)
Gfloat1.0Gravitational constant
softeningfloat0.1Softening length to prevent force singularities

insertmethod#

def insert(body: Body) -> None

Insert a body into the tree.

Parameters

NameTypeDescription
bodyBodyBody to insert

Bodyclass#

class Body(x: float, y: float, vx: float = 0.0, vy: float = 0.0, mass: float = 1.0)

Represents a body (particle) in N-body simulation.

forceattributeinstance attribute#

force: np.ndarray = np.zeros(2, dtype=np.float64)

massattributeinstance attribute#

mass: float = float(mass)

posattributeinstance attribute#

pos: np.ndarray = np.array([x, y], dtype=np.float64)

velattributeinstance attribute#

vel: np.ndarray = np.array([vx, vy], dtype=np.float64)

add_forcemethod#

def add_force(fx: float, fy: float) -> None

Add force component to the body.

Parameters

NameDescription
fx, fyForce components to add

reset_forcemethod#

def reset_force() -> None

Reset force to zero.

Nodeclass#

class Node(quad: Quad)

Represents a node in the quadtree.

NEattributeinstance attribute#

NE: Node | None = None

NWattributeinstance attribute#

NW: Node | None = None

SEattributeinstance attribute#

SE: Node | None = None

SWattributeinstance attribute#

SW: Node | None = None

bodyattributeinstance attribute#

body: Body | None = None

center_of_massattributeinstance attribute#

center_of_mass: np.ndarray = np.array([0.0, 0.0], dtype=np.float64)

massattributeinstance attribute#

mass: float = 0.0

quadattributeinstance attribute#

quad: Quad = quad

is_externalmethod#

def is_external() -> bool

Check if this is a leaf node (no children).