Provide FissionYieldDistribution.restrict_products

This commit is contained in:
Andrew Johnson 2020-03-18 20:09:19 -04:00
parent 58b644cd3b
commit 2c2d9d7f31
No known key found for this signature in database
GPG key ID: 253418E91B7F6FEB
2 changed files with 46 additions and 1 deletions

View file

@ -13,7 +13,7 @@ try:
except ImportError:
import xml.etree.ElementTree as ET
from numpy import empty
from numpy import empty, searchsorted
from openmc.checkvalue import check_type
@ -467,6 +467,36 @@ class FissionYieldDistribution(Mapping):
data_elem = ET.SubElement(yield_element, "data")
data_elem.text = " ".join(map(str, yield_obj.yields))
def restrict_products(self, possible_products):
"""Return a new distribution with select products
Parameters
----------
possible_products : iterable of str
Candidate pool of fission products. Existing products
not contained here will not exist in the new instance
Returns
-------
FissionYieldDistribution or None
A value of None indicates no values in
``possible_products`` exist in :attr:`products`
"""
overlap = set(self.products).intersection(possible_products)
if not overlap:
return None
products = sorted(overlap)
indices = searchsorted(self.products, products)
# coerce back to dictionary to pass back to __init__
new_yields = {}
for ene, yields in zip(self.energies, self.yield_matrix.copy()):
new_yields[ene] = dict(zip(products, yields[indices]))
return type(self)(new_yields)
class FissionYield(Mapping):

View file

@ -176,6 +176,21 @@ def test_fission_yield_distribution():
with pytest.raises(TypeError):
orig_yields *= similar
# Test restriction of fission products
strict_restrict = yield_dist.restrict_products(["Xe135", "Sm149"])
with_extras = yield_dist.restrict_products(
["Xe135", "Sm149", "H1", "U235"])
assert strict_restrict.products == ("Sm149", "Xe135", )
assert strict_restrict.energies == yield_dist.energies
assert with_extras.products == ("Sm149", "Xe135", )
assert with_extras.energies == yield_dist.energies
for ene, new_yields in strict_restrict.items():
for product in strict_restrict.products:
assert new_yields[product] == yield_dist[ene][product]
assert with_extras[ene][product] == yield_dist[ene][product]
assert yield_dist.restrict_products(["U235"]) is None
def test_validate():