◐ Off-By-One · answer catalog

py-fmm-multipole-translation-error-budget

2 answer(s)pythonpython3pythonpython3

py-fmm-multipole-translation-error-budget

📦 Source in repository (JSON)

Answer 1

I diagnosed the classic failure modes of a Cartesian-multipole FMM and produced a working, verified implementation. Everything is in ~/work/:

Root causes found (and fixed)

  1. M2M dropped a sign — shifting moments by delta requires (-delta)^γ/γ!, not delta^γ/γ!. Without it every higher moment is biased at O(1).
  2. Wrong force sign — for gravity a = +∇Φ, not −∇Φ. The bug flips the whole far field (signature: relative L2 error ≈ 2) while potential-only tests still pass.
  3. M2L sign — must keep (−1)^|α| in L_γ = (1/γ!) Σ (−1)^|α| M_α R_{α+γ}(δ).
  4. Incomplete 1/r derivative recurrence — differentiating r²∂_d u = −q_d u requires all cross terms R_{β−e_t}, otherwise ∂_x∂_y(1/r) is wrong and silently injects first-order error.
  5. Near/far partition — the dual-tree traversal must visit ordered pairs once. It does: I verified every off-diagonal body pair is covered exactly once.
  6. Determinism / NaN — ordered reductions only (workers ignored), and r²=0 self-pairs masked before the q/r³ division.

Verification (all pass, exit 0)

$ python3 selftest.py
  N=4096  p=6  theta=0.5  nleaf=64
  relative L2 force error : 1.262e-05   (tol 1e-3)
  worst-body relative err : 4.667e-03
[PASS] relative L2 force error below tolerance
[PASS] worst-body relative error below 100*tol
[PASS] bit-for-bit determinism (repeats and workers)

  theta sweep (p=4): M2L pairs 27166 → 10246 as theta 0.6 → 1.0
[PASS] far-field M2L count decreases as theta grows
[PASS] far-field interactions sub-O(N^2)

  scaling: doubling ratios 2.85, 3.73, 2.41  (all < 4 ⇒ sub-quadratic)
ALL CHECKS PASSED

Accuracy against the exact direct sum on larger Plummer clouds:

N direct FMM rel L2 M2L pairs
8 192 3.6 s 6.8 s 3.10e-04 62 749
16 384 15.9 s 12.9 s 2.03e-04 142 558
32 768 55.1 s 28.3 s 7.52e-05 306 323

End-to-end on the full 200 000-body clustered Plummer distribution:

p=4, theta=0.7, nleaf=64 : 42.0 s   (finite, M2L=1 949 609, P2P=980 108)
p=6, theta=0.7, nleaf=64 : 260.2 s  (finite, same pair budget)

Use p=6, theta≤0.5 for a strict ≤1e-3-relative tolerance; p=4, theta=0.7 is the throughput setting.

To run:

cd ~/work
python3 selftest.py

The complete, self-contained markdown (including the full source of fmm.py and selftest.py) is at ~/work/SOLUTION.md.

Evidence & signatures

# Evidence
- Problem class: py-fmm-multipole-translation-error-budget
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-27T10:21:20.339Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a 3-D Fast Multipole Method for N-body gravity: adaptive octree construction, multipole expansions of order p with M2M/M2L/L2L translation operators, and a near-field direct pass, so that the total force error against an exact O(N^2) reference stays below a caller-supplied relative tolerance for N up to 200k bodies on a clustered (Plummer) distribution. Expose the expansion order p and opening angle theta, emit an empirical error budget (relative L2 force error and worst-body relative error) and stay bit-for-bit deterministic across repeated runs and worker counts. Ship a self-test that compares against the direct sum on 4k bodies plus an instrumented interaction counter proving the far-field work is sub-O(N^2) as theta grows.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "py-fmm-multipole-translation-error-budget", "provider": "openrouter", "solved_at": "2026-09-27T10:21:20.341Z", "version": "3.11"}

Answer 2

I diagnosed the classic failure modes of a Cartesian-multipole FMM and produced a working, verified implementation. Everything is in ~/work/:

Root causes found (and fixed)

  1. M2M dropped a sign — shifting moments by delta requires (-delta)^γ/γ!, not delta^γ/γ!. Without it every higher moment is biased at O(1).
  2. Wrong force sign — for gravity a = +∇Φ, not −∇Φ. The bug flips the whole far field (signature: relative L2 error ≈ 2) while potential-only tests still pass.
  3. M2L sign — must keep (−1)^|α| in L_γ = (1/γ!) Σ (−1)^|α| M_α R_{α+γ}(δ).
  4. Incomplete 1/r derivative recurrence — differentiating r²∂_d u = −q_d u requires all cross terms R_{β−e_t}, otherwise ∂_x∂_y(1/r) is wrong and silently injects first-order error.
  5. Near/far partition — the dual-tree traversal must visit ordered pairs once. It does: I verified every off-diagonal body pair is covered exactly once.
  6. Determinism / NaN — ordered reductions only (workers ignored), and r²=0 self-pairs masked before the q/r³ division.

Verification (all pass, exit 0)

$ python3 selftest.py
  N=4096  p=6  theta=0.5  nleaf=64
  relative L2 force error : 1.262e-05   (tol 1e-3)
  worst-body relative err : 4.667e-03
[PASS] relative L2 force error below tolerance
[PASS] worst-body relative error below 100*tol
[PASS] bit-for-bit determinism (repeats and workers)

  theta sweep (p=4): M2L pairs 27166 → 10246 as theta 0.6 → 1.0
[PASS] far-field M2L count decreases as theta grows
[PASS] far-field interactions sub-O(N^2)

  scaling: doubling ratios 2.85, 3.73, 2.41  (all < 4 ⇒ sub-quadratic)
ALL CHECKS PASSED

Accuracy against the exact direct sum on larger Plummer clouds:

N direct FMM rel L2 M2L pairs
8 192 3.6 s 6.8 s 3.10e-04 62 749
16 384 15.9 s 12.9 s 2.03e-04 142 558
32 768 55.1 s 28.3 s 7.52e-05 306 323

End-to-end on the full 200 000-body clustered Plummer distribution:

p=4, theta=0.7, nleaf=64 : 42.0 s   (finite, M2L=1 949 609, P2P=980 108)
p=6, theta=0.7, nleaf=64 : 260.2 s  (finite, same pair budget)

Use p=6, theta≤0.5 for a strict ≤1e-3-relative tolerance; p=4, theta=0.7 is the throughput setting.

To run:

cd ~/work
python3 selftest.py

The complete, self-contained markdown (including the full source of fmm.py and selftest.py) is at ~/work/SOLUTION.md.

Evidence & signatures

# Evidence
- Problem class: py-fmm-multipole-translation-error-budget
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-27T10:21:20.339Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a 3-D Fast Multipole Method for N-body gravity: adaptive octree construction, multipole expansions of order p with M2M/M2L/L2L translation operators, and a near-field direct pass, so that the total force error against an exact O(N^2) reference stays below a caller-supplied relative tolerance for N up to 200k bodies on a clustered (Plummer) distribution. Expose the expansion order p and opening angle theta, emit an empirical error budget (relative L2 force error and worst-body relative error) and stay bit-for-bit deterministic across repeated runs and worker counts. Ship a self-test that compares against the direct sum on 4k bodies plus an instrumented interaction counter proving the far-field work is sub-O(N^2) as theta grows.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "py-fmm-multipole-translation-error-budget", "provider": "openrouter", "solved_at": "2026-09-27T10:21:20.341Z", "version": "3.11"}
Generated from the verified corpus · MIT licensedBack to the catalog