| 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) 2023- Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
module moments |
| 23 |
implicit none |
| 24 |
|
| 25 |
contains |
| 26 |
!----------------------------------------------------------------- |
| 27 |
! |
| 28 |
! subroutine to compute moments from a data cube |
| 29 |
! by interpolating the z dimension onto a finer grid |
| 30 |
! with the SPH kernel and then integrating |
| 31 |
! |
| 32 |
!----------------------------------------------------------------- |
| 33 |
subroutine get_moments(cube,zvals,hfac,moment_type,moments) |
| 34 |
use interpolations1D, only:interpolate1D |
| 35 |
use timing, only:wall_time,print_time |
| 36 |
real, intent(in) :: cube(:,:,:),zvals(:),hfac |
| 37 |
integer, intent(in) :: moment_type(:) |
| 38 |
real, intent(out), allocatable :: moments(:,:,:) |
| 39 |
real, allocatable :: profile(:),profile_smooth(:),hh(:),weight(:),zvals_smooth(:) |
| 40 |
integer, allocatable :: mask(:) |
| 41 |
real :: dz,dz_smooth,zmin |
| 42 |
integer :: i,j,k,m,nx,ny,nz,nm,nz_smooth,iu,jcount,ktmp(1),iprint,jprint |
| 43 |
real :: t1,t2 |
| 44 |
|
| 45 |
! compute array dimensions |
| 46 |
nx = size(cube,1) |
| 47 |
ny = size(cube,2) |
| 48 |
nz = size(cube,3) |
| 49 |
nm = size(moment_type) |
| 50 |
nz_smooth = 10*nz |
| 51 |
|
| 52 |
! allocate memory |
| 53 |
allocate(profile(nz),profile_smooth(nz_smooth),zvals_smooth(nz_smooth),hh(nz),weight(nz),mask(nz)) |
| 54 |
allocate(moments(nx,ny,nm)) |
| 55 |
|
| 56 |
! set up the interpolation grid |
| 57 |
dz = zvals(2)-zvals(1) |
| 58 |
dz_smooth = (dz*nz)/nz_smooth |
| 59 |
zmin = zvals(1) - 0.5*dz |
| 60 |
do k=1,nz_smooth |
| 61 |
zvals_smooth(k) = zmin + (k-0.5)*dz_smooth |
| 62 |
enddo |
| 63 |
|
| 64 |
! set smoothing length, weights and mask for interpolate1D routine |
| 65 |
mask = 1 |
| 66 |
hh = hfac*abs(dz) |
| 67 |
weight = abs(dz)/hh |
| 68 |
|
| 69 |
! select which pixel to print out for debugging purposes |
| 70 |
jprint = ny/2 |
| 71 |
iprint = nx/2 + 10 |
| 72 |
|
| 73 |
! progress bar and timing |
| 74 |
call wall_time(t1) |
| 75 |
write(*,"(/,a,45x,a)") ' 0% ',' 100%' |
| 76 |
write(*,"(1x,a)",advance='no') '|' |
| 77 |
|
| 78 |
jcount = 0 |
| 79 |
!$omp parallel do default(none) shared(cube,moments,moment_type,ny,nx,zvals,hh,weight,mask) & |
| 80 |
!$omp shared(nz,nz_smooth,nm,zmin,dz_smooth,zvals_smooth,jcount,iu,iprint,jprint) & |
| 81 |
!$omp private(j,i,k,m,ktmp,profile,profile_smooth) |
| 82 |
do j=1,ny |
| 83 |
! |
| 84 |
! print progress bar |
| 85 |
! |
| 86 |
!$omp atomic |
| 87 |
jcount = jcount + 1 |
| 88 |
if (mod(jcount,ny/50)==0) write(*,"('=')",advance='no') |
| 89 |
|
| 90 |
do i=1,nx |
| 91 |
! extract line profile in the z direction |
| 92 |
profile = cube(i,j,:) |
| 93 |
|
| 94 |
! interpolate to a finer grid |
| 95 |
call interpolate1D(zvals,hh,weight,profile,mask,nz,zmin,profile_smooth,nz_smooth,dz_smooth,normalise=.false.,iverbose=0) |
| 96 |
|
| 97 |
! compute desired moments at this pixel location |
| 98 |
do m=1,nm |
| 99 |
select case(moment_type(m)) |
| 100 |
case(9) ! peak velocity |
| 101 |
ktmp = maxloc(profile_smooth) |
| 102 |
moments(i,j,m) = zvals_smooth(ktmp(1)) |
| 103 |
case(8) ! peak intensity |
| 104 |
moments(i,j,m) = maxval(profile_smooth) |
| 105 |
case(1:2) |
| 106 |
moments(i,j,m) = sum(profile_smooth*zvals_smooth**moment_type(m)) |
| 107 |
case default ! moment 0 |
| 108 |
moments(i,j,m) = sum(profile_smooth) |
| 109 |
end select |
| 110 |
enddo |
| 111 |
! |
| 112 |
! print out line profile for a selected pixel for debugging purposes |
| 113 |
! |
| 114 |
if (j==jprint .and. i==iprint) then |
| 115 |
open(newunit=iu,file='profile.dat',status='replace') |
| 116 |
do k=1,nz |
| 117 |
write(iu,*) zvals(k),profile(k) |
| 118 |
enddo |
| 119 |
close(iu) |
| 120 |
|
| 121 |
open(newunit=iu,file='profile_smooth.dat',status='replace') |
| 122 |
do k=1,nz_smooth |
| 123 |
write(iu,*) zvals_smooth(k),profile_smooth(k) |
| 124 |
enddo |
| 125 |
close(iu) |
| 126 |
endif |
| 127 |
enddo |
| 128 |
enddo |
| 129 |
!$omp end parallel do |
| 130 |
|
| 131 |
! finish progress bar and timing |
| 132 |
call wall_time(t2) |
| 133 |
write(*,"(a)",advance='no') '|' |
| 134 |
call print_time(t2-t1) |
| 135 |
write(*,*) |
| 136 |
|
| 137 |
end subroutine get_moments |
| 138 |
|
| 139 |
end module moments |