Debian Codegraph @

src/interpolate3D_geom.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 !
25 ! Module containing all of the routines required for interpolation
26 ! from 3D data to a 3D grid in arbitrary coordinate systems (SLOW!)
27 !
28 !----------------------------------------------------------------------
29
30 module interpolations3Dgeom
31 use kernels, only:radkernel2,radkernel,cnormk3D
32 use geometry, only:labelcoordsys,coord_is_length,igeom_cartesian,coord_transform
33 use interpolation, only:doub_prec,iroll
34 implicit none
35 public :: interpolate3Dgeom,interpolate3Dgeom_vec
36
37 contains
38 !--------------------------------------------------------------------------
39 ! subroutine to interpolate from particle data to even grid of pixels
40 !
41 ! The data is interpolated according to the formula
42 !
43 ! datsmooth(pixel) = sum_b weight_b dat_b W(r-r_b, h_b)
44 !
45 ! where _b is the quantity at the neighbouring particle b and
46 ! W is the smoothing kernel, for which we use the usual cubic spline.
47 !
48 ! For a standard SPH smoothing the weight function for each particle should be
49 !
50 ! weight = pmass/(rho*h^3)
51 !
52 ! this version is written for slices through a rectangular volume, ie.
53 ! assumes a uniform pixel size in x,y, whilst the number of pixels
54 ! in the z direction can be set to the number of cross-section slices.
55 !
56 ! Input: particle coordinates : x,y,z (npart)
57 ! smoothing lengths : hh (npart)
58 ! weight for each particle : weight (npart)
59 ! scalar data to smooth : dat (npart)
60 !
61 ! Output: smoothed data : datsmooth (npix(1),npix(2),npix(3))
62 !
63 ! Daniel Price, Institute of Astronomy, Cambridge 16/7/03
64 ! Revised for "splash to grid", Monash University 02/11/09
65 ! Version for arbitrary coordinate systems 02/05/18
66 !--------------------------------------------------------------------------
67
68 subroutine interpolate3Dgeom(igeom,x,y,z,hh,weight,dat,itype,npart,&
69 xmin,datsmooth,npix,pixwidth,xorigin,normalise,periodic)
70 integer, intent(in) :: igeom,npart,npix(3)
71 real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat
72 integer, intent(in), dimension(npart) :: itype
73 real, intent(in) :: xmin(3),pixwidth(3),xorigin(3)
74 real(doub_prec), intent(out), dimension(npix(1),npix(2),npix(3)) :: datsmooth
75 logical, intent(in) :: normalise,periodic(3)
76 real(doub_prec), dimension(npix(1),npix(2),npix(3)) :: datnorm
77
78 integer :: i,ipix,jpix,kpix,ierr
79 integer :: iprintinterval,iprintnext
80 integer :: ipixmin(3),ipixmax(3)
81 integer :: ipixi,jpixi,kpixi,nsub,isub,jsub,ksub,nsubpix
82 integer :: maxsubpix,meansubpix,ncount
83 real :: xminpix(3),hmin,dsubpix(3)
84 real :: xi(3),xci(3),xcoord(3),hi,hi1,hi21,radkern,wab,q2,const
85 real :: term,termnorm,xpix(3),dx(3)
86 !real :: t_start,t_end
87 logical :: iprintprogress
88 !$ integer :: omp_get_num_threads
89 integer(kind=selected_int_kind(10)) :: iprogress ! up to 10 digits
90
91 datsmooth = 0.
92 datnorm = 0.
93 if (normalise) then
94 print "(1x,a)",'interpolating from particles to 3D '//trim(labelcoordsys(igeom))//' grid (normalised) ...'
95 else
96 print "(1x,a)",'interpolating from particles to 3D '//trim(labelcoordsys(igeom))//' grid (non-normalised) ...'
97 endif
98 if (any(pixwidth <= 0.)) then
99 print "(1x,a)",'interpolate3D: error: pixel width <= 0'
100 return
101 endif
102 if (any(hh(1:npart) <= tiny(hh))) then
103 print*,'interpolate3D: WARNING: ignoring some or all particles with h < 0'
104 endif
105
106 !
107 !--print a progress report if it is going to take a long time
108 ! (a "long time" is, however, somewhat system dependent)
109 !
110 iprintprogress = (npart >= 100000) .or. (npix(1)*npix(2) > 100000)
111 !$ iprintprogress = .false.
112 !
113 !--loop over particles
114 !
115 iprintinterval = 25
116 if (npart >= 1e6) iprintinterval = 10
117 iprintnext = iprintinterval
118 !
119 !--get starting CPU time
120 !
121 !call cpu_time(t_start)
122
123 xminpix(:) = xmin(:) - 0.5*pixwidth(:)
124 !
125 !--use a minimum smoothing length on the grid to make
126 ! sure that particles contribute to at least one pixel
127 !
128 hmin = 0.
129 do i=1,3
130 if (coord_is_length(i,igeom)) hmin = max(hmin,0.5*pixwidth(i))
131 enddo
132
133 const = cnormk3D ! normalisation constant (3D)
134 nsub = 0; ncount = 0; maxsubpix = 0; meansubpix = 0
135 !
136 !--loop over particles
137 !
138 !$omp parallel default(none) &
139 !$omp shared(hh,z,x,y,weight,dat,itype,datsmooth,npart) &
140 !$omp shared(xmin,radkernel,radkernel2) &
141 !$omp shared(xminpix,pixwidth,xorigin) &
142 !$omp shared(npix,const,igeom) &
143 !$omp shared(datnorm,normalise,periodic) &
144 !$omp shared(iprintprogress,iprintinterval) &
145 !$omp shared(hmin) &
146 !$omp private(hi,xi,xci,xcoord,xpix,radkern,hi1,hi21) &
147 !$omp private(term,termnorm) &
148 !$omp private(iprogress) &
149 !$omp firstprivate(iprintnext) &
150 !$omp private(ipixmin,ipixmax,ierr) &
151 !$omp private(ipix,jpix,kpix,ipixi,jpixi,kpixi) &
152 !$omp private(dx,q2,wab,isub,jsub,ksub,nsubpix,dsubpix) &
153 !$omp reduction(+:nsub,meansubpix,ncount) &
154 !$omp reduction(max:maxsubpix)
155 !$omp master
156 !$ print "(1x,a,i3,a)",'Using ',omp_get_num_threads(),' cpus'
157 !$omp end master
158
159 !$omp do schedule (guided, 2)
160 over_parts: do i=1,npart
161 !
162 !--report on progress
163 !
164 if (iprintprogress) then
165 iprogress = 100*i/npart
166 if (iprogress >= iprintnext) then
167 write(*,"('(',i3,'% -',i12,' particles done)')") iprogress,i
168 iprintnext = iprintnext + iprintinterval
169 endif
170 endif
171 !
172 !--skip particles with itype < 0
173 !
174 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts
175
176 hi = hh(i)
177 if (hi <= 0.) cycle over_parts
178 hi1 = 1./hi
179 hi21 = hi1*hi1
180 radkern = radkernel*hi ! radius of the smoothing kernel
181 termnorm = const*weight(i)
182 term = termnorm*dat(i)
183 !
184 !--set kernel related quantities
185 !
186 xci(1) = x(i) + xorigin(1) ! xci = position in cartesians
187 xci(2) = y(i) + xorigin(2)
188 xci(3) = z(i) + xorigin(3)
189 call get_pixel_limits(xci,xi,radkern,ipixmin,ipixmax,npix,pixwidth,xmin,periodic,igeom,ierr)
190 if (ierr /= 0) cycle over_parts
191
192 if (hi < hmin) then
193 nsub = nsub + 1
194 nsubpix = max(2, int(hmin/hi) + 1)
195 else
196 nsubpix = 1
197 endif
198 ! statistics on sub-pixel interpolation
199 maxsubpix = max(maxsubpix,nsubpix)
200 meansubpix = meansubpix + nsubpix
201 ncount = ncount + 1
202
203 dsubpix(:) = pixwidth(:) / real(nsubpix)
204 !
205 !--loop over pixels, adding the contribution from this particle
206 ! When nsubpix > 1 we sub-divide each pixel and average the kernel
207 ! so that small kernels still contribute to the coarse grid.
208 !
209 do kpix = ipixmin(3),ipixmax(3)
210 kpixi = kpix
211 if (periodic(3)) kpixi = iroll(kpix,npix(3))
212
213 do jpix = ipixmin(2),ipixmax(2)
214 jpixi = jpix
215 if (periodic(2)) jpixi = iroll(jpix,npix(2))
216
217 do ipix = ipixmin(1),ipixmax(1)
218 ipixi = ipix
219 if (periodic(1)) ipixi = iroll(ipix,npix(1))
220
221 wab = 0.
222 do ksub = 1,nsubpix
223 xcoord(3) = xmin(3) + (kpix-1)*pixwidth(3) + (ksub-0.5)*dsubpix(3)
224 do jsub = 1,nsubpix
225 xcoord(2) = xmin(2) + (jpix-1)*pixwidth(2) + (jsub-0.5)*dsubpix(2)
226 do isub = 1,nsubpix
227 xcoord(1) = xmin(1) + (ipix-1)*pixwidth(1) + (isub-0.5)*dsubpix(1)
228 call coord_transform(xcoord,3,igeom,xpix,3,igeom_cartesian)
229 dx = xpix(:) - xci(:)
230 q2 = (dx(1)*dx(1) + dx(2)*dx(2) + dx(3)*dx(3))*hi21
231 if (q2 < radkernel2) wab = wab + wkernel(q2)
232 enddo
233 enddo
234 enddo
235 wab = wab / real(nsubpix**3)
236 if (wab > 0.) then
237 !$omp atomic
238 datsmooth(ipixi,jpixi,kpixi) = datsmooth(ipixi,jpixi,kpixi) + term*wab
239 if (normalise) then
240 !$omp atomic
241 datnorm(ipixi,jpixi,kpixi) = datnorm(ipixi,jpixi,kpixi) + termnorm*wab
242 endif
243 endif
244 enddo
245 enddo
246 enddo
247 enddo over_parts
248 !$omp enddo
249 !$omp end parallel
250
251 !
252 !--normalise dat array
253 !
254 if (normalise) then
255 where (datnorm > tiny(datnorm))
256 datsmooth = datsmooth/datnorm
257 end where
258 endif
259 if (nsub > 0 .and. ncount > 0) print "(20x,a,i0,a,i0,a,f6.1/)",'particles on subgrid: ',nsub,&
260 ' max subpixels: ',maxsubpix,' mean subpixels: ',meansubpix/real(ncount)
261
262 end subroutine interpolate3Dgeom
263
264 !------------------------------------------------------------------------------
265 !
266 ! Same functionality as interpolate3Dgeom, but for vector quantities
267 !
268 !------------------------------------------------------------------------------
269 subroutine interpolate3Dgeom_vec(igeom,x,y,z,hh,weight,datvec,itype,npart,&
270 xmin,datsmooth,npix,pixwidth,xorigin,normalise,periodic)
271 integer, intent(in) :: igeom,npart,npix(3)
272 real, intent(in), dimension(npart) :: x,y,z,hh,weight
273 real, intent(in), dimension(npart,3) :: datvec
274 integer, intent(in), dimension(npart) :: itype
275 real, intent(in) :: xmin(3),pixwidth(3),xorigin(3)
276 real(doub_prec), intent(out), dimension(3,npix(1),npix(2),npix(3)) :: datsmooth
277 logical, intent(in) :: normalise,periodic(3)
278 real(doub_prec), dimension(npix(1),npix(2),npix(3)) :: datnorm
279
280 integer :: i,ipix,jpix,kpix,ierr
281 integer :: iprintinterval,iprintnext
282 integer :: ipixmin(3),ipixmax(3)
283 integer :: ipixi,jpixi,kpixi,isub,jsub,ksub,nsubpix
284 integer :: nsub,ncount,maxsubpix,meansubpix
285 real :: xminpix(3),hmin,dsubpix(3)
286 real :: xi(3),xci(3),xcoord(3),hi,hi1,hi21,radkern,wab,q2,const
287 real :: term(3),termnorm,xpix(3),dx(3),ddatnorm
288 !real :: t_start,t_end
289 logical :: iprintprogress
290 !$ integer :: omp_get_num_threads
291 integer(kind=selected_int_kind(10)) :: iprogress ! up to 10 digits
292
293 datsmooth = 0.
294 datnorm = 0.
295 if (normalise) then
296 print "(1x,a)",'interpolating to 3D '//trim(labelcoordsys(igeom))//' grid (normalised) ...'
297 else
298 print "(1x,a)",'interpolating to 3D '//trim(labelcoordsys(igeom))//' grid (non-normalised) ...'
299 endif
300 if (any(pixwidth <= 0.)) then
301 print "(1x,a)",'interpolate3D: error: pixel width <= 0'
302 return
303 endif
304 if (any(hh(1:npart) <= tiny(hh))) then
305 print*,'interpolate3D: WARNING: ignoring some or all particles with h < 0'
306 endif
307
308 !
309 !--print a progress report if it is going to take a long time
310 ! (a "long time" is, however, somewhat system dependent)
311 !
312 iprintprogress = (npart >= 100000) .or. (npix(1)*npix(2) > 100000)
313 !$ iprintprogress = .false.
314 !
315 !--loop over particles
316 !
317 iprintinterval = 25
318 if (npart >= 1e6) iprintinterval = 10
319 iprintnext = iprintinterval
320 !
321 !--get starting CPU time
322 !
323 !call cpu_time(t_start)
324
325 xminpix(:) = xmin(:) - 0.5*pixwidth(:)
326 !
327 !--use a minimum smoothing length on the grid to make
328 ! sure that particles contribute to at least one pixel
329 !
330 hmin = 0.
331 do i=1,3
332 if (coord_is_length(i,igeom)) hmin = max(hmin,0.5*pixwidth(i))
333 enddo
334
335 const = cnormk3D ! normalisation constant (3D)
336 nsub = 0; ncount = 0; maxsubpix = 0; meansubpix = 0
337 !
338 !--loop over particles
339 !
340 !$omp parallel default(none) &
341 !$omp shared(hh,z,x,y,weight,datvec,itype,datsmooth,npart) &
342 !$omp shared(xmin,radkernel,radkernel2) &
343 !$omp shared(xminpix,pixwidth,xorigin) &
344 !$omp shared(npix,const,igeom) &
345 !$omp shared(datnorm,normalise,periodic) &
346 !$omp shared(hmin) &
347 !$omp shared(iprintprogress,iprintinterval) &
348 !$omp private(hi,xi,xci,xcoord,xpix,radkern,hi1,hi21) &
349 !$omp private(term,termnorm) &
350 !$omp private(ipixmin,ipixmax,ierr) &
351 !$omp private(iprogress) &
352 !$omp firstprivate(iprintnext) &
353 !$omp private(ipix,jpix,kpix,ipixi,jpixi,kpixi) &
354 !$omp private(dx,q2,wab,isub,jsub,ksub,nsubpix,dsubpix) &
355 !$omp reduction(+:nsub,meansubpix,ncount) &
356 !$omp reduction(max:maxsubpix)
357 !$omp master
358 !$ print "(1x,a,i3,a)",'Using ',omp_get_num_threads(),' cpus'
359 !$omp end master
360
361 !$omp do schedule (guided, 2)
362 over_parts: do i=1,npart
363 !
364 !--report on progress
365 !
366 if (iprintprogress) then
367 iprogress = 100*i/npart
368 if (iprogress >= iprintnext) then
369 write(*,"('(',i3,'% -',i12,' particles done)')") iprogress,i
370 iprintnext = iprintnext + iprintinterval
371 endif
372 endif
373 !
374 !--skip particles with itype < 0
375 !
376 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts
377
378 hi = hh(i)
379 if (hi <= 0.) cycle over_parts
380 termnorm = const*weight(i)
381 hi1 = 1./hi
382 hi21 = hi1*hi1
383 radkern = radkernel*hi ! radius of the smoothing kernel
384 term(:) = termnorm*datvec(i,:)
385 !
386 !--set kernel related quantities
387 !
388 xci(1) = x(i) + xorigin(1) ! xci = position in cartesians
389 xci(2) = y(i) + xorigin(2)
390 xci(3) = z(i) + xorigin(3)
391 call get_pixel_limits(xci,xi,radkern,ipixmin,ipixmax,npix,pixwidth,xmin,periodic,igeom,ierr)
392
393 if (ierr /= 0) cycle over_parts
394
395 if (hi < hmin) then
396 nsub = nsub + 1
397 nsubpix = max(2, int(hmin/hi) + 1)
398 else
399 nsubpix = 1
400 endif
401 ! statistics on sub-pixel interpolation
402 maxsubpix = max(maxsubpix,nsubpix)
403 meansubpix = meansubpix + nsubpix
404 ncount = ncount + 1
405
406 dsubpix(:) = pixwidth(:) / real(nsubpix)
407 !
408 !--loop over pixels, adding the contribution from this particle
409 !
410 do kpix = ipixmin(3),ipixmax(3)
411 kpixi = kpix
412 if (periodic(3)) kpixi = iroll(kpix,npix(3))
413
414 do jpix = ipixmin(2),ipixmax(2)
415 jpixi = jpix
416 if (periodic(2)) jpixi = iroll(jpix,npix(2))
417
418 do ipix = ipixmin(1),ipixmax(1)
419 ipixi = ipix
420 if (periodic(1)) ipixi = iroll(ipix,npix(1))
421
422 wab = 0.
423 do ksub = 1,nsubpix
424 xcoord(3) = xmin(3) + (kpix-1)*pixwidth(3) + (ksub-0.5)*dsubpix(3)
425 do jsub = 1,nsubpix
426 xcoord(2) = xmin(2) + (jpix-1)*pixwidth(2) + (jsub-0.5)*dsubpix(2)
427 do isub = 1,nsubpix
428 xcoord(1) = xmin(1) + (ipix-1)*pixwidth(1) + (isub-0.5)*dsubpix(1)
429 call coord_transform(xcoord,3,igeom,xpix,3,igeom_cartesian)
430 dx = xpix(:) - xci(:)
431 q2 = (dx(1)*dx(1) + dx(2)*dx(2) + dx(3)*dx(3))*hi21
432 if (q2 < radkernel2) wab = wab + wkernel(q2)
433 enddo
434 enddo
435 enddo
436 wab = wab / real(nsubpix**3)
437 if (wab > 0.) then
438 !$omp atomic
439 datsmooth(1,ipixi,jpixi,kpixi) = datsmooth(1,ipixi,jpixi,kpixi) + term(1)*wab
440 !$omp atomic
441 datsmooth(2,ipixi,jpixi,kpixi) = datsmooth(2,ipixi,jpixi,kpixi) + term(2)*wab
442 !$omp atomic
443 datsmooth(3,ipixi,jpixi,kpixi) = datsmooth(3,ipixi,jpixi,kpixi) + term(3)*wab
444 if (normalise) then
445 !$omp atomic
446 datnorm(ipixi,jpixi,kpixi) = datnorm(ipixi,jpixi,kpixi) + termnorm*wab
447 endif
448 endif
449 enddo
450 enddo
451 enddo
452 enddo over_parts
453 !$omp enddo
454 !$omp end parallel
455
456 !
457 !--normalise dat array
458 !
459 if (normalise) then
460 !$omp parallel do default(none) schedule(static) &
461 !$omp shared(datsmooth,datnorm,npix) &
462 !$omp private(kpix,jpix,ipix,ddatnorm)
463 do kpix=1,npix(3)
464 do jpix=1,npix(2)
465 do ipix=1,npix(1)
466 if (datnorm(ipix,jpix,kpix) > tiny(datnorm)) then
467 ddatnorm = 1./datnorm(ipix,jpix,kpix)
468 datsmooth(1,ipix,jpix,kpix) = datsmooth(1,ipix,jpix,kpix)*ddatnorm
469 datsmooth(2,ipix,jpix,kpix) = datsmooth(2,ipix,jpix,kpix)*ddatnorm
470 datsmooth(3,ipix,jpix,kpix) = datsmooth(3,ipix,jpix,kpix)*ddatnorm
471 endif
472 enddo
473 enddo
474 enddo
475 !$omp end parallel do
476 endif
477
478 if (nsub > 0 .and. ncount > 0) print "(20x,a,i0,a,i0,a,f6.1/)",'particles on subgrid: ',nsub,&
479 ' max subpixels: ',maxsubpix,' mean subpixels: ',meansubpix/real(ncount)
480
481 end subroutine interpolate3Dgeom_vec
482
483 !------------------------------------------------------------
484 ! interface to kernel routine to avoid problems with openMP
485 !-----------------------------------------------------------
486 real function wkernel(q2)
487 use kernels, only:wfunc
488 real, intent(in) :: q2
489
490 wkernel = wfunc(q2)
491
492 end function wkernel
493
494 !--------------------------------------------------------------------------
495 !
496 ! utility routine for use in above routines
497 ! IN:
498 ! xci - coordinates of particle in cartesians
499 ! radkern - radius of kernel (e.g. 2h)
500 ! OUT:
501 ! xi - non-cartesian coordinates of particle (e.g. r, phi, z)
502 ! ipixmin,ipixmax,jpixmin,jpixmax - pixel limits
503 !
504 !--------------------------------------------------------------------------
505 subroutine get_pixel_limits(xci,xi,radkern,ipixmin,ipixmax,npix,pixwidth,xmin,periodic,igeom,ierr)
506 use geometry, only:coord_is_periodic,get_coord_limits
507 real, intent(in) :: xci(3),radkern,pixwidth(3),xmin(3)
508 real, intent(out) :: xi(3)
509 integer, intent(out) :: ipixmin(3),ipixmax(3),ierr
510 integer, intent(in) :: igeom,npix(3)
511 logical, intent(in) :: periodic(3)
512 real :: xpixmin(3),xpixmax(3)
513 integer :: i
514
515 ierr = 0
516 !
517 !--get limits of rendering in new coordinate system
518 !
519 call get_coord_limits(radkern,xci,xi,xpixmin,xpixmax,igeom)
520 !print*,' R min = ',xpixmin(1),' Rmax = ',xpixmax(1)
521 !
522 !--now work out contributions to pixels in the transformed space.
523 ! For periodic dimensions use the bounding-box range (may extend
524 ! outside [1,npix]); the caller wraps with iroll(). Clamp only
525 ! if the range exceeds a full period.
526 !
527 do i = 1, 3
528 ipixmin(i) = int((xpixmin(i) - xmin(i))/pixwidth(i)) + 1
529 ipixmax(i) = int((xpixmax(i) - xmin(i))/pixwidth(i)) + 1
530 if (periodic(i)) then
531 if (ipixmax(i) - ipixmin(i) >= npix(i)) then
532 ipixmin(i) = 1
533 ipixmax(i) = npix(i)
534 endif
535 else
536 if (ipixmin(i) < 1) ipixmin(i) = 1
537 if (ipixmax(i) > npix(i)) ipixmax(i) = npix(i)
538 if (ipixmin(i) > npix(i) .or. ipixmax(i) < 1) ierr = ierr + 1
539 endif
540 enddo
541
542 end subroutine get_pixel_limits
543
544 end module interpolations3Dgeom