mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-26 13:15:39 -04:00
Merge remote-tracking branch 'upstream/develop' into pyapi_lattice_outer
Conflicts: openmc/filter.py
This commit is contained in:
commit
8a31f2f242
3 changed files with 1202 additions and 83 deletions
340
openmc/cross.py
Normal file
340
openmc/cross.py
Normal file
|
|
@ -0,0 +1,340 @@
|
|||
from openmc import Filter, Nuclide
|
||||
|
||||
|
||||
class CrossScore(object):
|
||||
"""A special-purpose tally score used to encapsulate all combinations of two
|
||||
tally's scores as a outer product for tally arithmetic.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
left_score : str or CrossScore
|
||||
The left score in the outer product
|
||||
right_score : str or CrossScore
|
||||
The right score in the outer product
|
||||
binary_op : str
|
||||
The tally arithmetic binary operator (e.g., '+', '-', etc.) used to
|
||||
combine two tally's scores with this CrossNuclide
|
||||
|
||||
Attributes
|
||||
----------
|
||||
left_score : str or CrossScore
|
||||
The left score in the outer product
|
||||
right_score : str or CrossScore
|
||||
The right score in the outer product
|
||||
binary_op : str
|
||||
The tally arithmetic binary operator (e.g., '+', '-', etc.) used to
|
||||
combine two tally's scores with this CrossNuclide
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, left_score=None, right_score=None, binary_op=None):
|
||||
|
||||
self._left_score = None
|
||||
self._right_score = None
|
||||
self._binary_op = None
|
||||
|
||||
if left_score is not None:
|
||||
self.left_score = left_score
|
||||
if right_score is not None:
|
||||
self.right_score = right_score
|
||||
if binary_op is not None:
|
||||
self.binary_op = binary_op
|
||||
|
||||
@property
|
||||
def left_score(self):
|
||||
return self._left_score
|
||||
|
||||
@property
|
||||
def right_score(self):
|
||||
return self._right_score
|
||||
|
||||
@property
|
||||
def binary_op(self):
|
||||
return self._binary_op
|
||||
|
||||
@left_score.setter
|
||||
def left_score(self, left_score):
|
||||
self._left_score = left_score
|
||||
|
||||
@right_score.setter
|
||||
def right_score(self, right_score):
|
||||
self._right_score = right_score
|
||||
|
||||
@binary_op.setter
|
||||
def binary_op(self, binary_op):
|
||||
self._binary_op = binary_op
|
||||
|
||||
def __eq__(self, other):
|
||||
return str(other) == str(self)
|
||||
|
||||
def __repr__(self):
|
||||
string = '({0} {1} {2})'.format(self.left_score,
|
||||
self.binary_op, self.right_score)
|
||||
return string
|
||||
|
||||
|
||||
class CrossNuclide(object):
|
||||
"""A special-purpose nuclide used to encapsulate all combinations of two
|
||||
tally's nuclides as a outer product for tally arithmetic.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
left_nuclide : Nuclide or CrossNuclide
|
||||
The left nuclide in the outer product
|
||||
right_nuclide : Nuclide or CrossNuclide
|
||||
The right nuclide in the outer product
|
||||
binary_op : str
|
||||
The tally arithmetic binary operator (e.g., '+', '-', etc.) used to
|
||||
combine two tally's nuclides with this CrossNuclide
|
||||
|
||||
Attributes
|
||||
----------
|
||||
left_nuclide : Nuclide or CrossNuclide
|
||||
The left nuclide in the outer product
|
||||
right_nuclide : Nuclide or CrossNuclide
|
||||
The right nuclide in the outer product
|
||||
binary_op : str
|
||||
The tally arithmetic binary operator (e.g., '+', '-', etc.) used to
|
||||
combine two tally's nuclides with this CrossNuclide
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, left_nuclide=None, right_nuclide=None, binary_op=None):
|
||||
|
||||
self._left_nuclide = None
|
||||
self._right_nuclide = None
|
||||
self._binary_op = None
|
||||
|
||||
if left_nuclide is not None:
|
||||
self.left_nuclide = left_nuclide
|
||||
if right_nuclide is not None:
|
||||
self.right_nuclide = right_nuclide
|
||||
if binary_op is not None:
|
||||
self.binary_op = binary_op
|
||||
|
||||
@property
|
||||
def left_nuclide(self):
|
||||
return self._left_nuclide
|
||||
|
||||
@property
|
||||
def right_nuclide(self):
|
||||
return self._right_nuclide
|
||||
|
||||
@property
|
||||
def binary_op(self):
|
||||
return self._binary_op
|
||||
|
||||
@left_nuclide.setter
|
||||
def left_nuclide(self, left_nuclide):
|
||||
self._left_nuclide = left_nuclide
|
||||
|
||||
@right_nuclide.setter
|
||||
def right_nuclide(self, right_nuclide):
|
||||
self._right_nuclide = right_nuclide
|
||||
|
||||
@binary_op.setter
|
||||
def binary_op(self, binary_op):
|
||||
self._binary_op = binary_op
|
||||
|
||||
def __eq__(self, other):
|
||||
return str(other) == str(self)
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
string = ''
|
||||
|
||||
# If the Summary was linked, the left nuclide is a Nuclide object
|
||||
if isinstance(self.left_nuclide, Nuclide):
|
||||
string += '(' + self.left_nuclide.name
|
||||
# If the Summary was not linked, the left nuclide is the ZAID
|
||||
else:
|
||||
string += '(' + str(self.left_nuclide)
|
||||
|
||||
string += ' ' + self.binary_op + ' '
|
||||
|
||||
# If the Summary was linked, the right nuclide is a Nuclide object
|
||||
if isinstance(self.right_nuclide, Nuclide):
|
||||
string += self.right_nuclide.name + ')'
|
||||
# If the Summary was not linked, the right nuclide is the ZAID
|
||||
else:
|
||||
string += str(self.right_nuclide) + ')'
|
||||
|
||||
return string
|
||||
|
||||
|
||||
class CrossFilter(object):
|
||||
"""A special-purpose filter used to encapsulate all combinations of two
|
||||
tally's filter bins as a outer product for tally arithmetic.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
left_filter : Filter or CrossFilter
|
||||
The left filter in the outer product
|
||||
right_filter : Filter or CrossFilter
|
||||
The right filter in the outer product
|
||||
binary_op : str
|
||||
The tally arithmetic binary operator (e.g., '+', '-', etc.) used to
|
||||
combine two tally's filter bins with this CrossFilter
|
||||
|
||||
Attributes
|
||||
----------
|
||||
left_filter : Filter or CrossFilter
|
||||
The left filter in the outer product
|
||||
right_filter : Filter or CrossFilter
|
||||
The right filter in the outer product
|
||||
binary_op : str
|
||||
The tally arithmetic binary operator (e.g., '+', '-', etc.) used to
|
||||
combine two tally's filter bins with this CrossFilter
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, left_filter=None, right_filter=None, binary_op=None):
|
||||
|
||||
left_type = left_filter.type
|
||||
right_type = right_filter.type
|
||||
self.type = '({0} {1} {2})'.format(left_type, binary_op, right_type)
|
||||
|
||||
self._bins = {}
|
||||
self._bins['left'] = left_filter.bins
|
||||
self._bins['right'] = right_filter.bins
|
||||
self._num_bins = left_filter.num_bins * right_filter.num_bins
|
||||
|
||||
self._left_filter = None
|
||||
self._right_filter = None
|
||||
self._binary_op = None
|
||||
|
||||
if left_filter is not None:
|
||||
self.left_filter = left_filter
|
||||
if right_filter is not None:
|
||||
self.right_filter = right_filter
|
||||
if binary_op is not None:
|
||||
self.binary_op = binary_op
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.type, self.bins))
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
existing = memo.get(id(self))
|
||||
|
||||
# If this is the first time we have tried to copy this object, create a copy
|
||||
if existing is None:
|
||||
clone = type(self).__new__(type(self))
|
||||
clone._left_filter = self.left_filter
|
||||
clone._right_filter = self.right_filter
|
||||
clone._type = self.type
|
||||
clone._bins = self.bins
|
||||
clone._num_bins = self.num_bins
|
||||
memo[id(self)] = clone
|
||||
|
||||
return clone
|
||||
|
||||
# If this object has been copied before, return the first copy made
|
||||
else:
|
||||
return existing
|
||||
|
||||
@property
|
||||
def left_filter(self):
|
||||
return self._left_filter
|
||||
|
||||
@property
|
||||
def right_filter(self):
|
||||
return self._right_filter
|
||||
|
||||
@property
|
||||
def binary_op(self):
|
||||
return self._binary_op
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return self._type
|
||||
|
||||
@property
|
||||
def bins(self):
|
||||
return (self._bins['left'], self._bins['right'])
|
||||
|
||||
@property
|
||||
def num_bins(self):
|
||||
return self._num_bins
|
||||
|
||||
@property
|
||||
def stride(self):
|
||||
return self.left_filter.stride * self.right_filter.stride
|
||||
|
||||
@type.setter
|
||||
def type(self, filter_type):
|
||||
self._type = filter_type
|
||||
|
||||
@left_filter.setter
|
||||
def left_filter(self, left_filter):
|
||||
self._left_filter = left_filter
|
||||
|
||||
@right_filter.setter
|
||||
def right_filter(self, right_filter):
|
||||
self._right_filter = right_filter
|
||||
|
||||
@binary_op.setter
|
||||
def binary_op(self, binary_op):
|
||||
self._binary_op = binary_op
|
||||
|
||||
def __eq__(self, other):
|
||||
return str(other) == str(self)
|
||||
|
||||
def split_filters(self):
|
||||
|
||||
split_filters = []
|
||||
|
||||
# If left Filter is not a CrossFilter, simply append to list
|
||||
if isinstance(self.left_filter, Filter):
|
||||
split_filters.append(self.left_filter)
|
||||
# Recursively descend CrossFilter tree to collect all Filters
|
||||
else:
|
||||
split_filters.extend(self.left_filter.split_filters())
|
||||
|
||||
# If right Filter is not a CrossFilter, simply append to list
|
||||
if isinstance(self.right_filter, Filter):
|
||||
split_filters.append(self.right_filter)
|
||||
# Recursively descend CrossFilter tree to collect all Filters
|
||||
else:
|
||||
split_filters.extend(self.right_filter.split_filters())
|
||||
|
||||
return split_filters
|
||||
|
||||
def get_bin_index(self, filter_bin):
|
||||
"""Returns the index in the CrossFilter for some bin.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
filter_bin : 2-tuple
|
||||
A 2-tuple where each value corresponds to the bin of interest
|
||||
in the left and right filter, respectively. A bin is the integer
|
||||
ID for 'material', 'surface', 'cell', 'cellborn', and 'universe'
|
||||
Filters. The bin is an integer for the cell instance ID for
|
||||
'distribcell' Filters. The bin is a 2-tuple of floats for 'energy'
|
||||
and 'energyout' filters corresponding to the energy boundaries of
|
||||
the bin of interest. The bin is a (x,y,z) 3-tuple for 'mesh'
|
||||
filters corresponding to the mesh cell of interest.
|
||||
|
||||
Returns
|
||||
-------
|
||||
filter_index : int
|
||||
The index in the Tally data array for this filter bin.
|
||||
|
||||
"""
|
||||
|
||||
left_index = self.left_filter.get_bin_index(filter_bin[0])
|
||||
right_index = self.right_filter.get_bin_index(filter_bin[0])
|
||||
filter_index = left_index * self.right_filter.num_bins + right_index
|
||||
return filter_index
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
string = 'CrossFilter\n'
|
||||
filter_type = '({0} {1} {2})'.format(self.left_filter.type,
|
||||
self.binary_op,
|
||||
self.right_filter.type)
|
||||
filter_bins = '({0} {1} {2})'.format(self.left_filter.bins,
|
||||
self.binary_op,
|
||||
self.right_filter.bins)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tType', '=\t', filter_type)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tBins', '=\t', filter_bins)
|
||||
return string
|
||||
|
|
@ -43,15 +43,15 @@ class Filter(object):
|
|||
|
||||
def __eq__(self, filter2):
|
||||
# Check type
|
||||
if self._type != filter2._type:
|
||||
if self.type != filter2.type:
|
||||
return False
|
||||
|
||||
# Check number of bins
|
||||
elif len(self._bins) != len(filter2._bins):
|
||||
elif len(self.bins) != len(filter2.bins):
|
||||
return False
|
||||
|
||||
# Check bin edges
|
||||
elif not np.allclose(self._bins, filter2._bins):
|
||||
elif not np.allclose(self.bins, filter2.bins):
|
||||
return False
|
||||
|
||||
else:
|
||||
|
|
@ -66,12 +66,12 @@ class Filter(object):
|
|||
# If this is the first time we have tried to copy this object, create a copy
|
||||
if existing is None:
|
||||
clone = type(self).__new__(type(self))
|
||||
clone._type = self._type
|
||||
clone._bins = copy.deepcopy(self._bins, memo)
|
||||
clone._num_bins = self._num_bins
|
||||
clone._mesh = copy.deepcopy(self._mesh, memo)
|
||||
clone._offset = self._offset
|
||||
clone._stride = self._stride
|
||||
clone._type = self.type
|
||||
clone._bins = copy.deepcopy(self.bins, memo)
|
||||
clone._num_bins = self.num_bins
|
||||
clone._mesh = copy.deepcopy(self.mesh, memo)
|
||||
clone._offset = self.offset
|
||||
clone._stride = self.stride
|
||||
|
||||
memo[id(self)] = clone
|
||||
|
||||
|
|
@ -133,7 +133,7 @@ class Filter(object):
|
|||
else:
|
||||
bins = list(bins)
|
||||
|
||||
if self._type in ['cell', 'cellborn', 'surface', 'material',
|
||||
if self.type in ['cell', 'cellborn', 'surface', 'material',
|
||||
'universe', 'distribcell']:
|
||||
check_iterable_type('filter bins', bins, Integral)
|
||||
for edge in bins:
|
||||
|
|
@ -144,11 +144,11 @@ class Filter(object):
|
|||
if not isinstance(edge, Real):
|
||||
msg = 'Unable to add bin edge "{0}" to a "{1}" Filter ' \
|
||||
'since it is a non-integer or floating point ' \
|
||||
'value'.format(edge, self._type)
|
||||
'value'.format(edge, self.type)
|
||||
raise ValueError(msg)
|
||||
elif edge < 0.:
|
||||
msg = 'Unable to add bin edge "{0}" to a "{1}" Filter ' \
|
||||
'since it is a negative value'.format(edge, self._type)
|
||||
'since it is a negative value'.format(edge, self.type)
|
||||
raise ValueError(msg)
|
||||
|
||||
# Check that bin edges are monotonically increasing
|
||||
|
|
@ -156,7 +156,7 @@ class Filter(object):
|
|||
if index > 0 and bins[index] < bins[index-1]:
|
||||
msg = 'Unable to add bin edges "{0}" to a "{1}" Filter ' \
|
||||
'since they are not monotonically ' \
|
||||
'increasing'.format(bins, self._type)
|
||||
'increasing'.format(bins, self.type)
|
||||
raise ValueError(msg)
|
||||
|
||||
# mesh filters
|
||||
|
|
@ -175,7 +175,7 @@ class Filter(object):
|
|||
raise ValueError(msg)
|
||||
|
||||
# If all error checks passed, add bin edges
|
||||
self._bins = bins
|
||||
self._bins = np.array(bins)
|
||||
|
||||
# FIXME
|
||||
@num_bins.setter
|
||||
|
|
@ -190,7 +190,7 @@ class Filter(object):
|
|||
|
||||
self._mesh = mesh
|
||||
self.type = 'mesh'
|
||||
self.bins = self._mesh._id
|
||||
self.bins = self.mesh.id
|
||||
|
||||
@offset.setter
|
||||
def offset(self, offset):
|
||||
|
|
@ -201,8 +201,8 @@ class Filter(object):
|
|||
def stride(self, stride):
|
||||
check_type('filter stride', stride, Integral)
|
||||
if stride < 0:
|
||||
msg = 'Unable to set stride "{0}" for a "{1}" Filter since it is a ' \
|
||||
'negative value'.format(stride, self._type)
|
||||
msg = 'Unable to set stride "{0}" for a "{1}" Filter since it ' \
|
||||
'is a negative value'.format(stride, self.type)
|
||||
raise ValueError(msg)
|
||||
|
||||
self._stride = stride
|
||||
|
|
@ -260,14 +260,15 @@ class Filter(object):
|
|||
"""
|
||||
|
||||
if not self.can_merge(filter):
|
||||
msg = 'Unable to merge "{0}" with "{1}" filters'.format(self._type, filter._type)
|
||||
msg = 'Unable to merge "{0}" with "{1}" ' \
|
||||
'filters'.format(self.type, filter.type)
|
||||
raise ValueError(msg)
|
||||
|
||||
# Create deep copy of filter to return as merged filter
|
||||
merged_filter = copy.deepcopy(self)
|
||||
|
||||
# Merge unique filter bins
|
||||
merged_bins = list(set(self._bins + filter._bins))
|
||||
merged_bins = list(set(self.bins + filter.bins))
|
||||
merged_filter.bins = merged_bins
|
||||
merged_filter.num_bins = len(merged_bins)
|
||||
|
||||
|
|
@ -313,17 +314,17 @@ class Filter(object):
|
|||
|
||||
# Use lower energy bound to find index for energy Filters
|
||||
elif self.type in ['energy', 'energyout']:
|
||||
val = self.bins.index(filter_bin[0])
|
||||
val = np.where(self.bins == filter_bin[0])[0][0]
|
||||
filter_index = val
|
||||
|
||||
# Filter bins for distribcell are the "IDs" of each unique placement
|
||||
# of the Cell in the Geometry (integers starting at 0)
|
||||
elif self._type == 'distribcell':
|
||||
elif self.type == 'distribcell':
|
||||
filter_index = filter_bin
|
||||
|
||||
# Use ID for all other Filters (e.g., material, cell, etc.)
|
||||
else:
|
||||
val = self.bins.index(filter_bin)
|
||||
val = np.where(self.bins == filter_bin)[0][0]
|
||||
filter_index = val
|
||||
|
||||
except ValueError:
|
||||
|
|
@ -335,7 +336,7 @@ class Filter(object):
|
|||
|
||||
def __repr__(self):
|
||||
string = 'Filter\n'
|
||||
string += '{0: <16}{1}{2}\n'.format('\tType', '=\t', self._type)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tBins', '=\t', self._bins)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tOffset', '=\t', self._offset)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tType', '=\t', self.type)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tBins', '=\t', self.bins)
|
||||
string += '{0: <16}{1}{2}\n'.format('\tOffset', '=\t', self.offset)
|
||||
return string
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue