Skip to content

anneal #

Perform annealing of the temperature and/or global context parameters.

Functions:

  • anneal_temperature

    Gradually ramp the system temperature from a starting value to the final value.

  • anneal_state

    Gradually anneal from an initial state (i.e. set of global context parameters)

anneal_temperature #

anneal_temperature(
    simulation: Simulation,
    temperature_initial: Quantity,
    temperature_final: Quantity,
    n_steps: int,
    frequency: int,
)

Gradually ramp the system temperature from a starting value to the final value.

Parameters:

  • simulation (Simulation) –

    The current simulation.

  • temperature_initial (Quantity) –

    The initial temperature.

  • temperature_final (Quantity) –

    The final temperature.

  • n_steps (int) –

    The number of steps to anneal over.

  • frequency (int) –

    The frequency at which to increment the temperature.

Source code in femto/md/anneal.py
def anneal_temperature(
    simulation: openmm.app.Simulation,
    temperature_initial: openmm.unit.Quantity,
    temperature_final: openmm.unit.Quantity,
    n_steps: int,
    frequency: int,
):
    """Gradually ramp the system temperature from a starting value to the final value.

    Args:
        simulation: The current simulation.
        temperature_initial: The initial temperature.
        temperature_final: The final temperature.
        n_steps: The number of steps to anneal over.
        frequency: The frequency at which to increment the temperature.
    """

    n_increments = n_steps // frequency
    increment = (temperature_final - temperature_initial) / n_increments

    temperature = temperature_initial
    _set_temperature(simulation.integrator, temperature)

    for _ in range(n_increments):
        simulation.step(frequency)

        temperature += increment
        _set_temperature(simulation.integrator, temperature)

    _set_temperature(simulation.integrator, temperature_final)

anneal_state #

anneal_state(
    simulation: Simulation,
    state_initial: dict[str, float],
    state_final: dict[str, float],
    n_steps: int,
    frequency: int,
)

Gradually anneal from an initial state (i.e. set of global context parameters) to a final one.

Parameters:

  • simulation (Simulation) –

    The current simulation.

  • state_initial (dict[str, float]) –

    The initial state.

  • state_final (dict[str, float]) –

    The final state.

  • n_steps (int) –

    The number of steps to anneal over.

  • frequency (int) –

    The frequency at which to update the state.

Source code in femto/md/anneal.py
def anneal_state(
    simulation: openmm.app.Simulation,
    state_initial: dict[str, float],
    state_final: dict[str, float],
    n_steps: int,
    frequency: int,
):
    """Gradually anneal from an initial state (i.e. set of global context parameters)
    to a final one.

    Args:
        simulation: The current simulation.
        state_initial: The initial state.
        state_final: The final state.
        n_steps: The number of steps to anneal over.
        frequency: The frequency at which to update the state.
    """

    n_increments = n_steps // frequency

    state_initial = femto.md.utils.openmm.evaluate_ctx_parameters(
        state_initial, simulation.system
    )
    state_final = femto.md.utils.openmm.evaluate_ctx_parameters(
        state_final, simulation.system
    )

    state = {**state_initial}

    increments = {
        key: (state_final[key] - state_initial[key]) / n_increments
        for key in state_initial
    }

    for key in state:
        simulation.context.setParameter(key, state[key])

    for _ in range(n_increments):
        simulation.step(frequency)

        for key in state:
            state[key] += increments[key]
            simulation.context.setParameter(key, state[key])

    for key in state:
        simulation.context.setParameter(key, state_final[key])