Skip to content

Disambiguate colliding mesh, filter, and tally IDs in get_microxs_and_flux - #4257

Open
rpinho wants to merge 1 commit into
openmc-dev:developfrom
rpinho:fix-3549-microxs-mesh-ids
Open

rpinho wants to merge 1 commit into
openmc-dev:developfrom
rpinho:fix-3549-microxs-mesh-ids

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

openmc.deplete.get_microxs_and_flux writes a model's settings meshes before its tally meshes and silently drops duplicate mesh IDs in mesh_memo during XML export. When a domain mesh passed to get_microxs_and_flux shares an ID (for example mesh_id=1, or any ID reused after openmc.reset_auto_ids()) with a weight-window mesh, another settings mesh, or a mesh on a retained model tally, the tally mesh definition is omitted and the tally silently scores on the wrong mesh (or fails in split-XML mode). Live filter and tally IDs can also collide with the temporary filters and tallies created by get_microxs_and_flux when IDs were reset after building the model.

Who hits this: OpenMC users calling openmc.deplete.get_microxs_and_flux(model, domains=mesh, ...) on a model that already defines a weight-window mesh, another settings mesh, or a retained tally mesh with the same ID, or after openmc.reset_auto_ids() (#3549).

Changes:

  • Add Settings._iter_meshes() in openmc/settings.py yielding meshes referenced by settings XML (sources, entropy_mesh, temperature_field, ufs_mesh, weight_windows, weight_window_generators, and random_ray['source_region_meshes']).
  • Add _next_id(cls, occupied_ids) and _disambiguate_mesh_filters(...) in openmc/deplete/microxs.py to copy and assign distinct run-local IDs to colliding domain meshes (and their MeshFilter copies) and reserve live filter and tally IDs before creating temporary energy filters, domain filters, and MicroXS tallies.
  • Preserve caller-owned meshes, filters, weight windows, and model.tallies, restoring model.tallies in a finally block even if execution raises.
  • Add unit tests in tests/unit_tests/test_deplete_microxs.py covering weight-window/settings/retained-tally mesh ID collisions, openmc.reset_auto_ids() filter and tally ID collisions, combined and split XML export, and finally restoration.

Scope note: settings.weight_windows_file (HDF5 path-only weight windows without in-memory Settings.weight_windows objects) is out of scope for this PR.

Verified locally: all 47 tests in tests/unit_tests/test_deplete_microxs.py pass (with the new regression tests failing before the patch), combined/split XML and openmc.lib transport outputs match distinct-ID controls bit-for-bit, and 349 non-ENDF unit tests across weightwindows/, test_mesh.py, test_tallies.py, test_model.py, and test_deplete*.py pass with 0 regressions.

Fixes #3549

AI Assistance

This fix and its tests were found, written, and cross-checked with AI agents, including an independent clean-room verification pass from a separate model family before opening this PR.

  • Harness: Codex CLI (initial diagnosis and patch); Antigravity CLI (ID-unification polish and tests); Claude Code (independent clean-room reproduction and verification)
  • Model: GPT-6.2-Sol (Codex); Gemini 4 Neon (Antigravity); Claude Sonnet 5.5 (Claude Code)
  • Reasoning effort: high (all)

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)

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

Summary by CodeRabbit

  • Bug Fixes
    • Improved the reliability of micro cross section and flux calculations when mesh or tally IDs overlap with existing model settings or tallies.
    • Preserved existing model tallies after calculations, and ensured generated IDs remain valid after ID resets.
    • Added validation for energy group names before lookup.

@rpinho
rpinho requested a review from paulromano as a code owner November 26, 2026 03:46
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

get_microxs_and_flux now avoids ID conflicts between domain meshes, settings meshes, filters, and tallies. It conditionally includes model tallies in the run and restores the original tally collection after transport and statepoint processing, including when an exception occurs.

Changes: MicroXS mesh and tally handling

Layer / File(s) Summary
Mesh and filter ID handling
openmc/settings.py, openmc/deplete/microxs.py, tests/unit_tests/test_deplete_microxs.py
Settings._iter_meshes yields meshes referenced by configured settings. The microXS path assigns unoccupied filter and tally IDs and copies domain mesh filters when their meshes conflict with settings or retained tally meshes. Tests cover mesh and filter ID conflicts, including after ID resets.
Tally collection and transport lifecycle
openmc/deplete/microxs.py, tests/unit_tests/test_deplete_microxs.py
The run tally collection includes model tallies only when requested. A finally block restores the original collection after transport and result processing. Tests check tally ID handling after ID resets.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: jtramm

Merge Risk: Minimal · up to eb534

The change prevents mesh, filter, and tally ID collisions in get_microxs_and_flux and restores the model's tallies after the run, including on failure. No actionable merge-blocking risk was found. The remaining comment is a minor test-fixture consistency suggestion.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: colliding mesh, filter, and tool IDs in get_microxs_and_flux are disambiguated. The changeset adds ID reservation and copying of colliding domain meshes in openmc/depl…
Description check Passed The description follows the repository template. It has a Description section with a summary, motivation, and a link to issue #3549, and it includes a 'Fixes #3549' line. It has an AI Assistance secti…
Linked Issues check Passed [#3549] Requirement: when a model has weight windows, get_microxs_and_flux must use the mesh passed through domains, not an existing mesh with the same ID. At the reviewed head, `_disambiguate_mes…
Out of Scope Changes check Passed The changes in openmc/deplete/microxs.py, openmc/settings.py, and tests/unit_tests/test_deplete_microxs.py serve the ID-collision fix. Settings._iter_meshes() enumerates settings meshes for co…
Full details: Title check

Explanation

The title names the main change: colliding mesh, filter, and tool IDs in get_microxs_and_flux are disambiguated. The changeset adds ID reservation and copying of colliding domain meshes in openmc/deplete/microxs.py, plus a settings mesh iterator, which matches the title. The title is specific and concise.

Full details: Description check

Explanation

The description follows the repository template. It has a Description section with a summary, motivation, and a link to issue #3549, and it includes a 'Fixes #3549' line. It has an AI Assistance section that lists harness, model, and reasoning effort. The checklist is filled in, with some items left unchecked. The description is detailed and covers the scope note, the verification steps, and the tests added.

Full details: Linked Issues check

Explanation

[#3549] Requirement: when a model has weight windows, get_microxs_and_flux must use the mesh passed through domains, not an existing mesh with the same ID. At the reviewed head, _disambiguate_mesh_filters in openmc/deplete/microxs.py checks each domain MeshFilter mesh ID against meshes from Settings._iter_meshes() (which covers weight_windows and weight_window_generators), and against meshes on retained model tallies. On a collision, it copies the domain mesh and its MeshFilter, assigns a fresh run-local mesh ID and filter ID, and uses the copy in the temporary flux and reaction-rate tallies. Caller objects are not changed. Unit tests named in the PR cover weight-window mesh collisions in explicit, automatic, shared, and reset-ID modes, and check the exported XML references. The fix addresses the reported ID-collision mechanism. Residual limit: the PR excludes settings.weight_windows_file (path-only HDF5 weight windows). The issue workaround mentions editing ww.h5. The reproduction in the issue states only that weight windows are applied, so the excluded path does not block this verdict, but it is a known gap for file-based workflows.

Full details: Out of Scope Changes check

Explanation

The changes in openmc/deplete/microxs.py, openmc/settings.py, and tests/unit_tests/test_deplete_microxs.py serve the ID-collision fix. Settings._iter_meshes() enumerates settings meshes for collision checks. _next_id and the filter and tally ID reservation prevent collisions after openmc.reset_auto_ids(). The try/finally block restores model.tallies, which the new temporary tallies require. Moving transport and statepoint processing inside that block supports the same restoration. Two smaller changes are not tied to ID collisions: energy group names are checked with check_value before lookup in GROUP_STRUCTURES, and the energies string path no longer calls EnergyFilter.from_group_structure. These are minor validation and refactoring changes. They do not alter the scope of the linked issue, but a reviewer should confirm that the group-structure lookup gives the same bins as the previous path.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Additional comments: 1

tests/unit_tests/test_deplete_microxs.py: 1

199-199: Trivial · Maintainability & Code Quality · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Add the required run_in_tmpdir fixture.

These four tests create XML files but request only tmp_path. Add run_in_tmpdir to each test signature. Keep tmp_path for the explicit export paths. The fixture also isolates the working directory and restores it after failure. (raw.githubusercontent.com)

As per coding guidelines, “Use run_in_tmpdir fixture for tests that create files.”

Also applies to: 281-281, 363-363, 472-472

Source: Coding guidelines

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 81b1ceb8-03a2-4284-b2b4-82f0ea9166d9
Commits

Reviewing files that changed from the base of the PR and between 5d63501 and eb5344e.

Files selected for processing: 3

  • openmc/deplete/microxs.py
  • openmc/settings.py
  • tests/unit_tests/test_deplete_microxs.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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.

get_microxs_and_flux Incorrect Meshbase

1 participant