Skip to content

Commit

Permalink
[Tests] Simplify access to work and data folders
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jun 2, 2021
1 parent 7922a81 commit 59752c6
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 104 deletions.
19 changes: 9 additions & 10 deletions interfaces/cython/cantera/test/test_composite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
from pathlib import Path

import numpy as np
from collections import OrderedDict
Expand All @@ -15,7 +14,7 @@ class TestModels(utilities.CanteraTest):
@classmethod
def setUpClass(cls):
utilities.CanteraTest.setUpClass()
cls.yml_file = Path(cls.test_data_dir).joinpath("thermo-models.yaml")
cls.yml_file = cls.test_data_path / "thermo-models.yaml"
cls.yml = utilities.load_yaml(cls.yml_file)

def test_load_thermo_models(self):
Expand Down Expand Up @@ -128,7 +127,7 @@ def setUpClass(cls):
def tearDown(self):
temp_files = ["solutionarray.csv", "solutionarray.h5"]
for f in temp_files:
fpath = Path(self.test_work_dir).joinpath(f)
fpath = self.test_work_path / f
if fpath.is_file():
fpath.unlink()

Expand All @@ -149,7 +148,7 @@ def test_write_csv(self):
states.TPX = np.linspace(300, 1000, 7), 2e5, 'H2:0.5, O2:0.4'
states.equilibrate('HP')

outfile = Path(self.test_work_dir).joinpath("solutionarray.csv")
outfile = self.test_work_path / "solutionarray.csv"
states.write_csv(outfile)

data = np.genfromtxt(outfile, names=True, delimiter=',')
Expand All @@ -166,7 +165,7 @@ def test_write_csv(self):
def test_write_csv_str_column(self):
states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'})

outfile = Path(self.test_work_dir).joinpath("solutionarray.csv")
outfile = self.test_work_path / "solutionarray.csv"
states.write_csv(outfile)

b = ct.SolutionArray(self.gas, extra={'spam'})
Expand All @@ -176,7 +175,7 @@ def test_write_csv_str_column(self):
def test_write_csv_multidim_column(self):
states = ct.SolutionArray(self.gas, 3, extra={'spam': np.zeros((3, 5,))})

outfile = Path(self.test_work_dir).joinpath("solutionarray.csv")
outfile = self.test_work_path / "solutionarray.csv"
with self.assertRaisesRegex(NotImplementedError, 'not supported'):
states.write_csv(outfile)

Expand All @@ -193,7 +192,7 @@ def test_to_pandas(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf(self):
outfile = Path(self.test_work_dir).joinpath("solutionarray.h5")
outfile = self.test_work_path / "solutionarray.h5"

extra = {'foo': range(7), 'bar': range(7)}
meta = {'spam': 'eggs', 'hello': 'world'}
Expand Down Expand Up @@ -236,7 +235,7 @@ def test_write_hdf(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf_str_column(self):
outfile = Path(self.test_work_dir).joinpath("solutionarray.h5")
outfile = self.test_work_path / "solutionarray.h5"

states = ct.SolutionArray(self.gas, 3, extra={'spam': 'eggs'})
states.write_hdf(outfile, mode='w')
Expand All @@ -247,7 +246,7 @@ def test_write_hdf_str_column(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf_multidim_column(self):
outfile = Path(self.test_work_dir).joinpath("solutionarray.h5")
outfile = self.test_work_path / "solutionarray.h5"

states = ct.SolutionArray(self.gas, 3, extra={'spam': [[1, 2], [3, 4], [5, 6]]})
states.write_hdf(outfile, mode='w')
Expand Down Expand Up @@ -496,7 +495,7 @@ def test_yaml_outunits(self):
units = {'length': 'cm', 'quantity': 'mol', 'energy': 'cal'}
gas.write_yaml('h2o2-generated.yaml', units=units)
generated = utilities.load_yaml("h2o2-generated.yaml")
original = utilities.load_yaml(Path(self.cantera_data).joinpath("h2o2.yaml"))
original = utilities.load_yaml(self.cantera_data_path / "h2o2.yaml")
self.assertEqual(generated['units'], units)

for r1, r2 in zip(original['reactions'], generated['reactions']):
Expand Down
24 changes: 12 additions & 12 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ def convert(self, inputFile, thermo=None, transport=None,
if output is None:
output = Path(inputFile).stem # strip '.inp'
if inputFile is not None:
inputFile = Path(self.test_data_dir).joinpath(inputFile)
inputFile = self.test_data_path / inputFile
if thermo is not None:
thermo = Path(self.test_data_dir).joinpath(thermo)
thermo = self.test_data_path / thermo
if transport is not None:
transport = Path(self.test_data_dir).joinpath(transport)
transport = self.test_data_path / transport
if surface is not None:
surface = Path(self.test_data_dir).joinpath(surface)
surface = self.test_data_path / surface
if extra is not None:
extra = Path(self.test_data_dir).joinpath(extra)
output = Path(self.test_work_dir).joinpath(output + self.ext)
extra = self.test_data_path / extra
output = self.test_work_path / (output + self.ext)
if output.is_file():
output.unlink()
self._convert(inputFile, thermo=thermo, transport=transport,
Expand Down Expand Up @@ -359,14 +359,14 @@ def test_empty_reaction_section(self):

def test_reaction_comments1(self):
output = self.convert('pdep-test.inp')
text = Path(output).read_text()
text = output.read_text()
self.assertIn('Generic mechanism header', text)
self.assertIn('Single PLOG reaction', text)
self.assertIn('Multiple PLOG expressions at the same pressure', text)

def test_reaction_comments2(self):
output = self.convert('explicit-third-bodies.inp', thermo='dummy-thermo.dat')
text = Path(output).read_text()
text = output.read_text()
self.assertIn('An end of line comment', text)
self.assertIn('A comment after the last reaction', text)

Expand Down Expand Up @@ -470,7 +470,7 @@ def test_extra(self):
transport='gri30_tran.dat', output='gri30_extra',
extra='extra.yaml')

output = Path(self.test_work_dir).joinpath("gri30_extra" + self.ext)
output = self.test_work_path / ("gri30_extra" + self.ext)
yml = utilities.load_yaml(output)

desc = yml['description'].split('\n')[-1]
Expand All @@ -481,7 +481,7 @@ def test_extra(self):

def test_sri_zero(self):
self.convert('sri_convert_test.txt')
output = Path(self.test_work_dir).joinpath("sri_convert_test" + self.ext)
output = self.test_work_path / ("sri_convert_test" + self.ext)
mech = utilities.load_yaml(output)
D = mech['reactions'][0]['SRI']['D']
E = mech['reactions'][0]['SRI']['E']
Expand Down Expand Up @@ -569,7 +569,7 @@ def test_long_source_input(self):

gas = ct.Solution('pdep-test.yaml')

with open(Path(self.test_data_dir).joinpath("pdep-test.cti"), "r") as f:
with open(self.test_data_path / "pdep-test.cti", "r") as f:
data = f.read()
data_size_2048kB = data + ' '*2048*1024
gas2 = ct.Solution(source=data_size_2048kB)
Expand All @@ -584,7 +584,7 @@ def test_short_source_input(self):

gas = ct.Solution('pdep-test.yaml')

with open(Path(self.test_data_dir).joinpath("pdep-test.cti"), "r") as f:
with open(self.test_data_path / "pdep-test.cti", "r") as f:
data = f.read()
data_size_32kB = data + ' '*18000
gas2 = ct.Solution(source=data_size_32kB)
Expand Down
7 changes: 3 additions & 4 deletions interfaces/cython/cantera/test/test_equilibrium.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from os.path import join as pjoin

import numpy as np

Expand Down Expand Up @@ -153,7 +152,7 @@ def test_equil_TP(self):

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

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

@utilities.slow_test
def test_equil_HP(self):
Expand All @@ -177,7 +176,7 @@ def test_equil_HP(self):
data[i,1] = self.mix.T # equilibrated temperature
data[i,2:] = self.mix.species_moles

self.compare(data, pjoin(self.test_data_dir, 'koh-equil-HP.csv'))
self.compare(data, self.test_data_path / "koh-equil-HP.csv")


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

self.compare(data, pjoin(self.test_data_dir, 'gas-carbon-equil.csv'))
self.compare(data, self.test_data_path / "gas-carbon-equil.csv")

@utilities.slow_test
def test_gibbs(self):
Expand Down
13 changes: 4 additions & 9 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
import re
import itertools
from os.path import join as pjoin
import os

import cantera as ct
from . import utilities
Expand Down Expand Up @@ -816,7 +814,7 @@ def cathode_curr(E):
cathode_bulk.electric_potential -
anode_bulk.electric_potential])

self.compare(data, pjoin(self.test_data_dir, 'sofc-test.csv'), rtol=1e-7)
self.compare(data, self.test_data_path / "sofc-test.csv", rtol=1e-7)


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

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

Expand Down Expand Up @@ -925,17 +922,15 @@ def test_listFromFile(self):

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

def test_listFromXml(self):
gas = ct.Solution("h2o2.xml", transport_model=None)
p = os.path.dirname(__file__)
with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f:
with open(self.cantera_data_path / "h2o2.xml") as f:
R = ct.Reaction.listFromXml(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in gas.reactions()]
Expand Down
58 changes: 28 additions & 30 deletions interfaces/cython/cantera/test/test_onedim.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import cantera as ct
from . import utilities
import numpy as np
import os
from os.path import join as pjoin

from cantera.composite import _h5py

Expand Down Expand Up @@ -510,9 +508,9 @@ def test_save_restore(self):

self.create_sim(p, Tin, reactants)
self.solve_fixed_T()
filename = pjoin(self.test_work_dir, 'onedim-fixed-T.xml')
if os.path.exists(filename):
os.remove(filename)
filename = self.test_work_path / "onedim-fixed-T.xml"
if filename.is_file():
filename.unlink()

Y1 = self.sim.Y
u1 = self.sim.velocity
Expand Down Expand Up @@ -577,9 +575,9 @@ def test_save_restore_add_species(self):
p = 2 * ct.one_atm
Tin = 400

filename = pjoin(self.test_work_dir, 'onedim-add-species.xml')
if os.path.exists(filename):
os.remove(filename)
filename = self.test_work_path / "onedim-add-species.xml"
if filename.is_file():
filename.unlink()

self.create_sim(p, Tin, reactants, mech='h2o2.xml')
gas1 = self.gas
Expand All @@ -606,9 +604,9 @@ def test_save_restore_remove_species(self):
p = 2 * ct.one_atm
Tin = 400

filename = pjoin(self.test_work_dir, 'onedim-add-species.xml')
if os.path.exists(filename):
os.remove(filename)
filename = self.test_work_path / "onedim-add-species.xml"
if filename.is_file():
filename.unlink()

self.create_sim(p, Tin, reactants, mech='h2o2-plus.xml')
gas1 = self.gas
Expand All @@ -630,9 +628,9 @@ def test_save_restore_remove_species(self):
self.assertArrayNear(Y1[k1], Y2[k2])

def test_write_csv(self):
filename = pjoin(self.test_work_dir, 'onedim-write_csv.csv')
if os.path.exists(filename):
os.remove(filename)
filename = self.test_work_path / "onedim-write_csv.csv"
if filename.is_file():
filename.unlink()

self.create_sim(2e5, 350, 'H2:1.0, O2:2.0', mech='h2o2.xml')
self.sim.write_csv(filename)
Expand All @@ -645,9 +643,9 @@ def test_write_csv(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf(self):
filename = pjoin(self.test_work_dir, 'onedim-write_hdf.h5')
if os.path.exists(filename):
os.remove(filename)
filename = self.test_work_path / "onedim-write_hdf.h5"
if filename.is_file():
filename.unlink()

self.run_mix(phi=1.1, T=350, width=2.0, p=2.0, refine=False)
desc = 'mixture-averaged simulation'
Expand Down Expand Up @@ -771,7 +769,7 @@ def solve_mix(self, ratio=3.0, slope=0.1, curve=0.12, prune=0.0):

@utilities.slow_test
def test_mixture_averaged(self, saveReference=False):
referenceFile = pjoin(self.test_data_dir, 'DiffusionFlameTest-h2-mix.csv')
referenceFile = self.test_data_path / "DiffusionFlameTest-h2-mix.csv"
self.create_sim(p=ct.one_atm)

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

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

nPoints = []
Expand Down Expand Up @@ -885,7 +883,7 @@ def test_restart(self):
self.assertNear(mdot[-1], -self.sim.oxidizer_inlet.mdot, 1e-4)

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

nPoints = len(self.sim.grid)
Expand Down Expand Up @@ -916,12 +914,12 @@ def test_mixture_averaged_rad(self, saveReference=False):
rtol=1e-2, atol=1e-8, xtol=1e-2)
self.assertFalse(bad, bad)

filename = pjoin(self.test_work_dir, 'DiffusionFlameTest-h2-mix-rad.csv')
filename = self.test_work_path / "DiffusionFlameTest-h2-mix-rad.csv"
self.sim.write_csv(filename) # check output
self.assertTrue(os.path.exists(filename))
self.assertTrue(filename.is_file())
csv_data = np.genfromtxt(filename, dtype=float, delimiter=',', names=True)
self.assertIn('qdot', csv_data.dtype.names)
os.remove(filename)
filename.unlink()

def test_strain_rate(self):
# This doesn't test that the values are correct, just that they can be
Expand Down Expand Up @@ -1006,20 +1004,20 @@ def test_mixture_averaged(self, saveReference=False):
data[:,3] = sim.T
data[:,4:] = sim.Y.T

referenceFile = pjoin(self.test_data_dir, 'CounterflowPremixedFlame-h2-mix.csv')
referenceFile = self.test_data_path / "CounterflowPremixedFlame-h2-mix.csv"
if saveReference:
np.savetxt(referenceFile, data, '%11.6e', ', ')
else:
bad = utilities.compareProfiles(referenceFile, data,
rtol=1e-2, atol=1e-8, xtol=1e-2)
self.assertFalse(bad, bad)

filename = pjoin(self.test_work_dir, 'CounterflowPremixedFlame-h2-mix.csv')
filename = self.test_work_path / "CounterflowPremixedFlame-h2-mix.csv"
sim.write_csv(filename) # check output
self.assertTrue(os.path.exists(filename))
self.assertTrue(filename.is_file())
csv_data = np.genfromtxt(filename, dtype=float, delimiter=',', names=True)
self.assertNotIn('qdot', csv_data.dtype.names)
os.remove(filename)
filename.unlink()

def run_case(self, phi, T, width, P):
gas = ct.Solution('h2o2.xml')
Expand Down Expand Up @@ -1190,9 +1188,9 @@ def test_reacting_surface_case3(self):

@utilities.unittest.skipIf(isinstance(_h5py, ImportError), "h5py is not installed")
def test_write_hdf(self):
filename = pjoin(self.test_work_dir, 'impingingjet-write_hdf.h5')
if os.path.exists(filename):
os.remove(filename)
filename = self.test_work_path / "impingingjet-write_hdf.h5"
if filename.is_file():
filename.unlink()

self.run_reacting_surface(xch4=0.095, tsurf=900.0, mdot=0.06, width=0.1)
self.sim.write_hdf(filename)
Expand Down
Loading

0 comments on commit 59752c6

Please sign in to comment.