Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quantity addition in Python is incorrect when molar basis is used. #1493

Closed
peterscherpelz opened this issue May 23, 2023 · 2 comments · Fixed by #1517
Closed

Quantity addition in Python is incorrect when molar basis is used. #1493

peterscherpelz opened this issue May 23, 2023 · 2 comments · Fixed by #1517

Comments

@peterscherpelz
Copy link

Problem description

Quantity addition in Python is incorrect when molar basis is used.

Steps to reproduce

Create two quantities and set their basis to 'molar' before adding them. See script below.

Behavior

Enthalpy is not conserved even when both Quantities are set to constant='HP'. The resulting Quantity is different than if basis is kept as mass.

System information

  • Cantera version: 2.6.0
  • OS: Ubuntu 20.04
  • Python 3.8:

Attachments

import cantera as ct

gas = ct.Solution('gri30.yaml')

# Stream A (air)
A = ct.Quantity(gas, constant='HP')
A.TPX = 300.0, ct.one_atm, 'O2:0.21, N2:0.78, AR:0.01'
A.moles = 1
# Stream B (methane)
B = ct.Quantity(gas, constant='HP')
B.TPX = 1200.0, ct.one_atm, 'CH4:1'
B.moles = 0.42

A.basis = 'molar'
B.basis = 'molar'

print(A.enthalpy + B.enthalpy)
# Gives -8521222

# Compute the mixed state
M = A + B
print(M.enthalpy)
# Gives -5408171

In contrast, when not setting to molar basis, everything works fine:

import cantera as ct

gas = ct.Solution('gri30.yaml')

# Stream A (air)
A = ct.Quantity(gas, constant='HP')
A.TPX = 300.0, ct.one_atm, 'O2:0.21, N2:0.78, AR:0.01'
A.moles = 1
# Stream B (methane)
B = ct.Quantity(gas, constant='HP')
B.TPX = 1200.0, ct.one_atm, 'CH4:1'
B.moles = 0.42

print(A.enthalpy + B.enthalpy)
# Gives -8521222

# Compute the mixed state
M = A + B
print(M.enthalpy)
# Gives -8521222
@peterscherpelz
Copy link
Author

While I stumbled across this while using cantera, if my analysis is correct, the problem is fairly straightforward. See https://github.com/Cantera/cantera/blob/main/interfaces/cython/cantera/composite.py#L314 . Note that quantities are fetched from the underlying phase without checking that a mass basis is used - but they're always multiplied by the mass, even though the basis may have been set to molar.

speth added a commit to speth/cantera that referenced this issue Jun 29, 2023
@peterscherpelz
Copy link
Author

Thanks @speth for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

1 participant