From cccca4062aea16d8894a2257173ce33fad0a25d3 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 25 Apr 2016 09:04:52 -0500 Subject: [PATCH] Goodbye openmc.Executor. Hello openmc.run and openmc.plot. --- openmc/executor.py | 177 ++++++++---------- tests/test_plot/test_plot.py | 5 +- .../test_statepoint_restart.py | 17 +- tests/testing_harness.py | 25 +-- 4 files changed, 92 insertions(+), 132 deletions(-) diff --git a/openmc/executor.py b/openmc/executor.py index 214517d6ed..89bcc2e10d 100644 --- a/openmc/executor.py +++ b/openmc/executor.py @@ -1,131 +1,100 @@ from __future__ import print_function import subprocess from numbers import Integral -import os import sys -from openmc.checkvalue import check_type - if sys.version_info[0] >= 3: basestring = str -class Executor(object): - """Control execution of OpenMC +def _run(command, output, cwd): + # Launch a subprocess + p = subprocess.Popen(command, shell=True, cwd=cwd, stdout=subprocess.PIPE, + universal_newlines=True) - Attributes + # Capture and re-print OpenMC output in real-time + while True: + # If OpenMC is finished, break loop + line = p.stdout.readline() + if not line and p.poll() != None: + break + + # If user requested output, print to screen + if output: + print(line, end='') + + # Return the returncode (integer, zero if no problems encountered) + return p.returncode + + +def plot(output=True, openmc_exec='openmc', cwd='.'): + """Run OpenMC in plotting mode + + Parameters ---------- - working_directory : str - Path to working directory to run in + output : bool + Capture OpenMC output from standard out + openmc_exec : str + Path to OpenMC executable + cwd : str, optional + Path to working directory to run in. Defaults to the current working directory. """ - def __init__(self): - self._working_directory = '.' + return _run(openmc_exec + ' -p', output, cwd) - def _run_openmc(self, command, output): - # Launch a subprocess to run OpenMC - p = subprocess.Popen(command, shell=True, - cwd=self._working_directory, - stdout=subprocess.PIPE, - universal_newlines=True) - # Capture and re-print OpenMC output in real-time - while True: - # If OpenMC is finished, break loop - line = p.stdout.readline() - if not line and p.poll() != None: - break +def run(particles=None, threads=None, geometry_debug=False, + restart_file=None, tracks=False, mpi_procs=1, output=True, + openmc_exec='openmc', mpi_exec='mpiexec', cwd='.'): + """Run an OpenMC simulation. - # If user requested output, print to screen - if output: - print(line, end='') + Parameters + ---------- + particles : int, optional + Number of particles to simulate per generation. + threads : int, optional + Number of OpenMP threads. + geometry_debug : bool, optional + Turn on geometry debugging during simulation. Defaults to False. + restart_file : str, optional + Path to restart file to use + tracks : bool, optional + Write tracks for all particles. Defaults to False. + mpi_procs : int, optional + Number of MPI processes. + output : bool, optional + Capture OpenMC output from standard out. Defaults to True. + openmc_exec : str, optional + Path to OpenMC executable. Defaults to 'openmc'. + mpi_exec : str, optional + MPI execute command. Defaults to 'mpiexec'. + cwd : str, optional + Path to working directory to run in. Defaults to the current working directory. - # Return the returncode (integer, zero if no problems encountered) - return p.returncode + """ - @property - def working_directory(self): - return self._working_directory + post_args = ' ' + pre_args = '' - @working_directory.setter - def working_directory(self, working_directory): - check_type("Executor's working directory", working_directory, - basestring) - if not os.path.isdir(working_directory): - msg = 'Unable to set Executor\'s working directory to "{0}" ' \ - 'which does not exist'.format(working_directory) - raise ValueError(msg) + if isinstance(particles, Integral) and particles > 0: + post_args += '-n {0} '.format(particles) - self._working_directory = working_directory + if isinstance(threads, Integral) and threads > 0: + post_args += '-s {0} '.format(threads) - def plot_geometry(self, output=True, openmc_exec='openmc'): - """Run OpenMC in plotting mode""" + if geometry_debug: + post_args += '-g ' - return self._run_openmc(openmc_exec + ' -p', output) + if isinstance(restart_file, basestring): + post_args += '-r {0} '.format(restart_file) - def run_simulation(self, particles=None, threads=None, - geometry_debug=False, restart_file=None, - tracks=False, mpi_procs=1, output=True, - openmc_exec='openmc', mpi_exec=None): - """Run an OpenMC simulation. + if tracks: + post_args += '-t' - Parameters - ---------- - particles : int - Number of particles to simulate per generation - threads : int - Number of OpenMP threads - geometry_debug : bool - Turn on geometry debugging during simulation - restart_file : str - Path to restart file to use - tracks : bool - Write tracks for all particles - mpi_procs : int - Number of MPI processes - output : bool - Capture OpenMC output from standard out - openmc_exec : str - Path to OpenMC executable + if isinstance(mpi_procs, Integral) and mpi_procs > 1: + pre_args += '{} -n {} '.format(mpi_exec, mpi_procs) - """ + command = pre_args + openmc_exec + ' ' + post_args - post_args = ' ' - pre_args = '' - - if isinstance(particles, Integral) and particles > 0: - post_args += '-n {0} '.format(particles) - - if isinstance(threads, Integral) and threads > 0: - post_args += '-s {0} '.format(threads) - - if geometry_debug: - post_args += '-g ' - - if isinstance(restart_file, basestring): - post_args += '-r {0} '.format(restart_file) - - if tracks: - post_args += '-t' - - if isinstance(mpi_procs, Integral) and mpi_procs > 1: - np_present = True - else: - np_present = False - - if mpi_exec is not None and isinstance(mpi_exec, basestring): - mpi_exec_present = True - else: - mpi_exec_present = False - - if np_present or mpi_exec_present: - if mpi_exec_present: - pre_args += mpi_exec + ' ' - else: - pre_args += 'mpirun ' - pre_args += '-n {0} '.format(mpi_procs) - - command = pre_args + openmc_exec + ' ' + post_args - - return self._run_openmc(command, output) + return _run(command, output, cwd) diff --git a/tests/test_plot/test_plot.py b/tests/test_plot/test_plot.py index 015577d215..e40cef49c0 100644 --- a/tests/test_plot/test_plot.py +++ b/tests/test_plot/test_plot.py @@ -9,7 +9,7 @@ from testing_harness import TestHarness import h5py -from openmc import Executor +import openmc class PlotTestHarness(TestHarness): @@ -19,8 +19,7 @@ class PlotTestHarness(TestHarness): self._plot_names = plot_names def _run_openmc(self): - executor = Executor() - returncode = executor.plot_geometry(openmc_exec=self._opts.exe) + returncode = openmc.plot(openmc_exec=self._opts.exe) assert returncode == 0, 'OpenMC did not exit successfully.' def _test_output_created(self): diff --git a/tests/test_statepoint_restart/test_statepoint_restart.py b/tests/test_statepoint_restart/test_statepoint_restart.py index c842689d99..d39bf7cd5f 100644 --- a/tests/test_statepoint_restart/test_statepoint_restart.py +++ b/tests/test_statepoint_restart/test_statepoint_restart.py @@ -5,8 +5,7 @@ import os import sys sys.path.insert(0, os.pardir) from testing_harness import TestHarness -from openmc.statepoint import StatePoint -from openmc.executor import Executor +import openmc class StatepointRestartTestHarness(TestHarness): @@ -50,17 +49,15 @@ class StatepointRestartTestHarness(TestHarness): statepoint = statepoint[0] # Run OpenMC - executor = Executor() - if self._opts.mpi_exec is not None: - returncode = executor.run_simulation(mpi_procs=self._opts.mpi_np, - restart_file=statepoint, - openmc_exec=self._opts.exe, - mpi_exec=self._opts.mpi_exec) + returncode = openmc.run(mpi_procs=self._opts.mpi_np, + restart_file=statepoint, + openmc_exec=self._opts.exe, + mpi_exec=self._opts.mpi_exec) else: - returncode = executor.run_simulation(openmc_exec=self._opts.exe, - restart_file=statepoint) + returncode = openmc.run(openmc_exec=self._opts.exe, + restart_file=statepoint) assert returncode == 0, 'OpenMC did not exit successfully.' diff --git a/tests/testing_harness.py b/tests/testing_harness.py index 7d6dbc914f..78e5553e8c 100644 --- a/tests/testing_harness.py +++ b/tests/testing_harness.py @@ -13,9 +13,7 @@ import numpy as np sys.path.insert(0, os.path.join(os.pardir, os.pardir)) from input_set import InputSet, MGInputSet -from openmc.statepoint import StatePoint -from openmc.executor import Executor -import openmc.particle_restart as pr +import openmc class TestHarness(object): @@ -63,15 +61,13 @@ class TestHarness(object): self._cleanup() def _run_openmc(self): - executor = Executor() - if self._opts.mpi_exec is not None: - returncode = executor.run_simulation(mpi_procs=self._opts.mpi_np, - openmc_exec=self._opts.exe, - mpi_exec=self._opts.mpi_exec) + returncode = openmc.run(mpi_procs=self._opts.mpi_np, + openmc_exec=self._opts.exe, + mpi_exec=self._opts.mpi_exec) else: - returncode = executor.run_simulation(openmc_exec=self._opts.exe) + returncode = openmc.run(openmc_exec=self._opts.exe) assert returncode == 0, 'OpenMC did not exit successfully.' @@ -90,7 +86,7 @@ class TestHarness(object): """Digest info in the statepoint and return as a string.""" # Read the statepoint file. statepoint = glob.glob(os.path.join(os.getcwd(), self._sp_name))[0] - sp = StatePoint(statepoint) + sp = openmc.StatePoint(statepoint) # Write out k-combined. outstr = 'k-combined:\n' @@ -158,7 +154,7 @@ class CMFDTestHarness(TestHarness): """Digest info in the statepoint and return as a string.""" # Read the statepoint file. statepoint = glob.glob(os.path.join(os.getcwd(), self._sp_name))[0] - sp = StatePoint(statepoint) + sp = openmc.StatePoint(statepoint) # Write out the eigenvalue and tallies. outstr = super(CMFDTestHarness, self)._get_results() @@ -195,13 +191,12 @@ class ParticleRestartTestHarness(TestHarness): 'mpi_exec': self._opts.mpi_exec}) # Initial run - executor = Executor() - returncode = executor.run_simulation(**args) + returncode = openmc.run(**args) assert returncode == 0, 'OpenMC did not exit successfully.' # Run particle restart args.update({'restart_file': self._sp_name}) - returncode = executor.run_simulation(**args) + returncode = openmc.run(**args) assert returncode == 0, 'OpenMC did not exit successfully.' def _test_output_created(self): @@ -216,7 +211,7 @@ class ParticleRestartTestHarness(TestHarness): """Digest info in the statepoint and return as a string.""" # Read the particle restart file. particle = glob.glob(os.path.join(os.getcwd(), self._sp_name))[0] - p = pr.Particle(particle) + p = openmc.Particle(particle) # Write out the properties. outstr = ''