| 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-2009 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!---------------------------------------------------------------- |
| 24 |
! |
| 25 |
! module for probability density function calculation |
| 26 |
! and/or plotting on the particles |
| 27 |
! |
| 28 |
!---------------------------------------------------------------- |
| 29 |
module pdfs |
| 30 |
implicit none |
| 31 |
public :: pdf_calc,pdf_write,mean_variance |
| 32 |
|
| 33 |
contains |
| 34 |
!----------------------------------------------------------------- |
| 35 |
! |
| 36 |
! subroutine bins particles into x, works out number in each bin, |
| 37 |
! calculates normalisation for PDF. |
| 38 |
! |
| 39 |
!----------------------------------------------------------------- |
| 40 |
subroutine pdf_calc(npart,xpart,xminplot,xmaxplot,nbins,xbin,pdf,pdfmin,pdfmax,& |
| 41 |
usefixedbins,ierr,mask,weight) |
| 42 |
!use transforms, only:transform,transform_inverse,transform_limits,convert_to_ln_fac |
| 43 |
integer, intent(in) :: npart,nbins |
| 44 |
real, dimension(:), intent(in) :: xpart |
| 45 |
real, intent(in) :: xminplot,xmaxplot |
| 46 |
real, intent(out), dimension(nbins) :: xbin,pdf |
| 47 |
real, intent(out) :: pdfmin,pdfmax |
| 48 |
logical, intent(in) :: usefixedbins |
| 49 |
integer, intent(out) :: ierr |
| 50 |
integer, intent(in), dimension(:), optional :: mask |
| 51 |
real, intent(in), dimension(:), optional :: weight |
| 52 |
integer :: ibin,i |
| 53 |
real :: dx,totprob,fi |
| 54 |
real :: xmin,xmax,xminpart,xmaxpart,weighti,totvol |
| 55 |
logical :: use_part |
| 56 |
|
| 57 |
ierr = 0 |
| 58 |
print "(a,i3,a)",' calculating PDF using ',nbins,' bins' |
| 59 |
if (present(weight)) print "(a)",' (using individual weights)' |
| 60 |
! |
| 61 |
!--set bins in PDF: must always use all the particles |
| 62 |
! note that xpart will already have been transformed |
| 63 |
! so these are min and max in transformed space |
| 64 |
! |
| 65 |
xminpart = minval(xpart(1:npart)) |
| 66 |
xmaxpart = maxval(xpart(1:npart)) |
| 67 |
if (usefixedbins) then |
| 68 |
xmin = xminplot |
| 69 |
xmax = xmaxplot |
| 70 |
print "(a,1pe10.3,a,1pe10.3)",' PDF bins are fixed between the current x limits, min = ',xminplot,' max = ',xmaxplot |
| 71 |
if (xminpart < xmin) print "(a)",' WARNING: particles fall outside of (fixed) bin range, will pile up on first bin' |
| 72 |
if (xmaxpart > xmax) print "(a)",' WARNING: particles fall outside of (fixed) bin range, will pile up on last bin' |
| 73 |
dx = (xmax - xmin)/real(nbins) |
| 74 |
print "(a,1pe10.3)",' bin width = ',dx |
| 75 |
do ibin=1,nbins |
| 76 |
xbin(ibin) = xmin + (ibin-1)*dx |
| 77 |
enddo |
| 78 |
else |
| 79 |
xmin = xminpart |
| 80 |
xmax = xmaxpart |
| 81 |
dx = (xmax - xmin)/real(nbins-1) |
| 82 |
print "(a,1pe10.3)",' bin width = ',dx |
| 83 |
do ibin=1,nbins |
| 84 |
xbin(ibin) = xmin + (ibin-0.5)*dx |
| 85 |
enddo |
| 86 |
endif |
| 87 |
|
| 88 |
! |
| 89 |
!--now calculate probability of finding a particle at each x |
| 90 |
! |
| 91 |
pdf(:) = 0. |
| 92 |
totvol = 0. |
| 93 |
do i=1,npart |
| 94 |
if (present(mask)) then |
| 95 |
use_part = (mask(i) >= 0) |
| 96 |
else |
| 97 |
use_part = .true. |
| 98 |
endif |
| 99 |
!--do not use hidden particles |
| 100 |
if (use_part) then |
| 101 |
ibin = int((xpart(i) - xmin)/dx) + 1 |
| 102 |
if (ibin < 1) ibin = 1 |
| 103 |
if (ibin > nbins) ibin = nbins |
| 104 |
|
| 105 |
if (present(weight)) then |
| 106 |
weighti = weight(i) |
| 107 |
else |
| 108 |
weighti = 1. |
| 109 |
endif |
| 110 |
totvol = totvol + weighti |
| 111 |
|
| 112 |
pdf(ibin) = pdf(ibin) + weighti |
| 113 |
endif |
| 114 |
enddo |
| 115 |
print*,' sum of weights = ',totvol |
| 116 |
! |
| 117 |
!--get total area under pdf by trapezoidal rule |
| 118 |
! |
| 119 |
totprob = 0. |
| 120 |
do ibin=1,nbins |
| 121 |
fi = pdf(ibin) |
| 122 |
totprob = totprob + dx*fi !!0.5*dx*(fi + fprev) |
| 123 |
enddo |
| 124 |
! |
| 125 |
!--normalise pdf so total area is unity |
| 126 |
! |
| 127 |
print*,'normalisation factor = ',totprob,totvol*dx ! =totvol*dx for volume weighted |
| 128 |
|
| 129 |
if (totprob <= 0.) then |
| 130 |
ierr = 1 |
| 131 |
print "(a)",' error in normalisation factor: returning non-normalised PDF' |
| 132 |
else |
| 133 |
pdf(1:nbins) = pdf(1:nbins)/totprob |
| 134 |
! |
| 135 |
!--return min and max for adaptive plot limit setting |
| 136 |
! (exclude zero as min) |
| 137 |
! |
| 138 |
pdfmin = minval(pdf(1:nbins),mask=(pdf(1:nbins) > 0.)) |
| 139 |
pdfmax = maxval(pdf(1:nbins)) |
| 140 |
endif |
| 141 |
|
| 142 |
end subroutine pdf_calc |
| 143 |
|
| 144 |
!----------------------------------------------------------------- |
| 145 |
! interface which controls plotting of PDF |
| 146 |
! (so can easily change properties of PDF plotting, |
| 147 |
! e.g. histogram vs. line) |
| 148 |
!----------------------------------------------------------------- |
| 149 |
!subroutine pdf_plot(nbins,xbin,pb) |
| 150 |
! use plotutils, only:plotline !,plotbins |
| 151 |
! implicit none |
| 152 |
! integer, intent(in) :: nbins |
| 153 |
! real, dimension(:), intent(in) :: xbin,pb |
| 154 |
|
| 155 |
! |
| 156 |
!--plot as line segment, with blanking at zero |
| 157 |
! |
| 158 |
! call plotline(nbins,xbin,pb,blank=0.) |
| 159 |
! |
| 160 |
!--plot as histogram, with blanking of zero |
| 161 |
! |
| 162 |
! call plotbins(nbins,xbin,pb,blank=0.) |
| 163 |
|
| 164 |
!end subroutine pdf_plot |
| 165 |
|
| 166 |
!----------------------------------------------------------------- |
| 167 |
! routine to write pdf to file |
| 168 |
!----------------------------------------------------------------- |
| 169 |
subroutine pdf_write(nbins,xbin,pb,labelx,rootname,tagline) |
| 170 |
use asciiutils, only:safename |
| 171 |
character(len=*), intent(in) :: labelx,rootname,tagline |
| 172 |
integer, intent(in) :: nbins !,itransx |
| 173 |
real, intent(in), dimension(nbins) :: xbin,pb |
| 174 |
integer :: i,ierr |
| 175 |
integer, parameter :: iunit = 86 |
| 176 |
logical :: warned |
| 177 |
|
| 178 |
print "(a)",' writing to '//trim(rootname)//'_pdf_'//trim(safename(labelx))//'.dat' |
| 179 |
open(unit=iunit,file=trim(rootname)//'_pdf_'//trim(safename(labelx))//'.dat', & |
| 180 |
form='formatted',status='replace',iostat=ierr) |
| 181 |
if (ierr /= 0) then |
| 182 |
print "(a)",'ERROR: could not open file: no output' |
| 183 |
return |
| 184 |
endif |
| 185 |
|
| 186 |
write(iunit,"(a)",iostat=ierr) '# probability density function, calculated using '//trim(tagline) |
| 187 |
if (ierr /= 0) print "(a)",' ERROR writing header line' |
| 188 |
write(iunit,"(a,i5,a)",iostat=ierr) '# ',nbins,' bins evenly spaced in '//trim(labelx) |
| 189 |
write(iunit,"(a)") '# '//trim(labelx)//','//' P('//trim(labelx)//')' |
| 190 |
|
| 191 |
warned = .false. |
| 192 |
!--dump bins to file |
| 193 |
do i=1,nbins |
| 194 |
write(iunit,*,iostat=ierr) xbin(i),pb(i) |
| 195 |
if (ierr /= 0 .and. .not.warned) then |
| 196 |
print "(a)",' ERRORS during write' |
| 197 |
warned = .true. |
| 198 |
endif |
| 199 |
enddo |
| 200 |
close(iunit) |
| 201 |
|
| 202 |
return |
| 203 |
end subroutine pdf_write |
| 204 |
|
| 205 |
!------------------------------------------------- |
| 206 |
! Subroutine to calculate the mean and variance |
| 207 |
! of a set of data points |
| 208 |
! Mean is trivial but variance uses a special |
| 209 |
! formula to reduce round-off error |
| 210 |
! see Press et al Numerical Recipes, section 14.2 |
| 211 |
! this is similar to their subroutine avevar |
| 212 |
!------------------------------------------------- |
| 213 |
subroutine mean_variance(x,npts,xmean,xvariance) |
| 214 |
integer, intent(in) :: npts |
| 215 |
real, intent(in), dimension(npts) :: x |
| 216 |
real, intent(out) :: xmean, xvariance |
| 217 |
real :: roundoff, delta |
| 218 |
integer :: i |
| 219 |
! |
| 220 |
!--calculate average |
| 221 |
! |
| 222 |
xmean = 0. |
| 223 |
do i=1,npts |
| 224 |
xmean = xmean + x(i) |
| 225 |
enddo |
| 226 |
xmean = xmean/real(npts) |
| 227 |
! |
| 228 |
!--calculate variance using the corrected two-pass formula |
| 229 |
! |
| 230 |
! var = 1/(n-1)*( sum (x-\bar{x}) - 1/n * (sum(x-\bar{x}) )^2 ) |
| 231 |
! |
| 232 |
! where the last term corrects for the roundoff error |
| 233 |
! in the first term |
| 234 |
! |
| 235 |
xvariance = 0. |
| 236 |
roundoff = 0. |
| 237 |
|
| 238 |
do i=1,npts |
| 239 |
delta = x(i) - xmean |
| 240 |
roundoff = roundoff + delta |
| 241 |
xvariance = xvariance + delta*delta |
| 242 |
enddo |
| 243 |
xvariance = (xvariance - roundoff**2/npts)/real(npts-1) |
| 244 |
|
| 245 |
return |
| 246 |
end subroutine mean_variance |
| 247 |
|
| 248 |
end module pdfs |