diff --git a/openmc/model/model.py b/openmc/model/model.py index d927b65ae64..ec2e81fbf60 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -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 @@ -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": diff --git a/tests/unit_tests/test_model.py b/tests/unit_tests/test_model.py index 9234b2d2721..c6b797d058e 100644 --- a/tests/unit_tests/test_model.py +++ b/tests/unit_tests/test_model.py @@ -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")