Added a few basic thermo tests to the Matlab unit tests

This commit is contained in:
Ray Speth 2012-03-23 22:16:28 +00:00
parent 8ea4ae728a
commit 9124b361dc
7 changed files with 178 additions and 2 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,35 @@
<?xml version="1.0"?>
<ctml>
<validate reactions="yes" species="yes"/>
<!-- phase simple -->
<phase dim="3" id="simple">
<elementArray datasrc="elements.xml">C O H N</elementArray>
<speciesArray datasrc="nasa_gas.xml#species_data">CH4 O2 N2 CO2 H2O</speciesArray>
<thermo model="IdealGas"/>
<kinetics model="GasKinetics"/>
<transport model="None"/>
</phase>
<!-- phase full -->
<phase dim="3" id="full">
<elementArray datasrc="elements.xml">C O H N</elementArray>
<speciesArray datasrc="nasa_gas.xml#species_data">CH4 H2O O2 CO2 CO H2 N2</speciesArray>
<thermo model="IdealGas"/>
<kinetics model="GasKinetics"/>
<transport model="None"/>
</phase>
<!-- phase syngas -->
<phase dim="3" id="syngas">
<elementArray datasrc="elements.xml">C O H N</elementArray>
<speciesArray datasrc="nasa_gas.xml#species_data">CO H2 O2 N2 CO2 H2O</speciesArray>
<thermo model="IdealGas"/>
<kinetics model="GasKinetics"/>
<transport model="None"/>
</phase>
<!-- species definitions -->
<speciesData id="species_data"/>
<reactionData id="reaction_data"/>
</ctml>

72
test/matlab/TestThermo.m Normal file
View file

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

View file

@ -14,3 +14,6 @@ end
% unload the MEX file
clear ctmethods
% delete global objects created by some of the test
clear global static*