| 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-2014 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!-------------------------------------------------------------------------- |
| 24 |
! |
| 25 |
! Module with some auxiliary routines related to vector interpolation |
| 26 |
! |
| 27 |
!-------------------------------------------------------------------------- |
| 28 |
module interpolate_vec |
| 29 |
implicit none |
| 30 |
public :: interpolate_vec_average |
| 31 |
public :: mask_vectors |
| 32 |
|
| 33 |
private |
| 34 |
|
| 35 |
contains |
| 36 |
|
| 37 |
!-------------------------------------------------------------------------- |
| 38 |
! |
| 39 |
! Hides vector arrows (sets them to zero) where there are no particles |
| 40 |
! contained in the pixel (as opposed to merely contributing to the pixel) |
| 41 |
! |
| 42 |
! means you can avoid funny looking plots with arrows in otherwise |
| 43 |
! empty regions |
| 44 |
! |
| 45 |
! Daniel Price 26/3/07 |
| 46 |
! |
| 47 |
!-------------------------------------------------------------------------- |
| 48 |
subroutine mask_vectors(xplot,yplot,itype,npart,xmin,xmax,ymin,ymax, & |
| 49 |
vecpixx,vecpixy,npixvecx,npixvecy,minincell,blankval) |
| 50 |
integer, intent(in) :: npart,npixvecx,npixvecy,minincell |
| 51 |
integer, dimension(npart), intent(in) :: itype |
| 52 |
real, dimension(npart), intent(in) :: xplot,yplot |
| 53 |
real, intent(in) :: xmin,xmax,ymin,ymax,blankval |
| 54 |
real, dimension(npixvecx,npixvecy), intent(inout) :: vecpixx,vecpixy |
| 55 |
integer, dimension(npixvecx,npixvecy) :: nincell |
| 56 |
integer :: icellx,icelly,j |
| 57 |
real :: dxcell1,dycell1 |
| 58 |
character(len=16) :: chmin |
| 59 |
|
| 60 |
!--write nice, neat information line |
| 61 |
write(chmin,"(g10.0)") minincell |
| 62 |
print "(2x,a)",'(hiding arrows where there are < '//trim(adjustl(chmin))//' particles in pixel cell)' |
| 63 |
|
| 64 |
dxcell1 = (npixvecx - 1)/(xmax-xmin + tiny(xmin)) |
| 65 |
dycell1 = (npixvecy - 1)/(ymax-ymin + tiny(ymin)) |
| 66 |
! |
| 67 |
!--count particles which fall into each pixel ("cell") |
| 68 |
! |
| 69 |
nincell(:,:) = 0 |
| 70 |
do j=1,npart |
| 71 |
if (itype(j) >= 0) then ! exclude not-plotted particles |
| 72 |
icellx = int((xplot(j) - xmin)*dxcell1) + 1 |
| 73 |
icelly = int((yplot(j) - ymin)*dycell1) + 1 |
| 74 |
!--count number of particles in each cell |
| 75 |
if (icellx > 0 .and. icellx <= npixvecx & |
| 76 |
.and. icelly > 0 .and. icelly <= npixvecy) then |
| 77 |
nincell(icellx,icelly) = nincell(icellx,icelly) + 1 |
| 78 |
endif |
| 79 |
endif |
| 80 |
enddo |
| 81 |
! |
| 82 |
!--set vector arrow lengths to zero in cells where there are no particles |
| 83 |
! |
| 84 |
where (nincell < minincell) |
| 85 |
vecpixx = blankval |
| 86 |
vecpixy = blankval |
| 87 |
end where |
| 88 |
|
| 89 |
return |
| 90 |
end subroutine mask_vectors |
| 91 |
|
| 92 |
!-------------------------------------------------------------------------- |
| 93 |
! Interpolates vector quantity from particles to even grid of pixels |
| 94 |
! |
| 95 |
! This version just does a simple averaging by binning particles |
| 96 |
! and taking the average of vx,vy in the cell to give a vector for |
| 97 |
! that cell. This is because the interpolation of a vector quantity is |
| 98 |
! usually to a *coarser* grid than the particles. |
| 99 |
! |
| 100 |
! Input: particle coordinates : x,y (npart) |
| 101 |
! vector data to smooth : vecx (npart) |
| 102 |
! vecy (npart) |
| 103 |
! grid setup : xmin, ymin, dx |
| 104 |
! |
| 105 |
! Output: smoothed vector field : vecpixx (npixx,npixy) |
| 106 |
! : vecpixy (npixx,npixy) |
| 107 |
! |
| 108 |
! Daniel Price, Institute of Astronomy, Cambridge, 20/8/04 |
| 109 |
!-------------------------------------------------------------------------- |
| 110 |
|
| 111 |
subroutine interpolate_vec_average(x,y,vecx,vecy,itype, & |
| 112 |
xmin,ymin,dx,dy,vecpixx,vecpixy,npart,npixx,npixy,z,zmin,zmax) |
| 113 |
|
| 114 |
integer, intent(in) :: npart,npixx,npixy |
| 115 |
real, intent(in), dimension(npart) :: x,y,vecx,vecy |
| 116 |
integer, intent(in), dimension(npart) :: itype |
| 117 |
real, intent(in) :: xmin,ymin,dx,dy |
| 118 |
real, intent(out), dimension(npixx,npixy) :: vecpixx, vecpixy |
| 119 |
real, intent(in), optional :: z(npart),zmin,zmax |
| 120 |
integer :: i,j,k,ix,iy |
| 121 |
integer, dimension(npixx,npixy) :: ihoc,numcell |
| 122 |
integer, dimension(npart) :: ll |
| 123 |
logical :: xsec |
| 124 |
|
| 125 |
!print "(a,i3,a,i3,a)",' averaging vector field onto ',npixx,'x',npixy,' pixels...' |
| 126 |
if (dx <= 0. .or. dy <= 0.) then |
| 127 |
print*,'interpolate_vec: error: pixel width <= 0' |
| 128 |
return |
| 129 |
endif |
| 130 |
! |
| 131 |
!--interpolation is to a coarser grid, so just average |
| 132 |
! bin particles into cells using a link list |
| 133 |
! |
| 134 |
ihoc(:,:) = -1 ! head of chain |
| 135 |
numcell(:,:) = 0 |
| 136 |
xsec = present(z) .and. present(zmin) .and. present(zmax) |
| 137 |
over_parts: do i=1,npart |
| 138 |
if (xsec) then |
| 139 |
if (z(i) < zmin .or. z(i) > zmax) cycle over_parts |
| 140 |
endif |
| 141 |
if (itype(i) >= 0) then |
| 142 |
ix = int((x(i)-xmin)/dx)+1 |
| 143 |
iy = int((y(i)-ymin)/dy)+1 |
| 144 |
if ((ix >= 1).and.(ix <= npixx).and.(iy >= 1).and.(iy <= npixy)) then |
| 145 |
ll(i)=ihoc(ix,iy) ! set link list of this particle to old head of list |
| 146 |
ihoc(ix,iy) = i ! set head of chain to this particle |
| 147 |
endif |
| 148 |
endif |
| 149 |
enddo over_parts |
| 150 |
! |
| 151 |
!--add up total vx,vy in each cell |
| 152 |
! |
| 153 |
vecpixx(:,:) = 0. |
| 154 |
vecpixy(:,:) = 0. |
| 155 |
do j=1,npixy |
| 156 |
do i=1,npixx |
| 157 |
k = ihoc(i,j) |
| 158 |
do while (k /= -1) |
| 159 |
vecpixx(i,j) = vecpixx(i,j) + vecx(k) |
| 160 |
vecpixy(i,j) = vecpixy(i,j) + vecy(k) |
| 161 |
numcell(i,j) = numcell(i,j) + 1 |
| 162 |
k = ll(k) |
| 163 |
enddo |
| 164 |
enddo |
| 165 |
enddo |
| 166 |
! |
| 167 |
!--divide by number of particles in that cell to get average vx,vy |
| 168 |
! |
| 169 |
do j=1,npixy |
| 170 |
do i=1,npixx |
| 171 |
if (numcell(i,j) /= 0) then |
| 172 |
vecpixx(i,j) = vecpixx(i,j)/float(numcell(i,j)) |
| 173 |
vecpixy(i,j) = vecpixy(i,j)/float(numcell(i,j)) |
| 174 |
endif |
| 175 |
enddo |
| 176 |
enddo |
| 177 |
return |
| 178 |
|
| 179 |
end subroutine interpolate_vec_average |
| 180 |
|
| 181 |
end module interpolate_vec |