Skip to content

fix: Initialize unassigned total_soil_depth in soilLiqFlx.f90#621

Open
DarriEy wants to merge 1 commit intoCH-Earth:develop_sundialsfrom
DarriEy:fix/uninitialized-total-soil-depth
Open

fix: Initialize unassigned total_soil_depth in soilLiqFlx.f90#621
DarriEy wants to merge 1 commit intoCH-Earth:develop_sundialsfrom
DarriEy:fix/uninitialized-total-soil-depth

Conversation

@DarriEy
Copy link

@DarriEy DarriEy commented Feb 11, 2026

Summary

  • Initialize the local variable total_soil_depth in update_surfaceFlx_liquidFlux_computation_max_infiltration_rate (soilLiqFlx.f90)
  • The variable is declared (line 865) but never assigned, causing undefined behavior
  • Computes total_soil_depth = sum(in_surfaceFlx % mLayerDepth), consistent with how sum(mLayerDepth) is used inline in the master/develop branches

Problem

The Green-Ampt and TOPMODEL infiltration formulae divide by total_soil_depth (via depthWettingFront). When the variable is uninitialized:

  • Linux x86-64 / gfortran 13: Stack reads as 0.0, causing depthWettingFront = 0.0 and 0/0 = NaN in the infiltration rate. The NaN propagates through the flux vector into computResid:
    FATAL ERROR: .../computResid/NaN in residuals
    Note: The following floating-point exceptions are signalling: IEEE_INVALID_FLAG
    
  • macOS / ARM: The bug is masked because stack memory at that address happens to contain a non-zero value from a previous call frame.

Diagnosis

Debug prints added around the Green-Ampt computation confirmed:

DGA avCap= 0.798  rzLiq= 0.325  rzIce= 0.0
DGA dwf=   0.0    tsd=   0.0     ← total_soil_depth is uninitialized (zero)
DGA-GA Ks= 1.39E-06  wfs= 0.3   xMI= NaN

Building with -finit-real=snan confirms the variable is never assigned — the entire column 5 of the Jacobian becomes signaling NaN.

Test plan

  • Verified fix on Ubuntu 24.04 / WSL2 / gfortran 13.3.0 with a 3-year SUMMA simulation (Bow at Banff, lumped basin, infRateMax=GreenAmpt, nrgConserv=enthalpyForm)
  • Confirmed total_soil_depth value matches expected soil column depth (1.0 m for test domain)
  • Simulation completes with "finished simulation successfully"

🤖 Generated with Claude Code

The local variable `total_soil_depth` is declared (line 865) but never
assigned in `update_surfaceFlx_liquidFlux_computation_max_infiltration_rate`.
It is used in the Green-Ampt and TOPMODEL infiltration formulae, where
it appears in denominators (e.g., `wettingFrontSuction/depthWettingFront`
with `depthWettingFront = (rootZoneLiq/availCapacity) * min(rootingDepth,
total_soil_depth)`).

On platforms where the stack is not zero-initialized (e.g., Linux x86-64
with gfortran 13), `total_soil_depth` reads as 0.0, making
`depthWettingFront = 0.0` and causing 0/0 = NaN in the infiltration
rate computation. This NaN propagates through the flux vector into
`computResid`, producing the error:

    FATAL ERROR: .../computResid/NaN in residuals

On macOS/ARM the bug is masked because the stack memory at that address
happens to contain a non-zero value from a previous call.

Fix: Compute `total_soil_depth = sum(in_surfaceFlx % mLayerDepth)`
before the first use, consistent with how the upstream master/develop
branches use `sum(mLayerDepth)` inline.
@ashleymedin
Copy link
Contributor

Thanks Darri -- this is now fixed so we can close this without merging. @wknoben

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