Beispiel #1
0
def plot_all_average_velocity_and_variance(data_path: str,
                                           yaml_path: str,
                                           search_parameters: dict = {}):
    sns.set(style="white", context="talk")
    os.chdir("E:/")
    history = processing.get_master_yaml(yaml_path)
    timesteps = processing.get_parameter_range("dt",
                                               history,
                                               exclude={"dt": 1.0})

    fig = plotting.multiple_timescale_plot(
        search_parameters,
        break_time_step=40,
        metric=statistics.calculate_avg_vel,
        parameter="dt",
        parameter_range=timesteps,
        history=history,
        include_traj=False,
        data_path=data_path,
    )

    fig2 = plotting.multiple_timescale_plot(
        search_parameters,
        break_time_step=40,
        metric=statistics.calculate_variance,
        parameter="dt",
        parameter_range=timesteps,
        history=history,
        include_traj=False,
        data_path=data_path,
    )
    plt.show()
    return
Beispiel #2
0
def plot_avg_vel(
    ax,
    search_parameters: dict,
    scalarMap=None,
    logx=True,
    data_path: str = "Experiments/Data.nosync/",
    yaml_path: str = "Experiments/positive_phi_no_of_clusters",
    end_time_step: int = -1,
):
    """Plots average velocity of particles on log scale, colours lines according to
    number of clusters in the inital condition
    """
    if scalarMap is None:
        cm = plt.get_cmap("coolwarm")
        cNorm = mpl.colors.DivergingNorm(vmin=0, vcenter=2, vmax=4)
        scalarMap = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm)
    history = get_master_yaml(yaml_path)
    list_of_names = match_parameters(search_parameters, history)
    print(list_of_names)
    cycle = plt.rcParams["axes.prop_cycle"].by_key()["color"]

    for file_name in list_of_names:
        simulation_parameters = history[file_name]
        t, x, v = load_traj_data(file_name, data_path)

        cluster_count = _get_number_of_clusters(
            simulation_parameters["initial_dist_x"])
        if logx:
            ax.semilogx(
                t,
                v.mean(axis=1),
                color=cycle[cluster_count -
                            1],  # simulation_parameters["gamma"]),
                label=f"{cluster_count} Clusters",
                alpha=0.1,
                zorder=1,
            )
        else:
            ax.plot(
                t,
                v.mean(axis=1),
                color=cycle[cluster_count -
                            1],  # simulation_parameters["gamma"]),
                label=f"{cluster_count} Clusters",
                alpha=0.1,
                zorder=1,
            )
    # plt.tight_layout()
    return ax
Beispiel #3
0
def plot_convergence_from_clusters(
    ax,
    search_parameters: dict,
    yaml_path: str,
    data_path: str = "Experiments/Data.nosync/",
    logx=True,
):
    history = get_master_yaml(yaml_path)

    file_names = match_parameters(search_parameters, history)
    cycle = plt.rcParams["axes.prop_cycle"].by_key()["color"]
    for file_name in file_names:
        print(file_name)
        simulation_parameters = history[file_name]
        t, x, v = load_traj_data(file_name, data_path)
        error = calculate_l1_convergence(t, x, v, plot_hist=False)
        cluster_count = _get_number_of_clusters(
            simulation_parameters["initial_dist_x"])
        print(cluster_count)
        cluster_label = f"{cluster_count} cluster{'' if cluster_count==1 else 's'}"
        if logx:
            ax.semilogx(
                t,
                error,
                label=cluster_label,
                color=cycle[cluster_count - 1],
                alpha=0.25,
            )

        else:
            ax.plot(
                t,
                error,
                label=cluster_label,
                color=cycle[cluster_count - 1],
                alpha=0.25,
            )

    ax.set(xlabel="Time", ylabel=r"$\ell^1$ Error")
    handles, labels = ax.get_legend_handles_labels()
    # sort both labels and handles by labels
    labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
    ax.legend(handles, labels)
    plt.tight_layout()
    return ax
Beispiel #4
0
def plot_ODE_solution(data_path: str,
                      yaml_path: str,
                      search_parameters: dict = {},
                      **kwargs):
    os.chdir("E:/")
    history = processing.get_master_yaml(yaml_path)

    fig, ax = plt.subplots()
    timestep_range = processing.get_parameter_range("dt", history, **kwargs)
    for timestep in timestep_range:
        search_parameters["dt"] = timestep
        file_names = processing.match_parameters(search_parameters, history,
                                                 **kwargs)
        for file_name in file_names:
            t, x, v = processing.load_traj_data(file_name, data_path)
            # print(len(t))
            if file_name == file_names[0]:
                sum_avg_vel = np.zeros(len(v[:, 0]))
            print(file_name)
            # sum_avg_vel += v.mean(axis=1)
            ax.plot(t, v.mean(axis=1), "r--", alpha=0.1)

    def first_moment_ode(t, M):
        return G.step(M) - M

    sol = solve_ivp(
        first_moment_ode,
        (t.min(), t.max()),
        [v[0].mean()],
        t_eval=t.ravel(),
        rtol=10**-9,
        atol=10**-9,
    )
    t = sol.t
    M = sol.y
    ax.plot(t, M.ravel(), label="Numerical Sol")
    ax.plot(t, sum_avg_vel / len(file_names))
    ax.legend()

    plt.show()
    return
def phi_one_convergence(time_ax="linear"):
    # sns.set(style="white", context="paper")
    sns.set_style("ticks")
    fig1, ax1 = plt.subplots(figsize=(12, 6))

    cm = plt.get_cmap("coolwarm")
    cNorm = colors.DivergingNorm(vmin=-25, vcenter=0.0, vmax=25)
    scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
    yaml_path = "./Experiments/phi_one_convergence"
    history = get_master_yaml(yaml_path)
    file_names = match_parameters({}, history)
    dt = 0.01
    print(yaml_path)

    for file_name in file_names:
        x, v = load_traj_data(file_name, data_path="./Experiments/Data/")
        t = np.arange(0, len(v) * dt, dt)
        if time_ax == "linear":
            ax1.plot(
                t[:int(20 / dt)],
                v[:int(20 / dt)].mean(axis=1),
                color=scalarMap.to_rgba(np.sign(v[0].mean()) * v[0].var()),
            )
        else:
            ax1.semilogx(
                t[:int(20 / dt)],
                v[:int(20 / dt)].mean(axis=1),
                color=scalarMap.to_rgba(np.sign(v[0].mean()) * v[0].var()),
            )
    ax1.plot([0, 20], [1, 1], "k--", alpha=0.25)
    ax1.plot([0, 20], [-1, -1], "k--", alpha=0.25)
    ax1.set(xlabel="Time", ylabel=r"Average Velocity, $M^N(t)$")
    plt.subplots_adjust(top=0.905,
                        bottom=0.135,
                        left=0.115,
                        right=0.925,
                        hspace=0.2,
                        wspace=0.2)
    plt.show()
    return
def avg_vel(
    ax,
    scalarMap,
    file_path: str = "Experiments/Data/",
    yaml_path: str = "Experiments/vary_large_gamma_local",
    search_parameters: dict = {
        "particle_count": 450,
        "G": "Step",
        "scaling": "Local",
        "phi": "Gamma",
        "initial_dist_x": "two_clusters_2N_N",
        "initial_dist_v": "2N_N_cluster_const",
        "T_end": 100,
        "dt": 0.01,
    },
):

    list_of_names = []
    print(yaml_path)
    history = get_master_yaml(yaml_path)
    list_of_names = match_parameters(search_parameters, history)

    for file_name in list_of_names:
        simulation_parameters = history[file_name]
        t, x, v = load_traj_data(file_name, data_path=file_path)
        if t is None:
            t = np.arange(0,
                          len(x) * simulation_parameters["dt"],
                          simulation_parameters["dt"])
        if simulation_parameters["gamma"] >= 0.05:
            ax.semilogx(
                t,
                v.mean(axis=1),
                color=scalarMap.to_rgba(simulation_parameters["gamma"]),
                label="{:.2f}".format(simulation_parameters["gamma"]),
                # alpha=0.75,
            )
    return ax
Beispiel #7
0
def run_timestep_experiment_low_gamma_high_particles():
    timesteps = np.logspace(start=0, stop=-4, num=15)
    test_params = {
        "particle_count": 20 * [501],
        "gamma": [0.01],
        "G": ["Smooth"],
        "scaling": ["Local"],
        "D": [0.25],
        "phi": ["Gamma"],
        "initial_dist_x": ["two_clusters_2N_N"],
        "initial_dist_v": ["2N_N_cluster_const"],
        "T_end": [200.0],
        "dt": timesteps.tolist(),
        "option": ["numba"],
    }

    os.chdir("E:/")
    history = processing.get_master_yaml(yaml_path="timestep_experiments")
    fn = "LowGammaLoweringTimestepHighParticles"
    processing.run_experiment(test_params, history, experiment_name=fn)
    print(
        "Running reduced timestep with gamma =0.01 and N=500 --- does non-uniformity persist?"
    )
Beispiel #8
0
def run_timestep_experiment_phi_uniform():
    timesteps = np.logspace(start=0, stop=-4, num=15)
    test_params = {
        "particle_count":
        10 * [99, 501],  # (3 * np.arange(8, 150, 16)).tolist(),
        "G": ["Smooth"],
        "scaling": ["Local"],
        "D": [0.25],
        "phi": ["Uniform"],
        "initial_dist_x": ["two_clusters_2N_N"],
        "initial_dist_v": ["2N_N_cluster_const"],
        "T_end": [200.0],
        "dt": timesteps.tolist(),
        "option": ["numba"],
    }

    os.chdir("E:/")
    history = processing.get_master_yaml(yaml_path="timestep_experiments")
    fn = "UniformInteractionLoweringTimestep"
    processing.run_experiment(test_params, history, experiment_name=fn)
    print(
        "Running reduced timestep with phi=1 --- post-process to check against gamma =0.5"
    )
import particle.processing as processing

particles = 100

test_params = {
    "particle_count":
    100 * [particles],  # (3 * np.arange(8, 150, 16)).tolist(),
    # "gamma": (np.arange(0.2, 0.5, 0.05)).tolist(),
    "G": ["Smooth"],
    "scaling": ["Local"],
    "D": [1.0],
    "phi": ["Gamma"],
    "gamma": [0.1],
    "initial_dist_x": ["uniform_dn"],
    "initial_dist_v": ["pos_normal_dn"],
    "T_end": [2000.0],
    "dt": [0.01],
    "option": ["numba"],
}

history = processing.get_master_yaml(yaml_path="experiments_ran")
# fn = (
#     f"""{test_params["initial_dist_v"][0]}_"""
#     f"""vel_{test_params["scaling"][0]}_G{test_params["G"][0]}_"""
#     f"""T{int(test_params["T_end"][0])}_noise_report_Galpha"""
# )
fn = f"""PSMean0"""
processing.run_experiment(test_params, history, experiment_name=fn)
    "initial_dist_x": "one_cluster",
    "initial_dist_v": "pos_normal_dn",
    "T_end": 2000.0,
    # "dt": 0.015,
}

os.chdir("/Volumes/Extreme SSD/InteractingParticleSystems/noisysystem_temp")

final_plot_time = 5000000

yaml_path = (
    "./Experiments/one_cluster_vary_noise_scale_dt_100_runs_larger_gamma_long_run"
)
data_path = "./Experiments/Data.nosync/"

history = get_master_yaml(yaml_path)

fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(15, 5), sharex=True)

# Create colour bar and scale
cm = plt.get_cmap("coolwarm")
cNorm = colors.DivergingNorm(vmin=0, vcenter=0.25, vmax=0.5)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
# Set tick locations
cbar = fig.colorbar(scalarMap, ticks=np.arange(0, 0.5, 0.05))

# For each matching desired parameters, calculate the l1 error and plot

for diffusion in np.arange(0.05, 0.5, 0.05).tolist():
    search_parameters["D"] = diffusion
    file_names = match_parameters(search_parameters, history)
Beispiel #11
0
def plot_averaged_convergence_from_clusters(ax,
                                            search_parameters: dict,
                                            yaml_path: str,
                                            data_path: str,
                                            logx=True):
    history = get_master_yaml(yaml_path)
    for initial_dist_x in [
            "one_cluster",
            "two_clusters",
            "three_clusters",
            "four_clusters",
    ]:
        search_parameters["initial_dist_x"] = initial_dist_x
        file_names = match_parameters(search_parameters, history)
        cycle = plt.rcParams["axes.prop_cycle"].by_key()["color"]
        for idx, file_name in enumerate(file_names):
            print(file_name)
            simulation_parameters = history[file_name]
            cluster_count = _get_number_of_clusters(
                simulation_parameters["initial_dist_x"])
            cluster_label = f"{cluster_count} cluster{'' if cluster_count==1 else 's'}"

            if idx == 0:
                t, x, v = load_traj_data(file_name, data_path)
                t = t.flatten()
                error = calculate_l1_convergence(t[t <= 10],
                                                 x[t <= 10],
                                                 v[t <= 10],
                                                 plot_hist=False)
                error_store = np.zeros((len(file_names), len(error)))
                error_store[idx, :] = error
            else:
                t, x, v = load_traj_data(file_name, data_path)
                t = t.flatten()
                error = calculate_l1_convergence(t[t <= 10],
                                                 x[t <= 10],
                                                 v[t <= 10],
                                                 plot_hist=False)
                error_store[idx, :] = error

        if logx:
            t = t[t <= 10]
            ax.semilogx(
                t,
                np.mean(error_store, axis=0),
                label=cluster_label,
                color=cycle[cluster_count - 1],
            )
        else:
            t = t[t <= 10]
            ax.plot(
                t,
                np.mean(error_store, axis=0),
                label=cluster_label,
                color=cycle[cluster_count - 1],
            )
            # ax.plot(
            #     t[9:], moving_average(np.mean(error_store, axis=0), n=10), "r",
            # )

    ax.set(xlabel="Time", ylabel=r"$\ell^1$ Error")
    handles, labels = ax.get_legend_handles_labels()
    # sort both labels and handles by labels
    labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
    ax.legend(handles, labels)
    plt.tight_layout()
    return ax
Beispiel #12
0
def plot_averaged_avg_vel(
    ax,
    search_parameters: dict,
    logx=True,
    include_traj=True,
    scalarMap=None,
    data_path: str = "Experiments/Data.nosync/",
    yaml_path: str = "Experiments/positive_phi_no_of_clusters",
    start_time_step: int = 0,
    end_time_step: int = -1,
):
    """Plots average velocity of particles on log scale, colours lines according to
    number of clusters in the inital condition
    """
    if scalarMap is None:
        cm = plt.get_cmap("coolwarm")
        cNorm = mpl.colors.DivergingNorm(vmin=1, vcenter=2, vmax=4)
        scalarMap = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm)
    history = get_master_yaml(yaml_path)

    # for initial_dist_x in [
    #     "one_cluster",
    #     "two_clusters",
    #     "three_clusters",
    #     "four_clusters",
    # ]:
    #     search_parameters["initial_dist_x"] = initial_dist_x
    list_of_names = match_parameters(search_parameters, history)
    #     print(list_of_names)
    cycle = plt.rcParams["axes.prop_cycle"].by_key()["color"]
    for idx, file_name in enumerate(list_of_names):

        simulation_parameters = history[file_name]
        cluster_count = _get_number_of_clusters(
            simulation_parameters["initial_dist_x"])

        cluster_label = f"{cluster_count} cluster{'' if cluster_count==1 else 's'}"

        t, x, v = load_traj_data(file_name, data_path)
        t = t[start_time_step:end_time_step]
        v = v[start_time_step:end_time_step]
        avg_vel = v.mean(axis=1)
        cluster_count = _get_number_of_clusters(
            history[file_name]["initial_dist_x"])

        if include_traj and logx:
            ax.semilogx(t,
                        avg_vel,
                        color=cycle[cluster_count - 1],
                        alpha=0.1,
                        zorder=1)
        if include_traj and not logx:
            ax.plot(t,
                    avg_vel,
                    color=cycle[cluster_count - 1],
                    alpha=0.1,
                    zorder=1)

        if idx == 0:
            avg_vel_store = np.zeros((len(list_of_names), len(avg_vel)))

        avg_vel_store[idx, :] = avg_vel
    if logx:
        ax.semilogx(
            t,
            np.mean(avg_vel_store, axis=0),
            color=cycle[cluster_count - 1],  # history[file_name]["gamma"]),
            label=cluster_label,
            # alpha=0.5,
            zorder=2,
        )
    else:
        ax.plot(
            t,
            np.mean(avg_vel_store, axis=0),
            color=cycle[cluster_count - 1],  # history[file_name]["gamma"]),
            label=cluster_label,
            # alpha=0.5,
            zorder=2,
        )
    # plt.tight_layout()
    return ax