Debian Codegraph @

src/write_data_phantom.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-2019 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! Module implementing "splash to phantom" operation, writing
25 ! a binary dump file suitable for input to the PHANTOM code
26 !-----------------------------------------------------------------
27 module write_data_phantom
28 use iso_c_binding, only:c_float,c_double
29 implicit none
30 integer, parameter :: int8 = selected_int_kind(10)
31 integer, parameter :: sing_prec = c_float
32 integer, parameter :: doub_prec = c_double
33 character(len=10), parameter, public :: formatname='phantom'
34 integer, parameter :: lentag = 16
35
36 public :: write_sphdata_phantom
37 private
38
39 contains
40 !--------------------------------------------------------------------
41 !+
42 ! write header tag for tagged format
43 !+
44 !--------------------------------------------------------------------
45 function tag(string)
46 character(len=lentag) :: tag
47 character(len=*), intent(in) :: string
48
49 tag = adjustl(string)
50
51 end function tag
52
53 !--------------------------------------------------------------------
54 !+
55 ! write header tag for tagged format
56 !+
57 !--------------------------------------------------------------------
58 function convert_tag_to_phantom(string) result(mytag)
59 character(len=lentag) :: mytag
60 character(len=*), intent(in) :: string
61
62 mytag = adjustl(string)
63 select case(trim(mytag))
64 case('Potential')
65 mytag = 'poten'
66 end select
67
68 end function convert_tag_to_phantom
69
70 !--------------------------------------------------------------------
71 !+
72 ! write output data file that is readable by phantom
73 !+
74 !--------------------------------------------------------------------
75 subroutine write_sphdata_phantom(time,gamma,dat,ndim,ntotal,ntypes,npartoftype, &
76 masstype,ncolumns,udist,umass,utime,umagfd,labeltype,&
77 label_dat,ix,ih,ivx,iBfirst,ipmass,irho,iutherm,filename,hsoft_sink)
78
79 integer, intent(in) :: ndim,ntotal,ntypes,ncolumns
80 integer, intent(in) :: npartoftype(:)
81
82 real, intent(in) :: time !! real8 vs real4 error
83
84 real, intent(in) :: gamma
85 real, intent(in) :: dat(ntotal,ncolumns)
86 real, intent(in) :: masstype(:)
87 real(doub_prec), intent(in) :: udist,umass,utime,umagfd
88 character(len=*), intent(in) :: labeltype(ntypes),label_dat(ncolumns)
89 integer, intent(in) :: ix(3),ivx,ih,iBfirst,ipmass,irho,iutherm
90 character(len=*), intent(in) :: filename
91 real, intent(in) :: hsoft_sink
92
93 integer, parameter :: i_int = 1, &
94 i_int1 = 2, &
95 i_int2 = 3, &
96 i_int4 = 4, &
97 i_int8 = 5, &
98 i_real = 6, &
99 i_real4 = 7, &
100 i_real8 = 8
101 integer, parameter :: idump = 83
102 character(len=len(filename)+10) :: outfile
103
104 integer, parameter :: intval1=690706,intval2=780806
105 integer, parameter :: int1o=690706 !,int2o=780806
106 integer, parameter :: idimhead = 22
107 integer(kind=int8) :: nparttot,npartoftypetot(ntypes),number8
108 integer :: nums(8),idot
109 integer :: narraylengths,nblocks,nblockarrays,ntypesi
110 integer :: i,j,ierr,i1,index1,number,nptmass,iversion,np,maxrhead
111 real :: rheader(idimhead)
112 character(len=lentag) :: rheader_tags(idimhead)
113 real :: r1,hfact,macc,spinx,spiny,spinz
114 character(len=2), parameter :: vlabel(3) = (/'vx','vy','vz'/)
115 character(len=2), parameter :: Blabel(3) = (/'Bx','By','Bz'/)
116 logical :: mhd
117 logical, allocatable :: mask(:)
118 !
119 ! sink particle locations in dat array
120 !
121 integer, allocatable :: ilocsink(:)
122 !
123 !--define output file name
124 !
125 idot = index(filename,'.')-1
126 if (idot <= 0) idot = len_trim(filename)
127 outfile=filename(1:idot)//'.tmp'
128
129 narraylengths = 2
130 nblocks = 1 ! not parallel dump
131 hfact = 1.2 ! must be specified in phantom dumps
132 nptmass = 0 ! work this out later
133 !
134 !--check if we have enough data to write a PHANTOM dump
135 !
136 allocate(mask(ncolumns))
137 mask = .false.
138 if (ndim < 3) then
139 print "(a)",' ERROR: ndim < 3 but must be 3 for PHANTOM data -- cannot write PHANTOM dump, skipping...'
140 return
141 endif
142 if (any(ix(:) <= 0)) then
143 print "(a)",' ERROR: position labels not set -- cannot write PHANTOM dump, skipping...'
144 return
145 endif
146 if (ivx <= 0) then
147 print "(a)",' ERROR: velocity not found in data -- cannot write PHANTOM dump, skipping...'
148 return
149 endif
150 if (ih <= 0) then
151 print "(a)",' ERROR: smoothing length not found in data -- cannot write PHANTOM dump, skipping...'
152 return
153 endif
154 mhd = .false.
155 if (iBfirst > 0) then
156 mhd = .true.
157 narraylengths = 4
158 mask(iBfirst:iBfirst+ndim-1) = .true.
159 endif
160 ! do not write known quantities twice
161 mask(ix) = .true.
162 mask(ivx:ivx+ndim-1) = .true.
163 mask(ih) = .true.
164 if (iutherm > 0) mask(iutherm) = .true.
165 if (ipmass > 0) mask(ipmass) = .true.
166 if (irho > 0) mask(irho) = .true.
167
168 !
169 !--figure out whether we have sink particles
170 !
171 call extract_sink_particles_from_data(ntypes,npartoftype,labeltype,np,&
172 npartoftypetot,nptmass,ntypesi,ilocsink)
173
174 nparttot = np
175
176 !--fill rheader and check that we have equal mass particles
177 rheader_tags = ' '
178 rheader(:) = 0.
179 rheader(1) = time
180 rheader(2) = gamma
181 rheader(3) = hfact
182 rheader_tags(1) = 'time'
183 rheader_tags(2) = 'gamma'
184 rheader_tags(3) = 'hfact'
185 if (ipmass > 0) then
186 index1 = 1
187 do i=1,ntypesi
188 rheader(3+i) = dat(index1,ipmass)
189 rheader_tags(3+i) = 'massoftype'
190 if (npartoftype(i) > 0) then
191 if (any(dat(index1:index1+npartoftype(i)-1,ipmass) /= dat(index1,ipmass))) then
192 print "(a)",' WARNING: unequal mass particles detected but PHANTOM only accepts equal mass...'
193 endif
194 index1 = index1 + npartoftype(i) - 1
195 endif
196 enddo
197 else
198 do i=1,ntypesi
199 rheader(3+i) = masstype(i)
200 rheader_tags(3+i) = 'massoftype'
201 enddo
202 endif
203 maxrhead = 3+ntypesi
204
205 write(*,"(/,/,'--------> TIME = ',f10.4,"// &
206 "': full dump written to file ',a,' on unit ',i2,' <--------',/)") &
207 time,trim(outfile),idump
208
209 open(unit=idump,file=outfile,status='replace',form='unformatted',iostat=ierr)
210 if (ierr /= 0) then
211 write(*,*) 'error: can''t create new dumpfile '//trim(outfile)
212 return
213 endif
214 !
215 !--write full dump Phantom/sphNG file
216 !
217 i1 = intval1
218 r1 = real(intval2)
219 iversion = 1 ! file version to write
220 write (idump, err=100) intval1,r1,intval2,iversion,int1o
221 write (idump, err=100) fileident('F','Phantom',mhd=mhd)
222 !
223 !--single values
224 !
225 !--default int
226 number = 4+ntypesi
227 write (idump, err=100) number
228 write (idump, err=100) tag('nparttot'),tag('ntypes'),(tag('npartoftype'),i=1,ntypesi),tag('nblocks'),tag('nptmass')
229 write (idump, err=100) int(nparttot),ntypesi,(int(npartoftypetot(i)),i=1,ntypesi),nblocks,nptmass
230
231 !--int*1, int*2, int*4
232 number = 0
233 do i = 1, 3
234 write (idump, err=100) number
235 enddo
236 !--int*8
237 number = 2 + ntypesi
238 write (idump, err=100) number
239 write (idump, err=100) tag('nparttot'),tag('ntypes'),(tag('npartoftype'),i=1,ntypesi)
240 write (idump, err=100) nparttot,int(ntypesi,kind=int8),npartoftypetot(1:ntypesi)
241
242 !--default real
243 write (idump, err=100) maxrhead
244 write (idump, err=100) rheader_tags(1:maxrhead)
245 write (idump, err=100) rheader(1:maxrhead)
246
247 !--real*4
248 number = 0
249 write (idump, err=100) number
250 !--real*8
251 if (umagfd > 0.) then
252 number = 4
253 write (idump, err=100) number
254 write (idump, err=100) tag('udist'),tag('umass'),tag('utime'),tag('umagfd')
255 write (idump, err=100) udist, umass, utime, umagfd
256 else
257 number = 3
258 write (idump, err=100) number
259 write (idump, err=100) tag('udist'),tag('umass'),tag('utime')
260 write (idump, err=100) udist, umass, utime
261 endif
262
263 nblockarrays = narraylengths*nblocks
264 write (idump, err=100) nblockarrays
265 !
266 !--array length 1 header
267 !
268 number8 = np
269 nums(:) = 0
270 if (iutherm > 0) then
271 nums(i_real) = 7
272 else
273 nums(i_real) = 6
274 endif
275 ! write any array not already counted
276 nums(i_real) = nums(i_real) + count(.not.mask)
277
278 nums(i_real4) = 1
279 write (idump, err=100) number8, (nums(i), i=1,8)
280 !
281 !--array length 2 header
282 !
283 number8 = nptmass
284 nums(:) = 0
285 if (nptmass > 0) nums(i_real) = 13
286 write (idump, err=100) number8, (nums(i), i=1,8)
287 !
288 !--array length 3 header
289 !
290 if (narraylengths >= 3) then
291 number8 = 0
292 nums(1:8) = 0
293 write (idump, err=100) number8, (nums(i), i=1,8)
294 endif
295 !
296 !--array length 4 header
297 !
298 if (narraylengths >= 4) then
299 if (mhd) then
300 number8 = np
301 else
302 number8 = 0
303 endif
304 nums(:) = 0
305 if (mhd) nums(i_real4) = 3
306 write (idump, err=100) number8, (nums(i), i=1,8)
307 endif
308
309 !
310 !--array length 1 arrays
311 !
312 !--default int
313 !--int*1
314 !--int*2
315 !--int*4
316 !--int*8
317 !--default real
318 do j = 1, 3
319 write (idump, err=100) tag(label_dat(ix(j)))
320 write (idump, err=100) (dat(i,ix(j)), i=1, np)
321 enddo
322
323 do j = 1, 3
324 write (idump, err=100) tag(vlabel(j))
325 write (idump, err=100) (dat(i,ivx+j-1), i=1, np)
326 enddo
327
328 if (iutherm > 0) then
329 write (idump, err=100) tag(label_dat(iutherm))
330 write (idump, err=100) (dat(i,iutherm), i=1, np)
331 endif
332
333 do j=1,ncolumns
334 if (.not.mask(j)) then
335 print*,tag(label_dat(j)),'->',convert_tag_to_phantom(label_dat(j))
336 write (idump, err=100) convert_tag_to_phantom(label_dat(j))
337 write (idump, err=100) (dat(i,j), i=1, np)
338 endif
339 enddo
340
341 !--real*4
342 ! dump smoothing length as a real*4 to save space
343 write (idump, err=100) tag('h')
344 write (idump, err=100) (real(dat(i,ih),kind=sing_prec), i=1, np)
345 !
346 !--sink particle arrays
347 !
348 if (nptmass > 0 .and. allocated(ilocsink)) then
349 do j = 1, 3
350 write (idump, err=100) tag(label_dat(ix(j)))
351 write (idump, err=100) (dat(ilocsink(i),ix(j)),i=1,nptmass)
352 enddo
353 write (idump, err=100) tag(label_dat(ipmass))
354 write (idump, err=100) (dat(ilocsink(i),ipmass),i=1,nptmass)
355 write (idump, err=100) tag('h')
356 write (idump, err=100) (dat(ilocsink(i),ih),i=1,nptmass)
357
358 ! extra sink information
359 macc = 0.
360 spinx = 0.
361 spiny = 0.
362 spinz = 0.
363 write (idump, err=100) tag('hsoft')
364 write (idump, err=100) (hsoft_sink,i=1,nptmass)
365 write (idump, err=100) tag('maccreted')
366 write (idump, err=100) (macc,i=1,nptmass)
367 write (idump, err=100) tag('spinx')
368 write (idump, err=100) (spinx,i=1,nptmass)
369 write (idump, err=100) tag('spiny')
370 write (idump, err=100) (spiny,i=1,nptmass)
371 write (idump, err=100) tag('spinz')
372 write (idump, err=100) (spinz,i=1,nptmass)
373 do j = 1, 3
374 write (idump, err=100) tag(label_dat(ivx+j-1))
375 write (idump, err=100) (dat(ilocsink(i),ivx+j-1),i=1,nptmass)
376 enddo
377 endif
378
379 if (allocated(ilocsink)) deallocate(ilocsink)
380
381 if (mhd) then
382 do j=1,3
383 write (idump,err=100) tag(Blabel(j))
384 write (idump,err=100) (real(dat(i,iBfirst+j-1),kind=sing_prec),i=1, np)
385 enddo
386 endif
387
388 close(unit=idump)
389 return
390
391 100 continue
392 write(*,*) 'error whilst writing dumpfile '//trim(outfile)
393 close(unit=idump)
394
395 end subroutine write_sphdata_phantom
396
397 !--------------------------------------------------------------------
398 !+
399 ! contruct header string based on compile-time options
400 ! these are for information only (ie. not important for restarting)
401 !+
402 !--------------------------------------------------------------------
403 character(len=100) function fileident(firstchar,codestring,mhd)
404 character(len=1), intent(in) :: firstchar
405 character(len=*), intent(in), optional :: codestring
406 logical, intent(in), optional :: mhd
407 character(len=10) :: datestring, timestring, string
408 logical :: gotmhd
409 !
410 !--print date and time stamp in file header
411 !
412 call date_and_time(datestring,timestring)
413 datestring = datestring(7:8)//'/'//datestring(5:6)//'/'//datestring(1:4)
414 timestring = timestring(1:2)//':'//timestring(3:4)//':'//timestring(5:)
415
416 string = ' '
417
418 if (present(codestring)) then
419 fileident = firstchar//'T:'//trim(codestring)
420 else
421 fileident = firstchar//'T:Phantom'
422 endif
423
424 gotmhd = .false.
425 if (present(mhd)) gotmhd = mhd
426 if (gotmhd) then
427 fileident = trim(fileident)//' (mhd'//trim(string)//') : '//trim(datestring)//' '//trim(timestring)
428 else
429 fileident = trim(fileident)//' (hydro'//trim(string)//'): ' &
430 //trim(datestring)//' '//trim(timestring)
431 endif
432
433 end function fileident
434
435 !--------------------------------------------------------------------
436 !+
437 ! extract sink particle information from dat array, as these arrays
438 ! are written separately in phantom data files
439 !+
440 !--------------------------------------------------------------------
441 subroutine extract_sink_particles_from_data(ntypes,npartoftype,labeltype,np,noftype,nptmass,ntypesi,ilocsink)
442 integer, intent(in) :: ntypes,npartoftype(ntypes)
443 character(len=*), intent(in) :: labeltype(ntypes)
444 integer, intent(out) :: np,nptmass,ntypesi
445 integer(kind=int8), intent(out) :: noftype(ntypes)
446 integer, allocatable, intent(out) :: ilocsink(:)
447 integer :: i,j
448
449 np = 0
450 ntypesi = ntypes
451 noftype(:) = int(npartoftype(:),kind=8)
452 over_types: do i=1,ntypes
453 if (trim(labeltype(i))=='sink' .or. (trim(labeltype(i))=='dark matter' .and. npartoftype(i) <= 1000)) then
454 nptmass = npartoftype(i)
455 noftype(i) = 0
456 allocate(ilocsink(nptmass))
457 do j=1,nptmass
458 ilocsink(j) = np+j
459 enddo
460 ntypesi = ntypes - 1 ! do not write types after this
461 exit over_types
462 else
463 np = np + npartoftype(i)
464 endif
465 enddo over_types
466 if (nptmass > 0) print "(/,a,i2,a)",' WRITING ',nptmass,' SINK PARTICLES'
467
468 end subroutine extract_sink_particles_from_data
469
470 end module write_data_phantom