Skip to content

Commit 40354a0

Browse files
committed
add reading restarted cp2k aimd output and fix precision problem
1 parent ee8fc06 commit 40354a0

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

dpdata/cp2k/output.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,25 @@ class Cp2kSystems(object):
2121
"""
2222
deal with cp2k outputfile
2323
"""
24-
def __init__(self, log_file_name, xyz_file_name):
24+
def __init__(self, log_file_name, xyz_file_name, restart=False):
2525
self.log_file_object = open(log_file_name, 'r')
2626
self.xyz_file_object = open(xyz_file_name, 'r')
2727
self.log_block_generator = self.get_log_block_generator()
2828
self.xyz_block_generator = self.get_xyz_block_generator()
29-
self.restart_flag = False
30-
self.restart_flag_count = 0
29+
self.restart_flag = restart
3130
self.cell=None
31+
32+
if self.restart_flag:
33+
self.handle_single_log_frame(next(self.log_block_generator))
34+
35+
3236
def __del__(self):
3337
self.log_file_object.close()
3438
self.xyz_file_object.close()
3539
def __iter__(self):
3640
return self
3741
def __next__(self):
3842

39-
if self.restart_flag_count == 0:
40-
if self.check_restart():
41-
info_dict = {}
42-
self.restart_flag_count += 1
43-
log_info_dict = self.handle_single_log_frame(next(self.log_block_generator))
44-
4543
info_dict = {}
4644
log_info_dict = self.handle_single_log_frame(next(self.log_block_generator))
4745
xyz_info_dict = self.handle_single_xyz_frame(next(self.xyz_block_generator))
@@ -51,22 +49,11 @@ def __next__(self):
5149
assert all(eq1), (log_info_dict,xyz_info_dict,'There may be errors in the file')
5250
assert all(eq2), (log_info_dict,xyz_info_dict,'There may be errors in the file')
5351
assert all(eq3), (log_info_dict,xyz_info_dict,'There may be errors in the file')
54-
assert log_info_dict['energies']-xyz_info_dict['energies'] < 1E-4, (log_info_dict['energies'],xyz_info_dict['energies'],'There may be errors in the file')
52+
assert abs(log_info_dict['energies']-xyz_info_dict['energies'])<1E-4, (log_info_dict['energies'],xyz_info_dict['energies'],'There may be errors in the file')
5553
info_dict.update(log_info_dict)
5654
info_dict.update(xyz_info_dict)
5755
return info_dict
5856

59-
def check_restart(self):
60-
restart_patterns = re.compile(r'^ \*\s+ RESTART INFORMATION')
61-
restart_flag = False
62-
for i in range(0, 10):
63-
line = self.log_file_object.readline()
64-
if restart_patterns.match(line):
65-
restart_flag = True
66-
break
67-
68-
return restart_flag
69-
7057
def get_log_block_generator(self):
7158
lines = []
7259
delimiter_flag = False
@@ -130,7 +117,6 @@ def handle_single_log_frame(self, lines):
130117
force_lines.append(line)
131118
if energy_pattern_1.match(line):
132119
energy = float(energy_pattern_1.match(line).groupdict()['number']) * AU_TO_EV
133-
#print('1',float(energy_pattern_1.match(line).groupdict()['number']))
134120
#print('1to', energy)
135121
if energy_pattern_2.match(line):
136122
energy = float(energy_pattern_2.match(line).groupdict()['number']) * AU_TO_EV

dpdata/system.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ def __init__ (self,
10271027
- ``gaussian/md``: gaussian ab initio molecular dynamics
10281028
- ``cp2k/output``: cp2k output file
10291029
- ``cp2k/aimd_output``: cp2k aimd output dir(contains *pos*.xyz and *.log file)
1030+
- ``cp2k/restart_aimd_output``: cp2k restart aimd output dir(contains *pos*.xyz and *.log file)
10301031
- ``pwmat/movement``: pwmat md output file
10311032
- ``pwmat/out.mlmd``: pwmat scf output file
10321033
@@ -1091,10 +1092,18 @@ def has_virial(self) :
10911092
return ('virials' in self.data)
10921093

10931094
@register_from_funcs.register_funcs('cp2k/aimd_output')
1094-
def from_cp2k_aimd_output(self, file_dir):
1095+
def from_cp2k_aimd_output(self, file_dir, restart=False):
10951096
xyz_file=sorted(glob.glob("{}/*pos*.xyz".format(file_dir)))[0]
10961097
log_file=sorted(glob.glob("{}/*.log".format(file_dir)))[0]
1097-
for info_dict in Cp2kSystems(log_file, xyz_file):
1098+
for info_dict in Cp2kSystems(log_file, xyz_file, restart):
1099+
l = LabeledSystem(data=info_dict)
1100+
self.append(l)
1101+
1102+
@register_from_funcs.register_funcs('cp2k/restart_aimd_output')
1103+
def from_cp2k_aimd_output(self, file_dir, restart=True):
1104+
xyz_file = sorted(glob.glob("{}/*pos*.xyz".format(file_dir)))[0]
1105+
log_file = sorted(glob.glob("{}/*.log".format(file_dir)))[0]
1106+
for info_dict in Cp2kSystems(log_file, xyz_file, restart):
10981107
l = LabeledSystem(data=info_dict)
10991108
self.append(l)
11001109

tests/cp2k/restart_aimd/CH4-pos-1.xyz

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
5
2+
i = 6, time = 3.000, E = -8.0715742982
3+
H 5.3857900416 4.0742097705 3.5811120132
4+
H 3.9199512866 4.7900150110 4.3285090053
5+
H 5.6002847426 5.6077224621 4.4496425595
6+
H 5.3479559215 4.1480369706 5.3848299963
7+
C 5.0220694622 4.6784611518 4.4577943630
8+
5
9+
i = 7, time = 3.500, E = -8.0711045977
10+
H 5.3848978757 4.0776224380 3.5814113731
11+
H 3.9208809250 4.7887262191 4.3220837446
12+
H 5.6114006618 5.6027985800 4.4525980866
13+
H 5.3547898958 4.1463117535 5.3804926360
14+
C 5.0205568908 4.6788387910 4.4584233243
15+
5
16+
i = 8, time = 4.000, E = -8.0707608306
17+
H 5.3835297528 4.0817338403 3.5830596000
18+
H 3.9234320791 4.7879031911 4.3168681896
19+
H 5.6214054135 5.5987495057 4.4552276547
20+
H 5.3599417537 4.1448123874 5.3752072465
21+
C 5.0191820720 4.6790259231 4.4589444566
22+
5
23+
i = 9, time = 4.500, E = -8.0705194159
24+
H 5.3817673668 4.0863967978 3.5858600160
25+
H 3.9273863696 4.7876326936 4.3129364024
26+
H 5.6300924745 5.5954927653 4.4575136286
27+
H 5.3633363130 4.1434413688 5.3691425751
28+
C 5.0179801128 4.6790427107 4.4593553989
29+
5
30+
i = 10, time = 5.000, E = -8.0703422670
31+
H 5.3797263472 4.0914106472 3.5895251624
32+
H 3.9324173150 4.7880020164 4.3103298482
33+
H 5.6372698671 5.5929282695 4.4594480504
34+
H 5.3649502724 4.1420701065 5.3625404656
35+
C 5.0169867769 4.6789178539 4.4596571950

0 commit comments

Comments
 (0)