| 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 |
! THIS VERSION IS FOR READING UNFORMATTED OUTPUT FROM MATTHEW BATE'S CODE |
| 28 |
! (ie. STRAIGHT FROM THE DATA DUMP) |
| 29 |
! |
| 30 |
! *** CONVERTS TO SINGLE PRECISION *** |
| 31 |
! |
| 32 |
! the data is stored in the global array dat |
| 33 |
! |
| 34 |
! >> this subroutine must return values for the following: << |
| 35 |
! |
| 36 |
! ncolumns : number of data columns |
| 37 |
! ndim, ndimV : number of spatial, velocity dimensions |
| 38 |
! nstepsread : number of steps read from this file |
| 39 |
! |
| 40 |
! maxplot,maxpart,maxstep : dimensions of main data array |
| 41 |
! dat(maxplot,maxpart,maxstep) : main data array |
| 42 |
! |
| 43 |
! npartoftype(1:6,maxstep) : number of particles of each type in each timestep |
| 44 |
! |
| 45 |
! time(maxstep) : time at each step |
| 46 |
! gamma(maxstep) : gamma at each step |
| 47 |
! |
| 48 |
! most of these values are stored in global arrays |
| 49 |
! in the module 'particle_data' |
| 50 |
!------------------------------------------------------------------------- |
| 51 |
|
| 52 |
module readdata_mbate |
| 53 |
implicit none |
| 54 |
|
| 55 |
public :: read_data_mbate, set_labels_mbate |
| 56 |
|
| 57 |
private |
| 58 |
|
| 59 |
contains |
| 60 |
|
| 61 |
subroutine read_data_mbate(rootname,indexstart,ipos,nstepsread) |
| 62 |
use particle_data |
| 63 |
use params |
| 64 |
use settings_data, only:ndim,ndimV,ncolumns,ncalc,buffer_steps_in_file |
| 65 |
use settings_units, only:units=>units_default,get_nearest_length_unit,get_nearest_time_unit |
| 66 |
use labels, only:unitslabel=>unitslabel_default,headertags |
| 67 |
use mem_allocation |
| 68 |
integer, intent(in) :: indexstart,ipos |
| 69 |
integer, intent(out) :: nstepsread |
| 70 |
character(len=*), intent(in) :: rootname |
| 71 |
integer, parameter :: maxptmass = 1000 |
| 72 |
real, parameter :: pi=4.*atan(1.) |
| 73 |
integer :: i,j,ifile,ierr,ipart |
| 74 |
integer :: npart_max,nstep_max,ncolstep,npart,nptmassi,nunknown |
| 75 |
logical :: iexist,doubleprec |
| 76 |
|
| 77 |
character(len=len(rootname)+10) :: dumpfile |
| 78 |
integer :: nprint, n1, n2, nptmass, nstepsalloc |
| 79 |
integer, dimension(:), allocatable :: isteps, iphase |
| 80 |
integer, dimension(maxptmass) :: listpm |
| 81 |
|
| 82 |
!--use these lines if dump is double precision |
| 83 |
real(doub_prec), dimension(:,:), allocatable :: dattemp |
| 84 |
real(doub_prec) :: udisti,umassi,utimei |
| 85 |
real(doub_prec) :: timei, gammai |
| 86 |
real(doub_prec) :: rhozero, RK2 |
| 87 |
real(doub_prec) :: escap,tkin,tgrav,tterm |
| 88 |
real(doub_prec) :: dtmax,unitx |
| 89 |
character(len=20) :: unitlabelx |
| 90 |
|
| 91 |
!--use these lines for single precision |
| 92 |
real, dimension(:,:), allocatable :: dattemps |
| 93 |
real :: timesi, gammasi |
| 94 |
real :: rhozeros, RK2s |
| 95 |
real :: escaps,tkins,tgravs,tterms |
| 96 |
real :: dtmaxs,tcomp |
| 97 |
|
| 98 |
nstepsread = 0 |
| 99 |
nstep_max = 0 |
| 100 |
npart_max = maxpart |
| 101 |
ifile = 1 |
| 102 |
buffer_steps_in_file = .true. |
| 103 |
|
| 104 |
dumpfile = trim(rootname) |
| 105 |
! |
| 106 |
!--check if data file exists |
| 107 |
! |
| 108 |
inquire(file=dumpfile,exist=iexist) |
| 109 |
if (.not.iexist) then |
| 110 |
print "(a)",' *** error: '//trim(dumpfile)//': file not found ***' |
| 111 |
return |
| 112 |
endif |
| 113 |
! |
| 114 |
!--fix number of spatial dimensions |
| 115 |
! |
| 116 |
ndim = 3 |
| 117 |
ndimV = 3 |
| 118 |
ncolstep = 11 ! number of columns in file |
| 119 |
ncolumns = ncolstep |
| 120 |
! |
| 121 |
!--allocate memory initially |
| 122 |
! |
| 123 |
nstep_max = max(nstep_max,indexstart,1) |
| 124 |
|
| 125 |
j = indexstart |
| 126 |
nstepsread = 0 |
| 127 |
|
| 128 |
print "(1x,a)",'reading Matthew Bate''s/Willy Benz''s old SPH code format (on unit 15)' |
| 129 |
write(*,"(26('>'),1x,a,1x,26('<'))") trim(dumpfile) |
| 130 |
! |
| 131 |
!--open the (unformatted) binary file and read the number of particles |
| 132 |
! |
| 133 |
open(unit=15,iostat=ierr,file=dumpfile,status='old',form='unformatted') |
| 134 |
if (ierr /= 0) then |
| 135 |
print "(a)",'*** ERROR OPENING '//trim(dumpfile)//' ***' |
| 136 |
else |
| 137 |
! |
| 138 |
!--read the number of particles in the first step, |
| 139 |
! allocate memory and rewind |
| 140 |
! |
| 141 |
read(15,end=55,iostat=ierr) udisti,umassi,utimei,nprint,n1,n2,timei,gammai,rhozero,RK2 |
| 142 |
doubleprec = .true. |
| 143 |
!--try single precision if non-sensible values for time, gamma etc. |
| 144 |
if (ierr /= 0 .or. timei < 0. .or. timei > 1e30 & |
| 145 |
.or. gammai < 1. .or. gammai > 10. & |
| 146 |
.or. rhozero < 0. .or. RK2 < 0. .or. RK2 > 1.e10) then |
| 147 |
doubleprec = .false. |
| 148 |
endif |
| 149 |
nstepsalloc = 1 |
| 150 |
do while (ierr == 0) |
| 151 |
npart_max = max(npart_max,nprint) |
| 152 |
nstepsalloc = nstepsalloc + 1 |
| 153 |
read(15,iostat=ierr) udisti,umassi,utimei,nprint |
| 154 |
enddo |
| 155 |
ierr = 0 |
| 156 |
if (.not.allocated(dat) .or. nprint > npart_max) then |
| 157 |
npart_max = max(npart_max,INT(1.1*nprint)) |
| 158 |
call alloc(npart_max,nstepsalloc,ncolstep+ncalc) |
| 159 |
endif |
| 160 |
|
| 161 |
rewind(15) |
| 162 |
endif |
| 163 |
if (ierr /= 0) then |
| 164 |
print "(a)",'*** ERROR READING TIMESTEP HEADER ***' |
| 165 |
else |
| 166 |
! |
| 167 |
!--loop over the timesteps in this file |
| 168 |
! |
| 169 |
over_steps_in_file: do |
| 170 |
!npart_max = max(npart_max,nprint) |
| 171 |
! |
| 172 |
!--allocate/reallocate memory if j > maxstep |
| 173 |
! |
| 174 |
if (j > maxstep) then |
| 175 |
!if (nstepsread > 2) then |
| 176 |
! nstepsalloc = j + 2*nstepsread |
| 177 |
!else |
| 178 |
! nstepsalloc = j |
| 179 |
!endif |
| 180 |
call alloc(maxpart,nstepsalloc,maxcol) |
| 181 |
endif |
| 182 |
! |
| 183 |
!--allocate integer arrays required for data read |
| 184 |
! |
| 185 |
if (allocated(isteps)) deallocate(isteps) |
| 186 |
allocate(isteps(npart_max),stat=ierr) |
| 187 |
if (ierr /= 0) print*,'not enough memory in read_data_mbate' |
| 188 |
|
| 189 |
if (allocated(iphase)) deallocate(iphase) |
| 190 |
allocate(iphase(npart_max),stat=ierr) |
| 191 |
if (ierr /= 0) print*,'not enough memory in read_data_mbate' |
| 192 |
! |
| 193 |
!--now read the timestep data in the dumpfile |
| 194 |
! |
| 195 |
write(*,"(a,i5,a)",advance="no") '| step ',j,': ' |
| 196 |
|
| 197 |
if (doubleprec) then |
| 198 |
print "(a)",'double precision dump' |
| 199 |
! |
| 200 |
!--allocate a temporary array for (double precision) variables |
| 201 |
! |
| 202 |
if (allocated(dattemp)) deallocate(dattemp) |
| 203 |
allocate(dattemp(npart_max,ncolstep),stat=ierr) |
| 204 |
if (ierr /= 0) print*,'not enough memory in read_data_mbate' |
| 205 |
|
| 206 |
read(15,end=55,iostat=ierr) udisti, umassi, utimei, & |
| 207 |
nprint, n1, n2, timei, gammai, rhozero, RK2, & |
| 208 |
(dattemp(i,7), i=1, nprint),escap, tkin, tgrav, tterm, & |
| 209 |
(dattemp(i,1), i=1, nprint), (dattemp(i,2), i=1, nprint), & |
| 210 |
(dattemp(i,3), i=1, nprint), (dattemp(i,4), i=1, nprint), & |
| 211 |
(dattemp(i,5), i=1, nprint), (dattemp(i,6), i=1, nprint), & |
| 212 |
(dattemp(i,8), i=1, nprint), (dattemp(i,9), i=1, nprint), & |
| 213 |
(dattemp(i,10), i=1, nprint), (dattemp(i,11),i=1,nprint), & |
| 214 |
dtmax, (isteps(i), i=1,nprint), (iphase(i),i=1,nprint), & |
| 215 |
nptmass, (listpm(i), i=1,nptmass) |
| 216 |
else |
| 217 |
! |
| 218 |
!--allocate a temporary array for (double precision) variables |
| 219 |
! |
| 220 |
if (allocated(dattemps)) deallocate(dattemps) |
| 221 |
allocate(dattemps(npart_max,ncolstep),stat=ierr) |
| 222 |
if (ierr /= 0) print*,'not enough memory in read_data_mbate' |
| 223 |
|
| 224 |
print "(a)",'single precision dump' |
| 225 |
read(15,end=55,iostat=ierr) udisti, umassi, utimei, & |
| 226 |
nprint, n1, n2, timesi, gammasi, rhozeros, RK2s, & |
| 227 |
(dattemps(i,7), i=1, nprint),escaps, tkins, tgravs, tterms, & |
| 228 |
(dattemps(i,1), i=1, nprint), (dattemps(i,2), i=1, nprint), & |
| 229 |
(dattemps(i,3), i=1, nprint), (dattemps(i,4), i=1, nprint), & |
| 230 |
(dattemps(i,5), i=1, nprint), (dattemps(i,6), i=1, nprint), & |
| 231 |
(dattemps(i,8), i=1, nprint), (dattemps(i,9), i=1, nprint), & |
| 232 |
(dattemps(i,10), i=1, nprint), (dattemps(i,11),i=1,nprint), & |
| 233 |
dtmaxs, (isteps(i), i=1,nprint), (iphase(i),i=1,nprint), & |
| 234 |
nptmass, (listpm(i), i=1,nptmass) |
| 235 |
endif |
| 236 |
! |
| 237 |
!--set transformation factors between code units/real units |
| 238 |
! |
| 239 |
call get_nearest_time_unit(utimei,unitx,unitslabel(0)) |
| 240 |
units(0) = unitx ! convert to real*4 |
| 241 |
call get_nearest_length_unit(udisti,unitx,unitlabelx) |
| 242 |
units(1:3) = unitx |
| 243 |
unitslabel(1:3) = unitlabelx |
| 244 |
units(4:6) = udisti/utimei |
| 245 |
unitslabel(4:6) = ' [cm/s]' |
| 246 |
units(7) = unitx |
| 247 |
unitslabel(7) = unitlabelx |
| 248 |
units(8) = (udisti/utimei)**2 |
| 249 |
unitslabel(8) = ' [erg/g]' |
| 250 |
units(9) = umassi |
| 251 |
unitslabel(9) = ' [g]' |
| 252 |
units(10) = umassi/udisti**3 |
| 253 |
unitslabel(10) = ' [g/cm^3]' |
| 254 |
! |
| 255 |
!--convert to single precision and separate pt masses from normal particles |
| 256 |
! |
| 257 |
ipart = 0 |
| 258 |
do i=1,nprint |
| 259 |
if (iphase(i)==0) then |
| 260 |
ipart = ipart + 1 |
| 261 |
if (doubleprec) then |
| 262 |
dat(ipart,1:ncolstep,j) = real(dattemp(i,1:ncolstep)) |
| 263 |
else |
| 264 |
dat(ipart,1:ncolstep,j) = dattemps(i,1:ncolstep) |
| 265 |
endif |
| 266 |
endif |
| 267 |
enddo |
| 268 |
npart = ipart |
| 269 |
! |
| 270 |
!--place point masses after normal particles |
| 271 |
! |
| 272 |
nptmassi = 0 |
| 273 |
do i=1,nprint |
| 274 |
if (iphase(i) >= 1) then |
| 275 |
ipart = ipart + 1 |
| 276 |
nptmassi = nptmassi + 1 |
| 277 |
if (doubleprec) then |
| 278 |
dat(ipart,1:ncolstep,j) = real(dattemp(i,1:ncolstep)) |
| 279 |
else |
| 280 |
dat(ipart,1:ncolstep,j) = dattemps(i,1:ncolstep) |
| 281 |
endif |
| 282 |
endif |
| 283 |
enddo |
| 284 |
if (nptmass > 0) print*,' Number of point masses = ',nptmass |
| 285 |
if (nptmassi /= nptmass) print *,'WARNING: nptmass from iphase =',nptmassi,'not equal to nptmass' |
| 286 |
! |
| 287 |
!--put any others as unknown |
| 288 |
! |
| 289 |
nunknown = 0 |
| 290 |
do i=1,nprint |
| 291 |
if (iphase(i) < 0) then |
| 292 |
ipart = ipart + 1 |
| 293 |
nunknown = nunknown + 1 |
| 294 |
if (doubleprec) then |
| 295 |
dat(ipart,1:ncolstep,j) = real(dattemp(i,1:ncolstep)) |
| 296 |
else |
| 297 |
dat(ipart,1:ncolstep,j) = dattemps(i,1:ncolstep) |
| 298 |
endif |
| 299 |
endif |
| 300 |
enddo |
| 301 |
if (nunknown > 0) print *,nunknown,' particles of unknown type (probably dead)' |
| 302 |
|
| 303 |
if (allocated(dattemp)) deallocate(dattemp) |
| 304 |
if (allocated(dattemps)) deallocate(dattemps) |
| 305 |
if (allocated(isteps)) deallocate(isteps) |
| 306 |
if (allocated(iphase)) deallocate(iphase) |
| 307 |
|
| 308 |
npartoftype(1,j) = npart |
| 309 |
npartoftype(2,j) = nptmassi |
| 310 |
npartoftype(3,j) = nunknown |
| 311 |
|
| 312 |
headertags(1:14) = (/'udist','umass','utime','npart',& |
| 313 |
'n1 ','n2 ','time ','gamma',& |
| 314 |
'rho0 ','RK2 ','escap','tkin ',& |
| 315 |
'tgrav','tterm'/) |
| 316 |
if (doubleprec) then |
| 317 |
gamma(j) = real(gammai) |
| 318 |
time(j) = real(timei) |
| 319 |
headervals(1:14,j) = (/real(udisti),real(umassi),real(utimei),real(nprint),& |
| 320 |
real(n1),real(n2),real(timei),real(gammai),& |
| 321 |
real(rhozero),real(RK2),real(escap),real(tkin), & |
| 322 |
real(tgrav),real(tterm)/) |
| 323 |
else |
| 324 |
gamma(j) = gammasi |
| 325 |
time(j) = timesi |
| 326 |
headervals(1:14,j) = (/real(udisti),real(umassi),real(utimei),real(nprint),& |
| 327 |
real(n1),real(n2),real(timesi),real(gammasi),& |
| 328 |
real(rhozeros),real(RK2s),real(escaps),real(tkins), & |
| 329 |
real(tgravs),real(tterms)/) |
| 330 |
endif |
| 331 |
print*,' time = ',time(j),' gamma = ',gamma(j) |
| 332 |
|
| 333 |
if (ierr /= 0) then |
| 334 |
print "(a)",'*** INCOMPLETE DATA ***' |
| 335 |
nstepsread = nstepsread + 1 |
| 336 |
exit over_steps_in_file |
| 337 |
else |
| 338 |
nstepsread = nstepsread + 1 |
| 339 |
endif |
| 340 |
j = j + 1 |
| 341 |
|
| 342 |
enddo over_steps_in_file |
| 343 |
|
| 344 |
endif |
| 345 |
|
| 346 |
55 continue |
| 347 |
! |
| 348 |
!--reached end of file |
| 349 |
! |
| 350 |
close(15) |
| 351 |
if (j-1 > 0) then |
| 352 |
print*,'>> end of dump file: nsteps =',j-1,'ntot = ', & |
| 353 |
sum(npartoftype(:,j-1)),'nghost=',npartoftype(2,j-1) |
| 354 |
endif |
| 355 |
|
| 356 |
end subroutine read_data_mbate |
| 357 |
|
| 358 |
!------------------------------------------------------------ |
| 359 |
! set labels for each column of data |
| 360 |
!------------------------------------------------------------ |
| 361 |
subroutine set_labels_mbate |
| 362 |
use labels |
| 363 |
use params |
| 364 |
use settings_data |
| 365 |
use geometry, only:labelcoord |
| 366 |
integer :: i |
| 367 |
|
| 368 |
if (ndim <= 0 .or. ndim > 3) then |
| 369 |
print*,'*** ERROR: ndim = ',ndim,' in set_labels_mbate ***' |
| 370 |
return |
| 371 |
endif |
| 372 |
if (ndimV <= 0 .or. ndimV > 3) then |
| 373 |
print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_mbate ***' |
| 374 |
return |
| 375 |
endif |
| 376 |
|
| 377 |
do i=1,ndim |
| 378 |
ix(i) = i |
| 379 |
enddo |
| 380 |
ivx = 4 |
| 381 |
ih = 7 ! smoothing length |
| 382 |
iutherm = 8 ! thermal energy |
| 383 |
ipmass = 9 ! particle mass |
| 384 |
irho = 10 ! location of rho in data array |
| 385 |
if (ncolumns > 10) then |
| 386 |
label(11) = 'dgrav' |
| 387 |
endif |
| 388 |
|
| 389 |
label(ix(1:ndim)) = labelcoord(1:ndim,1) |
| 390 |
do i=1,ndimV |
| 391 |
label(ivx+i-1) = 'v\d'//labelcoord(i,1) |
| 392 |
enddo |
| 393 |
label(irho) = 'density' |
| 394 |
label(iutherm) = 'u' |
| 395 |
label(ih) = 'h' |
| 396 |
label(ipmass) = 'particle mass' |
| 397 |
|
| 398 |
! |
| 399 |
!--set labels for vector quantities |
| 400 |
! |
| 401 |
iamvec(ivx:ivx+ndimV-1) = ivx |
| 402 |
labelvec(ivx:ivx+ndimV-1) = 'v' |
| 403 |
do i=1,ndimV |
| 404 |
label(ivx+i-1) = trim(labelvec(ivx))//'\d'//labelcoord(i,1) |
| 405 |
enddo |
| 406 |
! |
| 407 |
!--set labels for each particle type |
| 408 |
! |
| 409 |
ntypes = 3 !!maxparttypes |
| 410 |
labeltype(1) = 'gas' |
| 411 |
labeltype(2) = 'sink' |
| 412 |
labeltype(3) = 'unknown' |
| 413 |
UseTypeInRenderings(1) = .true. |
| 414 |
UseTypeInRenderings(2) = .false. |
| 415 |
UseTypeInRenderings(3) = .true. |
| 416 |
|
| 417 |
end subroutine set_labels_mbate |
| 418 |
|
| 419 |
end module readdata_mbate |