Debian Codegraph @

src/exact_bondi.f90

raw
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 ! Exact solution for Bondi flow
25 ! ----------------------------------------------------------------------
26 module bondi
27 implicit none
28
29 public :: exact_bondi
30
31 real, private, parameter :: pi = 4.*atan(1.)
32
33 ! Constants from user input
34 real, private :: rcrit, rhocrit ! for non-rel
35 real, private :: den0, en0 ! for GR geodesic flow
36 real, private :: adiabat ! for GR sonic-point flow (and rcrit as well)
37
38 real, private :: C1,C2,Tc,n ! for GR sonic-point flow: intermediate constants not input by user
39 real, private :: mass1
40 logical, private :: iswind
41
42 private
43
44 contains
45
46 subroutine exact_bondi(iplot,time,gamma,const1,const2,m,relativistic,geodesic_flow,is_wind,xpts,ypts,ierr)
47 integer, intent(in) :: iplot
48 integer, intent(out) :: ierr
49 real, intent(in) :: time,gamma,const1,const2,m
50 logical, intent(in) :: relativistic, geodesic_flow,is_wind
51 real, dimension(:), intent(in) :: xpts
52 real, dimension(size(xpts)), intent(out) :: ypts
53 integer :: i,npts
54 real :: r,rhor,vr,ur
55
56 npts = size(xpts)
57
58 print "(a,es10.3)",' Plotting exact Bondi solution at t = ',time
59
60 if (.not.relativistic) then
61 rcrit = const1
62 rhocrit = const2
63 elseif (relativistic) then
64 if (geodesic_flow) then
65 den0 = const1
66 en0 = const2
67 elseif (.not.geodesic_flow) then
68 rcrit = const1
69 adiabat = const2
70 iswind = is_wind
71 endif
72 endif
73 !
74 !--determine which parameter to plot
75 !
76 do i=1,npts
77 r = xpts(i)
78
79 vr = 0.
80 ur = 0.
81 rhor = 0.
82
83 ! Note: Lambert functions solutions not great below 0.3 for some rcrit and rhocrit
84 if (.not. relativistic .and. r>0.3) then
85 call get_bondi_nonrel(rhor,vr,ur,r,m,gamma)
86 elseif (relativistic .and. r>2.) then
87 if (geodesic_flow) then
88 call get_bondi_geodesic(rhor,vr,ur,r,m,gamma)
89 elseif (.not. geodesic_flow) then
90 call get_bondi_sonicpoint(rhor,vr,ur,r,m,gamma)
91 endif
92 endif
93
94 select case(iplot)
95 case(1)
96 ypts(i) = vr
97 if (.not.is_wind) ypts(i) = -vr
98 case(2)
99 ypts(i) = ur
100 case(3)
101 ypts(i) = rhor
102 case default
103 ypts(i) = 0.
104 end select
105 enddo
106 ierr = 0
107
108 return
109 end subroutine exact_bondi
110
111
112 !------------------------------------------------------------------------
113 !--- Non-relativistic solution ------------------------------------------
114 !------------------------------------------------------------------------
115
116 subroutine get_bondi_nonrel(rho,v,u,r,mass,gamma)
117 real, intent(out) :: rho,v,u
118 real, intent(in) :: r,mass,gamma
119 real :: cs2,vr,mdot
120
121 cs2 = mass/(2.*rcrit)
122 mdot = rhocrit*4.*pi*rcrit**2*sqrt(cs2)
123
124 if (r>=rcrit) then
125 vr = sqrt(-cs2*lambertw_0(-func(r,rcrit)))
126 else
127 vr = sqrt(-cs2*lambertw_neg1(-func(r,rcrit)))
128 endif
129
130 v = vr
131 rho = mdot/(4.*pi*abs(v)*r**2)
132 u = 0.
133
134 end subroutine get_bondi_nonrel
135
136 ! See Barry, Parlange & Li 2000, for the analytic approximations used for the Lambert W function.
137 !
138 !--- Lambert W function for the principal branch (k=0) approaching from the negative
139 real function lambertw_0(x)
140 real, intent(in) :: x
141 real, parameter :: exp1 = exp(1.)
142 real :: eta, N1, N2
143 eta = 2. + 2.*exp1*x
144 N2 = 6. + 3.*Sqrt(2.) - ((-5764. - 4108.*Sqrt(2.) + (2237. + 1457.*Sqrt(2.))*exp1)*eta)/&
145 (-796. - 430.*Sqrt(2.) + (215. + 199.*Sqrt(2.))*exp1)
146 N1 = (1. - 1./Sqrt(2.))*(Sqrt(2.) + N2)
147 lambertw_0 = -1 + Sqrt(eta)/(1 + (Sqrt(eta)*N1)/(Sqrt(eta) + N2))
148 end function lambertw_0
149
150 !--- Lambert W function for the k=-1 branch
151 real function lambertw_neg1(x)
152 real, intent(in) :: x
153 real, parameter :: M1 = 0.3361, M2 = -0.0042, M3 = -0.0201
154 real :: sigma
155 sigma = -1. - Log(-x)
156 lambertw_neg1 = -1. - sigma - (2.*(1. - 1./(1. + (M1*Sqrt(sigma)*(1. + exp(M3*Sqrt(sigma))*M2*sigma))/Sqrt(2.))))/M1
157 end function lambertw_neg1
158
159 !--- Function used in the non-rel solution of velocity [D(r)] (See: Cranmer 2004)
160 real function func(r,rc)
161 real, intent(in) :: r,rc
162 func = (rc/r)**4 * exp(4.*(1.-rc/r) - 1.)
163 end function func
164
165
166 !------------------------------------------------------------------------
167 !--- GR geodesic flow solution ------------------------------------------
168 !------------------------------------------------------------------------
169
170 subroutine get_bondi_geodesic(rho,v,u,r,m,gamma)
171 real, intent(out) :: rho,v,u
172 real, intent(in) :: r,m,gamma
173 real :: sqrtg,alpha,dfunc,efunc
174
175 dfunc = den0/(r**2*sqrt(2.*m/r*(1.- 2.*m/r)))
176 efunc = en0/((sqrt(2.*m/r)*r**2)**gamma * sqrt(1.- 2.*m/r))
177
178 sqrtg = 1.
179 alpha = sqrt(1. - 2.*m/r)
180 rho = sqrtg/alpha*dfunc
181 v = sqrt(2.*m/r)*(1. - 2.*m/r)
182 u = efunc/dfunc
183
184 end subroutine get_bondi_geodesic
185
186
187 !------------------------------------------------------------------------
188 !--- GR sonic point flow solution ---------------------------------------
189 !------------------------------------------------------------------------
190 subroutine get_bondi_sonicpoint(rho,v,u,r,m,gamma)
191 real, intent(out) :: rho,v,u
192 real, intent(in) :: r,m,gamma
193 real :: T,uvel,term,u0,dens,sqrtg
194 real :: uc2,vc2
195
196 n = 1./(gamma-1.)
197 mass1 = m
198
199 uc2 = mass1/(2.*rcrit)
200 vc2 = uc2/(1.-3.*uc2)
201
202 Tc = vc2*n/(1.+n-vc2*n*(1.+n))
203 C1 = sqrt(uc2) * Tc**n * rcrit**2
204 C2 = (1. + (1.+n)*Tc)**2 * (1. - 2.*mass1/rcrit + C1**2/(rcrit**4*Tc**(2.*n)))
205
206 ! Given an r, solve eq 76 for T numerically (Hawley, Smarr and Wilson 1976a)
207 call Tsolve(T,r)
208
209 uvel = C1/(r**2 * T**n)
210 dens = adiabat*T**n
211 u = T*n
212
213 !get u0 at r
214 term = 1./(1.-2.*mass1/r)
215 u0 = sqrt(term*(1.+term*uvel**2))
216 v = uvel/u0
217
218 sqrtg = 1.
219 rho = sqrtg*u0*dens
220
221 end subroutine get_bondi_sonicpoint
222
223 ! Newton Raphson
224 subroutine Tsolve(T,r)
225 real, intent(in) :: r
226 real, intent(out) :: T
227 real :: Tnew, diff
228 logical :: converged
229 integer :: its
230 integer, parameter :: itsmax = 100
231 real, parameter :: tol = 1.e-5
232
233 ! These guess values may need to be adjusted for values of rcrit other than rcrit=8M
234 if ((iswind .and. r>=rcrit) .or. (.not.iswind .and. r<rcrit)) then
235 T = 0.760326*r**(-1.307)/2. ! This guess is calibrated for rcrit=8M, and works ok up to r ~ 10^7 M
236 elseif ((iswind .and. r<rcrit) .or. (.not.iswind .and. r>=rcrit)) then
237 T = 100.
238 endif
239
240 converged = .false.
241 its = 0
242 do while (.not.converged .and. its<itsmax)
243 Tnew = T - ffunc(T,r)/df(T,r)
244 diff = abs(Tnew - T)/abs(T)
245 converged = diff < tol
246 T = Tnew
247 its = its+1
248 enddo
249
250 if (.not.converged) print*,'Bondi exact solution not converged at r = ',r
251
252 end subroutine Tsolve
253
254 real function ffunc(T,r)
255 real, intent(in) :: T,r
256 ffunc = (1. + (1. + n)*T)**2*(1. - (2.*mass1)/r + C1**2/(r**4*T**(2.*n))) - C2
257 end function ffunc
258
259 real function df(T,r)
260 real, intent(in) :: T,r
261 df = (2.*(1. + T + n*T)*((1. + n)*r**3*(-2.*mass1 + r) - C1**2*T**(-1. - 2.*n)*(n + (-1. + n**2)*T)))/r**4
262 end function df
263
264 end module bondi