From 3d16d4b1007fbee2f48e823d08f2ae0ab29aea73 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 19 Jun 2025 22:43:45 +0200 Subject: [PATCH] Adding checks to geometry.plot to avoid material name overlaps (#3458) Co-authored-by: Patrick Shriwise --- openmc/geometry.py | 30 +++++++++++++++++++++-------- tests/unit_tests/dagmc/test_plot.py | 11 +++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/openmc/geometry.py b/openmc/geometry.py index 9b2af7b4a..f56b9b6fe 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -758,13 +758,27 @@ class Geometry: model.geometry = self model.materials = self.get_all_materials().values() - # Add placeholder materials for DAGMCUniverses - universes = self.get_all_universes() - for universe in universes.values(): - if isinstance(universe, openmc.DAGMCUniverse): - for name in universe.material_names: - mat_dag = openmc.Material(name=name) - mat_dag.add_nuclide('H1', 1.0) - model.materials.append(mat_dag) + # collect all the material names from the geometry + all_material_names = {m.name for m in model.materials if m.name is not None} + + # makes a placeholder material for each material name if it isn't + # already present on the model. These materials are otherwise missing + # from the geometry and are needed for plotting. + for universe in model.geometry.get_all_universes().values(): + if not isinstance(universe, openmc.DAGMCUniverse): + continue + for name in universe.material_names: + # if this name is already present in the model, skip it + # (this can happen if the same material name is used in multiple + # universes) + if name in all_material_names: + continue + # if the material name is not present on the model, + # create a placeholder material with the same name + # and add it to the model + mat_dag = openmc.Material(name=name) + mat_dag.add_nuclide('H1', 1.0) + model.materials.append(mat_dag) + all_material_names.add(name) return model.plot(*args, **kwargs) diff --git a/tests/unit_tests/dagmc/test_plot.py b/tests/unit_tests/dagmc/test_plot.py index afc2c395e..62022b258 100644 --- a/tests/unit_tests/dagmc/test_plot.py +++ b/tests/unit_tests/dagmc/test_plot.py @@ -48,14 +48,21 @@ def test_plotting_geometry_filled_with_dagmc_universe(request): dag_universe = openmc.DAGMCUniverse(request.path.parent / 'dagmc.h5m', auto_geom_ids=True) sphere1 = openmc.Sphere(r=50.0) - sphere2 = openmc.Sphere(r=60.0, boundary_type='vacuum') + sphere2 = openmc.Sphere(r=60.0) + sphere2 = openmc.Sphere(r=70.0, boundary_type='vacuum') - # Adding a material to the CSG Universe to check all materials are accounted for + # Adding a material to the CSG Universe to check universe materials are accounted for csg_material = openmc.Material(name='csg_material') csg_material.add_nuclide("H1", 1.0) + # Adding a material with the same name as a dagmc material to check that + # the plot can handel two materials with the same name from different universes + csg_material = openmc.Material(name=dag_universe.material_names[0]) + csg_material.add_nuclide("H1", 1.0) + cell1 = openmc.Cell(fill=dag_universe, region=-sphere1) cell2 = openmc.Cell(fill=csg_material, region=+sphere1 & -sphere2) geometry = openmc.Geometry([cell1, cell2]) + geometry.plot()