forked from rantonels/schwarzschild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solveode.py
237 lines (152 loc) · 4.9 KB
/
solveode.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import numpy as np
from scipy.integrate import odeint
import sys
import math
from multiprocessing import Process, Queue
import random
#some useful functions
def w(r):
return (1-1/r)
def wu(u):
return w(1/u)
def mUprime(u):
# This can be -U'. Who knows? Just (-1)^n until the hole is black and not white.
# White Hole: theoretical region where matter and energy can only escape, opposite of a black hole.
return -.5*(2*u - 3*u**2 )
def func(u,t):
# since we integrate over all phis, without stopping, THEN crop
# the solution where it hits the EH or diverges, we don't want
# it to wander aimlessly. We forced it to stop by erasing the derivative.
if (u[0]>1) or (u[0] < 0.0001):
return [0,0]
return [u[1], mUprime(u[0])]
def gradient(u,t):
#Jacobian of above
return [[0,1],[1-3*u[0] , 0]]
# give a solution for one initial condition
# returns pair: (array of phis, array of [u(phi), u'(phi)] pairs).
def geod(r0,r0prime,options={}):
u0 = [ 1/r0 , -r0prime/(r0*r0) ]
if('step' in options):
timestep = options['step']
else:
timestep = 0.005
if('maxangle' in options):
maxangle = options['maxangle']
else:
maxangle = 6.28
phi = np.arange(0,maxangle,timestep)
l = phi
u = odeint(func,u0,l, Dfun=gradient, printmessg=False)
return (l,u)
# solves a list of initial condition and yields
# list of solutions in the format above.
def geodqueue(q,sci,options):
out = {}
sys.stdout = open("/dev/null", "w")
sys.stderr = open("/dev/null", "w")
for el in sci:
#print el[0], el[1][0],el[1][1]
res = geod(el[1][0],el[1][1],options)
idd = el[0]
out[idd] = res
q.put(out)
# splits a list of initial conditions into 4 chunks and solves them using all cores.
# Initial conditions to this function must be provided
# as a dict of the form {index:conditions}, where index
# is an arbitrary integer.
def multicore_list(sc,options ={}): # sc is a dict with indices
sci = []
for i in sc:
sci.append( (i,sc[i]) )
#random.shuffle(sci)
#shuffling here is not really necessary. It makes things complex
l4 = len(sci)/4
chunks = [
sci[0:l4],
sci[l4:2*l4],
sci[2*l4:3*l4],
sci[3*l4:]
]
q = Queue()
processes = []
for i in range(4):
processes.append( Process(target=geodqueue, args=(q,chunks[i],options)) )
for i in range(4):
processes[i].start()
results = {}
for i in range(4):
got = q.get()
results.update(got)
for i in range(4):
processes[i].join()
#print len(results), len(sc)
return results
# computes a list of photonic paths starting at fixed r
# and with various view angles (radius vector / view vector angle, called theta)
def deflection_array(r,angles,options = {}):
rprimes = - r * 1/np.tan(angles)
inc = {}
for i in range(len(angles)):
inc[i] = [r,rprimes[i]]
res = multicore_list(inc,options)
ress = [ res[i] for i in range(len(angles)) ]
deflections = np.zeros((len(angles),5))
for i in range(len(rprimes)):
deflections[i,0] = angles[i]
#print res[i]
#exit()
phi = res[i][0]
path = res[i][1][:,0]
pder = res[i][1][:,1]
findex = -1
for t in range(len(path)):
if path[t] < 0.001:
findex = t
break
if path[t] > 0.999:
break
if findex == -1:
deflections[i,1] = -1
else:
deflections[i,1] = phi[t]
#deflections[i,2] = path[0]
#deflections[i,3] = path[1]
#deflections[i,4] = path[2]
return deflections
# tests
# these make nice files for gnuplot
if __name__ == "__main__":
thetas = np.arange(0.01,np.pi,0.01)
deff = deflection_array(10.0,thetas,{'maxangle':2*np.pi})
for i in range(len(deff)):
print deff[i][0], (deff[i][0] - (np.pi - deff[i][1] ))
exit()
rs = np.arange(1.47,1.53,0.0025)
dirs = np.arange(-40.,-4.,0.2)
bs = np.arange(0.1,4.,0.1)
#inc = [ [b*1000,-b*(1000**2)] for b in bs ]
inc = { d : [10.,d] for d in dirs }
print "SOLVING"
trajs = multicore_list(inc,{'maxangle':2*6.28})
print "SAVING"
for d in dirs:
f = open('curves/infall%f'%d,'w')
(l,u) = trajs[d]
for i in range(len(l)):
if u[i,0] > 1:
break
if u[i,0] < 0.0001:
break
f.write(str(l[i]) + "\t" +
str(1/u[i,0]) + "\t" +
str(u[i,0]) + "\t" +
str(u[i,1])
+ "\n"
)
f.close()
sys.exit()
for d in dirs:
print d
f = open('curves/vel%f'%d,'w')
(l,u) = geod(1.5,d)