Skip to content

sima0_13_003: Fix model crash in MPAS dynamical core and implement assorted code cleanup - #486

Merged
kuanchihwang merged 8 commits into
ESCOMP:developmentfrom
kuanchihwang:staging/mpas-dycore-fix-indirect-indexing-crash
Apr 14, 2026
Merged

sima0_13_003: Fix model crash in MPAS dynamical core and implement assorted code cleanup#486
kuanchihwang merged 8 commits into
ESCOMP:developmentfrom
kuanchihwang:staging/mpas-dycore-fix-indirect-indexing-crash

Conversation

@kuanchihwang

@kuanchihwang kuanchihwang commented Mar 31, 2026

Copy link
Copy Markdown
Collaborator

Tag name (required for release branches):

sima0_13_003 due to newly-added tests, otherwise bit-for-bit.

Originator(s):

kuanchihwang

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

This PR fixes a model crash in MPAS dynamical core, which is caused by a regression in recent versions of GNU Fortran compiler. When CAM-SIMA is configured with MPAS dynamical core, and is built with GNU Fortran compiler version 14 in debug mode, running the model will lead to a segmentation fault. However, the crash mysteriously disappears when the model is built in optimized mode. Older GNU Fortran compiler version 12 does not exhibit this behavior, neither does Intel Fortran compiler.

Closes #484

Additionally, this PR also brings the following:

  1. Update Python script for generating MPAS namelist definition file
  2. Reduce API surface area by making dyn_inquire_mesh_dimensions private
  3. Add lookup table for mapping indexes from advected to all constituents and refactor the rest of code to use it
  4. Fix incorrect constituent indexing inside set_physics_state_external during dynamics-physics coupling

The rationale behind each change is described in the commit messages.

Describe any changes made to the build system:

None

Describe any changes made to the namelist:

None

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

None

List all files eliminated and why:

None

List all files added and what they do:

None

List all existing files that have been modified, and describe the changes:

M       src/dynamics/mpas/assets/generate_namelist_definition.py
  * Update Python script for generating MPAS namelist definition file
M       src/dynamics/mpas/dyn_comp.F90
  * Add lookup table for mapping indexes from advected to all constituents
M       src/dynamics/mpas/dyn_comp_impl.F90
  * Add lookup table for mapping indexes from advected to all constituents
  * Fix MPAS dycore crash due to regression in recent GNU Fortran compiler
M       src/dynamics/mpas/dyn_coupling_impl.F90
  * Fix indentation
  * Add lookup table for mapping indexes from advected to all constituents
  * Fix MPAS dycore crash due to regression in recent GNU Fortran compiler
  * Fix incorrect constituent indexing
M       src/dynamics/mpas/dyn_grid.F90
  * Make `dyn_inquire_mesh_dimensions` private
M       src/dynamics/mpas/dyn_grid_impl.F90
  * Make `dyn_inquire_mesh_dimensions` private
M       src/dynamics/mpas/dyn_procedures.F90
  * Fix MPAS dycore crash due to regression in recent GNU Fortran compiler
M       src/dynamics/mpas/tests/unit/test_dyn_procedures.pf
  * Fix MPAS dycore crash due to regression in recent GNU Fortran compiler

Regression tests:

SMS_Ln9.ne3pg3_ne3pg3_mg37.FADIAB.derecho_gnu.cam-outfrq_se_cslam (Overall: FAIL)
SMS_Ln9.ne3pg3_ne3pg3_mg37.FKESSLER.derecho_intel.cam-outfrq_se_cslam_multitape (Overall: NLFAIL)

Known failing tests.

SMS_D_Ln9.mpasa120_mpasa120.QPC4.derecho_gnu.cam-outfrq_analy_ic_cam4 (Overall: DIFF)
SMS_D_Ln9.mpasa120_mpasa120.QPC4.derecho_intel.cam-outfrq_analy_ic_cam4 (Overall: DIFF)

Newly-added tests.

The `entry_id_pg` element in the namelist definition XML file was updated to version 2.0
in PR 460. However, the corresponding Python script was not updated.

After updating the Python script, it is also confirmed that:
1. The namelist definition XML file is up to date.
2. The namelist definition XML file is successfully validated against the XML schema at
   CIME/ParamGen/xml_schema/entry_id_pg.xsd.
The `dyn_inquire_mesh_dimensions` procedure does not need to be a public
API because nothing outside the `dyn_grid` module uses it.

Privatize it to reduce API surface area.
CAM-SIMA holds information about all constituents. However, MPAS dynamical core only knows about the advected ones.
This lookup table maps an index within the set of advected constituents to an index within the set of all constituents.

Refactor the rest of code to use it, removing duplicate and error-prone code logic throughout.
When CAM-SIMA is configured with MPAS dynamical core, and is built with GNU Fortran compiler
version 14 in debug mode, running the model will lead to a segmentation fault. However,
the crash mysteriously disappears when the model is built in optimized mode.

Older GNU Fortran compiler version 12 does not exhibit this behavior, neither does
Intel Fortran compiler.

This issue is caused by a regression in recent GNU Fortran compiler, where a procedure
call involving an array argument with indirect indexing is mishandled.

Split out the operations into simpler steps to fix this issue.
The `mpas_dynamical_core % map_constituent_index` type-bound function maps MPAS scalar index to
CAM-SIMA *advected* constituent index, but we are still one step away.

Should also use the `advected_constituent_index` lookup table to map *advected* constituent index to
*all* constituent index.
@kuanchihwang
kuanchihwang temporarily deployed to CI-tests-on-CIRRUS March 31, 2026 20:32 — with GitHub Actions Inactive
@kuanchihwang
kuanchihwang marked this pull request as ready for review March 31, 2026 20:40
@kuanchihwang
kuanchihwang requested a review from nusbaume March 31, 2026 20:40

@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.

Thanks for the bug fix and cleanup @kuanchihwang! To help us catch these potential issues in the future, I was wondering if we could add another set of MPAS+CAM4 regression tests, but this time with debug flags enabled? This should be doable by just adding the following lines to CAM-SIMA/cime_config/testdefs/testlist_cam.xml, ideally at the end of the CAM4 physics tests section:

<test compset="QPC4" grid="mpasa120_mpasa120" name="SMS_D_Ln9" testmods="cam/outfrq_analy_ic_cam4">
    <machines>
      <machine name="derecho" compiler="intel" category="aux_sima"/>
      <machine name="derecho" compiler="gnu" category="aux_sima"/>
    </machines>
    <options>
      <option name="wallclock">00:20:00</option>
      <option name="comment">Aquaplanet CAM4 test with MPAS dycore at 120 km resolution  with compiler debug flags enabled</option>
    </options>
 </test>

I also had one code-related question, but otherwise everything else looked good to me. Thanks again!

Comment thread src/dynamics/mpas/dyn_comp_impl.F90 Outdated
@kuanchihwang
kuanchihwang temporarily deployed to CI-tests-on-CIRRUS April 13, 2026 06:41 — with GitHub Actions Inactive
@jimmielin
jimmielin self-requested a review April 13, 2026 16:56
@nusbaume
nusbaume self-requested a review April 13, 2026 16:57

@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.

Everything looks great to me now. Thanks again @kuanchihwang!

@jimmielin jimmielin 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 @kuanchihwang!

@kuanchihwang kuanchihwang changed the title Fix model crash in MPAS dynamical core and implement assorted code cleanup sima0_13_003: Fix model crash in MPAS dynamical core and implement assorted code cleanup Apr 14, 2026
@kuanchihwang
kuanchihwang merged commit e1fa97d into ESCOMP:development Apr 14, 2026
19 checks passed
@kuanchihwang
kuanchihwang deleted the staging/mpas-dycore-fix-indirect-indexing-crash branch April 15, 2026 00:20
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.

4 participants