Debian Codegraph @

src/read_data_ndspmhd.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-2014 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 ! the data is stored in the global array dat
28 !
29 ! THIS VERSION FOR DAN'S SPMHD CODE (BINARY DUMPS)
30 ! -> Now automatically handles single/double precision
31 !
32 ! >> this subroutine must return values for the following: <<
33 !
34 ! ncolumns : number of data columns
35 ! ndim, ndimV : number of spatial, velocity dimensions
36 ! nstepsread : number of steps read from this file
37 !
38 ! maxplot,maxpart,maxstep : dimensions of main data array
39 ! dat(maxplot,maxpart,maxstep) : main data array
40 !
41 ! npartoftype(maxstep) : number of particles of each type in each timestep
42 !
43 ! time(maxstep) : time at each step
44 ! gamma(maxstep) : gamma at each step
45 !
46 ! most of these values are stored in global arrays
47 ! in the module 'particle_data'
48 !-------------------------------------------------------------------------
49
50 module readdata_ndspmhd
51 use params, only:doub_prec,sing_prec
52 implicit none
53
54 public :: read_data_ndspmhd, set_labels_ndspmhd, file_format_is_ndspmhd
55
56 private
57 contains
58
59 subroutine read_data_ndspmhd(rootname,indexstart,ipos,nstepsread)
60 use particle_data, only:npartoftype,time,gamma,headervals,dat,maxpart,maxstep,maxcol,iamtype
61 use filenames, only:nfiles
62 use settings_data, only:ndim,ndimV,ncolumns,ncalc,icoords,iformat, &
63 buffer_data,iverbose,debugmode
64 use mem_allocation, only:alloc
65 use geometry, only:labelcoordsys
66 use system_utils, only:lenvironment
67 use labels, only:labeltype,print_types,headertags
68 integer, intent(in) :: indexstart,ipos
69 integer, intent(out) :: nstepsread
70 character(len=*), intent(in) :: rootname
71 character(len=len(rootname)+4) :: datfile
72 integer :: i,icol,ierr,iunit,ilen,j,ilast
73 integer :: ncol_max,ndim_max,npart_max,ndimV_max,nstep_max
74 integer :: npartin,ntotin,ncolstep,nparti,ntoti
75 integer, dimension(3) :: ibound
76 logical :: reallocate, singleprecision
77
78 real, dimension(3) :: xmin, xmax
79 integer, parameter :: max_header_vars = 9
80 real(doub_prec) :: header_dp(max_header_vars)
81 real(sing_prec) :: header_sp(max_header_vars)
82 real :: header(max_header_vars), hfact
83 real(doub_prec), dimension(:), allocatable :: dattempd
84 real(sing_prec), dimension(:), allocatable :: dattemp
85 integer, dimension(:), allocatable :: itype
86 character(len=20) :: geomfile
87
88 iunit = 11 ! file unit number
89 ndim_max = 1
90 ndimV_max = 1
91 nstepsread = 0
92 if (rootname(1:1) /= ' ') then
93 datfile = trim(rootname)
94 !print*,'rootname = ',rootname
95 else
96 print*,' **** no data read **** '
97 return
98 endif
99
100 if (iverbose >= 1) print "(1x,a)",'reading ndspmhd format'
101 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
102 !
103 !--open data file and read data
104 !
105 open(unit=iunit,iostat=ierr,file=datfile,status='old',form='unformatted')
106 if (ierr /= 0) then
107 print*,' *** Error opening '//trim(datfile)//' ***'
108 return
109 endif
110 !
111 !--read first header line
112 !
113 singleprecision = .false.
114 read(iunit,iostat=ierr,end=80) header_dp(1),npartin,ntotin,header_dp(2), &
115 header_dp(3),ndim_max,ndimV_max,ncol_max,iformat
116 ! print*,'time = ',timeind,' hfact = ',hfactind,' ndim=',ndim_max,'ncol=',ncol_max
117 ! print*,'npart = ',npartin,ntotin,geomfile
118 if (ierr /= 0 .or. ndim_max <= 0 .or. ndim_max > 3 &
119 .or. ndimV_max <= 0 .or. ndimV_max > 3 &
120 .or. ncol_max <= 0 .or. ncol_max > 100 &
121 .or. npartin <= 0 .or. npartin > 1e7 .or. ntotin <= 0 .or. ntotin > 1e7 &
122 .or. iformat < 0 .or. iformat > 10) then
123 !
124 !--try single precision
125 !
126 rewind(iunit)
127 read(iunit,iostat=ierr,end=80) header_sp(1),npartin,ntotin,header_sp(2), &
128 header_sp(3),ndim_max,ndimV_max,ncol_max,iformat
129 singleprecision = .true.
130 if (ierr /= 0 .or. ndim_max <= 0 .or. ndim_max > 3 &
131 .or. ndimV_max <= 0 .or. ndimV_max > 3 &
132 .or. ncol_max <= 0 .or. ncol_max > 100 &
133 .or. npartin <= 0 .or. npartin > 1e7 .or. ntotin <= 0 .or. ntotin > 1e7 &
134 .or. iformat < 0 .or. iformat > 10) then
135
136 print "(a)",' *** Error reading first header ***'
137 print*,' time = ',header_sp(1),' hfact = ',header_sp(3),' ndim=',ndim_max,'ncol=',ncol_max
138 close(iunit)
139 return
140 endif
141 endif
142 !
143 !--allocate memory for data arrays
144 !
145 if (buffer_data) then
146 nstep_max = max(nfiles,maxstep,indexstart)
147 else
148 nstep_max = max(1,maxstep,indexstart)
149 endif
150 npart_max = max(int(1.5*ntotin),maxpart)
151 if (.not.allocated(dat) .or. ntotin > maxpart &
152 .or. nstep_max > maxstep .or. ncol_max > maxcol) then
153 call alloc(npart_max,nstep_max,ncol_max+ncalc,mixedtypes=.true.)
154 endif
155 !
156 !--rewind file
157 !
158 rewind(iunit)
159
160 i = indexstart
161 nstepsread = 0
162
163 reallocate = .false.
164 npart_max = maxpart
165 nstep_max = maxstep
166 geomfile = ' '
167 !
168 !--read header line for this timestep
169 !
170 if (singleprecision) then
171 if (debugmode) print "(a)",'DEBUG: single precision dump'
172 read(iunit,iostat=ierr) header_sp(1),nparti,ntoti,header_sp(2), &
173 header_sp(3),ndim,ndimV,ncolstep,iformat,ibound(1:ndim), &
174 header_sp(4:3+2*ndim),ilen,geomfile(1:ilen)
175 header = real(header_sp)
176 else
177 if (debugmode) print "(a)",'DEBUG: double precision dump'
178 read(iunit,iostat=ierr) header_dp(1),nparti,ntoti,header_dp(2), &
179 header_dp(3),ndim,ndimV,ncolstep,iformat,ibound(1:ndim), &
180 header_dp(4:3+2*ndim),ilen,geomfile(1:ilen)
181 header = real(header_dp)
182 endif
183 if (ierr /= 0) then
184 print*,'*** error reading timestep header ***'
185 close(iunit)
186 return
187 else ! count this as a successfully read timestep, even if data is partial
188 nstepsread = nstepsread + 1
189 endif
190
191 time(i) = header(1)
192 gamma(i) = header(2)
193 hfact = header(3)
194 xmin(1:ndim) = header(4:3+ndim)
195 xmax(1:ndim) = header(4+ndim:4+2*ndim-1)
196 headertags(1:7) = (/'time ','npart','ntot ','gamma','hfact','ndim ','ndimV'/)
197 headervals(1:7,i) = (/time(i),real(nparti),real(ntoti),gamma(i),hfact,real(ndim),real(ndimV)/)
198 npartoftype(1,i) = nparti
199 npartoftype(3,i) = ntoti - nparti
200 if (iverbose >= 1) then
201 print "(a14,':',es10.3,a6,':',i8,a8,':',i8)",' time',time(i),'npart',nparti,'ntotal',ntoti
202 print "(a14,':',i8,a8,':',f8.4,a8,':',f8.4)",' ncolumns',ncolstep,'gamma',gamma(i),'hfact',hfact
203 print "(a14,':',i8,a8,':',i8)",'ndim',ndim,'ndimV',ndimV
204 else
205 print "(1x,a,':',es10.3,a8,':',i8,a8,':',i8)",'time',time(i),'npart',nparti,'ntotal',ntoti
206 endif
207 select case(geomfile(1:6))
208 case('cylrpz')
209 icoords = 2
210 case('sphrpt')
211 icoords = 3
212 case default
213 icoords = 1
214 end select
215 if (icoords /= 1) print "(a14,a)",' geometry: ',trim(geomfile)//' ('//trim(labelcoordsys(icoords))//')'
216 if (iverbose >= 1 .and. any(ibound(1:ndim) /= 0)) then
217 print "(a14,':',a15,' =',3(f8.4))",'boundaries','xmin',xmin(1:ndim)
218 print "(15x,a15,' =',3(f8.4))",'xmax',xmax(1:ndim)
219 endif
220 !
221 !--check for errors in timestep header
222 !
223 if (ndim > 3 .or. ndimV > 3) then
224 print*,'*** error in header: ndim or ndimV in file> 3'
225 nstepsread = nstepsread - 1
226 ndim = ndim_max
227 ndimV = ndimV_max
228 close(iunit)
229 return
230 endif
231 if (ndim > ndim_max) ndim_max = ndim
232 if (ndimV > ndimV_max) ndimV_max = ndimV
233
234 if (ncolstep /= ncol_max) then
235 print*,'*** Warning number of columns not equal for timesteps'
236 ncolumns = ncolstep
237 if (iverbose >= 1) print*,'ncolumns = ',ncolumns,ncol_max
238 if (ncolumns > ncol_max) ncol_max = ncolumns
239 endif
240 if (ncolstep > maxcol) then
241 reallocate = .true.
242 ncolumns = ncolstep
243 ncol_max = ncolumns
244 else
245 ncolumns = ncolstep
246 endif
247
248 if (ntoti > maxpart) then
249 !print*, 'ntot greater than array limits!!'
250 reallocate = .true.
251 npart_max = int(1.5*ntoti)
252 endif
253 if (i > maxstep) then
254 nstep_max = i + max(10,INT(0.1*nstep_max))
255 reallocate = .true.
256 endif
257 !
258 !--reallocate memory for main data array
259 !
260 if (reallocate) then
261 call alloc(npart_max,nstep_max,ncol_max+ncalc,mixedtypes=.true.)
262 endif
263
264
265 if (ntoti > 0) then
266 if (singleprecision) then
267 allocate(dattemp(ntoti))
268 else
269 allocate(dattempd(ntoti))
270 endif
271 do icol=1,ncolstep
272 if (singleprecision) then
273 read (iunit,iostat=ierr) dattemp(1:ntoti)
274 dat(1:ntoti,icol,i) = real(dattemp(1:ntoti))
275 else
276 read (iunit,iostat=ierr) dattempd(1:ntoti)
277 dat(1:ntoti,icol,i) = real(dattempd(1:ntoti))
278 endif
279 if (ierr /= 0) print "(a,i2,a)",'*** error reading column ',icol,' ***'
280 enddo
281 if (allocated(dattempd)) deallocate(dattempd)
282 if (allocated(dattemp)) deallocate(dattemp)
283
284 allocate(itype(ntoti))
285 read(iunit,iostat=ierr) itype(1:ntoti)
286 if (ierr /= 0) then
287 if (debugmode) print "(a)",'DEBUG: itype not found in dump file'
288 iamtype(1:nparti,i) = 1
289 iamtype(nparti+1:ntoti,i) = 3
290 else
291 !
292 !--assign SPLASH types from ndspmhd types
293 !
294 npartoftype(:,i) = 0
295 do j=1,ntoti
296 if (j > nparti) then
297 if (itype(j)==12) then
298 iamtype(j,i) = 4
299 npartoftype(4,i) = npartoftype(4,i) + 1
300 else
301 iamtype(j,i) = 3
302 npartoftype(3,i) = npartoftype(3,i) + 1
303 endif
304 elseif (itype(j)==2) then
305 iamtype(j,i) = 2
306 npartoftype(2,i) = npartoftype(2,i) + 1
307 else
308 iamtype(j,i) = 1
309 npartoftype(1,i) = npartoftype(1,i) + 1
310 endif
311 enddo
312 endif
313 if (allocated(itype)) deallocate(itype)
314 else
315 npartoftype(:,i) = 0
316 npartoftype(1,i) = 1
317 dat(:,:,i) = 0.
318 endif
319 !
320 !--close data file and return
321 !
322 close(unit=11)
323
324 ilast = i
325 !
326 !--ONE FLUID DUST: FAKE IT AS IF IT IS TWO FLUIDS
327 ! (copy the particles, then copy gas properties onto first lot, then dust properties onto second lot)
328 !
329 ncolumns = ncol_max
330 ndim = ndim_max
331 ndimV = ndimV_max
332
333 call set_labels_ndspmhd
334
335 !if (iformat==5 .and. .not.lenvironment('NSPLASH_BARYCENTRIC')) then
336 ! call fake_twofluids
337 ! iformat = 1
338 !endif
339
340 if (any(npartoftype(2:,ilast) > 0)) call print_types(npartoftype(:,ilast),labeltype)
341
342 if (debugmode) print*,'DEBUG> Read steps ',indexstart,'->',indexstart + nstepsread - 1, &
343 ' last step ntot = ',sum(npartoftype(:,indexstart+nstepsread-1))
344 return
345
346 80 continue
347 print*,' *** data file empty : no timesteps ***'
348 return
349
350 end subroutine read_data_ndspmhd
351
352 !!------------------------------------------------------------
353 !! set labels for each column of data
354 !!------------------------------------------------------------
355
356 subroutine set_labels_ndspmhd
357 use labels, only:ix,ivx,ih,irho,iutherm,ipmass,ipr,iBfirst, &
358 idivB,iJfirst,iamvec,labelvec,label,labeltype, &
359 irhorestframe,idustfrac,ideltav
360 use params
361 use settings_data, only:ndim,ndimV,iformat,ntypes, &
362 UseTypeInRenderings,ncolumns
363 use geometry, only:labelcoord
364 integer :: i,icol
365
366 if (ndim <= 0 .or. ndim > 3) then
367 print*,'*** ERROR: ndim = ',ndim,' in set_labels_ndspmhd ***'
368 return
369 endif
370 if (ndimV <= 0 .or. ndimV > 3) then
371 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_ndspmhd ***'
372 return
373 endif
374
375 do i=1,ndim
376 ix(i) = i
377 enddo
378 ivx = ndim + 1
379 ih = ndim + ndimV + 1 ! smoothing length
380 irho = ndim + ndimV + 2 ! location of rho in data array
381 iutherm = ndim + ndimV + 3 ! thermal energy
382 ipmass = ndim + ndimV + 4 ! particle mass
383
384 label(ix(1:ndim)) = labelcoord(1:ndim,1)
385 !
386 !--label vector quantities (e.g. velocity) appropriately
387 !
388 iamvec(ivx:ivx+ndimV-1) = ivx
389 labelvec(ivx:ivx+ndimV-1) = 'v'
390
391 label(irho) = '\gr'
392 label(iutherm) = 'u'
393 label(ih) = 'h '
394 label(ipmass) = 'particle mass'
395 label(ndim + ndimV+5) = '\ga'
396 label(ndim + ndimV+6) = '\ga\du'
397 icol = ndim+ndimV + 7
398 if (iformat==2 .or. iformat==4) then
399 !
400 !--mag field (vector)
401 !
402 label(icol) = '\ga\dB'
403 iBfirst = icol+1 ! location of Bx
404 iamvec(iBfirst:iBfirst+ndimV-1) = iBfirst
405 labelvec(iBfirst:iBfirst+ndimV-1) = 'B'
406 icol = icol + ndimV
407 !
408 !--more scalars
409 !
410 icol = icol + 1
411 label(icol) = '\psi'
412
413 icol = icol + 1
414 ipr = icol ! pressure
415 label(ipr) = 'P'
416
417 icol = icol + 1
418 label(icol) = 'div v'
419
420 icol = icol + 1
421 idivB = icol
422 label(idivB) = 'div B'
423 !
424 !--current density (vector)
425 !
426 iJfirst = icol + 1
427 iamvec(icol+1:icol+ndimV) = icol + 1
428 labelvec(icol+1:icol+ndimV) = 'J'
429 icol = icol + ndimV
430
431 icol = icol + 1
432 label(icol) = 'grad h'
433
434 iamvec(icol+1:icol+ndimV) = icol + 1
435 labelvec(icol+1:icol+ndimV) = 'force'
436 icol = icol + ndimV
437
438 iamvec(icol+1:icol+ndimV) = icol + 1
439 labelvec(icol+1:icol+ndimV) = 'A'
440 icol = icol + ndimV
441
442 else
443 ipr = icol ! pressure
444 label(ipr) = 'P'
445 icol = icol + 1
446 label(icol) = 'div v'
447 icol = icol + 1
448 label(icol) = 'grad h'
449
450 iamvec(icol+1:icol+ndimV) = icol + 1
451 labelvec(icol+1:icol+ndimV) = 'force'
452 icol = icol + ndimV
453 iBfirst = 0
454
455 if (ncolumns > 20) then
456 iamvec(icol+1:icol+ndimV) = icol + 1
457 labelvec(icol+1:icol+ndimV) = 'del^{2} v'
458 icol = icol + ndimV
459
460 iamvec(icol+1:icol+ndimV) = icol + 1
461 labelvec(icol+1:icol+ndimV) = 'grad (div {\bf v})'
462 icol = icol + ndimV
463 endif
464 endif
465 if (iformat==5) then
466 icol = icol + 1
467 label(icol) = 'Dust fraction'
468 idustfrac = icol
469 iamvec(icol+1:icol+ndimV) = icol + 1
470 labelvec(icol+1:icol+ndimV) = '\Deltav'
471 ideltav = icol + 1
472 icol = icol + ndimV
473 elseif (iformat > 2) then
474 irhorestframe = irho
475 icol = icol + 1
476 irho = icol
477 label(icol) = 'rho*'
478 !irho = icol
479 icol = icol + 1
480 label(icol) = 'sqrt g'
481 iamvec(icol+1:icol+ndimV) = icol + 1
482 labelvec(icol+1:icol+ndimV) = 'pmom'
483 icol = icol + ndimV
484 endif
485 !
486 !--set labels for each type of particles
487 !
488 ntypes = 4
489 labeltype(1) = 'gas'
490 labeltype(2) = 'dust'
491 labeltype(3) = 'ghost'
492 labeltype(4) = 'ghost (dust)'
493 UseTypeInRenderings(:) = .true.
494
495 !-----------------------------------------------------------
496
497 return
498 end subroutine set_labels_ndspmhd
499
500 !-----------------------------------------------------------
501 !
502 ! check if a file is in ndspmhd format
503 !
504 !-----------------------------------------------------------
505 logical function file_format_is_ndspmhd(filename) result(is_ndspmhd)
506 character(len=*), intent(in) :: filename
507 integer :: iunit,npartin,ntotin,ndim_max,ndimV_max,ncol_max,iformat,ierr
508 real(kind=doub_prec) :: header_dp(3)
509
510 is_ndspmhd = .false.
511 !
512 ! filename must end in .dat
513 !
514 if (index(filename,'.dat') <= 0) return
515 !
516 ! open file and read the first line
517 !
518 open(newunit=iunit,iostat=ierr,file=filename,status='old',form='unformatted')
519 if (ierr /= 0) return
520 !
521 !--read first header line
522 !
523 read(iunit,iostat=ierr) header_dp(1),npartin,ntotin,header_dp(2), &
524 header_dp(3),ndim_max,ndimV_max,ncol_max,iformat
525
526 if (ierr /= 0 .or. ndim_max <= 0 .or. ndim_max > 3 &
527 .or. ndimV_max <= 0 .or. ndimV_max > 3 &
528 .or. ncol_max <= 0 .or. ncol_max > 100 &
529 .or. npartin <= 0 .or. npartin > 1e7 .or. ntotin <= 0 .or. ntotin > 1e7 &
530 .or. iformat < 0 .or. iformat > 10) then
531 is_ndspmhd = .false.
532 else
533 is_ndspmhd = .true.
534 endif
535 close(iunit) ! close the file
536
537 end function file_format_is_ndspmhd
538
539 end module readdata_ndspmhd