Skip to content

Commit 1a5b837

Browse files
authored
Merge pull request #94 from kkiesling/ignore-settings
ignore `plot_settings.pkl` file if model is changed
2 parents a8abcda + cfbc5a8 commit 1a5b837

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

openmc_plotter/main_window.py

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

78
from PySide2 import QtCore, QtGui
89
from PySide2.QtGui import QKeyEvent
@@ -37,6 +38,17 @@ def _openmcReload():
3738
openmc.lib.init(["-c"])
3839
openmc.lib.settings.verbosity = 1
3940

41+
def hash_file(filename):
42+
# return the md5 hash of a file
43+
h = hashlib.md5()
44+
with open(filename,'rb') as file:
45+
chunk = 0
46+
while chunk != b'':
47+
# read 32768 bytes at a time
48+
chunk = file.read(32768)
49+
h.update(chunk)
50+
return h.hexdigest()
51+
4052
class MainWindow(QMainWindow):
4153
def __init__(self,
4254
font=QtGui.QFontMetrics(QtGui.QFont()),
@@ -1065,6 +1077,21 @@ def restoreModelSettings(self):
10651077
with open('plot_settings.pkl', 'rb') as file:
10661078
model = pickle.load(file)
10671079

1080+
# check if loaded cell/mat ids hash match the pkl file:
1081+
current_mat_xml_hash = hash_file('materials.xml')
1082+
current_geom_xml_hash = hash_file('geometry.xml')
1083+
if (current_mat_xml_hash != model.mat_xml_hash) or \
1084+
(current_geom_xml_hash != model.geom_xml_hash):
1085+
# hashes do not match so ignore plot_settings.pkl file
1086+
msg_box = QMessageBox()
1087+
msg = "WARNING: Model has changed since storing plot " +\
1088+
"settings. Ignoring previous plot settings."
1089+
msg_box.setText(msg)
1090+
msg_box.setIcon(QMessageBox.Warning)
1091+
msg_box.setStandardButtons(QMessageBox.Ok)
1092+
msg_box.exec_()
1093+
return
1094+
10681095
# do not replace model if the version is out of date
10691096
if model.version != self.model.version:
10701097
print("WARNING: previous plot settings are for a different "
@@ -1185,6 +1212,10 @@ def saveSettings(self):
11851212
if len(self.model.subsequentViews) > 10:
11861213
self.model.subsequentViews = self.model.subsequentViews[-10:]
11871214

1215+
# get hashes for geometry.xml and material.xml at close
1216+
self.model.mat_xml_hash = hash_file('materials.xml')
1217+
self.model.geom_xml_hash = hash_file('geometry.xml')
1218+
11881219
with open('plot_settings.pkl', 'wb') as file:
11891220
if self.model.statepoint:
11901221
self.model.statepoint.close()
@@ -1193,3 +1224,4 @@ def saveSettings(self):
11931224
def exportTallyData(self):
11941225
# show export tool dialog
11951226
self.showExportDialog()
1227+

0 commit comments

Comments
 (0)