ESMF_XGridDestroy Subroutine

public subroutine ESMF_XGridDestroy(xgrid, keywordEnforcer, noGarbage, rc)

Arguments

Type IntentOptional Attributes Name
type(ESMF_XGrid), intent(inout) :: xgrid
type(ESMF_KeywordEnforcer), optional :: keywordEnforcer
logical, intent(in), optional :: noGarbage
integer, intent(out), optional :: rc

Calls

proc~~esmf_xgriddestroy~~CallsGraph proc~esmf_xgriddestroy ESMF_XGridDestroy c_esmc_vmrmfobject c_esmc_vmrmfobject proc~esmf_xgriddestroy->c_esmc_vmrmfobject c_esmc_vmvalidobject c_esmc_vmvalidobject proc~esmf_xgriddestroy->c_esmc_vmvalidobject esmf_arraydestroy esmf_arraydestroy proc~esmf_xgriddestroy->esmf_arraydestroy proc~esmf_basedestroy ESMF_BaseDestroy proc~esmf_xgriddestroy->proc~esmf_basedestroy proc~esmf_basegetstatus ESMF_BaseGetStatus proc~esmf_xgriddestroy->proc~esmf_basegetstatus proc~esmf_basesetstatus ESMF_BaseSetStatus proc~esmf_xgriddestroy->proc~esmf_basesetstatus proc~esmf_distgriddestroy ESMF_DistGridDestroy proc~esmf_xgriddestroy->proc~esmf_distgriddestroy proc~esmf_imerr ESMF_IMErr proc~esmf_xgriddestroy->proc~esmf_imerr proc~esmf_logfounddeallocerror ESMF_LogFoundDeallocError proc~esmf_xgriddestroy->proc~esmf_logfounddeallocerror proc~esmf_logfounderror ESMF_LogFoundError proc~esmf_xgriddestroy->proc~esmf_logfounderror proc~esmf_logseterror ESMF_LogSetError proc~esmf_xgriddestroy->proc~esmf_logseterror proc~esmf_meshdestroy ESMF_MeshDestroy proc~esmf_xgriddestroy->proc~esmf_meshdestroy proc~esmf_xgridgeombasedestroy ESMF_XGridGeomBaseDestroy proc~esmf_xgriddestroy->proc~esmf_xgridgeombasedestroy proc~esmf_xgridgetinit ESMF_XGridGetInit proc~esmf_xgriddestroy->proc~esmf_xgridgetinit proc~esmf_basedestroy->proc~esmf_imerr proc~esmf_basedestroy->proc~esmf_logfounderror c_esmc_basedestroy c_esmc_basedestroy proc~esmf_basedestroy->c_esmc_basedestroy proc~esmf_basegetinit ESMF_BaseGetInit proc~esmf_basedestroy->proc~esmf_basegetinit proc~esmf_basegetstatus->proc~esmf_logfounderror c_esmc_basegetstatus c_esmc_basegetstatus proc~esmf_basegetstatus->c_esmc_basegetstatus proc~esmf_basesetstatus->proc~esmf_logfounderror c_esmc_basesetstatus c_esmc_basesetstatus proc~esmf_basesetstatus->c_esmc_basesetstatus proc~esmf_distgriddestroy->proc~esmf_imerr proc~esmf_distgriddestroy->proc~esmf_logfounderror c_esmc_distgriddestroy c_esmc_distgriddestroy proc~esmf_distgriddestroy->c_esmc_distgriddestroy proc~esmf_distgridgetinit ESMF_DistGridGetInit proc~esmf_distgriddestroy->proc~esmf_distgridgetinit proc~esmf_imerr->proc~esmf_logfounderror proc~esmf_initcheckdeep ESMF_InitCheckDeep proc~esmf_imerr->proc~esmf_initcheckdeep esmf_breakpoint esmf_breakpoint proc~esmf_logfounddeallocerror->esmf_breakpoint proc~esmf_logrc2msg ESMF_LogRc2Msg proc~esmf_logfounddeallocerror->proc~esmf_logrc2msg proc~esmf_logwrite ESMF_LogWrite proc~esmf_logfounddeallocerror->proc~esmf_logwrite proc~esmf_logfounderror->esmf_breakpoint proc~esmf_logfounderror->proc~esmf_logrc2msg proc~esmf_logfounderror->proc~esmf_logwrite proc~esmf_logseterror->esmf_breakpoint proc~esmf_logseterror->proc~esmf_logrc2msg proc~esmf_logseterror->proc~esmf_logwrite proc~esmf_meshdestroy->proc~esmf_imerr proc~esmf_meshdestroy->proc~esmf_logfounderror proc~esmf_meshdestroy->c_esmc_basesetstatus c_esmc_meshdestroy c_esmc_meshdestroy proc~esmf_meshdestroy->c_esmc_meshdestroy c_esmc_meshgetisfree c_esmc_meshgetisfree proc~esmf_meshdestroy->c_esmc_meshgetisfree proc~esmf_meshgetinit ESMF_MeshGetInit proc~esmf_meshdestroy->proc~esmf_meshgetinit proc~esmf_xgridgeombasedestroy->proc~esmf_imerr proc~esmf_xgridgeombasegetinit ESMF_XGridGeomBaseGetInit proc~esmf_xgridgeombasedestroy->proc~esmf_xgridgeombasegetinit

Called by

proc~~esmf_xgriddestroy~~CalledByGraph proc~esmf_xgriddestroy ESMF_XGridDestroy proc~esmf_fielddestructgeom ESMF_FieldDestructGeom proc~esmf_fielddestructgeom->proc~esmf_xgriddestroy proc~f_esmf_xgriddestroy f_esmf_xgriddestroy proc~f_esmf_xgriddestroy->proc~esmf_xgriddestroy proc~flux_exchange flux_exchange proc~flux_exchange->proc~esmf_xgriddestroy proc~flux_exchange_sph flux_exchange_sph proc~flux_exchange_sph->proc~esmf_xgriddestroy proc~flux_exchange_sph_mesh flux_exchange_sph_mesh proc~flux_exchange_sph_mesh->proc~esmf_xgriddestroy proc~flux_exchange_sph~2 flux_exchange_sph proc~flux_exchange_sph~2->proc~esmf_xgriddestroy proc~flux_exchange~2 flux_exchange proc~flux_exchange~2->proc~esmf_xgriddestroy proc~test3 test3 proc~test3->proc~esmf_xgriddestroy proc~test4 test4 proc~test4->proc~esmf_xgriddestroy proc~test5 test5 proc~test5->proc~esmf_xgriddestroy proc~test6 test6 proc~test6->proc~esmf_xgriddestroy proc~test8 test8 proc~test8->proc~esmf_xgriddestroy proc~test_cart_bilinear_xgrid test_cart_bilinear_xgrid proc~test_cart_bilinear_xgrid->proc~esmf_xgriddestroy proc~test_regrid2xg test_regrid2xg proc~test_regrid2xg->proc~esmf_xgriddestroy proc~test_regrid2xg_clip test_regrid2xg_clip proc~test_regrid2xg_clip->proc~esmf_xgriddestroy proc~test_regrid2xg_contain test_regrid2xg_contain proc~test_regrid2xg_contain->proc~esmf_xgriddestroy proc~test_regrid2xg_half test_regrid2xg_half proc~test_regrid2xg_half->proc~esmf_xgriddestroy proc~test_regrid2xg_online test_regrid2xg_online proc~test_regrid2xg_online->proc~esmf_xgriddestroy proc~test_regrid2xgsph test_regrid2xgSph proc~test_regrid2xgsph->proc~esmf_xgriddestroy proc~test_regrid2xgsph~2 test_regrid2xgSph proc~test_regrid2xgsph~2->proc~esmf_xgriddestroy proc~test_regridxg test_regridxg proc~test_regridxg->proc~esmf_xgriddestroy proc~test_regridxg_const test_regridxg_const proc~test_regridxg_const->proc~esmf_xgriddestroy proc~test_side_and_elem_info test_side_and_elem_info proc~test_side_and_elem_info->proc~esmf_xgriddestroy proc~test_sph_bilinear_xgrid test_sph_bilinear_xgrid proc~test_sph_bilinear_xgrid->proc~esmf_xgriddestroy proc~user_final~7 user_final proc~user_final~7->proc~esmf_xgriddestroy proc~user_final~9 user_final proc~user_final~9->proc~esmf_xgriddestroy program~esmf_xgridex ESMF_XGridEx program~esmf_xgridex->proc~esmf_xgriddestroy program~esmf_xgridsparsematex ESMF_XGridSparseMatEx program~esmf_xgridsparsematex->proc~esmf_xgriddestroy program~esmf_xgridutest ESMF_XGridUTest program~esmf_xgridutest->proc~esmf_xgriddestroy

Source Code

  subroutine ESMF_XGridDestroy(xgrid, keywordEnforcer, noGarbage, rc)
!
! !ARGUMENTS:
    type(ESMF_XGrid), intent(inout)          :: xgrid       
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
    logical,          intent(in),   optional :: noGarbage
    integer,          intent(out),  optional :: rc     
!
! !STATUS:
! \begin{itemize}
! \item\apiStatusCompatibleVersion{5.2.0r}
! \item\apiStatusModifiedSinceVersion{5.2.0r}
! \begin{description}
! \item[8.1.0] Added argument {\tt noGarbage}.
!   The argument provides a mechanism to override the default garbage collection
!   mechanism when destroying an ESMF object.
! \end{description}
! \end{itemize}
!
! !DESCRIPTION:
! Destroys an {\tt ESMF\_XGrid}, releasing the resources associated
! with the object.
! 
! The arguments are:
! \begin{description}
! \item [xgrid]
!       {\tt ESMF\_XGrid} object.
! \item[{[noGarbage]}]
!      If set to {\tt .TRUE.} the object will be fully destroyed and removed
!      from the ESMF garbage collection system. Note however that under this
!      condition ESMF cannot protect against accessing the destroyed object
!      through dangling aliases -- a situation which may lead to hard to debug
!      application crashes.
!
!      It is generally recommended to leave the {\tt noGarbage} argument
!      set to {\tt .FALSE.} (the default), and to take advantage of the ESMF
!      garbage collection system which will prevent problems with dangling
!      aliases or incorrect sequences of destroy calls. However this level of
!      support requires that a small remnant of the object is kept in memory
!      past the destroy call. This can lead to an unexpected increase in memory
!      consumption over the course of execution in applications that use
!      temporary ESMF objects. For situations where the repeated creation and
!      destruction of temporary objects leads to memory issues, it is
!      recommended to call with {\tt noGarbage} set to {\tt .TRUE.}, fully
!      removing the entire temporary object from memory.
! \item [{[rc]}] 
!       Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
!------------------------------------------------------------------------------
    ! Local variables
    integer :: localrc, i
    type(ESMF_Status)   :: xgridstatus
    type(ESMF_Logical)  :: valid

    ! Initialize
    localrc = ESMF_RC_NOT_IMPL
    if (present(rc)) rc = ESMF_RC_NOT_IMPL

    ! check input variables
    ESMF_INIT_CHECK_DEEP(ESMF_XGridGetInit,xgrid,rc)

    if (.not. associated(xgrid%xgtypep)) then 
      call ESMF_LogSetError(rcToCheck=ESMF_RC_OBJ_BAD, &
        msg="Uninitialized or already destroyed XGrid: xgtypep unassociated", &
        ESMF_CONTEXT, rcToReturn=rc)
      return
    endif 

    ! See if this object is even still valid in garbage collection
    call c_ESMC_VMValidObject(xgrid%xgtypep%base, valid, localrc)
    if (ESMF_LogFoundError(localrc, &
                              ESMF_ERR_PASSTHRU, &
                              ESMF_CONTEXT, rcToReturn=rc)) return
    if (valid/=ESMF_TRUE) then
      ! nothing to be done here, return successfully
      if (present(rc)) rc = ESMF_SUCCESS
      return
    endif

    if(xgrid%xgtypep%storeOverlay) then
      call ESMF_MeshDestroy(xgrid%xgtypep%mesh, rc=localrc)
      if (ESMF_LogFoundError(localrc, &
          ESMF_ERR_PASSTHRU, &
          ESMF_CONTEXT, rcToReturn=rc)) return
    else
      call ESMF_DistGridDestroy(xgrid%xgtypep%distgridM, rc=localrc)
      if (ESMF_LogFoundError(localrc, &
          ESMF_ERR_PASSTHRU, &
          ESMF_CONTEXT, rcToReturn=rc)) return
    endif

    ! Destruct all xgrid internals and then free xgrid memory.
    call ESMF_BaseGetStatus(xgrid%xgtypep%base, xgridstatus, rc=localrc)
    if (ESMF_LogFoundError(localrc, &
        ESMF_ERR_PASSTHRU, &
        ESMF_CONTEXT, rcToReturn=rc)) return
        
    if (xgridstatus .eq. ESMF_STATUS_READY) then

      if((xgrid%xgtypep%is_proxy)) then

        if(associated(xgrid%xgtypep%sideA)) then
          do i = 1, size(xgrid%xgtypep%sideA,1)
            call ESMF_XGridGeomBaseDestroy(xgrid%xgtypep%sideA(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        if(associated(xgrid%xgtypep%sideB)) then
          do i = 1, size(xgrid%xgtypep%sideB,1)
            call ESMF_XGridGeomBaseDestroy(xgrid%xgtypep%sideB(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif

        if(associated(xgrid%xgtypep%distgridA)) then
          do i = 1, size(xgrid%xgtypep%distgridA,1)
            call ESMF_DistGridDestroy(xgrid%xgtypep%distgridA(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        if(associated(xgrid%xgtypep%distgridB)) then
          do i = 1, size(xgrid%xgtypep%distgridB,1)
            call ESMF_DistGridDestroy(xgrid%xgtypep%distgridB(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif

      endif ! proxy

      if(associated(xgrid%xgtypep%centroid)) then
          deallocate(xgrid%xgtypep%centroid)
      endif
      if(associated(xgrid%xgtypep%area)) then
          deallocate(xgrid%xgtypep%area)
      endif

      if(associated(xgrid%xgtypep%sparseMatA2X)) then
          do i = 1, size(xgrid%xgtypep%sparseMatA2X)
            if(associated(xgrid%xgtypep%sparseMatA2X(i)%factorIndexList)) &
              deallocate(xgrid%xgtypep%sparseMatA2X(i)%factorIndexList)
            if(associated(xgrid%xgtypep%sparseMatA2X(i)%factorList)) &
              deallocate(xgrid%xgtypep%sparseMatA2X(i)%factorList)
          enddo
          deallocate(xgrid%xgtypep%sparseMatA2X)
      endif
      if(associated(xgrid%xgtypep%sparseMatX2A)) then
          do i = 1, size(xgrid%xgtypep%sparseMatX2A)
            if(associated(xgrid%xgtypep%sparseMatX2A(i)%factorIndexList)) &
              deallocate(xgrid%xgtypep%sparseMatX2A(i)%factorIndexList)
            if(associated(xgrid%xgtypep%sparseMatX2A(i)%factorList)) &
              deallocate(xgrid%xgtypep%sparseMatX2A(i)%factorList)
          enddo
          deallocate(xgrid%xgtypep%sparseMatX2A)
      endif
      if(associated(xgrid%xgtypep%sparseMatB2X)) then
          do i = 1, size(xgrid%xgtypep%sparseMatB2X)
            if(associated(xgrid%xgtypep%sparseMatB2X(i)%factorIndexList)) &
              deallocate(xgrid%xgtypep%sparseMatB2X(i)%factorIndexList)
            if(associated(xgrid%xgtypep%sparseMatB2X(i)%factorList)) &
              deallocate(xgrid%xgtypep%sparseMatB2X(i)%factorList)
          enddo
          deallocate(xgrid%xgtypep%sparseMatB2X)
      endif
      if(associated(xgrid%xgtypep%sparseMatX2B)) then
          do i = 1, size(xgrid%xgtypep%sparseMatX2B)
            if(associated(xgrid%xgtypep%sparseMatX2B(i)%factorIndexList)) &
              deallocate(xgrid%xgtypep%sparseMatX2B(i)%factorIndexList)
            if(associated(xgrid%xgtypep%sparseMatX2B(i)%factorList)) &
              deallocate(xgrid%xgtypep%sparseMatX2B(i)%factorList)
          enddo
          deallocate(xgrid%xgtypep%sparseMatX2B)
      endif

      ! destroy all the fraction arrays for Xgrid created online
      if(xgrid%xgtypep%online == 1) then
        if(associated(xgrid%xgtypep%fracA2X)) then
          do i = 1, size(xgrid%xgtypep%fracA2X,1)
            call ESMF_ArrayDestroy(xgrid%xgtypep%fracA2X(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        if(associated(xgrid%xgtypep%fracB2X)) then
          do i = 1, size(xgrid%xgtypep%fracB2X,1)
            call ESMF_ArrayDestroy(xgrid%xgtypep%fracB2X(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        if(associated(xgrid%xgtypep%fracX2A)) then
          do i = 1, size(xgrid%xgtypep%fracX2A,1)
            call ESMF_ArrayDestroy(xgrid%xgtypep%fracX2A(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        if(associated(xgrid%xgtypep%fracX2B)) then
          do i = 1, size(xgrid%xgtypep%fracX2B,1)
            call ESMF_ArrayDestroy(xgrid%xgtypep%fracX2B(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        call ESMF_ArrayDestroy(xgrid%xgtypep%fracX, rc=localrc)
        if (ESMF_LogFoundError(localrc, &
            ESMF_ERR_PASSTHRU, &
            ESMF_CONTEXT, rcToReturn=rc)) return
        if(associated(xgrid%xgtypep%frac2A)) then
          do i = 1, size(xgrid%xgtypep%frac2A,1)
            call ESMF_ArrayDestroy(xgrid%xgtypep%frac2A(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
        if(associated(xgrid%xgtypep%frac2B)) then
          do i = 1, size(xgrid%xgtypep%frac2B,1)
            call ESMF_ArrayDestroy(xgrid%xgtypep%frac2B(i), rc=localrc)
            if (ESMF_LogFoundError(localrc, &
                ESMF_ERR_PASSTHRU, &
                ESMF_CONTEXT, rcToReturn=rc)) return
          enddo
        endif
      endif ! online

    endif ! valid status

    ! mark object invalid
    call ESMF_BaseSetStatus(xgrid%xgtypep%base, ESMF_STATUS_INVALID, rc=localrc)
    if (ESMF_LogFoundError(localrc, &
        ESMF_ERR_PASSTHRU, &
        ESMF_CONTEXT, rcToReturn=rc)) return

    if (present(noGarbage)) then
      if (noGarbage) then
        ! destroy Base object (which also removes it from garbage collection)
        call ESMF_BaseDestroy(xgrid%xgtypep%base, noGarbage, rc=localrc)
        if (ESMF_LogFoundError(localrc, &
          ESMF_ERR_PASSTHRU, &
          ESMF_CONTEXT, rcToReturn=rc)) return
        ! remove reference to this object from ESMF garbage collection table
        call c_ESMC_VMRmFObject(xgrid)
        ! deallocate the actual field data structure
        deallocate(xgrid%xgtypep, stat=localrc)
        if (ESMF_LogFoundDeallocError(localrc, &
          msg="Deallocating XGrid information", &
          ESMF_CONTEXT, rcToReturn=rc)) return
      endif
    endif

    ! Mark this XGrid as invalid
    nullify(xgrid%xgtypep)

    ! Set init status to indicate structure has been destroyed
    ESMF_INIT_SET_DELETED(xgrid)

    ! return successfully
    if (present(rc)) rc = ESMF_SUCCESS

  end subroutine ESMF_XGridDestroy