From 6d4d25acb2a53df609df76eb0a79ebb5e77b4804 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 13 Aug 2019 15:16:41 -0500 Subject: [PATCH] Implement add, imul, rmul for openmc.deplete.FissionYield Addition methods __add__ and __iadd__ expect the other argument to a FissionYield object and will add the yields from one to the other [in place for __iadd__]. Multiplication methods __imul__, __mul__, and __rmul__ expect the other argument to be a scalar and will scale the fission yields by this value [in place for __imul__] Also added a __repr__ method that returns a dictionary-like representation --- openmc/deplete/nuclide.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/openmc/deplete/nuclide.py b/openmc/deplete/nuclide.py index 86a10d429..615761cde 100644 --- a/openmc/deplete/nuclide.py +++ b/openmc/deplete/nuclide.py @@ -367,6 +367,8 @@ class FissionYield(Mapping): :class:`FissionYieldDistribution`, and allowing math operations on a single vector of yields. Can in turn be used like a dictionary to fetch fission yields. + Supports multiplication of a scalar to scale the fission + yields and addition of another set of yields. Does not support resizing / inserting new products that do not exist. @@ -401,6 +403,8 @@ class FissionYield(Mapping): 0.002 >>> (new + fy_vector)["Sm149"] 0.0009 + >>> dict(new) + {"Xe135": 0.002, "I129": 0.001, "Sm149": 0.0003} """ def __init__(self, products, yields): @@ -418,13 +422,32 @@ class FissionYield(Mapping): def __iter__(self): return iter(self.products) + def __add__(self, other): + new = self.copy() + new += other + return new + def __iadd__(self, other): """Increment value from other fission yield""" self.yields += other.yields return self - def __mul__(self, value): - return FissionYield(self.products, self.yields * value) + def __imul__(self, scalar): + self.yields *= scalar + return self + + def __mul__(self, scalar): + new = self.copy() + new *= scalar + return new + + def __rmul__(self, scalar): + new = self.copy() + new *= scalar + return new + + def __repr__(self): + return "<{}: {}>".format(self.__class__.__name__, repr(dict(self))) def copy(self): """Return an identical yield object, with unique yields"""