Model.run now returns last statepoint path rather than k_eff. Updated tests and search.

This commit is contained in:
alex-lyons 2020-03-04 14:43:10 +00:00 committed by Paul Romano
parent 5a9fe17323
commit e98ddf9d8a
5 changed files with 25 additions and 26 deletions

View file

@ -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

View file

@ -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)

View file

@ -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 = {}

View file

@ -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()

View file

@ -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()