Skip to content

Commit d9ddfbb

Browse files
authored
Merge pull request #9 from adelq/janaf-burcat
JANAF and Burcat fixes
2 parents 10f67cd + a69768b commit d9ddfbb

6 files changed

Lines changed: 53 additions & 57 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ python:
33
- "2.7"
44
- "3.4"
55
- "3.5"
6+
- "3.6"
67

78
install:
89
- sudo apt-get update

tests/__init__.py

Whitespace-only changes.

tests/test_janaf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ def test_titanium():
1111

1212
def test_rutile():
1313
db = Janafdb()
14-
rutile = db.getphasedata(formula='O2Ti', name='Rutile', phase='cr')
14+
rutile = db.getphasedata(formula='O2Ti', name='Rutile', phase='cr', cache=False)
1515
assert_allclose(rutile.cp([500, 550, 1800]), [67.203, 68.567, 78.283])

thermochem/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.6.0'
1+
__version__ = '0.7.0'

thermochem/burcat.py

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from xml.etree.ElementTree import parse
1616
except ImportError:
1717
from elementtree import parse
18-
from numpy import empty, array, dot, log
18+
import numpy as np
1919

2020
# Universal gas constant R
2121
R = 8.314472
@@ -62,11 +62,11 @@ def cpo(self, T):
6262
# I know perfectly that the most efficient way of evaluatin
6363
# polynomials is recursively but I want the implementation to
6464
# be as explicit as possible
65-
Ta = array([1, T, T ** 2, T ** 3, T ** 4], 'd')
65+
Ta = np.array([1, T, T ** 2, T ** 3, T ** 4], 'd')
6666
if T > 200 and T <= 1000:
67-
return dot(self.Tmin_[:5], Ta) * R
67+
return np.dot(self.Tmin_[:5], Ta) * R
6868
elif T > 1000 and T < 6000:
69-
return dot(self._Tmax[:5], Ta) * R
69+
return np.dot(self._Tmax[:5], Ta) * R
7070
else:
7171
raise ValueError("Temperature out of range")
7272

@@ -87,11 +87,11 @@ def ho(self, T):
8787
"""
8888
Computes the sensible enthalpy in J/mol
8989
"""
90-
Ta = array([1, T / 2, T ** 2 / 3, T ** 3 / 4, T ** 4 / 5, 1 / T], 'd')
90+
Ta = np.array([1, T / 2, T ** 2 / 3, T ** 3 / 4, T ** 4 / 5, 1 / T], 'd')
9191
if T > 200 and T <= 1000:
92-
return dot(self.Tmin_[:6], Ta) * R * T
92+
return np.dot(self.Tmin_[:6], Ta) * R * T
9393
elif T > 1000 and T < 6000:
94-
return dot(self._Tmax[:6], Ta) * R * T
94+
return np.dot(self._Tmax[:6], Ta) * R * T
9595
else:
9696
raise ValueError("Temperature out of range")
9797

@@ -105,11 +105,11 @@ def so(self, T):
105105
"""
106106
Computes enthropy in J/mol K
107107
"""
108-
Ta = array([log(T), T, T ** 2 / 2, T ** 3 / 3, T ** 4 / 4, 0, 1], 'd')
108+
Ta = np.array([np.log(T), T, T ** 2 / 2, T ** 3 / 3, T ** 4 / 4, 0, 1], 'd')
109109
if T > 200 and T <= 1000:
110-
return dot(self.Tmin_, Ta) * R
110+
return np.dot(self.Tmin_, Ta) * R
111111
elif T > 1000 and T < 6000:
112-
return dot(self._Tmax, Ta) * R
112+
return np.dot(self._Tmax, Ta) * R
113113
else:
114114
raise ValueError("Temperature out of range")
115115

@@ -324,7 +324,6 @@ def __unicode__(self):
324324

325325

326326
class Elementdb(object):
327-
328327
"""
329328
Class that reads the Alexander Burcat's thermochemical database
330329
for combustion.
@@ -416,41 +415,32 @@ def getelementdata(self, formula):
416415
"""
417416
Returns an element instance given the name of the element.
418417
"""
419-
Tmin_ = empty((7), 'd')
420-
_Tmax = empty((7), 'd')
418+
Tmin_ = np.zeros(7)
419+
_Tmax = np.zeros(7)
421420
comp = []
422421
for specie in self.db:
423-
try:
424-
for element in specie:
425-
try:
426-
if element.tag == "phase":
427-
if formula == element.find("formula").text:
428-
phase = element
429-
coefficients = phase.find("coefficients")
430-
low = coefficients.find("range_Tmin_to_1000")
431-
for (i, c) in zip(range(7), low):
432-
Tmin_[i] = float(c.text)
433-
434-
high = coefficients.find("range_1000_to_Tmax")
435-
for (i, c) in zip(range(7), high):
436-
_Tmax[i] = float(c.text)
437-
438-
elements = phase.find("elements")
439-
elements = elements.getchildren()
440-
for elem in elements:
441-
it = elem.items()
442-
# First is name of element, second is number
443-
# of atoms
444-
comp.append((it[0][1], int(it[1][1])))
445-
446-
mm = float(phase.find("molecular_weight").text) / 1000
447-
hfr = float(coefficients.find("hf298_div_r").text)
448-
449-
return Element(formula, Tmin_, _Tmax, mm, hfr, comp)
450-
except:
451-
pass
452-
except:
453-
pass
422+
for element in specie:
423+
if element.tag == "phase":
424+
if formula == element.find("formula").text:
425+
phase = element
426+
coefficients = phase.find("coefficients")
427+
low = coefficients.find("range_Tmin_to_1000")
428+
for (i, c) in zip(range(7), low):
429+
Tmin_[i] = float(c.text)
430+
431+
high = coefficients.find("range_1000_to_Tmax")
432+
for (i, c) in zip(range(7), high):
433+
_Tmax[i] = float(c.text)
434+
435+
elements = phase.find("elements").getchildren()
436+
for elem in elements:
437+
elem_data = elem.attrib
438+
comp.append((elem_data['name'], int(elem_data['num_of_atoms'])))
439+
440+
mm = float(phase.find("molecular_weight").text) / 1000
441+
hfr = float(coefficients.find("hf298_div_r").text)
442+
443+
return Element(formula, Tmin_, _Tmax, mm, hfr, comp)
454444

455445
def getmixturedata(self, components):
456446
"""

thermochem/janaf.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import print_function
1212

1313
import os
14+
import sys
1415
import numpy as np
1516
import pandas as pd
1617
from scipy.interpolate import interp1d
@@ -71,8 +72,9 @@ def __init__(self, rawdata_text):
7172
StringIO(self.rawdata_text),
7273
skiprows=2,
7374
header=None,
74-
delimiter=r'[\t\s]*',
75-
engine='python')
75+
delimiter=r'[\t\s]+',
76+
engine='python'
77+
)
7678
data.columns = ['T', 'Cp', 'S', '[G-H(Tr)]/T', 'H-H(Tr)', 'Delta_fH',
7779
'Delta_fG', 'log(Kf)']
7880
self.rawdata = data
@@ -86,7 +88,7 @@ def __init__(self, rawdata_text):
8688
if np.issubdtype(data.dtypes[c], np.floating):
8789
continue
8890
# Change INFINITE to inf
89-
data.loc[data[c] == 'INFINITE', c]
91+
data.loc[data[c] == 'INFINITE', c] = np.inf
9092
# Anything else becomes a nan.
9193
# Convert to floats.
9294
data[c] = pd.to_numeric(data[c], errors='coerce')
@@ -162,11 +164,11 @@ def search(self, searchstr):
162164

163165
return self.db[formulasearch | namesearch]
164166

165-
def getphasedata(self, formula=None, name=None, phase=None, nocache=False):
167+
def getphasedata(self, formula=None, name=None, phase=None, cache=True):
166168
"""
167169
Returns an element instance given the name of the element.
168170
formula, name and phase match the respective fields in the JANAF index.
169-
nocache = True means that we will always get the data from the web.
171+
cache = False means that we will always get the data from the web.
170172
171173
>>> db = Janafdb()
172174
>>> db.getphasedata(formula='O2Ti', phase='cr')
@@ -184,9 +186,8 @@ def getphasedata(self, formula=None, name=None, phase=None, nocache=False):
184186
"""
185187

186188
# Check that the phase type requested is valid.
187-
if phase not in Janafdb.VALIDPHASETYPES:
188-
raise ValueError("Valid phase types are " + str(
189-
Janafdb.VALIDPHASETYPES) + ".")
189+
if phase not in self.VALIDPHASETYPES:
190+
raise ValueError("Valid phase types are %s." % self.VALIDPHASETYPES)
190191

191192
# We can search on either an exact formula, partial text match in the
192193
# name, and exact phase type.
@@ -212,8 +213,10 @@ def getphasedata(self, formula=None, name=None, phase=None, nocache=False):
212213

213214
# At this point we have one record. Check if we have that file cached.
214215
cachedfilename = os.path.join(
215-
self.JANAF_cachedir, PhaseRecord['filename'].values[0] + '.txt')
216-
if os.path.exists(cachedfilename) and not nocache:
216+
self.JANAF_cachedir,
217+
"%s.txt" % PhaseRecord['filename'].values[0]
218+
)
219+
if cache and os.path.exists(cachedfilename):
217220
# Yes it was cached, so let's read it into memory.
218221
with open(cachedfilename, 'r') as f:
219222
textdata = f.read()
@@ -222,10 +225,12 @@ def getphasedata(self, formula=None, name=None, phase=None, nocache=False):
222225
response = urllib2.urlopen(Janafdb.JANAF_URL %
223226
PhaseRecord['filename'].values[0])
224227
textdata = response.read()
228+
if sys.version_info[0] > 2:
229+
textdata = textdata.decode()
225230

226231
# And cache the data so we aren't making unnecessary trips to the
227232
# web.
228-
if not nocache:
233+
if cache:
229234
with open(cachedfilename, 'w') as f:
230235
f.write(textdata)
231236

0 commit comments

Comments
 (0)