Debian Codegraph @

src/exact_ringspread.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-2012 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !----------------------------------------------------------------------
24 ! Plots Lynden-Bell & Pringle (1978) solution for viscous
25 ! ring spreading in an accretion disk.
26 !
27 ! Input: radius in disk
28 ! Output: surface density
29 !
30 ! D. Price, UofExeter 25.2.08
31 !----------------------------------------------------------------------
32 module ringspread
33 implicit none
34 public :: exact_ringspread, ringspreadfunc
35 private
36
37 contains
38
39 subroutine exact_ringspread(iplot,time,Mdisk,Rdisk,viscnu,xplot,yplot,ierr)
40 integer, intent(in) :: iplot
41 real, intent(in) :: time,Mdisk,Rdisk,viscnu
42 real, intent(in), dimension(:) :: xplot
43 real, intent(out), dimension(size(xplot)) :: yplot
44 integer, intent(out) :: ierr
45 real, parameter :: pi = 3.1415926536
46 integer :: i
47 real :: R2,sigma,tvisc
48 double precision :: tau,x
49 !
50 ! check for errors in input parameters
51 !
52 ierr = 0
53 if (Mdisk <= 0.) then
54 print*,'error: mass <= 0 in exact_ringspread'
55 ierr = 2
56 return
57 elseif (Rdisk <= 0.) then
58 print*,'error: rdisk < 0 in exact_ringspread'
59 ierr = 3
60 return
61 elseif (viscnu <= tiny(viscnu)) then
62 print*,'error: viscosity <= 0 in ringspreading solution'
63 ierr = 4
64 return
65 endif
66
67 R2 = Rdisk*Rdisk
68 tvisc = R2/(12.*viscnu)
69 tau = time/tvisc
70
71 print "(a,1pe9.2,a,1pe9.2,a,0pf6.2,a,f6.2)", &
72 ' Plotting ring spreading solution: tau = ',tau,' nu = ',viscnu,' R0 = ',Rdisk,' M = ',Mdisk
73
74 do i=1,size(xplot)
75 x = xplot(i)/Rdisk
76 sigma = Mdisk/real((pi*R2))*ringspreadfunc(x,tau)
77 !print*,'x = ',xplot(i),Rdisk,tau,sigma
78
79 select case(iplot)
80 case(1)
81 !--density
82 yplot(i) = sigma
83 case default
84 !--pressure
85 yplot(i) = 0.
86 end select
87 enddo
88
89 return
90 end subroutine exact_ringspread
91
92 !----------------------------------------------------------------------
93 ! evaluates the surface density as a function of x and tau
94 !----------------------------------------------------------------------
95 double precision function ringspreadfunc(x,tau)
96 double precision, intent(in) :: x, tau
97 double precision :: xfunc,besfunc,dummy,term
98
99 if (tau <= epsilon(tau) .or. x <= tiny(x)) then
100 ringspreadfunc = 0.
101 else
102 xfunc = 2.*x/tau
103 term = exp(-(1.+x*x)/tau)
104
105 !--prevent blowups at t=0: no point evaluating
106 ! the Bessel function if the exp term is zero.
107 if (term > tiny(term)) then
108 call bessik(xfunc,0.25d0,besfunc,dummy,dummy,dummy)
109 else
110 besfunc = 0.
111 endif
112 ringspreadfunc = 1./(tau*x**0.25)*term*besfunc
113 !print*,'ringspreadfunc = ',x,xfunc,-(1.+x*x)/tau,exp(-(1.+x*x)/tau),ringspreadfunc,xfunc,tau,besfunc
114 endif
115
116 return
117 end function ringspreadfunc
118
119
120 !----------------------------------------------------------------------
121 ! remainder of this file are routines for evaluating the modified
122 ! Bessel function in the above solution
123 !----------------------------------------------------------------------
124
125 subroutine bessik(x,xnu,ri,rk,rip,rkp)
126 integer, parameter :: maxit = 10000
127 double precision, intent(in) :: x, xnu
128 double precision, intent(out) :: ri,rip,rk,rkp
129 double precision, parameter :: eps = 1.e-10, fpmin = 1.e-30, &
130 xmin = 2., pi = 3.141592653589793d0
131
132 ! Returns the modified Bessel functions ri = I\nu, rk = K\nu and their
133 ! derivatives rip = I'\nu and rkp = K'\nu, for positive x and for
134 ! xn = \nu >= 0. The relative accuracy is within one or two significant
135 ! digits of eps.
136 !
137 ! All internal arithmetic in double precision
138 !
139 ! This routine written by Daniel Price, UofExeter 25/2/08
140 ! Adapted from Press et. al. (1992) Numerical Recipes in FORTRAN 77
141 !
142 integer:: i,l,nl
143 double precision :: a,a1,b,c,d,del,del1,delh,dels,e,f,fact,fact2, &
144 ff,gam1,gam2,gammi,gampl,h,p,pimu,q,q1,q2,qnew,ril,ril1,rimu,rip1,ripl, &
145 ritemp,rk1,rkmu,rkmup,rktemp,s,sum,sum1,x2,xi,xi2,xmu,xmu2
146
147 if (x <= 0. .or. xnu < 0.) then
148 print*,' bad arguments in bessik ',x,xnu
149 ! return
150 endif
151
152 nl = int(xnu+0.5d0) ! nl is the number of downward recurrences of the I's
153 xmu = xnu - nl ! and upward recurrences of K's. xmu lies between
154 xmu2 = xmu*xmu ! -1/2 and 1/2
155 xi = 1.d0/x
156 xi2 = 2.d0*xi
157 h = xnu*xi
158 if (h < fpmin) h = fpmin
159 b = xi2*xnu
160 d = 0.d0
161 c = h
162 do i=1,maxit
163 b = b + xi2
164 d = 1.d0/(b + d)
165 c = b + 1.d0/c
166 del = c*d
167 h = del*h
168 if (abs(del-1.d0) < eps) goto 1
169 enddo
170 print*,'x too large in bessik; try asymptotic expansion'
171 1 continue
172 ril = fpmin
173 ripl = h*ril
174 ril1 = ril
175 rip1 = ripl
176 fact = xnu*xi
177 do l=nl,1,-1
178 ritemp = fact*ril + ripl
179 fact = fact - xi
180 ripl = fact*ritemp + ril
181 ril = ritemp
182 enddo
183 f = ripl/ril
184 if (x < xmin) then
185 x2 = 0.5d0*x
186 pimu = pi*xmu
187 if (abs(pimu) < eps) then
188 fact = 1.d0
189 else
190 fact = pimu/sin(pimu)
191 endif
192 d = -log(x2)
193 e = xmu*d
194 if (abs(e) < eps) then
195 fact2 = 1.d0
196 else
197 fact2 = sinh(e)/e
198 endif
199 ! Chebyshev evaluation of Gamma1, Gamma2
200 call beschb(xmu,gam1,gam2,gampl,gammi)
201 ff = fact*(gam1*cosh(e) + gam2*fact2*d) ! f0
202 sum = ff
203 e = exp(e)
204 p = 0.5d0*e/gampl
205 q = 0.5d0/(e*gammi)
206 c = 1.d0
207 d = x2*x2
208 sum1 = p
209 do i=1,maxit
210 ff = (i*ff + p + q)/(i*i - xmu2)
211 c = c*d/i
212 p = p/(i - xmu)
213 q = q/(i + xmu)
214 del = c*ff
215 sum = sum + del
216 del1 = c*(p - i*ff)
217 sum1 = sum1 + del1
218 if (abs(del) < abs(sum)*eps) goto 2
219 enddo
220 print*,' bessk series failed to converge'
221 2 continue
222 rkmu = sum
223 rk1 = sum1*xi2
224 else
225 b = 2.d0*(1.d0 + x)
226 d = 1.d0/b
227 delh = d
228 h = delh
229 q1 = 0.d0
230 q2 = 1.d0
231 a1 = 0.25d0 - xmu2
232 c = a1
233 q = c
234 a = -a1
235 s = 1.d0 + q*delh
236 do i=2,maxit
237 a = a - 2*(i-1)
238 c = -a*c/i
239 qnew = (q1 - b*q2)/a
240 q1 = q2
241 q2 = qnew
242 q = q + c*qnew
243 b = b + 2.d0
244 d = 1.d0/(b + a*d)
245 delh = (b*d - 1.d0)*delh
246 h = h + delh
247 dels = q*delh
248 s = s + dels
249 if (abs(dels/s) < eps) goto 3
250 enddo
251 print*,'bessik: failure to converge in cf2'
252 3 continue
253 h = a1*h
254 rkmu = sqrt(pi/(2.d0*x))*exp(-x)/s
255 rk1 = rkmu*(xmu + x + 0.5d0 - h)*xi
256 endif
257 rkmup = xmu*xi*rkmu - rk1
258 rimu = xi/(f*rkmu - rkmup)
259 ri = (rimu*ril1)/ril
260 rip = (rimu*rip1)/ril
261 do i=1,nl
262 rktemp = (xmu + i)*xi2*rk1 + rkmu
263 rkmu = rk1
264 rk1 = rktemp
265 enddo
266 rk = rkmu
267 rkp = xnu*xi*rkmu - rk1
268
269 return
270 end subroutine bessik
271
272 subroutine beschb(x,gam1,gam2,gampl,gammi)
273 integer, parameter :: nuse1=7,nuse2=8
274 double precision, intent(in) :: x
275 double precision, intent(out) :: gam1,gam2,gammi,gampl
276 !
277 ! Evaluates Gamma_1 and Gamma_2 by Chebyshev expansion for |x| < 1/2.
278 ! Also returns 1/Gamma(1 + x) and 1/Gamma(1-x).
279 !
280 ! In double precision, set NUSE1 = 7, NUSE2 = 8.
281 ! In single precision, set NUSE1 = 2, NUSE2 = 5.
282 !
283 ! This routine written by Daniel Price, UofExeter 25/2/08
284 ! Adapted from Press et. al. (1992) Numerical Recipes in FORTRAN 77
285 !
286 double precision :: xx,c1(7),c2(8)
287 save c1,c2
288 data c1/-1.142022680371168d0, 6.5165112670737d-3, &
289 3.087090173086d-4, -3.4706269649d-6,6.9437664d-9, &
290 3.67795d-11,-1.356d-13/
291 data c2/1.843740587300905d0, -7.68528408447867d-2, &
292 1.2719271366546d-3, -4.9717367042d-6, -3.31261198d-8, &
293 2.423096d-10, -1.702d-13, -1.49d-15/
294 xx = 8.d0*x*x - 1.d0 ! multiply x by 2 to make range be -1 to 1,
295 gam1 = chebev(-1.d0,1.d0,c1,NUSE1,xx) ! and then apply transformation for
296 gam2 = chebev(-1.d0,1.d0,c2,NUSE2,xx) ! evaluating even Chebyshev series
297 gampl = gam2 - x*gam1
298 gammi = gam2 + x*gam1
299
300 return
301 end subroutine beschb
302
303 double precision function chebev(a,b,c,m,x)
304 integer, intent(in) :: m
305 double precision, intent(in) :: a,b,x,c(m)
306 !
307 ! Chebyshev evaluation: All arguments are input. c(1:m) is an array of
308 ! Chebyshev coefficients, the first m elements of c output from chebft
309 ! (which must have been called with the same a and b). The Chebyshev
310 ! polynomial \sum_{k=1}^m c_k T_{k-1}(y) - c1/2 is evaluated at a
311 ! point y = [ x - (b + a)/2 ] / [(b - a)/2], and the result is returned
312 ! as the function value.
313 !
314 ! This routine written by Daniel Price, UofExeter 25/2/08
315 ! Adapted from Press et. al. (1992) Numerical Recipes in FORTRAN 77
316 !
317 integer :: j
318 double precision :: d, dd, sv, y, y2
319
320 if ((x-a)*(x-b) > 0.) then
321 print*,'error: x not in range in chebev'
322 chebev = 0.
323 return
324 endif
325 d = 0.
326 dd = 0.
327 y = (2.*x - a - b)/(b - a) ! change of variable
328 y2 = 2.*y
329 do j=m,2,-1 ! Clenshaw's recurrence
330 sv = d
331 d = y2*d - dd + c(j)
332 dd = sv
333 enddo
334 chebev = y*d - dd + 0.5*c(1) ! last step is different
335
336 end function chebev
337
338 end module ringspread