Skip to content

Commit

Permalink
git commit -m "[Cython/Test] Make cython testing portable.
Browse files Browse the repository at this point in the history
Rather than changing to the work directory, ensure all input/output files come from
the the in-package test/data directory. This makes the Cython test suite standalone
and the tests can be run using unittest as: python -m unittest -v cantera.test
  • Loading branch information
bryanwweber committed Oct 26, 2016
1 parent 2264093 commit c673293
Show file tree
Hide file tree
Showing 13 changed files with 1,545 additions and 179 deletions.
3 changes: 2 additions & 1 deletion interfaces/cython/cantera/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import cantera

cantera.add_directory(os.path.join(os.path.dirname(__file__), 'data'))
cantera.add_directory(os.path.join(os.path.dirname(__file__), '..', 'examples', 'surface_chemistry'))

from .test_thermo import *
from .test_purefluid import *
Expand All @@ -12,4 +13,4 @@
from .test_func1 import *
from .test_reactor import *
from .test_onedim import *
from .test_convert import *
from .test_convert import *
288 changes: 159 additions & 129 deletions interfaces/cython/cantera/test/test_convert.py

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions interfaces/cython/cantera/test/test_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import unittest
import os
import warnings
from os.path import join as pjoin

import numpy as np

import cantera as ct
from . import utilities


class EquilTestCases(object):
def __init__(self, solver):
self.solver = solver
Expand Down Expand Up @@ -158,7 +159,8 @@ def test_equil_TP(self):

data[i,1:] = self.mix.species_moles

self.compare(data, '../data/koh-equil-TP.csv')
p = self.get_test_data_directory()
self.compare(data, pjoin(p, 'koh-equil-TP.csv'))

def test_equil_HP(self):
temperatures = range(350, 5000, 300)
Expand All @@ -181,7 +183,8 @@ def test_equil_HP(self):
data[i,1] = self.mix.T # equilibrated temperature
data[i,2:] = self.mix.species_moles

self.compare(data, '../data/koh-equil-HP.csv')
p = self.get_test_data_directory()
self.compare(data, pjoin(p, 'koh-equil-HP.csv'))


class TestEquil_GasCarbon(utilities.CanteraTest):
Expand Down Expand Up @@ -212,7 +215,8 @@ def solve(self, solver, **kwargs):
data[i,:2] = (phi[i], mix.T)
data[i,2:] = mix.species_moles

self.compare(data, '../data/gas-carbon-equil.csv')
p = self.get_test_data_directory()
self.compare(data, pjoin(p, 'gas-carbon-equil.csv'))

def test_gibbs(self):
self.solve('gibbs')
Expand Down
22 changes: 15 additions & 7 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import unittest
# import unittest
import numpy as np
import re
import itertools
from os.path import join as pjoin
import os

import cantera as ct
from . import utilities


class TestKinetics(utilities.CanteraTest):
def setUp(self):
self.phase = ct.Solution('h2o2.xml')
Expand Down Expand Up @@ -607,7 +610,8 @@ def cathode_curr(E):
cathode_bulk.electric_potential -
anode_bulk.electric_potential])

self.compare(data, '../data/sofc-test.csv')
p = self.get_test_data_directory()
self.compare(data, pjoin(p, 'sofc-test.csv'))


class TestDuplicateReactions(utilities.CanteraTest):
Expand Down Expand Up @@ -673,7 +677,8 @@ def test_fromCti(self):

def test_fromXml(self):
import xml.etree.ElementTree as ET
root = ET.parse('../../build/data/h2o2.xml').getroot()
p = os.path.dirname(__file__)
root = ET.parse(pjoin(p, '..', 'data', 'h2o2.xml')).getroot()
rxn_node = root.find('.//reaction[@id="0001"]')
r = ct.Reaction.fromXml(ET.tostring(rxn_node))

Expand All @@ -690,14 +695,16 @@ def test_listFromFile(self):
self.assertEqual(eq1, eq2)

def test_listFromCti(self):
with open('../../build/data/h2o2.cti') as f:
p = os.path.dirname(__file__)
with open(pjoin(p, '..', 'data', 'h2o2.cti')) as f:
R = ct.Reaction.listFromCti(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in self.gas.reactions()]
self.assertEqual(eq1, eq2)

def test_listFromXml(self):
with open('../../build/data/h2o2.xml') as f:
p = os.path.dirname(__file__)
with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f:
R = ct.Reaction.listFromCti(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in self.gas.reactions()]
Expand Down Expand Up @@ -997,15 +1004,16 @@ def test_modify_sticking(self):
self.assertNear(k1, 4*k2)

def test_motz_wise(self):
p = self.get_test_data_directory()
# Motz & Wise off for all reactions
gas1 = ct.Solution('ptcombust.xml', 'gas')
surf1 = ct.Interface('ptcombust.xml', 'Pt_surf', [gas1])
surf1.coverages = 'O(S):0.1, PT(S):0.5, H(S):0.4'
gas1.TP = surf1.TP

# Motz & Wise correction on for some reactions
gas2 = ct.Solution('../data/ptcombust-motzwise.cti', 'gas')
surf2 = ct.Interface('../data/ptcombust-motzwise.cti', 'Pt_surf', [gas2])
gas2 = ct.Solution(pjoin(p, 'ptcombust-motzwise.cti'), 'gas')
surf2 = ct.Interface(pjoin(p, 'ptcombust-motzwise.cti'), 'Pt_surf', [gas2])
surf2.TPY = surf1.TPY

k1 = surf1.forward_rate_constants
Expand Down
35 changes: 23 additions & 12 deletions interfaces/cython/cantera/test/test_onedim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import utilities
import numpy as np
import os
from os.path import join as pjoin


class TestOnedim(utilities.CanteraTest):
Expand Down Expand Up @@ -303,13 +304,14 @@ def test_prune(self):
# residual satisfies the error tolerances) on the new grid.

def test_save_restore(self):
test_data_dir = self.get_test_data_directory()
reactants= 'H2:1.1, O2:1, AR:5'
p = 2 * ct.one_atm
Tin = 400

self.create_sim(p, Tin, reactants)
self.solve_fixed_T()
filename = 'onedim-fixed-T{0}.xml'.format(utilities.python_version)
filename = pjoin(test_data_dir, 'onedim-fixed-T{0}.xml'.format(utilities.python_version))
if os.path.exists(filename):
os.remove(filename)

Expand Down Expand Up @@ -370,7 +372,8 @@ def test_save_restore_add_species(self):
p = 2 * ct.one_atm
Tin = 400

filename = 'onedim-add-species{0}.xml'.format(utilities.python_version)
test_data_dir = self.get_test_data_directory()
filename = pjoin(test_data_dir, 'onedim-add-species{0}.xml'.format(utilities.python_version))
if os.path.exists(filename):
os.remove(filename)

Expand Down Expand Up @@ -398,7 +401,8 @@ def test_save_restore_remove_species(self):
p = 2 * ct.one_atm
Tin = 400

filename = 'onedim-add-species{0}.xml'.format(utilities.python_version)
test_data_dir = self.get_test_data_directory()
filename = pjoin(test_data_dir, 'onedim-add-species{0}.xml'.format(utilities.python_version))
if os.path.exists(filename):
os.remove(filename)

Expand All @@ -422,7 +426,8 @@ def test_save_restore_remove_species(self):
self.assertArrayNear(Y1[k1], Y2[k2])

def test_write_csv(self):
filename = 'onedim-write_csv{0}.csv'.format(utilities.python_version)
p = self.get_test_data_directory()
filename = pjoin(p, 'onedim-write_csv{0}.csv'.format(utilities.python_version))
if os.path.exists(filename):
os.remove(filename)

Expand Down Expand Up @@ -509,7 +514,8 @@ def solve_mix(self, ratio=3.0, slope=0.1, curve=0.12, prune=0.0):
self.assertEqual(self.sim.transport_model, 'Mix')

def test_mixture_averaged(self, saveReference=False):
referenceFile = '../data/DiffusionFlameTest-h2-mix.csv'
p = self.get_test_data_directory()
referenceFile = pjoin(p, 'DiffusionFlameTest-h2-mix.csv')
self.create_sim(p=ct.one_atm)

nPoints = len(self.sim.grid)
Expand All @@ -534,7 +540,8 @@ def test_mixture_averaged(self, saveReference=False):
self.assertFalse(bad, bad)

def test_auto(self, saveReference=False):
referenceFile = '../data/DiffusionFlameTest-h2-auto.csv'
p = self.get_test_data_directory()
referenceFile = pjoin(p, 'DiffusionFlameTest-h2-auto.csv')
self.create_sim(p=ct.one_atm, mdot_fuel=2, mdot_ox=3)

nPoints = []
Expand Down Expand Up @@ -599,7 +606,8 @@ def test_extinction_case7(self):
self.run_extinction(mdot_fuel=0.2, mdot_ox=2.0, T_ox=600, width=0.2, P=0.05)

def test_mixture_averaged_rad(self, saveReference=False):
referenceFile = '../data/DiffusionFlameTest-h2-mix-rad.csv'
p = self.get_test_data_directory()
referenceFile = pjoin(p, 'DiffusionFlameTest-h2-mix-rad.csv')
self.create_sim(p=ct.one_atm)

nPoints = len(self.sim.grid)
Expand Down Expand Up @@ -664,7 +672,6 @@ def test_mixture_fraction(self):


class TestCounterflowPremixedFlame(utilities.CanteraTest):
referenceFile = '../data/CounterflowPremixedFlame-h2-mix.csv'
# Note: to re-create the reference file:
# (1) set PYTHONPATH to build/python2 or build/python3.
# (2) Start Python in the test/work directory and run:
Expand Down Expand Up @@ -707,10 +714,13 @@ def test_mixture_averaged(self, saveReference=False):
data[:,3] = sim.T
data[:,4:] = sim.Y.T

p = self.get_test_data_directory()
referenceFile = pjoin(p, 'CounterflowPremixedFlame-h2-mix.csv')

if saveReference:
np.savetxt(self.referenceFile, data, '%11.6e', ', ')
np.savetxt(referenceFile, data, '%11.6e', ', ')
else:
bad = utilities.compareProfiles(self.referenceFile, data,
bad = utilities.compareProfiles(referenceFile, data,
rtol=1e-2, atol=1e-8, xtol=1e-2)
self.assertFalse(bad, bad)

Expand Down Expand Up @@ -768,9 +778,10 @@ def test_case5(self):

class TestImpingingJet(utilities.CanteraTest):
def run_reacting_surface(self, xch4, tsurf, mdot, width):
p = self.get_test_data_directory()
# Simplified version of the example 'catalytic_combustion.py'
gas = ct.Solution('../data/ptcombust-simple.cti', 'gas')
surf_phase = ct.Interface('../data/ptcombust-simple.cti',
gas = ct.Solution(pjoin(p, 'ptcombust-simple.cti'), 'gas')
surf_phase = ct.Interface(pjoin(p, 'ptcombust-simple.cti'),
'Pt_surf', [gas])

tinlet = 300.0 # inlet temperature
Expand Down
18 changes: 12 additions & 6 deletions interfaces/cython/cantera/test/test_reactor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import math
import re
from os.path import join as pjoin
import os

import numpy as np
from .utilities import unittest
Expand Down Expand Up @@ -917,8 +919,9 @@ def test_coverages_regression1(self):
surf1.coverages = C
self.assertArrayNear(surf1.coverages, C)
data = []
test_file = 'test_coverages_regression1.csv'
reference_file = '../data/WallKinetics-coverages-regression1.csv'
p = self.get_test_data_directory()
test_file = pjoin(p, 'test_coverages_regression1.csv')
reference_file = pjoin(p, 'WallKinetics-coverages-regression1.csv')
data = []
for t in np.linspace(1e-6, 1e-3):
self.net.advance(t)
Expand All @@ -942,8 +945,9 @@ def test_coverages_regression2(self):
surf.coverages = C
self.assertArrayNear(surf.coverages, C)
data = []
test_file = 'test_coverages_regression2.csv'
reference_file = '../data/WallKinetics-coverages-regression2.csv'
p = self.get_test_data_directory()
test_file = pjoin(p, 'test_coverages_regression2.csv')
reference_file = pjoin(p, 'WallKinetics-coverages-regression2.csv')
data = []
for t in np.linspace(1e-6, 1e-3):
self.net.advance(t)
Expand Down Expand Up @@ -1281,8 +1285,9 @@ class CombustorTestImplementation(object):
consistent output.
"""

referenceFile = '../data/CombustorTest-integrateWithAdvance.csv'
def setUp(self):
p = os.path.dirname(__file__)
self.referenceFile = pjoin(p, 'data', 'CombustorTest-integrateWithAdvance.csv')
self.gas = ct.Solution('h2o2.xml')

# create a reservoir for the fuel inlet, and set to pure methane.
Expand Down Expand Up @@ -1369,8 +1374,9 @@ class WallTestImplementation(object):
consistent output.
"""

referenceFile = '../data/WallTest-integrateWithAdvance.csv'
def setUp(self):
p = os.path.dirname(__file__)
self.referenceFile = pjoin(p, 'data', 'WallTest-integrateWithAdvance.csv')
# reservoir to represent the environment
self.gas0 = ct.Solution('air.xml')
self.gas0.TP = 300, ct.one_atm
Expand Down
Loading

0 comments on commit c673293

Please sign in to comment.