Exemplo n.º 1
0
    def update_potential(self, pot_r, r_switch=None):
        """Update the potential using all states. """
        self.previous_potential = np.copy(self.potential)
        for state in self.states:
            kT = state.kT
            alpha0 = self.states[state]['alpha']
            form = self.states[state]['alpha_form']
            alpha = alpha_array(alpha0, pot_r, form=form)

            current_rdf = self.states[state]['current_rdf'][:, 1]
            target_rdf = self.states[state]['target_rdf'][:, 1]

            # For cases where rdf_cutoff != pot_cutoff, only update the
            # potential using RDF values < pot_cutoff.
            unused_rdf_vals = current_rdf.shape[0] - self.potential.shape[0]
            if unused_rdf_vals != 0:
                current_rdf = current_rdf[:-unused_rdf_vals]
                target_rdf = target_rdf[:-unused_rdf_vals]

            # The actual IBI step.
            self.potential += (kT * alpha * np.log(current_rdf / target_rdf) /
                               len(self.states))

        # Apply corrections to ensure continuous, well-behaved potentials.
        self.potential = tail_correction(pot_r, self.potential, r_switch)
        self.potential = head_correction(pot_r, self.potential,
                self.previous_potential, self.head_correction_form)
Exemplo n.º 2
0
    def update_potential(self, pot_r, r_switch=None):
        """Update the potential using all states. """
        self.previous_potential = np.copy(self.potential)
        for state in self.states:
            kT = state.kT
            alpha0 = self.states[state]['alpha']
            form = self.states[state]['alpha_form']
            alpha = alpha_array(alpha0, pot_r, form=form)

            current_rdf = self.states[state]['current_rdf'][:, 1]
            target_rdf = self.states[state]['target_rdf'][:, 1]

            # For cases where rdf_cutoff != pot_cutoff, only update the
            # potential using RDF values < pot_cutoff.
            unused_rdf_vals = current_rdf.shape[0] - self.potential.shape[0]
            if unused_rdf_vals != 0:
                current_rdf = current_rdf[:-unused_rdf_vals]
                target_rdf = target_rdf[:-unused_rdf_vals]

            # The actual IBI step.
            self.potential += (kT * alpha * np.log(current_rdf / target_rdf) /
                               len(self.states))

        # Apply corrections to ensure continuous, well-behaved potentials.
        self.potential = tail_correction(pot_r, self.potential, r_switch)
        self.potential = head_correction(pot_r, self.potential,
                                         self.previous_potential,
                                         self.head_correction_form)
Exemplo n.º 3
0
Arquivo: pair.py Projeto: cmelab/msibi
    def update_potential(self, pot_r, r_switch=None, verbose=False):
        """Update the potential using all states. """
        self.previous_potential = np.copy(self.potential)
        for state in self.states:
            kT = state.kT
            alpha0 = self.states[state]["alpha"]
            form = self.states[state]["alpha_form"]
            alpha = alpha_array(alpha0, pot_r, form=form)

            current_rdf = self.states[state]["current_rdf"][:, 1]
            target_rdf = self.states[state]["target_rdf"][:, 1]

            # For cases where rdf_cutoff != pot_cutoff, only update the
            # potential using RDF values < pot_cutoff.
            unused_rdf_vals = current_rdf.shape[0] - self.potential.shape[0]
            if unused_rdf_vals != 0:
                current_rdf = current_rdf[:-unused_rdf_vals]
                target_rdf = target_rdf[:-unused_rdf_vals]

            if verbose:  # pragma: no cover
                plt.plot(current_rdf, label="current rdf")
                plt.plot(target_rdf, label="target rdf")
                plt.legend()
                plt.show()

            # The actual IBI step.
            self.potential += (kT * alpha * np.log(current_rdf / target_rdf) /
                               len(self.states))

            if verbose:  # pragma: no cover
                plt.plot(pot_r,
                         self.previous_potential,
                         label="previous potential")
                plt.plot(pot_r, self.potential, label="potential")
                plt.legend()
                plt.show()

        # Apply corrections to ensure continuous, well-behaved potentials.
        if verbose:  # pragma: no cover
            plt.plot(pot_r, self.potential, label="uncorrected potential")
        self.potential = tail_correction(pot_r, self.potential, r_switch)
        if verbose:  # pragma: no cover
            plt.plot(pot_r, self.potential, label="tail correction")
        self.potential = head_correction(pot_r, self.potential,
                                         self.previous_potential,
                                         self.head_correction_form)
        if verbose:  # pragma: no cover
            plt.plot(pot_r, self.potential, label="head correction")
            plt.legend()
            plt.show()
Exemplo n.º 4
0
    def initialize(self, engine='hoomd', potentials_dir=None):
        """
        Create initial table potentials and the simulation input scripts.

        Parameters
        ----------
        engine : str, optional, default='hoomd'
            Engine used to run simulations
        potentials_dir : path, optional, default="'working_dir'/potentials"
            Directory to store potential files

        """

        if not potentials_dir:
            self.potentials_dir = os.path.join(os.getcwd(), 'potentials')
        else:
            self.potentials_dir = potentials_dir

        if not os.path.isdir(self.potentials_dir):
            os.mkdir(self.potentials_dir)

        if not os.path.isdir('rdfs'):
            os.mkdir('rdfs')

        table_potentials = []
        for pair in self.pairs:
            potential_file = os.path.join(self.potentials_dir,
                                          'pot.{0}.txt'.format(pair.name))
            pair.potential_file = potential_file
            table_potentials.append((pair.type1, pair.type2, potential_file))

            V = tail_correction(self.pot_r, pair.potential, self.r_switch)
            pair.potential = V

            # This file is written for viewing of how the potential evolves.
            pair.save_table_potential(self.pot_r, self.dr, iteration=0,

                                      engine=engine)

            # This file is overwritten at each iteration and actually used for
            # performing the query simulations.
            pair.save_table_potential(self.pot_r, self.dr, engine=self.engine)

        for state in self.states:
            if self.engine == 'hoomd':
                state.save_runscript(table_potentials, table_width=len(self.pot_r),
                                    engine=self.engine)
            elif self.engine == 'lammps':
                state.save_runscript(table_potentials=table_potentials, table_width=0,
                                    engine=self.engine)
Exemplo n.º 5
0
    def initialize(self, engine="hoomd", potentials_dir=None):
        """
        Create initial table potentials and the simulation input scripts.

        Parameters
        ----------
        engine : str, optional, default='hoomd'
            Engine used to run simulations
        potentials_dir : path, optional, default="'working_dir'/potentials"
            Directory to store potential files

        """

        if not potentials_dir:
            self.potentials_dir = os.path.join(os.getcwd(), "potentials")
        else:
            self.potentials_dir = potentials_dir

        if not os.path.isdir(self.potentials_dir):
            os.mkdir(self.potentials_dir)

        if not os.path.isdir("rdfs"):
            os.mkdir("rdfs")

        table_potentials = []
        for pair in self.pairs:
            potential_file = os.path.join(self.potentials_dir,
                                          "pot.{0}.txt".format(pair.name))
            pair.potential_file = potential_file
            table_potentials.append((pair.type1, pair.type2, potential_file))

            V = tail_correction(self.pot_r, pair.potential, self.r_switch)
            pair.potential = V

            # This file is written for viewing of how the potential evolves.
            pair.save_table_potential(self.pot_r,
                                      self.dr,
                                      iteration=0,
                                      engine=engine)

            # This file is overwritten at each iteration and actually used for
            # performing the query simulations.
            pair.save_table_potential(self.pot_r, self.dr, engine=engine)

        for state in self.states:
            state.save_runscript(table_potentials,
                                 table_width=len(self.pot_r),
                                 engine=engine)
Exemplo n.º 6
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)))
Exemplo n.º 7
0
    def _initialize(self, engine, n_steps, integrator, integrator_kwargs, dt,
                    gsd_period, potentials_dir):
        """Create initial table potentials and the simulation input scripts.

        Parameters
        ----------
        engine : str, default 'hoomd'
            Engine used to run simulations
        potentials_dir : path, default None
            Directory to store potential files. If None is given, a "potentials"
            folder in the current working directory is used.
        """
        if potentials_dir is None:
            self.potentials_dir = os.path.join(os.getcwd(), "potentials")
        else:
            self.potentials_dir = potentials_dir

        if not os.path.isdir(self.potentials_dir):
            os.mkdir(self.potentials_dir)

        if not os.path.isdir("rdfs"):
            os.mkdir("rdfs")

        table_potentials = []
        bonds = None
        angles = None

        for pair in self.pairs:
            potential_file = os.path.join(self.potentials_dir,
                                          f"pot.{pair.name}.txt")
            pair.potential_file = potential_file
            table_potentials.append((pair.type1, pair.type2, potential_file))

            V = tail_correction(self.pot_r, pair.potential, self.r_switch)
            pair.potential = V

            # This file is written for viewing of how the potential evolves.
            pair.save_table_potential(self.pot_r,
                                      self.dr,
                                      iteration=0,
                                      engine=engine)

            # This file is overwritten at each iteration and actually used for
            # performing the query simulations.
            pair.save_table_potential(self.pot_r, self.dr, engine=engine)

        if self.bonds:
            bonds = self.bonds
        if self.angles:
            angles = self.angles

        for state in self.states:
            state.save_runscript(n_steps=n_steps,
                                 integrator=integrator,
                                 integrator_kwargs=integrator_kwargs,
                                 dt=dt,
                                 gsd_period=gsd_period,
                                 table_potentials=table_potentials,
                                 table_width=len(self.pot_r),
                                 engine=engine,
                                 bonds=bonds,
                                 angles=angles)
Exemplo n.º 8
0
def test_tail_correction():
    dr = 0.05
    r = np.arange(0, 2.5, dr)
    V = mie(r, 1, 1)
    smooth_V = tail_correction(r, V, r_switch=2.25)
    assert smooth_V[-1] == 0.0
Exemplo n.º 9
0
def test_tail_correction():
    dr = 0.05
    r = np.arange(0, 2.5, dr)
    V = mie(r, 1, 1)
    smooth_V = tail_correction(r, V, r_switch=2.25)
    assert smooth_V[-1] == 0.0
Exemplo n.º 10
0
    def update_potential(self, pot_r, r_switch=None, verbose=False):
        """Update the potential using all states. """
        self.previous_potential = np.copy(self.potential)
        for state in self._states:
            kT = state.kT
            alpha0 = self._states[state]["alpha"]
            form = self._states[state]["alpha_form"]
            alpha = alpha_array(alpha0, pot_r, form=form)

            current_rdf = self._states[state]["current_rdf"]
            target_rdf = self._states[state]["target_rdf"]

            # For cases where rdf_cutoff != pot_cutoff, only update the
            # potential using RDF values < pot_cutoff.
            unused_rdf_vals = current_rdf.shape[0] - self.potential.shape[0]
            if unused_rdf_vals != 0:
                current_rdf = current_rdf[:-unused_rdf_vals,:]
                target_rdf = target_rdf[:-unused_rdf_vals,:]

            if verbose:  # pragma: no cover
                plt.plot(current_rdf[:,0], current_rdf[:,1], label="current rdf")
                plt.plot(target_rdf[:,0], target_rdf[:,1], label="target rdf")
                plt.legend()
                plt.show()

            # The actual IBI step.
            self.potential += (
                    kT * alpha * np.log(current_rdf[:,1] / target_rdf[:,1]) / len(self._states)
            )

            if verbose:  # pragma: no cover
                plt.plot(
                    pot_r, self.previous_potential, label="previous potential"
                )
                plt.plot(pot_r, self.potential, label="potential")
                plt.ylim(
                    (min(self.potential[np.isfinite(self.potential)])-1,10)
                )
                plt.legend()
                plt.show()

        # Apply corrections to ensure continuous, well-behaved potentials.
        pot = self.potential
        self.potential = tail_correction(pot_r, self.potential, r_switch)
        tail = self.potential
        self.potential = head_correction(
            pot_r,
            self.potential,
            self.previous_potential,
            self.head_correction_form
        )
        head = self.potential
        if verbose:  # pragma: no cover
            plt.plot(pot_r, head, label="head correction")
            plt.plot(pot_r, pot, label="uncorrected potential")
            idx_r, _ = find_nearest(pot_r, r_switch)
            plt.plot(pot_r[idx_r:], tail[idx_r:], label="tail correction")

            plt.ylim((min(pot[np.isfinite(pot)])-1, 10))
            plt.legend()
            plt.show()