OpenMM Python Wrapper
OpenMM is a high-performance toolkit for molecular simulation, implemented primarily in C++ with a robust Python wrapper. It enables users to perform complex molecular dynamics simulations with a focus on flexibility and performance, especially on GPUs. Currently at version 8.5.1, OpenMM maintains an active development cycle, regularly releasing updates that include performance enhancements and new features, such as expanded support for machine learning potentials in recent 8.x versions.
Common errors
-
ImportError: DLL load failed while importing _openmm: The specified module could not be found.
cause This error commonly occurs on Windows when OpenMM's C++ libraries or their dependencies are not correctly found by the Python environment, often due to issues with PATH or a corrupted `conda` environment.fixTry creating a new, clean `conda` environment and reinstalling OpenMM: `conda create -c conda-forge --name openmm_env python=3.9 openmm` then `conda activate openmm_env`. Also ensure system PATH includes necessary OpenMM library directories if installed from standalone installers. -
OpenMMException: Expected a Quantity object but got a float/int.
cause A numerical value (float or int) was provided where OpenMM expected a `openmm.unit.Quantity` object with a specified unit.fixAppend a unit from `openmm.unit` to the numerical value, e.g., change `300` to `300*unit.kelvin` or `1.0` to `1.0*unit.nanometers`. -
OpenMMException: Platform 'CUDA' is not available.
cause The OpenMM `CUDA` platform could not be initialized. This typically means NVIDIA CUDA Toolkit or compatible drivers are not installed, not correctly configured, or not visible to OpenMM.fixInstall the latest NVIDIA GPU drivers and the CUDA Toolkit appropriate for your system. Verify installation with `python -m openmm.testInstallation`.
Warnings
- breaking The `simtk` namespace was removed in OpenMM 8.x. All imports previously under `simtk.openmm` or `simtk.unit` should now be directly imported from `openmm` or `openmm.unit` respectively.
- gotcha OpenMM extensively uses a `unit` system. Omitting unit specifications (e.g., `300` instead of `300*unit.kelvin`) for physical quantities will lead to `OpenMMException` or incorrect behavior, as OpenMM expects `openmm.unit.Quantity` objects.
- gotcha For GPU acceleration (CUDA or OpenCL platforms), appropriate drivers must be installed on your system. Without them, OpenMM will fall back to the slower CPU platform or fail to initialize the specified GPU platform.
- breaking OpenMM 8.x introduced `PythonForce` to support machine learning potentials, which has higher overhead than traditional custom forces. If a force can be implemented with existing custom forces, that is generally more performant.
Install
-
pip install openmm -
conda install -c conda-forge openmm
Imports
- openmm
import openmm
- openmm.app
import simtk.openmm.app as app
from openmm.app import *
- openmm.unit
import simtk.unit as unit
from openmm.unit import *
Quickstart
import openmm.app as app
import openmm as mm
import openmm.unit as unit
# This example assumes a 'protein.pdb' file exists in the same directory.
# A minimal PDB can be generated or downloaded, e.g., from RCSB PDB (e.g., 1AKI).
# For a real simulation, ensure your PDB is properly prepared (e.g., with PDBFixer).
try:
pdb = app.PDBFile('protein.pdb')
except FileNotFoundError:
print("Error: 'protein.pdb' not found. Please provide a PDB file for the quickstart.")
exit()
# Create a force field for the system
forcefield = app.ForceField('amber14-all.xml', 'amber14/tip3pfb.xml')
# Create a system from the PDB topology and force field
system = forcefield.createSystem(pdb.topology,
nonbondedMethod=app.PME,
nonbondedCutoff=1.0*unit.nanometers,
constraints=app.HBonds)
# Create an integrator for advancing the simulation
integrator = mm.LangevinMiddleIntegrator(300*unit.kelvin, 1/unit.picosecond, 0.004*unit.picoseconds)
# Create a simulation object
simulation = app.Simulation(pdb.topology, system, integrator)
simulation.context.setPositions(pdb.positions)
# Minimize energy to relieve bad contacts
print('Minimizing energy...')
simulation.minimizeEnergy()
print(f'Potential energy after minimization: {simulation.context.getState(getEnergy=True).getPotentialEnergy()}')
# Add reporters for output
simulation.reporters.append(app.PDBReporter('output.pdb', 1000))
simulation.reporters.append(app.StateDataReporter('data.csv', 1000, step=True, potentialEnergy=True, temperature=True, separator=','))
# Run the simulation
print('Running simulation...')
simulation.step(10000) # Run 10,000 steps
print('Simulation complete. Output saved to output.pdb and data.csv')