Debian Codegraph @

src/read_data_gadget.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2023 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-------------------------------------------------------------------------
24 ! this subroutine reads from the data file(s)
25 ! change this to change the format of data input
26 !
27 ! THIS VERSION IS FOR OUTPUT FROM THE GADGET CODE
28 ! (works with GADGET v1.0, v2.0 and v3.0)
29 !
30 ! SOME CHOICES FOR THIS FORMAT CAN BE SET USING THE FOLLOWING
31 ! COMMAND-LINE FLAGS:
32 !
33 ! --format=2 reads the block-labelled GADGET format
34 ! rather than the default format.
35 ! --hsoft
36 ! if given a value > 0.0 will assign a
37 ! smoothing length to dark matter particles which can then be
38 ! used in the rendering
39 ! --extracols=temp,H2 abundance
40 ! if set to a comma separated list of column labels,
41 ! will attempt to read additional columns containing gas particle
42 ! properties beyond the end of file
43 ! --starpartcols=temp,vel
44 ! if set to a comma separated list of column labels,
45 ! will attempt to read additional columns containing star particle
46 ! properties beyond the end of file
47 ! --checkids
48 ! if set then reads and checks
49 ! particle IDs for negative values and flags these as accreted particles
50 ! --hcolumn=10
51 ! if set to a positive integer, specifies the location
52 ! of the smoothing length in the columns, overriding any default settings.
53 ! --ignore-iflagcool if set to 'YES' or `TRUE', does not assume that
54 ! extra columns are present even if the cooling flag is set in the header.
55 !
56 ! the data is stored in the global array dat
57 !
58 ! >> this subroutine must return values for the following: <<
59 !
60 ! ncolumns : number of data columns
61 ! ndim, ndimV : number of spatial, velocity dimensions
62 ! nstepsread : number of steps read from this file
63 !
64 ! dat(maxpart,maxplot,maxstep) : main data array
65 !
66 ! npartoftype(maxstep): number of particles of each type in each timestep
67 !
68 ! time(maxstep) : time at each step
69 ! gamma(maxstep) : gamma at each step
70 ! (used in calc_quantities for calculating the pressure)
71 !
72 ! most of these values are stored in global arrays
73 ! in the module 'particle_data'
74 !
75 ! Partial data read implemented Nov 2006 means that columns with
76 ! the 'required' flag set to false are not read (read is therefore much faster)
77 !-------------------------------------------------------------------------
78 module readdata_gadget
79 use params, only:maxplot
80 implicit none
81
82 public :: read_data_gadget, set_labels_gadget, file_format_is_gadget
83
84 real :: hsoft
85 character(len=4), dimension(maxplot) :: blocklabelgas
86 logical :: havewarned = .false.
87 logical :: auto_detected_block_format = .false.
88
89 private
90 contains
91
92 subroutine read_data_gadget(rootname,istepstart,ipos,nstepsread)
93 use particle_data, only:dat,npartoftype,masstype,time,gamma,maxpart,maxcol,maxstep
94 use params, only:doub_prec,sing_prec,maxparttypes
95 use settings_data, only:ndim,ndimV,ncolumns,ncalc,iformat,required,ipartialread, &
96 ntypes,debugmode,iverbose
97 use mem_allocation, only:alloc
98 use labels, only:ih,irho,ipmass,labeltype,iamvec
99 use system_utils, only:renvironment,lenvironment,ienvironment,envlist
100 integer, intent(in) :: istepstart,ipos
101 integer, intent(out) :: nstepsread
102 character(len=*), intent(in) :: rootname
103 character(len=len(rootname)+10) :: datfile,densfile,hfile
104 character(len=4) :: blocklabel
105 character(len=20) :: string
106 integer, dimension(6) :: npartoftypei,Nall
107 integer, dimension(:), allocatable :: iamtemp
108 integer :: i,j,n,itype,icol,ierr,ierrh,ierrrho,nhset,nvec,ifile
109 integer :: index1,index2,indexstart,indexend,nmassesdumped,ntypesused
110 integer :: ncolstep,npart_max,nstep_max,ntoti,nacc,ntotall,idot
111 integer :: iFlagSfr,iFlagFeedback,iFlagCool,nfiles,istart,nhfac
112 integer :: nextracols,nstarcols,i1,i2,i3,i4,lenblock,idumpformat
113 integer, dimension(6) :: i0,i1all,i2all
114 integer, parameter :: iunit = 11, iunitd = 102, iunith = 103
115 logical :: iexist,reallocate,checkids,goterrors
116 logical, dimension(6) :: ireadtype
117 real(doub_prec) :: timetemp,ztemp
118 real(doub_prec), dimension(6) :: massoftypei
119 real(sing_prec), dimension(:), allocatable :: dattemp1
120 real(sing_prec), dimension(:,:), allocatable :: dattemp
121 real :: hfact,hfactmean
122 real, parameter :: pi = 4.*atan(1.)
123
124 nstepsread = 0
125 goterrors = .false.
126 if (maxparttypes < 6) then
127 print*,' *** ERROR: not enough particle types for GADGET data read ***'
128 print*,' *** you need to edit splash parameters and recompile ***'
129 stop
130 endif
131
132 if (len_trim(rootname) > 0) then
133 datfile = trim(rootname)
134 else
135 print*,' **** no data read **** '
136 return
137 endif
138 !
139 !--check if first data file exists
140 !
141 inquire(file=datfile,exist=iexist)
142 if (.not.iexist) then
143 !
144 !--look for a file with .0 on the end for multiple-file reads
145 !
146 datfile=trim(rootname)//'.0'
147 inquire(file=datfile,exist=iexist)
148 if (.not.iexist) then
149 print "(a)",' *** error: '//trim(rootname)//': file not found ***'
150 return
151 endif
152 endif
153 !
154 !--set parameters which do not vary between timesteps
155 !
156 ndim = 3
157 ndimV = 3
158 idumpformat = 0
159 if (auto_detected_block_format) then
160 idumpformat = 2
161 else
162 idumpformat = ienvironment('GSPLASH_FORMAT')
163 endif
164 checkids = lenvironment('GSPLASH_CHECKIDS')
165 !
166 !--read data from snapshots
167 !
168 i = istepstart
169 !
170 !--i0 is the offset used to read the data into the arrays
171 ! (non-zero for read from multiple files)
172 ! The offset is different for each particle type, somewhat
173 ! complicating the data read -- we shuffle the particles from
174 ! multiple files so that they are in type order.
175 !
176 i0(:) = 0
177 !
178 !--loop over the number of files
179 !
180 ifile = 0
181 ntotall = 0
182 over_files: do while(iexist)
183
184 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
185 ifile = ifile + 1
186
187 !
188 !--open data file and read data
189 !
190 open(iunit,iostat=ierr,file=datfile,status='old',form='unformatted')
191 if (ierr /= 0) then
192 print "(a)", '*** ERROR OPENING FILE ***'
193 return
194 endif
195
196 !if (any(i0 > 0)) print*,'starting position for each type in data array: ',i0(:)
197 !
198 !--read header for this timestep
199 !
200 if (idumpformat==2) then
201 print "(a)",' >> reading block labelled Gadget format <<'
202 read(iunit,iostat=ierr) blocklabel,lenblock
203 !print*,ierr,blocklabel,lenblock
204 if (ierr /= 0 .or. lenblock /= 264) then
205 print "(/,a,/)",'*** ERROR READING HEADER: wrong endian? or wrong format? ***'
206 close(iunit)
207 if (ifile==1) then
208 return
209 else
210 exit over_files
211 endif
212 endif
213 else
214 if (ifile==1) print "(a)",' >> reading default Gadget format <<'
215 endif
216
217 npartoftypei(:) = 0
218 Nall(:) = 0
219 massoftypei(:) = 0.
220 iFlagCool = 0
221 nfiles = 0
222 read(iunit,iostat=ierr) npartoftypei(1:6),massoftypei(1:6),timetemp,ztemp, &
223 iFlagSfr,iFlagFeedback,Nall(1:6),iFlagCool,nfiles
224
225 ntoti = int(sum(npartoftypei(1:6))) ! int here is unnecessary, but avoids compiler warnings
226
227 if (nfiles > 1) then
228 ntotall = int(sum(Nall(1:6)))
229 else
230 ntotall = ntoti
231 endif
232 if (debugmode) then
233 print*,'DEBUG: ierr = ',ierr
234 print*,'DEBUG: ntoti = ',ntoti,' ntotall = ',ntotall,' nfiles = ',nfiles
235 print*,'DEBUG: npartoftype = ',npartoftypei(1:6),' Nall = ',Nall(1:6)
236 print*,'DEBUG: iFlagSfr = ',iFlagSfr,' iFlagFeedback = ',iFlagFeedback,' iFlagCool = ',iFlagCool
237 print*,'DEBUG: time = ',timetemp,' z = ',ztemp
238 endif
239
240 if (ierr /= 0 .or. ntoti <= 0 .or. ntotall <= 0 .or. any(npartoftypei < 0) .or. nfiles < 0 &
241 .or. nfiles > 1e6) then
242 print "(/,a)", '*** ERROR READING TIMESTEP HEADER: wrong endian? ***'
243 print "(/,a)", ' (see splash userguide for compiler-dependent'
244 print "(a)", ' ways to change endianness on the command line)'
245 print "(/,a)", ' (set --format=2 if you are using the block-labelled Gadget format)'
246 close(iunit)
247 if (ifile==1) then
248 return
249 else
250 exit over_files
251 endif
252 endif
253
254 !
255 !--if we are reading from multiple files,
256 ! check that the sequence starts from the correct file
257 !
258 if (nfiles > 1) then
259 idot = len_trim(datfile)-1
260 if (ifile==1 .and. datfile(idot:idot+1) /= '.0') then
261 if (nfiles < 100) then
262 string = "(/,a,i2,a,/,a,/)"
263 else
264 string = "(/,a,i7,a,/,a,/)"
265 endif
266 if (nfiles > 10000) then
267 !--this is the most likely scenario here
268 print "(a)",'*** ERROR reading timestep header: wrong endian? ***'
269 else
270 print string,' ERROR: read is from multiple files (nfiles = ',nfiles,')',&
271 ' but this is not the first file (does not end in .0): skipping...'
272 endif
273 close(iunit)
274 return
275 endif
276 endif
277
278 if (idumpformat==2) then
279 ncolstep = 1
280 do while (ierr==0)
281 call read_blockheader(idumpformat,iunit,0,index2,blocklabelgas(ncolstep),&
282 lenblock,nvec,npartoftypei(1))
283 read(iunit,iostat=ierr)
284 if (ierr==0 .and. index2 > 0) then
285 select case(blocklabelgas(ncolstep))
286 case('ID ','INFO')
287 ! not a column
288 case default
289 ncolstep = ncolstep + nvec
290 end select
291 endif
292 enddo
293 ncolstep = ncolstep - 1
294 rewind(iunit)
295 read(iunit,iostat=ierr)
296 read(iunit,iostat=ierr)
297 iformat = 2
298 nextracols = 0
299 nstarcols = 0
300
301 else
302
303 iformat = 0
304 if (iFlagCool==1 .and. .not.(lenvironment('GSPLASH_IGNORE-IFLAGCOOL') &
305 .or. lenvironment('GSPLASH_IGNORE_IFLAGCOOL'))) then
306 iformat = 1
307 ncolstep = 12 ! 3 x pos, 3 x vel, pmass, utherm, rho, Ne, Nh, h
308 if (ifile==1) print "(a)",' cooling flag on : assuming Ne, Nh dumped before h'
309 else
310 iformat = 0
311 ncolstep = 10 ! 3 x pos, 3 x vel, pmass, utherm, rho, h
312 endif
313 if (iFlagSfr==1) then
314 if (ifile==1) print "(a)",' star formation flag on: assuming star formation rate dumped '
315 ncolstep = ncolstep + 1
316 iformat = iformat + 10
317 endif
318
319 call envlist('GSPLASH_EXTRACOLS',nextracols)
320 if (nextracols > 0) then
321 print "(a,i2,a)",' READING ',nextracols,' EXTRA COLUMNS '
322 ncolstep = ncolstep + nextracols
323 endif
324 call envlist('GSPLASH_STARPARTCOLS',nstarcols)
325 if (nstarcols > 0) then
326 print "(a,i2,a)",' READING ',nstarcols,' STAR PARTICLE COLUMN(S) '
327 ncolstep = ncolstep + nstarcols
328 endif
329 !call envlist('GSPLASH_EXTRAVECCOLS',nextraveccols)
330 !if (nextraveccols > 0) then
331 ! print "(a,i2,a)",' READING ',nextraveccols,' EXTRA COLUMNS '
332 ! ncolstep = ncolstep + nextraveccols
333 !endif
334 endif
335
336 if (ifile==1) then
337 ncolumns = ncolstep
338 !
339 !--call set labels to get ih, ipmass, irho for use in the read routine
340 !
341 hsoft = 0. ! to avoid unset variable
342 call set_labels_gadget
343 endif
344
345 if (ifile==1) then
346 print*,'time : ',timetemp
347 print "(1x,a,f8.2)",'z (redshift) : ',ztemp
348 endif
349 print*,'Npart (by type) : ',npartoftypei(1:6)
350 if (ifile==1) print*,'Mass (by type) : ',massoftypei(1:6)
351 ! print "(10x,'|',6(1x,a12,'|'))", (labeltype(itype),itype=1,ntypes)
352 ! print "(a10,'|',6(i11,2x,'|'))", 'Npart : ',npartoftypei
353 ! print "(a10,'|',6(es11.3,2x,'|'))",'Mass : ',massoftypei
354 print*,'N_gas : ',npartoftypei(1)
355 print*,'N_total : ',ntoti
356 if (ifile==1) print*,'N data columns : ',ncolstep
357 if (nfiles > 1 .and. ifile==1) then
358 print*,'Nall : ',Nall(1:6)
359 endif
360
361 if (nfiles > 1) then
362 if (ifile==1) print "(a,i4,a)",' reading from ',nfiles,' files'
363 elseif (nfiles < 0) then
364 print*,'*** ERROR: nfiles = ',nfiles,' in file header: aborting'
365 return
366 endif
367
368 if (ifile==1) then
369 !--Softening lengths for Dark Matter Particles...
370 hsoft = renvironment('GSPLASH_DARKMATTER_HSOFT')
371 !
372 !--try to read dark matter and star particle smoothing lengths and/or density from a separate
373 ! one column ascii file. If only density, use this to compute smoothing lengths.
374 !
375 densfile = trim(rootname)//'.dens'
376 hfile = trim(rootname)//'.hsml'
377 hfact = 1.2 ! related to the analytic neighbour number (hfact=1.2 gives 58 neighbours in 3D)
378 open(unit=iunitd,file=densfile,iostat=ierrrho,status='old',form='formatted')
379 open(unit=iunith,file=hfile,iostat=ierrh,status='old',form='formatted')
380
381 if (idumpformat==2) then
382 if (ih==0 .and. (hsoft > tiny(hsoft) .or. ierrrho==0 .or. ierrh==0)) then
383 ncolumns = ncolumns + 1
384 blocklabelgas(ncolumns) = 'HSML'
385 ih = ncolumns
386 call set_labels_gadget
387 endif
388 if (irho==0 .and. (hsoft > tiny(hsoft) .or. ierrrho==0 .or. ierrh==0)) then
389 ncolumns = ncolumns + 1
390 blocklabelgas(ncolumns) = 'RHO '
391 irho = ncolumns
392 call set_labels_gadget
393 endif
394 endif
395
396 !
397 !--if successfully read header, increment the nstepsread counter
398 !
399 nstepsread = nstepsread + 1
400 endif
401 !
402 !--now read data
403 !
404 reallocate = .false.
405 npart_max = maxpart
406 nstep_max = max(maxstep,1)
407
408 if (ntoti > maxpart) then
409 reallocate = .true.
410 if (maxpart > 0) then
411 ! if we are reallocating, try not to do it again
412 npart_max = int(1.1*ntotall)
413 else
414 ! if first time, save on memory
415 npart_max = int(ntotall)
416 endif
417 endif
418 if (i >= maxstep .and. i /= 1) then
419 nstep_max = i + max(10,INT(0.1*nstep_max))
420 reallocate = .true.
421 endif
422 !
423 !--reallocate memory for main data array
424 !
425 if (reallocate .or. .not.(allocated(dat))) then
426 call alloc(npart_max,nstep_max,max(ncolumns+ncalc,maxcol))
427 endif
428
429 !
430 !--copy npartoftypei into allocated header arrays
431 ! and set the offset position of particle types in the main data arrays
432 !
433 if (nfiles==1 .or. ifile==1) then
434 i0(1) = 0
435 do itype=2,ntypes
436 if (nfiles==1) then
437 i0(itype) = sum(npartoftypei(1:itype-1)) ! this is avoid depending on Nall at all for single file read
438 else
439 i0(itype) = sum(Nall(1:itype-1))
440 endif
441 enddo
442 npartoftype(1:6,i) = npartoftypei(1:6)
443 else
444 i0(1) = npartoftype(1,i)
445 do itype=2,ntypes
446 i0(itype) = sum(Nall(1:itype-1)) + npartoftype(itype,i)
447 enddo
448 npartoftype(1:6,i) = npartoftype(1:6,i) + npartoftypei(1:6)
449 endif
450 if (debugmode) print*,'DEBUG: starting position for each type in data array: ',i0(:)
451 !
452 !--set time to be used in the legend
453 !
454 if (ifile==1) then
455 time(i) = real(timetemp)
456 else
457 if (abs(real(timetemp)-time(i)) > tiny(0.)) print*,'ERROR: time different between files in multiple-file read'
458 if (sum(Nall) /= ntotall) then
459 print*,' ERROR: Nall differs between files'
460 goterrors = .true.
461 endif
462 endif
463 !
464 !--read particle data
465 !
466 got_particles: if (ntoti > 0) then
467 !
468 !--read positions of all particles
469 ! (note that errors on position read are fatal)
470 !
471 call read_blockheader(idumpformat,iunit,ntoti,index2,blocklabel,lenblock,nvec,ntoti)
472 if (iformat==2) then
473 ierr = 0
474 do while(blocklabel /= 'POS ' .and. ierr == 0)
475 print "(a)",' WARNING: skipping unknown block '//blocklabel//' in data read'
476 read(iunit, iostat=ierr)
477 call read_blockheader(idumpformat,iunit,ntoti,index2,blocklabel,lenblock,nvec,ntoti)
478 enddo
479 endif
480 if (any(required(1:3))) then
481 print*,'positions ',index2
482 call allocate_temp(dattemp,3,ntoti)
483 read(iunit,iostat=ierr) (dattemp(:,j),j=1,index2)
484 if (nfiles > 1) then
485 !
486 !--read data into type order if multiple files are present:
487 ! this means the offset position is different for each type
488 !
489 if (sum(npartoftypei) /= index2) print*,' ERROR: number of positions /= sum of types'
490 n = 0
491 do itype=1,ntypes
492 do j=i0(itype)+1,i0(itype)+npartoftypei(itype)
493 n = n + 1
494 dat(j,1:3,i) = dattemp(1:3,n)
495 enddo
496 enddo
497 !read (iunit, iostat=ierr) ((dat(j,1:3,i),j=i0(itype)+1,i0(itype)+npartoftypei(itype)),itype=1,ntypes)
498 else
499 do j=1,index2
500 dat(j,1:3,i) = dattemp(1:3,j)
501 enddo
502 ! read (iunit, iostat=ierr) (dat(j,1:3,i),j=1,index2)
503 endif
504 if (ierr /= 0) then
505 print "(a)",'error encountered whilst reading positions '
506 deallocate(dattemp)
507 return
508 endif
509 else
510 read(iunit, iostat=ierr)
511 if (ierr /= 0) then
512 print "(a)",'error skipping positions '
513 return
514 endif
515 endif
516 !
517 !--same for velocities
518 !
519 call read_blockheader(idumpformat,iunit,ntoti,index2,blocklabel,lenblock,nvec,ntoti)
520 if (iformat==2 .and. blocklabel /= 'VEL ') then
521 print "(a)",' WARNING: expecting velocity, got '//blocklabel//' in data read'
522 endif
523 if (any(required(4:6))) then
524 print*,'velocities ',index2
525 call allocate_temp(dattemp,3,ntoti)
526
527 !if (.not.allocated(dattemp)) allocate(dattemp(3,ntoti))
528
529 read (iunit, iostat=ierr) (dattemp(:,j),j=1,index2)
530 if (nfiles > 1) then
531 !--see above re: type order
532 if (sum(npartoftypei) /= index2) print*,' ERROR: number of velocities /= sum of types'
533 n = 0
534 do itype=1,ntypes
535 do j=i0(itype)+1,i0(itype)+npartoftypei(itype)
536 n = n + 1
537 dat(j,4:6,i) = dattemp(1:3,n)
538 enddo
539 enddo
540 !read (iunit, iostat=ierr) ((dat(j,4:6,i),j=i0(itype)+1,i0(itype)+npartoftypei(itype)),itype=1,ntypes)
541 else
542 do j=1,index2
543 dat(j,4:6,i) = dattemp(1:3,j)
544 enddo
545 !read (iunit, iostat=ierr) (dat(j,4:6,i),j=1,index2)
546 endif
547 if (ierr /= 0) then
548 print "(a)",'error encountered whilst reading velocities'
549 goterrors = .true.
550 endif
551 else
552 read(iunit, iostat=ierr)
553 if (ierr /= 0) then
554 print "(a)",'error skipping velocities '
555 if (allocated(dattemp)) deallocate(dattemp)
556 return
557 endif
558 endif
559 !
560 !--skip read of particle ID (only required if we sort the particles
561 ! back into their correct order, which is not implemented at present)
562 ! OR if using particle ID to flag dead particles
563 !
564 ! For multiple files we only allocate and read the IDs for one file
565 !
566 if (checkids) then
567 print*,'particle ID ',ntoti
568 if (allocated(iamtemp)) deallocate(iamtemp)
569 allocate(iamtemp(ntoti))
570 endif
571
572 call read_blockheader(idumpformat,iunit,ntoti,index2,blocklabel,lenblock,nvec,ntoti)
573 if (iformat==2 .and. blocklabel /= 'ID ') then
574 print "(a)",' WARNING: expecting particle ID, got '//blocklabel//' in data read'
575 endif
576
577 if (index2 > 0) then
578 if (checkids .and. required(ih)) then
579 !--particle IDs are currently only used to set h -ve for accreted particles
580 ! so do not read if h not required
581 read (iunit,iostat=ierr) iamtemp(1:index2)
582 else
583 read (iunit,iostat=ierr) ! skip this line
584 endif
585 if (ierr /= 0) then
586 print "(a)",'error encountered whilst reading particle ID'
587 goterrors = .true.
588 endif
589 endif
590 !
591 !--read particle masses
592 !
593 !--work out total number of masses dumped
594 nmassesdumped = 0
595 do itype = 1,6
596 if (abs(massoftypei(itype)) < tiny(massoftypei)) then
597 nmassesdumped = nmassesdumped + npartoftypei(itype)
598 endif
599 enddo
600
601 if (nmassesdumped==0) then
602 masstype(1:6,i) = real(massoftypei(1:6))
603 else
604 if (required(ipmass)) then
605 print*,'particle masses ',nmassesdumped
606 !--read this number of entries
607 if (nmassesdumped > 0) then
608 if (allocated(dattemp1)) deallocate(dattemp1)
609 allocate(dattemp1(nmassesdumped))
610 call read_blockheader(idumpformat,iunit,nmassesdumped,index2,blocklabel,lenblock,nvec,-1)
611 if (iformat==2 .and. blocklabel /= 'MASS') then
612 print "(a)",' WARNING: expecting particle masses, got '//blocklabel//' in data read'
613 endif
614 else
615 index2 = 0
616 endif
617
618 if (index2 > 0) then
619 read(iunit,iostat=ierr) dattemp1(1:index2)
620 endif
621 if (ierr /= 0) then
622 print "(a)",'error reading particle masses'
623 goterrors = .true.
624 endif
625 !--now copy to the appropriate sections of the dat array
626 indexstart = 1
627 !index1 = 1
628
629 do itype=1,6
630 if (npartoftypei(itype) /= 0) then
631 !--work out the appropriate section of the dat array for this particle type
632 index1 = i0(itype) + 1
633 index2 = i0(itype) + npartoftypei(itype)
634
635 if (abs(massoftypei(itype)) < tiny(massoftypei)) then ! masses dumped
636 indexend = indexstart + npartoftypei(itype) - 1
637 if (debugmode) &
638 print*,' read ',npartoftypei(itype),' masses for '//trim(labeltype(itype))// &
639 ' particles',index1,'->',index2,indexstart,'->',indexend
640
641 dat(index1:index2,ipmass,i) = dattemp1(indexstart:indexend)
642 indexstart = indexend + 1
643 else ! masses not dumped
644 if (debugmode) print "(a,es10.3,i10,a,i10)",&
645 ' setting masses for '//trim(labeltype(itype))//' particles = ', &
646 real(massoftypei(itype)),index1,'->',index2
647
648 dat(index1:index2,ipmass,i) = real(massoftypei(itype))
649 endif
650 !index1 = index2 + 1
651 endif
652 enddo
653 if (allocated(dattemp1)) deallocate(dattemp1)
654 elseif (nmassesdumped > 0) then
655 read(iunit,iostat=ierr)
656 if (ierr /= 0) then
657 print "(a)",'error reading particle masses'
658 goterrors = .true.
659 endif
660 endif
661 endif
662 !
663 !--read other quantities for rest of particles
664 !
665 print*,'gas properties ',npartoftypei(1)
666 if (ipmass==0) then
667 istart = 7
668 else
669 istart = 8
670 endif
671 icol = istart-1
672 gas_properties: do while (icol < ncolstep) !icol=istart,ncolstep !-nextraveccols
673 !!print*,icol
674 i3 = 0
675 i4 = 0
676 ireadtype(:) = .false.
677
678 if (idumpformat==2) then
679 if (icol+1 <= ih) then
680 call read_blockheader(idumpformat,iunit,npartoftypei(1),index2,&
681 blocklabel,lenblock,nvec,npartoftypei(1))
682 else
683 call read_blockheader(idumpformat,iunit,0,index2,&
684 blocklabel,lenblock,nvec,npartoftypei(1))
685 endif
686 if (nvec > 1) iamvec(icol+1:icol+nvec) = icol + 1
687 icol = icol + nvec
688 !
689 !--work out from the number of entries what mix of particle types
690 ! the quantity is defined on
691 !
692 if (index2==ntoti) then
693 i1 = i0(1) + 1
694 i2 = i1 + ntoti - 1
695 print "(1x,a,i0,' x ',i0,a)",blocklabel//' (',nvec,index2,': all particles)'
696 ireadtype(:) = .true.
697 elseif (index2==npartoftypei(1)) then
698 i1 = i0(1) + 1
699 i2 = i1 + index2 - 1
700 print "(1x,a,i0,' x ',i0,a)",blocklabel//' (',nvec,index2,': gas particles only)'
701 ireadtype(1) = .true.
702 elseif (index2==npartoftypei(2)) then
703 i1 = i0(2) + 1
704 i2 = i1 + index2 - 1
705 print "(1x,a,i0,' x ',i0,a)",blocklabel//' (',nvec,index2,': dark matter particles only)'
706 ireadtype(2) = .true.
707 elseif (index2==npartoftypei(1)+npartoftypei(2)) then
708 i1 = i0(1) + 1
709 i2 = i1 + index2 - 1
710 print "(1x,a,i0,' x ',i0,a)",blocklabel//' (',nvec,index2,': gas+dark matter particles only)'
711 ireadtype(1:2) = .true.
712 elseif (index2==npartoftypei(5)) then
713 i1 = i0(5) + 1
714 i2 = i1 + index2 - 1
715 print "(1x,a,i0,' x ',i0,a)",blocklabel//' (',nvec,index2,': star particles only)'
716 ireadtype(5) = .true.
717 elseif (index2==npartoftypei(1)+npartoftypei(5)) then
718 i1 = i0(1) + 1
719 i2 = i1 + npartoftypei(1) - 1
720 i3 = i0(5) + 1
721 i4 = i3 + npartoftypei(5) - 1
722 print "(1x,a,i0,' x ',i0,a)",blocklabel//' (',nvec,index2,': gas+star particles only)'
723 ireadtype(1) = .true.
724 ireadtype(5) = .true.
725 else
726 print*,blocklabel//': ERROR in block length/quantity defined on unknown mix of types n = (',index2,')'
727 i1 = i0(1)+1
728 i2 = i0(1)+index2
729 !--skip in order to avoid seg fault below...
730 read (iunit,iostat=ierr)
731 cycle gas_properties
732 endif
733 else
734 nvec = 1
735 icol = icol + nvec
736 if (icol > ncolstep-nstarcols) then
737 i1 = i0(5) + 1
738 i2 = i1 + npartoftypei(5) - 1
739 print*,'star particle properties ',icol,i1,i2
740 ireadtype(5) = .true.
741 else
742 !--default is a quantity defined only on gas particles
743 i1 = i0(1) + 1
744 i2 = i1 + npartoftypei(1) - 1
745 ireadtype(1) = .true.
746 endif
747 endif
748
749 !
750 !--construct the array offsets required when reading from multiple files
751 !
752 ntypesused = 0
753 do itype=1,6
754 if (ireadtype(itype) .and. npartoftypei(itype) > 0) then
755 ntypesused = ntypesused + 1
756 i1all(ntypesused) = i0(itype) + 1
757 i2all(ntypesused) = i0(itype) + npartoftypei(itype)
758 endif
759 enddo
760
761 if (npartoftypei(1) > 0) then
762 if (required(icol)) then
763 if (i3 > 0) then
764 if (nfiles > 1) then
765 read (iunit,iostat=ierr) (dat(i1all(itype):i2all(itype),icol,i),itype=1,ntypesused)
766 else
767 read (iunit,iostat=ierr) dat(i1:i2,icol,i),dat(i3:i4,icol,i)
768 endif
769 else
770 if (nvec > 1) then
771 if (nfiles > 1) then
772 !read (iunit,iostat=ierr) (((dat(k,j,i),j=icol-nvec+1,icol),k=i1all(itype),i2all(itype)),itype=1,ntypesused)
773 call allocate_temp(dattemp,nvec,ntoti)
774 read (iunit,iostat=ierr) ((dattemp(1:nvec,j),j=i1all(itype),i2all(itype)),itype=1,ntypesused)
775 do itype=1,ntypesused
776 do j=i1all(itype),i2all(itype)
777 dat(j,icol-nvec+1:icol,i) = real(dattemp(1:nvec,j))
778 enddo
779 enddo
780 else
781 !read (iunit,iostat=ierr) ((dat(k,j,i),j=icol-nvec+1,icol),k=i1,i2)
782 call allocate_temp(dattemp,nvec,i2-i1+1)
783 read (iunit,iostat=ierr) (dattemp(1:nvec,j),j=1,i2-i1+1)
784 do j=i1,i2
785 dat(j,icol-nvec+1:icol,i) = real(dattemp(1:nvec,j))
786 enddo
787 endif
788 else
789 if (nfiles > 1) then
790 call allocate_temp1(dattemp1,ntoti)
791 read (iunit,iostat=ierr) ((dattemp1(j),j=i1all(itype),i2all(itype)),itype=1,ntypesused)
792 ! convert to real*8 if compiled in single precision
793 do itype=1,ntypesused
794 dat(i1all(itype):i2all(itype),icol,i) = real(dattemp1(i1all(itype):i2all(itype)))
795 enddo
796 else
797 call allocate_temp1(dattemp1,i2-i1+1)
798 read (iunit,iostat=ierr) dattemp1(1:1+i2-i1)
799 dat(i1:i2,icol,i) = real(dattemp1(1:1+i2-i1)) ! convert to real*8 if compiled in double prec
800 endif
801 endif
802 endif
803 else
804 read (iunit,iostat=ierr)
805 endif
806 if (ierr /= 0) then
807 print "(1x,a,i3)",'ERROR READING PARTICLE DATA from column ',icol
808 goterrors = .true.
809 endif
810 endif
811 enddo gas_properties
812 if (allocated(dattemp1)) deallocate(dattemp1)
813
814 !if (nextraveccols > 0) then
815 ! print*,'chemical species ',index2
816 ! read (iunit, iostat=ierr) (dat(j,4:6,i),j=1,index2)
817 ! if (ierr /= 0) then
818 ! print "(a)",'error encountered whilst reading velocities'
819 ! endif
820 !endif
821 !
822 !--close data file now that we have finished reading data
823 !
824 close(unit=iunit)
825
826 !
827 !--DEAL WITH ACCRETED PARTICLES (in this file only)
828 ! if particle ID is less than zero, treat this as an accreted particle
829 ! (give it a negative smoothing length)
830 !
831 if (checkids) then
832 nacc = 0
833 !--only do this if the smoothing length is required in the data read
834 if (required(ih)) then
835 n = 0
836 !do itype=1,ntypes
837 itype = 1
838 do j=1,npartoftypei(itype)
839 n = n + 1
840 if (iamtemp(n) < 0) then
841 !if (itype > 1) print*,' id -ve on non-gas particle ',itype,j
842 dat(i0(itype)+j,ih,i) = -abs(dat(i0(itype)+j,ih,i))
843 nacc = nacc + 1
844 endif
845 enddo
846 !enddo
847 if (nacc > 0) then
848 print "(a,i10,a,/,a)",' marking ',nacc,' '//trim(labeltype(1))// &
849 ' particles with negative ID as accreted/dead', &
850 ' (giving them a negative smoothing length so they will be ignored in renderings)'
851 else
852 print "(a)",' no particles with negative ID (i.e. accreted particles) found'
853 endif
854 endif
855 if (allocated(iamtemp)) deallocate(iamtemp)
856 endif
857
858 endif got_particles
859 !
860 !--now memory has been allocated, set arrays which are constant for all time
861 !
862 gamma = 5./3.
863 !
864 !--set flag to indicate that only part of this file has been read
865 !
866 if (.not.all(required(1:ncolstep))) ipartialread = .true.
867 !
868 !--for read from multiple files, work out the next file in the sequence
869 !
870 iexist = .false.
871 if (nfiles > 1 .and. ifile < nfiles) then
872 !--see if the next file exists
873 idot = index(datfile,'.',back=.true.)
874 if (idot <= 0) then
875 print "(a)",' ERROR: read from multiple files but could not determine next file in sequence'
876 goterrors = .true.
877 else
878 write(string,*) ifile
879 write(datfile,"(a,i1)") trim(datfile(1:idot))//trim(adjustl(string))
880 iexist = .false.
881 inquire(file=datfile,exist=iexist)
882 if (.not.iexist) then
883 print "(a)",' ERROR: read from multiple files '// &
884 'but could not find '//trim(datfile)//': next in sequence'
885 goterrors = .true.
886 endif
887 endif
888 endif
889
890 enddo over_files
891 !
892 !--for some reason the smoothing length output by GADGET is
893 ! twice the usual SPH smoothing length
894 ! (do this after we have read data from all of the files)
895 !
896 if (required(ih) .and. ih > 0 .and. size(dat(1,:,:)) >= ih .and. npartoftype(1,i) > 0) then
897 print "(a)",' converting GADGET smoothing length on gas particles to usual SPH definition (x 0.5)'
898 dat(1:npartoftype(1,i),ih,i) = 0.5*dat(1:npartoftype(1,i),ih,i)
899 endif
900
901 if (nfiles > 1. .and. any(npartoftype(1:6,i) /= Nall(1:6))) then
902 print*,'ERROR: sum of Npart across multiple files /= Nall in data read '
903 print*,'Npart = ',npartoftype(1:6,i)
904 print*,'Nall = ',Nall(1:6)
905 goterrors = .true.
906 endif
907 !
908 !--look for dark matter smoothing length/density files
909 !
910 if (ierrh==0 .or. ierrrho==0) then
911 if (ierrh==0) then
912 print "(a)",' READING DARK MATTER SMOOTHING LENGTHS from '//trim(hfile)
913 ierr = 0
914 index1 = npartoftype(1,i)+1
915 index2 = npartoftype(1,i)+sum(npartoftype(2:,i))
916 read(iunith,*,iostat=ierr) (dat(j,ih,i),j=index1,index2)
917 close(unit=iunith)
918 if (ierr < 0) then
919 nhset = 0
920 do j=index1,index2
921 if (dat(j,ih,i) > 0.) nhset = nhset + 1
922 enddo
923 print "(a,i10,a,/)",' *** END-OF-FILE: GOT ',nhset,' SMOOTHING LENGTHS ***'
924 elseif (ierr > 0) then
925 print "(a)", ' *** ERROR reading smoothing lengths from file'
926 goterrors = .true.
927 else
928 print "(a,i10,a)",' SMOOTHING LENGTHS READ OK for ',index2-index1+1,' dark matter / star particles '
929 endif
930 hsoft = 1.0 ! just so dark matter rendering is allowed in set_labels_gadget routine
931 endif
932
933 if (ierrrho==0) then
934 print "(a)",' READING DARK MATTER DENSITIES FROM '//trim(densfile)
935 ierr = 0
936 index1 = npartoftype(1,i)+1
937 index2 = npartoftype(1,i)+sum(npartoftype(2:,i))
938 read(iunitd,*,iostat=ierr) (dat(j,irho,i),j=index1,index2)
939 close(iunitd)
940 if (ierr < 0) then
941 nhset = 0
942 do j=index1,index2
943 if (dat(j,irho,i) > 0.) nhset = nhset + 1
944 enddo
945 print "(a,i10,a,/)",' *** END-OF-FILE: GOT ',nhset,' DENSITIES ***'
946 elseif (ierr > 0) then
947 print "(a)", ' *** ERROR reading dark matter densities from file'
948 goterrors = .true.
949 else
950 print "(a,i10,a)",' DENSITY READ OK for ',index2-index1+1,' dark matter / star particles '
951 endif
952 if (ierrh /= 0 .and. ipmass > 0) then
953 where(dat(:,irho,i) > tiny(dat))
954 dat(:,ih,i) = hfact*(dat(:,ipmass,i)/dat(:,irho,i))**(1./3.)
955 elsewhere
956 dat(:,ih,i) = 0.
957 end where
958 print "(a,i10,a,f5.2,a)", &
959 ' SMOOTHING LENGTHS SET for ',index2-index1+1,' DM/star particles using h = ',hfact,'*(m/rho)**(1/3)'
960 endif
961 hsoft = 1.0 ! just so dark matter rendering is allowed in set_labels_gadget routine
962 endif
963 else
964 !
965 !--if a value for the dark matter smoothing length is set
966 ! via the environment variable GSPLASH_DARKMATTER_HSOFT,
967 ! give dark matter particles this smoothing length
968 ! and a density of 1 (so column density plots work)
969 !
970 if (hsoft > tiny(hsoft)) then
971 if (required(ih)) then
972 print "(a,1pe10.3,a)",' ASSIGNING SMOOTHING LENGTH of h = ',hsoft, &
973 ' to dark matter particles'
974 !print*,'ih = ',ih,' npartoftype = ',npartoftype(1:2,i), shape(dat)
975 if (ih > 0) then
976 dat(npartoftype(1,i)+1:npartoftype(1,i)+npartoftype(2,i),ih,i) = hsoft
977 else
978 print*,' ERROR: smoothing length not found in data arrays'
979 goterrors = .true.
980 endif
981 endif
982 if (required(irho)) then
983 if (irho > 0) then
984 dat(npartoftype(1,i)+1:npartoftype(1,i)+npartoftype(2,i),irho,i) = 1.0
985 else
986 print*,' ERROR: place for density not found in data arrays'
987 goterrors = .true.
988 endif
989 endif
990 else
991 if (npartoftype(1,i) <= 0 .and. sum(npartoftype(:,i)) > 0) then
992 print "(66('*'),4(/,a),/)",'* NOTE!! For GADGET data using dark matter only, column density ',&
993 '* plots can be produced by setting --hsoft',&
994 '* to give a (fixed) dark matter smoothing length'
995 hsoft = (maxval(dat(:,1,i)) - minval(dat(:,1,i)))/sum(npartoftype(2:,i))**(1./3.)
996 print*,' suggested value for --hsoft= ',hsoft
997 hsoft = 0.
998
999 print "(7(/,a),/)",'* Alternatively, and for best results, calculate a number density', &
1000 '* on dark matter particles, set individual smoothing lengths from', &
1001 '* this using h = hfact*(n)**(-1/3), with hfact=1.2 and either ', &
1002 '* dump the results back into the HSML array in the original dump ', &
1003 '* file (if using the block-labelled format), or create an ascii ',&
1004 '* file called '//trim(hfile)//' containing the smoothing length ',&
1005 '* values for the dark matter particles.'
1006 print "(2(/,a),/,66('*'),/)", '* Also make sure normalised interpolations are OFF when plotting ',&
1007 '* dark matter density '
1008 endif
1009 endif
1010 endif
1011 !
1012 !--pause with fatal errors
1013 !
1014 if (goterrors .and. .not.(lenvironment('GSPLASH_IGNORE_ERRORS').or.lenvironment('GSPLASH_IGNORE-ERRORS'))) then
1015 print "(/,a)",'*** ERRORS detected during data read: data will be corrupted'
1016 print "(a,/)",' Please REPORT this and/or fix your file ***'
1017 print "(a)",' (set --ignore-errors=yes to skip this message)'
1018 if (iverbose >= 1) then
1019 print "(a)",' > Press any key to bravely proceed anyway <'
1020 read*
1021 endif
1022 endif
1023 !
1024 !--give a friendly warning about using too few or too many neighbours
1025 ! (only works with equal mass particles because otherwise we need the number density estimate)
1026 !
1027 if (ih > 0 .and. required(ih) .and. ipmass > 0 .and. required(ipmass) &
1028 .and. abs(massoftypei(1)) < tiny(0.) .and. ndim==3 .and. .not.havewarned) then
1029 nhfac = 100
1030 if (npartoftype(1,i) > nhfac) then
1031 hfactmean = 0.
1032 do j=1,nhfac
1033 hfact = dat(j,ih,i)*(dat(j,irho,i)/(dat(j,ipmass,i)))**(1./ndim)
1034 hfactmean = hfactmean + hfact
1035 enddo
1036 hfact = hfactmean/real(nhfac)
1037 havewarned = .true.
1038 if (hfact < 1.125 .or. hfact > 1.45) then
1039 print "(/,a)",'** FRIENDLY NEIGHBOUR WARNING! **'
1040 print "(3x,a,f5.1,a,/,3x,a,f5.2,a,i1,a)", &
1041 'It looks like you are using around ',4./3.*pi*(2.*hfact)**3,' neighbours,', &
1042 'corresponding to h = ',hfact,'*(m/rho)^(1/',ndim,') in 3D:'
1043
1044 if (hfact < 1.15) then
1045 print "(4(/,3x,a))",'This is a quite a low number of neighbours for the cubic spline and ', &
1046 'may result in increased noise and inaccurate wave propagation speeds', &
1047 '(a cubic lattice is also an unstable initial configuration for the ',&
1048 ' particles in this regime -- see Morris 1996, Borve et al. 2004).'
1049 elseif (hfact > 1.45) then
1050 print "(4(/,3x,a))",'Using h >~ 1.5*(m/rho)^(1/3) with the cubic spline results in the', &
1051 'particle pairing instability due to the first neighbour being placed under', &
1052 'the hump in the kernel gradient. Whilst not fatal, it results in a', &
1053 'loss of resolution so is a bit of a waste of cpu time.'
1054 print "(4(/,3x,a))",'If you are attempting to perform a "resolution study" by increasing the', &
1055 'neighbour number, this is a *bad idea*, as you are also increasing h.', &
1056 '(a better way is to increase the smoothness of the integrals without changing h', &
1057 ' by adopting a smoother kernel such as the M5 Quintic that goes to 3h).'
1058 endif
1059 print "(/,3x,a,/,3x,a,/)", &
1060 'A good default range is h = 1.2-1.3 (m/rho)^1/ndim ', &
1061 'corresponding to around 58-75 neighbours in 3D.'
1062 else
1063 print "(/,1x,a,f5.1,a,/,1x,a,f5.2,a,i1,a,/)", &
1064 'Simulations employ ',4./3.*pi*(2.*hfact)**3,' neighbours,', &
1065 'corresponding to h = ',hfact,'*(m/rho)^(1/',ndim,') in 3D'
1066 endif
1067 endif
1068 else
1069 !print*,'not true'
1070 endif
1071 !
1072 !--cover the special case where no particles have been read
1073 !
1074 if (ntotall <= 0) then
1075 npartoftype(1,i) = 1
1076 dat(:,:,i) = 0.
1077 endif
1078
1079 if (nstepsread > 0) then
1080 print*,'>> last step ntot =',sum(npartoftype(:,istepstart+nstepsread-1))
1081 endif
1082 return
1083
1084 contains
1085
1086 !!-----------------------------------------------------------------
1087 !! small utility to transparently handle block labelled data read
1088 !!-----------------------------------------------------------------
1089 subroutine read_blockheader(idumpfmt,lun,nexpected,ndumped,blklabel,lenblk,nvec,ngas)
1090 integer, intent(in) :: idumpfmt,lun,nexpected
1091 integer, intent(out) :: ndumped
1092 character(len=4), intent(out) :: blklabel
1093 integer, intent(out) :: lenblk
1094 integer, intent(out) :: nvec
1095 integer, intent(in) :: ngas
1096
1097 blklabel = ' '
1098 if (idumpfmt==2) then
1099 read(lun, iostat=ierr) blklabel,lenblk
1100 if (ierr /= 0) then
1101 ndumped = 0
1102 return
1103 endif
1104 if (blklabel=='POS ' .OR. blklabel=='VEL ' .OR. blklabel=='ACCE' .OR. blklabel=='BFLD' .OR. &
1105 blklabel=='BPOL' .OR. blklabel=='BTOR' .or. (lenblk-8)/12==ngas) then
1106 ndumped = (lenblk-8)/12
1107 nvec = 3
1108 else
1109 ndumped = (lenblk-8)/4
1110 nvec = 1
1111 endif
1112 !print*,blklabel,lenblk,ndumped,nexpected
1113 !if (nexpected > 0) then
1114 ! if (ndumped /= nexpected) then
1115 ! print*,'warning: number of '//blklabel//' dumped (',ndumped,') /= expected (',nexpected,')'
1116 ! endif
1117 !endif
1118 else
1119 ndumped = nexpected
1120 endif
1121
1122 end subroutine read_blockheader
1123
1124 end subroutine read_data_gadget
1125
1126 !------------------------------------------------------------
1127 ! allocate temporary memory, but avoid reallocating
1128 ! memory if size is the same
1129 !------------------------------------------------------------
1130 subroutine allocate_temp(dattemp,isize,jsize)
1131 use params, only:sing_prec
1132 real(kind=sing_prec), allocatable, intent(inout) :: dattemp(:,:)
1133 integer, intent(in) :: isize,jsize
1134
1135 if (allocated(dattemp)) then
1136 if (size(dattemp(:,1)) /= isize .or. size(dattemp(1,:)) /= jsize) then
1137 deallocate(dattemp)
1138 endif
1139 endif
1140 if (.not.allocated(dattemp)) then
1141 allocate(dattemp(isize,jsize))
1142 endif
1143
1144 end subroutine allocate_temp
1145
1146 !------------------------------------------------------------
1147 ! allocate temporary memory, but avoid reallocating
1148 ! memory if size is the same
1149 !------------------------------------------------------------
1150 subroutine allocate_temp1(dattemp1,jsize)
1151 use params, only:sing_prec
1152 real(kind=sing_prec), allocatable, intent(inout) :: dattemp1(:)
1153 integer, intent(in) :: jsize
1154
1155 if (allocated(dattemp1)) then
1156 if (size(dattemp1(:)) /= jsize) then
1157 deallocate(dattemp1)
1158 endif
1159 endif
1160 if (.not.allocated(dattemp1)) then
1161 allocate(dattemp1(jsize))
1162 endif
1163
1164 end subroutine allocate_temp1
1165
1166 !!------------------------------------------------------------
1167 !! set labels for each column of data
1168 !!------------------------------------------------------------
1169
1170 subroutine set_labels_gadget
1171 use labels, only:label,iamvec,labelvec,labeltype,ix,ivx,ipmass,lenlabel,set_vector_labels, &
1172 ih,irho,ipr,iutherm,iBfirst,iBpol,iBtor,idivB,iax,make_vector_label
1173 use params
1174 use settings_data, only:ndim,ndimV,ncolumns,ntypes,UseTypeInRenderings,iformat
1175 use geometry, only:labelcoord
1176 use system_utils, only:envlist,ienvironment
1177 use asciiutils, only:lcase
1178 integer :: i,nextracols,nstarcols,icol,ihset
1179 character(len=30), dimension(10) :: labelextra
1180 character(len=lenlabel) :: tmplabel
1181
1182 if (ndim <= 0 .or. ndim > 3) then
1183 print*,'*** ERROR: ndim = ',ndim,' in set_labels_gadget ***'
1184 return
1185 endif
1186 if (ndimV <= 0 .or. ndimV > 3) then
1187 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_gadget ***'
1188 return
1189 endif
1190
1191 if (iformat==2) then
1192 icol = 0
1193 do i=1,size(blocklabelgas)
1194 icol = icol + 1
1195 select case(trim(blocklabelgas(i)))
1196 case('POS')
1197 ix(1) = icol
1198 ix(2) = icol+1
1199 ix(3) = icol+2
1200 case('VEL')
1201 ivx = icol
1202 case('ACCE')
1203 iax = icol
1204 case('BFLD')
1205 iBfirst = icol
1206 case('BPOL')
1207 iBpol = icol
1208 case('BTOR')
1209 iBtor = icol
1210 case('MASS')
1211 ipmass = icol
1212 case('U')
1213 iutherm = icol
1214 case('RHO')
1215 irho = icol
1216 case('NE')
1217 label(icol) = 'N_{e}'
1218 case('NH')
1219 label(icol) = 'N_{H}'
1220 case('HSML')
1221 ih = icol
1222 case('NHP')
1223 label(icol) = 'N_{H+}'
1224 case('NHE')
1225 label(icol) = 'N_{He}'
1226 case('NHEP')
1227 label(icol) = 'N_{He+}'
1228 case('elec')
1229 label(icol) = 'N_{e}'
1230 case('HI')
1231 label(icol) = 'HI'
1232 case('HII')
1233 label(icol) = 'HII'
1234 case('HeI')
1235 label(icol) = 'HeI'
1236 case('HeII')
1237 label(icol) = 'HeII'
1238 case('H2I')
1239 label(icol) = 'H_{2}I'
1240 case('H2II')
1241 label(icol) = 'H_{2}II'
1242 case('HM')
1243 label(icol) = 'HM'
1244 case('HD')
1245 label(icol) = 'HD'
1246 case('DI')
1247 label(icol) = 'DI'
1248 case('DII')
1249 label(icol) = 'DII'
1250 case('HeHp')
1251 label(icol) = 'He Hp'
1252 case('SFR')
1253 label(icol) = 'Star Formation Rate'
1254 case('AGE')
1255 label(icol) = 'Stellar Formation Time'
1256 case('DETI')
1257 label(icol) = 'Delay Time'
1258 case('HSMS')
1259 label(icol) = 'Stellar Smoothing Length'
1260 case('ACRS')
1261 label(icol) = 'Stellar Spreading Length'
1262 case('Z')
1263 label(icol) = 'Metallicity'
1264 case('POT')
1265 label(icol) = 'Potential'
1266 case('IPOT')
1267 label(icol) = 'Integrated Potential'
1268 case('PHID')
1269 label(icol) = 'DPotential Dt'
1270 case('ENDT')
1271 label(icol) = 'Rate Of Change Of Entropy'
1272 case('STRD')
1273 label(icol) = 'Diagonal Stress Tensor'
1274 case('STRO')
1275 label(icol) = 'Off Diagonal Stress Tensor'
1276 case('STRB')
1277 label(icol) = 'Bulk Stress Tensor'
1278 case('SHCO')
1279 label(icol) = 'Shear Coefficient'
1280 case('TSTP')
1281 label(icol) = 'Time Step'
1282 case('BFSM')
1283 label(icol) = 'Smoothed Magnetic Field'
1284 case('DBDT')
1285 label(icol) = 'dB/dt'
1286 case('VBLK')
1287 label(icol) = 'Bulk Velocity'
1288 case('VRMS')
1289 label(icol) = 'RMSVelocity'
1290 case('VTAN')
1291 label(icol) = 'RMSTangential Velocity'
1292 case('VRAD')
1293 label(icol) = 'RMSRadial Velocity'
1294 case('MHIX')
1295 label(icol) = 'Main Halo Index'
1296 case('TNGB')
1297 label(icol) = 'True Number Of Neighbours'
1298 case('NGB')
1299 label(icol) = 'True Number Of Neighbours'
1300 case('DPP')
1301 label(icol) = 'Magnetos Reacc Coefficient'
1302 case('VDIV')
1303 label(icol) = 'Velocity Divergence'
1304 case('VROT')
1305 label(icol) = 'Velocity Curl'
1306 case('VORT')
1307 label(icol) = 'Vorticity'
1308 case('DIVB')
1309 label(icol) = 'div B'
1310 idivB = icol
1311 case('RDIB')
1312 label(icol) = 'h|div B|/|B|'
1313 case('ABVC')
1314 label(icol) = 'alpha_{visc}'
1315 case('ACVC')
1316 label(icol) = 'alpha_{cond}'
1317 case('AMDC')
1318 label(icol) = 'alpha_{resist}'
1319 case('VTRB')
1320 label(icol) = 'Turb Velociy'
1321 case('LTRB')
1322 label(icol) = 'Turb Length'
1323 case('ADYN')
1324 label(icol) = 'Alpha Dynamo'
1325 case('EDYN')
1326 label(icol) = 'Eta Dynamo'
1327 case('PHI')
1328 label(icol) = 'DPotential Dt'
1329 case('XPHI')
1330 label(icol) = 'Cold Gas Fraction (\Phi)'
1331 case('GPHI')
1332 label(icol) = 'Div B cleaning Function Grad Phi'
1333 case('ROTB')
1334 label(icol) = 'Rotation B'
1335 case('SRTB')
1336 label(icol) = 'Smoothed Rotation B'
1337 case('EULA')
1338 label(icol) = 'Euler Potential A'
1339 case('EULB')
1340 label(icol) = 'Euler Potential B'
1341 case('COOR')
1342 label(icol) = 'Cooling Rate'
1343 case('CONR')
1344 label(icol) = 'Conduction Rate'
1345 case('DENN')
1346 label(icol) = 'Number density'
1347 case('EGYP')
1348 label(icol) = 'Energy Reservoir For Feedback'
1349 case('EGYC')
1350 label(icol) = 'Energy Reservoir For Cold Phase'
1351 case('CRC0')
1352 label(icol) = 'Cosmic Ray C0'
1353 case('CRQ0')
1354 label(icol) = 'Cosmic Ray q0'
1355 case('CRP0')
1356 label(icol) = 'Cosmic Ray P0'
1357 case('CRE0')
1358 label(icol) = 'Cosmic Ray E0'
1359 case('CRn0')
1360 label(icol) = 'Cosmic Ray n0'
1361 case('CRco')
1362 label(icol) = 'Cosmic Ray Thermalization Time'
1363 case('CRdi')
1364 label(icol) = 'Cosmic Ray Dissipation Time'
1365 case('BHMA')
1366 label(icol) = 'Black hole mass'
1367 case('ACRB')
1368 label(icol) = 'Black hole Accretion Length'
1369 case('BHMD')
1370 label(icol) = 'Black hole Mdot'
1371 case('BHPC')
1372 label(icol) = 'Black hole NProgs'
1373 case('BHMB')
1374 label(icol) = 'Black hole Mass bubbles'
1375 case('BHMI')
1376 label(icol) = 'Black hole Mass initial'
1377 case('BHMR')
1378 label(icol) = 'Black hole Mass radio'
1379 case('VSIG')
1380 label(icol) = 'Maximum Signal Velocity'
1381 case('MACH')
1382 label(icol) = 'Mach Number'
1383 case('SHSP')
1384 label(icol) = 'Shock Speed'
1385 case('SHRH')
1386 label(icol) = 'Shock Upstr Density'
1387 case('SHPU')
1388 label(icol) = 'Shock Upstr Pressure'
1389 case('SHPD')
1390 label(icol) = 'Shock Downstr Pressure'
1391 case('SHVU')
1392 label(icol) = 'Shock Upstr Velocity'
1393 case('SHVD')
1394 label(icol) = 'Shock Downstr Velocity'
1395 case('MALF')
1396 label(icol) = 'Alfven Mach Number'
1397 case('SHAU')
1398 label(icol) = 'Shock Upstr Alfven'
1399 case('SHAD')
1400 label(icol) = 'Shock Downstr Alfven'
1401 case('SHOB')
1402 label(icol) = 'Shock Obliquity'
1403 case('SHCP')
1404 label(icol) = 'Shock Compression Ratio'
1405 case('SHNR')
1406 label(icol) = 'Shock Normal'
1407 case('DTEG')
1408 label(icol) = 'Dt Energy'
1409 case('PSCS')
1410 label(icol) = 'Preshock Sound Speed'
1411 case('PSDE')
1412 label(icol) = 'Preshock Density'
1413 case('PSEN')
1414 label(icol) = 'Preshock Energy'
1415 case('PSXC')
1416 label(icol) = 'Preshock XCR'
1417 case('DJMP')
1418 label(icol) = 'Density Jump'
1419 case('EJMP')
1420 label(icol) = 'Energy Jump'
1421 case('CRDE')
1422 label(icol) = 'CR_Dt E'
1423 case('TIPS')
1424 label(icol) = 'Tidal Tensor PS'
1425 case('DIPS')
1426 label(icol) = 'Distortion Tensor PS'
1427 case('CACO')
1428 label(icol) = 'Caustic Counter'
1429 case('FLDE')
1430 label(icol) = 'Flow Determinant'
1431 case('STDE')
1432 label(icol) = 'Stream Density'
1433 case('SOMA')
1434 label(icol) = '2lpt mass'
1435 case('ANRA')
1436 label(icol) = 'Annihilation Radiation'
1437 case('LACA')
1438 label(icol) = 'Last Caustic'
1439 case('SHOR')
1440 label(icol) = 'Sheet Orientation'
1441 case('INDE')
1442 label(icol) = 'Init Density'
1443 case('TEMP')
1444 label(icol) = 'Temperature'
1445 case('XNUC')
1446 label(icol) = 'Nuclear mass fractions'
1447 case('P')
1448 label(icol) = 'Coordinates'
1449 case('RADG')
1450 label(icol) = 'photon number density'
1451 case('RADA')
1452 label(icol) = 'rad acceleration'
1453 case('ET')
1454 label(icol) = 'Delay Time'
1455 case('PTSU')
1456 label(icol) = 'PSum'
1457 case('DMNB')
1458 label(icol) = 'Dark matter number of neighbours'
1459 case('NTSC')
1460 label(icol) = 'Num Total Scatter'
1461 case('SHSM')
1462 label(icol) = 'SIDM smoothing length'
1463 case('SRHO')
1464 label(icol) = 'SIDM density'
1465 case('SVEL')
1466 label(icol) = 'SVel Disp'
1467 case('DMHS')
1468 label(icol) = 'SIDM smoothing length'
1469 case('DMDE')
1470 label(icol) = 'SIDM density'
1471 case('DMVD')
1472 label(icol) = 'DM Velocity Dispersion'
1473 case('Zs')
1474 label(icol) = 'Mass of Metals'
1475 case('CII')
1476 label(icol) = 'Contribution by SNII'
1477 case('CIa')
1478 label(icol) = 'Contribution by SNIa'
1479 case('CAGB')
1480 label(icol) = 'Contribution by AGB'
1481 case('ZAge')
1482 label(icol) = 'Metallicity-averaged time'
1483 case('ZAlv')
1484 label(icol) = 'long-living-Metallicity-averaged time'
1485 case('iM')
1486 label(icol) = 'SSPInitial Mass'
1487 case('CLDX')
1488 label(icol) = 'Cloud Fraction'
1489 case('HOTT')
1490 label(icol) = 'Hot Phase Temperature'
1491 case('TRCK')
1492 label(icol) = 'Track Contributes'
1493 case('ZsMT')
1494 label(icol) = 'smoothed metallicity'
1495 case('ZSMT')
1496 label(icol) = 'smoothed metallicities (all elements)'
1497 case('MHOT')
1498 label(icol) = 'Hot mass'
1499 case('MCLD')
1500 label(icol) = 'Cold mass'
1501 case('MMOL')
1502 label(icol) = 'Molecular mass'
1503 case('EHOT')
1504 label(icol) = 'Hot energy'
1505 case('MSF')
1506 label(icol) = 'Star formation mass'
1507 case('MFST')
1508 label(icol) = 'Multi Phase Steps'
1509 case('NMF')
1510 label(icol) = 'Num Times In MF'
1511 case('EOUT')
1512 label(icol) = 'Energy To Other'
1513 case('CLCK')
1514 label(icol) = 'Multi Phase Clock'
1515 case('Egy0')
1516 label(icol) = 'Multi Phase Energy Tot0'
1517 case('TDYN')
1518 label(icol) = 'Cold Phase TDyn'
1519 case('EREC')
1520 label(icol) = 'Energy From Other'
1521 case('GRAD')
1522 label(icol) = 'Div B cleaning grad Psi'
1523 case('TCOO')
1524 label(icol) = 'Cooling Time'
1525 case('EKRC')
1526 label(icol) = 'Kinetic Energy From Last Snap'
1527 case('TSMP')
1528 label(icol) = 't_start MP'
1529 case('CRpN')
1530 label(icol) = 'LMBCRp Normalization'
1531 case('CReN')
1532 label(icol) = 'LMBCRp Normalization'
1533 case('CRpS')
1534 label(icol) = 'LMBCRp Slope'
1535 case('CReS')
1536 label(icol) = 'LMBCRe Slope'
1537 case('CRpC')
1538 label(icol) = 'LMBCRp Cut'
1539 case('CReC')
1540 label(icol) = 'LMBCRe Cut'
1541 case('CRpP')
1542 label(icol) = 'LMBCRp Pressure'
1543 case('CReP')
1544 label(icol) = 'LMBCRe Pressure'
1545 case('RHOO')
1546 label(icol) = 'Density Old'
1547 case('SYNE')
1548 label(icol) = 'LMBCRe Synchrotron'
1549 case('AGSH')
1550 label(icol) = 'AGS Softening'
1551 case('AGSD')
1552 label(icol) = 'AGS Density'
1553 case('AGSZ')
1554 label(icol) = 'AGS Zeta'
1555 case('AGSO')
1556 label(icol) = 'AGS Omega'
1557 case('AGSC')
1558 label(icol) = 'AGS Correction'
1559 case('AGSN')
1560 label(icol) = 'AGS Neighbours'
1561 case('MGPH')
1562 label(icol) = 'Modified Gravity Phi'
1563 case('MGGP')
1564 label(icol) = 'Modified Gravity Grad Phi'
1565 case('MGAC')
1566 label(icol) = 'Modified Gravity Acceleration'
1567 case('GRDU')
1568 label(icol) = 'Internal Energy Gradient'
1569 case('GRDP')
1570 label(icol) = 'Pressure Gradient'
1571 case('MGVX')
1572 label(icol) = 'Gradient of x-velocity for MFM'
1573 case('MGVY')
1574 label(icol) = 'Gradient of y-velocity for MFM'
1575 case('MGVZ')
1576 label(icol) = 'Gradient of z-velocity for MFM'
1577 case('MGRH')
1578 label(icol) = 'Gradient of density for MFM'
1579 case('MGU')
1580 label(icol) = 'Gradient of specific internal energy for MFM'
1581 case('QP')
1582 label(icol) = 'Quantum Pressure'
1583 case('QDP')
1584 label(icol) = 'Quantum Pressure Gradient'
1585 case('RHOS')
1586 label(icol) = 'Stellar Density Around Stars'
1587 case('SMTS')
1588 label(icol) = 'Smoothing Length for Stellar Density'
1589 case('NEIS')
1590 label(icol) = 'Number Of Neighbours for Stellar Density'
1591 case('ID ')
1592 icol = icol - 1
1593 case default
1594 label(icol) = trim(lcase(blocklabelgas(i)))
1595 end select
1596 enddo
1597 else
1598 do i=1,ndim
1599 ix(i) = i
1600 enddo
1601 ivx = 4
1602 ipmass = 7
1603 irho = 9 ! location of rho in data array
1604 ipr = 0
1605 iutherm = 8 ! thermal energy
1606 if (iformat==1 .or. iformat==11 .and. ncolumns > 10) then
1607 label(10) = 'Ne'
1608 label(11) = 'Nh'
1609 ih = 12 ! smoothing length
1610 if (iformat==11) label(13) = 'Star formation rate'
1611 else
1612 ih = 10
1613 if (iformat==1) label(11) = 'Star formation rate'
1614 endif
1615 ihset = ienvironment('GSPLASH_HCOLUMN',errval=-1)
1616 if (ihset > 0) ih = ihset
1617 !
1618 !--deal with extra columns
1619 !
1620 if (ncolumns > ih) then
1621 call envlist('GSPLASH_EXTRACOLS',nextracols,labelextra)
1622 do i=ih+1,ih+nextracols
1623 label(i) = trim(labelextra(i-ih))
1624 enddo
1625 call envlist('GSPLASH_STARPARTCOLS',nstarcols,labelextra)
1626 do i=ih+nextracols+1,ih+nextracols+nstarcols
1627 label(i) = trim(labelextra(i-ih-nextracols))
1628 enddo
1629 endif
1630 endif
1631 !
1632 !--set labels of the quantities read in
1633 !
1634 if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1)
1635 if (irho > 0) label(irho) = 'density'
1636 if (iutherm > 0) label(iutherm) = 'u'
1637 if (ipmass > 0) label(ipmass) = 'particle mass'
1638 if (ih > 0) label(ih) = 'h'
1639
1640 call set_vector_labels(ncolumns,ndimV,iamvec,labelvec,label,labelcoord(:,1))
1641
1642 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
1643 call make_vector_label('a',iax,ndimV,iamvec,labelvec,label,labelcoord(:,1))
1644 call make_vector_label('B',iBfirst,ndimV,iamvec,labelvec,label,labelcoord(:,1))
1645 call make_vector_label('B_{pol}',iBpol,ndimV,iamvec,labelvec,label,labelcoord(:,1))
1646 call make_vector_label('B_{tor}',iBtor,ndimV,iamvec,labelvec,label,labelcoord(:,1))
1647
1648 !--set labels for each particle type
1649 !
1650 ntypes = 6
1651 labeltype(1) = 'gas'
1652 labeltype(2) = 'dark matter'
1653 labeltype(3) = 'boundary 1'
1654 labeltype(4) = 'boundary 2'
1655 labeltype(5) = 'star'
1656 labeltype(6) = 'sink / black hole'
1657 UseTypeInRenderings(1) = .true.
1658 !
1659 !--dark matter particles are of non-SPH type (ie. cannot be used in renderings)
1660 ! unless they have had a smoothing length defined
1661 !
1662 if (hsoft > tiny(hsoft)) then
1663 UseTypeInRenderings(2) = .true.
1664 else
1665 UseTypeInRenderings(2) = .false.
1666 endif
1667 UseTypeInRenderings(3:6) = .false.
1668
1669 end subroutine set_labels_gadget
1670
1671 !-----------------------------------------------------------
1672 !
1673 ! check if a file is in phantom/sphNG format
1674 !
1675 !-----------------------------------------------------------
1676 logical function file_format_is_gadget(filename) result(is_gadget)
1677 use params, only:doub_prec
1678 character(len=*), intent(in) :: filename
1679 integer :: iunit,ierr,lenblock
1680 real(doub_prec) :: time,z
1681 real(doub_prec) :: massoftypei(6)
1682 character(len=4) :: blocklabel
1683 integer :: noftype(6),Nall(6),iFlagSfr,iFlagFeedback,iFlagcool,nfiles
1684
1685 is_gadget = .false.
1686 !
1687 ! open file and read the first line
1688 !
1689 open(newunit=iunit,iostat=ierr,file=filename,status='old',form='unformatted')
1690 if (ierr /= 0) return
1691 !
1692 ! attempt to read the first line of the header
1693 !
1694 noftype(:) = -1
1695 massoftypei(:) = -1.
1696 time = -1.
1697 z = -1.
1698 iFlagSfr = -1
1699
1700 read(iunit,iostat=ierr) noftype(1:6),massoftypei(1:6),time,z,iFlagSfr,&
1701 iFlagFeedback,Nall(1:6),iFlagCool,nfiles
1702 !print*,' got ',ierr,noftype(1:6),massoftypei(1:6),time,z,iFlagSfr, &
1703 ! iFlagFeedback,Nall(1:6),iFlagCool,nfiles
1704
1705 if (ierr==0 .and. all(noftype(1:6) >= 0) .and. all(massoftypei >= 0.) &
1706 .and. time >= 0. .and. iFlagSfr >= 0 .and. iFlagCool >= 0 &
1707 .and. iFlagFeedback >= 0 .and. all(nall(1:6) >= 0) .and. nfiles >= 0 &
1708 .and. nfiles <= 1e6) is_gadget = .true.
1709
1710 if (.not.is_gadget) then
1711 rewind(iunit)
1712 ! try block labelled gadget format
1713 read(iunit,iostat=ierr) blocklabel,lenblock
1714 if (ierr == 0 .and. lenblock == 264) then
1715 auto_detected_block_format = .true.
1716 is_gadget = .true.
1717 endif
1718 endif
1719
1720 close(iunit) ! close the file
1721
1722 end function file_format_is_gadget
1723
1724 end module readdata_gadget