Skip to content

Restore the slope sign in the SUPERBEE limiter - #2049

Open
aliencaocao wants to merge 2 commits into
MFlowCode:masterfrom
aliencaocao:fix/muscl-superbee-sign
Open

aliencaocao wants to merge 2 commits into
MFlowCode:masterfrom
aliencaocao:fix/muscl-superbee-sign

Conversation

@aliencaocao

@aliencaocao aliencaocao commented Oct 13, 2026 •

Copy link
Copy Markdown

Fixes #1819.

Change

In src/simulation/m_muscl.fpp the SUPERBEE branch computes max(min(2|a|,|b|), min(|a|,2|b|)), which is always positive. This PR adds the same sign restoration the minmod and MC branches already use:

if (slopeL < 0._wp) slope = -slope

It also adds the missing sign(a) factor to the SUPERBEE line of docs/documentation/equations.md. The documented formula then matches minmod, MC, and the literature.

Why this is the correct slope

The limiter follows the SUPERBEE slope of Roe (1986), written with its sign factor in De Vuyst et al. (2016), Section 2.1 as S(a, b) = sgn(a) · 1(ab ≥ 0) · max[min(|a|, 2|b|), min(2|a|, |b|)]. With a = qᵢ₊₁ − qᵢ and b = qᵢ − qᵢ₋₁, it is

σᵢ = sign(a) · max( min(2|a|, |b|), min(|a|, 2|b|) )    if a·b > 0,   0 otherwise
q(i∓½) = qᵢ ∓ ½ σᵢ

Inside the if, both differences share one sign. sign(slopeL) is therefore the sign of the slope, and the magnitude line already matches the formula. The only missing piece was the sign. For increasing data, the result is bit for bit identical to the original code's result.

Before and after

The case is the same as in the issue: a smooth density bump that a uniform wind carries once around a periodic box. The exact answer after one lap is the starting shape.

pr pr
  • Exact: peak 2.00.
  • Before: the peak is 1.48 and the RMS error is 0.060. The density forms a lopsided smear, up to 0.58 away from the starting shape.
  • After: the peak is 1.97 and the RMS error is 0.0064. The round bump stays in place, at most 0.06 away from the starting shape. The slightly flattened top is the usual clipping of smooth extrema by TVD limiters, whose accuracy drops to first order at local extrema.

Tests and goldens

The suite already contains SUPERBEE cases. Their goldens were generated with the sign error and change with this PR. The goldens were regenerated on CPU with GCC 16.2 in double precision without case optimization (./mfc.sh test --generate --only ...):

  • 01DC0251 3D -> muscl_order=2 -> muscl_eps=0 -> muscl_lim=5
  • 347964C3 2D -> muscl_order=2 -> muscl_eps=0 -> muscl_lim=5
  • 3EE4302C 1D -> muscl_order=2 -> muscl_lim=5
  • 6B227FF5 2D -> muscl_order=2 -> muscl_lim=5
  • 71258175 1D -> muscl_order=2 -> muscl_eps=0 -> muscl_lim=5
  • A7C19B7B 3D -> muscl_order=2 -> muscl_lim=5

Every other test in the suite passes unchanged against its existing golden (from a full suite run with the same compiler and flags).

Contribution Policy

All contributions are expected to demonstrate:

  • A clear understanding of the codebase
  • Alignment with product direction
  • Thoughtful reasoning behind changes
  • Evidence of real-world usage or hands-on experience with the problem

If these expectations are not met, we would prefer to implement the changes ourselves rather than spend time reviewing low-effort submissions.

Acknowledgement

  • I confirm this PR meets the above expectations and reflects my own understanding and real-world context.

PR template credit: junegunn

SUPERBEE built the limited slope from absolute values and never
restored the sign of the slope differences, unlike minmod and MC.
equations.md gains the matching sign(a) factor. All six SUPERBEE
goldens change.

Fixes MFlowCode#1819.
Copilot AI balanced review requested due to automatic review settings October 13, 2026 19:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Claude Code Review

Head SHA: a91eb2b

Files changed:

  • 2
  • docs/documentation/equations.md
  • src/simulation/m_muscl.fpp

Findings:

  • medium src/simulation/m_muscl.fpp: The fix is correct. The superbee expression -min(-min(2|a|,|b|), -min(|a|,2|b|)) equals max(min(2|a|,|b|), min(|a|,2|b|)), which is always non-negative. Before this change, a negative-slope region got a positive limited slope. The added if (slopeL < 0._wp) slope = -slope matches the minmod and MC branches, and the equations.md update matches the code. However, this changes CFD results for every case with muscl_lim=5. toolchain/mfc/test/cases.py lines 559-563 include muscl_lim=5 cases. The diff touches no golden files. Unless those cases are regenerated, they will fail in CI. Regenerate the affected golden files and state in the PR that the change in results is expected, since CLAUDE.md requires verification for PRs that change CFD results. Confidence: high on the sign fix, medium that golden files need updating.
  • medium src/simulation/m_muscl.fpp: The van Albada and van Leer branches need no sign flip, because their formulas keep the sign of the slopes. I found no other missing sign handling in this hunk.

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 61.89%. Comparing base (47568a0) to head (a91eb2b).

Files with missing lines Patch % Lines
src/simulation/m_muscl.fpp 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2049      +/-   ##
==========================================
- Coverage   61.90%   61.89%   -0.01%     
==========================================
  Files          86       86              
  Lines       22885    22886       +1     
  Branches     3369     3370       +1     
==========================================
  Hits        14166    14166              
  Misses       6220     6220              
- Partials     2499     2500       +1     

☔ 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.

@sbryngelson sbryngelson reopened this Oct 14, 2026
@wilfonba

Copy link
Copy Markdown
Collaborator

@sbryngelson I am confident this change is correct and an improvement to the code. It is ready to merge once the runners finish. It's a one line source change + golden file regeneration for affected numerics, so it should pass.

@github-actions

Copy link
Copy Markdown

Lines of Code

File Lines Diff
src/simulation/m_muscl.fpp 187 +1
Directory Lines Diff
simulation 28527 +1
total 47586 +1

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Bug: SUPERBEE limiter returns a positive slope for decreasing data

4 participants