Debian Codegraph @

src/write_data_gadget.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 ! Module implementing "splash to gadget" operation, writing
25 ! a binary dump file suitable for input to the GADGET code
26 !-----------------------------------------------------------------
27 module write_data_gadget
28 implicit none
29 character(len=10), parameter, public :: formatname='gadget'
30
31 public :: write_sphdata_gadget
32 private
33
34 contains
35
36 subroutine write_sphdata_gadget(time,dat,iamtype,ntotal,ntypes,npartoftype, &
37 masstype,ncolumns,filename)
38 use labels, only:ih,ivx,ix,iutherm,irho,ipmass
39 use settings_data, only:ndim
40 use limits, only:lim
41 use params, only:int1
42 integer, intent(in) :: ntotal,ntypes,ncolumns
43 integer, intent(in), dimension(:) :: npartoftype
44 real, intent(in) :: time
45 real, intent(in), dimension(ntotal,ncolumns) :: dat
46 integer(kind=int1), intent(in), dimension(:) :: iamtype
47 real, intent(in), dimension(:) :: masstype
48 character(len=*), intent(in) :: filename
49
50 integer, parameter :: idump = 83
51 character(len=len(filename)+10) :: outfile
52 integer, dimension(6) :: nall,ncrap,noftype
53 real(kind=8), dimension(6) :: massoftype
54 real(kind=8) :: boxsize,dtime
55 real(kind=8), parameter :: dumz = 0.d0
56 real(kind=4), dimension(15) :: unused
57 integer, parameter :: iflagsfr = 0, iflagfeedback = 0, iflagcool = 0
58 integer, parameter :: nfiles = 1
59 integer :: ierr,i,j,nmasses,ngas,itype
60 integer, dimension(:), allocatable :: iorder
61 !
62 !--define output file name
63 !
64 outfile=trim(filename)//'.gadget'
65 !
66 !--check if we have enough data to write a GADGET dump
67 !
68 if (ndim < 3) then
69 print "(a)",' ERROR: ndim < 3 but must be 3 for GADGET data -- cannot write PHANTOM dump, skipping...'
70 return
71 endif
72 if (any(ix(:) <= 0)) then
73 print "(a)",' ERROR: position labels not set -- cannot write GADGET dump, skipping...'
74 return
75 endif
76 if (ivx <= 0) then
77 print "(a)",' ERROR: velocity not found in data -- cannot write GADGET dump, skipping...'
78 return
79 endif
80 if (iutherm <= 0) then
81 print "(a)",' ERROR: thermal energy not found in data -- cannot write GADGET dump, skipping...'
82 return
83 endif
84 if (irho <= 0) then
85 print "(a)",' ERROR: density not found in data -- cannot write GADGET dump, skipping...'
86 return
87 endif
88 if (ih <= 0) then
89 print "(a)",' ERROR: smoothing length not found in data -- cannot write GADGET dump, skipping...'
90 return
91 endif
92 !
93 !--open dumpfile
94 !
95 write(*,"(/,/,'--------> TIME = ',f10.4,"// &
96 "': writing GADGET snapshot file ',a,' <--------',/)") time,trim(outfile)
97
98 print "(a)", ' WARNING: conversion to GADGET format is LIMITED in scope...'
99 print "(a)", ' Currently converts only basic hydro quantities (x,v,m,u,rho,h) '
100 print "(a)", ' and header quantities may be guessed/fudged ...your mileage may vary'
101 print "(a,/)",' (if you use this functionality and want it improved, just let me know)'
102
103 print "(a,i2)",' writing to unit ',idump
104 open(unit=idump,file=outfile,status='replace',form='unformatted',iostat=ierr)
105 if (ierr /= 0) then
106 write(*,*) 'error: can''t create new dumpfile ',trim(outfile)
107 return
108 endif
109
110 massoftype(:) = 0.
111 nall(:) = 0
112 noftype(:) = 0
113 massoftype(1:ntypes) = masstype(1:ntypes)
114 nall(1:ntypes) = npartoftype(1:ntypes)
115 noftype(1:ntypes) = npartoftype(1:ntypes)
116 ncrap(:) = 0
117 boxsize = abs(lim(ix(1),2) - lim(ix(1),1))
118 unused(:) = 0
119 dtime = time
120
121 write(idump,err=100) noftype(1:6),massoftype(1:6),dtime,dumz, &
122 iflagsfr,iflagfeedback,nall(1:6),iflagcool,nfiles,boxsize, &
123 dumz,dumz,dumz,iflagsfr,iflagsfr,ncrap(1:6),iflagsfr,unused(:)
124 !
125 !--work out how many particle masses to write
126 !
127 nmasses = 0
128 do j=1,6
129 if (massoftype(j) <= 0.) then
130 nmasses = nmasses + noftype(j)
131 endif
132 enddo
133 print*,'nmasses = ',nmasses
134 ngas = npartoftype(1)
135 print*,'ngas = ',ngas
136
137 if (ntotal > ngas .and. (size(iamtype) > 1)) then
138 !--must print the particles ordered by type
139 allocate(iorder(ntotal),stat=ierr)
140 j = 0
141 do itype=1,min(ntypes,6)
142 if (npartoftype(itype) > 0) then
143 do i=1,ntotal
144 if (iamtype(i)==itype) then
145 j = j + 1
146 iorder(j) = i
147 endif
148 enddo
149 endif
150 enddo
151 if (j < ntotal) then
152 print*,' ERROR: too many particle types in conversion to gadget format'
153 do i=j+1,ntotal
154 iorder(i) = i
155 enddo
156 endif
157 write(idump,err=100) ((dat(iorder(i),ix(j)),j=1,3),i=1,ntotal)
158 write(idump,err=100) ((dat(iorder(i),j),j=ivx,ivx+2),i=1,ntotal)
159 write(idump,err=100) (iorder(i),i=1,ntotal) ! particle id
160 write(idump,err=100) (dat(iorder(i),ipmass), i=1,nmasses)
161 write(idump,err=100) (dat(iorder(i),iutherm),i=1,ngas)
162 write(idump,err=100) (dat(iorder(i),irho), i=1,ngas)
163 write(idump,err=100) (2.*dat(iorder(i),ih), i=1,ngas)
164 deallocate(iorder)
165 else
166 write(idump,err=100) ((dat(i,ix(j)),j=1,3),i=1,ntotal)
167 write(idump,err=100) ((dat(i,j),j=ivx,ivx+2),i=1,ntotal)
168 write(idump,err=100) (i,i=1,ntotal) ! particle id
169 write(idump,err=100) (dat(i,ipmass), i=1,nmasses)
170 write(idump,err=100) (dat(i,iutherm),i=1,ngas)
171 write(idump,err=100) (dat(i,irho), i=1,ngas)
172 write(idump,err=100) (2.*dat(i,ih), i=1,ngas)
173 endif
174
175 print*,'finished writing file -- OK'
176
177 close(unit=idump)
178 return
179
180 100 continue
181 write(*,*) 'error whilst writing dumpfile '//trim(outfile)
182 close(unit=idump)
183
184 end subroutine write_sphdata_gadget
185
186 end module write_data_gadget