Animations

All the work done below serves to animate the postions of the bodies in my system for each case I consider. All the actual animations can be viewed in the visualization notebooks for each case.


In [8]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from moviepy.video.io.bindings import mplfig_to_npimage
import moviepy.editor as mpy

Base Question


In [9]:
f = open('base_question_data.npz','r')
r = np.load('base_question_data.npz')
sol_base = r['arr_0']
ic_base = r['arr_1']
f.close()

Creating animation with main galaxy as the center:


In [10]:
duration = 10.0

fig_mpl, ax = plt.subplots(1,figsize=(10,10), facecolor='white')

x = np.zeros(122)
y = np.zeros(122)
for i in range(0,int(len(ic_base)/4)):
    x[i] = sol_base[0][4*i]
    y[i] = sol_base[0][4*i+1]
    

plt.sca(ax)

plt.ylim(-50,50)
plt.xlim(-50,50)

scat = ax.scatter(x, y);

def make_frame_mpl(t):
    newx = np.zeros(122)
    newy = np.zeros(122)
    for i in range(0,121):
        newx[i] = sol_base[10*t][4*i]
        newy[i] = sol_base[10*t][4*i+1]
    scat.set_offsets(np.transpose(np.vstack([newx, newy])))
    return mplfig_to_npimage(fig_mpl)

animation_base = mpy.VideoClip(make_frame_mpl, duration=duration)


Writing animation to disk:


In [7]:
animation_base.write_videofile("base_animation.mp4", fps=24)


[MoviePy] >>>> Building video base_animation.mp4
[MoviePy] Writing video base_animation.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: base_animation.mp4 

Creating animation centered around center of mass:


In [23]:
duration = 10.0

fig_mpl, ax = plt.subplots(1,figsize=(10,10), facecolor='white')

x_com = np.zeros(122)
y_com = np.zeros(122)
S = 1e11
M = 1e11
for i in range(0,int(len(ic_base)/4)):
    cm_x_base = (S*sol_base[0][0])/(M+S)
    cm_y_base = (S*sol_base[0][1])/(M+S)
    x_com[i] = sol_base[0][4*i]-cm_x
    y_com[i] = sol_base[0][4*i+1]-cm_y
    x_com[121] = 0-cm_x_base
    y_com[121] = 0-cm_y_base

plt.sca(ax)

plt.ylim(-100,100)
plt.xlim(-100,100)

scat = ax.scatter(x_com, y_com);

def make_frame_mpl_com(t):
    cm_x_base = (S*sol_base[10*t][0])/(M+S)
    cm_y_base = (S*sol_base[10*t][1])/(M+S)
    newx_com = np.zeros(122)
    newy_com = np.zeros(122)
    for i in range(0,121):
        newx_com[i] = sol_base[10*t][4*i]-cm_x_base
        newy_com[i] = sol_base[10*t][4*i+1]-cm_y_base
        newx_com[121] = 0-cm_x_base
        newy_com[121] = 0-cm_y_base
    scat.set_offsets(np.transpose(np.vstack([newx_com, newy_com])))
    return mplfig_to_npimage(fig_mpl)

animation_base_com = mpy.VideoClip(make_frame_mpl_com, duration=duration)



In [24]:
animation_base_com.write_videofile("base_animation_com.mp4", fps=24)


[MoviePy] >>>> Building video base_animation_com.mp4
[MoviePy] Writing video base_animation_com.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: base_animation_com.mp4 

Additional Question 1


In [26]:
f = open('additional_1_data.npz','r')
r = np.load('additional_1_data.npz')
sol_add1 = r['arr_0']
ic_add1 = r['arr_1']
f.close()

Creating animation centered at main galaxy:


In [27]:
duration = 10.0

fig_mpl, ax = plt.subplots(1,figsize=(10,10), facecolor='white')

x_1 = np.zeros(122)
y_1 = np.zeros(122)
for i in range(0,int(len(ic_add1)/4)):
    x_1[i] = sol_add1[0][4*i]
    y_1[i] = sol_add1[0][4*i+1]
    

plt.sca(ax)

plt.ylim(-50,50)
plt.xlim(-50,50)

scat = ax.scatter(x_1, y_1);

def make_frame_mpl_1(t):
    newx_1 = np.zeros(122)
    newy_1 = np.zeros(122)
    for i in range(0,121):
        newx_1[i] = sol_add1[10*t][4*i]
        newy_1[i] = sol_add1[10*t][4*i+1]
    scat.set_offsets(np.transpose(np.vstack([newx_1, newy_1])))
    return mplfig_to_npimage(fig_mpl)

animation_add1 = mpy.VideoClip(make_frame_mpl_1, duration=duration)



In [28]:
animation_add1.write_videofile("additional_1_animation.mp4", fps=24)


[MoviePy] >>>> Building video additional_1_animation.mp4
[MoviePy] Writing video additional_1_animation.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: additional_1_animation.mp4 

Creating animation around center of mass:


In [29]:
duration = 10.0

fig_mpl, ax = plt.subplots(1,figsize=(10,10), facecolor='white')

x_com_1 = np.zeros(122)
y_com_1 = np.zeros(122)
S_add1 = (1e11)/3
M_add1 = 1e11
for i in range(0,int(len(ic_add1)/4)):
    cm_x_1 = (S_add1*sol_add1[0][0])/(M_add1+S_add1)
    cm_y_1 = (S_add1*sol_add1[0][1])/(M_add1+S_add1)
    x_com_1[i] = sol_add1[0][4*i]-cm_x_1
    y_com_1[i] = sol_add1[0][4*i+1]-cm_y_1
    x_com_1[121] = 0-cm_x_1
    y_com_1[121] = 0-cm_y_1

plt.sca(ax)

plt.ylim(-100,100)
plt.xlim(-100,100)

scat = ax.scatter(x_com_1, y_com_1);

def make_frame_mpl_com_1(t):
    cm_x_1 = (S_add1*sol_add1[10*t][0])/(M_add1+S_add1)
    cm_y_1 = (S_add1*sol_add1[10*t][1])/(M_add1+S_add1)
    newx_com_1 = np.zeros(122)
    newy_com_1 = np.zeros(122)
    for i in range(0,121):
        newx_com_1[i] = sol_add1[10*t][4*i]-cm_x_1
        newy_com_1[i] = sol_add1[10*t][4*i+1]-cm_y_1
        newx_com_1[121] = 0-cm_x_1
        newy_com_1[121] = 0-cm_y_1
    scat.set_offsets(np.transpose(np.vstack([newx_com_1, newy_com_1])))
    return mplfig_to_npimage(fig_mpl)

animation_1_com = mpy.VideoClip(make_frame_mpl_com_1, duration=duration)



In [30]:
animation_1_com.write_videofile("additional_1_animation_com.mp4", fps=24)


[MoviePy] >>>> Building video additional_1_animation_com.mp4
[MoviePy] Writing video additional_1_animation_com.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: additional_1_animation_com.mp4 

Additional Question 2


In [32]:
f = open('additional_2_data.npz','r')
r = np.load('additional_2_data.npz')
sol_add2 = r['arr_0']
ic_add2 = r['arr_1']
f.close()

Creating animation centered around main galaxy:


In [33]:
duration = 10.0

fig_mpl, ax = plt.subplots(1,figsize=(10,10), facecolor='white')

x_2 = np.zeros(122)
y_2 = np.zeros(122)
for i in range(0,int(len(ic_add2)/4)):
    x_2[i] = sol_add2[0][4*i]
    y_2[i] = sol_add2[0][4*i+1]
    

plt.sca(ax)

plt.ylim(-50,50)
plt.xlim(-50,50)

scat = ax.scatter(x_2, y_2);

def make_frame_mpl_2(t):
    newx_2 = np.zeros(122)
    newy_2 = np.zeros(122)
    for i in range(0,121):
        newx_2[i] = sol_add2[10*t][4*i]
        newy_2[i] = sol_add2[10*t][4*i+1]
    scat.set_offsets(np.transpose(np.vstack([newx_2, newy_2])))
    return mplfig_to_npimage(fig_mpl)

animation_add2 = mpy.VideoClip(make_frame_mpl_2, duration=duration)



In [34]:
animation_add2.write_videofile("additional_2_animation.mp4", fps=24)


[MoviePy] >>>> Building video additional_2_animation.mp4
[MoviePy] Writing video additional_2_animation.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: additional_2_animation.mp4 

Center of mass animation:


In [35]:
duration = 10.0

fig_mpl, ax = plt.subplots(1,figsize=(10,10), facecolor='white')

x_com_2 = np.zeros(122)
y_com_2 = np.zeros(122)
S_add2 = 1e11
M_add2 = (1e11)/3
for i in range(0,int(len(ic_add2)/4)):
    cm_x_2 = (S_add2*sol_add2[0][0])/(M_add2+S_add2)
    cm_y_2 = (S_add2*sol_add2[0][1])/(M_add2+S_add2)
    x_com_2[i] = sol_add2[0][4*i]-cm_x_2
    y_com_2[i] = sol_add2[0][4*i+1]-cm_y_2
    x_com_2[121] = 0-cm_x_2
    y_com_2[121] = 0-cm_y_2

plt.sca(ax)

plt.ylim(-100,100)
plt.xlim(-100,100)

scat = ax.scatter(x_com_2, y_com_2);

def make_frame_mpl_com_2(t):
    cm_x_2 = (S_add2*sol_add2[10*t][0])/(M_add2+S_add2)
    cm_y_2 = (S_add2*sol_add2[10*t][1])/(M_add2+S_add2)
    newx_com_2 = np.zeros(122)
    newy_com_2 = np.zeros(122)
    for i in range(0,121):
        newx_com_2[i] = sol_add2[10*t][4*i]-cm_x_2
        newy_com_2[i] = sol_add2[10*t][4*i+1]-cm_y_2
        newx_com_2[121] = 0-cm_x_2
        newy_com_2[121] = 0-cm_y_2
    scat.set_offsets(np.transpose(np.vstack([newx_com_2, newy_com_2])))
    return mplfig_to_npimage(fig_mpl)

animation_2_com = mpy.VideoClip(make_frame_mpl_com_2, duration=duration)



In [36]:
animation_2_com.write_videofile("additional_2_animation_com.mp4", fps=24)


[MoviePy] >>>> Building video additional_2_animation_com.mp4
[MoviePy] Writing video additional_2_animation_com.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: additional_2_animation_com.mp4 


In [ ]: