Uh oh!
There was an error while loading. Please reload this page.
Fix weight window energy defaults being derived before the particle type is known - #4091
Merged
Merged
Conversation
GuyStenforce-pushed
the
ww-particle-type-fix
branch
from
August 31, 2026 19:05
aaf8210 to
78adf76CompareGuySten
marked this pull request as ready for review
August 31, 2026 19:54
5 tasks
paulromano
approved these changes
Sep 1, 2026
paulromano
enabled auto-merge (squash)
September 1, 2026 13:26
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
Fix weight window energy defaults being derived before the particle type is known
Scope: this affects weight windows created through the C API during a
simulation without an explicit
energy_bounds. Photon ones previously receivedneutron default energy bounds; they now receive photon ones. Weight windows
defined in XML are unaffected, as are any with an explicit
energy_bounds, asare any created before
openmc_simulation_init().Problem
WeightWindows::set_defaults()derives the default energy grid fromdata::energy_min/maxfor the object's particle type, and only does so when thegrid is empty:
The
WeightWindows(int32_t id)constructor, which is the one the C API andopenmc.libuse, called it immediately:ParticleTypedefault-constructs toPDG_NEUTRON, so this does not error. Itsilently derives neutron bounds.
set_particle_type()does not re-derive them,and because the grid is no longer empty every subsequent
set_defaults()callis a no-op. So the early call does not merely compute the wrong answer, it
prevents the right one from ever being computed.
The XML constructor is not affected: it assigns
particle_type_before callingset_defaults()at the end.The window in which this is observable is narrower than it first appears.
data::energy_min/maxare populated byinitialize_data(), which runs fromopenmc_simulation_init(), notopenmc_init(). Before simulationinitialization both arrays hold their static defaults of
0.0andINFTYforevery particle, so the neutron and photon defaults coincide and nothing is
visibly wrong. The bug bites weight windows constructed through the C API once a
simulation is running -- which is the regime weight window generation operates
in, and is why
WeightWindowsGeneratorneeds its workaround.Two things in the existing code point at this:
WeightWindowsGenerator::WeightWindowsGeneratorcallsset_defaults()againafter
set_mesh(),set_energy_bounds()andset_particle_type(). That is aworkaround, and it only works because the generator constructs through a path
where the grid is still empty.
energy_bounds_.size() == 0guard is what turns an early call into apermanent one.
MWE
Changes
WeightWindows(int32_t id)no longer callsset_defaults(). A commentexplains why, so it is not reinstated.
set_particle_type()callsset_defaults(), which is the point at which thedefaults are actually determined. It is a no-op when an explicit grid has
already been supplied.
openmc_weight_windows_export()callsset_defaults()once per weight windowbefore writing, so objects built through the C API whose particle type was
never set do not export an empty energy grid.
WeightWindowsGeneratordrops its trailingset_defaults()call, which isnow redundant. This is the workaround the bug forced, so its removal is the
clearest demonstration that the fix works.
The XML constructor assigns the particle type through
set_particle_type()instead of writing
particle_type_directly, and drops its own trailingset_defaults(). Two side benefits: the XML path now gets the sameneutron/photon validation as the C API path, and every existing XML-based
weight window test exercises the new call site.
After this,
set_defaults()has exactly two call sites --set_particle_type()and the export backstop -- so every path that assigns a particle type derives
its defaults the same way.
bounds_size()already treats an empty energy grid as one bin:so
set_mesh()continues to allocate a correctly shaped1 x n_spatialboundstensor in the window between construction and the particle type being set.
Testing
New
tests/unit_tests/weightwindows/test_ww_defaults.py, which initializes amodel with both neutron and photon data and calls
simulation_init()so thatdata::energy_min/maxare actually populated:Identical bounds are the signature of the defaults being derived from the
default particle type at construction.
energy_boundsgrid survives a laterset_particle_type().set_particle_type().Notes
Found while reviewing #4057, but it is independent of that PR and reproduces on
develop. It may allow #4057 to drop itsreset_energy_bounds()addition,since a freshly constructed
openmc.lib.WeightWindowswill now pick up correctdefaults on its own once the particle type is assigned.
Checklist
I have followed the style guidelines for Python source files (if applicable)I have made corresponding changes to the documentation (if applicable)