Skip to content

Commit

Permalink
[Python] Check pressure is equal when mixing Quantity at constant HP
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Jun 29, 2023
1 parent 36faa94 commit 7127351
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 @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/python/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7127351

Please sign in to comment.