Created
May 4, 2018 21:32
-
-
Save ghutchis/0ecd97aac76b1b3cf99d7113ffb9e376 to your computer and use it in GitHub Desktop.
Force field energies and gradients using Open Babel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import sys | |
import os | |
import pybel | |
if __name__ == "__main__": | |
# Syntax script.py filename.mol2 | |
# read molecule | |
if (sys.argv) != 2: | |
exit | |
filename = sys.argv[1] | |
extension = os.path.splitext(filename)[1] | |
# read a single molecule from the supplied file | |
# turn this into a while loop if you want all molecules in a file | |
mol = next(pybel.readfile(extension[1:], filename)) | |
# global variable for the MMFF94 force field | |
ff = pybel._forcefields["mmff94"] | |
if ff.Setup(mol.OBMol) is False: | |
print("Cannot set up force field, exiting") | |
exit() | |
print("Current energy {}".format(ff.Energy()) ) | |
# see about gradients | |
for atom in mol.atoms: | |
# vector objects have to be de-referenced individually (sigh) | |
coords = atom.vector | |
grad = ff.GetGradient(atom.OBAtom) | |
print(grad.GetX(), grad.GetY(), grad.GetZ(), sep=',') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This needs openbabel/openbabel#1833 first.