LCOV - code coverage report
Current view: | top level - physics/cam - physics_types.F90 (source / functions) |  |  | Hit | Total | Coverage
Test: | coverage.info |  | Lines: | 687 | 872 | 78.8 %
Date: | 2024-12-17 22:39:59 |  | Functions: | 19 | 31 | 61.3 %

              Line data    Source code
           1             : !-------------------------------------------------------------------------------
           2             : !physics data types module
           3             : !-------------------------------------------------------------------------------
           4             : module physics_types
           5             :
           6             :   use shr_kind_mod,     only: r8 => shr_kind_r8
           7             :   use ppgrid,           only: pcols, pver
           8             :   use constituents,     only: pcnst, qmin, cnst_name, cnst_get_ind
           9             :   use geopotential,     only: geopotential_t
          10             :   use physconst,        only: zvir, gravit, cpair, rair
          11             :   use air_composition,  only: cpairv, rairv
          12             :   use phys_grid,        only: get_ncols_p, get_rlon_all_p, get_rlat_all_p, get_gcol_all_p
          13             :   use cam_logfile,      only: iulog
          14             :   use cam_abortutils,   only: endrun
          15             :   use phys_control,     only: waccmx_is
          16             :   use shr_const_mod,    only: shr_const_rwv
          17             :
          18             :   implicit none
          19             :   private          ! Make default type private to the module
          20             :
          21             : ! Public types:
          22             :
          23             :   public physics_state
          24             :   public physics_tend
          25             :   public physics_ptend
          26             :
          27             : ! Public interfaces
          28             :
          29             :   public physics_update
          30             :   public physics_state_check ! Check state object for invalid data.
          31             :   public physics_ptend_reset
          32             :   public physics_ptend_init
          33             :   public physics_state_set_grid
          34             :   public physics_dme_adjust  ! adjust dry mass and energy for change in water
          35             :                              ! cannot be applied to eul or sld dycores
          36             :   public physics_state_copy  ! copy a physics_state object
          37             :   public physics_ptend_copy  ! copy a physics_ptend object
          38             :   public physics_ptend_sum   ! accumulate physics_ptend objects
          39             :   public physics_ptend_scale ! Multiply physics_ptend objects by a constant factor.
          40             :   public physics_tend_init   ! initialize a physics_tend object
          41             :
          42             :   public set_state_pdry      ! calculate dry air masses in state variable
          43             :   public set_wet_to_dry
          44             :   public set_dry_to_wet
          45             :   public physics_type_alloc
          46             :
          47             :   public physics_state_alloc   ! allocate individual components within state
          48             :   public physics_state_dealloc ! deallocate individual components within state
          49             :   public physics_tend_alloc    ! allocate individual components within tend
          50             :   public physics_tend_dealloc  ! deallocate individual components within tend
          51             :   public physics_ptend_alloc   ! allocate individual components within tend
          52             :   public physics_ptend_dealloc ! deallocate individual components within tend
          53             :
          54             :   public physics_cnst_limit ! apply limiters to constituents (waccmx)
          55             : !-------------------------------------------------------------------------------
          56             :   integer, parameter, public :: phys_te_idx = 1
          57             :   integer ,parameter, public :: dyn_te_idx = 2
          58             :
          59             :   type physics_state
          60             :      integer                                     :: &
          61             :           lchnk,                &! chunk index
          62             :           ngrdcol,              &! -- Grid        -- number of active columns (on the grid)
          63             :           psetcols=0,           &! --             -- max number of columns set - if subcols = pcols*psubcols, else = pcols
          64             :           ncol=0                 ! --             -- sum of nsubcol for all ngrdcols - number of active columns
          65             :      real(r8), dimension(:), allocatable         :: &
          66             :           lat,     &! latitude (radians)
          67             :           lon,     &! longitude (radians)
          68             :           ps,      &! surface pressure
          69             :           psdry,   &! dry surface pressure
          70             :           phis,    &! surface geopotential
          71             :           ulat,    &! unique latitudes  (radians)
          72             :           ulon      ! unique longitudes (radians)
          73             :      real(r8), dimension(:,:),allocatable        :: &
          74             :           t,       &! temperature (K)
          75             :           u,       &! zonal wind (m/s)
          76             :           v,       &! meridional wind (m/s)
          77             :           s,       &! dry static energy
          78             :           omega,   &! vertical pressure velocity (Pa/s)
          79             :           pmid,    &! midpoint pressure (Pa)
          80             :           pmiddry, &! midpoint pressure dry (Pa)
          81             :           pdel,    &! layer thickness (Pa)
          82             :           pdeldry, &! layer thickness dry (Pa)
          83             :           rpdel,   &! reciprocal of layer thickness (Pa)
          84             :           rpdeldry,&! recipricol layer thickness dry (Pa)
          85             :           lnpmid,  &! ln(pmid)
          86             :           lnpmiddry,&! log midpoint pressure dry (Pa)
          87             :           exner,   &! inverse exner function w.r.t. surface pressure (ps/p)^(R/cp)
          88             :           zm        ! geopotential height above surface at midpoints (m)
          89             :
          90             :      real(r8), dimension(:,:,:),allocatable      :: &
          91             :           q         ! constituent mixing ratio (kg/kg moist or dry air depending on type)
          92             :
          93             :      real(r8), dimension(:,:),allocatable        :: &
          94             :           pint,     &! interface pressure (Pa)
          95             :           pintdry,  &! interface pressure dry (Pa)
          96             :           lnpint,   &! ln(pint)
          97             :           lnpintdry,&! log interface pressure dry (Pa)
          98             :           zi         ! geopotential height above surface at interfaces (m)
          99             :
         100             :      real(r8), dimension(:,:),allocatable          :: &
         101             :                            ! Second dimension is (phys_te_idx) CAM physics total energy and
         102             :                            ! (dyn_te_idx) dycore total energy computed in physics
         103             :           te_ini,         &! vertically integrated total (kinetic + static) energy of initial state
         104             :           te_cur           ! vertically integrated total (kinetic + static) energy of current state
         105             :      real(r8), dimension(:), allocatable           :: &
         106             :           tw_ini,         &! vertically integrated total water of initial state
         107             :           tw_cur           ! vertically integrated total water of new state
         108             :      real(r8), dimension(:,:),allocatable          :: &
         109             :           temp_ini,       &! Temperature of initial state (used for energy computations)
         110             :           z_ini            ! Height of initial state (used for energy computations)
         111             :      integer :: count ! count of values with significant energy or water imbalances
         112             :      integer, dimension(:),allocatable           :: &
         113             :           latmapback, &! map from column to unique lat for that column
         114             :           lonmapback, &! map from column to unique lon for that column
         115             :           cid        ! unique column id
         116             :      integer :: ulatcnt, &! number of unique lats in chunk
         117             :                 uloncnt   ! number of unique lons in chunk
         118             :
         119             :   end type physics_state
         120             :
         121             : !-------------------------------------------------------------------------------
         122             :   type physics_tend
         123             :
         124             :      integer   ::   psetcols=0 ! max number of columns set- if subcols = pcols*psubcols, else = pcols
         125             :
         126             :      real(r8), dimension(:,:),allocatable        :: dtdt, dudt, dvdt
         127             :      real(r8), dimension(:),  allocatable        :: flx_net
         128             :      real(r8), dimension(:),  allocatable        :: &
         129             :           te_tnd,  &! cumulative boundary flux of total energy
         130             :           tw_tnd    ! cumulative boundary flux of total water
         131             :   end type physics_tend
         132             :
         133             : !-------------------------------------------------------------------------------
         134             : ! This is for tendencies returned from individual parameterizations
         135             :   type physics_ptend
         136             :
         137             :      integer   ::   psetcols=0 ! max number of columns set- if subcols = pcols*psubcols, else = pcols
         138             :
         139             :      character*24 :: name    ! name of parameterization which produced tendencies.
         140             :
         141             :      logical ::             &
         142             :           ls = .false.,               &! true if dsdt is returned
         143             :           lu = .false.,               &! true if dudt is returned
         144             :           lv = .false.                 ! true if dvdt is returned
         145             :
         146             :      logical,dimension(pcnst) ::  lq = .false.  ! true if dqdt() is returned
         147             :
         148             :      integer ::             &
         149             :           top_level,        &! top level index for which nonzero tendencies have been set
         150             :           bot_level          ! bottom level index for which nonzero tendencies have been set
         151             :
         152             :      real(r8), dimension(:,:),allocatable   :: &
         153             :           s,                &! heating rate (J/kg/s)
         154             :           u,                &! u momentum tendency (m/s/s)
         155             :           v                  ! v momentum tendency (m/s/s)
         156             :      real(r8), dimension(:,:,:),allocatable :: &
         157             :           q                  ! consituent tendencies (kg/kg/s)
         158             :
         159             : ! boundary fluxes
         160             :      real(r8), dimension(:),allocatable     ::&
         161             :           hflux_srf,     &! net heat flux at surface (W/m2)
         162             :           hflux_top,     &! net heat flux at top of model (W/m2)
         163             :           taux_srf,      &! net zonal stress at surface (Pa)
         164             :           taux_top,      &! net zonal stress at top of model (Pa)
         165             :           tauy_srf,      &! net meridional stress at surface (Pa)
         166             :           tauy_top        ! net meridional stress at top of model (Pa)
         167             :      real(r8), dimension(:,:),allocatable   ::&
         168             :           cflx_srf,      &! constituent flux at surface (kg/m2/s)
         169             :           cflx_top        ! constituent flux top of model (kg/m2/s)
         170             :
         171             :   end type physics_ptend
         172             :
         173             :
         174             : !===============================================================================
         175             : contains
         176             : !===============================================================================
         177        1536 :   subroutine physics_type_alloc(phys_state, phys_tend, begchunk, endchunk, psetcols)
         178             :     implicit none
         179             :     type(physics_state), pointer :: phys_state(:)
         180             :     type(physics_tend), pointer :: phys_tend(:)
         181             :     integer, intent(in) :: begchunk, endchunk
         182             :     integer, intent(in) :: psetcols
         183             :
         184             :     integer :: ierr=0, lchnk
         185             :
         186       10800 :     allocate(phys_state(begchunk:endchunk), stat=ierr)
         187        1536 :     if( ierr /= 0 ) then
         188           0 :        write(iulog,*) 'physics_types: phys_state allocation error = ',ierr
         189           0 :        call endrun('physics_types: failed to allocate physics_state array')
         190             :     end if
         191             :
         192        7728 :     do lchnk=begchunk,endchunk
         193        7728 :        call physics_state_alloc(phys_state(lchnk),lchnk,pcols)
         194             :     end do
         195             :
         196       10800 :     allocate(phys_tend(begchunk:endchunk), stat=ierr)
         197        1536 :     if( ierr /= 0 ) then
         198           0 :        write(iulog,*) 'physics_types: phys_tend allocation error = ',ierr
         199           0 :        call endrun('physics_types: failed to allocate physics_tend array')
         200             :     end if
         201             :
         202        7728 :     do lchnk=begchunk,endchunk
         203        7728 :        call physics_tend_alloc(phys_tend(lchnk),phys_state(lchnk)%psetcols)
         204             :     end do
         205             :
         206        1536 :   end subroutine physics_type_alloc
         207             : !===============================================================================
         208    65560896 :   subroutine physics_update(state, ptend, dt, tend)
         209             : !-----------------------------------------------------------------------
         210             : ! Update the state and or tendency structure with the parameterization tendencies
         211             : !-----------------------------------------------------------------------
         212             :     use scamMod,         only: scm_crm_mode, single_column
         213             :     use phys_control,    only: phys_getopts
         214             :     use cam_thermo,      only: cam_thermo_dry_air_update ! Routine which updates physconst variables (WACCM-X)
         215             :     use air_composition, only: dry_air_species_num
         216             :     use qneg_module   ,  only: qneg3
         217             :
         218             : !------------------------------Arguments--------------------------------
         219             :     type(physics_ptend), intent(inout)  :: ptend   ! Parameterization tendencies
         220             :
         221             :     type(physics_state), intent(inout)  :: state   ! Physics state variables
         222             :
         223             :     real(r8), intent(in) :: dt                     ! time step
         224             :
         225             :     type(physics_tend ), intent(inout), optional  :: tend  ! Physics tendencies over timestep
         226             :     ! tend is usually only needed by calls from physpkg.
         227             : !
         228             : !---------------------------Local storage-------------------------------
         229             :     integer :: k,m                                 ! column,level,constituent indices
         230             :     integer :: ixcldice, ixcldliq                  ! indices for CLDICE and CLDLIQ
         231             :     integer :: ixnumice, ixnumliq
         232             :     integer :: ixnumsnow, ixnumrain
         233             :     integer :: ncol                                ! number of columns
         234             :     integer :: ixh, ixh2    ! constituent indices for H, H2
         235             :
         236   131121792 :     real(r8) :: zvirv(state%psetcols,pver)  ! Local zvir array pointer
         237             :
         238    65560896 :     real(r8),allocatable :: cpairv_loc(:,:)
         239    65560896 :     real(r8),allocatable :: rairv_loc(:,:)
         240             :
         241             :     ! PERGRO limits cldliq/ice for macro/microphysics:
         242             :     character(len=24), parameter :: pergro_cldlim_names(4) = &
         243             :          (/ "stratiform", "cldwat    ", "micro_mg  ", "macro_park" /)
         244             :
         245             :     ! cldliq/ice limits that are always on.
         246             :     character(len=24), parameter :: cldlim_names(2) = &
         247             :          (/ "convect_deep", "zm_conv_tend" /)
         248             :
         249             :     ! Whether to do validation of state on each call.
         250             :     logical :: state_debug_checks
         251             :
         252             :     !-----------------------------------------------------------------------
         253             :
         254             :     ! The column radiation model does not update the state
         255    65560896 :     if(single_column.and.scm_crm_mode) return
         256             :
         257             :
         258             :     !-----------------------------------------------------------------------
         259             :     ! If no fields are set, then return
         260   889582968 :     if (.not. (any(ptend%lq(:)) .or. ptend%ls .or. ptend%lu .or. ptend%lv)) then
         261     8935056 :        ptend%name  = "none"
         262     8935056 :        ptend%psetcols = 0
         263     8935056 :        return
         264             :     end if
         265             :
         266             :     !-----------------------------------------------------------------------
         267             :     ! Check that the state/tend/ptend are all dimensioned with the same number of columns
         268    56625840 :     if (state%psetcols /= ptend%psetcols) then
         269             :        call endrun('ERROR in physics_update with ptend%name='//trim(ptend%name) &
         270           0 :             //': state and ptend must have the same number of psetcols.')
         271             :     end if
         272             :
         273    56625840 :     if (present(tend)) then
         274    29802096 :        if (state%psetcols /= tend%psetcols) then
         275             :           call endrun('ERROR in physics_update with ptend%name='//trim(ptend%name) &
         276           0 :                //': state and tend must have the same number of psetcols.')
         277             :        end if
         278             :     end if
         279             :
         280             :
         281             :     !-----------------------------------------------------------------------
         282    56625840 :     call phys_getopts(state_debug_checks_out=state_debug_checks)
         283             :
         284    56625840 :     ncol = state%ncol
         285             :
         286             :     ! Update u,v fields
         287    56625840 :     if(ptend%lu) then
         288  1540972080 :        do k = ptend%top_level, ptend%bot_level
         289 25456919760 :           state%u  (:ncol,k) = state%u  (:ncol,k) + ptend%u(:ncol,k) * dt
         290  1524578760 :           if (present(tend)) &
         291 16213628952 :                tend%dudt(:ncol,k) = tend%dudt(:ncol,k) + ptend%u(:ncol,k)
         292             :        end do
         293             :     end if
         294             :
         295    56625840 :     if(ptend%lv) then
         296  1540972080 :        do k = ptend%top_level, ptend%bot_level
         297 25456919760 :           state%v  (:ncol,k) = state%v  (:ncol,k) + ptend%v(:ncol,k) * dt
         298  1524578760 :           if (present(tend)) &
         299 16213628952 :                tend%dvdt(:ncol,k) = tend%dvdt(:ncol,k) + ptend%v(:ncol,k)
         300             :        end do
         301             :     end if
         302             :
         303             :    ! Update constituents, all schemes use time split q: no tendency kept
         304    56625840 :     call cnst_get_ind('CLDICE', ixcldice, abort=.false.)
         305    56625840 :     call cnst_get_ind('CLDLIQ', ixcldliq, abort=.false.)
         306             :     ! Check for number concentration of cloud liquid and cloud ice (if not present
         307             :     ! the indices will be set to -1)
         308    56625840 :     call cnst_get_ind('NUMICE', ixnumice, abort=.false.)
         309    56625840 :     call cnst_get_ind('NUMLIQ', ixnumliq, abort=.false.)
         310    56625840 :     call cnst_get_ind('NUMRAI', ixnumrain, abort=.false.)
         311    56625840 :     call cnst_get_ind('NUMSNO', ixnumsnow, abort=.false.)
         312             :
         313  2378285280 :     do m = 1, pcnst
         314  2378285280 :        if(ptend%lq(m)) then
         315 79098286032 :           do k = ptend%top_level, ptend%bot_level
         316 >13075*10^8 :              state%q(:ncol,k,m) = state%q(:ncol,k,m) + ptend%q(:ncol,k,m) * dt
         317             :           end do
         318             :
         319             :           ! now test for mixing ratios which are too small
         320             :           ! don't call qneg3 for number concentration variables
         321             :           if (m /= ixnumice  .and.  m /= ixnumliq .and. &
         322   841471128 :               m /= ixnumrain .and.  m /= ixnumsnow ) then
         323   740182392 :              call qneg3(trim(ptend%name), state%lchnk, ncol, state%psetcols, pver, m, m, qmin(m:m), state%q(:,1:pver,m:m))
         324             :           else
         325  9521141184 :              do k = ptend%top_level, ptend%bot_level
         326             :                 ! checks for number concentration
         327 >15728*10^7 :                 state%q(:ncol,k,m) = max(1.e-12_r8,state%q(:ncol,k,m))
         328 >15739*10^7 :                 state%q(:ncol,k,m) = min(1.e10_r8,state%q(:ncol,k,m))
         329             :              end do
         330             :           end if
         331             :
         332             :        end if
         333             :
         334             :     end do
         335             :
         336             :     !------------------------------------------------------------------------
         337             :     ! This is a temporary fix for the large H, H2 in WACCM-X
         338             :     ! Well, it was supposed to be temporary, but it has been here
         339             :     ! for a while now.
         340             :     !------------------------------------------------------------------------
         341    56625840 :     if ( waccmx_is('ionosphere') .or. waccmx_is('neutral') ) then
         342           0 :        call cnst_get_ind('H', ixh)
         343           0 :        do k = ptend%top_level, ptend%bot_level
         344           0 :           state%q(:ncol,k,ixh) = min(state%q(:ncol,k,ixh), 0.01_r8)
         345             :        end do
         346             :
         347           0 :        call cnst_get_ind('H2', ixh2)
         348           0 :        do k = ptend%top_level, ptend%bot_level
         349           0 :           state%q(:ncol,k,ixh2) = min(state%q(:ncol,k,ixh2), 6.e-5_r8)
         350             :        end do
         351             :     endif
         352             :
         353             :     ! Special tests for cloud liquid and ice:
         354             :     ! Enforce a minimum non-zero value.
         355    56625840 :     if (ixcldliq > 1) then
         356    56625840 :        if(ptend%lq(ixcldliq)) then
         357             : #ifdef PERGRO
         358             :           if ( any(ptend%name == pergro_cldlim_names) ) &
         359             :                call state_cnst_min_nz(1.e-12_r8, ixcldliq, ixnumliq)
         360             : #endif
         361    83406240 :           if ( any(ptend%name == cldlim_names) ) &
         362     1495368 :                call state_cnst_min_nz(1.e-36_r8, ixcldliq, ixnumliq)
         363             :        end if
         364             :     end if
         365             :
         366    56625840 :     if (ixcldice > 1) then
         367    56625840 :        if(ptend%lq(ixcldice)) then
         368             : #ifdef PERGRO
         369             :           if ( any(ptend%name == pergro_cldlim_names) ) &
         370             :                call state_cnst_min_nz(1.e-12_r8, ixcldice, ixnumice)
         371             : #endif
         372    87873768 :           if ( any(ptend%name == cldlim_names) ) &
         373     1495368 :                call state_cnst_min_nz(1.e-36_r8, ixcldice, ixnumice)
         374             :        end if
         375             :     end if
         376             :
         377             :     !------------------------------------------------------------------------
         378             :     ! Get indices for molecular weights and call WACCM-X cam_thermo_update
         379             :     !------------------------------------------------------------------------
         380    56625840 :     if (dry_air_species_num>0) then
         381           0 :       call cam_thermo_dry_air_update(state%q, state%t, state%lchnk, state%ncol)
         382             :     endif
         383             :
         384             :     !-----------------------------------------------------------------------
         385             :     ! cpairv_loc and rairv_loc need to be allocated to a size which matches state and ptend
         386             :     ! If psetcols == pcols, the cpairv is the correct size and just copy
         387             :     ! If psetcols > pcols and all cpairv match cpair, then assign the constant cpair
         388   169877520 :     allocate(cpairv_loc(state%psetcols,pver))
         389    56625840 :     if (state%psetcols == pcols) then
         390 89582078880 :        cpairv_loc(:,:) = cpairv(:,:,state%lchnk)
         391           0 :     else if (state%psetcols > pcols .and. all(cpairv(:,:,:) == cpair)) then
         392           0 :        cpairv_loc(:,:) = cpair
         393             :     else
         394           0 :        call endrun('physics_update: cpairv is not allowed to vary when subcolumns are turned on')
         395             :     end if
         396   169877520 :     allocate(rairv_loc(state%psetcols,pver))
         397    56625840 :     if (state%psetcols == pcols) then
         398 89582078880 :        rairv_loc(:,:) = rairv(:,:,state%lchnk)
         399           0 :     else if (state%psetcols > pcols .and. all(rairv(:,:,:) == rair)) then
         400           0 :        rairv_loc(:,:) = rair
         401             :     else
         402           0 :        call endrun('physics_update: rairv_loc is not allowed to vary when subcolumns are turned on')
         403             :     end if
         404             :
         405    56625840 :     if ( waccmx_is('ionosphere') .or. waccmx_is('neutral') ) then
         406           0 :       zvirv(:,:) = shr_const_rwv / rairv_loc(:,:) - 1._r8
         407             :     else
         408 89582078880 :       zvirv(:,:) = zvir
         409             :     endif
         410             :
         411             :     !-------------------------------------------------------------------------------------------------------------
         412             :     ! Update temperature from dry static energy (moved from above for WACCM-X so updating after cpairv_loc update)
         413             :     !-------------------------------------------------------------------------------------------------------------
         414             :
         415    56625840 :     if(ptend%ls) then
         416  3643038432 :        do k = ptend%top_level, ptend%bot_level
         417 60183139104 :           state%t(:ncol,k) = state%t(:ncol,k) + ptend%s(:ncol,k)*dt/cpairv_loc(:ncol,k)
         418  3604282704 :           if (present(tend)) &
         419 32442842448 :                tend%dtdt(:ncol,k) = tend%dtdt(:ncol,k) + ptend%s(:ncol,k)/cpairv_loc(:ncol,k)
         420             :        end do
         421             :     end if
         422             :
         423             :     ! Derive new geopotential fields if heating or water tendency not 0.
         424             :
         425    56625840 :     if (ptend%ls .or. ptend%lq(1)) then
         426             :        call geopotential_t  (                                                                    &
         427             :             state%lnpint, state%lnpmid, state%pint  , state%pmid  , state%pdel  , state%rpdel  , &
         428             :             state%t     , state%q(:,:,:), rairv_loc(:,:), gravit  , zvirv              , &
         429    40244904 :             state%zi    , state%zm      , ncol         )
         430             :        ! update dry static energy for use in next process
         431  3783020976 :        do k = ptend%top_level, ptend%bot_level
         432  3742776072 :           state%s(:ncol,k) = state%t(:ncol,k)*cpairv_loc(:ncol,k) &
         433 66278677248 :                            + gravit*state%zm(:ncol,k) + state%phis(:ncol)
         434             :        end do
         435             :     end if
         436             :
         437    56625840 :     if (state_debug_checks) call physics_state_check(state, ptend%name)
         438             :
         439    56625840 :     deallocate(cpairv_loc, rairv_loc)
         440             :
         441             :     ! Deallocate ptend
         442    56625840 :     call physics_ptend_dealloc(ptend)
         443             :
         444    56625840 :     ptend%name  = "none"
         445  2378285280 :     ptend%lq(:) = .false.
         446    56625840 :     ptend%ls    = .false.
         447    56625840 :     ptend%lu    = .false.
         448    56625840 :     ptend%lv    = .false.
         449   122186736 :     ptend%psetcols = 0
         450             :
         451             :   contains
         452             :
         453     2990736 :     subroutine state_cnst_min_nz(lim, qix, numix)
         454             :       ! Small utility function for setting minimum nonzero
         455             :       ! constituent concentrations.
         456             :
         457             :       ! Lower limit and constituent index
         458             :       real(r8), intent(in) :: lim
         459             :       integer,  intent(in) :: qix
         460             :       ! Number concentration that goes with qix.
         461             :       ! Ignored if <= 0 (and therefore constituent is not present).
         462             :       integer,  intent(in) :: numix
         463             :
         464     2990736 :       if (numix > 0) then
         465             :          ! Where q is too small, zero mass and number
         466             :          ! concentration.
         467 13941767952 :          where (state%q(:ncol,:,qix) < lim)
         468     2990736 :             state%q(:ncol,:,qix) = 0._r8
         469     2990736 :             state%q(:ncol,:,numix) = 0._r8
         470             :          end where
         471             :       else
         472             :          ! If no number index, just do mass.
         473           0 :           where (state%q(:ncol,:,qix) < lim)
         474           0 :              state%q(:ncol,:,qix) = 0._r8
         475             :           end where
         476             :       end if
         477             :
         478    65560896 :     end subroutine state_cnst_min_nz
         479             :
         480             :
         481             :   end subroutine physics_update
         482             :
         483             : !===============================================================================
         484             :
         485    61105752 :   subroutine physics_state_check(state, name)
         486             : !-----------------------------------------------------------------------
         487             : ! Check a physics_state object for invalid data (e.g NaNs, negative
         488             : ! temperatures).
         489             : !-----------------------------------------------------------------------
         490             :     use shr_infnan_mod, only: assignment(=), &
         491             :                               shr_infnan_posinf, shr_infnan_neginf
         492             :     use shr_assert_mod, only: shr_assert_in_domain
         493             :     use constituents,   only: pcnst
         494             :
         495             : !------------------------------Arguments--------------------------------
         496             :     ! State to check.
         497             :     type(physics_state), intent(in)           :: state
         498             :     ! Name of the package responsible for this state.
         499             :     character(len=*),    intent(in), optional :: name
         500             :
         501             : !---------------------------Local storage-------------------------------
         502             :     ! Shortened name for ncol.
         503             :     integer :: ncol
         504             :     ! Double precision positive/negative infinity.
         505             :     real(r8) :: posinf_r8, neginf_r8
         506             :     ! Canned message.
         507             :     character(len=64) :: msg
         508             :     ! Constituent index
         509             :     integer :: m
         510             :
         511             : !-----------------------------------------------------------------------
         512             :
         513    61105752 :     ncol = state%ncol
         514             :
         515    61105752 :     posinf_r8 = shr_infnan_posinf
         516    61105752 :     neginf_r8 = shr_infnan_neginf
         517             :
         518             :     ! It may be reasonable to check some of the integer components of the
         519             :     ! state as well, but this is not yet implemented.
         520             :
         521             :     ! Check for NaN first to avoid any IEEE exceptions.
         522             :
         523    61105752 :     if (present(name)) then
         524             :        msg = "NaN produced in physics_state by package "// &
         525    61105752 :             trim(name)//"."
         526             :     else
         527           0 :        msg = "NaN found in physics_state."
         528             :     end if
         529             :
         530             :     ! 1-D variables
         531           0 :     call shr_assert_in_domain(state%ps(:ncol),          is_nan=.false., &
         532    61105752 :          varname="state%ps",        msg=msg)
         533           0 :     call shr_assert_in_domain(state%psdry(:ncol),       is_nan=.false., &
         534    61105752 :          varname="state%psdry",     msg=msg)
         535           0 :     call shr_assert_in_domain(state%phis(:ncol),        is_nan=.false., &
         536    61105752 :          varname="state%phis",      msg=msg)
         537           0 :     call shr_assert_in_domain(state%te_ini(:ncol,:),    is_nan=.false., &
         538    61105752 :          varname="state%te_ini",    msg=msg)
         539           0 :     call shr_assert_in_domain(state%te_cur(:ncol,:),    is_nan=.false., &
         540    61105752 :          varname="state%te_cur",    msg=msg)
         541           0 :     call shr_assert_in_domain(state%tw_ini(:ncol),      is_nan=.false., &
         542    61105752 :          varname="state%tw_ini",    msg=msg)
         543           0 :     call shr_assert_in_domain(state%tw_cur(:ncol),      is_nan=.false., &
         544    61105752 :          varname="state%tw_cur",    msg=msg)
         545           0 :     call shr_assert_in_domain(state%temp_ini(:ncol,:),  is_nan=.false., &
         546    61105752 :          varname="state%temp_ini",  msg=msg)
         547           0 :     call shr_assert_in_domain(state%z_ini(:ncol,:),  is_nan=.false., &
         548    61105752 :          varname="state%z_ini",  msg=msg)
         549             :
         550             :     ! 2-D variables (at midpoints)
         551           0 :     call shr_assert_in_domain(state%t(:ncol,:),         is_nan=.false., &
         552    61105752 :          varname="state%t",         msg=msg)
         553           0 :     call shr_assert_in_domain(state%u(:ncol,:),         is_nan=.false., &
         554    61105752 :          varname="state%u",         msg=msg)
         555           0 :     call shr_assert_in_domain(state%v(:ncol,:),         is_nan=.false., &
         556    61105752 :          varname="state%v",         msg=msg)
         557           0 :     call shr_assert_in_domain(state%s(:ncol,:),         is_nan=.false., &
         558    61105752 :          varname="state%s",         msg=msg)
         559           0 :     call shr_assert_in_domain(state%omega(:ncol,:),     is_nan=.false., &
         560    61105752 :          varname="state%omega",     msg=msg)
         561           0 :     call shr_assert_in_domain(state%pmid(:ncol,:),      is_nan=.false., &
         562    61105752 :          varname="state%pmid",      msg=msg)
         563           0 :     call shr_assert_in_domain(state%pmiddry(:ncol,:),   is_nan=.false., &
         564    61105752 :          varname="state%pmiddry",   msg=msg)
         565           0 :     call shr_assert_in_domain(state%pdel(:ncol,:),      is_nan=.false., &
         566    61105752 :          varname="state%pdel",      msg=msg)
         567           0 :     call shr_assert_in_domain(state%pdeldry(:ncol,:),   is_nan=.false., &
         568    61105752 :          varname="state%pdeldry",   msg=msg)
         569           0 :     call shr_assert_in_domain(state%rpdel(:ncol,:),     is_nan=.false., &
         570    61105752 :          varname="state%rpdel",     msg=msg)
         571           0 :     call shr_assert_in_domain(state%rpdeldry(:ncol,:),  is_nan=.false., &
         572    61105752 :          varname="state%rpdeldry",  msg=msg)
         573           0 :     call shr_assert_in_domain(state%lnpmid(:ncol,:),    is_nan=.false., &
         574    61105752 :          varname="state%lnpmid",    msg=msg)
         575           0 :     call shr_assert_in_domain(state%lnpmiddry(:ncol,:), is_nan=.false., &
         576    61105752 :          varname="state%lnpmiddry", msg=msg)
         577           0 :     call shr_assert_in_domain(state%exner(:ncol,:),     is_nan=.false., &
         578    61105752 :          varname="state%exner",     msg=msg)
         579           0 :     call shr_assert_in_domain(state%zm(:ncol,:),        is_nan=.false., &
         580    61105752 :          varname="state%zm",        msg=msg)
         581             :
         582             :     ! 2-D variables (at interfaces)
         583           0 :     call shr_assert_in_domain(state%pint(:ncol,:),      is_nan=.false., &
         584    61105752 :          varname="state%pint",      msg=msg)
         585           0 :     call shr_assert_in_domain(state%pintdry(:ncol,:),   is_nan=.false., &
         586    61105752 :          varname="state%pintdry",   msg=msg)
         587           0 :     call shr_assert_in_domain(state%lnpint(:ncol,:),    is_nan=.false., &
         588    61105752 :          varname="state%lnpint",    msg=msg)
         589           0 :     call shr_assert_in_domain(state%lnpintdry(:ncol,:), is_nan=.false., &
         590    61105752 :          varname="state%lnpintdry", msg=msg)
         591           0 :     call shr_assert_in_domain(state%zi(:ncol,:),        is_nan=.false., &
         592    61105752 :          varname="state%zi",        msg=msg)
         593             :
         594             :     ! 3-D variables
         595           0 :     call shr_assert_in_domain(state%q(:ncol,:,:),       is_nan=.false., &
         596    61105752 :          varname="state%q",         msg=msg)
         597             :
         598             :     ! Now run other checks (i.e. values are finite and within a range that
         599             :     ! is physically meaningful).
         600             :
         601    61105752 :     if (present(name)) then
         602             :        msg = "Invalid value produced in physics_state by package "// &
         603    61105752 :             trim(name)//"."
         604             :     else
         605           0 :        msg = "Invalid value found in physics_state."
         606             :     end if
         607             :
         608             :     ! 1-D variables
         609             :     call shr_assert_in_domain(state%ps(:ncol),          lt=posinf_r8, gt=0._r8, &
         610    61105752 :          varname="state%ps",        msg=msg)
         611             :     call shr_assert_in_domain(state%psdry(:ncol),       lt=posinf_r8, gt=0._r8, &
         612    61105752 :          varname="state%psdry",     msg=msg)
         613             :     call shr_assert_in_domain(state%phis(:ncol),        lt=posinf_r8, gt=neginf_r8, &
         614    61105752 :          varname="state%phis",      msg=msg)
         615             :     call shr_assert_in_domain(state%te_ini(:ncol,:),    lt=posinf_r8, gt=neginf_r8, &
         616    61105752 :          varname="state%te_ini",    msg=msg)
         617             :     call shr_assert_in_domain(state%te_cur(:ncol,:),    lt=posinf_r8, gt=neginf_r8, &
         618    61105752 :          varname="state%te_cur",    msg=msg)
         619             :     call shr_assert_in_domain(state%tw_ini(:ncol),      lt=posinf_r8, gt=neginf_r8, &
         620    61105752 :          varname="state%tw_ini",    msg=msg)
         621             :     call shr_assert_in_domain(state%tw_cur(:ncol),      lt=posinf_r8, gt=neginf_r8, &
         622    61105752 :          varname="state%tw_cur",    msg=msg)
         623             :     call shr_assert_in_domain(state%temp_ini(:ncol,:),  lt=posinf_r8, gt=neginf_r8, &
         624    61105752 :          varname="state%temp_ini",  msg=msg)
         625             :     call shr_assert_in_domain(state%z_ini(:ncol,:),  lt=posinf_r8, gt=neginf_r8, &
         626    61105752 :          varname="state%z_ini",  msg=msg)
         627             :
         628             :     ! 2-D variables (at midpoints)
         629             :     call shr_assert_in_domain(state%t(:ncol,:),         lt=posinf_r8, gt=0._r8, &
         630    61105752 :          varname="state%t",         msg=msg)
         631             :     call shr_assert_in_domain(state%u(:ncol,:),         lt=posinf_r8, gt=neginf_r8, &
         632    61105752 :          varname="state%u",         msg=msg)
         633             :     call shr_assert_in_domain(state%v(:ncol,:),         lt=posinf_r8, gt=neginf_r8, &
         634    61105752 :          varname="state%v",         msg=msg)
         635             :     call shr_assert_in_domain(state%s(:ncol,:),         lt=posinf_r8, gt=neginf_r8, &
         636    61105752 :          varname="state%s",         msg=msg)
         637             :     call shr_assert_in_domain(state%omega(:ncol,:),     lt=posinf_r8, gt=neginf_r8, &
         638    61105752 :          varname="state%omega",     msg=msg)
         639             :     call shr_assert_in_domain(state%pmid(:ncol,:),      lt=posinf_r8, gt=0._r8, &
         640    61105752 :          varname="state%pmid",      msg=msg)
         641             :     call shr_assert_in_domain(state%pmiddry(:ncol,:),   lt=posinf_r8, gt=0._r8, &
         642    61105752 :          varname="state%pmiddry",   msg=msg)
         643             :     call shr_assert_in_domain(state%pdel(:ncol,:),      lt=posinf_r8, gt=neginf_r8, &
         644    61105752 :          varname="state%pdel",      msg=msg)
         645             :     call shr_assert_in_domain(state%pdeldry(:ncol,:),   lt=posinf_r8, gt=neginf_r8, &
         646    61105752 :          varname="state%pdeldry",   msg=msg)
         647             :     call shr_assert_in_domain(state%rpdel(:ncol,:),     lt=posinf_r8, gt=neginf_r8, &
         648    61105752 :          varname="state%rpdel",     msg=msg)
         649             :     call shr_assert_in_domain(state%rpdeldry(:ncol,:),  lt=posinf_r8, gt=neginf_r8, &
         650    61105752 :          varname="state%rpdeldry",  msg=msg)
         651             :     call shr_assert_in_domain(state%lnpmid(:ncol,:),    lt=posinf_r8, gt=neginf_r8, &
         652    61105752 :          varname="state%lnpmid",    msg=msg)
         653             :     call shr_assert_in_domain(state%lnpmiddry(:ncol,:), lt=posinf_r8, gt=neginf_r8, &
         654    61105752 :          varname="state%lnpmiddry", msg=msg)
         655             :     call shr_assert_in_domain(state%exner(:ncol,:),     lt=posinf_r8, gt=0._r8, &
         656    61105752 :          varname="state%exner",     msg=msg)
         657             :     call shr_assert_in_domain(state%zm(:ncol,:),        lt=posinf_r8, gt=neginf_r8, &
         658    61105752 :          varname="state%zm",        msg=msg)
         659             :
         660             :     ! 2-D variables (at interfaces)
         661             :     call shr_assert_in_domain(state%pint(:ncol,:),      lt=posinf_r8, gt=0._r8, &
         662    61105752 :          varname="state%pint",      msg=msg)
         663             :     call shr_assert_in_domain(state%pintdry(:ncol,:),   lt=posinf_r8, gt=0._r8, &
         664    61105752 :          varname="state%pintdry",   msg=msg)
         665             :     call shr_assert_in_domain(state%lnpint(:ncol,:),    lt=posinf_r8, gt=neginf_r8, &
         666    61105752 :          varname="state%lnpint",    msg=msg)
         667             :     call shr_assert_in_domain(state%lnpintdry(:ncol,:), lt=posinf_r8, gt=neginf_r8, &
         668    61105752 :          varname="state%lnpintdry", msg=msg)
         669             :     call shr_assert_in_domain(state%zi(:ncol,:),        lt=posinf_r8, gt=neginf_r8, &
         670    61105752 :          varname="state%zi",        msg=msg)
         671             :
         672             :     ! 3-D variables
         673  2566441584 :     do m = 1,pcnst
         674             :        call shr_assert_in_domain(state%q(:ncol,:,m),    lt=posinf_r8, gt=neginf_r8, &
         675  2566441584 :             varname="state%q ("//trim(cnst_name(m))//")", msg=msg)
         676             :     end do
         677             :
         678    61105752 :   end subroutine physics_state_check
         679             :
         680             : !===============================================================================
         681             :
         682    37254168 :   subroutine physics_ptend_sum(ptend, ptend_sum, ncol)
         683             : !-----------------------------------------------------------------------
         684             : ! Add ptend fields to ptend_sum for ptend logical flags = .true.
         685             : ! Where ptend logical flags = .false, don't change ptend_sum
         686             : !-----------------------------------------------------------------------
         687             :
         688             : !------------------------------Arguments--------------------------------
         689             :     type(physics_ptend), intent(in)     :: ptend   ! New parameterization tendencies
         690             :     type(physics_ptend), intent(inout)  :: ptend_sum   ! Sum of incoming ptend_sum and ptend
         691             :     integer, intent(in)                 :: ncol    ! number of columns
         692             :
         693             : !---------------------------Local storage-------------------------------
         694             :     integer :: i,k,m                               ! column,level,constituent indices
         695             :     integer :: psetcols                            ! maximum number of columns
         696             :     integer :: ierr = 0
         697             :
         698             : !-----------------------------------------------------------------------
         699    37254168 :     if (ptend%psetcols /= ptend_sum%psetcols) then
         700           0 :        call endrun('physics_ptend_sum error: ptend and ptend_sum must have the same value for psetcols')
         701             :     end if
         702             :
         703    37254168 :     if (ncol > ptend_sum%psetcols) then
         704           0 :        call endrun('physics_ptend_sum error: ncol must be less than or equal to psetcols')
         705             :     end if
         706             :
         707    37254168 :     psetcols = ptend_sum%psetcols
         708             :
         709    37254168 :     ptend_sum%top_level = ptend%top_level
         710    37254168 :     ptend_sum%bot_level = ptend%bot_level
         711             :
         712             : ! Update u,v fields
         713    37254168 :     if(ptend%lu) then
         714    10430424 :        if (.not. allocated(ptend_sum%u)) then
         715    17888688 :           allocate(ptend_sum%u(psetcols,pver), stat=ierr)
         716     5962896 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%u')
         717  9433301472 :           ptend_sum%u=0.0_r8
         718             :
         719    17888688 :           allocate(ptend_sum%taux_srf(psetcols), stat=ierr)
         720     5962896 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%taux_srf')
         721   101369232 :           ptend_sum%taux_srf=0.0_r8
         722             :
         723    11925792 :           allocate(ptend_sum%taux_top(psetcols), stat=ierr)
         724     5962896 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%taux_top')
         725   101369232 :           ptend_sum%taux_top=0.0_r8
         726             :        end if
         727    10430424 :        ptend_sum%lu = .true.
         728             :
         729   980459856 :        do k = ptend%top_level, ptend%bot_level
         730 16207666056 :           do i = 1, ncol
         731 16197235632 :              ptend_sum%u(i,k) = ptend_sum%u(i,k) + ptend%u(i,k)
         732             :           end do
         733             :        end do
         734   174163824 :        do i = 1, ncol
         735   163733400 :           ptend_sum%taux_srf(i) = ptend_sum%taux_srf(i) + ptend%taux_srf(i)
         736   174163824 :           ptend_sum%taux_top(i) = ptend_sum%taux_top(i) + ptend%taux_top(i)
         737             :        end do
         738             :     end if
         739             :
         740    37254168 :     if(ptend%lv) then
         741    10430424 :        if (.not. allocated(ptend_sum%v)) then
         742    17888688 :           allocate(ptend_sum%v(psetcols,pver), stat=ierr)
         743     5962896 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%v')
         744  9433301472 :           ptend_sum%v=0.0_r8
         745             :
         746    17888688 :           allocate(ptend_sum%tauy_srf(psetcols), stat=ierr)
         747     5962896 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%tauy_srf')
         748   101369232 :           ptend_sum%tauy_srf=0.0_r8
         749             :
         750    11925792 :           allocate(ptend_sum%tauy_top(psetcols), stat=ierr)
         751     5962896 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%tauy_top')
         752   101369232 :           ptend_sum%tauy_top=0.0_r8
         753             :        end if
         754    10430424 :        ptend_sum%lv = .true.
         755             :
         756   980459856 :        do k = ptend%top_level, ptend%bot_level
         757 16207666056 :           do i = 1, ncol
         758 16197235632 :              ptend_sum%v(i,k) = ptend_sum%v(i,k) + ptend%v(i,k)
         759             :           end do
         760             :        end do
         761   174163824 :        do i = 1, ncol
         762   163733400 :           ptend_sum%tauy_srf(i) = ptend_sum%tauy_srf(i) + ptend%tauy_srf(i)
         763   174163824 :           ptend_sum%tauy_top(i) = ptend_sum%tauy_top(i) + ptend%tauy_top(i)
         764             :        end do
         765             :     end if
         766             :
         767             :
         768    37254168 :     if(ptend%ls) then
         769    22356216 :        if (.not. allocated(ptend_sum%s)) then
         770    22356216 :           allocate(ptend_sum%s(psetcols,pver), stat=ierr)
         771     7452072 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%s')
         772 11789177904 :           ptend_sum%s=0.0_r8
         773             :
         774    22356216 :           allocate(ptend_sum%hflux_srf(psetcols), stat=ierr)
         775     7452072 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%hflux_srf')
         776   126685224 :           ptend_sum%hflux_srf=0.0_r8
         777             :
         778    14904144 :           allocate(ptend_sum%hflux_top(psetcols), stat=ierr)
         779     7452072 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%hflux_top')
         780   126685224 :           ptend_sum%hflux_top=0.0_r8
         781             :        end if
         782    22356216 :        ptend_sum%ls = .true.
         783             :
         784  2101484304 :        do k = ptend%top_level, ptend%bot_level
         785 34738960104 :           do i = 1, ncol
         786 34716603888 :              ptend_sum%s(i,k) = ptend_sum%s(i,k) + ptend%s(i,k)
         787             :           end do
         788             :        end do
         789   373296816 :        do i = 1, ncol
         790   350940600 :           ptend_sum%hflux_srf(i) = ptend_sum%hflux_srf(i) + ptend%hflux_srf(i)
         791   373296816 :           ptend_sum%hflux_top(i) = ptend_sum%hflux_top(i) + ptend%hflux_top(i)
         792             :        end do
         793             :     end if
         794             :
         795   292163328 :     if (any(ptend%lq(:))) then
         796             :
         797    35758800 :        if (.not. allocated(ptend_sum%q)) then
         798    47678400 :           allocate(ptend_sum%q(psetcols,pver,pcnst), stat=ierr)
         799    11919600 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%q')
         800 >77314*10^7 :           ptend_sum%q=0.0_r8
         801             :
         802    35758800 :           allocate(ptend_sum%cflx_srf(psetcols,pcnst), stat=ierr)
         803    11919600 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%cflx_srf')
         804  8319880800 :           ptend_sum%cflx_srf=0.0_r8
         805             :
         806    23839200 :           allocate(ptend_sum%cflx_top(psetcols,pcnst), stat=ierr)
         807    11919600 :           if ( ierr /= 0 ) call endrun('physics_ptend_sum error: allocation error for ptend_sum%cflx_top')
         808  8319880800 :           ptend_sum%cflx_top=0.0_r8
         809             :        end if
         810             :
         811  1501869600 :        do m = 1, pcnst
         812  1501869600 :           if(ptend%lq(m)) then
         813   455762160 :              ptend_sum%lq(m) = .true.
         814 42841643040 :              do k = ptend%top_level, ptend%bot_level
         815 >70820*10^7 :                 do i = 1,ncol
         816 >70774*10^7 :                    ptend_sum%q(i,k,m) = ptend_sum%q(i,k,m) + ptend%q(i,k,m)
         817             :                 end do
         818             :              end do
         819  7610168160 :              do i = 1,ncol
         820  7154406000 :                 ptend_sum%cflx_srf(i,m) = ptend_sum%cflx_srf(i,m) + ptend%cflx_srf(i,m)
         821  7610168160 :                 ptend_sum%cflx_top(i,m) = ptend_sum%cflx_top(i,m) + ptend%cflx_top(i,m)
         822             :              end do
         823             :           end if
         824             :        end do
         825             :
         826             :     end if
         827             :
         828    37254168 :   end subroutine physics_ptend_sum
         829             :
         830             : !===============================================================================
         831             :
         832    13402584 :   subroutine physics_ptend_scale(ptend, fac, ncol)
         833             : !-----------------------------------------------------------------------
         834             : ! Scale ptend fields for ptend logical flags = .true.
         835             : ! Where ptend logical flags = .false, don't change ptend.
         836             : !
         837             : ! Assumes that input ptend is valid (e.g. that
         838             : ! ptend%lu .eqv. allocated(ptend%u)), and therefore
         839             : ! does not check allocation status of individual arrays.
         840             : !-----------------------------------------------------------------------
         841             :
         842             : !------------------------------Arguments--------------------------------
         843             :     type(physics_ptend), intent(inout)  :: ptend   ! Incoming ptend
         844             :     real(r8), intent(in) :: fac                    ! Factor to multiply ptend by.
         845             :     integer, intent(in)                 :: ncol    ! number of columns
         846             :
         847             : !---------------------------Local storage-------------------------------
         848             :     integer :: m                                   ! constituent index
         849             :
         850             : !-----------------------------------------------------------------------
         851             :
         852             : ! Update u,v fields
         853    13402584 :     if (ptend%lu) &
         854             :          call multiply_tendency(ptend%u, &
         855     4467528 :          ptend%taux_srf, ptend%taux_top)
         856             :
         857    13402584 :     if (ptend%lv) &
         858             :          call multiply_tendency(ptend%v, &
         859     4467528 :          ptend%tauy_srf, ptend%tauy_top)
         860             :
         861             : ! Heat
         862    13402584 :     if (ptend%ls) &
         863             :          call multiply_tendency(ptend%s, &
         864    13402584 :          ptend%hflux_srf, ptend%hflux_top)
         865             :
         866             : ! Update constituents
         867   562908528 :     do m = 1, pcnst
         868   549505944 :        if (ptend%lq(m)) &
         869             :             call multiply_tendency(ptend%q(:,:,m), &
         870   294856848 :             ptend%cflx_srf(:,m), ptend%cflx_top(:,m))
         871             :     end do
         872             :
         873             :
         874             :   contains
         875             :
         876   303791904 :     subroutine multiply_tendency(tend_arr, flx_srf, flx_top)
         877             :       real(r8), intent(inout) :: tend_arr(:,:) ! Tendency array (pcols, plev)
         878             :       real(r8), intent(inout) :: flx_srf(:)    ! Surface flux (or stress)
         879             :       real(r8), intent(inout) :: flx_top(:)    ! Top-of-model flux (or stress)
         880             :
         881             :       integer :: k
         882             :
         883 28556438976 :       do k = ptend%top_level, ptend%bot_level
         884 >47205*10^7 :          tend_arr(:ncol,k) = tend_arr(:ncol,k) * fac
         885             :       end do
         886  5072618304 :       flx_srf(:ncol) = flx_srf(:ncol) * fac
         887  5072618304 :       flx_top(:ncol) = flx_top(:ncol) * fac
         888             :
         889   303791904 :     end subroutine multiply_tendency
         890             :
         891             :   end subroutine physics_ptend_scale
         892             :
         893             : !===============================================================================
         894             :
         895           0 : subroutine physics_ptend_copy(ptend, ptend_cp)
         896             :
         897             :    !-----------------------------------------------------------------------
         898             :    ! Copy a physics_ptend object.  Allocate ptend_cp internally before copy.
         899             :    !-----------------------------------------------------------------------
         900             :
         901             :    type(physics_ptend), intent(in)    :: ptend    ! ptend source
         902             :    type(physics_ptend), intent(out)   :: ptend_cp ! copy of ptend
         903             :
         904             :    !-----------------------------------------------------------------------
         905             :
         906           0 :    ptend_cp%name      = ptend%name
         907             :
         908           0 :    ptend_cp%ls = ptend%ls
         909           0 :    ptend_cp%lu = ptend%lu
         910           0 :    ptend_cp%lv = ptend%lv
         911           0 :    ptend_cp%lq = ptend%lq
         912             :
         913           0 :    call physics_ptend_alloc(ptend_cp, ptend%psetcols)
         914             :
         915           0 :    ptend_cp%top_level = ptend%top_level
         916           0 :    ptend_cp%bot_level = ptend%bot_level
         917             :
         918           0 :    if (ptend_cp%ls) then
         919           0 :       ptend_cp%s = ptend%s
         920           0 :       ptend_cp%hflux_srf = ptend%hflux_srf
         921           0 :       ptend_cp%hflux_top = ptend%hflux_top
         922             :    end if
         923             :
         924           0 :    if (ptend_cp%lu) then
         925           0 :       ptend_cp%u = ptend%u
         926           0 :       ptend_cp%taux_srf  = ptend%taux_srf
         927           0 :       ptend_cp%taux_top  = ptend%taux_top
         928             :    end if
         929             :
         930           0 :    if (ptend_cp%lv) then
         931           0 :       ptend_cp%v = ptend%v
         932           0 :       ptend_cp%tauy_srf  = ptend%tauy_srf
         933           0 :       ptend_cp%tauy_top  = ptend%tauy_top
         934             :    end if
         935             :
         936           0 :    if (any(ptend_cp%lq(:))) then
         937           0 :       ptend_cp%q = ptend%q
         938           0 :       ptend_cp%cflx_srf  = ptend%cflx_srf
         939           0 :       ptend_cp%cflx_top  = ptend%cflx_top
         940             :    end if
         941             :
         942           0 : end subroutine physics_ptend_copy
         943             :
         944             : !===============================================================================
         945             :
         946    53650584 :   subroutine physics_ptend_reset(ptend)
         947             : !-----------------------------------------------------------------------
         948             : ! Reset the parameterization tendency structure to "empty"
         949             : !-----------------------------------------------------------------------
         950             :
         951             : !------------------------------Arguments--------------------------------
         952             :     type(physics_ptend), intent(inout)  :: ptend   ! Parameterization tendencies
         953             : !-----------------------------------------------------------------------
         954             :
         955    53650584 :     if(ptend%ls) then
         956 51883158096 :        ptend%s = 0._r8
         957   557530776 :        ptend%hflux_srf = 0._r8
         958   557530776 :        ptend%hflux_top = 0._r8
         959             :     endif
         960    53650584 :     if(ptend%lu) then
         961 18856807200 :        ptend%u = 0._r8
         962   202633200 :        ptend%taux_srf = 0._r8
         963   202633200 :        ptend%taux_top = 0._r8
         964             :     endif
         965    53650584 :     if(ptend%lv) then
         966 18856807200 :        ptend%v = 0._r8
         967   202633200 :        ptend%tauy_srf = 0._r8
         968   202633200 :        ptend%tauy_top = 0._r8
         969             :     endif
         970   574013880 :     if(any (ptend%lq(:))) then
         971 >29959*10^8 :        ptend%q = 0._r8
         972 32240078352 :        ptend%cflx_srf = 0._r8
         973 32240078352 :        ptend%cflx_top = 0._r8
         974             :     end if
         975             :
         976    53650584 :     ptend%top_level = 1
         977    53650584 :     ptend%bot_level = pver
         978             :
         979    53650584 :     return
         980             :   end subroutine physics_ptend_reset
         981             :
         982             : !===============================================================================
         983  2941583904 :   subroutine physics_ptend_init(ptend, psetcols, name, ls, lu, lv, lq)
         984             : !-----------------------------------------------------------------------
         985             : ! Allocate the fields in the structure which are specified.
         986             : ! Initialize the parameterization tendency structure to "empty"
         987             : !-----------------------------------------------------------------------
         988             :
         989             : !------------------------------Arguments--------------------------------
         990             :     type(physics_ptend), intent(out)    :: ptend    ! Parameterization tendencies
         991             :     integer, intent(in)                 :: psetcols ! maximum number of columns
         992             :     character(len=*)                    :: name     ! optional name of parameterization which produced tendencies.
         993             :     logical, optional                   :: ls       ! if true, then fields to support dsdt are allocated
         994             :     logical, optional                   :: lu       ! if true, then fields to support dudt are allocated
         995             :     logical, optional                   :: lv       ! if true, then fields to support dvdt are allocated
         996             :     logical, dimension(pcnst),optional  :: lq       ! if true, then fields to support dqdt are allocated
         997             :
         998             : !-----------------------------------------------------------------------
         999             :
        1000    70037712 :     if (allocated(ptend%s)) then
        1001           0 :        call endrun(' physics_ptend_init: ptend should not be allocated before calling this routine')
        1002             :     end if
        1003             :
        1004    70037712 :     ptend%name     = name
        1005    70037712 :     ptend%psetcols =  psetcols
        1006             :
        1007             :     ! If no fields being stored, initialize all values to appropriate nulls and return
        1008    70037712 :     if (.not. present(ls) .and. .not. present(lu) .and. .not. present(lv) .and. .not. present(lq) ) then
        1009    16387128 :        ptend%ls       = .false.
        1010    16387128 :        ptend%lu       = .false.
        1011    16387128 :        ptend%lv       = .false.
        1012   688259376 :        ptend%lq(:)    = .false.
        1013    16387128 :        ptend%top_level = 1
        1014    16387128 :        ptend%bot_level = pver
        1015    16387128 :        return
        1016             :     end if
        1017             :
        1018    53650584 :     if (present(ls)) then
        1019    32795928 :        ptend%ls = ls
        1020             :     else
        1021    20854656 :        ptend%ls = .false.
        1022             :     end if
        1023             :
        1024    53650584 :     if (present(lu)) then
        1025    11919600 :        ptend%lu = lu
        1026             :     else
        1027    41730984 :        ptend%lu = .false.
        1028             :     end if
        1029             :
        1030    53650584 :     if (present(lv)) then
        1031    11919600 :        ptend%lv = lv
        1032             :     else
        1033    41730984 :        ptend%lv = .false.
        1034             :     end if
        1035             :
        1036    53650584 :     if (present(lq)) then
        1037  1939947408 :        ptend%lq(:) = lq(:)
        1038             :     else
        1039   313377120 :        ptend%lq(:) = .false.
        1040             :     end if
        1041             :
        1042    53650584 :     call physics_ptend_alloc(ptend, psetcols)
        1043             :
        1044    53650584 :     call physics_ptend_reset(ptend)
        1045             :
        1046    53650584 :     return
        1047    70037712 :   end subroutine physics_ptend_init
        1048             :
        1049             : !===============================================================================
        1050             :
        1051        6192 :   subroutine physics_state_set_grid(lchnk, phys_state)
        1052             : !-----------------------------------------------------------------------
        1053             : ! Set the grid components of the physics_state object
        1054             : !-----------------------------------------------------------------------
        1055             :
        1056             :     integer,             intent(in)    :: lchnk
        1057             :     type(physics_state), intent(inout) :: phys_state
        1058             :
        1059             :     ! local variables
        1060             :     integer  :: i, ncol
        1061             :     real(r8) :: rlon(pcols)
        1062             :     real(r8) :: rlat(pcols)
        1063             :
        1064             :     !-----------------------------------------------------------------------
        1065             :     ! get_ncols_p requires a state which does not have sub-columns
        1066        6192 :     if (phys_state%psetcols .ne. pcols) then
        1067           0 :        call endrun('physics_state_set_grid: cannot pass in a state which has sub-columns')
        1068             :     end if
        1069             :
        1070        6192 :     ncol = get_ncols_p(lchnk)
        1071             :
        1072        6192 :     if(ncol<=0) then
        1073           0 :        write(iulog,*) lchnk, ncol
        1074           0 :        call endrun('physics_state_set_grid')
        1075             :     end if
        1076             :
        1077        6192 :     call get_rlon_all_p(lchnk, ncol, rlon)
        1078        6192 :     call get_rlat_all_p(lchnk, ncol, rlat)
        1079        6192 :     phys_state%ncol  = ncol
        1080        6192 :     phys_state%lchnk = lchnk
        1081      103392 :     do i=1,ncol
        1082       97200 :        phys_state%lat(i) = rlat(i)
        1083      103392 :        phys_state%lon(i) = rlon(i)
        1084             :     end do
        1085        6192 :     call init_geo_unique(phys_state,ncol)
        1086             :
        1087        6192 :   end subroutine physics_state_set_grid
        1088             :
        1089             :
        1090        6192 :   subroutine init_geo_unique(phys_state,ncol)
        1091             :     integer,             intent(in)    :: ncol
        1092             :     type(physics_state), intent(inout) :: phys_state
        1093             :     logical :: match
        1094             :     integer :: i, j, ulatcnt, uloncnt
        1095             :
        1096      105264 :     phys_state%ulat=-999._r8
        1097      105264 :     phys_state%ulon=-999._r8
        1098      105264 :     phys_state%latmapback=0
        1099      105264 :     phys_state%lonmapback=0
        1100             :     match=.false.
        1101        6192 :     ulatcnt=0
        1102        6192 :     uloncnt=0
        1103        6192 :     match=.false.
        1104      103392 :     do i=1,ncol
        1105      807210 :        do j=1,ulatcnt
        1106      807210 :           if(phys_state%lat(i) .eq. phys_state%ulat(j)) then
        1107        1858 :              match=.true.
        1108        1858 :              phys_state%latmapback(i)=j
        1109             :           end if
        1110             :        end do
        1111       97200 :        if(.not. match) then
        1112       95342 :           ulatcnt=ulatcnt+1
        1113       95342 :           phys_state%ulat(ulatcnt)=phys_state%lat(i)
        1114       95342 :           phys_state%latmapback(i)=ulatcnt
        1115             :        end if
        1116             :
        1117       97200 :        match=.false.
        1118      597614 :        do j=1,uloncnt
        1119      597614 :           if(phys_state%lon(i) .eq. phys_state%ulon(j)) then
        1120       38438 :              match=.true.
        1121       38438 :              phys_state%lonmapback(i)=j
        1122             :           end if
        1123             :        end do
        1124       97200 :        if(.not. match) then
        1125       58762 :           uloncnt=uloncnt+1
        1126       58762 :           phys_state%ulon(uloncnt)=phys_state%lon(i)
        1127       58762 :           phys_state%lonmapback(i)=uloncnt
        1128             :        end if
        1129      103392 :        match=.false.
        1130             :
        1131             :     end do
        1132        6192 :     phys_state%uloncnt=uloncnt
        1133        6192 :     phys_state%ulatcnt=ulatcnt
        1134             :
        1135        6192 :     call get_gcol_all_p(phys_state%lchnk,pcols,phys_state%cid)
        1136             :
        1137             :
        1138        6192 :   end subroutine init_geo_unique
        1139             :
        1140             : !===============================================================================
        1141           0 :   subroutine physics_cnst_limit(state)
        1142             :     type(physics_state), intent(inout) :: state
        1143             :
        1144             :     integer :: i,k, ncol
        1145             :
        1146             :     real(r8) :: mmrSum_O_O2_H                ! Sum of mass mixing ratios for O, O2, and H
        1147             :     real(r8), parameter :: mmrMin=1.e-20_r8  ! lower limit of o2, o, and h mixing ratios
        1148             :     real(r8), parameter :: N2mmrMin=1.e-6_r8 ! lower limit of N2 mass mixing ratio
        1149             :     real(r8), parameter :: H2lim=6.e-5_r8    ! H2 limiter: 10x global H2 MMR (Roble, 1995)
        1150             :     integer :: ixo, ixo2, ixh, ixh2
        1151             :
        1152           0 :     if ( waccmx_is('ionosphere') .or. waccmx_is('neutral') ) then
        1153           0 :        call cnst_get_ind('O', ixo)
        1154           0 :        call cnst_get_ind('O2', ixo2)
        1155           0 :        call cnst_get_ind('H', ixh)
        1156           0 :        call cnst_get_ind('H2', ixh2)
        1157             :
        1158           0 :        ncol = state%ncol
        1159             :
        1160             :        !------------------------------------------------------------
        1161             :        ! Ensure N2 = 1-(O2 + O + H) mmr is greater than 0
        1162             :        ! Check for unusually large H2 values and set to lower value.
        1163             :        !------------------------------------------------------------
        1164             :
        1165           0 :        do k=1,pver
        1166           0 :           do i=1,ncol
        1167             :
        1168           0 :              if (state%q(i,k,ixo) < mmrMin) state%q(i,k,ixo) = mmrMin
        1169           0 :              if (state%q(i,k,ixo2) < mmrMin) state%q(i,k,ixo2) = mmrMin
        1170             :
        1171           0 :              mmrSum_O_O2_H = state%q(i,k,ixo)+state%q(i,k,ixo2)+state%q(i,k,ixh)
        1172             :
        1173           0 :              if ((1._r8-mmrMin-mmrSum_O_O2_H) < 0._r8) then
        1174             :
        1175           0 :                 state%q(i,k,ixo) = state%q(i,k,ixo) * (1._r8 - N2mmrMin) / mmrSum_O_O2_H
        1176             :
        1177           0 :                 state%q(i,k,ixo2) = state%q(i,k,ixo2) * (1._r8 - N2mmrMin) / mmrSum_O_O2_H
        1178             :
        1179           0 :                 state%q(i,k,ixh) = state%q(i,k,ixh) * (1._r8 - N2mmrMin) / mmrSum_O_O2_H
        1180             :
        1181             :              endif
        1182             :
        1183           0 :              if(state%q(i,k,ixh2) > H2lim) then
        1184           0 :                 state%q(i,k,ixh2) = H2lim
        1185             :              endif
        1186             :
        1187             :           end do
        1188             :        end do
        1189             :
        1190             :     end if
        1191           0 :   end subroutine physics_cnst_limit
        1192             :
        1193             : !===============================================================================
        1194           0 :   subroutine physics_dme_adjust(state, tend, qini, liqini, iceini, dt)
        1195             :     use air_composition, only: dry_air_species_num,thermodynamic_active_species_num
        1196             :     use air_composition, only: thermodynamic_active_species_idx
        1197             :     use dycore,          only: dycore_is
        1198             :     !-----------------------------------------------------------------------
        1199             :     !
        1200             :     ! Purpose: Adjust the dry mass in each layer back to the value of physics input state
        1201             :     !
        1202             :     ! Method: Conserve the integrated mass, momentum and total energy in each layer
        1203             :     !         by scaling the specific mass of consituents, specific momentum (velocity)
        1204             :     !         and specific total energy by the relative change in layer mass. Solve for
        1205             :     !         the new temperature by subtracting the new kinetic energy from total energy
        1206             :     !         and inverting the hydrostatic equation
        1207             :     !
        1208             :     !         The mass in each layer is modified, changing the relationship of the layer
        1209             :     !         interfaces and midpoints to the surface pressure. The result is no longer in
        1210             :     !         the original hybrid coordinate.
        1211             :     !
        1212             :     !         This procedure cannot be applied to the "eul" or "sld" dycores because they
        1213             :     !         require the hybrid coordinate.
        1214             :     !
        1215             :     ! Author: Byron Boville
        1216             :
        1217             :     ! !REVISION HISTORY:
        1218             :     !   03.03.28  Boville    Created, partly from code by Lin in p_d_adjust
        1219             :     !
        1220             :     !-----------------------------------------------------------------------
        1221             :
        1222             :     implicit none
        1223             :     !
        1224             :     ! Arguments
        1225             :     !
        1226             :     type(physics_state), intent(inout) :: state
        1227             :     type(physics_tend ), intent(inout) :: tend
        1228             :     real(r8),            intent(in   ) :: qini(pcols,pver)    ! initial specific humidity
        1229             :     real(r8),            intent(in   ) :: liqini(pcols,pver)  ! initial total liquid
        1230             :     real(r8),            intent(in   ) :: iceini(pcols,pver)  ! initial total ice
        1231             :     real(r8),            intent(in   ) :: dt                  ! model physics timestep
        1232             :     !
        1233             :     !---------------------------Local workspace-----------------------------
        1234             :     !
        1235             :     integer  :: lchnk         ! chunk identifier
        1236             :     integer  :: ncol          ! number of atmospheric columns
        1237             :     integer  :: k,m           ! Longitude, level indices
        1238             :     real(r8) :: fdq(pcols)    ! mass adjustment factor
        1239             :     real(r8) :: te(pcols)     ! total energy in a layer
        1240             :     real(r8) :: utmp(pcols)   ! temp variable for recalculating the initial u values
        1241             :     real(r8) :: vtmp(pcols)   ! temp variable for recalculating the initial v values
        1242             :
        1243             :     real(r8) :: zvirv(pcols,pver)    ! Local zvir array pointer
        1244             :
        1245             :     real(r8) :: tot_water (pcols,2)  ! total water (initial, present)
        1246             :     real(r8) :: tot_water_chg(pcols) ! total water change
        1247             :
        1248             :
        1249             :     real(r8),allocatable :: cpairv_loc(:,:)
        1250             :     integer :: m_cnst
        1251             :     !
        1252             :     !-----------------------------------------------------------------------
        1253             :
        1254           0 :     if (state%psetcols .ne. pcols) then
        1255           0 :        call endrun('physics_dme_adjust: cannot pass in a state which has sub-columns')
        1256             :     end if
        1257             :
        1258           0 :     lchnk = state%lchnk
        1259           0 :     ncol  = state%ncol
        1260             :
        1261             :     ! adjust dry mass in each layer back to input value, while conserving
        1262             :     ! constituents, momentum, and total energy
        1263           0 :     state%ps(:ncol) = state%pint(:ncol,1)
        1264             :
        1265             :     !
        1266             :     ! original code for backwards compatability with FV and EUL
        1267             :     !
        1268           0 :     if (.not.(dycore_is('MPAS') .or. dycore_is('SE'))) then
        1269           0 :       do k = 1, pver
        1270             :
        1271             :         ! adjusment factor is just change in water vapor
        1272           0 :         fdq(:ncol) = 1._r8 + state%q(:ncol,k,1) - qini(:ncol,k)
        1273             :
        1274             :         ! adjust constituents to conserve mass in each layer
        1275           0 :         do m = 1, pcnst
        1276           0 :           state%q(:ncol,k,m) = state%q(:ncol,k,m) / fdq(:ncol)
        1277             :         end do
        1278             :         ! compute new total pressure variables
        1279           0 :         state%pdel  (:ncol,k  ) = state%pdel(:ncol,k  ) * fdq(:ncol)
        1280           0 :         state%ps(:ncol)         = state%ps(:ncol)       + state%pdel(:ncol,k)
        1281           0 :         state%pint  (:ncol,k+1) = state%pint(:ncol,k  ) + state%pdel(:ncol,k)
        1282           0 :         state%lnpint(:ncol,k+1) = log(state%pint(:ncol,k+1))
        1283           0 :         state%rpdel (:ncol,k  ) = 1._r8/ state%pdel(:ncol,k  )
        1284             :       end do
        1285             :     else
        1286           0 :       do k = 1, pver
        1287           0 :         tot_water(:ncol,1) = qini(:ncol,k) +liqini(:ncol,k)+iceini(:ncol,k) !initial total H2O
        1288           0 :         tot_water(:ncol,2) = 0.0_r8
        1289           0 :         do m_cnst=dry_air_species_num+1,thermodynamic_active_species_num
        1290           0 :           m = thermodynamic_active_species_idx(m_cnst)
        1291           0 :           tot_water(:ncol,2) = tot_water(:ncol,2)+state%q(:ncol,k,m)
        1292             :         end do
        1293           0 :         fdq(:ncol) = 1._r8 + tot_water(:ncol,2) - tot_water(:ncol,1)
        1294             :         ! adjust constituents to conserve mass in each layer
        1295           0 :         do m = 1, pcnst
        1296           0 :           state%q(:ncol,k,m) = state%q(:ncol,k,m) / fdq(:ncol)
        1297             :         end do
        1298             :         ! compute new total pressure variables
        1299           0 :         state%pdel  (:ncol,k  ) = state%pdel(:ncol,k  ) * fdq(:ncol)
        1300           0 :         state%ps(:ncol)         = state%ps(:ncol)       + state%pdel(:ncol,k)
        1301           0 :         state%pint  (:ncol,k+1) = state%pint(:ncol,k  ) + state%pdel(:ncol,k)
        1302           0 :         state%lnpint(:ncol,k+1) = log(state%pint(:ncol,k+1))
        1303           0 :         state%rpdel (:ncol,k  ) = 1._r8/ state%pdel(:ncol,k  )
        1304             :         !note that mid-level variables (e.g. pmid) are not recomputed
        1305             :       end do
        1306             :     endif
        1307           0 :     if ( waccmx_is('ionosphere') .or. waccmx_is('neutral') ) then
        1308           0 :       zvirv(:,:) = shr_const_rwv / rairv(:,:,state%lchnk) - 1._r8
        1309             :     else
        1310           0 :       zvirv(:,:) = zvir
        1311             :     endif
        1312             :
        1313           0 :   end subroutine physics_dme_adjust
        1314             :
        1315             : !-----------------------------------------------------------------------
        1316             :
        1317             : !===============================================================================
        1318    16387128 :   subroutine physics_state_copy(state_in, state_out)
        1319             :
        1320             :     use ppgrid,       only: pver, pverp
        1321             :     use constituents, only: pcnst
        1322             :
        1323             :     implicit none
        1324             :
        1325             :     !
        1326             :     ! Arguments
        1327             :     !
        1328             :     type(physics_state), intent(in)    :: state_in
        1329             :     type(physics_state), intent(out)   :: state_out
        1330             :
        1331             :     !
        1332             :     ! Local variables
        1333             :     !
        1334             :     integer i, k, m, ncol
        1335             :
        1336             :     ! Allocate state_out with same subcol dimension as state_in
        1337    16387128 :     call physics_state_alloc ( state_out, state_in%lchnk, state_in%psetcols)
        1338             :
        1339    16387128 :     ncol = state_in%ncol
        1340             :
        1341    16387128 :     state_out%psetcols = state_in%psetcols
        1342    16387128 :     state_out%ngrdcol  = state_in%ngrdcol
        1343    16387128 :     state_out%lchnk    = state_in%lchnk
        1344    16387128 :     state_out%ncol     = state_in%ncol
        1345    16387128 :     state_out%count    = state_in%count
        1346             :
        1347   273626928 :     do i = 1, ncol
        1348   257239800 :        state_out%lat(i)      = state_in%lat(i)
        1349   257239800 :        state_out%lon(i)      = state_in%lon(i)
        1350   257239800 :        state_out%ps(i)       = state_in%ps(i)
        1351   273626928 :        state_out%phis(i)     = state_in%phis(i)
        1352             :      end do
        1353   563640984 :      state_out%te_ini(:ncol,:) = state_in%te_ini(:ncol,:)
        1354   563640984 :      state_out%te_cur(:ncol,:) = state_in%te_cur(:ncol,:)
        1355   273626928 :      state_out%tw_ini(:ncol)   = state_in%tw_ini(:ncol)
        1356   273626928 :      state_out%tw_cur(:ncol)   = state_in%tw_cur(:ncol)
        1357             :
        1358  1540390032 :     do k = 1, pver
        1359 25463691432 :        do i = 1, ncol
        1360 23923301400 :           state_out%temp_ini(i,k)  = state_in%temp_ini(i,k)
        1361 23923301400 :           state_out%z_ini(i,k)     = state_in%z_ini(i,k)
        1362 23923301400 :           state_out%t(i,k)         = state_in%t(i,k)
        1363 23923301400 :           state_out%u(i,k)         = state_in%u(i,k)
        1364 23923301400 :           state_out%v(i,k)         = state_in%v(i,k)
        1365 23923301400 :           state_out%s(i,k)         = state_in%s(i,k)
        1366 23923301400 :           state_out%omega(i,k)     = state_in%omega(i,k)
        1367 23923301400 :           state_out%pmid(i,k)      = state_in%pmid(i,k)
        1368 23923301400 :           state_out%pdel(i,k)      = state_in%pdel(i,k)
        1369 23923301400 :           state_out%rpdel(i,k)     = state_in%rpdel(i,k)
        1370 23923301400 :           state_out%lnpmid(i,k)    = state_in%lnpmid(i,k)
        1371 23923301400 :           state_out%exner(i,k)     = state_in%exner(i,k)
        1372 25447304304 :           state_out%zm(i,k)        = state_in%zm(i,k)
        1373             :        end do
        1374             :     end do
        1375             :
        1376  1556777160 :     do k = 1, pverp
        1377 25737318360 :        do i = 1, ncol
        1378 24180541200 :           state_out%pint(i,k)      = state_in%pint(i,k)
        1379 24180541200 :           state_out%lnpint(i,k)    = state_in%lnpint(i,k)
        1380 25720931232 :           state_out%zi(i,k)        = state_in% zi(i,k)
        1381             :        end do
        1382             :     end do
        1383             :
        1384             :
        1385   273626928 :        do i = 1, ncol
        1386   273626928 :           state_out%psdry(i)  = state_in%psdry(i)
        1387             :        end do
        1388  1540390032 :        do k = 1, pver
        1389 25463691432 :           do i = 1, ncol
        1390 23923301400 :              state_out%lnpmiddry(i,k) = state_in%lnpmiddry(i,k)
        1391 23923301400 :              state_out%pmiddry(i,k)   = state_in%pmiddry(i,k)
        1392 23923301400 :              state_out%pdeldry(i,k)   = state_in%pdeldry(i,k)
        1393 25447304304 :              state_out%rpdeldry(i,k)  = state_in%rpdeldry(i,k)
        1394             :           end do
        1395             :        end do
        1396  1556777160 :        do k = 1, pverp
        1397 25737318360 :           do i = 1, ncol
        1398 24180541200 :              state_out%pintdry(i,k)   = state_in%pintdry(i,k)
        1399 25720931232 :              state_out%lnpintdry(i,k) = state_in%lnpintdry(i,k)
        1400             :           end do
        1401             :        end do
        1402             :
        1403   688259376 :     do m = 1, pcnst
        1404 63172378440 :        do k = 1, pver
        1405 >10440*10^8 :           do i = 1, ncol
        1406 >10433*10^8 :              state_out%q(i,k,m) = state_in%q(i,k,m)
        1407             :           end do
        1408             :        end do
        1409             :     end do
        1410             :
        1411    16387128 :   end subroutine physics_state_copy
        1412             : !===============================================================================
        1413             :
        1414           0 :   subroutine physics_tend_init(tend)
        1415             :
        1416             :     implicit none
        1417             :
        1418             :     !
        1419             :     ! Arguments
        1420             :     !
        1421             :     type(physics_tend), intent(inout) :: tend
        1422             :
        1423             :     !
        1424             :     ! Local variables
        1425             :     !
        1426             :
        1427           0 :     if (.not. allocated(tend%dtdt)) then
        1428           0 :        call endrun('physics_tend_init: tend must be allocated before it can be initialized')
        1429             :     end if
        1430             :
        1431           0 :     tend%dtdt    = 0._r8
        1432           0 :     tend%dudt    = 0._r8
        1433           0 :     tend%dvdt    = 0._r8
        1434           0 :     tend%flx_net = 0._r8
        1435           0 :     tend%te_tnd  = 0._r8
        1436           0 :     tend%tw_tnd  = 0._r8
        1437             :
        1438           0 : end subroutine physics_tend_init
        1439             :
        1440             : !===============================================================================
        1441             :
        1442           0 : subroutine set_state_pdry (state,pdeld_calc)
        1443             :
        1444             :   use ppgrid,  only: pver
        1445             :   implicit none
        1446             :
        1447             :   type(physics_state), intent(inout) :: state
        1448             :   logical, optional, intent(in) :: pdeld_calc    !  .true. do calculate pdeld [default]
        1449             :                                                  !  .false. don't calculate pdeld
        1450             :   integer ncol
        1451             :   integer k
        1452             :   logical do_pdeld_calc
        1453             :
        1454           0 :   if ( present(pdeld_calc) ) then
        1455           0 :      do_pdeld_calc = pdeld_calc
        1456             :   else
        1457             :      do_pdeld_calc = .true.
        1458             :   endif
        1459             :
        1460           0 :   ncol = state%ncol
        1461             :
        1462             :
        1463           0 :   state%psdry(:ncol) = state%pint(:ncol,1)
        1464           0 :   state%pintdry(:ncol,1) = state%pint(:ncol,1)
        1465             :
        1466           0 :   if (do_pdeld_calc)  then
        1467           0 :      do k = 1, pver
        1468           0 :         state%pdeldry(:ncol,k) = state%pdel(:ncol,k)*(1._r8-state%q(:ncol,k,1))
        1469             :      end do
        1470             :   endif
        1471           0 :   do k = 1, pver
        1472           0 :      state%pintdry(:ncol,k+1) = state%pintdry(:ncol,k)+state%pdeldry(:ncol,k)
        1473           0 :      state%pmiddry(:ncol,k) = (state%pintdry(:ncol,k+1)+state%pintdry(:ncol,k))/2._r8
        1474           0 :      state%psdry(:ncol) = state%psdry(:ncol) + state%pdeldry(:ncol,k)
        1475             :   end do
        1476             :
        1477           0 :   state%rpdeldry(:ncol,:) = 1._r8/state%pdeldry(:ncol,:)
        1478           0 :   state%lnpmiddry(:ncol,:) = log(state%pmiddry(:ncol,:))
        1479           0 :   state%lnpintdry(:ncol,:) = log(state%pintdry(:ncol,:))
        1480             :
        1481           0 : end subroutine set_state_pdry
        1482             :
        1483             : !===============================================================================
        1484             :
        1485     5956704 : subroutine set_wet_to_dry(state, convert_cnst_type)
        1486             :
        1487             :   ! Convert mixing ratios from a wet to dry basis for constituents of type
        1488             :   ! convert_cnst_type.  Constituents are given a type when they are added
        1489             :   ! to the constituent array by a call to cnst_add during the register
        1490             :   ! phase of initialization.  There are two constituent types: 'wet' for
        1491             :   ! water species and 'dry' for non-water species.
        1492             :
        1493             :   use constituents,  only: pcnst, cnst_type
        1494             :
        1495             :   type(physics_state), intent(inout) :: state
        1496             :   character(len=3),    intent(in)    :: convert_cnst_type
        1497             :
        1498             :   ! local variables
        1499             :   integer m, ncol
        1500             :   character(len=*), parameter :: sub = 'set_wet_to_dry'
        1501             :   !-----------------------------------------------------------------------------
        1502             :
        1503             :   ! check input
        1504     5956704 :   if (.not.(convert_cnst_type == 'wet' .or. convert_cnst_type == 'dry')) then
        1505           0 :     write(iulog,*) sub//': FATAL: convert_cnst_type not recognized: '//convert_cnst_type
        1506           0 :     call endrun(sub//': FATAL: convert_cnst_type not recognized: '//convert_cnst_type)
        1507             :   end if
        1508             :
        1509     5956704 :   ncol = state%ncol
        1510             :
        1511   250181568 :   do m = 1, pcnst
        1512   250181568 :      if (cnst_type(m) == convert_cnst_type) then
        1513 >14578*10^7 :         state%q(:ncol,:,m) = state%q(:ncol,:,m)*state%pdel(:ncol,:)/state%pdeldry(:ncol,:)
        1514             :      end if
        1515             :   end do
        1516             :
        1517     5956704 : end subroutine set_wet_to_dry
        1518             :
        1519             : !===============================================================================
        1520             :
        1521     2978352 : subroutine set_dry_to_wet(state, convert_cnst_type)
        1522             :
        1523             :   ! Convert mixing ratios from a dry to wet basis for constituents of type
        1524             :   ! convert_cnst_type.  Constituents are given a type when they are added
        1525             :   ! to the constituent array by a call to cnst_add during the register
        1526             :   ! phase of initialization.  There are two constituent types: 'wet' for
        1527             :   ! water species and 'dry' for non-water species.
        1528             :
        1529             :   use constituents,  only: pcnst, cnst_type
        1530             :
        1531             :   type(physics_state), intent(inout) :: state
        1532             :   character(len=3),    intent(in)    :: convert_cnst_type
        1533             :
        1534             :   ! local variables
        1535             :   integer m, ncol
        1536             :   character(len=*), parameter :: sub = 'set_dry_to_wet'
        1537             :   !-----------------------------------------------------------------------------
        1538             :
        1539             :   ! check input
        1540     2978352 :   if (.not.(convert_cnst_type == 'wet' .or. convert_cnst_type == 'dry')) then
        1541           0 :     write(iulog,*) sub//': FATAL: convert_cnst_type not recognized: '//convert_cnst_type
        1542           0 :     call endrun(sub//': FATAL: convert_cnst_type not recognized: '//convert_cnst_type)
        1543             :   end if
        1544             :
        1545     2978352 :   ncol = state%ncol
        1546             :
        1547   125090784 :   do m = 1, pcnst
        1548   125090784 :      if (cnst_type(m) == convert_cnst_type) then
        1549 >13884*10^7 :         state%q(:ncol,:,m) = state%q(:ncol,:,m)*state%pdeldry(:ncol,:)/state%pdel(:ncol,:)
        1550             :      end if
        1551             :   end do
        1552             :
        1553     2978352 : end subroutine set_dry_to_wet
        1554             :
        1555             : !===============================================================================
        1556             :
        1557    16393320 : subroutine physics_state_alloc(state,lchnk,psetcols)
        1558             :
        1559             :   use infnan,     only: inf, assignment(=)
        1560             :
        1561             : ! allocate the individual state components
        1562             :
        1563             :   type(physics_state), intent(inout) :: state
        1564             :   integer,intent(in)                 :: lchnk
        1565             :
        1566             :   integer, intent(in)                :: psetcols
        1567             :
        1568             :   integer :: ierr=0
        1569             :
        1570    16393320 :   state%lchnk    = lchnk
        1571    16393320 :   state%psetcols = psetcols
        1572    16393320 :   state%ngrdcol  = get_ncols_p(lchnk)  ! Number of grid columns
        1573             :
        1574             :   !----------------------------------
        1575             :   ! Following variables will be overwritten by sub-column generator, if sub-columns are being used
        1576             :
        1577             :   !  state%ncol - is initialized in physics_state_set_grid,  if not using sub-columns
        1578             :
        1579             :   !----------------------------------
        1580             :
        1581    49179960 :   allocate(state%lat(psetcols), stat=ierr)
        1582    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lat')
        1583             :
        1584    32786640 :   allocate(state%lon(psetcols), stat=ierr)
        1585    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lon')
        1586             :
        1587    32786640 :   allocate(state%ps(psetcols), stat=ierr)
        1588    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%ps')
        1589             :
        1590    32786640 :   allocate(state%psdry(psetcols), stat=ierr)
        1591    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%psdry')
        1592             :
        1593    32786640 :   allocate(state%phis(psetcols), stat=ierr)
        1594    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%phis')
        1595             :
        1596    32786640 :   allocate(state%ulat(psetcols), stat=ierr)
        1597    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%ulat')
        1598             :
        1599    32786640 :   allocate(state%ulon(psetcols), stat=ierr)
        1600    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%ulon')
        1601             :
        1602    49179960 :   allocate(state%t(psetcols,pver), stat=ierr)
        1603    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%t')
        1604             :
        1605    32786640 :   allocate(state%u(psetcols,pver), stat=ierr)
        1606    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%u')
        1607             :
        1608    32786640 :   allocate(state%v(psetcols,pver), stat=ierr)
        1609    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%v')
        1610             :
        1611    32786640 :   allocate(state%s(psetcols,pver), stat=ierr)
        1612    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%s')
        1613             :
        1614    32786640 :   allocate(state%omega(psetcols,pver), stat=ierr)
        1615    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%omega')
        1616             :
        1617    32786640 :   allocate(state%pmid(psetcols,pver), stat=ierr)
        1618    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%pmid')
        1619             :
        1620    32786640 :   allocate(state%pmiddry(psetcols,pver), stat=ierr)
        1621    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%pmiddry')
        1622             :
        1623    32786640 :   allocate(state%pdel(psetcols,pver), stat=ierr)
        1624    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%pdel')
        1625             :
        1626    32786640 :   allocate(state%pdeldry(psetcols,pver), stat=ierr)
        1627    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%pdeldry')
        1628             :
        1629    32786640 :   allocate(state%rpdel(psetcols,pver), stat=ierr)
        1630    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%rpdel')
        1631             :
        1632    32786640 :   allocate(state%rpdeldry(psetcols,pver), stat=ierr)
        1633    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%rpdeldry')
        1634             :
        1635    32786640 :   allocate(state%lnpmid(psetcols,pver), stat=ierr)
        1636    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lnpmid')
        1637             :
        1638    32786640 :   allocate(state%lnpmiddry(psetcols,pver), stat=ierr)
        1639    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lnpmiddry')
        1640             :
        1641    32786640 :   allocate(state%exner(psetcols,pver), stat=ierr)
        1642    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%exner')
        1643             :
        1644    32786640 :   allocate(state%zm(psetcols,pver), stat=ierr)
        1645    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%zm')
        1646             :
        1647    65573280 :   allocate(state%q(psetcols,pver,pcnst), stat=ierr)
        1648    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%q')
        1649             :
        1650    49179960 :   allocate(state%pint(psetcols,pver+1), stat=ierr)
        1651    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%pint')
        1652             :
        1653    32786640 :   allocate(state%pintdry(psetcols,pver+1), stat=ierr)
        1654    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%pintdry')
        1655             :
        1656    32786640 :   allocate(state%lnpint(psetcols,pver+1), stat=ierr)
        1657    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lnpint')
        1658             :
        1659    32786640 :   allocate(state%lnpintdry(psetcols,pver+1), stat=ierr)
        1660    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lnpintdry')
        1661             :
        1662    32786640 :   allocate(state%zi(psetcols,pver+1), stat=ierr)
        1663    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%zi')
        1664             :
        1665    49179960 :   allocate(state%te_ini(psetcols,2), stat=ierr)
        1666    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%te_ini')
        1667             :
        1668    32786640 :   allocate(state%te_cur(psetcols,2), stat=ierr)
        1669    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%te_cur')
        1670             :
        1671    32786640 :   allocate(state%tw_ini(psetcols), stat=ierr)
        1672    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%tw_ini')
        1673             :
        1674    32786640 :   allocate(state%tw_cur(psetcols), stat=ierr)
        1675    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%tw_cur')
        1676             :
        1677    32786640 :   allocate(state%temp_ini(psetcols,pver), stat=ierr)
        1678    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%temp_ini')
        1679             :
        1680    32786640 :   allocate(state%z_ini(psetcols,pver), stat=ierr)
        1681    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%z_ini')
        1682             :
        1683    49179960 :   allocate(state%latmapback(psetcols), stat=ierr)
        1684    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%latmapback')
        1685             :
        1686    32786640 :   allocate(state%lonmapback(psetcols), stat=ierr)
        1687    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%lonmapback')
        1688             :
        1689    32786640 :   allocate(state%cid(psetcols), stat=ierr)
        1690    16393320 :   if ( ierr /= 0 ) call endrun('physics_state_alloc error: allocation error for state%cid')
        1691             :
        1692    16393320 :   state%lat(:) = inf
        1693    16393320 :   state%lon(:) = inf
        1694    16393320 :   state%ulat(:) = inf
        1695    16393320 :   state%ulon(:) = inf
        1696    16393320 :   state%ps(:) = inf
        1697    16393320 :   state%psdry(:) = inf
        1698    16393320 :   state%phis(:) = inf
        1699    16393320 :   state%t(:,:) = inf
        1700    16393320 :   state%u(:,:) = inf
        1701    16393320 :   state%v(:,:) = inf
        1702    16393320 :   state%s(:,:) = inf
        1703    16393320 :   state%omega(:,:) = inf
        1704    16393320 :   state%pmid(:,:) = inf
        1705    16393320 :   state%pmiddry(:,:) = inf
        1706    16393320 :   state%pdel(:,:) = inf
        1707    16393320 :   state%pdeldry(:,:) = inf
        1708    16393320 :   state%rpdel(:,:) = inf
        1709    16393320 :   state%rpdeldry(:,:) = inf
        1710    16393320 :   state%lnpmid(:,:) = inf
        1711    16393320 :   state%lnpmiddry(:,:) = inf
        1712    16393320 :   state%exner(:,:) = inf
        1713    16393320 :   state%zm(:,:) = inf
        1714    16393320 :   state%q(:,:,:) = inf
        1715             :
        1716    16393320 :   state%pint(:,:) = inf
        1717    16393320 :   state%pintdry(:,:) = inf
        1718    16393320 :   state%lnpint(:,:) = inf
        1719    16393320 :   state%lnpintdry(:,:) = inf
        1720    16393320 :   state%zi(:,:) = inf
        1721             :
        1722    16393320 :   state%te_ini(:,:) = inf
        1723    16393320 :   state%te_cur(:,:) = inf
        1724    16393320 :   state%tw_ini(:) = inf
        1725    16393320 :   state%tw_cur(:) = inf
        1726    16393320 :   state%temp_ini(:,:) = inf
        1727    16393320 :   state%z_ini(:,:)  = inf
        1728             :
        1729    16393320 : end subroutine physics_state_alloc
        1730             :
        1731             : !===============================================================================
        1732             :
        1733     5962896 : subroutine physics_state_dealloc(state)
        1734             :
        1735             : ! deallocate the individual state components
        1736             :
        1737             :   type(physics_state), intent(inout) :: state
        1738             :   integer                            :: ierr = 0
        1739             :
        1740     5962896 :   deallocate(state%lat, stat=ierr)
        1741     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lat')
        1742             :
        1743     5962896 :   deallocate(state%lon, stat=ierr)
        1744     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lon')
        1745             :
        1746     5962896 :   deallocate(state%ps, stat=ierr)
        1747     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%ps')
        1748             :
        1749     5962896 :   deallocate(state%psdry, stat=ierr)
        1750     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%psdry')
        1751             :
        1752     5962896 :   deallocate(state%phis, stat=ierr)
        1753     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%phis')
        1754             :
        1755     5962896 :   deallocate(state%ulat, stat=ierr)
        1756     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%ulat')
        1757             :
        1758     5962896 :   deallocate(state%ulon, stat=ierr)
        1759     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%ulon')
        1760             :
        1761     5962896 :   deallocate(state%t, stat=ierr)
        1762     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%t')
        1763             :
        1764     5962896 :   deallocate(state%u, stat=ierr)
        1765     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%u')
        1766             :
        1767     5962896 :   deallocate(state%v, stat=ierr)
        1768     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%v')
        1769             :
        1770     5962896 :   deallocate(state%s, stat=ierr)
        1771     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%s')
        1772             :
        1773     5962896 :   deallocate(state%omega, stat=ierr)
        1774     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%omega')
        1775             :
        1776     5962896 :   deallocate(state%pmid, stat=ierr)
        1777     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%pmid')
        1778             :
        1779     5962896 :   deallocate(state%pmiddry, stat=ierr)
        1780     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%pmiddry')
        1781             :
        1782     5962896 :   deallocate(state%pdel, stat=ierr)
        1783     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%pdel')
        1784             :
        1785     5962896 :   deallocate(state%pdeldry, stat=ierr)
        1786     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%pdeldry')
        1787             :
        1788     5962896 :   deallocate(state%rpdel, stat=ierr)
        1789     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%rpdel')
        1790             :
        1791     5962896 :   deallocate(state%rpdeldry, stat=ierr)
        1792     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%rpdeldry')
        1793             :
        1794     5962896 :   deallocate(state%lnpmid, stat=ierr)
        1795     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lnpmid')
        1796             :
        1797     5962896 :   deallocate(state%lnpmiddry, stat=ierr)
        1798     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lnpmiddry')
        1799             :
        1800     5962896 :   deallocate(state%exner, stat=ierr)
        1801     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%exner')
        1802             :
        1803     5962896 :   deallocate(state%zm, stat=ierr)
        1804     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%zm')
        1805             :
        1806     5962896 :   deallocate(state%q, stat=ierr)
        1807     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%q')
        1808             :
        1809     5962896 :   deallocate(state%pint, stat=ierr)
        1810     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%pint')
        1811             :
        1812     5962896 :   deallocate(state%pintdry, stat=ierr)
        1813     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%pintdry')
        1814             :
        1815     5962896 :   deallocate(state%lnpint, stat=ierr)
        1816     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lnpint')
        1817             :
        1818     5962896 :   deallocate(state%lnpintdry, stat=ierr)
        1819     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lnpintdry')
        1820             :
        1821     5962896 :   deallocate(state%zi, stat=ierr)
        1822     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%zi')
        1823             :
        1824     5962896 :   deallocate(state%te_ini, stat=ierr)
        1825     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%te_ini')
        1826             :
        1827     5962896 :   deallocate(state%te_cur, stat=ierr)
        1828     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%te_cur')
        1829             :
        1830     5962896 :   deallocate(state%tw_ini, stat=ierr)
        1831     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%tw_ini')
        1832             :
        1833     5962896 :   deallocate(state%tw_cur, stat=ierr)
        1834     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%tw_cur')
        1835             :
        1836     5962896 :   deallocate(state%temp_ini, stat=ierr)
        1837     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%temp_ini')
        1838             :
        1839     5962896 :   deallocate(state%z_ini, stat=ierr)
        1840     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%z_ini')
        1841             :
        1842     5962896 :   deallocate(state%latmapback, stat=ierr)
        1843     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%latmapback')
        1844             :
        1845     5962896 :   deallocate(state%lonmapback, stat=ierr)
        1846     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%lonmapback')
        1847             :
        1848     5962896 :   deallocate(state%cid, stat=ierr)
        1849     5962896 :   if ( ierr /= 0 ) call endrun('physics_state_dealloc error: deallocation error for state%cid')
        1850             :
        1851     5962896 : end subroutine physics_state_dealloc
        1852             :
        1853             : !===============================================================================
        1854             :
        1855        6192 : subroutine physics_tend_alloc(tend,psetcols)
        1856             :
        1857             :   use infnan, only : inf, assignment(=)
        1858             : ! allocate the individual tend components
        1859             :
        1860             :   type(physics_tend), intent(inout)  :: tend
        1861             :
        1862             :   integer, intent(in)                :: psetcols
        1863             :
        1864             :   integer :: ierr = 0
        1865             :
        1866        6192 :   tend%psetcols = psetcols
        1867             :
        1868       18576 :   allocate(tend%dtdt(psetcols,pver), stat=ierr)
        1869        6192 :   if ( ierr /= 0 ) call endrun('physics_tend_alloc error: allocation error for tend%dtdt')
        1870             :
        1871       12384 :   allocate(tend%dudt(psetcols,pver), stat=ierr)
        1872        6192 :   if ( ierr /= 0 ) call endrun('physics_tend_alloc error: allocation error for tend%dudt')
        1873             :
        1874       12384 :   allocate(tend%dvdt(psetcols,pver), stat=ierr)
        1875        6192 :   if ( ierr /= 0 ) call endrun('physics_tend_alloc error: allocation error for tend%dvdt')
        1876             :
        1877       18576 :   allocate(tend%flx_net(psetcols), stat=ierr)
        1878        6192 :   if ( ierr /= 0 ) call endrun('physics_tend_alloc error: allocation error for tend%flx_net')
        1879             :
        1880       12384 :   allocate(tend%te_tnd(psetcols), stat=ierr)
        1881        6192 :   if ( ierr /= 0 ) call endrun('physics_tend_alloc error: allocation error for tend%te_tnd')
        1882             :
        1883       12384 :   allocate(tend%tw_tnd(psetcols), stat=ierr)
        1884        6192 :   if ( ierr /= 0 ) call endrun('physics_tend_alloc error: allocation error for tend%tw_tnd')
        1885             :
        1886        6192 :   tend%dtdt(:,:) = inf
        1887        6192 :   tend%dudt(:,:) = inf
        1888        6192 :   tend%dvdt(:,:) = inf
        1889        6192 :   tend%flx_net(:) = inf
        1890        6192 :   tend%te_tnd(:) = inf
        1891        6192 :   tend%tw_tnd(:) = inf
        1892             :
        1893        6192 : end subroutine physics_tend_alloc
        1894             :
        1895             : !===============================================================================
        1896             :
        1897           0 : subroutine physics_tend_dealloc(tend)
        1898             :
        1899             : ! deallocate the individual tend components
        1900             :
        1901             :   type(physics_tend), intent(inout)  :: tend
        1902             :   integer :: ierr = 0
        1903             :
        1904           0 :   deallocate(tend%dtdt, stat=ierr)
        1905           0 :   if ( ierr /= 0 ) call endrun('physics_tend_dealloc error: deallocation error for tend%dtdt')
        1906             :
        1907           0 :   deallocate(tend%dudt, stat=ierr)
        1908           0 :   if ( ierr /= 0 ) call endrun('physics_tend_dealloc error: deallocation error for tend%dudt')
        1909             :
        1910           0 :   deallocate(tend%dvdt, stat=ierr)
        1911           0 :   if ( ierr /= 0 ) call endrun('physics_tend_dealloc error: deallocation error for tend%dvdt')
        1912             :
        1913           0 :   deallocate(tend%flx_net, stat=ierr)
        1914           0 :   if ( ierr /= 0 ) call endrun('physics_tend_dealloc error: deallocation error for tend%flx_net')
        1915             :
        1916           0 :   deallocate(tend%te_tnd, stat=ierr)
        1917           0 :   if ( ierr /= 0 ) call endrun('physics_tend_dealloc error: deallocation error for tend%te_tnd')
        1918             :
        1919           0 :   deallocate(tend%tw_tnd, stat=ierr)
        1920           0 :   if ( ierr /= 0 ) call endrun('physics_tend_dealloc error: deallocation error for tend%tw_tnd')
        1921           0 : end subroutine physics_tend_dealloc
        1922             :
        1923             : !===============================================================================
        1924             :
        1925    53650584 : subroutine physics_ptend_alloc(ptend,psetcols)
        1926             :
        1927             : ! allocate the individual ptend components
        1928             :
        1929             :   type(physics_ptend), intent(inout) :: ptend
        1930             :
        1931             :   integer, intent(in)                :: psetcols
        1932             :
        1933             :   integer :: ierr = 0
        1934             :
        1935    53650584 :   ptend%psetcols = psetcols
        1936             :
        1937    53650584 :   if (ptend%ls) then
        1938    98387784 :      allocate(ptend%s(psetcols,pver), stat=ierr)
        1939    32795928 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%s')
        1940             :
        1941    98387784 :      allocate(ptend%hflux_srf(psetcols), stat=ierr)
        1942    32795928 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%hflux_srf')
        1943             :
        1944    65591856 :      allocate(ptend%hflux_top(psetcols), stat=ierr)
        1945    32795928 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%hflux_top')
        1946             :   end if
        1947             :
        1948    53650584 :   if (ptend%lu) then
        1949    35758800 :      allocate(ptend%u(psetcols,pver), stat=ierr)
        1950    11919600 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%u')
        1951             :
        1952    35758800 :      allocate(ptend%taux_srf(psetcols), stat=ierr)
        1953    11919600 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%taux_srf')
        1954             :
        1955    23839200 :      allocate(ptend%taux_top(psetcols), stat=ierr)
        1956    11919600 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%taux_top')
        1957             :   end if
        1958             :
        1959    53650584 :   if (ptend%lv) then
        1960    35758800 :      allocate(ptend%v(psetcols,pver), stat=ierr)
        1961    11919600 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%v')
        1962             :
        1963    35758800 :      allocate(ptend%tauy_srf(psetcols), stat=ierr)
        1964    11919600 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%tauy_srf')
        1965             :
        1966    23839200 :      allocate(ptend%tauy_top(psetcols), stat=ierr)
        1967    11919600 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%tauy_top')
        1968             :   end if
        1969             :
        1970   574013880 :   if (any(ptend%lq)) then
        1971   184756896 :      allocate(ptend%q(psetcols,pver,pcnst), stat=ierr)
        1972    46189224 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%q')
        1973             :
        1974   138567672 :      allocate(ptend%cflx_srf(psetcols,pcnst), stat=ierr)
        1975    46189224 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%cflx_srf')
        1976             :
        1977    92378448 :      allocate(ptend%cflx_top(psetcols,pcnst), stat=ierr)
        1978    46189224 :      if ( ierr /= 0 ) call endrun('physics_ptend_alloc error: allocation error for ptend%cflx_top')
        1979             :   end if
        1980             :
        1981    53650584 : end subroutine physics_ptend_alloc
        1982             :
        1983             : !===============================================================================
        1984             :
        1985    64077912 : subroutine physics_ptend_dealloc(ptend)
        1986             :
        1987             : ! deallocate the individual ptend components
        1988             :
        1989             :   type(physics_ptend), intent(inout) :: ptend
        1990             :   integer :: ierr = 0
        1991             :
        1992    64077912 :   ptend%psetcols = 0
        1993             :
        1994    64077912 :   if (allocated(ptend%s)) deallocate(ptend%s, stat=ierr)
        1995    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%s')
        1996             :
        1997    64077912 :   if (allocated(ptend%hflux_srf))   deallocate(ptend%hflux_srf, stat=ierr)
        1998    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%hflux_srf')
        1999             :
        2000    64077912 :   if (allocated(ptend%hflux_top))  deallocate(ptend%hflux_top, stat=ierr)
        2001    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%hflux_top')
        2002             :
        2003    64077912 :   if (allocated(ptend%u))   deallocate(ptend%u, stat=ierr)
        2004    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%u')
        2005             :
        2006    64077912 :   if (allocated(ptend%taux_srf)) deallocate(ptend%taux_srf, stat=ierr)
        2007    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%taux_srf')
        2008             :
        2009    64077912 :   if (allocated(ptend%taux_top))   deallocate(ptend%taux_top, stat=ierr)
        2010    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%taux_top')
        2011             :
        2012    64077912 :   if (allocated(ptend%v)) deallocate(ptend%v, stat=ierr)
        2013    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%v')
        2014             :
        2015    64077912 :   if (allocated(ptend%tauy_srf))   deallocate(ptend%tauy_srf, stat=ierr)
        2016    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%tauy_srf')
        2017             :
        2018    64077912 :   if (allocated(ptend%tauy_top))   deallocate(ptend%tauy_top, stat=ierr)
        2019    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%tauy_top')
        2020             :
        2021    64077912 :   if (allocated(ptend%q))  deallocate(ptend%q, stat=ierr)
        2022    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%q')
        2023             :
        2024    64077912 :   if (allocated(ptend%cflx_srf))   deallocate(ptend%cflx_srf, stat=ierr)
        2025    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%cflx_srf')
        2026             :
        2027    64077912 :   if(allocated(ptend%cflx_top))   deallocate(ptend%cflx_top, stat=ierr)
        2028    64077912 :   if ( ierr /= 0 ) call endrun('physics_ptend_dealloc error: deallocation error for ptend%cflx_top')
        2029             :
        2030    64077912 : end subroutine physics_ptend_dealloc
        2031             :
        2032           0 : end module physics_types

Generated by: LCOV version 1.14