Repository navigation
Fix division by zero when integrating Tabulated1D with discontinuities - #47
davrens101 wants to merge 4 commits into
Conversation
ENDF-6 TAB1 functions represent physical discontinuities (such as reaction thresholds or resonance boundaries) by repeating coordinate points (x_i == x_{i+1}) across region boundaries. In Tabulated1D._integrate(), computing slopes or logarithmic ratios on zero-width bins resulted in division by zero (producing NaN and runtime warnings) in both integral() and integrate().
Check for zero-width bins in _integrate() and return zero contribution across the zero-width interval.
Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com>
HunterBelanger
left a comment
There was a problem hiding this comment.
Hi @davrens101, thanks for fixing this bug that I inadvertently introduced with the integration functionality. I have one suggestion to simplify your correction.
| @@ -345,14 +345,35 @@ def _integrate(p, x0, y0, x1, y1): | |||
There was a problem hiding this comment.
Instead of the other modifications you made, I think it would be simpler to add a single check at the beginning of the method:
if x1 == x0:
return 0.
That way the remainder of the logic in this method doesn't need to be changed.
There was a problem hiding this comment.
Thanks for the suggestion. Applied your simpler early return (if x1 == x0: return 0. at the top of _integrate()), dropping the per-interpolation checks while keeping regression test test_integrate_discontinuous. Tests pass with PYTHONPATH=src (6 passed). Pushed as bb93987 on fix-discontinuous-integrate.
Simplify Tabulated1D._integrate() to return 0. at the top when x1 == x0, avoiding division by zero on zero-width bins (ENDF-6 TAB1 discontinuities) without changing the remaining per-interpolation logic. Keep regression test test_integrate_discontinuous.
The reviewer-requested early return 'if x1 == x0: return 0.' works for scalar bounds (integrate) but crashes for array bounds (integral) because x1 == x0 returns an ambiguous array. Handle both scalar and array zero-width bins so integral() over ENDF-6 TAB1 step discontinuities no longer raises ValueError.
The p5 branch used 'np.expm1(z) / z if z != 0. else 1.', where z is always an array after masked indexing (x0n/x1n/y0n/y1n). For array bounds with 2+ p5 intervals this raises 'truth value of an array is ambiguous'. Switch to the same np.divide(out=..., where=...) pattern already used for p4, preserving the 1.0-at-z==0 limit and scalar fast-path via out.item().
Summary
In ENDF-6 format files,$x_i = x_{i+1}$ ) across adjacent interpolation regions.
TAB1records represent step discontinuities (e.g. cross-section jumps at reaction thresholds or resonance region boundaries) by repeating coordinate points (When evaluating$p=2$ ), linear-log ($p=3$ ), and log-log ($p=5$ ) integration branches because
Tabulated1D.integral()orTabulated1D.integrate()over such a function,Tabulated1D._integrate()encountered division by zero in the linear-linear (x1 - x0 == 0. This producedNaNs andRuntimeWarning: divide by zerowarnings.Proposed Changes
Tabulated1D._integrate():0.0.dx != 0) and preserve0.0for zero-width entries.test_integrate_discontinuousintests/test_function.py.