mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-27 13:45:36 -04:00
Merge pull request #1493 from drewejohnson/numpy-mul-fy
Disallow numpy ufuncs being applied directly to FissionYields
This commit is contained in:
commit
630be279f2
2 changed files with 15 additions and 1 deletions
|
|
@ -533,6 +533,7 @@ class FissionYield(Mapping):
|
|||
return zip(self.products, self.yields)
|
||||
|
||||
def __add__(self, other):
|
||||
"""Add one set of fission yields to this set, return new yields"""
|
||||
if not isinstance(other, FissionYield):
|
||||
return NotImplemented
|
||||
new = FissionYield(self.products, self.yields.copy())
|
||||
|
|
@ -550,12 +551,14 @@ class FissionYield(Mapping):
|
|||
return self + other
|
||||
|
||||
def __imul__(self, scalar):
|
||||
"""Scale these fission yields by a real scalar"""
|
||||
if not isinstance(scalar, Real):
|
||||
return NotImplemented
|
||||
self.yields *= scalar
|
||||
return self
|
||||
|
||||
def __mul__(self, scalar):
|
||||
"""Return a new set of yields scaled by a real scalar"""
|
||||
if not isinstance(scalar, Real):
|
||||
return NotImplemented
|
||||
new = FissionYield(self.products, self.yields.copy())
|
||||
|
|
@ -568,3 +571,8 @@ class FissionYield(Mapping):
|
|||
def __repr__(self):
|
||||
return "<{} containing {} products and yields>".format(
|
||||
self.__class__.__name__, len(self))
|
||||
|
||||
# Avoid greedy numpy operations like np.float64 * fission_yield
|
||||
# converting this to an array on the fly. Force __rmul__ and
|
||||
# __radd__. See issue #1492
|
||||
__array_ufunc__ = None
|
||||
|
|
|
|||
|
|
@ -149,12 +149,18 @@ def test_fission_yield_distribution():
|
|||
# __getitem__ return yields as a view into yield matrix
|
||||
assert orig_yields.yields.base is yield_dist.yield_matrix
|
||||
|
||||
# Fission yield feature uses scaled and incremented
|
||||
# Scale and increment fission yields
|
||||
mod_yields = orig_yields * 2
|
||||
assert numpy.array_equal(orig_yields.yields * 2, mod_yields.yields)
|
||||
mod_yields += orig_yields
|
||||
assert numpy.array_equal(orig_yields.yields * 3, mod_yields.yields)
|
||||
|
||||
mod_yields = 2.0 * orig_yields
|
||||
assert numpy.array_equal(orig_yields.yields * 2, mod_yields.yields)
|
||||
|
||||
mod_yields = numpy.float64(2.0) * orig_yields
|
||||
assert numpy.array_equal(orig_yields.yields * 2, mod_yields.yields)
|
||||
|
||||
# Failure modes for adding, multiplying yields
|
||||
similar = numpy.empty_like(orig_yields.yields)
|
||||
with pytest.raises(TypeError):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue