From c4a75f706256d575c798f9f4046d6d3b41490695 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 11 Mar 2024 22:35:32 -0500 Subject: [PATCH] Ensure that Model.run() works when specifying a custom XML path (#2889) --- openmc/model/model.py | 10 +++++++--- tests/unit_tests/test_deplete_chain.py | 5 +++-- tests/unit_tests/test_deplete_nuclide.py | 5 +++-- tests/unit_tests/test_model.py | 6 ++++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index 5194fec1da..abef9bb7a6 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -488,8 +488,11 @@ class Model: # if the provided path doesn't end with the XML extension, assume the # input path is meant to be a directory. If the directory does not # exist, create it and place a 'model.xml' file there. - if not str(xml_path).endswith('.xml') and not xml_path.exists(): - os.mkdir(xml_path) + if not str(xml_path).endswith('.xml'): + if not xml_path.exists(): + os.mkdir(xml_path) + elif not xml_path.is_dir(): + raise FileExistsError(f"File exists and is not a directory: '{xml_path}'") xml_path /= 'model.xml' # if this is an XML file location and the file's parent directory does # not exist, create it before continuing @@ -709,9 +712,10 @@ class Model: self.export_to_model_xml(**export_kwargs) else: self.export_to_xml(**export_kwargs) + path_input = export_kwargs.get("path", None) openmc.run(particles, threads, geometry_debug, restart_file, tracks, output, Path('.'), openmc_exec, mpi_args, - event_based) + event_based, path_input) # Get output directory and return the last statepoint written if self.settings.output and 'path' in self.settings.output: diff --git a/tests/unit_tests/test_deplete_chain.py b/tests/unit_tests/test_deplete_chain.py index e4e0231355..9753a53f73 100644 --- a/tests/unit_tests/test_deplete_chain.py +++ b/tests/unit_tests/test_deplete_chain.py @@ -5,6 +5,7 @@ from itertools import product from math import log import os from pathlib import Path +import warnings import numpy as np from openmc.mpi import comm @@ -438,9 +439,9 @@ def test_validate(simple_chain): simple_chain["C"].yield_data = {0.0253: {"A": 1.4, "B": 0.6}} assert simple_chain.validate(strict=True, tolerance=0.0) - with pytest.warns(None) as record: + with warnings.catch_warnings(): + warnings.simplefilter("error") assert simple_chain.validate(strict=False, quiet=False, tolerance=0.0) - assert len(record) == 0 # Mess up "earlier" nuclide's reactions decay_mode = simple_chain["A"].decay_modes.pop() diff --git a/tests/unit_tests/test_deplete_nuclide.py b/tests/unit_tests/test_deplete_nuclide.py index 6482d76a59..f2bb7d1b65 100644 --- a/tests/unit_tests/test_deplete_nuclide.py +++ b/tests/unit_tests/test_deplete_nuclide.py @@ -1,6 +1,7 @@ """Tests for the openmc.deplete.Nuclide class.""" import copy +import warnings import lxml.etree as ET import numpy as np @@ -277,9 +278,9 @@ def test_validate(): } # nuclide is good and should have no warnings raise - with pytest.warns(None) as record: + with warnings.catch_warnings(): + warnings.simplefilter("error") assert nuc.validate(strict=True, quiet=False, tolerance=0.0) - assert len(record) == 0 # invalidate decay modes decay = nuc.decay_modes.pop() diff --git a/tests/unit_tests/test_model.py b/tests/unit_tests/test_model.py index 24f136c8c4..16fa18d453 100644 --- a/tests/unit_tests/test_model.py +++ b/tests/unit_tests/test_model.py @@ -585,3 +585,9 @@ def test_single_xml_exec(run_in_tmpdir): with pytest.raises(RuntimeError, match='input_dir'): openmc.run(path_input='input_dir/pincell.xml') + + # Make sure path can be specified with run + pincell_model.run(path='my_model.xml') + + os.mkdir('subdir') + pincell_model.run(path='subdir')