Skip to content

Geometry debug function - #4012

Open
viktormai wants to merge 40 commits into
openmc-dev:developfrom
viktormai:geometry-debug-function
Open

Geometry debug function#4012
viktormai wants to merge 40 commits into
openmc-dev:developfrom
viktormai:geometry-debug-function

Conversation

@viktormai

Copy link
Copy Markdown
Contributor

This PR adds a 3D geometry debugging utility to Model for identifying overlap and undefined regions within an OpenMC geometry by sampling a user-defined bounding box.

Main changes

Added Model.geometry_debug():

  • Samples a 3D region using a stack of 2D slices obtained through openmc.lib.slice_data.
  • Detects both overlap regions (using the overlap sentinel) and internal undefined regions, returning a summary containing the total number of overlap and undefined samples and example coordinates for each type of geometry issue.
  • Optionally prints a human-readable summary.

Added _classify_undefined_regions() helper:

  • Separates undefined pixels into boundary-connected ("outside") undefined regions and internal undefined regions enclosed within the geometry.
  • Uses a breadth-first search starting from the image boundary to identify boundary-connected undefined pixels.
  • Issues a warning when undefined regions exist but none are connected to the sampled slice boundary, indicating a potentially enclosed undefined region or insufficient sampling resolution.

This pair of functions provides a simple way to locate common geometry construction problems—particularly overlaps and enclosed undefined regions—by returning representative coordinates that users can inspect directly, rather than relying solely on transport failures or visual inspection. These sample points can also be leveraged by AI agents to easily fix geometry issues in the model. Reusable undefined-region classification logic that can be leveraged by future plotting and geometry diagnostics for plotting undefined regions is also established.

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)

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

Thanks for the initial shot at this! Here are my initial thoughts:

Comment thread openmc/model/model.py
Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py
@viktormai

Copy link
Copy Markdown
Contributor Author

Thanks for checking it out @paulromano. I have added some updates for the bounding box idea for both overlapping and undefined regions, including a warning if undefined regions are under-resolved. Let me know what you think.

@viktormai
viktormai marked this pull request as ready for review July 22, 2026 15:10

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

Thanks for the updates @viktormai! Here is another round of comments:

Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py Outdated
Comment thread openmc/model/model.py Outdated

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

Thanks for the updates @viktormai! This is getting closer. Now that it is looking better, you should also add some unit tests for the capability to ensure it works properly.

Comment thread openmc/model/model.py
Comment on lines +2963 to +2964
result : dict
Dictionary summarizing the sampled geometry.

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.

This doesn't really tell the user how to interpret the dict that's returned. There should be a description of the (key, value) pairs in the dictionary.

Comment thread openmc/model/model.py
Comment on lines +3044 to +3045
xc = x0 + (pix[:, 1] + 0.5) * (x1 - x0) / nx
yc = y1 - (pix[:, 0] + 0.5) * (y1 - y0) / ny

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.

This looks like it just uses the pixel centers. I think it would be more conservative to use the bounds of the pixel itself rather than just its center (if I'm interpreting it correctly, this is already how it works for the undefined pixels?).

Comment thread openmc/model/model.py
Comment on lines +3106 to +3109
for box in overlap_boxes + undefined_boxes:
bbox = box["bbox"]
bbox.lower_left = np.round(bbox.lower_left, 4)
bbox.upper_right = np.round(bbox.upper_right, 4)

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.

While it's OK to round numbers in the output if print_summary=True (looks like that's already the case), I wouldn't modify the data that is actually returned in the dictionary

Comment thread openmc/model/model.py
Comment on lines +3116 to +3117
"n_overlaps": len(overlap_boxes),
"n_undefined_regions": len(undefined_boxes),

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.

These are easily inspectable from the data itself and can be removed IMO

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