Repository navigation
Description
Overview
NFWTruncatedSph loses precision for grid points near its scale radius, x = r/r_s ≈ 1. The NFW auxiliaries g(x) = (1 − f)/(x² − 1) and f(x) = arccos(1/x)/√(x² − 1) (or the arccosh form inside) are each ratios of two vanishing quantities there, so the closed forms cancel. Reproduced on main 5b85fbf (kappa_s=0.1, r_s=1, r_t=5): convergence off by up to 9e13 relative at x = 1 − 1e-15, float64 deflection up to 0.18 relative, float32 (JAX) deflection up to 0.098; jax.grad of the deflection at exactly x = 1 is wrong because f has a constant branch there.
Plan
- Write u = 1 − x², computed as (1 − x)(1 + x) so it is exact near x = 1.
- Use the series g = Σ uᵏ/(2k + 3) and f = 1 + u·g. One series covers both sides of x = 1, and real, complex and JAX inputs alike, because it is analytic in u.
- Apply it inside |u| < 0.1 in the shared
coord_func_f_fromandAbstractgNFW.coord_func_g. The tNFW convergence (reads f and g), the tNFW deflection (reads f), andNFWSph's g-based convergence all inherit the fix. Keep the closed forms outside the window and mask their inputs, so JAX gradients stay finite. - Add the Witness test: convergence and deflection at x = 1 ± 10⁻ᵏ against mpmath, JAX float32 deflection, and
jax.gradat x = 1. - Tier + merge mode:
effective level: supervised; merge mode: auto-merge on green if the Witness passes after a merge slot
Detailed implementation plan
Work Classification: Library
Worktree root: ~/Code/PyAutoLabs-wt/nfwtruncatedsph-near-scale-radius/
Affected Repositories
- PyAutoGalaxy (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| PyAutoGalaxy (task worktree) | feature/nfwtruncatedsph-near-scale-radius @ origin/main 5b85fbf | clean |
Open sibling PRs in this shift: PyAutoGalaxy#735 (edits concentration/mass_at_200 docstrings in abstract.py and the total-mass method in nfw_truncated.py) and #738 (cnfw.py, mass/abstract/abstract.py). This change edits different hunks (coord_func_f_from, coord_func_g), so a clean merge is expected.
Suggested branch: feature/nfwtruncatedsph-near-scale-radius
Implementation Steps
autogalaxy/profiles/mass/dark/abstract.py: add a module-levelcoord_func_g_series_from(u, xp)(Horner over 16 terms of uᵏ/(2k + 3); truncation error < 1e-16 for |u| ≤ 0.1) and a window constant. Incoord_func_f_from, computeu = (1 − r)(1 + r)andnear = |u| < 0.1. Return1 + u·g_series(u)wherenearholds, and mask the arccos/arccosh branch inputs away from the window. InAbstractgNFW.coord_func_g, returng_series(u)wherenearholds and the closed forms elsewhere.- Nothing changes in
nfw_truncated.py:coord_func_m_from(deflection) andcoord_func_l(convergence) read the shared functions. - New
test_autogalaxy/profiles/mass/dark/test_nfw_truncated_scale_radius.py, the Witness: convergence at x = 1 ± 10⁻ᵏ (k = 3…15) agrees with the value at x = 1 to 1e-6 (float64);deflections_yx_2d_fromandradial_deflection_fromagree with an mpmath reference to 1e-10 (float64) and 1e-4 (JAX float32);jax.gradof the deflection at exactly x = 1 agrees with the mpmath derivative to 1e-6. Add g/f unit checks against mpmath on both sides of the window edge. - Run the existing dark-profile tests (
test_nfw.py,test_nfw_truncated*.py,test_gnfw.py,test_abstract.py). Inside |u| < 0.1 their values shift only at round-off level.
Key Files
autogalaxy/profiles/mass/dark/abstract.py:coord_func_f_fromandAbstractgNFW.coord_func_gautogalaxy/profiles/mass/dark/nfw_truncated.py: consumers (no edit)
Original Prompt
Click to expand starting prompt
NFWTruncatedSph convergence loses precision near r = r_s
Type: bug
Target: PyAutoGalaxy
Repos:
- PyAutoGalaxy
Difficulty: small
Autonomy: supervised
Priority: low
Status: formalised
Consequence: glance
Witness: A unit test evaluatesNFWTruncatedSph.convergence_2d_fromat r/r_s = 1 ± 10^-k for k = 3 … 15 (and exactly 1) and asserts every value agrees with the value at r/r_s = 1 to 1e-6 relative in float64; and the same fordeflections_yx_2d_from/radial_deflection_from(float64 to 1e-10, jax float32 to 1e-4 relative against an mpmath reference), andjax.gradof the deflection at exactly r/r_s = 1 agrees with the mpmath derivative to 1e-6.
Review-minutes: 3
Unattended: ready
Verified: 2026-11-19 5b85fbf (PyAutoGalaxy)
Lane: any
Finding
@PyAutoGalaxy/autogalaxy/profiles/mass/dark/nfw_truncated.py NFWTruncatedSph.coord_func_l
uses coord_func_g from @PyAutoGalaxy/autogalaxy/profiles/mass/dark/abstract.py, which forms
g(x) = (1 − f(x)) / (x² − 1). Near x = r/r_s = 1 both numerator and denominator go to zero and the
subtraction cancels. Measured on PyAutoGalaxy main 2ec0e36, 2026-11-19, with kappa_s=0.1,
scale_radius=1, truncation_radius=5 (relative error against the value at exactly x = 1):
|x-1| = 1e-5: 1.4e-5 1e-6: 2.7e-5 1e-7: 8.2e-3 1e-8: 0.32
x = 1 ± 1e-15: convergence negative; x = 1 - 1e-12: 5.7e4 (garbage)
Found while writing the total-mass witness for PyAutoGalaxy#734: a log-spaced radial grid with a
node at x = 1 + 1.3e-15 made a 2D mass integral 4e-4 low. The same coord_func_g serves
NFWSph / gNFW; check whether they show it too.
Why it matters
Any grid point within ~1e-6 r_s of a halo's scale radius gets a wrong convergence (and the
deflection's coord_func_m should be checked the same way). Rare for one halo, routine for
a line-of-sight population of thousands of halos on an oversampled grid.
Ask
- Replace g(x) near x = 1 by its Taylor series (g(1) = 1/3, g'(1) known) within |x − 1| < ~1e-3,
for real and JAX inputs; keep the complex continuation the gNFW family uses. - Check
coord_func_m_from(deflection) at the same radii. - Add the witness test.
Deflection near r = r_s — adversary measurements (2026-11-19)
The independent adversary review (Fable 5.2) of PyAutoGalaxy#737 (shift 2026-11-19-pm, finding F1)
measured the same cancellation in the deflection closed form (nfw_truncated.py:92-95, the
x ≥ 0.5 branch #737 leaves unchanged), identical on main:
float64: 4e-13 at x = 1.0004, 5.6e-11 at x = 0.999999
float32: 2.5e-4 at x = 0.9999 / 1.0001, 1.9e-3 at x = 0.99995, 9.5e-2 at x = 0.999999
(~0.2% of points in 0.5 ≤ x ≤ 5 exceed 1e-5)
jax.grad at exactly x = 1: d/dr 0.2715 vs true 0.00747 — from the `x == 1` constant
branch in `coord_func_f_from`
So a float32 GPU line-of-sight run is still up to ~10% wrong for grid points within ~1e-6 r_s of
a halo's scale radius. Ask step 2 now covers this: series-expand the deflection near x = 1 too,
for real and JAX float32 inputs, and give the x = 1 branch the correct derivative. Land after
PyAutoGalaxy#737 (it rewrites the x < 0.5 branch of the same function).
Provenance
[from: shift worker 2026-11-19-pm, task nfwtruncatedsph_total_mass_method_omits_the_nfw] found
while writing the PyAutoGalaxy#734 witness test.
[from: independent adversary review, Fable 5.2, shift 2026-11-19-pm] deflection measurements on PyAutoGalaxy#737 (F1).
Activity
Jammy2211 commentedon Nov 20, 2026
Shipped
Merged 2026-11-20 via the human's
/prm(shift 2026-11-19-pm2): #740The NFW coordinate functions f(x) and g(x) now use a series in u = (1−x)(1+x) within |1−x²| < 0.1, so
NFWTruncatedSphconvergence and deflection are exact to round-off near r = r_s (real, complex and JAX inputs), andjax.gradat r = r_s is correct. New public names:SCALE_RADIUS_SERIES_WINDOW,coord_func_g_series_from,coord_func_u_from.Witness vs 50-digit mpmath (kappa_s=0.1, r_s=1, r_t=5): convergence error 8.9e13 → <1e-10; float64 deflection 0.18 → 2.2e-15; JAX float32 deflection 0.098 → 1.4e-6.
test_nfw_truncated_scale_radius.py9 passed (8/9 fail on main). Decision taken (accepted at /prm): the literal k = 3…15 continuity check is unsatisfiable for k ≲ 6, so continuity is checked for |x−1| < 1.5e-7 and all 27 points against mpmath at 1e-10. Tests mass/dark + mass/abstract 341 passed; independent Opus 5.5 review CLEAN.