Adding checks to geometry.plot to avoid material name overlaps (#3458)

Co-authored-by: Patrick Shriwise <pshriwise@gmail.com>
This commit is contained in:
Jonathan Shimwell 2025-06-19 22:43:45 +02:00 committed by GitHub
parent 3e32aed2da
commit 3d16d4b100
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 10 deletions

View file

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

View file

@ -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()