Skip to content

Writing a modification

A modification is a subclass of BaseModification with a NAME and an apply method that changes an OpenMM System. This one scales the masses of the alchemical atoms:

from typing import List

import openmm as mm

from fes_ml.alchemical.modifications.base_modification import BaseModification, Phase


class MassScalingModification(BaseModification):
    NAME = "MassScaling"
    phase = Phase.PARAMETERS
    requires_lambda = True  # a schedule entry without a value is an error

    def apply(self, system: mm.System, alchemical_atoms: List[int], lambda_value: float,
              factor: float = 2.0, *args, **kwargs) -> mm.System:
        for index in alchemical_atoms:
            mass = system.getParticleMass(index)
            system.setParticleMass(index, mass * (1 + (factor - 1) * lambda_value))
        return system

apply receives the System, the atoms of its group, its λ value, its options from modifications_kwargs, and the inputs it declares. It must accept **kwargs: the strategies pass extra keyword arguments.

Registering it

In a script, register the class before creating the states:

from fes_ml.alchemical import Alchemist

Alchemist.register_modification("MassScaling", MassScalingModification)

In a package, use an entry point instead, so that fes-ml finds it on import:

[project.entry-points."alchemical.modifications"]
MassScaling = "my_package.mass_scaling:MassScalingModification"

Then use it like any built-in: {"MassScaling": [0.0, 0.5, 1.0]}, "MassScaling:ligand", and options in modifications_kwargs["MassScaling"].

Dependencies and inputs

  • List modifications that must run before or after yours in pre_dependencies and post_dependencies, and ones that must not run with it in excludes; see groups and dependencies.
  • If apply needs data that only the creation strategy has, declare it: inputs = ("topology",). The strategy then passes it as a keyword argument, computed once per group. Both strategies provide topology (OpenMM), mols (Sire), parm7 and mm_charges; the OpenFF strategy also provides positions, topology_off, original_offxml, openmm_charges, and top_file/crd_file when EMLE needs them.
  • To let a Context change your λ without rebuilding the System, keep λ in an OpenMM global parameter named by self.lambda_parameter and set the class attribute LAMBDA_PARAMETER (for example "lambda_mass"); named groups get the suffix _<group> automatically.
  • In a group, self.alchemical_group is the group name and self.alchemical_context gives access to the other groups' modifications (see LJSoftCoreModification for an example).

Testing it

Test against an independent reference, not against fes-ml itself: a closed-form energy, or plain OpenMM on the unmodified system with the parameters changed by hand. tests/reference/oracle.py has helpers for this. See CONTRIBUTING.