user_init Subroutine

private subroutine user_init(comp, importState, exportState, clock, rc)

Arguments

Type IntentOptional Attributes Name
type(ESMF_CplComp) :: comp
type(ESMF_State) :: importState
type(ESMF_State) :: exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc

Source Code

  subroutine user_init(comp, importState, exportState, clock, rc)
    type(ESMF_CplComp) :: comp
    type(ESMF_State) :: importState, exportState
    type(ESMF_Clock) :: clock
    integer, intent(out) :: rc

    ! Local variables
    integer :: itemcount
    type(ESMF_Field) :: F_lnd, F_ocn, F_atm
    type(ESMF_State) :: lnd_state, ocn_state
    type(ESMF_VM) :: vm
    type(ESMF_Grid)  :: lnd_grid, ocn_grid, atm_grid
    type(ESMF_XGrid) :: xgrid
    type(ESMF_Field) :: flux
    type(ESMF_RouteHandle) :: rh1, rh2, rh3
    real(ESMF_KIND_R8), pointer         :: xfptr(:)
    integer :: localPet
    character(len=64)    :: focn(1)
    integer              :: cnt

    ! Initialize return code
    rc = ESMF_SUCCESS

    !call ESMF_StateGet(importState, itemcount=itemcount, rc=rc)
    !if (rc/=ESMF_SUCCESS) return ! bail out
    !print *, "Import State contains ", itemcount, " items."

    call ESMF_CplCompGet(comp, vm=vm, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_VMGet (vm, localPet=localPet)
    print *, "User Coupler Init starting, localPet =", localPet

    ! Get destination Field out of export state
    call ESMF_StateGet(exportState, "F_atm", F_atm, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    !call ESMF_StatePrint(exportState, nestedFlag=.true., rc=rc)
    !if (rc/=ESMF_SUCCESS) return ! bail out
    !call ESMF_StatePrint(importState, nestedFlag=.true., rc=rc)
    !if (rc/=ESMF_SUCCESS) return ! bail out

    ! Get source Fields out of import state
    call ESMF_StateGet(importState, itemName="ocean_export/F_ocn", field=F_ocn, &
        rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_StateGet(importState, itemName="land_export/F_lnd", field=F_lnd, &
        rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    ! Create an XGrid
    ! - grab grids
    call ESMF_FieldGet(F_lnd, grid=lnd_grid, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_FieldGet(F_ocn, grid=ocn_grid, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_FieldGet(F_atm, grid=atm_grid, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    ! Finally ready to do an flux exchange from A side to B side
    xgrid = ESMF_XGridCreate(sideAGrid=(/ocn_grid, lnd_grid/), sideBGrid=(/atm_grid/), &
        sideAMaskValues=(/2,3,4/), &
        rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    flux = ESMF_FieldCreate(xgrid, typekind=ESMF_TYPEKIND_R8, name='xgrid_flux', rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_FieldGet(flux, farrayPtr=xfptr, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    xfptr = 0.0

    ! Precompute and store an FieldRegrid operation
    call ESMF_FieldRegridStore(xgrid, F_lnd, flux, &
      routehandle=rh1, rc=rc)
    call ESMF_RoutehandleSet(rh1, name='lnd2xgrid', rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    call ESMF_FieldRegridStore(xgrid, F_ocn, flux, &
      routehandle=rh2, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_RoutehandleSet(rh2, name='ocn2xgrid', rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    call ESMF_FieldRegridStore(xgrid, flux, F_atm, &
      routehandle=rh3, rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_RoutehandleSet(rh3, name='xgrid2atm', rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out

    call ESMF_StateAdd(exportState, (/rh1,rh2,rh3/), rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    call ESMF_StateAdd(exportState, (/flux/), rc=rc)
    if (rc/=ESMF_SUCCESS) return ! bail out
    
    print *, "User Coupler Init returning"
   
  end subroutine user_init