python4oceanographers

Turning ripples into waves

Wave animations

I started to update several of my old MatlabTM scripts for the Waves & Tides course I'm teaching. They are a compilation of courses I took in the past, and/or scripts I found online. Most of them are not that interesting, unless you are taking my course :), but others produce some nice animations that I decided to post here.

These animation are quite simple, all we'll need are numpy, matplotlib and the the very handy JSAnimation module to modify the standard IPython display.

In [2]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from JSAnimation import IPython_display

The first animation is a partial standing wave. Sometimes, wave-wave interaction (like a reflected wave in a steep slope beach), can create partial standing waves. Here we plot the wave envelope (dashed line) from a reflected wave with an amplitude that is half on the incident wave. Note that if we use the same amplitude the interaction will result in a full standing wave.

In [3]:
T = 20.
w = 2 * np.pi / T
ai = 1.  # Incident.
ar = 0.5  # Reflected.
d = 0
k = w**2 / 9.81  # Deep water wave.

t = np.arange(0, 100.5, 0.5)

def basic_animation(frames=100, interval=30):
    fig = plt.figure()
    ax = plt.axes(xlim=(0, 1000), ylim=(-2, 2))
    
    # Animated.
    line, = ax.plot([], [], 'b', lw=2)
    text = ax.text(1, 2.05, '')

    # Non-animated.
    x = np.arange(0, 1001)
    A = (ai**2 + ar**2 + 2*ai * ar * np.cos(2 * k * x + d))**(0.5)
    A1 = ai * np.cos(k * x) + ar * np.cos(k * x + d)
    A2 = ai * np.sin(k * x) - ar * np.sin(k * x + d)
    gamma = np.arctan2(A2, A1)
    ax.set_xlabel('Distance [m]')
    ax.set_ylabel('Surface Elevation [m]')
    ax.set_title('a_i = %s, a_r = %s, T = %s\nDeep Water Wave' % (ai, ar, T))
    ax.plot(x,  A, 'k:')
    ax.plot(x, -A, 'k:')
    ax.plot(x, [0]*len(x), 'k')
    line.set_data([], [])
    
    def init():
        return line, text

    def animate(i):
        text.set_text('Time [s] = %s' % t[k])
        y = A * np.cos(w * t[i] - gamma)
        line.set_data(x, y)
        return line, text

    return animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=frames, interval=interval)

basic_animation(frames=len(t))
Out[3]:


Once Loop Reflect