| 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-2020 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!----------------------------------------------------------------- |
| 24 |
! |
| 25 |
! Shamrock native binary reader for SPLASH |
| 26 |
! |
| 27 |
!----------------------------------------------------------------- |
| 28 |
module readdata_shamrock |
| 29 |
use params, only:maxplot |
| 30 |
use iso_fortran_env, only:int64,int8 |
| 31 |
use json_utils, only:json_read,json_get_value_by_path, & |
| 32 |
json_array_length,json_array_get_element,json_success, & |
| 33 |
json_kind_number,json_kind_string,json_kind_object, & |
| 34 |
json_kind_array |
| 35 |
implicit none |
| 36 |
private |
| 37 |
|
| 38 |
type :: shamrock_field |
| 39 |
character(len=64) :: name = '' |
| 40 |
character(len=16) :: ftype = '' |
| 41 |
integer :: ncomp = 0 |
| 42 |
integer :: col_start = 0 |
| 43 |
logical :: is_vector = .false. |
| 44 |
end type shamrock_field |
| 45 |
|
| 46 |
character(len=32) :: tagarr(maxplot) |
| 47 |
|
| 48 |
public :: read_data_shamrock, set_labels_shamrock |
| 49 |
|
| 50 |
contains |
| 51 |
|
| 52 |
!----------------------------------------------------------------- |
| 53 |
! read the data from a Shamrock file |
| 54 |
!----------------------------------------------------------------- |
| 55 |
subroutine read_data_shamrock(rootname,istepstart,ipos,nstepsread) |
| 56 |
use particle_data, only:dat,npartoftype,masstype,maxcol,maxpart,headervals |
| 57 |
use settings_data, only:ndim,ndimV,ncolumns,ncalc,ipartialread,iverbose,debugmode |
| 58 |
use mem_allocation, only:alloc |
| 59 |
use labels, only:headertags,irho,ih |
| 60 |
use asciiutils, only:numfromfile,match_tag |
| 61 |
character(len=*), intent(in) :: rootname |
| 62 |
integer, intent(in) :: istepstart,ipos |
| 63 |
integer, intent(out) :: nstepsread |
| 64 |
|
| 65 |
type(shamrock_field), allocatable :: fields(:) |
| 66 |
logical :: iexist,reallocate |
| 67 |
integer :: nfields,ncolstep,npart,nsteps_to_read,idx_xyz,nheader,npatches,ierr,i,iu |
| 68 |
integer, allocatable :: pids(:) |
| 69 |
integer(kind=int64) :: base_pos |
| 70 |
integer(kind=int64), allocatable :: offsets(:), bytecounts(:), field_counts(:) |
| 71 |
character(len=:), allocatable :: user_header,sched_header,file_header |
| 72 |
character(len=len(rootname)+16) :: datfile |
| 73 |
real :: kernel_hfact, particle_mass |
| 74 |
|
| 75 |
nstepsread = 0 |
| 76 |
nsteps_to_read = 1 |
| 77 |
npart = 0 |
| 78 |
ncolstep = 4 |
| 79 |
|
| 80 |
if (len_trim(rootname) == 0) then |
| 81 |
print*,' **** no data read **** ' |
| 82 |
return |
| 83 |
endif |
| 84 |
|
| 85 |
datfile = trim(rootname) |
| 86 |
inquire(file=datfile,exist=iexist) |
| 87 |
if (.not. iexist) then |
| 88 |
datfile = trim(rootname)//'.sham' |
| 89 |
inquire(file=datfile,exist=iexist) |
| 90 |
endif |
| 91 |
if (.not. iexist) then |
| 92 |
print "(a)",' *** error: '//trim(rootname)//': file not found ***' |
| 93 |
return |
| 94 |
endif |
| 95 |
|
| 96 |
if (iverbose==1 .and. ipos==1) print "(1x,a)",'reading Shamrock native format' |
| 97 |
open(newunit=iu,file=trim(datfile),status='old',access='stream',& |
| 98 |
form='unformatted',action='read',iostat=ierr) |
| 99 |
if (ierr /= 0) then |
| 100 |
print*,' ERROR opening '//trim(datfile) |
| 101 |
return |
| 102 |
endif |
| 103 |
|
| 104 |
call read_int64_and_string(iu,user_header,ierr) |
| 105 |
if (ierr /= 0) print "(a)",' ERROR reading user header' |
| 106 |
call read_int64_and_string(iu,sched_header,ierr) |
| 107 |
if (ierr /= 0) print "(a)",' ERROR reading scheduler header' |
| 108 |
call read_int64_and_string(iu,file_header,ierr) |
| 109 |
if (ierr /= 0) print "(a)",' ERROR reading file header' |
| 110 |
|
| 111 |
inquire(unit=iu,pos=base_pos,iostat=ierr) |
| 112 |
if (ierr /= 0) return |
| 113 |
|
| 114 |
call read_shamrock_header(user_header,sched_header,file_header, & |
| 115 |
fields,offsets,bytecounts,pids,kernel_hfact,particle_mass,ierr) |
| 116 |
if (ierr /= 0) return |
| 117 |
|
| 118 |
nfields = size(fields) |
| 119 |
tagarr(:) = '' |
| 120 |
call initialise_columns(fields,nfields,ncolstep,ierr) |
| 121 |
if (ierr /= 0) return |
| 122 |
|
| 123 |
npatches = size(pids) |
| 124 |
allocate(field_counts(nfields)) |
| 125 |
|
| 126 |
idx_xyz = match_tag(fields(:)%name,'xyz') |
| 127 |
if (idx_xyz <= 0) then |
| 128 |
print*,' ERROR: xyz field missing in Shamrock layout' |
| 129 |
return |
| 130 |
endif |
| 131 |
|
| 132 |
npart = 0 |
| 133 |
do i = 1,npatches |
| 134 |
call read_patch_counts(iu,base_pos+offsets(i),nfields,field_counts,ierr) |
| 135 |
if (ierr /= 0) exit |
| 136 |
npart = npart + int(field_counts(idx_xyz)) |
| 137 |
enddo |
| 138 |
|
| 139 |
if (npart <= 0) then |
| 140 |
print*,' ERROR: no particles found in Shamrock dump' |
| 141 |
return |
| 142 |
endif |
| 143 |
|
| 144 |
ncolumns = ncolstep |
| 145 |
reallocate = (npart > maxpart) |
| 146 |
if (reallocate .or. .not.(allocated(dat))) then |
| 147 |
call alloc(npart,nsteps_to_read,max(ncolumns+ncalc,maxcol),mixedtypes=.false.) |
| 148 |
endif |
| 149 |
|
| 150 |
dat(1:npart,1:ncolumns,istepstart) = 0.0 |
| 151 |
ipartialread = .false. |
| 152 |
ndim = 3 |
| 153 |
ndimV = 3 |
| 154 |
|
| 155 |
call read_shamrock_data(iu,base_pos,npatches,fields,offsets, & |
| 156 |
istepstart,npart,field_counts,idx_xyz,ierr) |
| 157 |
|
| 158 |
if (ierr /= 0) print "(a)",' ERROR reading Shamrock data' |
| 159 |
close(iu) |
| 160 |
|
| 161 |
call set_labels_shamrock |
| 162 |
|
| 163 |
! extract all possible values from the user header |
| 164 |
nheader = 4 |
| 165 |
headertags(1:4) = (/'npart','pmass','hfact','nfile'/) |
| 166 |
headervals(1,istepstart) = real(npart) |
| 167 |
headervals(2,istepstart) = real(particle_mass) |
| 168 |
headervals(3,istepstart) = real(kernel_hfact) |
| 169 |
headervals(4,istepstart) = real(numfromfile(datfile)) |
| 170 |
if (debugmode) print*,user_header |
| 171 |
call get_header_pairs(user_header,headertags,headervals(:,istepstart),nheader) |
| 172 |
|
| 173 |
masstype(1,istepstart) = real(particle_mass) |
| 174 |
npartoftype(1,istepstart) = npart |
| 175 |
|
| 176 |
! compute the density from the mass and the smoothing length |
| 177 |
if (ih > 0 .and. irho > 0) then |
| 178 |
do i=1,npart |
| 179 |
dat(i,irho,istepstart) = particle_mass*(kernel_hfact/abs(dat(i,ih,istepstart)))**3 |
| 180 |
enddo |
| 181 |
endif |
| 182 |
nstepsread = 1 |
| 183 |
|
| 184 |
if (allocated(field_counts)) deallocate(field_counts) |
| 185 |
if (allocated(fields)) deallocate(fields) |
| 186 |
if (allocated(offsets)) deallocate(offsets) |
| 187 |
if (allocated(bytecounts)) deallocate(bytecounts) |
| 188 |
if (allocated(pids)) deallocate(pids) |
| 189 |
|
| 190 |
end subroutine read_data_shamrock |
| 191 |
|
| 192 |
!----------------------------------------------------------------- |
| 193 |
! set the labels for the data |
| 194 |
!----------------------------------------------------------------- |
| 195 |
subroutine set_labels_shamrock |
| 196 |
use labels, only:label,labeltype,ix,iamvec,labelvec,irho,ih,ipmass, & |
| 197 |
set_vector_labels,label_synonym,ivx |
| 198 |
use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings,ncolumns |
| 199 |
use geometry, only:labelcoord |
| 200 |
integer :: i |
| 201 |
|
| 202 |
ix(1) = 1 |
| 203 |
ix(2) = 2 |
| 204 |
if (ndim >= 3) ix(3) = 3 |
| 205 |
|
| 206 |
do i = 1, ncolumns |
| 207 |
label(i) = trim(tagarr(i)) |
| 208 |
select case (trim(label(i))) |
| 209 |
case('rho') |
| 210 |
irho = i |
| 211 |
case('h','hpart') |
| 212 |
ih = i |
| 213 |
case('mass') |
| 214 |
ipmass = i |
| 215 |
case('vx') |
| 216 |
ivx = i |
| 217 |
end select |
| 218 |
enddo |
| 219 |
|
| 220 |
call set_vector_labels(ncolumns,ndimV,iamvec,labelvec,label,labelcoord(:,1)) |
| 221 |
|
| 222 |
if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1) |
| 223 |
if (irho > 0) label(irho) = 'density' |
| 224 |
if (ipmass > 0) label(ipmass) = 'particle mass' |
| 225 |
if (ih > 0) label(ih) = 'h' |
| 226 |
|
| 227 |
ntypes = 1 |
| 228 |
labeltype(1) = 'gas' |
| 229 |
UseTypeInRenderings(:) = .true. |
| 230 |
|
| 231 |
end subroutine set_labels_shamrock |
| 232 |
|
| 233 |
!----------------------------------------------------------------- |
| 234 |
! initialise the columns |
| 235 |
!----------------------------------------------------------------- |
| 236 |
subroutine initialise_columns(fields,nfields,ncolstep,ierr) |
| 237 |
use labels, only:iamvec |
| 238 |
type(shamrock_field), intent(inout) :: fields(:) |
| 239 |
integer, intent(in) :: nfields |
| 240 |
integer, intent(inout) :: ncolstep |
| 241 |
integer, intent(out) :: ierr |
| 242 |
integer :: i |
| 243 |
|
| 244 |
ierr = 0 |
| 245 |
tagarr(:) = '' |
| 246 |
ncolstep = 4 |
| 247 |
tagarr(1) = 'x' |
| 248 |
tagarr(2) = 'y' |
| 249 |
tagarr(3) = 'z' |
| 250 |
tagarr(4) = 'rho' |
| 251 |
iamvec(:) = 0 |
| 252 |
|
| 253 |
do i = 1, nfields |
| 254 |
select case (trim(fields(i)%ftype)) |
| 255 |
case('f64_3') |
| 256 |
fields(i)%ncomp = 3 |
| 257 |
if (trim(fields(i)%name) == 'xyz') then |
| 258 |
fields(i)%col_start = 1 |
| 259 |
fields(i)%is_vector = .false. |
| 260 |
else |
| 261 |
call register_vector_field(fields(i),ncolstep,ierr) |
| 262 |
if (ierr /= 0) return |
| 263 |
endif |
| 264 |
case('f64') |
| 265 |
fields(i)%ncomp = 1 |
| 266 |
call register_scalar_field(fields(i),ncolstep,ierr) |
| 267 |
if (ierr /= 0) return |
| 268 |
case default |
| 269 |
print*,' WARNING: unsupported field type ',trim(fields(i)%ftype) |
| 270 |
end select |
| 271 |
enddo |
| 272 |
|
| 273 |
end subroutine initialise_columns |
| 274 |
|
| 275 |
!----------------------------------------------------------------- |
| 276 |
! register a scalar field |
| 277 |
!----------------------------------------------------------------- |
| 278 |
subroutine register_scalar_field(field,ncolstep,ierr) |
| 279 |
type(shamrock_field), intent(inout) :: field |
| 280 |
integer, intent(inout) :: ncolstep |
| 281 |
integer, intent(out) :: ierr |
| 282 |
character(len=32) :: label |
| 283 |
|
| 284 |
ierr = 0 |
| 285 |
label = field%name(1:min(len_trim(field%name),len(label))) |
| 286 |
field%col_start = find_or_add_label(label,ncolstep,ierr) |
| 287 |
field%is_vector = .false. |
| 288 |
|
| 289 |
end subroutine register_scalar_field |
| 290 |
|
| 291 |
!----------------------------------------------------------------- |
| 292 |
! register a vector field |
| 293 |
!----------------------------------------------------------------- |
| 294 |
subroutine register_vector_field(field,ncolstep,ierr) |
| 295 |
use labels, only:iamvec |
| 296 |
type(shamrock_field), intent(inout) :: field |
| 297 |
integer, intent(inout) :: ncolstep |
| 298 |
integer, intent(out) :: ierr |
| 299 |
character(len=32) :: vec_labels(3),base_label |
| 300 |
integer :: base, name_len |
| 301 |
|
| 302 |
ierr = 0 |
| 303 |
vec_labels(:) = '' |
| 304 |
base_label = '' |
| 305 |
select case (trim(field%name)) |
| 306 |
case('vxyz') |
| 307 |
base_label = 'v' |
| 308 |
case('axyz') |
| 309 |
base_label = 'a' |
| 310 |
case('axyz_ext') |
| 311 |
base_label = 'aext' |
| 312 |
case default |
| 313 |
name_len = min(len_trim(field%name), len(base_label)) |
| 314 |
if (name_len > 0) then |
| 315 |
base_label = field%name(1:name_len) |
| 316 |
else |
| 317 |
base_label = 'vec' |
| 318 |
endif |
| 319 |
end select |
| 320 |
|
| 321 |
vec_labels = (/trim(base_label),trim(base_label),trim(base_label)/) |
| 322 |
|
| 323 |
base = find_or_add_vector(vec_labels,ncolstep,ierr) |
| 324 |
if (ierr /= 0) return |
| 325 |
field%col_start = base |
| 326 |
field%is_vector = .true. |
| 327 |
iamvec(base:base+2) = base |
| 328 |
|
| 329 |
end subroutine register_vector_field |
| 330 |
|
| 331 |
!----------------------------------------------------------------- |
| 332 |
! find the index of a label |
| 333 |
!----------------------------------------------------------------- |
| 334 |
integer function find_or_add_label(label,ncolstep,ierr) result(idx) |
| 335 |
character(len=*), intent(in) :: label |
| 336 |
integer, intent(inout) :: ncolstep |
| 337 |
integer, intent(out) :: ierr |
| 338 |
integer :: i |
| 339 |
|
| 340 |
ierr = 0 |
| 341 |
do i = 1, ncolstep |
| 342 |
if (trim(tagarr(i)) == trim(label)) then |
| 343 |
idx = i |
| 344 |
return |
| 345 |
endif |
| 346 |
enddo |
| 347 |
ncolstep = ncolstep + 1 |
| 348 |
if (ncolstep > maxplot) then |
| 349 |
ierr = 1 |
| 350 |
idx = 0 |
| 351 |
return |
| 352 |
endif |
| 353 |
tagarr(ncolstep) = trim(label) |
| 354 |
idx = ncolstep |
| 355 |
|
| 356 |
end function find_or_add_label |
| 357 |
|
| 358 |
!----------------------------------------------------------------- |
| 359 |
! find the index of a vector |
| 360 |
!----------------------------------------------------------------- |
| 361 |
integer function find_or_add_vector(labeli,ncolstep,ierr) result(idx) |
| 362 |
character(len=*), intent(in) :: labeli(3) |
| 363 |
integer, intent(inout) :: ncolstep |
| 364 |
integer, intent(out) :: ierr |
| 365 |
integer :: i, base |
| 366 |
|
| 367 |
ierr = 0 |
| 368 |
do i = 1, ncolstep-2 |
| 369 |
if (trim(tagarr(i)) == trim(labeli(1)) .and. trim(tagarr(i+1)) == trim(labeli(2)) & |
| 370 |
.and. trim(tagarr(i+2)) == trim(labeli(3))) then |
| 371 |
idx = i |
| 372 |
return |
| 373 |
endif |
| 374 |
enddo |
| 375 |
|
| 376 |
base = ncolstep + 1 |
| 377 |
if (base+2 > maxplot) then |
| 378 |
ierr = 1 |
| 379 |
idx = 0 |
| 380 |
return |
| 381 |
endif |
| 382 |
tagarr(base) = trim(labeli(1)) |
| 383 |
tagarr(base+1) = trim(labeli(2)) |
| 384 |
tagarr(base+2) = trim(labeli(3)) |
| 385 |
ncolstep = base + 2 |
| 386 |
idx = base |
| 387 |
|
| 388 |
end function find_or_add_vector |
| 389 |
|
| 390 |
!----------------------------------------------------------------- |
| 391 |
! read the header of the Shamrock file |
| 392 |
!----------------------------------------------------------------- |
| 393 |
subroutine read_shamrock_header(user_header,sched_header,file_header,fields,offsets, & |
| 394 |
bytecounts,pids,kernel_hfact,particle_mass,ierr) |
| 395 |
character(len=*), intent(in) :: user_header,sched_header,file_header |
| 396 |
type(shamrock_field), allocatable, intent(out) :: fields(:) |
| 397 |
integer(kind=int64), allocatable, intent(out) :: offsets(:), bytecounts(:) |
| 398 |
integer, allocatable, intent(out) :: pids(:) |
| 399 |
real, intent(out) :: kernel_hfact, particle_mass |
| 400 |
integer, intent(out) :: ierr |
| 401 |
character(len=:), allocatable :: layout_text |
| 402 |
integer :: nfields,i,istat,arr_len |
| 403 |
|
| 404 |
ierr = 0 |
| 405 |
kernel_hfact = 1.2 |
| 406 |
particle_mass = 0.0 |
| 407 |
|
| 408 |
call extract_real(user_header,'solver_config.gpart_mass',particle_mass) |
| 409 |
call extract_kernel_hfact(user_header,kernel_hfact) |
| 410 |
|
| 411 |
istat = json_success |
| 412 |
call json_read(sched_header,'patchdata_layout',layout_text,istat) |
| 413 |
if (istat /= json_success) then |
| 414 |
ierr = 1 |
| 415 |
allocate(fields(0), offsets(0), bytecounts(0), pids(0)) |
| 416 |
return |
| 417 |
endif |
| 418 |
|
| 419 |
call json_array_length(layout_text,nfields,istat) |
| 420 |
if (istat /= json_success .or. nfields <= 0) then |
| 421 |
ierr = 1 |
| 422 |
if (allocated(layout_text)) deallocate(layout_text) |
| 423 |
allocate(fields(0), offsets(0), bytecounts(0), pids(0)) |
| 424 |
return |
| 425 |
endif |
| 426 |
|
| 427 |
allocate(fields(nfields)) |
| 428 |
do i = 1, nfields |
| 429 |
call get_field(layout_text,i-1,fields(i),ierr) |
| 430 |
if (ierr /= json_success) exit |
| 431 |
enddo |
| 432 |
if (allocated(layout_text)) deallocate(layout_text) |
| 433 |
|
| 434 |
if (ierr /= 0) then |
| 435 |
if (allocated(fields)) deallocate(fields) |
| 436 |
allocate(fields(0), offsets(0), bytecounts(0), pids(0)) |
| 437 |
return |
| 438 |
endif |
| 439 |
|
| 440 |
call json_read(file_header,'offsets',offsets,istat) |
| 441 |
if (istat /= json_success) then |
| 442 |
ierr = 1 |
| 443 |
allocate(offsets(0), bytecounts(0), pids(0)) |
| 444 |
return |
| 445 |
endif |
| 446 |
|
| 447 |
call json_read(file_header,'bytecounts',bytecounts,istat) |
| 448 |
if (istat /= json_success) then |
| 449 |
ierr = 1 |
| 450 |
allocate(bytecounts(0), pids(0)) |
| 451 |
return |
| 452 |
endif |
| 453 |
|
| 454 |
call json_read(file_header,'pids',pids,istat) |
| 455 |
if (istat /= json_success) then |
| 456 |
ierr = 1 |
| 457 |
allocate(pids(0)) |
| 458 |
return |
| 459 |
endif |
| 460 |
|
| 461 |
arr_len = size(pids) |
| 462 |
if (size(offsets) /= arr_len .or. size(bytecounts) /= arr_len) then |
| 463 |
ierr = 1 |
| 464 |
endif |
| 465 |
|
| 466 |
end subroutine read_shamrock_header |
| 467 |
|
| 468 |
!----------------------------------------------------------------- |
| 469 |
! extract a single field from the layout array |
| 470 |
!----------------------------------------------------------------- |
| 471 |
subroutine get_field(layout_text,index,field,ierr) |
| 472 |
character(len=*), intent(in) :: layout_text |
| 473 |
integer, intent(in) :: index |
| 474 |
type(shamrock_field), intent(inout) :: field |
| 475 |
integer, intent(out) :: ierr |
| 476 |
character(len=:), allocatable :: elem_text |
| 477 |
character(len=:), allocatable :: tmp |
| 478 |
integer :: kind, stat |
| 479 |
|
| 480 |
ierr = json_success |
| 481 |
|
| 482 |
call json_array_get_element(layout_text,index,elem_text,kind,stat) |
| 483 |
if (stat /= json_success .or. kind /= json_kind_object) then |
| 484 |
ierr = 1 |
| 485 |
if (allocated(elem_text)) deallocate(elem_text) |
| 486 |
return |
| 487 |
endif |
| 488 |
|
| 489 |
call json_read(elem_text,'field_name',tmp,stat) |
| 490 |
if (stat /= json_success) then |
| 491 |
ierr = 1 |
| 492 |
if (allocated(tmp)) deallocate(tmp) |
| 493 |
if (allocated(elem_text)) deallocate(elem_text) |
| 494 |
return |
| 495 |
endif |
| 496 |
field%name = trim(tmp) |
| 497 |
if (allocated(tmp)) deallocate(tmp) |
| 498 |
|
| 499 |
call json_read(elem_text,'type',tmp,stat) |
| 500 |
if (stat /= json_success) then |
| 501 |
ierr = 1 |
| 502 |
if (allocated(tmp)) deallocate(tmp) |
| 503 |
if (allocated(elem_text)) deallocate(elem_text) |
| 504 |
return |
| 505 |
endif |
| 506 |
field%ftype = trim(tmp) |
| 507 |
if (allocated(tmp)) deallocate(tmp) |
| 508 |
|
| 509 |
if (allocated(elem_text)) deallocate(elem_text) |
| 510 |
|
| 511 |
end subroutine get_field |
| 512 |
|
| 513 |
!----------------------------------------------------------------- |
| 514 |
! get the header pairs from the user header by parsing the JSON |
| 515 |
! string for key:value pairs |
| 516 |
!----------------------------------------------------------------- |
| 517 |
subroutine get_header_pairs(user_header,headertags,headervals,nheader) |
| 518 |
use settings_data, only:debugmode |
| 519 |
character(len=*), intent(in) :: user_header |
| 520 |
character(len=*), intent(out) :: headertags(:) |
| 521 |
real, intent(out) :: headervals(:) |
| 522 |
integer, intent(inout) :: nheader |
| 523 |
character(len=len(user_header)) :: string |
| 524 |
character(len=len(headertags(1))) :: key_string |
| 525 |
character(len=32) :: val_string |
| 526 |
integer :: icolon,ierr,i1,i2 |
| 527 |
|
| 528 |
string = user_header |
| 529 |
icolon = index(user_header,':') |
| 530 |
do while (icolon > 0) |
| 531 |
! key is the thing between the previous two double quotes |
| 532 |
i2 = index(string(1:icolon-1),'"',back=.true.) |
| 533 |
i1 = index(string(1:i2-1),'"',back=.true.) |
| 534 |
key_string = string(i1+1:min(i2-1,i1+len(key_string))) |
| 535 |
|
| 536 |
! val is the thing after the colon |
| 537 |
val_string = string(icolon+1:icolon+len(val_string)) |
| 538 |
|
| 539 |
! try to the value as a real, if this fails, skip it |
| 540 |
read(val_string,*,iostat=ierr) headervals(nheader) |
| 541 |
if (ierr == 0) then |
| 542 |
nheader = nheader + 1 |
| 543 |
headertags(nheader) = trim(key_string) |
| 544 |
if (debugmode) print*,trim(key_string)//':',headervals(nheader) |
| 545 |
endif |
| 546 |
string = string(icolon+1:) ! move to the next colon |
| 547 |
icolon = index(string,':') |
| 548 |
enddo |
| 549 |
|
| 550 |
end subroutine get_header_pairs |
| 551 |
|
| 552 |
!----------------------------------------------------------------- |
| 553 |
! read 64 bit integer and the string following it from file |
| 554 |
!----------------------------------------------------------------- |
| 555 |
subroutine read_int64_and_string(iu,str,ierr) |
| 556 |
integer, intent(in) :: iu |
| 557 |
character(len=:), allocatable, intent(out) :: str |
| 558 |
integer, intent(out) :: ierr |
| 559 |
integer(kind=int64) :: len_str |
| 560 |
|
| 561 |
ierr = 0 |
| 562 |
read(iu,iostat=ierr) len_str |
| 563 |
if (ierr /= 0 .or. len_str < 0_int64) then |
| 564 |
ierr = 1 |
| 565 |
allocate(character(len=0) :: str) |
| 566 |
return |
| 567 |
endif |
| 568 |
|
| 569 |
allocate(character(len=int(len_str)) :: str) |
| 570 |
read(iu,iostat=ierr) str |
| 571 |
|
| 572 |
end subroutine read_int64_and_string |
| 573 |
|
| 574 |
!----------------------------------------------------------------- |
| 575 |
! read the patch counts from the file |
| 576 |
!----------------------------------------------------------------- |
| 577 |
subroutine read_patch_counts(iu,pos_start,nfields,counts,ierr) |
| 578 |
integer, intent(in) :: iu |
| 579 |
integer(kind=int64), intent(in) :: pos_start |
| 580 |
integer, intent(in) :: nfields |
| 581 |
integer(kind=int64), intent(out) :: counts(:) |
| 582 |
integer, intent(out) :: ierr |
| 583 |
integer(kind=int64) :: prehead |
| 584 |
integer :: j |
| 585 |
|
| 586 |
ierr = 0 |
| 587 |
read(iu,pos=pos_start,iostat=ierr) prehead |
| 588 |
if (ierr /= 0) return |
| 589 |
do j = 1, nfields |
| 590 |
read(iu,iostat=ierr) counts(j) |
| 591 |
if (ierr /= 0) return |
| 592 |
enddo |
| 593 |
|
| 594 |
end subroutine read_patch_counts |
| 595 |
|
| 596 |
!----------------------------------------------------------------- |
| 597 |
! read the data from the Shamrock file into the data arrays |
| 598 |
!----------------------------------------------------------------- |
| 599 |
subroutine read_shamrock_data(iu,base_pos,npatches,fields,offsets, & |
| 600 |
istepstart,npart,counts_buffer,idx_xyz,ierr) |
| 601 |
type(shamrock_field), intent(in) :: fields(:) |
| 602 |
integer, intent(in) :: npatches |
| 603 |
integer(kind=int64), intent(in) :: offsets(:),base_pos |
| 604 |
integer(kind=int64), intent(inout) :: counts_buffer(:) |
| 605 |
integer, intent(in) :: iu, istepstart, npart |
| 606 |
integer, intent(in) :: idx_xyz |
| 607 |
integer, intent(out) :: ierr |
| 608 |
integer :: patch,nfields,i1,nobj,ios,npart_got |
| 609 |
integer(kind=int64) :: pos |
| 610 |
|
| 611 |
ierr = 0 |
| 612 |
nfields = size(fields) |
| 613 |
i1 = 0 |
| 614 |
npart_got = 0 |
| 615 |
|
| 616 |
do patch = 1,npatches |
| 617 |
pos = base_pos + offsets(patch) |
| 618 |
call read_patch_counts(iu,pos,nfields,counts_buffer,ios) |
| 619 |
if (ios /= 0) then |
| 620 |
ierr = 1 |
| 621 |
return |
| 622 |
endif |
| 623 |
nobj = int(counts_buffer(idx_xyz)) |
| 624 |
call read_patch_data_block(iu,nfields,fields,counts_buffer, & |
| 625 |
istepstart,i1,ierr) |
| 626 |
if (ierr /= 0) return |
| 627 |
i1 = i1 + nobj |
| 628 |
npart_got = npart_got + nobj |
| 629 |
enddo |
| 630 |
|
| 631 |
if (npart_got /= npart) then |
| 632 |
print*,' WARNING: particle count mismatch in Shamrock reader' |
| 633 |
endif |
| 634 |
|
| 635 |
end subroutine read_shamrock_data |
| 636 |
|
| 637 |
!----------------------------------------------------------------- |
| 638 |
! read the data from a single patch into the data arrays |
| 639 |
!----------------------------------------------------------------- |
| 640 |
subroutine read_patch_data_block(iu,nfields,fields,counts,istepstart,i1,ierr) |
| 641 |
type(shamrock_field), intent(in) :: fields(:) |
| 642 |
integer, intent(in) :: nfields,istepstart,i1,iu |
| 643 |
integer(kind=int64), intent(in) :: counts(:) |
| 644 |
integer, intent(out) :: ierr |
| 645 |
integer :: idx |
| 646 |
|
| 647 |
ierr = 0 |
| 648 |
! stream is already positioned at the first field payload by read_patch_counts |
| 649 |
|
| 650 |
do idx = 1, nfields |
| 651 |
select case (trim(fields(idx)%ftype)) |
| 652 |
case('f64_3') |
| 653 |
call read_vector_field(iu,fields(idx),counts(idx),i1,istepstart,ierr) |
| 654 |
case('f64') |
| 655 |
call read_scalar_field(iu,fields(idx),counts(idx),i1,istepstart,ierr) |
| 656 |
case default |
| 657 |
! skip unsupported field types for now |
| 658 |
end select |
| 659 |
if (ierr /= 0) return |
| 660 |
enddo |
| 661 |
|
| 662 |
end subroutine read_patch_data_block |
| 663 |
|
| 664 |
!----------------------------------------------------------------- |
| 665 |
! read the data from a vector field into the data arrays |
| 666 |
!----------------------------------------------------------------- |
| 667 |
subroutine read_vector_field(iu,field,count,i1,istepstart,ierr) |
| 668 |
use particle_data, only:dat |
| 669 |
type(shamrock_field), intent(in) :: field |
| 670 |
integer(kind=int64), intent(in) :: count |
| 671 |
integer, intent(in) :: iu, i1, istepstart |
| 672 |
integer, intent(out) :: ierr |
| 673 |
real, allocatable :: flat(:) |
| 674 |
integer :: p,comp,base_col |
| 675 |
integer(kind=int64) :: size_bytes, aligned_bytes, pad_bytes |
| 676 |
integer(kind=int8), allocatable :: pad(:) |
| 677 |
|
| 678 |
ierr = 0 |
| 679 |
if (count <= 0) return |
| 680 |
|
| 681 |
base_col = field%col_start |
| 682 |
allocate(flat(int(count)*field%ncomp)) |
| 683 |
read(iu,iostat=ierr) flat |
| 684 |
if (ierr /= 0) then |
| 685 |
ierr = 1 |
| 686 |
deallocate(flat) |
| 687 |
return |
| 688 |
endif |
| 689 |
do p = 1,int(count) |
| 690 |
do comp = 1, field%ncomp |
| 691 |
dat(i1+p,base_col+comp-1,istepstart) = real(flat((p-1)*field%ncomp+comp)) |
| 692 |
enddo |
| 693 |
enddo |
| 694 |
deallocate(flat) |
| 695 |
|
| 696 |
size_bytes = count*field%ncomp*8_int64 |
| 697 |
aligned_bytes = align8(size_bytes) |
| 698 |
pad_bytes = aligned_bytes - size_bytes |
| 699 |
if (pad_bytes > 0_int64) then |
| 700 |
allocate(pad(pad_bytes)) |
| 701 |
read(iu,iostat=ierr) pad |
| 702 |
deallocate(pad) |
| 703 |
if (ierr /= 0) ierr = 1 |
| 704 |
endif |
| 705 |
|
| 706 |
end subroutine read_vector_field |
| 707 |
|
| 708 |
!----------------------------------------------------------------- |
| 709 |
! read the data from a scalar field into the data arrays |
| 710 |
!----------------------------------------------------------------- |
| 711 |
subroutine read_scalar_field(iu,field,count,i1,istepstart,ierr) |
| 712 |
use particle_data, only:dat |
| 713 |
type(shamrock_field), intent(in) :: field |
| 714 |
integer(kind=int64), intent(in) :: count |
| 715 |
integer, intent(in) :: iu,i1,istepstart |
| 716 |
integer, intent(out) :: ierr |
| 717 |
real, allocatable :: values(:) |
| 718 |
integer(kind=int64) :: size_bytes, aligned_bytes, pad_bytes |
| 719 |
integer(kind=int8), allocatable :: pad(:) |
| 720 |
|
| 721 |
ierr = 0 |
| 722 |
if (count <= 0) return |
| 723 |
allocate(values(count)) |
| 724 |
read(iu,iostat=ierr) values |
| 725 |
if (ierr /= 0) then |
| 726 |
ierr = 1 |
| 727 |
deallocate(values) |
| 728 |
return |
| 729 |
endif |
| 730 |
dat(i1+1:i1+count,field%col_start,istepstart) = real(values) |
| 731 |
deallocate(values) |
| 732 |
|
| 733 |
size_bytes = count*8_int64 |
| 734 |
aligned_bytes = align8(size_bytes) |
| 735 |
pad_bytes = aligned_bytes - size_bytes |
| 736 |
if (pad_bytes > 0_int64) then |
| 737 |
allocate(pad(pad_bytes)) |
| 738 |
read(iu,iostat=ierr) pad |
| 739 |
deallocate(pad) |
| 740 |
if (ierr /= 0) ierr = 1 |
| 741 |
endif |
| 742 |
|
| 743 |
end subroutine read_scalar_field |
| 744 |
|
| 745 |
!----------------------------------------------------------------- |
| 746 |
! align the number of bytes to the next multiple of 8 |
| 747 |
!----------------------------------------------------------------- |
| 748 |
integer(kind=int64) function align8(nbytes) result(res) |
| 749 |
integer(kind=int64), intent(in) :: nbytes |
| 750 |
integer(kind=int64) :: rem |
| 751 |
|
| 752 |
res = nbytes |
| 753 |
rem = modulo(res,8_int64) |
| 754 |
if (rem /= 0_int64) res = res + (8_int64 - rem) |
| 755 |
|
| 756 |
end function align8 |
| 757 |
|
| 758 |
!----------------------------------------------------------------- |
| 759 |
! extract a real value from a JSON text |
| 760 |
!----------------------------------------------------------------- |
| 761 |
subroutine extract_real(json_text,path,value) |
| 762 |
use json_utils, only:json_get_value_by_path |
| 763 |
character(len=*), intent(in) :: json_text |
| 764 |
character(len=*), intent(in) :: path |
| 765 |
real, intent(out) :: value |
| 766 |
character(len=:), allocatable :: raw |
| 767 |
integer :: value_kind, ierr, ios |
| 768 |
|
| 769 |
value = 0. |
| 770 |
call json_get_value_by_path(json_text,path,raw,value_kind,ierr) |
| 771 |
if (ierr == json_success .and. value_kind == json_kind_number) then |
| 772 |
read(raw,*,iostat=ios) value |
| 773 |
if (ios /= 0) value = 0. |
| 774 |
endif |
| 775 |
if (allocated(raw)) deallocate(raw) |
| 776 |
|
| 777 |
end subroutine extract_real |
| 778 |
|
| 779 |
!----------------------------------------------------------------- |
| 780 |
! extract the kernel hfact from a JSON text |
| 781 |
!----------------------------------------------------------------- |
| 782 |
subroutine extract_kernel_hfact(json_text,hfact) |
| 783 |
use json_utils, only:json_get_value_by_path |
| 784 |
character(len=*), intent(in) :: json_text |
| 785 |
real, intent(out) :: hfact |
| 786 |
character(len=:), allocatable :: kernel |
| 787 |
integer :: value_kind, ierr |
| 788 |
|
| 789 |
hfact = 1.2 |
| 790 |
call json_get_value_by_path(json_text,'solver_config.kernel_id',kernel,value_kind,ierr) |
| 791 |
if (ierr == json_success .and. value_kind == json_kind_string) then |
| 792 |
select case (kernel(1:min(2,len_trim(kernel)))) |
| 793 |
case('M4','M5') |
| 794 |
hfact = 1.2 |
| 795 |
case('M6') |
| 796 |
hfact = 1.0 |
| 797 |
case default |
| 798 |
hfact = 1.2 |
| 799 |
end select |
| 800 |
endif |
| 801 |
if (allocated(kernel)) deallocate(kernel) |
| 802 |
|
| 803 |
end subroutine extract_kernel_hfact |
| 804 |
|
| 805 |
end module readdata_shamrock |
| 806 |
|