subroutine compute_flux2D(vm, flux_density, area, fraction, fraction2, allsum, dstflux, rc)
type(ESMF_VM), intent(in) :: vm
real(ESMF_KIND_R8), pointer :: flux_density(:,:)
real(ESMF_KIND_R8), pointer :: area(:,:)
real(ESMF_KIND_R8), pointer :: fraction(:,:)
real(ESMF_KIND_R8), pointer :: fraction2(:,:)
real(ESMF_KIND_R8), intent(out) :: allsum(3)
logical, intent(in), optional :: dstflux
integer, intent(out), optional :: rc
real(ESMF_KIND_R8) :: sum(3)
integer :: i,j, localrc, npet, lpet
logical :: l_dstflux
if(present(rc)) rc = ESMF_SUCCESS
l_dstflux = .false.
if(present(dstflux)) l_dstflux = dstflux
call ESMF_VMGet(vm, petCount=npet, localPet=lpet, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
!if(lpet == 0) write(*, '(A, 4I5)') 'compute_flux2D bounds: ', &
! lbound(flux_density, 1), ubound(flux_density, 1), &
! lbound(flux_density, 2), ubound(flux_density, 2)
sum = 0.
do i = lbound(flux_density, 1), ubound(flux_density, 1)
do j = lbound(flux_density, 2), ubound(flux_density, 2)
if(l_dstflux) then
sum(1) = sum(1) + flux_density(i,j)*area(i,j)*fraction2(i,j)
else
sum(1) = sum(1) + flux_density(i,j)*area(i,j)*fraction(i,j)*fraction2(i,j)
endif
sum(2) = sum(2) + area(i,j)*fraction(i,j)
sum(3) = sum(3) + area(i,j)
enddo
enddo
call ESMF_VMAllReduce(vm, sum, allsum, 3, ESMF_REDUCE_SUM, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
end subroutine compute_flux2D