Skip to content

Examples

Marcus Wieder edited this page Jan 24, 2024 · 5 revisions

Chiron can be used to perform Langevin dynamics simulations of systems, use different MCMC Moves to target the Boltzmann distribution more efficiently and use different enhanced sampling methods to converge faster. In the following examples are given to outline its functionality.

Langevin dynamics of a test system in vacuum

In this example, a Lennard-Jones fluid is simulated, and the trajectory is reported. We start by importing the potential (this is a custom potential using the familiar 12-6 Lennard-Jones potential and the LennardJonesFluid testsystem from openmmtools.

from chiron.potential import LJPotential
from openmmtools.testsystems import LennardJonesFluid

lj_fluid = LennardJonesFluid(reduced_density=0.8, n_particles=100)
lj_potential = LJPotential(lj_fluid.topology)

We need to initialize the pseudo-random number generator. Next, we import the ThermodynamicState and SamplerState, which define the probability density of the MCMC chain and the current state of the sampler.

# set the seed of the pseudo-random number generator
from chiron.utils import PRNG
PRNG.set_seed(1234)

# set up the thermodynamic and sampler state. the thermodynamic state contains 
# all information about the probability density that is targeted, and the sampler state defines 
# the state of the sampler (initially, only the starting positions and a seed to initialize a 
# new random number stream)
  
from chiron.states import SamplerState, ThermodynamicState

thermodynamic_state = ThermodynamicState(
        potential=potential, temperature=300 * unit.kelvin
    )

sampler_state = SamplerState(testsystem.positions, PRNG.get_random_key())

In the next code block, the Langevin Integrator and its reporter are set up (which will automatically write properties of interest). The reporter will automatically write in the directory specified in the BaseReporter.set_directory. The LangevinIntegrator consumes an instance of the SamplerState and ThermodynamicState and returns a new SamplerState.

from chiron.reporters import LangevinDynamicsReporter
from chiron.reporters import BaseReporter

# set up reporter directory
BaseReporter.set_directory(prep_temp_dir.join("langevin_dymamics"))
reporter = LangevinDynamicsReporter()

report_frequency = 100 # report every nth integration step
n_steps = 10_000 # simulation for nth steps (with timestep=1 fs)

integrator = LangevinIntegrator(reporter=reporter, report_frequency=report_frequency)
sampler_state = integrator.run(
        sampler_state,
        thermodynamic_state,
        n_steps=n_steps,
    )

Free energy difference of harmonic oscillators

Clone this wiki locally