From 06c649f1ea3af56f61eb6db238232cc2f0751f06 Mon Sep 17 00:00:00 2001 From: Haipeng Lin Date: Thu, 12 Mar 2026 17:54:11 -0400 Subject: [PATCH 1/2] Fixes for snapshot output in GNU debug --- src/control/cam_snapshot_common.F90 | 9 ++++++++- src/physics/cam/physpkg.F90 | 23 +++++++++++++++++++---- src/physics/cam7/physpkg.F90 | 9 +++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/control/cam_snapshot_common.F90 b/src/control/cam_snapshot_common.F90 index 6a342b67be..8fd659113b 100644 --- a/src/control/cam_snapshot_common.F90 +++ b/src/control/cam_snapshot_common.F90 @@ -1235,7 +1235,14 @@ subroutine cam_pbuf_snapshot_all_outfld(lchnk, file_num, pbuf) ! Retrieve the pbuf data. Special handling for certain ! integer-type fields. - if( trim(pbuf_snapshot(i)%ddt_string) == 'clubbtop') then + ! This will allow data written out to not be mangled, + ! and also allow writing out snapshots with debug on in GNU compiler. + if( trim(pbuf_snapshot(i)%ddt_string) == 'clubbtop' .or. & + trim(pbuf_snapshot(i)%ddt_string) == 'ZM_JT' .or. & + trim(pbuf_snapshot(i)%ddt_string) == 'ZM_MAXG' .or. & + trim(pbuf_snapshot(i)%ddt_string) == 'ZM_IDEEP' .or. & + trim(pbuf_snapshot(i)%ddt_string) == 'NMXRGN' .or. & + trim(pbuf_snapshot(i)%ddt_string) == 'ACNUNM') then call pbuf_get_field(pbuf, pbuf_idx, tmpptr2d_int) ! copy into real allocate(tmpptr2d(size(tmpptr2d_int, 1), size(tmpptr2d_int, 2))) diff --git a/src/physics/cam/physpkg.F90 b/src/physics/cam/physpkg.F90 index ad0837ea9b..852f9cd0a3 100644 --- a/src/physics/cam/physpkg.F90 +++ b/src/physics/cam/physpkg.F90 @@ -1531,6 +1531,12 @@ subroutine tphysac (ztodt, cam_in, & ifld = pbuf_get_index('AST') call pbuf_get_field(pbuf, ifld, ast, start=(/1,1,itim_old/), kount=(/pcols,pver,1/) ) + ! zero out local variables that may be written to snapshot for safety. + fh2o(:) = 0._r8 ! used in chem_timestep_tend. + surfric(:) = 0._r8 ! out from vertical_diffusion_tend. + obklen(:) = 0._r8 ! out from vertical_diffusion_tend. + flx_heat(:) = 0._r8 ! first out from gw_drag_cam. + ! ! accumulate fluxes into net flux array for spectral dycores ! jrm Include latent heat of fusion for snow @@ -2416,6 +2422,19 @@ subroutine tphysbc (ztodt, state, & call surface_emissions_set( lchnk, ncol, pbuf ) call elevated_emissions_set( lchnk, ncol, pbuf ) + ! Zero-initialize subroutine-level variables for snapshot + cmfmc(:,:) = 0._r8 + cmfcme(:,:) = 0._r8 + zdu(:,:) = 0._r8 + rliq(:) = 0._r8 + rice(:) = 0._r8 + dlf(:,:) = 0._r8 + dlf2(:,:) = 0._r8 + rliq2(:) = 0._r8 + det_s(:) = 0._r8 + det_ice(:) = 0._r8 + net_flx(:) = 0._r8 + ! !=================================================== ! Dry adjustment @@ -2515,10 +2534,6 @@ subroutine tphysbc (ztodt, state, & dlf(:,:) = 0._r8 end if - ! Zero-initialize subroutine-level variables for snapshot - dlf2(:,:) = 0._r8 - rliq2(:) = 0._r8 - if (trim(cam_take_snapshot_before) == "convect_shallow_tend") then call cam_snapshot_all_outfld_tphysbc(cam_snapshot_before_num, state, tend, cam_in, cam_out, pbuf, & flx_heat, cmfmc, cmfcme, zdu, rliq, rice, dlf, dlf2, rliq2, det_s, det_ice, net_flx) diff --git a/src/physics/cam7/physpkg.F90 b/src/physics/cam7/physpkg.F90 index a3f0c49fe6..492bdbb982 100644 --- a/src/physics/cam7/physpkg.F90 +++ b/src/physics/cam7/physpkg.F90 @@ -1623,6 +1623,15 @@ subroutine tphysac (ztodt, cam_in, & call pbuf_get_field(pbuf, rliqbc_idx, rliqbc) rliq(:ncol) = rliqbc(:ncol) + ! zero out local variables that may be written to snapshot for safety. + fh2o(:) = 0._r8 ! used in chem_timestep_tend. + surfric(:) = 0._r8 ! out from vertical_diffusion_tend. + obklen(:) = 0._r8 ! out from vertical_diffusion_tend. + flx_heat(:) = 0._r8 ! first out from gw_drag_cam. + det_s(:) = 0._r8 ! out from clubb_tend_cam. + det_ice(:) = 0._r8 ! out from clubb_tend_cam. + net_flx(:) = 0._r8 ! out from radiation_tend. + ! ! accumulate fluxes into net flux array for spectral dycores ! jrm Include latent heat of fusion for snow From b7d8ceb2e2c164d5bec12010d511b777c9872fea Mon Sep 17 00:00:00 2001 From: Haipeng Lin Date: Fri, 13 Mar 2026 12:46:22 -0400 Subject: [PATCH 2/2] move zeroing out to further up in tphysbc; also fix cam7/tphysbc --- src/physics/cam/physpkg.F90 | 26 +++++++++++++------------- src/physics/cam7/physpkg.F90 | 11 +++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/physics/cam/physpkg.F90 b/src/physics/cam/physpkg.F90 index 852f9cd0a3..2f0df4c81f 100644 --- a/src/physics/cam/physpkg.F90 +++ b/src/physics/cam/physpkg.F90 @@ -2364,6 +2364,19 @@ subroutine tphysbc (ztodt, state, & call t_stopf('bc_init') + ! Zero-initialize subroutine-level variables for snapshot + cmfmc(:,:) = 0._r8 + cmfcme(:,:) = 0._r8 + zdu(:,:) = 0._r8 + rliq(:) = 0._r8 + rice(:) = 0._r8 + dlf(:,:) = 0._r8 + dlf2(:,:) = 0._r8 + rliq2(:) = 0._r8 + det_s(:) = 0._r8 + det_ice(:) = 0._r8 + net_flx(:) = 0._r8 + !=================================================== ! Global mean total energy fixer !=================================================== @@ -2422,19 +2435,6 @@ subroutine tphysbc (ztodt, state, & call surface_emissions_set( lchnk, ncol, pbuf ) call elevated_emissions_set( lchnk, ncol, pbuf ) - ! Zero-initialize subroutine-level variables for snapshot - cmfmc(:,:) = 0._r8 - cmfcme(:,:) = 0._r8 - zdu(:,:) = 0._r8 - rliq(:) = 0._r8 - rice(:) = 0._r8 - dlf(:,:) = 0._r8 - dlf2(:,:) = 0._r8 - rliq2(:) = 0._r8 - det_s(:) = 0._r8 - det_ice(:) = 0._r8 - net_flx(:) = 0._r8 - ! !=================================================== ! Dry adjustment diff --git a/src/physics/cam7/physpkg.F90 b/src/physics/cam7/physpkg.F90 index 492bdbb982..c5dccb3647 100644 --- a/src/physics/cam7/physpkg.F90 +++ b/src/physics/cam7/physpkg.F90 @@ -2797,6 +2797,17 @@ subroutine tphysbc (ztodt, state, & call t_stopf('bc_init') + ! Zero-initialize subroutine-level variables for snapshot + cmfmc(:,:) = 0._r8 + cmfcme(:,:) = 0._r8 + zdu(:,:) = 0._r8 + rliq(:) = 0._r8 + rice(:) = 0._r8 + dlf(:,:) = 0._r8 + dlf2(:,:) = 0._r8 + rliq2(:) = 0._r8 + net_flx(:) = 0._r8 + !=================================================== ! Global mean total energy fixer !===================================================