Debian Codegraph @

src/partutils.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 !
25 ! utility routines to do with particle identification
26 !
27 !-----------------------------------------------------------
28 module part_utils
29 use params, only:int1
30 implicit none
31
32 public :: igettype,get_tracked_particle,get_itrackpart
33 public :: locate_nth_particle_of_type
34 public :: locate_first_two_of_type
35 public :: locate_particle_from_string
36 public :: get_binary,got_particles_of_type
37 public :: get_positions_of_type,is_trackstring
38 private
39
40 contains
41
42 !---------------------------------------------
43 ! utility returning the type of particle i
44 ! when particles are ordered by type
45 !---------------------------------------------
46 pure integer function igettype(i,noftype)
47 use params, only:maxparttypes
48 integer, intent(in) :: i
49 integer, dimension(maxparttypes), intent(in) :: noftype
50 integer :: ntot,ntot1,jtype
51
52 ntot = 0
53 igettype = 1 ! so even if in error, will not lead to seg fault
54 over_types: do jtype=1,maxparttypes
55 ntot1 = ntot + noftype(jtype)
56 if (i > ntot .and. i <= ntot1) then
57 igettype = jtype
58 exit over_types
59 endif
60 ntot = ntot1
61 enddo over_types
62
63 end function igettype
64
65 !-------------------------------------------------------------------
66 ! routine to find which particle is being tracked, when it is
67 ! given in the form of type:offset
68 !-------------------------------------------------------------------
69 integer function get_tracked_particle(string,noftype,iamtype,dat,irho)
70 character(len=*), intent(in) :: string
71 integer, dimension(:), intent(in) :: noftype
72 integer(kind=int1), dimension(:), intent(in) :: iamtype
73 integer, intent(in) :: irho
74 real, dimension(:,:), intent(in) :: dat
75 integer :: ntot,itype,ioffset,ierr
76
77 call get_itrackpart(string,itype,ioffset,ierr)
78 if ((itype <= 0 .or. itype > size(noftype)) .and. ioffset > 0) then
79 !--type not set, itrackpart = itrackoffset
80 get_tracked_particle = ioffset
81 elseif (ierr == 0 .and. ioffset > 0) then
82 !--want to select nth particle of a particular type
83 call locate_nth_particle_of_type(ioffset,get_tracked_particle, &
84 itype,iamtype,noftype,ntot)
85 elseif (ierr == 0) then
86 ntot = sum(noftype)
87 get_tracked_particle = locate_particle_from_string(string,ntot,dat,irho)
88 else
89 get_tracked_particle = 0
90 endif
91
92 end function get_tracked_particle
93
94 !-------------------------------------------------------------------
95 ! routine to find which particle is being tracked, when it is
96 ! given in the form of type:offset
97 !-------------------------------------------------------------------
98 subroutine get_itrackpart(string,itracktype,itrackpart,ierr)
99 character(len=*), intent(in) :: string
100 integer, intent(out) :: itracktype,itrackpart,ierr
101 integer :: ic
102
103 ic = index(string,':')
104 if (ic > 0) then
105 read(string(1:ic-1),*,iostat=ierr) itracktype
106 read(string(ic+1:),*,iostat=ierr) itrackpart
107 if (itrackpart==0) itracktype = 0
108 else
109 itracktype = 0
110 read(string,*,iostat=ierr) itrackpart
111 if (itrackpart < 0 .or. ierr /= 0) itrackpart = 0
112 ! return ierr = 0 if the string is not a particle id but is still valid
113 ! e.g. string='maxdens'
114 if (ierr /= 0 .and. is_trackstring(string)) ierr = 0
115 endif
116
117 end subroutine get_itrackpart
118
119 !-------------------------------------------------------------------
120 ! routine to locate first two particles of a given type in the data
121 !-------------------------------------------------------------------
122 subroutine locate_first_two_of_type(i1,i2,itype,iamtype,noftype,ntot)
123 integer, intent(out) :: i1,i2,ntot
124 integer, intent(in) :: itype
125 integer(kind=int1), dimension(:), intent(in) :: iamtype
126 integer, dimension(:), intent(in) :: noftype
127 integer :: i,nfound
128
129 !--locate first two sink particles in the data
130 ntot = sum(noftype)
131 if (size(iamtype(:))==1) then
132 i1 = sum(noftype(1:itype-1)) + 1
133 i2 = i1 + 1
134 else
135 i1 = 0
136 i2 = 0
137 i = 0
138 nfound = 0
139 do while ((i1==0 .or. i2==0) .and. i <= ntot)
140 i = i + 1
141 if (iamtype(i)==itype) nfound = nfound + 1
142 if (nfound==1) i1 = i
143 if (nfound==2) i2 = i
144 enddo
145 endif
146
147 end subroutine locate_first_two_of_type
148
149 !-------------------------------------------------------------
150 ! routine to locate nth particle of a given type in the data
151 !-------------------------------------------------------------
152 pure subroutine locate_nth_particle_of_type(n,ipos,itype,iamtype,noftype,ntot)
153 integer, intent(out) :: ipos,ntot
154 integer, intent(in) :: n,itype
155 integer(kind=int1), dimension(:), intent(in) :: iamtype
156 integer, dimension(:), intent(in) :: noftype
157 integer :: i,nfound
158
159 ntot = sum(noftype)
160 if (size(iamtype(:))==1) then
161 ipos = sum(noftype(1:itype-1)) + n
162 else
163 ipos = 0
164 i = 0
165 nfound = 0
166 do while (ipos==0 .and. i < ntot)
167 i = i + 1
168 if (iamtype(i)==itype) nfound = nfound + 1
169 if (nfound==n) ipos = i
170 enddo
171 endif
172
173 end subroutine locate_nth_particle_of_type
174
175 !-------------------------------------------------------------
176 ! locate the particle corresponding to various strings
177 ! e.g. maxdens = particle of maximum density
178 !-------------------------------------------------------------
179 pure integer function locate_particle_from_string(string,ntot,dat,irho) result(ipos)
180 character(len=*), intent(in) :: string
181 integer, intent(in) :: ntot
182 real, intent(in) :: dat(:,:)
183 integer, intent(in) :: irho
184 integer :: ipos_tmp(1),ntoti
185
186 ipos = 0
187 ipos_tmp = 0
188 ntoti = min(ntot,size(dat(:,1))) ! prevent bounds overflow
189 select case(string(1:7))
190 case('maxdens')
191 if (irho > 0 .and. irho <= size(dat(1,:))) ipos_tmp = maxloc(dat(1:ntoti,irho))
192 ipos = ipos_tmp(1)
193 end select
194
195 end function locate_particle_from_string
196
197 !-------------------------------------------------------------
198 ! validate strings for function above
199 !-------------------------------------------------------------
200 logical function is_trackstring(string)
201 character(len=*), intent(in) :: string
202
203 select case(string(1:7))
204 case('maxdens')
205 is_trackstring = .true.
206 case default
207 is_trackstring = .false.
208 end select
209
210 end function is_trackstring
211
212 !-------------------------------------------------------------
213 ! check if any particles of type 'mytype' exist
214 !-------------------------------------------------------------
215 pure logical function got_particles_of_type(mytype,labeltype,npartoftype)
216 character(len=*), intent(in) :: mytype,labeltype(:)
217 integer, intent(in) :: npartoftype(:,:)
218 integer :: itype
219
220 got_particles_of_type = .false.
221 do itype=1,size(npartoftype(:,1))
222 if (index(labeltype(itype),mytype) > 0) then
223 if (any(npartoftype(itype,:) > 0)) got_particles_of_type = .true.
224 endif
225 enddo
226
227 end function got_particles_of_type
228
229 !----------------------------------------------------------
230 ! routine to get properties of particle binary system
231 ! INPUT:
232 ! i1, i2 : indexes of two particles to use
233 ! dat(npart,ncolumns) : particle data
234 ! ix(ndim) : columns containing positions
235 ! ivx : column of first velocity component
236 ! ipmass : column containing mass
237 ! OUTPUT:
238 ! x0 : centre of mass position
239 ! v0 : velocity of centre of mass
240 ! angle : angle of binary about centre of mass (radians)
241 ! omega : angular velocity of binary around centre of mass
242 !----------------------------------------------------------
243 subroutine get_binary(i1,i2,dat,x0,v0,angle,omega,ndim,ndimV,ncolumns,ix,ivx,ipmass,iverbose,ierr)
244 integer, intent(in) :: i1,i2,ndim,ndimV,ncolumns,ivx,ipmass,iverbose
245 integer, dimension(ndim), intent(in) :: ix
246 real, dimension(:,:), intent(in) :: dat
247 real, dimension(ndim), intent(out) :: x0,v0
248 real, intent(out) :: angle,omega
249 integer, intent(out) :: ierr
250 integer :: max
251 real, dimension(ndim) :: x1,x2,v1,v2,dx,dv
252 real :: m1,m2,dmtot,dR2
253 real, parameter :: pi = 4.*atan(1.)
254
255 ierr = 0
256 max = size(dat(:,1))
257 if (i1 <= 0 .or. i2 <= 0 .or. i1 > max .or. i2 > max) then
258 if (iverbose >= 2) print*,' star 1 = ',i1,' star 2 = ',i2
259 print "(a)",' ERROR locating sink particles in the data'
260 ierr = 1
261 return
262 endif
263
264 x1 = 0.
265 x2 = 0.
266 x1(1:ndim) = dat(i1,ix(1:ndim))
267 x2(1:ndim) = dat(i2,ix(1:ndim))
268 !--get centre of mass
269 if (ipmass > 0 .and. ipmass <= ncolumns) then
270 m1 = dat(i1,ipmass)
271 m2 = dat(i2,ipmass)
272 else
273 m1 = 1.
274 m2 = 1.
275 endif
276 dmtot = 1./(m1 + m2)
277 x0 = (m1*x1 + m2*x2)*dmtot
278 if (iverbose >= 1) then
279 print "(a,3(1x,es10.3),a,es10.3)",' :: star 1 pos =',x1(1:ndim),' m = ',m1
280 print "(a,3(1x,es10.3),a,es10.3)",' :: star 2 pos =',x2(1:ndim),' m = ',m2
281 print "(a,3(1x,es10.3))",' :: c. of mass =',x0(1:ndim)
282 endif
283 !--work out angle needed to rotate into corotating frame
284 dx = x0 - x1
285 angle = -atan2(dx(2),dx(1))
286
287 !--get velocities
288 if (ivx > 0 .and. ivx + ndimV <= ncolumns) then
289 v1 = dat(i1,ivx:ivx+ndimV-1)
290 v2 = dat(i2,ivx:ivx+ndimV-1)
291 v0 = (m1*v1 + m2*v2)*dmtot
292 dv = v2 - v0
293 dx = x2 - x0
294 dR2 = 1./dot_product(dx,dx)
295 omega = dv(1)*(-dx(2)*dR2) + dv(2)*(dx(1)*dR2)
296 if (iverbose >= 1) print "(a,3(1x,es10.3))",' :: vel c of m =',v0(1:ndimV)
297 if (iverbose >= 1) print "(a,1x,es10.3,a,g12.4,a)",' :: omega =',omega,&
298 ' angle =',-angle*180./pi,' degrees'
299 else
300 v0 = 0.
301 omega = 0.
302 endif
303
304 end subroutine get_binary
305
306 !----------------------------------------------------------
307 ! routine to extract positions of sink particles
308 ! INPUT:
309 ! dat(npart,ncolumns) : particle data
310 ! ix(3) : columns containing positions
311 ! itype : type of each particle
312 ! ntypes : number of particle types
313 ! npartoftype : number of particles of each type
314 ! OUTPUT:
315 ! nsinks : number of sink particles
316 ! xpts : x positions of sink particles
317 ! ypts : y positions of sink particles
318 ! zpts : z positions of sink particles
319 ! ierr : error code, ierr=0 means no error was encountered
320 !----------------------------------------------------------
321 subroutine get_positions_of_type(dat,npartoftype,itype,iget_type,ix,n,xpts,ypts,zpts,ierr)
322 integer, intent(in) :: npartoftype(:),ix(3),iget_type
323 integer(kind=int1), intent(in) :: itype(:)
324 real, intent(in) :: dat(:,:)
325 integer, intent(out) :: n,ierr
326 real, intent(out), allocatable :: xpts(:),ypts(:),zpts(:)
327 integer :: i,j
328 integer, allocatable :: ilist(:)
329
330 ierr = 0
331 n = 0
332 if (iget_type > 0) n = npartoftype(iget_type)
333
334 allocate(xpts(n),ypts(n),zpts(n),ilist(n),stat=ierr)
335 if (ierr /= 0) then
336 print*,' ERROR allocating memory for sink particle positions, aborting...'
337 return
338 endif
339 j = 0
340 do i=1,sum(npartoftype)
341 if (itype(i)==iget_type) then
342 j = j + 1
343 ilist(j) = i
344 endif
345 enddo
346 if (j /= n) print*,' WARNING: found ',j,' particles but expecting ',n
347 if (j < n) n = j
348
349 xpts = dat(ilist(1:n),ix(1))
350 ypts = dat(ilist(1:n),ix(2))
351 zpts = dat(ilist(1:n),ix(3))
352
353 deallocate(ilist)
354
355 end subroutine get_positions_of_type
356
357 end module part_utils