Skip to content

Commit

Permalink
Use to_html5_video to display animations; Change function names
Browse files Browse the repository at this point in the history
  • Loading branch information
narahahn committed Mar 11, 2019
1 parent 7966a2a commit 1a139fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 53 deletions.
67 changes: 26 additions & 41 deletions doc/examples/animations-pulsating-sphere.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"import sfs\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import animations_pulsating_sphere as ani\n",
"from IPython.display import display, Image, HTML"
"import animations_pulsating_sphere as animation\n",
"from IPython.display import Image, HTML"
]
},
{
Expand All @@ -34,7 +34,7 @@
"omega = 2 * np.pi * f # angular frequency\n",
"\n",
"# Axis limits\n",
"figsize = (8, 8)\n",
"figsize = (6, 6)\n",
"xmin, xmax = -1, 1\n",
"ymin, ymax = -1, 1\n",
"\n",
Expand All @@ -56,20 +56,17 @@
"outputs": [],
"source": [
"grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.025)\n",
"ani_obj = ani.animation_particle_displacement(\n",
" omega, center, radius, amplitude, grid, frames, figsize, c='Gray')\n",
"ani_obj.save('pulsating_sphere_displacement_square_grid.gif',\n",
" dpi=80, writer='imagemagick')\n",
"ani = animation.particle_displacement(\n",
" omega, center, radius, amplitude, grid, frames, figsize, c='Gray')\n",
"plt.close()\n",
"Image('pulsating_sphere_displacement_square_grid.gif')"
"HTML(ani.to_html5_video())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can play with the animation more interactively by using `.to_jshtml`.\n",
"The playback speed and direction can be changed."
"You can play with the animation more interactively by using `.to_jshtml`."
]
},
{
Expand All @@ -78,7 +75,7 @@
"metadata": {},
"outputs": [],
"source": [
"display(HTML(ani_obj.to_jshtml(default_mode='loop')))"
"HTML(ani.to_jshtml())"
]
},
{
Expand Down Expand Up @@ -125,12 +122,10 @@
"outputs": [],
"source": [
"grid = hex_grid([xmin, xmax], [ymin, ymax], 0.0125, 'vertical')\n",
"ani_obj = ani.animation_particle_displacement(\n",
" omega, center, radius, amplitude, grid, frames, figsize, c='Gray')\n",
"ani_obj.save('pulsating_sphere_displacement_hexagonal_grid.gif',\n",
" dpi=80, writer='imagemagick')\n",
"ani = animation.particle_displacement(\n",
" omega, center, radius, amplitude, grid, frames, figsize, c='Gray')\n",
"plt.close()\n",
"Image('pulsating_sphere_displacement_hexagonal_grid.gif')"
"HTML(ani.to_html5_video())"
]
},
{
Expand All @@ -151,12 +146,10 @@
"source": [
"grid = [np.random.uniform(xmin, xmax, 4000),\n",
" np.random.uniform(ymin, ymax, 4000), 0]\n",
"ani_obj = ani.animation_particle_displacement(\n",
" omega, center, radius, amplitude, grid, frames, figsize, c='Gray')\n",
"ani_obj.save('pulsating_sphere_displacement_random_grid.gif',\n",
" dpi=80, writer='imagemagick')\n",
"ani = animation.particle_displacement(\n",
" omega, center, radius, amplitude, grid, frames, figsize, c='Gray')\n",
"plt.close()\n",
"Image('pulsating_sphere_displacement_random_grid.gif')"
"HTML(ani.to_html5_video())"
]
},
{
Expand All @@ -174,12 +167,10 @@
"source": [
"amplitude = 1e-3\n",
"grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.04)\n",
"ani_obj = ani.animation_particle_velocity(\n",
" omega, center, radius, amplitude, grid, frames, figsize)\n",
"ani_obj.save('pulsating_sphere_velocity.gif',\n",
" dpi=80, writer='imagemagick')\n",
"ani = animation.particle_velocity(\n",
" omega, center, radius, amplitude, grid, frames, figsize)\n",
"plt.close()\n",
"Image('pulsating_sphere_velocity_small_amplitude.gif')"
"HTML(ani.to_html5_video())"
]
},
{
Expand Down Expand Up @@ -221,13 +212,11 @@
"max_pressure = omega * impedance_pw * amplitude\n",
"\n",
"grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.005)\n",
"movie = ani.animation_sound_pressure(\n",
"ani = animation.sound_pressure(\n",
" omega, center, radius, amplitude, grid, frames, pulsate=True,\n",
" figsize=figsize, vmin=-max_pressure, vmax=max_pressure)\n",
"movie.save('pulsating_sphere_pressure.gif',\n",
" dpi=80, writer='imagemagick')\n",
"plt.close()\n",
"Image('pulsating_sphere_pressure.gif')"
"HTML(ani.to_html5_video())"
]
},
{
Expand Down Expand Up @@ -265,13 +254,11 @@
"max_pressure = omega * impedance_pw * amplitude\n",
"\n",
"grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.005)\n",
"movie = ani.animation_sound_pressure(\n",
"ani = animation.sound_pressure(\n",
" omega, center, radius, amplitude, grid, frames, pulsate=False,\n",
" figsize=figsize, vmin=-max_pressure, vmax=max_pressure)\n",
"movie.save('pulsating_sphere_pressure_small_amplitude.gif',\n",
" dpi=80, writer='imagemagick')\n",
"plt.close()\n",
"Image('pulsating_sphere_pressure_small_amplitude.gif')"
"HTML(ani.to_html5_video())"
]
},
{
Expand All @@ -288,8 +275,8 @@
"outputs": [],
"source": [
"L = 10 * amplitude\n",
"xmin, xmax = radius - L, radius + L\n",
"ymin, ymax = -L, L"
"xmin_zoom, xmax_zoom = radius - L, radius + L\n",
"ymin_zoom, ymax_zoom = -L, L"
]
},
{
Expand All @@ -298,14 +285,12 @@
"metadata": {},
"outputs": [],
"source": [
"grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=L / 100)\n",
"ani_obj = ani.animation_sound_pressure(\n",
"grid = sfs.util.xyz_grid([xmin_zoom, xmax_zoom], [ymin_zoom, ymax_zoom], 0, spacing=L / 100)\n",
"ani = animation.sound_pressure(\n",
" omega, center, radius, amplitude, grid, frames, pulsate=True,\n",
" figsize=figsize, vmin=-max_pressure, vmax=max_pressure)\n",
"ani_obj.save('pulsating_sphere_pressure_zoom.gif',\n",
" dpi=80, writer='imagemagick')\n",
"plt.close()\n",
"Image('pulsating_sphere_pressure_zoom.gif')"
"HTML(ani.to_html5_video())"
]
},
{
Expand Down
22 changes: 10 additions & 12 deletions doc/examples/animations_pulsating_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from matplotlib import animation


def animation_particle_displacement(omega, center, radius, amplitude, grid,
frames, figsize=(8, 8), interval=80,
blit=True, **kwargs):
def particle_displacement(omega, center, radius, amplitude, grid, frames,
figsize=(8, 8), interval=80, blit=True, **kwargs):
"""Generate sound particle animation."""

velocity = sfs.mono.source.pulsating_sphere_velocity(
Expand All @@ -31,9 +30,8 @@ def update_frame_displacement(i):
interval=interval, blit=blit)


def animation_particle_velocity(omega, center, radius, amplitude, grid, frames,
figsize=(8, 8), interval=80, blit=True,
**kwargs):
def particle_velocity(omega, center, radius, amplitude, grid, frames,
figsize=(8, 8), interval=80, blit=True, **kwargs):
"""Generate particle velocity animation."""

velocity = sfs.mono.source.pulsating_sphere_velocity(
Expand All @@ -54,9 +52,9 @@ def update_frame_velocity(i):
fig, update_frame_velocity, frames, interval=interval, blit=True)


def animation_sound_pressure(omega, center, radius, amplitude, grid, frames,
pulsate=False, figsize=(8, 8), interval=80,
blit=True, **kwargs):
def sound_pressure(omega, center, radius, amplitude, grid, frames,
pulsate=False, figsize=(8, 8), interval=80, blit=True,
**kwargs):
"""Generate sound pressure animation."""

pressure = sfs.mono.source.pulsating_sphere(
Expand Down Expand Up @@ -97,14 +95,14 @@ def update_frame_pressure(i):
# Particle displacement
amplitude = 5e-2 # amplitude of the surface displacement
grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.025)
ani = animation_particle_displacement(
ani = particle_displacement(
omega, center, radius, amplitude, grid, frames, c='Gray')
ani.save('pulsating_sphere_displacement.gif', dpi=80, writer='imagemagick')

# Particle velocity
amplitude = 1e-3 # amplitude of the surface displacement
grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.04)
ani = animation_particle_velocity(
ani = particle_velocity(
omega, center, radius, amplitude, grid, frames)
ani.save('pulsating_sphere_velocity.gif', dpi=80, writer='imagemagick')

Expand All @@ -113,7 +111,7 @@ def update_frame_pressure(i):
impedance_pw = sfs.default.rho0 * sfs.default.c
max_pressure = omega * impedance_pw * amplitude
grid = sfs.util.xyz_grid([xmin, xmax], [ymin, ymax], 0, spacing=0.005)
ani = animation_sound_pressure(
ani = sound_pressure(
omega, center, radius, amplitude, grid, frames, pulsate=True,
colorbar=True, vmin=-max_pressure, vmax=max_pressure)
ani.save('pulsating_sphere_pressure.gif', dpi=80, writer='imagemagick')

0 comments on commit 1a139fc

Please sign in to comment.