module mod1
use shr_kind_mod, only: r8=>shr_kind_r8
use ppgrid, only: pcols, pver, begchunk, endchunk
use phys_buffer, only: pbuf_add, pbuf_fld
use physics_types, only: physics_state, physics_ptend
use time_manager, only: is_first_step
implicit none
private
save
public :: mod1_register, mod1_init, mod1_tend
integer :: fld1_idx ! save the index into the pbuf
contains
subroutine mod1_register
! Add 3D pbuf field with global scope. This field will
! persist across timesteps, and automatically be written
! to the restart file.
call pbuf_add('FLD1', 'global', 1, pver, 1, fld1_idx)
end subroutine mod1_register
subroutine mod1_init(pbuf)
! Example of initializing a field in the physics buffer. Note that this
! is not necessary if the 'run' subroutine which computes the field values
! is called before any other subroutines which need to access the values.
!
! Note that init routines are called outside the loop over chunks (the
! data structure used by the physics grid decompostion), and so all the
! chunks, begchunk:endchunk, need to be dealt with.
type(pbuf_fld) :: pbuf(:)
real(r8), pointer :: fld1(:,:,:) ! all the chunks
! associate the pointer with the physics buffer field
fld1 => pbuf(fld1_idx)%fld_ptr(1,1:pcols,1:pver,begchunk:endchunk,1)
! Initialize field values
! Note -- init subroutines are typically called on the first step of a
! restart as well as during the first step of an initial run.
! For 'global' fields, to avoid having the values that are
! restored from the restart file from being overwritten, an
! initialization done in an init method needs to happen inside
! an is_first_step conditional.
if (is_first_step) then
fld1 = 0._r8
end if
......
end subroutine mod1_init
subroutine mod1_tend(state, ptend, pbuf)
! Example of setting a field in the physics buffer.
!
! Note that 'tend' routines are called inside the loop over chunks (the
! data structure used by the physics grid decompostion), and so only
! the chunk corresponding to the state variables, state%lchnk, is set.
type(physics_state), intent(in) :: state ! State variables
type(physics_ptend), intent(out) :: ptend ! physics tendencies
type(pbuf_fld) :: pbuf(:)
real(r8), pointer :: fld1(:,:) ! just one chunk
integer :: i, k, lchnk, ncol
lchnk = state%lchnk
ncol = state%ncol ! number of active columns in this chunk
! associate the pointer with the physics buffer field
fld1 => pbuf(fld1_idx)%fld_ptr(1,1:pcols,1:pver,lchnk,1)
! set the field
do k = 1, plev
do i = 1, ncol
fld1(i,k) = ...
end do
end do
......
end subroutine mod1_tend
end module mod1