Skip to content

Commit 52b6eaa

Browse files
Add from gaussian gjf method (#452)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4d9359a commit 52b6eaa

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

dpdata/gaussian/gjf.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Generate Gaussian input file."""
55

66
import itertools
7+
import re
78
import uuid
89
import warnings
910
from typing import List, Optional, Tuple, Union
@@ -272,3 +273,68 @@ def make_gaussian_input(
272273
)
273274
buff.append("\n")
274275
return "\n".join(buff)
276+
277+
278+
def read_gaussian_input(inp: str):
279+
"""Read Gaussian input.
280+
281+
Parameters
282+
----------
283+
inp : str
284+
Gaussian input str
285+
286+
Returns
287+
-------
288+
dict
289+
system data
290+
"""
291+
flag = 0
292+
coords = []
293+
elements = []
294+
cells = []
295+
for line in inp.split("\n"):
296+
if not line.strip():
297+
# empty line
298+
flag += 1
299+
elif flag == 0:
300+
# keywords
301+
if line.startswith("#"):
302+
# setting
303+
keywords = line.split()
304+
elif line.startswith("%"):
305+
pass
306+
elif flag == 1:
307+
# title
308+
pass
309+
elif flag == 2:
310+
# multi and coords
311+
s = line.split()
312+
if len(s) == 2:
313+
pass
314+
elif len(s) == 4:
315+
if s[0] == "TV":
316+
cells.append(list(map(float, s[1:4])))
317+
else:
318+
# element
319+
elements.append(re.sub("\\(.*?\\)|\\{.*?}|\\[.*?]", "", s[0]))
320+
coords.append(list(map(float, s[1:4])))
321+
elif flag == 3:
322+
# end
323+
break
324+
atom_names, atom_types, atom_numbs = np.unique(
325+
elements, return_inverse=True, return_counts=True
326+
)
327+
if len(cells):
328+
nopbc = False
329+
else:
330+
nopbc = True
331+
cells = np.array([np.eye(3)]) * 100
332+
return {
333+
"atom_names": list(atom_names),
334+
"atom_numbs": list(atom_numbs),
335+
"atom_types": atom_types,
336+
"cells": np.array(cells).reshape(1, 3, 3),
337+
"nopbc": nopbc,
338+
"coords": np.array(coords).reshape(1, -1, 3),
339+
"orig": np.zeros(3),
340+
}

dpdata/plugins/gaussian.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ def from_labeled_system(self, file_name, **kwargs):
2727
class GaussiaGJFFormat(Format):
2828
"""Gaussian input file."""
2929

30+
def from_system(self, file_name: str, **kwargs):
31+
"""Read Gaussian input file.
32+
33+
Parameters
34+
----------
35+
file_name : str
36+
file name
37+
**kwargs : dict
38+
keyword arguments
39+
"""
40+
with open(file_name) as fp:
41+
text = fp.read()
42+
return dpdata.gaussian.gjf.read_gaussian_input(text)
43+
3044
def to_system(self, data: dict, file_name: str, **kwargs):
3145
"""Generate Gaussian input file.
3246

tests/test_gaussian_gjf.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import unittest
33

4+
from comp_sys import CompSys
45
from context import dpdata
56

67

@@ -11,3 +12,16 @@ def setUp(self):
1112
def test_dump_gaussian_gjf(self):
1213
self.system.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*")
1314
os.remove("tmp.gjf")
15+
16+
17+
class TestGaussianGJFComp(unittest.TestCase, CompSys):
18+
def setUp(self):
19+
self.system_1 = dpdata.LabeledSystem(
20+
"poscars/OUTCAR.h2o.md", fmt="vasp/outcar"
21+
)[0]
22+
self.system_1.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*")
23+
self.system_2 = dpdata.System(
24+
"tmp.gjf", fmt="gaussian/gjf", type_map=self.system_1.get_atom_names()
25+
)
26+
os.remove("tmp.gjf")
27+
self.places = 6

0 commit comments

Comments
 (0)