Exemple #1
0
def visualise(sim):
    X = np.arange(sim.A.size).reshape(sim.A.shape) % sim.A.shape[0]
    Y = (np.arange(sim.A.size).reshape(sim.A.shape) % sim.A.shape[1]).T

    U = cos(sim.A)
    V = sin(sim.A)

    fig, ax = plt.subplots(1, 1, figsize=(15, 15))

    rects = []

    # create rectangles for vortex/antivortex determination
    for i in range(sim.V.shape[0]):
        for j in range(sim.V.shape[1]):
            rect = patches.Rectangle(xy=(i, j), height=1, width=1)
            rects.append(rect)
    rects = PatchCollection(rects)

    # Set colors for the rectangles
    col = 'RdBu'
    r_cmap = plt.get_cmap(col)
    r_cmap_r = plt.get_cmap(col + "_r") #eto kostil' =)
    rects.set_cmap(r_cmap)
    rects.set_clim(vmin=-1, vmax=1)

    rects.set_animated(True)
    rects.set_array(sim.V.flatten('F') / 2)
    ax.add_collection(rects)

    # create legend
    legend_boxes = [patches.Patch(facecolor=r_cmap(0.7), label='Antiortex'),
                    patches.Patch(facecolor=r_cmap_r(0.7), label='Vortex')]
    ax.legend(handles=legend_boxes)

    # build an initial quiver plot
    q = ax.quiver(X, Y, U, V, pivot='tail', cmap=plt.cm.get_cmap('hsv'), units='inches', scale=4)
    fig.colorbar(q, label='Angles (2 pi)')
    ax.set_xlim(-1, sim.A.shape[0])
    ax.set_ylim(-1, sim.A.shape[1])
    q.set_UVC(U, V, C=sim.A)

    return q, fig, rects
def GetXYAnimation(lattice_shape,
                   beta,
                   steps,
                   iters_per_step,
                   filename,
                   J=1,
                   random_state=None):
    import matplotlib.pylab as plt
    import matplotlib.animation as animation
    import matplotlib.patches as patches
    from matplotlib.collections import PatchCollection

    xy = XYModelMetropolisSimulation(lattice_shape=lattice_shape,
                                     beta=beta,
                                     J=J,
                                     random_state=random_state)

    X = np.arange(xy.L.size).reshape(xy.L.shape) % xy.L.shape[0]
    Y = (np.arange(xy.L.size).reshape(xy.L.shape) % xy.L.shape[1]).T

    U = np.cos(2 * np.pi * xy.L)
    V = np.sin(2 * np.pi * xy.L)

    fig, ax = plt.subplots(1, 1)

    rects = []
    colors = []

    for i in range(xy.L.shape[0]):
        for j in range(xy.L.shape[1]):
            rect = patches.Rectangle(xy=(i - 0.5, j - 0.5),
                                     height=1,
                                     width=1,
                                     facecolor="red")
            rects.append(rect)
            colors.append(np.abs(xy.H_matrix[i, j]))

    rects = PatchCollection(rects)
    rects.set_clim([0, 4])
    rects.set_animated(True)
    rects.set_array(np.array(colors))
    ax.add_collection(rects)

    Q = ax.quiver(X, Y, U, V, pivot='tail', color='b', units='inches')

    ax.set_xlim(-1, xy.L.shape[0])
    ax.set_ylim(-1, xy.L.shape[1])

    def update_quiver(num, rects, Q, steps, xy):
        for _ in range(steps):
            xy.make_step()

        colors = np.abs(xy.H_matrix.flatten('F'))
        rects.set_array(np.array(colors))

        U = np.cos(2 * np.pi * xy.L)
        V = np.sin(2 * np.pi * xy.L)

        Q.set_UVC(U, V)

        return rects, Q,

    ani = animation.FuncAnimation(fig,
                                  update_quiver,
                                  frames=steps,
                                  fargs=(rects, Q, iters_per_step, xy),
                                  interval=25,
                                  blit=False)

    ani.save(filename)