コード例 #1
0
ファイル: pair.py プロジェクト: mattwthompson/msibi
    def compute_current_rdf(self, state, r_range, n_bins, smooth=True,
                            max_frames=1e3):
        """ """
        pairs = self.states[state]['pair_indices']
        # TODO: More elegant way to handle units.
        #       See https://github.com/ctk3b/msibi/issues/2
        g_r_all = None
        first_frame = 0
        max_frames = int(max_frames)
        for last_frame in range(max_frames,
                                state.traj.n_frames + max_frames,
                                max_frames):
            r, g_r = md.compute_rdf(state.traj[first_frame:last_frame],
                                    pairs, r_range= r_range, n_bins=n_bins)
            if g_r_all is None:
                g_r_all = np.zeros_like(g_r)
            g_r_all += g_r * len(state.traj[first_frame:last_frame]) / state.traj.n_frames
            first_frame = last_frame
        #r *= 10.0

        rdf = np.vstack((r, g_r)).T
        self.states[state]['current_rdf'] = rdf

        if smooth:
            current_rdf = self.states[state]['current_rdf']
            current_rdf[:, 1] = savitzky_golay(current_rdf[:, 1], 9, 2, deriv=0, rate=1)
            for row in current_rdf:
                row[1] = np.maximum(row[1], 0)

        # Compute fitness function comparing the two RDFs.
        f_fit = calc_similarity(rdf[:, 1], self.states[state]['target_rdf'][:, 1])
        self.states[state]['f_fit'].append(f_fit)
コード例 #2
0
ファイル: pair.py プロジェクト: jennyfothergill/msibi
    def _add_state(self, state, smooth=True):
        """Add a state to be used in optimizing this pair.

        Parameters
        ----------
        state : msibi.state.State
            A state object created previously.
        """
        target_rdf = self.get_state_rdf(state, query=False)
        if state._opt.smooth_rdfs:
            target_rdf[:, 1] = savitzky_golay(
                target_rdf[:, 1], 9, 2, deriv=0, rate=1
            )
            negative_idx = np.where(target_rdf < 0)
            target_rdf[negative_idx] = 0

        self._states[state] = {
            "target_rdf": target_rdf,
            "current_rdf": None,
            "alpha": state.alpha,
            "alpha_form": "linear",
            "pair_indices": None,
            "f_fit": [],
            "path": state.dir
        }
コード例 #3
0
ファイル: pair.py プロジェクト: cmelab/msibi
    def compute_current_rdf(self,
                            state,
                            r_range,
                            n_bins,
                            smooth=True,
                            max_frames=50,
                            verbose=False):
        """ """
        pairs = self.states[state]["pair_indices"]
        # TODO: More elegant way to handle units.
        #       See https://github.com/ctk3b/msibi/issues/2
        g_r_all = None
        first_frame = 0
        max_frames = int(max_frames)
        for last_frame in range(max_frames, state.traj.n_frames + max_frames,
                                max_frames):
            r, g_r = md.compute_rdf(
                state.traj[first_frame:last_frame],
                pairs,
                r_range=r_range / 10,
                n_bins=n_bins,
            )
            if g_r_all is None:
                g_r_all = np.zeros_like(g_r)
            g_r_all += (g_r * len(state.traj[first_frame:last_frame]) /
                        state.traj.n_frames)
            first_frame = last_frame
        r *= 10
        rdf = np.vstack((r, g_r_all)).T
        self.states[state]["current_rdf"] = rdf

        if smooth:
            current_rdf = self.states[state]["current_rdf"]
            current_rdf[:, 1] = savitzky_golay(current_rdf[:, 1],
                                               9,
                                               2,
                                               deriv=0,
                                               rate=1)
            for row in current_rdf:
                row[1] = np.maximum(row[1], 0)
            if verbose:  # pragma: no cover
                plt.title(f"RDF smoothing for {state.name}")
                plt.plot(r, g_r, label="unsmoothed")
                plt.plot(r, current_rdf[:, 1], label="smoothed")
                plt.legend()
                plt.show()

        # Compute fitness function comparing the two RDFs.
        f_fit = calc_similarity(rdf[:, 1], self.states[state]["target_rdf"][:,
                                                                            1])
        self.states[state]["f_fit"].append(f_fit)
コード例 #4
0
ファイル: pair.py プロジェクト: chloeoframe/msibi
    def compute_current_rdf(self,
                            state,
                            r_range,
                            n_bins,
                            smooth=True,
                            max_frames=1e3):
        """ """
        pairs = self.states[state]['pair_indices']
        # TODO: More elegant way to handle units.
        #       See https://github.com/ctk3b/msibi/issues/2
        g_r_all = None
        first_frame = 0
        max_frames = int(max_frames)
        for last_frame in range(max_frames, state.traj.n_frames + max_frames,
                                max_frames):
            r, g_r = md.compute_rdf(state.traj[first_frame:last_frame],
                                    pairs,
                                    r_range=r_range / 10,
                                    n_bins=n_bins)
            if g_r_all is None:
                g_r_all = np.zeros_like(g_r)
            g_r_all += g_r * len(
                state.traj[first_frame:last_frame]) / state.traj.n_frames
            first_frame = last_frame
        r *= 10
        rdf = np.vstack((r, g_r_all)).T
        self.states[state]['current_rdf'] = rdf

        if smooth:
            current_rdf = self.states[state]['current_rdf']
            current_rdf[:, 1] = savitzky_golay(current_rdf[:, 1],
                                               9,
                                               2,
                                               deriv=0,
                                               rate=1)
            for row in current_rdf:
                row[1] = np.maximum(row[1], 0)

        # Compute fitness function comparing the two RDFs.
        f_fit = calc_similarity(rdf[:, 1], self.states[state]['target_rdf'][:,
                                                                            1])
        self.states[state]['f_fit'].append(f_fit)
コード例 #5
0
ファイル: test_utils.py プロジェクト: jennyfothergill/msibi
def test_savitzky_golay():
    x = np.arange(0, 1, 0.01)
    y = 2 * x + 1
    y2 = savitzky_golay(y, 3, 1)
    assert y.shape == y2.shape
    assert np.allclose(y, y2)

    y = x ** 3.0
    y2 = savitzky_golay(y, 3, 1)
    assert calc_similarity(y, y2) > 0.99

    with pytest.raises(ValueError):
        y2 = savitzky_golay(y, 3.1, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 2, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 4, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 3, 3)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 3, 2)
コード例 #6
0
ファイル: test_utils.py プロジェクト: mattwthompson/msibi
def test_savitzky_golay():
    x = np.arange(0, 1, 0.01)
    y = 2 * x + 1
    y2 = savitzky_golay(y, 3, 1)
    assert y.shape == y2.shape
    assert np.allclose(y, y2)

    y = x**3.0
    y2 = savitzky_golay(y, 3, 1)
    assert calc_similarity(y, y2) > 0.99

    with pytest.raises(ValueError):
        y2 = savitzky_golay(y, 3.1, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 2, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 4, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 3, 3)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 3, 2)
コード例 #7
0
ファイル: pair.py プロジェクト: jennyfothergill/msibi
    def compute_current_rdf(self, state, smooth, verbose=False, query=True):

        rdf = self.get_state_rdf(state, query=query)
        self._states[state]["current_rdf"] = rdf

        if state._opt.smooth_rdfs:
            current_rdf = self._states[state]["current_rdf"]
            current_rdf[:, 1] = savitzky_golay(
                current_rdf[:, 1], 9, 2, deriv=0, rate=1
            )
            negative_idx = np.where(current_rdf < 0)
            current_rdf[negative_idx] = 0
            if verbose:  # pragma: no cover
                plt.title(f"RDF smoothing for {state.name}")
                plt.plot(rdf[:,0], rdf[:, 1], label="unsmoothed")
                plt.plot(rdf[:,0], current_rdf[:,1], label="smoothed")
                plt.legend()
                plt.show()

        # Compute fitness function comparing the two RDFs.
        f_fit = calc_similarity(
            rdf[:, 1], self._states[state]["target_rdf"][:, 1]
        )
        self._states[state]["f_fit"].append(f_fit)
コード例 #8
0
            r[last_bad_index + 1:last_bad_index + 3],
            potential[last_bad_index + 2:last_bad_index + 0:-1],
            fill_value='extrapolate')
    else:
        f = scipy.interpolate.interp1d(
            r[last_bad_index + 1:last_bad_index + 3],
            potential[last_bad_index + 1:last_bad_index + 3],
            fill_value='extrapolate')

    for i in np.arange(last_bad_index + 1):
        potential[i] = f(r[i])
    return potential


rdfs = glob.glob('*-flhe_nvt.txt')
kT = 2.5
r_switch = 1.1
for rdf_file in rdfs:
    filename = "-".join(rdf_file.split('-')[0:2])
    print(filename)
    rdf = np.loadtxt(rdf_file)
    dr = rdf[1, 0] - rdf[0, 0]
    potential = _invert_rdf(rdf[:, 1], kT)
    potential = tail_correction(rdf[:, 0], potential, r_switch)
    potential = _linear_head(rdf[:, 0], potential)
    forces = -1.0 * np.gradient(potential, dr)

    potential = savitzky_golay(potential, 9, 2, deriv=0, rate=1)
    np.savetxt(filename + ".pot",
               np.column_stack((rdf[:, 0], potential, forces)))