mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 06:05:58 -04:00
Remove _smart_set_bins in favor of overriding Filter.bins.setter
This commit is contained in:
parent
2ef1c23d5a
commit
55f7260b2a
2 changed files with 45 additions and 73 deletions
|
|
@ -14,7 +14,10 @@ import pandas as pd
|
|||
|
||||
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',
|
||||
|
|
@ -170,7 +173,7 @@ class Filter(IDManagerMixin):
|
|||
# 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
|
||||
|
||||
|
|
@ -425,12 +428,15 @@ class Filter(IDManagerMixin):
|
|||
|
||||
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)
|
||||
|
|
@ -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):
|
||||
|
|
@ -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):
|
||||
|
|
@ -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):
|
||||
|
|
@ -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):
|
||||
|
|
@ -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):
|
||||
|
|
@ -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)
|
||||
|
|
@ -720,7 +692,7 @@ class MeshFilter(Filter):
|
|||
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
|
||||
|
|
@ -1169,15 +1141,11 @@ class DistribcellFilter(Filter):
|
|||
|
||||
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
|
||||
|
|
@ -1188,7 +1156,7 @@ class DistribcellFilter(Filter):
|
|||
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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -1799,7 +1763,7 @@ class EnergyFunctionFilter(Filter):
|
|||
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):
|
||||
|
|
@ -1836,7 +1800,7 @@ class EnergyFunctionFilter(Filter):
|
|||
|
||||
@property
|
||||
def bins(self):
|
||||
raise RuntimeError('EnergyFunctionFilters have no bins.')
|
||||
raise AttributeError('EnergyFunctionFilters have no bins.')
|
||||
|
||||
@property
|
||||
def num_bins(self):
|
||||
|
|
|
|||
|
|
@ -237,10 +237,10 @@ class Tally(IDManagerMixin):
|
|||
|
||||
# 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
|
||||
|
|
@ -2817,25 +2817,33 @@ class Tally(IDManagerMixin):
|
|||
|
||||
# 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue