Skip to content

Commit ee8fc06

Browse files
committed
fix reading restarted cp2k aimd and precision problem
1 parent cf4c99b commit ee8fc06

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

dpdata/cp2k/output.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
delimiter_patterns.append(delimiter_p1)
1515
delimiter_patterns.append(delimiter_p2)
1616
avail_patterns = []
17-
1817
avail_patterns.append(re.compile(r'^ INITIAL POTENTIAL ENERGY'))
1918
avail_patterns.append(re.compile(r'^ ENSEMBLE TYPE'))
2019

@@ -27,13 +26,22 @@ def __init__(self, log_file_name, xyz_file_name):
2726
self.xyz_file_object = open(xyz_file_name, 'r')
2827
self.log_block_generator = self.get_log_block_generator()
2928
self.xyz_block_generator = self.get_xyz_block_generator()
29+
self.restart_flag = False
30+
self.restart_flag_count = 0
3031
self.cell=None
3132
def __del__(self):
3233
self.log_file_object.close()
3334
self.xyz_file_object.close()
3435
def __iter__(self):
3536
return self
3637
def __next__(self):
38+
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+
3745
info_dict = {}
3846
log_info_dict = self.handle_single_log_frame(next(self.log_block_generator))
3947
xyz_info_dict = self.handle_single_xyz_frame(next(self.xyz_block_generator))
@@ -43,11 +51,22 @@ def __next__(self):
4351
assert all(eq1), (log_info_dict,xyz_info_dict,'There may be errors in the file')
4452
assert all(eq2), (log_info_dict,xyz_info_dict,'There may be errors in the file')
4553
assert all(eq3), (log_info_dict,xyz_info_dict,'There may be errors in the file')
46-
assert log_info_dict['energies']==xyz_info_dict['energies'], (log_info_dict['energies'],xyz_info_dict['energies'],'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')
4755
info_dict.update(log_info_dict)
4856
info_dict.update(xyz_info_dict)
4957
return info_dict
5058

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+
5170
def get_log_block_generator(self):
5271
lines = []
5372
delimiter_flag = False
@@ -111,6 +130,8 @@ def handle_single_log_frame(self, lines):
111130
force_lines.append(line)
112131
if energy_pattern_1.match(line):
113132
energy = float(energy_pattern_1.match(line).groupdict()['number']) * AU_TO_EV
133+
#print('1',float(energy_pattern_1.match(line).groupdict()['number']))
134+
#print('1to', energy)
114135
if energy_pattern_2.match(line):
115136
energy = float(energy_pattern_2.match(line).groupdict()['number']) * AU_TO_EV
116137
if cell_length_pattern.match(line):
@@ -159,9 +180,9 @@ def handle_single_log_frame(self, lines):
159180
info_dict['atom_names'] = atom_names
160181
info_dict['atom_numbs'] = atom_numbs
161182
info_dict['atom_types'] = np.asarray(atom_types_list)
162-
info_dict['cells'] = np.asarray([self.cell]).astype('float32')
163-
info_dict['energies'] = np.asarray([energy]).astype('float32')
164-
info_dict['forces'] = np.asarray([forces_list]).astype('float32')
183+
info_dict['cells'] = np.asarray([self.cell]).astype('float64')
184+
info_dict['energies'] = np.asarray([energy]).astype('float64')
185+
info_dict['forces'] = np.asarray([forces_list]).astype('float64')
165186
return info_dict
166187

167188
def handle_single_xyz_frame(self, lines):
@@ -203,8 +224,8 @@ def handle_single_xyz_frame(self, lines):
203224
info_dict['atom_names'] = atom_names
204225
info_dict['atom_numbs'] = atom_numbs
205226
info_dict['atom_types'] = np.asarray(atom_types_list)
206-
info_dict['coords'] = np.asarray([coords_list]).astype('float32')
207-
info_dict['energies'] = np.array([energy]).astype('float32')
227+
info_dict['coords'] = np.asarray([coords_list]).astype('float64')
228+
info_dict['energies'] = np.array([energy]).astype('float64')
208229
info_dict['orig']=[0,0,0]
209230
return info_dict
210231

0 commit comments

Comments
 (0)