Debian Codegraph @

src/read_data_tipsy.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 READING TIPSY FILES
28 !
29 ! => HANDLES BOTH BINARY AND ASCII TIPSY FORMATS
30 ! (DETECTS WHICH ONE AUTOMATICALLY)
31 !
32 ! BINARY FORMAT READING REQUIRES F2003 STREAM I/O
33 ! WHICH MAY NOT BE IMPLEMENTED ON OLDER COMPILERS
34 !
35 ! the data is stored in the global array dat
36 !
37 ! >> this subroutine must return values for the following: <<
38 !
39 ! ncolumns : number of data columns
40 ! ndim, ndimV : number of spatial, velocity dimensions
41 ! nstepsread : number of steps read from this file
42 !
43 ! maxplot,maxpart,maxstep : dimensions of main data array
44 ! dat(maxplot,maxpart,maxstep) : main data array
45 !
46 ! npartoftype(1:6,maxstep) : number of particles of each type in each timestep
47 !
48 ! time(maxstep) : time at each step
49 ! gamma(maxstep) : gamma at each step
50 !
51 ! most of these values are stored in global arrays
52 ! in the module 'particle_data'
53 !-------------------------------------------------------------------------
54
55 module readdata_tipsy
56 use settings_data, only:debugmode
57 use byteswap, only:bs
58 implicit none
59
60 public :: read_data_tipsy, set_labels_tipsy, file_format_is_tipsy
61
62 private
63
64 contains
65
66 subroutine read_data_tipsy(rootname,indexstart,ipos,nstepsread)
67 use particle_data, only:dat,time,npartoftype,gamma,maxpart
68 use params
69 use settings_data, only:ndim,ndimV,ncolumns
70 use mem_allocation, only:alloc
71 use labels, only:label,ih,ipmass,irho,ivx
72 integer, intent(in) :: indexstart,ipos
73 integer, intent(out) :: nstepsread
74 character(len=*), intent(in) :: rootname
75 integer, parameter :: iunit = 16
76 integer :: j,ierr
77 integer :: nprint,ngas,ndark,nptmass,npart_max,nstep_max
78 integer :: ncol,nread,iambinaryfile
79 logical :: iexist,do_byteswap
80 character(len=len(rootname)) :: dumpfile
81 character(len=11) :: fmt
82 real :: timei, hfact
83
84 nstepsread = 0
85 nstep_max = 0
86 npart_max = maxpart
87 dumpfile = trim(rootname)
88 !
89 !--check if first data file exists
90 !
91 inquire(file=dumpfile,exist=iexist)
92 if (.not.iexist) then
93 print "(a)",' *** error: '//trim(dumpfile)//': file not found ***'
94 return
95 endif
96
97 nstep_max = max(nstep_max,indexstart,1)
98 j = indexstart
99 nstepsread = 0
100
101 write(*,"(26('>'),1x,a,1x,26('<'))") trim(dumpfile)
102 !
103 !--determine whether file is binary or ascii and open it
104 !
105 inquire(file=dumpfile,form=fmt)
106
107 select case(trim(adjustl(fmt)))
108 case('UNFORMATTED')
109 iambinaryfile = 1
110 open(unit=iunit,file=dumpfile,status='old',form='unformatted',access='stream',iostat=ierr)
111 case('FORMATTED')
112 iambinaryfile = 0
113 open(unit=iunit,file=dumpfile,status='old',form='formatted',iostat=ierr)
114 case default
115 !--if compiler cannot distinguish the two, try ascii first, then binary
116 iambinaryfile = -1
117 open(unit=iunit,file=dumpfile,status='old',form='formatted',iostat=ierr)
118 end select
119
120 if (ierr /= 0) then
121 print "(a)",'*** ERROR OPENING '//trim(dumpfile)//' ***'
122 return
123 endif
124 !
125 !--read the file header
126 ! try ascii format first, and if unsuccessful try binary
127 !
128 if (iambinaryfile==1) then
129 write(*,"(a)",advance='no') ' reading binary tipsy format:'
130 call read_tipsyheader_binary(iunit,do_byteswap,ierr)
131 else
132 if (iambinaryfile==0) print "(a)",' reading ascii tipsy format '
133 call read_tipsyheader_ascii(iunit,ierr,iambinaryfile)
134 if (iambinaryfile < 0) then
135 if (ierr==0) then
136 !--if successful ascii header read, file is ascii
137 iambinaryfile = 0
138 print "(a)",' reading ascii tipsy format '
139 else
140 !--otherwise, close ascii file, and assume file is binary
141 close(unit=iunit)
142 iambinaryfile = 1
143 open(unit=iunit,file=dumpfile,status='old',form='unformatted',&
144 access='stream',iostat=ierr)
145 write(*,"(a)",advance='no') ' reading binary tipsy format:'
146 call read_tipsyheader_binary(iunit,do_byteswap,ierr)
147 endif
148 endif
149 endif
150 if (ierr /= 0) then
151 print*
152 ndim = 0
153 ncolumns = 0
154 close(unit=iunit)
155 return
156 endif
157
158 print "(a,f10.2,1(a,i1))",' time: ',timei,' ndim: ',ndim
159 print "(4(a,i10))",' ntot: ',nprint,' ngas: ',ngas,' ndark: ',ndark,' nstar: ',nptmass
160
161 ndimV = ndim
162 ncol = 2*ndim + 4
163 ncolumns = ncol
164 !
165 !--allocate memory
166 !
167 if (.not.allocated(dat) .or. nprint > npart_max) then
168 npart_max = max(npart_max,nprint)
169 call alloc(npart_max,nstep_max,ncolumns)
170 endif
171 !
172 !--now read the timestep data in the dumpfile
173 !
174 dat(:,:,j) = 0.
175 time(j) = timei
176
177 nread = 0
178 call set_labels_tipsy
179
180 if (iambinaryfile==1) then
181 call read_tipsybody_binary(iunit,do_byteswap,ierr,nread)
182 else
183 call read_tipsybody_ascii(iunit,ierr,nread)
184 endif
185 close(unit=iunit)
186
187 if (nread < ncol) then
188 print "(a,i2)",' WARNING: END OF FILE: READ TO COLUMN ',nread
189 ncolumns = nread
190 endif
191 !
192 !--often tipsy dumps contain only a (fixed) gravitational softening length
193 ! for sph particles. In this case we need to create a sensible smoothing length
194 ! (and warn about the evils of using fixed softening lengths for sph particles)
195 !
196 if (ngas >= 0 .and. nread >= irho .and. all(abs(dat(1:ngas,ih,j)-dat(1,ih,j)) <= tiny(dat))) then
197 hfact=1.2
198 print "(a)",' WARNING: fixed softening lengths detected: simulation may contain artificial fragmentation!'
199 print "(a,f5.2,a,i1,a)",' : creating SPH smoothing lengths using h = ',hfact,'*(m/rho)**(1/',ndim,')'
200 dat(1:ngas,ih,j) = hfact*(dat(1:ngas,ipmass,j)/(dat(1:ngas,irho,j) + tiny(dat)))**(1./ndim)
201 endif
202
203 nstepsread = nstepsread + 1
204 npartoftype(1,j) = ngas
205 npartoftype(2,j) = ndark
206 npartoftype(3,j) = nptmass
207 gamma(j) = 1.666666666667
208 j = j + 1
209
210 if (allocated(npartoftype)) then
211 print*,'>> end of dump file: nsteps =',j-1,'ntot = ',sum(npartoftype(:,j-1))
212 endif
213
214 return
215
216 contains
217
218 !----------------------------------------------------
219 ! ascii header read
220 !----------------------------------------------------
221 subroutine read_tipsyheader_ascii(iunit,ierr,iwarn)
222 integer, intent(in) :: iunit,iwarn
223 integer, intent(out) :: ierr
224
225 read(iunit,*,end=55,iostat=ierr) nprint,ngas,nptmass
226 read(iunit,*,end=55,iostat=ierr) ndim
227 read(iunit,*,end=55,iostat=ierr) timei
228 ndark = nprint - ngas - nptmass
229 !--errors in header read
230 if (nprint <= 0 .or. nprint > 1e10 .or. ndim <= 0 .or. ndim > 3 .or. ndark < 0) then
231 if (iwarn >= 0) print "(a)",' ERROR reading ascii file header '
232 ierr = 2
233 return
234 endif
235
236 return
237
238 55 continue
239 if (iwarn >= 0) print "(a)",' ERROR: end of file in ascii header read '
240 ierr = -1
241 return
242
243 end subroutine read_tipsyheader_ascii
244
245 !----------------------------------------------------
246 ! binary header read
247 !----------------------------------------------------
248 subroutine read_tipsyheader_binary(iunitb,do_byteswap,ierr)
249 integer, intent(in) :: iunitb
250 logical, intent(out) :: do_byteswap
251 integer, intent(out) :: ierr
252 real(doub_prec) :: timedb
253 integer :: ipad
254
255 ierr = 0
256 do_byteswap = .false.
257 read(iunitb,iostat=ierr,end=55) timedb,nprint,ndim,ngas,ndark,nptmass,ipad
258 if (debugmode) print*,'header = ',timedb,nprint,ndim,ngas,ndark,nptmass
259
260 !--check for wrong endianness and byte-swap if necessary
261 if (ierr /= 0 .or. timedb < 0. .or. bad_header(ndim,nprint,ngas,ndark,nptmass)) then
262 timedb = bs(timedb); ndim = bs(ndim); nprint = bs(nprint); ngas = bs(ngas)
263 ndark = bs(ndark); nptmass = bs(nptmass)
264 if (ierr /= 0 .or. timedb < 0. .or. &
265 bad_header(ndim,nprint,ngas,ndark,nptmass)) then
266 print "(a)",' ERROR reading binary file header'
267 ierr = 2
268 else
269 do_byteswap = .true.
270 write(*,"(a)",advance='no') ' big endian: '
271 endif
272 endif
273 timei = real(timedb)
274
275 if (ndim==0) ndim = 3
276
277 return
278
279 55 continue
280 print "(a)",' ERROR: end of file in binary header read'
281 ierr = -1
282
283 end subroutine read_tipsyheader_binary
284
285 !----------------------------------------------------
286 ! ascii body read
287 !----------------------------------------------------
288 subroutine read_tipsybody_ascii(iunit,ierr,nread)
289 integer, intent(in) :: iunit
290 integer, intent(out) :: ierr, nread
291 integer :: i,ic,icol,nerr
292
293 !--pmass,x,y,z,vx,vy,vz
294 do ic=1,2*ndim+1
295 nerr = 0
296 nread = nread + 1
297 if (ic==1) then ! pmass
298 icol = ndim + 1
299 elseif (ic >= 2 .and. ic <= ndim+1) then ! x, y, z
300 icol = ic - 1
301 else ! everything after
302 icol = ic
303 endif
304 !print "(1x,a)",trim(label(icol))
305 nerr = 0
306 do i=1,nprint
307 read(iunit,*,end=44,iostat=ierr) dat(i,icol,j)
308 if (ierr /= 0) nerr = nerr + 1
309 enddo
310 if (nerr > 0) print *,'*** WARNING: ERRORS READING '//trim(label(icol))//' ON ',nerr,' LINES'
311 enddo
312 !--h dark matter
313 if (ndark > 0) then
314 nerr = 0
315 do i=ngas+1,ngas+ndark-1
316 read(iunit,*,end=44,iostat=ierr) dat(i,ih,j)
317 if (ierr /= 0) nerr = nerr + 1
318 enddo
319 if (nerr > 0) print *,'*** WARNING: ERRORS READING DARK MATTER H ON ',nerr,' LINES'
320 endif
321 !--h star particles
322 if (nptmass > 0) then
323 nerr = 0
324 do i=ngas+ndark+1,ngas+ndark+nptmass
325 read(iunit,*,end=44,iostat=ierr) dat(i,ih,j)
326 if (ierr /= 0) nerr = nerr + 1
327 enddo
328 if (nerr > 0) print *,'*** WARNING: ERRORS READING PTMASS H ON ',nerr,' LINES'
329 endif
330 !--density, temperature, sph smoothing length
331 do icol=2*ndim+2,ncol
332 nread = nread + 1
333 !print "(1x,a)",trim(label(icol))
334 do i=1,ngas
335 read(iunit,*,end=44,iostat=ierr) dat(i,icol,j)
336 if (ierr /= 0) nerr = nerr + 1
337 enddo
338 if (nerr > 0) print *,'*** WARNING: ERRORS READING '//trim(label(icol))//' ON ',nerr,' LINES'
339 enddo
340
341 ierr = 0
342 return
343
344 44 continue
345 ierr = -1
346
347 end subroutine read_tipsybody_ascii
348
349 !----------------------------------------------------
350 ! binary body read
351 !----------------------------------------------------
352 subroutine read_tipsybody_binary(iunitb,do_byteswap,ierr,nread)
353 use settings_data, only:debugmode
354 integer, intent(in) :: iunitb
355 logical, intent(in) :: do_byteswap
356 integer, intent(out) :: ierr,nread
357 integer :: i,nerr
358 real(kind=4) :: pmass,xyz(3),vxyz(3),rho,temp,h,dummy
359
360 !--gas particles
361 nerr = 0
362 if (debugmode) print*,'DEBUG: reading ',ngas,' gas particles'
363 do i=1,ngas
364 !--pmass,x,y,z,vx,vy,vz,rho,temp,h
365 read(iunitb,end=44,iostat=ierr) pmass,xyz(1:ndim),vxyz(1:ndim),&
366 rho,temp,h,dummy,dummy
367 if (do_byteswap) then
368 pmass = bs(pmass); xyz = bs(xyz); vxyz = bs(vxyz); rho = bs(rho)
369 temp = bs(temp); h = bs(h)
370 endif
371 dat(i,ipmass,j) = pmass
372 dat(i,1:ndim,j) = xyz(1:ndim)
373 dat(i,ivx:ivx+ndim-1,j) = vxyz(1:ndim)
374 dat(i,irho,j) = rho
375 dat(i,irho+1,j) = temp
376 dat(i,ih,j) = h
377 if (ierr /= 0) nerr = nerr + 1
378 enddo
379 nread = ncolumns
380 if (nerr > 0) print *,'*** WARNING: ERRORS READING GAS PARTICLES ON ',nerr,' LINES'
381
382 !--dark matter
383 if (ndark > 0) then
384 nerr = 0
385 do i=ngas+1,ngas+ndark
386 !--only read as far as velocities, then eps as smoothing length
387 read(iunitb,end=44,iostat=ierr) pmass,xyz,vxyz,h,dummy
388 if (do_byteswap) then
389 pmass = bs(pmass); xyz = bs(xyz); vxyz = bs(vxyz); h = bs(h)
390 endif
391 dat(i,ipmass,j) = pmass
392 dat(i,1:ndim,j) = xyz(1:ndim)
393 dat(i,ivx:ivx+ndim-1,j) = vxyz(1:ndim)
394 dat(i,ih,j) = h
395 if (ierr /= 0) nerr = nerr + 1
396 enddo
397 if (nerr > 0) print *,'*** WARNING: ERRORS READING DARK MATTER PARTICLES ON ',nerr,' LINES'
398 endif
399
400 !--star particles
401 if (nptmass > 0) then
402 nerr = 0
403 do i=ngas+ndark+1,ngas+ndark+nptmass
404 !--only read as far as velocities, then eps as smoothing length
405 read(iunitb,end=44,iostat=ierr) pmass,xyz(1:ndim),vxyz(1:ndim),&
406 dummy,dummy,h,dummy
407 if (do_byteswap) then
408 pmass = bs(pmass); xyz = bs(xyz); vxyz = bs(vxyz); h = bs(h)
409 endif
410 dat(i,ipmass,j) = pmass
411 dat(i,1:ndim,j) = xyz(1:ndim)
412 dat(i,ivx:ivx+ndim-1,j) = vxyz(1:ndim)
413 dat(i,ih,j) = h
414 if (ierr /= 0) nerr = nerr + 1
415 enddo
416 if (nerr > 0) print *,'*** WARNING: ERRORS READING STAR PARTICLES ON ',nerr,' LINES'
417 endif
418
419 ierr = 0
420 return
421
422 44 continue
423 ierr = -1
424
425 end subroutine read_tipsybody_binary
426
427 end subroutine read_data_tipsy
428
429 !--------------------------------------------------------
430 ! function to check if values read from
431 ! the tipsy header are sensible
432 !--------------------------------------------------------
433 logical function bad_header(ndim,nprint,ngas,ndark,nptmass)
434 integer, intent(in) :: ndim,nprint,ngas,ndark,nptmass
435
436 bad_header = (ndim < 0 .or. ndim > 3 &
437 .or. nprint <= 0 .or. ngas < 0 .or. ndark < 0 .or. nptmass < 0 &
438 .or. nprint > 1e10 .or. ngas > 1.e10 &
439 .or. ndark > 1.e10 .or. nptmass > 1.e8 )
440
441 end function bad_header
442
443 !-----------------------------------------------------------
444 !
445 ! check if a file is in phantom/sphNG format
446 !
447 !-----------------------------------------------------------
448 logical function file_format_is_tipsy(filename) result(is_tipsy)
449 character(len=*), intent(in) :: filename
450 integer :: iunit,nprint,ndim,ngas,ndark,nptmass,ierr
451 real(kind=8) :: timedb
452
453 is_tipsy = .false.
454 !
455 ! open file and read the first line
456 !
457 open(newunit=iunit,iostat=ierr,file=filename,status='old',form='unformatted',access='stream')
458 if (ierr /= 0) return
459 !
460 ! check the first line to determine tipsy format
461 !
462 read(iunit,iostat=ierr) timedb,nprint,ndim,ngas,ndark,nptmass
463 if (.not.(ierr /= 0 .or. timedb < 0. .or. &
464 bad_header(ndim,nprint,ngas,ndark,nptmass))) then
465 is_tipsy = .true.
466 elseif (.not. ierr /= 0) then
467 ! try byte-swapping
468 timedb = bs(timedb); ndim = bs(ndim); nprint = bs(nprint); ngas = bs(ngas)
469 ndark = bs(ndark); nptmass = bs(nptmass)
470 if (.not. (timedb < 0. .or. bad_header(ndim,nprint,ngas,ndark,nptmass))) then
471 is_tipsy = .true.
472 endif
473 endif
474 close(iunit) ! close the file
475
476 end function file_format_is_tipsy
477
478 !!------------------------------------------------------------
479 !! set labels for each column of data
480 !!------------------------------------------------------------
481 subroutine set_labels_tipsy
482 use labels, only:label,labelvec,labeltype,iamvec,&
483 ix,ivx,ih,irho,ipmass,make_vector_label
484 use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings
485 use geometry, only:labelcoord
486 integer :: i
487
488 if (ndim <= 0 .or. ndim > 3) then
489 print*,'*** ERROR: ndim = ',ndim,' in set_labels_tipsy ***'
490 return
491 endif
492 if (ndimV <= 0 .or. ndimV > 3) then
493 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_tipsy ***'
494 return
495 endif
496
497 do i=1,ndim
498 ix(i) = i
499 enddo
500 ipmass = ndim + 1
501 ivx = ndim + 2
502 irho = ivx + ndim
503 !iutherm = irho + 1
504 label(irho+1) = 'temperature'
505 ih = irho + 2
506
507 label(ix(1:ndim)) = labelcoord(1:ndim,1)
508 label(ih) = 'h'
509 !if (iutherm > 0) label(iutherm) = 'temperature'
510 label(ipmass) = 'particle mass'
511 label(irho) = 'density'
512
513 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
514 !
515 !--set labels for each particle type
516 !
517 ntypes = 3
518 labeltype(1) = 'gas'
519 labeltype(2) = 'dark matter'
520 labeltype(3) = 'star'
521 UseTypeInRenderings(1) = .true.
522 UseTypeInRenderings(2) = .false.
523 UseTypeInRenderings(3) = .false.
524
525 end subroutine set_labels_tipsy
526
527 end module readdata_tipsy