Skip to content

Declare constants at the native precision of MPAS #1282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kuanchihwang
Copy link
Contributor

@kuanchihwang kuanchihwang commented Jan 28, 2025

When building MPAS as a dynamical core, certain constants in the mpas_constants module are imported from the physconst module, which is a part of CAM/CAM-SIMA. However, multiple issues arise if the precision of those constants differs from MPAS.

For example, building MPAS in single precision mode with CAM-SIMA fails due to multiple occurrences of type mismatch between actual and dummy arguments.

mpas_geometry_utils.F:885:157:

  885 |          call mpas_log_write('$r', MPAS_LOG_ERR, realArgs=(/mpas_triangle_signed_area_sphere(a,b,c,sphereRadius) - pii/2.0_RKIND*sphereRadius*sphereRadius/))
      |                                                                                                                                                             1
Error: Type mismatch in argument 'realargs' at (1); passed REAL(8) to REAL(4)

Here, pii is declared by CAM-SIMA to be double precision, and it causes unintended floating-point promotion in the expression.

The solution is to ensure that constants in the mpas_constants module are declared at the native precision of MPAS. Note that CAM does not support running MPAS in single precision mode at all, so the above build error cannot be reproduced.

In addition, some best practices of modern Fortran have been implemented.

For stand-alone MPAS, the compiled executables stay bitwise identical, with or without this PR. No further tests are needed. For CAM/CAM-SIMA, it generates bitwise identical model results, with or without this PR. Additionally for CAM-SIMA, its regression tests all pass, indicating identical model results to the previous baseline.

When building MPAS as a dycore, certain constants in the `mpas_constants` module are
imported from the `physconst` module, which is a part of CAM/CAM-SIMA. However,
multiple issues arise if the precision of those constants differs from MPAS.

For example, building MPAS in single precision mode with CAM-SIMA fails due to
multiple occurrences of type mismatch between actual and dummy arguments.

mpas_geometry_utils.F:885:157:

  885 |          call mpas_log_write('$r', MPAS_LOG_ERR, realArgs=(/mpas_triangle_signed_area_sphere(a,b,c,sphereRadius) - pii/2.0_RKIND*sphereRadius*sphereRadius/))
      |                                                                                                                                                             1
Error: Type mismatch in argument 'realargs' at (1); passed REAL(8) to REAL(4)

Here, `pii` is declared by CAM-SIMA to be double precision, and it causes unintended
floating-point promotion in the expression.

The solution is to ensure that constants in the `mpas_constants` module are declared
at the native precision of MPAS.
Copy link
Collaborator

@abishekg7 abishekg7 left a comment

Choose a reason for hiding this comment

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

Overall looks good. I did have a few comments and question though.

@kuanchihwang kuanchihwang force-pushed the staging/dycore-with-native-constant-precision branch from 0bee41d to a0f530b Compare February 20, 2025 00:08
@mgduda mgduda requested a review from abishekg7 February 20, 2025 01:11
Copy link
Collaborator

@abishekg7 abishekg7 left a comment

Choose a reason for hiding this comment

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

Looks good!

@mgduda
Copy link
Contributor

mgduda commented Feb 20, 2025

@kuanchihwang The code changes themselves look good. However, I'd like to request a few minor changes to the commit messages so that they more specifically describe what is being modified. For example, the commit messages

Enforce implicit none at the module level

Enforce `implicit none` at the module level so it applies everywhere
within. It is also a best practice in Fortran.

and

Trim trailing whitespaces

give no indication of what code is affected.

Could you revisit the commit messages and ensure that they make it clear what module, file, or part of the model is being affected? I'm not suggesting we add a list of modified files to each commit message, but rather, reference module or file names in the text. For example, the last commit message I mentioned above could become:

Clean up whitespace in the mpas_constants module

Several instances of trailing spaces have been removed, and a blank line has
been added above the 'contains' statement in the mpas_constants module for
consistency with other MPAS modules.

In the `mpas_constants` module, enforce `implicit none` at the module level
so it applies everywhere within. It is also a best practice in Fortran.
…e level of `mpas_constants`

A blanket `use` statement imports all public entities from that module. For
the case here, it causes namespace pollution at the module level of
`mpas_constants`. Also, it is difficult to tell where the imported entities
come from.

Therefore, `use` statements should always include the `only` option. It is
also a best practice in Fortran.
It is a best practice in Fortran to always specify the accessibility of
module variables (as well as procedures).

For the case here, `RKIND` is imported from `mpas_kind_types`. Due to
the lack of accessibility attributes, other modules that
`use mpas_constants` also have access to `RKIND`.

Therefore, avoid causing unexpected namespace pollution to other
modules by specifying explicit accessibility attributes.
Several instances of trailing spaces have been removed, and a blank line has
been added above the `contains` statement in the `mpas_constants` module for
consistency with other MPAS modules.
@kuanchihwang kuanchihwang force-pushed the staging/dycore-with-native-constant-precision branch from a0f530b to 1353687 Compare February 21, 2025 23:29
@kuanchihwang
Copy link
Contributor Author

I have amended the commit messages to include more specific information about what they modify. The commit contents remain the same as can be seen by comparison.

@mgduda mgduda merged commit b669db1 into MPAS-Dev:develop Feb 28, 2025
@kuanchihwang kuanchihwang deleted the staging/dycore-with-native-constant-precision branch February 28, 2025 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants