From d9c8e594c76f7f99b6acf5027de93ccfd26173e9 Mon Sep 17 00:00:00 2001 From: Skywalker <49872736+cn-skywalker@users.noreply.github.com> Date: Wed, 29 Jan 2025 03:44:04 +0800 Subject: [PATCH] fix the bug in function differentiate_mats() (#3277) Co-authored-by: Paul Romano --- openmc/model/model.py | 27 ++++++++++++++----- .../test_deplete_coupled_operator.py | 4 +-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index 80859c55db..c8329c2fbb 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -1199,7 +1199,7 @@ class Model: diff_volume_method : str Specifies how the volumes of the new materials should be found. - None: Do not assign volumes to the new materials (Default) - - 'divide_equally': Divide the original material volume equally between the new materials + - 'divide equally': Divide the original material volume equally between the new materials - 'match cell': Set the volume of the material to the volume of the cell they fill """ self.differentiate_mats(diff_volume_method, depletable_only=True) @@ -1214,7 +1214,7 @@ class Model: diff_volume_method : str Specifies how the volumes of the new materials should be found. - None: Do not assign volumes to the new materials (Default) - - 'divide_equally': Divide the original material volume equally between the new materials + - 'divide equally': Divide the original material volume equally between the new materials - 'match cell': Set the volume of the material to the volume of the cell they fill depletable_only : bool Default is True, only depletable materials will be differentiated. If False, all materials will be @@ -1225,9 +1225,15 @@ class Model: # Count the number of instances for each cell and material self.geometry.determine_paths(instances_only=True) + # Get list of materials + if self.materials: + materials = self.materials + else: + materials = list(self.geometry.get_all_materials().values()) + # Find all or depletable_only materials which have multiple instance distribmats = set() - for mat in self.materials: + for mat in materials: # Differentiate all materials with multiple instances diff_mat = mat.num_instances > 1 # If depletable_only is True, differentiate only depletable materials @@ -1259,11 +1265,20 @@ class Model: for cell in self.geometry.get_all_material_cells().values(): if cell.fill in distribmats: mat = cell.fill - if diff_volume_method != 'match cell': + + # Clone materials + if cell.num_instances > 1: cell.fill = [mat.clone() for _ in range(cell.num_instances)] - elif diff_volume_method == 'match cell': + else: cell.fill = mat.clone() - cell.fill.volume = cell.volume + + # For 'match cell', assign volumes based on the cells + if diff_volume_method == 'match cell': + if cell.fill_type == 'distribmat': + for clone_mat in cell.fill: + clone_mat.volume = cell.volume + else: + cell.fill.volume = cell.volume if self.materials is not None: self.materials = openmc.Materials( diff --git a/tests/unit_tests/test_deplete_coupled_operator.py b/tests/unit_tests/test_deplete_coupled_operator.py index fe79d621b1..ba291e07dd 100644 --- a/tests/unit_tests/test_deplete_coupled_operator.py +++ b/tests/unit_tests/test_deplete_coupled_operator.py @@ -114,7 +114,7 @@ def test_diff_volume_method_divide_equally(model_with_volumes): ) all_cells = list(operator.model.geometry.get_all_cells().values()) - assert all_cells[0].fill[0].volume == 51 - assert all_cells[1].fill[0].volume == 51 + assert all_cells[0].fill.volume == 51 + assert all_cells[1].fill.volume == 51 # mat2 is not depletable assert all_cells[2].fill.volume is None