Debian Codegraph @

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