| 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-2012 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!---------------------------------------------------------------------- |
| 24 |
! |
| 25 |
! Module containing all of the routines required for cross sections |
| 26 |
! through 3D data |
| 27 |
! |
| 28 |
!---------------------------------------------------------------------- |
| 29 |
|
| 30 |
module xsections3D |
| 31 |
use kernels, only:cnormk3D,radkernel,radkernel2,wfunc,select_kernel |
| 32 |
implicit none |
| 33 |
public :: interpolate3D_fastxsec, interpolate3D_xsec_vec |
| 34 |
|
| 35 |
contains |
| 36 |
|
| 37 |
!-------------------------------------------------------------------------- |
| 38 |
! |
| 39 |
! ** In this version 3D data is interpolated to a single 2D cross section |
| 40 |
! ** This is much faster than interpolating to a 3D grid |
| 41 |
! ** and is efficient if only one or two cross sections are needed. |
| 42 |
! |
| 43 |
! ** Note that the cross section is always taken in the z co-ordinate |
| 44 |
! ** so should submit the appropriate arrays as x, y and z. |
| 45 |
! |
| 46 |
! Input: particle coordinates : x,y,z (npart) |
| 47 |
! particle masses : pmass (npart) |
| 48 |
! density on particles : rho (npart) - must be computed separately |
| 49 |
! smoothing lengths : hh (npart) - could be computed from density |
| 50 |
! scalar data to smooth : dat (npart) |
| 51 |
! cross section location: zslice |
| 52 |
! |
| 53 |
! Output: smoothed data : datsmooth (npixx,npixy) |
| 54 |
! |
| 55 |
! Daniel Price, Institute of Astronomy, Cambridge, 23/9/03 |
| 56 |
!-------------------------------------------------------------------------- |
| 57 |
|
| 58 |
subroutine interpolate3D_fastxsec(x,y,z,hh,weight,dat,itype,npart,& |
| 59 |
xmin,ymin,zslice,datsmooth,npixx,npixy,pixwidthx,pixwidthy,normalise,iverbose) |
| 60 |
|
| 61 |
integer, intent(in) :: npart,npixx,npixy,iverbose |
| 62 |
real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat |
| 63 |
integer, intent(in), dimension(npart) :: itype |
| 64 |
real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy,zslice |
| 65 |
real, intent(out), dimension(npixx,npixy) :: datsmooth |
| 66 |
logical, intent(in) :: normalise |
| 67 |
real, dimension(npixx,npixy) :: datnorm |
| 68 |
|
| 69 |
integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax |
| 70 |
real :: hi,hi1,radkern,q2,wab,const,xi,yi,hi21 |
| 71 |
real :: termnorm,term,dy,dy2,dz,dz2,ypix,rescalefac |
| 72 |
real, dimension(npixx) :: dx2i |
| 73 |
|
| 74 |
! force cubic kernel if not already set |
| 75 |
if (.not.associated(wfunc)) call select_kernel(0) |
| 76 |
|
| 77 |
datsmooth = 0. |
| 78 |
datnorm = 0. |
| 79 |
if (iverbose >= 0) then |
| 80 |
if (normalise) then |
| 81 |
print*,'taking fast cross section (normalised)...',zslice |
| 82 |
else |
| 83 |
print*,'taking fast cross section (non-normalised)...',zslice |
| 84 |
endif |
| 85 |
endif |
| 86 |
if (pixwidthx <= 0. .or. pixwidthy <= 0. .and. iverbose >= -1) then |
| 87 |
print*,'interpolate3D_xsec: error: pixel width <= 0' |
| 88 |
return |
| 89 |
elseif (npart <= 0 .and. iverbose >= -1) then |
| 90 |
print*,'interpolate3D_xsec: error: npart = 0' |
| 91 |
return |
| 92 |
endif |
| 93 |
if (any(hh(1:npart) <= tiny(hh)) .and. iverbose >= -1) then |
| 94 |
print*,'interpolate3D_xsec: WARNING: ignoring some or all particles with h < 0' |
| 95 |
endif |
| 96 |
const = cnormk3D |
| 97 |
! |
| 98 |
!--renormalise dat array by first element to speed things up |
| 99 |
! |
| 100 |
if (dat(1) > tiny(dat)) then |
| 101 |
rescalefac = dat(1) |
| 102 |
else |
| 103 |
rescalefac = 1.0 |
| 104 |
endif |
| 105 |
! |
| 106 |
!--loop over particles |
| 107 |
! |
| 108 |
over_parts: do i=1,npart |
| 109 |
! |
| 110 |
!--skip particles with itype < 0 |
| 111 |
! |
| 112 |
if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts |
| 113 |
! |
| 114 |
!--set kernel related quantities |
| 115 |
! |
| 116 |
hi = hh(i) |
| 117 |
if (hi <= 0.) cycle over_parts |
| 118 |
hi1 = 1./hi |
| 119 |
hi21 = hi1*hi1 |
| 120 |
radkern = radkernel*hi ! radius of the smoothing kernel |
| 121 |
! |
| 122 |
!--for each particle, work out distance from the cross section slice. |
| 123 |
! |
| 124 |
dz = zslice - z(i) |
| 125 |
dz2 = dz**2*hi21 |
| 126 |
! |
| 127 |
!--if this is < 2h then add the particle's contribution to the pixels |
| 128 |
! otherwise skip all this and start on the next particle |
| 129 |
! |
| 130 |
if (dz2 < radkernel2) then |
| 131 |
|
| 132 |
xi = x(i) |
| 133 |
yi = y(i) |
| 134 |
termnorm = const*weight(i) |
| 135 |
term = termnorm*dat(i)/rescalefac |
| 136 |
! |
| 137 |
!--for each particle work out which pixels it contributes to |
| 138 |
! |
| 139 |
ipixmin = int((xi - radkern - xmin)/pixwidthx) |
| 140 |
jpixmin = int((yi - radkern - ymin)/pixwidthy) |
| 141 |
ipixmax = int((xi + radkern - xmin)/pixwidthx) + 1 |
| 142 |
jpixmax = int((yi + radkern - ymin)/pixwidthy) + 1 |
| 143 |
|
| 144 |
if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute |
| 145 |
if (jpixmin < 1) jpixmin = 1 ! to pixels in the image |
| 146 |
if (ipixmax > npixx) ipixmax = npixx |
| 147 |
if (jpixmax > npixy) jpixmax = npixy |
| 148 |
! |
| 149 |
!--precalculate an array of dx2 for this particle (optimisation) |
| 150 |
! |
| 151 |
do ipix=ipixmin,ipixmax |
| 152 |
dx2i(ipix) = ((xmin + (ipix-0.5)*pixwidthx - xi)**2)*hi21 + dz2 |
| 153 |
enddo |
| 154 |
! |
| 155 |
!--loop over pixels, adding the contribution from this particle |
| 156 |
! |
| 157 |
do jpix = jpixmin,jpixmax |
| 158 |
ypix = ymin + (jpix-0.5)*pixwidthy |
| 159 |
dy = ypix - yi |
| 160 |
dy2 = dy*dy*hi21 |
| 161 |
do ipix = ipixmin,ipixmax |
| 162 |
q2 = dx2i(ipix) + dy2 |
| 163 |
! |
| 164 |
!--SPH kernel - standard cubic spline |
| 165 |
! |
| 166 |
if (q2 < radkernel2) then |
| 167 |
wab = wfunc(q2) |
| 168 |
! |
| 169 |
!--calculate data value at this pixel using the summation interpolant |
| 170 |
! |
| 171 |
datsmooth(ipix,jpix) = datsmooth(ipix,jpix) + term*wab |
| 172 |
if (normalise) datnorm(ipix,jpix) = datnorm(ipix,jpix) + termnorm*wab |
| 173 |
|
| 174 |
endif |
| 175 |
|
| 176 |
enddo |
| 177 |
enddo |
| 178 |
|
| 179 |
endif ! if particle within 2h of slice |
| 180 |
enddo over_parts ! over particles |
| 181 |
! |
| 182 |
!--normalise dat array |
| 183 |
! |
| 184 |
if (normalise) then |
| 185 |
!--normalise everywhere (required if not using SPH weighting) |
| 186 |
where (datnorm > tiny(datnorm)) |
| 187 |
datsmooth = datsmooth/datnorm |
| 188 |
end where |
| 189 |
endif |
| 190 |
datsmooth = datsmooth*rescalefac |
| 191 |
|
| 192 |
return |
| 193 |
|
| 194 |
end subroutine interpolate3D_fastxsec |
| 195 |
|
| 196 |
!-------------------------------------------------------------------------- |
| 197 |
! program to interpolate from particle data to even grid of pixels |
| 198 |
! |
| 199 |
! The data is smoothed using the SPH summation interpolant, |
| 200 |
! that is, we compute the smoothed array according to |
| 201 |
! |
| 202 |
! datsmooth(pixel) = sum_b m_b dat_b/rho_b W(r-r_b, h_b) |
| 203 |
! |
| 204 |
! where _b is the quantity at the neighbouring particle b and |
| 205 |
! W is the smoothing kernel, for which we use the usual cubic spline |
| 206 |
! |
| 207 |
! ** In this version 3D data is interpolated to a single 2D cross section |
| 208 |
! ** This is much faster than interpolating to a 3D grid |
| 209 |
! ** and is efficient if only one or two cross sections are needed. |
| 210 |
! |
| 211 |
! ** Note that the cross section is always taken in the z co-ordinate |
| 212 |
! ** so should submit the appropriate arrays as x, y and z. |
| 213 |
! |
| 214 |
! Input: particle coordinates : x,y,z (npart) |
| 215 |
! particle masses : pmass (npart) |
| 216 |
! density on particles : rho (npart) - must be computed separately |
| 217 |
! smoothing lengths : hh (npart) - could be computed from density |
| 218 |
! vector data to smooth : vecx (npart) |
| 219 |
! vecy (npart) |
| 220 |
! cross section location: zslice |
| 221 |
! |
| 222 |
! Output: smoothed vector field : vecsmoothx (npixx,npixy) |
| 223 |
! : vecsmoothy (npixx,npixy) |
| 224 |
! |
| 225 |
! Daniel Price, Institute of Astronomy, Cambridge, 23/9/03 |
| 226 |
!-------------------------------------------------------------------------- |
| 227 |
|
| 228 |
subroutine interpolate3D_xsec_vec(x,y,z,hh,weight,vecx,vecy,itype,npart,& |
| 229 |
xmin,ymin,zslice,vecsmoothx,vecsmoothy,npixx,npixy,pixwidthx,pixwidthy,normalise,iverbose) |
| 230 |
|
| 231 |
integer, intent(in) :: npart,npixx,npixy,iverbose |
| 232 |
real, intent(in), dimension(npart) :: x,y,z,hh,weight,vecx,vecy |
| 233 |
integer, intent(in), dimension(npart) :: itype |
| 234 |
real, intent(in) :: xmin,ymin,pixwidthx,pixwidthy,zslice |
| 235 |
real, intent(out), dimension(npixx,npixy) :: vecsmoothx, vecsmoothy |
| 236 |
logical, intent(in) :: normalise |
| 237 |
real, dimension(npixx,npixy) :: datnorm |
| 238 |
|
| 239 |
integer :: i,ipix,jpix,ipixmin,ipixmax,jpixmin,jpixmax |
| 240 |
real :: hi,hi1,radkern,q2,wab,const |
| 241 |
real :: termx,termy,termnorm,dx,dy,dz,dz2,xpix,ypix |
| 242 |
|
| 243 |
! force cubic kernel if not already set |
| 244 |
if (.not.associated(wfunc)) call select_kernel(0) |
| 245 |
|
| 246 |
vecsmoothx = 0. |
| 247 |
vecsmoothy = 0. |
| 248 |
datnorm = 0. |
| 249 |
if (iverbose >= 0) then |
| 250 |
if (normalise) then |
| 251 |
print*,'taking fast cross section (normalised)...',zslice |
| 252 |
else |
| 253 |
print*,'taking fast cross section (non-normalised)...',zslice |
| 254 |
endif |
| 255 |
endif |
| 256 |
if (pixwidthx <= 0. .or. pixwidthy <= 0. .and. iverbose >= -1) then |
| 257 |
print*,'interpolate3D_xsec_vec: error: pixel width <= 0' |
| 258 |
return |
| 259 |
endif |
| 260 |
if (any(hh(1:npart) <= tiny(hh)) .and. iverbose >= -1) then |
| 261 |
print*,'interpolate3D_xsec_vec: WARNING: ignoring some or all particles with h < 0' |
| 262 |
endif |
| 263 |
const = cnormk3D ! normalisation constant (3D) |
| 264 |
! |
| 265 |
!--loop over particles |
| 266 |
! |
| 267 |
over_parts: do i=1,npart |
| 268 |
! |
| 269 |
!--skip particles with itype < 0 |
| 270 |
! |
| 271 |
if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts |
| 272 |
! |
| 273 |
!--set kernel related quantities |
| 274 |
! |
| 275 |
hi = hh(i) |
| 276 |
if (hi <= 0.) cycle over_parts |
| 277 |
hi1 = 1./hi |
| 278 |
radkern = radkernel*hi ! radius of the smoothing kernel |
| 279 |
! |
| 280 |
!--for each particle, work out distance from the cross section slice. |
| 281 |
! |
| 282 |
dz = zslice - z(i) |
| 283 |
dz2 = dz**2 |
| 284 |
! |
| 285 |
!--if this is < 2h then add the particle's contribution to the pixels |
| 286 |
! otherwise skip all this and start on the next particle |
| 287 |
! |
| 288 |
if (abs(dz) < radkern) then |
| 289 |
termnorm = const*weight(i) |
| 290 |
termx = termnorm*vecx(i) |
| 291 |
termy = termnorm*vecy(i) |
| 292 |
! |
| 293 |
!--for each particle work out which pixels it contributes to |
| 294 |
! |
| 295 |
ipixmin = int((x(i) - radkern - xmin)/pixwidthx) |
| 296 |
jpixmin = int((y(i) - radkern - ymin)/pixwidthy) |
| 297 |
ipixmax = int((x(i) + radkern - xmin)/pixwidthx) + 1 |
| 298 |
jpixmax = int((y(i) + radkern - ymin)/pixwidthy) + 1 |
| 299 |
|
| 300 |
if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute |
| 301 |
if (jpixmin < 1) jpixmin = 1 ! to pixels in the image |
| 302 |
if (ipixmax > npixx) ipixmax = npixx |
| 303 |
if (jpixmax > npixy) jpixmax = npixy |
| 304 |
! |
| 305 |
!--loop over pixels, adding the contribution from this particle |
| 306 |
! |
| 307 |
do jpix = jpixmin,jpixmax |
| 308 |
ypix = ymin + (jpix-0.5)*pixwidthy |
| 309 |
dy = ypix - y(i) |
| 310 |
do ipix = ipixmin,ipixmax |
| 311 |
xpix = xmin + (ipix-0.5)*pixwidthx |
| 312 |
dx = xpix - x(i) |
| 313 |
q2 = (dx*dx + dy*dy + dz2)*hi1*hi1 |
| 314 |
! |
| 315 |
!--SPH kernel - standard cubic spline |
| 316 |
! |
| 317 |
if (q2 < radkernel2) then |
| 318 |
wab = wfunc(q2) |
| 319 |
! |
| 320 |
!--calculate data value at this pixel using the summation interpolant |
| 321 |
! |
| 322 |
vecsmoothx(ipix,jpix) = vecsmoothx(ipix,jpix) + termx*wab |
| 323 |
vecsmoothy(ipix,jpix) = vecsmoothy(ipix,jpix) + termy*wab |
| 324 |
if (normalise) datnorm(ipix,jpix) = datnorm(ipix,jpix) + termnorm*wab |
| 325 |
endif |
| 326 |
|
| 327 |
enddo |
| 328 |
enddo |
| 329 |
|
| 330 |
endif ! if particle within 2h of slice |
| 331 |
enddo over_parts ! over particles |
| 332 |
! |
| 333 |
!--normalise dat array(s) |
| 334 |
! |
| 335 |
if (normalise) then |
| 336 |
where (datnorm > tiny(datnorm)) |
| 337 |
vecsmoothx = vecsmoothx/datnorm |
| 338 |
vecsmoothy = vecsmoothy/datnorm |
| 339 |
end where |
| 340 |
endif |
| 341 |
|
| 342 |
return |
| 343 |
|
| 344 |
end subroutine interpolate3D_xsec_vec |
| 345 |
|
| 346 |
end module xsections3D |