[Cython] Turned Mixture.species_moles into a property

This commit is contained in:
Ray Speth 2013-01-30 22:05:52 +00:00
parent 92016a2b31
commit cccc5c7a8b
2 changed files with 27 additions and 34 deletions

View file

@ -229,40 +229,33 @@ cdef class Mixture:
"""
self.mix.setPhaseMoles(self.phase_index(p), moles)
def species_moles(self, species=None):
property species_moles:
"""
Returns the number of moles of species *k* if *k* is specified,
or the number of of moles of each species otherwise.
"""
if species is not None:
return self.mix.speciesMoles(species)
cdef np.ndarray[np.double_t, ndim=1] data = np.empty(self.n_species)
for k in range(self.n_species):
data[k] = self.mix.speciesMoles(k)
return data
def set_species_moles(self, moles):
"""
Set the moles of the species [kmol]. The moles may be specified either
The number of moles of each species. May be set either
as a string, or as an array. If an array is used, it must be
dimensioned at least as large as the total number of species in the
mixture. Note that the species may belong to any phase, and
unspecified species are set to zero.
>>> mix.set_species_moles('C(s):1.0, CH4:2.0, O2:0.2')
>>> mix.species_moles = 'C(s):1.0, CH4:2.0, O2:0.2'
"""
if isinstance(moles, (str, unicode)):
self.mix.setMolesByName(stringify(moles))
return
def __get__(self):
cdef np.ndarray[np.double_t, ndim=1] data = np.empty(self.n_species)
for k in range(self.n_species):
data[k] = self.mix.speciesMoles(k)
return data
if len(moles) != self.n_species:
raise ValueError('mole array must be of length n_species')
def __set__(self, moles):
if isinstance(moles, (str, unicode)):
self.mix.setMolesByName(stringify(moles))
return
cdef np.ndarray[np.double_t, ndim=1] data = \
np.ascontiguousarray(moles, dtype=np.double)
self.mix.setMoles(&data[0])
if len(moles) != self.n_species:
raise ValueError('mole array must be of length n_species')
cdef np.ndarray[np.double_t, ndim=1] data = \
np.ascontiguousarray(moles, dtype=np.double)
self.mix.setMoles(&data[0])
def element_moles(self, e):
"""

View file

@ -119,9 +119,9 @@ class TestMixture(utilities.CanteraTest):
self.assertEqual(self.mix.phase_moles(1), 4)
def test_species_moles(self):
self.mix.set_species_moles('H2:1.0, N2:4.0')
self.mix.species_moles = 'H2:1.0, N2:4.0'
P = self.mix.phase_moles()
S = self.mix.species_moles()
S = self.mix.species_moles
self.assertEqual(P[0], 1)
self.assertEqual(P[1], 4)
@ -130,18 +130,18 @@ class TestMixture(utilities.CanteraTest):
self.assertEqual(S[self.mix.species_index(1, 'N2')], 4)
S[2] = 7
self.mix.set_species_moles(S)
self.assertNear(self.mix.species_moles(2), S[2])
self.mix.species_moles = S
self.assertNear(self.mix.species_moles[2], S[2])
self.assertNear(self.mix.phase_moles(0), sum(S[:self.phase1.n_species]))
with self.assertRaises(ValueError):
self.mix.set_species_moles((1,2,3))
self.mix.species_moles = (1,2,3)
with self.assertRaises(TypeError):
self.mix.set_species_moles(9)
self.mix.species_moles = 9
def test_element_moles(self):
self.mix.set_species_moles('H2:1.0, OH:4.0')
self.mix.species_moles = 'H2:1.0, OH:4.0'
self.assertNear(self.mix.element_moles('H'), 6)
self.assertNear(self.mix.element_moles('O'), 4)
@ -156,7 +156,7 @@ class TestMixture(utilities.CanteraTest):
self.assertArrayNear(C[self.phase1.n_species:], C2)
def test_equilibrate1(self):
self.mix.set_species_moles('H2:1.0, O2:0.5, N2:1.0')
self.mix.species_moles = 'H2:1.0, O2:0.5, N2:1.0'
self.mix.T = 400
self.mix.P = 2 * ct.one_atm
@ -169,7 +169,7 @@ class TestMixture(utilities.CanteraTest):
self.assertNear(self.mix.P, 2 * ct.one_atm)
def test_equilibrate2(self):
self.mix.set_species_moles('H2:1.0, O2:0.5, N2:1.0')
self.mix.species_moles = 'H2:1.0, O2:0.5, N2:1.0'
self.mix.T = 400
self.mix.P = 2 * ct.one_atm