Skip to content

Commit

Permalink
Check allocation status for allocatable variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kuanchihwang committed Apr 19, 2024
1 parent 35dd726 commit e47f906
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions src/dynamics/mpas/dyn_grid.F90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module dyn_grid
! Modules from CAM.
use cam_abortutils, only: endrun
use cam_abortutils, only: check_allocate, endrun
use cam_constituents, only: num_advected
use cam_grid_support, only: cam_grid_register, cam_grid_attribute_register, &
horiz_coord_t, horiz_coord_create, &
Expand Down Expand Up @@ -138,6 +138,7 @@ end subroutine model_grid_init
subroutine init_reference_pressure()
! Number of pure pressure levels at model top.
integer, parameter :: num_pure_p_lev = 0
integer :: ierr
integer :: k, l
! Reference pressure (Pa) at layer interfaces and midpoints.
real(kind_r8), allocatable :: p_ref_int(:)
Expand All @@ -153,12 +154,15 @@ subroutine init_reference_pressure()
! Compute reference height.
call mpas_dynamical_core % get_variable_value(rdzw, 'all', 'rdzw', time_level=1)

allocate(dzw(pver))
allocate(dzw(pver), stat=ierr)
call check_allocate(ierr, 'init_reference_pressure', 'dzw', 'dyn_grid', __LINE__)

dzw(:) = 1.0_kind_r8 / rdzw

allocate(zw(pverp))
allocate(zu(pver))
allocate(zw(pverp), stat=ierr)
call check_allocate(ierr, 'init_reference_pressure', 'zw', 'dyn_grid', __LINE__)
allocate(zu(pver), stat=ierr)
call check_allocate(ierr, 'init_reference_pressure', 'zu', 'dyn_grid', __LINE__)

! In MPAS, zeta coordinates are stored in increasing order (i.e., bottom to top of atmosphere).
! In CAM-SIMA, however, index order is reversed (i.e., top to bottom of atmosphere).
Expand All @@ -171,11 +175,13 @@ subroutine init_reference_pressure()
end do

! Compute reference pressure from reference height.
allocate(p_ref_int(pverp))
allocate(p_ref_int(pverp), stat=ierr)
call check_allocate(ierr, 'init_reference_pressure', 'p_ref_int', 'dyn_grid', __LINE__)

call std_atm_pres(zw, p_ref_int)

allocate(p_ref_mid(pver))
allocate(p_ref_mid(pver), stat=ierr)
call check_allocate(ierr, 'init_reference_pressure', 'p_ref_mid', 'dyn_grid', __LINE__)

p_ref_mid(:) = 0.5_kind_r8 * (p_ref_int(1:pver) + p_ref_int(2:pverp))

Expand Down Expand Up @@ -209,6 +215,7 @@ subroutine init_physics_grid()
character(max_hcoordname_len), allocatable :: dyn_attribute_name(:)
integer :: hdim1_d, hdim2_d
integer :: i
integer :: ierr
integer, allocatable :: indextocellid(:) ! Global indexes of cell centers.
real(kind_r8), allocatable :: areacell(:) ! Cell areas (square meters).
real(kind_r8), allocatable :: latcell(:) ! Cell center latitudes (radians).
Expand All @@ -225,7 +232,8 @@ subroutine init_physics_grid()
call mpas_dynamical_core % get_variable_value(latcell, 'mesh', 'latCell')
call mpas_dynamical_core % get_variable_value(loncell, 'mesh', 'lonCell')

allocate(dyn_column(ncells_solve))
allocate(dyn_column(ncells_solve), stat=ierr)
call check_allocate(ierr, 'init_physics_grid', 'dyn_column', 'dyn_grid', __LINE__)

do i = 1, ncells_solve
! Column information.
Expand All @@ -249,12 +257,14 @@ subroutine init_physics_grid()
dyn_column(i) % global_dyn_block = indextocellid(i)
dyn_column(i) % local_dyn_block = i
! `dyn_block_index` is not used due to no dynamics block offset, but it still needs to be allocated.
allocate(dyn_column(i) % dyn_block_index(0))
allocate(dyn_column(i) % dyn_block_index(0), stat=ierr)
call check_allocate(ierr, 'init_physics_grid', 'dyn_column % dyn_block_index', 'dyn_grid', __LINE__)
end do

! `phys_grid_init` expects to receive the `area` attribute from dynamics.
! However, do not let it because dynamics grid is different from physics grid.
allocate(dyn_attribute_name(0))
allocate(dyn_attribute_name(0), stat=ierr)
call check_allocate(ierr, 'init_physics_grid', 'dyn_attribute_name', 'dyn_grid', __LINE__)

call phys_grid_init(hdim1_d, hdim2_d, 'mpas', dyn_column, 'mpas_cell', dyn_attribute_name)
end subroutine init_physics_grid
Expand All @@ -268,6 +278,7 @@ end subroutine init_physics_grid
!> (KCW, 2024-03-28)
subroutine define_cam_grid()
integer :: i
integer :: ierr
integer, allocatable :: indextocellid(:) ! Global indexes of cell centers.
integer, allocatable :: indextoedgeid(:) ! Global indexes of edge nodes.
integer, allocatable :: indextovertexid(:) ! Global indexes of vertex nodes.
Expand Down Expand Up @@ -309,7 +320,8 @@ subroutine define_cam_grid()
call mpas_dynamical_core % get_variable_value(latcell, 'mesh', 'latCell')
call mpas_dynamical_core % get_variable_value(loncell, 'mesh', 'lonCell')

allocate(global_grid_index(ncells_solve))
allocate(global_grid_index(ncells_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'global_grid_index', 'dyn_grid', __LINE__)

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

Expand All @@ -318,9 +330,12 @@ subroutine define_cam_grid()
lon_coord => horiz_coord_create('lonCell', 'nCells', ncells_global, 'longitude', 'degrees_east', &
1, ncells_solve, loncell * rad_to_deg, map=global_grid_index)

allocate(cell_area(ncells_solve))
allocate(cell_weight(ncells_solve))
allocate(global_grid_map(3, ncells_solve))
allocate(cell_area(ncells_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'cell_area', 'dyn_grid', __LINE__)
allocate(cell_weight(ncells_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'cell_weight', 'dyn_grid', __LINE__)
allocate(global_grid_map(3, ncells_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'global_grid_map', 'dyn_grid', __LINE__)

do i = 1, ncells_solve
cell_area(i) = areacell(i)
Expand Down Expand Up @@ -371,7 +386,8 @@ subroutine define_cam_grid()
call mpas_dynamical_core % get_variable_value(latedge, 'mesh', 'latEdge')
call mpas_dynamical_core % get_variable_value(lonedge, 'mesh', 'lonEdge')

allocate(global_grid_index(nedges_solve))
allocate(global_grid_index(nedges_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'global_grid_index', 'dyn_grid', __LINE__)

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

Expand All @@ -380,7 +396,8 @@ subroutine define_cam_grid()
lon_coord => horiz_coord_create('lonEdge', 'nEdges', nedges_global, 'longitude', 'degrees_east', &
1, nedges_solve, lonedge * rad_to_deg, map=global_grid_index)

allocate(global_grid_map(3, nedges_solve))
allocate(global_grid_map(3, nedges_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'global_grid_map', 'dyn_grid', __LINE__)

do i = 1, nedges_solve
global_grid_map(1, i) = int(i, kind_imap)
Expand All @@ -406,7 +423,8 @@ subroutine define_cam_grid()
call mpas_dynamical_core % get_variable_value(latvertex, 'mesh', 'latVertex')
call mpas_dynamical_core % get_variable_value(lonvertex, 'mesh', 'lonVertex')

allocate(global_grid_index(nvertices_solve))
allocate(global_grid_index(nvertices_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'global_grid_index', 'dyn_grid', __LINE__)

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

Expand All @@ -415,7 +433,8 @@ subroutine define_cam_grid()
lon_coord => horiz_coord_create('lonVertex', 'nVertices', nvertices_global, 'longitude', 'degrees_east', &
1, nvertices_solve, lonvertex * rad_to_deg, map=global_grid_index)

allocate(global_grid_map(3, nvertices_solve))
allocate(global_grid_map(3, nvertices_solve), stat=ierr)
call check_allocate(ierr, 'define_cam_grid', 'global_grid_map', 'dyn_grid', __LINE__)

do i = 1, nvertices_solve
global_grid_map(1, i) = int(i, kind_imap)
Expand Down

0 comments on commit e47f906

Please sign in to comment.