Skip to content

Instantly share code, notes, and snippets.

@andersx
Created December 4, 2013 09:33
Show Gist options
  • Save andersx/7784817 to your computer and use it in GitHub Desktop.
Save andersx/7784817 to your computer and use it in GitHub Desktop.
Constraint optimization in Python with Open Babel
import openbabel as ob
# Standard openbabel molecule load
conv = ob.OBConversion()
conv.SetInAndOutFormats('xyz','xyz')
mol = obOBMol()
conv.ReadFile(mol,'my_mol.xyz')
# Define constraints
constraints = ob.OBFFConstraints()
constraints.AddDistanceConstraint(1, 10, 3.4) # Angstroms
constraints.AddAngleConstraint(1, 2, 3, 120.0) # Degrees
constraints.AddTorsionConstraint(1, 2, 3, 4, 180.0) # Degrees
# Setup the force field with the constraints
forcefield = ob.OBForceField.FindForceField("MMFF94")
forcefield.Setup(mol, constraints)
forcefield.SetConstraints(constraints)
# Do a 500 steps conjugate gradient minimiazation
# and save the coordinates to mol.
forcefield.ConjugateGradients(500)
forcefield.GetCoordinates(mol)
# Write the mol to a file
conv.WriteFile(mol,'my_mol.xyz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment