Debian Codegraph @

src/write_sphdata.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-2023 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! module containing output routines for writing SPH data
25 ! as read by SPLASH to output file in various formats
26 !-----------------------------------------------------------------
27 module write_sphdata
28 implicit none
29 public :: issphformat,write_sphdump
30 private
31
32 contains
33
34 !-----------------------------------------------------------------
35 ! utility to check if a format selection is valid
36 !-----------------------------------------------------------------
37 logical function issphformat(string)
38 use asciiutils, only:lcase
39 character(len=*), intent(in) :: string
40 logical :: verbose
41
42 issphformat = .false.
43 verbose = .true.
44 select case(trim(lcase(string)))
45 case('ascii','csv')
46 issphformat = .true.
47 case('binary','binarystream','stream')
48 issphformat = .true.
49 case('rsph')
50 issphformat = .true.
51 case('phantom')
52 issphformat = .true.
53 case('gadget')
54 issphformat = .true.
55 case('none')
56 verbose = .false.
57 end select
58
59 if (.not.issphformat .and. verbose) then
60 print "(a)",' convert mode ("splash to X dumpfiles"): '
61 print "(a,/)",' splash to ascii : convert SPH data to ascii file dumpfile.ascii'
62 print "(a)", ' to csv : convert SPH data to csv file dumpfile.csv'
63 print "(a)", ' to binary : convert SPH data to simple unformatted binary dumpfile.binary: '
64 print "(a)", ' write(1) time,np,ncols'
65 print "(a)", ' do i=1,np'
66 print "(a)", ' write(1) dat(1:ncols),itype'
67 print "(a)", ' enddo'
68 print "(a)", ' to stream : convert SPH data to streamed binary format dumpfile.binarystream: '
69 if (kind(1.)==8) then
70 print "(a)", ' t,np,ncols,np*(dat(1:ncols),itype) [8,4,4,np*(ncols*8,4)] bytes'
71 else
72 print "(a)", ' t,np,ncols,np*(dat(1:ncols),itype) [4,4,4,np*(ncols*4,4)] bytes'
73 endif
74 print "(a)", ' to phantom : convert SPH data to binary dump file for PHANTOM'
75 print "(a)", ' to gadget : convert SPH data to default GADGET snapshot file format'
76 elseif (.not.issphformat) then
77 print "(a)",'Convert mode: '
78 print "(a)",' splash to ascii : convert to ascii; type "splash to" for other formats'
79 endif
80
81 return
82 end function issphformat
83
84 subroutine write_sphdump(time,gamma,dat,npart,ntypes,npartoftype,masstype,itype,ncolumns,filename,outformat,listofcolumns)
85 use labels, only:labeltype,label,unitslabel,irho,ipmass,ix,iBfirst,ih,iutherm,ivx
86 use settings_units, only:units
87 use settings_data, only:ndim,icoords,icoordsnew,xorigin
88 use params, only:int1,maxplot,doub_prec
89 use write_data_phantom, only:write_sphdata_phantom
90 use write_data_gadget, only:write_sphdata_gadget
91 use filenames, only:tagline
92 use geomutils, only:change_coords
93 use asciiutils, only:lcase
94 integer, intent(in) :: npart,ntypes,ncolumns
95 integer, intent(in), dimension(:) :: npartoftype
96 integer(kind=int1), intent(in), dimension(:) :: itype
97 real, intent(in) :: time,gamma
98 real, intent(in), dimension(npart,ncolumns) :: dat
99 real, intent(in), dimension(:) :: masstype
100 character(len=*), intent(in) :: filename,outformat
101 integer, intent(in), dimension(:), optional :: listofcolumns
102 integer, parameter :: iunit = 83
103 integer, parameter :: maxline = 1000
104 integer :: ierr,i,idim,i1,i2,ncols
105 integer, dimension(ncolumns) :: iorder
106 character(len=40) :: fmtstring,fmtstring2,fmtstringlab,outfile
107 character(len=6) :: ext
108 real(kind=doub_prec), dimension(maxplot) :: vals
109 real(kind=doub_prec) :: udist,umass,utime,umagfd
110 real, dimension(3) :: x0,v0
111 logical :: change_coordsys,use_csv
112 !
113 !--allow for the possibility of writing columns in a different order
114 !
115 if (present(listofcolumns)) then
116 call get_order_from_list(listofcolumns,iorder,ncols)
117 else
118 ncols = ncolumns
119 do i=1,ncols
120 iorder(i) = i
121 enddo
122 endif
123
124 select case(trim(lcase(outformat)))
125 case ('ascii','csv')
126 ext = '.ascii'
127 use_csv = (trim(lcase(outformat))=='csv')
128 if (use_csv) ext = '.csv'
129
130 print "(/,5('-'),'>',a,i2,a,1x,'<',5('-'),/)",' WRITING TO FILE '//trim(filename)//trim(ext)//' WITH ',ncols,' COLUMNS'
131
132 !--format the header lines to go in the ascii file
133 if (kind(1.)==8) then
134 if (use_csv) then
135 write(fmtstring,"(i10,a)") ncols,"(es23.15,',')"
136 write(fmtstringlab,"(i10,a)") ncols,"(a23,','),a"
137 else
138 write(fmtstring,"(i10,a)") ncols,'(es23.15,1x)'
139 write(fmtstringlab,"(i10,a)") ncols,'(a23,1x),a'
140 endif
141 else
142 if (use_csv) then
143 write(fmtstring,"(i10,a)") ncols,"(es15.7,',')"
144 write(fmtstringlab,"(i10,a)") ncols,"(a15,','),a"
145 else
146 write(fmtstring,"(i10,a)") ncols,'(es15.7,1x)'
147 write(fmtstringlab,"(i10,a)") ncols,'(a15,1x),a'
148 endif
149 endif
150 fmtstring2 = '('//trim(adjustl(fmtstring))//',i0)'
151 fmtstring = '('//trim(adjustl(fmtstring))//')'
152 if (use_csv) then
153 fmtstringlab = '('//trim(adjustl(fmtstringlab))//')'
154 else
155 fmtstringlab = '(''#'',1x,'//trim(adjustl(fmtstringlab))//')'
156 endif
157
158 open(unit=iunit,file=trim(filename)//trim(ext),status='replace',form='formatted',iostat=ierr)
159 if (ierr /= 0) then
160 print "(a)",' ERROR OPENING FILE FOR WRITING'
161 return
162 endif
163 if (.not.use_csv) then
164 write(iunit,"(a)",iostat=ierr) '# '//trim(filename)//trim(ext)//', created by '//trim(tagline)
165 write(iunit,"('#')",iostat=ierr)
166 write(iunit,"('#',1x,'time:',13x,'time unit (',a,')')",iostat=ierr) trim(unitslabel(0))
167 write(iunit,"('#',2(1x,1pe15.7))",iostat=ierr) time,units(0)
168 write(iunit,"('#')",iostat=ierr)
169 write(iunit,"('#',1x,'npart:',30(1x,a12))",iostat=ierr) (trim(labeltype(i)),i=1,ntypes)
170 write(iunit,"('#',7x,30(1x,i12))",iostat=ierr) npartoftype(1:ntypes)
171 write(iunit,"('# units:')",iostat=ierr)
172 write(iunit,"('#'"//fmtstring(2:),iostat=ierr) units(iorder(1:ncols))
173 write(iunit,fmtstringlab,iostat=ierr) unitslabel(iorder(1:ncols))
174 write(iunit,"('#')",iostat=ierr)
175 endif
176 if (ierr /= 0) print "(a)",' ERROR WRITING ASCII HEADER'
177 !
178 !--write body
179 !
180 change_coordsys = (icoordsnew /= icoords .and. ndim > 0 .and. all(ix(1:ndim) > 0))
181 x0 = xorigin(:) ! note that it is not currently possible to do splash to ascii
182 v0 = 0. ! with coords set relative to a tracked particle, so just use xorigin
183
184 if (size(itype) > 1) then
185 write(iunit,fmtstringlab,iostat=ierr) label(iorder(1:ncols)),'itype'
186 do i=1,npart
187 vals(1:ncolumns) = dat(i,1:ncolumns)
188 if (change_coordsys) call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
189 write(iunit,fmtstring2,iostat=ierr) vals(iorder(1:ncols)),itype(i)
190 enddo
191 else
192 write(iunit,fmtstringlab,iostat=ierr) label(iorder(1:ncols))
193 if (change_coordsys) then
194 do i=1,npart
195 vals(1:ncolumns) = dat(i,1:ncolumns)
196 call change_coords(vals,ncolumns,ndim,icoords,icoordsnew,x0,v0)
197 write(iunit,fmtstring,iostat=ierr) vals(iorder(1:ncols))
198 enddo
199 else
200 do i=1,npart
201 write(iunit,fmtstring,iostat=ierr) dat(i,iorder(1:ncols))
202 enddo
203 endif
204 endif
205 close(iunit)
206 if (ierr /= 0) then
207 print "(a)",' ERROR WRITING ASCII FILE'
208 endif
209 return
210
211 case ('binary','binarystream','stream')
212 !
213 !--This is the most basic binary (ie. unformatted) file format I could think of,
214 ! as an alternative to ascii for large files.
215 !
216 select case(trim(outformat))
217 case('binarystream','BINARYSTREAM','stream')
218 print "(/,5('-'),'>',a,i2,a,1x,'<',5('-'),/)",' WRITING TO FILE '//trim(filename)//'.binarystream WITH ',ncolumns,' COLUMNS'
219 open(unit=iunit,file=trim(filename)//'.binarystream',status='replace',form='unformatted',access='stream',iostat=ierr)
220 case default
221 print "(/,5('-'),'>',a,i2,a,1x,'<',5('-'),/)",' WRITING TO FILE '//trim(filename)//'.binary WITH ',ncolumns,' COLUMNS'
222 open(unit=iunit,file=trim(filename)//'.binary',status='replace',form='unformatted',iostat=ierr)
223 end select
224 if (ierr /= 0) then
225 print "(a)",' ERROR OPENING FILE FOR WRITING'
226 return
227 endif
228 write(iunit,iostat=ierr) time,npart,ncols
229 if (ierr /= 0) print "(a)",' ERROR WRITING HEADER LINE TO BINARY FILE '
230 !
231 !--write body
232 !
233 if (size(itype) > 1) then
234 do i=1,npart
235 write(iunit,iostat=ierr) dat(i,iorder(1:ncols)),int(itype(i))
236 enddo
237 else
238 do i=1,npart
239 write(iunit,iostat=ierr) dat(i,iorder(1:ncols))
240 enddo
241 endif
242 close(iunit)
243 if (ierr /= 0) print "(a)",' ERROR WRITING BINARY FILE'
244 return
245
246 case ('rsph')
247 !
248 !--Files for Steinar Borve's RSPH format
249 !
250 if (ndim < 2) then
251 print "(a)",' ERROR: cannot write RSPH format for < 2D'
252 return
253 endif
254 outfile = 'rsph2D_pos.dat'
255 print "(a)",' writing to '//trim(outfile)
256 open(unit=iunit,file=outfile,status='replace',form='formatted',iostat=ierr)
257 if (ierr /= 0) then
258 print "(a)",' ERROR OPENING '//trim(outfile)//' FOR WRITING'
259 return
260 endif
261 write(iunit,"(i1)") ndim
262 write(iunit,"(a)") 'position'
263 write(iunit,"(i4)") maxline
264 write(iunit,*) (minval(dat(1:npart,ix(i))),i=1,ndim)
265 write(iunit,*) (maxval(dat(1:npart,ix(i))),i=1,ndim)
266 write(iunit,*) time
267 write(iunit,*) npart
268 do idim=1,2
269 i1 = 1
270 i2 = 0
271 do while (i2 < npart)
272 i2 = min(i2 + maxline,npart)
273 write(iunit,*) dat(i1:i2,ix(idim))
274 i1 = i2 + 1
275 enddo
276 enddo
277 close(unit=iunit)
278
279 outfile = 'rsph2D_rho.dat'
280 print "(a)",' writing to '//trim(outfile)
281 open(unit=iunit,file=outfile,status='replace',form='formatted',iostat=ierr)
282 if (ierr /= 0) then
283 print "(a)",' ERROR OPENING FILE FOR WRITING'
284 return
285 endif
286 write(iunit,"(i1)") ndim
287 write(iunit,"(a)") 'density'
288 write(iunit,"(i4)") maxline
289 write(iunit,*) (minval(dat(1:npart,ix(i))),i=1,ndim)
290 write(iunit,*) (maxval(dat(1:npart,ix(i))),i=1,ndim)
291 i1 = 1
292 i2 = 0
293 do while (i2 < npart)
294 i2 = min(i2 + maxline,npart)
295 write(iunit,*) dat(i1:i2,irho)
296 i1 = i2 + 1
297 enddo
298 close(unit=iunit)
299
300 outfile = 'rsph2D_siz.dat'
301 print "(a)",' writing to '//trim(outfile)
302 open(unit=iunit,file=outfile,status='replace',form='formatted',iostat=ierr)
303 if (ierr /= 0) then
304 print "(a)",' ERROR OPENING FILE FOR WRITING'
305 return
306 endif
307 write(iunit,"(i1)") ndim
308 write(iunit,"(a)") 'size'
309 write(iunit,"(i4)") maxline
310 write(iunit,*) (minval(dat(1:npart,ix(i))),i=1,ndim)
311 write(iunit,*) (maxval(dat(1:npart,ix(i))),i=1,ndim)
312 i1 = 1
313 i2 = 0
314 do while (i2 < npart)
315 i2 = min(i2 + maxline,npart)
316 write(iunit,*) ((dat(i,ipmass)/dat(i,irho))**(1./ndim),i=i1,i2)
317 i1 = i2 + 1
318 enddo
319 close(unit=iunit)
320
321 case('phantom')
322 udist = 0.d0
323 umagfd = 0.d0
324 if (ix(1) > 0) udist = units(ix(1))
325 utime = units(0)
326 if (ipmass > 0) then
327 umass = units(ipmass)
328 else
329 print "(a)",' WARNING: units for mass unknown, written as 1.0'
330 umass = 1.0d0
331 endif
332 if (iBfirst > 0) umagfd = units(iBfirst)
333 call write_sphdata_phantom(time,gamma,dat,ndim,npart,ntypes,npartoftype,&
334 masstype,ncolumns,udist,utime,umass,umagfd,&
335 labeltype,label,ix,ih,ivx,iBfirst,&
336 ipmass,irho,iutherm,filename,0.)
337 case('gadget')
338 call write_sphdata_gadget(time,dat,itype,npart,ntypes,npartoftype,&
339 masstype,ncolumns,filename)
340 case default
341 print "(a)",' ERROR: unknown output format '''//trim(outformat)//''' in write_sphdump'
342 return
343 end select
344
345 end subroutine write_sphdump
346
347 subroutine get_order_from_list(list_in,list_out,n)
348 integer, intent(in) :: list_in(:)
349 integer, intent(out) :: list_out(:)
350 integer, intent(out) :: n
351 integer :: i
352 logical :: done
353 !
354 ! set list_out = list_in, but only while list_in
355 ! is non-zero and numbers are in range
356 !
357 i = 1
358 done = .false.
359 list_out = 0
360 do while(.not.done .and. i <= size(list_in) .and. i <= size(list_out))
361 if (list_in(i) > 0 .and. list_in(i) <= size(list_out)) then
362 list_out(i) = list_in(i)
363 else
364 done = .true.
365 endif
366 i = i + 1
367 enddo
368 n = count(list_out > 0)
369 ! if no valid entries, set list_out to 1..n
370 if (n==0) then
371 n = min(size(list_out),size(list_in))
372 do i=1,n
373 list_out(i) = i
374 enddo
375 endif
376
377 end subroutine get_order_from_list
378
379 end module write_sphdata