Debian Codegraph @

src/read_data_gadget_hdf5.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 HDF5 OUTPUT FROM THE GADGET CODE
28 !
29 ! SOME CHOICES FOR THIS FORMAT CAN BE SET USING THE FOLLOWING
30 ! ENVIRONMENT VARIABLES:
31 !
32 ! GSPLASH_USE_Z if 'YES' uses redshift in the legend instead of time
33 ! GSPLASH_USE_IDS if 'YES' resorts particles according to their ParticleIDs
34 ! GSPLASH_DARKMATTER_HSOFT if given a value > 0.0 will assign a
35 ! smoothing length to dark matter particles which can then be
36 ! used in the rendering
37 !
38 ! the data is stored in the global array dat
39 !
40 ! >> this subroutine must return values for the following: <<
41 !
42 ! ncolumns : number of data columns
43 ! ndim, ndimV : number of spatial, velocity dimensions
44 ! nstepsread : number of steps read from this file
45 !
46 ! dat(maxpart,maxplot,maxstep) : main data array
47 !
48 ! npartoftype(maxstep): number of particles of each type in each timestep
49 !
50 ! time(maxstep) : time at each step
51 ! gamma(maxstep) : gamma at each step
52 ! (used in calc_quantities for calculating the pressure)
53 !
54 ! most of these values are stored in global arrays
55 ! in the module 'particle_data'
56 !
57 ! Partial data read implemented Nov 2006 means that columns with
58 ! the 'required' flag set to false are not read (read is therefore much faster)
59 !-------------------------------------------------------------------------
60 !
61 ! The module below contains interface routines to c functions
62 ! that perform the actual calls to the HDF5 libs
63 !
64 !-------------------------------------------------------------------------
65 module readdata_gadget_hdf5
66 use params, only:maxplot,doub_prec
67 use labels, only:lenlabel
68 use, intrinsic :: iso_c_binding, only:c_int,c_double,c_char
69 implicit none
70 real :: hsoft
71 character(len=lenlabel), dimension(maxplot) :: blocklabelgas
72 integer, dimension(maxplot) :: blocksize
73 logical :: havewarned = .false.
74 integer, parameter :: maxtypes = 6
75 logical :: useids = .false.
76 logical :: arepo = .false.
77
78 public :: read_data_gadget_hdf5, set_labels_gadget_hdf5
79
80 interface
81 subroutine read_gadget_hdf5_header(filename,maxtypes,maxhdr,npartoftypei,massoftypei,&
82 timeh,zh,headervals,iFlagSfr,iFlagFeedback,Nall,iFlagCool, &
83 igotids,ndim,ndimV,nfiles,ncol,ierr) bind(c)
84 import
85 character(kind=c_char), dimension(*), intent(in) :: filename
86 integer(kind=c_int), intent(in), value :: maxtypes,maxhdr
87 integer(kind=c_int), intent(out) :: iFlagSfr,iFlagFeedback,iFlagCool,igotids
88 integer(kind=c_int), dimension(6), intent(out) :: npartoftypei,Nall
89 real(kind=c_double), dimension(6), intent(out) :: massoftypei
90 real(kind=c_double), intent(out) :: timeh,zh
91 real(kind=c_double), dimension(maxhdr), intent(out) :: headervals
92 integer(kind=c_int), intent(out) :: ndim,ndimV,nfiles,ncol,ierr
93 end subroutine read_gadget_hdf5_header
94
95 subroutine read_gadget_hdf5_data(filename,maxtypes,npartoftypei,&
96 ncol,isrequired,i0,ierr) bind(c)
97 import
98 character(kind=c_char), dimension(*), intent(in) :: filename
99 integer(kind=c_int), intent(in), value :: maxtypes
100 integer(kind=c_int), dimension(6), intent(in) :: npartoftypei
101 integer(kind=c_int), intent(in), value :: ncol
102 integer(kind=c_int), intent(out) :: ierr
103 integer(kind=c_int), dimension(ncol), intent(in) :: isrequired
104 integer(kind=c_int), dimension(maxtypes), intent(in) :: i0
105 end subroutine read_gadget_hdf5_data
106 end interface
107
108 contains
109 !---------------------------------------------------------------------------
110 !
111 ! function to safely convert a string from c format (ie. with a terminating
112 ! ascii null character) back to a normal Fortran string
113 !
114 !---------------------------------------------------------------------------
115 function fstring(array)
116 character(kind=c_char), dimension(:), intent(in) :: array
117 character(len=size(array)-1) :: fstring
118 integer :: i
119
120 fstring = ''
121 do i=1,size(array)
122 if (array(i)==achar(0)) exit
123 fstring(i:i) = array(i)
124 enddo
125
126 end function fstring
127
128 !---------------------------------------------------------------------------
129 !
130 ! function to reformat the HDF5 label into the splash column label
131 ! by inserting a space whereever a capital letter occurs
132 !
133 !---------------------------------------------------------------------------
134 function reformatlabel(label)
135 character(len=*), intent(in) :: label
136 character(len=2*len(label)) :: reformatlabel
137 integer :: is,ia,ib,ip
138
139 reformatlabel = label
140 ip = 1
141 do is = 2, len_trim(label)
142 ip = ip + 1
143 ia = iachar(reformatlabel(ip:ip))
144 ib = iachar(reformatlabel(ip-1:ip-1))
145 if ((ia >= iachar('A').and.ia <= iachar('Z')) .and. .not. &
146 (ib >= iachar('A').and.ib <= iachar('Z'))) then
147 reformatlabel = reformatlabel(1:ip-1)//' '//reformatlabel(ip:)
148 ip = ip + 1
149 endif
150 enddo
151
152 end function reformatlabel
153
154 !-------------------------------------------------------------------------
155 !
156 ! The routine that reads the data into splash's internal arrays
157 !
158 !-------------------------------------------------------------------------
159 subroutine read_data_gadget_hdf5(rootname,istepstart,ipos,nstepsread)
160 use particle_data, only:dat,npartoftype,masstype,time,gamma,headervals,maxpart,maxcol,maxstep
161 use params, only:doub_prec,maxparttypes,maxplot,maxhdr
162 use settings_data, only:ndim,ndimV,ncolumns,ncalc,required,ipartialread, &
163 ntypes,debugmode,iverbose
164 use mem_allocation, only:alloc
165 use labels, only:ih,irho,ipmass
166 use system_utils, only:renvironment,lenvironment,ienvironment,envlist
167 use asciiutils, only:cstring
168 integer, intent(in) :: istepstart,ipos
169 integer, intent(out) :: nstepsread
170 character(len=*), intent(in) :: rootname
171 character(len=len(rootname)+10) :: datfile,densfile,hfile
172 character(len=20) :: string
173 integer, dimension(maxparttypes) :: npartoftypei,Nall
174 integer :: i,j,itype,ierr,ierrh,ierrrho,nhset,ifile
175 integer :: index1,index2
176 integer :: ncolstep,npart_max,nstep_max,ntoti,ntotall,idot
177 integer :: iFlagSfr,iFlagFeedback,iFlagCool,igotids,nfiles,nhfac
178 integer, dimension(6) :: i0
179 integer, parameter :: iunit = 11, iunitd = 102, iunith = 103
180 logical :: iexist,reallocate,debug,goterrors,compute_h_from_rho_m
181 real(doub_prec) :: timetemp,ztemp,headervalstmp(maxhdr)
182 real(doub_prec), dimension(6) :: massoftypei
183 real :: hfact,hfactmean,pmassi
184 real, parameter :: pi = 4.*atan(1.)
185 integer, dimension(maxplot) :: isrequired
186
187 nstepsread = 0
188 goterrors = .false.
189 compute_h_froM_rho_m = .false.
190 if (maxparttypes < 6) then
191 print*,' *** ERROR: not enough particle types for GADGET data read ***'
192 print*,' *** you need to edit splash parameters and recompile ***'
193 stop
194 endif
195
196 if (len_trim(rootname) > 0) then
197 datfile = trim(rootname)
198 else
199 print*,' **** no data read **** '
200 return
201 endif
202 !
203 !--check if first data file exists
204 !
205 useids = lenvironment('GSPLASH_USEIDS') .or. lenvironment('GSPLASH_CHECKIDS')
206 if (useids) then
207 print "(1x,a)",'reading GADGET HDF5 format: sorted by particle id (--useids)'
208 else
209 print "(1x,a)",'reading GADGET HDF5 format: use --useids to sort by particle id'
210 endif
211 inquire(file=datfile,exist=iexist)
212 if (.not.iexist) then
213 !
214 !--look for a file with .0 on the end for multiple-file reads
215 !
216 datfile=trim(rootname)//'.0.hdf5'
217 inquire(file=datfile,exist=iexist)
218 if (.not.iexist) then
219 print "(a)",' *** error: '//trim(rootname)//': file not found ***'
220 return
221 endif
222 endif
223 !
224 !--set parameters which do not vary between timesteps
225 !
226 ndim = 0
227 ndimV = 0
228 ! idumpformat = ienvironment('GSPLASH_FORMAT')
229 ! checkids = lenvironment('GSPLASH_CHECKIDS')
230 debug = lenvironment('GSPLASH_DEBUG') .or. debugmode
231 !
232 !--read data from snapshots
233 !
234 i = istepstart
235 !
236 !--i0 is the offset used to read the data into the arrays
237 ! (non-zero for read from multiple files)
238 ! The offset is different for each particle type, somewhat
239 ! complicating the data read -- we shuffle the particles from
240 ! multiple files so that they are in type order.
241 !
242 i0(:) = 0
243 !
244 !--loop over the number of files
245 !
246 ifile = 0
247 ntotall = 0
248 over_files: do while(iexist)
249
250 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
251 ifile = ifile + 1
252
253 !
254 !--open file and read header information
255 !
256 npartoftypei(:) = 0.
257 Nall(:) = 0.
258 massoftypei(:) = 0.
259 if (debug) print*,'DEBUG: reading header...'
260 call read_gadget_hdf5_header(cstring(datfile),maxtypes,maxhdr, &
261 npartoftypei,massoftypei,timetemp,ztemp,headervalstmp,&
262 iFlagSfr,iFlagFeedback,Nall,iFlagCool,igotids,ndim,ndimV,&
263 nfiles,ncolstep,ierr)
264 if (ierr /= 0) then
265 print "(a)", '*** ERROR READING HEADER ***'
266 return
267 endif
268
269 ! read(iunit,iostat=ierr) npartoftypei(1:6),massoftypei,timetemp,ztemp, &
270 ! iFlagSfr,iFlagFeedback,Nall(1:6),iFlagCool,nfiles
271
272 ntoti = int(sum(npartoftypei(1:6))) ! int here is unnecessary, but avoids compiler warnings
273
274 if (nfiles > 1) then
275 ntotall = int(sum(Nall(1:6)))
276 else
277 ntotall = ntoti
278 endif
279 !
280 !--if we are reading from multiple files,
281 ! check that the sequence starts from the correct file
282 !
283 if (nfiles > 1) then
284 idot = index(datfile,'.hdf5')
285 idot = index(datfile(1:idot-1),'.',back=.true.)
286 if (ifile==1 .and. datfile(idot:idot+1) /= '.0') then
287 if (nfiles < 100) then
288 string = "(/,a,i2,a,/,a,/)"
289 else
290 string = "(/,a,i7,a,/,a,/)"
291 endif
292 print string,' ERROR: read is from multiple files (nfiles = ',nfiles,')',&
293 ' but this is not the first file (does not end in .0.hdf5): skipping...'
294 close(iunit)
295 return
296 endif
297 endif
298
299 if (ifile==1) then
300 ncolumns = ncolstep
301 !
302 !--call set labels to get ih, ipmass, irho for use in the read routine
303 !
304 hsoft = 0. ! to avoid unset variable
305 call set_labels_gadget_hdf5
306 endif
307
308 if (ifile==1) then
309 print*,'time : ',timetemp
310 print "(1x,a,f8.2)",'z (redshift) : ',ztemp
311 endif
312 print "(a,6(1x,i10))",' Npart (by type) : ',npartoftypei(1:6)
313 if (any(massoftypei > 0.)) print "(a,6(1x,es10.3))",' Mass (by type) : ',massoftypei
314 print "(a,6(1x,i10))",' N_gas : ',npartoftypei(1)
315 print "(a,1x,i10)",' N_total : ',ntoti
316 if (ifile==1) print "(a,1x,i10)",' N data columns : ',ncolstep
317 if (nfiles > 1 .and. ifile==1) then
318 print "(a,6(1x,i10))",' Nall : ',Nall(1:6)
319 endif
320
321 if (nfiles > 1) then
322 if (ifile==1) print "(a,i4,a)",' reading from ',nfiles,' files'
323 elseif (nfiles <= 0) then
324 print*,'*** ERROR: nfiles = ',nfiles,' in file header: aborting'
325 return
326 endif
327
328 if (ifile==1) then
329 !--Softening lengths for Dark Matter Particles...
330 hsoft = renvironment('GSPLASH_DARKMATTER_HSOFT')
331 !
332 !--try to read dark matter and star particle smoothing lengths and/or density from a separate
333 ! one column ascii file. If only density, use this to compute smoothing lengths.
334 !
335 densfile = trim(rootname)//'.dens'
336 hfile = trim(rootname)//'.hsml'
337 hfact = 1.2 ! related to the analytic neighbour number (hfact=1.2 gives 58 neighbours in 3D)
338 open(unit=iunitd,file=densfile,iostat=ierrrho,status='old',form='formatted')
339 open(unit=iunith,file=hfile,iostat=ierrh,status='old',form='formatted')
340 if (ih==0 .and. (hsoft > tiny(hsoft) .or. ierrrho==0 .or. ierrh==0)) then
341 ncolumns = ncolumns + 1
342 blocklabelgas(ncolumns) = 'SmoothingLength'
343 ih = ncolumns
344 call set_labels_gadget_hdf5
345 endif
346 if (irho==0 .and. (hsoft > tiny(hsoft) .or. ierrrho==0 .or. ierrh==0)) then
347 ncolumns = ncolumns + 1
348 blocklabelgas(ncolumns) = 'Density'
349 irho = ncolumns
350 call set_labels_gadget_hdf5
351 endif
352 !
353 !--if h is still not there, try to set h from density and masses
354 !
355 if (ih==0 .and. irho > 0 .and. ipmass > 0) then
356 ncolumns = ncolumns + 1
357 blocklabelgas(ncolumns) = 'SmoothingLength'
358 ih = ncolumns
359 call set_labels_gadget_hdf5
360 compute_h_from_rho_m = .true.
361 endif
362
363 !
364 !--if successfully read header, increment the nstepsread counter
365 !
366 nstepsread = nstepsread + 1
367 endif
368 !
369 !--now read data
370 !
371 reallocate = .false.
372 npart_max = maxpart
373 nstep_max = max(maxstep,1)
374
375 if (ntoti > maxpart) then
376 reallocate = .true.
377 if (maxpart > 0) then
378 ! if we are reallocating, try not to do it again
379 npart_max = int(1.1*ntotall)
380 else
381 ! if first time, save on memory
382 npart_max = int(ntotall)
383 endif
384 endif
385 if (i >= maxstep .and. i /= 1) then
386 nstep_max = i + max(10,INT(0.1*nstep_max))
387 reallocate = .true.
388 endif
389 !
390 !--reallocate memory for main data array
391 !
392 if (reallocate .or. .not.(allocated(dat))) then
393 if (igotids==1) then
394 call alloc(npart_max,nstep_max,max(ncolumns+ncalc,maxcol),mixedtypes=.true.)
395 else
396 call alloc(npart_max,nstep_max,max(ncolumns+ncalc,maxcol))
397 endif
398 endif
399 masstype(1:6,i) = massoftypei(1:6)
400 !
401 !--copy npartoftypei into allocated header arrays
402 ! and set the offset position of particle types in the main data arrays
403 !
404 if (nfiles==1 .or. ifile==1) then
405 i0(1) = 0
406 do itype=2,ntypes
407 if (nfiles==1) then
408 i0(itype) = sum(npartoftypei(1:itype-1)) ! this is avoid depending on Nall at all for single file read
409 else
410 i0(itype) = sum(Nall(1:itype-1))
411 endif
412 enddo
413 npartoftype(:,i) = npartoftypei
414 else
415 i0(1) = npartoftype(1,i)
416 do itype=2,ntypes
417 i0(itype) = sum(Nall(1:itype-1)) + npartoftype(itype,i)
418 enddo
419 npartoftype(:,i) = npartoftype(:,i) + npartoftypei
420 endif
421 if (debugmode) print*,'DEBUG: starting position for each type in data array: ',i0(:)
422 !
423 !--set time to be used in the legend
424 !
425 if (ifile==1) then
426 !--use this line for code time
427 time(i) = real(timetemp)
428 headervals(:,i) = real(headervalstmp(:))
429 else
430 if (abs(real(timetemp)-time(i)) > tiny(0.)) print*,'ERROR: time different between files in multiple-file read '
431 if (sum(Nall) /= ntotall) then
432 print*,' ERROR: Nall differs between files'
433 goterrors = .true.
434 endif
435 endif
436 !
437 !--read particle data
438 !
439 got_particles: if (ntoti > 0) then
440
441 isrequired(:) = 0
442 where (required(1:ncolumns)) isrequired(1:ncolumns) = 1
443
444 call read_gadget_hdf5_data(cstring(datfile),maxtypes,npartoftypei,ncolumns,isrequired,i0,ierr)
445
446 endif got_particles
447 !
448 !--now memory has been allocated, set arrays which are constant for all time
449 !
450 gamma = 5./3.
451 !
452 !--set flag to indicate that only part of this file has been read
453 !
454 if (.not.all(required(1:ncolstep))) ipartialread = .true.
455 !
456 !--for read from multiple files, work out the next file in the sequence
457 !
458 iexist = .false.
459 if (nfiles > 1 .and. ifile < nfiles) then
460 !--see if the next file exists
461 idot = index(datfile,'.hdf5')
462 idot = index(datfile(1:idot-1),'.',back=.true.)
463 if (idot <= 0) then
464 print "(a)",' ERROR: read from multiple files but could not determine next file in sequence'
465 goterrors = .true.
466 else
467 write(string,*) ifile
468 if (ifile < 10) then
469 write(datfile,"(a,i1)") trim(datfile(1:idot))//trim(adjustl(string))//'.hdf5'
470 elseif (ifile < 100) then
471 write(datfile,"(a,i2)") trim(datfile(1:idot))//trim(adjustl(string))//'.hdf5'
472 else
473 write(datfile,"(a,i3)") trim(datfile(1:idot))//trim(adjustl(string))//'.hdf5'
474 endif
475 iexist = .false.
476 inquire(file=datfile,exist=iexist)
477 if (.not.iexist) then
478 print "(a)",' ERROR: read from multiple files '// &
479 'but could not find '//trim(datfile)//': next in sequence'
480 goterrors = .true.
481 endif
482 endif
483 endif
484
485 enddo over_files
486
487 if (compute_h_from_rho_m) then
488 dat(1:npartoftype(1,i),ih,i) = (dat(1:npartoftype(1,i),ipmass,i)/dat(1:npartoftype(1,i),irho,i))**(1./3.)
489 print "(a)",' this is an AREPO snapshot: using (m/rho)**(1/3) as smoothing length'
490 elseif (arepo .and. ih > 0) then
491 dat(1:npartoftype(1,i),ih,i) = dat(1:npartoftype(1,i),ih,i)**(1./3.)
492 print "(a)",' this is an AREPO snapshot, using Volume**(1./3.) as smoothing length'
493 elseif (ih > 0 .and. required(ih) .and. size(dat,2) >= ih .and. npartoftype(1,i) > 0) then
494 !
495 !--for some reason the smoothing length output by GADGET is
496 ! twice the usual SPH smoothing length
497 ! (do this after we have read data from all of the files)
498 !
499 print "(a)",' converting GADGET h on gas particles to usual SPH definition (x 0.5)'
500 dat(1:npartoftype(1,i),ih,i) = 0.5*dat(1:npartoftype(1,i),ih,i)
501 endif
502 if (nfiles > 1. .and. any(npartoftype(1:6,i) /= Nall(1:6))) then
503 print*,'ERROR: sum of Npart across multiple files /= Nall in data read '
504 print*,'Npart = ',npartoftype(1:6,i)
505 print*,'Nall = ',Nall(1:6)
506 goterrors = .true.
507 endif
508 !
509 !--look for dark matter smoothing length/density files
510 !
511 if (ierrh==0 .or. ierrrho==0) then
512 if (ierrh==0) then
513 print "(a)",' READING DARK MATTER SMOOTHING LENGTHS from '//trim(hfile)
514 ierr = 0
515 index1 = npartoftype(1,i)+1
516 index2 = npartoftype(1,i)+sum(npartoftype(2:,i))
517 read(iunith,*,iostat=ierr) (dat(j,ih,i),j=index1,index2)
518 close(unit=iunith)
519 if (ierr < 0) then
520 nhset = 0
521 do j=index1,index2
522 if (dat(j,ih,i) > 0.) nhset = nhset + 1
523 enddo
524 print "(a,i10,a,/)",' *** END-OF-FILE: GOT ',nhset,' SMOOTHING LENGTHS ***'
525 elseif (ierr > 0) then
526 print "(a)", ' *** ERROR reading smoothing lengths from file'
527 goterrors = .true.
528 else
529 print "(a,i10,a)",' SMOOTHING LENGTHS READ OK for ',index2-index1+1,' dark matter / star particles '
530 endif
531 hsoft = 1.0 ! just so dark matter rendering is allowed in set_labels_gadget_hdf5 routine
532 endif
533
534 if (ierrrho==0) then
535 print "(a)",' READING DARK MATTER DENSITIES FROM '//trim(densfile)
536 ierr = 0
537 index1 = npartoftype(1,i)+1
538 index2 = npartoftype(1,i)+sum(npartoftype(2:,i))
539 read(iunitd,*,iostat=ierr) (dat(j,irho,i),j=index1,index2)
540 close(iunitd)
541 if (ierr < 0) then
542 nhset = 0
543 do j=index1,index2
544 if (dat(j,irho,i) > 0.) nhset = nhset + 1
545 enddo
546 print "(a,i10,a,/)",' *** END-OF-FILE: GOT ',nhset,' DENSITIES ***'
547 elseif (ierr > 0) then
548 print "(a)", ' *** ERROR reading dark matter densities from file'
549 goterrors = .true.
550 else
551 print "(a,i10,a)",' DENSITY READ OK for ',index2-index1+1,' dark matter / star particles '
552 endif
553 if (ierrh /= 0 .and. ipmass > 0) then
554 where(dat(:,irho,i) > tiny(dat))
555 dat(:,ih,i) = hfact*(dat(:,ipmass,i)/dat(:,irho,i))**(1./3.)
556 elsewhere
557 dat(:,ih,i) = 0.
558 end where
559 print "(a,i10,a,f5.2,a)", &
560 ' SMOOTHING LENGTHS SET for ',index2-index1+1,' DM/star particles using h = ',hfact,'*(m/rho)**(1/3)'
561 endif
562 hsoft = 1.0 ! just so dark matter rendering is allowed in set_labels_gadget_hdf5 routine
563 endif
564 else
565 !
566 !--if a value for the dark matter smoothing length is set
567 ! via the environment variable GSPLASH_DARKMATTER_HSOFT,
568 ! give dark matter particles this smoothing length
569 ! and a density of 1 (so column density plots work)
570 !
571 if (hsoft > tiny(hsoft)) then
572 if (required(ih)) then
573 print "(a,1pe10.3,a)",' ASSIGNING SMOOTHING LENGTH of h = ',hsoft, &
574 ' to dark matter particles'
575 !print*,'ih = ',ih,' npartoftype = ',npartoftype(1:2,i), shape(dat)
576 if (ih > 0) then
577 dat(npartoftype(1,i)+1:npartoftype(1,i)+npartoftype(2,i),ih,i) = hsoft
578 else
579 print*,' ERROR: smoothing length not found in data arrays'
580 goterrors = .true.
581 endif
582 endif
583 if (required(irho)) then
584 if (irho > 0) then
585 dat(npartoftype(1,i)+1:npartoftype(1,i)+npartoftype(2,i),irho,i) = 1.0
586 else
587 print*,' ERROR: place for density not found in data arrays'
588 goterrors = .true.
589 endif
590 endif
591 else
592 if (npartoftype(1,i) <= 0 .and. sum(npartoftype(:,i)) > 0) then
593 print "(66('*'),4(/,a),/)",'* NOTE!! For GADGET data using dark matter only, column density ',&
594 '* plots can be produced by setting the GSPLASH_DARKMATTER_HSOFT ',&
595 '* environment variable to give the dark matter smoothing length', &
596 '* (for a fixed smoothing length)'
597 hsoft = (maxval(dat(:,1,i)) - minval(dat(:,1,i)))/sum(npartoftype(2:,i))**(1./3.)
598 print*,' suggested value for GSPLASH_DARKMATTER_HSOFT = ',hsoft
599 hsoft = 0.
600
601 print "(7(/,a),/)",'* Alternatively, and for best results, calculate a number density', &
602 '* on dark matter particles, set individual smoothing lengths from', &
603 '* this using h = hfact*(n)**(-1/3), with hfact=1.2 and either ', &
604 '* dump the results back into the HSML array in the original dump ', &
605 '* file (if using the block-labelled format), or create an ascii ',&
606 '* file called '//trim(hfile)//' containing the smoothing length ',&
607 '* values for the dark matter particles.'
608 print "(2(/,a),/,66('*'),/)", '* Also make sure normalised interpolations are OFF when plotting ',&
609 '* dark matter density '
610 endif
611 endif
612 endif
613 !
614 !--pause with fatal errors
615 !
616 if (goterrors .and. .not.lenvironment('GSPLASH_IGNORE_ERRORS')) then
617 print "(/,a)",'*** ERRORS detected during data read: data will be corrupted'
618 print "(a,/)",' Please REPORT this and/or fix your file ***'
619 print "(a)",' (set GSPLASH_IGNORE_ERRORS=yes to skip this message)'
620 if (iverbose >= 1) then
621 print "(a)",' > Press any key to bravely proceed anyway <'
622 read*
623 endif
624 endif
625 !
626 !--give a friendly warning about using too few or too many neighbours
627 ! (only works with equal mass particles because otherwise we need the number density estimate)
628 !
629 if (ih > 0 .and. required(ih) .and. ipmass > 0 .and. required(ipmass) &
630 .and. abs(massoftypei(1)) < tiny(0.) .and. ndim==3 .and. .not.havewarned &
631 .and. .not.compute_h_from_rho_m) then
632 nhfac = 100
633 if (npartoftype(1,i) > nhfac) then
634 hfactmean = 0.
635 do j=1,nhfac
636 pmassi = dat(j,ipmass,i)
637 if (pmassi > 0.) then
638 pmassi = 1./pmassi
639 else
640 pmassi = 0.
641 endif
642 hfact = dat(j,ih,i)*(dat(j,irho,i)*pmassi)**(1./ndim)
643 hfactmean = hfactmean + hfact
644 enddo
645 hfact = hfactmean/real(nhfac)
646 havewarned = .true.
647 ! CHECK that h is not implausibly small due to the multiply-by-0.5 -- if so, reverse this
648 if (hfact < 0.75) then
649 dat(1:npartoftype(1,i),ih,i) = dat(1:npartoftype(1,i),ih,i)*2.0
650 hfact = hfact*2.0
651 endif
652 if (hfact < 1.125 .or. hfact > 1.45) then
653 print "(/,a)",'** FRIENDLY NEIGHBOUR WARNING! **'
654 print "(3x,a,f5.1,a,/,3x,a,f4.2,a,i1,a)", &
655 'It looks like you are using around ',4./3.*pi*(2.*hfact)**3,' neighbours,', &
656 'corresponding to h = ',hfact,'*(m/rho)^(1/',ndim,') in 3D:'
657
658 if (hfact < 1.15) then
659 print "(4(/,3x,a))",'This is a quite a low number of neighbours for the cubic spline and ', &
660 'may result in increased noise and inaccurate wave propagation speeds', &
661 '(a cubic lattice is also an unstable initial configuration for the ',&
662 ' particles in this regime -- see Morris 1996, Borve et al. 2004).'
663 elseif (hfact > 1.45) then
664 print "(4(/,3x,a))",'Using h >~ 1.5*(m/rho)^(1/3) with the cubic spline results in the', &
665 'particle pairing instability due to the first neighbour being placed under', &
666 'the hump in the kernel gradient. Whilst not fatal, it results in a', &
667 'loss of resolution so is a bit of a waste of cpu time.'
668 print "(4(/,3x,a))",'If you are attempting to perform a "resolution study" by increasing the', &
669 'neighbour number, this is a *bad idea*, as you are also increasing h.', &
670 '(a better way is to increase the smoothness of the integrals without changing h', &
671 ' by adopting a smoother kernel such as the M6 Quintic that goes to 3h).'
672 endif
673 print "(/,3x,a,/,3x,a,/)", &
674 'A good default is h = 1.2 (m/rho)^1/ndim ', &
675 'corresponding to around 58 neighbours in 3D.'
676 else
677 print "(/,1x,a,f5.1,a,/,1x,a,f4.2,a,i1,a,/)", &
678 'Simulations employ ',4./3.*pi*(2.*hfact)**3,' neighbours,', &
679 'corresponding to h = ',hfact,'*(m/rho)^(1/',ndim,') in 3D'
680 endif
681 endif
682 else
683 !print*,'not true'
684 endif
685 !
686 !--cover the special case where no particles have been read
687 !
688 if (ntotall <= 0) then
689 npartoftype(1,i) = 1
690 dat(:,:,i) = 0.
691 endif
692
693 if (nstepsread > 0) then
694 print "(a,i10,a)",' >> read ',sum(npartoftype(:,istepstart+nstepsread-1)),' particles'
695 endif
696
697 end subroutine read_data_gadget_hdf5
698
699 subroutine read_gadgethdf5_data_fromc(icol,npartoftypei,temparr,id,itype,i0,name,iblock) bind(c)
700 use, intrinsic :: iso_c_binding, only:c_int,c_double
701 use particle_data, only:dat,iamtype
702 use settings_data, only:debugmode
703 use labels, only:label
704 use system_utils, only:lenvironment
705 integer(kind=c_int), intent(in) :: icol,npartoftypei,itype,i0,iblock
706 real(kind=c_double), dimension(npartoftypei), intent(inout) :: temparr
707 integer(kind=c_int), dimension(npartoftypei), intent(in) :: id
708 character(kind=c_char), dimension(256), intent(in) :: name
709 integer(kind=c_int) :: i,icolput
710 integer :: ncolmax,nmax,nerr,idi
711 character(len=256) :: datasetname
712 logical :: matched
713
714 datasetname = fstring(name)
715
716 icolput = icol
717 nmax = size(dat,1)
718 ncolmax = size(dat,2)
719 if (icolput > ncolmax .or. icolput==0) then
720 print "(a,i2,a)",' ERROR: column = ',icolput,' out of range in receive_data_fromc'
721 return
722 endif
723 matched = .true.
724 if (trim(blocklabelgas(iblock)) /= trim(datasetname)) then
725 matched = .false.
726 do i=1,ncolmax
727 if (trim(reformatlabel(datasetname))==trim(label(i))) then
728 if (debugmode) print "(a,i0)",'DEBUG: matched '//trim(datasetname)//' to column ',i
729 icolput = i
730 matched = .true.
731 endif
732 enddo
733 if (.not.matched) then
734 temparr = 0.
735 if (debugmode) print "(a,i0)",'DEBUG: Warning: skipping unmatched dataset '//&
736 trim(datasetname)//' for particle type ',itype
737 endif
738 endif
739
740 !useids = .not.lenvironment('GSPLASH_FIXID')
741 if (all(id <= 0) .or. size(iamtype,1) <= 1) useids = .false.
742 if (debugmode .and. matched) print "(a,i2,a,i2,a,i8,a,l1)",'DEBUG: reading column ',icol,&
743 ' type ',itype,' -> '//trim(label(icolput))//', offset ',i0,' use IDs=',useids
744
745 if (useids) then
746 nerr = 0
747 !print*,' id range is ',minval(id),' to ',maxval(id),' type ',itype+1,' column = ',trim(label(icolput))
748 do i=1,npartoftypei
749 idi = id(i)
750 if (idi <= 0 .or. idi > nmax) then
751 !
752 !--correct for particle IDs > 1e9 (used to represent recycled particles?)
753 !
754 if (idi > 1000000000) then
755 idi = idi - 1000000000
756 if (idi <= nmax .and. idi > 0) then
757 dat(idi,icolput,1) = real(temparr(i))
758 iamtype(idi,1) = int(itype + 1,kind=kind(iamtype))
759 else
760 nerr = nerr + 1
761 if (debugmode .and. nerr <= 10) print*,i,'fixed id = ',idi
762 endif
763 else
764 nerr = nerr + 1
765 if (debugmode .and. nerr <= 10) print*,i,' id = ',idi,idi-1000000000
766 endif
767 else
768 dat(idi,icolput,1) = real(temparr(i))
769 iamtype(idi,1) = int(itype + 1,kind=kind(iamtype))
770 endif
771 enddo
772 if (nerr > 0) print*,'ERROR: got particle ids outside array dimensions ',nerr,' times'
773 else
774 if (i0 < 0) then
775 print*,'ERROR: i0 = ',i0,' but should be positive: SOMETHING IS VERY WRONG...'
776 return
777 elseif (i0+npartoftypei > nmax) then
778 print "(a,i8,a)",' ERROR: offset = ',i0,': read will exceed array dimensions in receive_data_fromc'
779 nmax = nmax - i0
780 else
781 nmax = npartoftypei
782 endif
783 do i=1,nmax
784 dat(i0+i,icolput,1) = real(temparr(i))
785 enddo
786 if (size(iamtype,1) > 1) then
787 do i=1,nmax
788 iamtype(i0+i,1) = int(itype + 1,kind=kind(iamtype))
789 enddo
790 endif
791 endif
792
793 end subroutine read_gadgethdf5_data_fromc
794
795 !!------------------------------------------------------------
796 !! set labels for each column of data
797 !!------------------------------------------------------------
798
799 subroutine set_labels_gadget_hdf5
800 use labels, only:label,iamvec,labelvec,labeltype,ix,ivx,ipmass, &
801 ih,irho,ipr,iutherm,iBfirst,iax,make_vector_label
802 use params
803 use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings,debugmode
804 use geometry, only:labelcoord
805 use system_utils, only:envlist,ienvironment
806 use asciiutils, only:lcase
807 integer :: i,icol,irank
808
809 if (ndim <= 0 .or. ndim > 3) then
810 print*,'*** ERROR: ndim = ',ndim,' in set_labels_gadget_hdf5 ***'
811 return
812 endif
813 if (ndimV <= 0 .or. ndimV > 3) then
814 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_gadget_hdf5 ***'
815 return
816 endif
817
818 icol = 1
819 ix = 0
820 do i=1,size(blocklabelgas)
821 irank = blocksize(i)
822 if (irank > 0 .and. (len_trim(blocklabelgas(i)) > 0)) then
823 if (debugmode) print*,i,trim(blocklabelgas(i))
824 select case(blocklabelgas(i))
825 case('Coordinates')
826 ix(1) = icol
827 ix(2) = icol + 1
828 if (irank >= 3) ix(3) = icol + 2
829 case('Velocities','Velocity')
830 ivx = icol
831 case('SmoothingLength','SmoothingLengths')
832 ih = icol
833 case('Volume')
834 ih = icol
835 arepo = .true.
836 case('Masses','Mass')
837 ipmass = icol
838 case('InternalEnergy','InternalEnergies')
839 iutherm = icol
840 case('Density','Densities')
841 irho = icol
842 case('MagneticField')
843 iBfirst = icol
844 case('Pressures','Pressure')
845 ipr = icol
846 end select
847 label(icol:icol+irank-1) = reformatlabel(blocklabelgas(i))
848
849 if (irank==ndimV) then
850 call make_vector_label(label(icol),icol,ndimV,iamvec,labelvec,label,labelcoord(:,1))
851 endif
852 icol = icol + irank
853 endif
854 enddo
855 !
856 !--set labels of the quantities read in
857 !
858 if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1)
859 if (irho > 0) label(irho) = 'density' ! needed to convert to "column density"
860 if (iutherm > 0) label(iutherm) = 'u' ! otherwise \int u dz looks ugly
861 !if (ipmass > 0) label(ipmass) = 'particle mass'
862 !if (ih > 0) label(ih) = 'h'
863 !
864 !--set labels for vector quantities
865 !
866 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
867 call make_vector_label('a',iax,ndimV,iamvec,labelvec,label,labelcoord(:,1))
868 call make_vector_label('B',iBfirst,ndimV,iamvec,labelvec,label,labelcoord(:,1))
869
870 !--set labels for each particle type
871 !
872 ntypes = 6
873 labeltype(1) = 'gas'
874 labeltype(2) = 'dark matter'
875 labeltype(3) = 'boundary 1'
876 labeltype(4) = 'boundary 2'
877 labeltype(5) = 'star'
878 labeltype(6) = 'sink / black hole'
879 UseTypeInRenderings(1) = .true.
880 !
881 !--dark matter particles are of non-SPH type (ie. cannot be used in renderings)
882 ! unless they have had a smoothing length defined
883 !
884 if (hsoft > tiny(hsoft)) then
885 UseTypeInRenderings(2) = .true.
886 else
887 UseTypeInRenderings(2) = .false.
888 endif
889 UseTypeInRenderings(3:6) = .false.
890
891 end subroutine set_labels_gadget_hdf5
892
893 subroutine set_blocklabel_gadget(icol,irank,name) bind(c)
894 use, intrinsic :: iso_c_binding, only:c_int, c_char
895 integer(kind=c_int), intent(in) :: icol,irank
896 character(kind=c_char), dimension(256), intent(in) :: name
897
898 blocklabelgas(icol+1) = fstring(name)
899 blocksize(icol+1) = irank
900 !print*,icol+1,' name = ',trim(blocklabelgas(icol+1)),' x ',irank
901
902 end subroutine set_blocklabel_gadget
903
904 subroutine set_header_label_gadget(i,name) bind(c)
905 use, intrinsic :: iso_c_binding, only:c_int, c_char
906 use labels, only:headertags
907 integer(kind=c_int), intent(in) :: i
908 character(kind=c_char), dimension(256), intent(in) :: name
909
910 headertags(i+1) = fstring(name)
911
912 end subroutine set_header_label_gadget
913
914 end module readdata_gadget_hdf5