Skip to content

Commit

Permalink
[Python] Check for compatibility before adding Quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Aug 24, 2015
1 parent 25b1dc9 commit a77b79b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def __init__(self, phase, mass=None, moles=None, constant='UV'):
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:
Expand Down Expand Up @@ -144,6 +148,9 @@ def __rmul__(self, other):
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)
Expand Down
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 @@ -989,3 +989,11 @@ def test_equilibrate(self):
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

0 comments on commit a77b79b

Please sign in to comment.