N-body gravity simulation in O(N)

Lobsters Hottest News

Summary

This video details the principles of the Fast Multipole Method (FMM) for achieving N-body gravity simulation in O(N) time, including multipole expansions and local expansions, and compares it with the naive O(N²) and Barnes-Hut algorithms.

<p><a href="https://lobste.rs/s/v1ejq9/n_body_gravity_simulation_o_n">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 07/27/26, 09:43 AM

**TL;DR:** The video explains in detail how to implement an N-body gravity simulation in O(N) time using the Fast Multipole Method (FMM), contrasting it with the limitations of naive O(N²) and Barnes-Hut O(N log N) approaches, with a focus on the mathematical principles of complex numbers, multipole expansions, and local expansions. ## Overview This video demonstrates a real-time N-body simulation where hundreds of thousands of particles interact via gravity. Common implementations include the naive brute-force method (O(N²)) and the Barnes-Hut algorithm (O(N log N)). The author originally planned to implement Barnes-Hut but, after research, found it "not cool enough and error bounds are tricky". A chance link led to the **Fast Multipole Method (FMM)**: - Proposed by Leslie Greengard and Vladimir Rokhlin in 1987 - Time complexity O(N) - Strict mathematical error bounds - Named one of the top ten algorithms of the 20th century ## From Brute Force to Hierarchical Thinking The N-body problem involves computing gravitational forces between N particles. In two dimensions, the force magnitude is proportional to the product of masses divided by distance (not squared), and the force vector is obtained from the gradient of the potential function. ### Simplifying the Potential with Complex Numbers The 2D potential is related to the logarithm of distance. Representing particle positions as complex numbers, displacements in polar form allow the product inside the logarithm to be split into a sum. The real part of the complex number is exactly the logarithm of the magnitude. Using the **Cauchy–Riemann equations**, the derivative of a complex differentiable function can be expressed via partial derivatives. The force is the negative gradient of the potential; through the complex derivative, a compact complex expression for the force is obtained. ### Hierarchical Approach Both FMM and Barnes-Hut use a quadtree (in 2D) to organize particles hierarchically. Tree nodes contain a box, and each level can be subdivided into four child boxes. This allows particles to be classified into near-field (requiring exact computation) and far-field (can be approximated). Barnes-Hut approximates a far-field cluster using its center of mass, with a criterion that the node size to particle distance ratio is below a threshold θ. However, it still computes far-field contributions per particle, and the center-of-mass approximation has limited accuracy. ## The Core of FMM: Multipole Expansion For more accurate far-field potential approximation of a cluster, FMM uses **multipole expansion**: - The potential from a source cluster is written as a Laurent series about the origin (typically the node center). - Coefficients \(A_k\) depend only on the source positions and masses and can be precomputed. - Evaluation simply requires substituting the target position \(Z\) with negative powers. This series converges outside the cluster's convergence radius (i.e., the bounding circle of the cluster). The error bound is: \[ \left| \Phi(Z) - \Phi_P(Z) \right| \le \frac{\sum m_i}{R} \cdot \frac{(R/|Z|)^{P+1}}{1 - R/|Z|} \] Error decays exponentially with \(R/|Z|\). With \(P=10\), the error is already nearly invisible. Unlike Barnes-Hut, FMM's multipole expansion uses the node center (not the center of mass) as the origin, ensuring a controllable convergence radius. ## Local Expansion: The Real Acceleration Multipole expansions only broadcast from far-field sources to a single target point. To completely avoid per-particle computation, FMM introduces **local expansion**: centered on the target box, it collects all distant source multipole expansions and merges them into a single series around the target center. This allows a single evaluation for all particles inside the target box. The local expansion is also a Laurent series, whose coefficients are obtained by "translating" the multipole coefficients from distant sources. The whole process consists of three steps: 1. **Upward Pass**: Compute the multipole expansion for each node (from leaves to root). 2. **Downward Pass**: Propagate the parent's local expansion downward and merge it with local expansions converted from sibling nodes' multipole expansions. 3. **Direct Computation**: For near-field particles (same node or adjacent nodes), use naive brute force. Because each node's multipole and local expansions require only O(P²) or O(P log P) operations, and the tree depth is O(log N), the overall complexity is O(N). ## Error Analysis The error in FMM comes from truncating multipole and local expansions. Accuracy can be controlled by choosing an appropriate truncation order \(P\). The video gives exponential error bounds; in practice, \(P\) between 10 and 20 achieves satisfactory accuracy. ## Summary The Fast Multipole Method reduces O(N²) brute-force computation to O(N) through **multipole expansion** and **local expansion**, while guaranteeing bounded error. It is faster and more accurate than Barnes-Hut, making it the gold standard for large-scale N-body simulations. The video ends with a real-time demonstration showing hundreds of thousands of particles interacting smoothly, validating the algorithm's effectiveness. **Source:** https://www.youtube.com/watch?v=FhMftauQZqU

Similar Articles

NEMORA: Neural Equivariant Multipole Operators for Long-Range Atomistic Learning

arXiv cs.LG

NEMORA 是一种将 Fast Multipole Method 推广为可学习等变算子的新方法,用于在等变图神经网络中建模长程原子间相互作用,具备线性时间与内存复杂度,可处理数十万原子体系,并在长程基准测试上将力和能量误差降低一个至三个数量级。

PhysLDM: Latent Diffusion for High-Fidelity Deformable Simulation

Hugging Face Daily Papers

PhysLDM 提出了一种用于高保真可变形体物理模拟的时空隐空间扩散框架,采用整体式 VAE 实现约 78 倍 token 压缩与 ~2.48 mm 重建精度,并证明扩散模型比确定性回归更能建模混沌动力学,实现零样本泛化与逆问题求解。

SimForcing: Distilling Simulation Motion Priors into Real-Domain Robot World Models

Hugging Face Daily Papers

SimForcing 提出一个仿真引导框架,通过潜空间运动蒸馏与多块仿真条件化,将仿真中的运动先验蒸馏到真实域机器人世界模型中,在 Bridge 和 InternData-A1 上取得最优的 PSNR/SSIM/LPIPS/FVD,并可用于初始化视觉-语言-动作模型以提升 LIBERO 成功率。