Skip to content

Commit d1a1be1

Browse files
authored
Ensure that Model.run() works when specifying a custom XML path (openmc-dev#2889)
1 parent 8422adc commit d1a1be1

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎openmc/model/model.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,11 @@ def export_to_model_xml(self, path='model.xml', remove_surfs=False):
488488
# if the provided path doesn't end with the XML extension, assume the
489489
# input path is meant to be a directory. If the directory does not
490490
# exist, create it and place a 'model.xml' file there.
491-
if not str(xml_path).endswith('.xml') and not xml_path.exists():
492-
os.mkdir(xml_path)
491+
if not str(xml_path).endswith('.xml'):
492+
if not xml_path.exists():
493+
os.mkdir(xml_path)
494+
elif not xml_path.is_dir():
495+
raise FileExistsError(f"File exists and is not a directory: '{xml_path}'")
493496
xml_path /= 'model.xml'
494497
# if this is an XML file location and the file's parent directory does
495498
# not exist, create it before continuing
@@ -709,9 +712,10 @@ def run(self, particles=None, threads=None, geometry_debug=False,
709712
self.export_to_model_xml(**export_kwargs)
710713
else:
711714
self.export_to_xml(**export_kwargs)
715+
path_input = export_kwargs.get("path", None)
712716
openmc.run(particles, threads, geometry_debug, restart_file,
713717
tracks, output, Path('.'), openmc_exec, mpi_args,
714-
event_based)
718+
event_based, path_input)
715719

716720
# Get output directory and return the last statepoint written
717721
if self.settings.output and 'path' in self.settings.output:

‎tests/unit_tests/test_deplete_chain.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from math import log
66
import os
77
from pathlib import Path
8+
import warnings
89

910
import numpy as np
1011
from openmc.mpi import comm
@@ -438,9 +439,9 @@ def test_validate(simple_chain):
438439
simple_chain["C"].yield_data = {0.0253: {"A": 1.4, "B": 0.6}}
439440

440441
assert simple_chain.validate(strict=True, tolerance=0.0)
441-
with pytest.warns(None) as record:
442+
with warnings.catch_warnings():
443+
warnings.simplefilter("error")
442444
assert simple_chain.validate(strict=False, quiet=False, tolerance=0.0)
443-
assert len(record) == 0
444445

445446
# Mess up "earlier" nuclide's reactions
446447
decay_mode = simple_chain["A"].decay_modes.pop()

‎tests/unit_tests/test_deplete_nuclide.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for the openmc.deplete.Nuclide class."""
22

33
import copy
4+
import warnings
45

56
import lxml.etree as ET
67
import numpy as np
@@ -277,9 +278,9 @@ def test_validate():
277278
}
278279

279280
# nuclide is good and should have no warnings raise
280-
with pytest.warns(None) as record:
281+
with warnings.catch_warnings():
282+
warnings.simplefilter("error")
281283
assert nuc.validate(strict=True, quiet=False, tolerance=0.0)
282-
assert len(record) == 0
283284

284285
# invalidate decay modes
285286
decay = nuc.decay_modes.pop()

‎tests/unit_tests/test_model.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,9 @@ def test_single_xml_exec(run_in_tmpdir):
585585

586586
with pytest.raises(RuntimeError, match='input_dir'):
587587
openmc.run(path_input='input_dir/pincell.xml')
588+
589+
# Make sure path can be specified with run
590+
pincell_model.run(path='my_model.xml')
591+
592+
os.mkdir('subdir')
593+
pincell_model.run(path='subdir')

0 commit comments

Comments
 (0)