| 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-2025 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
module get_h |
| 23 |
! |
| 24 |
! this module contains routines to compute a smoothing length |
| 25 |
! for post-processing of simulations where h is not output |
| 26 |
! in the file (e.g. dark matter, stars etc) |
| 27 |
! |
| 28 |
implicit none |
| 29 |
public :: compute_h_and_density |
| 30 |
|
| 31 |
real, parameter :: tolh = 1.e-4 |
| 32 |
integer, parameter :: its_max = 50 |
| 33 |
|
| 34 |
private |
| 35 |
|
| 36 |
contains |
| 37 |
|
| 38 |
! |
| 39 |
! public-facing routine to compute h and density |
| 40 |
! from a set of positions. No guess for h is needed |
| 41 |
! |
| 42 |
subroutine compute_h_and_density(ndim,n,hfac,x,h,dens) |
| 43 |
integer, intent(in) :: ndim,n |
| 44 |
real, intent(in) :: hfac |
| 45 |
real, intent(in) :: x(ndim,n) |
| 46 |
real, intent(out) :: h(n),dens(n) |
| 47 |
real :: hguess |
| 48 |
|
| 49 |
print*,' >>> Computing smoothing lengths for ',n,' particles...' |
| 50 |
|
| 51 |
hguess = abs(maxval(x(1,1:n)) - minval(x(1,1:n)))/n**(1./3.) |
| 52 |
h = hguess |
| 53 |
print*, ' guessing h = ',hguess |
| 54 |
|
| 55 |
! call guess_h(ndim,x,h) |
| 56 |
call densityiterate(ndim,n,hfac,x,h,dens) |
| 57 |
|
| 58 |
end subroutine compute_h_and_density |
| 59 |
|
| 60 |
! |
| 61 |
! routine to perform the simultaneous |
| 62 |
! (number) density and smoothing length calculation |
| 63 |
! |
| 64 |
subroutine densityiterate(ndim,n,hfac,x,h,dens) |
| 65 |
use kernels, only:wfunc,dwfunc,radkernel2,select_kernel_by_name,& |
| 66 |
cnormk1D,cnormk2D,cnormk3D |
| 67 |
use timing, only:wall_time,print_time |
| 68 |
integer, intent(in) :: ndim,n |
| 69 |
real, intent(in) :: hfac |
| 70 |
real, intent(in) :: x(ndim,n) |
| 71 |
real, intent(inout) :: h(n) |
| 72 |
real, intent(out) :: dens(n) |
| 73 |
real :: xi(ndim),dx(ndim) |
| 74 |
real :: rhoi,gradhi,hi,hi_old,hi1,hi21,hterm |
| 75 |
real :: q2,q,wi,dwi,dwdhi,t1,t2 |
| 76 |
logical :: converged |
| 77 |
integer, parameter :: its_max = 50 |
| 78 |
integer :: its,i,j,k |
| 79 |
|
| 80 |
call wall_time(t1) |
| 81 |
call select_kernel_by_name('cubic') |
| 82 |
k = 0 |
| 83 |
!$omp parallel do schedule(dynamic) default(none)& |
| 84 |
!$omp shared(n,x,h,dens,ndim,radkernel2,wfunc,dwfunc,hfac,k) & |
| 85 |
!$omp shared(cnormk1D,cnormk2D,cnormk3D) & |
| 86 |
!$omp private(xi,dx,rhoi,gradhi) & |
| 87 |
!$omp private(hi,hi_old,hi1,hi21,hterm) & |
| 88 |
!$omp private(q2,q,wi,dwi,dwdhi,converged,its) |
| 89 |
do i=1,n |
| 90 |
!$omp atomic |
| 91 |
k = k + 1 |
| 92 |
if (mod(k,10000)==0) then |
| 93 |
!$omp critical |
| 94 |
print*,k |
| 95 |
!$omp end critical |
| 96 |
endif |
| 97 |
xi = x(1:ndim,i) |
| 98 |
hi = h(i) |
| 99 |
hi_old = hi |
| 100 |
converged = .false. |
| 101 |
its = 0 |
| 102 |
do while(.not.converged .and. its < its_max) |
| 103 |
its = its + 1 |
| 104 |
rhoi = 0. |
| 105 |
gradhi = 0. |
| 106 |
hi1 = 1./hi |
| 107 |
hi21 = hi1*hi1 |
| 108 |
hterm = hi1**ndim |
| 109 |
do j=1,n |
| 110 |
dx = xi - x(:,j) |
| 111 |
q2 = dot_product(dx,dx)*hi21 |
| 112 |
if (q2 < radkernel2) then |
| 113 |
q = sqrt(q2) |
| 114 |
wi = wfunc(q2)*hterm |
| 115 |
dwi = dwfunc(q2)*hterm*hi1 |
| 116 |
rhoi = rhoi + wi |
| 117 |
dwdhi = (-q*dwi - ndim*wi*hi1) |
| 118 |
gradhi = gradhi + dwdhi |
| 119 |
!print*,'j=',j,wi,hterm |
| 120 |
endif |
| 121 |
enddo |
| 122 |
select case(ndim) |
| 123 |
case(1) |
| 124 |
rhoi = rhoi*cnormk1D |
| 125 |
gradhi = gradhi*cnormk1D |
| 126 |
case(2) |
| 127 |
rhoi = rhoi*cnormk2D |
| 128 |
gradhi = gradhi*cnormk2D |
| 129 |
case default |
| 130 |
rhoi = rhoi*cnormk3D |
| 131 |
gradhi = gradhi*cnormk3D |
| 132 |
end select |
| 133 |
call update_h(hfac,hi_old,rhoi,hi,gradhi,ndim,converged) |
| 134 |
enddo |
| 135 |
|
| 136 |
! store converged solution |
| 137 |
dens(i) = rhoi |
| 138 |
h(i) = hi |
| 139 |
enddo |
| 140 |
!$omp end parallel do |
| 141 |
call wall_time(t2) |
| 142 |
call print_time(t2-t1) |
| 143 |
|
| 144 |
end subroutine densityiterate |
| 145 |
|
| 146 |
! |
| 147 |
! cleanup and iterate h once the sums over |
| 148 |
! neighbours are done |
| 149 |
! |
| 150 |
subroutine update_h(hfac,h_old,rho,h,gradh,ndim,converged) |
| 151 |
real, intent(in) :: hfac,h_old,rho |
| 152 |
real, intent(inout) :: h,gradh |
| 153 |
integer, intent(in) :: ndim |
| 154 |
logical, intent(out) :: converged |
| 155 |
real :: dhdrho,rhoh,omega,func,dfdh1,hnew |
| 156 |
|
| 157 |
rhoh = (hfac/abs(h))**ndim |
| 158 |
dhdrho = -h/(ndim*rho) |
| 159 |
omega = 1. - dhdrho*gradh |
| 160 |
gradh = 1./omega |
| 161 |
|
| 162 |
! Newton Raphson solver, see Price & Monaghan (2007) |
| 163 |
func = rhoh - rho |
| 164 |
if (omega > tiny(omega)) then |
| 165 |
dfdh1 = dhdrho/omega |
| 166 |
else |
| 167 |
dfdh1 = dhdrho/abs(omega + epsilon(omega)) |
| 168 |
endif |
| 169 |
|
| 170 |
! damped newton-raphson iteration |
| 171 |
hnew = h - func*dfdh1 |
| 172 |
if (hnew > 1.2*h) then |
| 173 |
hnew = 1.2*h |
| 174 |
elseif (hnew < 0.8*h) then |
| 175 |
hnew = 0.8*h |
| 176 |
endif |
| 177 |
|
| 178 |
! convergence condition |
| 179 |
converged = ((abs(hnew-h)/h_old) < tolh .and. omega > 0. .and. h > 0.) |
| 180 |
h = hnew |
| 181 |
|
| 182 |
end subroutine update_h |
| 183 |
|
| 184 |
end module get_h |