Skip to content

Commit

Permalink
Merge pull request #1111 from JSelf42/master
Browse files Browse the repository at this point in the history
Added PCM and Solvent sections to QChem input and tests
  • Loading branch information
shyuep committed Apr 18, 2018
2 parents 7e0daf4 + 8b81fdb commit 69ae21a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
58 changes: 55 additions & 3 deletions pymatgen/io/qchem_io/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Classes for reading/manipulating/writing QChem ouput files.
"""

__author__ = "Brandon Wood, Samuel Blau, Shyam Dwaraknath"
__author__ = "Brandon Wood, Samuel Blau, Shyam Dwaraknath, Julian Self"
__copyright__ = "Copyright 2018, The Materials Project"
__version__ = "0.1"
__email__ = "b.wood@berkeley.edu"
Expand Down Expand Up @@ -42,10 +42,12 @@ class QCInput(MSONable):
Ex. opt = {"CONSTRAINT": ["tors 2 3 4 5 25.0", "tors 2 5 7 9 80.0"], "FIXED": ["2 XY"]}
"""

def __init__(self, molecule, rem, opt=None):
def __init__(self, molecule, rem, opt=None, pcm=None, solvent=None):
self.molecule = molecule
self.rem = rem
self.opt = opt
self.pcm = pcm
self.solvent = solvent

def __str__(self):
combined_list = []
Expand All @@ -59,6 +61,14 @@ def __str__(self):
if self.opt:
combined_list.append(self.opt_template(self.opt))
combined_list.append("")
# pcm section
if self.pcm:
combined_list.append(self.pcm_template(self.pcm))
combined_list.append("")
# solvent section
if self.solvent:
combined_list.append(self.solvent_template(self.solvent))
combined_list.append("")
return '\n'.join(combined_list)

@staticmethod
Expand All @@ -78,9 +88,15 @@ def from_string(cls, string):
rem = cls.read_rem(string)
# only molecule and rem are necessary everything else is checked
opt = None
pcm = None
solvent = None
if "opt" in sections:
opt = cls.read_opt(string)
return cls(molecule, rem, opt=opt)
if "pcm" in sections:
pcm = cls.read_pcm(string)
if "solvent" in sections:
solvent = cls.read_solvent(string)
return cls(molecule, rem, opt=opt, pcm=pcm, solvent=solvent)

def write_file(self, filename):
with open(filename, 'w') as f:
Expand Down Expand Up @@ -148,6 +164,24 @@ def opt_template(opt):
opt_list.append("$end")
return '\n'.join(opt_list)

@staticmethod
def pcm_template(pcm):
pcm_list = []
pcm_list.append("$pcm")
for key, value in pcm.items():
pcm_list.append(" {key} = {value}".format(key=key, value=value))
pcm_list.append("$end")
return '\n'.join(pcm_list)

@staticmethod
def solvent_template(solvent):
solvent_list = []
solvent_list.append("$solvent")
for key, value in solvent.items():
solvent_list.append(" {key} = {value}".format(key=key, value=value))
solvent_list.append("$end")
return '\n'.join(solvent_list)

@staticmethod
def find_sections(string):
patterns = {"sections": r"^\s*?\$([a-z]+)", "multiple_jobs": r"(@@@)"}
Expand Down Expand Up @@ -236,3 +270,21 @@ def read_opt(string):
string, header_pattern=cc_header, row_pattern=cc_row, footer_pattern=cc_footer)
opt["CONNECT"] = [val[0] for val in cc_table[0]]
return opt

@staticmethod
def read_pcm(string):
header = r"^\s*\$pcm"
row = r"\s*(\S+)\s+=?\s+(\S+)"
footer = r"^\s*\$end"
pcm_table = read_table_pattern(string, header_pattern=header, row_pattern=row, footer_pattern=footer)
pcm = {key: val for key, val in pcm_table[0]}
return pcm

@staticmethod
def read_solvent(string):
header = r"^\s*\$solvent"
row = r"\s*(\S+)\s+=?\s+(\S+)"
footer = r"^\s*\$end"
solvent_table = read_table_pattern(string, header_pattern=header, row_pattern=row, footer_pattern=footer)
solvent = {key: val for key, val in solvent_table[0]}
return solvent
45 changes: 44 additions & 1 deletion pymatgen/io/qchem_io/tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pymatgen.util.testing import PymatgenTest
from pymatgen.io.qchem_io.inputs import QCInput

__author__ = "Brandon Wood, Samuel Blau, Shyam Dwaraknath"
__author__ = "Brandon Wood, Samuel Blau, Shyam Dwaraknath, Julian Self"
__copyright__ = "Copyright 2018, The Materials Project"
__version__ = "0.1"
__email__ = "b.wood@berkeley.edu"
Expand Down Expand Up @@ -88,6 +88,22 @@ def test_opt_template(self):
$end"""
self.assertEqual(opt_actual, opt_test)

def test_pcm_template(self):
pcm_params = {"theory": "cpcm"}
pcm_test = QCInput.pcm_template(pcm_params)
pcm_actual = """$pcm
theory = cpcm
$end"""
self.assertEqual(pcm_actual, pcm_test)

def test_solvent_template(self):
solvent_params = {"dielectric": "5.0"}
solvent_test = QCInput.solvent_template(solvent_params)
solvent_actual = """$solvent
dielectric = 5.0
$end"""
self.assertEqual(solvent_actual, solvent_test)

def test_find_sections(self):
str_single_job_input = """$molecule
0 1
Expand Down Expand Up @@ -473,6 +489,33 @@ def test_from_multi_jobs_file(self):
self.assertEqual(molecule_2_actual, job_list_test[1].molecule)
self.assertEqual(rem_2_actual, job_list_test[1].rem)

def test_read_pcm(self):
str_pcm = """I'm once again trying to break you!
$pcm
theory = cpcm
radii = uff
vdwscale = 1.1
$end"""
pcm_test = QCInput.read_pcm(str_pcm)
pcm_actual = {
"theory": "cpcm",
"radii": "uff",
"vdwscale": "1.1"
}
self.assertDictEqual(pcm_actual, pcm_test)

def test_read_solvent(self):
str_solvent = """Once again, I'm trying to break you!
$solvent
dielectric = 5.0
$end"""
solvent_test = QCInput.read_solvent(str_solvent)
solvent_actual = {
"dielectric": "5.0",
}
self.assertDictEqual(solvent_actual, solvent_test)

if __name__ == "__main__":
unittest.main()

0 comments on commit 69ae21a

Please sign in to comment.