Skip to content

Commit

Permalink
[Python] Raise exception when setting composition with empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Aug 7, 2017
1 parent 4c489c1 commit 8a4142d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,16 @@ def test_setCompositionSingleSpecies(self):
self.assertEqual(gas.Y[0], 1.0)

def test_setCompositionSlice_bad(self):
X0 = self.phase.X
with self.assertRaises(ValueError):
self.phase['H2','O2'].Y = [0.1, 0.2, 0.3]
self.assertArrayNear(self.phase.X, X0)

def test_setCompositionEmpty_bad(self):
X0 = self.phase.X
with self.assertRaises(ValueError):
self.phase.Y = np.array([])
self.assertArrayNear(self.phase.X, X0)

def test_set_equivalence_ratio_stoichiometric(self):
gas = ct.Solution('gri30.xml')
Expand Down
9 changes: 5 additions & 4 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,15 @@ cdef class ThermoPhase(_SolutionBase):

if len(values) == self.n_species:
data = np.ascontiguousarray(values, dtype=np.double)
elif len(values) == len(self._selected_species):
elif len(values) == len(self._selected_species) != 0:
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."
" Got {}. Expected {} or {}.".format(
len(values), self.n_species, len(self._selected_species)))
msg = "Got {}. Expected {}".format(len(values), self.n_species)
if len(self._selected_species):
msg += ' or {}'.format(len(self._selected_species))
raise ValueError('Array has incorrect length. ' + msg + '.')
method(self.thermo, &data[0])

property molecular_weights:
Expand Down

0 comments on commit 8a4142d

Please sign in to comment.