Debian Codegraph @

src/read_data_flash_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-2009 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 FLASH HDF5 TRACER PARTICLES
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(maxplot,maxpart,maxstep) : main data array
38 !
39 ! npartoftype(1:6,maxstep) : number of particles of each type in each timestep
40 ! ntot(maxstep) : total number of particles in each timestep
41 !
42 ! time(maxstep) : time at each step
43 ! gamma(maxstep) : gamma at each step
44 !
45 ! most of these values are stored in global arrays
46 ! in the module 'particle_data'
47 !-------------------------------------------------------------------------
48 module flash_hdf5read
49 use, intrinsic :: iso_c_binding, only:c_int,c_float,c_char
50 !interface to the c versions
51 interface
52 subroutine read_flash_hdf5_header(filename,time,npart,ncol,ierr) bind(c)
53 import
54 character(kind=c_char,len=1), intent(in) :: filename
55 real(kind=c_float), intent(out) :: time
56 integer(kind=c_int), intent(out) :: npart,ncol,ierr
57 end subroutine read_flash_hdf5_header
58
59 subroutine read_flash_hdf5_data(filename,npart,ncol,isrequired,ierr) bind(c)
60 import
61 character(kind=c_char,len=1), intent(in) :: filename
62 integer(kind=c_int), intent(in) :: npart,ncol
63 integer(kind=c_int), dimension(ncol), intent(in) :: isrequired
64 integer(kind=c_int), intent(out) :: ierr
65 end subroutine read_flash_hdf5_data
66 end interface
67
68 contains
69
70 !
71 ! function which maps from the order in which columns
72 ! are read from the HDF5 file to the order in which they
73 ! are stored in SPLASH. Differs because there are a couple
74 ! of useless arrays that we do not read/store (ie. first column
75 ! is on/off tag, 5th column is particle ID which we use to order
76 ! the particles)
77 !
78 integer function icolshuffle(icol)
79 integer, intent(in) :: icol
80
81 select case(icol)
82 case(1)
83 icolshuffle = 4
84 case(2,3,4)
85 icolshuffle = icol - 1
86 case(5)
87 icolshuffle = 0
88 case default
89 icolshuffle = icol
90 end select
91
92 end function icolshuffle
93
94 end module flash_hdf5read
95
96 !-------------------------------------------------------------------------
97 !
98 ! The routine that reads the data into splash's internal arrays
99 !
100 !-------------------------------------------------------------------------
101
102 module readdata_flash_hdf5
103 implicit none
104
105 public :: read_data_flash_hdf5, set_labels_flash_hdf5
106
107 private
108 contains
109
110 subroutine read_data_flash_hdf5(dumpfile,indexstart,ipos,nstepsread)
111 use particle_data, only:dat,npartoftype,masstype,time,gamma,maxpart,maxcol
112 use params
113 use settings_data, only:ndim,ndimV,ncolumns,ncalc,required,ipartialread,lowmemorymode
114 use mem_allocation, only:alloc
115 use flash_hdf5read
116 use asciiutils, only:cstring
117 use labels, only:ih,irho
118 use system_utils, only:renvironment
119 integer, intent(in) :: indexstart,ipos
120 integer, intent(out) :: nstepsread
121 character(len=*), intent(in) :: dumpfile
122
123 integer :: i,j,ncolstep,ilastrequired
124 integer :: nprint,npart_max,nstep_max,ierr
125 integer, dimension(0:maxplot) :: isrequired
126 logical :: iexist
127 real(c_float) :: tread
128 real :: hfact,totmass
129
130 nstepsread = 0
131 nstep_max = 0
132 npart_max = maxpart
133 !
134 !--check if first data file exists
135 !
136 inquire(file=dumpfile,exist=iexist)
137 if (.not.iexist) then
138 print "(a)",' *** error: '//trim(dumpfile)//': file not found ***'
139 return
140 endif
141 !
142 !--fix number of spatial dimensions (0 means no particle coords)
143 !
144 ndim = 3
145 ndimV = 3
146
147 j = indexstart
148 nstepsread = 0
149 print "(a)",' reading FLASH tracer particles (HDF5) data format '
150
151 write(*,"(26('>'),1x,a,1x,26('<'))") trim(dumpfile)
152
153 call read_flash_hdf5_header(cstring(dumpfile),tread,nprint,ncolstep,ierr)
154 ncolstep = ncolstep - 1 ! subtract particle ID column
155 print "(a,i10,a,es10.3,a,i2)",' npart = ',nprint,' time = ',tread
156
157 call set_labels_flash_hdf5
158 if (ih > 0 .and. required(ih)) required(irho) = .true.
159 !
160 !--(re)allocate memory
161 !
162 nstep_max = max(nstep_max,indexstart,1)
163 if (.not.allocated(dat) .or. (nprint > maxpart) .or. (ncolstep+ncalc) > maxcol) then
164 npart_max = max(npart_max,nprint,maxpart)
165 if (lowmemorymode) then
166 ilastrequired = 0
167 do i=1,ncolstep+ncalc
168 if (required(i)) ilastrequired = i
169 enddo
170 call alloc(npart_max,j,ilastrequired)
171 else
172 call alloc(npart_max,nstep_max,max(ncolstep+ncalc,maxcol))
173 endif
174 endif
175 !
176 !--set the necessary parameters
177 !
178 ncolumns = ncolstep
179 nstepsread = nstepsread + 1
180 npartoftype(:,j) = 0
181 npartoftype(1,j) = nprint
182
183 totmass = renvironment('FSPLASH_TOTMASS',-1.0)
184 if (totmass > 0.) then
185 print "(a,1pe10.3)",' setting total mass for all particles using FSPLASH_TOTMASS=',totmass
186 else
187 print "(a)",' FSPLASH_TOTMASS not set, assuming total mass of all particles is 1.0'
188 totmass = 1.0
189 endif
190 masstype(1,j) = totmass/real(nprint)
191 time(j) = tread
192 gamma(j) = 5./3.
193 !
194 !--map "required" array to integers
195 ! also remap to the order read from the c data read
196 !
197 isrequired(:) = 0
198 do i=1,ncolstep
199 if (icolshuffle(i) /= 0 .and. required(icolshuffle(i))) then
200 !print*,'required '//trim(label(icolshuffle(i)))//' so must read ',i
201 isrequired(i) = 1
202 endif
203 enddo
204
205 if (.not.all(required(1:ncolstep))) then
206 ipartialread = .true.
207 else
208 ipartialread = .false.
209 endif
210 !
211 !--now read the timestep data in the dumpfile
212 ! (to avoid Fortran calling C with the array, we don't actually
213 ! pass the dat array here - instead we get c to
214 ! "call back" to fill the dat array, below)
215 !
216 call read_flash_hdf5_data(cstring(dumpfile),nprint,ncolstep+1,isrequired(1:ncolstep+1),ierr)
217
218 if (required(ih)) then
219 hfact = 1.2
220 hfact = renvironment('FSPLASH_HFACT',1.2)
221 print "(a,i2,a,f5.2,a)",' creating smoothing length in column ',ih,' using h =',hfact,'(m/rho)^(1/3)'
222 dat(1:nprint,ih,j) = hfact*(masstype(1,j)/dat(1:nprint,irho,j))**(1./3.)
223 endif
224
225 return
226 end subroutine read_data_flash_hdf5
227
228 subroutine read_flash_data_fromc(icol,npart,temparr,id) bind(c)
229 use, intrinsic :: iso_c_binding, only:c_int,c_double
230 use particle_data, only:dat
231 use flash_hdf5read, only:icolshuffle
232 use labels, only:label
233 integer(kind=c_int), intent(in) :: icol,npart
234 real(kind=c_double), dimension(npart), intent(in) :: temparr
235 integer(kind=c_int), dimension(npart), intent(in) :: id
236 integer(kind=c_int) :: i,icolput
237
238 icolput = icolshuffle(icol)
239 if (icolput > size(dat(1,:,1)) .or. icolput==0) then
240 print "(a,i2,a)",' ERROR: column = ',icolput,' out of range in receive_data_fromc'
241 return
242 endif
243 print "(a,i2,a)",' reading column ',icol,' -> '//trim(label(icolput))
244
245 do i=1,npart
246 if (id(i) < 1 .or. id(i) > npart) then
247 print*,' ERROR in particle id = ',id(i)
248 else
249 dat(id(i),icolput,1) = real(temparr(i))
250 endif
251 enddo
252
253 return
254 end subroutine read_flash_data_fromc
255
256 !!-------------------------------------------------------------------
257 !! set labels for each column of data
258 !!
259 !! read these from a file called 'columns' in the current directory
260 !! then take sensible guesses as to which quantities are which
261 !! from the column labels
262 !!
263 !!-------------------------------------------------------------------
264
265 subroutine set_labels_flash_hdf5
266 use labels, only:label,labeltype,ix,irho,ipmass,ih,ivx,iamvec,labelvec
267 use params
268 use settings_data, only:ntypes,ndim,ndimV,UseTypeInRenderings,ncolumns
269 use geometry, only:labelcoord
270 integer :: i
271
272 do i=1,ndim
273 ix(i) = i
274 label(i) = labelcoord(i,1)
275 enddo
276 ih = 5
277 ivx = 6
278 ipmass = 0
279 label(4) = 'density (from grid)'
280 label(ih) = 'smoothing length'
281 irho = 4
282 if (ncolumns >= 9) then
283 irho = 9
284 label(9) = 'density (on particles)'
285 else
286 irho = 4
287 endif
288
289 if (ivx > 0) then
290 iamvec(ivx:ivx+ndimV-1) = ivx
291 labelvec(ivx:ivx+ndimV-1) = 'v'
292 do i=1,ndimV
293 label(ivx+i-1) = 'v\d'//labelcoord(i,1)
294 enddo
295 endif
296 !
297 !--set labels for each particle type
298 !
299 ntypes = 1
300 labeltype(1) = 'tracer'
301 UseTypeInRenderings(1) = .true.
302
303 end subroutine set_labels_flash_hdf5
304 end module readdata_flash_hdf5