[Python] Allow setting composition of sliced Solution objects

The following syntax:

    >>> phase['H2','O2'].X = [1.0, 0.5]

Is equivalent to:

    >>> phase.X = 'H2:1.0, O2:0.5'
This commit is contained in:
Ray Speth 2014-04-08 16:26:47 +00:00
parent d19f975940
commit 051381d862
2 changed files with 19 additions and 4 deletions

View file

@ -104,6 +104,16 @@ class TestThermoPhase(utilities.CanteraTest):
with self.assertRaises(Exception):
self.phase.Y = {'H2':1.0, 'O2':'xx'}
def test_setCompositionSlice(self):
self.phase['H2', 'O2'].X = 0.1, 0.9
X = self.phase.X
self.assertNear(X[0], 0.1)
self.assertNear(X[3], 0.9)
def test_setCompositionSlice_bad(self):
with self.assertRaises(ValueError):
self.phase['H2','O2'].Y = [0.1, 0.2, 0.3]
def test_report(self):
report = self.phase.report()
self.assertTrue(self.phase.name in report)

View file

@ -246,11 +246,16 @@ cdef class ThermoPhase(_SolutionBase):
return data
cdef void _setArray1(self, thermoMethod1d method, values) except *:
if len(values) != self.n_species:
raise ValueError("Array has incorrect length")
cdef np.ndarray[np.double_t, ndim=1] data
cdef np.ndarray[np.double_t, ndim=1] data = \
np.ascontiguousarray(values, dtype=np.double)
if len(values) == self.n_species:
data = np.ascontiguousarray(values, dtype=np.double)
elif len(values) == len(self._selected_species):
data = np.zeros(self.n_species, dtype=np.double)
for i,k in enumerate(self._selected_species):
data[k] = values[i]
else:
raise ValueError("Array has incorrect length")
method(self.thermo, &data[0])
property molecular_weights: