Skip to content

Commit

Permalink
Displacement moves now accept nbr_list.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisiacovella committed Dec 28, 2023
1 parent 6edaed8 commit 038e246
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Examples/LJ_mcmove.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@

mc_move = MetropolisDisplacementMove(
seed=1234,
displacement_sigma=0.1 * unit.nanometer,
nr_of_moves=10,
displacement_sigma=0.01 * unit.nanometer,
nr_of_moves=1000,
simulation_reporter=reporter,
)

mc_move.run(sampler_state, thermodynamic_state, True)
mc_move.run(sampler_state, thermodynamic_state, nbr_list, True)
13 changes: 10 additions & 3 deletions chiron/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ def apply(
thermodynamic_state: ThermodynamicState,
sampler_state: SamplerState,
reporter: SimulationReporter,
nbr_list=None,
):
"""Apply a metropolized move to the sampler state.
Expand All @@ -388,12 +389,15 @@ def apply(
The initial sampler state to apply the move to. This is modified.
reporter: SimulationReporter
The reporter to write the data to.
nbr_list: Neighbor List or Pair List routine,
The routine to use to calculate the interacting atoms.
Default is None and will use an unoptimized pairlist without PBC
"""
import jax.numpy as jnp

# Compute initial energy
initial_energy = thermodynamic_state.get_reduced_potential(
sampler_state
sampler_state, nbr_list
) # NOTE: in kT
log.debug(f"Initial energy is {initial_energy} kT.")
# Store initial positions of the atoms that are moved.
Expand All @@ -417,7 +421,7 @@ def apply(
proposed_positions
)
proposed_energy = thermodynamic_state.get_reduced_potential(
sampler_state
sampler_state, nbr_list
) # NOTE: in kT
# Accept or reject with Metropolis criteria.
delta_energy = proposed_energy - initial_energy
Expand Down Expand Up @@ -589,14 +593,17 @@ def run(
self,
sampler_state: SamplerState,
thermodynamic_state: ThermodynamicState,
nbr_list=None,
progress_bar=True,
):
from tqdm import tqdm

for trials in (
tqdm(range(self.nr_of_moves)) if progress_bar else range(self.nr_of_moves)
):
self.apply(thermodynamic_state, sampler_state, self.simulation_reporter)
self.apply(
thermodynamic_state, sampler_state, self.simulation_reporter, nbr_list
)
if trials % 100 == 0:
log.debug(f"Acceptance rate: {self.n_accepted / self.n_proposed}")
if self.simulation_reporter is not None:
Expand Down
9 changes: 7 additions & 2 deletions chiron/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,18 @@ def are_states_compatible(cls, state1, state2):
"""
pass

def get_reduced_potential(self, sampler_state: SamplerState) -> float:
def get_reduced_potential(
self, sampler_state: SamplerState, nbr_list=None
) -> float:
"""
Compute the reduced potential for the given sampler state.
Parameters
----------
sampler_state : SamplerState
The sampler state for which to compute the reduced potential.
nbr_list : NeighborList or PairList, optional
The neighbor list or pair list routine to use for calculating the reduced potential.
Returns
-------
Expand All @@ -266,7 +270,8 @@ def get_reduced_potential(self, sampler_state: SamplerState) -> float:
log.debug(f"sample state: {sampler_state.x0}")
reduced_potential = (
unit.Quantity(
self.potential.compute_energy(sampler_state.x0), unit.kilojoule_per_mole
self.potential.compute_energy(sampler_state.x0, nbr_list),
unit.kilojoule_per_mole,
)
) / unit.AVOGADRO_CONSTANT_NA
log.debug(f"reduced potential: {reduced_potential}")
Expand Down

0 comments on commit 038e246

Please sign in to comment.