Skip to content

Fix crash in physics_check_data when registry variable has initial_value but not read from file, yet is modified (intent out) from physics scheme - #422

Merged
jimmielin merged 4 commits into
ESCOMP:developmentfrom
jimmielin:hplin/intent_out_check_bugfix
Sep 29, 2025
Merged

Conversation

@jimmielin

Copy link
Copy Markdown
Collaborator

Phew, that is a long title!

Tag name (required for release branches):
Originator(s): @jimmielin

Description (include the issue title, and the keyword ['closes', 'fixes', 'resolves'] followed by the issue number):

This is a proposed fix for a corner case where a variable is introduced into the CAM registry that has an initial_value but never read from file, as well as having an intent(out) or intent(inout) from a physics scheme.

Such a case might happen because a variable might only be set in a scheme's "init" phase, yet used for "run" phases, and thus necessitates its introduction into the registry to persist state across timesteps. However, because this variable needs to be set in the scheme's "init" phase (and thus has intent(out)) it triggers the physics data check routine.

The physics_check_data select case uses the find_input_name_idx to retrieve the name_idx, which is then used to get the actual input name, via phys_var_stdnames(name_idx); however, if a variable is intent(out) yet never read from file, find_input_name_idx in physics_data.F90 returns init_mark_idx = -2, which is out of bounds for phys_var_stdnames, and yet not covered by the guard case statements in the select case in physics_check_data; this commit adds the guard case for init_mark_idx with a comment to denote the corner case's triggering conditions.

Describe any changes made to build system: updates write_init_files.py which handles code generation for physics_inputs.F90.

To replicate

  • checkout jimmielin/CAM-SIMA at branch hplin/gw_drag @ 2cfe2971b05dcbe81fca4b1157a5ca8c408e7f81
  • modify .gitmodules to use jimmielin/atmospheric_physics at b2eabf666d6b06e63e59a429e6a4b5bcc32bfcd7 for ncar-physics external
  • checkout externals
  • create case --compiler gnu --run-unsupported --compset FPHYStest --res ne3pg3_ne3pg3_mg37; select physics suite ./xmlchange CAM_CONFIG_OPTS="--physics-suites gw_cam4 --dyn none"
  • use user_nl_cam
ncdata = '/glade/derecho/scratch/hplin/250826_cam64_109_gwdev.FHIST_C4.ne3pg3.gnu.dbg/run/250826_cam64_109_gwdev.FHIST_C4.ne3pg3.gnu.dbg.cam.h1i.1979-01-01-01800.nc'
ncdata_check = '/glade/derecho/scratch/hplin/250826_cam64_109_gwdev.FHIST_C4.ne3pg3.gnu.dbg/run/250826_cam64_109_gwdev.FHIST_C4.ne3pg3.gnu.dbg.cam.h2i.1979-01-01-01800.nc'

! cam4
pver = 26

! gw options.
effgw_oro              = 0.125D0
gw_apply_tndmax                = .true.
gw_dc          = 0.D0
gw_lndscl_sgh          = .true.
gw_oro_south_fac               = 1.d0
gw_prndl               = 0.25d0
pgwv           = 0
tau_0_ubc              = .false.

observe crash at end of first timestep where physics check data normally runs.

dec2433.hsn.de.hpc.ucar.edu 1: At line 578 of file /glade/derecho/scratch/hplin/250826_sima.FPHYStest_gw.ne3pg3.gnu.dbg/bld/atm/obj/phys_init/physics_inputs.F90
dec2433.hsn.de.hpc.ucar.edu 1: Fortran runtime error: Index '-2' of dimension 1 of array 'phys_var_stdnames' below lower bound of 1
dec2433.hsn.de.hpc.ucar.edu 1:
dec2433.hsn.de.hpc.ucar.edu 1: Error termination. Backtrace:
dec2433.hsn.de.hpc.ucar.edu 1: #0  0x4de52b in __physics_inputs_MOD_physics_check_data
dec2433.hsn.de.hpc.ucar.edu 1:  at /glade/derecho/scratch/hplin/250826_sima.FPHYStest_gw.ne3pg3.gnu.dbg/bld/atm/obj/phys_init/physics_inputs.F90:578
dec2433.hsn.de.hpc.ucar.edu 1: #1  0x4d30b4 in __phys_comp_MOD_phys_timestep_final
dec2433.hsn.de.hpc.ucar.edu 1:  at /glade/u/home/hplin/2508_dev_gw/CAM-SIMA.dev/src/physics/utils/phys_comp.F90:292
dec2433.hsn.de.hpc.ucar.edu 1: #2  0x449495 in __cam_comp_MOD_cam_timestep_final
dec2433.hsn.de.hpc.ucar.edu 1:  at /glade/u/home/hplin/2508_dev_gw/CAM-SIMA.dev/src/control/cam_comp.F90:543
dec2433.hsn.de.hpc.ucar.edu 4: #2  0x449495 in __cam_comp_MOD_cam_timestep_final
dec2433.hsn.de.hpc.ucar.edu 4:  at /glade/u/home/hplin/2508_dev_gw/CAM-SIMA.dev/src/control/cam_comp.F90:543

Describe any changes made to the namelist: N/A

List any changes to the defaults for the input datasets (e.g. boundary datasets): N/A

List all files eliminated and why: N/A

List all files added and what they do: N/A

List all existing files that have been modified, and describe the changes:
(Helpful git command: git diff --name-status development...<your_branch_name>)

M       src/data/write_init_files.py
  - adds guard case to handle init_mark_idx in physics_data_check.

If there are new failures (compared to the test/existing-test-failures.txt file),
have them OK'd by the gatekeeper, note them here, and add them to the file.
If there are baseline differences, include the test and the reason for the
diff. What is the nature of the change? Roundoff?

All B4B

…lue but not read from file, but is modified (intent out) from physics scheme

This is a proposed fix for a corner case where a variable is introduced
into the CAM registry that has an initial_value but never read from file,
as well as having an intent(out) or intent(inout) from a physics scheme.

Such a case might happen because a variable might only be set in a scheme's
"init" phase, yet used for "run" phases, and thus necessitates its introduction
into the registry to persist state across timesteps. However, because this
variable needs to be set in the scheme's "init" phase (and thus has intent(out))
it triggers the physics data check routine.

The physics_check_data select case uses the find_input_name_idx to retrieve
the name_idx, which is then used to get the actual input name, via
phys_var_stdnames(name_idx); however, if a variable is intent(out) yet never
read from file, find_input_name_idx in physics_data.F90 returns init_mark_idx = -2,
which is out of bounds for phys_var_stdnames, and yet not covered by
the guard case statements in the select case in physics_check_data; this commit
adds the guard case for init_mark_idx with a comment to denote the corner
case's triggering conditions.
@jimmielin jimmielin changed the title Fix crash in physics_check_data when registry variable has initial_vaue but not read from file, but is modified (intent out) from physics scheme Fix crash in physics_check_data when registry variable has initial_value but not read from file, but is modified (intent out) from physics scheme Aug 27, 2025
@jimmielin jimmielin changed the title Fix crash in physics_check_data when registry variable has initial_value but not read from file, but is modified (intent out) from physics scheme Fix crash in physics_check_data when registry variable has initial_value but not read from file, yet is modified (intent out) from physics scheme Aug 27, 2025

@nusbaume nusbaume left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the world's most nit-picky nitpick, but otherwise this looks great to me. Thanks for fixing this corner case @jimmielin!

Comment thread src/data/write_init_files.py Outdated
@@ -1415,6 +1415,12 @@ def write_phys_check_subroutine(outfile, host_dict, host_vars, host_imports,
outfile.comment("If variable is a constituent, then do nothing. We'll handle these later", 6)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't relevant for this PR (or anything really), but any chance we can add a period to the end of the We'll handle these later comment? That way my OCD won't fixate on it every time I look at this code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nusbaume, I found another one too when searching for later", ) so I added both of them!

@nusbaume nusbaume added the bug-fix This PR was created to fix a specific bug. label Sep 26, 2025

@peverwhee peverwhee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @jimmielin !

@jimmielin

Copy link
Copy Markdown
Collaborator Author

SIMA regression tests all bit-for-bit passed

@jimmielin jimmielin self-assigned this Sep 29, 2025
@jimmielin
jimmielin merged commit 158c163 into ESCOMP:development Sep 29, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix This PR was created to fix a specific bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants