Skip to content

Commit

Permalink
Add more documentation to chemical, update the method of creating che…
Browse files Browse the repository at this point in the history
…micals
  • Loading branch information
CalebBell committed Aug 7, 2017
1 parent c1170ac commit 89980cb
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 127 deletions.
39 changes: 39 additions & 0 deletions tests/test_chemical.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,45 @@ def test_H_Chemical():
assert_allclose(dH_20K_gas, 1000*(48.9411675-48.2041134), rtol=1E-1) # Web tables, but hardly matches because of the excess


def test_Mixture_input_forms():
# Run a test initializing a mixture from mole fractions, mass fractions,
# liquid fractions, gas fractions (liq/gas are with volumes of pure components at T and P)
kwargs = {'ws': [0.5, 0.5], 'zs': [0.7188789914193495, 0.2811210085806504],
'Vfls': [0.44054617180108374, 0.5594538281989162],
'Vfgs': [0.7229421485513368, 0.2770578514486633]}
for key, val in kwargs.items():
m = Mixture(['water', 'ethanol'], **{key:val})
assert_allclose(m.zs, kwargs['zs'], rtol=1E-6)
assert_allclose(m.zs, m.xs)
assert_allclose(m.Vfls(), kwargs['Vfls'], rtol=1E-5)
assert_allclose(m.Vfgs(), kwargs['Vfgs'])

with pytest.raises(Exception):
Mixture(['water', 'ethanol'])

Mixture(['water'], ws=[1], T=300, P=1E5)


def test_Mixture_predefined():
for name in ['Air', 'air', u'Air', ['air']]:
air = Mixture(name)
assert air.CASs == ['7727-37-9', '7440-37-1', '7782-44-7']
assert_allclose(air.zs, [0.7811979754734807, 0.009206322604387548, 0.20959570192213187], rtol=1E-4)
assert_allclose(air.ws, [0.7557, 0.0127, 0.2316], rtol=1E-3)

R401A = Mixture('R401A')
assert R401A.CASs == ['75-45-6', '75-37-6', '2837-89-0']
assert_allclose(R401A.zs, [0.578852219944875, 0.18587468325478565, 0.2352730968003393], rtol=1E-4)
assert_allclose(R401A.ws, [0.53, 0.13, 0.34], rtol=1E-3)

natural_gas = Mixture('Natural gas')
assert natural_gas.CASs == ['74-82-8', '7727-37-9', '124-38-9', '74-84-0', '74-98-6', '75-28-5', '106-97-8', '78-78-4', '109-66-0', '110-54-3']
assert_allclose(natural_gas.zs, [0.9652228316853225, 0.002594967217109564, 0.005955831022086067, 0.018185509193506685, 0.004595963476244077, 0.0009769695915451998, 0.001006970610302194, 0.0004729847624453981, 0.0003239924667435125, 0.0006639799746946288], rtol=1E-3)
assert_allclose(natural_gas.ws, [0.921761382642074, 0.004327306490959737, 0.015603023404535107, 0.03255104882657324, 0.012064018096027144, 0.0033802050703076055, 0.0034840052260078393, 0.002031403047104571, 0.001391502087253131, 0.003406105109157664], rtol=1E-4)




@pytest.mark.slow
@pytest.mark.meta_Chemical
def test_all_chemicals():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_doctests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from numpy import linspace
import matplotlib.pyplot as plt

from thermo.identifiers import CASfromAny
from thermo.identifiers import CAS_from_any
#warnings.simplefilter("always") #error for error
warnings.simplefilter("ignore") #error for error

Expand Down
2 changes: 2 additions & 0 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def test_misc_elements():
assert periodic_table['H'].InChI == 'InChI=1S/H'

assert periodic_table['H'].smiles == '[H]'
assert periodic_table[1].smiles == '[H]'
assert periodic_table['1'].smiles == '[H]'

def test_Hill_formula():
Hill_formulas = {'ClNa': {'Na': 1, 'Cl': 1}, 'BrI': {'I': 1, 'Br': 1},
Expand Down
67 changes: 65 additions & 2 deletions tests/test_identifiers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling.
Copyright (C) 2016, Caleb Bell <Caleb.Andrew.Bell@gmail.com>
Copyright (C) 2016-2017, Caleb Bell <Caleb.Andrew.Bell@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,4 +37,67 @@ def test_pubchem_dict():

def test_mixture_from_any():
with pytest.raises(Exception):
mixture_from_any(['water', 'methanol'])
mixture_from_any(['water', 'methanol'])
with pytest.raises(Exception):
mixture_from_any('NOTAMIXTURE')

for name in ['Air', 'air', u'Air', ['air']]:
assert mixture_from_any(name) == 'Air'

names = ['R-401A ', ' R-401A ', 'R401A ', 'r401a', 'r-401A', 'refrigerant-401A', 'refrigerant 401A']
for name in names:
assert mixture_from_any(name) == 'R401A'

assert mixture_from_any('R512A') == 'R512A'
assert mixture_from_any([u'air']) == 'Air'


def test_CAS_from_any():
assert CAS_from_any('7732-18-5 ') == '7732-18-5'
assert CAS_from_any(' 7732 -18-5 ') == '7732-18-5'
# direct in dictionary case
assert CAS_from_any('water') == '7732-18-5'

# Not in the main dict, but a synonym cas exists in the database
assert CAS_from_any('136-16-3') == '582-36-5'

assert CAS_from_any('inchi=1s/C2H6O/c1-2-3/h3H,2H2,1H3') == '64-17-5'
assert CAS_from_any('inchi=1/C2H6O/c1-2-3/h3H,2H2,1H3') == '64-17-5'
assert CAS_from_any('InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3') == '64-17-5'
assert CAS_from_any('InChI=1/C2H6O/c1-2-3/h3H,2H2,1H3') == '64-17-5'
assert CAS_from_any('InChIKey=LFQSCWFLJHTTHZ-UHFFFAOYSA-N') == '64-17-5'
assert CAS_from_any('inchikey=LFQSCWFLJHTTHZ-UHFFFAOYSA-N') == '64-17-5'
assert CAS_from_any(' inchikey=LFQSCWFLJHTTHZ-UHFFFAOYSA-N') == '64-17-5'

assert CAS_from_any('InChI=1S/C6H15N/c1-5-6(2)7(3)4/h6H,5H2,1-4H3') == '921-04-0'

assert CAS_from_any('pubchem=702') == '64-17-5'

assert CAS_from_any('oxidane') == '7732-18-5'

assert CAS_from_any('CCCCCCCCCC') == '124-18-5'
assert CAS_from_any('SMILES=CCCCCCCCCC') == '124-18-5'

assert CAS_from_any('S') == '7704-34-9'
assert CAS_from_any('O') == '7782-44-7'

assert CAS_from_any('InChiKey=QVGXLLKOCUKJST-UHFFFAOYSA-N') == '17778-80-2'
# Just because it's an element does not mean the CAS number refers to the
# monatomic form unfortunately - this is the CAS for Monooxygen

assert CAS_from_any('1') == '1333-74-0'


# Unknown inchi
with pytest.raises(Exception):
CAS_from_any('InChI=1S/C13H14N2O2S/c18-13-15-14-12(17-13)8-16-11-6-5-9-3-1-2-4-10(9)7-11/h5-7H,1-4,8H2,(H,15,18)')
with pytest.raises(Exception):
CAS_from_any('InChI=1/C13H14N2O2S/c18-13-15-14-12(17-13)8-16-11-6-5-9-3-1-2-4-10(9)7-11/h5-7H,1-4,8H2,(H,15,18)')
with pytest.raises(Exception):
CAS_from_any('InChIKey=QHHWJJGJTYTIPM-UHFFFAOYSA-N')
# unknown pubchem
with pytest.raises(Exception):
CAS_from_any('pubchem=902100')
# unknown CAS
with pytest.raises(Exception):
CAS_from_any('1411769-41-9')
Loading

0 comments on commit 89980cb

Please sign in to comment.