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
| Name | Description |
|---|---|
BarnesHutSimulation | N-body simulation using Barnes-Hut algorithm. |
BarnesHutTree | Quadtree-based Barnes-Hut N-body tree. |
Body | Represents a body (particle) in N-body simulation. |
Node | Represents 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 = Gbodiesattributeinstance attribute#
bodies: list[Body] = list(bodies)boundsattributeinstance attribute#
bounds: Quad = boundsdtattributeinstance attribute#
dt: float = dtsofteningattributeinstance attribute#
softening: float = softeningthetaattributeinstance attribute#
theta: float = thetabuild_treemethod#
def build_tree() -> BarnesHutTreeBuild a new Barnes-Hut tree from current particle positions.
Returns
BarnesHutTree- New BarnesHutTree
get_center_of_massmethod#
def get_center_of_mass() -> np.ndarrayCalculate center of mass of all bodies.
Returns
np.ndarray- Position vector of center of mass
get_kinetic_energymethod#
def get_kinetic_energy() -> floatCalculate total kinetic energy of all bodies.
Returns
float- Total kinetic energy
stepmethod#
def step() -> BarnesHutTreeAdvance 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.
collect_nodesmethod#
def collect_nodes() -> list[Node]Collect all nodes in the tree (for visualization).
compute_force_onmethod#
def compute_force_on(body: Body, theta: float = 0.5, G: float = 1.0, softening: float = 0.1) -> NoneCompute force on a body from all other bodies in tree.
insertmethod#
def insert(body: Body) -> NoneInsert a body into the tree.
Parameters
| Name | Type | Description |
|---|---|---|
body | Body | Body 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)Nodeclass#
class Node(quad: Quad)Represents a node in the quadtree.
NEattributeinstance attribute#
NE: Node | None = NoneNWattributeinstance attribute#
NW: Node | None = NoneSEattributeinstance attribute#
SE: Node | None = NoneSWattributeinstance attribute#
SW: Node | None = Nonebodyattributeinstance attribute#
body: Body | None = Nonecenter_of_massattributeinstance attribute#
center_of_mass: np.ndarray = np.array([0.0, 0.0], dtype=np.float64)massattributeinstance attribute#
mass: float = 0.0quadattributeinstance attribute#
quad: Quad = quad