diff --git a/interfaces/cython/cantera/composite.py b/interfaces/cython/cantera/composite.py index f468dcd47..cfa7cf180 100644 --- a/interfaces/cython/cantera/composite.py +++ b/interfaces/cython/cantera/composite.py @@ -68,6 +68,10 @@ class Quantity(object): self.state = phase.TDY self._phase = phase + # A unique key to prevent adding phases with different species + # definitions + self._id = hash((phase.name,) + tuple(phase.species_names)) + if mass is not None: self.mass = mass elif moles is not None: @@ -144,6 +148,9 @@ class Quantity(object): return Quantity(self.phase, mass=self.mass * other) def __iadd__(self, other): + if (self._id != other._id): + raise ValueError('Cannot add Quantities with different phase ' + 'definitions.') assert(self.constant == other.constant) a1,b1 = getattr(self.phase, self.constant) a2,b2 = getattr(other.phase, self.constant) diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 9846a1880..0b4f1d1bc 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -989,3 +989,11 @@ class TestQuantity(utilities.CanteraTest): self.assertNear(q1.T, 300) q1.equilibrate('HP') self.assertNear(q1.T, T2) + + def test_incompatible(self): + gas2 = ct.Solution('h2o2.xml') + q1 = ct.Quantity(self.gas) + q2 = ct.Quantity(gas2) + + with self.assertRaises(Exception): + q1+q2