-
Notifications
You must be signed in to change notification settings - Fork 0
/
pta_animation.py
377 lines (280 loc) · 11.8 KB
/
pta_animation.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#from mpl_toolkits.mplot3d import Axes3D
#import mpl_toolkits.mplot3d.art3d as art3d
#from mpl_toolkits.mplot3d.art3d import Poly3DCollection
#from matplotlib.pyplot import *
#from matplotlib.patches import Polygon,Circle
'''
12/21/2014
Speed up: make beam/cap there own classes and store x,y,z point matrices
'''
import numpy as np
import subprocess
from mayavi import mlab
from mayavi.sources.builtin_surface import BuiltinSurface
import time
# Remove vtk warnings
# https://github.com/enthought/mayavi/issues/3
try:
__IPYTHON__
ipython = True
except NameError:
ipython = False
### THIS CAUSES ERRORS IN IPYTHON
import vtk
output=vtk.vtkFileOutputWindow()
output.SetFileName("log.txt")
vtk.vtkOutputWindow().SetInstance(output)
from configuration import *
sin = lambda x: np.sin(unitfunc(x))
cos = lambda x: np.cos(unitfunc(x))
def gaussian(x,a,b,c):
return a*np.exp(-0.5*((x-b)/c)**2)
class Pulsar:
def __init__(self,xp,yp,zp,period,timestep=0.0,rotaxis=(0,0,0),beamZ=BEAMZ,xrot=0.0,yrot=0.0,zrot=0.0,pulsephase = 0.2):
# Pulsar characteristics
self.xp = xp
self.yp = yp
self.zp = zp
self.period = period
self.xrotaxis,self.yrotaxis,self.zrotaxis = rotaxis #could go to a two coordinate system?
self.phase = 0
# Initial conditions
self.xrot = xrot
self.yrot = yrot
self.zrot = zrot
self.pulsephase = pulsephase #more to do here, the initial pulse phase
# Generation
#self.beam1,self.cap1 = self.plot_beam(beamZ,xrot,yrot,zrot)
#self.beam2,self.cap2 = self.plot_beam(-1*beamZ,xrot,yrot,zrot)
#self.beam1 = self.plot_beam(beamZ,xrot,yrot,zrot)
#self.beam2 = self.plot_beam(-1*beamZ,xrot,yrot,zrot)
self.beams = self.plot_beam2(beamZ,xrot,yrot,zrot)
self.sphere = self.plot_sphere()
if EARTH_ON and PULSE_ON:
self.pulsetrain = self.plot_pulsetrain()
# Animation
self.timestep = timestep
self.Rmatrix = self.build_Rmatrix()
def plot_beam2(self,Z=1,xrot=None,yrot=None,zrot=None): #modified from
n = BEAMN
t = np.linspace(-np.pi, np.pi, n)
z = np.exp(1j * t)
x = z.real.copy()*BEAMR
y = z.imag.copy()*BEAMR
z = np.zeros_like(x)
triangles = [(0, i, i + 1) for i in range(1, n)]
triangles.extend([((n+1), (n+1)+i, (n+1)+i +1) for i in range(1, n)])
triangles.extend([(2*(n+1), 2*(n+1)+i, 2*(n+1)+i+1) for i in range(1, n)])
triangles.extend([(3*(n+1), 3*(n+1)+i, 3*(n+1)+i + 1) for i in range(1, n)])
x = np.r_[0, x]
y = np.r_[0, y]
z = np.r_[Z, z] - Z
zneg = -1*z#np.r_[-1*Z, z] + Z
xcap = np.copy(x)
ycap = np.copy(y)
zcap = np.zeros_like(x)-Z
zcapneg = np.zeros_like(x)+Z
#t = np.r_[0, t]
x = np.r_[x,xcap]
y = np.r_[y,ycap]
zneg = np.r_[-1*z,-1*zcap]
z = np.r_[z,zcap]
x = np.r_[x,x]
y = np.r_[y,y]
z = np.r_[z,zneg]
if xrot != 0.0 or yrot != 0.0 or zrot != 0.0:
pts = np.matrix(np.vstack((x,y,z)))
R = np.identity(3)
if xrot != 0.0:
c = cos(xrot)
s = sin(xrot)
R = np.matrix([[1,0,0],[0,c,-s],[0,s,c]])*R
if yrot != 0.0:
c = cos(yrot)
s = sin(yrot)
R = np.matrix([[c,0,s],[0,1,0],[-s,0,c]])*R
if zrot != 0.0:
c = cos(zrot)
s = sin(zrot)
R = np.matrix([[c,-s,0],[s,c,0],[0,0,1]])*R
for i in range(len(x)):
x[i],y[i],z[i] = R*pts[:,i]
#ptscap = np.matrix(np.vstack((xcap,ycap,zcap)))
#for i in range(len(x)):
# xcap[i],ycap[i],zcap[i] = R*ptscap[:,i]
return mlab.triangular_mesh(x+self.xp, y+self.yp, z+self.zp, triangles,color=COLOR_BEAM)
beam = mlab.triangular_mesh(x+self.xp, y+self.yp, z+self.zp, triangles,color=COLOR_BEAM)
#Plot cap
cap = mlab.triangular_mesh(xcap+self.xp,ycap+self.yp,zcap+self.zp,triangles,color=COLOR_BEAM)
return beam,cap
def plot_beam(self,Z=1,xrot=None,yrot=None,zrot=None): #modified from
n = BEAMN
t = np.linspace(-np.pi, np.pi, n)
z = np.exp(1j * t)
x = z.real.copy()*BEAMR
y = z.imag.copy()*BEAMR
z = np.zeros_like(x)
triangles = [(0, i, i + 1) for i in range(1, n)]
x = np.r_[0, x]
y = np.r_[0, y]
z = np.r_[Z, z] - Z
xcap = np.copy(x)
ycap = np.copy(y)
zcap = np.zeros_like(x)-Z
t = np.r_[0, t]
if xrot != 0.0 or yrot != 0.0 or zrot != 0.0:
pts = np.matrix(np.vstack((x,y,z)))
R = np.identity(3)
if xrot != 0.0:
c = cos(xrot)
s = sin(xrot)
R = np.matrix([[1,0,0],[0,c,-s],[0,s,c]])*R
if yrot != 0.0:
c = cos(yrot)
s = sin(yrot)
R = np.matrix([[c,0,s],[0,1,0],[-s,0,c]])*R
if zrot != 0.0:
c = cos(zrot)
s = sin(zrot)
R = np.matrix([[c,-s,0],[s,c,0],[0,0,1]])*R
for i in range(len(x)):
x[i],y[i],z[i] = R*pts[:,i]
#x,y,z = R*pts
#x = np.array(x).flatten()
#y = np.array(y).flatten()
#z = np.array(z).flatten()
#print R*pts
#print np.shape(np.array(x).flatten()),y,z
#raise SystemExit
ptscap = np.matrix(np.vstack((xcap,ycap,zcap)))
for i in range(len(x)):
xcap[i],ycap[i],zcap[i] = R*ptscap[:,i]
#xcap,ycap,zcap = R*ptscap
#xcap = np.array(xcap).flatten()
#ycap = np.array(ycap).flatten()
#zcap = np.array(zcap).flatten()
beam = mlab.triangular_mesh(x+self.xp, y+self.yp, z+self.zp, triangles,color=COLOR_BEAM)
cap = mlab.triangular_mesh(xcap+self.xp, ycap+self.yp, zcap+self.zp, triangles,color=COLOR_BEAM)
return beam,cap
def plot_sphere(self):
# Create a sphere for the pulsar
phi, theta = np.mgrid[0:np.pi:SPHERE_RESOLUTION, 0:2 * np.pi:SPHERE_RESOLUTION]
x = RADIUS_PULSAR * np.sin(phi) * np.cos(theta)
y = RADIUS_PULSAR * np.sin(phi) * np.sin(theta)
z = RADIUS_PULSAR * np.cos(phi)
sphere = mlab.mesh(x+self.xp , y+self.yp, z+self.zp,color=COLOR_PULSAR)
return sphere
def plot_pulsetrain(self):
self.pulsex = np.linspace(self.xp,0,PULSE_RESOLUTION) #these do not change
self.pulsey = np.linspace(self.yp,0,PULSE_RESOLUTION)
self.pulsez = np.linspace(self.zp,0,PULSE_RESOLUTION)
self.pulser = np.sqrt(self.pulsex**2 + self.pulsey**2 + self.pulsez**2)
self.Npulses = int(np.ceil(np.max(self.pulser)/(2*np.pi)))+1 #+1 for loop, move this there?
zpulse = np.zeros_like(self.pulsez)
for n in range(self.Npulses):
zpulse += gaussian(self.pulser,3.0,n*2*np.pi,0.3) #change pulse width
pulsetrain = mlab.plot3d(self.pulsex,self.pulsey,self.pulsez+zpulse,color=COLOR_PULSE,tube_radius=0.15)#),line_width=1036.0)
return pulsetrain
def get_components(self):
return self.sphere,self.beams
#return self.sphere,self.beam1,self.cap1,self.beam2,self.cap2
def build_Rmatrix(self):
self.rotation = self.timestep * (2*np.pi/self.period)
R = np.matrix([[np.cos(self.rotation),-np.sin(self.rotation),0],[np.sin(self.rotation),np.cos(self.rotation),0],[0,0,1]]) #SHOULD THIS JUST BE COS AND SIN?
RXneg = np.matrix([[1,0,0],[0,cos(-self.xrotaxis),-sin(-self.xrotaxis)],[0,sin(-self.xrotaxis),cos(-self.xrotaxis)]])
RXpos = np.matrix([[1,0,0],[0,cos(self.xrotaxis),-sin(self.xrotaxis)],[0,sin(self.xrotaxis),cos(self.xrotaxis)]])
RYneg = np.matrix([[cos(-self.yrotaxis),0,sin(-self.yrotaxis)],[0,1,0],[-sin(-self.yrotaxis),0,cos(-self.yrotaxis)]])
RYpos = np.matrix([[cos(self.yrotaxis),0,sin(self.yrotaxis)],[0,1,0],[-sin(self.yrotaxis),0,cos(self.yrotaxis)]])
RZneg = np.matrix([[cos(-self.zrotaxis),-sin(-self.zrotaxis),0],[sin(-self.zrotaxis),cos(-self.zrotaxis),0],[0,0,1]])
RZpos = np.matrix([[cos(self.zrotaxis),-sin(self.zrotaxis),0],[sin(self.zrotaxis),cos(self.zrotaxis),0],[0,0,1]])
R = RZpos*RYpos*RXpos*R*RXneg*RYneg*RZneg
return R
def rotate(self):
items = self.get_components()[1:]
#print self.phase
for item in items:
x,y,z = item.mlab_source.x-self.xp,item.mlab_source.y-self.yp,item.mlab_source.z-self.zp
#a = time.time()
pts = np.matrix(np.vstack((x,y,z)))
#b = time.time()
x,y,z = self.Rmatrix*pts
x = np.array(x).flatten()
y = np.array(y).flatten()
z = np.array(z).flatten()
#for i in range(len(x)):
# x[i],y[i],z[i] = self.Rmatrix*pts[:,i]
#c = time.time()
item.mlab_source.set(x=x+self.xp,y=y+self.yp,z=z+self.zp)
#d = time.time()
#print b-a,d-c
def propagate(self):
zpulse = np.zeros_like(self.pulsez)
self.phase += np.abs(self.rotation/(2*np.pi))
if self.phase >= 1.0:
self.phase = self.phase % 1
for n in range(self.Npulses):
zpulse += gaussian(self.pulser,3.0,(n-self.phase)*2*np.pi,0.3) #change pulse width
self.pulsetrain.mlab_source.set(z=self.pulsez+zpulse)
def animate(self):
self.rotate()
if EARTH_ON and PULSE_ON:
self.propagate()
#mlab.options.offscreen = True
#mayavi.engine.current_scene.scene.off_screen_rendering = True
mlab.figure(1, bgcolor=BGCOLOR, fgcolor=FGCOLOR, size=SIZE)
#Make Earth
'''
phi, theta = np.mgrid[0:np.pi:SPHERE_RESOLUTION, 0:2 * np.pi:SPHERE_RESOLUTION]
x = RADIUS_EARTH * np.sin(phi) * np.cos(theta)
y = RADIUS_EARTH * np.sin(phi) * np.sin(theta)
z = RADIUS_EARTH * np.cos(phi)
earth = mlab.mesh(x, y, z,color=COLOR_EARTH)
'''
if EARTH_ON:
continents_src = BuiltinSurface(source='earth',name='Continents')
continents_src.data_source.on_ratio = 2
continents_src.data_source.radius = 2.5
continents = mlab.pipeline.surface(continents_src,color=(0,0,0))
sphere = mlab.points3d(0,0,0,scale_mode='none',scale_factor=5,color=COLOR_EARTH,resolution=50)#,opacity=05,name='Earth')
#Star field
if STARS_ON:
np.random.seed(RANDOM_SEED) #is this needed elsewhere?
x = np.random.uniform(-SKYBOX,SKYBOX,N_STARS)
y = np.random.uniform(-SKYBOX,SKYBOX,N_STARS)
z = np.random.uniform(-SKYBOX,SKYBOX,N_STARS)
mlab.points3d(x,y,z,color=COLOR_STAR,scale_factor=0.5)
ps = []
for PULSAR in PULSARS:
ps.append(eval("Pulsar(%s)"%PULSAR))
mlab.view(CAMERA_AZIMUTH,CAMERA_ELEVATION)
f = mlab.gcf()
f.scene.camera.zoom(CAMERA_ZOOM)
f.scene.anti_aliasing_frames = ANTIALIASING
#mlab.show()
#raise SystemExit
@mlab.animate(delay=50,ui=False)
def anim():
i=0
while i < N_FRAMES:
a = time.time()
f.scene.camera.azimuth(CAMERA_ROTATE_AZIMUTH)
f.scene.camera.elevation(CAMERA_ROTATE_ELEVATION)
f.scene.disable_render= True
for p in ps:
p.animate()
f.scene.disable_render = False
f.scene.render()
f.scene.save(OUTPUT_FILENAME_FORMAT%i)#,size=SIZE) #SIZE needed?
i+=1
b = time.time()
print "(%i/%i)"%(i,N_FRAMES)
print b-a
yield
print "Done"
if not ipython:
subprocess.call("rm log.txt",shell=True)
a = anim()
mlab.show()
#try:
# subprocess.call("convert -delay 5 *png -loop 0 animated.gif",shel=True)
#convert -delay 5 *png -loop 0 animated.gif