diff --git a/openmc/geometry.py b/openmc/geometry.py index 379a2bb4e0..3b69333625 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -37,7 +37,6 @@ class Geometry: def __init__(self, root=None): self._root_universe = None self._offsets = {} - self._redundant_surface_map = None self.merge_surfaces = False self.surface_precision = 10 if root is not None: @@ -430,38 +429,6 @@ class Geometry: surfaces = cell.region.get_surfaces(surfaces) return surfaces - def get_redundant_surfaces(self): - """Return all of the topologically redundant surface IDs. - - Uses surface_precision attribute of Geometry instance for rounding and - comparing surface coefficients. - - .. versionadded:: 0.12 - - Returns - ------- - dict - Dictionary whose keys are the ID of a redundant surface and whose - values are the topologically equivalent :class:`openmc.Surface` - that should replace it. - - """ - # check if redundant surfaces have not been calculated yet - if self._redundant_surface_map is None: - redundancies = defaultdict(list) - for surf in self.get_all_surfaces().values(): - coeffs = tuple(round(surf._coefficients[k], - self.surface_precision) - for k in surf._coeff_keys) - key = (surf._type,) + coeffs - redundancies[key].append(surf) - - self._redundant_surface_map = {replace.id: keep - for keep, *redundant in redundancies.values() - for replace in redundant} - - return self._redundant_surface_map - def _get_domains_by_name(self, name, case_sensitive, matching, domain_type): if not case_sensitive: name = name.lower() @@ -609,10 +576,33 @@ class Geometry: return self._get_domains_by_name(name, case_sensitive, matching, 'lattice') def remove_redundant_surfaces(self): - """Remove redundant surfaces from the geometry.""" + """Remove and return all of the redundant surfaces. + Uses surface_precision attribute of Geometry instance for rounding and + comparing surface coefficients. + + .. versionadded:: 0.12 + + Returns + ------- + redundant_surfaces + Dictionary whose keys are the ID of a redundant surface and whose + values are the topologically equivalent :class:`openmc.Surface` + that should replace it. + + """ # Get redundant surfaces - redundant_surfaces = self.get_redundant_surfaces() + redundancies = defaultdict(list) + for surf in self.get_all_surfaces().values(): + coeffs = tuple(round(surf._coefficients[k], + self.surface_precision) + for k in surf._coeff_keys) + key = (surf._type,) + coeffs + redundancies[key].append(surf) + + redundant_surfaces = {replace.id: keep + for keep, *redundant in redundancies.values() + for replace in redundant} if redundant_surfaces: # Iterate through all cells contained in the geometry @@ -621,7 +611,7 @@ class Geometry: if cell.region: cell.region.remove_redundant_surfaces(redundant_surfaces) - self._redundant_surface_map = None + return redundant_surfaces def determine_paths(self, instances_only=False): """Determine paths through CSG tree for cells and materials. diff --git a/openmc/model/model.py b/openmc/model/model.py index 7126987cbf..98136b2d59 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -422,6 +422,9 @@ class Model: warnings.warn("remove_surfs kwarg will be deprecated soon, please " "set the Geometry.merge_surfaces attribute instead.") self.geometry.merge_surfaces = True + # Can be used to modify tallies in case any surfaces are redundant + redundant_surfaces = self.geometry.remove_redundant_surfaces() + self.geometry.export_to_xml(d) # If a materials collection was specified, export it. Otherwise, look diff --git a/tests/unit_tests/test_geometry.py b/tests/unit_tests/test_geometry.py index 6a092126ed..9db112fd29 100644 --- a/tests/unit_tests/test_geometry.py +++ b/tests/unit_tests/test_geometry.py @@ -302,7 +302,7 @@ def test_rotation_matrix(): assert geom.find((0.0, -0.5, 0.0))[-1] == c1 assert geom.find((0.0, -1.5, 0.0))[-1] == c2 -def test_remove_redundant_surfaces(run_in_tmpdir): +def test_remove_redundant_surfaces(): """Test ability to remove redundant surfaces""" m1 = openmc.Material() @@ -344,11 +344,8 @@ def test_remove_redundant_surfaces(run_in_tmpdir): materials=openmc.Materials([m1, m2, m3])) # There should be 6 redundant surfaces in this geometry - n_redundant_surfs = len(geom.get_redundant_surfaces().keys()) + n_redundant_surfs = len(geom.remove_redundant_surfaces().keys()) assert n_redundant_surfs == 6 - # Remove redundant surfaces on export - model.export_to_xml() - geom = openmc.Geometry.from_xml() # There should be 0 remaining redundant surfaces - n_redundant_surfs = len(geom.get_redundant_surfaces().keys()) + n_redundant_surfs = len(geom.remove_redundant_surfaces().keys()) assert n_redundant_surfs == 0