Debian Codegraph @

src/read_composition.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-2025 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------!-----------------------------------------------------------------
22 !
23 ! This file is for reading the composition file for each particle
24 ! it also performs a check to see if composition file exists or not
25 !
26 !-----------------------------------------------------------------
27 module readcomposition
28 implicit none
29
30 public :: check_for_composition_file,read_composition
31
32 private
33
34 contains
35
36 ! This function returns the prefix of the filename
37 function get_prefix(filename) result(prefix)
38 character(len=*), intent(in) :: filename
39 character(len=len(filename)) :: prefix
40
41 integer :: iu
42
43 iu = index(filename,'_',back=.true.)
44 if (iu > 1) then
45 prefix = filename(1:iu-1)
46 else
47 prefix = filename
48 endif
49
50 end function get_prefix
51
52 subroutine check_for_composition_file(dumpfile,ntotal,ncolstep,icomp_col_start,ncomp,labels,filename)
53 use asciiutils, only:get_ncolumns,get_nrows,read_column_labels,basename
54 use settings_data, only:debugmode
55 character(len=*), intent(in) :: dumpfile
56 integer, intent(in) :: ntotal
57 integer, intent(inout) :: ncolstep
58 character(len=*), intent(inout) :: labels(:)
59 integer, intent(out) :: ncomp,icomp_col_start
60 character(len=*), intent(out) :: filename
61
62 integer :: iu,nrows,nheaderlines,nlabels,ierr
63 character(len=len(dumpfile)) :: prefix
64
65 logical :: iexist
66
67 ncomp = 0
68 icomp_col_start = 0
69
70 ! first see if file_00000.cols exists
71 filename = trim(dumpfile)//'.cols'
72 inquire(file=filename,exist=iexist)
73 if (debugmode) print*,' DEBUG: looking for '//trim(filename)//' exist = ',iexist
74
75 ! then see if a local file_00000.comp file exists
76 if (.not.iexist) then
77 filename = trim(dumpfile)//'.comp'
78 inquire(file=filename,exist=iexist)
79 if (debugmode) print*,' DEBUG: looking for '//trim(filename)//' exist = ',iexist
80 endif
81
82 ! or see if a global file.comp file exists
83 if (.not.iexist) then
84 prefix = get_prefix(dumpfile)
85 filename = trim(prefix)//'.comp'
86 inquire(file=filename,exist=iexist)
87 if (debugmode) print*,' DEBUG: looking for '//trim(filename)//' exist = ',iexist
88 endif
89
90 ! look for a file in the current directory, not the same directory as the data
91 if (.not.iexist) then
92 filename = basename(filename)
93 inquire(file=filename,exist=iexist)
94 if (debugmode) print*,' DEBUG: looking for '//trim(filename)//' exist = ',iexist
95 endif
96
97 ! look for an AV_00100 file to match the dumpfile number
98 if (.not.iexist) then
99 filename = get_av_filename(dumpfile)
100 inquire(file=filename,exist=iexist)
101 if (debugmode) print*,' DEBUG: looking for '//trim(filename)//' exist = ',iexist
102 if (iexist) then
103 icomp_col_start = ncolstep + 1
104 ncolstep = ncolstep + 1
105 ncomp = ncomp + 1
106 labels(icomp_col_start) = 'A_V'
107 print "(a,i0,a)", '> got ',ncomp,' extra columns from '//trim(filename)
108 return
109 endif
110 endif
111
112 !if (debugmode) print*,'DEBUG: looking for '//trim(filename)//' exist = ',iexist
113 if (.not.iexist) return
114
115 ! see if Kepler composition file exists
116 open(newunit=iu,file=filename,iostat=ierr,status='old')
117 if (ierr /= 0) then
118 print "(1x,a)", 'ERROR opening '//trim(filename)
119 endif
120
121 if (ierr == 0) then
122 call get_ncolumns(iu,ncomp,nheaderlines)
123 call get_nrows(iu,nheaderlines,nrows)
124 ! check nrows equals number of particles
125 if (nrows /= ntotal) then
126 print "(1x,a,i0,a,i0,a)",'ERROR: reading '//trim(filename)//' nrows (',nrows,') /= nparticles (',ntotal,')'
127 return
128 endif
129 if (ncomp > 0) then
130 icomp_col_start = ncolstep + 1
131 ncolstep = ncolstep + ncomp
132 call read_column_labels(iu,nheaderlines,ncomp,nlabels,&
133 labels(icomp_col_start:icomp_col_start+ncomp-1))
134 print "(a,i0,a)", '> got ',ncomp,' extra columns from '//trim(filename)
135 endif
136 endif
137 close(iu)
138
139 end subroutine check_for_composition_file
140
141 subroutine read_composition(filename,ntotal,dat,icomp_col_start,ncomp,iorig)
142 use asciiutils, only:get_ncolumns
143 use iso_fortran_env, only:int64
144 real, intent(inout) :: dat(:,:)
145 character(len=*), intent(in) :: filename
146 integer, intent(in) :: ncomp,icomp_col_start
147 integer, intent(inout) :: ntotal
148 integer(kind=int64), allocatable, intent(in) :: iorig(:)
149 integer :: iu,ierr,i,ncols,nhdr
150
151 open(newunit=iu,file=trim(filename),form='formatted',status='old',iostat=ierr)
152 if (ierr /= 0) then
153 print "(a)",' ERROR opening '//trim(filename)
154 elseif (icomp_col_start+ncomp-1 <= size(dat(1,:))) then
155 if (index(filename,'AV_') > 0) then
156 call read_av_into_column(filename,ntotal,dat,icomp_col_start,iorig)
157 else
158 ! get number of columns
159 call get_ncolumns(iu,ncols,nhdr)
160 ! skip header lines
161 do i=1,nhdr
162 read(iu,*,iostat=ierr)
163 enddo
164 ! read data from file
165 do i=1,ntotal
166 read(iu,*,iostat=ierr) dat(i,icomp_col_start:icomp_col_start+ncomp-1)
167 if (ierr /= 0) print*,' ERROR reading '//trim(filename)//' on line ',i+nhdr
168 enddo
169 endif
170 else
171 print "(a)",' ERROR: wrong number of columns in '//trim(filename)//'.comp'
172 endif
173
174 end subroutine read_composition
175
176 !-----------------------------------------------------------------
177 ! This function returns the filename of the AV file
178 ! for the given dumpfile, e.g. dump_00700 -> AV_00700
179 !-----------------------------------------------------------------
180 function get_av_filename(dumpfile) result(avfile)
181 character(len=*), intent(in) :: dumpfile
182 character(len=len(dumpfile)+4) :: avfile
183 integer :: i,islash
184
185 i = index(trim(dumpfile), '_', back=.true.)
186 avfile = ''
187 if (i > 0 .and. i < len_trim(dumpfile)) then
188 islash = index(trim(dumpfile), '/', back=.true.)
189 if (islash > 0) then
190 avfile = dumpfile(1:islash)//'AV_'//dumpfile(i+1:len_trim(dumpfile))
191 else
192 avfile = 'AV_'//dumpfile(i+1:len_trim(dumpfile))
193 endif
194 endif
195
196 end function get_av_filename
197
198 !-----------------------------------------------------------------
199 ! Read the AV file (stream unformatted: npart_av, id(:), val(:)) into one column.
200 ! id_tmp in the file are original particle IDs. If iorig is allocated, match by
201 ! iorig(particle_index); otherwise id_tmp is treated as dump index 1..ntotal.
202 !-----------------------------------------------------------------
203 subroutine read_av_into_column(filename,ntotal,dat,icol,iorig)
204 use iso_fortran_env, only:int64
205 use params, only:doub_prec
206 real, intent(inout) :: dat(:,:)
207 character(len=*), intent(in) :: filename
208 integer, intent(in) :: icol
209 integer, intent(in) :: ntotal
210 integer(kind=int64), allocatable, intent(in) :: iorig(:)
211 integer :: iu,ierr,npart_av,k,max_id,orig_id
212 integer, allocatable :: id_tmp(:)
213 real(doub_prec), allocatable :: val_tmp(:), lookup(:)
214
215 dat(1:ntotal,icol) = 0.0
216 open(newunit=iu,file=trim(filename),access='stream',form='unformatted',status='old',iostat=ierr)
217 if (ierr /= 0) then
218 print*, "WARNING: Could not open A_V file "//trim(filename)//", using A_V = 0"
219 return
220 endif
221 read(iu,iostat=ierr) npart_av
222 if (ierr /= 0 .or. npart_av <= 0) then
223 print*, "WARNING: Invalid A_V header in "//trim(filename)//", using A_V = 0"
224 close(iu)
225 return
226 endif
227 allocate(id_tmp(npart_av), val_tmp(npart_av))
228 read(iu,iostat=ierr) id_tmp
229 read(iu,iostat=ierr) val_tmp
230 close(iu)
231 if (ierr /= 0) then
232 print*, "WARNING: Error reading A_V data from "//trim(filename)//", using A_V = 0"
233 deallocate(id_tmp, val_tmp)
234 return
235 endif
236 max_id = maxval(id_tmp(1:npart_av))
237 if (max_id < 1) then
238 deallocate(id_tmp, val_tmp)
239 return
240 endif
241 allocate(lookup(max_id))
242 lookup = 0.0d0
243 do k = 1, npart_av
244 if (id_tmp(k) >= 1 .and. id_tmp(k) <= max_id) then
245 lookup(id_tmp(k)) = val_tmp(k)
246 endif
247 enddo
248 if (allocated(iorig) .and. size(iorig) >= ntotal) then
249 print "(a,i0,a,i0,a)", ' Loaded A_V for ',npart_av,' particles from '//&
250 trim(filename)//' using ',max_id,' unique IDs'
251 do k = 1, ntotal
252 orig_id = int(iorig(k), kind(orig_id))
253 if (orig_id >= 1 .and. orig_id <= max_id) then
254 dat(k,icol) = real(lookup(orig_id))
255 endif
256 enddo
257 else
258 print "(a,i0,a)", ' Loaded A_V for ',npart_av,' particles from '// &
259 trim(filename)//' (no iorig found in dump)'
260 do k = 1, ntotal
261 if (k <= max_id) dat(k,icol) = real(lookup(k))
262 enddo
263 endif
264 deallocate(id_tmp, val_tmp, lookup)
265 end subroutine read_av_into_column
266
267 end module readcomposition