Skip to content

Commit b6beb4a

Browse files
committed
Add support for arbitrarily named model.xml file
1 parent f8e4ba2 commit b6beb4a

3 files changed

Lines changed: 43 additions & 27 deletions

File tree

openmc_plotter/__main__.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
from argparse import ArgumentParser
42
from pathlib import Path
53
from threading import Thread
@@ -13,28 +11,28 @@
1311
from . import __version__
1412
from .main_window import MainWindow, _openmcReload
1513

14+
1615
def main():
1716
ap = ArgumentParser(description='OpenMC Plotter GUI')
1817
version_str = f'OpenMC Plotter Version: {__version__}'
1918
ap.add_argument('-v', '--version', action='version', version=version_str,
2019
help='Display version info.')
21-
ap.add_argument('-d', '--model-directory', default=os.curdir,
22-
help='Location of model dir (default is current dir)')
2320
ap.add_argument('-e','--ignore-settings', action='store_false',
2421
help='Ignore plot_settings.pkl file if present.')
2522
ap.add_argument('-s', '--threads', type=int, default=None,
2623
help='If present, number of threads used to generate plots.')
24+
ap.add_argument('model_path', nargs='?', default=os.curdir,
25+
help='Location of model XML file or a directory containing '
26+
'XML files (default is current dir)')
2727

2828
args = ap.parse_args()
2929

30-
os.chdir(args.model_directory)
31-
3230
run_app(args)
3331

3432

3533
def run_app(user_args):
36-
path_icon = str(Path(__file__).parent / 'assets/openmc_logo.png')
37-
path_splash = str(Path(__file__).parent / 'assets/splash.png')
34+
path_icon = str(Path(__file__).parent / 'assets' / 'openmc_logo.png')
35+
path_splash = str(Path(__file__).parent / 'assets' / 'splash.png')
3836

3937
app = QApplication(sys.argv)
4038
app.setOrganizationName("OpenMC")
@@ -53,7 +51,7 @@ def run_app(user_args):
5351
QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom)
5452
app.processEvents()
5553
# load OpenMC model on another thread
56-
openmc_args = {'threads': user_args.threads}
54+
openmc_args = {'threads': user_args.threads, 'model_path': user_args.model_path}
5755
loader_thread = Thread(target=_openmcReload, kwargs=openmc_args)
5856
loader_thread.start()
5957
# while thread is working, process app events
@@ -67,7 +65,7 @@ def run_app(user_args):
6765

6866
font_metric = QtGui.QFontMetrics(app.font())
6967
screen_size = app.primaryScreen().size()
70-
mainWindow = MainWindow(font_metric, screen_size)
68+
mainWindow = MainWindow(font_metric, screen_size, user_args.model_path)
7169
# connect splashscreen to main window, close when main window opens
7270
mainWindow.loadGui(use_settings_pkl=user_args.ignore_settings)
7371
mainWindow.show()

openmc_plotter/main_window.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
from functools import partial
3+
from pathlib import Path
34
import pickle
45
from threading import Thread
56

@@ -26,7 +27,7 @@
2627
from .tools import ExportDataDialog
2728

2829

29-
def _openmcReload(threads=None):
30+
def _openmcReload(threads=None, model_path='.'):
3031
# reset OpenMC memory, instances
3132
openmc.lib.reset()
3233
openmc.lib.finalize()
@@ -35,18 +36,22 @@ def _openmcReload(threads=None):
3536
args = ["-c"]
3637
if threads is not None:
3738
args += ["-s", str(threads)]
39+
args.append(model_path)
3840
openmc.lib.init(args)
3941
openmc.lib.settings.verbosity = 1
4042

43+
4144
class MainWindow(QMainWindow):
4245
def __init__(self,
4346
font=QtGui.QFontMetrics(QtGui.QFont()),
44-
screen_size=QtCore.QSize()):
47+
screen_size=QtCore.QSize(),
48+
model_path='.'):
4549
super().__init__()
4650

4751
self.screen = screen_size
4852
self.font_metric = font
4953
self.setWindowTitle('OpenMC Plot Explorer')
54+
self.model_path = Path(model_path)
5055

5156
def loadGui(self, use_settings_pkl=True):
5257

@@ -449,7 +454,7 @@ def loadModel(self, reload=False, use_settings_pkl=True):
449454
if reload:
450455
self.resetModels()
451456
else:
452-
self.model = PlotModel(use_settings_pkl)
457+
self.model = PlotModel(use_settings_pkl, self.model_path)
453458

454459
# update plot and model settings
455460
self.updateRelativeBases()
@@ -1152,7 +1157,7 @@ def saveSettings(self):
11521157
self.model.statepoint.close()
11531158

11541159
# get hashes for material.xml and geometry.xml at close
1155-
mat_xml_hash, geom_xml_hash = hash_model()
1160+
mat_xml_hash, geom_xml_hash = hash_model(self.model_path)
11561161

11571162
pickle_data = {
11581163
'version': self.model.version,
@@ -1161,7 +1166,11 @@ def saveSettings(self):
11611166
'mat_xml_hash': mat_xml_hash,
11621167
'geom_xml_hash': geom_xml_hash
11631168
}
1164-
with open('plot_settings.pkl', 'wb') as file:
1169+
if self.model_path.is_file():
1170+
settings_pkl = self.model_path.with_name('plot_settings.pkl')
1171+
else:
1172+
settings_pkl = self.model_path / 'plot_settings.pkl'
1173+
with settings_pkl.open('wb') as file:
11651174
pickle.dump(pickle_data, file)
11661175

11671176
def exportTallyData(self):

openmc_plotter/plotmodel.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@
6161
'Rel. Error': 'rel_err'}
6262

6363

64-
def hash_file(filename):
64+
def hash_file(path):
6565
# return the md5 hash of a file
6666
h = hashlib.md5()
67-
with open(filename,'rb') as file:
67+
with path.open('rb') as file:
6868
chunk = 0
6969
while chunk != b'':
7070
# read 32768 bytes at a time
@@ -73,15 +73,17 @@ def hash_file(filename):
7373
return h.hexdigest()
7474

7575

76-
def hash_model():
76+
def hash_model(model_path):
7777
"""Get hash values for materials.xml and geometry.xml (or model.xml)"""
78-
# TODO: Add support for model in file other than model.xml
79-
if Path('model.xml').is_file():
80-
mat_xml_hash = hash_file('model.xml')
78+
if model_path.is_file():
79+
mat_xml_hash = hash_file(model_path)
80+
geom_xml_hash = ""
81+
elif (model_path / 'model.xml').exists():
82+
mat_xml_hash = hash_file(model_path / 'model.xml')
8183
geom_xml_hash = ""
8284
else:
83-
mat_xml_hash = hash_file('materials.xml')
84-
geom_xml_hash = hash_file('geometry.xml')
85+
mat_xml_hash = hash_file(model_path / 'materials.xml')
86+
geom_xml_hash = hash_file(model_path / 'geometry.xml')
8587
return mat_xml_hash, geom_xml_hash
8688

8789

@@ -92,6 +94,8 @@ class PlotModel:
9294
----------
9395
use_settings_pkl : bool
9496
If True, use plot_settings.pkl file to reload settings
97+
model_path : pathlib.Path
98+
Path to model XML file or directory
9599
96100
Attributes
97101
----------
@@ -128,7 +132,7 @@ class PlotModel:
128132
have unapplied changes
129133
"""
130134

131-
def __init__(self, use_settings_pkl):
135+
def __init__(self, use_settings_pkl, model_path):
132136
""" Initialize PlotModel class attributes """
133137

134138
# Retrieve OpenMC Cells/Materials
@@ -160,8 +164,13 @@ def __init__(self, use_settings_pkl):
160164

161165
self.defaultView = self.getDefaultView()
162166

163-
if use_settings_pkl and os.path.isfile('plot_settings.pkl'):
164-
with open('plot_settings.pkl', 'rb') as file:
167+
if model_path.is_file():
168+
settings_pkl = model_path.with_name('plot_settings.pkl')
169+
else:
170+
settings_pkl = model_path / 'plot_settings.pkl'
171+
172+
if use_settings_pkl and settings_pkl.is_file():
173+
with settings_pkl.open('rb') as file:
165174
try:
166175
data = pickle.load(file)
167176
except AttributeError:
@@ -189,7 +198,7 @@ def __init__(self, use_settings_pkl):
189198

190199
# get materials.xml and geometry.xml hashes to
191200
# restore additional settings if possible
192-
mat_xml_hash, geom_xml_hash = hash_model()
201+
mat_xml_hash, geom_xml_hash = hash_model(model_path)
193202
if mat_xml_hash == data['mat_xml_hash'] and \
194203
geom_xml_hash == data['geom_xml_hash']:
195204
restore_domains = True

0 commit comments

Comments
 (0)