minor cleanup

This commit is contained in:
Dave Goodwin 2004-06-02 12:57:42 +00:00
parent d887c76272
commit aa2b31276e
5 changed files with 150 additions and 79 deletions

View file

@ -20,6 +20,13 @@ class Func1:
classes are designed to be used with the Cantera kernel. """
def __init__(self, typ, n, coeffs=[]):
"""
typ - functor type
n - order
coeffs - coefficient array
"""
self.n = n
self.coeffs = asarray(coeffs,'d')
self._func_id = _cantera.func_new(typ, n, self.coeffs)
@ -73,7 +80,8 @@ class Func1:
return RatioFunction(other, self)
def func_id(self):
"""Return the integer index used internally to access the kernel-level object."""
"""Return the integer index used internally to access the
kernel-level object."""
return self._func_id
@ -88,6 +96,9 @@ class Polynomial(Func1):
>>> p2 = Polynomial([6.0, 8.0]) # 8t + 6
"""
def __init__(self, coeffs=[]):
"""
coeffs - polynomial coefficients
"""
Func1.__init__(self, 2, len(coeffs)-1, coeffs)
@ -181,7 +192,13 @@ def Const(value):
class PeriodicFunction(Func1):
"""Converts a function into a periodic function with period T."""
def __init__(self, func, T):
"""
func - initial non-periodic function
T - period [s]
"""
Func1.__init__(self, 50, func.func_id(), array([T],'d'))

View file

@ -34,6 +34,8 @@ class Phase:
pass
def phase_id(self):
"""The integer index used to access the kernel-level object.
Internal."""
return self._phase_id
def nElements(self):
@ -41,7 +43,12 @@ class Phase:
return _cantera.phase_nelements(self._phase_id)
def atomicWeights(self, elements = []):
"""Array of element molar masses [kg/kmol]."""
"""Array of element molar masses [kg/kmol].
If a sequence of element symbols is supplied, only the values
for those elements are returned, ordered as in the
list. Otherwise, the values are for all elements in the phase,
ordered as in the input file. """
atw = _cantera.phase_getarray(self._phase_id,1)
if elements:
ae = []
@ -57,9 +64,13 @@ class Phase:
"""Number of species."""
return _cantera.phase_nspecies(self._phase_id)
def nAtoms(self, species = -1, element = -1):
def nAtoms(self, species = None, element = None):
"""Number of atoms of element 'element' in species 'species'.
The element and species may be specified by name or by number."""
The element and species may be specified by name or by number.
>>> ph.nAtoms('CH4','H')
___ 4
"""
try:
m = self.elementIndex(element)
k = self.speciesIndex(species)
@ -86,30 +97,34 @@ class Phase:
return _cantera.phase_molardensity(self._phase_id)
def meanMolecularWeight(self):
"""Mean molar mass [kg/kmol].
DEPRECATED: use meanMolarMass"""
"""Mean molar mass [kg/kmol]."""
return _cantera.phase_meanmolwt(self._phase_id)
def meanMolarMass(self):
"""Mean molar mass [kg/kmol]."""
return _cantera.phase_meanmolwt(self._phase_id)
def molarMasses(self, species = []):
def molarMasses(self, species = None):
"""Array of species molar masses [kg/kmol]."""
mm = _cantera.phase_getarray(self._phase_id,22)
return self.selectSpecies(mm, species)
def molecularWeights(self, species = []):
"""Array of species molar masses [kg/kmol].
DEPRECATED: use molarMasses"""
def molecularWeights(self, species = None):
"""Array of species molar masses [kg/kmol]."""
return self.molarMasses(species)
def moleFractions(self, species = []):
"""Species mole fraction array."""
def moleFractions(self, species = None):
"""Species mole fraction array.
If optional argument 'species'
is supplied, then only the values for the selected species are
returned.
>>> x1 = ph.moleFractions() # all species
>>> x2 = ph.moleFractions(['OH', 'CH3'. 'O2'])
"""
x = _cantera.phase_getarray(self._phase_id,20)
return self.selectSpecies(x, species)
def moleFraction(self, species=-1):
def moleFraction(self, species):
"""Mole fraction of a species, referenced by name or
index number.
>>> ph.moleFraction(4)
@ -119,13 +134,19 @@ class Phase:
return _cantera.phase_molefraction(self._phase_id,k)
def massFractions(self, species = []):
"""Species mass fraction array."""
def massFractions(self, species = None):
"""Species mass fraction array.
If optional argument 'species'
is supplied, then only the values for the selected species are
returned.
>>> y1 = ph.massFractions() # all species
>>> y2 = ph.massFractions(['OH', 'CH3'. 'O2'])
"""
y = _cantera.phase_getarray(self._phase_id,21)
return self.selectSpecies(y, species)
def massFraction(self, species=-1):
def massFraction(self, species):
"""Mass fraction of one species, referenced by name or
index number.
>>> ph.massFraction(4)
@ -136,7 +157,7 @@ class Phase:
def elementName(self,m):
"""Name of element m."""
"""Name of the element with index number m."""
return _cantera.phase_getstring(self._phase_id,1,m)
def elementNames(self):
@ -144,7 +165,7 @@ class Phase:
nel = self.nElements()
return map(self.elementName,range(nel))
def elementIndex(self, element=-1):
def elementIndex(self, element):
"""The index of element 'element', which may be specified as
a string or an integer index. In the latter case, the index is
checked for validity and returned. If no such element is
@ -173,7 +194,7 @@ class Phase:
return map(self.speciesName,range(nsp))
def speciesIndex(self, species=-1):
def speciesIndex(self, species):
"""The index of species 'species', which may be specified as
a string or an integer index. In the latter case, the index is
checked for validity and returned. If no such species is
@ -198,15 +219,18 @@ class Phase:
_cantera.phase_setfp(self._phase_id,2,rho)
def setMoleFractions(self, x, norm = 1):
"""Set the mole fractions. The values may be input either
in a string or a sequence.
"""Set the mole fractions.
x - string or array of mole fraction values
norm - If non-zero (default), array values will be
scaled to sum to 1.0.
>>> ph.setMoleFractions('CO:1, H2:7, H2O:7.8')
>>> x = [1.0]*ph.nSpecies()
>>> ph.setMoleFractions(x)
By default, the input values will be scaled to sum to 1.0.
If this is not desired, supply a third parameter 'norm' set to zero
>>> ph.setMoleFractions(x, norm = 0)
(Note that this only works if an array is input.)
>>> ph.setMoleFractions(x, norm = 0) # don't normalize values
"""
if type(x) == types.StringType:
_cantera.phase_setstring(self._phase_id,1,x)
@ -216,7 +240,7 @@ class Phase:
def setMassFractions(self, x, norm = 1):
"""Set the mass fractions.
See also: setMoleFractions
See: setMoleFractions
"""
if type(x) == types.StringType:
_cantera.phase_setstring(self._phase_id,2,x)
@ -224,7 +248,11 @@ class Phase:
_cantera.phase_setarray(self._phase_id,2,norm,Numeric.asarray(x))
def setState_TRX(self, t, rho, x):
"""Set the temperature, density, and mole fractions."""
"""Set the temperature, density, and mole fractions. The mole
fractions may be entered as a string or array,
>>> ph.setState_TRX(600.0, 2.0e-3, 'CH4:0.4, O2:0.6')
"""
self.setTemperature(t)
self.setMoleFractions(x)
self.setDensity(rho)
@ -236,15 +264,24 @@ class Phase:
self.setDensity(rho)
def setState_TR(self, t, rho):
"""Set the temperature and density."""
"""Set the temperature and density, leaving the composition
unchanged."""
self.setTemperature(t)
self.setDensity(rho)
def selectSpecies(self, f, sp):
if sp:
def selectSpecies(self, f, species):
"""Given an array 'f' of floating-point species properties,
return a Numeric array of those values corresponding to species
listed in 'species'. This method is used internally to implement
species selection in methods like moleFractions, massFractions, etc.
>>> f = ph.chemPotentials()
>>> muo2, muh2 = ph.selectSpecies(f, ['O2', 'H2'])
"""
if species:
fs = []
k = 0
for s in sp:
for s in species:
k = self.speciesIndex(s)
fs.append(f[k])
return Numeric.asarray(fs)

View file

@ -10,7 +10,9 @@ def thermoIndex(id):
return _cantera.thermo_thermoIndex(id)
class ThermoPhase(Phase):
""" Class ThermoPhase may be used to represent the intensive state
""" Phases of matter.
Class ThermoPhase may be used to represent the intensive state
of a homogeneous phase of matter, which might be a gas, liquid, or solid.
"""
@ -20,8 +22,12 @@ class ThermoPhase(Phase):
def __init__(self, xml_phase=None, index=-1):
"""Create a new object representing a phase of matter, or wrap
an existing kernel instance."""
"""
xml_phase - CTML node specifying the attributes of this phase
index - optional. If positive, create only a Python wrapper for
an existing kernel object
"""
self._phase_id = 0
self._owner = 0

View file

@ -3,6 +3,8 @@
Constant-pressure, adiabatic kinetics simulation.
"""
import sys
from Cantera import *
from Cantera.Reactor import *
from Cantera.Func import *
@ -41,25 +43,29 @@ for n in range(100):
# plot the results if matplotlib is installed.
# see http://matplotlib.sourceforge.net to get it
try:
from matplotlib.matlab import *
clf
subplot(2,2,1)
plot(tim,data[:,0])
xlabel('Time (s)');
ylabel('Temperature (K)');
subplot(2,2,2)
plot(tim,data[:,1])
xlabel('Time (s)');
ylabel('OH Mole Fraction');
subplot(2,2,3)
plot(tim,data[:,2]);
xlabel('Time (s)');
ylabel('H Mole Fraction');
subplot(2,2,4)
plot(tim,data[:,3]);
xlabel('Time (s)');
ylabel('H2 Mole Fraction');
show()
except:
pass
args = sys.argv
if len(args) > 1 and args[1] == '-plot':
try:
from matplotlib.matlab import *
clf
subplot(2,2,1)
plot(tim,data[:,0])
xlabel('Time (s)');
ylabel('Temperature (K)');
subplot(2,2,2)
plot(tim,data[:,1])
xlabel('Time (s)');
ylabel('OH Mole Fraction');
subplot(2,2,3)
plot(tim,data[:,2]);
xlabel('Time (s)');
ylabel('H Mole Fraction');
subplot(2,2,4)
plot(tim,data[:,3]);
xlabel('Time (s)');
ylabel('H2 Mole Fraction');
show()
except:
pass
else:
print """To view a plot of these results, run this script with the option -plot"""

View file

@ -18,7 +18,7 @@ instructive.
"""
import sys
from Cantera import *
from Cantera.Reactor import *
from Cantera.Func import *
@ -96,27 +96,32 @@ import os
print 'Output written to file piston.csv'
print 'Directory: '+os.getcwd()
if 1:
from matplotlib.matlab import *
clf
subplot(2,2,1)
plot(tm, temp[:,0],'g-',tm, temp[:,1],'b-')
legend(['Reactor 1','Reactor 2'],2)
xlabel('Time (s)');
ylabel('Temperature (K)');
args = sys.argv
if len(args) > 1 and args[1] == '-plot':
try:
from matplotlib.matlab import *
clf
subplot(2,2,1)
plot(tm, temp[:,0],'g-',tm, temp[:,1],'b-')
legend(['Reactor 1','Reactor 2'],2)
xlabel('Time (s)');
ylabel('Temperature (K)');
subplot(2,2,2)
plot(tm, pres[:,0],'g-',tm, pres[:,1],'b-')
legend(['Reactor 1','Reactor 2'],2)
xlabel('Time (s)');
ylabel('Pressure (Bar)');
subplot(2,2,2)
plot(tm, pres[:,0],'g-',tm, pres[:,1],'b-')
legend(['Reactor 1','Reactor 2'],2)
xlabel('Time (s)');
ylabel('Pressure (Bar)');
subplot(2,2,3)
plot(tm, vol[:,0],'g-',tm, vol[:,1],'b-')
legend(['Reactor 1','Reactor 2'],2)
xlabel('Time (s)');
ylabel('Volume (m^3)');
subplot(2,2,3)
plot(tm, vol[:,0],'g-',tm, vol[:,1],'b-')
legend(['Reactor 1','Reactor 2'],2)
xlabel('Time (s)');
ylabel('Volume (m^3)');
show()
#except:
# pass
show()
except:
pass
else:
print """To view a plot of these results, run this script with the option -plot"""