Debian Codegraph @

src/read_data_cactus_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-2017 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 THE CACTUS 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 !-------------------------------------------------------------------------
52 !
53 ! The routine that reads the data into splash's internal arrays
54 !
55 !-------------------------------------------------------------------------
56
57 module readdata_cactus_hdf5
58 implicit none
59
60 public :: read_data_cactus_hdf5, set_labels_cactus_hdf5
61
62 private
63 contains
64
65 subroutine read_data_cactus_hdf5(rootname,istepstart,ipos,nstepsread)
66 use particle_data, only:dat,npartoftype,masstype,time,gamma,maxpart,maxcol,maxstep,iamtype
67 use params, only:doub_prec
68 use settings_data, only:ndim,ndimV,ncolumns,ncalc,ipartialread,iverbose,buffer_steps_in_file
69 use settings_page, only:legendtext
70 use mem_allocation, only:alloc
71 use labels, only:ih,irho,ipmass
72 use system_utils, only:renvironment,lenvironment,ienvironment,envlist
73 use asciiutils, only:cstring
74 use cactushdf5read, only:open_cactus_hdf5_file,read_cactus_hdf5_data,close_cactus_hdf5_file,&
75 datfileprev,file_is_open,ntoti_prev,ncol_prev,nstep_prev,compute_extra_columns
76 use dataread_utils, only:count_types
77 integer, intent(in) :: istepstart,ipos
78 integer, intent(out) :: nstepsread
79 character(len=*), intent(in) :: rootname
80 character(len=len(rootname)+10) :: datfile
81 integer :: i,istep,ierr,nextra
82 integer :: nunknown,ignoretl
83 integer :: ncolstep,npart_max,nstep_max,nsteps_to_read,ntoti
84 logical :: iexist,reallocate,goterrors,ignore_time_levels
85 real(doub_prec) :: timetemp,dx,vol
86
87 nstepsread = 0
88 goterrors = .false.
89
90 if (len_trim(rootname) > 0) then
91 datfile = trim(rootname)
92 else
93 print*,' **** no data read **** '
94 return
95 endif
96 !
97 !--check if first data file exists
98 !
99 if (iverbose==1 .and. ipos==1) print "(1x,a)",'reading CACTUS HDF5 format'
100 inquire(file=datfile,exist=iexist)
101 if (.not.iexist) then
102 !
103 !--append .h5 on the endif not already present
104 !
105 datfile=trim(rootname)//'.h5'
106 inquire(file=datfile,exist=iexist)
107 endif
108 !
109 !--close previous file if filenames do not match
110 !
111 if (trim(datfile)/=trim(datfileprev) .and. file_is_open) then
112 call close_cactus_hdf5_file(ierr)
113 file_is_open = .false.
114 endif
115
116 if (.not.iexist) then
117 print "(a)",' *** error: '//trim(rootname)//': file not found ***'
118 return
119 endif
120 !
121 !--set parameters which do not vary between timesteps
122 !
123 ndim = 3
124 ndimV = 3
125 ignore_time_levels = lenvironment('CSPLASH_IGNORE_TIME_LEVELS')
126 ignoretl = 0
127 if (ignore_time_levels) ignoretl = 1
128 nextra = 0
129 !
130 !--read data from snapshots
131 !
132 i = istepstart
133 if (.not.file_is_open) write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
134 !
135 !--open file and read header information
136 !
137 if (file_is_open) then
138 ntoti = ntoti_prev
139 ncolstep = ncol_prev
140 nstep_max = nstep_prev
141 else
142 call open_cactus_hdf5_file(cstring(datfile),ipos,ntoti,ncolstep,nstep_max,ndim,ndimV,timetemp,ignoretl,ierr)
143 if (ierr /= 0) then
144 print "(a)", '*** ERROR READING HEADER ***'
145 call close_cactus_hdf5_file(ierr)
146 return
147 endif
148 file_is_open = .true.
149 datfileprev = datfile
150 ntoti_prev = ntoti
151 ncol_prev = ncolstep
152 nstep_prev = nstep_max
153 endif
154 call compute_extra_columns(ncolstep,nextra)
155 ncolumns = ncolstep + nextra
156 if (iverbose >= 1) print "(3(a,1x,i10))",' npart: ',ntoti,' ncolumns: ',ncolstep,' nsteps: ',nstep_max
157
158 istep = 1
159 over_snapshots: do istep=1,nstep_max
160 !
161 !--now read data
162 !
163 reallocate = .false.
164 npart_max = maxpart
165 if (buffer_steps_in_file) then
166 nsteps_to_read = nstep_max
167 else
168 nsteps_to_read = max(maxstep,1)
169 endif
170 if (nsteps_to_read > maxstep) reallocate = .true.
171
172 if (ntoti > maxpart) then
173 reallocate = .true.
174 if (maxpart > 0) then
175 ! if we are reallocating, try not to do it again
176 npart_max = int(1.1*ntoti)
177 else
178 ! if first time, save on memory
179 npart_max = int(ntoti)
180 endif
181 endif
182 !
183 !--reallocate memory for main data array
184 !
185 if (reallocate .or. .not.(allocated(dat))) then
186 call alloc(npart_max,nsteps_to_read,max(ncolumns+ncalc,maxcol),mixedtypes=.true.)
187 endif
188
189 !
190 !--read particle data
191 !
192 got_particles: if (ntoti > 0) then
193
194 if (buffer_steps_in_file .or. ipos==istep) then
195 call read_cactus_hdf5_data(cstring(datfile),istep,ntoti,timetemp,dx,ignoretl,ierr)
196 call set_labels_cactus_hdf5
197 ! set smoothing length and particle mass
198 !print*,' Setting h = ',dx, 'ndim = ',ndim,' in column ',ih,' step ',i
199 if (ih > 0) dat(:,ih,i) = real(dx)
200 vol = dx**ndim
201 if (ipmass > 0 .and. irho > 0) dat(:,ipmass,i) = dat(:,irho,i)*real(vol)
202 !
203 ! compute extra quantities (tr K, 3^R, etc)
204 !
205 call compute_extra_columns(ncolstep,nextra,dat(:,:,i))
206 !
207 ! get number of cells of each type (normal, ghost)
208 !
209 call count_types(ntoti,iamtype(:,i),npartoftype(:,i),nunknown)
210 masstype(:,i) = 0. ! all masses read from file
211 time(i) = real(timetemp)
212 i = i + 1
213 endif
214
215 nstepsread = nstepsread + 1
216
217 endif got_particles
218 !
219 !--now memory has been allocated, set arrays which are constant for all time
220 !
221 gamma = 5./3.
222 !
223 !--set flag to indicate that only part of this file has been read
224 !
225 ipartialread = .false.
226 !
227 !--call set labels to identify location of smoothing length
228 !
229 call set_labels_cactus_hdf5
230
231 enddo over_snapshots
232
233 if (nstepsread > 0) then
234 print "(a,i10,a)",' >> read ',sum(npartoftype(:,istepstart)),' cells'
235 endif
236
237 !if (ipos==nstep_max) then
238 ! call close_cactus_hdf5_file(ierr)
239 ! file_is_open = .false.
240 !endif
241
242 end subroutine read_data_cactus_hdf5
243
244 subroutine read_cactus_hdf5_data_fromc(icol,ntot,np,temparr) bind(c)
245 use, intrinsic :: iso_c_binding, only:c_int,c_double
246 use particle_data, only:dat
247 use settings_data, only:debugmode
248 use labels, only:label
249 integer(kind=c_int), intent(in) :: icol,ntot,np
250 real(kind=c_double), intent(in) :: temparr(np)
251 integer(kind=c_int) :: icolput
252 integer :: nmax,i1,i2
253
254 icolput = icol
255 i1 = ntot-np+1
256 i2 = ntot
257
258 if (debugmode) print "(a,i2,a,i8,a,i8)",&
259 'DEBUG: reading column ',icol,' -> '//trim(label(icolput))//' parts ',i1,' to ',i2
260
261 ! check column is within array limits
262 if (icolput > size(dat(1,:,1)) .or. icolput==0) then
263 print "(a,i2,a)",' ERROR: column = ',icolput,' out of range in receive_data_fromc'
264 return
265 endif
266 if (i2 > size(dat(:,1,1))) then
267 print*,' ERROR with index range: ',i1,':',i2,' exceeds size ',size(dat(:,1,1)),' for column ',icol
268 read*
269 return
270 endif
271 ! ensure no array overflows
272 nmax = min(i2,size(dat(:,1,1)))
273
274 ! copy data into main splash array
275 dat(i1:i2,icolput,1) = real(temparr(1:np))
276
277 return
278 end subroutine read_cactus_hdf5_data_fromc
279
280 subroutine read_cactus_itype_fromc(ntot,np,itype) bind(c)
281 use, intrinsic :: iso_c_binding, only:c_int
282 use particle_data, only:iamtype
283 use params, only:int1
284 integer(kind=c_int), intent(in) :: ntot,np
285 integer(kind=c_int), intent(in) :: itype(np)
286 integer :: i1,i2,len_type
287
288 i1 = ntot-np+1
289 i2 = ntot
290 ! set particle type
291 len_type = size(iamtype(:,1))
292 if (len_type > 1) then
293 if (i2 > len_type) then
294 print*,'error with itype length',i2,len_type
295 return
296 endif
297 iamtype(i1:i2,1) = int(itype(1:np),kind=int1)
298 endif
299
300 return
301 end subroutine read_cactus_itype_fromc
302
303 !!------------------------------------------------------------
304 !! set labels for each column of data
305 !!------------------------------------------------------------
306
307 subroutine set_labels_cactus_hdf5
308 use labels, only:label,iamvec,labelvec,labeltype,ix,ivx,ipmass,iutherm,ih,irho
309 use params
310 use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings
311 use geometry, only:labelcoord
312 use system_utils, only:envlist,ienvironment
313 use cactushdf5read, only:blocklabel
314 use asciiutils, only:lcase
315 integer :: i,icol
316
317 if (ndim <= 0 .or. ndim > 3) then
318 print*,'*** ERROR: ndim = ',ndim,' in set_labels_cactus_hdf5 ***'
319 return
320 endif
321 if (ndimV <= 0 .or. ndimV > 3) then
322 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_cactus_hdf5 ***'
323 return
324 endif
325 blocklabel(1:5) = (/'x ','y ','z ','dx','m '/)
326
327 ix(1) = 1
328 ix(2) = 2
329 ix(3) = 3
330 ih = 4
331 ipmass = 5
332 iutherm = 0
333 irho = 0
334 do icol=1,size(blocklabel)
335 select case(trim(blocklabel(icol)))
336 case('vel[0]')
337 ivx = icol
338 case('dens','density')
339 if (irho==0) irho = icol
340 case('rho')
341 irho = icol
342 end select
343 label(icol) = trim(blocklabel(icol))
344 enddo
345
346 ! set labels of the quantities read in
347 if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1)
348 !if (irho > 0) label(irho) = 'density'
349 !if (iutherm > 0) label(iutherm) = 'u'
350 !if (ipmass > 0) label(ipmass) = 'particle mass'
351 !if (ih > 0) label(ih) = 'h'
352
353 ! set labels for vector quantities
354 if (ivx > 0) then
355 iamvec(ivx:ivx+ndimV-1) = ivx
356 labelvec(ivx:ivx+ndimV-1) = 'v'
357 do i=1,ndimV
358 label(ivx+i-1) = trim(labelvec(ivx))//'_'//labelcoord(i,1)
359 enddo
360 endif
361
362 ! set labels for each particle type
363 ntypes = 2
364 labeltype(1) = 'gas'
365 labeltype(2) = 'ghost'
366 UseTypeInRenderings(:) = .true.
367 UseTypeInRenderings(2) = .false.
368
369
370 !-----------------------------------------------------------
371 return
372 end subroutine set_labels_cactus_hdf5
373 end module readdata_cactus_hdf5