OpenMM Python Wrapper

8.5.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic molecular dynamics simulation using OpenMM. It loads a PDB file, applies a force field to create a system, sets up a Langevin integrator, performs energy minimization, and then runs a short simulation, saving trajectory and state data. A `protein.pdb` file is required in the execution directory for this example to run.

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')

view raw JSON →