Newer
Older
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
!-----------------------------------------------------------------------
!
SUBROUTINE tree_sort
!
!
!-----------------------------------------------------------------------
!
!
! Subroutine to insert the bodies into the tree up to the l_end level
! Bodies are re-arranged in pos_sort and vel_sort arrays that
! are sorted following the tree schema.
!
!-----------------------------------------------------------------------
USE fly_h
INCLUDE 'mpif.h'
! Declaration of local variables.
! -------------------------------
REAL(KIND=8), DIMENSION (nsubcell,ndim) :: pm1
REAL(KIND=8), DIMENSION (ndim):: pl,pcl,pcl_par
REAL(KIND=8) :: cl_size
INTEGER(KIND=4) :: nbodlist,ip_pr
INTEGER(KIND=4), DIMENSION (1) :: n_sort
INTEGER(KIND=4) :: n_sort_PE0
INTEGER(KIND=4), DIMENSION (nb_loc) :: sub_temp1,par_temp1
INTEGER(KIND=4), DIMENSION (ndim) :: nindex
INTEGER(KIND=4), DIMENSION (nsubcell) ::isub
INTEGER :: i,j,k,ind_cl,ic,tag,istatus(MPI_STATUS_SIZE)
CHARACTER(LEN=4) ::ifl
CHARACTER(LEN=256) :: name_file
DATA pm1/4*-1.,4*1.,2*-1.,2*1.,2*-1.,2*1.,-1.,1.,-1.,1., &
& -1.,1.,-1.,1./,nindex/4,2,1/
!-----------------------------------------------------------------------
#ifdef SORT
root=nbodsmax+1
pos_sort=0.0
vel_sort=0.0
IF(l_end.gt.4) l_end=4
IF(l_end.lt.2) l_end=2
CALL MPI_BARRIER(MPI_COMM_WORLD,ierror)
!----------------------------------------------------------------------
! Compute coordinates of center of root cell.
!----------------------------------------------------------------------
subp=0 !zeroing all the tree
DO k=1,ndim
pos_cell(k,1)=rmin(k)+0.5*rsize
ENDDO
IF(me.eq.0) write(uterm,*) 'FLY_SORT: nbodies=',nbodies,' Ending level=', l_end
!-----------------------------------------------------------------------
! Place all bodies on active body list, having root as parent; place
! root on active cell list.
!-----------------------------------------------------------------------
par_temp1(1:nb_res_loc(me+1))=root
nbodlist=nb_res_loc(me+1)
lmax=1
cell_ss(1,1)=root
cell_ss(1,2)=root
size_level(1)=rsize
!-----------------------------------------------------------------------
! Create local complete tree structure
!-----------------------------------------------------------------------
CALL MPI_BARRIER(MPI_COMM_WORLD,ierror)
DO i=1,l_end
ip_pr=cell_ss(i,2)
cl_size=size_level(i)*0.5
DO j=cell_ss(i,1),cell_ss(i,2)
DO k=1,nsubcell
ind_cl=j-nbodsmax
ip_pr=ip_pr+1
pcl_par(1:3)=pos_cell(1:3,ind_cl)
subp(k,ind_cl)=ip_pr
pcl(1)=pcl_par(1)+pm1(k,1)*0.5*cl_size
pcl(2)=pcl_par(2)+pm1(k,2)*0.5*cl_size
pcl(3)=pcl_par(3)+pm1(k,3)*0.5*cl_size
ind_loc_2=ip_pr-nbodsmax
pos_cell(1:3,ind_loc_2)=pcl(1:3)
ENDDO !k
ENDDO !j
cell_ss(i+1,1)=cell_ss(i,2)+1
cell_ss(i+1,2)=ip_pr
size_level(i+1)=size_level(i)*0.5
ENDDO !i
!-----------------------------------------------------------------------
! Compute parent for each local body
!-----------------------------------------------------------------------
DO i=1,nbodlist
pl(1:3)= pos(1:3,i)
DO j=1,l_end
sub_temp1(i)=1
ind_loc=par_temp1(i)-nbodsmax
pcl(1:ndim)=pos_cell(1:ndim,ind_loc)
DO k=1,ndim
IF(pl(k).GE.pcl(k)) sub_temp1(i)=sub_temp1(i)+nindex(k)
ENDDO
ic=sub_temp1(i)
ind_cl=par_temp1(i)-nbodsmax
par_temp1(i)=subp(ic,ind_cl)
ENDDO !j
ENDDO !i
!-----------------------------------------------------------------------
! Place bodies in sorted arraies and output data
!-----------------------------------------------------------------------
CALL MPI_BARRIER(MPI_COMM_WORLD, ierror)
IF(me.eq.0) THEN
! Open output binary body data file.
! ----------------------------------
name_file=ibodfile
IF(tstep.GT.1000) WRITE(ifl,'(I4.4)') tstep
IF(tstep.GE.100 .AND. tstep .LT. 1000) WRITE(ifl,'(I3.3)') tstep
IF(tstep.GE.10 .AND. tstep .LT. 100) WRITE(ifl,'(I2.2)') tstep
IF(tstep.LT. 10) WRITE(ifl,'(I1.1)') tstep
name_file=TRIM(name_file)//TRIM(ifl) //'_sort'
IF(ouasc.eq.'A') THEN
OPEN(UNIT=ubodsasc,FILE=name_file,STATUS='UNKNOWN')
ELSE
call c_open_so_bin(name_file)
ENDIF
ENDIF !if me.eq.0
! Loop on all the cells of the last level
! ----------------------------------------
lmax=l_end+1
n_acc=0
n_acc1=0
DO j=cell_ss(lmax,1),cell_ss(lmax,2)
n_sort(1)=0
DO i=1,nbodlist
IF(par_temp1(i).ne.j) CYCLE
n_sort(1)=n_sort(1)+1
pos_sort(1:3,n_sort(1))=pos(1:3,i)
ENDDO ! do i
n_acc=n_acc+n_sort(1)
CALL MPI_BARRIER(MPI_COMM_WORLD, ierror)
!-----------------------------------------------------------------------
! For this cell write output
!-----------------------------------------------------------------------
IF(me.eq.0) THEN
n_sort_PE0=n_sort(1)
nlong=n_sort_PE0*3
n_acc1=0
IF(nlong.gt.0) THEN
IF(ouasc.eq.'A') THEN
DO p=1,n_sort_PE0
WRITE(ubodsasc,1000) pos_sort(1,p),pos_sort(2,p),pos_sort(3,p)
ENDDO
ELSE
call c_write_so_bin(pos_sort(1,1),nlong)
ENDIF !if ouasc
ENDIF
pos_sort=0
n_acc1=n_acc1+n_sort(1)
DO PE_indx=1,NPES-1 ! read and transmit
tag=1
nlong=1
CALL MPI_RECV(n_sort(1), nlong, MPI_INTEGER4, PE_indx, tag, MPI_COMM_WORLD, istatus, ierror)
tag=2
nlong=n_sort(1)*3
IF(nlong.gt.0) THEN
CALL MPI_RECV(pos_sort(1,1), nlong, MPI_REAL8, PE_indx, tag, MPI_COMM_WORLD, istatus, ierror)
IF(ouasc.eq.'A') THEN
DO p=1,n_sort(1)
WRITE(ubodsasc,1000) pos_sort(1,p),pos_sort(2,p),pos_sort(3,p)
ENDDO
ELSE
call c_write_so_bin(pos_sort(1,1),nlong)
ENDIF !if ouasc
ENDIF
pos_sort=0
n_acc1=n_acc1+n_sort(1)
ENDDO
ELSE ! if me.eq.0
tag=1
nlong=1
CALL MPI_SEND(n_sort(1), nlong, MPI_INTEGER4, PE0, tag, MPI_COMM_WORLD, ierror)
tag=2
nlong=n_sort(1)*3
IF(nlong.gt.0) THEN
CALL MPI_SEND(pos_sort(1,1), nlong, MPI_REAL8, PE0, tag, MPI_COMM_WORLD, ierror)
ENDIF
ENDIF ! if me.eq.0
!-----------------------------------------------------------------------
! Next cell of the last level
!-----------------------------------------------------------------------
CALL MPI_BARRIER(MPI_COMM_WORLD, ierror)
ENDDO !do j
!-----------------------------------------------------------------------
! Place velocities in sorted arraies and output data
!-----------------------------------------------------------------------
! Loop on all the cells of the last level
! ----------------------------------------
!
lmax=l_end+1
DO j=cell_ss(lmax,1),cell_ss(lmax,2)
n_sort(1)=0
DO i=1,nbodlist
IF(par_temp1(i).ne.j) CYCLE
n_sort(1)=n_sort(1)+1
vel_sort(1:3,n_sort(1))=vel(1:3,i)
ENDDO ! do i
n_acc=n_acc+n_sort(1)
CALL MPI_BARRIER(MPI_COMM_WORLD, ierror)
!-----------------------------------------------------------------------
! For this cell write output
!-----------------------------------------------------------------------
IF(me.eq.0) THEN
n_sort_PE0=n_sort(1)
nlong=n_sort_PE0*3
IF(nlong.gt.0) THEN
IF(ouasc.eq.'A') THEN
DO p=1,n_sort_PE0
WRITE(ubodsasc,1000) vel_sort(1,p),vel_sort(2,p),vel_sort(3,p)
ENDDO
ELSE
call c_write_so_bin(vel_sort(1,1),nlong)
ENDIF !if ouasc
ENDIF
vel_sort=0
DO PE_indx=1,NPES-1 ! read and transmit
tag=3
nlong=1
CALL MPI_RECV(n_sort(1), nlong, MPI_INTEGER4, PE_indx, tag, MPI_COMM_WORLD, istatus, ierror)
tag=4
nlong=n_sort(1)*3
IF(nlong.gt.0) THEN
CALL MPI_RECV(vel_sort(1,1), nlong, MPI_REAL8, PE_indx, tag, MPI_COMM_WORLD, istatus, ierror)
IF(ouasc.eq.'A') THEN
DO p=1,n_sort(1)
WRITE(ubodsasc,1000) vel_sort(1,p),vel_sort(2,p),vel_sort(3,p)
ENDDO
ELSE
call c_write_so_bin(vel_sort(1,1),nlong)
ENDIF !if ouasc
ENDIF
ENDDO
ELSE ! if me.eq.0
tag=3
nlong=1
CALL MPI_SEND(n_sort(1), nlong, MPI_INTEGER4, PE0, tag, MPI_COMM_WORLD, ierror)
tag=4
nlong=n_sort(1)*3
IF(nlong.gt.0) THEN
CALL MPI_SEND(vel_sort(1,1), nlong, MPI_REAL8, PE0, tag, MPI_COMM_WORLD, ierror)
ENDIF
ENDIF ! if me.eq.0
!-----------------------------------------------------------------------
! Next cell of the last level
!-----------------------------------------------------------------------
CALL MPI_BARRIER(MPI_COMM_WORLD, ierror)
ENDDO !do j
1000 FORMAT(3(1X,F20.10))
IF(me.eq.0) THEN
IF(ouasc.eq.'A') THEN
CLOSE(ubodsout)
ELSE
call c_close_so_bin()
ENDIF
write(uterm,*) 'FLY_SORT. Output sorted file ',TRIM(name_file),' is written.'
call flush(6)
ENDIF
#endif
RETURN
END