diff --git a/interfaces/cython/cantera/composite.py b/interfaces/cython/cantera/composite.py index bcc77c646a..f5fa36afd9 100644 --- a/interfaces/cython/cantera/composite.py +++ b/interfaces/cython/cantera/composite.py @@ -317,6 +317,13 @@ def __iadd__(self, other): raise ValueError('Cannot add Quantities with different phase ' 'definitions.') assert self.constant == other.constant + + if self.constant == 'HP': + dp_rel = 2 * abs(self.P - other.P) / (self.P + other.P) + if dp_rel > 1.0e-7: + raise ValueError('Cannot add Quantities at constant pressure when' + f'pressure is not equal ({self.P} != {other.P})') + a1,b1 = getattr(self.phase, self.constant) a2,b2 = getattr(other.phase, self.constant) m = self.mass + other.mass diff --git a/test/python/test_thermo.py b/test/python/test_thermo.py index 5d6af0bf18..fd3f9e35c1 100644 --- a/test/python/test_thermo.py +++ b/test/python/test_thermo.py @@ -1681,6 +1681,14 @@ def test_add(self): self.assertNear(q1.V + q2.V, q3.V) self.assertArrayNear(q1.X*q1.moles + q2.X*q2.moles, q3.X*q3.moles) + def test_add_errors(self): + q1 = ct.Quantity(self.gas, mass=5) + q2 = ct.Quantity(self.gas, mass=5) + q1.constant = q2.constant = 'HP' + q2.TP = q1.T, 1.2 * q1.P + with pytest.raises(ValueError, match="pressure is not equal"): + q3 = q1 + q2 + def test_equilibrate(self): self.gas.TPX = 300, 101325, 'CH4:1.0, O2:0.2, N2:1.0' q1 = ct.Quantity(self.gas)