| 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) 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 VTK FILES |
| 28 |
! Supports both UNSTRUCTURED_GRID and STRUCTURED_GRID formats |
| 29 |
! |
| 30 |
!------------------------------------------------------------------------- |
| 31 |
module readdata_vtk |
| 32 |
use params, only:maxplot |
| 33 |
implicit none |
| 34 |
|
| 35 |
public :: read_data_vtk, set_labels_vtk |
| 36 |
character(len=32) :: tagarr(maxplot) |
| 37 |
|
| 38 |
private |
| 39 |
|
| 40 |
contains |
| 41 |
|
| 42 |
subroutine read_data_vtk(rootname,istepstart,ipos,nstepsread) |
| 43 |
use particle_data, only:dat,npartoftype,masstype,maxcol,maxpart,headervals |
| 44 |
use settings_data, only:ndim,ndimV,ncolumns,ncalc,ipartialread,iverbose |
| 45 |
use mem_allocation, only:alloc |
| 46 |
use labels, only:ih,irho,ipmass,headertags |
| 47 |
use system_utils, only:get_command_option |
| 48 |
use asciiutils, only:get_value,numfromfile |
| 49 |
integer, intent(in) :: istepstart,ipos |
| 50 |
integer, intent(out) :: nstepsread |
| 51 |
character(len=*), intent(in) :: rootname |
| 52 |
character(len=len(rootname)+10) :: datfile |
| 53 |
integer :: i,ierr |
| 54 |
integer :: ncolstep,npart,nsteps_to_read,nheader |
| 55 |
logical :: iexist,reallocate,is_binary |
| 56 |
real, allocatable :: pmass(:) |
| 57 |
real :: hfac |
| 58 |
|
| 59 |
nstepsread = 0 |
| 60 |
nsteps_to_read = 1 |
| 61 |
|
| 62 |
if (len_trim(rootname) > 0) then |
| 63 |
datfile = trim(rootname) |
| 64 |
else |
| 65 |
print*,' **** no data read **** ' |
| 66 |
return |
| 67 |
endif |
| 68 |
! |
| 69 |
!--check if first data file exists |
| 70 |
! |
| 71 |
if (iverbose==1 .and. ipos==1) print "(1x,a)",'reading vtk format' |
| 72 |
inquire(file=datfile,exist=iexist) |
| 73 |
if (.not.iexist) then |
| 74 |
! |
| 75 |
!--append .vtk on the endif not already present |
| 76 |
! |
| 77 |
datfile=trim(rootname)//'.vtk' |
| 78 |
inquire(file=datfile,exist=iexist) |
| 79 |
endif |
| 80 |
if (.not.iexist) then |
| 81 |
print "(a)",' *** error: '//trim(rootname)//': file not found ***' |
| 82 |
return |
| 83 |
endif |
| 84 |
! |
| 85 |
!--read data from snapshots |
| 86 |
! |
| 87 |
i = istepstart |
| 88 |
write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile) |
| 89 |
! |
| 90 |
!--open file and read header information |
| 91 |
! |
| 92 |
call read_vtk_header(trim(datfile),ndim,ndimV,ncolstep,npart,is_binary,ierr) |
| 93 |
if (ierr > 0) then |
| 94 |
print*,'ERROR reading vtk file: ierr=',ierr |
| 95 |
return |
| 96 |
else |
| 97 |
print "(1x,a,i0,a,i0)",'npart = ',npart,' ncolumns = ',ncolstep |
| 98 |
endif |
| 99 |
! |
| 100 |
!--allocate or reallocate memory for main data array |
| 101 |
! |
| 102 |
ncolumns = ncolstep |
| 103 |
nstepsread = 1 |
| 104 |
reallocate = (npart > maxpart) |
| 105 |
if (reallocate .or. .not.(allocated(dat))) then |
| 106 |
call alloc(npart,nsteps_to_read,max(ncolumns+ncalc,maxcol),mixedtypes=.false.) |
| 107 |
endif |
| 108 |
! |
| 109 |
!--now memory has been allocated, read main data arrays |
| 110 |
! |
| 111 |
ipartialread = .false. |
| 112 |
nheader = 2 |
| 113 |
call set_labels_vtk |
| 114 |
call read_vtk_legacy_binary(trim(datfile),npart,ncolstep,ierr,dat(:,:,i),headertags,headervals(:,i),nheader) |
| 115 |
call set_labels_vtk |
| 116 |
|
| 117 |
! set header variables from useful information from the file |
| 118 |
headertags(1) = 'npart' |
| 119 |
headervals(1,i) = real(npart) |
| 120 |
headertags(2) = 'nfile' |
| 121 |
headervals(2,i) = real(numfromfile(datfile)) |
| 122 |
|
| 123 |
! |
| 124 |
!--set particle mass and number of particles of type 1 |
| 125 |
! |
| 126 |
if (npart >= 1) then |
| 127 |
! |
| 128 |
!--reconstruct particle mass from h and rho |
| 129 |
! |
| 130 |
if (ih > 0 .and. irho > 0 .and. ipmass==0) then |
| 131 |
allocate(pmass(npart)) |
| 132 |
hfac = 1.0 |
| 133 |
pmass = dat(1:npart,irho,i)*(dat(1:npart,ih,i)/hfac)**ndim |
| 134 |
if (any(abs(pmass - pmass(1)) > 1e-6)) print*,' WARNING: unequal particle masses not supported' |
| 135 |
!print*,pmass(1:10) |
| 136 |
masstype(1,i) = pmass(1) |
| 137 |
deallocate(pmass) |
| 138 |
else |
| 139 |
masstype(1,i) = 1./npart |
| 140 |
endif |
| 141 |
npartoftype(1,i) = npart |
| 142 |
endif |
| 143 |
|
| 144 |
end subroutine read_data_vtk |
| 145 |
|
| 146 |
!---------------------------------------------------- |
| 147 |
! read header information |
| 148 |
!---------------------------------------------------- |
| 149 |
subroutine read_vtk_header(filename,ndim,ndimV,ncolstep,npart,is_binary,ierr) |
| 150 |
character(len=*) :: filename |
| 151 |
integer, intent(out) :: ndim,ndimV,ncolstep,npart,ierr |
| 152 |
logical, intent(out) :: is_binary |
| 153 |
character(len=256) :: version,header |
| 154 |
character(len=6) :: fileformat |
| 155 |
character(len=30) :: dataset,datatype |
| 156 |
integer :: iu |
| 157 |
|
| 158 |
ierr = 0 |
| 159 |
npart = 0 |
| 160 |
ndim = 3 |
| 161 |
ndimV = 3 |
| 162 |
ncolstep = 0 |
| 163 |
tagarr(:) = '' |
| 164 |
|
| 165 |
open(newunit=iu,file=filename,status='old',action='read',form='formatted',iostat=ierr) |
| 166 |
if (ierr /= 0) then |
| 167 |
print*,' ERROR opening '//trim(filename) |
| 168 |
return |
| 169 |
endif |
| 170 |
|
| 171 |
! read version information from first line |
| 172 |
read(iu,"(a)",iostat=ierr) version |
| 173 |
if (ierr /= 0) then |
| 174 |
print*,' ERROR reading vtk version from first line' |
| 175 |
endif |
| 176 |
|
| 177 |
! read data description from second line |
| 178 |
read(iu,"(a)",iostat=ierr) header |
| 179 |
if (ierr /= 0) then |
| 180 |
print*,' ERROR reading vtk header from second line' |
| 181 |
else |
| 182 |
print "(a)",trim(version)//': '//trim(header) |
| 183 |
endif |
| 184 |
|
| 185 |
! read file format from third line |
| 186 |
is_binary = .false. |
| 187 |
read(iu,"(a)",iostat=ierr) fileformat |
| 188 |
if (ierr /= 0) then |
| 189 |
print*,' ERROR reading vtk file format from third line' |
| 190 |
else |
| 191 |
if (trim(fileformat)=='BINARY' .or. trim(fileformat)=='binary') then |
| 192 |
is_binary = .true. |
| 193 |
else |
| 194 |
print "(2x,a)",trim(fileformat) |
| 195 |
endif |
| 196 |
endif |
| 197 |
|
| 198 |
! read dataset type |
| 199 |
read(iu,*,iostat=ierr) dataset,datatype |
| 200 |
if (ierr /= 0) then |
| 201 |
print*,trim(dataset),trim(datatype) |
| 202 |
print*,' ERROR reading vtk dataset type ',ierr |
| 203 |
else |
| 204 |
if (trim(datatype) /= 'UNSTRUCTURED_GRID' .and. trim(datatype) /= 'STRUCTURED_GRID') then |
| 205 |
print "(a)",' ERROR: got '//trim(datatype)//' but splash reader only supports UNSTRUCTURED_GRID or STRUCTURED_GRID' |
| 206 |
ierr = 1 |
| 207 |
return |
| 208 |
endif |
| 209 |
endif |
| 210 |
close(iu) |
| 211 |
|
| 212 |
if (is_binary) then |
| 213 |
! For both UNSTRUCTURED_GRID and STRUCTURED_GRID, we need to read the file |
| 214 |
! to determine npart and ncolstep from the actual data sections |
| 215 |
call read_vtk_legacy_binary(filename,npart,ncolstep,ierr) |
| 216 |
else |
| 217 |
print "(a)",' ERROR: got fileformat='//trim(fileformat)//' but splash reader only supports BINARY' |
| 218 |
endif |
| 219 |
|
| 220 |
end subroutine read_vtk_header |
| 221 |
|
| 222 |
!---------------------------------------------------- |
| 223 |
! read vtk legacy binary format |
| 224 |
!---------------------------------------------------- |
| 225 |
subroutine read_vtk_legacy_binary(filename,npart,ncolstep,ierr,dat,headertags,headervals,nheader) |
| 226 |
use settings_data, only:debugmode |
| 227 |
use byteswap, only:bs |
| 228 |
use labels, only:iamvec,ih,ipmass,irho |
| 229 |
use geometry, only:igeom_cartesian,igeom_spherical,coord_transform |
| 230 |
character(len=*), intent(in) :: filename |
| 231 |
integer, intent(out) :: npart,ncolstep,ierr |
| 232 |
real, intent(out), optional :: dat(:,:) |
| 233 |
character(len=*), intent(inout), optional :: headertags(:) |
| 234 |
real, intent(inout), optional :: headervals(:) |
| 235 |
integer, intent(inout), optional :: nheader |
| 236 |
character(len=32) :: dataset,datatype,tmp |
| 237 |
character(len=256) :: line |
| 238 |
character(len=1) :: newline |
| 239 |
real(kind=4), allocatable :: xyz(:,:),rval(:,:) |
| 240 |
real(kind=8), allocatable :: r8val(:,:) |
| 241 |
real(kind=4), allocatable :: x1c(:),x2c(:),x3c(:) |
| 242 |
integer :: iu,nline,n,i,nvec,nfields,npts,ispace |
| 243 |
integer :: nx,ny,nz,grid_type,ncells,icell,j,k,nvertices |
| 244 |
logical :: found_dimensions,found_points |
| 245 |
logical :: in_cell_data,in_point_data |
| 246 |
real :: xin(3),xout(3) |
| 247 |
integer :: igeom |
| 248 |
|
| 249 |
! open the file with stream access and use get_next_line to parse the ascii lines |
| 250 |
open(newunit=iu,file=filename,status='old',action='read',form='unformatted',iostat=ierr,access='stream') |
| 251 |
nline = 0 |
| 252 |
ncolstep = 0 |
| 253 |
iamvec(:) = 0 |
| 254 |
found_dimensions = .false. |
| 255 |
found_points = .false. |
| 256 |
in_cell_data = .false. |
| 257 |
in_point_data = .false. |
| 258 |
grid_type = 0 ! 0=unknown, 1=unstructured, 2=structured |
| 259 |
ncells = 0 |
| 260 |
|
| 261 |
do while(ierr == 0) |
| 262 |
call get_next_line(iu,line,n,nline,ierr) |
| 263 |
|
| 264 |
! Process the line as needed, e.g., print it |
| 265 |
select case (line(1:6)) |
| 266 |
case('DIMENS') |
| 267 |
! Handle DIMENSIONS section for STRUCTURED_GRID |
| 268 |
read(line(1:n),*,iostat=ierr) dataset,nx,ny,nz |
| 269 |
if (ierr == 0) then |
| 270 |
nvertices = nx * ny * nz |
| 271 |
ncells = (nx-1) * (ny-1) * (nz-1) ! number of cells in structured grid |
| 272 |
npart = ncells ! use cells as particles for structured grids |
| 273 |
found_dimensions = .true. |
| 274 |
grid_type = 2 ! structured grid |
| 275 |
if (.not.present(headervals)) print "(a,i0,' x ',i0,' x ',i0,' ncells = ',i0)",& |
| 276 |
' STRUCTURED_GRID: ',nx-1,ny-1,nz-1,ncells |
| 277 |
else |
| 278 |
print*,' ERROR reading DIMENSIONS section' |
| 279 |
endif |
| 280 |
case('POINTS') |
| 281 |
read(line(1:n),*,iostat=ierr) dataset,npts,datatype |
| 282 |
found_points = .true. |
| 283 |
|
| 284 |
! For STRUCTURED_GRID, verify that npts matches our calculated npart |
| 285 |
if (found_dimensions) then |
| 286 |
if (npts /= nvertices) then |
| 287 |
print*,' ERROR: POINTS count (',npts,') does not match DIMENSIONS product (',nvertices,')' |
| 288 |
ierr = 1 |
| 289 |
exit |
| 290 |
endif |
| 291 |
else |
| 292 |
! For UNSTRUCTURED_GRID, use the npts from POINTS section |
| 293 |
npart = npts |
| 294 |
grid_type = 1 ! unstructured grid |
| 295 |
if (debugmode) print*,' UNSTRUCTURED_GRID: npart = ',npart |
| 296 |
endif |
| 297 |
|
| 298 |
!print*,trim(dataset),npart,trim(datatype) |
| 299 |
allocate(xyz(3,npts),rval(1,npts)) |
| 300 |
read(iu,iostat=ierr) xyz,newline |
| 301 |
if (ierr /= 0) then |
| 302 |
print*,' ERROR reading POINTS data' |
| 303 |
deallocate(xyz,rval) |
| 304 |
exit |
| 305 |
endif |
| 306 |
if (present(dat) .and. npart==npts) then |
| 307 |
dat(1:npart,1:3) = bs(transpose(xyz)) ! byteswap from big endian -> little |
| 308 |
endif |
| 309 |
deallocate(xyz,rval) |
| 310 |
ncolstep = ncolstep + 3 |
| 311 |
case('FIELD ') |
| 312 |
nvec = 0 |
| 313 |
read(line(1:n),*,iostat=ierr) tmp,tmp,nfields |
| 314 |
if (debugmode) print*,' number of fields = ',nfields |
| 315 |
do i=1,nfields |
| 316 |
! get the next line |
| 317 |
call get_next_line(iu,line,n,nline,ierr) |
| 318 |
if (ierr /= 0 .or. n < 1) exit |
| 319 |
! extract the tag and number of vectors and points |
| 320 |
ispace = index(line,' ') ! position of first space |
| 321 |
tagarr(ncolstep+1) = line(1:ispace-1) ! tag is everything up to first space |
| 322 |
read(line(ispace:n),*,iostat=ierr) nvec,npts,datatype |
| 323 |
!print*,'LINE:',line(1:n)! |
| 324 |
!print*,'GOT: ',tagarr(ncolstep+1),': nvec=',nvec,'npts=',npts,trim(datatype) |
| 325 |
select case(trim(datatype)) |
| 326 |
case('float') |
| 327 |
! read/convert floats to working precision |
| 328 |
allocate(rval(nvec,npts)) |
| 329 |
read(iu,iostat=ierr) rval,newline |
| 330 |
if (debugmode) print*,bs(rval(1,1:min(npts,10))) |
| 331 |
if (npts==npart) then |
| 332 |
if (present(dat)) dat(1:npart,ncolstep+1:ncolstep+nvec) = bs(transpose(rval)) ! byteswap from big endian -> little |
| 333 |
if (nvec > 1) iamvec(ncolstep+1:ncolstep+nvec) = ncolstep |
| 334 |
ncolstep = ncolstep + nvec |
| 335 |
elseif (npts==1 .and. present(headervals) .and. present(headertags) .and. present(nheader)) then |
| 336 |
! put single scalar values in the header |
| 337 |
call add_to_header(headervals,headertags,nheader,real(bs(rval(1,1))),tagarr(ncolstep+1)) |
| 338 |
elseif (trim(tagarr(ncolstep+1)) == 'X1C_NATIVE_COORDINATES') then |
| 339 |
if (.not. allocated(x1c)) allocate(x1c(npts)) |
| 340 |
if (ierr == 0) x1c = bs(rval(1,:)) |
| 341 |
elseif (trim(tagarr(ncolstep+1)) == 'X2C_NATIVE_COORDINATES') then |
| 342 |
if (.not. allocated(x2c)) allocate(x2c(npts)) |
| 343 |
if (ierr == 0) x2c = bs(rval(1,:)) |
| 344 |
elseif (trim(tagarr(ncolstep+1)) == 'X3C_NATIVE_COORDINATES') then |
| 345 |
if (.not. allocated(x3c)) allocate(x3c(npts)) |
| 346 |
if (ierr == 0) x3c = bs(rval(1,:)) |
| 347 |
else |
| 348 |
if (present(dat) .and. debugmode) print*,'skipping float '//trim(tagarr(ncolstep+1))//' as npts /= ncells' |
| 349 |
endif |
| 350 |
deallocate(rval) |
| 351 |
case('double') |
| 352 |
! read/convert doubles to working precision |
| 353 |
allocate(r8val(nvec,npts)) |
| 354 |
read(iu,iostat=ierr) r8val,newline |
| 355 |
if (debugmode) print*,bs(r8val(1,1:min(npts,10))) |
| 356 |
if (npts==npart) then |
| 357 |
if (present(dat)) dat(1:npart,ncolstep+1:ncolstep+nvec) = bs(transpose(r8val)) ! byteswap from big endian -> little |
| 358 |
if (nvec > 1) iamvec(ncolstep+1:ncolstep+nvec) = ncolstep |
| 359 |
ncolstep = ncolstep + nvec |
| 360 |
elseif (npts==1 .and. present(headervals) .and. present(headertags) .and. present(nheader)) then |
| 361 |
! put single scalar values in the header |
| 362 |
call add_to_header(headervals,headertags,nheader,real(bs(r8val(1,1))),tagarr(ncolstep+1)) |
| 363 |
else |
| 364 |
if (present(dat)) print*,'WARNING: skipping double '//trim(tagarr(ncolstep+1))//' as npts /= npart' |
| 365 |
endif |
| 366 |
deallocate(r8val) |
| 367 |
case('int') |
| 368 |
! just silently skip integers |
| 369 |
allocate(rval(nvec,npts)) |
| 370 |
read(iu,iostat=ierr) rval,newline |
| 371 |
deallocate(rval) |
| 372 |
case default |
| 373 |
if (present(dat)) print*,'Warning: unknown data type '//trim(datatype)//' assuming 4-bytes and skipping' |
| 374 |
allocate(rval(nvec,npts)) |
| 375 |
read(iu,iostat=ierr) rval,newline |
| 376 |
deallocate(rval) |
| 377 |
end select |
| 378 |
enddo |
| 379 |
case('CELL_D') |
| 380 |
! CELL_DATA section - data applies to cells, not points |
| 381 |
read(line(1:n),*,iostat=ierr) dataset,npts |
| 382 |
in_cell_data = .true. |
| 383 |
in_point_data = .false. |
| 384 |
if (debugmode) print*,' Entering CELL_DATA section with ',npts,' cells' |
| 385 |
if (found_dimensions .and. npts /= ncells) then |
| 386 |
if (debugmode) print*,' WARNING: CELL_DATA count (',npts,') does not match expected cells (',ncells,')' |
| 387 |
endif |
| 388 |
case('POINT_') |
| 389 |
! POINT_DATA section - data applies to points |
| 390 |
read(line(1:n),*,iostat=ierr) dataset,npts |
| 391 |
in_point_data = .true. |
| 392 |
in_cell_data = .false. |
| 393 |
if (debugmode) print*,' Entering POINT_DATA section with ',npts,' points' |
| 394 |
if (npts /= npart) then |
| 395 |
if (debugmode) print*,' WARNING: POINT_DATA count (',npts,') does not match npart (',npart,')' |
| 396 |
endif |
| 397 |
case('SCALAR') |
| 398 |
! SCALARS section - read scalar data |
| 399 |
read(line(1:n),*,iostat=ierr) dataset,tmp,datatype |
| 400 |
if (ierr == 0) then |
| 401 |
tagarr(ncolstep+1) = tmp |
| 402 |
if (debugmode) print*,' Reading SCALARS: '//trim(tmp)//' type: '//trim(datatype) |
| 403 |
|
| 404 |
! Check if LOOKUP_TABLE is on the same line or next line |
| 405 |
if (index(line,'LOOKUP_TABLE') > 0) then |
| 406 |
! LOOKUP_TABLE is on the same line, skip it |
| 407 |
if (debugmode) print*,' LOOKUP_TABLE on same line, skipping' |
| 408 |
else |
| 409 |
! Read the next line which should be LOOKUP_TABLE |
| 410 |
call get_next_line(iu,line,n,nline,ierr) |
| 411 |
if (ierr == 0 .and. index(line(1:n),'LOOKUP_TABLE') > 0) then |
| 412 |
if (debugmode) print*,' Skipping LOOKUP_TABLE line' |
| 413 |
endif |
| 414 |
endif |
| 415 |
|
| 416 |
! Determine how many data points to read based on context |
| 417 |
if (in_cell_data) then |
| 418 |
npts = ncells |
| 419 |
if (debugmode) print*,' Reading SCALARS for cells: ',npts |
| 420 |
else |
| 421 |
npts = npart |
| 422 |
if (debugmode) print*,' Reading SCALARS for points: ',npts |
| 423 |
endif |
| 424 |
|
| 425 |
! Read the binary data directly (not as text lines) |
| 426 |
select case(trim(datatype)) |
| 427 |
case('float') |
| 428 |
allocate(rval(1,npts)) |
| 429 |
read(iu,iostat=ierr) rval,newline |
| 430 |
ncolstep = ncolstep + 1 |
| 431 |
if (ierr == 0) then |
| 432 |
if (present(dat)) dat(1:npts,ncolstep+1) = bs(rval(1,:)) |
| 433 |
if (debugmode) print*,' Successfully read SCALARS data for '//trim(tmp) |
| 434 |
else |
| 435 |
if (debugmode) print*,' ERROR reading SCALARS binary data for '//trim(tmp) |
| 436 |
endif |
| 437 |
deallocate(rval) |
| 438 |
case('double') |
| 439 |
allocate(r8val(1,npts)) |
| 440 |
read(iu,iostat=ierr) r8val,newline |
| 441 |
ncolstep = ncolstep + 1 |
| 442 |
if (ierr == 0) then |
| 443 |
if (present(dat)) dat(1:npts,ncolstep+1) = bs(r8val(1,:)) |
| 444 |
if (debugmode) print*,' Successfully read SCALARS data for '//trim(tmp) |
| 445 |
else |
| 446 |
if (debugmode) print*,' ERROR reading SCALARS binary data for '//trim(tmp) |
| 447 |
endif |
| 448 |
deallocate(r8val) |
| 449 |
case default |
| 450 |
if (present(dat)) print*,'Warning: unknown SCALARS data type '//trim(datatype)//' skipping' |
| 451 |
end select |
| 452 |
endif |
| 453 |
case('VECTOR') |
| 454 |
! VECTORS section - read vector data |
| 455 |
read(line(1:n),*,iostat=ierr) dataset,tmp,datatype |
| 456 |
if (ierr == 0) then |
| 457 |
tagarr(ncolstep+1) = tmp |
| 458 |
if (debugmode) print*,' Reading VECTORS: '//trim(tmp)//' type: '//trim(datatype) |
| 459 |
|
| 460 |
! Determine how many data points to read based on context |
| 461 |
if (in_cell_data) then |
| 462 |
npts = ncells |
| 463 |
if (debugmode) print*,' Reading VECTORS for cells: ',npts |
| 464 |
else |
| 465 |
npts = npart |
| 466 |
if (debugmode) print*,' Reading VECTORS for points: ',npts |
| 467 |
endif |
| 468 |
|
| 469 |
! Read the data based on type |
| 470 |
select case(trim(datatype)) |
| 471 |
case('float') |
| 472 |
allocate(rval(3,npts)) |
| 473 |
read(iu,iostat=ierr) rval,newline |
| 474 |
if (ierr == 0) then |
| 475 |
if (present(dat)) dat(1:npts,ncolstep+1:ncolstep+3) = bs(transpose(rval)) |
| 476 |
iamvec(ncolstep+1:ncolstep+3) = ncolstep |
| 477 |
ncolstep = ncolstep + 3 |
| 478 |
endif |
| 479 |
deallocate(rval) |
| 480 |
case('double') |
| 481 |
allocate(r8val(3,npts)) |
| 482 |
read(iu,iostat=ierr) r8val,newline |
| 483 |
if (ierr == 0) then |
| 484 |
if (present(dat)) dat(1:npts,ncolstep+1:ncolstep+3) = bs(transpose(r8val)) |
| 485 |
iamvec(ncolstep+1:ncolstep+3) = ncolstep |
| 486 |
ncolstep = ncolstep + 3 |
| 487 |
endif |
| 488 |
deallocate(r8val) |
| 489 |
case default |
| 490 |
if (present(dat)) print*,'Warning: unknown VECTORS data type '//trim(datatype)//' skipping' |
| 491 |
end select |
| 492 |
endif |
| 493 |
case('LOOKUP') |
| 494 |
! LOOKUP_TABLE section - just skip this line |
| 495 |
if (debugmode) print*,' Skipping LOOKUP_TABLE section' |
| 496 |
end select |
| 497 |
enddo |
| 498 |
|
| 499 |
! For structured grids, add fake h and mass columns |
| 500 |
if (grid_type == 2 .and. found_dimensions) then |
| 501 |
if (debugmode) print*,' Adding fake h and mass columns for structured grid' |
| 502 |
|
| 503 |
! Add h column (cell size) |
| 504 |
ncolstep = ncolstep + 1 |
| 505 |
tagarr(ncolstep) = 'h' |
| 506 |
ih = ncolstep |
| 507 |
|
| 508 |
if (present(dat)) dat(1:npart,ncolstep) = 1.0 ! Placeholder - should be calculated from actual coordinates |
| 509 |
if (debugmode) print*,' Added fake h column (cell size)' |
| 510 |
|
| 511 |
! Add mass column (cell mass = density * cell volume) |
| 512 |
ncolstep = ncolstep + 1 |
| 513 |
tagarr(ncolstep) = 'mass' |
| 514 |
ipmass = ncolstep |
| 515 |
print*,'Added mass column (cell mass)',ipmass |
| 516 |
|
| 517 |
if (present(dat)) dat(1:npart,ncolstep) = 1.0 ! Placeholder - should be calculated from density * cell_volume |
| 518 |
if (debugmode) print*,' Added fake mass column (cell mass)' |
| 519 |
|
| 520 |
! Construct position data from cell coordinates |
| 521 |
if (present(dat) .and. allocated(x1c) .and. allocated(x2c) .and. allocated(x3c)) then |
| 522 |
!print*,'npts = ',npts,size(x1c),size(x2c),size(x3c),nx-1,ny-1,nz-1 |
| 523 |
igeom = igeom_spherical |
| 524 |
do k = 1, nz-1 |
| 525 |
xin(2) = x3c(k) |
| 526 |
do j = 1, ny-1 |
| 527 |
xin(3) = x2c(j) |
| 528 |
do i = 1, nx-1 |
| 529 |
xin(1) = x1c(i) |
| 530 |
icell = i + (j-1)*(nx-1) + (k-1)*(nx-1)*(ny-1) |
| 531 |
call coord_transform(xin,3,igeom,xout,3,igeom_cartesian) |
| 532 |
dat(icell,1) = xout(1) |
| 533 |
dat(icell,2) = xout(2) |
| 534 |
dat(icell,3) = xout(3) |
| 535 |
if (i < nx-1) then |
| 536 |
dat(icell,ih) = x1c(i+1) - x1c(i) |
| 537 |
else |
| 538 |
dat(icell,ih) = x1c(i) - x1c(i-1) |
| 539 |
endif |
| 540 |
dat(icell,ipmass) = dat(icell,ih)**3 * dat(icell,irho) |
| 541 |
enddo |
| 542 |
enddo |
| 543 |
enddo |
| 544 |
if (debugmode) print*,' Constructed position data from cell coordinates' |
| 545 |
endif |
| 546 |
endif |
| 547 |
|
| 548 |
! Final validation |
| 549 |
if (.not. found_points) then |
| 550 |
print*,' ERROR: no POINTS section found in VTK file' |
| 551 |
ierr = 1 |
| 552 |
elseif (grid_type == 2 .and. .not. found_dimensions) then |
| 553 |
print*,' ERROR: STRUCTURED_GRID format but no DIMENSIONS section found' |
| 554 |
ierr = 1 |
| 555 |
elseif (npart <= 0) then |
| 556 |
print*,' ERROR: invalid number of points: ',npart |
| 557 |
ierr = 1 |
| 558 |
endif |
| 559 |
|
| 560 |
close(iu) |
| 561 |
|
| 562 |
end subroutine read_vtk_legacy_binary |
| 563 |
|
| 564 |
!------------------------------------------------------ |
| 565 |
! add a value to the header |
| 566 |
!------------------------------------------------------ |
| 567 |
subroutine add_to_header(headervals,headertags,nheader,value,tag) |
| 568 |
real, intent(inout) :: headervals(:) |
| 569 |
character(len=*), intent(inout) :: headertags(:) |
| 570 |
integer, intent(inout) :: nheader |
| 571 |
real, intent(in) :: value |
| 572 |
character(len=*), intent(in) :: tag |
| 573 |
|
| 574 |
nheader = nheader + 1 |
| 575 |
headervals(nheader) = value |
| 576 |
headertags(nheader) = trim(tag) |
| 577 |
|
| 578 |
end subroutine add_to_header |
| 579 |
|
| 580 |
!------------------------------------------------------ |
| 581 |
! read a line of ascii text from an unformatted stream |
| 582 |
!------------------------------------------------------ |
| 583 |
subroutine get_next_line(iu,line,n,nline,ierr) |
| 584 |
use settings_data, only:debugmode |
| 585 |
integer, intent(in) :: iu |
| 586 |
character(len=*), intent(out) :: line |
| 587 |
integer, intent(inout) :: nline |
| 588 |
integer, intent(out) :: n,ierr |
| 589 |
|
| 590 |
n = 1 |
| 591 |
line = '' |
| 592 |
do while(n < len(line)) |
| 593 |
read(iu, iostat=ierr) line(n:n) |
| 594 |
if (ierr /= 0) exit |
| 595 |
if (line(n:n) == char(10)) exit |
| 596 |
n = n + 1 |
| 597 |
end do |
| 598 |
n = n - 1 ! n is the line length |
| 599 |
|
| 600 |
if (ierr == 0) then |
| 601 |
nline = nline + 1 |
| 602 |
if (debugmode) print "(a,i2,a)",' DEBUG: ',nline,' : '//trim(line(1:n)) |
| 603 |
endif |
| 604 |
|
| 605 |
end subroutine get_next_line |
| 606 |
|
| 607 |
!------------------------------------------------------------ |
| 608 |
! set labels for each column of data |
| 609 |
!------------------------------------------------------------ |
| 610 |
subroutine set_labels_vtk |
| 611 |
use labels, only:label,labeltype,ix,ipmass,ih,irho,labelvec,iamvec,& |
| 612 |
set_vector_labels,label_synonym,ivx |
| 613 |
use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings,ncolumns |
| 614 |
use geometry, only:labelcoord |
| 615 |
integer :: i |
| 616 |
|
| 617 |
if (ndim <= 0 .or. ndim > 3) then |
| 618 |
print*,'*** ERROR: ndim = ',ndim,' in set_labels_fits ***' |
| 619 |
return |
| 620 |
endif |
| 621 |
if (ndimV <= 0 .or. ndimV > 3) then |
| 622 |
print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_fits ***' |
| 623 |
return |
| 624 |
endif |
| 625 |
|
| 626 |
ix(1) = 1 |
| 627 |
ix(2) = 2 |
| 628 |
if (ndim >= 3) ix(3) = 3 |
| 629 |
!ipmass = ndim+3 |
| 630 |
do i=4,ncolumns |
| 631 |
label(i) = trim(tagarr(i)) |
| 632 |
if (label_synonym(label(i))=='density') irho = i |
| 633 |
if (label_synonym(label(i))=='h') ih = i |
| 634 |
if (iamvec(i) > 0 .and.trim(label(i))=='v') ivx = i |
| 635 |
enddo |
| 636 |
call set_vector_labels(ncolumns,ndimV,iamvec,labelvec,label,labelcoord(:,1)) |
| 637 |
|
| 638 |
! set labels of the quantities read in |
| 639 |
if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1) |
| 640 |
if (irho > 0) label(irho) = 'density' |
| 641 |
if (ipmass > 0) label(ipmass) = 'particle mass' |
| 642 |
if (ih > 0) label(ih) = 'h' |
| 643 |
! set labels for each particle type |
| 644 |
ntypes = 1 |
| 645 |
labeltype(1) = 'gas' |
| 646 |
UseTypeInRenderings(:) = .true. |
| 647 |
|
| 648 |
end subroutine set_labels_vtk |
| 649 |
|
| 650 |
end module readdata_vtk |