Debian Codegraph @

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