user_initP1 Subroutine

public subroutine user_initP1(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_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_Mesh)     :: srcMesh, dstMesh
    type(ESMF_DistGrid) :: srcElementDistGrid, dstElementDistGrid
    type(ESMF_DistGrid) :: srcNodeDistGrid, dstNodeDistGrid
    type(ESMF_VM)       :: dstVM

    ! Initialize return code
    rc = ESMF_SUCCESS

    print *, "User Coupler Init phase=1 starting"

    ! Need to 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

    ! Get source Field out of import state
    call ESMF_StateGet(importState, "srcField", srcField, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) return ! bail out

    ! Get the source side Mesh and DistGrid
    call ESMF_FieldGet(srcField, mesh=srcMesh, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) return ! bail out
    call ESMF_MeshGet(srcMesh, elementDistgrid=srcElementDistGrid, &
      nodalDistgrid=srcNodeDistGrid, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) return ! bail out
    
    ! Get destination Field out of export state
    call ESMF_StateGet(exportState, "dstField", dstField, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) return ! bail out
    
    ! Get the destination side VM
    call ESMF_FieldGet(dstField, vm=dstVM, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) 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).
    dstElementDistGrid = ESMF_DistGridCreate(srcElementDistGrid, vm=dstVM, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) return ! bail out
    dstNodeDistGrid = ESMF_DistGridCreate(srcNodeDistGrid, vm=dstVM, rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, &
      file=__FILE__)) return ! bail out

    ! Create the dstMesh that allows dstElementDistGrid and dstNodeDistGrid to
    ! be taken to Comp2. The method that is chosen to create dstMesh really 
    ! only creates a formal Mesh for the purpose of being a container for
    ! the two DistGrid objects, while also identifying itself as a geom object
    ! of type Mesh. It is not a fully usable Mehs!
    dstMesh = ESMF_MeshCreate(dstElementDistGrid, &
      nodalDistgrid=dstNodeDistGrid, 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 in order to hand it 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=1 returning"
   
  end subroutine user_initP1