Debian Codegraph @

src/read_data_sphysics.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-2013 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 ! the data is stored in the global array dat
28 !
29 ! THIS VERSION FOR THE DUAL SPHYSICS CODE
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 ! maxplot,maxpart,maxstep : dimensions of main data array
38 ! dat(maxplot,maxpart,maxstep) : main data array
39 !
40 ! npartoftype(maxstep) : number of particles of each type 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 !--local module to store header information so we can later set the labels
49 module sphysicsdata
50 use params
51 use labels, only:lenlabel
52 implicit none
53
54 end module sphysicsdata
55
56 module readdata_sphysics
57 implicit none
58
59 public :: read_data_sphysics, set_labels_sphysics
60
61 private
62 contains
63
64 subroutine read_data_sphysics(rootname,indexstart,ipos,nstepsread)
65 use particle_data, only:npartoftype,masstype,time,gamma,dat,maxpart,maxstep,maxcol,iamtype
66 use params
67 use filenames, only:nfiles
68 use settings_data, only:ndim,ndimV,ncolumns,ncalc, &
69 buffer_data,iverbose,debugmode,ntypes
70 use mem_allocation, only:alloc
71 use labels, only:ipr,ivx,ih,irho,labeltype
72 integer, intent(in) :: indexstart,ipos
73 integer, intent(out) :: nstepsread
74 character(len=*), intent(in) :: rootname
75 character(len=len(rootname)+4) :: datfile
76 integer :: i,ierr,iunit,j,iblock
77 integer :: npart_max,nstep_max
78
79 integer, dimension(:), allocatable :: itype
80 character(len=20) :: geomfile
81 character(len=7) :: keyword
82 character(len=70) :: title
83 character(len=1) :: dumchar
84 character(len=16) :: unitsys
85 integer :: istep,jtype,np,ione,kk,idum,nblock
86 real(kind=sing_prec) :: version,timesingle,dum
87 real(kind=doub_prec) :: versiond,timedbl,dumd
88 real :: timein,dx,dy
89 logical :: singleprecision
90
91 iunit = 11 ! file unit number
92 nstepsread = 0
93 if (rootname(1:1) /= ' ') then
94 datfile = trim(rootname)
95 else
96 print*,' **** no data read **** '
97 return
98 endif
99
100 if (iverbose >= 1) print "(1x,a)",'reading Dual SPHysics format'
101 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
102
103 ndim = 2
104 ndimV = 2
105 !
106 !--open data file and read data
107 !
108 open(unit=iunit,iostat=ierr,file=datfile,status='old',form='unformatted',access='stream')
109 if (ierr /= 0) then
110 print*,' *** Error opening '//trim(datfile)//' ***'
111 return
112 endif
113 !
114 !--read first header line
115 !
116 read(iunit,iostat=ierr,end=80) keyword
117 read(iunit,iostat=ierr,end=80) version
118 read(iunit,iostat=ierr,end=80) title
119 read(iunit,iostat=ierr,end=80) istep
120 read(iunit,iostat=ierr,end=80) timesingle
121 timein = timesingle
122 read(iunit,iostat=ierr,end=80) np
123 read(iunit,iostat=ierr,end=80) ione
124 if (ierr /= 0 .or. np <= 0 .or. timesingle < 0.) then
125 !
126 !--try single precision
127 !
128 rewind(iunit)
129 read(iunit,iostat=ierr,end=80) keyword
130 read(iunit,iostat=ierr,end=80) versiond
131 version = versiond
132 read(iunit,iostat=ierr,end=80) title
133 read(iunit,iostat=ierr,end=80) istep
134 read(iunit,iostat=ierr,end=80) timedbl
135 timein = timedbl
136 read(iunit,iostat=ierr,end=80) np
137 read(iunit,iostat=ierr,end=80) ione
138 singleprecision = .false.
139 if (ierr /= 0 .or. np < 0 .or. timedbl < 0.) then
140 print "(a)",' *** Error reading first header ***'
141 close(iunit)
142 return
143 endif
144 endif
145 print "(a,f4.2)",' keyword = '//trim(keyword)//' version = ',version
146 !print "(a)",' title = '//trim(title)
147 print "(a,i6,a,es10.3,a,i6)",' step = ',istep,' time = ',timein,' np = ',np
148 !
149 !--allocate memory for data arrays
150 !
151 if (buffer_data) then
152 nstep_max = max(nfiles,maxstep,indexstart)
153 else
154 nstep_max = max(1,maxstep,indexstart)
155 endif
156 npart_max = max(int(1.1*np),maxpart)
157 ncolumns = 7
158 if (.not.allocated(dat) .or. npart_max > maxpart &
159 .or. nstep_max > maxstep .or. ncolumns+ncalc > maxcol) then
160 call alloc(npart_max,nstep_max,ncolumns+ncalc,mixedtypes=.true.)
161 endif
162
163 i = indexstart
164 nstepsread = 0
165 time(i) = timein
166 gamma(i) = 5./3.
167 npartoftype(1,i) = np
168
169 do j=1,np
170 read(iunit,iostat=ierr,end=67) idum,(dat(j,kk,i),kk=1,2),dum
171 enddo
172 read(iunit,iostat=ierr,end=67) nblock
173 read(iunit,iostat=ierr,end=67) (idum,j=1,nblock)
174
175 ntypes = 4
176 allocate(itype(np))
177 read(iunit,iostat=ierr,end=67) (itype(j),j=1,nblock)
178 npartoftype(:,i) = 0
179 do j=1,np
180 jtype = itype(j)
181 !--map types from code types to splash types
182 select case(jtype)
183 case(1)
184 jtype = 2 ! boundary
185 case(2)
186 jtype = 1 ! water
187 case default ! unknown
188 jtype = 4
189 end select
190 iamtype(j,i) = jtype
191 npartoftype(jtype,i) = npartoftype(jtype,i) + 1
192 enddo
193 deallocate(itype)
194 read(iunit,iostat=ierr,end=67) (dumchar,j=1,nblock)
195 read(iunit,iostat=ierr,end=67) (idum,j=1,nblock)
196
197 call set_labels_sphysics
198 do iblock=1,1
199
200 read(iunit,iostat=ierr,end=67) nblock
201 read(iunit,iostat=ierr,end=67) idum
202
203 do j=1,nblock
204 read(iunit,iostat=ierr,end=67) compName(j),unitsys,idum,idum,dum
205 !print*,trim(compName(j))
206 enddo
207
208 do j=1,np
209 read(iunit,iostat=ierr,end=67) dum,dat(j,ipr,i),dat(j,ivx,i),dat(j,ivx+1,i),dat(j,1,i),dat(j,2,i)
210 if (abs(dum) < tiny(0.)) then
211 npartoftype(iamtype(j,i),i) = npartoftype(iamtype(j,i),i) - 1 ! remove from previous type
212 iamtype(j,i) = 3
213 npartoftype(3,i) = npartoftype(3,i) + 1 ! add to "box" type
214 endif
215 enddo
216 enddo
217 !
218 !--fake other properties: density, mass, smoothing length etc.
219 !
220 masstype(1,i) = 1./npartoftype(1,i)
221 !
222 !--assume smoothing length to be the max dimension divided by the number of particles^(1/ndim)
223 !
224 dx = maxval(dat(1:np,1,i)) - minval(dat(1:np,1,i))
225 dy = maxval(dat(1:np,2,i)) - minval(dat(1:np,2,i))
226 dat(:,ih,i) = dx/(npartoftype(1,i))**(1./ndim)*(dy/dx)
227 print*,' WARNING: ASSUMING SMOOTHING LENGTH = ',dat(1,ih,i),' AND ARBITRARY PARTICLE MASSES'
228 dat(:,irho,i) = 1.
229 nstepsread = 1
230 do jtype=1,ntypes
231 write(*,"(' n(',a,') = ',i6)",advance="no") trim(labeltype(jtype)),npartoftype(jtype,i)
232 enddo
233 write(*,*)
234 !read*
235
236
237 goto 68
238 67 continue
239 print "(a)",' > end of file reached <'
240 68 continue
241 !
242 !--close data file and return
243 !
244 close(unit=11)
245
246 if (debugmode) print*,'DEBUG> Read steps ',indexstart,'->',indexstart + nstepsread - 1, &
247 ' last step ntot = ',sum(npartoftype(:,indexstart+nstepsread-1))
248 return
249
250 80 continue
251 print*,' *** data file empty : no timesteps ***'
252 return
253
254 end subroutine read_data_sphysics
255
256 !!------------------------------------------------------------
257 !! set labels for each column of data
258 !!------------------------------------------------------------
259
260 subroutine set_labels_sphysics
261 use labels, only:ix,ivx,ih,irho,ipr,&
262 iamvec,labelvec,label,labeltype
263 use params
264 use settings_data, only:ndim,ndimV,UseTypeInRenderings
265 use geometry, only:labelcoord
266 integer :: i
267
268 if (ndim <= 0 .or. ndim > 3) then
269 print*,'*** ERROR: ndim = ',ndim,' in set_labels_sphysics ***'
270 return
271 endif
272 if (ndimV <= 0 .or. ndimV > 3) then
273 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_sphysics ***'
274 return
275 endif
276
277 do i=1,ndim
278 ix(i) = i
279 enddo
280 ivx = ndim + 1
281 ipr = ndim + ndimV + 1
282 irho = ipr + 1
283 ih = irho + 1
284 label(ipr) = 'pressure'
285 label(irho) = 'density'
286 label(ih) = 'h'
287
288 label(ix(1:ndim)) = labelcoord(1:ndim,1)
289 !
290 !--label vector quantities (e.g. velocity) appropriately
291 !
292 iamvec(ivx:ivx+ndimV-1) = ivx
293 labelvec(ivx:ivx+ndimV-1) = 'v'
294
295 !
296 !--set labels for each type of particles
297 !
298 labeltype(1) = 'water'
299 labeltype(2) = 'boundary'
300 labeltype(3) = 'box'
301 labeltype(4) = 'unknown'
302 UseTypeInRenderings(1) = .true.
303 UseTypeInRenderings(2) = .false.
304 UseTypeInRenderings(3) = .false.
305 UseTypeInRenderings(4) = .false.
306
307 !-----------------------------------------------------------
308
309 return
310 end subroutine set_labels_sphysics
311 end module readdata_sphysics