33import os
44import pickle
55from threading import Thread
6+ import hashlib
67
78from PySide2 import QtCore , QtGui
89from 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+
4052class 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