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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions src/framework/mpas_constants.F
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,33 @@
!> \brief MPAS Constant Module
!> \author Michael Duda
!> \date 03/27/13
!> \details
!> \details
!> This module provides various constants that can be used in different parts of MPAS.
!> They may or may not be a physical quantity.
!
!-----------------------------------------------------------------------

module mpas_constants

use mpas_kind_types
use mpas_kind_types, only: RKIND

implicit none

public
private :: RKIND

#ifdef MPAS_CAM_DYCORE
use physconst, only : pii => pi
use physconst, only : gravity => gravit
use physconst, only : omega
use physconst, only : a => rearth
use physconst, only : cp => cpair
use physconst, only : rgas => rair
use physconst, only : rv => rh2o
real (kind=RKIND) :: rvord = huge(1.0_RKIND) ! Derived in mpas_constants_compute_derived
real (kind=RKIND) :: cv = huge(1.0_RKIND) ! Derived in mpas_constants_compute_derived
real (kind=RKIND) :: cvpm = huge(1.0_RKIND) ! Derived in mpas_constants_compute_derived
! Set at run-time by `mpas_constants_compute_derived`.
real (kind=RKIND), protected :: pii = huge(1.0_RKIND)
real (kind=RKIND), protected :: a = huge(1.0_RKIND)
real (kind=RKIND), protected :: omega = huge(1.0_RKIND)
real (kind=RKIND), protected :: gravity = huge(1.0_RKIND)
real (kind=RKIND), protected :: rgas = huge(1.0_RKIND)
real (kind=RKIND), protected :: rv = huge(1.0_RKIND)
real (kind=RKIND), protected :: cp = huge(1.0_RKIND)
real (kind=RKIND), protected :: rvord = huge(1.0_RKIND)
real (kind=RKIND), protected :: cv = huge(1.0_RKIND)
real (kind=RKIND), protected :: cvpm = huge(1.0_RKIND)
#else
real (kind=RKIND), parameter :: pii = 3.141592653589793_RKIND !< Constant: Pi
real (kind=RKIND), parameter :: a = 6371229.0_RKIND !< Constant: Spherical Earth radius [m]
Expand All @@ -49,6 +55,7 @@ module mpas_constants
real (kind=RKIND), parameter :: p0 = 1.0e5_RKIND !< Constant: 100000 Pa
real (kind=RKIND), parameter :: prandtl = 1.0_RKIND !< Constant: Prandtl number


contains


Expand All @@ -59,7 +66,7 @@ module mpas_constants
!> \brief Computes derived constants
!> \author Michael Duda
!> \date 8 May 2020
!> \details
!> \details
!> This routine provides a place where physical constants provided by
!> the mpas_constants module may be computed at runtime. For example,
!> if some constants depend on namelist options or other runtime
Expand All @@ -74,9 +81,25 @@ module mpas_constants
!-----------------------------------------------------------------------
subroutine mpas_constants_compute_derived()

implicit none

#ifdef MPAS_CAM_DYCORE
use physconst, only: external_pii => pi
use physconst, only: external_a => rearth
use physconst, only: external_omega => omega
use physconst, only: external_gravity => gravit
use physconst, only: external_rgas => rair
use physconst, only: external_rv => rh2o
use physconst, only: external_cp => cpair

! Convert external constants to the native precision of MPAS (i.e., `RKIND`).

pii = real(external_pii, RKIND)
a = real(external_a, RKIND)
omega = real(external_omega, RKIND)
gravity = real(external_gravity, RKIND)
rgas = real(external_rgas, RKIND)
rv = real(external_rv, RKIND)
cp = real(external_cp, RKIND)

!
! In the case of CAM-MPAS, rgas may depend on a CAM namelist option,
! so physical constants that depend on rgas must be computed here after
Expand Down