Skip to content

Fix Resetting of Auto IDs When Generating MGXS - #3437

Merged
paulromano merged 2 commits into
openmc-dev:developfrom
jtramm:update_auto_ids2
Jun 13, 2025
Merged

paulromano merged 2 commits into
openmc-dev:developfrom
jtramm:update_auto_ids2

Conversation

@jtramm

@jtramm jtramm commented Jun 11, 2025

Copy link
Copy Markdown
Contributor

Description

Currently if you load a model from an xml file, any subsequent surfaces you add to the model will start from index 1, often conflicting with existing surfaces. This PR adds a check after loading materials and geometry objects from XML to set the ID to one above the largest currently in the model.

This use case comes up with random ray, where we might want to load an existing .xml model but then place it inside a smaller box with vacuum boundaries, which involves the creation of additional cells and surfaces.

This PR also adds a more careful treatment of checking for unnamed materials.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 15) 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)

@jtramm
jtramm requested a review from Copilot June 11, 2025 01:45

Copilot AI 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.

Pull Request Overview

This PR ensures that after loading materials and geometry from XML, any new objects receive IDs higher than existing ones to avoid conflicts. Key changes include:

  • Introduction of update_auto_ids to sync ID counters based on loaded IDs.
  • Removal of reset_auto_ids calls from MGXS generation routines.
  • Enhanced handling of unnamed or whitespace-only material names.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
openmc/model/model.py Removed obsolete reset_auto_ids calls from MGXS generation methods
openmc/mixin.py Added update_auto_ids to update next_id for all IDManagerMixin subclasses
openmc/material.py Imported and invoked update_auto_ids in Materials.from_xml_element
openmc/geometry.py Imported and invoked update_auto_ids in get_universe to sync IDs after geometry load
Comments suppressed due to low confidence (5)

openmc/model/model.py:1554

  • After loading a pre-existing model, new MGXS routines may still generate conflicting IDs; consider calling update_auto_ids() before constructing the new Model to ensure next_id is set above any loaded IDs.
model = openmc.Model()

openmc/model/model.py:1751

  • The removal of reset_auto_ids() here requires an explicit call to update_auto_ids() to avoid ID collisions when adding new objects in the stochastic slab MGXS generation.
model = openmc.Model()

openmc/model/model.py:1862

  • In the material-wise MGXS method, consider invoking update_auto_ids() after deep-copying self so that new tallies and cells receive non-conflicting IDs.
model = copy.deepcopy(self)

openmc/mixin.py:120

  • Add unit tests for update_auto_ids() to verify that next_id is correctly set above the maximum of used_ids in typical XML-load scenarios.
def update_auto_ids():

openmc/mixin.py:120

  • [nitpick] The name update_auto_ids might be ambiguous; consider renaming to sync_next_ids or refresh_auto_ids to clearly differentiate it from the old reset_auto_ids behavior.
def update_auto_ids():

Comment thread openmc/material.py Outdated
@jtramm
jtramm requested a review from paulromano June 11, 2025 13:33

@paulromano paulromano 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.

@jtramm Even though next_id will indeed still be at 1 assuming the model you loaded didn't use ID 1, this shouldn't be a problem because when new objects are created, the id setter from IDManagerMixin checks whether next_id is already within the set of used_ids and increments it until it finds one that isn't used. So, to my understanding there shouldn't be any conflicts. Are you finding otherwise?

@jtramm

jtramm commented Jun 12, 2025

Copy link
Copy Markdown
Contributor Author

Yes, I was getting conflicts in the JET model which motivated this. However, I'm now wondering if the other change I made in this PR to remove the calls to:

openmc.reset_auto_ids()

in openmc.convert_to_multigroup() might have been all that was needed. Let me double check...

@jtramm jtramm changed the title Automatically generate valid object IDs after loading from XML Fix Resetting of Auto IDs When Generating MGXS Jun 12, 2025
@jtramm

jtramm commented Jun 12, 2025

Copy link
Copy Markdown
Contributor Author

Indeed -- the problem was being caused in model.convert_to_multigroup, and the other changes in this PR are sufficient to fix the issue. The change to the IDManagerMixin is not necessary. Great catch, @paulromano!!

@paulromano paulromano 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.

Sweet, LGTM now!

@paulromano
paulromano enabled auto-merge (squash) June 12, 2025 22:43
@paulromano
paulromano merged commit aeb1052 into openmc-dev:develop Jun 13, 2025
yardasol pushed a commit to yardasol/openmc that referenced this pull request Dec 25, 2025
apingegno pushed a commit to apingegno/openmc that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants