Skip to content

Commit 05743d8

Browse files
committed
restore domain properties if file hashes are unchanged as well
1 parent 6a5f909 commit 05743d8

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

openmc_plotter/main_window.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import pickle
55
from threading import Thread
6-
import hashlib
76

87
from PySide2 import QtCore, QtGui
98
from PySide2.QtGui import QKeyEvent
@@ -21,7 +20,7 @@
2120
except ImportError:
2221
_HAVE_VTK = False
2322

24-
from .plotmodel import PlotModel, DomainTableModel
23+
from .plotmodel import PlotModel, DomainTableModel, hash_file
2524
from .plotgui import PlotImage, ColorDialog
2625
from .docks import DomainDock, TallyDock
2726
from .overlays import ShortcutsOverlay
@@ -1154,10 +1153,16 @@ def saveSettings(self):
11541153
if self.model.statepoint:
11551154
self.model.statepoint.close()
11561155

1156+
# get hashes for geometry.xml and material.xml at close
1157+
mat_xml_hash = hash_file('materials.xml')
1158+
geom_xml_hash = hash_file('geometry.xml')
1159+
11571160
pickle_data = {
11581161
'version': self.model.version,
11591162
'currentView': self.model.currentView,
1160-
'statepoint': self.model.statepoint
1163+
'statepoint': self.model.statepoint,
1164+
'mat_xml_hash': mat_xml_hash,
1165+
'geom_xml_hash': geom_xml_hash
11611166
}
11621167
with open('plot_settings.pkl', 'wb') as file:
11631168
pickle.dump(pickle_data, file)

openmc_plotter/plotmodel.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import threading
66
import os
77
import pickle
8+
import hashlib
89

910
from PySide2.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
1011
from PySide2.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
@@ -60,6 +61,16 @@
6061
'Std. Dev.': 'std_dev',
6162
'Rel. Error': 'rel_err'}
6263

64+
def hash_file(filename):
65+
# return the md5 hash of a file
66+
h = hashlib.md5()
67+
with open(filename,'rb') as file:
68+
chunk = 0
69+
while chunk != b'':
70+
# read 32768 bytes at a time
71+
chunk = file.read(32768)
72+
h.update(chunk)
73+
return h.hexdigest()
6374
class PlotModel():
6475
""" Geometry and plot settings for OpenMC Plot Explorer model
6576
@@ -149,6 +160,8 @@ def __init__(self, use_settings_pkl):
149160
self.defaultView = self.getDefaultView()
150161

151162
else:
163+
restore_domains = False
164+
152165
# check GUI version
153166
if data['version'] != self.version:
154167
print("WARNING: previous plot settings are for a different "
@@ -159,6 +172,14 @@ def __init__(self, use_settings_pkl):
159172
else:
160173
view = data['currentView']
161174

175+
# get materials.xml and geometry.xml hashes to
176+
# restore additional settings if possible
177+
mat_xml_hash = hash_file('materials.xml')
178+
geom_xml_hash = hash_file('geometry.xml')
179+
if mat_xml_hash == data['mat_xml_hash'] and \
180+
geom_xml_hash == data['geom_xml_hash']:
181+
restore_domains = True
182+
162183
# restore statepoint file
163184
try:
164185
self.statepoint = data['statepoint']
@@ -171,7 +192,8 @@ def __init__(self, use_settings_pkl):
171192
msg_box.exec_()
172193
self.statepoint = None
173194

174-
self.defaultView = PlotView(restore_view=view)
195+
self.defaultView = PlotView(restore_view=view,
196+
restore_domains=restore_domains)
175197

176198
else:
177199
self.defaultView = self.getDefaultView()
@@ -929,6 +951,9 @@ class PlotView:
929951
Height of plot view in model units
930952
restore_view : PlotView or None
931953
view object with specified parameters to restore
954+
restore_domains : bool (optional)
955+
If True and restore_view is provided, then also restore domain
956+
properties. Default False.
932957
933958
Attributes
934959
----------
@@ -948,7 +973,8 @@ class PlotView:
948973
plotbase_attrs = ('level', 'origin', 'width', 'height',
949974
'h_res', 'v_res', 'basis', 'color_overlaps')
950975

951-
def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None):
976+
def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None,
977+
restore_domains=False):
952978
"""Initialize PlotView attributes"""
953979

954980
if restore_view is not None:
@@ -959,9 +985,14 @@ def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None):
959985
self.view_params = ViewParam(origin=origin, width=width, height=height)
960986

961987
# Get model domain info
962-
self.cells = self.getDomains('cell')
963-
self.materials = self.getDomains('material')
964-
self.selectedTally = None
988+
if restore_domains and restore_view is not None:
989+
self.cells = restore_view.cells
990+
self.materials = restore_view.materials
991+
self.selectedTally = restore_view.selectedTally
992+
else:
993+
self.cells = self.getDomains('cell')
994+
self.materials = self.getDomains('material')
995+
self.selectedTally = None
965996

966997
def __getattr__(self, name):
967998
if name in self.attrs:

0 commit comments

Comments
 (0)