Skip to content

Commit

Permalink
Remove _smart_set_bins in favor of overriding Filter.bins.setter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Dec 20, 2017
1 parent 2ef1c23 commit 55f7260
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 73 deletions.
78 changes: 21 additions & 57 deletions openmc/filter.py
Expand Up @@ -14,7 +14,10 @@

import openmc
import openmc.checkvalue as cv
from .cell import Cell
from .material import Material
from .mixin import IDManagerMixin
from .universe import Universe


_FILTER_TYPES = ['universe', 'material', 'cell', 'cellborn', 'surface',
Expand Down Expand Up @@ -170,7 +173,7 @@ def from_hdf5(cls, group, **kwargs):
# If the HDF5 'type' variable matches this class's short_name, then
# there is no overriden from_hdf5 method. Pass the bins to __init__.
if group['type'].value.decode() == cls.short_name.lower():
out = cls(group['bins'].value, filter_id)
out = cls(group['bins'].value, filter_id=filter_id)
out._num_bins = group['n_bins'].value
return out

Expand Down Expand Up @@ -425,12 +428,15 @@ def get_pandas_dataframe(self, data_size, stride, **kwargs):

class WithIDFilter(Filter):
"""Abstract parent for filters of types with ids (Cell, Material, etc.)."""
def _smart_set_bins(self, bins, bin_type):

@Filter.bins.setter
def bins(self, bins):
# Format the bins as a 1D numpy array.
bins = np.atleast_1d(bins)

# Check the bin values.
cv.check_iterable_type('filter bins', bins, (Integral, bin_type))
cv.check_iterable_type('filter bins', bins,
(Integral, self.expected_type))
for edge in bins:
if isinstance(edge, Integral):
cv.check_greater_than('filter bin', edge, 0, equality=True)
Expand Down Expand Up @@ -463,13 +469,7 @@ class UniverseFilter(WithIDFilter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
def bins(self, bins):
self._smart_set_bins(bins, openmc.Universe)
expected_type = Universe


class MaterialFilter(WithIDFilter):
Expand All @@ -493,13 +493,7 @@ class MaterialFilter(WithIDFilter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
def bins(self, bins):
self._smart_set_bins(bins, openmc.Material)
expected_type = Material


class CellFilter(WithIDFilter):
Expand All @@ -523,13 +517,7 @@ class CellFilter(WithIDFilter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
def bins(self, bins):
self._smart_set_bins(bins, openmc.Cell)
expected_type = Cell


class CellFromFilter(WithIDFilter):
Expand All @@ -553,13 +541,7 @@ class CellFromFilter(WithIDFilter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
def bins(self, bins):
self._smart_set_bins(bins, openmc.Cell)
expected_type = Cell


class CellbornFilter(WithIDFilter):
Expand All @@ -583,13 +565,7 @@ class CellbornFilter(WithIDFilter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
def bins(self, bins):
self._smart_set_bins(bins, openmc.Cell)
expected_type = Cell


class SurfaceFilter(Filter):
Expand All @@ -614,11 +590,7 @@ class SurfaceFilter(Filter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
@Filter.bins.setter
def bins(self, bins):
# Format the bins as a 1D numpy array.
bins = np.atleast_1d(bins)
Expand Down Expand Up @@ -720,7 +692,7 @@ def from_hdf5(cls, group, **kwargs):
mesh_obj = kwargs['meshes'][mesh_id]
filter_id = int(group.name.split('/')[-1].lstrip('filter '))

out = cls(mesh_obj, filter_id)
out = cls(mesh_obj, filter_id=filter_id)
out._num_bins = group['n_bins'].value

return out
Expand Down Expand Up @@ -1169,15 +1141,11 @@ def from_hdf5(cls, group, **kwargs):

filter_id = int(group.name.split('/')[-1].lstrip('filter '))

out = cls(group['bins'].value, filter_id)
out = cls(group['bins'].value, filter_id=filter_id)
out._num_bins = group['n_bins'].value

return out

@property
def bins(self):
return self._bins

@property
def num_bins(self):
# Need to handle number of bins carefully -- for distribcell tallies, we
Expand All @@ -1188,7 +1156,7 @@ def num_bins(self):
def paths(self):
return self._paths

@bins.setter
@Filter.bins.setter
def bins(self, bins):
# Format the bins as a 1D numpy array.
bins = np.atleast_1d(bins)
Expand Down Expand Up @@ -1690,11 +1658,7 @@ class DelayedGroupFilter(Filter):
The number of filter bins
"""
@property
def bins(self):
return self._bins

@bins.setter
@Filter.bins.setter
def bins(self, bins):
# Format the bins as a 1D numpy array.
bins = np.atleast_1d(bins)
Expand Down Expand Up @@ -1799,7 +1763,7 @@ def from_hdf5(cls, group, **kwargs):
y = group['y'].value
filter_id = int(group.name.split('/')[-1].lstrip('filter '))

return cls(energy, y, filter_id)
return cls(energy, y, filter_id=filter_id)

@classmethod
def from_tabulated1d(cls, tab1d):
Expand Down Expand Up @@ -1836,7 +1800,7 @@ def y(self):

@property
def bins(self):
raise RuntimeError('EnergyFunctionFilters have no bins.')
raise AttributeError('EnergyFunctionFilters have no bins.')

@property
def num_bins(self):
Expand Down
40 changes: 24 additions & 16 deletions openmc/tallies.py
Expand Up @@ -237,10 +237,10 @@ def sum(self):

# Convert NumPy arrays to SciPy sparse LIL matrices
if self.sparse:
self._sum = \
sps.lil_matrix(self._sum.flatten(), self._sum.shape)
self._sum_sq = \
sps.lil_matrix(self._sum_sq.flatten(), self._sum_sq.shape)
self._sum = sps.lil_matrix(self._sum.flatten(),
self._sum.shape)
self._sum_sq = sps.lil_matrix(self._sum_sq.flatten(),
self._sum_sq.shape)

# Indicate that Tally results have been read
self._results_read = True
Expand Down Expand Up @@ -2817,25 +2817,33 @@ def get_slice(self, scores=[], filters=[], filter_bins=[], nuclides=[]):

# Remove and/or reorder filter bins to user specifications
bin_indices = []
num_bins = 0

for filter_bin in filter_bins[i]:
bin_index = find_filter.get_bin_index(filter_bin)
if filter_type in [openmc.EnergyFilter,
openmc.EnergyoutFilter]:
bin_indices.extend([bin_index])
if issubclass(filter_type, openmc.RealFilter):
bin_indices.extend([bin_index, bin_index+1])
num_bins += 1
elif filter_type in [openmc.DistribcellFilter,
openmc.MeshFilter]:
bin_indices = [0]
num_bins = find_filter.num_bins
else:
bin_indices.append(bin_index)
num_bins += 1

find_filter.bins = np.unique(find_filter.bins[bin_indices])
find_filter._num_bins = num_bins
# Set bins for mesh/distribcell filters apart from others
if filter_type is openmc.MeshFilter:
bins = find_filter.mesh
elif filter_type is openmc.DistribcellFilter:
bins = find_filter.bins
else:
bins = np.unique(find_filter.bins[bin_indices])

# Create new filter
new_filter = filter_type(bins)

# Set number of bins manually for mesh/distribcell filters
if filter_type in (openmc.DistribcellFilter, openmc.MeshFilter):
new_filter._num_bins = find_filter._num_bins

# Replace existing filter with new one
for j, test_filter in enumerate(new_tally.filters):
if isinstance(test_filter, filter_type):
new_tally.filters[j] = new_filter

# If original tally was sparse, sparsify the sliced tally
new_tally.sparse = self.sparse
Expand Down

0 comments on commit 55f7260

Please sign in to comment.