Skip to content

Instantly share code, notes, and snippets.

@ghutchis
Created October 9, 2018 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghutchis/4b52ef8ccd97270d120e1ef1b54144af to your computer and use it in GitHub Desktop.
Save ghutchis/4b52ef8ccd97270d120e1ef1b54144af to your computer and use it in GitHub Desktop.
Calculation dipole moments using Open Babel in Python
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import math
import pybel
# You can get a list of charge models
# using `obabel -L charges`
methods = [ 'mmff94', 'gasteiger', 'eem2015bm' ]
for argument in sys.argv[1:]:
filename, extension = os.path.splitext(argument)
# read the molecule from the supplied file
for mol in pybel.readfile(extension[1:], argument):
for method in methods:
# on the command-line, you can get partial charges like this
# obabel CHF3.g03 -onul --partialcharge gasteiger --print
cm = ob.OBChargeModel_FindType(method)
cm.ComputeCharges(mol.OBMol)
# but in C++ or Python we can get the dipole moment vector
dipole = cm.GetDipoleMoment(mol.OBMol)
moment = math.sqrt(dipole.GetX()**2+dipole.GetY()**2+dipole.GetZ()**2)
print(filename, method, moment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment