Skip to content

Instantly share code, notes, and snippets.

@kzfm
Created November 6, 2010 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kzfm/665286 to your computer and use it in GitHub Desktop.
Save kzfm/665286 to your computer and use it in GitHub Desktop.
GAMESS
import openbabel as ob
from tempfile import mkstemp, mkdtemp
from os import removedirs, unlink, system, environ
import re
import os.path
import string
from random import choice
def randstr(n):
"""ランダムなファイル名を生成するため"""
return u''.join(choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') for i in xrange(n))
class Gamess(object):
def __init__(self):
self.tempdir = mkdtemp()
self.gamess = "/Users/kzfm/gamess/rungms"
self.jobname = ''
self.cwd = os.getcwd()
def run(self, mol, runtype="energy", basis="6-31G"):
self.jobname = randstr(6)
obc = ob.OBConversion()
obc.SetInFormat("gamout")
gamin = self.write_file(mol, runtype=runtype, basis=basis)
gamout = self.tempdir + "/" + self.jobname + ".out"
os.chdir(self.tempdir)
os.system("%s %s> %s 2> /dev/null" % (self.gamess, self.jobname, gamout))
# エラーが出たのでstringを渡したらなおった
# TypeError: in method 'OBConversion_ReadFile', argument 3 of type 'std::string'
new_mol = ob.OBMol()
s = open(gamout).read()
next = obc.ReadString(new_mol, s)
os.chdir(self.cwd)
unlink(gamin)
unlink(gamout)
return new_mol
def optimize(self, mol, basis="6-31G"):
return self.run(mol, runtype="optimize",basis=basis)
def singlepoint_energy(self, mol, basis="6-31G"):
return self.run(mol, runtype="energy",basis=basis)
def header(self, runtype="optimize", basis="6-31G"):
basis_section = " $BASIS GBASIS=N31 NGAUSS=6 NDFUNC=1 $END\n"
runtype_section = """ $CONTRL SCFTYP=RHF RUNTYP=OPTIMIZE $END
$STATPT OPTTOL=0.0001 NSTEP=20 $END"""
if basis in ["sto3g", "STO-3G"]:
basis_section = " $BASIS GBASIS=STO NGAUSS=3 $END\n"
elif basis in ["631g", "6-31G(d)", "6-31G"]:
basis_section = " $BASIS GBASIS=N31 NGAUSS=6 NDFUNC=1 $END\n"
elif basis in ["631gdp","6-31G(d,p)"]:
basis_section = " $BASIS GBASIS=N31 NGAUSS=6 NDFUNC=1 NPFUNC=1 $END\n"
elif basis in["631+gdp", "6-31G+(d,p)"]:
basis_section = " $BASIS GBASIS=N31 NGAUSS=6 NDFUNC=1 NPFUNC=1 DIFFSP=.TRUE. $END\n"
if runtype == "energy":
runtype_section = """ $CONTRL SCFTYP=RHF RUNTYP=ENERGY ECP=SBK $END"""
section = basis_section + runtype_section
return section
def write_file(self, mol, runtype="energy", basis="6-31G"):
obc = ob.OBConversion()
obc.SetOutFormat("gamin")
gamess_input_file = self.tempdir + "/" + self.jobname +".inp"
gamess_input_str = obc.WriteString(mol)
h = self.header(runtype=runtype, basis=basis)
ng = gamess_input_str.replace(" $CONTRL COORD=CART UNITS=ANGS $END",h)
with open(gamess_input_file, "w") as f:
f.write(ng)
return gamess_input_file
def __del__(self):
#print "remove " + self.tempdir
removedirs(self.tempdir)
if __name__ == '__main__':
g = Gamess()
obc = ob.OBConversion()
obc.SetInFormat("mol")
mol = ob.OBMol()
next = obc.ReadFile(mol,"test.mol")
newmol = g.run(mol)
print [(obatom.GetIdx(), obatom.GetType(), obatom.GetPartialCharge()) for obatom in ob.OBMolAtomIter(newmol)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment