TestHarness adjustments for #402

This commit is contained in:
Sterling Harper 2015-06-28 00:06:33 -06:00
parent e05c1e4074
commit f13351f2ee
25 changed files with 201 additions and 210907 deletions

View file

@ -2,6 +2,7 @@ from __future__ import print_function
import filecmp
import glob
import hashlib
from optparse import OptionParser
import os
from subprocess import Popen, STDOUT, PIPE, call
@ -11,27 +12,29 @@ import numpy as np
sys.path.insert(0, '../..')
from openmc.statepoint import StatePoint
import openmc.particle_restart as pr
class TestHarness():
class TestHarness(object):
"""General class for running OpenMC regression tests."""
def __init__(self, statepoint_name, tallies_present=False):
self._sp_name = statepoint_name
self._tallies = tallies_present
self._opts = None
self._args = None
def execute_test(self):
"""Run OpenMC with the appropriate arguments and check the outputs."""
self._parse_args()
try:
self._run_openmc()
self._test_output_created()
self._get_results()
results = self._get_results()
self._write_results(results)
self._compare_results()
finally:
self._cleanup()
def _parse_args(self):
parser = OptionParser()
parser.add_option('--mpi_exec', dest='mpi_exec', default='')
@ -42,7 +45,6 @@ class TestHarness():
raise Exception('Must specify OpenMC executable from command line '
'with --exe.')
def _run_openmc(self):
if self._opts.mpi_exec != '':
proc = Popen([self._opts.mpi_exec, '-np', self._opts.mpi_np,
@ -55,7 +57,6 @@ class TestHarness():
returncode = proc.returncode
assert returncode == 0, 'OpenMC did not exit successfully.'
def _test_output_created(self):
"""Make sure statepoint.* and tallies.out have been created."""
statepoint = glob.glob(os.path.join(os.getcwd(), self._sp_name))
@ -68,9 +69,8 @@ class TestHarness():
assert os.path.exists(os.path.join(os.getcwd(), 'tallies.out')), \
'Tally output file does not exist.'
def _get_results(self):
"""Digest info in the statepoint and create a simpler ASCII file."""
def _get_results(self, hash_output=False):
"""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)
@ -95,10 +95,18 @@ class TestHarness():
outstr += '\n'.join(results) + '\n'
tally_num += 1
# Write results to a file.
with open('results_test.dat','w') as fh:
fh.write(outstr)
# Hash the results if necessary.
if hash_output:
sha512 = hashlib.sha512()
sha512.update(outstr.encode('utf-8'))
outstr = sha512.hexdigest()
return outstr
def _write_results(self, results_string):
"""Write the results to an ASCII file."""
with open('results_test.dat','w') as fh:
fh.write(results_string)
def _compare_results(self):
compare = filecmp.cmp('results_test.dat', 'results_true.dat')
@ -106,7 +114,6 @@ class TestHarness():
os.rename('results_test.dat', 'results_error.dat')
assert compare, 'Results do not agree.'
def _cleanup(self):
output = glob.glob(os.path.join(os.getcwd(), 'statepoint.*.*'))
output.append(os.path.join(os.getcwd(), 'tallies.out'))
@ -114,3 +121,121 @@ class TestHarness():
for f in output:
if os.path.exists(f):
os.remove(f)
class HashedTestHarness(TestHarness):
"""Specialized TestHarness that hashes the results."""
def _get_results(self):
"""Digest info in the statepoint and return as a string."""
return TestHarness._get_results(self, True)
class PlotTestHarness(TestHarness):
"""Specialized TestHarness for running OpenMC plotting tests."""
def execute_test(self):
"""Run OpenMC with the appropriate arguments and check the outputs."""
self._parse_args()
try:
self._run_openmc()
self._test_output_created()
finally:
self._cleanup()
def _run_openmc(self):
if self._opts.mpi_exec != '':
proc = Popen([self._opts.mpi_exec, '-np', self._opts.mpi_np,
self._opts.exe, '-p', os.getcwd()],
stderr=STDOUT, stdout=PIPE)
else:
proc = Popen([self._opts.exe, '-p', os.getcwd()],
stderr=STDOUT, stdout=PIPE)
print(proc.communicate()[0])
returncode = proc.returncode
assert returncode == 0, 'OpenMC did not exit successfully.'
def _test_output_created(self):
"""Make sure *.ppm has been created."""
assert os.path.exists(os.path.join(os.getcwd(), '1_plot.ppm')), \
'Plot output file does not exist.'
def _cleanup(self):
TestHarness._cleanup(self)
output = glob.glob(os.path.join(os.getcwd(), '*.ppm'))
for f in output:
if os.path.exists(f):
os.remove(f)
class CMFDTestHarness(TestHarness):
"""Specialized TestHarness for running OpenMC CMFD tests."""
def _get_results(self):
"""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.read_results()
# Write out the eigenvalue and tallies.
outstr = TestHarness._get_results(self)
# Write out CMFD data.
outstr += 'cmfd indices\n'
outstr += '\n'.join(['{0:12.6E}'.format(x) for x in sp._cmfd_indices])
outstr += '\nk cmfd\n'
outstr += '\n'.join(['{0:12.6E}'.format(x) for x in sp._k_cmfd])
outstr += '\ncmfd entropy\n'
outstr += '\n'.join(['{0:12.6E}'.format(x) for x in sp._cmfd_entropy])
outstr += '\ncmfd balance\n'
outstr += '\n'.join(['{0:12.6E}'.format(x) for x in sp._cmfd_balance])
outstr += '\ncmfd dominance ratio\n'
outstr += '\n'.join(['{0:10.3E}'.format(x) for x in sp._cmfd_dominance])
outstr += '\ncmfd openmc source comparison\n'
outstr += '\n'.join(['{0:12.6E}'.format(x) for x in sp._cmfd_srccmp])
outstr += '\ncmfd source\n'
cmfdsrc = np.reshape(sp._cmfd_src, np.product(sp._cmfd_indices),
order='F')
outstr += '\n'.join(['{0:12.6E}'.format(x) for x in cmfdsrc])
outstr += '\n'
return outstr
class ParticleRestartTestHarness(TestHarness):
"""Specialized TestHarness for running OpenMC particle restart tests."""
def _test_output_created(self):
"""Make sure the restart file has been created."""
particle = glob.glob(os.path.join(os.getcwd(), self._sp_name))
assert len(particle) == 1, 'Either multiple or no particle restart ' \
'files exist.'
assert particle[0].endswith('binary') \
or particle[0].endswith('h5'), \
'Particle restart file is not a binary or hdf5 file.'
def _get_results(self):
"""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)
# Write out the properties.
outstr = ''
outstr += 'current batch:\n'
outstr += "{0:12.6E}\n".format(p.current_batch)
outstr += 'current gen:\n'
outstr += "{0:12.6E}\n".format(p.current_gen)
outstr += 'particle id:\n'
outstr += "{0:12.6E}\n".format(p.id)
outstr += 'run mode:\n'
outstr += "{0:12.6E}\n".format(p.run_mode)
outstr += 'particle weight:\n'
outstr += "{0:12.6E}\n".format(p.weight)
outstr += 'particle energy:\n'
outstr += "{0:12.6E}\n".format(p.energy)
outstr += 'particle xyz:\n'
outstr += "{0:12.6E} {1:12.6E} {2:12.6E}\n".format(p.xyz[0],p.xyz[1],
p.xyz[2])
outstr += 'particle uvw:\n'
outstr += "{0:12.6E} {1:12.6E} {2:12.6E}\n".format(p.uvw[0],p.uvw[1],
p.uvw[2])
return outstr