Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CellInstanceFilter #88

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions openmc_plotter/docks.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,11 @@ def _createFilterTree(self, spatial_filters):
continue

def _bin_sort_val(bin):
if isinstance(bin, Iterable) and all([isinstance(val, float) for val in bin]):
return np.sum(bin)
if isinstance(bin, Iterable):
if all([isinstance(val, float) for val in bin]):
return np.sum(bin)
else:
return tuple(bin)
else:
return bin

Expand Down
57 changes: 32 additions & 25 deletions openmc_plotter/plotmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
openmc.MaterialFilter,
openmc.CellFilter,
openmc.DistribcellFilter,
openmc.CellInstanceFilter,
openmc.MeshFilter)

_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
Expand Down Expand Up @@ -330,6 +331,9 @@ def create_tally_image(self, view=None):

units_out = list(units)[0]

contains_distribcell = tally.contains_filter(openmc.DistribcellFilter)
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
if tally_value == 'rel_err':
# get both the std. dev. data and mean data
Expand Down Expand Up @@ -360,28 +364,23 @@ def create_tally_image(self, view=None):
nuclides,
view)
return image + (units_out,)
elif tally.contains_filter(openmc.DistribcellFilter):
elif contains_distribcell or contains_cellinstance:
if tally_value == 'rel_err':
mean_data = self._create_distribcell_image(tally,
'mean',
scores,
nuclides)
std_dev_data = self._create_distribcell_image(tally,
'std_dev',
scores,
nuclides)
image_data = 100 * np.divide(std_dev_data[0],
mean_data[0],
out=np.zeros_like(mean_data[0]),
where=mean_data != 0)
mean_data = self._create_distribcell_image(
tally, 'mean', scores, nuclides, contains_cellinstance)
std_dev_data = self._create_distribcell_image(
tally, 'std_dev', scores, nuclides)
image_data = 100 * np.divide(
std_dev_data[0], mean_data[0],
out=np.zeros_like(mean_data[0]),
where=mean_data != 0
)
data_min = np.min(image_data)
data_max = np.max(image_data)
return image_data, None, data_min, data_max, '% error'
else:
image = self._create_distribcell_image(tally,
tally_value,
scores,
nuclides)
image = self._create_distribcell_image(
tally, tally_value, scores, nuclides, contains_cellinstance)
return image + (units_out,)
else:
# same as above, get the std. dev. data
Expand Down Expand Up @@ -501,19 +500,27 @@ def _do_op(array, tally_value, ax=0):

return image_data, None, data_min, data_max

def _create_distribcell_image(self, tally, tally_value, scores, nuclides):
dfilter = tally.find_filter(openmc.DistribcellFilter)

def _create_distribcell_image(self, tally, tally_value, scores, nuclides, cellinstance=False):
# Get flattened array of tally results
data = tally.get_values(scores=scores, nuclides=nuclides, value=tally_value)
data = data.flatten()

cell_id = dfilter.bins[0]
# create a mask for ids that match the cell
# Create an empty array of appropriate shape for image
image_data = np.full_like(self.ids, np.nan, dtype=float)

cell_id_mask = self.cell_ids == cell_id
for i, v in enumerate(data):
instance_mask = self.instances == i
# Determine appropriate set of bins depending on filter type
if cellinstance:
f = tally.find_filter(openmc.CellInstanceFilter)
bins = f.bins
else:
f = tally.find_filter(openmc.DistribcellFilter)
bins = [(f.bins[0], i) for i in range(data.size)]

# Iterate over tally bins, setting any pixels that have matching (cell
# ID, instance) each time
for v, (cell_id, instance) in zip(data, bins):
cell_id_mask = (self.cell_ids == cell_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how much of a performance hit this might be, but having this in the loop is going to cause a repeated calculation of this mask for DistribCellFilter's. The same is true for CellInstanceFilter's that contain instances of the same cell.

I suppose that since we're typically working with around 100k pixels in the image data this shouldn't be too bad. Mainly noting it here in case we run into a performance issue with these filter types in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I'm well aware that this is performance-shitty code. But, so far on the models I've played with it doesn't seem to be a big deal. It would be easy enough to optimize if we find later on that it is causing significant lags on larger models. I can go ahead and add a comment in the code about this since I'm sure we'll probably forget.

instance_mask = (self.instances == instance)
image_data[cell_id_mask & instance_mask] = v

data_min = np.min(data)
Expand Down