From 1312a1cc637b82c69757a282849e20edf57ca5eb Mon Sep 17 00:00:00 2001 From: alex-lyons Date: Tue, 3 Mar 2020 13:37:24 +0000 Subject: [PATCH] Obtain last statepoint from a glob of output dir. Reverted regex filter in executor as this is no longer needed --- openmc/executor.py | 17 +----------- openmc/model/model.py | 27 ++++++++++++++++--- .../inputs_true.dat | 1 + .../trigger_statepoint_restart/test.py | 3 +++ 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/openmc/executor.py b/openmc/executor.py index 76d100d32a..ebd1a450bc 100644 --- a/openmc/executor.py +++ b/openmc/executor.py @@ -11,10 +11,6 @@ def _run(args, output, cwd): p = subprocess.Popen(args, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) - # Compile a regex object to identify statepoint writes - sp_rxprog = re.compile(r'Creating state point (.+)\.\.\.') - last_statepoint = None - # Capture and re-print OpenMC output in real-time lines = [] while True: @@ -23,11 +19,6 @@ def _run(args, output, cwd): if not line and p.poll() is not None: break - # If a statepoint was written, capture its filename - sp_match = sp_rxprog.search(line) - if sp_match: - last_statepoint = sp_match.group(1) - lines.append(line) if output: # If user requested output, print to screen @@ -37,7 +28,6 @@ def _run(args, output, cwd): if p.returncode != 0: raise subprocess.CalledProcessError(p.returncode, ' '.join(args), ''.join(lines)) - return last_statepoint def plot_geometry(output=True, openmc_exec='openmc', cwd='.'): @@ -195,11 +185,6 @@ def run(particles=None, threads=None, geometry_debug=False, event_based : bool, optional Turns on event-based parallelism, instead of default history-based - Returns - ------- - str - Name of the last written statepoint file, or None - Raises ------ subprocess.CalledProcessError @@ -229,4 +214,4 @@ def run(particles=None, threads=None, geometry_debug=False, if mpi_args is not None: args = mpi_args + args - return _run(args, output, cwd) + _run(args, output, cwd) diff --git a/openmc/model/model.py b/openmc/model/model.py index e59c679b52..2089721032 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -1,5 +1,6 @@ from collections.abc import Iterable from pathlib import Path +import time import openmc from openmc.checkvalue import check_type, check_value @@ -175,7 +176,7 @@ class Model: will be created. """ - # Create directory if + # Create directory if required d = Path(directory) if not d.is_dir(): d.mkdir(parents=True) @@ -201,7 +202,7 @@ class Model: def run(self, **kwargs): """Creates the XML files, runs OpenMC, and returns k-effective. - The last statepoint filename is available in model.statepoint + The last statepoint Path is available in model.statepoint Parameters ---------- @@ -211,14 +212,32 @@ class Model: Returns ------- uncertainties.UFloat - Combined estimator of k-effective from the statepoint + Combined estimator of k-effective from the last statepoint + (None if no statepoint was written) """ self.export_to_xml() - self.statepoint = openmc.run(**kwargs) + # 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 + openmc.run(**kwargs) + + # Get output directory and last statepoint written by this run + if self.settings.output and 'path' in self.settings.output: + output_dir = Path(self.settings.output['path']) + else: + output_dir = Path.cwd() + for sp in output_dir.glob('statepoint.*.h5'): + 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: diff --git a/tests/regression_tests/trigger_statepoint_restart/inputs_true.dat b/tests/regression_tests/trigger_statepoint_restart/inputs_true.dat index decc87b0c5..26890f9f86 100644 --- a/tests/regression_tests/trigger_statepoint_restart/inputs_true.dat +++ b/tests/regression_tests/trigger_statepoint_restart/inputs_true.dat @@ -25,6 +25,7 @@ 1000 1 + 1 diff --git a/tests/regression_tests/trigger_statepoint_restart/test.py b/tests/regression_tests/trigger_statepoint_restart/test.py index 961a576a55..44b809454b 100644 --- a/tests/regression_tests/trigger_statepoint_restart/test.py +++ b/tests/regression_tests/trigger_statepoint_restart/test.py @@ -33,6 +33,7 @@ def model(): settings.trigger_max_batches = 1000 settings.trigger_batch_interval = 1 settings.trigger_active = True + settings.verbosity = 1 # to test that this works even with no output # Tallies tallies = openmc.Tallies() @@ -88,6 +89,7 @@ class TriggerStatepointRestartTestHarness(PyAPITestHarness): # First non-restart run k_combined_1 = self._model.run(**args) sp_batchno_1 = 0 + assert self._model.statepoint with openmc.StatePoint(self._model.statepoint) as sp: sp_batchno_1 = sp.current_batch assert sp_batchno_1 > 10 @@ -102,6 +104,7 @@ class TriggerStatepointRestartTestHarness(PyAPITestHarness): args['restart_file'] = restart_spfile[0] k_combined_2 = self._model.run(**args) sp_batchno_2 = 0 + assert self._model.statepoint with openmc.StatePoint(self._model.statepoint) as sp: sp_batchno_2 = sp.current_batch assert sp_batchno_2 > 10