Skip to content

Fix 1D flow perturbation applying random factor twice (index aliasing)#1397

Open
sbryngelson wants to merge 1 commit intoMFlowCode:masterfrom
sbryngelson:fix/1d-perturb-flow-double-application
Open

Fix 1D flow perturbation applying random factor twice (index aliasing)#1397
sbryngelson wants to merge 1 commit intoMFlowCode:masterfrom
sbryngelson:fix/1d-perturb-flow-double-application

Conversation

@sbryngelson
Copy link
Copy Markdown
Member

@sbryngelson sbryngelson commented May 5, 2026

Summary

Fixes #1369.

In s_perturb_surrounding_flow (src/pre_process/m_perturbation.fpp), the original code was:

q_prim_vf(eqn_idx%mom%end)%sf(i, j, k) = rand_real*q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k)
q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k) = (1._wp + rand_real)*q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k)

In 1D, eqn_idx%mom%end == eqn_idx%mom%beg (same slot), so:

  • Line 1 writes rand * v0 into the only velocity slot
  • Line 2 reads the now-modified slot and writes (1 + rand) * rand * v0

The perturbation is applied twice, giving rand*(1+rand)*v0 instead of (1+rand)*v0.

Fix

Save the original mom%beg value before any writes, scale mom%beg first, then only assign mom%end when num_vels > 1:

v_beg = q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k)
q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k) = (1._wp + rand_real)*v_beg
if (num_vels > 1) q_prim_vf(eqn_idx%mom%end)%sf(i, j, k) = rand_real*v_beg

This also correctly preserves multi-D behaviour: mom%end gets rand * v_beg (the original x-velocity), not rand*(1+rand)*v_beg.

Verification

Ran a 1D case (m=9, v0=1.0, perturb_flow_mag=0.1) and read the post-pre_process velocity field. All 10 cell values fell in [1.0, 1.1] — consistent with (1 + rand) * v0 where rand ∈ [0, 0.1]. Under the old bug the values would have been near zero (rand*(1+rand)*v0 ≪ 1). Not in the test suite, so verified manually.

…iasing

In 1D, eqn_idx%mom%end == eqn_idx%mom%beg (same slot), so the old code
wrote rand*v0 into the slot then read that modified value to compute
(1+rand)*rand*v0, doubling the perturbation.

Fix: save the original mom%beg value before any writes, scale mom%beg
first, and only assign mom%end when num_vels > 1 (i.e., multi-D).

Fixes MFlowCode#1369
@sbryngelson sbryngelson marked this pull request as ready for review May 6, 2026 14:07
@qodo-code-review
Copy link
Copy Markdown
Contributor

ⓘ You've reached your Qodo monthly free-tier limit. Reviews pause until next month — upgrade your plan to continue now, or link your paid account if you already have one.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

Warning

Rate limit exceeded

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

To continue reviewing without waiting, purchase usage credits in the billing tab.

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f2c32410-ef93-46e0-ad88-9cd5a0fdc9a3

📥 Commits

Reviewing files that changed from the base of the PR and between d513442 and 381ef1e.

📒 Files selected for processing (1)
  • src/pre_process/m_perturbation.fpp

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.

@sbryngelson
Copy link
Copy Markdown
Member Author

Automated Code Review

Summary: Fix is correct. One suggestion.

Strengths

The fix is mechanically correct:

  • num_vels > 1 is the right guard — it uses the same quantity that determines whether mom%beg and mom%end are aliased, and also correctly handles the MHD case where num_vels = 3 even in 1D
  • v_beg correctly typed as real(wp) — promotes stp field data to wp for computation, consistent with project conventions
  • 2D/3D behaviour exactly preserved: mom%beg gets (1+rand)*v_beg, mom%end gets rand*v_beg — identical to the original code's output in non-aliased cases
  • num_vels is guaranteed initialized before s_perturb_surrounding_flow is ever called

Suggestion

No automated test covers perturb_flow. Since the bug went undetected, a programmatic test case in toolchain/mfc/test/cases.py would prevent regression — a simple 1D case with perturb_flow: T, perturb_flow_fluid: 1, perturb_flow_mag: 0.1 whose golden output should fall in [1.0, 1.1] for a unit-velocity IC.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.75%. Comparing base (d513442) to head (381ef1e).

Files with missing lines Patch % Lines
src/pre_process/m_perturbation.fpp 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1397      +/-   ##
==========================================
- Coverage   64.75%   64.75%   -0.01%     
==========================================
  Files          71       71              
  Lines       18721    18722       +1     
  Branches     1551     1552       +1     
==========================================
  Hits        12123    12123              
- Misses       5640     5641       +1     
  Partials      958      958              

☔ 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

None yet

Development

Successfully merging this pull request may close these issues.

bug: 1D flow perturbation applies random factor twice due to index aliasing

1 participant