Debian Codegraph @

src/interpolate2D.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 2D interpolation
26 !
27 !----------------------------------------------------------------------
28
29 module interpolations2D
30 use kernels, only:radkernel2,radkernel,cnormk2D,wfunc,pint,select_kernel,soft_func
31 use timing, only:wall_time,print_time
32 use interpolation, only:iroll
33 implicit none
34 public :: interpolate2D, interpolate2D_xsec, interpolate2D_vec
35 public :: interpolate_part, interpolate_part1
36 public :: interpolate2D_pixels, interpolate2D_fromgrid
37
38 private
39
40 contains
41 !--------------------------------------------------------------------------
42 ! subroutine to interpolate from particle data to even grid of pixels
43 !
44 ! The data is smoothed using the SPH summation interpolant,
45 ! that is, we compute the smoothed array according to
46 !
47 ! datsmooth(pixel) = sum_j w_j dat_j W(r-r_j, h_j)
48 !
49 ! where _j is the quantity at the neighbouring particle j and
50 ! W is the smoothing kernel, for which we use the usual cubic spline.
51 ! For an SPH interpolation the weight for each particle should be
52 ! the dimensionless quantity
53 !
54 ! w_j = m_j / (rho_j * h_j**ndim)
55 !
56 ! Other weights can be used (e.g. constants), but in this case the
57 ! normalisation option should also be set.
58 !
59 ! Input: particle coordinates : x,y (npart)
60 ! smoothing lengths : hh (npart)
61 ! interpolation weights : weight (npart)
62 ! scalar data to smooth : dat (npart)
63 !
64 ! number of pixels in x,y : npixx,npixy
65 ! pixel width : pixwidth
66 ! option to normalise interpolation : normalise (.true. or .false.)
67 !
68 ! Output: smoothed data : datsmooth (npixx,npixy)
69 !
70 ! Written by Daniel Price 2003-2020
71 ! Exact rendering implemented by Maya Petkova and Daniel Price 2018
72 !--------------------------------------------------------------------------
73
74 subroutine interpolate2D(x,y,hh,weight,dat,itype,npart, &
75 xmin,ymin,datsmooth,npixx,npixy,pixwidthx,pixwidthy,&
76 normalise,exact,periodicx,periodicy,iverbose)
77
78 integer, intent(in) :: npart,npixx,npixy
79 real, intent(in), dimension(npart) :: x,y,hh,weight,dat
80 integer, intent(in), dimension(npart) :: itype
81 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy
82 real, intent(out), dimension(npixx,npixy) :: datsmooth
83 logical, intent(in) :: normalise,exact,periodicx,periodicy
84 integer, intent(in) :: iverbose
85 real, dimension(npixx,npixy) :: datnorm
86
87 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax
88 integer :: ipixi,jpixi
89 real :: hi,hi1,radkern,q2,wab,const
90 real :: term,termnorm,dx,dy,dy2,xpix,ypix,hi21
91 real :: t_start,t_end,t_used,xmini,ymini,denom,datnorm_min
92 real, dimension(npixx) :: dx2i
93
94 ! Maya
95 real :: pixint,d1,d2,r0
96
97 call wall_time(t_start)
98 if (.not.associated(wfunc)) call select_kernel(0)
99
100 datsmooth = 0.
101 datnorm = 0.
102 if (iverbose > 0) then
103 if (exact) then
104 print "(1x,a)",'interpolating from particles to 2D grid (exact)...'
105 elseif (normalise) then
106 print "(1x,a)",'interpolating from particles to 2D grid (normalised)...'
107 else
108 print "(1x,a)",'interpolating from particles to 2D grid (non-normalised)...'
109 endif
110 endif
111 if (abs(pixwidthx) < tiny(0.) .or. abs(pixwidthy) < tiny(0.)) then
112 print "(1x,a)",'interpolate2D: error: pixel size = 0'
113 return
114 endif
115 ! allow for negative pixel widths (just flips image)
116 xmini = min(xmin,xmin + npixy*pixwidthy)
117 ymini = min(ymin,ymin + npixy*pixwidthx)
118
119 if (any(hh(1:npart) <= tiny(hh)) .and. iverbose >= 0) then
120 print*,'interpolate2D: warning: ignoring some or all particles with h < 0'
121 endif
122 const = cnormk2D ! normalisation constant
123 !
124 !--loop over particles
125 !
126 !$omp parallel do default(none) &
127 !$omp shared(hh,x,y,weight,dat,datsmooth,datnorm,itype,npart) &
128 !$omp shared(xmini,ymini,normalise,exact,radkernel,const) &
129 !$omp shared(pixwidthx,pixwidthy,periodicx,periodicy,npixx,npixy) &
130 !$omp private(hi,radkern,wab) &
131 !$omp private(hi1,hi21,term,termnorm,jpixi,ipixi) &
132 !$omp private(ipixmin,ipixmax,jpixmin,jpixmax,denom) &
133 !$omp private(q2,xpix,ypix,dx,dy,dx2i,dy2,r0,d1,d2,pixint) &
134 !$omp private(i,ipix,jpix) &
135 !$omp schedule (guided, 10)
136 over_parts: do i=1,npart
137 !
138 !--skip particles with itype < 0
139 !
140 if (itype(i) < 0) cycle over_parts
141 !
142 !--skip particles with zero weights
143 !
144 termnorm = const*weight(i)
145 if (termnorm <= 0.) cycle over_parts
146 !
147 !--skip particles with wrong h's
148 !
149 hi = hh(i)
150 if (hi <= tiny(hi)) cycle over_parts
151 !
152 !--set kernel related quantities
153 !
154 hi1 = 1./hi
155 hi21 = hi1*hi1
156 radkern = radkernel*hi ! radius of the smoothing kernel
157 term = termnorm*dat(i)
158 denom = 1./abs(pixwidthx*pixwidthy)/const*hi**2
159 !
160 !--for each particle work out which pixels it contributes to
161 !
162 ipixmin = int((x(i) - radkern - xmini)/abs(pixwidthx))
163 jpixmin = int((y(i) - radkern - ymini)/abs(pixwidthy))
164 ipixmax = int((x(i) + radkern - xmini)/abs(pixwidthx)) + 1
165 jpixmax = int((y(i) + radkern - ymini)/abs(pixwidthy)) + 1
166 ipixmin = min(ipixmin,ipixmax)
167 ipixmax = max(ipixmin,ipixmax)
168 jpixmin = min(jpixmin,jpixmax)
169 jpixmax = max(jpixmin,jpixmax)
170
171 if (.not.periodicx) then
172 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
173 if (ipixmax > npixx) ipixmax = npixx ! to pixels in the image
174 endif
175 if (.not.periodicy) then
176 if (jpixmin < 1) jpixmin = 1
177 if (jpixmax > npixy) jpixmax = npixy
178 endif
179
180 if (exact .and. ipixmax-ipixmin < 5) then
181 !
182 !--loop over pixels boundaries, adding the contribution from this particle
183 !
184 !--first pixel row
185 !
186 if (jpixmax >= jpixmin) then
187 jpix = jpixmin
188 jpixi = jpix
189 if (periodicy) jpixi = iroll(jpix,npixy)
190 ypix = ymini + (jpix-0.5)*pixwidthy
191 dy = ypix - y(i)
192
193 do ipix = ipixmin,ipixmax
194 ipixi = ipix
195 if (periodicx) ipixi = iroll(ipix,npixx)
196 xpix = xmini + (ipix-0.5)*pixwidthx
197 dx = xpix - x(i)
198
199 !--top boundary
200 r0 = 0.5*pixwidthy - dy
201 d1 = 0.5*pixwidthx + dx
202 d2 = 0.5*pixwidthx - dx
203 pixint = pint(r0, d1, d2, hi1)
204
205 wab = pixint*denom
206 !$omp atomic
207 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) + term*wab
208 if (normalise) then
209 !$omp atomic
210 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) + termnorm*wab
211 endif
212 enddo
213 endif
214
215 !
216 !--first pixel column
217 !
218 if (ipixmax >= ipixmin) then
219 ipix = ipixmin
220 ipixi = ipix
221 if (periodicx) ipixi = iroll(ipix,npixx)
222 xpix = xmini + (ipix-0.5)*pixwidthx
223 dx = xpix - x(i)
224
225 do jpix = jpixmin,jpixmax
226 jpixi = jpix
227 if (periodicy) jpixi = iroll(jpixi,npixy)
228 ypix = ymini + (jpix-0.5)*pixwidthy
229 dy = ypix - y(i)
230
231 !--left boundary
232 r0 = 0.5*pixwidthx - dx
233 d1 = 0.5*pixwidthy - dy
234 d2 = 0.5*pixwidthy + dy
235 pixint = pint(r0, d1, d2, hi1)
236
237 wab = pixint*denom
238 !$omp atomic
239 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) + term*wab
240 if (normalise) then
241 !$omp atomic
242 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) + termnorm*wab
243 endif
244 enddo
245 endif
246
247 !
248 !--other pixels
249 !
250 do jpix = jpixmin,jpixmax
251 jpixi = jpix
252 if (periodicy) jpixi = iroll(jpix,npixy)
253 ypix = ymini + (jpix-0.5)*pixwidthy
254 dy = ypix - y(i)
255
256 do ipix = ipixmin,ipixmax
257 ipixi = ipix
258 if (periodicx) ipixi = iroll(ipix,npixx)
259 xpix = xmini + (ipix-0.5)*pixwidthx
260 dx = xpix - x(i)
261 !
262 !--Kernel integral
263 !
264 !--bottom boundary
265 r0 = 0.5*pixwidthy + dy
266 d1 = 0.5*pixwidthx - dx
267 d2 = 0.5*pixwidthx + dx
268 pixint = pint(r0, d1, d2, hi1)
269
270 wab = pixint*denom
271 !$omp atomic
272 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) + term*wab
273 if (normalise) then
274 !$omp atomic
275 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) + termnorm*wab
276 endif
277
278 if (jpix < jpixmax) then
279 jpixi = jpix+1
280 if (periodicy) jpixi = iroll(jpixi,npixy)
281
282 !$omp atomic
283 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) - term*wab
284 if (normalise) then
285 !$omp atomic
286 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) - termnorm*wab
287 endif
288
289 jpixi = jpix
290 if (periodicy) jpixi = iroll(jpix,npixy)
291 endif
292
293 !--right boundary
294 r0 = 0.5*pixwidthx + dx
295 d1 = 0.5*pixwidthy + dy
296 d2 = 0.5*pixwidthy - dy
297 pixint = pint(r0, d1, d2, hi1)
298
299 wab = pixint*denom
300 !$omp atomic
301 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) + term*wab
302 if (normalise) then
303 !$omp atomic
304 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) + termnorm*wab
305 endif
306
307 if (ipix < ipixmax) then
308 ipixi = ipix+1
309 if (periodicx) ipixi = iroll(ipixi,npixx)
310
311 !$omp atomic
312 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) - term*wab
313 if (normalise) then
314 !$omp atomic
315 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) - termnorm*wab
316 endif
317 endif
318 enddo
319 enddo
320 else
321 !
322 !--precalculate an array of dx2 for this particle (optimisation)
323 !
324 do ipix=ipixmin,ipixmax
325 dx2i(ipix) = ((xmini + (ipix-0.5)*pixwidthx - x(i))**2)*hi21
326 enddo
327 !
328 !--loop over pixels, adding the contribution from this particle
329 !
330 do jpix = jpixmin,jpixmax
331 jpixi = jpix
332 if (periodicy) jpixi = iroll(jpix,npixy)
333 ypix = ymini + (jpix-0.5)*pixwidthy
334 dy = ypix - y(i)
335 dy2 = dy*dy*hi21
336 do ipix = ipixmin,ipixmax
337 ipixi = ipix
338 if (periodicx) ipixi = iroll(ipix,npixx)
339 ! xpix = xmin + (ipix-0.5)*pixwidthx
340 ! dx = xpix - x(i)
341 q2 = dx2i(ipix) + dy2 ! dx2 pre-calculated; dy2 pre-multiplied by hi21
342 !q2 = (dx*dx + dy*dy)*hi1*hi1
343
344
345 !
346 !--SPH kernel
347 !
348 wab = wkernel(q2)
349 !
350 !--calculate data value at this pixel using the summation interpolant
351 !
352 !$omp atomic
353 datsmooth(ipixi,jpixi) = datsmooth(ipixi,jpixi) + term*wab
354 if (normalise) then
355 !$omp atomic
356 datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) + termnorm*wab
357 endif
358 enddo
359 enddo
360 endif
361
362 enddo over_parts
363 !$omp end parallel do
364
365 !if (exact) then
366 ! print*, 'sum of datpix = ', sum(datsmooth)/(npixx*npixy)
367 ! print*, 'max of datpix = ', maxval(datsmooth)
368 ! print*, 'min of datpix = ', minval(datsmooth)
369 !endif
370 !
371 !--normalise dat array
372 !
373 if (normalise) then
374 !where (datnorm > 0.)
375 ! datsmooth = datsmooth/datnorm
376 !end where
377 !
378 ! compute the minimum possible value for datnorm, then multiply
379 ! with a kernel-softened version of 1/x to avoid dividing by zero
380 !
381 datnorm_min = minval(const*weight,mask=(weight > 0.))
382 datsmooth = datsmooth*soft_func(datnorm,datnorm_min)
383 endif
384
385 call wall_time(t_end)
386 t_used = t_end - t_start
387 if (t_used > 10.) call print_time(t_used)
388
389 end subroutine interpolate2D
390
391 !--------------------------------------------------------------------------
392 !
393 ! ** this version does vector quantities
394 !
395 ! Input: particle coordinates : x,y (npart)
396 ! smoothing lengths : hh (npart)
397 ! interpolation weights : weight (npart)
398 ! vector data to smooth : vecx (npart)
399 ! vecy (npart)
400 !
401 ! Output: smoothed vector field : vecsmoothx (npixx,npixy)
402 ! : vecsmoothy (npixx,npixy)
403 !
404 ! Daniel Price, University of Exeter, March 2005
405 ! Exact rendering implemented by Maya Petkova and Daniel Price 2018Ã¥
406 !--------------------------------------------------------------------------
407
408 subroutine interpolate2D_vec(x,y,hh,weight,vecx,vecy,itype,npart, &
409 xmin,ymin,vecsmoothx,vecsmoothy,npixx,npixy,pixwidthx,pixwidthy,&
410 normalise,exact,periodicx,periodicy)
411
412 integer, intent(in) :: npart,npixx,npixy
413 real, intent(in), dimension(npart) :: x,y,hh,weight,vecx,vecy
414 integer, intent(in), dimension(npart) :: itype
415 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy
416 real, intent(out), dimension(npixx,npixy) :: vecsmoothx,vecsmoothy
417 logical, intent(in) :: normalise,exact,periodicx,periodicy
418 real, dimension(npixx,npixy) :: datnorm
419
420 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax
421 integer :: ipixi,jpixi
422 real :: hi,hi1,radkern,q2,wab,const
423 real :: termnorm,termx,termy,dx,dy,xpix,ypix
424
425 vecsmoothx = 0.
426 vecsmoothy = 0.
427 datnorm = 0.
428 if (normalise) then
429 print "(1x,a)",'interpolating vector field from particles to 2D grid (normalised)...'
430 else
431 print "(1x,a)",'interpolating vector field from particles to 2D grid (non-normalised)...'
432 endif
433 if (pixwidthx <= 0. .or. pixwidthy <= 0.) then
434 print*,'interpolate2D_vec: error: pixel width <= 0'
435 return
436 endif
437 if (any(hh(1:npart) <= tiny(hh))) then
438 print*,'interpolate2D_vec: warning: ignoring some or all particles with h < 0'
439 endif
440 const = cnormk2D ! normalisation constant
441 if (.not.associated(wfunc)) call select_kernel(0)
442 !
443 !--loop over particles
444 !
445 over_parts: do i=1,npart
446 !
447 !--skip particles with itype < 0
448 !
449 if (itype(i) < 0) cycle over_parts
450 !
451 !--skip particles with zero weights
452 !
453 termnorm = const*weight(i)
454 if (termnorm <= 0.) cycle over_parts
455 !
456 !--skip particles with wrong h's
457 !
458 hi = hh(i)
459 if (hi <= tiny(hi)) cycle over_parts
460 !
461 !--set kernel related quantities
462 !
463 hi1 = 1./hi
464 radkern = radkernel*hi ! radius of the smoothing kernel
465 termx = termnorm*vecx(i)
466 termy = termnorm*vecy(i)
467 !
468 !--for each particle work out which pixels it contributes to
469 !
470 ipixmin = int((x(i) - radkern - xmin)/pixwidthx)
471 jpixmin = int((y(i) - radkern - ymin)/pixwidthy)
472 ipixmax = int((x(i) + radkern - xmin)/pixwidthx) + 1
473 jpixmax = int((y(i) + radkern - ymin)/pixwidthy) + 1
474
475 if (.not.periodicx) then
476 if (ipixmin < 1) ipixmin = 1
477 if (ipixmax > npixx) ipixmax = npixx
478 endif
479 if (.not.periodicy) then
480 if (jpixmin < 1) jpixmin = 1
481 if (jpixmax > npixy) jpixmax = npixy
482 endif
483 !
484 !--loop over pixels, adding the contribution from this particle
485 !
486 do jpix = jpixmin,jpixmax
487 jpixi = jpix
488 if (periodicy) jpixi = iroll(jpix,npixy)
489 ypix = ymin + (jpix-0.5)*pixwidthy
490 dy = ypix - y(i)
491 do ipix = ipixmin,ipixmax
492 ipixi = ipix
493 if (periodicx) ipixi = iroll(ipix,npixx)
494 xpix = xmin + (ipix-0.5)*pixwidthx
495 dx = xpix - x(i)
496 q2 = (dx*dx + dy*dy)*hi1*hi1
497 !
498 !--SPH kernel
499 !
500 wab = wfunc(q2)
501 !
502 !--calculate data value at this pixel using the summation interpolant
503 !
504 vecsmoothx(ipixi,jpixi) = vecsmoothx(ipixi,jpixi) + termx*wab
505 vecsmoothy(ipixi,jpixi) = vecsmoothy(ipixi,jpixi) + termy*wab
506 if (normalise) datnorm(ipixi,jpixi) = datnorm(ipixi,jpixi) + termnorm*wab
507
508 enddo
509 enddo
510
511 enddo over_parts
512 !
513 !--normalise dat arrays
514 !
515 if (normalise) then
516 where (datnorm > 0.)
517 vecsmoothx = vecsmoothx/datnorm
518 vecsmoothy = vecsmoothy/datnorm
519 end where
520 endif
521
522 end subroutine interpolate2D_vec
523
524 !--------------------------------------------------------------------------
525 ! subroutine to interpolate from particle data to even grid of pixels
526 !
527 ! this version takes any 1D cross section through a 2D data set
528 ! the 1D line is specified by two points, (x1,y1) and (x2,y2)
529 ! (ie. this is for arbitrary oblique cross sections)
530 !
531 ! NB: A similar version could be used for 2D oblique cross sections
532 ! of 3D data. In this case we would need to find the intersection
533 ! between the smoothing sphere and the cross section plane. However
534 ! in 3D it is simpler just to rotate the particles first and then take
535 ! a straight cross section.
536 !
537 ! Input: particle coordinates : x,y (npart)
538 ! smoothing lengths : hh (npart)
539 ! interpolation weights : weight (npart)
540 ! scalar data to smooth : dat (npart)
541 !
542 ! Output: smoothed data : datsmooth (npixx)
543 !
544 ! Daniel Price, Institute of Astronomy, Cambridge, Feb 2004
545 !--------------------------------------------------------------------------
546
547 subroutine interpolate2D_xsec(x,y,hh,weight,dat,itype,npart,&
548 x1,y1,x2,y2,datsmooth,npixx,normalise)
549
550 integer, intent(in) :: npart,npixx
551 real, intent(in), dimension(npart) :: x,y,hh,weight,dat
552 integer, intent(in), dimension(npart) :: itype
553 real, intent(in) :: x1,y1,x2,y2
554 real, intent(out), dimension(npixx) :: datsmooth
555 logical, intent(in) :: normalise
556 real, dimension(npixx) :: datnorm
557
558 integer :: i,ipix,ipixmin,ipixmax
559 real :: hi,hi1,radkern,q2,wab,const
560 real :: term,termnorm,dx,dy,xpix,ypix,pixwidth,xpixwidth,xlength
561 real :: gradient,yintercept,aa,bb,cc,determinant,det
562 real :: xstart,xend,ystart,yend,rstart,rend
563 real :: tol
564 logical :: xsame, ysame, debug
565
566 debug = .false.
567 !
568 !--check for errors in input
569 !
570 tol = 1.e-3
571 ysame = (abs(y2 - y1) < tol)
572 xsame = (abs(x2 - x1) < tol)
573 if (xsame.and.ysame) then
574 print*,'error: interpolate2D_xsec: zero length cross section'
575 return
576 endif
577 if (npixx==0) then
578 print*,'error: interpolate2D_xsec: npix = 0 '
579 return
580 endif
581 print*,'oblique 1D cross section through 2D data: npix =',npixx
582 !
583 !--work out the equation of the line y = mx + c from the two points input
584 !
585 gradient = 0.
586 if (.not.xsame) gradient = (y2-y1)/(x2-x1)
587 yintercept = y2 - gradient*x2
588 print*,'cross section line: y = ',gradient,'x + ',yintercept
589 !
590 !--work out length of line and divide into pixels
591 !
592 xlength = sqrt((x2-x1)**2 + (y2-y1)**2)
593 pixwidth = xlength/real(npixx)
594 xpixwidth = (x2 - x1)/real(npixx)
595 if (debug) then
596 print*,'length of line = ',xlength
597 print*,'pixel width = ',pixwidth, ' in x direction = ',xpixwidth
598 endif
599 !
600 !--now interpolate to the line of pixels
601 !
602 datsmooth = 0.
603 datnorm = 0.
604 const = cnormk2D ! normalisation constant
605 if (.not.associated(wfunc)) call select_kernel(0)
606 !
607 !--loop over particles
608 !
609 over_parts: do i=1,npart
610 !
611 !--skip particles with itype < 0
612 !
613 if (itype(i) < 0) cycle over_parts
614 !
615 !--skip particles with zero weights
616 !
617 termnorm = const*weight(i)
618 if (termnorm <= 0.) cycle over_parts
619 !
620 !--skip particles with wrong h's
621 !
622 hi = hh(i)
623 if (hi <= tiny(hi)) cycle over_parts
624 !
625 !--set kernel related quantities
626 !
627 hi1 = 1./hi
628 radkern = radkernel*hi ! radius of the smoothing kernel
629 term = termnorm*dat(i)
630 !
631 !--for each particle work out which pixels it contributes to
632 ! to do this we need to work out the two points at which the line
633 ! intersects the particles smoothing circle
634 ! given by the equation (x-xi)^2 + (y-yi)^2 = (2h)^2.
635 ! The x co-ordinates of these points are the solutions to a
636 ! quadratic with co-efficients:
637
638 aa = 1. + gradient**2
639 bb = 2.*gradient*(yintercept - y(i)) - 2.*x(i)
640 cc = x(i)**2 + y(i)**2 - 2.*yintercept*y(i) + yintercept**2 &
641 - radkern**2
642 !
643 !--work out whether there are any real solutions and find them
644 !
645 determinant = bb**2 - 4.*aa*cc
646 if (determinant < 0) then
647 !!print*,' particle ',i,': does not contribute ',x(i),y(i)
648 else
649 det = sqrt(determinant)
650 xstart = (-bb - det)/(2.*aa)
651 xend = (-bb + det)/(2.*aa)
652 if (xstart < x1) xstart = x1
653 if (xstart > x2) xstart = x2
654 if (xend > x2) xend = x2
655 if (xend < x1) xend = x1
656 ystart = gradient*xstart + yintercept
657 yend = gradient*xend + yintercept
658 !
659 !--work out position in terms of distance (no. of pixels) along the line
660 !
661 rstart = sqrt((xstart-x1)**2 + (ystart-y1)**2)
662 rend = sqrt((xend-x1)**2 + (yend-y1)**2)
663
664 ipixmin = int(rstart/pixwidth)
665 ipixmax = int(rend/pixwidth) + 1
666
667 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
668 if (ipixmax < 1) ipixmax = 1
669 if (ipixmax > npixx) ipixmax = npixx
670 if (ipixmin > npixx) ipixmax = npixx
671 !
672 !--loop over pixels, adding the contribution from this particle
673 !
674 !if (debug) print*,' particle ',i,': ',ipixmin,ipixmax,xstart,x(i),xend
675 do ipix = ipixmin,ipixmax
676
677 xpix = x1 + (ipix-0.5)*xpixwidth
678 ypix = gradient*xpix + yintercept
679 dy = ypix - y(i)
680 dx = xpix - x(i)
681 q2 = (dx*dx + dy*dy)*hi1*hi1
682 !
683 !--SPH kernel
684 !
685 wab = wfunc(q2)
686 !
687 !--calculate data value at this pixel using the summation interpolant
688 !
689 datsmooth(ipix) = datsmooth(ipix) + term*wab
690 if (normalise) datnorm(ipix) = datnorm(ipix) + termnorm*wab
691
692 enddo
693
694 endif
695
696 enddo over_parts
697 !
698 !--normalise dat array
699 !
700 if (normalise) then
701 where (datnorm > 0.)
702 datsmooth = datsmooth/datnorm
703 end where
704 endif
705
706 end subroutine interpolate2D_xsec
707
708 !--------------------------------------------------------------------------
709 ! subroutine to render particles onto a pixel array
710 ! at the maximum or minimum colour
711 !
712 ! Written by Daniel Price 21/7/2008
713 !--------------------------------------------------------------------------
714 subroutine interpolate_part(x,y,hh,npart,xmin,ymin,datsmooth,npixx,npixy,pixwidth,datval,brightness)
715 integer, intent(in) :: npart,npixx,npixy
716 real, intent(in), dimension(npart) :: x,y,hh
717 real, intent(in) :: xmin,ymin,pixwidth,datval
718 real, intent(inout), dimension(npixx,npixy) :: datsmooth
719 real, intent(inout), dimension(npixx,npixy), optional :: brightness
720 integer :: i
721
722 if (pixwidth <= 0.) then
723 print "(1x,a)",'interpolate_part: error: pixel width <= 0'
724 return
725 endif
726 if (any(hh(1:npart) <= tiny(hh))) then
727 print*,'interpolate_part: warning: ignoring some or all particles with h < 0'
728 endif
729 !
730 !--loop over particles
731 !
732 if (present(brightness)) then
733 do i=1,npart
734 call interpolate_part1(x(i),y(i),hh(i),xmin,ymin,datsmooth,npixx,npixy,pixwidth,datval)
735 enddo
736 else
737 do i=1,npart
738 call interpolate_part1(x(i),y(i),hh(i),xmin,ymin,datsmooth,npixx,npixy,pixwidth,datval,brightness)
739 enddo
740 endif
741
742 end subroutine interpolate_part
743
744 !--------------------------------------------------------------------------
745 ! subroutine to render a single particle onto a pixel array
746 !
747 ! Written by Daniel Price 21/7/2008
748 !--------------------------------------------------------------------------
749 subroutine interpolate_part1(xi,yi,hi,xmin,ymin,datsmooth,npixx,npixy,pixwidth,datval,brightness)
750 real, intent(in) :: xi,yi,hi,xmin,ymin,pixwidth,datval
751 integer, intent(in) :: npixx,npixy
752 real, intent(inout), dimension(npixx,npixy) :: datsmooth
753 real, intent(inout), dimension(npixx,npixy), optional :: brightness
754 integer :: ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax
755 real :: radkern,radkern2,rab2
756 real :: dx,dy2,xpix,ypix
757
758 !
759 !--skip particles with wrong h's
760 !
761 if (hi <= tiny(hi)) return
762 !
763 !--set kernel related quantities
764 !
765 radkern = max(hi,2.*pixwidth)
766 radkern2 = radkern*radkern ! radius of the smoothing kernel
767 !
768 !--for each particle work out which pixels it contributes to
769 !
770 ipixmin = int((xi - radkern - xmin)/pixwidth)
771 jpixmin = int((yi - radkern - ymin)/pixwidth)
772 ipixmax = int((xi + radkern - xmin)/pixwidth) + 1
773 jpixmax = int((yi + radkern - ymin)/pixwidth) + 1
774
775 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
776 if (jpixmin < 1) jpixmin = 1 ! to pixels in the image
777 if (ipixmax > npixx) ipixmax = npixx
778 if (jpixmax > npixy) jpixmax = npixy
779 !
780 !--loop over pixels, adding the contribution from this particle
781 !
782 do jpix = jpixmin,jpixmax
783 ypix = ymin + (jpix-0.5)*pixwidth
784 dy2 = (ypix - yi)**2
785 do ipix = ipixmin,ipixmax
786 xpix = xmin + (ipix-0.5)*pixwidth
787 dx = xpix - xi
788 rab2 = dx**2 + dy2
789 !
790 !--set data value at this pixel to maximum
791 !
792 if (rab2 < radkern2) then
793 datsmooth(ipix,jpix) = datval
794 if (present(brightness)) then
795 brightness(ipix,jpix) = 1.0
796 endif
797 endif
798 enddo
799 enddo
800
801 end subroutine interpolate_part1
802
803 !--------------------------------------------------------------------------
804 ! subroutine to interpolate from arbitrary data to even grid of pixels
805 !
806 ! The data is smoothed using the SPH summation interpolant,
807 ! that is, we compute the smoothed array according to
808 !
809 ! datsmooth(pixel) = sum_j dat_j W(r-r_j, \Delta) / sum_j W(r-r_j, \Delta)
810 !
811 ! where _j is the quantity at the neighbouring particle j and
812 ! W is the smoothing kernel. The interpolation is normalised.
813 !
814 ! Input: data points : x,y (npart)
815 ! third scalar to use as weight : dat (npart) (optional)
816 !
817 ! number of pixels in x,y : npixx,npixy
818 ! pixel width : pixwidth
819 ! option to normalise interpolation : normalise (.true. or .false.)
820 !
821 ! Output: smoothed data : datsmooth (npixx,npixy)
822 !
823 ! Written by Daniel Price 2017
824 !--------------------------------------------------------------------------
825
826 subroutine interpolate2D_pixels(x,y,itype,npart, &
827 xmin,ymin,xmax,ymax,datsmooth,npixx,npixy,&
828 normalise,adaptive,dat,datpix2,fac,weights)
829
830 use timing, only:wall_time,print_time
831 integer, intent(in) :: npart,npixx,npixy
832 real, intent(in), dimension(npart) :: x,y
833 integer, intent(in), dimension(npart) :: itype
834 real, intent(in) :: xmin,ymin,xmax,ymax
835 real, intent(out), dimension(npixx,npixy) :: datsmooth
836 logical, intent(in) :: normalise,adaptive
837 real, intent(in), dimension(npart), optional :: dat,weights
838 real, dimension(npixx,npixy), intent(out), optional :: datpix2
839 real, intent(in), optional :: fac
840
841 real, dimension(npixx,npixy) :: datnorm,datold
842 real, dimension(npixx) :: dx2i,qq2,wabi
843
844 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax,its,itsmax
845 real :: hi,hi1,radkernx,radkerny,q2,wab,const
846 real :: term,termnorm,dy,xpix,ypix,ddx,ddy
847 real :: xi,yi,pixwidthx,pixwidthy,dy2
848 real :: t1,t2,hfac
849
850 if (adaptive) then
851 print "(1x,a)",'interpolating from particles to 2D pixels (adaptive)...'
852 else
853 print "(1x,a)",'interpolating from particles to 2D pixels...'
854 endif
855
856 if (adaptive) then
857 itsmax = 3
858 else
859 itsmax = 1
860 endif
861 ! default smoothing length in units of pixel scale
862 hfac = 1.5
863 if (present(fac)) then
864 if (fac > 0.) hfac = fac
865 endif
866 call wall_time(t1)
867 if (.not.associated(wfunc)) call select_kernel(0)
868
869 iterations: do its=1,itsmax
870 datsmooth = 0.
871 datnorm = 0.
872
873 const = cnormk2D ! normalisation constant
874 !
875 !--loop over particles
876 !
877 ddx = npixx/(xmax - xmin)
878 ddy = npixy/(ymax - ymin)
879
880 pixwidthx = 1. !/npixx
881 pixwidthy = 1. !/npixy
882 if (pixwidthx <= 0. .or. pixwidthy <= 0.) then
883 print "(1x,a)",'interpolate2D: error: pixel width <= 0'
884 return
885 endif
886
887 !$omp parallel do default(none) &
888 !$omp shared(npart,itype,x,y,xmin,ymin,ddx,ddy,its,itsmax,weights) &
889 !$omp shared(datold,datsmooth,datnorm,npixx,npixy,const,radkernel,radkernel2,dat) &
890 !$omp shared(pixwidthx,pixwidthy,normalise,hfac) &
891 !$omp private(i,xi,yi,ipix,jpix,hi,hi1) &
892 !$omp private(radkernx,radkerny,ipixmin,ipixmax,jpixmin,jpixmax) &
893 !$omp private(dx2i,xpix,ypix,dy,dy2,q2,wab,term,termnorm,qq2,wabi)
894 over_parts: do i=1,npart
895 !
896 !--skip particles with itype < 0
897 !
898 if (itype(i) < 0) cycle over_parts
899
900 !
901 !--scale particle positions into viewport coordinates
902 !
903 xi = (x(i) - xmin)*ddx
904 yi = (y(i) - ymin)*ddy
905 hi = 1.0*pixwidthx
906 if (itsmax==1) hi=hi*hfac ! in units of pixel spacing
907
908 ipix = int(xi)
909 jpix = int(yi)
910 if (its > 1 .and. ipix >= 1 .and. ipix <= npixx.and. jpix >= 1 .and. jpix <= npixy) then
911 hi = max(min(hfac/sqrt(datold(ipix,jpix)),100.),1.)
912 endif
913 hi1 = 1./hi
914 termnorm = const*hi1*hi1
915 !
916 !--set kernel related quantities
917 !
918 radkernx = radkernel*hi ! radius of the smoothing kernel
919 radkerny = radkernel*hi ! radius of the smoothing kernel
920 if (present(weights)) then
921 termnorm = termnorm*weights(i)
922 endif
923 if (present(dat)) then
924 term = termnorm*dat(i)
925 else
926 term = termnorm
927 endif
928 if (termnorm <= 0.) cycle over_parts
929 !
930 !--for each particle work out which pixels it contributes to
931 !
932 ipixmin = int((xi - radkernx))
933 jpixmin = int((yi - radkerny))
934 ipixmax = int((xi + radkernx)) + 1
935 jpixmax = int((yi + radkerny)) + 1
936
937 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
938 if (ipixmax > npixx) ipixmax = npixx ! to pixels in the image
939 if (jpixmin < 1) jpixmin = 1
940 if (jpixmax > npixy) jpixmax = npixy
941 !
942 !--precalculate an array of dx2 for this particle (optimisation)
943 !
944 do ipix=ipixmin,ipixmax
945 xpix = (ipix-0.5)*pixwidthx
946 dx2i(ipix) = ((xpix - xi)**2)*hi1*hi1
947 enddo
948 !
949 !--loop over pixels, adding the contribution from this particle
950 !
951 do jpix = jpixmin,jpixmax
952 ypix = (jpix-0.5)*pixwidthy
953 dy = ypix - yi
954 dy2 = dy*dy*hi1*hi1
955
956 do ipix = ipixmin,ipixmax
957 q2 = dx2i(ipix) + dy2
958 !
959 !--SPH kernel
960 !
961 if (q2 < radkernel2) then
962 wab = wkernel(q2)
963 !
964 !--calculate data value at this pixel using the summation interpolant
965 !
966 !$omp atomic
967 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + term*wab
968 if (normalise) then
969 !$omp atomic
970 datnorm(ipix,jpix) = datnorm(ipix,jpix) + termnorm*wab
971 endif
972 endif
973
974 enddo
975 enddo
976
977 enddo over_parts
978 !$omp end parallel do
979
980 if (present(dat)) then
981 datold = datnorm
982 else
983 datold = datsmooth
984 endif
985 !
986 !--normalise dat array
987 !
988 if (normalise) then
989 where (datnorm > 0.)
990 datsmooth = datsmooth/datnorm
991 end where
992 endif
993
994 enddo iterations
995
996 if (present(datpix2)) datpix2 = datnorm
997 call wall_time(t2)
998 if (t2-t1 > 1.) call print_time(t2-t1)
999
1000 end subroutine interpolate2D_pixels
1001
1002 !--------------------------------------------------------------------------
1003 ! subroutine to interpolate from even grid of pixels TO arbitrary points
1004 !
1005 ! Input: data points : x,y (npart)
1006 ! smoothing length : hh (npart)
1007 ! number of pixels in x,y : npixx,npixy
1008 ! pixel width : pixwidth
1009 ! data on pixels : datpix(npixx,npixy)
1010 !
1011 ! Output: smoothed data : dat (npart)
1012 !
1013 ! Written by Daniel Price 2021
1014 !--------------------------------------------------------------------------
1015
1016 subroutine interpolate2D_fromgrid(x,y,hh,dat,gradh,sigma,mask,npart, &
1017 xmin,ymin,datpix,npixx,npixy,pixwidthx,pixwidthy)
1018
1019 use timing, only:wall_time,print_time
1020 integer, intent(in) :: npart,npixx,npixy
1021 real, intent(in), dimension(npart) :: x,y,hh
1022 real, intent(out), dimension(npart) :: dat,gradh,sigma
1023 integer, intent(in), dimension(npart) :: mask
1024 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy
1025 real, intent(in), dimension(npixx,npixy) :: datpix
1026
1027 real, dimension(npixx) :: dx2i,qq2
1028
1029 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax
1030 real :: hi,hi1,radkernx,radkerny,q2,wab,const,datpart
1031 real :: term,dy,xpix,ypix,xi,yi,dy2,dwabdh,gradhi
1032 real :: t1,t2
1033
1034 !print "(1x,a)",'interpolating from 2D pixels to particles...'
1035
1036 call wall_time(t1)
1037 if (.not.associated(wfunc)) call select_kernel(0)
1038
1039 const = cnormk2D ! normalisation constant
1040
1041 if (pixwidthx <= 0. .or. pixwidthy <= 0.) then
1042 print "(1x,a)",'interpolate2D: error: pixel width <= 0'
1043 return
1044 endif
1045 !
1046 !--loop over particles
1047 !
1048 !$omp parallel do default(none) &
1049 !$omp shared(npart,mask,x,y,hh,xmin,ymin) &
1050 !$omp shared(dat,gradh,sigma,datpix,npixx,npixy,const,radkernel,radkernel2) &
1051 !$omp shared(pixwidthx,pixwidthy) &
1052 !$omp private(i,xi,yi,ipix,jpix,hi,hi1,datpart) &
1053 !$omp private(radkernx,radkerny,ipixmin,ipixmax,jpixmin,jpixmax) &
1054 !$omp private(dx2i,xpix,ypix,dy,dy2,q2,wab,term,qq2,dwabdh,gradhi)
1055 over_parts: do i=1,npart
1056 !
1057 !--skip particles with itype < 0
1058 !
1059 if (mask(i) < 0) cycle over_parts
1060
1061 xi = x(i)
1062 yi = y(i)
1063 hi = hh(i) ! in units of pixel spacing
1064
1065 hi1 = 1./hi
1066 term = const*pixwidthx*pixwidthy*hi1*hi1 ! this is dx*dy/h**2
1067 if (term <= 0.) cycle over_parts
1068 !
1069 !--set kernel related quantities
1070 !
1071 radkernx = radkernel*hi ! radius of the smoothing kernel
1072 radkerny = radkernel*hi ! radius of the smoothing kernel
1073 !
1074 !--for each particle work out which pixels it contributes to
1075 !
1076 ipixmin = int((xi - radkernx - xmin)/abs(pixwidthx))
1077 jpixmin = int((yi - radkerny - ymin)/abs(pixwidthy))
1078 ipixmax = int((xi + radkernx - xmin)/abs(pixwidthx)) + 1
1079 jpixmax = int((yi + radkerny - ymin)/abs(pixwidthy)) + 1
1080
1081 if (ipixmin < 1) ipixmin = 1 ! make sure we only receive contributions
1082 if (ipixmax > npixx) ipixmax = npixx ! from pixels in the image
1083 if (jpixmin < 1) jpixmin = 1
1084 if (jpixmax > npixy) jpixmax = npixy
1085 !
1086 !--precalculate an array of dx2 for this particle (optimisation)
1087 !
1088 do ipix=ipixmin,ipixmax
1089 xpix = xmin + (ipix-0.5)*pixwidthx
1090 dx2i(ipix) = ((xpix - xi)**2)*hi1*hi1
1091 enddo
1092 !
1093 !--loop over pixels, adding the contribution TO this particle
1094 !
1095 datpart = 0.
1096 gradhi = 0.
1097 do jpix = jpixmin,jpixmax
1098 ypix = ymin + (jpix-0.5)*pixwidthy
1099 dy = ypix - yi
1100 dy2 = dy*dy*hi1*hi1
1101
1102 do ipix = ipixmin,ipixmax
1103 q2 = dx2i(ipix) + dy2
1104 !
1105 !--SPH kernel
1106 !
1107 if (q2 < radkernel2) then
1108 wab = wkernel(q2)
1109 !
1110 !--interpolate to particle using the summation interpolant
1111 !
1112 datpart = datpart + datpix(ipix,jpix)*wab
1113 dwabdh = -(2.*wab + sqrt(q2)*dwkernel(q2))
1114 gradhi = gradhi + datpix(ipix,jpix)*dwabdh
1115 endif
1116 enddo
1117 enddo
1118 dat(i) = term*datpart
1119 gradh(i) = term*gradhi*hi1
1120
1121 datpart = 0.
1122 do jpix = jpixmin,jpixmax
1123 ypix = ymin + (jpix-0.5)*pixwidthy
1124 dy = ypix - yi
1125 dy2 = dy*dy*hi1*hi1
1126
1127 do ipix = ipixmin,ipixmax
1128 q2 = dx2i(ipix) + dy2
1129 !
1130 !--SPH kernel
1131 !
1132 if (q2 < radkernel2) then
1133 wab = wkernel(q2)
1134 !
1135 !--interpolate to particle using the summation interpolant
1136 !
1137 datpart = datpart + (datpix(ipix,jpix) - dat(i))**2*wab
1138 endif
1139 enddo
1140 enddo
1141 sigma(i) = sqrt(term*datpart)
1142
1143 enddo over_parts
1144 !$omp end parallel do
1145
1146 call wall_time(t2)
1147 if (t2-t1 > 1.) call print_time(t2-t1)
1148
1149 end subroutine interpolate2D_fromgrid
1150
1151 !------------------------------------------------------------
1152 ! interface to kernel routine to avoid problems with openMP
1153 !-----------------------------------------------------------
1154 real function wkernel(q2)
1155 use kernels, only:wfunc
1156 real, intent(in) :: q2
1157
1158 wkernel = wfunc(q2)
1159
1160 end function wkernel
1161
1162 real function dwkernel(q2)
1163 use kernels, only:dwfunc
1164 real, intent(in) :: q2
1165
1166 dwkernel = dwfunc(q2)
1167
1168 end function dwkernel
1169
1170 end module interpolations2D