#-----------------------------------------------------------------------
# Animation de l'évolution d'une somme de deux OPPH
#-----------------------------------------------------------------------

########################
##Paramètres modifiables
########################
# Vitesse de groupe
Vg = 0.4

# Modèle :
# 1 : w = a.k     (non dispersif)
# 2 : w = a.k^2   (type electron libre)
# 3 : w = a.k^0.5 (type vagues, défilement dans l'autre sens)
# 4 : w = a.k^3   (non physique, mais plus visible)
type = 3

# Contrôle des paramètres
assert(0.01 <= Vg <= 2.0)
assert(type in (1, 2, 3, 4))

#########################
##Bibliothèques utilisées
#########################
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as ani

#######################
##Definitions et tracés
#######################

# Foncton définissant la vitesse de phase en fonction du
# nombre d'onde sigma

@np.vectorize
def Vp(sig) :
    # Pas d'étalement :
    if type == 1 :
        return Vg

    # a.k^2
    if type == 2 :
        return Vg/2.0

    # a.k^0.5
    if type == 3 :
        if sig==0.0 :
            return 0.0
        return Vg*2.0

    # a.k^3
    if type == 4 :
        return Vg/3.0

# Détection utilisation hors Pyzo
if '__iep__' not in globals() :
    matplotlib.interactive(False)

# Abscisses
X = np.linspace(0, 160, 4096)

# Calcul de la forme de l'onde à un instant t
def Onde(t) :
    return np.cos(1*t-X/Vp(1))*np.cos(0.05*t-0.05*X/Vg)

# Calcul de la forme de l'enveloppe à un instant t
def Env(t) :
    return np.cos(0.05*t-0.05*X/Vg)

# Premier tracé pour préparer l'animation
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ond, = plt.plot(X, Onde(0), 'b')
envp, = plt.plot(X, Env(0), 'r')
envm, = plt.plot(X, -1*Env(0),'r')

lines = [ond, envp, envm]

# Décoration
if type == 1 :
    ax.set_title(r"Non dispersif : $\omega = \alpha k$")
elif type == 2 :
    ax.set_title(r"Type électron : $\omega = \alpha k^2$")
elif type == 3 :
    ax.set_title(r"Type vagues : $\omega = \alpha \sqrt{k}$")
elif type == 4 :
    ax.set_title(r"Autre : $\omega = \alpha k^3$")

# Animation
def SizeChanged(ax, old=[]) :
    current = [ ax.bbox.width, ax.bbox.height ]
    if old != current :
        old[:] = current
        return True
    return False

def Update(t) :
    ond.set_data(X, Onde(t))
    envp.set_data(X, Env(t))
    envm.set_data(X, -1*Env(t))
    if SizeChanged(plt.gca()) :
        plt.gcf().canvas.draw()
    return lines

def Init() :
    ond.set_data(X, np.ma.array(X, mask=True))
    envp.set_data(X, np.ma.array(X, mask=True))
    envm.set_data(X, np.ma.array(X, mask=True))
    return lines

anim = ani.FuncAnimation(fig, Update,
                         frames=np.linspace(int(10/Vg), int(130/Vg), int(241/Vg)),
                         interval = 50,
                         blit=True, init_func=Init)

# Détection utilisation hors Pyzo
if '__iep__' not in globals() :
    plt.show()
