Example #1
0
def plot_1DPotential_dhdpos(potential: _potential1DCls,
                            positions: list,
                            color=style.potential_color(1),
                            x_range=None,
                            y_range=None,
                            title: str = None,
                            ax=None,
                            yUnit: str = "kT"):
    # generat Data
    energies = potential.force(positions=positions)

    # is there already a figure?
    if (ax is None):
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = None

    # plot
    ax.plot(positions, energies, c=color)
    ax.set_xlim(min(x_range), max(x_range)) if (
        x_range != None) else ax.set_xlim(min(positions), max(positions))
    ax.set_ylim(min(y_range), max(y_range)) if (
        y_range != None) else ax.set_ylim(min(energies), max(energies))

    ax.set_xlabel('$r$')
    ax.set_ylabel('$\partial V / \partial r\\ [' + yUnit + ']$')
    ax.set_title(title) if (
        title != None) else ax.set_title("Potential " + str(potential.name))

    if (ax != None):
        return fig, ax
    else:
        return ax
Example #2
0
def plot_1DPotential(potential: _potential1DCls,
                     positions: list,
                     out_path: str = None,
                     x_range=None,
                     y_range=None,
                     title: str = None,
                     ax=None):
    fig, axes = plt.subplots(nrows=1, ncols=2)
    plot_1DPotential_V(potential=potential,
                       positions=positions,
                       ax=axes[0],
                       x_range=x_range,
                       y_range=y_range,
                       title="",
                       color=style.potential_color(0))
    plot_1DPotential_dhdpos(potential=potential,
                            positions=positions,
                            ax=axes[1],
                            x_range=x_range,
                            y_range=y_range,
                            title="")

    fig.tight_layout()
    fig.subplots_adjust(top=0.8)
    fig.suptitle(title, y=0.95) if (title != None) else fig.suptitle(
        "" + str(potential.name), y=0.96)

    if (out_path):
        fig.savefig(out_path)
        plt.close(fig)

    return fig, out_path
Example #3
0
def envPot_diffS_compare(eds_potential: pot.envelopedPotential,
                         s_values: list,
                         positions: list,
                         y_range: tuple = None,
                         title: str = None,
                         out_path: str = None):
    ##row/column ratio
    per_row = 4
    n_rows = (len(s_values) // per_row) + 1 if (
        (len(s_values) % per_row) > 0) else (len(s_values) // per_row)

    ##plot
    fig, axes = plt.subplots(nrows=n_rows, ncols=per_row, figsize=(20, 10))
    axes = [ax for ax_row in axes for ax in ax_row]

    for ind, (ax, s) in enumerate(zip(axes, s_values)):
        color = style.potential_color(ind % len(style.potential_color))
        eds_potential.s = s
        y = eds_potential.ene(positions)
        ax.plot(positions, y, c=color)

        # styling
        ax.set_xlim(min(positions), max(positions))
        ax.set_title("s_" + str(significant_decimals(s)))
        ax.set_ylabel("Vr/[kJ]")
        ax.set_xlabel("r")
        if (y_range != None): ax.set_ylim(y_range)

    ##optionals
    if (title): fig.suptitle(title)
    if (out_path): fig.savefig(out_path)
    #fig.show()
    return fig, axes
Example #4
0
def plot_1DPotential_Termoverlay(potential: _potential1DCls,
                                 positions: list,
                                 x_range=None,
                                 y_range=None,
                                 title: str = None,
                                 ax=None):
    # generate dat
    energies = potential.ene(positions=positions)
    dVdpos = potential.force(positions=positions)

    # is there already a figure?
    if (ax is None):
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = None

    color = style.potential_color(1)
    color1 = style.potential_color(2)
    color2 = style.potential_color(3)

    ax.plot(positions, energies, label="V", c=color)
    ax.plot(positions, list(map(abs, dVdpos)), label="absdVdpos", c=color1)
    ax.plot(positions, dVdpos, label="dVdpos", c=color2)
    ax.set_xlim(min(x_range), max(x_range)) if (
        x_range != None) else ax.set_xlim(min(positions), max(positions))
    ax.set_ylim(min(y_range),
                max(y_range)) if (y_range != None) else ax.set_ylim(
                    min([min(energies), min(dVdpos)]),
                    max([max(energies), max(dVdpos)]))

    ax.set_ylabel("$V/kJ$")
    ax.set_xlabel("$x$")
    ax.legend()
    ax.set_title(title) if (
        title != None) else ax.set_title("Potential " +
                                         str(potential.__name__))

    if (ax != None):
        return fig, ax
    else:
        return ax
Example #5
0
def envPot_differentS_overlay_plot(eds_potential: pot.envelopedPotential,
                                   s_values: list,
                                   positions: list,
                                   y_range: tuple = None,
                                   hide_legend: bool = False,
                                   title: str = None,
                                   out_path: str = None,
                                   axes=None):
    # generate energy values
    ys = []
    for s in s_values:
        eds_potential.s = s
        enes = eds_potential.ene(positions)
        ys.append(enes)

    # plotting
    if (axes is None):
        fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20, 10))
    else:
        fig = None

    for i, (s, y) in enumerate(reversed(list(zip(s_values, ys)))):
        color = style.potential_color(i % 12)
        axes.plot(positions,
                  y,
                  label="s_" + str(significant_decimals(s)),
                  color=color)

    # styling
    axes.set_xlim(min(positions), max(positions))
    axes.set_ylabel("Vr/[kJ]")
    axes.set_xlabel("r")
    if (title is None):
        axes.set_title("different $V_{r}$s with different s-values overlayed ")
    else:
        axes.set_title(title)

    ##optionals
    if (y_range != None): axes.set_ylim(y_range)
    if (not hide_legend): axes.legend()
    if (title and not isinstance(fig, type(None))): fig.suptitle(title)
    if (out_path and not isinstance(fig, type(None))): fig.savefig(out_path)
    #if (not isinstance(fig, type(None))): fig.show()

    return fig, axes
Example #6
0
def plot_envelopedPotential_system(eds_potential: pot.envelopedPotential,
                                   positions: list,
                                   s_value: float = None,
                                   Eoffi: list = None,
                                   y_range: tuple = None,
                                   title: str = None,
                                   out_path: str = None):
    if (s_value != None):
        eds_potential.s = s_value  # set new s
    if (Eoffi != None):
        if (len(Eoffi) == len(eds_potential.V_is)):
            eds_potential.set_Eoff(Eoffi)
        else:
            raise IOError("There are " + str(len(eds_potential.V_is)) +
                          " states and " + str(Eoffi) +
                          ", but the numbers have to be equal!")

    ##calc energies
    energy_Vr = eds_potential.ene(positions)
    energy_Vis = [state.ene(positions) for state in eds_potential.V_is]
    num_states = len(eds_potential.V_is)

    ##plot nicely
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))
    axes = [ax for ax_row in axes for ax in ax_row]
    y_values = energy_Vis + [energy_Vr]
    labels = ["state_" + str(ind)
              for ind in range(1,
                               len(energy_Vis) + 1)] + ["refState"]

    for i, (ax, y, label) in enumerate(zip(axes, y_values, labels)):
        color = style.potential_color(i % 12)
        ax.plot(positions, y, c=color)
        ax.set_xlim(min(positions), max(positions))
        ax.set_ylim(y_range)
        ax.set_title(label)
        ax.set_ylabel("Vr/[kJ]")
        ax.set_xlabel("r_" + label)

    ##optionals
    if (title): fig.suptitle(title)
    if (out_path): fig.savefig(out_path)
    #fig.show()
    return fig, axes
Example #7
0
def envPot_differentS_overlay_min0_plot(eds_potential: pot.envelopedPotential,
                                        s_values: list,
                                        positions: list,
                                        y_range: tuple = None,
                                        hide_legend: bool = False,
                                        title: str = None,
                                        out_path: str = None):
    # generate energy values
    ys = []
    scale = 1  # 0.1
    for s in s_values:
        eds_potential.s = s
        enes = eds_potential.ene(positions)
        y_min = min(enes)
        y = list(map(lambda z: (z - y_min) * scale, enes))
        ys.append(y)

    # plotting
    fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20, 10))
    for i, (s, y) in enumerate(reversed(list(zip(s_values, ys)))):
        color = style.potential_color(i % len(style.potential_color))
        axes.plot(positions,
                  y,
                  label="s_" + str(significant_decimals(s)),
                  c=color)

    if (y_range != None):
        axes.set_ylim(y_range)
    axes.set_xlim(min(positions), max(positions))

    # styling
    axes.set_ylabel("Vr/[kJ]")
    axes.set_xlabel("r")
    axes.set_title(
        "different Vrs aligned at 0 with different s-values overlayed ")

    ##optionals
    if (not hide_legend): axes.legend()
    if (title): fig.suptitle(title)
    if (out_path): fig.savefig(out_path)
    #fig.show()

    return fig, axes
Example #8
0
def oneD_simulation_analysis_plot(
    system: systemCls,
    title: str = "",
    out_path: str = None,
    limits_coordinate_space: Tuple[float, float] = None,
    limits_potential_system_energy: Tuple[float, float] = None,
    limits_force: Tuple[float, float] = None,
    resolution_full_space=style.potential_resolution,
    figsize: Tuple[float, float] = figaspect(0.25)
) -> Tuple[plt.figure, str]:
    """
        This plot gives insight into sampled coordinate - space, the position distribution and the force timeseries

    Parameters
    ----------
    system: systemCls
        a system that carried out a simulation, which should be visualized.
    title: str, optional
        title to the output plot
    out_path: str, optional
        store the plot to the given path
    limits_coordinate_space: tuple, optional
        the coordinate space range
    limits_potential_system_energy: tuple, optional
        y-limits for the Potential energies
    limits_force: tuple, optional
        y-limits for force plots
    resolution_full_space: int, optional
        how many points, shall be used to visualize the potential function.

    Returns
    -------
    Tuple[plt.figure, str]
        returns the generated figure and the output str
    """

    # gather data
    traj = system.trajectory
    last_frame = traj.shape[0] - 1

    x = list(traj.position)
    y = traj.total_potential_energy
    shift = traj.dhdpos

    # dynamic plot range
    if (isinstance(limits_coordinate_space, type(None))):
        x_pot = np.linspace(
            min(x) + min(x) * 0.25,
            max(x) + max(x) * 0.25, resolution_full_space)
    elif (type(limits_coordinate_space) == range):
        x_pot = limits_coordinate_space
    else:
        x_pot = np.linspace(min(limits_coordinate_space),
                            max(limits_coordinate_space) + 1,
                            resolution_full_space)

    ytot_space = system.potential.ene(x_pot)

    # plot
    w, h = figsize
    fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=[w, h])

    #plot coordinate exploration
    ax1.scatter(x, y, c=style.trajectory_color, alpha=style.alpha_traj)  # traj
    ax1.plot(x_pot, ytot_space, c=style.potential_light)
    ax1.scatter(x[0], y[0], c=style.traj_start,
                alpha=style.alpha_val)  # start_point
    ax1.scatter(x[last_frame],
                y[last_frame],
                c=style.traj_end,
                alpha=style.alpha_val)  # end_point

    if (isinstance(system.potential, metadynamicsPotential)
        ):  #for metadynamics, show original potential
        ax1.plot(x_pot,
                 system.potential.origPotential.ene(x_pot),
                 c="k",
                 alpha=style.alpha_val,
                 zorder=10,
                 label="original Potential")

    if (not isinstance(limits_potential_system_energy, type(None))):
        ax1.set_ylim(limits_potential_system_energy)

    # plot position distribution
    color = style.potential_color(2)
    viol = ax2.violinplot(x, showmeans=False, showextrema=False)
    ax2.boxplot(x)
    ax2.scatter([1], [x[0]], c=style.traj_start,
                alpha=style.alpha_val)  # start_point
    ax2.scatter([1], [x[last_frame]], c=style.traj_end,
                alpha=style.alpha_val)  # end_point
    viol["bodies"][0].set_facecolor(color)

    #plot force time series
    color = style.potential_color(3)
    ax3.plot(range(len(x)), shift, color=color)

    #visual_ranges:
    min_pos = min(x_pot) - min(x_pot) * 0.05
    max_pos = max(x_pot) + min(x_pot) * 0.05
    diff = max_pos - min_pos
    min_pos -= diff * 0.05
    max_pos += diff * 0.05

    ax1.set_xlim([min_pos, max_pos])
    ax2.set_ylim([min_pos, max_pos])

    if (limits_force is not None):
        ax3.set_ylim([min(limits_force), max(limits_force)])

    # Labels
    ax1.set_ylabel("$V[kT]$")
    ax1.set_xlabel("$r$")
    ax1.set_title("Potential Sampling")

    ax2.set_ylabel("$r$")
    ax2.set_xlabel("$simulation$")
    ax2.set_title("Explored Space")

    ax3.set_xlabel("$t$")

    if (issubclass(system.sampler.__class__,
                   (stochastic.stochasticSampler, optimizers.optimizer))):
        ax3.set_title("Shifts")
        ax3.set_ylabel("$dr$")

    elif (issubclass(system.sampler.__class__, (newtonian.newtonianSampler))):
        ax3.set_title("Forces")
        ax3.set_ylabel("$\partial V/ \partial r$")

    else:
        ax3.set_title("Shifts")  # FIX this part!
        ax3.set_ylabel("$dr$")
        # raise Exception("Did not find samplers type  >"+str(system.samplers.__class__)+"< ")

    ax2.set_xticks([])

    fig.suptitle(title, y=1.1)
    fig.subplots_adjust(top=0.85)
    fig.tight_layout()

    if (out_path):
        fig.savefig(out_path)
        plt.close(fig)

    return fig, out_path
Example #9
0
def oneD_biased_simulation_analysis_plot(
    system: systemCls,
    out_path: str = None,
    title: str = "",
    limits_coordinate_space: Tuple[float, float] = None,
    limits_potential_system_energy: Tuple[float, float] = None,
    limits_force: Tuple[float, float] = None,
    resolution_full_space: int = style.potential_resolution,
    figsize: Tuple[float, float] = figaspect(0.25)
) -> str:
    '''
    Plot giving the sampled space, position distribution and forces

    Parameters
    ----------
    sys: system Type
        The simulated system
    limits_potential_system_energy: tuple
        Defines the range of the x axis of the first plot
    limits_potential_system_energy: tuple
        Defines the range of the y axis of the first plot
    title: String
        Title for the plot
    out_path: str
        If specified, figure will be saved to this location
    resolution_full_space: int
        Number of points used for visualizing the potential space


    Returns
    -------
    out_path: str
        Location the figure is saved to
    fig: Figure object
    '''

    # gather data
    traj = system.trajectory
    last_frame = traj.shape[0] - 1

    x = list(traj.position)
    y = traj.total_potential_energy
    shift = traj.dhdpos

    # dynamic plot range
    if (limits_coordinate_space is None):
        x_pot = np.linspace(
            min(x) + min(x) * 0.25,
            max(x) + max(x) * 0.25, resolution_full_space)
    elif (type(limits_coordinate_space) == range):
        x_pot = limits_coordinate_space
    else:
        x_pot = np.linspace(min(limits_coordinate_space),
                            max(limits_coordinate_space) + 1,
                            resolution_full_space)

    ytot_space = system.potential.ene(x_pot)

    # plot
    w, h = figsize
    fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=[w, h])

    # traj

    # plot energy landscape of original and bias potential
    if isinstance(system.potential, metadynamicsPotential):
        # special figure for metadynamics simulations
        # plot energy landscape of original potential
        ytot_orig_space = system.potential.origPotential.ene(x_pot)
        ax1.plot(x_pot, ytot_orig_space, c='red')
        # plot energy landscape of total potential
        # ytot_bias_space = sys.potential.origPotential.ene(x_pot) + sys.potential.finished_steps* sys.potential.addPotential.ene(x_pot)
        # ax1.plot(x_pot, ytot_bias_space, c='blue')

        ax1.scatter(x, y, c=style.trajectory_color, alpha=style.alpha_traj)
        ax1.scatter(x[0], y[0], c=style.traj_start,
                    alpha=style.alpha_val)  # start_point
        ax1.scatter(x[last_frame],
                    y[last_frame],
                    c=style.traj_end,
                    alpha=style.alpha_val)  # end_point
    else:
        ax1.scatter(x, y, c=style.trajectory_color, alpha=style.alpha_traj)

        oP = system.potential.origPotential
        oP._update_functions()
        ytot_orig_space = oP.ene(x_pot)
        ytot_bias_space = system.potential.addPotential.ene(x_pot)

        ax1.plot(x_pot, ytot_orig_space, c='red')
        ax1.plot(x_pot, ytot_bias_space, c=style.potential_light)

        # plot energy landscape of total potential
        ax1.plot(x_pot, ytot_space, c='blue')

        ax1.scatter(x[0], y[0], c=style.traj_start,
                    alpha=style.alpha_val)  # start_point
        ax1.scatter(x[last_frame],
                    y[last_frame],
                    c=style.traj_end,
                    alpha=style.alpha_val)  # end_point

    color = style.potential_color(2)
    viol = ax2.violinplot(x, showmeans=False, showextrema=False)
    ax2.boxplot(x)
    ax2.scatter([1], [x[0]], c=style.traj_start,
                alpha=style.alpha_val)  # start_point
    ax2.scatter([1], [x[last_frame]], c=style.traj_end,
                alpha=style.alpha_val)  # end_point

    viol["bodies"][0].set_facecolor(color)

    color = style.potential_color(3)
    ax3.plot(range(len(x)), shift, color=color)

    # Labels
    ax1.set_ylabel("$V_pot$")
    ax1.set_xlabel("$x$")
    ax1.set_title("Potential Sampling")

    # dynamic plot range
    if not (limits_potential_system_energy is None):
        ax1.set_ylim(limits_potential_system_energy)

    if (limits_force is not None):
        ax3.set_ylim([min(limits_force), max(limits_force)])

    #labels
    ax2.set_ylabel("$x$")
    ax2.set_xlabel("$simulation$")
    ax2.set_title("x-Distribution")

    ax3.set_ylabel("$dhdpos$")
    ax3.set_xlabel("$t$")
    ax3.set_title("Forces/shifts")

    ax2.set_xticks([])

    fig.suptitle(title, y=1.08)
    fig.tight_layout()

    if (out_path):
        fig.savefig(out_path)
        plt.close(fig)

    return out_path, fig