| 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 (SLOW!) |
| 27 |
! |
| 28 |
!---------------------------------------------------------------------- |
| 29 |
|
| 30 |
module interpolations3D |
| 31 |
use kernels, only:radkernel2,radkernel,cnormk3D,wallint |
| 32 |
use interpolation, only:iroll |
| 33 |
use timing, only:wall_time,print_time |
| 34 |
implicit none |
| 35 |
integer, parameter :: doub_prec = kind(0.d0) |
| 36 |
public :: interpolate3D,interpolate3D_vec |
| 37 |
|
| 38 |
contains |
| 39 |
!-------------------------------------------------------------------------- |
| 40 |
! subroutine to interpolate from particle data to even grid of pixels |
| 41 |
! |
| 42 |
! The data is interpolated according to the formula |
| 43 |
! |
| 44 |
! datsmooth(pixel) = sum_b weight_b dat_b W(r-r_b, h_b) |
| 45 |
! |
| 46 |
! where _b is the quantity at the neighbouring particle b and |
| 47 |
! W is the smoothing kernel, for which we use the usual cubic spline. |
| 48 |
! |
| 49 |
! For a standard SPH smoothing the weight function for each particle should be |
| 50 |
! |
| 51 |
! weight = pmass/(rho*h^3) |
| 52 |
! |
| 53 |
! this version is written for slices through a rectangular volume, ie. |
| 54 |
! assumes a uniform pixel size in x,y, whilst the number of pixels |
| 55 |
! in the z direction can be set to the number of cross-section slices. |
| 56 |
! |
| 57 |
! Input: particle coordinates : x,y,z (npart) |
| 58 |
! smoothing lengths : hh (npart) |
| 59 |
! weight for each particle : weight (npart) |
| 60 |
! scalar data to smooth : dat (npart) |
| 61 |
! |
| 62 |
! Output: smoothed data : datsmooth (npixx,npixy,npixz) |
| 63 |
! |
| 64 |
! Daniel Price, Institute of Astronomy, Cambridge 16/7/03 |
| 65 |
! Revised for "splash to grid", Monash University 02/11/09 |
| 66 |
! Maya Petkova contributed exact subgrid interpolation, April 2019 |
| 67 |
!-------------------------------------------------------------------------- |
| 68 |
|
| 69 |
subroutine interpolate3D(x,y,z,hh,weight,dat,itype,npart,& |
| 70 |
xmin,ymin,zmin,datsmooth,npixx,npixy,npixz,pixwidthx,pixwidthy,pixwidthz,& |
| 71 |
normalise,periodicx,periodicy,periodicz) |
| 72 |
|
| 73 |
integer, intent(in) :: npart,npixx,npixy,npixz |
| 74 |
real, intent(in), dimension(npart) :: x,y,z,hh,weight,dat |
| 75 |
integer, intent(in), dimension(npart) :: itype |
| 76 |
real, intent(in) :: xmin,ymin,zmin,pixwidthx,pixwidthy,pixwidthz |
| 77 |
real(doub_prec), intent(out), dimension(npixx,npixy,npixz) :: datsmooth |
| 78 |
logical, intent(in) :: normalise,periodicx,periodicy,periodicz |
| 79 |
real(doub_prec), allocatable :: datnorm(:,:,:) |
| 80 |
|
| 81 |
integer :: i,ipix,jpix,kpix |
| 82 |
integer :: iprintinterval,iprintnext |
| 83 |
integer :: ipixmin,ipixmax,jpixmin,jpixmax,kpixmin,kpixmax |
| 84 |
integer :: ipixi,jpixi,kpixi,nxpix,nwarn,threadid |
| 85 |
real :: xminpix,yminpix,zminpix,hmin !,dhmin3 |
| 86 |
real, dimension(npixx) :: dx2i |
| 87 |
real :: xi,yi,zi,hi,hi1,hi21,radkern,wab,q2,const,dyz2,dz2 |
| 88 |
real :: term,termnorm,dy,dz,ypix,zpix,xpixi,pixwidthmax,dfac |
| 89 |
real :: t_start,t_end,t_used |
| 90 |
logical :: iprintprogress |
| 91 |
|
| 92 |
! Exact rendering |
| 93 |
real :: pixint, wint |
| 94 |
logical, parameter :: exact_rendering = .true. ! use exact rendering y/n |
| 95 |
integer :: usedpart, negflag |
| 96 |
|
| 97 |
!$ integer :: omp_get_num_threads,omp_get_thread_num |
| 98 |
integer(kind=selected_int_kind(10)) :: iprogress,j ! up to 10 digits |
| 99 |
|
| 100 |
if (exact_rendering) then |
| 101 |
print "(1x,a)",'interpolating to 3D grid (exact/Petkova+2018 on subgrid) ...' |
| 102 |
elseif (normalise) then |
| 103 |
print "(1x,a)",'interpolating to 3D grid (normalised) ...' |
| 104 |
else |
| 105 |
print "(1x,a)",'interpolating to 3D grid (non-normalised) ...' |
| 106 |
endif |
| 107 |
if (pixwidthx <= 0. .or. pixwidthy <= 0 .or. pixwidthz <= 0) then |
| 108 |
print "(1x,a)",'interpolate3D: error: pixel width <= 0' |
| 109 |
return |
| 110 |
endif |
| 111 |
!if (any(hh(1:npart) <= tiny(hh))) then |
| 112 |
! print*,'interpolate3D: WARNING: ignoring some or all particles with h < 0' |
| 113 |
!endif |
| 114 |
|
| 115 |
call wall_time(t_start) |
| 116 |
|
| 117 |
datsmooth = 0. |
| 118 |
if (normalise) then |
| 119 |
allocate(datnorm(npixx,npixy,npixz)) |
| 120 |
datnorm = 0. |
| 121 |
endif |
| 122 |
! |
| 123 |
!--print a progress report if it is going to take a long time |
| 124 |
! (a "long time" is, however, somewhat system dependent) |
| 125 |
! |
| 126 |
iprintprogress = (npart >= 100000) .or. (npixx*npixy > 100000) !.or. exact_rendering |
| 127 |
! |
| 128 |
!--loop over particles |
| 129 |
! |
| 130 |
iprintinterval = 25 |
| 131 |
if (npart >= 1e6) iprintinterval = 10 |
| 132 |
iprintnext = iprintinterval |
| 133 |
! |
| 134 |
!--get starting CPU time |
| 135 |
! |
| 136 |
!call cpu_time(t_start) |
| 137 |
|
| 138 |
usedpart = 0 |
| 139 |
|
| 140 |
xminpix = xmin - 0.5*pixwidthx |
| 141 |
yminpix = ymin - 0.5*pixwidthy |
| 142 |
zminpix = zmin - 0.5*pixwidthz |
| 143 |
pixwidthmax = max(pixwidthx,pixwidthy,pixwidthz) |
| 144 |
! |
| 145 |
!--use a minimum smoothing length on the grid to make |
| 146 |
! sure that particles contribute to at least one pixel |
| 147 |
! |
| 148 |
hmin = 0.5*pixwidthmax |
| 149 |
!dhmin3 = 1./(hmin*hmin*hmin) |
| 150 |
|
| 151 |
const = cnormk3D ! normalisation constant (3D) |
| 152 |
nwarn = 0 |
| 153 |
j = 0_8 |
| 154 |
threadid = 1 |
| 155 |
! |
| 156 |
!--loop over particles |
| 157 |
! |
| 158 |
!$omp parallel default(none) & |
| 159 |
!$omp shared(hh,z,x,y,weight,dat,itype,datsmooth,npart) & |
| 160 |
!$omp shared(xmin,ymin,zmin,radkernel,radkernel2) & |
| 161 |
!$omp shared(xminpix,yminpix,zminpix,pixwidthx,pixwidthy,pixwidthz) & |
| 162 |
!$omp shared(npixx,npixy,npixz,const) & |
| 163 |
!$omp shared(datnorm,normalise,periodicx,periodicy,periodicz) & |
| 164 |
!$omp shared(hmin,pixwidthmax) & |
| 165 |
!$omp shared(iprintprogress,iprintinterval,j) & |
| 166 |
!$omp private(hi,xi,yi,zi,radkern,hi1,hi21) & |
| 167 |
!$omp private(term,termnorm,xpixi,iprogress) & |
| 168 |
!$omp private(ipixmin,ipixmax,jpixmin,jpixmax,kpixmin,kpixmax) & |
| 169 |
!$omp private(ipix,jpix,kpix,ipixi,jpixi,kpixi) & |
| 170 |
!$omp private(dx2i,nxpix,zpix,dz,dz2,dyz2,dy,ypix,q2,wab) & |
| 171 |
!$omp private(pixint,wint,negflag,dfac,threadid) & |
| 172 |
!$omp firstprivate(iprintnext) & |
| 173 |
!$omp reduction(+:nwarn,usedpart) |
| 174 |
!$omp master |
| 175 |
!$ print "(1x,a,i3,a)",'Using ',omp_get_num_threads(),' cpus' |
| 176 |
!$omp end master |
| 177 |
|
| 178 |
!$omp do schedule (guided, 2) |
| 179 |
over_parts: do i=1,npart |
| 180 |
! |
| 181 |
!--report on progress |
| 182 |
! |
| 183 |
if (iprintprogress) then |
| 184 |
!$omp atomic |
| 185 |
j=j+1_8 |
| 186 |
!$ threadid = omp_get_thread_num() |
| 187 |
iprogress = 100*j/npart |
| 188 |
if (iprogress >= iprintnext .and. threadid==1) then |
| 189 |
write(*,"(i3,'%.')",advance='no') iprogress |
| 190 |
iprintnext = iprintnext + iprintinterval |
| 191 |
endif |
| 192 |
endif |
| 193 |
! |
| 194 |
!--skip particles with itype < 0 |
| 195 |
! |
| 196 |
if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts |
| 197 |
|
| 198 |
hi = hh(i) |
| 199 |
if (hi <= 0.) then |
| 200 |
cycle over_parts |
| 201 |
elseif (hi < hmin) then |
| 202 |
! |
| 203 |
!--use minimum h to capture subgrid particles |
| 204 |
! (get better results *without* adjusting weights) |
| 205 |
! |
| 206 |
termnorm = const*weight(i) !*(hi*hi*hi)*dhmin3 |
| 207 |
if (.not.exact_rendering) hi = hmin |
| 208 |
else |
| 209 |
termnorm = const*weight(i) |
| 210 |
endif |
| 211 |
|
| 212 |
! |
| 213 |
!--set kernel related quantities |
| 214 |
! |
| 215 |
xi = x(i) |
| 216 |
yi = y(i) |
| 217 |
zi = z(i) |
| 218 |
|
| 219 |
hi1 = 1./hi |
| 220 |
hi21 = hi1*hi1 |
| 221 |
radkern = radkernel*hi ! radius of the smoothing kernel |
| 222 |
!termnorm = const*weight(i) |
| 223 |
term = termnorm*dat(i) |
| 224 |
dfac = hi**3/(pixwidthx*pixwidthy*pixwidthz*const) |
| 225 |
!dfac = hi**3/(pixwidthx*pixwidthy*const) |
| 226 |
! |
| 227 |
!--for each particle work out which pixels it contributes to |
| 228 |
! |
| 229 |
ipixmin = int((xi - radkern - xmin)/pixwidthx) |
| 230 |
jpixmin = int((yi - radkern - ymin)/pixwidthy) |
| 231 |
kpixmin = int((zi - radkern - zmin)/pixwidthz) |
| 232 |
ipixmax = int((xi + radkern - xmin)/pixwidthx) + 1 |
| 233 |
jpixmax = int((yi + radkern - ymin)/pixwidthy) + 1 |
| 234 |
kpixmax = int((zi + radkern - zmin)/pixwidthz) + 1 |
| 235 |
|
| 236 |
if (.not.periodicx) then |
| 237 |
if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute |
| 238 |
if (ipixmax > npixx) ipixmax = npixx ! to pixels in the image |
| 239 |
endif |
| 240 |
if (.not.periodicy) then |
| 241 |
if (jpixmin < 1) jpixmin = 1 |
| 242 |
if (jpixmax > npixy) jpixmax = npixy |
| 243 |
endif |
| 244 |
if (.not.periodicz) then |
| 245 |
if (kpixmin < 1) kpixmin = 1 |
| 246 |
if (kpixmax > npixz) kpixmax = npixz |
| 247 |
endif |
| 248 |
|
| 249 |
negflag = 0 |
| 250 |
|
| 251 |
! |
| 252 |
!--precalculate an array of dx2 for this particle (optimisation) |
| 253 |
! |
| 254 |
nxpix = 0 |
| 255 |
do ipix=ipixmin,ipixmax |
| 256 |
nxpix = nxpix + 1 |
| 257 |
ipixi = ipix |
| 258 |
if (periodicx) ipixi = iroll(ipix,npixx) |
| 259 |
xpixi = xminpix + ipix*pixwidthx |
| 260 |
!--watch out for errors with periodic wrapping... |
| 261 |
if (nxpix <= size(dx2i)) then |
| 262 |
dx2i(nxpix) = ((xpixi - xi)**2)*hi21 |
| 263 |
endif |
| 264 |
enddo |
| 265 |
|
| 266 |
!--if particle contributes to more than npixx pixels |
| 267 |
! (i.e. periodic boundaries wrap more than once) |
| 268 |
! truncate the contribution and give warning |
| 269 |
if (nxpix > npixx) then |
| 270 |
nwarn = nwarn + 1 |
| 271 |
ipixmax = ipixmin + npixx - 1 |
| 272 |
endif |
| 273 |
! |
| 274 |
!--loop over pixels, adding the contribution from this particle |
| 275 |
! |
| 276 |
do kpix = kpixmin,kpixmax |
| 277 |
kpixi = kpix |
| 278 |
if (periodicz) kpixi = iroll(kpix,npixz) |
| 279 |
|
| 280 |
zpix = zminpix + kpix*pixwidthz |
| 281 |
dz = zpix - zi |
| 282 |
dz2 = dz*dz*hi21 |
| 283 |
|
| 284 |
do jpix = jpixmin,jpixmax |
| 285 |
jpixi = jpix |
| 286 |
if (periodicy) jpixi = iroll(jpix,npixy) |
| 287 |
|
| 288 |
ypix = yminpix + jpix*pixwidthy |
| 289 |
dy = ypix - yi |
| 290 |
dyz2 = dy*dy*hi21 + dz2 |
| 291 |
|
| 292 |
nxpix = 0 |
| 293 |
do ipix = ipixmin,ipixmax |
| 294 |
if ((kpix==kpixmin).and.(jpix==jpixmin).and.(ipix==ipixmin)) then |
| 295 |
usedpart = usedpart + 1 |
| 296 |
endif |
| 297 |
|
| 298 |
nxpix = nxpix + 1 |
| 299 |
ipixi = ipix |
| 300 |
if (periodicx) ipixi = iroll(ipix,npixx) |
| 301 |
|
| 302 |
q2 = dx2i(nxpix) + dyz2 ! dx2 pre-calculated; dy2 pre-multiplied by hi21 |
| 303 |
|
| 304 |
if (exact_rendering .and. ipixmax-ipixmin <= 4) then |
| 305 |
if (q2 < radkernel2 + 3.*pixwidthmax**2*hi21) then |
| 306 |
xpixi = xminpix + ipix*pixwidthx |
| 307 |
|
| 308 |
! Contribution of the cell walls in the xy-plane |
| 309 |
pixint = 0.0 |
| 310 |
wint = wallint(zpix-zi+0.5*pixwidthz,xi,yi,xpixi,ypix,pixwidthx,pixwidthy,hi) |
| 311 |
pixint = pixint + wint |
| 312 |
|
| 313 |
wint = wallint(zi-zpix+0.5*pixwidthz,xi,yi,xpixi,ypix,pixwidthx,pixwidthy,hi) |
| 314 |
pixint = pixint + wint |
| 315 |
|
| 316 |
! Contribution of the cell walls in the xz-plane |
| 317 |
wint = wallint(ypix-yi+0.5*pixwidthy,xi,zi,xpixi,zpix,pixwidthx,pixwidthz,hi) |
| 318 |
pixint = pixint + wint |
| 319 |
|
| 320 |
wint = wallint(yi-ypix+0.5*pixwidthy,xi,zi,xpixi,zpix,pixwidthx,pixwidthz,hi) |
| 321 |
pixint = pixint + wint |
| 322 |
|
| 323 |
! Contribution of the cell walls in the yz-plane |
| 324 |
wint = wallint(xpixi-xi+0.5*pixwidthx,zi,yi,zpix,ypix,pixwidthz,pixwidthy,hi) |
| 325 |
pixint = pixint + wint |
| 326 |
|
| 327 |
wint = wallint(xi-xpixi+0.5*pixwidthx,zi,yi,zpix,ypix,pixwidthz,pixwidthy,hi) |
| 328 |
pixint = pixint + wint |
| 329 |
|
| 330 |
wab = pixint*dfac ! /(pixwidthx*pixwidthy*pixwidthz*const)*hi**3 |
| 331 |
|
| 332 |
if (pixint < -0.01d0) then |
| 333 |
print*, "Error: (",ipixi,jpixi,kpixi,") -> ", pixint, term*wab |
| 334 |
endif |
| 335 |
|
| 336 |
! |
| 337 |
!--calculate data value at this pixel using the summation interpolant |
| 338 |
! |
| 339 |
!$omp atomic |
| 340 |
datsmooth(ipixi,jpixi,kpixi) = datsmooth(ipixi,jpixi,kpixi) + term*wab |
| 341 |
if (normalise) then |
| 342 |
!$omp atomic |
| 343 |
datnorm(ipixi,jpixi,kpixi) = datnorm(ipixi,jpixi,kpixi) + termnorm*wab |
| 344 |
endif |
| 345 |
endif |
| 346 |
else |
| 347 |
if (q2 < radkernel2) then |
| 348 |
! |
| 349 |
!--SPH kernel - standard cubic spline |
| 350 |
! |
| 351 |
wab = wkernel(q2) |
| 352 |
! |
| 353 |
!--calculate data value at this pixel using the summation interpolant |
| 354 |
! |
| 355 |
!$omp atomic |
| 356 |
datsmooth(ipixi,jpixi,kpixi) = datsmooth(ipixi,jpixi,kpixi) + term*wab |
| 357 |
if (normalise) then |
| 358 |
!$omp atomic |
| 359 |
datnorm(ipixi,jpixi,kpixi) = datnorm(ipixi,jpixi,kpixi) + termnorm*wab |
| 360 |
endif |
| 361 |
endif |
| 362 |
endif |
| 363 |
enddo |
| 364 |
enddo |
| 365 |
enddo |
| 366 |
enddo over_parts |
| 367 |
!$omp enddo |
| 368 |
!$omp end parallel |
| 369 |
|
| 370 |
if (nwarn > 0) then |
| 371 |
print "(a,i11,a,/,a)",' interpolate3D: WARNING: contributions truncated from ',nwarn,' particles',& |
| 372 |
' that wrap periodic boundaries more than once' |
| 373 |
endif |
| 374 |
! |
| 375 |
!--normalise dat array |
| 376 |
! |
| 377 |
if (normalise) then |
| 378 |
where (datnorm > tiny(datnorm)) |
| 379 |
datsmooth = datsmooth/datnorm |
| 380 |
end where |
| 381 |
endif |
| 382 |
if (allocated(datnorm)) deallocate(datnorm) |
| 383 |
|
| 384 |
call wall_time(t_end) |
| 385 |
t_used = t_end - t_start |
| 386 |
if (t_used > 10.) call print_time(t_used) |
| 387 |
|
| 388 |
!print*, 'Number of particles in the volume: ', usedpart |
| 389 |
|
| 390 |
end subroutine interpolate3D |
| 391 |
|
| 392 |
subroutine interpolate3D_vec(x,y,z,hh,weight,datvec,itype,npart,& |
| 393 |
xmin,ymin,zmin,datsmooth,npixx,npixy,npixz,pixwidthx,pixwidthy,pixwidthz,& |
| 394 |
normalise,periodicx,periodicy,periodicz) |
| 395 |
|
| 396 |
integer, intent(in) :: npart,npixx,npixy,npixz |
| 397 |
real, intent(in), dimension(npart) :: x,y,z,hh,weight |
| 398 |
real, intent(in), dimension(npart,3) :: datvec |
| 399 |
integer, intent(in), dimension(npart) :: itype |
| 400 |
real, intent(in) :: xmin,ymin,zmin,pixwidthx,pixwidthy,pixwidthz |
| 401 |
real(doub_prec), intent(out), dimension(3,npixx,npixy,npixz) :: datsmooth |
| 402 |
logical, intent(in) :: normalise,periodicx,periodicy,periodicz |
| 403 |
real(doub_prec), dimension(npixx,npixy,npixz) :: datnorm |
| 404 |
|
| 405 |
integer :: i,ipix,jpix,kpix |
| 406 |
integer :: iprintinterval,iprintnext |
| 407 |
integer :: ipixmin,ipixmax,jpixmin,jpixmax,kpixmin,kpixmax |
| 408 |
integer :: ipixi,jpixi,kpixi,nxpix,nwarn |
| 409 |
real :: xminpix,yminpix,zminpix |
| 410 |
real, dimension(npixx) :: dx2i |
| 411 |
real :: xi,yi,zi,hi,hi1,hi21,radkern,wab,q2,const,dyz2,dz2 |
| 412 |
real :: termnorm,dy,dz,ypix,zpix,xpixi,ddatnorm |
| 413 |
real, dimension(3) :: term |
| 414 |
!real :: t_start,t_end |
| 415 |
logical :: iprintprogress |
| 416 |
!$ integer :: omp_get_num_threads |
| 417 |
integer(kind=selected_int_kind(10)) :: iprogress ! up to 10 digits |
| 418 |
|
| 419 |
datsmooth = 0. |
| 420 |
datnorm = 0. |
| 421 |
if (normalise) then |
| 422 |
print "(1x,a)",'interpolating to 3D grid (normalised) ...' |
| 423 |
else |
| 424 |
print "(1x,a)",'interpolating to 3D grid (non-normalised) ...' |
| 425 |
endif |
| 426 |
if (pixwidthx <= 0. .or. pixwidthy <= 0. .or. pixwidthz <= 0.) then |
| 427 |
print "(1x,a)",'interpolate3D: error: pixel width <= 0' |
| 428 |
return |
| 429 |
endif |
| 430 |
if (any(hh(1:npart) <= tiny(hh))) then |
| 431 |
print*,'interpolate3D: WARNING: ignoring some or all particles with h < 0' |
| 432 |
endif |
| 433 |
|
| 434 |
! |
| 435 |
!--print a progress report if it is going to take a long time |
| 436 |
! (a "long time" is, however, somewhat system dependent) |
| 437 |
! |
| 438 |
iprintprogress = (npart >= 100000) .or. (npixx*npixy > 100000) |
| 439 |
!$ iprintprogress = .false. |
| 440 |
! |
| 441 |
!--loop over particles |
| 442 |
! |
| 443 |
iprintinterval = 25 |
| 444 |
if (npart >= 1e6) iprintinterval = 10 |
| 445 |
iprintnext = iprintinterval |
| 446 |
! |
| 447 |
!--get starting CPU time |
| 448 |
! |
| 449 |
!call cpu_time(t_start) |
| 450 |
|
| 451 |
xminpix = xmin - 0.5*pixwidthx |
| 452 |
yminpix = ymin - 0.5*pixwidthy |
| 453 |
zminpix = zmin - 0.5*pixwidthz |
| 454 |
|
| 455 |
const = cnormk3D ! normalisation constant (3D) |
| 456 |
nwarn = 0 |
| 457 |
|
| 458 |
!$omp parallel default(none) & |
| 459 |
!$omp shared(hh,z,x,y,weight,datvec,itype,datsmooth,npart) & |
| 460 |
!$omp shared(xmin,ymin,zmin,radkernel,radkernel2) & |
| 461 |
!$omp shared(xminpix,yminpix,zminpix,pixwidthx,pixwidthy,pixwidthz) & |
| 462 |
!$omp shared(npixx,npixy,npixz,const) & |
| 463 |
!$omp shared(iprintprogress,iprintinterval) & |
| 464 |
!$omp shared(datnorm,normalise,periodicx,periodicy,periodicz) & |
| 465 |
!$omp private(hi,xi,yi,zi,radkern,hi1,hi21) & |
| 466 |
!$omp private(term,termnorm,xpixi) & |
| 467 |
!$omp private(iprogress,iprintnext) & |
| 468 |
!$omp private(ipixmin,ipixmax,jpixmin,jpixmax,kpixmin,kpixmax) & |
| 469 |
!$omp private(ipix,jpix,kpix,ipixi,jpixi,kpixi) & |
| 470 |
!$omp private(dx2i,nxpix,zpix,dz,dz2,dyz2,dy,ypix,q2,wab) & |
| 471 |
!$omp reduction(+:nwarn) |
| 472 |
!$omp master |
| 473 |
!$ print "(1x,a,i3,a)",'Using ',omp_get_num_threads(),' cpus' |
| 474 |
!$omp end master |
| 475 |
! |
| 476 |
!--loop over particles |
| 477 |
! |
| 478 |
!$omp do schedule (guided, 2) |
| 479 |
over_parts: do i=1,npart |
| 480 |
! |
| 481 |
!--report on progress |
| 482 |
! |
| 483 |
if (iprintprogress) then |
| 484 |
iprogress = 100*i/npart |
| 485 |
if (iprogress >= iprintnext) then |
| 486 |
write(*,"('(',i3,'% -',i12,' particles done)')") iprogress,i |
| 487 |
iprintnext = iprintnext + iprintinterval |
| 488 |
endif |
| 489 |
endif |
| 490 |
! |
| 491 |
!--skip particles with itype < 0 |
| 492 |
! |
| 493 |
if (itype(i) < 0 .or. weight(i) < tiny(0.)) cycle over_parts |
| 494 |
|
| 495 |
hi = hh(i) |
| 496 |
if (hi <= 0.) cycle over_parts |
| 497 |
|
| 498 |
! |
| 499 |
!--set kernel related quantities |
| 500 |
! |
| 501 |
xi = x(i) |
| 502 |
yi = y(i) |
| 503 |
zi = z(i) |
| 504 |
|
| 505 |
hi1 = 1./hi |
| 506 |
hi21 = hi1*hi1 |
| 507 |
radkern = radkernel*hi ! radius of the smoothing kernel |
| 508 |
termnorm = const*weight(i) |
| 509 |
term(:) = termnorm*datvec(i,:) |
| 510 |
! |
| 511 |
!--for each particle work out which pixels it contributes to |
| 512 |
! |
| 513 |
ipixmin = int((xi - radkern - xmin)/pixwidthx) |
| 514 |
jpixmin = int((yi - radkern - ymin)/pixwidthy) |
| 515 |
kpixmin = int((zi - radkern - zmin)/pixwidthz) |
| 516 |
ipixmax = int((xi + radkern - xmin)/pixwidthx) + 1 |
| 517 |
jpixmax = int((yi + radkern - ymin)/pixwidthy) + 1 |
| 518 |
kpixmax = int((zi + radkern - zmin)/pixwidthz) + 1 |
| 519 |
|
| 520 |
if (.not.periodicx) then |
| 521 |
if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute |
| 522 |
if (ipixmax > npixx) ipixmax = npixx ! to pixels in the image |
| 523 |
endif |
| 524 |
if (.not.periodicy) then |
| 525 |
if (jpixmin < 1) jpixmin = 1 |
| 526 |
if (jpixmax > npixy) jpixmax = npixy |
| 527 |
endif |
| 528 |
if (.not.periodicz) then |
| 529 |
if (kpixmin < 1) kpixmin = 1 |
| 530 |
if (kpixmax > npixz) kpixmax = npixz |
| 531 |
endif |
| 532 |
! |
| 533 |
!--precalculate an array of dx2 for this particle (optimisation) |
| 534 |
! |
| 535 |
nxpix = 0 |
| 536 |
do ipix=ipixmin,ipixmax |
| 537 |
nxpix = nxpix + 1 |
| 538 |
ipixi = ipix |
| 539 |
if (periodicx) ipixi = iroll(ipix,npixx) |
| 540 |
xpixi = xminpix + ipix*pixwidthx |
| 541 |
!--watch out for errors with perioic wrapping... |
| 542 |
if (nxpix <= size(dx2i)) then |
| 543 |
dx2i(nxpix) = ((xpixi - xi)**2)*hi21 |
| 544 |
endif |
| 545 |
enddo |
| 546 |
|
| 547 |
!--if particle contributes to more than npixx pixels |
| 548 |
! (i.e. periodic boundaries wrap more than once) |
| 549 |
! truncate the contribution and give warning |
| 550 |
if (nxpix > npixx) then |
| 551 |
nwarn = nwarn + 1 |
| 552 |
ipixmax = ipixmin + npixx - 1 |
| 553 |
endif |
| 554 |
! |
| 555 |
!--loop over pixels, adding the contribution from this particle |
| 556 |
! |
| 557 |
do kpix = kpixmin,kpixmax |
| 558 |
kpixi = kpix |
| 559 |
if (periodicz) kpixi = iroll(kpix,npixz) |
| 560 |
zpix = zminpix + kpix*pixwidthz |
| 561 |
dz = zpix - zi |
| 562 |
dz2 = dz*dz*hi21 |
| 563 |
|
| 564 |
do jpix = jpixmin,jpixmax |
| 565 |
jpixi = jpix |
| 566 |
if (periodicy) jpixi = iroll(jpix,npixy) |
| 567 |
ypix = yminpix + jpix*pixwidthy |
| 568 |
dy = ypix - yi |
| 569 |
dyz2 = dy*dy*hi21 + dz2 |
| 570 |
|
| 571 |
nxpix = 0 |
| 572 |
do ipix = ipixmin,ipixmax |
| 573 |
ipixi = ipix |
| 574 |
if (periodicx) ipixi = iroll(ipix,npixx) |
| 575 |
nxpix = nxpix + 1 |
| 576 |
q2 = dx2i(nxpix) + dyz2 ! dx2 pre-calculated; dy2 pre-multiplied by hi21 |
| 577 |
! |
| 578 |
!--SPH kernel - standard cubic spline |
| 579 |
! |
| 580 |
if (q2 < radkernel2) then |
| 581 |
wab = wkernel(q2) |
| 582 |
! |
| 583 |
!--calculate data value at this pixel using the summation interpolant |
| 584 |
! |
| 585 |
!$omp atomic |
| 586 |
datsmooth(1,ipixi,jpixi,kpixi) = datsmooth(1,ipixi,jpixi,kpixi) + term(1)*wab |
| 587 |
!$omp atomic |
| 588 |
datsmooth(2,ipixi,jpixi,kpixi) = datsmooth(2,ipixi,jpixi,kpixi) + term(2)*wab |
| 589 |
!$omp atomic |
| 590 |
datsmooth(3,ipixi,jpixi,kpixi) = datsmooth(3,ipixi,jpixi,kpixi) + term(3)*wab |
| 591 |
if (normalise) then |
| 592 |
!$omp atomic |
| 593 |
datnorm(ipixi,jpixi,kpixi) = datnorm(ipixi,jpixi,kpixi) + termnorm*wab |
| 594 |
endif |
| 595 |
endif |
| 596 |
enddo |
| 597 |
enddo |
| 598 |
enddo |
| 599 |
enddo over_parts |
| 600 |
!$omp enddo |
| 601 |
!$omp end parallel |
| 602 |
|
| 603 |
if (nwarn > 0) then |
| 604 |
print "(a,i11,a,/,a)",' interpolate3D: WARNING: contributions truncated from ',nwarn,' particles',& |
| 605 |
' that wrap periodic boundaries more than once' |
| 606 |
endif |
| 607 |
! |
| 608 |
!--normalise dat array |
| 609 |
! |
| 610 |
if (normalise) then |
| 611 |
!$omp parallel do default(none) schedule(static) & |
| 612 |
!$omp shared(datsmooth,datnorm,npixz,npixy,npixx) & |
| 613 |
!$omp private(kpix,jpix,ipix,ddatnorm) |
| 614 |
do kpix=1,npixz |
| 615 |
do jpix=1,npixy |
| 616 |
do ipix=1,npixx |
| 617 |
if (datnorm(ipix,jpix,kpix) > tiny(datnorm)) then |
| 618 |
ddatnorm = 1./datnorm(ipix,jpix,kpix) |
| 619 |
datsmooth(1,ipix,jpix,kpix) = datsmooth(1,ipix,jpix,kpix)*ddatnorm |
| 620 |
datsmooth(2,ipix,jpix,kpix) = datsmooth(2,ipix,jpix,kpix)*ddatnorm |
| 621 |
datsmooth(3,ipix,jpix,kpix) = datsmooth(3,ipix,jpix,kpix)*ddatnorm |
| 622 |
endif |
| 623 |
enddo |
| 624 |
enddo |
| 625 |
enddo |
| 626 |
!$omp end parallel do |
| 627 |
endif |
| 628 |
|
| 629 |
return |
| 630 |
|
| 631 |
end subroutine interpolate3D_vec |
| 632 |
|
| 633 |
!------------------------------------------------------------ |
| 634 |
! interface to kernel routine to avoid problems with openMP |
| 635 |
!----------------------------------------------------------- |
| 636 |
real function wkernel(q2) |
| 637 |
use kernels, only:wfunc |
| 638 |
real, intent(in) :: q2 |
| 639 |
|
| 640 |
wkernel = wfunc(q2) |
| 641 |
|
| 642 |
end function wkernel |
| 643 |
|
| 644 |
end module interpolations3D |