Debian Codegraph @

src/interpolate3D_projection.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-2017 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !----------------------------------------------------------------------
24 !
25 ! Module containing all of the routines required for 3D projections
26 ! (where rendered quantity is integrated along the line of sight)
27 !
28 !----------------------------------------------------------------------
29
30 module projections3D
31 implicit none
32
33 integer, parameter :: maxcoltable = 1000
34 real, dimension(0:maxcoltable) :: coltable
35 real, private :: dq2table = 4./maxcoltable
36 real, private :: ddq2table = maxcoltable/4.
37
38 public :: setup_integratedkernel
39 public :: interpolate3D_projection,interpolate3D_proj_points
40 public :: interpolate3D_proj_vec,interp3D_proj_vec_synctron
41 public :: wfromtable
42 public :: coltable
43 logical, public :: have_setup_kernel = .false.
44
45 private
46
47 contains
48
49 subroutine setup_integratedkernel
50 !-------------------------------------------------------------
51 ! tabulates the integral through the cubic spline kernel
52 ! tabulated in (r/h)**2 so that sqrt is not necessary
53 !-------------------------------------------------------------
54 use kernels, only:wfunc,radkernel2,cnormk3D,select_kernel
55 integer :: i,j
56 real :: rxy2,deltaz,dz,z,q2,wkern,coldens
57 integer, parameter :: npts = 100
58
59 ! force cubic kernel if not already set
60 if (.not.associated(wfunc)) call select_kernel(0)
61 have_setup_kernel = .true.
62
63 !print "(1x,a)",'setting up integrated kernel table...'
64 dq2table = radkernel2/maxcoltable
65 ddq2table = 1./dq2table
66
67 do i=0,maxcoltable-1
68 !
69 !--tabulate for (cylindrical) r**2 between 0 and radkernel**2
70 !
71 rxy2 = i*dq2table
72 !
73 !--integrate z between 0 and sqrt(radkernel^2 - rxy^2)
74 !
75 deltaz = sqrt(radkernel2 - rxy2)
76 dz = deltaz/real(npts-1)
77 coldens = 0.
78 if (isnan(deltaz)) print "(a)",'WARNING: NaN in kernel table setup'
79 do j=1,npts
80 z = (j-1)*dz
81 q2 = rxy2 + z*z
82 wkern = wfunc(q2)
83 if (j==1 .or. j==npts) then
84 coldens = coldens + 0.5*wkern*dz ! trapezoidal rule
85 else
86 coldens = coldens + wkern*dz
87 endif
88 enddo
89 coltable(i)=2.0*coldens*cnormk3D
90 enddo
91 coltable(maxcoltable) = 0.
92
93 return
94 end subroutine setup_integratedkernel
95 !
96 ! This function interpolates from the table of integrated kernel values
97 ! to give w(q)
98 !
99 real function wfromtable(q2)
100 real, intent(in) :: q2
101 real :: dxx,dwdx
102 integer :: index, index1
103 !
104 !--find nearest index in table
105 !
106 index = max(int(q2*ddq2table),0) ! the max prevents seg faults on NaNs for q2
107 !index = min(index,maxcoltable) ! should be unnecessary if q2 < radkernel checked
108 index1 = min(index + 1,maxcoltable)
109 !
110 !--find increment along from this index
111 !
112 dxx = q2 - index*dq2table
113 !
114 !--find gradient
115 !
116 dwdx = (coltable(index1) - coltable(index))*ddq2table
117 !
118 !--compute value of integrated kernel
119 !
120 wfromtable = coltable(index) + dwdx*dxx
121
122 end function wfromtable
123
124 !--------------------------------------------------------------------------
125 ! subroutine to interpolate from particle data to even grid of pixels
126 !
127 ! The data is smoothed using the SPH summation interpolant,
128 ! that is, we compute the smoothed array according to
129 !
130 ! datsmooth(pixel) = sum_b weight_b dat_b W(r-r_b, h_b)
131 !
132 ! where _b is the quantity at the neighbouring particle b and
133 ! W is the smoothing kernel, for which we use the usual cubic spline
134 !
135 ! ** In this version 3D data is interpolated to a 2D grid by use of an
136 ! ** integrated form of the kernel (that is W_ab in this case is
137 ! ** the integral through the 3D kernel to give a 2D kernel)
138 ! ** This results in a column density map of the interpolated quantity
139 ! ** From a similar routine by Matthew Bate.
140 !
141 ! The (dimensionless) weight for each particle should be
142 !
143 ! weight = pmass/(rho*h^3)
144 !
145 ! the interface is written in this form to avoid floating exceptions
146 ! on physically scaled data.
147 !
148 ! Input: particle coordinates : x,y,z (npart) - note that z is only required for perspective
149 ! smoothing lengths : hh (npart)
150 ! weight for each particle : weight (npart)
151 ! scalar data to smooth : dat (npart)
152 !
153 ! Output: smoothed data : datsmooth (npixx,npixy)
154 !
155 ! Written by Daniel Price September 2003
156 ! 3D perspective added Nov 2005
157 !--------------------------------------------------------------------------
158
159 subroutine interpolate3D_projection(x,y,z,hh,weight,dat,itype,npart, &
160 xmin,ymin,datsmooth,npixx,npixy,pixwidthx,pixwidthy,normalise,zobserver,dscreen, &
161 useaccelerate,exact_rendering,iverbose)
162
163 use kernels, only:radkernel,radkernel2,cnormk3D,wallint,soft_func
164 use timing, only:wall_time,print_time
165 integer, intent(in) :: npart,npixx,npixy
166 real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat
167 integer, intent(in), dimension(npart) :: itype
168 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy,zobserver,dscreen
169 real, intent(out), dimension(npixx,npixy) :: datsmooth
170 logical, intent(in) :: normalise
171 real, dimension(npixx,npixy) :: datnorm
172 logical, intent(in) :: useaccelerate,exact_rendering
173 integer, intent(in) :: iverbose
174 real :: row(npixx)
175
176 integer :: ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax,npixpartx,npixparty
177 integer :: iprintinterval, iprintnext,ipixi,jpixi,jpixcopy
178 integer :: nsubgrid,nfull,nok,ncpus
179 !$ integer :: omp_get_num_threads
180 integer(kind=selected_int_kind(10)) :: iprogress,i ! up to 10 digits
181 real :: hi,hi1,hi21,radkern,wab,q2,xi,yi,xminpix,yminpix
182 real :: term,termnorm,dy,dy2,ypix,zfrac,hsmooth,horigi
183 real :: xpixmin,xpixmax,xmax,ypixmin,ypixmax,ymax
184 real :: hmin,fac,hminall,dfac,pixwidthz,pixint,zi,xpixi,zpix,term_exact,termnorm_exact
185 real, dimension(npixx) :: xpix,dx2i
186 real :: t_start,t_end,t_used,datnorm_min
187 logical :: iprintprogress,use3Dperspective,accelerate
188 character(len=32) :: string
189
190 datsmooth = 0.
191 term = 0.
192 datnorm_min = huge(0.)
193 string = 'projecting'
194 if (normalise) then
195 string = trim(string)//' (normalised)'
196 datnorm = 0.
197 elseif (useaccelerate) then
198 string = trim(string)//' (fast)'
199 elseif (exact_rendering) then
200 string = trim(string)//' (exact/Petkova)'
201 endif
202
203 ncpus = 0
204 !$omp parallel
205 !$omp master
206 !$ ncpus = omp_get_num_threads()
207 !$omp end master
208 !$omp end parallel
209
210 if (ncpus > 0 .and. iverbose >= 0) then
211 write (*,"(1x,a,': ',i4,' x ',i4,' on ',i3,' cpus')") trim(string),npixx,npixy,ncpus
212 elseif (iverbose >= 0) then
213 write (*,"(1x,a,': ',i4,' x ',i4)") trim(string),npixx,npixy
214 endif
215
216 if (pixwidthx <= 0. .or. pixwidthy <= 0) then
217 if (iverbose >= -1) print "(1x,a)",'ERROR: pixel width <= 0'
218 return
219 endif
220 !nout = count(hh(1:npart) <= 0.)
221 !if (nout > 0) then
222 ! print*,'interpolate3D_projection: warning: ignoring ',nout,' particles with h <= 0'
223 !endif
224 !
225 !--check column density table has actually been setup
226 !
227 if (.not.have_setup_kernel) call setup_integratedkernel
228 !
229 !--print a progress report if it is going to take a long time
230 ! (a "long time" is, however, somewhat system dependent)
231 !
232 iprintprogress = (npart >= 100000) .or. (npixx*npixy > 100000) .and. iverbose >= 0
233 !$ iprintprogress = .false.
234 !
235 !--loop over particles
236 !
237 iprintinterval = 25
238 if (npart >= 1e6) iprintinterval = 10
239 iprintnext = iprintinterval
240 use3Dperspective = abs(dscreen) > tiny(dscreen)
241 !
242 !--get starting CPU time
243 !
244 call wall_time(t_start)
245
246 xminpix = xmin - 0.5*pixwidthx
247 yminpix = ymin - 0.5*pixwidthy
248 xmax = xmin + npixx*pixwidthx
249 ymax = ymin + npixy*pixwidthy
250 !
251 !--use a minimum smoothing length on the grid to make
252 ! sure that particles contribute to at least one pixel
253 !
254 hmin = 0.5*max(pixwidthx,pixwidthy)
255
256 !dhmin3 = 1./(hmin*hmin*hmin)
257 !
258 !--store x value for each pixel (for optimisation)
259 !
260 do ipix=1,npixx
261 xpix(ipix) = xminpix + ipix*pixwidthx
262 enddo
263 nsubgrid = 0
264 nok = 0
265 hminall = huge(hminall)
266
267 !$omp parallel default(none) &
268 !$omp shared(hh,z,x,y,weight,dat,itype,npart,iprintprogress,iprintinterval) &
269 !$omp shared(xmin,ymin,xmax,ymax,xminpix,yminpix,xpix,pixwidthx,pixwidthy) &
270 !$omp shared(npixx,npixy,dscreen,zobserver,use3dperspective,useaccelerate) &
271 !$omp shared(normalise,radkernel,radkernel2,datsmooth,datnorm,cnormk3D,exact_rendering) &
272 !$omp firstprivate(hmin) & !,dhmin3) &
273 !$omp private(hi,zfrac,xi,yi,radkern,xpixmin,xpixmax,ypixmin,ypixmax) &
274 !$omp private(hsmooth,horigi,hi1,hi21,term,termnorm,npixpartx,npixparty,jpixi,ipixi) &
275 !$omp private(ipixmin,ipixmax,jpixmin,jpixmax,accelerate) &
276 !$omp private(dx2i,row,q2,ypix,dy,dy2,wab,termnorm_exact,term_exact) &
277 !$omp private(i,ipix,jpix,jpixcopy,fac,dfac,pixwidthz,pixint,zi,xpixi,zpix) &
278 !$omp private(iprogress) &
279 !$omp firstprivate(iprintnext) &
280 !$omp reduction(+:nsubgrid,nok) &
281 !$omp reduction(min:hminall)
282 !$omp do schedule (guided, 2)
283 over_particles: do i=1,npart
284 !
285 !--report on progress
286 !
287 if (iprintprogress) then
288 iprogress = 100*i/npart
289 if (iprogress >= iprintnext) then
290 write(*,"('(',i3,'% -',i12,' particles done)')") iprogress,i
291 iprintnext = iprintnext + iprintinterval
292 endif
293 endif
294 !
295 !--skip particles with itype < 0
296 !
297 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_particles
298 !
299 !--set h related quantities
300 !
301 hi = hh(i)
302 horigi = hi
303 if (hi <= 0.) then
304 cycle over_particles
305 elseif (use3Dperspective) then
306 if (z(i) > zobserver) cycle over_particles
307 zfrac = abs(dscreen/(z(i)-zobserver))
308 hi = hi*zfrac
309 endif
310
311 radkern = radkernel*hi ! radius of the smoothing kernel
312
313 !--cycle as soon as we know the particle does not contribute
314 xi = x(i)
315 xpixmin = xi - radkern
316 if (xpixmin > xmax) cycle over_particles
317 xpixmax = xi + radkern
318 if (xpixmax < xmin) cycle over_particles
319
320 yi = y(i)
321 ypixmin = yi - radkern
322 if (ypixmin > ymax) cycle over_particles
323 ypixmax = yi + radkern
324 if (ypixmax < ymin) cycle over_particles
325
326 !--take resolution length as max of h and 1/2 pixel width
327 if (.not.exact_rendering .and. hi < hmin) then
328 hminall = min(hi,hminall)
329 nsubgrid = nsubgrid + 1
330 hsmooth = hmin
331 fac = 1. !(horigi*horigi*horigi)*dhmin3 ! factor by which to adjust the weight
332 else
333 fac = 1.
334 hsmooth = hi
335 nok = nok + 1
336 endif
337
338 radkern = radkernel*hsmooth
339 !
340 !--set kernel related quantities
341 !
342 hi1 = 1./hsmooth
343 hi21 = hi1*hi1
344 termnorm = weight(i)*fac*horigi
345 term = termnorm*dat(i) ! h gives the z length scale (NB: no perspective)
346 !
347 !--quantities for exact 3D wall integrals
348 !
349 dfac = horigi**3/(pixwidthx*pixwidthy*cnormk3D)
350 termnorm_exact = cnormk3D*weight(i)
351 term_exact = termnorm_exact*dat(i)
352 pixwidthz = 2.*radkern
353 zi = 0.; zpix = 0.
354 !
355 !--for each particle work out which pixels it contributes to
356 !
357 npixpartx = int(radkern/pixwidthx) + 1
358 npixparty = int(radkern/pixwidthy) + 1
359 jpixi = int((yi-ymin)/pixwidthy) + 1
360 ipixi = int((xi-xmin)/pixwidthx) + 1
361 ipixmin = ipixi - npixpartx
362 ipixmax = ipixi + npixpartx
363 jpixmin = jpixi - npixparty
364 jpixmax = jpixi + npixparty
365 !
366 !--loop over pixels, adding the contribution from this particle
367 ! copy by quarters if all pixels within domain
368 !
369 accelerate = useaccelerate .and. (.not.normalise) &
370 .and. npixpartx > 5 .and. npixparty > 5 &
371 .and. ipixmin >= 1 .and. ipixmax <= npixx &
372 .and. jpixmin >= 1 .and. jpixmax <= npixy
373
374 if (accelerate) then
375 !--adjust xi, yi to centre of pixel
376 xi = xminpix + ipixi*pixwidthx
377 yi = yminpix + jpixi*pixwidthy
378 !
379 !--precalculate an array of dx2 for this particle (optimisation)
380 !
381 do ipix=ipixmin,ipixmax
382 dx2i(ipix) = ((xpix(ipix) - xi)**2)*hi21
383 enddo
384 do jpix = jpixi,jpixmax
385 ypix = yminpix + jpix*pixwidthy
386 dy = ypix - yi
387 dy2 = dy*dy*hi21
388 do ipix = ipixi,ipixmax
389 q2 = dx2i(ipix) + dy2
390 !
391 !--SPH kernel - integral through cubic spline
392 ! interpolate from a pre-calculated table
393 !
394 if (q2 < radkernel2) then
395 wab = wfromtable(q2)
396 !
397 !--calculate data value at this pixel using the summation interpolant
398 !
399 !$omp atomic
400 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + term*wab
401 row(ipix) = term*wab
402 else
403 row(ipix) = 0.
404 endif
405 enddo
406 !--NB: the following actions can and should be vectorized (but I don't know how...)
407 !--copy top right -> top left
408 do ipix=ipixmin,ipixi-1
409 !$omp atomic
410 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + row(ipixmax-(ipix-ipixmin))
411 enddo
412 if (jpix /= jpixi) then
413 jpixcopy = jpixi - (jpix-jpixi)
414 !--copy top right -> bottom left
415 do ipix=ipixmin,ipixi-1
416 !$omp atomic
417 datsmooth(ipix,jpixcopy) = datsmooth(ipix,jpixcopy) + row(ipixmax-(ipix-ipixmin))
418 enddo
419 !--copy top right -> bottom right
420 do ipix=ipixi,ipixmax
421 !$omp atomic
422 datsmooth(ipix,jpixcopy) = datsmooth(ipix,jpixcopy) + row(ipix)
423 enddo
424 endif
425 enddo
426
427 else
428 ipixmin = int((xi - radkern - xmin)/pixwidthx)
429 ipixmax = int((xi + radkern - xmin)/pixwidthx) + 1
430 jpixmin = int((yi - radkern - ymin)/pixwidthy)
431 jpixmax = int((yi + radkern - ymin)/pixwidthy) + 1
432
433 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
434 if (jpixmin < 1) jpixmin = 1 ! to pixels in the image
435 if (ipixmax > npixx) ipixmax = npixx ! (note that this optimises
436 if (jpixmax > npixy) jpixmax = npixy ! much better than using min/max)
437 !
438 !--precalculate an array of dx2 for this particle (optimisation)
439 !
440 do ipix=ipixmin,ipixmax
441 dx2i(ipix) = ((xpix(ipix) - xi)**2)*hi21
442 enddo
443
444 do jpix = jpixmin,jpixmax
445 ypix = yminpix + jpix*pixwidthy
446 dy = ypix - yi
447 dy2 = dy*dy*hi21
448 do ipix = ipixmin,ipixmax
449 q2 = dx2i(ipix) + dy2 ! dx2 pre-calculated; dy2 pre-multiplied by hi21
450
451 if (exact_rendering .and. ipixmax-ipixmin <= 2 .and. q2 < radkernel2 + 3.*pixwidthx*pixwidthy*hi21) then
452 xpixi = xminpix + ipix*pixwidthx
453
454 ! Contribution of the cell walls in the xy-plane
455 pixint = 2.*wallint(0.5*pixwidthz,xi,yi,xpixi,ypix,pixwidthx,pixwidthy,hi)
456 !pixint = pixint + wallint(0.5*pixwidthz,xi,yi,xpixi,ypix,pixwidthx,pixwidthy,hi)
457
458 ! Contribution of the cell walls in the xz-plane
459 pixint = pixint + wallint(ypix-yi+0.5*pixwidthy,xi,zi,xpixi,zpix,pixwidthx,pixwidthz,hi)
460 pixint = pixint + wallint(yi-ypix+0.5*pixwidthy,xi,zi,xpixi,zpix,pixwidthx,pixwidthz,hi)
461
462 ! Contribution of the cell walls in the yz-plane
463 pixint = pixint + wallint(xpixi-xi+0.5*pixwidthx,zi,yi,zpix,ypix,pixwidthz,pixwidthy,hi)
464 pixint = pixint + wallint(xi-xpixi+0.5*pixwidthx,zi,yi,zpix,ypix,pixwidthz,pixwidthy,hi)
465
466 wab = pixint*dfac
467 !
468 !--calculate data value at this pixel using the summation interpolant
469 !
470 !$omp atomic
471 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + term_exact*wab
472 if (normalise) then
473 !$omp atomic
474 datnorm(ipix,jpix) = datnorm(ipix,jpix) + termnorm_exact*wab
475 endif
476 else
477 !
478 !--SPH kernel - integral through cubic spline
479 ! interpolate from a pre-calculated table
480 !
481 if (q2 < radkernel2) then
482 wab = wfromtable(q2)
483 !
484 !--calculate data value at this pixel using the summation interpolant
485 !
486 !$omp atomic
487 datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + term*wab
488
489 if (normalise) then
490 !$omp atomic
491 datnorm(ipix,jpix) = datnorm(ipix,jpix) + termnorm*wab
492 endif
493 endif
494 endif
495 enddo
496 enddo
497 endif
498
499 enddo over_particles
500 !$omp enddo
501 !$omp end parallel
502 !
503 !--normalise dat array
504 !
505 if (normalise) then
506 !--normalise everywhere (required if not using SPH weighting)
507 !where (datnorm > tiny(datnorm))
508 ! datsmooth = datsmooth/(datnorm)
509 !end where
510 !
511 ! compute the minimum possible value for datnorm, then multiply
512 ! with a kernel-softened version of 1/x to avoid dividing by zero
513 !
514 datnorm_min = minval(weight*hh,mask=(weight > tiny(0.)))
515 datsmooth = datsmooth*soft_func(datnorm,datnorm_min)
516 endif
517 !
518 !--warn about subgrid interpolation
519 !
520 if (nsubgrid > 1) then
521 nfull = int((xmax-xmin)/(hminall)) + 1
522 if (nsubgrid > 0.1*nok .and. iverbose >= -1) print "(a,i9,a,/,a,i6,a)",&
523 ' Warning: pixel size > 2h for ',nsubgrid,' particles', &
524 ' need',nfull,' pixels for full resolution'
525 endif
526 !
527 !--get/print timings
528 !
529 call wall_time(t_end)
530 t_used = t_end - t_start
531 if (t_used > 10. .and. iverbose >= 0) call print_time(t_used)
532
533 return
534
535 end subroutine interpolate3D_projection
536
537 !--------------------------------------------------------------------------
538 !
539 ! Same as previous but for a vector quantity
540 !
541 ! Input: particle coordinates : x,y (npart)
542 ! smoothing lengths : hh (npart)
543 ! weight for each particle : weight (npart)
544 ! vector data to smooth : vecx (npart)
545 ! vecy (npart)
546 !
547 ! Output: smoothed vector field : vecsmoothx (npixx,npixy)
548 ! : vecsmoothy (npixx,npixy)
549 !
550 ! Daniel Price 23/12/04
551 !--------------------------------------------------------------------------
552
553 subroutine interpolate3D_proj_vec(x,y,z,hh,weight,vecx,vecy,itype,npart,&
554 xmin,ymin,vecsmoothx,vecsmoothy,npixx,npixy,pixwidthx,pixwidthy,normalise,&
555 zobserver,dscreen,iverbose)
556
557 use kernels, only:radkernel,radkernel2
558 integer, intent(in) :: npart,npixx,npixy,iverbose
559 real, intent(in), dimension(npart) :: x,y,z,hh,weight,vecx,vecy
560 integer, intent(in), dimension(npart) :: itype
561 real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy,zobserver,dscreen
562 real, intent(out), dimension(npixx,npixy) :: vecsmoothx, vecsmoothy
563 logical, intent(in) :: normalise
564 real, dimension(:,:), allocatable :: datnorm
565
566 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax,ierr
567 real :: hi,hi1,hi21,radkern,q2,wab,rab2,const,zfrac,hsmooth
568 real :: termx,termy,termnorm,dx,dy,dy2,xpix,ypix
569
570 vecsmoothx = 0.
571 vecsmoothy = 0.
572 termx = 0.
573 termy = 0.
574 if (normalise) then
575 if (iverbose >= 0) print "(1x,a)",'projecting vector (normalised) from particles to pixels...'
576 allocate(datnorm(npixx,npixy),stat=ierr)
577 if (ierr /= 0) then
578 if (iverbose >= -1) print "(a)",'interpolate3D_proj_vec: error allocating memory'
579 return
580 endif
581 datnorm = 0.
582 else
583 if (iverbose >= 0) print "(1x,a)",'projecting vector from particles to pixels...'
584 endif
585 if (pixwidthx <= 0. .or. pixwidthy <= 0.) then
586 if (iverbose >= -1) print "(a)",'interpolate3D_proj_vec: error: pixel width <= 0'
587 return
588 endif
589 !
590 !--loop over particles
591 !
592 !$omp parallel default(none) &
593 !$omp shared(hh,z,x,y,weight,vecx,vecy,itype,vecsmoothx,vecsmoothy,npart) &
594 !$omp shared(xmin,ymin,pixwidthx,pixwidthy,zobserver,dscreen,datnorm) &
595 !$omp shared(npixx,npixy,normalise,radkernel,radkernel2) &
596 !$omp private(hi,radkern,const,zfrac,ypix,xpix) &
597 !$omp private(hsmooth,hi1,hi21,termx,termy,termnorm) &
598 !$omp private(ipixmin,ipixmax,jpixmin,jpixmax) &
599 !$omp private(dy,dy2,dx,rab2,q2,wab) &
600 !$omp private(i,ipix,jpix)
601
602 !$omp do schedule(guided, 2)
603 over_particles: do i=1,npart
604 !
605 !--skip particles with itype < 0
606 !
607 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_particles
608 !
609 !--set kernel related quantities
610 !
611 hi = hh(i)
612 const = weight(i)*hi ! h gives the z length scale (NB: no perspective)
613 if (hi <= 0.) then
614 cycle over_particles
615 elseif (abs(dscreen) > tiny(dscreen)) then
616 if (z(i) > zobserver) cycle over_particles
617 zfrac = abs(dscreen/(z(i)-zobserver))
618 hi = hi*zfrac
619 endif
620
621 !--take resolution length as max of h and 1/2 pixel width
622 hsmooth = max(hi,0.5*min(pixwidthx,pixwidthy))
623
624 radkern = radkernel*hsmooth ! radius of the smoothing kernel
625 hi1 = 1./hsmooth
626 hi21 = hi1*hi1
627
628 termx = const*vecx(i)
629 termy = const*vecy(i)
630 termnorm = const
631 !
632 !--for each particle work out which pixels it contributes to
633 !
634 ipixmin = int((x(i) - radkern - xmin)/pixwidthx)
635 jpixmin = int((y(i) - radkern - ymin)/pixwidthy)
636 ipixmax = int((x(i) + radkern - xmin)/pixwidthx) + 1
637 jpixmax = int((y(i) + radkern - ymin)/pixwidthy) + 1
638
639 ! PRINT*,'particle ',i,' x, y, z = ',x(i),y(i),z(i),dat(i),rho(i),hi
640 ! PRINT*,'pixels = ',ipixmin,ipixmax,jpixmin,jpixmax
641
642 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
643 if (jpixmin < 1) jpixmin = 1 ! to pixels in the image
644 if (ipixmax > npixx) ipixmax = npixx
645 if (jpixmax > npixy) jpixmax = npixy
646 !
647 !--loop over pixels, adding the contribution from this particle
648 !
649 do jpix = jpixmin,jpixmax
650 ypix = ymin + (jpix-0.5)*pixwidthy
651 dy = ypix - y(i)
652 dy2 = dy*dy
653 do ipix = ipixmin,ipixmax
654 xpix = xmin + (ipix-0.5)*pixwidthx
655 dx = xpix - x(i)
656 rab2 = dx**2 + dy2
657 q2 = rab2*hi21
658 !
659 !--SPH kernel - integral through cubic spline
660 ! interpolate from a pre-calculated table
661 !
662 if (q2 < radkernel2) then
663 wab = wfromtable(q2)
664 !
665 !--calculate data value at this pixel using the summation interpolant
666 !
667 !$omp atomic
668 vecsmoothx(ipix,jpix) = vecsmoothx(ipix,jpix) + termx*wab
669 !$omp atomic
670 vecsmoothy(ipix,jpix) = vecsmoothy(ipix,jpix) + termy*wab
671
672 if (normalise) then
673 !$omp atomic
674 datnorm(ipix,jpix) = datnorm(ipix,jpix) + termnorm*wab
675 endif
676 endif
677
678 enddo
679 enddo
680
681 enddo over_particles
682 !$omp enddo
683 !$omp end parallel
684
685 if (normalise .and. allocated(datnorm)) then
686 !--normalise everywhere
687 where (datnorm > tiny(datnorm))
688 vecsmoothx = vecsmoothx/datnorm
689 vecsmoothy = vecsmoothy/datnorm
690 end where
691 endif
692 if (allocated(datnorm)) deallocate(datnorm)
693
694 end subroutine interpolate3D_proj_vec
695
696 !--------------------------------------------------------------------------
697 !
698 ! Computes synchrotron emission (Stokes Q, U) for a given B field
699 ! at present assuming no faraday rotation
700 !
701 ! For references see:
702 !
703 ! Urbanik et al. (1997), A&A 326, 465
704 ! Sokoloff et al. (1998), MNRAS, 299, 189
705 ! Gomez & Cox (2004), ApJ 615, 744 (for Cosmic Ray distribution esp.)
706 !
707 ! Faraday rotation could be included easily but
708 ! I have not yet done so.
709 !
710 ! Input: particle coordinates : x,y (npart)
711 ! smoothing lengths : hh (npart)
712 ! weight for each particle : weight (npart)
713 ! vector data to smooth : vecx (npart)
714 ! vecy (npart)
715 !
716 ! Output: smoothed vector field : stokesQ (npixx,npixy)
717 ! : stokesU (npixx,npixy)
718 ! : stokesI (npixx,npixy)
719 !
720 ! DOES NOT WORK FOR ROTATED CONFIGURATIONS YET!!
721 ! (ie. z is assumed to be z_galaxy in the cosmic ray distribution)
722 !
723 ! Daniel Price 14/03/07
724 !--------------------------------------------------------------------------
725
726 subroutine interp3D_proj_vec_synctron(x,y,z,hh,weight,vecx,vecy,itype,npart,&
727 xmin,ymin,stokesQ,stokesU,stokesI,npixx,npixy,pixwidth,rcrit,zcrit,alpha, &
728 qpixwidth,getIonly,utherm,uthermcutoff)
729
730 use kernels, only:radkernel,radkernel2
731 integer, intent(in) :: npart,npixx,npixy
732 real, intent(in), dimension(npart) :: x,y,z,hh,weight,vecx,vecy
733 integer, intent(in), dimension(npart) :: itype
734 real, intent(in) :: xmin,ymin,pixwidth,rcrit,zcrit,alpha,qpixwidth
735 logical, intent(in) :: getIonly
736 real, intent(out), dimension(npixx,npixy) :: stokesQ,stokesU,stokesI
737 real, intent(in), dimension(npart), optional :: utherm
738 real, intent(in), optional :: uthermcutoff
739
740 integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax
741 real :: hi,hi1,hi21,radkern,q2,wab,const,hsmooth
742 real :: termx,termy,term,dy,dy2,ypix,xi,yi,zi
743 real :: crdens,emissivity,Bperp,angle,pintrinsic,rcyl
744 real, dimension(npixx) :: dx2i
745
746 if (getIonly) then
747 stokesI = 0.
748 else
749 stokesU = 0.
750 stokesQ = 0.
751 endif
752 termx = 0.
753 termy = 0.
754 term = 0.
755 pintrinsic = (3. + 3.*alpha)/(5. + 3.*alpha)
756
757 if (getIonly) then
758 print "(1x,a)",'getting synchrotron intensity map from B field...'
759 else
760 print "(1x,a)",'getting synchrotron polarisation map from B field...'
761 print*,' assuming cosmic ray electron distribution exp(-r/',rcrit,' -z/',zcrit,') (kpc)'
762 print*,' synchrotron spectral index I_nu = nu^-',alpha
763 print*,' intrinsic polarisation fraction = ',pintrinsic
764 endif
765 if (present(utherm) .and. present(uthermcutoff)) then
766 print*,' using only particles with utherm > ',uthermcutoff
767 endif
768
769 if (pixwidth <= 0.) then
770 print "(a)",'interpolate3D_proj_vec_synchrotron: error: pixel width <= 0'
771 return
772 endif
773 !
774 !--loop over particles
775 !
776 !$omp parallel default(none) &
777 !$omp shared(hh,z,x,y,weight,vecx,vecy,itype,stokesq,stokesu,stokesi,npart) &
778 !$omp shared(xmin,ymin,pixwidth,rcrit,zcrit,alpha,radkernel,radkernel2) &
779 !$omp shared(npixx,npixy,pintrinsic,qpixwidth,getionly,utherm,uthermcutoff) &
780 !$omp private(hi,xi,yi,zi,radkern,const) &
781 !$omp private(hsmooth,hi1,hi21,term,termx,termy) &
782 !$omp private(rcyl,crdens,bperp,emissivity,angle) &
783 !$omp private(ipixmin,ipixmax,jpixmin,jpixmax) &
784 !$omp private(dy,dy2,dx2i,ypix,q2,wab) &
785 !$omp private(i,ipix,jpix)
786
787 !$omp do schedule(guided, 2)
788 over_particles: do i=1,npart
789 !
790 !--skip particles with itype < 0
791 !
792 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_particles
793 !
794 !--skip particles with utherm < uthermcutoff
795 !
796 if (present(utherm) .and. present(uthermcutoff)) then
797 if (utherm(i) < uthermcutoff) cycle over_particles
798 endif
799 !
800 !--set kernel related quantities
801 !
802 hi = hh(i)
803 if (hi <= 0.) cycle over_particles
804 const = weight(i)*hi ! h gives the z length scale (NB: no perspective)
805 zi = z(i)
806
807 !--take resolution length as max of h and 1/2 pixel width
808 ! (for intensity calculation, qpixwidth is pixel width of Q,U calculation)
809 hsmooth = max(hi,0.5*pixwidth,0.5*qpixwidth)
810 hi1 = 1./hsmooth
811 hi21 = hi1*hi1
812 radkern = radkernel*hsmooth ! radius of the smoothing kernel
813 xi = x(i)
814 yi = y(i)
815
816 !--assumed distribution of cosmic ray electrons in galaxy
817 ! (should use UNROTATED x,y if rotation added)
818 rcyl = sqrt(xi**2 + yi**2)
819 crdens = exp(-rcyl/rcrit - abs(zi)/zcrit)
820
821 !--calculate synchrotron emissivity based on Bperp and a spectral index alpha
822 Bperp = sqrt(vecx(i)**2 + vecy(i)**2)
823 emissivity = crdens*Bperp**(1. + alpha)
824
825 if (getIonly) then
826 term = emissivity*const
827 termx = 0.
828 termy = 0.
829 else
830 term = 0.
831 !--faraday rotation would change angle here
832 angle = atan2(vecy(i),vecx(i))
833 termx = pintrinsic*emissivity*const*cos(angle)
834 termy = pintrinsic*emissivity*const*sin(angle)
835 endif
836 !
837 !--for each particle work out which pixels it contributes to
838 !
839 ipixmin = int((xi - radkern - xmin)/pixwidth)
840 jpixmin = int((yi - radkern - ymin)/pixwidth)
841 ipixmax = int((xi + radkern - xmin)/pixwidth) + 1
842 jpixmax = int((yi + radkern - ymin)/pixwidth) + 1
843
844 ! PRINT*,'particle ',i,' x, y, z = ',x(i),y(i),z(i),dat(i),rho(i),hi
845 ! PRINT*,'pixels = ',ipixmin,ipixmax,jpixmin,jpixmax
846
847 if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute
848 if (jpixmin < 1) jpixmin = 1 ! to pixels in the image
849 if (ipixmax > npixx) ipixmax = npixx
850 if (jpixmax > npixy) jpixmax = npixy
851
852 !
853 !--precalculate an array of dx2 for this particle (optimisation)
854 !
855 do ipix=ipixmin,ipixmax
856 dx2i(ipix) = ((xmin + (ipix-0.5)*pixwidth - xi)**2)*hi21
857 enddo
858 !
859 !--loop over pixels, adding the contribution from this particle
860 !
861 do jpix = jpixmin,jpixmax
862 ypix = ymin + (jpix-0.5)*pixwidth
863 dy = ypix - yi
864 dy2 = dy*dy*hi21
865 do ipix = ipixmin,ipixmax
866 !xpix = xmin + (ipix-0.5)*pixwidth
867 !dx = xpix - xi
868 q2 = dx2i(ipix) + dy2
869 !
870 !--SPH kernel - integral through cubic spline
871 ! interpolate from a pre-calculated table
872 !
873 if (q2 < radkernel2) then
874 wab = wfromtable(q2)
875 !
876 !--calculate data value at this pixel using the summation interpolant
877 !
878 if (getIonly) then
879 !$omp atomic
880 stokesI(ipix,jpix) = stokesI(ipix,jpix) + term*wab
881 else
882 !$omp atomic
883 stokesQ(ipix,jpix) = stokesQ(ipix,jpix) + termx*wab
884 !$omp atomic
885 stokesU(ipix,jpix) = stokesU(ipix,jpix) + termy*wab
886 endif
887 endif
888
889 enddo
890 enddo
891
892 enddo over_particles
893 !$omp enddo
894 !$omp end parallel
895
896 return
897
898 end subroutine interp3D_proj_vec_synctron
899
900 !--------------------------------------------------------------------------
901 !
902 ! Same as previous but for column integrated quantities on
903 ! a collection of irregular points
904 !
905 ! Input: particle coordinates : x,y,z (npart)
906 ! smoothing lengths : hh (npart)
907 ! weight for each particle : weight (npart)
908 ! vector data to smooth : dat (npart)
909 ! vecy (npart)
910 !
911 ! Output: smoothed vector field : datsmooth (npoints)
912 !
913 ! Daniel Price 2023
914 !--------------------------------------------------------------------------
915
916 subroutine interpolate3D_proj_points(x,y,z,hh,weight,dat,itype,npart,&
917 xpoints,ypoints,zpoints,datsmooth,npoints,normalise,iverbose)
918
919 use kernels, only:radkernel2
920 integer, intent(in) :: npart,npoints,iverbose
921 real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat
922 integer, intent(in), dimension(npart) :: itype
923 real, intent(in), dimension(npoints) :: xpoints,ypoints,zpoints
924 real, intent(out), dimension(npoints) :: datsmooth
925 logical, intent(in) :: normalise
926 real, dimension(:), allocatable :: datnorm
927
928 integer :: i,j,ierr
929 real :: hi,hi1,hi21,q2,wab,rab2
930 real :: term,termnorm,dx,dy,dz
931
932 datsmooth = 0.
933 term = 0.
934 if (normalise) then
935 if (iverbose >= 0) print "(1x,a)",'projecting (normalised) from particles to points...'
936 allocate(datnorm(npoints),stat=ierr)
937 if (ierr /= 0) then
938 if (iverbose >= -1) print "(a)",'interpolate3D_proj_points: error allocating memory'
939 return
940 endif
941 datnorm = 0.
942 else
943 if (iverbose >= 0) print "(1x,a)",'projecting from particles to points...'
944 endif
945 !
946 !--check column density table has actually been setup
947 !
948 if (.not.have_setup_kernel) call setup_integratedkernel
949 !
950 !--loop over particles
951 !
952 !$omp parallel default(none) &
953 !$omp shared(hh,z,x,y,weight,dat,itype,datsmooth,npart) &
954 !$omp shared(xpoints,ypoints,zpoints,datnorm) &
955 !$omp shared(npoints,normalise,radkernel2) &
956 !$omp private(hi,hi1,hi21,term,termnorm) &
957 !$omp private(i,j) &
958 !$omp private(dz,dy,dx,rab2,q2,wab)
959 !$omp do schedule(guided, 2)
960 over_particles: do i=1,npart
961 !
962 !--skip particles with itype < 0
963 !
964 if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_particles
965 !
966 !--set kernel related quantities
967 !
968 hi = hh(i)
969 if (hi <= 0.) cycle over_particles
970
971 hi1 = 1./hi
972 hi21 = hi1*hi1
973
974 termnorm = weight(i)*hi
975 term = termnorm*dat(i)
976
977 !
978 !--loop over points, adding the contribution from this particle
979 !
980 over_points: do j = 1,npoints
981 dz = zpoints(j) - z(i)
982 if (dz > 0.) cycle over_points ! skip if point is behind particle
983 dy = ypoints(j) - y(i)
984 dx = xpoints(j) - x(i)
985
986 rab2 = dx*dx + dy*dy
987 q2 = rab2*hi21
988 !
989 !--SPH kernel - integral through cubic spline
990 ! interpolate from a pre-calculated table
991 !
992 if (q2 < radkernel2) then
993 wab = wfromtable(q2)
994 !
995 !--calculate data value at this pixel using the summation interpolant
996 !
997 !$omp atomic
998 datsmooth(j) = datsmooth(j) + term*wab
999
1000 if (normalise) then
1001 !$omp atomic
1002 datnorm(j) = datnorm(j) + termnorm*wab
1003 endif
1004 endif
1005 enddo over_points
1006
1007 enddo over_particles
1008 !$omp enddo
1009 !$omp end parallel
1010
1011 if (normalise .and. allocated(datnorm)) then
1012 !--normalise everywhere
1013 where (datnorm > tiny(datnorm))
1014 datsmooth = datsmooth/datnorm
1015 end where
1016 endif
1017 if (allocated(datnorm)) deallocate(datnorm)
1018
1019 end subroutine interpolate3D_proj_points
1020
1021 end module projections3D