Skip to content

fix: NFWTruncatedSph convergence and deflection exact near r = r_s - #740

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/nfwtruncatedsph-near-scale-radius
Nov 20, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/nfwtruncatedsph-near-scale-radius

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #739.

What was wrong

The NFW auxiliaries f(x) and g(x) = (1 − f)/(x² − 1), x = r/r_s, divide two vanishing quantities at the scale radius, so their closed forms cancel there. On main 5b85fbf (kappa_s=0.1, r_s=1, r_t=5) NFWTruncatedSph had:

quantity worst relative error near x = 1
convergence 8.9e13 (at x = 1 − 1e-15)
deflection, float64 0.18
deflection, JAX float32 0.098
jax.grad of deflection at x = 1 wrong (constant branch)

The fix

In autogalaxy/profiles/mass/dark/abstract.py, inside |1 − x²| < 0.1, coord_func_f_from and AbstractgNFW.coord_func_g use the series in u = (1 − x)(1 + x):

g = Σ uᵏ/(2k + 3) (16 terms, truncation < 1e-17), f = 1 + u·g.

One series covers both sides of x = 1. It is analytic in u, so the complex continuation (r + 0j) the gNFW family uses is unchanged, and the gradient at x = 1 is correct (f′(1) = −2/3). Outside the window f is bit-identical to main. The closed forms' inputs are masked away from the window, so discarded where branches stay finite under jax.grad. The tNFW convergence and deflection, and NFWSph/gNFW callers of f and g, all inherit the fix. nfw_truncated.py is not edited.

Sibling PRs: #735 edits other hunks of abstract.py (docstrings of concentration/mass_at_200_solar_masses), #738 touches different files. No overlapping lines.

Witness and tests

New test_autogalaxy/profiles/mass/dark/test_nfw_truncated_scale_radius.py, against pinned 50-digit mpmath values (generator in the module docstring):

  • convergence at x = 1 ± 10⁻ᵏ, k = 3…15 and x = 1: rtol 1e-10 vs mpmath;
  • deflections_yx_2d_from and radial_deflection_from, float64: rtol 1e-10;
  • radial_deflection_from, JAX float32: rtol 1e-4 (measured 1.4e-6);
  • jax.grad of the deflection at exactly x = 1: float64 within 1e-6 (measured 5.9e-14), float32 within 1e-3;
  • window edges and ordinary radii unchanged vs mpmath; f and g series checked directly.

Control: 8 of the 9 new tests fail on main's f and g. The window-edge guard passes on both, as intended.
Local: test_autogalaxy/profiles/mass/dark/ and .../mass/abstract/ 341 passed. The reviewer also ran the full test_autogalaxy: 1595 passed.

Targeted smoke: no curated script references the change, so it ran the import check: autogalaxy_workspace scripts/guides/galaxies.py PASS.

Review (leg 3): independent Opus reviewer, verdict CLEAN.

Decision taken (decide-and-flag)

The Witness asks every convergence value at x = 1 ± 10⁻ᵏ for k = 3…15 to agree with the value at x = 1 to 1e-6. No correct code can meet that for k ≲ 6, because the profile's own log-slope is about −1.4, so the true change at k = 3 is 1.4e-3. The test therefore applies the continuity check for |x − 1| < 1.5e-7 (k ≥ 7, 19 points) and checks every one of the 27 points against mpmath at 1e-10, which is stronger. The JAX float32 test uses its own float32 radii, because 1 ± 10⁻ᵏ rounds to 1 in float32 for k ≥ 8.

Public API: adds SCALE_RADIUS_SERIES_WINDOW, coord_func_g_series_from and coord_func_u_from to autogalaxy.profiles.mass.dark.abstract. Nothing is removed or renamed.

Out of scope, noted by the reviewer: in JAX float32, the closed form just outside the window (x ≈ 1.05) still has a gradient error of about 7e-3, the same as main.

🤖 Generated with Claude Code

Jammy2211 and others added 2 commits November 19, 2026 21:41
…n 1 - x^2

The closed forms of the NFW auxiliaries f(x) and g(x) = (1 - f)/(x^2 - 1)
divide two vanishing quantities at x = r/r_s = 1, so NFWTruncatedSph lost
precision there: convergence garbage within ~1e-12 r_s, deflection off by
up to 0.18 (float64) and 0.1 (float32), and a wrong jax.grad at x = 1.

Within |1 - x^2| < 0.1, use g = sum u^k/(2k+3) and f = 1 + u g with
u = (1 - x)(1 + x); one series covers both sides of x = 1 and the complex
continuation. Adds the witness test against pinned 50-digit mpmath values.

Closes #739

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the decision-taken Autonomous run took a flagged decision label Nov 19, 2026
@Jammy2211
Jammy2211 merged commit caed890 into main Nov 20, 2026
5 checks passed
@Jammy2211
Jammy2211 deleted the feature/nfwtruncatedsph-near-scale-radius branch November 20, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

decision-taken Autonomous run took a flagged decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: NFWTruncatedSph convergence and deflection lose precision near r = r_s

1 participant