Skip to content

Avoid NaN standard deviation from roundoff in Tally.std_dev - #4127

Open
GuySten wants to merge 1 commit into
openmc-dev:developfrom
GuySten:claude/zealous-archimedes-izi0od
Open

GuySten wants to merge 1 commit into
openmc-dev:developfrom
GuySten:claude/zealous-archimedes-izi0od

Conversation

@GuySten

@GuySten GuySten commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

The batch variance is formed as sum_sq/n - mean**2, a difference of accumulated sums. When every realization scores nearly the same value the true variance is zero and rounding can leave the difference just below it, so the square root produces NaN and a RuntimeWarning. Because get_pandas_dataframe() ends with df.dropna(axis=1), a single NaN silently removes the whole "std. dev." column from the dataframe.

Clamp the variance at zero, as mean_stdev() in src/output.cpp already does and as keff_std is guarded in src/eigenvalue.cpp.

Handle a single realization separately, where n - 1 is a division by zero. The standard deviation stays NaN for bins that scored, as it was, but the user now gets a statement of the problem rather than numpy's "invalid value encountered in divide"; bins that never scored stay at zero and stay silent.

Fixes #2408

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

The batch variance is formed as sum_sq/n - mean**2, a difference of
accumulated sums. When every realization scores nearly the same value the
true variance is zero and rounding can leave the difference just below it,
so the square root produces NaN and a RuntimeWarning. Because
get_pandas_dataframe() ends with df.dropna(axis=1), a single NaN silently
removes the whole "std. dev." column from the dataframe.

Clamp the variance at zero, as mean_stdev() in src/output.cpp already does
and as keff_std is guarded in src/eigenvalue.cpp.

Handle a single realization separately, where n - 1 is a division by zero.
The standard deviation stays NaN for bins that scored, as it was, but the
user now gets a statement of the problem rather than numpy's "invalid value
encountered in divide"; bins that never scored stay at zero and stay silent.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MGFo4cAtRRZGxr3sWnqNPc
@GuySten
GuySten marked this pull request as ready for review September 12, 2026 11:37
@GuySten GuySten added the Bugs label Sep 12, 2026
@GuySten
GuySten requested a review from paulromano September 12, 2026 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numerical issue for std_dev when converting to a pandas dataframe

2 participants