Debian Codegraph @

src/read_data_amuse_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-2014 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 AMUSE FRAMEWORK
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 amusehdf5read
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 real :: hsoft
61 character(len=lenlabel), dimension(maxplot) :: blocklabel
62 logical :: havewarned = .false.
63 integer, parameter :: maxtypes = 6
64
65 interface
66 subroutine read_amuse_hdf5_header(filename,npart,ncol,ndim,ndimV,time,ierr) bind(c)
67 import
68 character(kind=c_char), dimension(*), intent(in) :: filename
69 integer(kind=c_int), intent(out) :: npart,ncol,ndim,ndimV,ierr
70 real(kind=c_double), intent(out) :: time
71 end subroutine read_amuse_hdf5_header
72
73 subroutine read_amuse_hdf5_data(filename,maxtypes,npartoftypei,&
74 ncol,isrequired,ierr) bind(c)
75 import
76 implicit none
77 character(kind=c_char), dimension(*), intent(in) :: filename
78 integer(kind=c_int), intent(in), value :: maxtypes
79 integer(kind=c_int), dimension(6), intent(in) :: npartoftypei
80 integer(kind=c_int), intent(in), value :: ncol
81 integer(kind=c_int), intent(out) :: ierr
82 integer(kind=c_int), dimension(ncol), intent(in) :: isrequired
83 end subroutine read_amuse_hdf5_data
84 end interface
85
86 end module amusehdf5read
87
88 !-------------------------------------------------------------------------
89 !
90 ! The routine that reads the data into splash's internal arrays
91 !
92 !-------------------------------------------------------------------------
93
94 module readdata_amuse_hdf5
95 implicit none
96
97 public :: read_data_amuse_hdf5, set_labels_amuse_hdf5
98
99 private
100 contains
101
102 subroutine read_data_amuse_hdf5(rootname,istepstart,ipos,nstepsread)
103 use particle_data, only:dat,npartoftype,masstype,time,gamma,maxpart,maxcol,maxstep
104 use params, only:doub_prec,maxplot
105 use settings_data, only:ndim,ndimV,ncolumns,ncalc,required,ipartialread, &
106 ntypes,iverbose,debugmode
107 use settings_page, only:legendtext
108 use mem_allocation, only:alloc
109 use labels, only:ih,irho,ipmass
110 use system_utils, only:renvironment,lenvironment,ienvironment,envlist
111 use asciiutils, only:cstring
112 use amusehdf5read, only:havewarned,read_amuse_hdf5_header,read_amuse_hdf5_data
113 integer, intent(in) :: istepstart,ipos
114 integer, intent(out) :: nstepsread
115 character(len=*), intent(in) :: rootname
116 character(len=len(rootname)+10) :: datfile
117 integer :: i,j,ierr
118 integer :: nhfac
119 integer :: ncolstep,npart_max,nstep_max,ntoti
120 logical :: iexist,reallocate,debug,goterrors
121 real(doub_prec) :: timetemp
122 real :: hfact,hfactmean,pmassi
123 real, parameter :: pi = 3.1415926536
124 integer, dimension(maxplot) :: isrequired
125
126 nstepsread = 0
127 goterrors = .false.
128 debug = debugmode
129
130 if (len_trim(rootname) > 0) then
131 datfile = trim(rootname)
132 else
133 print*,' **** no data read **** '
134 return
135 endif
136 !
137 !--check if first data file exists
138 !
139 print "(1x,a)",'reading AMUSE HDF5 format'
140 inquire(file=datfile,exist=iexist)
141 if (.not.iexist) then
142 !
143 !--append .hdf5 on the endif not already present
144 !
145 datfile=trim(rootname)//'.hdf5'
146 inquire(file=datfile,exist=iexist)
147 if (.not.iexist) then
148 print "(a)",' *** error: '//trim(rootname)//': file not found ***'
149 return
150 endif
151 endif
152 !
153 !--set parameters which do not vary between timesteps
154 !
155 ndim = 3
156 ndimV = 3
157 !
158 !--read data from snapshots
159 !
160 i = istepstart
161 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
162 !
163 !--open file and read header information
164 !
165 if (debug) print*,'DEBUG: reading header...'
166 call read_amuse_hdf5_header(cstring(datfile),ntoti,ncolstep,ndim,ndimV,timetemp,ierr)
167 if (ierr /= 0) then
168 print "(a)", '*** ERROR READING HEADER ***'
169 return
170 endif
171 ncolumns = ncolstep
172
173 if (iverbose >= 1) print "(2(a,1x,i10))",' npart: ',ntoti,' ncolumns: ',ncolstep
174 !
175 !--now read data
176 !
177 reallocate = .false.
178 npart_max = maxpart
179 nstep_max = max(maxstep,1)
180
181 if (ntoti > maxpart) then
182 reallocate = .true.
183 if (maxpart > 0) then
184 ! if we are reallocating, try not to do it again
185 npart_max = int(1.1*ntoti)
186 else
187 ! if first time, save on memory
188 npart_max = int(ntoti)
189 endif
190 endif
191 if (i >= maxstep .and. i /= 1) then
192 nstep_max = i + max(10,INT(0.1*nstep_max))
193 reallocate = .true.
194 endif
195 !
196 !--reallocate memory for main data array
197 !
198 if (reallocate .or. .not.(allocated(dat))) then
199 call alloc(npart_max,nstep_max,max(ncolumns+ncalc,maxcol))
200 endif
201
202 !
203 !--copy header data into allocated arrays
204 !
205 npartoftype(1,i) = ntoti
206 time(i) = real(timetemp)
207 masstype(:,i) = 0. ! all masses read from file
208 !
209 !--read particle data
210 !
211 got_particles: if (ntoti > 0) then
212
213 isrequired(:) = 0
214 where (required(1:ncolumns)) isrequired(1:ncolumns) = 1
215
216 call read_amuse_hdf5_data(cstring(datfile),ntypes,npartoftype(:,i),ncolumns,isrequired,ierr)
217
218 nstepsread = 1
219 endif got_particles
220 !
221 !--now memory has been allocated, set arrays which are constant for all time
222 !
223 gamma = 5./3.
224 !
225 !--set flag to indicate that only part of this file has been read
226 !
227 if (.not.all(required(1:ncolstep))) ipartialread = .true.
228 !
229 !--call set labels to identify location of smoothing length
230 !
231 call set_labels_amuse_hdf5
232 !
233 !--for read from multiple files, work out the next file in the sequence
234 !
235 !
236 !--for some reason the smoothing length output by GADGET is
237 ! twice the usual SPH smoothing length
238 ! (do this after we have read data from all of the files)
239 !
240 if (required(ih) .and. size(dat(1,:,:)) >= ih .and. npartoftype(1,i) > 0 .and. ih > 0) then
241 print "(a)",' converting GADGET smoothing length on gas particles to usual SPH definition (x 0.5)'
242 dat(1:npartoftype(1,i),ih,i) = 0.5*dat(1:npartoftype(1,i),ih,i)
243 endif
244
245 !
246 !--give a friendly warning about using too few or too many neighbours
247 ! (only works with equal mass particles because otherwise we need the number density estimate)
248 !
249 if (ih > 0 .and. required(ih) .and. ipmass > 0 .and. required(ipmass) &
250 .and. abs(masstype(1,i)) < tiny(0.) .and. ndim==3 .and. .not.havewarned) then
251 nhfac = 100
252 if (npartoftype(1,i) > nhfac) then
253 hfactmean = 0.
254 do j=1,nhfac
255 pmassi = dat(j,ipmass,i)
256 if (pmassi > 0.) then
257 pmassi = 1./pmassi
258 else
259 pmassi = 0.
260 endif
261 hfact = dat(j,ih,i)*(dat(j,irho,i)*pmassi)**(1./ndim)
262 hfactmean = hfactmean + hfact
263 enddo
264 hfact = hfactmean/real(nhfac)
265 havewarned = .true.
266 print "(/,1x,a,f5.1,a,/,1x,a,f4.2,a,i1,a,/)", &
267 'Simulations employ ',4./3.*pi*(2.*hfact)**3,' neighbours,', &
268 'corresponding to h = ',hfact,'*(m/rho)^(1/',ndim,') in 3D'
269 endif
270 endif
271 !
272 !--cover the special case where no particles have been read
273 !
274 if (ntoti <= 0) then
275 npartoftype(1,i) = 1
276 dat(:,:,i) = 0.
277 endif
278
279 if (nstepsread > 0) then
280 print "(a,i10,a)",' >> read ',sum(npartoftype(:,istepstart+nstepsread-1)),' particles'
281 endif
282 return
283
284 end subroutine read_data_amuse_hdf5
285
286 subroutine read_amuse_hdf5_data_fromc(icol,npartoftypei,temparr,itype) bind(c)
287 use, intrinsic :: iso_c_binding, only:c_int,c_double
288 use particle_data, only:dat,iamtype
289 use settings_data, only:debugmode
290 use labels, only:label
291 integer(kind=c_int), intent(in) :: icol,npartoftypei,itype
292 real(kind=c_double), intent(in) :: temparr(npartoftypei)
293 integer(kind=c_int) :: i,icolput
294 integer :: nmax
295
296 icolput = icol
297 if (debugmode) print "(a,i2,a,i2,a,i8)",'DEBUG: reading column ',icol,' type ',itype,' -> '//trim(label(icolput))
298
299 ! check column is within array limits
300 if (icolput > size(dat(1,:,1)) .or. icolput==0) then
301 print "(a,i2,a)",' ERROR: column = ',icolput,' out of range in receive_data_fromc'
302 return
303 endif
304
305 ! ensure no array overflows
306 nmax = min(npartoftypei,size(dat(:,1,1)))
307
308 ! copy data into main splash array
309 dat(1:nmax,icolput,1) = real(temparr(1:nmax))
310
311 ! set particle type
312 if (size(iamtype(:,1)) > 1) then
313 do i=1,nmax
314 iamtype(i,1) = int(itype + 1,kind=kind(iamtype))
315 enddo
316 endif
317
318 end subroutine read_amuse_hdf5_data_fromc
319
320 !!------------------------------------------------------------
321 !! set labels for each column of data
322 !!------------------------------------------------------------
323
324 subroutine set_labels_amuse_hdf5
325 use labels, only:label,iamvec,labelvec,labeltype,ix,ivx,ipmass, &
326 ih,irho,iutherm,iax,make_vector_label
327 use params
328 use settings_data, only:ndim,ndimV,UseTypeInRenderings
329 use geometry, only:labelcoord
330 use system_utils, only:envlist,ienvironment
331 use amusehdf5read, only:blocklabel
332 use asciiutils, only:lcase
333 integer :: icol
334
335 if (ndim <= 0 .or. ndim > 3) then
336 print*,'*** ERROR: ndim = ',ndim,' in set_labels_amuse_hdf5 ***'
337 return
338 endif
339 if (ndimV <= 0 .or. ndimV > 3) then
340 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_amuse_hdf5 ***'
341 return
342 endif
343
344 ix = 0
345 iutherm = 0
346 do icol=1,size(blocklabel)
347 select case(trim(lcase(blocklabel(icol))))
348 case('x')
349 ix(1) = icol
350 case('y')
351 ix(2) = icol
352 case('z')
353 ix(3) = icol
354 case('vx')
355 ivx = icol
356 case('ax')
357 iax = icol
358 case('h_smooth')
359 ih = icol
360 case('mass')
361 ipmass = icol
362 case('density')
363 irho = icol
364 end select
365 label(icol) = trim(blocklabel(icol))
366 enddo
367
368 ! set labels of the quantities read in
369 if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1)
370
371 ! set labels for vector quantities
372 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
373 call make_vector_label('a',iax,ndimV,iamvec,labelvec,label,labelcoord(:,1))
374
375 ! set labels for each particle type
376 labeltype(1) = 'gas'
377 UseTypeInRenderings(:) = .false.
378 UseTypeInRenderings(1) = .true.
379
380 !-----------------------------------------------------------
381 return
382 end subroutine set_labels_amuse_hdf5
383
384 subroutine set_blocklabel(icol,name) bind(c)
385 use, intrinsic :: iso_c_binding, only:c_int, c_char
386 use amusehdf5read, only:blocklabel
387 use asciiutils, only:fstring
388 integer(kind=c_int), intent(in) :: icol
389 character(kind=c_char), intent(in) :: name(256)
390
391 blocklabel(icol) = trim(fstring(name))
392 !print*,icol,' name = ',trim(blocklabel(icol))
393
394 end subroutine set_blocklabel
395 end module readdata_amuse_hdf5