subroutine probe_h_vs_eta_residual(grid, ms, bt_work, stage, outer_step)
!! Debug probe: print max|sum_k(h_layer) - (H_ref + bt_eta_end)|
!! over interior cells. In Eulerian-z this is forced to zero
!! by `apply_bt_correction`'s h-rescale. In Lagrangian mode
!! the rescale is skipped, so this residual is what the slow
!! continuity actually drifts to (expected to be FP). Watching
!! how it grows over time across many outer steps is what
!! pins down whether the long-run momentum NaN is FP
!! accumulation in `sum_k(h_layer) - H - bt_eta_end`.
type(hgrid_t), intent(in) :: grid
type(multilayer_state_t), intent(inout) :: ms
type(barotropic_workstate_t), intent(in) :: bt_work
integer, intent(in) :: stage, outer_step
integer :: i, j, k, i0, i1, j0, j1, ig
real(wp) :: max_res, sum_h, target_h, res
if (.not. bcdiag_enabled) return
if (outer_step > bcdiag_step_limit) return
!$acc update self(ms%h_layer, bt_work%bt_eta_end, bt_work%bt_H_ref)
ig = grid%nghost
i0 = ig + 1
i1 = grid%nx_total - ig
j0 = ig + 1
j1 = grid%ny_total - ig
max_res = 0.0_wp
do j = j0, j1
do i = i0, i1
sum_h = 0.0_wp
do k = 1, ms%nz_ml
sum_h = sum_h + ms%h_layer(i, j, k)
end do
target_h = bt_work%bt_H_ref(i, j) + bt_work%bt_eta_end(i, j)
res = abs(sum_h - target_h)
if (res > max_res) max_res = res
end do
end do
write (output_unit, &
'("[bcdiag] step=", i0, " stage=", i0, " ", a40, " max|h_sum - (H+eta_end)|=", es12.4)') &
outer_step, stage, "h_vs_eta_residual", max_res
flush (output_unit)
end subroutine probe_h_vs_eta_residual