Skip to content

Commit

Permalink
[Cython/Test] Make python tests portable
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanwweber committed Nov 9, 2016
1 parent 7f971ff commit 42d936e
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 207 deletions.
217 changes: 109 additions & 108 deletions interfaces/cython/cantera/test/test_convert.py

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions interfaces/cython/cantera/test/test_equilibrium.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from __future__ import division

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

def check(self, gas, **moles):
nTotal = sum(moles.values())
for name,X in moles.items():
for name, X in moles.items():
self.assertAlmostEqual(gas[name].X[0], X/nTotal)

def test_equil_complete_stoichiometric(self):
Expand Down Expand Up @@ -135,14 +135,11 @@ def __init__(self, *args, **kwargs):

class TestKOH_Equil(utilities.CanteraTest):
"Test roughly based on examples/multiphase/plasma_equilibrium.py"
@classmethod
def setUpClass(cls):
cls.phases = ct.import_phases('KOH.xml',
def setUp(self):
self.phases = ct.import_phases('KOH.xml',
['K_solid', 'K_liquid', 'KOH_a', 'KOH_b', 'KOH_liquid',
'K2O2_solid', 'K2O_solid', 'KO2_solid', 'ice', 'liquid_water',
'KOH_plasma'])

def setUp(self):
self.mix = ct.Mixture(self.phases)

def test_equil_TP(self):
Expand All @@ -158,7 +155,7 @@ def test_equil_TP(self):

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

self.compare(data, '../data/koh-equil-TP.csv')
self.compare(data, pjoin(self.test_data_dir, 'koh-equil-TP.csv'))

def test_equil_HP(self):
temperatures = range(350, 5000, 300)
Expand All @@ -181,18 +178,17 @@ 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')
self.compare(data, pjoin(self.test_data_dir, 'koh-equil-HP.csv'))


class TestEquil_GasCarbon(utilities.CanteraTest):
"Test rougly based on examples/multiphase/adiabatic.py"
@classmethod
def setUpClass(cls):
cls.gas = ct.Solution('gri30.xml')
cls.carbon = ct.Solution('graphite.xml')
cls.fuel = 'CH4'
cls.mix_phases = [(cls.gas, 1.0), (cls.carbon, 0.0)]
cls.n_species = cls.gas.n_species + cls.carbon.n_species
def setUp(self):
self.gas = ct.Solution('gri30.xml')
self.carbon = ct.Solution('graphite.xml')
self.fuel = 'CH4'
self.mix_phases = [(self.gas, 1.0), (self.carbon, 0.0)]
self.n_species = self.gas.n_species + self.carbon.n_species

def solve(self, solver, **kwargs):
n_points = 12
Expand All @@ -212,7 +208,7 @@ 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')
self.compare(data, pjoin(self.test_data_dir, 'gas-carbon-equil.csv'))

def test_gibbs(self):
self.solve('gibbs')
Expand Down
40 changes: 23 additions & 17 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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,14 +609,14 @@ def cathode_curr(E):
cathode_bulk.electric_potential -
anode_bulk.electric_potential])

self.compare(data, '../data/sofc-test.csv')
self.compare(data, pjoin(self.test_data_dir, 'sofc-test.csv'))


class TestDuplicateReactions(utilities.CanteraTest):
infile = 'duplicate-reactions.cti'

def check(self, name):
with self.assertRaises(Exception) as cm:
with self.assertRaises(ct.CanteraError) as cm:
ct.Solution(self.infile, name)
self.assertIn('duplicate reaction', str(cm.exception))

Expand Down Expand Up @@ -652,11 +654,12 @@ def test_declared_duplicate(self):

class TestReaction(utilities.CanteraTest):
@classmethod
def setUpClass(cls):
cls.gas = ct.Solution('h2o2.xml')
cls.gas.X = 'H2:0.1, H2O:0.2, O2:0.7, O:1e-4, OH:1e-5, H:2e-5'
cls.gas.TP = 900, 2*ct.one_atm
cls.species = ct.Species.listFromFile('h2o2.xml')
def setUpClass(self):
super().setUpClass()
self.gas = ct.Solution('h2o2.xml')
self.gas.X = 'H2:0.1, H2O:0.2, O2:0.7, O:1e-4, OH:1e-5, H:2e-5'
self.gas.TP = 900, 2*ct.one_atm
self.species = ct.Species.listFromFile('h2o2.xml')

def test_fromCti(self):
r = ct.Reaction.fromCti('''three_body_reaction('2 O + M <=> O2 + M',
Expand All @@ -673,7 +676,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 +694,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 @@ -727,7 +733,7 @@ def test_negative_A(self):

self.assertFalse(r.allow_negative_pre_exponential_factor)

with self.assertRaises(Exception):
with self.assertRaises(ct.CanteraError):
gas = ct.Solution(thermo='IdealGas', kinetics='GasKinetics',
species=species, reactions=[r])

Expand Down Expand Up @@ -864,17 +870,17 @@ def test_modify_invalid(self):
tbr = self.gas.reaction(0)
R2 = ct.ElementaryReaction(tbr.reactants, tbr.products)
R2.rate = tbr.rate
with self.assertRaises(Exception):
with self.assertRaises(ct.CanteraError):
self.gas.modify_reaction(0, R2)

# different reactants
R = self.gas.reaction(7)
with self.assertRaises(Exception):
with self.assertRaises(ct.CanteraError):
self.gas.modify_reaction(23, R)

# different products
R = self.gas.reaction(14)
with self.assertRaises(Exception):
with self.assertRaises(ct.CanteraError):
self.gas.modify_reaction(15, R)

def test_modify_elementary(self):
Expand Down Expand Up @@ -1004,8 +1010,8 @@ def test_motz_wise(self):
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('ptcombust-motzwise.cti', 'gas')
surf2 = ct.Interface('ptcombust-motzwise.cti', 'Pt_surf', [gas2])
surf2.TPY = surf1.TPY

k1 = surf1.forward_rate_constants
Expand Down
10 changes: 5 additions & 5 deletions interfaces/cython/cantera/test/test_mixture.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 setUpClass(self):
super().setUpClass()
self.phase1 = ct.Solution('h2o2.xml')
self.phase2 = ct.Solution('air.xml')

def setUp(self):
self.mix = ct.Mixture([(self.phase1, 1.0), (self.phase2, 2.0)])
Expand Down Expand Up @@ -190,5 +190,5 @@ def test_invalid_property(self):

def test_invalid_phase_type(self):
water = ct.Water()
with self.assertRaises(Exception):
with self.assertRaises(ct.CanteraError):
self.mix = ct.Mixture([(self.phase1, 1.0), (water, 2.0)])
Loading

0 comments on commit 42d936e

Please sign in to comment.