Skip to content

Commit 7fb45d8

Browse files
robinzybnjzjz
authored andcommitted
add decrecaption warning when dpdata throws errors while parsing cp2k (#558)
Squashed commit of the following: commit dfb6191 Merge: 1b55e6c 5028af6 Author: Yongbin Zhuang <[email protected]> Date: Mon Oct 30 22:02:15 2023 +0100 add decrecaption warning when dpdata throws errors while parsing cp2k (#558) commit 5028af6 Author: robinzyb <[email protected]> Date: Mon Oct 30 21:28:21 2023 +0100 raise PendingDeprecationWarning commit 3ee373d Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon Oct 30 19:51:57 2023 +0000 [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci commit cec3fc2 Author: robinzyb <[email protected]> Date: Mon Oct 30 20:51:26 2023 +0100 add exact except for the cp2k plugin. commit 7a09854 Author: robinzyb <[email protected]> Date: Mon Oct 30 20:38:23 2023 +0100 fixed typo in checking cp2k pattern match. commit 1a4b985 Author: robinzyb <[email protected]> Date: Mon Oct 30 14:34:50 2023 +0100 update error for cp2k aimdoutput if none pattern is matched commit a1a171a Merge: 621d686 1b55e6c Author: Yongbin Zhuang <[email protected]> Date: Mon Oct 30 13:59:10 2023 +0100 Merge branch 'deepmodeling:devel' into devel commit 621d686 Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri Oct 20 22:53:54 2023 +0000 [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci commit 03cc010 Author: robinzyb <[email protected]> Date: Sat Oct 21 00:50:51 2023 +0200 add decrecaption warning when dpdata throws errors while parsing cp2k commit 0576449 Merge: 4840bbc dbe2bc9 Author: Yongbin Zhuang <[email protected]> Date: Sat Oct 21 00:30:18 2023 +0200 Merge branch 'deepmodeling:devel' into devel commit 4840bbc Author: robinzyb <[email protected]> Date: Thu Sep 7 14:12:19 2023 +0200 Update README.md for recommendation of using cp2kdata
1 parent 1b55e6c commit 7fb45d8

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

dpdata/cp2k/output.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ def __next__(self):
7676
def get_log_block_generator(self):
7777
lines = []
7878
delimiter_flag = False
79+
yield_flag = False
7980
while True:
8081
line = self.log_file_object.readline()
8182
if line:
8283
lines.append(line)
8384
if any(p.match(line) for p in delimiter_patterns):
8485
if delimiter_flag is True:
86+
yield_flag = True
8587
yield lines
8688
lines = []
8789
delimiter_flag = False
@@ -91,17 +93,23 @@ def get_log_block_generator(self):
9193
if any(p.match(line) for p in avail_patterns):
9294
delimiter_flag = True
9395
else:
96+
if not yield_flag:
97+
raise StopIteration("None of the delimiter patterns are matched")
9498
break
9599
if delimiter_flag is True:
96100
raise RuntimeError("This file lacks some content, please check")
97101

98102
def get_xyz_block_generator(self):
99103
p3 = re.compile(r"^\s*(\d+)\s*")
104+
yield_flag = False
100105
while True:
101106
line = self.xyz_file_object.readline()
102107
if not line:
108+
if not yield_flag:
109+
raise StopIteration("None of the xyz patterns are matched")
103110
break
104111
if p3.match(line):
112+
yield_flag = True
105113
atom_num = int(p3.match(line).group(1))
106114
lines = []
107115
lines.append(line)

dpdata/plugins/cp2k.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,48 @@
44
from dpdata.cp2k.output import Cp2kSystems
55
from dpdata.format import Format
66

7+
string_warning = """
8+
Hi, you got an error from dpdata,
9+
please check if your cp2k files include full information,
10+
otherwise its version is not supported by dpdata.
11+
Try use dpdata plugin from cp2kdata package,
12+
for details, please refer to
13+
https://robinzyb.github.io/cp2kdata/
14+
"""
15+
716

817
@Format.register("cp2k/aimd_output")
918
class CP2KAIMDOutputFormat(Format):
1019
def from_labeled_system(self, file_name, restart=False, **kwargs):
1120
xyz_file = sorted(glob.glob(f"{file_name}/*pos*.xyz"))[0]
1221
log_file = sorted(glob.glob(f"{file_name}/*.log"))[0]
13-
return tuple(Cp2kSystems(log_file, xyz_file, restart))
22+
try:
23+
return tuple(Cp2kSystems(log_file, xyz_file, restart))
24+
except (StopIteration, RuntimeError) as e:
25+
# StopIteration is raised when pattern match is failed
26+
raise PendingDeprecationWarning(string_warning) from e
1427

1528

1629
@Format.register("cp2k/output")
1730
class CP2KOutputFormat(Format):
1831
def from_labeled_system(self, file_name, restart=False, **kwargs):
19-
data = {}
20-
(
21-
data["atom_names"],
22-
data["atom_numbs"],
23-
data["atom_types"],
24-
data["cells"],
25-
data["coords"],
26-
data["energies"],
27-
data["forces"],
28-
tmp_virial,
29-
) = dpdata.cp2k.output.get_frames(file_name)
30-
if tmp_virial is not None:
31-
data["virials"] = tmp_virial
32-
return data
32+
try:
33+
data = {}
34+
(
35+
data["atom_names"],
36+
data["atom_numbs"],
37+
data["atom_types"],
38+
data["cells"],
39+
data["coords"],
40+
data["energies"],
41+
data["forces"],
42+
tmp_virial,
43+
) = dpdata.cp2k.output.get_frames(file_name)
44+
if tmp_virial is not None:
45+
data["virials"] = tmp_virial
46+
return data
47+
# TODO: in the future, we should add exact error type here
48+
# TODO: when pattern match is failed
49+
# TODO: For now just use RuntimeError as a placeholder.
50+
except RuntimeError as e:
51+
raise PendingDeprecationWarning(string_warning) from e

0 commit comments

Comments
 (0)