Repository navigation
fix: NFWTruncatedSph convergence and deflection exact near r = r_s - #740
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
NFWTruncatedSphhad:jax.gradof deflection at x = 1The fix
In
autogalaxy/profiles/mass/dark/abstract.py, inside |1 − x²| < 0.1,coord_func_f_fromandAbstractgNFW.coord_func_guse 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
wherebranches stay finite underjax.grad. The tNFW convergence and deflection, and NFWSph/gNFW callers of f and g, all inherit the fix.nfw_truncated.pyis not edited.Sibling PRs: #735 edits other hunks of
abstract.py(docstrings ofconcentration/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):deflections_yx_2d_fromandradial_deflection_from, float64: rtol 1e-10;radial_deflection_from, JAX float32: rtol 1e-4 (measured 1.4e-6);jax.gradof the deflection at exactly x = 1: float64 within 1e-6 (measured 5.9e-14), float32 within 1e-3;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 fulltest_autogalaxy: 1595 passed.Targeted smoke: no curated script references the change, so it ran the import check:
autogalaxy_workspace scripts/guides/galaxies.pyPASS.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_fromandcoord_func_u_fromtoautogalaxy.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