def plot_frames(iom, blockid=0, view=None, imgsize=(12,9)):
    """Plot the phase of a wavefunction for a series of timesteps.
    :param iom: An ``IOManager`` instance providing the simulation data.
    :param view: The aspect ratio.
    """
    parameters = iom.load_parameters()

    grid = iom.load_grid(blockid="global")
    timegrid = iom.load_wavefunction_timegrid(blockid=blockid)

    # Precompute eigenvectors for efficiency
    Potential = PotentialFactory().create_potential(parameters)
    eigenvectors = Potential.evaluate_eigenvectors_at(grid)

    for step in timegrid:
        print(" Plotting frame of timestep # " + str(step))

        wave = iom.load_wavefunction(blockid=blockid, timestep=step)
        values = [ wave[j,...] for j in xrange(parameters["ncomponents"]) ]

        # Transform the values to the eigenbasis
        # TODO: improve this:
        if parameters["algorithm"] == "fourier":
            ve = Potential.project_to_eigen(grid, values, eigenvectors)
        else:
            ve = values

        fig = figure(figsize=imgsize)

        for index, component in enumerate(ve):
            ax = fig.add_subplot(parameters["ncomponents"],1,index+1)
            ax.ticklabel_format(style="sci", scilimits=(0,0), axis="y")

            # Plot the wavefunction
            ax.plot(grid, component*conj(component), color="gray")
            ax.set_ylabel(r"$\langle \varphi_"+str(index)+r"| \varphi_"+str(index)+r"\rangle$")
            ax.set_xlabel(r"$x$")

            # Compute the phase from the wavefunction restricted to "important" regions
            restr_grid = grid[component*conj(component) > 10e-8]
            restr_comp = component[component*conj(component) > 10e-8]

            # Plot the phase
            ax.plot(restr_grid, angle(restr_comp), "-", color="green")
            ax.plot(restr_grid, ComplexMath.continuate(angle(restr_comp)), ".", color="green")

            # Set the aspect window
            if view is not None:
                ax.set_xlim(view[:2])
                #ax.set_ylim(view[2:])

        fig.suptitle(r"$\arg \Psi$ at time $"+str(step*parameters["dt"])+r"$")
        fig.savefig("wavefunction_phase_block"+str(blockid)+"_"+ (7-len(str(step)))*"0"+str(step) +GD.output_format)
        close(fig)

    print(" Plotting frames finished")
def plot_parameters(gid, data):
    print("Plotting the wavepacket parameters of group '"+str(gid)+"'")

    if len(data) == 0:
        return

    # Plot the time evolution of the parameters P, Q, S, p and q
    fig = figure(figsize=(12,12))
    ax = [ fig.add_subplot(4,2,i) for i in xrange(1,8) ]

    for datum in data:
        timegrid, PI = datum
        Phist, Qhist, Shist, phist, qhist = PI

        ax[0].plot(timegrid, real(Phist), label=r"$\Re P$")
        ax[0].grid(True)
        ax[0].set_title(r"$\Re P$")

        ax[1].plot(timegrid, imag(Phist), label=r"$\Im P$")
        ax[1].grid(True)
        ax[1].set_title(r"$\Im P$")

        ax[2].plot(timegrid, real(Qhist), label=r"$\Re Q$")
        ax[2].grid(True)
        ax[2].set_title(r"$\Re Q$")

        ax[3].plot(timegrid, imag(Qhist), label=r"$\Im Q$")
        ax[3].grid(True)
        ax[3].set_title(r"$\Im Q$")

        ax[4].plot(timegrid, real(qhist), label=r"$q$")
        ax[4].grid(True)
        ax[4].set_title(r"$q$")

        ax[5].plot(timegrid, real(phist), label=r"$p$")
        ax[5].grid(True)
        ax[5].set_title(r"$p$")

        ax[6].plot(timegrid, real(Shist), label=r"$S$")
        ax[6].grid(True)
        ax[6].set_title(r"$S$")

    fig.suptitle("Wavepacket parameters")
    fig.savefig("wavepacket_parameters_group"+str(gid)+GD.output_format)
    close(fig)


    # Plot the time evolution of the parameters P, Q, S, p and q
    # This time plot abs/angle instead of real/imag
    fig = figure(figsize=(12,12))
    ax = [ fig.add_subplot(4,2,i) for i in xrange(1,8) ]

    for datum in data:
        timegrid, PI = datum
        Phist, Qhist, Shist, phist, qhist = PI

        ax[0].plot(timegrid, abs(Phist), label=r"$|P|$")
        ax[0].grid(True)
        ax[0].set_title(r"$|P|$")

        ax[1].plot(timegrid, ComplexMath.cont_angle(Phist), label=r"$\arg P$")
        ax[1].grid(True)
        ax[1].set_title(r"$\arg P$")

        ax[2].plot(timegrid, abs(Qhist), label=r"$|Q|$")
        ax[2].grid(True)
        ax[2].set_title(r"$|Q|$")

        ax[3].plot(timegrid, ComplexMath.cont_angle(Qhist), label=r"$\arg Q$")
        ax[3].grid(True)
        ax[3].set_title(r"$\arg Q$")

        ax[4].plot(timegrid, real(qhist), label=r"$q$")
        ax[4].grid(True)
        ax[4].set_title(r"$q$")

        ax[5].plot(timegrid, real(phist), label=r"$p$")
        ax[5].grid(True)
        ax[5].set_title(r"$p$")

        ax[6].plot(timegrid, real(Shist), label=r"$S$")
        ax[6].grid(True)
        ax[6].set_title(r"$S$")

    fig.suptitle("Wavepacket parameters")
    fig.savefig("wavepacket_parameters_absang_group"+str(gid)+GD.output_format)
    close(fig)
def plot_parameters(data, index=0):
    """Plot the data parameters (P, Q, S, p, q) over time.
    For each new I{index} we start a new figure.
    """
    print("Plotting the parameters of data block '"+str(index)+"'")

    timegrid, Phist, Qhist, Shist, phist, qhist = data

    # Plot the time evolution of the parameters P, Q, S, p and q
    fig = figure(figsize=(12,12))

    ax = fig.add_subplot(4,2,1)
    for item in Phist:
        ax.plot(timegrid, real(item), label=r"$\Re P$")
    ax.grid(True)
    ax.set_title(r"$\Re P$")

    ax = fig.add_subplot(4,2,2)
    for item in Phist:
        ax.plot(timegrid, imag(item), label=r"$\Im P$")
    ax.grid(True)
    ax.set_title(r"$\Im P$")

    ax = fig.add_subplot(4,2,3)
    for item in Qhist:
        ax.plot(timegrid, real(item), label=r"$\Re Q$")
    ax.grid(True)
    ax.set_title(r"$\Re Q$")

    ax = fig.add_subplot(4,2,4)
    for item in Qhist:
        ax.plot(timegrid, imag(item), label=r"$\Im Q$")
    ax.grid(True)
    ax.set_title(r"$\Im Q$")

    ax = fig.add_subplot(4,2,5)
    for item in qhist:
        ax.plot(timegrid, real(item), label=r"$q$")
    ax.grid(True)
    ax.set_title(r"$q$")

    ax = fig.add_subplot(4,2,6)
    for item in phist:
        ax.plot(timegrid, real(item), label=r"$p$")
    ax.grid(True)
    ax.set_title(r"$p$")

    ax = fig.add_subplot(4,2,7)
    for item in Shist:
        ax.plot(timegrid, real(item), label=r"$S$")
    ax.grid(True)
    ax.set_title(r"$S$")

    fig.suptitle("Wavepacket parameters")
    fig.savefig("wavepacket_parameters_block"+str(index)+GD.output_format)
    close(fig)


    # Plot the time evolution of the parameters P, Q, S, p and q
    # This time plot abs/angle instead of real/imag
    fig = figure(figsize=(12,12))

    ax = fig.add_subplot(4,2,1)
    for item in Phist:
        ax.plot(timegrid, abs(item), label=r"$|P|$")
    ax.grid(True)
    ax.set_title(r"$|P|$")

    ax = fig.add_subplot(4,2,2)
    for item in Phist:
        ax.plot(timegrid, ComplexMath.cont_angle(item), label=r"$\arg P$")
    ax.grid(True)
    ax.set_title(r"$\arg P$")

    ax = fig.add_subplot(4,2,3)
    for item in Qhist:
        ax.plot(timegrid, abs(item), label=r"$|Q|$")
    ax.grid(True)
    ax.set_title(r"$|Q|$")

    ax = fig.add_subplot(4,2,4)
    for item in Qhist:
        ax.plot(timegrid, ComplexMath.cont_angle(item), label=r"$\arg Q$")
    ax.grid(True)
    ax.set_title(r"$\arg Q$")

    ax = fig.add_subplot(4,2,5)
    for item in qhist:
        ax.plot(timegrid, real(item), label=r"$q$")
    ax.grid(True)
    ax.set_title(r"$q$")

    ax = fig.add_subplot(4,2,6)
    for item in phist:
        ax.plot(timegrid, real(item), label=r"$p$")
    ax.grid(True)
    ax.set_title(r"$p$")

    ax = fig.add_subplot(4,2,7)
    for item in Shist:
        ax.plot(timegrid, abs(item), label=r"$S$")
    ax.grid(True)
    ax.set_title(r"$S$")

    fig.suptitle("Wavepacket parameters")
    fig.savefig("wavepacket_parameters_abs_ang_block"+str(index)+GD.output_format)
    close(fig)


    # Plot the complex trajectory of the parameters P
    fig = figure()
    ax = fig.gca()
    for item in Phist:
        ax.plot(real(item), imag(item), "-o", label=r"Trajectory $P$")
    ax.grid(True)
    ax.set_title(r"Trajectory of $P$")
    fig.savefig("wavepacket_parameters_trajectoryP_block"+str(index)+GD.output_format)
    close(fig)


    # Plot the complex trajectory of the parameters Q
    fig = figure()
    ax = fig.gca()
    for item in Qhist:
        ax.plot(real(item), imag(item), "-o", label=r"Trajectory $Q$")
    ax.grid(True)
    ax.set_title(r"Trajectory of $Q$")
    fig.savefig("wavepacket_parameters_trajectoryQ_block"+str(index)+GD.output_format)
    close(fig)