Geometry debug function#4012
Conversation
paulromano
left a comment
There was a problem hiding this comment.
Thanks for the initial shot at this! Here are my initial thoughts:
| z = z0 + (k + 0.5) * dz | ||
| origin = ((x0 + x1) / 2.0, (y0 + y1) / 2.0, z) | ||
|
|
||
| geom_data, _ = openmc.lib.slice_data( |
There was a problem hiding this comment.
You should also use the new slice_data_overlap_info to get information about the overlaps. Rather than storing an explicit list of every coordinate, I would store a mapping of (univ, cell1, cell2) to a bounding box that covers all points found to overlap for that combination of cells.
| self.settings.particles = 2 * int(max_length) | ||
|
|
||
| @staticmethod | ||
| def _classify_undefined_regions(cell_ids: np.ndarray) -> tuple[np.ndarray, np.ndarray, np.ndarray]: |
There was a problem hiding this comment.
I asked GPT-5.6 Sol whether there was a better way to implement this functionality and here is what it told me:
SciPy’s ndimage module is a much better fit ... and SciPy is already an OpenMC dependency.
The simplest replacement is scipy.ndimage.binary_fill_holes:
from scipy import ndimage undefined = cell_ids == _NOT_FOUND # Holes in the defined-pixel mask are internal undefined regions. internal = ndimage.binary_fill_holes(~undefined) & undefined outside = undefined & ~internalIts default connectivity is the same four-neighbor connectivity used by the current BFS. This eliminates the deque, boundary initialization, and Python-level pixel traversal.
In a local benchmark with a representative undefined mask:
Grid Current BFS binary_fill_holes100 × 100 3.8 ms 0.15 ms 500 × 500 95 ms 2.8 ms 1000 × 1000 393 ms 11 ms That’s roughly 25–35× faster, although slice_data() may still dominate the overall geometry_debug() runtime.
Another semantically direct option is binary_propagation, seeded with undefined boundary pixels. It performed about the same but requires more setup. ndimage.label was fastest in my benchmark—around 3.5 ms at 1000²—but requires additional logic to identify labels touching the boundary and allocates an integer label array.
My recommendation for the PR would therefore be binary_fill_holes: it is considerably shorter, preserves the existing connectivity semantics, adds no dependency, and moves the expensive traversal into compiled SciPy code.
| Lower-left corner of the sampled 3D region. | ||
| upper_right : Sequence[float] | ||
| Upper-right corner of the sampled 3D region. | ||
| n_samples : int or Sequence[int] |
There was a problem hiding this comment.
Elsewhere in the API, when the user provides a single number of samples/points, it gets distributed over the three dimensions rather than being used for each dimension.
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():
Added _classify_undefined_regions() helper:
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