Skip to content

Avoid division by zero in getMixDiffCoeffsMass - #2191

Merged
speth merged 2 commits into
Cantera:mainfrom
g3bk47:patch-12
Nov 24, 2026
Merged

speth merged 2 commits into
Cantera:mainfrom
g3bk47:patch-12

Conversation

@g3bk47

@g3bk47 g3bk47 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request

In the current implementation of getMixDiffCoeffsMass, a mixture where the mass fraction of one species is 1 will cause a division by zero when calculating sum2:

sum2 *= p * m_molefracs[k] / (mmw - m_mw[k]*m_molefracs[k]);
d[k] = 1.0 / (sum1 + sum2);

In principle, this is not wrong, since the next line divides by sum2 so that d[k] is correctly set to zero. However, if Cantera is running in an environment where floating point exceptions are trapped, the code fails:

import cantera as ct
import ctypes

gas = ct.Solution("gri30.yaml")
gas.TPX = 300, 1e5, "CH4:1"

# enable trapping on division by zero (0x04)
ctypes.CDLL("libm.so.6").feenableexcept(0x04)

print(gas.mix_diff_coeffs) # works
print(gas.mix_diff_coeffs_mass) # fails

The other diffusion coefficient functions (getMixDiffCoeffs and getMixDiffCoeffsMole) explicitly handle this case:

if (sum2 <= 0.0) {

if (sum2 <= 0.0) {

The proposed fix is to rearrange the calculation to

double denom = mmw - m_mw[k]*m_molefracs[k];
d[k] = denom / std::max(Tiny, p*(denom*sum1 + m_molefracs[k]*sum2));

This should not significantly affect performance, correctly recovers d[k] = 0 for unity mass fraction, makes the code safe to use in environments with floating point trapping and also makes the behavior of getMixDiffCoeffsMass consistent with its counterparts. The treatment with std::max(Tiny, ...) is consistent with other parts of the transport code, e.g.:

m_molefracs[k] = std::max(Tiny, m_molefracs[k]);

AI Statement (required)

  • No generative AI was used. This contribution was written entirely without AI assistance.

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • AI Statement is included
  • The pull request is ready for review

@g3bk47
g3bk47 marked this pull request as ready for review November 5, 2026 21:35
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.45%. Comparing base (096972d) to head (5ff19e4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2191      +/-   ##
==========================================
- Coverage   77.45%   77.45%   -0.01%     
==========================================
  Files         470      470              
  Lines       57184    57183       -1     
  Branches     9195     9195              
==========================================
- Hits        44292    44291       -1     
  Misses       9823     9823              
  Partials     3069     3069              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@speth speth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @g3bk47. I have just one very minor suggestion.

Comment thread src/transport/GasTransport.cpp Resolved
@speth
speth merged commit b3377c7 into Cantera:main Nov 24, 2026
50 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants