Debian Codegraph @

src/read_data_mhutch.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-2013 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 SARAH MADDISON+MARK HUTCHISON'S DUSTY-SPH CODE
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_mhutch
51 implicit none
52
53 public :: read_data_mhutch, set_labels_mhutch
54
55 private
56 contains
57
58 subroutine read_data_mhutch(rootname,indexstart,ipos,nstepsread)
59 use particle_data, only:npartoftype,time,gamma,dat,maxpart,maxstep,maxcol,iamtype
60 use params
61 use settings_data, only:ndim,ndimV,ncolumns,ncalc,iverbose,debugmode
62 use mem_allocation, only:alloc
63 use system_utils, only:lenvironment
64 integer, intent(in) :: indexstart,ipos
65 integer, intent(out) :: nstepsread
66 character(len=*), intent(in) :: rootname
67 character(len=len(rootname)+4) :: datfile
68 integer :: i,icol,ierr,iunit,j,ilast
69 integer :: ihead1,ihead2,ihead3 ! # of items on header lines
70 integer :: ncol_max,ndim_max,npart_max,ndimV_max,nstep_max
71 integer :: ncolstep,np,nstepsinfile
72
73 integer :: norigin,ncr,istart,iout,nmlmax,index,ratio
74 integer :: fluidsw,itrace
75 integer, dimension(:), allocatable :: head1
76 integer, dimension(:), allocatable :: head2
77 real(doub_prec), dimension(:), allocatable :: head3
78 real(doub_prec) :: timei,dt,sopt0
79 real(doub_prec) :: gammai
80 real(doub_prec), dimension(:), allocatable :: dattemp
81 integer, dimension(:), allocatable :: iam
82 integer, dimension(:), allocatable :: iwas
83
84 iunit = 11 ! file unit number
85 ndim_max = 1
86 ndimV_max = 1
87 nstepsread = 0
88 if (rootname(1:1) /= ' ') then
89 datfile = trim(rootname)
90 !print*,'rootname = ',rootname
91 else
92 print*,' **** no data read **** '
93 return
94 endif
95
96 if (iverbose >= 1) print "(1x,a)",'reading Maddison/Hutchison format'
97 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
98 !
99 !--open data file and read data
100 !
101 open(unit=iunit,iostat=ierr,file=datfile,status='old',form='unformatted')
102 if (ierr /= 0) then
103 print*,' *** Error opening '//trim(datfile)//' ***'
104 return
105 endif
106 !
107 !--read first header line
108 !
109 read(iunit,iostat=ierr,end=80) ihead1,ihead2,ihead3,ncolstep,np
110
111 allocate(head1(ihead1))
112 allocate(head2(ihead2))
113 allocate(head3(ihead3))
114
115 read(iunit,iostat=ierr,end=80) head1(1:ihead1)
116 read(iunit,iostat=ierr,end=80) head2(1:ihead2)
117 read(iunit,iostat=ierr,end=80) head3(1:ihead3)
118
119 ! Header one variables
120 norigin = head1(1)
121 ncr = head1(2)
122 istart = head1(3)
123 iout = head1(4)
124 nmlmax = head1(5)
125 index = head1(6)
126 ratio = head1(7)
127 itrace = head1(8)
128
129 ! Header two variables
130 ndim = abs(head2(1))
131 fluidsw = head2(2)
132 !velsw = head2(3)
133 !dragsw = head2(4)
134 !Bkernsw = head2(5)
135 !Dkernsw = head2(6)
136 !freesw = head2(7)
137 !stopgas = head2(8)
138 !coolsw = head2(9)
139 !interpsw = head2(10)
140 !voidsw = head2(11)
141 !phasesw = head2(12)
142 !gravsw = head2(13)
143 !photosw = head2(14)
144 !eossw = head2(15)
145 !masssw = head2(16)
146 !usenumdens = head2(17)
147 !varhsw = head2(18)
148 !tracesw = head2(19)
149 !timesw = head2(20)
150 !growsw = head2(21)
151 !shocksw = head2(22)
152 !wavesw = head2(23)
153 !plotsw = head2(24)
154 !disc1dsw = head2(25)
155
156 ! Header three variables
157 dt = head3(1)
158 timei = head3(2)
159 sopt0 = head3(3)
160 gammai = head3(4)
161
162 deallocate(head1)
163 deallocate(head2)
164 deallocate(head3)
165
166 ncolstep = ncolstep-2 ! minus 2 because iwas and iam read individually
167 ndimV = 3 ! always have 3 velocity components written to file
168
169 print "(a,i2,a,f8.4)",' ncolumns: ',ncolstep,' gamma: ',gammai
170
171 !
172 !--check for basic errors in first line
173 !
174
175 if (ierr /= 0 .or. ncr < 0 .or. istart < 0 &
176 .or. iout < 0 .or. nmlmax < 0 .or. index < 0 .or. ratio < 0) then
177
178 print "(a)",' *** Error reading header ***'
179 print*,' norigin = ',norigin,' ncr = ',ncr,' istart =',istart,' iout = ',iout
180 print*,' nmlmax = ',nmlmax,' index = ',index,' ratio =',ratio
181 close(iunit)
182 return
183 endif
184 !
185 !--Check for errors
186 !
187 if (ierr /= 0 .or. np < 0 .or. np > 1.e9 .or. itrace > np) then
188 print*,'n = ',np,' dt = ',dt,' time = ',timei,' i = ',itrace
189 print*,'*** error reading timestep header ***'
190 close(iunit)
191 return
192 endif
193
194 !
195 !--check for errors in 3rd line
196 !
197 if (ndim > 3 .or. ndimV > 3) then
198 print*,'*** error in header: ndim or ndimV in file > 3'
199 ndim = 3
200 ndimV = 3
201 close(iunit)
202 return
203 endif
204
205 nstepsinfile = 1 ! nmlmax/iout
206 nstep_max = 1 ! max(nstepsinfile,maxstep)
207 nstepsread = 0
208 npart_max = maxpart
209 ncol_max = ncolstep
210 !
211 !--read first step
212 !
213 over_steps: do i = indexstart,indexstart + nstepsinfile - 1
214 !
215 !--allocate memory for data arrays
216 !
217 nstep_max = nstepsinfile !max(nstep_max,nfiles,maxstep,indexstart)
218 npart_max = max(np,maxpart,norigin)
219
220 if (.not.allocated(dat) .or. np > maxpart &
221 .or. nstep_max > maxstep .or. ncol_max > maxcol) then
222 call alloc(npart_max,nstep_max,ncolstep+ncalc,mixedtypes=.true.)
223 endif
224 !
225 !--now that memory is allocated, put header quantities -> splash quantities
226 !
227 time(i) = timei
228 gamma(i) = gammai
229 npartoftype(1,i) = np
230 if (iverbose >= 1) then
231 print "(a,i5,a,f8.4,a,i8,a,f8.4)",' step:',i,' time:',time(i),' npart:',np,' dt:',dt
232 else
233 print "(a,i5,a,f8.4,a,i8,a,i8)",' step:',i,' time:',time(i),' npart:',np
234 endif
235
236 if (ncolstep /= ncol_max) then
237 print*,'*** Warning number of columns not equal for timesteps'
238 ncolumns = ncolstep
239 if (iverbose >= 1) print*,'ncolumns = ',ncolumns,ncol_max
240 if (ncolumns > ncol_max) ncol_max = ncolumns
241 endif
242
243 ncolumns = ncolstep
244 nstepsread = nstepsread + 1
245
246 !
247 !--read data for this timestep
248 !
249 allocate(iwas(np))
250 read(iunit,iostat=ierr,end=80) iwas(1:np)
251
252 if ( any(iwas < 1).or.any(iwas > norigin) ) then
253 do j = 1,np
254 iwas(j) = j
255 enddo
256 endif
257
258 npartoftype(:,i) = 0
259 allocate(iam(norigin))
260
261 iam(:) = 3
262
263 read(iunit,iostat=ierr,end=80) iam(iwas(:))
264
265 do j=1,norigin
266 select case(iam(j))
267 case(1) ! GAS
268 npartoftype(1,i) = npartoftype(1,i) + 1
269 iamtype(j,i) = 1_int1
270 case(0) ! DUST
271 npartoftype(2,i) = npartoftype(2,i) + 1
272 iamtype(j,i) = 2_int1
273 case default ! DELETED PARTICLES
274 npartoftype(3,i) = npartoftype(3,i) + 1
275 iamtype(j,i) = 3_int1
276 end select
277 enddo
278 deallocate(iam)
279
280 if (kind(dat) /= kind(dattemp)) then
281 if (debugmode) print*,' converting kind from ',kind(dattemp),' to ',kind(dat)
282 allocate(dattemp(norigin))
283
284 !dattemp(:) = 0D0
285
286 !--convert precision
287 !do icol=1,ncolstep-1 ! all columns except h
288 do icol=1,ncolstep ! all columns with h
289 read(iunit,iostat=ierr,end=80) dattemp(iwas(:))
290 dat(1:norigin,icol,i) = real(dattemp)
291 enddo
292 deallocate(dattemp)
293 else
294 !--read directly into dat array if data types are the same
295 !do icol=1,ncolstep-1 ! all columns except h
296 do icol=1,ncolstep ! all columns with h
297 read(iunit,iostat=ierr,end=80) dat(iwas(:),icol,i)
298 enddo
299 endif
300
301 deallocate(iwas)
302 enddo over_steps
303
304 close(unit=11)
305
306 ilast = indexstart+nstepsinfile - 1
307 ncolumns = ncol_max
308
309 call set_labels_mhutch
310
311 if ( fluidsw < 0 .and. .not.lenvironment('NSPLASH_BARYCENTRIC') ) then
312 call fake_twofluids
313 endif
314
315 if (npartoftype(2,ilast) > 0) then
316 print*,' ngas = ',npartoftype(1,ilast),' ndust = ',npartoftype(2,ilast)
317 if (npartoftype(3,ilast) > 0) print*,' nunknown = ',npartoftype(3,ilast)
318 endif
319 if (debugmode) print*,'DEBUG> Read steps ',indexstart,'->',indexstart + nstepsread - 1, &
320 ' last step ntot = ',sum(npartoftype(:,indexstart+nstepsread-1))
321 return
322
323 80 continue
324 print*,' *** data file empty : no timesteps ***'
325 return
326
327 contains
328
329 !--------------------------------------------
330 subroutine fake_twofluids
331 use labels, only:idustfrac,irho,ix,ih,ipmass,ivx,ideltav
332 integer :: ndust,jdust
333 integer :: ntoti
334 real :: rhodust,rhogas,rhotot,dustfraci,pmassgas,pmassdust,pmassj
335 real, dimension(ndimV) :: veli,vgas,vdust,deltav
336
337 !integer :: itemp
338 !integer :: ifx,ify,ifz
339 integer :: ir_grain
340 !integer :: ics
341 !integer :: idhdt
342 !integer :: idepsdt
343
344 !do i=1,ndim
345 ! ix(i) = i ! x,y,z positions
346 !enddo
347 !!--2D means x-z
348 !if (ndim==2) ix(2) = 3 ! x,z positions if 2D
349 !ivx = 4 ! velocity (vector so it takes 3 rows)
350 !irho = 7 ! density
351 !ipmass = 8 ! mass
352 !iutherm = 9 ! thermal energy
353 !idendt = 10 ! time derivative of thermal energy
354 !itemp = 11 ! temperature
355 !ifx = 12 ! force in x direction
356 !ify = 13 ! force in y direction
357 !ifz = 14 ! force in z direction
358 ir_grain = 15 ! grain size
359 !ics = 16 ! sound speed
360 !ih = 17 ! smoothing length
361 !idhdt = 18 ! time derivative of h
362 !idustfrac = 19 ! dust fraction
363 !idepsdt = 20 ! time derivative of dust fraction
364
365
366 if (idustfrac > 0 .and. irho > 0) then
367 do i=indexstart,indexstart+nstepsread-1
368 ntoti = sum(npartoftype(:,i))
369 if (.not.allocated(dat) .or. (ntoti + npartoftype(1,i)) > maxpart) then
370 call alloc(ntoti + npartoftype(1,i),maxstep,maxcol,mixedtypes=.true.)
371 endif
372 ndust = 0
373 !--zero the properties of newly created dust particles
374 dat(ntoti+1:ntoti+npartoftype(1,i),:,i) = 0.
375 do j=1,ntoti
376 if (iamtype(j,i)==1) then
377 ndust = ndust + 1 ! one dust particle for every gas particle
378 rhotot = dat(j,irho,i)
379 dustfraci = dat(j,idustfrac,i)
380 rhogas = rhotot*(1. - dustfraci)
381 rhodust = rhotot*dustfraci
382 !--replace global properties with gas-only stuff
383 dat(j,irho,i) = rhogas
384 !--copy x, smoothing length onto dust particle
385 jdust = ntoti + ndust
386 !--fix dust fraction for viewing purposes
387 !dat(jdust,idustfrac,i) = dustfraci
388 !dat(j,idustfrac,i) = 1.-dustfraci
389 !--fix grain size to be for dust only
390 dat(jdust,ir_grain,i) = dat(j,ir_grain,i)
391 dat(j,ir_grain,i) = 0.
392
393 !--fill in dust properties
394 if (ndim > 0) dat(jdust,ix(1:ndim),i) = dat(j,ix(1:ndim),i)
395 if (ih > 0) dat(jdust,ih,i) = dat(j,ih,i)
396 if (irho > 0) dat(jdust,irho,i) = rhodust
397 iamtype(ntoti + ndust,i) = 2
398
399 !--particle masses
400 if (ipmass > 0) then
401 pmassj = dat(j,ipmass,i)
402 pmassgas = pmassj*(1. - dustfraci)
403 pmassdust = pmassj*dustfraci
404 dat(j,ipmass,i) = pmassgas
405 dat(jdust,ipmass,i) = pmassdust
406 endif
407
408 !--velocities
409 if (ideltav > 0 .and. ivx > 0 .and. ndimV > 0) then
410 veli(:) = dat(j,ivx:ivx+ndimV-1,i)
411 deltav(:) = dat(j,ideltav:ideltav+ndimV-1,i)
412 if ( rhodust < 1.e-30 ) then
413 vgas(:) = veli(:)
414 !vdust(:) = 0.
415 vdust(:) = 1.e10 ! Dirty way to clean up axis
416 else
417 vgas(:) = veli(:) - rhodust/rhotot*deltav(:)
418 vdust(:) = veli(:) + rhogas/rhotot*deltav(:)
419 endif
420 dat(j ,ivx:ivx+ndimV-1,i) = vgas(:)
421 dat(jdust,ivx:ivx+ndimV-1,i) = vdust(:)
422 endif
423 endif
424 enddo
425 if (iverbose >= 1) then
426 print "(a,i10,a)",' Creating ',ndust,' fictional dust particles...'
427 print "(a)",' (set NSPLASH_BARYCENTRIC=yes to plot barycentric values)'
428 endif
429 npartoftype(2,i) = npartoftype(2,i) + ndust
430 enddo
431 else
432 print "(a)",' ERROR: could not locate dust-to-gas ratio and/or density'
433 endif
434
435 end subroutine fake_twofluids
436
437 end subroutine read_data_mhutch
438
439 !------------------------------------------------------------
440 ! set labels for each column of data
441 !------------------------------------------------------------
442 subroutine set_labels_mhutch
443 use labels, only:ix,ivx,ih,irho,iutherm,ipmass, &
444 iamvec,labelvec,label,labeltype, &
445 idustfrac,ideltav
446 use params
447 use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings
448 use geometry, only:labelcoord
449 integer :: i
450
451 integer :: idendt,itemp
452 integer :: ifx,ify,ifz
453 integer :: ir_grain
454 integer :: ics
455 integer :: idhdt
456 integer :: idepsdt
457 integer :: iddeltavdt
458 integer :: itcour
459 integer :: itstop
460 integer :: itdiff
461
462 if (ndim <= 0 .or. ndim > 3) then
463 print*,'*** ERROR: ndim = ',ndim,' in set_labels_mhutch ***'
464 return
465 endif
466 if (ndimV <= 0 .or. ndimV > 3) then
467 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_mhutch ***'
468 return
469 endif
470
471 do i=1,ndim
472 ix(i) = i ! x,y,z positions
473 enddo
474 !--2D means x-z
475 if (ndim==2) ix(2) = 3 ! x,z positions if 2D
476 ivx = 4 ! velocity (vector so it takes 3 rows)
477 irho = 7 ! density
478 ipmass = 8 ! mass
479 iutherm = 9 ! thermal energy
480 idendt = 10 ! time derivative of thermal energy
481 itemp = 11 ! temperature
482 ifx = 12 ! force in x direction
483 ify = 13 ! force in y direction
484 ifz = 14 ! force in z direction
485 ir_grain = 15 ! grain size
486 ics = 16 ! sound speed
487 ih = 17 ! smoothing length
488 idhdt = 18 ! time derivative of h
489 idustfrac = 19 ! dust fraction
490 idepsdt = 20 ! time derivative of dust fraction
491
492 ideltav = 21
493 iddeltavdt = 24
494
495 itcour = 27
496 itstop = 28
497 itdiff = 29
498
499
500 !!TESTING
501 ! itemp = 10 ! temperature
502 ! ifx = 11 ! force in x direction
503 ! ify = 12 ! force in y direction
504 ! ifz = 13 ! force in z direction
505 ! ir_grain = 14 ! grain size
506 ! ics = 15 ! sound speed
507 ! ih = 16 ! smoothing length
508 ! idhdt = 17 ! time derivative of h
509 ! idustfrac = 18 ! dust fraction
510 ! idepsdt = 19 ! time derivative of dust fraction
511 !
512 ! ideltav = 20
513 !!TESTING
514
515
516 iamvec(ifx:ifz) = ifx
517 iamvec(ivx:ivx+ndimV-1) = ivx
518 labelvec(ivx:ivx+ndimV-1) = 'v'
519 labelvec(ifx:ifz) = 'f'
520
521 label(1:3) = labelcoord(1:3,1)
522 label(ivx:ivx+ndimV-1) = 'v_'//labelcoord(1:3,1)
523 label(irho) = '\rho'
524 label(ipmass) = 'm_{particle}'
525 label(iutherm) = 'u'
526 label(idendt) = 'du/dt'
527 label(itemp) = 'Temp'
528 label(ifx:ifz) = 'f_'//labelcoord(1:3,1)
529 label(ir_grain) = 's_{grain}'
530 label(ics) = 'cs'
531 label(ih) = 'h'
532 label(idhdt) = 'dh/dt'
533 label(idustfrac) = '\epsilon'
534 label(idepsdt) = 'd\epsilon/dt'
535 label(ideltav) = '\Delta v_x'
536 label(ideltav+1) = '\Delta v_y'
537 label(ideltav+2) = '\Delta v_z'
538 label(iddeltavdt) = 'd\Delta v_x/dt'
539 label(iddeltavdt+1) = 'd\Delta v_y/dt'
540 label(iddeltavdt+2) = 'd\Delta v_z/dt'
541
542 label(itcour) = 't_{cour}'
543 label(itstop) = 't_{stop}'
544 label(itdiff) = 't_{diff}'
545
546 !
547 !--set labels for each type of particles
548 !
549 ntypes = 3
550 labeltype(1) = 'gas'
551 labeltype(2) = 'dust'
552 labeltype(3) = 'unknown'
553 UseTypeInRenderings(1) = .true.
554 UseTypeInRenderings(2) = .true.
555 UseTypeInRenderings(3) = .false.
556
557 end subroutine set_labels_mhutch
558
559 end module readdata_mhutch