-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_particles.f90
155 lines (143 loc) · 3.65 KB
/
sort_particles.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
subroutine sort_particles
use loguns, only:iprint
use part, only:rho,npart
use mem_allocation, only:alloc
use linklist, only:ncellsloop,ifirstincell,ll
use get_neighbour_lists, only:get_neighbour_list
implicit none
integer, dimension(size(rho)) :: ilist,listneigh
logical, dimension(size(rho)) :: sorted
integer :: i,icell,ipart,nneigh,n,j,idone
real :: t1,t2
write(iprint,*) 'sorting particles...'
call cpu_time(t1)
ipart = 0
sorted = .false.
do icell=1,ncellsloop
call get_neighbour_list(icell,listneigh,nneigh)
i = ifirstincell(icell)
idone = -1
do while (i.ne.-1 .and. ipart.lt.npart)
idone = idone + 1
if (i.le.npart .and. .not.sorted(i)) then
ipart = ipart + 1
ilist(ipart) = i
sorted(i) = .true.
if (ipart.lt.npart) then
do n=idone+1,nneigh
j = listneigh(n)
if (j.le.npart .and. .not.sorted(j)) then
ipart = ipart + 1
ilist(ipart) = j
sorted(j) = .true.
endif
enddo
endif
endif
i = ll(i)
enddo
enddo
print*,'ncellsloop = ',ncellsloop,' ipart = ',ipart, 'npart= ',npart
!
!--do not re-order ghosts (but ireal will be wrong)
!
! do i=1,npart
! ilist(i) = i
! enddo
do i=npart+1,size(ilist)
ilist(i) = i
enddo
call alloc(size(rho),sortlist=ilist)
call cpu_time(t2)
write(iprint,*) 'completed in ',t2-t1,'s'
end subroutine sort_particles
subroutine indexx(n, arr, indx)
!************************************************************
! *
! This is INDEXX using the quicksort algorithm. *
! *
!************************************************************
implicit none
integer, parameter :: m=7, nstack=500
integer, intent(in) :: n
real, dimension(n), intent(in) :: arr
integer, dimension(n), intent(out) :: indx
integer :: i,j,k,l,ir,jstack,indxt,itemp
integer, dimension(nstack) :: istack
real :: a
do j = 1, n
indx(j) = j
enddo
jstack = 0
l = 1
ir = n
1 if (ir - l.lt.m) then
do j = l + 1, ir
indxt = indx(j)
a = arr(indxt)
do i = j - 1, 1, -1
if (arr(indx(i)).le.a) goto 2
indx(i + 1) = indx(i)
end do
i = 0
2 indx(i + 1) = indxt
end do
if (jstack.eq.0) return
ir = istack(jstack)
l = istack(jstack - 1)
jstack = jstack - 2
else
k = (l + ir)/2
itemp = indx(k)
indx(k) = indx(l + 1)
indx(l + 1) = itemp
if (arr(indx(l + 1)).gt.arr(indx(ir))) then
itemp = indx(l + 1)
indx(l + 1) = indx(ir)
indx(ir) = itemp
endif
if (arr(indx(l)).gt.arr(indx(ir))) then
itemp = indx(l)
indx(l) = indx(ir)
indx(ir) = itemp
endif
if (arr(indx(l + 1)).gt.arr(indx(l))) then
itemp = indx(l + 1)
indx(l + 1) = indx(l)
indx(l) = itemp
endif
i = l + 1
j = ir
indxt = indx(l)
a = arr(indxt)
3 continue
i = i + 1
if (arr(indx(i)).lt.a) goto 3
4 continue
j = j - 1
if (arr(indx(j)).gt.a) goto 4
if (j.lt.i) goto 5
itemp = indx(i)
indx(i) = indx(j)
indx(j) = itemp
goto 3
5 indx(l) = indx(j)
indx(j) = indxt
jstack = jstack + 2
if (jstack.gt.nstack) then
print*,'fatal error!!! stacksize exceeded in sort'
print*,'need to set parameter nstack higher in subroutine indexx '
stop
endif
if (ir - i + 1.ge.j - l) then
istack(jstack) = ir
istack(jstack - 1) = i
ir = j - 1
else
istack(jstack) = j - 1
istack(jstack - 1) = l
l = i
endif
endif
goto 1
end subroutine indexx