Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion openmc/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2704,7 +2704,8 @@ def convert_to_multigroup(
Parameters
----------
method : {"material_wise", "stochastic_slab", "infinite_medium"}, optional
Method to generate the MGXS.
Method to generate the MGXS. One cross section is produced per
material (keyed by material name).
groups : openmc.mgxs.EnergyGroups, str, or sequence of float, optional
Energy group structure for the MGXS. Can be an
:class:`openmc.mgxs.EnergyGroups` object, a string name of a
Expand Down Expand Up @@ -2779,6 +2780,20 @@ def convert_to_multigroup(
material.name = f"material {material.id}"
material.name = re.sub(r'[^a-zA-Z0-9]', '_', material.name)

# The MGXS library keys cross section data by material name, so
# distinct materials that share a name cannot be written separately.
name_to_ids = {}
for material in self.materials:
name_to_ids.setdefault(material.name, set()).add(material.id)
shared = sorted(
name for name, ids in name_to_ids.items() if len(ids) > 1
)
if shared:
raise ValueError(
"Each material must have a unique name to be written to the "
f"MGXS library, but these names are shared: {shared}."
)

# If needed, generate the needed MGXS data library file
if not Path(mgxs_path).is_file() or overwrite_mgxs_library:
if method == "infinite_medium":
Expand Down
22 changes: 22 additions & 0 deletions tests/unit_tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,3 +1038,25 @@ def test_id_map_to_rgb():
)
# Check that overlap region is green
assert np.allclose(rgb_overlap[5:, 5:], [0.0, 1.0, 0.0])


def test_convert_to_multigroup_raises_on_duplicate_material_names(run_in_tmpdir):
"""Distinct materials that share a name must raise rather than silently
collapse to a single cross section in the MGXS library."""
steel_a = openmc.Material(name="steel")
steel_a.add_element("Fe", 1.0)
steel_a.set_density("g/cm3", 7.9)
steel_b = openmc.Material(name="steel")
steel_b.add_element("Fe", 1.0)
steel_b.set_density("g/cm3", 7.9)

s1 = openmc.Sphere(r=1.0)
s2 = openmc.Sphere(r=2.0, boundary_type="vacuum")
c1 = openmc.Cell(fill=steel_a, region=-s1)
c2 = openmc.Cell(fill=steel_b, region=+s1 & -s2)
model = openmc.Model(
openmc.Geometry([c1, c2]), openmc.Materials([steel_a, steel_b])
)

with pytest.raises(ValueError, match="unique name"):
model.convert_to_multigroup(method="material_wise")
Loading