lmmolecule guide
lmmolecule embeds OpenMM 8.2.0 behind a deterministic LMScript schema so molecular dynamics simulations, custom Lepton potentials, and structural analyses run as self-contained scripts without Python or external force-field databases.
Overview
An lmmolecule program builds a physical model through the global md context in three stages: configure the execution platform (Reference double precision or SIMD CPU), assemble particles and forces on a System object, and instantiate a Sim context to minimize energy, integrate trajectories, and sample observables.
Unit conventions
Every method in lmmolecule uses OpenMM's coherent molecular dynamics unit system:
- Length, coordinates, box dimensions, and cutoffs: nanometers (1 nm = 10 Angstroms).
- Time and integration timestep dt: picoseconds (1 ps = 1000 fs).
- Mass: atomic mass units (amu, numerically equal to g/mol).
- Velocity: nm/ps (1 nm/ps = 1000 m/s).
- Energy (ke, pe, energy, epsilon): kJ/mol, where 1 amu * (nm/ps)^2 = 1 kJ/mol and kB = 0.008314462618 kJ/(mol*K).
- Force (fx, fy, fz): kJ/(mol*nm).
- Charge: elementary charge e, with 1/(4*pi*eps0) = 138.935457644382 kJ*nm/(mol*e^2).
- Angle and dihedral phase: radians.
- Temperature and pressure: kelvin (K) and bar.
Building lattices, gases, and rigid water
System provides built-in geometry generators for common condensed-matter benchmarks as well as file loaders:
- sys.fcc(cells, a, mass) builds a face-centered cubic crystal of 4 * cells^3 atoms with lattice constant a (nm) and sets the periodic box to cells * a.
- sys.rocksalt(cells, a, mass_a, mass_b, q) builds an 8 * cells^3 two-species NaCl-type rock-salt crystal with alternating +q and -q charges.
- sys.gas(n, box_len, mass, seed) places n particles at random non-overlapping positions inside a cubic box.
- sys.water(model, x, y, z) and sys.water_box(model, n, spacing, seed) create rigid TIP3P or SPC/E water molecules with intramolecular exclusions and SETTLE distance constraints already applied.
- sys.load_xyz(path, mass) and sys.load_pdb(path) load external coordinates; load_pdb converts Angstrom coordinates to nanometers, reads CRYST1 box dimensions, and assigns atomic masses from a 42-element table.
let sys = md.system()
.water_box("tip3p", 6, 0.31, 42)
.coulomb("pme", 0.9)
.dispersion_correction(true);
Forces and custom Lepton potentials
Standard bonded and nonbonded interactions are configured directly on System with bond, angle, torsion, constrain, lj, lj_particle, charge, coulomb, dielectric, dispersion_correction, and exclude. When analytic potentials outside standard harmonic or Lennard-Jones 12-6 forms are needed, OpenMM's embedded Lepton compiler evaluates custom energy expressions:
- sys.custom_external(expr, params) applies an external potential function of particle coordinates x, y, and z (for example harmonic traps or walls).
- sys.custom_bond(i, j, expr) applies a pair bond potential function of interatomic distance r (plus pi and e), for example a Morse potential 120*(1-exp(-12*(r-0.16)))^2.
- sys.custom_nonbonded(expr, params, cutoff) applies a custom pairwise nonbonded interaction function of distance r across all non-excluded pairs within cutoff.
Before optimization, lmmolecule validates every Lepton expression tree and rejects non-finite constant sub-expressions (such as 1/0 or sqrt(-1)).
Integrators, thermostats, barostats, and reproducibility
Call sys.sim(integrator, dt, temp_K, friction, seed) to compile the system into an OpenMM Context:
- Use "verlet" for NVE microcanonical dynamics, or combine "verlet" with sys.andersen(temp_K, rate, seed) for Andersen collision thermostatting.
- Use "langevin" for NVT canonical dynamics via OpenMM's BAOAB LangevinMiddleIntegrator with friction in 1/ps.
- Add sys.barostat(pressure_bar, temp_K, freq, seed) before sys.sim to sample the NPT isothermal-isobaric ensemble via Monte Carlo volume moves.
By default, lmmolecule runs on OpenMM's double-precision Reference platform. Calling md.platform("cpu") switches subsequent simulations to the SIMD CPU platform with DeterministicForces enabled; md.threads(1) keeps single-thread CPU runs byte-identical across reruns on the same machine.
Structural and transport observables
In addition to instantaneous ke(), pe(), energy(), temperature(), dof(), box_volume(), distance(i, j), angle_val(i, j, k), and per-particle x/y/z, vx/vy/vz, and fx/fy/fz accessors, Sim includes built-in condensed-matter analysis methods:
- sim.mark() records current unwrapped particle positions, and sim.msd() returns the mean squared displacement (in nm^2) of mobile particles since mark(), enabling direct Einstein diffusion calculations D = MSD / (6 * t).
- sim.rdf(path, rmax, bins, frames, stride) and sim.rdf_pair(path, sym_a, sym_b, rmax, bins, frames, stride) advance the simulation across frames snapshots separated by stride steps and write the normalized radial distribution function g(r) to path.