From ea8a0f661a3643c68f380ed28995873336eb2240 Mon Sep 17 00:00:00 2001 From: David A Minton Date: Fri, 22 Oct 2021 02:53:33 -0400 Subject: [PATCH] Fixed broken sort --- src/encounter/encounter_check.f90 | 2 +- src/util/util_sort.f90 | 46 ++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/encounter/encounter_check.f90 b/src/encounter/encounter_check.f90 index 4cc53d082..c64dbd68a 100644 --- a/src/encounter/encounter_check.f90 +++ b/src/encounter/encounter_check.f90 @@ -452,7 +452,7 @@ subroutine encounter_check_all_sort_and_sweep_plplm(nplm, nplt, xplm, vplm, xplt ntot_last = ntot end if - + !$omp parallel do default(private) schedule(static) & !$omp shared(xplm, xplt, vplm, vplt, rencm, renct, boundingbox) & !$omp firstprivate(dt, nplm, nplt, ntot) diff --git a/src/util/util_sort.f90 b/src/util/util_sort.f90 index ca6e2d0e2..d27489da5 100644 --- a/src/util/util_sort.f90 +++ b/src/util/util_sort.f90 @@ -74,10 +74,14 @@ module pure subroutine util_sort_dp(arr) do i = 2, n tmp = arr(i) j = i - 1 - do while ((j >= 1) .and. (arr(j) > tmp)) + do while ((j > 1) .and. (arr(j) > tmp)) arr(j + 1) = arr(j) j = j - 1 end do + if ((j == 1) .and. (arr(j) > tmp)) then + arr(j + 1) = arr(j) + j = j - 1 + end if arr(j + 1) = tmp end do @@ -108,11 +112,14 @@ module pure subroutine util_sort_index_dp(arr, ind) do i = 2, n itmp = ind(i) j = i - 1 - do while ((j >= 2) .and. (arr(ind(j)) > itmp)) + do while ((j > 1) .and. (arr(ind(j)) > arr(itmp))) ind(j + 1) = ind(j) j = j - 1 end do - if ((j == 1) .and. (arr(ind(1)) > itmp)) ind(2) = ind(1) + if ((j == 1) .and. (arr(ind(j)) > arr(itmp))) then + ind(j + 1) = ind(j) + j = j - 1 + end if ind(j + 1) = itmp end do @@ -137,10 +144,14 @@ module pure subroutine util_sort_i4b(arr) do i = 2, n tmp = arr(i) j = i - 1 - do while ((j >= 1) .and. (arr(j) > tmp)) + do while ((j > 1) .and. (arr(j) > tmp)) arr(j + 1) = arr(j) j = j - 1 end do + if ((j == 1) .and. (arr(j) > tmp)) then + arr(j + 1) = arr(j) + j = j - 1 + end if arr(j + 1) = tmp end do @@ -168,21 +179,17 @@ module pure subroutine util_sort_index_i4b(arr, ind) allocate(ind(n)) ind = [(i, i=1, n)] end if - - itmp = ind(2) - if (arr(ind(1)) > itmp) then - ind(2) = ind(1) - ind(1) = itmp - end if - do i = 2, n itmp = ind(i) j = i - 1 - do while ((j >= 2) .and. (arr(ind(j)) > itmp)) + do while ((j > 1) .and. (arr(ind(j)) > arr(itmp))) ind(j + 1) = ind(j) j = j - 1 end do - if ((j == 1) .and. (arr(ind(1)) > itmp)) ind(2) = ind(1) + if ((j == 1) .and. (arr(ind(j)) > arr(itmp))) then + ind(j + 1) = ind(j) + j = j - 1 + end if ind(j + 1) = itmp end do @@ -207,10 +214,14 @@ module pure subroutine util_sort_sp(arr) do i = 2, n tmp = arr(i) j = i - 1 - do while ((j >= 1) .and. (arr(j) > tmp)) + do while ((j > 1) .and. (arr(j) > tmp)) arr(j + 1) = arr(j) j = j - 1 end do + if ((j == 1) .and. (arr(j) > tmp)) then + arr(j + 1) = arr(j) + j = j - 1 + end if arr(j + 1) = tmp end do @@ -241,11 +252,14 @@ module pure subroutine util_sort_index_sp(arr, ind) do i = 2, n itmp = ind(i) j = i - 1 - do while ((j >= 2) .and. (arr(ind(j)) > itmp)) + do while ((j > 1) .and. (arr(ind(j)) > arr(itmp))) ind(j + 1) = ind(j) j = j - 1 end do - if ((j == 1) .and. (arr(ind(1)) > itmp)) ind(2) = ind(1) + if ((j == 1) .and. (arr(ind(j)) > arr(itmp))) then + ind(j + 1) = ind(j) + j = j - 1 + end if ind(j + 1) = itmp end do