subroutine user_initP2(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_Mesh) :: srcMesh, dstMesh
type(ESMF_DistGrid) :: dstDistGrid
! Initialize return code
rc = ESMF_SUCCESS
print *, "User Coupler Init phase=2 starting"
! Need to re-reconcile import and export states
call ESMF_StateReconcile(importState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_StateReconcile(exportState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
! Access source Field and Mesh
call ESMF_StateGet(importState, "srcField", srcField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_FieldGet(srcField, mesh=srcMesh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
! Access destination Field, Mesh, and DistGrid
call ESMF_StateGet(exportState, "dstField", dstField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_FieldGet(dstField, mesh=dstMesh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_MeshGet(dstMesh, elementDistgrid=dstDistGrid, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
!call ESMF_DistGridPrint(dstDistGrid, rc=rc)
!if (rc/=ESMF_SUCCESS) return ! bail out
! Create a new dstMesh from srcMesh, considering dstDistGrid
! Now that dstDistGrid is reconciled, it can be used without the need
! for passing in the dstVM argument. The resulting dstMesh will function
! correctly in the CplComp context, as well as the Comp2 context.
dstMesh = ESMF_MeshCreate(srcMesh, elementDistgrid=dstDistGrid, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
! Set the new dstMesh in the dstField to make it available to Comp2
call ESMF_FieldEmptySet(dstField, mesh=dstMesh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
print *, "User Coupler Init phase=2 returning"
end subroutine user_initP2