Skip to content

Reject chem_params%diffusion without chemistry - #2108

Merged
sbryngelson merged 1 commit into
MFlowCode:masterfrom
platelett:codex/fix-chem-diffusion-without-chemistry
Oct 25, 2026
Merged

sbryngelson merged 1 commit into
MFlowCode:masterfrom
platelett:codex/fix-chem-diffusion-without-chemistry

Conversation

@platelett

Copy link
Copy Markdown
Contributor

Fixes #1908.

A case that sets chem_params%diffusion = 'T' without chemistry = 'T' passes ./mfc.sh validate, then segfaults on the first time step. This PR rejects that case in the validator. It also adds the chemistry guard that #1908 asks for to the matching deallocation. No working case changes and no goldens move.

Why it crashes

s_initialize_rhs_module allocates the arrays the diffusion path writes only under if (chemistry):

  • the species slots of flux_src_n;
  • flux_src_n(E), when nothing else allocates it.

Several sites test chem_params%diffusion on its own, so they still run in a non-reacting case:

  • m_riemann_state.fpp: if (chem_params%diffusion) zeroes flux_src_vf(E:species%end).
  • m_rhs.fpp: s_compute_additional_physics_rhs runs on viscous .or. surface_tension .or. chem_params%diffusion .or. heat_conduction. Inside it, each direction's if (chem_params%diffusion) block reads flux_src_n(E).

For example, examples/1D_sodshocktube with that one line added:

 Simulating a regular 399x0x0 case on 1 rank(s) on CPUs.
 [  0%] step 1 of 21 (t_step 0) ...
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Change

  • The rule. check_chemistry gets one rule, next to the other chemistry checks:
    self.prohibit(diffusion and not chemistry, "chem_params%diffusion requires chemistry = T")
    It follows the existing "parameter set without its feature" checks, such as sigma is set but surface_tension is not enabled. Guarding the six-plus Fortran sites instead would spread one condition over several files.
  • The deallocation. s_finalize_rhs_module gets the chemistry .and. guard its allocation already has, as flux_src_n energy-slot deallocation lacks the chemistry guard its allocation has #1908 asks. With the new rule it is unreachable, but allocate and deallocate are now symmetric.
  • Tests. Two unit tests in toolchain/mfc/test_case_validator.py: one for the rejection, and one showing that diffusion with chemistry is still accepted.

Side effects

  • A case with chem_params%diffusion = 'T' and no chemistry is now rejected at validation. It used to crash on step 1.
  • All 20 examples that set chem_params%diffusion also set chemistry, so they still validate.
  • No solver results change.
Verification
Check Result
Sod shock tube + chem_params%diffusion = 'T', master (GNU 13.4 + Open MPI, CPU) passes validation, then SIGSEGV on step 1
Same case, this PR Case parameter constraint violations: 1. chem_params%diffusion requires chemistry = T
pytest toolchain/mfc/test_case_validator.py 96 passed, including the 2 new tests
All 15 chemistry regression tests, including the diffusion cases 8E56ACC1, 4587725A and 41AD0140 (GNU 13.4 + Open MPI) 15 passed against the existing goldens
./mfc.sh precheck, which validates every example case passes
Full regression suite, GNU 13.3 + Open MPI 4.1.6, CPU (on a branch that also includes our other fixes) 725 passed, 0 failed (39 convergence cases skipped by default)

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.

This PR is from Diablo (Tsinghua University) participating in the SC26 Student Cluster Competition.

Motivation
Diffusion without chemistry passes validation but accesses unallocated flux arrays on the first time step.

Expectation
Reject the unsupported combination before execution and match the allocation and deallocation guards.

Change
The diffusion flux arrays (flux_src_n(E) and the species slots) are
allocated only when chemistry is on, but the Riemann-state zeroing and
the s_compute_additional_physics_rhs loops test chem_params%diffusion
alone. A non-reacting case with chem_params%diffusion = T passed
validation and then segfaulted on the first time step.

Prohibit the combination in check_chemistry, next to the other
chemistry checks, and give the matching deallocation in
s_finalize_rhs_module the chemistry guard its allocation has. Two
validator unit tests cover the rejection and the accepted
chemistry + diffusion case.

Result
Invalid non-reacting diffusion cases are rejected. Chemistry diffusion remains accepted. The precheck passes and regression reference files are unchanged.

Fixes MFlowCode#1908

Co-authored-by: ruru25254649 <ruru.2525.4649@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WPhkY8p1EHhGY7jScizJmC
@platelett
platelett requested a review from a team as a code owner October 25, 2026 09:56
@platelett
platelett requested review from danieljvickers and removed request for a team October 25, 2026 09:56
@github-actions

Copy link
Copy Markdown

Claude Code Review

Head SHA: 26a3d39

Files changed:

  • 3
  • src/simulation/m_rhs.fpp
  • toolchain/mfc/case_validator.py
  • toolchain/mfc/test_case_validator.py

Findings:
No issues found. Checked in step 4: src/simulation/m_rhs.fpp (the new chemistry .and. condition in the deallocation matches the allocation at line 261, which sits inside if (chemistry)), toolchain/mfc/case_validator.py, and toolchain/mfc/test_case_validator.py.

@codecov

codecov Bot commented Oct 25, 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.95%. Comparing base (ee99151) to head (26a3d39).

Files with missing lines Patch % Lines
src/simulation/m_rhs.fpp 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2108   +/-   ##
=======================================
  Coverage   61.95%   61.95%           
=======================================
  Files          86       86           
  Lines       22906    22906           
  Branches     3378     3378           
=======================================
  Hits        14192    14192           
  Misses       6206     6206           
  Partials     2508     2508           

☔ 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 merged commit 91993f1 into MFlowCode:master Oct 25, 2026
63 checks passed
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.

flux_src_n energy-slot deallocation lacks the chemistry guard its allocation has

2 participants