Debian Codegraph @

src/read_data_oilonwater.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 READING UNFORMATTED OUTPUT FROM MATTHEW BATE'S CODE
28 ! (ie. STRAIGHT FROM THE DATA DUMP)
29 !
30 ! *** CONVERTS TO SINGLE PRECISION ***
31 !
32 ! the data is stored in the global array dat
33 !
34 ! >> this subroutine must return values for the following: <<
35 !
36 ! ncolumns : number of data columns
37 ! ndim, ndimV : number of spatial, velocity dimensions
38 ! nstepsread : number of steps read from this file
39 !
40 ! maxplot,maxpart,maxstep : dimensions of main data array
41 ! dat(maxplot,maxpart,maxstep) : main data array
42 !
43 ! npartoftype(1:6,maxstep) : number of particles of each type in each timestep
44 !
45 ! time(maxstep) : time at each step
46 ! gamma(maxstep) : gamma at each step
47 !
48 ! most of these values are stored in global arrays
49 ! in the module 'particle_data'
50 !-------------------------------------------------------------------------
51
52 !--short module storing units information
53 module oilonwaterread
54 use params
55 implicit none
56 real(doub_prec) :: udisti,umassi,utimei
57
58 end module oilonwaterread
59
60
61 module readdata_oilonwater
62 implicit none
63
64 public :: read_data_oilonwater, set_labels_oilonwater
65
66 private
67 contains
68
69 subroutine read_data_oilonwater(rootname,indexstart,ipos,nstepsread)
70 use particle_data
71 use params
72 use settings_data, only:ndim,ndimV,ncolumns,ncalc
73 use mem_allocation
74 use labels, only:ih,ipmass
75 use oilonwaterread, only:udisti,umassi,utimei
76 integer, intent(in) :: indexstart,ipos
77 integer, intent(out) :: nstepsread
78 character(len=*), intent(in) :: rootname
79 integer :: i,j,k,ifile,ierr,ipart
80 integer :: npart_max,nstep_max,ncolstep,npart,nunknown
81 logical :: iexist,doubleprec
82
83 character(len=len(rootname)+10) :: dumpfile
84 integer :: nprint, n1, n2, nptmass, nstepsalloc
85 integer :: npartoil, npartwater
86 integer, dimension(:), allocatable :: isteps, iphase
87
88 !--use these lines if dump is double precision
89 real(doub_prec), dimension(:,:), allocatable :: dattemp
90 real(doub_prec) :: timei, gammai
91 real(doub_prec) :: rhozero, RK2
92 real(doub_prec) :: escap,tkin,tgrav,tterm
93 real(doub_prec) :: dtmax
94
95 !--use these lines for single precision
96 real :: timesi, gammasi
97 real :: rhozeros, RK2s
98 real :: escaps,tkins,tgravs,tterms
99 real :: dtmaxs
100
101 nstepsread = 0
102 nstep_max = 0
103 npart_max = maxpart
104 ifile = 1
105
106 dumpfile = trim(rootname)
107 !
108 !--check if data file exists
109 !
110 inquire(file=dumpfile,exist=iexist)
111 if (.not.iexist) then
112 print "(a)",' *** error: '//trim(dumpfile)//': file not found ***'
113 return
114 endif
115 !
116 !--fix number of spatial dimensions
117 !
118 ndim = 3
119 ndimV = 3
120 ncolstep = 18 ! number of columns in file
121 ncolumns = ncolstep
122 !
123 !--allocate memory initially
124 !
125 nstep_max = max(nstep_max,indexstart,1)
126
127 j = indexstart
128 nstepsread = 0
129 doubleprec = .false.
130
131 print "(1x,a)",'reading oil-on-water code format'
132 write(*,"(26('>'),1x,a,1x,26('<'))") trim(dumpfile)
133 !
134 !--open the (unformatted) binary file and read the number of particles
135 !
136 open(unit=15,iostat=ierr,file=dumpfile,status='old',form='unformatted')
137 if (ierr /= 0) then
138 print "(a)",'*** ERROR OPENING '//trim(dumpfile)//' ***'
139 else
140 !
141 !--read the number of particles in the first step,
142 ! allocate memory and rewind
143 !
144 read(15,end=55,iostat=ierr) udisti,umassi,utimei,nprint,n1,n2,timei,gammai,rhozero,RK2
145 print*,'npart = ',nprint
146 if (.not.allocated(dat) .or. nprint > npart_max) then
147 npart_max = max(npart_max,INT(1.1*nprint))
148 call alloc(npart_max,nstep_max,ncolstep+ncalc,mixedtypes=.true.)
149 endif
150 doubleprec = .true.
151 !--try single precision if non-sensible values for time, gamma etc.
152 if (ierr /= 0 .or. timei < 0. .or. timei > 1e30 &
153 .or. gammai < 1. .or. gammai > 10. &
154 .or. rhozero < 0. .or. RK2 < 0. .or. RK2 > 1.e10) then
155 doubleprec = .false.
156 endif
157 rewind(15)
158 endif
159
160 call set_labels_oilonwater
161
162 if (ierr /= 0) then
163 print "(a)",'*** ERROR READING TIMESTEP HEADER ***'
164 else
165 !
166 !--loop over the timesteps in this file
167 !
168 over_steps_in_file: do
169 npart_max = max(npart_max,nprint)
170 !
171 !--allocate/reallocate memory if j > maxstep
172 !
173 if (j > maxstep) then
174 if (nstepsread >= 2) then
175 nstepsalloc = 2*nstepsread
176 else
177 nstepsalloc = j
178 endif
179 call alloc(maxpart,nstepsalloc,maxcol,mixedtypes=.true.)
180 endif
181 !
182 !--allocate integer arrays required for data read
183 !
184 if (allocated(isteps)) deallocate(isteps)
185 allocate(isteps(npart_max),stat=ierr)
186 if (ierr /= 0) print*,'not enough memory in read_data_oilonwater'
187
188 if (allocated(iphase)) deallocate(iphase)
189 allocate(iphase(npart_max),stat=ierr)
190 if (ierr /= 0) print*,'not enough memory in read_data_oilonwater'
191 !
192 !--now read the timestep data in the dumpfile
193 !
194 write(*,"(a,i5,a)",advance="no") '| step ',j,': '
195
196 if (doubleprec) then
197 print "(a)",'double precision dump'
198 !
199 !--allocate a temporary array for (double precision) variables
200 !
201 if (allocated(dattemp)) deallocate(dattemp)
202 allocate(dattemp(npart_max,ncolstep),stat=ierr)
203 if (ierr /= 0) print*,'not enough memory in read_data_oilonwater'
204
205 read(15,end=55,iostat=ierr) udisti, umassi, utimei, &
206 nprint, n1, n2, timei, gammai, rhozero, RK2, &
207 (dattemp(i,ih), i=1, nprint),escap, tkin, tgrav, tterm, &
208 ((dattemp(i,k), i=1, nprint),k=1,ih-1), &
209 ((dattemp(i,k), i=1, nprint),k=ih+1,11), &
210 dtmax, &
211 (isteps(i), i=1,nprint), &
212 ((dattemp(i,k), i=1, nprint),k=12,18), &
213 nptmass, &
214 ((dattemp(i,k), i=nprint+1,nprint+nptmass),k=1,6), &
215 (dattemp(i,ipmass), i=nprint+1,nprint+nptmass), &
216 (dattemp(i,ih), i=nprint+1,nprint+nptmass), &
217 (isteps(i), i=nprint+1,nprint+nptmass), &
218 (iphase(i), i=1,nprint)
219 else
220 print "(a)",'single precision dump'
221 read(15,end=55,iostat=ierr) udisti, umassi, utimei, &
222 nprint, n1, n2, timesi, gammasi, rhozeros, RK2s, &
223 (dat(i,ih,j), i=1, nprint),escaps, tkins, tgravs, tterms, &
224 ((dat(i,k,j), i=1, nprint),k=1,ih-1), &
225 ((dat(i,k,j), i=1, nprint),k=ih+1,11), &
226 dtmaxs, &
227 (isteps(i),i=1, nprint), &
228 ((dat(i,k,j), i=1, nprint),k=12,18), &
229 nptmass, &
230 ((dat(i,k,j), i=nprint+1,nprint+nptmass),k=1,6), &
231 (dat(i,ipmass,j), i=nprint+1,nprint+nptmass), &
232 (dat(i,ih,j), i=nprint+1,nprint+nptmass), &
233 (isteps(i), i=nprint+1,nprint+nptmass), &
234 (iphase(i), i=1,nprint)
235 endif
236 !
237 !--extract time and gamma
238 !
239 if (doubleprec) then
240 gamma(j) = real(gammai)
241 time(j) = real(timei)
242 else
243 gamma(j) = gammasi
244 time(j) = timesi
245 endif
246 print*,' time = ',time(j),' gamma = ',gamma(j)
247
248 npart = nprint+nptmass
249 !
250 !--convert to single precision if necessary
251 !
252 if (doubleprec) then
253 dat(1:npart,1:ncolstep,j) = real(dattemp(1:npart,1:ncolstep))
254 endif
255 !
256 !--set particle types using iphase
257 !
258 ipart = 0
259 npartoil = 0
260 npartwater = 0
261 nunknown = 0
262 do i=1,nprint
263 select case(iphase(i))
264 case(0)
265 npartoil = npartoil + 1
266 iamtype(i,j) = 1
267 case(1)
268 npartwater = npartwater + 1
269 iamtype(i,j) = 2
270 case default
271 nunknown = nunknown + 1
272 iamtype(i,j) = 4
273 end select
274 enddo
275 !
276 !--set particle type for point masses
277 !
278 if (nptmass > 0) then
279 iamtype(nprint+1:nprint+nptmass,j) = 3
280 endif
281
282 if (nunknown > 0) print *,nunknown,' particles of unknown type (probably dead)'
283
284 if (allocated(dattemp)) deallocate(dattemp)
285 if (allocated(isteps)) deallocate(isteps)
286 if (allocated(iphase)) deallocate(iphase)
287
288 npartoftype(1,j) = npartoil
289 npartoftype(2,j) = npartwater
290 npartoftype(3,j) = nptmass
291 npartoftype(4,j) = nunknown
292 print "(1x,3(a,i10))",' n(oil) = ',npartoil,' n(water) = ',npartwater,' nptmass = ',nptmass
293
294 if (ierr /= 0) then
295 print "(a)",'*** INCOMPLETE DATA ***'
296 nstepsread = nstepsread + 1
297 exit over_steps_in_file
298 else
299 nstepsread = nstepsread + 1
300 endif
301 j = j + 1
302
303 enddo over_steps_in_file
304
305 endif
306
307 55 continue
308 !
309 !--reached end of file
310 !
311 close(15)
312 if (j-1 > 0) then
313 print*,'>> end of dump file: nsteps =',j-1
314 endif
315
316 return
317
318 end subroutine read_data_oilonwater
319
320 !!------------------------------------------------------------
321 !! set labels for each column of data
322 !!------------------------------------------------------------
323
324 subroutine set_labels_oilonwater
325 use labels, only:label,unitslabel=>unitslabel_default,&
326 ix,ih,ipmass,irho,iutherm,ivx,labeltype,&
327 labelvec,labelzintegration,iamvec,&
328 make_vector_label
329 use params
330 use physcon, only:solarrcgs,solarmcgs
331 use settings_data
332 use geometry, only:labelcoord
333 use oilonwaterread, only:udisti,umassi,utimei
334 use settings_units, only:units=>units_default,unitzintegration
335 integer :: i
336
337 if (ndim <= 0 .or. ndim > 3) then
338 print*,'*** ERROR: ndim = ',ndim,' in set_labels_oilonwater ***'
339 return
340 endif
341 if (ndimV <= 0 .or. ndimV > 3) then
342 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_oilonwater ***'
343 return
344 endif
345
346 do i=1,ndim
347 ix(i) = i
348 enddo
349 ivx = 4
350 ih = 7 ! smoothing length
351 iutherm = 8 ! thermal energy
352 ipmass = 9 ! particle mass
353 irho = 10 ! location of rho in data array
354 if (ncolumns > 10) then
355 label(11) = 'dgrav'
356 label(12) = 'torque t'
357 label(13) = 'torque g'
358 label(14) = 'torque p'
359 label(15) = 'torque v'
360 label(16) = 'torque c'
361 label(17) = 'hecomp'
362 label(18) = 'potential energy'
363 endif
364
365 label(ix(1:ndim)) = labelcoord(1:ndim,1)
366 do i=1,ndimV
367 label(ivx+i-1) = 'v\d'//labelcoord(i,1)
368 enddo
369 label(irho) = 'density'
370 label(iutherm) = 'u'
371 label(ih) = 'h'
372 label(ipmass) = 'particle mass'
373
374 !
375 !--set labels for vector quantities
376 !
377 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
378
379 !
380 !--set transformation factors between code units/real units
381 !
382 units(1:3) = udisti/solarrcgs
383 unitslabel(1:3) = ' [R_{\(2281)}]'
384 units(4:6) = udisti/utimei
385 unitslabel(4:6) = ' [cm/s]'
386 units(ih) = units(1)
387 unitslabel(ih) = unitslabel(1)
388 units(iutherm) = (udisti/utimei)**2
389 unitslabel(iutherm) = ' [erg/g]'
390 units(ipmass) = umassi/solarmcgs
391 unitslabel(ipmass) = ' [M_{\(2281)}]'
392 units(irho) = umassi/udisti**3
393 unitslabel(irho) = ' [g/cm^3]'
394
395 !--unit for z integration - leave this as cm to get g/cm^2 in column density
396 unitzintegration = udisti
397 labelzintegration = ' [cm]'
398
399 !--time units for legend
400 units(0) = utimei/3.1536e7
401 unitslabel(0) = ' yrs'
402
403 !
404 !--set labels for each particle type
405 !
406 ntypes = 4
407 labeltype(1) = 'gas (oil)'
408 labeltype(2) = 'gas (water)'
409 labeltype(3) = 'point mass'
410 labeltype(4) = 'unknown'
411 UseTypeInRenderings(1) = .true.
412 UseTypeInRenderings(2) = .true.
413 UseTypeInRenderings(3) = .false.
414 UseTypeInRenderings(4) = .true.
415
416 end subroutine set_labels_oilonwater
417 end module readdata_oilonwater