Beispiel #1
0
def match_model_to_agent(agent_dirname, dt, dx):
    agent_fnames = bu.get_filenames(agent_dirname)
    m_agent_0 = bu.filename_to_model(agent_fnames[0])
    # x_agent = np.linspace(-m_agent_0.L / 2.0, m_agent_0.L / 2.0,
    #                       m_agent_0.c.a.shape[0])
    ts = np.array([bu.filename_to_model(f).t for f in agent_fnames])

    rho_0 = m_agent_0.rho_0
    origin_flag = m_agent_0.origin_flag
    dx_agent = m_agent_0.dx
    mu = 0.5 * m_agent_0.chi * m_agent_0.v_0
    if m_agent_0.onesided_flag:
        mu /= 2.0
    rho_D = m_agent_0.v_0 ** 2 / m_agent_0.p_0
    L = m_agent_0.L
    c_D = m_agent_0.c_D
    c_sink = m_agent_0.c_sink
    c_sink = m_agent_0.c_sink
    c_source = m_agent_0.c_source

    m = ModelCoarse1D(dt,
                      rho_0, rho_D, origin_flag, dx_agent,
                      mu,
                      L, dx,
                      c_D, c_sink, c_source)

    fig = plt.figure()
    ax = fig.gca()
    ax.set_xlim(-500.0, 500.0)
    ax.set_ylim(0.0, 1000.0)
    plot_rho = ax.plot(m.get_x(), m.get_rho(), c='red')[0]
    plot_arho = ax.plot(m.get_x(), get_rho_agent(m_agent_0, m), c='green')[0]
    plt.ion()
    plt.show()

    every = 10

    while m.t < 20000.0:
        if not m.i % every:
            rho_coarse = m.get_rho()
            plot_rho.set_ydata(rho_coarse)

            i_agent_fname = find_nearest_index(m.t, ts)
            m_agent = bu.filename_to_model(agent_fnames[i_agent_fname])
            rho_agent = get_rho_agent(m_agent, m)
            plot_arho.set_ydata(rho_agent)
            ax.set_ylim(0.0, 1.1 * max(rho_coarse.max(), rho_agent.max()))
            fig.canvas.draw()
            print(m.t, np.mean(m.get_rho()), agent_fnames[i_agent_fname])
        m.iterate()
    return m
m_coarse = ModelCoarse1D(dt,
                         rho_0, rho_D, origin_flag, dx_agent,
                         mu,
                         L, dx,
                         c_D, c_sink, c_source)

for _ in range(200):
    m_coarse.iterate()
rho_coarse = m_coarse.get_rho()

agent_fnames = bu.get_filenames(agent_dirname)
ts = np.array([bu.filename_to_model(f).t for f in agent_fnames])
i_agent_fname = find_nearest_index(m_coarse.t, ts)
m_agent = bu.filename_to_model(agent_fnames[i_agent_fname])
rho_agent = get_rho_agent(m_agent, m_coarse)

ms = [bu.filename_to_model(fname) for fname in bu.get_filenames(agent_dirname)]
rho_agent = np.mean([get_rho_agent(m, m_coarse)
                     for m in ms if m.t > m_coarse.t], axis=0)
x_tild = m_coarse.get_x() / np.sqrt(rho_D / c_sink)
ax.plot(x_tild, 1.0 * rho_coarse / rho_0, c=set2[0], label='Coarse')
ax.plot(x_tild + 0.1, rho_agent / rho_0, c=set2[1], label='Agent')

ax.legend(loc='lower right', fontsize=26, frameon=True)
ax.set_xlabel(r'$\tilde{x}$', fontsize=35)
ax.set_ylabel(r'$\rho / \rho_0$', fontsize=35)
ax.tick_params(axis='both', labelsize=26, pad=10.0)
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(0.0, 65.0)