! $Id: user_model1.F90,v 1.15 2011/06/30 06:00:31 theurich Exp $ ! ! Example/test code which shows User Component calls. !------------------------------------------------------------------------- !------------------------------------------------------------------------- ! ! !DESCRIPTION: ! User-supplied Component ! ! !\begin{verbatim} module user_model1 ! ESMF Framework module use ESMF implicit none public userm1_setvm, userm1_register ! global data integer, save :: srcF90(100,150) contains !------------------------------------------------------------------------- ! ! The Register routine sets the subroutines to be called ! ! as the init, run, and finalize routines. Note that these are ! ! private to the module. subroutine userm1_setvm(comp, rc) type(ESMF_GridComp) :: comp integer, intent(out) :: rc #ifdef ESMF_TESTWITHTHREADS type(ESMF_VM) :: vm logical :: pthreadsEnabled #endif ! Initialize return code rc = ESMF_SUCCESS #ifdef ESMF_TESTWITHTHREADS ! The following call will turn on ESMF-threading (single threaded) ! for this component. If you are using this file as a template for ! your own code development you probably don't want to include the ! following call unless you are interested in exploring ESMF's ! threading features. ! First test whether ESMF-threading is supported on this machine call ESMF_VMGetGlobal(vm, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_VMGet(vm, pthreadsEnabledFlag=pthreadsEnabled, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out if (pthreadsEnabled) then call ESMF_GridCompSetVMMinThreads(comp, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out endif #endif end subroutine subroutine userm1_register(comp, rc) type(ESMF_GridComp) :: comp integer, intent(out) :: rc ! Initialize return code rc = ESMF_SUCCESS print *, "User Comp1 Register starting" ! Register the callback routines. call ESMF_GridCompSetEntryPoint(comp, ESMF_METHOD_INITIALIZE, userRoutine=user_init, & rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_GridCompSetEntryPoint(comp, ESMF_METHOD_RUN, userRoutine=user_run, & rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_GridCompSetEntryPoint(comp, ESMF_METHOD_FINALIZE, userRoutine=user_final, & rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out print *, "Registered Initialize, Run, and Finalize routines" print *, "User Comp1 Register returning" end subroutine !------------------------------------------------------------------------- ! ! User Comp Component created by higher level calls, here is the ! ! Initialization routine. subroutine user_init(comp, importState, exportState, clock, rc) type(ESMF_GridComp) :: comp type(ESMF_State) :: importState, exportState type(ESMF_Clock) :: clock integer, intent(out) :: rc ! Local variables type(ESMF_ArraySpec) :: arrayspec type(ESMF_DistGrid) :: distgrid type(ESMF_Array) :: srcArray1, dstArray1 type(ESMF_VM) :: vm integer :: petCount, i, j ! Initialize return code rc = ESMF_SUCCESS print *, "User Comp1 Init starting" ! Determine petCount call ESMF_GridCompGet(comp, vm=vm, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_VMGet(vm, petCount=petCount, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_ArraySpecSet(arrayspec, typekind=ESMF_TYPEKIND_I4, rank=2, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out ! Create the srcArray1 and add it to the exportState distgrid = ESMF_DistGridCreate(minIndex=(/1,1/), maxIndex=(/100,150/), & regDecomp=(/petCount,1/), rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out srcArray1 = ESMF_ArrayCreate(arrayspec=arrayspec, distgrid=distgrid, & indexflag=ESMF_INDEX_GLOBAL, name="srcArray1", rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_StateAdd(exportState, (/srcArray1/), rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out ! Create the dstArray1 and add it to the importState distgrid = ESMF_DistGridCreate(minIndex=(/1,1/), maxIndex=(/100,150/), & regDecomp=(/1,petCount/), rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out dstArray1 = ESMF_ArrayCreate(arrayspec=arrayspec, distgrid=distgrid, & indexflag=ESMF_INDEX_GLOBAL, name="dstArray1", rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_StateAdd(importState, (/dstArray1/), rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out ! Fill the srcF90 array and ArrayScatter() it across srcArray1 do j=1,150 do i=1,100 srcF90(i,j) = j*1000 + i enddo enddo call ESMF_ArrayScatter(srcArray1, srcF90, rootPet=0, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out print *, "User Comp1 Init returning" end subroutine user_init !------------------------------------------------------------------------- ! ! The Run routine where data is computed. ! ! subroutine user_run(comp, importState, exportState, clock, rc) type(ESMF_GridComp) :: comp type(ESMF_State) :: importState, exportState type(ESMF_Clock) :: clock integer, intent(out) :: rc ! Local variables type(ESMF_Array) :: dstArray1 type(ESMF_VM) :: vm integer :: localPet, i, j integer :: dstF90(100,150) ! Initialize return code rc = ESMF_SUCCESS print *, "User Comp1 Run starting" ! Determine localPet call ESMF_GridCompGet(comp, vm=vm, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_VMGet(vm, localPet=localPet, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out ! Get the Arrays from the states call ESMF_StateGet(importState, "dstArray1", dstArray1, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out ! Initialize dstF90 dstF90 = 0 ! ArrayGather() dstArray1 data into dstF90 on rootPet=0 call ESMF_ArrayGather(dstArray1, dstF90, rootPet=0, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out ! Compare dstF90 against srcF90 on rootPet=0 if (localPet == 0) then do j=1,150 do i=1,100 if (dstF90(i,j) /= srcF90(i,j) + 123456) then print *, "dstF90(i,j) does not compare to srcF90(i,j)", i, j rc = ESMF_FAILURE return ! bail out indicating problem endif enddo enddo endif print *, "User Comp1 Run returning" end subroutine user_run !------------------------------------------------------------------------- ! ! The Finalization routine where things are deleted and cleaned up. ! ! subroutine user_final(comp, importState, exportState, clock, rc) type(ESMF_GridComp) :: comp type(ESMF_State) :: importState, exportState type(ESMF_Clock) :: clock integer, intent(out) :: rc ! Local variables type(ESMF_DistGrid) :: distgrid type(ESMF_Array) :: array ! Initialize return code rc = ESMF_SUCCESS print *, "User Comp1 Final starting" call ESMF_StateGet(exportState, "srcArray1", array, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_ArrayGet(array, distgrid=distgrid, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_ArrayDestroy(array, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_DistGridDestroy(distgrid, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_StateGet(importState, "dstArray1", array, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_ArrayGet(array, distgrid=distgrid, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_ArrayDestroy(array, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out call ESMF_DistGridDestroy(distgrid, rc=rc) if (rc/=ESMF_SUCCESS) return ! bail out print *, "User Comp1 Final returning" end subroutine user_final end module user_model1 !\end{verbatim}