Skip to content

Comments

Fix MUSCL THINC right-state using already-overwritten left-state values#1181

Open
sbryngelson wants to merge 1 commit intoMFlowCode:masterfrom
sbryngelson:fix/muscl-thinc-overwrite
Open

Fix MUSCL THINC right-state using already-overwritten left-state values#1181
sbryngelson wants to merge 1 commit intoMFlowCode:masterfrom
sbryngelson:fix/muscl-thinc-overwrite

Conversation

@sbryngelson
Copy link
Member

@sbryngelson sbryngelson commented Feb 21, 2026

Summary

Severity: HIGH — right-state reconstruction uses overwritten left-state values.

File: src/simulation/m_muscl.fpp, lines 285-301

In the MUSCL-THINC interface reconstruction, the left-state values (vL_rs_vf for contxb, contxe, and advxb) are overwritten during left reconstruction. The right-state reconstruction then divides by these already-overwritten values instead of the original cell-center values. While the math accidentally cancels (left THINC value / left THINC value = original ratio), this relies on exact cancellation and is fragile.

Before

! Left reconstruction overwrites vL_rs_vf
vL_rs_vf(j,k,l,contxb) = vL_rs_vf(j,k,l,contxb) / vL_rs_vf(j,k,l,advxb) * aTHINC
vL_rs_vf(j,k,l,advxb) = aTHINC
! Right reconstruction uses the overwritten values
vR_rs_vf(j,k,l,contxb) = vL_rs_vf(j,k,l,contxb) / vL_rs_vf(j,k,l,advxb) * aTHINC
!                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overwritten!

After

! Save original density ratios before any overwrites
rho_b = vL_rs_vf(j,k,l,contxb) / vL_rs_vf(j,k,l,advxb)
rho_e = vL_rs_vf(j,k,l,contxe) / (1._wp - vL_rs_vf(j,k,l,advxb))
! Both reconstructions use the saved ratios
vL_rs_vf(j,k,l,contxb) = rho_b * aTHINC_L
vR_rs_vf(j,k,l,contxb) = rho_b * aTHINC_R

Why this went undetected

The algebraic cancellation makes this produce correct results in most cases. It would only fail if the THINC clamping pushed aTHINC to exactly 0 or 1, causing 0/0.

Test plan

  • Run MUSCL-THINC interface compression test
  • Verify left and right reconstructed states are physically consistent

🤖 Generated with Claude Code

Fixes #1202

Copilot AI review requested due to automatic review settings February 21, 2026 03:23
@codeant-ai
Copy link
Contributor

codeant-ai bot commented Feb 21, 2026

CodeAnt AI is reviewing your PR.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 21, 2026

Warning

Rate limit exceeded

@sbryngelson has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 28 minutes and 26 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codeant-ai codeant-ai bot added the size:S This PR changes 10-29 lines, ignoring generated files label Feb 21, 2026
@codeant-ai
Copy link
Contributor

codeant-ai bot commented Feb 21, 2026

Nitpicks 🔍

🔒 No security issues identified
⚡ Recommended areas for review

  • Division by zero
    The new code computes rho_b and rho_e by dividing by vL_rs_vf_${XYZ}$(...,advxb) and by (1 - vL_rs_vf_${XYZ}$(...,advxb)). If advxb equals 0 or 1 (or is extremely close), these divisions can produce infinities or very large values, causing NaNs/instabilities downstream. The denominators need a safe-guard (clamping or small epsilon) before division.

  • Unbounded ratios
    rho_b and rho_e are used directly to scale contxb/contxe after being computed. If the denominator is very small the computed ratios may be nonphysical (negative, >1, or huge), which will produce unphysical volume fractions when later multiplied by aTHINC. Consider clamping or validating rho_b/rho_e to a physically consistent range before applying.

  • GPU private list change
    The GPU parallel-loop private list was extended to include rho_b and rho_e. Verify these variables are fully local and initialized per iteration on the device and that their use introduces no unintended dependencies or extra register pressure. Also confirm the added temporaries do not exceed device resource limits for large collapse values.

@codeant-ai
Copy link
Contributor

codeant-ai bot commented Feb 21, 2026

CodeAnt AI finished reviewing your PR.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes MUSCL-THINC right-state reconstruction by preserving pre-overwrite density ratios from the left state and reusing them for both left and right reconstructions.

Changes:

  • Save pre-reconstruction density ratios (rho_b, rho_e) before THINC overwrites vL_rs_vf_* values.
  • Update left/right reconstructions to use the saved ratios instead of dividing by potentially overwritten left-state fields.

cubic-dev-ai[bot]

This comment was marked as off-topic.

The right reconstruction divides by left-state values that were already
overwritten during left reconstruction. Save original density ratios
(contxb/advxb and contxe/(1-advxb)) before left reconstruction and
reuse them for both left and right states.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Feb 21, 2026

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.05%. Comparing base (84c46e0) to head (66a6a8c).

Files with missing lines Patch % Lines
src/simulation/m_muscl.fpp 0.00% 6 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1181   +/-   ##
=======================================
  Coverage   44.05%   44.05%           
=======================================
  Files          70       70           
  Lines       20498    20496    -2     
  Branches     1990     1990           
=======================================
  Hits         9030     9030           
+ Misses      10329    10327    -2     
  Partials     1139     1139           

☔ View full report in Codecov by Sentry.
📢 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files

Development

Successfully merging this pull request may close these issues.

Grid stretching uses wrong array bounds for y_cc and z_cc

1 participant