From 9124b361dce63a605e0f5cf7c047c2a11f08d632 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 23 Mar 2012 22:16:28 +0000 Subject: [PATCH] Added a few basic thermo tests to the Matlab unit tests --- ext/matlab_xunit/VerboseTestRunDisplay.m | 2 +- ext/matlab_xunit/assertAlmostEqual.m | 52 +++++++++++++++++ test/SConscript | 5 +- test/data/steam-reforming.cti | 11 ++++ test/data/steam-reforming.xml | 35 ++++++++++++ test/matlab/TestThermo.m | 72 ++++++++++++++++++++++++ test/matlab/runCanteraTests.m | 3 + 7 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 ext/matlab_xunit/assertAlmostEqual.m create mode 100644 test/data/steam-reforming.cti create mode 100644 test/data/steam-reforming.xml create mode 100644 test/matlab/TestThermo.m diff --git a/ext/matlab_xunit/VerboseTestRunDisplay.m b/ext/matlab_xunit/VerboseTestRunDisplay.m index 18e85b1d3..0be9d6df1 100644 --- a/ext/matlab_xunit/VerboseTestRunDisplay.m +++ b/ext/matlab_xunit/VerboseTestRunDisplay.m @@ -103,7 +103,7 @@ classdef VerboseTestRunDisplay < TestRunDisplay end function str = leaderDots(self, name) - num_dots = max(0, 60 - self.numIndentationSpaces() - numel(name)); + num_dots = max(0, 40 - self.numIndentationSpaces() - numel(name)); str = repmat('.', 1, num_dots); end diff --git a/ext/matlab_xunit/assertAlmostEqual.m b/ext/matlab_xunit/assertAlmostEqual.m new file mode 100644 index 000000000..63e195c79 --- /dev/null +++ b/ext/matlab_xunit/assertAlmostEqual.m @@ -0,0 +1,52 @@ +function assertAlmostEqual(A, B, reltol, message) +%assertEqual Assert that inputs are equal within relative tolerance +% assertEqual(A, B, RELTOL) throws an exception of any of the values in A and +% B are not equal within the specified tolerance. NaN values are considered +% to be equal. A and B have to have the same class and sparsity to be +% considered equal. +% +% assertEqual(A, B) uses the following relative tolerance value: +% +% 100 * eps(class(A)) +% +% assertEqual(A, B, RELTOL, MESSAGE) uses the specified message string when +% throwing the exception. With this syntax, use RELTOL = [] to specify the +% default relative tolerance. +% +% Note that if either A or B are not floating-point arrays, then A and B are +% compared using ISEQUALWITHEQUALNANS and the relative tolerance value is not +% used. +% +% Examples +% -------- +% % This call returns silently. +% assertAlmostEqual(1.0, 1.0 + eps); +% +% % This call throws an error. +% assertAlmostEqual(1.0, 1.1); +% +% See also assertEqual, mtest.utils.isAlmostEqual + +% Steven L. Eddins +% Copyright 2008-2009 The MathWorks, Inc. + +if ~(issparse(A) == issparse(B)) + throw(MException('assertAlmostEqual:sparsityNotEqual', message)); +end + +if ~strcmp(class(A), class(B)) + throw(MException('assertAlmostEqual:classNotEqual', message)); +end + +if nargin < 3 || isempty(reltol) + reltol = 100 * eps(class(A)); +end + +if nargin < 4 + message = sprintf('Inputs are not equal within relative tolerance: %g', ... + reltol); +end + +if ~xunit.utils.isAlmostEqual(A, B, reltol) + throw(MException('assertAlmostEqual:tolExceeded', message)); +end diff --git a/test/SConscript b/test/SConscript index 264976a13..d026a6771 100644 --- a/test/SConscript +++ b/test/SConscript @@ -81,9 +81,12 @@ def addMatlabTest(script, dependencies=None): matlabOptions = ['-nojvm','-nodisplay'] if os.path.exists(outfile): os.remove(outfile) + + environ = dict(os.environ) + environ.update(env['ENV']) code = subprocess.call([pjoin(env['matlab_path'], 'bin', 'matlab')] + matlabOptions + ['-r', runCommand], - env=env['ENV'], cwd=Dir('#test/matlab').abspath) + env=environ, cwd=Dir('#test/matlab').abspath) results = open(outfile).read() print '-------- Matlab test results --------' print results diff --git a/test/data/steam-reforming.cti b/test/data/steam-reforming.cti new file mode 100644 index 000000000..87ec86893 --- /dev/null +++ b/test/data/steam-reforming.cti @@ -0,0 +1,11 @@ +ideal_gas(name = 'simple', + elements = 'C O H N', + species = 'nasa_gas: CH4 O2 N2 CO2 H2O') + +ideal_gas(name = 'full', + elements = 'C O H N', + species = 'nasa_gas: CH4 H2O O2 CO2 CO H2 N2') + +ideal_gas(name = 'syngas', + elements = 'C O H N', + species = 'nasa_gas: CO H2 O2 N2 CO2 H2O') diff --git a/test/data/steam-reforming.xml b/test/data/steam-reforming.xml new file mode 100644 index 000000000..5bb1e56a9 --- /dev/null +++ b/test/data/steam-reforming.xml @@ -0,0 +1,35 @@ + + + + + + + C O H N + CH4 O2 N2 CO2 H2O + + + + + + + + C O H N + CH4 H2O O2 CO2 CO H2 N2 + + + + + + + + C O H N + CO H2 O2 N2 CO2 H2O + + + + + + + + + diff --git a/test/matlab/TestThermo.m b/test/matlab/TestThermo.m new file mode 100644 index 000000000..1f1fdeccc --- /dev/null +++ b/test/matlab/TestThermo.m @@ -0,0 +1,72 @@ +classdef TestThermo < TestCase + properties + gas + end + + methods + function self = TestThermo(name) + self = self@TestCase(name); + end + + function setUp(self) + global staticTestThermoGas + if isempty(staticTestThermoGas) + staticTestThermoGas = importPhase('../data/steam-reforming.xml', 'full'); + end + self.gas = staticTestThermoGas; + set(self.gas, 'T', 300, 'P', oneatm, 'Y', [0.5, 0, 0.5, 0, 0, 0, 0]); + end + +% function tearDown(self) +% end + + function testCounts(self) + assertEqual(nElements(self.gas), 4) + assertEqual(nSpecies(self.gas), 7) + end + + function testElements(self) + for i = 1:nElements(self.gas) + name = elementName(self.gas, i); + assertEqual(i, elementIndex(self.gas, name)) + end + end + + function testSpecies(self) + for i = 1:nSpecies(self.gas) + name = speciesName(self.gas, i); + assertEqual(i, speciesIndex(self.gas, name)) + end + end + + function test_nAtoms(self) + assertEqual(nAtoms(self.gas, 1, 3), 4) + assertEqual(nAtoms(self.gas, 1, 4), 0) + assertEqual(nAtoms(self.gas, 3, 2), 2) + assertExceptionThrown(@() nAtoms(self.gas, 2, 5), '') + assertExceptionThrown(@() nAtoms(self.gas, 8, 1), '') + end + + function testSetState(self) + u0 = intEnergy_mass(self.gas); + h0 = enthalpy_mass(self.gas); + s0 = entropy_mass(self.gas); + v0 = 1/density(self.gas); + T0 = temperature(self.gas); + P0 = pressure(self.gas); + + set(self.gas, 'T', 400, 'P', 5*oneatm); + assertAlmostEqual(temperature(self.gas), 400) + assertAlmostEqual(pressure(self.gas), 5*oneatm) + + set(self.gas, 'H', h0, 'P', P0); + assertAlmostEqual(temperature(self.gas), T0, 1e-8) + assertAlmostEqual(entropy_mass(self.gas), s0, 1e-8) + + set(self.gas, 'T', 400, 'P', 5*oneatm); + set(self.gas, 'U', u0, 'V', v0); + assertAlmostEqual(pressure(self.gas), P0, 1e-8) + assertAlmostEqual(enthalpy_mass(self.gas), h0, 1e-8) + end + end +end diff --git a/test/matlab/runCanteraTests.m b/test/matlab/runCanteraTests.m index 7816f021a..a542b8292 100644 --- a/test/matlab/runCanteraTests.m +++ b/test/matlab/runCanteraTests.m @@ -14,3 +14,6 @@ end % unload the MEX file clear ctmethods + +% delete global objects created by some of the test +clear global static*