Commit 92c80197 authored by Zoheyr Doctor's avatar Zoheyr Doctor 🔭
Browse files

Merge branch 'add_artists' into 'master'

Add artists

See merge request !2
parents e143369d d3d63e60
......@@ -2,6 +2,7 @@
import numpy as np
from twirl.binary import Binary
from twirl.artists import BinaryBlackHole
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.patches import Circle
......@@ -84,9 +85,9 @@ def main():
}
pos_dict = {} # dictionary for all compact object positions
binary_dict = {} # dictionary for all Binary objects
bbh_dict = {} # dictionary for all BinaryBlackHole objects
# loop over all events
for event in events:
for ei,event in enumerate(events):
print('evolving ',event)
event_pos_dict = {} # dictionary for positions of this event
......@@ -108,37 +109,20 @@ def main():
binary.evolve(n_frames)
binary_dict[event] = binary
# make BBH artist
bbh = BinaryBlackHole(
binary = binary,
ax = ax[ei],
name=event,
BH_scale = 1./200
)
bbh_dict[event] = bbh
artist_dict = {} # dictionary for matplotlib artists
def init():
for ei,event in enumerate(events):
# create artists for both stars
z1,x1,y1 = binary_dict[event].pos1_projected[0,:]
z2,x2,y2 = binary_dict[event].pos2_projected[0,:]
artist_dict['%s_m1'%event] = ax[ei].add_artist(
Circle((x1,y1),
radius=binary_dict[event].m1/200,
edgecolor='white',
facecolor='black'
)
)
artist_dict['%s_m2'%event] = ax[ei].add_artist(
Circle((x2,y2),
radius=binary_dict[event].m2/200,
edgecolor='white',
facecolor='black'
)
)
# make sure star that is closer is shown on top of the other
zorder_1 = int(z1>z2)
zorder_2 = ~zorder_1
artist_dict['%s_m1'%event].set_zorder(zorder_1)
artist_dict['%s_m2'%event].set_zorder(zorder_2)
print(bbh_dict.keys())
artist_dict.update(bbh_dict[event].setup_artists())
# configure plot axes, title
ax[ei].set_xlim([-1,1])
......@@ -147,29 +131,13 @@ def main():
if not args.no_names:
print('putting names on events')
ax[ei].set_title(event)
print(artist_dict)
return artist_dict
def animate(i):
for ei,event in enumerate(events):
# update positions of stars
z1,x1,y1 = binary_dict[event].pos1_projected[i,:]
z2,x2,y2 = binary_dict[event].pos2_projected[i,:]
artist_dict['%s_m1'%event].set_center((x1,y1))
artist_dict['%s_m2'%event].set_center((x2,y2))
# make sure star that is closer is shown on top of the other
if z1>z2:
zorder_1 = 2
zorder_2 = 1
else:
zorder_1 = 1
zorder_2 = 2
artist_dict['%s_m1'%event].set_zorder(zorder_1)
artist_dict['%s_m2'%event].set_zorder(zorder_2)
return artist_dict
artist_dict.update(bbh_dict[event].get_artists(i))
return artist_dict
anim = animation.FuncAnimation(fig,animate,init_func=init,frames=n_frames)
#anim.save('orbit.gif',fps=fps,writer='imagemagick',extra_args=['-vcodec', 'libx264'])
......
#!/usr/bin/env python
import numpy as np
from twirl.artists import BlackHoleRingdown, BinaryBlackHole
from twirl.binary import InspiralingBinary
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.patches import Circle
import sys
plt.style.use('dark_background')
fig,ax = plt.subplots()
binary = InspiralingBinary(
m1=30.,
m2=30.,
alpha=60.,
beta=30.,
gamma=30.,
frame_rate=30.,
omega0 = 30.
)
binary.inspiral()
G = 4.3e-3 * 3.1e13 # km Msol^-1 (km/s)^2
c = 3e5
bbh = BinaryBlackHole(
binary=binary,
ax = ax,
name='BBH',
BH_scale=G/c**2,
)
ringdown = BlackHoleRingdown(
M=60.,
amplitude0 = 1.,
omega=0.03,
tau=15,
ax=ax,
name='BH',
BH_scale = G/c**2
)
def init():
ax.set_xlim([-1000,1000])
ax.set_ylim([-1000,1000])
return bbh.setup_artists()
def animate(i):
if i == binary.nframes:
for name,artist in bbh.artists.items():
artist.remove()
ringdown.setup_artists()
if i > binary.nframes:
return ringdown.get_artists(i-binary.nframes)
if i < binary.nframes:
return bbh.get_artists(i)
fps = 30
anim = animation.FuncAnimation(fig,animate,init_func=init,frames = binary.nframes+60)
anim.save('inspiral_ringdown.mp4',fps=fps,extra_args=['-vcodec', 'libx264'])
#!/usr/bin/env python
import numpy as np
from twirl.binary import InspiralingBinary
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.patches import Circle
import sys
plt.style.use('dark_background')
fig,ax = plt.subplots()
fps = 30.
omega0 = 20.
m1 = 30.,
m2 = 30.,
binary = InspiralingBinary(
m1 = 30.,
m2 = 30.,
alpha = 45.,
beta = 20.,
gamma = 80.,
omega0 = omega0
)
print('tc:',binary.tc)
print('omegamax',binary.omegamax)
binary.inspiral()
print('t:',binary.t)
pos1 = binary.pos1_projected
pos2 = binary.pos2_projected
print('pos shape:',pos1.shape)
x1s = pos1[:,1]
y1s = pos1[:,2]
x2s = pos2[:,1]
y2s = pos2[:,2]
artist_dict = {}
def init():
artist_dict['m1'] = ax.add_artist(
Circle((x1s[0],y1s[0]),
radius=100.,
edgecolor='white',
facecolor='black'
)
)
artist_dict['m2'] = ax.add_artist(
Circle((x2s[0],y2s[0]),
radius=100.,
edgecolor='white',
facecolor='black'
)
)
ax.set_xlim([-2000,2000])
ax.set_ylim([-2000,2000])
return artist_dict
def animate(i):
artist_dict['m1'].set_center((x1s[i],y1s[i]))
artist_dict['m2'].set_center((x2s[i],y2s[i]))
return artist_dict
anim = animation.FuncAnimation(fig,animate,init_func=init,frames = len(x1s))
anim.save('inspiral.mp4',fps=fps,extra_args=['-vcodec', 'libx264'])
......@@ -2,6 +2,7 @@
import numpy as np
from twirl.binary import Binary
from twirl.artists import BinaryBlackHole
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.patches import Circle
......@@ -73,7 +74,7 @@ def main():
) for event in events
}
binary_dict = {} # dictionary for all Binary objects
binary_dict = {} # dictionary for all BBH artist objects
# loop over all events
for event in events:
print('evolving ',event)
......@@ -95,42 +96,24 @@ def main():
n_frames = int(length*fps)
binary.evolve(n_frames)
# write out evolution to dictionary
binary_dict[event] = binary
for ei,event in enumerate(events):
fig,ax = plt.subplots()
ax.set_axis_off()
artist_dict = {} # dictionary for matplotlib artists
bbh = BinaryBlackHole(
binary = binary_dict[event],
ax = ax,
name=event,
BH_scale = 1./200
)
def init():
z1,x1,y1 = binary_dict[event].pos1_projected[0,:]
z2,x2,y2 = binary_dict[event].pos2_projected[0,:]
# create artists for both stars
artist_dict['%s_m1'%event] = ax.add_artist(
Circle((x1,y1),
radius=binary_dict[event].m1/200,
edgecolor='white',
facecolor='black'
)
)
artist_dict['%s_m2'%event] = ax.add_artist(
Circle((x2,y2),
radius=binary_dict[event].m2/200,
edgecolor='white',
facecolor='black'
)
)
# make sure star that is closer is shown on top of the other
zorder_1 = int(z1>z2)
zorder_2 = ~zorder_1
artist_dict['%s_m1'%event].set_zorder(zorder_1)
artist_dict['%s_m2'%event].set_zorder(zorder_2)
artist_dict.update(bbh.setup_artists())
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.set_title(event)
......@@ -138,23 +121,7 @@ def main():
return artist_dict
def animate(i):
# update positions of stars
z1,x1,y1 = binary_dict[event].pos1_projected[i,:]
z2,x2,y2 = binary_dict[event].pos2_projected[i,:]
artist_dict['%s_m1'%event].set_center((x1,y1))
artist_dict['%s_m2'%event].set_center((x2,y2))
# make sure star that is closer is shown on top of the other
if z1>z2:
zorder_1 = 2
zorder_2 = 1
else:
zorder_1 = 1
zorder_2 = 2
artist_dict['%s_m1'%event].set_zorder(zorder_1)
artist_dict['%s_m2'%event].set_zorder(zorder_2)
artist_dict.update(bbh.get_artists(i))
return artist_dict
anim = animation.FuncAnimation(fig,animate,init_func=init,frames=n_frames)
......
......@@ -13,13 +13,14 @@ setup(
scripts = [
'bin/animate_binaries_from_file',
'bin/make_event_gifs',
'bin/inspiral_test',
'bin/inspiral_ringdown',
],
packages = [
'twirl',
],
py_modules = [
'twirl.binary',
'twirl.artists',
],
data_files = [],
requires = [])
......
import numpy as np
import twirl.binary
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse,Circle
class BinaryBlackHole:
"""
A class to define the matplotlib artists
for a binary black hole.
"""
def __init__(self,
binary,
ax,
name,
BH_scale = 1,
):
"""
Parameters
----------
binary: Binary
An evolved Binary object
ax: axis object
The matplotlib axis to plot the artists on
name: str
unique name of the binary for identification
BH_scale: float
scale of BH on image WRT BH mass in solar masses
Returns
-------
None
"""
self.binary = binary
self.ax = ax
self.name = name
self.BH_scale = BH_scale
self.edgecolor='white',
self.facecolor='black',
self.artists = {}
def setup_artists(self):
z1,x1,y1 = self.binary.pos1_projected[0,:]
z2,x2,y2 = self.binary.pos2_projected[0,:]
self.artists['{}_m1'.format(self.name)] = self.ax.add_artist(
Circle((x1,y1), radius=self.BH_scale*self.binary.m1,
edgecolor='white',
facecolor='black'
)
)
self.artists['{}_m2'.format(self.name)] = self.ax.add_artist(
Circle((x2,y2), radius=self.BH_scale*self.binary.m2,
edgecolor='white',
facecolor='black'
)
)
# make sure star that is closer is shown on top of the other
zorder_1 = int(z1>z2)
zorder_2 = ~zorder_1
self.artists['{}_m1'.format(self.name)].set_zorder(zorder_1)
self.artists['{}_m2'.format(self.name)].set_zorder(zorder_2)
return self.artists
def get_artists(self,i):
z1,x1,y1 = self.binary.pos1_projected[i,:]
z2,x2,y2 = self.binary.pos2_projected[i,:]
self.artists['{}_m1'.format(self.name)].set_center((x1,y1))
self.artists['{}_m2'.format(self.name)].set_center((x2,y2))
# make sure star that is closer is shown on top of the other
zorder_1 = int(z1>z2)
zorder_2 = ~zorder_1
self.artists['{}_m1'.format(self.name)].set_zorder(zorder_1)
self.artists['{}_m2'.format(self.name)].set_zorder(zorder_2)
return self.artists
class BlackHoleRingdown:
def __init__(self,
M,
amplitude0,
omega,
tau,
ax,
name,
BH_scale = 1
):
self.M = M
self.tau = tau
self._amplitude0 = amplitude0
self.omega = omega
self.ax = ax
self.name = name
self.artists = {}
self.BH_scale = BH_scale
def amplitude(self,i):
return self._amplitude0 * np.exp(-i/self.tau) * np.cos(2.*2.*np.pi*i/self.tau)
def get_height_width(self,i):
return 1 + self.amplitude(i), 1 - self.amplitude(i)
def setup_artists(self):
height,width = self.get_height_width(0)
self.artists['{}_ringdown'.format(self.name)] = self.ax.add_artist(
Ellipse((0,0),
self.BH_scale*self.M*height,self.BH_scale*self.M*width,
edgecolor='white',facecolor='black'
)
)
return self.artists
def get_artists(self,i):
height,width = self.get_height_width(i)
self.artists['{}_ringdown'.format(self.name)].height = self.BH_scale*self.M*height
self.artists['{}_ringdown'.format(self.name)].width = self.BH_scale*self.M*width
return self.artists
......@@ -171,6 +171,10 @@ class InspiralingBinary(Binary):
self._pos1_projected = [pos1_proj]
self._pos2_projected = [pos2_proj]
@property
def nframes(self):
return len(self.t)
@property
def omega(self):
return (0.5)*((((8.*np.pi)**(8./3))/5.)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment