-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubsonic.py
202 lines (155 loc) · 6.33 KB
/
subsonic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import matplotlib
from matplotlib import rc
from matplotlib import axes
from matplotlib import interactive, use
from matplotlib import ticker
from numpy import *
import numpy.ma as ma
from pylab import *
from scipy.integrate import cumulative_trapezoid, simpson
from scipy.interpolate import interp1d
from scipy.optimize import minimize, root, root_scalar
import glob
import re
import os
import time
#Uncomment the following if you want to use LaTeX in figures
# rc('font',**{'family':'serif'})
# rc('mathtext',fontset='cm')
# rc('mathtext',rm='stix')
# rc('text', usetex=True)
# #add amsmath to the preamble
# matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amssymb,amsmath}"]
close('all')
ioff()
use('Agg')
import configparser as cp
conffile = 'globals.conf'
config = cp.ConfigParser(inline_comment_prefixes="#")
config.read(conffile)
ifplot = config['DEFAULT'].getboolean('ifplot')
if ifplot:
import plots
formatsequence = ['k-', 'g:', 'b--', 'r-.']
# K = 0.5
omega = 0.0
# f0 = 10.0
def lowk(theta, theta0, rrat, beta, umag):
ufac = 1.+(1./tan(theta)**2-1./tan(theta0)**2)*rrat/beta
ufac = minimum((ufac/ufac[-1])**(4.)*umag/umag[-1], 3.)
return ufac
def fcfun(x0, K):
nx = 1e6
x = x0 * arange(nx)/double(nx)
return 1.5 * simpson(exp(K * x * (1.+x**2)) * x / (1.-x**2)**2,x=x) #, initial = 0)
def intfun(x, f0, K):
# formal solution for f = normalized (u/rho). f0 is the value at x = cos(theta) = cos(theta_out) \simeq 0
return exp(-K * x * (1.+x**2)) * (f0 + 1.5 * cumulative_trapezoid(exp(K * x * (1.+x**2)) * x / (1.-x**2)**2,x=x, initial = 0.))
def luintfun(f, x):
# int I from 0 to x. lnu = I(x0) - I(x)
return -3. * cumulative_trapezoid(x / f * (3.*omega**2*(1.-x**2)**2 - 2./(1.-x**2)**2), x=x, initial=0.)
def uint(theta0, f0, K, firstpoint=False, theta_out = pi/2.):
nth = 10000
theta = (theta_out-theta0) * arange(nth)/double(nth-1)+ theta0
x = cos(theta[::-1])
# theta = theta[::-1]
fint = intfun(x, f0, K)
fint = maximum(fint, 0.)
luint = luintfun(fint,x)[::-1]
luint = luint-luint[-1] # so that the function (ln u) is 0 @ theta_out
if firstpoint:
return theta[0], fint[-1], luint[0] # theta0, f at the surface, ln u at the surface (should be =3)
else:
return theta, fint[::-1], luint
def fzero_solution(conf = 'ASOL_slowT4', snapshot = None):
# solve for f(theta0) = 0
# reading the data:
rstar = config[conf].getfloat('rstar')
m1 = config[conf].getfloat('m1')
mu30 = config[conf].getfloat('mu30')
mdot = config[conf].getfloat('mdot') * 4.*pi # now it is in G M / c kappa
xifac = config[conf].getfloat('xifac')
r_e = config[conf].getfloat('r_e_coeff') * (mu30**2/mdot)**(2./7.)*m1**(-10./7.) * xifac # magnetosphere radius
afac = config[conf].getfloat('afac')
drrat = config[conf].getfloat('drrat')
Dthick = config[conf].getfloat('Dthick')
theta0 = arcsin(sqrt(rstar/r_e)) # polar cap radius
theta_out = arcsin(1./sqrt(1.+drrat**2))
k = afac / drrat * r_e / m1 / mdot # k parameter
print("r_e = ", r_e, " = ", r_e/rstar, "R*")
print("theta0 = ", theta0)
print("k = ", k)
print("expected f0 = ", 0.75 * Dthick**2)
if snapshot is not None:
linesT = loadtxt(snapshot) # 'vcomp/tireoutT.dat'
rT = linesT[:,0] ; uT = linesT[:,3] ; rhoT = linesT[:,1]
thetaT = arcsin(sqrt(rT*rstar/r_e))
vT = linesT[:,2] ; rhoT = linesT[:,1]
mdotT = -(vT * rhoT)[-1]*4.*pi*r_e**2*drrat # units?
fT = uT/rhoT * rT[-1]
print("measured mdot = ", mdotT/4./pi)
print("Re = ", rT[-1]*rstar)
print("fT = ",fT)
#mdot = mdotT
#k = afac / drrat * r_e / m1 / mdot # k parameter
#print("internal k = ", k)
theta0 = thetaT.min()
theta_out = thetaT.max()
# we want to find the f0 that produces f(theta0)=0
# logarithmic bracketing
# minimal f0 should be 3/4 of the int, because we do not want f_surface to change sign
f0 = fcfun(cos(theta0), k)
print("f0min = ", f0)
# ii = input('f0')
lf1 = log10(f0)-2.0 ; lf2 = log10(f0)+2.0 ; tol = 1e-10
theta, fint1, u1 = uint(theta0, 10.**lf1, k, theta_out = theta_out, firstpoint = True)
theta, fint2, u2 = uint(theta0, 10.**lf2, k, theta_out = theta_out, firstpoint = True)
umagrat = -12. * log(sin(theta0)) + log(1.+3.*cos(theta0)**2) # + log(3.)
ucrit1 = u1-umagrat-log(3.) ; ucrit2 = u2-umagrat - log(3.)
# (1.+3.*cos(theta)**2)/(1.+3.*cos(theta0)**2)*(sin(theta0)/sin(theta))**6
print(umagrat)
# ii = input("theta")
print("f0 = ", 10.**lf1, ": f(0) = ", fint1, "; u[-1] = ", ucrit1)
print("f0 = ", 10.**lf2, ": f(0) = ", fint2, "; u[-1] = ", ucrit2)
# same sign is not expected
if (ucrit1*ucrit2 >= 0.):
return 0.
while (abs(lf2-lf1) > tol ):
lf = (lf1+lf2)/2.
theta, fint, u = uint(theta0, 10.**lf, k, theta_out = theta_out, firstpoint = True)
ucrit = u-umagrat - log(3.)
print("f0 = ", 10.**lf, ": f(0) = ", fint)
if ((ucrit*ucrit1) >= 0.):
lf1 = lf
else:
lf2 = lf
theta, fint, u = uint(theta0, 10.**lf2,k, theta_out = theta_out)
u = exp(u-umagrat)
print("beta = ", 0.75 * fint[0] * sin(theta0)**2)
beta = 0.75 * fint[0] * sin(theta0)**2
print("f0 = ", fint[-1])
umag = (1.+3.*cos(theta)**2)/(1.+3.*cos(theta0)**2)*(sin(theta0)/sin(theta))**12
if snapshot is not None:
umagsnap = (1.+3.*cos(thetaT)**2)/(1.+3.*cos(theta0)**2)*(sin(theta0)/sin(thetaT))**12
umagsnap0 = (1.+3.*cos(thetaT)**2)/sin(thetaT)**12
print("U/Umag_out = ",(u/umag))
plots.subfint(theta, fint, u/umag, thetaT, uT, fT = fT * umagsnap0/umagsnap[0])
# ASCII output:
fout = open('uint.dat', 'w+')
fout.write('# theta f u/umag\n')
nx = size(theta)
for k in arange(nx):
fout.write(str(theta[k])+' '+str(fint[k])+' '+str((u/umag)[k])+'\n')
fout.flush()
fout.close()
fout = open('uint_xi.dat', 'w+')
fout.write('# theta f u/umag\n')
nx = size(theta)
for k in arange(nx):
xi = (sin(theta[k])/sin(theta0))**2
fout.write(str(xi)+' '+str(fint[k])+' '+str((u/umag)[k])+'\n')
fout.flush()
fout.close()
return lf
# usage:
# fzero_solution(conf='ASOL_slowT4', snapshot='vcomp/tireoutT.dat')