Skip to content

Commit

Permalink
Various fixes and style adjustments
Browse files Browse the repository at this point in the history
Fix the following issues related to standard conformance and/or compiler bugs:

* Different character lengths in array constructor.
* Logicals must be compared with `.eqv.` instead of `==`.
* Deeply nested function calls generate wrong results.
* `index_unique` function generates wrong results when the actual argument is
  a character string array.
  • Loading branch information
kuanchihwang committed Apr 17, 2024
1 parent 48f6cee commit 70301e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/dynamics/mpas/driver/dyn_mpas_subdriver.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ pure recursive function parse_stream_name(stream_name) result(var_info_list)
character(*), intent(in) :: stream_name
type(var_info_type), allocatable :: var_info_list(:)

character(64), allocatable :: var_name_list(:)
integer :: i, n, offset
type(var_info_type), allocatable :: var_info_list_append(:)

Expand Down Expand Up @@ -1300,9 +1301,11 @@ pure recursive function parse_stream_name(stream_name) result(var_info_list)
var_info_list = [var_info_list, var_info_list_append]

! Discard duplicate variable information by names.
var_info_list = var_info_list(index_unique(var_info_list(:) % name))
var_name_list = var_info_list(:) % name
var_info_list = var_info_list(index_unique(var_name_list))

deallocate(var_info_list_append)
deallocate(var_name_list)
end function parse_stream_name

!> Return the index of unique elements in `array`, which can be any intrinsic data types, as an integer array.
Expand All @@ -1314,6 +1317,7 @@ pure function index_unique(array)
class(*), intent(in) :: array(:)
integer, allocatable :: index_unique(:)

character(:), allocatable :: array_c(:)
integer :: i, n
logical :: mask_unique(size(array))

Expand All @@ -1329,11 +1333,20 @@ pure function index_unique(array)

select type (array)
type is (character(*))
! Workaround for a bug in Cray wrapper compiler for GNU Fortran.
! When a character string array is passed as the actual argument to the unlimited polymorphic dummy argument,
! its array indexing is mishandled.
allocate(character(len(array)) :: array_c(size(array)))

array_c(:) = array(:)

do i = 1, n
if (.not. any(array(i) == array .and. mask_unique)) then
if (.not. any(array_c(i) == array_c .and. mask_unique)) then
mask_unique(i) = .true.
end if
end do

deallocate(array_c)
type is (integer(int32))
do i = 1, n
if (.not. any(array(i) == array .and. mask_unique)) then
Expand All @@ -1348,7 +1361,7 @@ pure function index_unique(array)
end do
type is (logical)
do i = 1, n
if (.not. any(array(i) == array .and. mask_unique)) then
if (.not. any((array(i) .eqv. array) .and. mask_unique)) then
mask_unique(i) = .true.
end if
end do
Expand Down Expand Up @@ -1552,6 +1565,7 @@ subroutine dyn_mpas_get_global_mesh_dimension(self, &
integer, intent(out) :: ncells_global, nedges_global, nvertices_global, nvertlevels, ncells_max, nedges_max
real(rkind), intent(out) :: sphere_radius

character(*), parameter :: subname = 'dyn_mpas_subdriver::dyn_mpas_get_global_mesh_dimension'
integer, allocatable :: maxedges_value
integer, allocatable :: ncellssolve_value
integer, allocatable :: nedgessolve_value
Expand Down
12 changes: 7 additions & 5 deletions src/dynamics/mpas/dyn_grid.F90
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module dyn_grid
use physics_column_type, only: kind_pcol, physics_column_t
use physics_grid, only: phys_grid_init
use ref_pres, only: ref_pres_init
use shr_kind_mod, only: kind_r8 => shr_kind_r8
use spmd_utils, only: iam
use std_atm_profile, only: std_atm_pres
use string_utils, only: stringify
use vert_coord, only: pver, pverp, vert_coord_init

! Modules from CESM Share.
use shr_kind_mod, only: kind_r8 => shr_kind_r8

! Modules from external libraries.
use pio, only: file_desc_t

Expand All @@ -32,7 +34,7 @@ module dyn_grid

! Grid names that are to be registered with CAM-SIMA by calling `cam_grid_register`.
! Grid ids can be determined by calling `dyn_grid_id`.
character(max_hcoordname_len), parameter :: dyn_grid_name(*) = [ &
character(*), parameter :: dyn_grid_name(*) = [ character(max_hcoordname_len) :: &
'mpas_cell', &
'cam_cell', &
'mpas_edge', &
Expand Down Expand Up @@ -309,7 +311,7 @@ subroutine define_cam_grid()

allocate(global_grid_index(ncells_solve))

global_grid_index(:) = int(indextocellid, kind_imap)
global_grid_index(:) = int(indextocellid(1:ncells_solve), kind_imap)

lat_coord => horiz_coord_create('latCell', 'nCells', ncells_global, 'latitude', 'degrees_north', &
1, ncells_solve, latcell * rad_to_deg, map=global_grid_index)
Expand Down Expand Up @@ -371,7 +373,7 @@ subroutine define_cam_grid()

allocate(global_grid_index(nedges_solve))

global_grid_index(:) = int(indextoedgeid, kind_imap)
global_grid_index(:) = int(indextoedgeid(1:nedges_solve), kind_imap)

lat_coord => horiz_coord_create('latEdge', 'nEdges', nedges_global, 'latitude', 'degrees_north', &
1, nedges_solve, latedge * rad_to_deg, map=global_grid_index)
Expand Down Expand Up @@ -406,7 +408,7 @@ subroutine define_cam_grid()

allocate(global_grid_index(nvertices_solve))

global_grid_index(:) = int(indextovertexid, kind_imap)
global_grid_index(:) = int(indextovertexid(1:nvertices_solve), kind_imap)

lat_coord => horiz_coord_create('latVertex', 'nVertices', nvertices_global, 'latitude', 'degrees_north', &
1, nvertices_solve, latvertex * rad_to_deg, map=global_grid_index)
Expand Down

0 comments on commit 70301e0

Please sign in to comment.