OpenMM MDAnalysis Reporter

0.1 · active · verified Fri Apr 17

The openmm-mdanalysis-reporter is a Python library that bridges OpenMM simulations with MDAnalysis. It allows users to create MDAnalysis Universe objects directly from OpenMM State objects and write trajectory data to any MDAnalysis-supported format, particularly the Universal Archive Format (UAF). The current version is 0.1, indicating an early development stage with an infrequent release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a minimal OpenMM simulation and attach the `MDAnalysisReporter` to write trajectory data. It uses a simple single-particle system for brevity. For real simulations, you would load a PDB, define forces, and set proper periodic boundary conditions. The example also shows how to optionally load the generated `.uaf` file using MDAnalysis for verification.

import openmm.app as app
import openmm as om
from openmm_mdanalysis_reporter import MDAnalysisReporter
import os

# Create a simple system (e.g., single atom, just for demonstration)
# In a real scenario, you'd load a PDB/GMX file with app.PDBFile, etc.
system = om.System()
system.addParticle(1.0) # mass in amu

# Create an integrator
integrator = om.VerletIntegrator(0.001) # 1 fs timestep

# Create a simulation object
# Use 'Reference' platform for broad compatibility without GPU
platform = om.Platform.getPlatformByName('Reference') 
simulation = app.Simulation(system, integrator, platform)
simulation.context.setPositions([[0, 0, 0]]) # Set initial position
simulation.context.setVelocitiesToTemperature(300 * om.unit.kelvin) # Set initial velocities

# Define output file path for the trajectory
output_file = "trajectory.uaf" # UAF is recommended for MDAnalysisReporter

# Add the MDAnalysisReporter
# Report every 10 steps, enforce periodic box (set to False for this non-periodic system)
# For periodic systems, ensure system.setDefaultPeriodicBoxVectors is called.
reporter = MDAnalysisReporter(output_file, 10, enforcePeriodicBox=False)
simulation.reporters.append(reporter)

print(f"Running simulation and writing to {output_file}...")
simulation.step(100) # Run for 100 steps
print("Simulation finished.")

# Optional: Load and inspect the generated trajectory with MDAnalysis
try:
    import MDAnalysis as mda
    u = mda.Universe(output_file)
    print(f"Loaded trajectory with {u.trajectory.n_frames} frames.")
    # Clean up the generated file
    os.remove(output_file)
    print(f"Cleaned up {output_file}.")
except ImportError:
    print("MDAnalysis not installed, skipping trajectory inspection and cleanup.")
except Exception as e:
    print(f"Error loading trajectory with MDAnalysis: {e}. Attempting cleanup.")
    if os.path.exists(output_file):
        os.remove(output_file) # Still try to clean up

view raw JSON →