Debian Codegraph @

src/read_data_falcON_hdf5.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-2015 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 HDF5 OUTPUT FROM W. DEHNEN'S FALCON CODE
28 !
29 ! the data is stored in the global array dat
30 !
31 ! >> this subroutine must return values for the following: <<
32 !
33 ! ncolumns : number of data columns
34 ! ndim, ndimV : number of spatial, velocity dimensions
35 ! nstepsread : number of steps read from this file
36 !
37 ! dat(maxpart,maxplot,maxstep) : main data array
38 !
39 ! npartoftype(maxstep): number of particles of each type in each timestep
40 !
41 ! time(maxstep) : time at each step
42 ! gamma(maxstep) : gamma at each step
43 ! (used in calc_quantities for calculating the pressure)
44 !
45 ! most of these values are stored in global arrays
46 ! in the module 'particle_data'
47 !
48 ! Columns with the 'required' flag set to false are not read
49 !-------------------------------------------------------------------------
50 !
51 ! The module below contains interface routines to c functions
52 ! that perform the actual calls to the HDF5 libs
53 !
54 !-------------------------------------------------------------------------
55 module falcONhdf5read
56 use params, only:maxplot,doub_prec
57 use labels, only:lenlabel
58 use, intrinsic :: iso_c_binding, only:c_int,c_double,c_char
59 implicit none
60 character(len=lenlabel), dimension(maxplot) :: blocklabel
61 integer, parameter :: maxtypes = 6
62 integer :: i_current_step
63
64 interface
65 ! opens a falcON HDF5 snapshot file
66 subroutine open_falcON_file(filename,ierr) bind(c,name="open_falcON_file")
67 import
68 character(c_char), intent(in) :: filename(*)
69 integer(c_int), intent(out) :: ierr
70 end subroutine open_falcON_file
71
72 ! queries whether a file is open
73 function falcON_file_is_open() bind(c,name="falcON_file_is_open")
74 import
75 integer(c_int) :: falcON_file_is_open
76 end function falcON_file_is_open
77
78 ! closes the currently open (if any) falcON HDF5 snapshot file.
79 subroutine close_falcON_file() bind(c,name="close_falcON_file")
80 ! no arguments
81 end subroutine close_falcON_file
82
83 ! queries if there is another snapshot present the currently open file
84 function num_falcON_snapshots(ierr) bind(c,name="num_falcON_snapshots")
85 import
86 integer(c_int) :: num_falcON_snapshots
87 integer(c_int), intent(out) :: ierr
88 end function num_falcON_snapshots
89
90 ! set falcON debugging level
91 subroutine set_falcON_debugging_level(level) bind(c,name="set_falcON_debugging_level")
92 import
93 integer(c_int), intent(in) :: level
94 end subroutine set_falcON_debugging_level
95
96 ! read falcON header
97 subroutine open_falcON_snapshot(ntype,npart,ncol,dimX,dimV,time,hper,ierr) &
98 bind(c,name="open_falcON_snapshot")
99 import
100 integer(c_int), intent(out) :: ntype,ncol,dimX,dimV,ierr
101 integer(c_int), intent(out) :: npart(*)
102 real(c_double), intent(out) :: time,hper(3)
103 end subroutine open_falcON_snapshot
104
105 ! read falcON data
106 subroutine read_falcON_snapshot(ierr) bind(c,name="read_falcON_snapshot")
107 import
108 integer(c_int), intent(out) :: ierr
109 end subroutine read_falcON_snapshot
110 end interface
111
112 contains
113
114 ! map types from falcON to splash
115 integer function itypemap_falcON(itype)
116 integer, intent(in) :: itype
117
118 select case(itype)
119 case(1) ! sinks
120 itypemap_falcON = 2
121 case(2) ! gas
122 itypemap_falcON = 1
123 case(3:4)
124 itypemap_falcON = itype
125 case(5:maxtypes)
126 itypemap_falcON = itype+1
127 case default
128 itypemap_falcON = 5 ! unknown
129 end select
130
131 end function itypemap_falcON
132
133 ! get starting position in particle array
134 integer function ioffset(itype,npartoftype)
135 integer, intent(in) :: itype
136 integer, intent(in) :: npartoftype(:)
137 integer :: i
138
139 ioffset = 0
140 do i=1,size(npartoftype)
141 if (i < itype) ioffset = ioffset + npartoftype(i)
142 enddo
143
144 end function ioffset
145
146 end module falcONhdf5read
147
148 !-------------------------------------------------------------------------
149 !
150 ! The routine that reads the data into splash's internal arrays
151 !
152 !-------------------------------------------------------------------------
153
154 module readdata_falcON_hdf5
155 implicit none
156
157 public :: read_data_falcON_hdf5, set_labels_falcON_hdf5
158
159 private
160 contains
161
162 subroutine read_data_falcON_hdf5(rootname,istepstart,ipos,nstepsread)
163 use particle_data, only:dat,npartoftype,masstype,time,gamma,maxpart,maxcol
164 use params, only:doub_prec,maxparttypes !,maxplot
165 use settings_data, only:ndim,ndimV,ncolumns,ncalc,ipartialread, &
166 ntypes,debugmode,iverbose,buffer_steps_in_file
167 use mem_allocation, only:alloc
168 use labels, only:print_types,labeltype
169 use system_utils, only:lenvironment
170 use asciiutils, only:cstring
171 use dataread_utils, only:check_range
172 use falcONhdf5read
173 integer, intent(in) :: istepstart,ipos
174 integer, intent(out) :: nstepsread
175 character(len=*), intent(in) :: rootname
176 character(len=len(rootname)+10) :: datfile
177 integer :: i,j,ierr,ierror(8),istep,nsteps_to_read
178 integer :: ncolstep,npart_max,nstep_max,ntoti,ntotall
179 integer :: npartoftypei(maxparttypes)
180 logical :: iexist,reallocate,debug,goterrors
181 real(doub_prec) :: timetemp,hperiodic(3)
182 !integer, dimension(maxplot) :: isrequired
183
184 nstepsread = 0
185 goterrors = .false.
186
187 if (len_trim(rootname) > 0) then
188 datfile = trim(rootname)
189 else
190 print*,' **** no data read **** '
191 return
192 endif
193 !
194 !--check if first data file exists
195 !
196 if (iverbose==1 .and. ipos==1) print "(1x,a)",'reading FalcON hdf5 format'
197 inquire(file=datfile,exist=iexist)
198 if (.not.iexist) then
199 !
200 !--append .h5 on the endif not already present
201 !
202 datfile=trim(rootname)//'.h5'
203 inquire(file=datfile,exist=iexist)
204 if (.not.iexist) then
205 print "(a)",' *** error: '//trim(rootname)//': file not found ***'
206 return
207 endif
208 endif
209 !
210 ! set parameters which do not vary between timesteps
211 !
212 ndim = 3
213 ndimV = 3
214 debug = (debugmode .or. lenvironment('FSPLASH_DEBUG'))
215 !
216 ! read data from snapshots
217 !
218 i = istepstart
219 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
220 !
221 ! open file and read header information
222 !
223 if (debug) print*,'DEBUG: reading header...'
224
225 call open_falcON_file(cstring(datfile),ierr)
226 if (ierr /= 0) then
227 print "(a)", '*** ERROR OPENING FALCON FILE ***'
228 return
229 endif
230
231 if (falcON_file_is_open() /= 1) then
232 print "(a)", '*** ERROR: falcON_file_is_open /= 1 after opening ***'
233 return
234 endif
235
236 if (debug) call set_falcON_debugging_level(3);
237
238 nstep_max = num_falcON_snapshots(ierr);
239 if (debug) print*,'got ',nstep_max,' falcON snapshots in file'
240 if (nstep_max <= 0) then
241 print "(a)",'*** ERROR: no falcON snapshots found in file ***'
242 return
243 endif
244
245 ntotall = 0
246 if (buffer_steps_in_file) then
247 nsteps_to_read = nstep_max
248 else
249 nsteps_to_read = 1
250 endif
251
252 i = istepstart
253 over_snapshots: do istep=1,nstep_max
254 !
255 ! read falcON header
256 !
257 if (debug) print*,'DEBUG: opening snapshot ',i
258 npartoftypei(:) = 0
259 call open_falcON_snapshot(ntypes, npartoftypei, &
260 ncolstep, ndim, ndimV, timetemp, &
261 hperiodic, ierr)
262 !
263 ! error checking on header info
264 !
265 ierror(:) = 0
266 call check_range(ntypes,'ntypes',min=1,err=ierror(1))
267 call check_range(npartoftypei(1:ntypes),'npartoftype',min=0,err=ierror(2))
268 call check_range(sum(npartoftypei(1:ntypes)),'ntot',min=1,err=ierror(3))
269 call check_range(ndim,'ndim',min=1,max=3,err=ierror(4))
270 call check_range(ndimV,'ndimV',min=ndim,max=3,err=ierror(5))
271 call check_range(timetemp,'time',min=0.d0,err=ierror(6))
272 call check_range(ierr,'error during header read',min=0,max=0,err=ierror(7))
273 if (any(ierror(1:7) > 0)) then
274 print*,'*** ERROR during falcON header read ***'
275 return
276 endif
277 ncolumns = ncolstep
278 ntoti = sum(npartoftypei(1:ntypes))
279 ntotall = max(ntoti,ntotall)
280 !
281 ! print header information
282 !
283 if (iverbose >= 1 .and. buffer_steps_in_file .or. istep==ipos) then
284 !print "(2(a,1x,i10))",' npart: ',ntoti,' ncolumns: ',ncolstep
285 !print "(a,i2)",' ntypes: ',ntypes,'
286 !print*,' npartoftype = ',(npartoftypei(itypemap_falcON(j)),j=1,ntypes)
287 !print*,' ncolstep = ',ncolstep,' ndim = ',ndim,ndimV
288 print*,' time = ',timetemp !,' hper = ',hperiodic(:)
289 endif
290 !
291 ! now read data
292 !
293 reallocate = .false.
294 npart_max = maxpart
295
296 if (ntoti > maxpart) then
297 reallocate = .true.
298 if (maxpart > 0) then
299 ! if we are reallocating, try not to do it again
300 npart_max = int(1.1*ntotall)
301 else
302 ! if first time, save on memory
303 npart_max = int(ntoti)
304 endif
305 endif
306 !
307 ! reallocate memory for main data array
308 !
309 if (reallocate .or. .not.(allocated(dat))) then
310 call alloc(npart_max,nsteps_to_read,max(ncolumns+ncalc,maxcol))
311 endif
312
313 !
314 ! copy header data into allocated arrays
315 !
316 if (buffer_steps_in_file .or. istep==ipos) then
317 do j=1,ntypes
318 npartoftype(itypemap_falcON(j),i) = npartoftypei(j)
319 enddo
320 time(i) = real(timetemp)
321 masstype(:,i) = 0. ! all masses read from file
322 endif
323 !
324 ! read particle data
325 !
326 got_particles: if (ntoti > 0) then
327 !isrequired(:) = 0
328 !where (required(1:ncolumns)) isrequired(1:ncolumns) = 1
329 if (buffer_steps_in_file .or. istep==ipos) then
330 i_current_step = i
331 call read_falcON_snapshot(ierr);
332 if (ierr /= 0) then
333 print "(/,1x,a,/)",' *** ERROR reading falcON snapshot ***'
334 print*,'Press any key to continue (but there is likely something wrong with the file...)'
335 read*
336 endif
337 call print_types(npartoftype(:,i),labeltype)
338 i = i + 1
339 endif
340 nstepsread = nstepsread + 1
341 else
342 !
343 ! cover the special case where no particles have been read
344 !
345 npartoftype(1,i) = 1
346 dat(:,:,i) = 0.
347
348 endif got_particles
349
350 enddo over_snapshots
351 !
352 ! now memory has been allocated, set arrays which are constant for all time
353 !
354 gamma = 5./3.
355 !
356 ! set flag to indicate that only part of this file has been read
357 !
358 ipartialread = .false.
359 !if (.not.all(required(1:ncolstep))) ipartialread = .true.
360 !
361 ! call set labels to identify location of smoothing length
362 !
363 call set_labels_falcON_hdf5
364
365 !if (nstepsread > 0) then
366 ! print "(a,i10,a)",' >> read ',sum(npartoftype(:,i)),' particles'
367 !endif
368 call close_falcON_file()
369 return
370
371 end subroutine read_data_falcON_hdf5
372
373 subroutine read_falcON_data_into_splash(icol,npartoftypei,temparr,itypec) bind(c,name="read_falcON_data_into_splash")
374 use, intrinsic :: iso_c_binding, only:c_int,c_double
375 use particle_data, only:dat,iamtype,npartoftype
376 use settings_data, only:debugmode
377 use labels, only:label
378 use falcONhdf5read, only:itypemap_falcON,ioffset,i_current_step
379 integer(kind=c_int), intent(in) :: icol,npartoftypei,itypec
380 real(kind=c_double), intent(in) :: temparr(*)
381 integer(kind=c_int) :: icolput
382 integer :: istart,iend,nmax,itype,i
383
384 itype = itypec + 1 ! convert from c to Fortran indexing
385
386 icolput = icol + 1
387 if (debugmode) print "(3(a,i2),a,i8)",'DEBUG: Step ',i_current_step,' column ',icol,&
388 ' type ',itypemap_falcON(itype),' -> '//trim(label(icolput))
389
390 ! check column is within array limits
391 if (icolput > size(dat(1,:,1)) .or. icolput==0) then
392 print "(a,i2,a)",' ERROR: column = ',icolput,' out of range in receive_data_fromc'
393 return
394 endif
395
396 ! ensure no array overflows
397 istart = ioffset(itypemap_falcON(itype),npartoftype(:,i_current_step)) + 1
398 iend = min(istart + npartoftypei - 1,size(dat(:,1,1)))
399 nmax = iend - istart + 1
400
401 ! copy data into main splash array
402 if (debugmode) print*,'DEBUG: COPYING TO ',istart,iend,' total = ',1,nmax
403
404 ! this should never happen
405 if (i_current_step < 1 .or. i_current_step > size(dat(1,1,:))) then
406 print*,'INTERNAL ERROR in indexing during falcON read'
407 return
408 endif
409 dat(istart:iend,icolput,i_current_step) = real(temparr(1:nmax),kind=kind(dat))
410
411 ! set particle type
412 if (size(iamtype(:,1)) > 1) then
413 print*,' SETTING TYPES ',istart,iend
414 do i=istart,iend
415 iamtype(i,i_current_step) = int(itypemap_falcON(itype),kind=kind(iamtype))
416 enddo
417 endif
418
419 return
420 end subroutine read_falcON_data_into_splash
421
422 !------------------------------------------------------------
423 ! set labels for each column of data
424 !------------------------------------------------------------
425 subroutine set_labels_falcON_hdf5
426 use labels, only:label,iamvec,labelvec,ix,ivx,ipmass, &
427 ih,irho,iax,iutherm !ipr,iutherm
428 use settings_data, only:ndim,ndimV,UseTypeInRenderings
429 use geometry, only:labelcoord
430 use falcONhdf5read, only:blocklabel
431 use asciiutils, only:lcase
432 integer :: i,icol
433
434 if (ndim <= 0 .or. ndim > 3) then
435 print*,'*** ERROR: ndim = ',ndim,' in set_labels_falcON_hdf5 ***'
436 return
437 endif
438 if (ndimV <= 0 .or. ndimV > 3) then
439 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_falcON_hdf5 ***'
440 return
441 endif
442
443 ix = 0
444 iutherm = 0
445 do icol=1,size(blocklabel)
446 select case(trim(lcase(blocklabel(icol))))
447 case('x')
448 ix(1) = icol
449 case('y')
450 ix(2) = icol
451 case('z')
452 ix(3) = icol
453 case('vx')
454 ivx = icol
455 case('ax')
456 iax = icol
457 case('h')
458 ih = icol
459 case('mass')
460 ipmass = icol
461 case('srho')
462 irho = icol
463 end select
464 label(icol) = trim(blocklabel(icol))
465 enddo
466
467 ! set labels of the quantities read in
468 if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1)
469 if (irho > 0) label(irho) = 'density'
470 !if (iutherm > 0) label(iutherm) = 'u'
471 if (ipmass > 0) label(ipmass) = 'particle mass'
472
473 ! set labels for vector quantities
474 if (ivx > 0) then
475 iamvec(ivx:ivx+ndimV-1) = ivx
476 labelvec(ivx:ivx+ndimV-1) = 'v'
477 do i=1,ndimV
478 label(ivx+i-1) = trim(labelvec(ivx))//'_'//labelcoord(i,1)
479 enddo
480 endif
481
482 if (iax > 0) then
483 iamvec(iax:iax+ndimV-1) = iax
484 labelvec(iax:iax+ndimV-1) = 'a'
485 do i=1,ndimV
486 label(iax+i-1) = trim(labelvec(iax))//'_'//labelcoord(i,1)
487 enddo
488 endif
489
490 ! labels for each particle type already set
491 UseTypeInRenderings(:) = .false.
492 UseTypeInRenderings(1) = .true.
493
494 !-----------------------------------------------------------
495 return
496 end subroutine set_labels_falcON_hdf5
497
498 subroutine set_splash_block_label(icol,name) bind(c)
499 use, intrinsic :: iso_c_binding, only:c_int, c_char
500 use falcONhdf5read, only:blocklabel
501 use asciiutils, only:fstring
502 integer(kind=c_int), intent(in) :: icol
503 character(kind=c_char), intent(in) :: name(256)
504
505 blocklabel(icol+1) = trim(fstring(name))
506 !print*,icol,' name = ',trim(blocklabel(icol))
507
508 end subroutine set_splash_block_label
509
510 subroutine set_splash_particle_label(itypec,name) bind(c)
511 use, intrinsic :: iso_c_binding, only:c_int, c_char
512 use asciiutils, only:fstring
513 use labels, only:labeltype
514 use falcONhdf5read, only:itypemap_falcON
515 integer(kind=c_int), intent(in) :: itypec
516 character(kind=c_char), intent(in) :: name(256)
517
518 !print*,' got type = ',itypec,' setting ',itypemap_falcON(itypec+1),'= ',trim(fstring(name))
519 labeltype(itypemap_falcON(itypec+1)) = trim(fstring(name))
520
521 end subroutine set_splash_particle_label
522 end module readdata_falcON_hdf5