用Python实现Vicsek模型

时间:2017-07-06 03:26:13

标签: python python-3.x runtime-error

我试图在python中运行用于实现Vicsek模型的代码,可以在此处下载此代码:https://github.com/eddiejessup/vicsek

但是,当我在spider中执行此代码时,它会向我显示此消息:

  

from particle_numerics import vicsek_angular,vicsek_vectorial

     

ModuleNotFoundError:没有名为' particle_numerics'

的模块

我知道文件夹中有一个名为particle_numerics.pyx的文件,但我不知道如何使这段代码正常工作......我该怎么办?

代码如下:

from __future__ import print_function, division
import numpy as np
import matplotlib.pyplot as plt
from ciabatta import ejm_rcparams
from model import vicsek_model_factory




def plot_vicsek(model, n):
    fig = plt.figure()
    ax = fig.gca()
    q = ax.quiver(model.r, model.L * model.u)
    ax.set_xlim(-model.L_half, model.L_half)
    ax.set_ylim(-model.L_half, model.L_half)
    ax.set_aspect('equal')
    plt.ion()
    plt.show()
    for _ in range(n):
        model.iterate()
        q.set_offsets(model.r)
        q.set_UVC(*(model.L * model.u.T))
        fig.canvas.draw()


def make_illustration_snapshot():
    fig = plt.figure()
    ax = fig.gca()
    model = vicsek_model_factory(model='angular',
                             n=200, L=100.0, eta=0.2, v_0=0.5)
   for _ in range(100):
        model.iterate()
ejm_rcparams.set_pretty_plots(use_latex=True, use_pgf=True)
ejm_rcparams.prettify_axes(ax)

ax.set_xlim(-model.L_half, model.L_half)
ax.set_ylim(-model.L_half, model.L_half)
ax.set_aspect('equal')

i_source = np.argmin(np.sum(np.square(model.r), axis=1))
r_i = model.r[i_source]
c = plt.Circle(r_i, radius=model.r_v, fill=False, edgecolor='red')
ax.add_patch(c)
colors = np.zeros([model.n])
colors[i_source] = 1.0
i_neighbs = model._neighbs(i_source)
for i_neighb in i_neighbs:
    colors[i_neighb] = 0.5

s = 2.0
ax.quiver(model.r[:, 0], model.r[:, 1],
          s * model.L * model.u[:, 0], s * model.L * model.u[:, 1],
          colors, pivot='mid', edgecolor='none')
ax.axis('off')

plt.savefig('vicsek_snapshot_demo.pdf', bbox_inches='tight',
            transparent=True)


def make_ordered_snapshot():
    model = vicsek_model_factory(model='angular',
                             n=100, L=100.0, eta=0.05, v_0=0.5)
    for _ in range(100):
        model.iterate()

fig = plt.figure()
ax = fig.gca()
ejm_rcparams.set_pretty_plots(use_latex=True, use_pgf=True)
ejm_rcparams.prettify_axes(ax)

ax.set_xlim(-model.L_half, model.L_half)
ax.set_ylim(-model.L_half, model.L_half)
ax.set_aspect('equal')
s = 2.0
ax.quiver(model.r[:, 0], model.r[:, 1],
          s * model.L * model.u[:, 0], s * model.L * model.u[:, 1],
          pivot='mid', edgecolor='none')
ax.axis('off')

plt.savefig('vicsek_snapshot_ordered.pdf', bbox_inches='tight',
            transparent=True)


def make_disordered_snapshot():
    model = vicsek_model_factory(model='angular',
                             n=100, L=100.0, eta=0.8, v_0=0.5)
    for _ in range(100):
         model.iterate()

fig = plt.figure()
ax = fig.gca()
ejm_rcparams.set_pretty_plots(use_latex=True, use_pgf=True)
ejm_rcparams.prettify_axes(ax)
ax.set_xlim(-model.L_half, model.L_half)
ax.set_ylim(-model.L_half, model.L_half)
ax.set_aspect('equal')
s = 2.0
ax.quiver(model.r[:, 0], model.r[:, 1],
          s * model.L * model.u[:, 0], s * model.L * model.u[:, 1],
          pivot='mid', edgecolor='none')
ax.axis('off')

plt.savefig('vicsek_snapshot_disordered.pdf', bbox_inches='tight',
            transparent=True)


def eta_scan(model, n, L, v_0, num_equil, num_measure, etas):
    for eta in etas:
        m = vicsek_model_factory(model=model, n=n, L=L, eta=eta, v_0=v_0)
        # Equilibrate
        for _ in range(num_equil):
            m.iterate()
        # Take measurements
        mags = []
        for _ in range(num_measure):
        m.iterate()
        mags.append(m.macro_u_mag)
    yield eta, np.mean(mags), np.std(mags), len(mags)


def make_vicsek_stats():
    n = 2048
    L = 32.0
    v_0 = 0.5
    num_equil = 100
    num_measure = 500

for model in ('angular', 'vectorial'):
    if model == 'angular':
        etas = np.linspace(0.6, 0.8, 20)
    else:
        etas = np.linspace(0.55, 0.65, 20)
    stats = eta_scan(model, n, L, v_0, num_equil, num_measure, etas)
    np.savetxt('{}_stats.txt'.format(model), list(stats))

if __name__ == '__main__':
    plot_vicsek(vicsek_model_factory(model='angular',
                                 n=200, L=50.0, eta=0.05, v_0=0.5), 100)

0 个答案:

没有答案