From e98ddf9d8a54b9cbb0b3ba50a00ba8c6b9343be0 Mon Sep 17 00:00:00 2001 From: alex-lyons Date: Wed, 4 Mar 2020 14:43:10 +0000 Subject: [PATCH] Model.run now returns last statepoint path rather than k_eff. Updated tests and search. --- openmc/model/model.py | 20 ++++++------------- openmc/search.py | 4 +++- openmc/statepoint.py | 5 +++-- .../trigger_statepoint_restart/test.py | 18 ++++++++++------- tests/unit_tests/test_filters.py | 4 ++-- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index 2089721032..cb9907331f 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -42,8 +42,6 @@ class Model: Tallies information plots : openmc.Plots Plot information - statepoint : str - The last statepoint filename written when the model is run """ @@ -211,8 +209,8 @@ class Model: Returns ------- - uncertainties.UFloat - Combined estimator of k-effective from the last statepoint + Path + the name of the last statepoint written by this run (None if no statepoint was written) """ @@ -222,11 +220,11 @@ class Model: # Setting tstart here ensures we don't pick up any old statepoint # files that might preexist in the output directory tstart = time.time() - self.statepoint = None + last_statepoint = None openmc.run(**kwargs) - # Get output directory and last statepoint written by this run + # Get output directory and return the last statepoint written by this run if self.settings.output and 'path' in self.settings.output: output_dir = Path(self.settings.output['path']) else: @@ -235,11 +233,5 @@ class Model: mtime = sp.stat().st_mtime if mtime >= tstart: # >= allows for poor clock resolution tstart = mtime - self.statepoint = sp - - # Open the last statepoint to get the final k-effective - keff = None - if self.statepoint: - with openmc.StatePoint(self.statepoint) as sp: - keff = sp.k_combined - return keff + last_statepoint = sp + return last_statepoint diff --git a/openmc/search.py b/openmc/search.py index 6be8a50ea5..ee5dd1f077 100644 --- a/openmc/search.py +++ b/openmc/search.py @@ -51,7 +51,9 @@ def _search_keff(guess, target, model_builder, model_args, print_iterations, model = model_builder(guess, **model_args) # Run the model and obtain keff - keff = model.run(output=print_output) + sp_filepath = model.run(output=print_output) + with openmc.StatePoint(sp_filepath) as sp: + keff = sp.k_combined # Record the history guesses.append(guess) diff --git a/openmc/statepoint.py b/openmc/statepoint.py index 7d9895f8e3..dcb8b7924c 100644 --- a/openmc/statepoint.py +++ b/openmc/statepoint.py @@ -21,7 +21,7 @@ class StatePoint: Parameters ---------- - filename : str + filepath : str or Path Path to file to load autolink : bool, optional Whether to automatically link in metadata from a summary.h5 file and @@ -115,7 +115,8 @@ class StatePoint: """ - def __init__(self, filename, autolink=True): + def __init__(self, filepath, autolink=True): + filename = str(filepath) # in case it's a Path self._f = h5py.File(filename, 'r') self._meshes = {} self._filters = {} diff --git a/tests/regression_tests/trigger_statepoint_restart/test.py b/tests/regression_tests/trigger_statepoint_restart/test.py index 44b809454b..5f37383abd 100644 --- a/tests/regression_tests/trigger_statepoint_restart/test.py +++ b/tests/regression_tests/trigger_statepoint_restart/test.py @@ -86,31 +86,35 @@ class TriggerStatepointRestartTestHarness(PyAPITestHarness): args = {'openmc_exec': config['exe'], 'event_based': config['event']} if config['mpi']: args['mpi_args'] = [config['mpiexec'], '-n', config['mpi_np']] + # First non-restart run - k_combined_1 = self._model.run(**args) + spfile = self._model.run(**args) sp_batchno_1 = 0 - assert self._model.statepoint - with openmc.StatePoint(self._model.statepoint) as sp: + assert sp_file + with openmc.StatePoint(spfile) as sp: sp_batchno_1 = sp.current_batch + k_combined_1 = sp.k_combined assert sp_batchno_1 > 10 self._write_inputs(self._get_inputs()) self._compare_inputs() self._test_output_created() self._write_results(self._get_results()) self._compare_results() + # Second restart run restart_spfile = glob.glob(os.path.join(os.getcwd(), self._restart_sp)) assert len(restart_spfile) == 1 args['restart_file'] = restart_spfile[0] - k_combined_2 = self._model.run(**args) + spfile = self._model.run(**args) sp_batchno_2 = 0 - assert self._model.statepoint - with openmc.StatePoint(self._model.statepoint) as sp: + assert spfile + with openmc.StatePoint(spfile) as sp: sp_batchno_2 = sp.current_batch + k_combined_2 = sp.k_combined assert sp_batchno_2 > 10 assert sp_batchno_1 == sp_batchno_2, \ 'Different final batch number after restart' - assert str(k_combined_1) == str(k_combined_2), \ + assert k_combined_1 == k_combined_2, \ 'Different final k_combined after restart' self._write_inputs(self._get_inputs()) self._compare_inputs() diff --git a/tests/unit_tests/test_filters.py b/tests/unit_tests/test_filters.py index 10587a8125..bf62abb65d 100644 --- a/tests/unit_tests/test_filters.py +++ b/tests/unit_tests/test_filters.py @@ -173,10 +173,10 @@ def test_first_moment(run_in_tmpdir, box_model): for t in box_model.tallies: t.estimator = 'analog' - box_model.run() + sp_name = box_model.run() # Check that first moment matches the score from the plain tally - with openmc.StatePoint('statepoint.10.h5') as sp: + with openmc.StatePoint(sp_name) as sp: # Get scores from tally without expansion filters flux, scatter = sp.tallies[plain_tally.id].mean.ravel()