From 2afefba206769cee22acab1abfd860030c8834f5 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 8 Sep 2022 00:04:05 +0000 Subject: [PATCH 1/3] deepcopy method for FissionYield class --- openmc/deplete/nuclide.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/openmc/deplete/nuclide.py b/openmc/deplete/nuclide.py index 7b6ddd788..962e3e6d6 100644 --- a/openmc/deplete/nuclide.py +++ b/openmc/deplete/nuclide.py @@ -13,7 +13,7 @@ try: except ImportError: import xml.etree.ElementTree as ET -from numpy import empty, searchsorted +import numpy as np from openmc.checkvalue import check_type @@ -470,7 +470,7 @@ class FissionYieldDistribution(Mapping): shared_prod = set.union(*(set(x) for x in fission_yields.values())) ordered_prod = sorted(shared_prod) - yield_matrix = empty((len(energies), len(shared_prod))) + yield_matrix = np.empty((len(energies), len(shared_prod))) for g_index, energy in enumerate(energies): prod_map = fission_yields[energy] @@ -560,7 +560,7 @@ class FissionYieldDistribution(Mapping): return None products = sorted(overlap) - indices = searchsorted(self.products, products) + indices = np.searchsorted(self.products, products) # coerce back to dictionary to pass back to __init__ new_yields = {} @@ -681,6 +681,11 @@ class FissionYield(Mapping): return "<{} containing {} products and yields>".format( self.__class__.__name__, len(self)) + def __deepcopy__(self, memo): + result = FissionYield(self.products, np.copy(self.yields)) + memo[id(self)] = result + return result + # Avoid greedy numpy operations like np.float64 * fission_yield # converting this to an array on the fly. Force __rmul__ and # __radd__. See issue #1492 From 3c7483becbadeb5fc4323137ea30500f66f9e8c0 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 8 Sep 2022 02:08:18 +0000 Subject: [PATCH 2/3] Add a simple unittest for the FissionYield deepcopy --- tests/unit_tests/test_deplete_nuclide.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_deplete_nuclide.py b/tests/unit_tests/test_deplete_nuclide.py index 705beb32a..e73af6955 100644 --- a/tests/unit_tests/test_deplete_nuclide.py +++ b/tests/unit_tests/test_deplete_nuclide.py @@ -1,7 +1,7 @@ """Tests for the openmc.deplete.Nuclide class.""" import xml.etree.ElementTree as ET - +import copy import numpy as np import pytest from openmc.deplete import nuclide @@ -335,3 +335,14 @@ def test_validate(): assert "decay mode" in record[0].message.args[0] assert "0 reaction" in record[1].message.args[0] assert "1.0" in record[2].message.args[0] + + +def test_deepcopy(): + """Test deepcopying a FissionYield object""" + nuc = nuclide.FissionYield(products=("I129", "Sm149", "Xe135"), yields=np.array((0.001, 0.0003, 0.002))) + copied_nuc = copy.deepcopy(nuc) + # Check the deepcopy equals the original + assert copied_nuc == nuc + # Mutate the original and verify the copy remains intact + nuc *= 2 + assert copied_nuc != nuc \ No newline at end of file From 09de637b3a3795a5188b7ebec4d8095ec5ceba2d Mon Sep 17 00:00:00 2001 From: Josh May <85899979+joshmay1@users.noreply.github.com> Date: Thu, 8 Sep 2022 14:42:28 -0700 Subject: [PATCH 3/3] Change copy to match convention Co-authored-by: Paul Romano --- openmc/deplete/nuclide.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/deplete/nuclide.py b/openmc/deplete/nuclide.py index 962e3e6d6..04e0c26c5 100644 --- a/openmc/deplete/nuclide.py +++ b/openmc/deplete/nuclide.py @@ -682,7 +682,7 @@ class FissionYield(Mapping): self.__class__.__name__, len(self)) def __deepcopy__(self, memo): - result = FissionYield(self.products, np.copy(self.yields)) + result = FissionYield(self.products, self.yields.copy()) memo[id(self)] = result return result