Skip to content

Commit

Permalink
Eliminate circular dependency in chgres_cube
Browse files Browse the repository at this point in the history
New module to hold the atmospheric target data and its clean up routine.

New module to hold the surface target data and its clean up routine.

Convert write_data.F90 to a Fortran module.

Fixes ufs-community#459.
  • Loading branch information
GeorgeGayno-NOAA authored Mar 25, 2022
1 parent a8b9a01 commit 31271f7
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 238 deletions.
2 changes: 2 additions & 0 deletions sorc/chgres_cube.fd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

set(lib_src
atmosphere.F90
atmosphere_target_data.F90
grib2_util.F90
input_data.F90
model_grid.F90
program_setup.F90
search_util.F90
static_data.F90
surface_target_data.F90
surface.F90
thompson_mp_climo_data.F90
wam_climo_data.f90
Expand Down
72 changes: 22 additions & 50 deletions sorc/chgres_cube.fd/atmosphere.F90
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ module atmosphere

use esmf

use atmosphere_target_data, only : lev_target, levp1_target, nvcoord_target, &
vcoord_target, delp_target_grid, &
dzdt_target_grid, ps_target_grid, &
temp_target_grid, tracers_target_grid, &
u_s_target_grid, v_s_target_grid, &
u_w_target_grid, v_w_target_grid, &
zh_target_grid, qnwfa_climo_target_grid, &
qnifa_climo_target_grid

use input_data, only : lev_input, &
levp1_input, &
tracers_input_grid, &
Expand Down Expand Up @@ -56,50 +65,32 @@ module atmosphere
thomp_pres_climo_input_grid, &
lev_thomp_mp_climo

use write_data, only : write_fv3_atm_header_netcdf, &
write_fv3_atm_bndy_data_netcdf, &
write_fv3_atm_data_netcdf

implicit none

private

integer, public :: lev_target !< num vertical levels
integer, public :: levp1_target !< num levels plus 1
integer, public :: nvcoord_target !< num vertical coordinate variables

real(esmf_kind_r8), allocatable, public :: vcoord_target(:,:) !< vertical coordinate

type(esmf_field), public :: delp_target_grid !< pressure thickness
type(esmf_field), public :: dzdt_target_grid !< vertical velocity
type(esmf_field) :: dzdt_b4adj_target_grid !< vertical vel before vert adj
type(esmf_field), allocatable, public :: tracers_target_grid(:) !< tracers
type(esmf_field), allocatable :: tracers_b4adj_target_grid(:) !< tracers before vert adj
type(esmf_field), public :: ps_target_grid !< surface pressure
type(esmf_field) :: ps_b4adj_target_grid !< sfc pres before terrain adj
type(esmf_field) :: pres_target_grid !< 3-d pressure
type(esmf_field) :: pres_b4adj_target_grid !< 3-d pres before terrain adj
type(esmf_field), public :: temp_target_grid !< temperautre
type(esmf_field) :: temp_b4adj_target_grid !< temp before vert adj
type(esmf_field) :: terrain_interp_to_target_grid !< Input grid terrain interpolated to target grid.
type(esmf_field), public :: u_s_target_grid !< u-wind, 'south' edge
type(esmf_field), public :: v_s_target_grid !< v-wind, 'south' edge
type(esmf_field) :: wind_target_grid !< 3-d wind, grid box center
type(esmf_field) :: wind_b4adj_target_grid !< 3-d wind before vert adj
type(esmf_field) :: wind_s_target_grid !< 3-d wind, 'south' edge
type(esmf_field), public :: u_w_target_grid !< u-wind, 'west' edge
type(esmf_field), public :: v_w_target_grid !< v-wind, 'west' edge
type(esmf_field) :: wind_w_target_grid !< 3-d wind, 'west' edge
type(esmf_field), public :: zh_target_grid !< 3-d height

! Fields associated with thompson microphysics climatological tracers.

type(esmf_field) :: qnifa_climo_b4adj_target_grid !< number concentration of ice
!! friendly aerosols before vert adj
type(esmf_field), public :: qnifa_climo_target_grid !< number concentration of ice
!! friendly aerosols on target
!! horiz/vert grid.
type(esmf_field) :: qnwfa_climo_b4adj_target_grid !< number concentration of water
!! friendly aerosols before vert adj
type(esmf_field), public :: qnwfa_climo_target_grid !< number concentration of water
!! friendly aerosols on target
!! horiz/vert grid.
type(esmf_field) :: thomp_pres_climo_b4adj_target_grid !< pressure of each level on
!! target grid

Expand Down Expand Up @@ -429,7 +420,7 @@ subroutine atmosphere_driver(localpet)
! Free up memory.
!-----------------------------------------------------------------------------------

call cleanup_target_atm_data
call cleanup_all_target_atm_data

end subroutine atmosphere_driver

Expand Down Expand Up @@ -2189,42 +2180,23 @@ end subroutine cleanup_target_atm_b4adj_data

!> Cleanup target grid atmospheric field objects.
!! @author George Gayno
subroutine cleanup_target_atm_data
subroutine cleanup_all_target_atm_data

use atmosphere_target_data, only : cleanup_atmosphere_target_data

implicit none

integer :: i, rc
integer :: rc

print*,"- DESTROY TARGET GRID ATMOSPHERIC FIELDS."
print*,"- DESTROY LOCAL TARGET GRID ATMOSPHERIC FIELDS."

call ESMF_FieldDestroy(delp_target_grid, rc=rc)
call ESMF_FieldDestroy(dzdt_target_grid, rc=rc)
call ESMF_FieldDestroy(ps_target_grid, rc=rc)
call ESMF_FieldDestroy(pres_target_grid, rc=rc)
call ESMF_FieldDestroy(temp_target_grid, rc=rc)
call ESMF_FieldDestroy(u_s_target_grid, rc=rc)
call ESMF_FieldDestroy(v_s_target_grid, rc=rc)
call ESMF_FieldDestroy(wind_target_grid, rc=rc)
call ESMF_FieldDestroy(wind_s_target_grid, rc=rc)
call ESMF_FieldDestroy(wind_w_target_grid, rc=rc)
call ESMF_FieldDestroy(u_w_target_grid, rc=rc)
call ESMF_FieldDestroy(v_w_target_grid, rc=rc)
call ESMF_FieldDestroy(zh_target_grid, rc=rc)

do i = 1, num_tracers
call ESMF_FieldDestroy(tracers_target_grid(i), rc=rc)
enddo

deallocate(tracers_target_grid)

if (ESMF_FieldIsCreated(qnifa_climo_target_grid)) then
call ESMF_FieldDestroy(qnifa_climo_target_grid, rc=rc)
endif
call ESMF_FieldDestroy(pres_target_grid, rc=rc)

if (ESMF_FieldIsCreated(qnwfa_climo_target_grid)) then
call ESMF_FieldDestroy(qnwfa_climo_target_grid, rc=rc)
endif
call cleanup_atmosphere_target_data

end subroutine cleanup_target_atm_data
end subroutine cleanup_all_target_atm_data

end module atmosphere
83 changes: 83 additions & 0 deletions sorc/chgres_cube.fd/atmosphere_target_data.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
!> @file
!! @brief Define atmospheric target data variables.
!! @author George Gayno NCEP/EMC

!> Module to hold variables and ESMF fields associated
!! with the target grid atmospheric data.
!!
!! @author George Gayno NCEP/EMC
module atmosphere_target_data

use esmf

implicit none

private

integer, public :: lev_target !< Number of vertical levels.
integer, public :: levp1_target !< Number of vertical levels plus 1.
integer, public :: nvcoord_target !< Number of vertical coordinate variables.

real(esmf_kind_r8), allocatable, public :: vcoord_target(:,:) !< Vertical coordinate.

type(esmf_field), public :: delp_target_grid !< Pressure thickness.
type(esmf_field), public :: dzdt_target_grid !< Vertical velocity.
type(esmf_field), public :: ps_target_grid !< Surface pressure.
type(esmf_field), public :: temp_target_grid !< Temperautre.
type(esmf_field), allocatable, public :: tracers_target_grid(:) !< Tracers.
type(esmf_field), public :: u_s_target_grid !< U-wind, 'south' edge of grid cell.
type(esmf_field), public :: v_s_target_grid !< V-wind, 'south' edge of grid cell.
type(esmf_field), public :: u_w_target_grid !< U-wind, 'west' edge of grid cell.
type(esmf_field), public :: v_w_target_grid !< V-wind, 'west' edge of grid cell.
type(esmf_field), public :: zh_target_grid !< 3-d height.
type(esmf_field), public :: qnifa_climo_target_grid !< Number concentration of ice
!! friendly aerosols.
type(esmf_field), public :: qnwfa_climo_target_grid !< Number concentration of water
!! friendly aerosols.

public :: cleanup_atmosphere_target_data

contains

!> Free up memory for fields and variables in this module.
!!
!! @author George.Gayno NOAA/EMC
subroutine cleanup_atmosphere_target_data

use program_setup, only : num_tracers

implicit none

integer :: i, rc

print*,"- DESTROY TARGET GRID ATMOSPHERIC FIELDS."

if (ESMF_FieldIsCreated(delp_target_grid)) call ESMF_FieldDestroy(delp_target_grid, rc=rc)
if (ESMF_FieldIsCreated(dzdt_target_grid)) call ESMF_FieldDestroy(dzdt_target_grid, rc=rc)
if (ESMF_FieldIsCreated(ps_target_grid)) call ESMF_FieldDestroy(ps_target_grid, rc=rc)
if (ESMF_FieldIsCreated(temp_target_grid)) call ESMF_FieldDestroy(temp_target_grid, rc=rc)
if (ESMF_FieldIsCreated(u_s_target_grid)) call ESMF_FieldDestroy(u_s_target_grid, rc=rc)
if (ESMF_FieldIsCreated(v_s_target_grid)) call ESMF_FieldDestroy(v_s_target_grid, rc=rc)
if (ESMF_FieldIsCreated(u_w_target_grid)) call ESMF_FieldDestroy(u_w_target_grid, rc=rc)
if (ESMF_FieldIsCreated(v_w_target_grid)) call ESMF_FieldDestroy(v_w_target_grid, rc=rc)
if (ESMF_FieldIsCreated(zh_target_grid)) call ESMF_FieldDestroy(zh_target_grid, rc=rc)

do i = 1, num_tracers
if (ESMF_FieldIsCreated(tracers_target_grid(i))) call ESMF_FieldDestroy(tracers_target_grid(i), rc=rc)
enddo

if (allocated (tracers_target_grid)) deallocate(tracers_target_grid)

if (ESMF_FieldIsCreated(qnifa_climo_target_grid)) then
call ESMF_FieldDestroy(qnifa_climo_target_grid, rc=rc)
endif

if (ESMF_FieldIsCreated(qnwfa_climo_target_grid)) then
call ESMF_FieldDestroy(qnwfa_climo_target_grid, rc=rc)
endif

if (allocated (vcoord_target)) deallocate(vcoord_target)

end subroutine cleanup_atmosphere_target_data

end module atmosphere_target_data
Loading

0 comments on commit 31271f7

Please sign in to comment.