Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | nDomains | |||
integer, | intent(in) | :: | offsetElem(0:nDomains) | |||
integer, | intent(in) | :: | elemID | |||
integer, | intent(out) | :: | domain |
subroutine ElemToRank(nDomains,offsetElem,elemID,domain)
! From https://www.hopr-project.org/externals/Meshformat.pdf, Algorithm 7
! "Find domain containing element index"
!
implicit none
integer,intent(in) :: nDomains
integer,intent(in) :: offsetElem(0:nDomains)
integer,intent(in) :: elemID
integer,intent(out) :: domain
! Local
integer :: maxSteps
integer :: low,up,mid
integer :: i
domain = 0
maxSteps = int(log10(real(nDomains))/log10(2.0))+1
low = 0
up = nDomains-1
if(offsetElem(low) < elemID .and. elemID <= offsetElem(low+1)) then
domain = low
elseif(offsetElem(up) < elemID .and. elemID <= offsetElem(up+1)) then
domain = up
else
do i = 1,maxSteps
mid = (up-low)/2+low
if(offsetElem(mid) < elemID .and. elemID <= offsetElem(mid+1)) then
domain = mid
return
elseif(elemID > offsetElem(mid+1)) then
low = mid+1
else
up = mid
endif
enddo
endif
endsubroutine ElemToRank