Skip to content

Instantly share code, notes, and snippets.

@acruzpr
Forked from andersx/pbconstraint.py
Created January 24, 2019 02:39
Show Gist options
  • Save acruzpr/a6c69636d246452e4fbcd3468b2ddb37 to your computer and use it in GitHub Desktop.
Save acruzpr/a6c69636d246452e4fbcd3468b2ddb37 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