Merge pull request #958 from paulromano/calledprocesserror

Raise CalledProcessError if openmc.run fails
This commit is contained in:
Sterling Harper 2018-01-24 14:52:16 -05:00 committed by GitHub
commit 277c720f54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 40 deletions

View file

@ -15,18 +15,22 @@ def _run(args, output, cwd):
stderr=subprocess.STDOUT, universal_newlines=True)
# Capture and re-print OpenMC output in real-time
lines = []
while True:
# If OpenMC is finished, break loop
line = p.stdout.readline()
if not line and p.poll() is not None:
break
lines.append(line)
if output:
# If user requested output, print to screen
print(line, end='')
# Return the returncode (integer, zero if no problems encountered)
return p.returncode
# Raise an exception if return status is non-zero
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, ' '.join(args),
''.join(lines))
def plot_geometry(output=True, openmc_exec='openmc', cwd='.'):
@ -41,8 +45,13 @@ def plot_geometry(output=True, openmc_exec='openmc', cwd='.'):
cwd : str, optional
Path to working directory to run in
Raises
------
subprocess.CalledProcessError
If the `openmc` executable returns a non-zero status
"""
return _run([openmc_exec, '-p'], output, cwd)
_run([openmc_exec, '-p'], output, cwd)
def plot_inline(plots, openmc_exec='openmc', cwd='.', convert_exec='convert'):
@ -63,6 +72,11 @@ def plot_inline(plots, openmc_exec='openmc', cwd='.', convert_exec='convert'):
convert_exec : str, optional
Command that can convert PPM files into PNG files
Raises
------
subprocess.CalledProcessError
If the `openmc` executable returns a non-zero status
"""
from IPython.display import Image, display
@ -121,6 +135,11 @@ def calculate_volumes(threads=None, output=True, cwd='.',
Path to working directory to run in. Defaults to the current working
directory.
Raises
------
subprocess.CalledProcessError
If the `openmc` executable returns a non-zero status
See Also
--------
openmc.VolumeCalculation
@ -133,7 +152,7 @@ def calculate_volumes(threads=None, output=True, cwd='.',
if mpi_args is not None:
args = mpi_args + args
return _run(args, output, cwd)
_run(args, output, cwd)
def run(particles=None, threads=None, geometry_debug=False,
@ -167,8 +186,12 @@ def run(particles=None, threads=None, geometry_debug=False,
MPI execute command and any additional MPI arguments to pass,
e.g. ['mpiexec', '-n', '8'].
"""
Raises
------
subprocess.CalledProcessError
If the `openmc` executable returns a non-zero status
"""
args = [openmc_exec]
if isinstance(particles, Integral) and particles > 0:
@ -189,4 +212,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)

View file

@ -139,13 +139,10 @@ class MGXSTestHarness(PyAPITestHarness):
if self._opts.mpi_exec is not None:
mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np]
returncode = openmc.run(openmc_exec=self._opts.exe,
mpi_args=mpi_args)
openmc.run(openmc_exec=self._opts.exe, mpi_args=mpi_args)
else:
returncode = openmc.run(openmc_exec=self._opts.exe)
assert returncode == 0, 'OpenMC did not exit successfully.'
openmc.run(openmc_exec=self._opts.exe)
sp = openmc.StatePoint('statepoint.' + str(batches) + '.h5')

View file

@ -36,13 +36,9 @@ class MGXSTestHarness(PyAPITestHarness):
# Initial run
if self._opts.mpi_exec is not None:
mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np]
returncode = openmc.run(openmc_exec=self._opts.exe,
mpi_args=mpi_args)
openmc.run(openmc_exec=self._opts.exe, mpi_args=mpi_args)
else:
returncode = openmc.run(openmc_exec=self._opts.exe)
assert returncode == 0, 'CE OpenMC calculation did not exit' \
'successfully.'
openmc.run(openmc_exec=self._opts.exe)
# Build MG Inputs
# Get data needed to execute Library calculations.
@ -73,10 +69,9 @@ class MGXSTestHarness(PyAPITestHarness):
# Re-run MG mode.
if self._opts.mpi_exec is not None:
mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np]
returncode = openmc.run(openmc_exec=self._opts.exe,
mpi_args=mpi_args)
openmc.run(openmc_exec=self._opts.exe, mpi_args=mpi_args)
else:
returncode = openmc.run(openmc_exec=self._opts.exe)
openmc.run(openmc_exec=self._opts.exe)
def _cleanup(self):
super(MGXSTestHarness, self)._cleanup()

View file

@ -19,8 +19,7 @@ class PlotTestHarness(TestHarness):
self._plot_names = plot_names
def _run_openmc(self):
returncode = openmc.plot_geometry(openmc_exec=self._opts.exe)
assert returncode == 0, 'OpenMC did not exit successfully.'
openmc.plot_geometry(openmc_exec=self._opts.exe)
def _test_output_created(self):
"""Make sure *.ppm has been created."""

View file

@ -50,14 +50,10 @@ class StatepointRestartTestHarness(TestHarness):
# Run OpenMC
if self._opts.mpi_exec is not None:
mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np]
returncode = openmc.run(restart_file=statepoint,
openmc_exec=self._opts.exe,
mpi_args=mpi_args)
openmc.run(restart_file=statepoint, openmc_exec=self._opts.exe,
mpi_args=mpi_args)
else:
returncode = openmc.run(openmc_exec=self._opts.exe,
restart_file=statepoint)
assert returncode == 0, 'OpenMC did not exit successfully.'
openmc.run(openmc_exec=self._opts.exe, restart_file=statepoint)
if __name__ == '__main__':

View file

@ -62,14 +62,10 @@ class TestHarness(object):
def _run_openmc(self):
if self._opts.mpi_exec is not None:
returncode = openmc.run(
openmc_exec=self._opts.exe,
mpi_args=[self._opts.mpi_exec, '-n', self._opts.mpi_np])
openmc.run(openmc_exec=self._opts.exe,
mpi_args=[self._opts.mpi_exec, '-n', self._opts.mpi_np])
else:
returncode = openmc.run(openmc_exec=self._opts.exe)
assert returncode == 0, 'OpenMC did not exit successfully.'
openmc.run(openmc_exec=self._opts.exe)
def _test_output_created(self):
"""Make sure statepoint.* and tallies.out have been created."""
@ -189,13 +185,11 @@ class ParticleRestartTestHarness(TestHarness):
args['mpi_args'] = [self._opts.mpi_exec, '-n', self._opts.mpi_np]
# Initial run
returncode = openmc.run(**args)
assert returncode == 0, 'OpenMC did not exit successfully.'
openmc.run(**args)
# Run particle restart
args.update({'restart_file': self._sp_name})
returncode = openmc.run(**args)
assert returncode == 0, 'OpenMC did not exit successfully.'
openmc.run(**args)
def _test_output_created(self):
"""Make sure the restart file has been created."""