Debian Codegraph @

src/exact_rochelobe.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 ! Plots Roche Lobes (equipotential surface) for a binary system
25 ! Guts of it adapted from an original C routine by Simon Portugies-Zwart
26 !-----------------------------------------------------------------------
27 module rochelobe
28 implicit none
29 real, parameter :: roche_accuracy = 1.e-6
30 integer, parameter :: maxits = 100
31
32 contains
33
34 !-----------------------------------------------------------------------
35 ! Plot Roche Lobe
36 !
37 ! INPUT:
38 ! x1, y1 : position of primary
39 ! x2, y2 : position of secondary
40 ! m1, m2 : masses of components
41 !
42 ! OUTPUT:
43 ! xplot, yplot: contains (half of) the Roche lobe solution
44 ! ierr : error condition to splash indicating plotting was done here
45 !-----------------------------------------------------------------------
46 subroutine exact_rochelobe(x1,y1,x2,y2,m1,m2,xplot,yplot,ierr)
47 use plotlib, only:plot_line
48 real, intent(in) :: x1,y1,x2,y2,m1,m2
49 real, dimension(:), intent(inout) :: xplot,yplot
50 integer, intent(out) :: ierr
51 real :: roche_radius1,roche_radius2,q,L1
52 real :: rlimit,llimit,xmax,xmin,ymax,xtmp
53 real :: angle,cosangle,sinangle,dx,dy,sep
54 integer :: i,npts
55
56 npts = (size(xplot)-1)/2
57 if (npts < 1) return
58
59 sep = sqrt((x2 - x1)**2 + (y2 - y1)**2)
60 print "(4(a,es10.3))",' plotting Roche potential, m1 = ',m1,' m2 = ',m2,' sep = ',sep
61
62 roche_radius1 = roche_radius(m1, m2)
63 roche_radius2 = roche_radius(m2, m1)
64 q = m2/m1
65 L1 = first_Lagrangian_point(1./q)
66
67 ! We assume primary on the left
68 if (m1 < m2) then
69 q = 1/q
70 L1 = first_Lagrangian_point(1./q)
71 endif
72
73 rlimit = right_limit(q, L1)
74 llimit = left_limit(q, L1)
75
76 call compute_lobes(q, L1, npts, xplot, yplot)
77
78 xmin = xplot(1)
79 xmax = xplot(2*npts)
80 if (m1 < m2) then
81 xtmp = xmin
82 xmin = 1.-xmax
83 xmax = 1.-xtmp
84 endif
85
86 ymax = -1000.
87 do i=1,2*npts
88 ymax = max(ymax,yplot(i))
89 enddo
90
91 ! some tedious fiddling for q>1 case
92 if (m1 < m2) then
93 xplot(:) = 1. - xplot(:)
94 endif
95
96 ! scale to actual separation
97 xplot = xplot*sep
98 yplot = yplot*sep
99
100 ! work out angle needed to rotate into corotating frame
101 dx = x2 - x1
102 dy = y2 - y1
103 angle = -atan2(dy,dx)
104 cosangle = cos(angle)
105 sinangle = sin(angle)
106
107 ! lobes are computed assuming primary is at the origin, so shift to xprim,yprim
108 ! unrotated, this is just plot_line(xplot,yplot) and plot_line(xplot,-yplot)
109 call plot_line(2*npts+1,xplot*cosangle + yplot*sinangle + x1,-xplot*sinangle + yplot*cosangle + y1)
110 call plot_line(2*npts+1,xplot*cosangle - yplot*sinangle + x1,-xplot*sinangle - yplot*cosangle + y1)
111 !--return non-zero ierr value as we do the plotting here
112 ierr = 1
113
114 end subroutine exact_rochelobe
115 !
116 ! calculates outer limit of roche-lobe
117 !
118 subroutine rlimit(q, L, x, f, df, dummy)
119 real, intent(in) :: q,L,x,dummy
120 real, intent(out) :: f,df
121 real :: qi,q11,cnst,r1,r2,r3
122
123 qi = 1./q
124 q11 = 1./(1.+qi)
125 cnst = qi/L+1./(1.-L)+0.5*(1.+qi)*(L-q11)**2
126
127 r1 = abs(x)
128 r2 = abs(1-x)
129 r3 = abs(x-q11)
130
131 f = qi/r1+1./r2+0.5*(1.+qi)*r3**2 - cnst
132 df = -qi*x/r1**3 +(1.-x)/r2**3 + (1.+qi)*(x-q11)
133
134 end subroutine rlimit
135
136 real function rtsafe(func,q,L,x1,x2,xll,xacc)
137 real, intent(in) :: q,L,x1,x2,xll,xacc
138 external :: func
139 integer :: j
140 real :: df,dx,dxold,f,fh,fl
141 real :: temp,xh,xl,rts
142
143 call func(q,L,x1,fl,df,xll)
144 call func(q,L,x2,fh,df,xll)
145
146 if ((fl > 0.0 .and. fh > 0.0) .or. (fl < 0.0 .and. fh < 0.0)) then
147 !print*,'Error occured in rtsafe, exiting...',q,L,x1,x2,fl,fh
148 rtsafe = 0.
149 return
150 endif
151
152 if (abs(fl) < tiny(fl)) then
153 rtsafe = x1
154 return
155 endif
156
157 if (abs(fh) < tiny(fh)) then
158 rtsafe = x2
159 return
160 endif
161
162 call func(q, L, x1, f, df, xll)
163 call func(q, L, x2, f, df, xll)
164
165 if (fl < 0.0) then
166 xl=x1
167 xh=x2
168 else
169 xh=x1
170 xl=x2
171 endif
172
173 rts = 0.5*(x1+x2)
174 dxold = abs(x2-x1)
175 dx = dxold
176 call func(q, L, rts, f, df, xll)
177 do j=1,maxits
178 if ((((rts-xh)*df - f)*((rts-xl)*df - f) >= 0.0) &
179 .or. (abs(2.0*f) > abs(dxold*df))) then
180 dxold = dx
181 dx = 0.5*(xh-xl)
182 rts = xl+dx
183 if (abs(xl-rts) < tiny(rts)) then
184 rtsafe = rts
185 return
186 endif
187 else
188 dxold = dx
189 dx = f/df
190 temp = rts
191 rts = rts - dx
192 if (abs(temp-rts) < tiny(rts)) then
193 rtsafe = rts
194 return
195 endif
196 endif
197 if (abs(dx) < xacc) then
198 rtsafe = rts
199 return
200 endif
201
202 call func(q, L, rts, f, df, xll)
203 if (f < 0.0) then
204 xl = rts
205 else
206 xh = rts
207 endif
208 enddo
209 rtsafe = 0.
210 return
211 end function rtsafe
212
213 real function left_limit(q, L)
214 real, intent(in) :: q,L
215
216 left_limit = rtsafe(rlimit,q,L,-0.5*L,-L,0., roche_accuracy);
217
218 end function left_limit
219
220 real function right_limit(q, L)
221 real, intent(in) :: q,L
222
223 right_limit = rtsafe(rlimit,q,L,1.5-0.5*L,2.0-L,0., roche_accuracy);
224
225 end function right_limit
226
227 !
228 ! return roche radius as fraction of the semi-major axis.
229 ! So to obtain the true roche_radius call:
230 ! real Rl = semi_major_axis * roche_radius(m1, m2);
231 ! Eggleton PP., ApJ, 1983, 268, 368.
232 !
233 real function roche_radius(mthis, mother)
234 real, intent(in) :: mthis,mother
235 real :: mr,q1_3,q2_3
236
237 mr = mthis/mother
238 q1_3 = mr**(1./3.)
239 q2_3 = q1_3**2
240
241 roche_radius = 0.49*q2_3/(0.6*q2_3 + log(1 + q1_3))
242
243 end function roche_radius
244
245 real function first_Lagrangian_point(qinv)
246 real, intent(in) :: qinv
247 real :: fL, dfL, dL, L, q11
248
249 q11 = 1./(1.+qinv)
250 L = 0.5 + 0.2222222*log10(qinv)
251
252 dL = 1.e7
253 do while (abs(dL)>1.e-6)
254 fL = qinv/L**2- 1./(1.-L)**2 - (1.+qinv)*L + 1.
255 dfL=-2*qinv/L**3 - 2./(1.-L)**3 - (1.+qinv)
256 dL = -fL/(dfL*L)
257 L = L*(1.+dL)
258 enddo
259
260 first_Lagrangian_point = L
261
262 end function first_Lagrangian_point
263
264 subroutine rline(q, L, y, f, df, xl)
265 real, intent(in) :: q, L, y, xl
266 real, intent(out) :: f, df
267 real :: xsq,onexsq,qi,q11,cnst,cnst2,r1,r2
268
269 xsq=xl*xl
270 onexsq=(1.-xl)**2
271
272 qi=q
273 q11=1./(1.+qi)
274 cnst =qi/L+1./(1.-L) + 0.5*(1.+qi)*(L-q11)**2
275 cnst2=0.5*(1.+qi)*(xl-q11)**2 - cnst
276
277 r1=sqrt(xsq+y)
278 r2=sqrt(onexsq+y)
279 f=qi/r1+1./r2+cnst2
280 df =-0.5*qi/r1**3 - 0.5/r2**3
281
282 end subroutine rline
283
284 subroutine compute_lobes(q, L, npts, xplot, yplot)
285 real, intent(in) :: q, L
286 integer, intent(in) :: npts
287 real, intent(out), dimension(2*npts+1) :: xplot,yplot
288 real :: qi,q11,cnst,lrl,rrl,y1,y2,ysq,dxl,dxr
289 integer :: i
290
291 qi = 1/q
292 q11 = 1./(1.+qi)
293 cnst = qi/L+1./(1.-L) + 0.5*(1.+qi)*(L-q11)**2
294
295 lrl = left_limit(q, L)
296 xplot(1) = lrl
297 yplot(1) = 0.
298
299 xplot(npts+1) = L
300 yplot(npts+1) = 0.
301
302 rrl = right_limit(q, L)
303 xplot(2*npts) = rrl
304 yplot(2*npts) = 0.
305
306 y1 = 0.
307 y2 = L*L
308
309 !--left lobe
310 dxl = (xplot(npts+1)-xplot(1))/real(npts)
311 do i=1,npts
312 xplot(i+1) = xplot(1) + i*dxl
313 ysq = rtsafe(rline,qi,L,y1,y2,xplot(i+1),roche_accuracy)
314 yplot(i+1) = sqrt(ysq)
315 enddo
316
317 !--right lobe
318 dxr = (xplot(2*npts)-xplot(npts+1))/real(npts)
319 do i=1,npts
320 xplot(npts+i+1) = xplot(npts+1) + i*dxr
321 ysq = rtsafe(rline,qi,L,y1,y2,xplot(npts+i+1),roche_accuracy)
322 yplot(npts+i+1) = sqrt(ysq)
323 enddo
324
325 end subroutine compute_lobes
326
327 end module rochelobe