Debian Codegraph @

src/read_data_starsmasher.f90

raw
1 !-------------------------------------------------------------------------
2 ! this subroutine reads from the data file(s)
3 ! change this to change the format of data input
4 !
5 ! THIS VERSION IS FOR OUTPUT FROM THE STARSMASHER CODE
6 !
7 !-------------------------------------------------------------------------
8
9 module readdata_starsmasher
10 implicit none
11
12 public :: read_data_starsmasher, set_labels_starsmasher
13
14 private
15 integer :: frame = -1 ! default value of -1
16
17 contains
18
19
20 subroutine read_data_starsmasher(rootname,istepstart,ipos,nstepsread)
21 use particle_data, only:dat,iamtype,npartoftype,time,gamma,headervals,&
22 maxpart,maxcol,maxstep
23 use params
24 use settings_data, only:ndim,ndimV,ncolumns,ncalc,iformat,required,ipartialread
25 use mem_allocation, only:alloc
26 use labels, only:ih,irho,headertags
27 use system_utils, only:renvironment,lenvironment
28 use prompting, only:prompt
29 integer, intent(in) :: istepstart,ipos
30 integer, intent(out) :: nstepsread
31 character(len=*), intent(in) :: rootname
32 character(len=len(rootname)+10) :: datfile
33 integer :: i,j,ierr
34 integer :: ncolstep,npart_max,nstep_max,ntoti
35 logical :: iexist,reallocate
36 real(doub_prec) :: timetemp,dummy
37 integer :: ntot, nnopt, nout, nit, nav, ngr, nrelax
38 real(doub_prec) :: hmin, hmax, sep0, tf, dtout, alpha, beta, eta2, trelax, dt, omega2
39 real(doub_prec) :: dx, dy, dz, dm, dh, drho, dvx, dvy, dvz, dudot
40 real(doub_prec) :: duth, dmmu
41 real(doub_prec) :: theta
42 real(doub_prec) :: gram, sec, cm, kelvin, erg, boltz
43 parameter(gram=1.d0,sec=1.d0,cm=1.d0,kelvin=1.d0)
44 parameter(erg=gram*cm**2/sec**2)
45 parameter(boltz=1.380658d-16*erg/kelvin)
46 real(doub_prec) :: munit, runit, gravconst
47 parameter(munit=1.9891d33,runit=6.9599d10)
48 parameter(gravconst = 6.67390d-08)
49
50 nstepsread = 0
51 npart_max = maxpart
52
53 if (len_trim(rootname).gt.0) then
54 datfile = trim(rootname)
55 else
56 print*,' **** no data read **** '
57 return
58 endif
59 !
60 !--check if first data file exists
61 !
62 inquire(file=datfile,exist=iexist)
63 if (.not.iexist) then
64 print "(a)",' *** error: '//trim(datfile)//': file not found ***'
65 return
66 endif
67 !
68 !--set parameters which do not vary between timesteps
69 !
70 ndim = 3
71 ndimV = 3
72 !
73 !--read data from snapshots
74 !
75 i = istepstart
76
77 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
78 !
79 !--open data file and read data
80 !
81 open(11,iostat=ierr,file=datfile,status='old',form='unformatted')
82 if (ierr /= 0) then
83 print "(a)", '*** ERROR OPENING FILE ***'
84 return
85 endif
86 !
87 !--read header for this timestep
88 !
89
90 read(11, iostat = ierr) &
91 ntot, nnopt, hmin, hmax, sep0, tf, dtout, nout, nit, timetemp, &
92 nav, alpha, beta, eta2, ngr, nrelax, trelax, dt, omega2
93
94 if (omega2 > 0.d0 .and. frame == -1) then
95 ! frame can equal -1 only the first time through, so this question
96 ! will get asked (at most) only once
97 print *, 'Period of rotating frame=',8*atan(1.d0)/omega2**0.5d0
98 frame = 0
99 call prompt('Do you want movie in the inertial frame? (1=yes)',frame,0,1)
100 endif
101 if (frame.eq.-1) frame=0
102
103 if (ierr /= 0) then
104 print "(a)", '*** ERROR READING TIMESTEP HEADER ***'
105 return
106 endif
107
108 iformat = 0
109 ncolstep = 13 ! 3 x pos, 3 x vel, pmass, rho, utherm, mean_mu, h, du/dt, temperature
110 ncolumns = ncolstep
111
112 irho = 8
113 ih = ncolstep
114
115 ntoti = ntot !int(sum(npartoftypei(1:6)))
116 print*,'time : ',timetemp
117 print*,'N_total : ',ntoti
118 print*,'N data columns : ',ncolstep
119
120 !
121 !--if successfully read header, increment the nstepsread counter
122 !
123 nstepsread = nstepsread + 1
124 !
125 !--now read data
126 !
127 reallocate = .false.
128 npart_max = maxpart
129 nstep_max = max(maxstep,1)
130
131 if (ntoti.gt.maxpart) then
132 reallocate = .true.
133 if (maxpart.gt.0) then
134 ! if we are reallocating, try not to do it again
135 npart_max = int(1.1*ntoti)
136 else
137 ! if first time, save on memory
138 npart_max = int(ntoti)
139 endif
140 endif
141 if (i.ge.maxstep .and. i.ne.1) then
142 nstep_max = i + max(10,INT(0.1*nstep_max))
143 reallocate = .true.
144 endif
145
146 !
147 !--reallocate memory for main data array
148 !
149 if (reallocate .or. .not.(allocated(dat))) then
150 call alloc(npart_max,nstep_max,max(ncolstep+ncalc,maxcol),mixedtypes=.true.)
151 endif
152 !
153 !--copy header into header arrays
154 !
155 npartoftype(:,i) = 0
156 !
157 !--set time to be used in the legend
158 !
159 time(i) = real(timetemp)
160 !
161 !--store other quantities from the file header
162 !
163 headertags(1:18) = (/'ntot ','nnopt ','hmin ','hmax ',&
164 'sep0 ','tf ','dtout ','nout ',&
165 'nit ','nav ','alpha ','beta ',&
166 'eta2 ','ngr ','nrelax','trelax',&
167 'dt ','omega2'/)
168 headervals(1:18,i) = (/real(ntot),real(nnopt),real(hmin),real(hmax),&
169 real(sep0),real(tf),real(dtout),real(nout),&
170 real(nit),real(nav),real(alpha),real(beta), &
171 real(eta2),real(ngr),real(nrelax),real(trelax),&
172 real(dt),real(omega2)/)
173 !
174 !--read particle data
175 !
176 if (ntoti.gt.0) then
177 !
178 !--read particles' data
179 !
180 do j=1,ntot
181
182 read(11, iostat = ierr) &
183 dx, dy, dz, & ! positions
184 dm, & ! mass
185 dh, drho, & ! h & rho
186 dvx, dvy, dvz, & ! velocities
187 dummy, dummy, dummy, & ! vxdot, vydot, vzdot
188 duth, & ! uthermal
189 dudot, & ! udot
190 dummy, & ! dummy, dummy, dummy, & ! gx, gy, gz, gpot
191 dmmu, & ! mean_mu
192 dummy, dummy!, dummy, dummy ! aa, bb, cc, divv
193
194 if(frame.eq.1) then
195 theta=omega2**0.5d0*timetemp
196 ! rotation is counterclockwise:
197 dat(j,1,i)=dx*cos(theta)-dy*sin(theta)
198 dat(j,2,i)=dx*sin(theta)+dy*cos(theta)
199 dat(j,4,i)=dvx*cos(theta)-dvy*sin(theta)
200 dat(j,5,i)=dvx*sin(theta)+dvy*cos(theta)
201 else
202 dat(j,1,i) = dx
203 dat(j,2,i) = dy
204 dat(j,4,i) = dvx
205 dat(j,5,i) = dvy
206 endif
207
208 dat(j,3,i) = dz
209 dat(j,6,i) = dvz
210
211 dat(j,7,i) = dm
212
213 dat(j,8,i) = drho
214 dat(j,9,i) = duth
215 dat(j,10,i) = dmmu
216 dat(j,11,i) = dh
217 dat(j,12,i) = dudot
218 dat(j,13,i) = duth*gravconst*munit/runit/(1.5d0*boltz/dmmu)
219
220 if (abs(duth) > tiny(duth)) then
221 iamtype(j,i) = 1
222 npartoftype(1,i) = npartoftype(1,i)+1
223 else
224 iamtype(j,i) = 2
225 npartoftype(2,i) = npartoftype(2,i)+1
226 endif
227
228 end do
229 ! pos = 1,2,3
230 ! vel = 4,5,6
231 ! mass = 7
232 ! rho, u, mean_mu, h = 8, 9, 10, 11
233
234 else
235 ntoti = 1
236 npartoftype(1,i) = 1
237 dat(:,:,i) = 0.
238 endif
239
240 !
241 !--now memory has been allocated, set arrays which are constant for all time
242 !
243 gamma = 5./3.
244 !
245 !--set flag to indicate that only part of this file has been read
246 !
247 if (.not.all(required(1:ncolstep))) ipartialread = .true.
248 !
249 !--close data file and return
250 !
251 close(unit=11)
252
253 if (nstepsread.gt.0) then
254 print*,'>> last step ntot =',sum(npartoftype(:,istepstart+nstepsread-1))
255 endif
256 return
257
258 end subroutine read_data_starsmasher
259
260 !!------------------------------------------------------------
261 !! set labels for each column of data
262 !!------------------------------------------------------------
263
264 subroutine set_labels_starsmasher
265 use labels, only:label,iamvec,labelvec,labeltype,ix,ivx,ipmass,ih,&
266 irho,ipr,iutherm,make_vector_label
267 use params
268 use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings
269 use geometry, only:labelcoord
270 use system_utils, only:renvironment
271 integer :: i
272
273 if (ndim.le.0 .or. ndim.gt.3) then
274 print*,'*** ERROR: ndim = ',ndim,' in set_labels ***'
275 return
276 endif
277 if (ndimV.le.0 .or. ndimV.gt.3) then
278 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels ***'
279 return
280 endif
281
282 do i=1,ndim
283 ix(i) = i
284 enddo
285 ivx = 4
286 ipmass = 7
287 irho = 8 ! location of rho in data array
288 ipr = 0
289 iutherm = 9 ! thermal energy
290 ih = 11
291
292 label(ix(1:ndim)) = labelcoord(1:ndim,1)
293 label(irho) = 'density'
294 label(iutherm) = 'specific internal energy u'
295 label(ih) = 'h'
296 label(10) = 'mean_mu'
297 label(ipmass) = 'particle mass'
298 label(12) = 'du/dt'
299 label(13) = 'temperature'
300 !
301 !--set labels for vector quantities
302 !
303 call make_vector_label('v',ivx,ndimV,iamvec,labelvec,label,labelcoord(:,1))
304 !
305 !--set labels for each particle type
306 !
307 ntypes = 2
308 labeltype(1) = 'gas'
309 labeltype(2) = 'compact object'
310 UseTypeInRenderings(1) = .true.
311 UseTypeInRenderings(2) = .false.
312
313 end subroutine set_labels_starsmasher
314
315 end module readdata_starsmasher