Skip to content

Fix division by zero when integrating Tabulated1D with discontinuities - #47

Open
davrens101 wants to merge 4 commits into
paulromano:mainfrom
davrens101:fix-discontinuous-integrate
Open

davrens101 wants to merge 4 commits into
paulromano:mainfrom
davrens101:fix-discontinuous-integrate

Conversation

@davrens101

Copy link
Copy Markdown

Summary

In ENDF-6 format files, TAB1 records represent step discontinuities (e.g. cross-section jumps at reaction thresholds or resonance region boundaries) by repeating coordinate points ($x_i = x_{i+1}$) across adjacent interpolation regions.

When evaluating Tabulated1D.integral() or Tabulated1D.integrate() over such a function, Tabulated1D._integrate() encountered division by zero in the linear-linear ($p=2$), linear-log ($p=3$), and log-log ($p=5$) integration branches because x1 - x0 == 0. This produced NaNs and RuntimeWarning: divide by zero warnings.

Proposed Changes

  • In Tabulated1D._integrate():
    • For scalar bounds with $x_1 - x_0 = 0$, return 0.0.
    • For array bounds with zero-width entries ($dx = 0$), compute slopes and logarithm ratios only on non-zero width intervals (dx != 0) and preserve 0.0 for zero-width entries.
  • Add regression test test_integrate_discontinuous in tests/test_function.py.

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 HunterBelanger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @davrens101, thanks for fixing this bug that I inadvertently introduced with the integration functionality. I have one suggestion to simplify your correction.

Comment thread src/endf/function.py Outdated
Comment on lines 341 to 344
@@ -345,14 +345,35 @@ def _integrate(p, x0, y0, x1, y1):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

davrens101 and others added 3 commits November 11, 2026 20:49
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().

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants