13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
13 changes: 12 additions & 1 deletion openmc_plotter/docks.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -826,6 +826,11 @@ def __init__(self, model, main_window, field, colormaps=None):
zero_connector = partial(main_window.toggleTallyMaskZero)
self.maskZeroBox.stateChanged.connect(zero_connector)

# Volume normalization check box
self.volumeNormBox = QCheckBox()
volume_connector = partial(main_window.toggleTallyVolumeNorm)
self.volumeNormBox.stateChanged.connect(volume_connector)

# Clip data to min/max check box
self.clipDataBox = QCheckBox()
clip_connector = partial(main_window.toggleTallyDataClip)
Expand All@@ -849,6 +854,7 @@ def __init__(self, model, main_window, field, colormaps=None):
self.layout.addRow("Log Scale: ", self.scaleBox)
self.layout.addRow("Clip Data: ", self.clipDataBox)
self.layout.addRow("Mask Zeros: ", self.maskZeroBox)
self.layout.addRow("Volume normalize: ", self.volumeNormBox)
self.layout.addRow("Contours: ", self.contoursBox)
self.layout.addRow("Contour Levels:", self.contourLevelsLine)
self.setLayout(self.layout)
Expand DownExpand Up@@ -881,6 +887,10 @@ def updateMaskZeros(self):
cv = self.model.currentView
self.maskZeroBox.setChecked(cv.tallyMaskZeroValues)

def updateVolumeNorm(self):
Comment thread
paulromano marked this conversation as resolved.
cv = self.model.currentView
self.volumeNormBox.setChecked(cv.tallyVolumeNorm)

def updateDataClip(self):
cv = self.model.currentView
self.clipDataBox.setChecked(cv.clipTallyData)
Expand All@@ -900,6 +910,7 @@ def update(self):

self.updateMinMax()
self.updateMaskZeros()
self.updateVolumeNorm()
self.updateDataClip()
self.updateDataIndicator()
self.updateTallyContours()
6 changes: 5 additions & 1 deletion openmc_plotter/main_window.py
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -951,6 +951,10 @@ def toggleTallyMaskZero(self, state):
av = self.model.activeView
av.tallyMaskZeroValues = bool(state)

def toggleTallyVolumeNorm(self, state):
av = self.model.activeView
av.tallyVolumeNorm = bool(state)

def editTallyAlpha(self, value, apply=False):
av = self.model.activeView
av.tallyDataAlpha = value
Expand Down
8 changes: 4 additions & 4 deletions openmc_plotter/plotgui.py
Original file line numberDiff line numberDiff 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 DownExpand 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
141 changes: 67 additions & 74 deletions openmc_plotter/plotmodel.py
Original file line numberDiff line numberDiff 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, Optional

from PySide6.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
Expand All@@ -28,9 +28,14 @@
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}

_REACTION_UNITS = 'Reactions per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_REACTION_UNITS = 'reactions/source'
_PRODUCTION_UNITS = 'particles/source'
_ENERGY_UNITS = 'eV/source'

_REACTION_UNITS_VOL = 'reactions/cm³/source'
_PRODUCTION_UNITS_VOL = 'particles/cm³/source'
_ENERGY_UNITS_VOL = 'eV/cm³/source'


_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
Expand All@@ -42,24 +47,33 @@
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_ENERGY_SCORES = {'heating', 'heating-local', 'kappa-fission',
'fission-q-prompt', 'fission-q-recoverable',
'damage-energy'}

_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_SCORE_UNITS['flux'] = 'particle-cm/source'
_SCORE_UNITS['current'] = 'particle/source'
_SCORE_UNITS['events'] = 'events/source'
_SCORE_UNITS['inverse-velocity'] = 'particle-s/source'
_SCORE_UNITS['decay-rate'] = 'particle/s/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS for s in _ENERGY_SCORES})

_SCORE_UNITS_VOL = {p: _PRODUCTION_UNITS_VOL for p in _PRODUCTIONS}
_SCORE_UNITS_VOL['flux'] = 'particle/cm²/source'
_SCORE_UNITS_VOL['current'] = 'particle/cm³/source'
_SCORE_UNITS_VOL['events'] = 'events/cm³/source'
_SCORE_UNITS_VOL['inverse-velocity'] = 'particle-s/cm³/source'
_SCORE_UNITS_VOL['decay-rate'] = 'particle/s/cm³/source'
_SCORE_UNITS.update({s: _ENERGY_UNITS_VOL for s in _ENERGY_SCORES})


_TALLY_VALUES = {'Mean': 'mean',
'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 DownExpand Up@@ -382,11 +396,11 @@ def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))

def create_tally_image(self, view=None):
def create_tally_image(self, view: Optional[PlotView] = None):
"""
Parameters
----------
view :
view : PlotView
View used to set bounds of the tally data

Returns
Expand DownExpand Up@@ -436,6 +450,10 @@ def create_tally_image(self, view=None):
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)

if tally.contains_filter(openmc.MeshFilter):
# Check for volume normalization in order to change units
if view.tallyVolumeNorm:
units_out = _SCORE_UNITS_VOL.get(scores[0], _REACTION_UNITS_VOL)

if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
Expand DownExpand Up@@ -635,7 +653,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 +673,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 DownExpand Up@@ -738,18 +712,36 @@ def _do_op(array, tally_value, ax=0):
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)

# Account for mesh filter translation
if mesh_filter.translation is not None:
t = mesh_filter.translation
origin = (view.origin[0] - t[0], view.origin[1] - t[1], view.origin[2] - t[2])
else:
origin = view.origin

# Get mesh bins from openmc.lib
mesh_cpp = openmc.lib.meshes[mesh.id]
mesh_bins = mesh_cpp.get_plot_bins(
Comment thread
paulromano marked this conversation as resolved.
origin=origin,
width=(view.width, view.height),
basis=view.basis,
pixels=(view.h_res, view.v_res),
)

# Apply volume normalization
if view.tallyVolumeNorm:
data /= mesh_cpp.volumes

# 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]]

# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)

# set image data, reverse y-axis
image_data = data[::-1, ...]

# 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]]

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

@property
def cell_ids(self):
Expand DownExpand Up@@ -939,6 +931,7 @@ def __init__(self):
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.tallyVolumeNorm = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
Expand Down
35 changes: 18 additions & 17 deletions openmc_plotter/tools.py
Original file line numberDiff line numberDiff 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
Loading