subroutine user_initP1(comp, importState, exportState, clock, rc)
type(ESMF_CplComp) :: comp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! Local variables
type(ESMF_Field) :: srcField, dstField
type(ESMF_Grid) :: srcGrid, dstGrid
type(ESMF_DistGrid) :: srcDistGrid, dstDistGrid
type(ESMF_VM) :: dstVM
! Initialize return code
rc = ESMF_SUCCESS
print *, "User Coupler Init phase=1 starting"
call ESMF_LogWrite (msg='User Coupler Init phase=1 starting', &
logmsgFlag=ESMF_LOGMSG_TRACE)
! Need to reconcile import and export states
call ESMF_StateReconcile(importState, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateReconcile(exportState, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get source Field out of import state
call ESMF_StateGet(importState, "srcField", srcField, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get the source side Grid and DistGrid
call ESMF_FieldGet(srcField, grid=srcGrid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_GridGet(srcGrid, distgrid=srcDistGrid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get destination Field out of export state
call ESMF_StateGet(exportState, "dstField", dstField, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get the destination side VM
call ESMF_FieldGet(dstField, vm=dstVM, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Create the dstDistGrid from the srcDistGrid, but on dstVM
! The dstVM is important in order to specify which is the correct context
! of the dstDistGrid. The created dstDistGrid is only fully functional on
! the PETs that are part of dstVM, other PETs must first generate proxies
! via a StateReconcile. Before reconciliation care must be given to which
! calls the dstDistGrid is passed (generally they must also take the dstVM).
dstDistGrid = ESMF_DistGridCreate(srcDistGrid, vm=dstVM, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Create the dstGrid on the dstDistGrid limited to PETs in dstVM
! The dstGrid is used as a vehicle to hand the dstDistGrid to the Comp2.
! It must be created on the dstVM because only there the dstDistGrid
! currently exists.
dstGrid = ESMF_GridCreate(dstDistGrid, vm=dstVM, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Set the new dstGrid in the dstField
! The dstGrid with dstDistGrid is handed to Comp2 inside the dstField.
call ESMF_FieldEmptySet(dstField, grid=dstGrid, vm=dstVM, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_LogWrite (msg='User Coupler Init phase=1 complete', &
logmsgFlag=ESMF_LOGMSG_TRACE)
print *, "User Coupler Init phase=1 returning"
end subroutine user_initP1