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

Generalize mesh tally plotting #134

Merged
merged 10 commits into from
Feb 15, 2024
2 changes: 1 addition & 1 deletion openmc_plotter/docks.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def updateNuclides(self):
self.model.appliedNuclides = tuple(applied_nuclides)

if 'total' in applied_nuclides:
self.model.appliedNuclides = ['total',]
self.model.appliedNuclides = ('total',)
for nuclide, nuclide_box in self.nuclide_map.items():
if nuclide != 'total':
nuclide_box.setFlags(QtCore.Qt.ItemIsUserCheckable)
Expand Down
2 changes: 1 addition & 1 deletion openmc_plotter/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def openStatePoint(self):
msg_box.exec()
return
filename, ext = QFileDialog.getOpenFileName(self, "Open StatePoint",
".", "statepoint*.h5")
".", "*.h5")
if filename:
try:
self.model.openStatePoint(filename)
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy as np

from .plot_colors import rgb_normalize, invert_rgb
from .plotmodel import DomainDelegate
from .plotmodel import DomainDelegate, PlotModel
from .plotmodel import _NOT_FOUND, _VOID_REGION, _OVERLAP, _MODEL_PROPERTIES
from .scientific_spin_box import ScientificDoubleSpinBox
from .custom_widgets import HorizontalLine
Expand All @@ -23,7 +23,7 @@

class PlotImage(FigureCanvas):

def __init__(self, model, parent, main_window):
def __init__(self, model: PlotModel, parent, main_window):

self.figure = Figure(dpi=main_window.logicalDpiX())
super().__init__(self.figure)
Expand Down Expand Up @@ -339,8 +339,8 @@ def mouseReleaseEvent(self, event):

def wheelEvent(self, event):

if event.delta() and event.modifiers() == QtCore.Qt.ShiftModifier:
numDegrees = event.delta() / 8
if event.angleDelta() and event.modifiers() == QtCore.Qt.ShiftModifier:
numDegrees = event.angleDelta() / 8

if 24 < self.main_window.zoom + numDegrees < 5001:
self.main_window.editZoom(self.main_window.zoom + numDegrees)
Expand Down
79 changes: 22 additions & 57 deletions openmc_plotter/plotmodel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations
from ast import literal_eval
from collections import defaultdict
import copy
import hashlib
import itertools
import os
from pathlib import Path
import pickle
import threading
from typing import Literal, Tuple

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand Down Expand Up @@ -60,6 +60,8 @@
'Std. Dev.': 'std_dev',
'Rel. Error': 'rel_err'}

TallyValueType = Literal['mean', 'std_dev', 'rel_err']


def hash_file(path):
# return the md5 hash of a file
Expand Down Expand Up @@ -386,7 +388,7 @@ def create_tally_image(self, view=None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand Down Expand Up @@ -635,7 +637,10 @@ def _create_distribcell_image(self, tally, tally_value, scores, nuclides, cellin

return image_data, None, data_min, data_max

def _create_tally_mesh_image(self, tally, tally_value, scores, nuclides, view=None):
def _create_tally_mesh_image(
self, tally: openmc.Tally, tally_value: TallyValueType,
scores: Tuple[str], nuclides: Tuple[str], view: PlotView = None
):
# some variables used throughout
if view is None:
view = self.currentView
Expand All @@ -652,57 +657,10 @@ def _do_op(array, tally_value, ax=0):
# start with reshaped data
data = tally.get_reshaped_data(tally_value)

# determine basis indices
if view.basis == 'xy':
h_ind = 0
v_ind = 1
ax = 2
elif view.basis == 'yz':
h_ind = 1
v_ind = 2
ax = 0
else:
h_ind = 0
v_ind = 2
ax = 1

# adjust corners of the mesh for a translation
# applied to the mesh filter
lower_left = mesh.lower_left
upper_right = mesh.upper_right
width = mesh.width
dimension = mesh.dimension
if hasattr(mesh_filter, 'translation') and mesh_filter.translation is not None:
lower_left += mesh_filter.translation
upper_right += mesh_filter.translation

# For 2D meshes, add an extra z dimension
if len(mesh.dimension) == 2:
lower_left = np.hstack((lower_left, -1e50))
upper_right = np.hstack((upper_right, 1e50))
width = np.hstack((width, 2e50))
dimension = np.hstack((dimension, 1))

# reduce data to the visible slice of the mesh values
k = int((view.origin[ax] - lower_left[ax]) // width[ax])

# setup slice
data_slice = [None, None, None]
data_slice[h_ind] = slice(dimension[h_ind])
data_slice[v_ind] = slice(dimension[v_ind])
data_slice[ax] = k

if k < 0 or k > dimension[ax]:
return (None, None, None, None)

# move mesh axes to the end of the filters
filter_idx = [type(filter) for filter in tally.filters].index(openmc.MeshFilter)
data = np.moveaxis(data, filter_idx, -1)

# reshape data (with zyx ordering for mesh data)
data = data.reshape(data.shape[:-1] + tuple(dimension[::-1]))
data = data[..., data_slice[2], data_slice[1], data_slice[0]]

# sum over the rest of the tally filters
for tally_filter in tally.filters:
if type(tally_filter) == openmc.MeshFilter:
Expand Down Expand Up @@ -742,14 +700,21 @@ def _do_op(array, tally_value, ax=0):
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]
# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
paulromano marked this conversation as resolved.
Show resolved Hide resolved
origin=view.origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# return data extents (in cm) for the tally
extents = [lower_left[h_ind], upper_right[h_ind],
lower_left[v_ind], upper_right[v_ind]]
# set image data
image_data = np.full_like(self.ids, np.nan, dtype=float)
mask = (mesh_bins >= 0)
image_data[mask] = data[mesh_bins[mask]]

return image_data, extents, data_min, data_max
return image_data, None, data_min, data_max

@property
def cell_ids(self):
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,36 @@ def populate(self):
mesh = mesh_filter.mesh
assert(mesh.n_dimension == 3)

llc = mesh.lower_left
bbox = mesh.bounding_box

llc = bbox.lower_left
self.xminBox.setValue(llc[0])
self.yminBox.setValue(llc[1])
self.zminBox.setValue(llc[2])

urc = mesh.upper_right
urc = bbox.upper_right
self.xmaxBox.setValue(urc[0])
self.ymaxBox.setValue(urc[1])
self.zmaxBox.setValue(urc[2])

dims = mesh.dimension
self.xResBox.setValue(dims[0])
self.yResBox.setValue(dims[1])
self.zResBox.setValue(dims[2])

bounds_msg = "Using MeshFilter to set bounds automatically."
for box in self.bounds_spin_boxes:
box.setEnabled(False)
box.setToolTip(bounds_msg)

resolution_msg = "Using MeshFilter to set resolution automatically."
self.xResBox.setEnabled(False)
self.xResBox.setToolTip(resolution_msg)
self.yResBox.setEnabled(False)
self.yResBox.setToolTip(resolution_msg)
self.zResBox.setEnabled(False)
self.zResBox.setToolTip(resolution_msg)
dims = mesh.dimension
if len(dims) == 3:
self.xResBox.setValue(dims[0])
self.yResBox.setValue(dims[1])
self.zResBox.setValue(dims[2])

resolution_msg = "Using MeshFilter to set resolution automatically."
self.xResBox.setEnabled(False)
self.xResBox.setToolTip(resolution_msg)
self.yResBox.setEnabled(False)
self.yResBox.setToolTip(resolution_msg)
self.zResBox.setEnabled(False)
self.zResBox.setToolTip(resolution_msg)

else:
# initialize using the bounds of the current view
Expand All @@ -214,14 +217,12 @@ def populate(self):

def export_data(self):
# cache current and active views
cv = self.model.currentView
av = self.model.activeView
try:
# export the tally data
self._export_data()
finally:
#always reset to the original view
self.model.currentView = cv
# always reset to the original view
self.model.activeView = av
self.model.makePlot()

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
],

# Dependencies
'python_requires': '>=3.6',
'python_requires': '>=3.8',
'install_requires': [
'openmc>0.12.2', 'numpy', 'matplotlib', 'PySide6'
'openmc>0.14.0', 'numpy', 'matplotlib', 'PySide6'
],
'extras_require': {
'test' : ['pytest', 'pytest-qt'],
Expand Down
Loading