-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader.f
More file actions
563 lines (484 loc) · 15.4 KB
/
Copy pathreader.f
File metadata and controls
563 lines (484 loc) · 15.4 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#if reader == 1
#include "readers/masclet.f"
#endif
#if reader == 0
#include "readers/particle_general.f"
#endif
#if reader == 2
#include "readers/gadget_unformatted.f"
#endif
#if reader == 3
#include "readers/gizmo_hdf5.f"
#endif
#if reader == 4
#include "readers/enzo_hdf5.f"
#endif
*********************************************************************
subroutine sort_dm_particles(n_dm,npart_esp,n_st,ir_kern_stars)
*********************************************************************
* reorders dm particles by species (assumes there are n_esp
* especies, each 8 times lighter than the previous one)
*********************************************************************
use particles
implicit none
include 'input_files/asohf_parameters.dat'
integer n_dm
integer npart_esp(0:n_esp-1)
integer n_st,ir_kern_stars
integer i,j,k,n,iesp,conta
real mlow,mhigh,bas,maxmass,minmass
integer,allocatable::indices(:)
real,allocatable::scr(:,:)
integer,allocatable::scrint(:,:)
write(*,*) 'sorting particles by mass'
maxmass=maxval(masap(1:n_dm))
minmass=minval(masap(1:n_dm))
npart_esp=0
conta=0
allocate(indices(1:n_dm))
do iesp=0,n_esp-1
mhigh=2*maxmass/8.0**iesp
mlow=0.5*maxmass/8.0**iesp
if (mhigh.lt.minmass) exit
do i=1,n_dm
bas=masap(i)
if (bas.lt.mhigh) then
if (bas.gt.mlow) then
conta=conta+1
indices(conta)=i
end if
end if
end do
if (iesp.eq.0) then
npart_esp(iesp)=conta
else
npart_esp(iesp)=conta-sum(npart_esp(0:iesp-1))
end if
if (npart_esp(iesp).gt.0) then
write(*,*) 'of species',iesp,', no. particles:',npart_esp(iesp)
end if
end do
if (conta.ne.n_dm.or.sum(npart_esp(0:n_esp-1)).ne.n_dm) then
write(*,*) 'wrong sorting, cannot continue',conta,n_dm
stop 1
end if
allocate(scr(7,n_dm),scrint(1,n_dm))
!$omp parallel do shared(scr,scrint,rxpa,rypa,rzpa,u2dm,u3dm,u4dm,masap,
!$omp+ oripa,indices,n_dm),
!$omp+ private(i),
!$omp+ default(none)
do i=1,n_dm
scr(1,i)=rxpa(indices(i))
scr(2,i)=rypa(indices(i))
scr(3,i)=rzpa(indices(i))
scr(4,i)=u2dm(indices(i))
scr(5,i)=u3dm(indices(i))
scr(6,i)=u4dm(indices(i))
scr(7,i)=masap(indices(i))
scrint(1,i)=oripa(indices(i))
end do
deallocate(indices)
!$omp parallel do shared(scr,scrint,rxpa,rypa,rzpa,u2dm,u3dm,u4dm,masap,
!$omp+ oripa,indices,n_dm),
!$omp+ private(i),
!$omp+ default(none)
do i=1,n_dm
rxpa(i)=scr(1,i)
rypa(i)=scr(2,i)
rzpa(i)=scr(3,i)
u2dm(i)=scr(4,i)
u3dm(i)=scr(5,i)
u4dm(i)=scr(6,i)
masap(i)=scr(7,i)
oripa(i)=scrint(1,i)
end do
deallocate(scr,scrint)
if (n_st.gt.0) then
if (ir_kern_stars.gt.n_esp-1) then
write(*,*) 'warning: ir_kern_stars > n_esp-1',
& ir_kern_stars,n_esp-1
write(*,*) 'fix n_esp to ir_kern_stars+1, at least'
stop 1
end if
npart_esp(ir_kern_stars)=npart_esp(ir_kern_stars)+n_st
write(*,*) 'stars: of species',ir_kern_stars,
& ', no. particles:',npart_esp(ir_kern_stars)
end if
c write(*,*) 'checking...'
* check
c bas=masap(1)
c do i=2,n_dm
c if (masap(i).gt.1.0001*bas) then
c write(*,*) 'wrong, i=',i
c stop
c end if
c bas=masap(i)
c end do
c stop
return
end
*********************************************************************
subroutine sort_dm_particles_localdensity(n_dm,npart_esp,n_st,
& ir_kern_stars,rodo,re0)
*********************************************************************
* assigns dm particles in species, according to local density,
* and reorders them accordingly.
*********************************************************************
use particles
implicit none
include 'input_files/asohf_parameters.dat'
integer n_dm
integer npart_esp(0:n_esp-1)
integer n_st,ir_kern_stars
real*4 rodo,re0
real*4 dx,dy,dz
common /espaciado/ dx,dy,dz
integer i,j,k,n,iesp,conta,nx,ny,nz,ix,jy,kz,plev
real mlow,mhigh,bas,maxmass,minmass
real xmin,ymin,zmin
integer,allocatable::indices(:)
real,allocatable::scr(:,:)
integer,allocatable::scrint(:,:)
real,allocatable::dens(:,:,:)
integer,allocatable::mocklevel(:)
nx=nmax
ny=nmay
nz=nmaz
allocate(dens(nx,ny,nz))
!$omp parallel do shared(nx,ny,nz,dens),
!$omp+ private(ix,jy,kz),
!$omp+ default(none)
do kz=1,nz
do jy=1,ny
do ix=1,nx
dens(ix,jy,kz)=0.0
end do
end do
end do
xmin=-nx*dx/2.0
ymin=-ny*dy/2.0
zmin=-nz*dz/2.0
!$omp parallel do shared(n_dm,rxpa,rypa,rzpa,xmin,ymin,zmin,dx,dy,dz,
!$omp+ masap,nx,ny,nz),
!$omp+ private(ix,jy,kz,i),
!$omp+ reduction(+:dens), default(none)
do i=1,n_dm
ix=int((rxpa(i)-xmin)/dx)+1
jy=int((rypa(i)-ymin)/dy)+1
kz=int((rzpa(i)-zmin)/dz)+1
if (ix.lt.1) ix=1
if (ix.gt.nx) ix=nx
if (jy.lt.1) jy=1
if (jy.gt.ny) jy=ny
if (kz.lt.1) kz=1
if (kz.gt.nz) kz=nz
dens(ix,jy,kz)=dens(ix,jy,kz)+masap(i)
end do
bas=dx*dy*dz*rodo*re0**3
!$omp parallel do shared(nx,ny,nz,dens,bas),
!$omp+ private(ix,jy,kz),
!$omp+ default(none)
do kz=1,nz
do jy=1,ny
do ix=1,nx
dens(ix,jy,kz)=dens(ix,jy,kz)/bas
end do
end do
end do
allocate(mocklevel(n_dm))
!$omp parallel do shared(n_dm,rxpa,rypa,rzpa,xmin,ymin,zmin,dx,dy,dz,
!$omp+ nx,ny,nz,dens,mocklevel),
!$omp+ private(ix,jy,kz,i,bas),
!$omp+ default(none)
do i=1,n_dm
ix=int((rxpa(i)-xmin)/dx)+1
jy=int((rypa(i)-ymin)/dy)+1
kz=int((rzpa(i)-zmin)/dz)+1
if (ix.lt.1) ix=1
if (ix.gt.nx) ix=nx
if (jy.lt.1) jy=1
if (jy.gt.ny) jy=ny
if (kz.lt.1) kz=1
if (kz.gt.nz) kz=nz
bas=dens(ix,jy,kz)
if (bas.gt.0.0) then
mocklevel(i)=max(min(int(log(bas)/log(8.0)),n_esp-1),0)
else
mocklevel(i)=0
end if
end do
deallocate(dens)
write(*,*) 'sorting particles by local density'
npart_esp=0
conta=0
allocate(indices(1:n_dm))
do iesp=0,n_esp-1
do i=1,n_dm
plev=mocklevel(i)
if (plev.eq.iesp) then
conta=conta+1
indices(conta)=i
end if
end do
if (iesp.eq.0) then
npart_esp(iesp)=conta
else
npart_esp(iesp)=conta-sum(npart_esp(0:iesp-1))
end if
if (npart_esp(iesp).gt.0) then
write(*,*) 'of species',iesp,', no. particles:',npart_esp(iesp)
end if
end do
deallocate(mocklevel)
if (conta.ne.n_dm.or.sum(npart_esp(0:n_esp-1)).ne.n_dm) then
write(*,*) 'wrong sorting, cannot continue',conta,n_dm
stop 1
end if
allocate(scr(7,n_dm),scrint(1,n_dm))
!$omp parallel do shared(scr,scrint,rxpa,rypa,rzpa,u2dm,u3dm,u4dm,masap,
!$omp+ oripa,indices,n_dm),
!$omp+ private(i),
!$omp+ default(none)
do i=1,n_dm
scr(1,i)=rxpa(indices(i))
scr(2,i)=rypa(indices(i))
scr(3,i)=rzpa(indices(i))
scr(4,i)=u2dm(indices(i))
scr(5,i)=u3dm(indices(i))
scr(6,i)=u4dm(indices(i))
scr(7,i)=masap(indices(i))
scrint(1,i)=oripa(indices(i))
end do
deallocate(indices)
!$omp parallel do shared(scr,scrint,rxpa,rypa,rzpa,u2dm,u3dm,u4dm,masap,
!$omp+ oripa,indices,n_dm),
!$omp+ private(i),
!$omp+ default(none)
do i=1,n_dm
rxpa(i)=scr(1,i)
rypa(i)=scr(2,i)
rzpa(i)=scr(3,i)
u2dm(i)=scr(4,i)
u3dm(i)=scr(5,i)
u4dm(i)=scr(6,i)
masap(i)=scr(7,i)
oripa(i)=scrint(1,i)
end do
deallocate(scr,scrint)
if (n_st.gt.0) then
if (ir_kern_stars.gt.n_esp-1) then
write(*,*) 'warning: ir_kern_stars > n_esp-1',
& ir_kern_stars,n_esp-1
write(*,*) 'fix n_esp to ir_kern_stars+1, at least'
stop 1
end if
npart_esp(ir_kern_stars)=npart_esp(ir_kern_stars)+n_st
write(*,*) 'stars: of species',ir_kern_stars,
& ', no. particles:',npart_esp(ir_kern_stars)
end if
c write(*,*) 'checking...'
* check
c bas=masap(1)
c do i=2,n_dm
c if (masap(i).gt.1.0001*bas) then
c write(*,*) 'wrong, i=',i
c stop
c end if
c bas=masap(i)
c end do
c stop
return
end
*********************************************************************
subroutine sort_dm_particles_localdensity_and_mass(n_dm,
& npart_esp,n_st,ir_kern_stars,rodo,re0)
*********************************************************************
* assigns dm particles in species, according to local density,
* and reorders them accordingly.
*********************************************************************
use particles
implicit none
include 'input_files/asohf_parameters.dat'
integer n_dm
integer npart_esp(0:n_esp-1)
integer n_st,ir_kern_stars
real*4 rodo,re0
real*4 dx,dy,dz
common /espaciado/ dx,dy,dz
integer i,j,k,n,iesp,conta,nx,ny,nz,ix,jy,kz,plev,nn,ii,jj,kk
integer fac_grid,maxlev
real mlow,mhigh,bas,maxmass,minmass,fac,levmass,levdens
real xmin,ymin,zmin,dxpa,dypa,dzpa
integer,allocatable::indices(:)
real,allocatable::scr(:,:)
integer,allocatable::scrint(:,:)
real,allocatable::dens(:,:,:)
integer,allocatable::mocklevel(:)
maxmass=maxval(masap(1:n_dm))
minmass=minval(masap(1:n_dm))
maxlev=1 ! particles at this level and above are pointlike, lower bigger cloud
fac_grid=2 ! we consider this factor the base grid
nx=nmax*fac_grid
ny=nmay*fac_grid
nz=nmaz*fac_grid
dxpa=dx/fac_grid
dypa=dy/fac_grid
dzpa=dz/fac_grid
allocate(dens(nx,ny,nz))
!$omp parallel do shared(nx,ny,nz,dens),
!$omp+ private(ix,jy,kz),
!$omp+ default(none)
do kz=1,nz
do jy=1,ny
do ix=1,nx
dens(ix,jy,kz)=0.0
end do
end do
end do
xmin=-nx*dxpa/2.0
ymin=-ny*dypa/2.0
zmin=-nz*dzpa/2.0
!$omp parallel do shared(n_dm,rxpa,rypa,rzpa,xmin,ymin,zmin,dxpa,dypa,
!$omp+ dzpa,masap,nx,ny,nz,maxmass,maxlev),
!$omp+ private(ix,jy,kz,i,plev,ii,jj,kk,nn,fac),
!$omp+ reduction(+:dens), default(none)
do i=1,n_dm
ix=int((rxpa(i)-xmin)/dxpa)+1
jy=int((rypa(i)-ymin)/dypa)+1
kz=int((rzpa(i)-zmin)/dzpa)+1
if (ix.lt.1) ix=1
if (ix.gt.nx) ix=nx
if (jy.lt.1) jy=1
if (jy.gt.ny) jy=ny
if (kz.lt.1) kz=1
if (kz.gt.nz) kz=nz
plev=int(log(maxmass/masap(i))+0.01)
nn=2**max(maxlev-plev,0)-1
fac=1./float((2*nn+1)**3)
do kk=-nn,nn
do jj=-nn,nn
do ii=-nn,nn
dens(ix,jy,kz)=dens(ix,jy,kz)+fac*masap(i)
end do
end do
end do
end do
bas=dx*dy*dz*rodo*re0**3
!$omp parallel do shared(nx,ny,nz,dens,bas),
!$omp+ private(ix,jy,kz),
!$omp+ default(none)
do kz=1,nz
do jy=1,ny
do ix=1,nx
dens(ix,jy,kz)=dens(ix,jy,kz)/bas
end do
end do
end do
allocate(mocklevel(n_dm))
!$omp parallel do shared(n_dm,rxpa,rypa,rzpa,xmin,ymin,zmin,dxpa,dypa,
!$omp+ dzpa,nx,ny,nz,dens,mocklevel,maxmass,masap),
!$omp+ private(ix,jy,kz,i,bas,levmass,levdens),
!$omp+ default(none)
do i=1,n_dm
ix=int((rxpa(i)-xmin)/dxpa)+1
jy=int((rypa(i)-ymin)/dypa)+1
kz=int((rzpa(i)-zmin)/dzpa)+1
if (ix.lt.1) ix=1
if (ix.gt.nx) ix=nx
if (jy.lt.1) jy=1
if (jy.gt.ny) jy=ny
if (kz.lt.1) kz=1
if (kz.gt.nz) kz=nz
bas=dens(ix,jy,kz)
if (bas.gt.0.0) then
levmass=log(maxmass/masap(i))/log(8.0)
levdens=log(bas)/log(8.0)
mocklevel(i)=max(min(
& int(max(levmass,levdens)+0.01),
& n_esp-1),0)
else
mocklevel(i)=0
end if
end do
deallocate(dens)
write(*,*) 'sorting particles by local density + particle mass'
npart_esp=0
conta=0
allocate(indices(1:n_dm))
do iesp=0,n_esp-1
do i=1,n_dm
plev=mocklevel(i)
if (plev.eq.iesp) then
conta=conta+1
indices(conta)=i
end if
end do
if (iesp.eq.0) then
npart_esp(iesp)=conta
else
npart_esp(iesp)=conta-sum(npart_esp(0:iesp-1))
end if
if (npart_esp(iesp).gt.0) then
write(*,*) 'of species',iesp,', no. particles:',npart_esp(iesp)
end if
end do
deallocate(mocklevel)
if (conta.ne.n_dm.or.sum(npart_esp(0:n_esp-1)).ne.n_dm) then
write(*,*) 'wrong sorting, cannot continue',conta,n_dm
stop 1
end if
allocate(scr(7,n_dm),scrint(1,n_dm))
!$omp parallel do shared(scr,scrint,rxpa,rypa,rzpa,u2dm,u3dm,u4dm,masap,
!$omp+ oripa,indices,n_dm),
!$omp+ private(i),
!$omp+ default(none)
do i=1,n_dm
scr(1,i)=rxpa(indices(i))
scr(2,i)=rypa(indices(i))
scr(3,i)=rzpa(indices(i))
scr(4,i)=u2dm(indices(i))
scr(5,i)=u3dm(indices(i))
scr(6,i)=u4dm(indices(i))
scr(7,i)=masap(indices(i))
scrint(1,i)=oripa(indices(i))
end do
deallocate(indices)
!$omp parallel do shared(scr,scrint,rxpa,rypa,rzpa,u2dm,u3dm,u4dm,masap,
!$omp+ oripa,indices,n_dm),
!$omp+ private(i),
!$omp+ default(none)
do i=1,n_dm
rxpa(i)=scr(1,i)
rypa(i)=scr(2,i)
rzpa(i)=scr(3,i)
u2dm(i)=scr(4,i)
u3dm(i)=scr(5,i)
u4dm(i)=scr(6,i)
masap(i)=scr(7,i)
oripa(i)=scrint(1,i)
end do
deallocate(scr,scrint)
if (n_st.gt.0) then
if (ir_kern_stars.gt.n_esp-1) then
write(*,*) 'warning: ir_kern_stars > n_esp-1',
& ir_kern_stars,n_esp-1
write(*,*) 'fix n_esp to ir_kern_stars+1, at least'
stop 1
end if
npart_esp(ir_kern_stars)=npart_esp(ir_kern_stars)+n_st
write(*,*) 'stars: of species',ir_kern_stars,
& ', no. particles:',npart_esp(ir_kern_stars)
end if
c write(*,*) 'checking...'
* check
c bas=masap(1)
c do i=2,n_dm
c if (masap(i).gt.1.0001*bas) then
c write(*,*) 'wrong, i=',i
c stop
c end if
c bas=masap(i)
c end do
c stop
return
end