From 1758c86a3f364f2ef3e96561199e5bb2846f5f51 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 6 Sep 2012 19:56:43 +0000 Subject: [PATCH] Starting a test suite for the new Python module --- interfaces/cython/cantera/test/__init__.py | 4 +++ .../cython/cantera/test/test_kinetics.py | 11 +++++++ .../cython/cantera/test/test_mixture.py | 20 +++++++++++++ interfaces/cython/cantera/test/test_thermo.py | 29 +++++++++++++++++++ .../cython/cantera/test/test_transport.py | 11 +++++++ interfaces/cython/cantera/test/utilities.py | 12 ++++++++ interfaces/cython/setup.py.in | 2 +- 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 interfaces/cython/cantera/test/__init__.py create mode 100644 interfaces/cython/cantera/test/test_kinetics.py create mode 100644 interfaces/cython/cantera/test/test_mixture.py create mode 100644 interfaces/cython/cantera/test/test_thermo.py create mode 100644 interfaces/cython/cantera/test/test_transport.py create mode 100644 interfaces/cython/cantera/test/utilities.py diff --git a/interfaces/cython/cantera/test/__init__.py b/interfaces/cython/cantera/test/__init__.py new file mode 100644 index 000000000..407f24ec7 --- /dev/null +++ b/interfaces/cython/cantera/test/__init__.py @@ -0,0 +1,4 @@ +from .test_thermo import * +from .test_kinetics import * +from .test_transport import * +from .test_mixture import * diff --git a/interfaces/cython/cantera/test/test_kinetics.py b/interfaces/cython/cantera/test/test_kinetics.py new file mode 100644 index 000000000..1b4b1c549 --- /dev/null +++ b/interfaces/cython/cantera/test/test_kinetics.py @@ -0,0 +1,11 @@ +import unittest + +import cantera as ct +from . import utilities + +class TestKinetics(utilities.CanteraTest): + def setUp(self): + self.phase = ct.Solution('h2o2.xml') + + def test_nReactions(self): + self.assertEqual(self.phase.nReactions, 27) diff --git a/interfaces/cython/cantera/test/test_mixture.py b/interfaces/cython/cantera/test/test_mixture.py new file mode 100644 index 000000000..893801d9f --- /dev/null +++ b/interfaces/cython/cantera/test/test_mixture.py @@ -0,0 +1,20 @@ +import unittest + +import cantera as ct +from . import utilities + +class TestMixture(utilities.CanteraTest): + @classmethod + def setUpClass(cls): + cls.phase1 = ct.Solution('h2o2.xml') + cls.phase2 = ct.Solution('air.xml') + + def test_properties(self): + mix = ct.Mixture([(self.phase1, 1.0), (self.phase2, 2.0)]) + self.assertEqual(mix.nSpecies, self.phase1.nSpecies + self.phase2.nSpecies) + + mix.temperature = 350 + self.assertEqual(mix.temperature, 350) + + mix.pressure = 2e5 + self.assertEqual(mix.pressure, 2e5) diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py new file mode 100644 index 000000000..7cb778ebf --- /dev/null +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -0,0 +1,29 @@ +import unittest +import numpy as np + +import cantera as ct +from . import utilities + +class TestThermoPhase(utilities.CanteraTest): + def setUp(self): + self.phase = ct.Solution('h2o2.xml') + + def test_nSpecies(self): + self.assertEqual(self.phase.nSpecies, 9) + + def test_setComposition(self): + X = np.zeros(self.phase.nSpecies) + X[2] = 1.0 + self.phase.setMoleFractions(X) + Y = self.phase.massFractions + + self.assertEqual(list(X), list(Y)) + + def test_badLength(self): + X = np.zeros(5) + with self.assertRaises(ValueError): + self.phase.setMoleFractions(X) + + def test_getState(self): + self.assertNear(self.phase.pressure, ct.OneAtm) + self.assertNear(self.phase.temperature, 300) diff --git a/interfaces/cython/cantera/test/test_transport.py b/interfaces/cython/cantera/test/test_transport.py new file mode 100644 index 000000000..0f5bad665 --- /dev/null +++ b/interfaces/cython/cantera/test/test_transport.py @@ -0,0 +1,11 @@ +import unittest + +import cantera as ct +from . import utilities + +class TestTransport(utilities.CanteraTest): + def setUp(self): + self.phase = ct.Solution('h2o2.xml') + + def test_viscosity(self): + self.assertTrue(self.phase.viscosity > 0.0) diff --git a/interfaces/cython/cantera/test/utilities.py b/interfaces/cython/cantera/test/utilities.py new file mode 100644 index 000000000..bb57488e6 --- /dev/null +++ b/interfaces/cython/cantera/test/utilities.py @@ -0,0 +1,12 @@ +import numpy as np +import unittest + +class CanteraTest(unittest.TestCase): + def assertNear(self, a, b, rtol=1e-8, atol=1e-12, msg=None): + cmp = 2 * abs(a - b)/(abs(a) + abs(b) + atol) + if cmp > rtol: + message = ('AssertNear: %.14g - %.14g = %.14g\n' % (a, b, a-b) + + 'Relative error of %10e exceeds rtol = %10e' % (cmp, rtol)) + if msg: + message = msg + '\n' + message + self.fail(message) diff --git a/interfaces/cython/setup.py.in b/interfaces/cython/setup.py.in index d8b3f7e28..331f2c37c 100644 --- a/interfaces/cython/setup.py.in +++ b/interfaces/cython/setup.py.in @@ -26,7 +26,7 @@ setup(name="Cantera", author="Raymond Speth", author_email="speth@mit.edu", url="http://code.google.com/p/cantera", - packages = ["cantera"], + packages = ["cantera", "cantera.test"], cmdclass = {'build_ext': build_ext}, ext_modules = exts, package_data = {'cantera': dataFiles})