A high-performance, lightweight 2D/3D spatial partitioning and neighbor search library written in pure Mojo.
Designed for particle physics engines, Smoothed Particle Hydrodynamics (SPH), Discrete Element Method (DEM), collision detection, and astrophysical N-body gravitational simulations.
- 🚀 Pure Mojo 1.2 Implementation: Zero external C/Python dependencies, leveraging Mojo's memory safety, unboxed value types, compile-time performance, and SIMD hardware acceleration.
-
🔍 Spatial Hashing (2D & 3D): Constant-time
$\mathcal{O}(1)$ expected radius neighbor lookup using the Teschner et al. (2003) XOR-prime hash formulation with duplicate collision prevention. -
🌳 Barnes-Hut Trees (Quadtree & Octree): Hierarchical
$\mathcal{O}(N \log N)$ n-body force calculation with Multipole Acceptance Criteria ($\theta$ ) and Plummer softening. - 🌊 SPH & Physics Kernels: Monaghan cubic spline smoothing kernels, Müller spiky pressure gradients, Lennard-Jones (12-6) molecular potentials, and symplectic time integrators (Velocity Verlet & Symplectic Euler).
-
📦 Multi-Channel Distribution: Precompiled binary releases (
mojospace.mojoc), direct Pixi git dependencies, and prefix.dev channel packaging (pixi publish). -
📚 Rigorous Mathematical Documentation: Complete mathematical formulas, derivations, complexity analysis, and academic citations in
docs/.
Add mojospace to your project's pixi.toml dependencies:
[dependencies]
mojospace = { git = "https://github.com/anclp/mojospace" }Pixi will automatically fetch, compile, and place mojospace into your environment's $CONDA_PREFIX/lib/mojo in the background.
Every tagged release builds and attaches mojospace.mojoc:
- Download
mojospace.mojocfrom GitHub Releases. - Place it in your project folder.
- Import directly with
import mojospace.
pixi add --channel https://prefix.dev/anclp mojospaceFor detailed instructions, see docs/INSTALLATION_AND_DISTRIBUTION.md.
from std.collections import List
from src.math.vec3 import Vec3
from src.particle.particle3d import Particle3D
from src.spatial_hash.hash_grid_3d import SpatialHashGrid3D
def main():
# 1. Initialize grid with cell size matching cutoff radius
var grid = SpatialHashGrid3D(cell_size=1.0, table_size=10007)
# 2. Populate particles
var particles = List[Particle3D]()
particles.append(Particle3D(0, Vec3(0.0, 0.0, 0.0)))
particles.append(Particle3D(1, Vec3(0.5, 0.2, 0.1)))
particles.append(Particle3D(2, Vec3(50.0, 50.0, 50.0)))
# 3. Build spatial hash table
grid.build(particles)
# 4. Query all neighbors within radius 1.0 from particle 0
var neighbors = grid.query_radius(particles[0].pos, 1.0, particles)
print("Found neighbors count:", len(neighbors))
for i in range(len(neighbors)):
print("Neighbor particle ID:", neighbors[i])from std.collections import List
from src.math.vec3 import Vec3
from src.math.bounds import AABB3D
from src.particle.particle3d import Particle3D
from src.trees.octree import BarnesHutOctree
def main():
var bounds = AABB3D(Vec3(-100.0, -100.0, -100.0), Vec3(100.0, 100.0, 100.0))
var octree = BarnesHutOctree(bounds, max_depth=10)
var particles = List[Particle3D]()
# ... populate particles ...
octree.build(particles)
# Calculate force on target particle using MAC theta = 0.5
var force = octree.compute_force_barnes_hut(
target_idx=0,
particles=particles,
theta=0.5,
G=1.0,
softening=0.01
)
print("Gravitational force:", force.x, force.y, force.z)Check out the scientific documentation in the docs/ directory:
- Spatial Hashing Formulations: Coordinate mapping, Teschner prime hash constants, cell size tuning, collision behavior, and complexity proofs.
-
Barnes-Hut & Hierarchical Trees: Quadtree & Octree subdivision, center of mass aggregation, Multipole Acceptance Criteria (
$\theta$ ), Plummer softening, and tree pruning. - Particle Physics & Smoothing Kernels: Monaghan cubic spline SPH kernel, Müller spiky pressure gradient, Lennard-Jones 12-6 potential, and Velocity Verlet integration.
- API Reference: Comprehensive signature and complexity reference for all structs and functions.
- Installation & Distribution: Guide for Git dependencies, binary releases, and package publishing.
This project uses Pixi to manage the Mojo environment across platforms.
# Run full unit test suite
pixi run test
# Run neighbor search benchmark
pixi run bench
# Run N-body gravitational simulation demo
pixi run demo-nbody
# Run SPH fluid neighbor demo
pixi run demo-sph
# Precompile binary package (.mojoc)
pixi run package
# Publish to prefix.dev channel
pixi publish --to https://prefix.dev/anclp/mojospace(On Windows, run commands through WSL2).
If you use mojospace in academic research, please cite:
@software{mojospace2026,
author = {mojospace Contributors},
title = {mojospace: Lightweight 2D/3D Spatial Hashing and Barnes-Hut Trees in Pure Mojo},
year = {2026},
url = {https://github.com/anclp/mojospace}
}Key published references:
- Teschner, M., et al. (2003). Optimized Spatial Hashing for Collision Detection of Deformable Objects. VMV 2003, pp. 47–54.
- Barnes, J., & Hut, P. (1986). A hierarchical $O(N \log N)$ force-calculation algorithm. Nature, 324(6096), 446–449. doi:10.1038/324446a0
- Monaghan, J. J. (1992). Smoothed Particle Hydrodynamics. Ann. Rev. Astron. Astrophys., 30(1), 543–574. doi:10.1146/annurev.aa.30.090192.002551
- Verlet, L. (1967). Computer "Experiments" on Classical Fluids. I. Phys. Rev., 159(1), 98–103. doi:10.1103/PhysRev.159.98
This project is licensed under the MIT License.