From 62d7113fa8fc6b064453bf3b4e3fa7fca291d945 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 17:20:32 -0400 Subject: [PATCH 01/10] checkpoint --- dpdata/gaussian/gjf.py | 65 ++++++++++++++++++++++++++++++++++++++ dpdata/plugins/gaussian.py | 12 +++++++ 2 files changed, 77 insertions(+) diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index c2051761c..9ef747155 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -272,3 +272,68 @@ def make_gaussian_input( ) buff.append("\n") return "\n".join(buff) + + +def read_gaussian_input(inp: str): + """Read Gaussian input. + + Parameters + ---------- + inp : str + Gaussian input str + + Returns + ------- + dict + system data + """ + flag = 0 + coords = [] + elements = [] + cells = [] + for line in inp.split("\n"): + if not line.strip(): + # empty line + flag += 1 + elif flag == 0: + # keywords + if line.startswith("#"): + # setting + keywords = line.split() + elif line.startswith("%"): + pass + elif flag == 1: + # title + pass + elif flag == 2: + # multi and coords + s = line.split() + if len(s) == 2: + pass + elif len(s) == 4: + if s[0] == "TV": + cells.append(list(map(float, s[1:4]))) + else: + # element + elements.append(re.sub(u"\\(.*?\\)|\\{.*?}|\\[.*?]", "", s[0])) + coords.append(list(map(float, s[1:4]))) + elif flag == 3: + # end + break + atom_names, atom_types, atom_numbs = np.unique( + elements, return_inverse=True, return_counts=True + ) + if len(cells): + nopbc = False + else: + nopbc = True + cells = np.array([np.eye(3)]) * 100 + return { + "atom_names": list(atom_names), + "atom_numbs": list(atom_numbs), + "atom_types": atom_types, + "cells": cells, + "nopbc": nopbc, + "coords": coords.reshape(1, -1, 3), + "orig": np.zeros(3), + } diff --git a/dpdata/plugins/gaussian.py b/dpdata/plugins/gaussian.py index 1ea097cc0..c26a658b5 100644 --- a/dpdata/plugins/gaussian.py +++ b/dpdata/plugins/gaussian.py @@ -26,6 +26,18 @@ def from_labeled_system(self, file_name, **kwargs): @Format.register("gaussian/gjf") class GaussiaGJFFormat(Format): """Gaussian input file.""" + def from_system(self, file_name: str, **kwargs): + """Read Gaussian input file. + + Parameters + ---------- + file_name : str + file name + """ + with open(file_name) as fp: + text = fp.read() + return dpdata.gaussian.gjf.read_gaussian_input(text) + def to_system(self, data: dict, file_name: str, **kwargs): """Generate Gaussian input file. From a1478bdcf8386ea748c69e0bcbd028bfa13a45bf Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 17:23:44 -0400 Subject: [PATCH 02/10] add tests --- tests/test_gaussian_gjf.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_gaussian_gjf.py b/tests/test_gaussian_gjf.py index 861eae287..dc08cf3fb 100644 --- a/tests/test_gaussian_gjf.py +++ b/tests/test_gaussian_gjf.py @@ -2,6 +2,7 @@ import unittest from context import dpdata +from comp_sys import CompSys class TestGaussianGJF(unittest.TestCase): @@ -11,3 +12,12 @@ def setUp(self): def test_dump_gaussian_gjf(self): self.system.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*") os.remove("tmp.gjf") + + +class TestGaussianGJFComp(unittest.TestCase, CompSys): + def setUp(self): + self.system_1 = dpdata.LabeledSystem("poscars/OUTCAR.h2o.md", fmt="vasp/outcar") + self.system_2.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*") + self.system_2 = dpdata.System("tmp.gjf", fmt="gaussian/gjf") + os.remove("tmp.gjf") + self.places = 6 From 5024722dd614bef73f07808a3d9c4f3cfa6c512a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Apr 2023 22:00:50 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/gaussian/gjf.py | 6 +++--- dpdata/plugins/gaussian.py | 4 ++-- tests/test_gaussian_gjf.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index 9ef747155..aa182cd66 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -276,12 +276,12 @@ def make_gaussian_input( def read_gaussian_input(inp: str): """Read Gaussian input. - + Parameters ---------- inp : str Gaussian input str - + Returns ------- dict @@ -315,7 +315,7 @@ def read_gaussian_input(inp: str): cells.append(list(map(float, s[1:4]))) else: # element - elements.append(re.sub(u"\\(.*?\\)|\\{.*?}|\\[.*?]", "", s[0])) + elements.append(re.sub("\\(.*?\\)|\\{.*?}|\\[.*?]", "", s[0])) coords.append(list(map(float, s[1:4]))) elif flag == 3: # end diff --git a/dpdata/plugins/gaussian.py b/dpdata/plugins/gaussian.py index c26a658b5..6b4380d60 100644 --- a/dpdata/plugins/gaussian.py +++ b/dpdata/plugins/gaussian.py @@ -26,9 +26,10 @@ def from_labeled_system(self, file_name, **kwargs): @Format.register("gaussian/gjf") class GaussiaGJFFormat(Format): """Gaussian input file.""" + def from_system(self, file_name: str, **kwargs): """Read Gaussian input file. - + Parameters ---------- file_name : str @@ -38,7 +39,6 @@ def from_system(self, file_name: str, **kwargs): text = fp.read() return dpdata.gaussian.gjf.read_gaussian_input(text) - def to_system(self, data: dict, file_name: str, **kwargs): """Generate Gaussian input file. diff --git a/tests/test_gaussian_gjf.py b/tests/test_gaussian_gjf.py index dc08cf3fb..20a02c8f4 100644 --- a/tests/test_gaussian_gjf.py +++ b/tests/test_gaussian_gjf.py @@ -1,8 +1,8 @@ import os import unittest -from context import dpdata from comp_sys import CompSys +from context import dpdata class TestGaussianGJF(unittest.TestCase): From a17834dc562b0bfc929ed60cc1cb0a6f0f951177 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 18:08:05 -0400 Subject: [PATCH 04/10] fix typo --- tests/test_gaussian_gjf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_gaussian_gjf.py b/tests/test_gaussian_gjf.py index 20a02c8f4..daa823f58 100644 --- a/tests/test_gaussian_gjf.py +++ b/tests/test_gaussian_gjf.py @@ -17,7 +17,7 @@ def test_dump_gaussian_gjf(self): class TestGaussianGJFComp(unittest.TestCase, CompSys): def setUp(self): self.system_1 = dpdata.LabeledSystem("poscars/OUTCAR.h2o.md", fmt="vasp/outcar") - self.system_2.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*") + self.system_1.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*") self.system_2 = dpdata.System("tmp.gjf", fmt="gaussian/gjf") os.remove("tmp.gjf") self.places = 6 From eff92c08a2c70706d89e9b38add45d9b86e81135 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 18:45:39 -0400 Subject: [PATCH 05/10] Import re --- dpdata/gaussian/gjf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index aa182cd66..68a2cd52e 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -4,6 +4,7 @@ """Generate Gaussian input file.""" import itertools +import re import uuid import warnings from typing import List, Optional, Tuple, Union From f3902106d6ff206f0858c81b9d611c1ce3d67135 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 18:54:39 -0400 Subject: [PATCH 06/10] Fix gjf.py --- dpdata/gaussian/gjf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index 68a2cd52e..76ace76dc 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -335,6 +335,6 @@ def read_gaussian_input(inp: str): "atom_types": atom_types, "cells": cells, "nopbc": nopbc, - "coords": coords.reshape(1, -1, 3), + "coords": np.array(coords).reshape(1, -1, 3), "orig": np.zeros(3), } From 77e4f695ff64d17afbc6c6d5f0c8b5dcf2a6c3e2 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 18:59:12 -0400 Subject: [PATCH 07/10] Fix gjf.py --- dpdata/gaussian/gjf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index 76ace76dc..21300a60b 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -333,7 +333,7 @@ def read_gaussian_input(inp: str): "atom_names": list(atom_names), "atom_numbs": list(atom_numbs), "atom_types": atom_types, - "cells": cells, + "cells": np.array(cells).reshape(1, 3, 3), "nopbc": nopbc, "coords": np.array(coords).reshape(1, -1, 3), "orig": np.zeros(3), From 2167e5472cdf4437ff0c0bd2fb92d4616cad26f9 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 20:19:29 -0400 Subject: [PATCH 08/10] Fix test_gaussian_gjf.py --- tests/test_gaussian_gjf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_gaussian_gjf.py b/tests/test_gaussian_gjf.py index daa823f58..5a58b020f 100644 --- a/tests/test_gaussian_gjf.py +++ b/tests/test_gaussian_gjf.py @@ -16,8 +16,8 @@ def test_dump_gaussian_gjf(self): class TestGaussianGJFComp(unittest.TestCase, CompSys): def setUp(self): - self.system_1 = dpdata.LabeledSystem("poscars/OUTCAR.h2o.md", fmt="vasp/outcar") + self.system_1 = dpdata.LabeledSystem("poscars/OUTCAR.h2o.md", fmt="vasp/outcar")[0] self.system_1.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*") - self.system_2 = dpdata.System("tmp.gjf", fmt="gaussian/gjf") + self.system_2 = dpdata.System("tmp.gjf", fmt="gaussian/gjf", type_map=self.system_1.get_atom_names()) os.remove("tmp.gjf") self.places = 6 From 057cefb742d74e2d29b79567605cb6d3f457866c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Apr 2023 00:19:57 +0000 Subject: [PATCH 09/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_gaussian_gjf.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_gaussian_gjf.py b/tests/test_gaussian_gjf.py index 5a58b020f..2e5f4ea8f 100644 --- a/tests/test_gaussian_gjf.py +++ b/tests/test_gaussian_gjf.py @@ -16,8 +16,12 @@ def test_dump_gaussian_gjf(self): class TestGaussianGJFComp(unittest.TestCase, CompSys): def setUp(self): - self.system_1 = dpdata.LabeledSystem("poscars/OUTCAR.h2o.md", fmt="vasp/outcar")[0] + self.system_1 = dpdata.LabeledSystem( + "poscars/OUTCAR.h2o.md", fmt="vasp/outcar" + )[0] self.system_1.to_gaussian_gjf("tmp.gjf", keywords="force b3lyp/6-31g*") - self.system_2 = dpdata.System("tmp.gjf", fmt="gaussian/gjf", type_map=self.system_1.get_atom_names()) + self.system_2 = dpdata.System( + "tmp.gjf", fmt="gaussian/gjf", type_map=self.system_1.get_atom_names() + ) os.remove("tmp.gjf") self.places = 6 From c644fa644aa61d8e8909cfa7d71b57d9a4ec3416 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 14 Apr 2023 20:50:40 -0400 Subject: [PATCH 10/10] Update gaussian.py --- dpdata/plugins/gaussian.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dpdata/plugins/gaussian.py b/dpdata/plugins/gaussian.py index 6b4380d60..a22ce8630 100644 --- a/dpdata/plugins/gaussian.py +++ b/dpdata/plugins/gaussian.py @@ -34,6 +34,8 @@ def from_system(self, file_name: str, **kwargs): ---------- file_name : str file name + **kwargs : dict + keyword arguments """ with open(file_name) as fp: text = fp.read()