[Python] Check for compatibility before adding Quantities
This commit is contained in:
parent
25b1dc90f2
commit
a77b79b00c
2 changed files with 15 additions and 0 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue