| 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 1D interpolation |
| 26 |
! |
| 27 |
!---------------------------------------------------------------------- |
| 28 |
|
| 29 |
module interpolations1D |
| 30 |
implicit none |
| 31 |
public :: interpolate1D |
| 32 |
|
| 33 |
contains |
| 34 |
|
| 35 |
!-------------------------------------------------------------------------- |
| 36 |
! subroutine to interpolate from particle data to even 1D grid of pixels |
| 37 |
! |
| 38 |
! The data is smoothed using the SPH summation interpolant, |
| 39 |
! that is, we compute the smoothed array according to |
| 40 |
! |
| 41 |
! datsmooth(pixel) = sum_j w_j W(r-r_j, h_j) |
| 42 |
! |
| 43 |
! where _j is the quantity at the neighbouring particle j and |
| 44 |
! W is the smoothing kernel, for which we use the usual cubic spline. |
| 45 |
! For an SPH interpolation the weight for each particle should be |
| 46 |
! the dimensionless quantity |
| 47 |
! |
| 48 |
! w_j = m_j / (rho_j * h_j**ndim) |
| 49 |
! |
| 50 |
! Other weights can be used (e.g. constants), but in this case the |
| 51 |
! normalisation option should also be set. |
| 52 |
! |
| 53 |
! Input: particle coordinates : x (npart) |
| 54 |
! smoothing lengths : hh (npart) |
| 55 |
! interpolation weights : weight (npart) |
| 56 |
! scalar data to smooth : dat (npart) |
| 57 |
! |
| 58 |
! number of pixels in x : npixx |
| 59 |
! pixel width : pixwidth |
| 60 |
! option to normalise interpolation : normalise (.true. or .false.) |
| 61 |
! |
| 62 |
! Output: smoothed data : datsmooth (npixx) |
| 63 |
! |
| 64 |
! Written by Daniel Price 2003-2006 |
| 65 |
!-------------------------------------------------------------------------- |
| 66 |
|
| 67 |
subroutine interpolate1D(x,hh,weight,dat,itype,npart, & |
| 68 |
xmin,datsmooth,npixx,pixwidth,normalise,iverbose) |
| 69 |
|
| 70 |
use kernels, only:cnormk1D,radkernel,wfunc,select_kernel |
| 71 |
integer, intent(in) :: npart,npixx |
| 72 |
real, intent(in), dimension(npart) :: x,hh,weight,dat |
| 73 |
integer, intent(in), dimension(npart) :: itype |
| 74 |
real, intent(in) :: xmin,pixwidth |
| 75 |
real, intent(out), dimension(npixx) :: datsmooth |
| 76 |
logical, intent(in) :: normalise |
| 77 |
integer, intent(in) :: iverbose |
| 78 |
real, dimension(npixx) :: datnorm |
| 79 |
|
| 80 |
integer :: i,ipix,ipixmin,ipixmax,i1,i2 |
| 81 |
real :: hi,hi1,radkern,q2,wab,const |
| 82 |
real :: term,termnorm,dx,xpix |
| 83 |
|
| 84 |
datsmooth = 0. |
| 85 |
term = 0. |
| 86 |
if (iverbose > 0) then |
| 87 |
if (normalise) then |
| 88 |
print*,'interpolating (normalised) from particles to 1D grid: npix,xmin,max=',npixx,xmin,xmin+npixx*pixwidth |
| 89 |
else |
| 90 |
print*,'interpolating (non-normalised) from particles to 1D grid: npix,xmin,max=',npixx,xmin,xmin+npixx*pixwidth |
| 91 |
endif |
| 92 |
endif |
| 93 |
if (abs(pixwidth) <= tiny(0.)) then |
| 94 |
if (iverbose >= 0) print*,'interpolate1D: error: pixel width = 0' |
| 95 |
return |
| 96 |
endif |
| 97 |
if (any(hh(1:npart) <= tiny(hh))) then |
| 98 |
if (iverbose > 0) print*,'interpolate1D: warning: ignoring some or all particles with h < 0' |
| 99 |
endif |
| 100 |
|
| 101 |
if (.not.associated(wfunc)) call select_kernel(0) |
| 102 |
|
| 103 |
const = cnormk1D ! normalisation constant |
| 104 |
! |
| 105 |
!--loop over particles |
| 106 |
! |
| 107 |
over_parts: do i=1,npart |
| 108 |
! |
| 109 |
!--skip particles with itype < 0 |
| 110 |
! |
| 111 |
if (itype(i) < 0) cycle over_parts |
| 112 |
! |
| 113 |
!--skip particles with zero weights |
| 114 |
! |
| 115 |
termnorm = const*weight(i) |
| 116 |
if (termnorm <= 0.) cycle over_parts |
| 117 |
! |
| 118 |
!--skip particles with wrong h's |
| 119 |
! |
| 120 |
hi = hh(i) |
| 121 |
if (hi <= tiny(hi)) cycle over_parts |
| 122 |
! |
| 123 |
!--set kernel related quantities |
| 124 |
! |
| 125 |
hi1 = 1./hi |
| 126 |
radkern = radkernel*hi ! radius of the smoothing kernel |
| 127 |
term = termnorm*dat(i) |
| 128 |
! |
| 129 |
!--for each particle work out which pixels it contributes to |
| 130 |
! |
| 131 |
i1 = int((x(i) - radkern - xmin)/pixwidth) |
| 132 |
i2 = int((x(i) + radkern - xmin)/pixwidth) + 1 |
| 133 |
ipixmin = i1 |
| 134 |
ipixmax = i2 |
| 135 |
if (i1 > i2) then |
| 136 |
ipixmax = i1 |
| 137 |
ipixmin = i2 |
| 138 |
endif |
| 139 |
|
| 140 |
if (ipixmin < 1) ipixmin = 1 ! make sure they only contribute |
| 141 |
if (ipixmax > npixx) ipixmax = npixx ! to pixels in the image |
| 142 |
! |
| 143 |
!--loop over pixels, adding the contribution from this particle |
| 144 |
! |
| 145 |
do ipix = ipixmin,ipixmax |
| 146 |
xpix = xmin + (ipix-0.5)*pixwidth |
| 147 |
dx = xpix - x(i) |
| 148 |
q2 = dx*dx*hi1*hi1 |
| 149 |
! |
| 150 |
!--SPH kernel - standard cubic spline |
| 151 |
! |
| 152 |
wab = wfunc(q2) |
| 153 |
! |
| 154 |
!--calculate data value at this pixel using the summation interpolant |
| 155 |
! |
| 156 |
datsmooth(ipix) = datsmooth(ipix) + term*wab |
| 157 |
if (normalise) datnorm(ipix) = datnorm(ipix) + termnorm*wab |
| 158 |
enddo |
| 159 |
|
| 160 |
enddo over_parts |
| 161 |
! |
| 162 |
!--normalise dat array |
| 163 |
! |
| 164 |
if (normalise) then |
| 165 |
where (datnorm > 0.) |
| 166 |
datsmooth = datsmooth/datnorm |
| 167 |
end where |
| 168 |
endif |
| 169 |
|
| 170 |
end subroutine interpolate1D |
| 171 |
|
| 172 |
end module interpolations1D |