2
2
import numpy as np
3
3
import re
4
4
from collections import OrderedDict
5
+
6
+ from scipy .constants .constants import R
5
7
from .cell import cell_to_low_triangle
6
8
from ..unit import EnergyConversion , LengthConversion , ForceConversion , PressureConversion
7
9
@@ -29,6 +31,8 @@ def __init__(self, log_file_name, xyz_file_name, restart=False):
29
31
self .xyz_block_generator = self .get_xyz_block_generator ()
30
32
self .restart_flag = restart
31
33
self .cell = None
34
+ self .print_level = None
35
+ self .atomic_kinds = None
32
36
33
37
if self .restart_flag :
34
38
self .handle_single_log_frame (next (self .log_block_generator ))
@@ -43,13 +47,14 @@ def __iter__(self):
43
47
def __next__ (self ):
44
48
info_dict = {}
45
49
log_info_dict = self .handle_single_log_frame (next (self .log_block_generator ))
50
+ #print(log_info_dict)
46
51
xyz_info_dict = self .handle_single_xyz_frame (next (self .xyz_block_generator ))
47
- eq1 = [v1 == v2 for v1 ,v2 in zip (log_info_dict ['atom_numbs' ], xyz_info_dict ['atom_numbs' ])]
48
- eq2 = [v1 == v2 for v1 ,v2 in zip (log_info_dict ['atom_names' ], xyz_info_dict ['atom_names' ])]
49
- eq3 = [v1 == v2 for v1 ,v2 in zip (log_info_dict ['atom_types' ], xyz_info_dict ['atom_types' ])]
50
- assert all (eq1 ), (log_info_dict ,xyz_info_dict ,'There may be errors in the file. If it is a restart task; use restart=True' )
51
- assert all (eq2 ), (log_info_dict ,xyz_info_dict ,'There may be errors in the file. If it is a restart task; use restart=True' )
52
- assert all (eq3 ), (log_info_dict ,xyz_info_dict ,'There may be errors in the file. If it is a restart task; use restart=True' )
52
+ # eq1 = [v1==v2 for v1,v2 in zip(log_info_dict['atom_numbs'], xyz_info_dict['atom_numbs'])]
53
+ # eq2 = [v1==v2 for v1,v2 in zip(log_info_dict['atom_names'], xyz_info_dict['atom_names'])]
54
+ # eq3 = [v1==v2 for v1,v2 in zip(log_info_dict['atom_types'], xyz_info_dict['atom_types'])]
55
+ # assert all(eq1), (log_info_dict,xyz_info_dict,'There may be errors in the file. If it is a restart task; use restart=True')
56
+ # assert all(eq2), (log_info_dict,xyz_info_dict,'There may be errors in the file. If it is a restart task; use restart=True')
57
+ # assert all(eq3), (log_info_dict,xyz_info_dict,'There may be errors in the file. If it is a restart task; use restart=True')
53
58
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
59
info_dict .update (log_info_dict )
55
60
info_dict .update (xyz_info_dict )
@@ -103,11 +108,18 @@ def handle_single_log_frame(self, lines):
103
108
cell_angle_pattern = re .compile (r' INITIAL CELL ANGLS\[deg\]\s+=\s+(?P<alpha>\S+)\s+(?P<beta>\S+)\s+(?P<gamma>\S+)' )
104
109
cell_A , cell_B , cell_C = (0 ,0 ,0 ,)
105
110
cell_alpha , cell_beta , cell_gamma = (0 ,0 ,0 ,)
111
+ cell_a_pattern = re .compile (r' CELL\| Vector a \[angstrom\]:\s+(?P<ax>\S+)\s+(?P<ay>\S+)\s+(?P<az>\S+)' )
112
+ cell_b_pattern = re .compile (r' CELL\| Vector b \[angstrom\]:\s+(?P<bx>\S+)\s+(?P<by>\S+)\s+(?P<bz>\S+)' )
113
+ cell_c_pattern = re .compile (r' CELL\| Vector c \[angstrom\]:\s+(?P<cx>\S+)\s+(?P<cy>\S+)\s+(?P<cz>\S+)' )
106
114
force_start_pattern = re .compile (r' ATOMIC FORCES in' )
107
115
force_flag = False
108
116
force_end_pattern = re .compile (r' SUM OF ATOMIC FORCES' )
109
117
force_lines = []
110
118
cell_flag = 0
119
+ print_level_pattern = re .compile (r' GLOBAL\| Global print level\s+(?P<print_level>\S+)' )
120
+ print_level_flag = 0
121
+ atomic_kinds_pattern = re .compile (r'\s+\d+\. Atomic kind:\s+(?P<akind>\S+)' )
122
+ atomic_kinds = []
111
123
for line in lines :
112
124
if force_start_pattern .match (line ):
113
125
force_flag = True
@@ -131,9 +143,47 @@ def handle_single_log_frame(self, lines):
131
143
cell_beta = np .deg2rad (float (cell_angle_pattern .match (line ).groupdict ()['beta' ]))
132
144
cell_gamma = np .deg2rad (float (cell_angle_pattern .match (line ).groupdict ()['gamma' ]))
133
145
cell_flag += 1
146
+ if print_level_pattern .match (line ):
147
+ print_level = print_level_pattern .match (line ).groupdict ()['print_level' ]
148
+ print_level_flag += 1
149
+ if cell_a_pattern .match (line ):
150
+ cell_ax = float (cell_a_pattern .match (line ).groupdict ()['ax' ])
151
+ cell_ay = float (cell_a_pattern .match (line ).groupdict ()['ay' ])
152
+ cell_az = float (cell_a_pattern .match (line ).groupdict ()['az' ])
153
+ cell_flag += 1
154
+ if cell_b_pattern .match (line ):
155
+ cell_bx = float (cell_b_pattern .match (line ).groupdict ()['bx' ])
156
+ cell_by = float (cell_b_pattern .match (line ).groupdict ()['by' ])
157
+ cell_bz = float (cell_b_pattern .match (line ).groupdict ()['bz' ])
158
+ cell_flag += 1
159
+ if cell_c_pattern .match (line ):
160
+ cell_cx = float (cell_c_pattern .match (line ).groupdict ()['cx' ])
161
+ cell_cy = float (cell_c_pattern .match (line ).groupdict ()['cy' ])
162
+ cell_cz = float (cell_c_pattern .match (line ).groupdict ()['cz' ])
163
+ cell_flag += 1
164
+
165
+ if atomic_kinds_pattern .match (line ):
166
+ akind = atomic_kinds_pattern .match (line ).groupdict ()['akind' ]
167
+ atomic_kinds .append (akind )
168
+ if print_level_flag == 1 :
169
+ self .print_level = print_level
170
+ if print_level == 'LOW' :
171
+ raise RuntimeError ("please provide cp2k output with higher print level(at least MEDIUM)" )
172
+
173
+
134
174
if cell_flag == 2 :
135
175
self .cell = cell_to_low_triangle (cell_A ,cell_B ,cell_C ,
136
176
cell_alpha ,cell_beta ,cell_gamma )
177
+ elif cell_flag == 5 :
178
+ self .cell = np .asarray (
179
+ [
180
+ [cell_ax , cell_ay , cell_az ],
181
+ [cell_bx , cell_by , cell_bz ],
182
+ [cell_cx , cell_cy , cell_cz ]]
183
+ ).astype ('float32' )
184
+ if atomic_kinds :
185
+ self .atomic_kinds = atomic_kinds
186
+ #print(self.atomic_kinds)
137
187
# lx = cell_A
138
188
# xy = cell_B * np.cos(cell_gamma)
139
189
# xz = cell_C * np.cos(cell_beta)
@@ -146,27 +196,32 @@ def handle_single_log_frame(self, lines):
146
196
147
197
element_index = - 1
148
198
element_dict = OrderedDict ()
149
- atom_types_list = []
199
+ atom_types_idx_list = []
150
200
forces_list = []
151
201
for line in force_lines [3 :]:
152
202
line_list = line .split ()
153
- if element_dict .get (line_list [2 ]):
154
- element_dict [line_list [2 ]][1 ]+= 1
203
+ #print(line_list)
204
+ if element_dict .get (line_list [1 ]):
205
+ element_dict [line_list [1 ]][1 ]+= 1
155
206
else :
156
207
element_index += 1
157
- element_dict [line_list [2 ]]= [element_index ,1 ]
158
- atom_types_list .append (element_dict [line_list [2 ]][0 ])
208
+ element_dict [line_list [1 ]]= [element_index ,1 ]
209
+ atom_types_idx_list .append (element_dict [line_list [1 ]][0 ])
159
210
forces_list .append ([float (line_list [3 ])* AU_TO_EV_EVERY_ANG ,
160
211
float (line_list [4 ])* AU_TO_EV_EVERY_ANG ,
161
212
float (line_list [5 ])* AU_TO_EV_EVERY_ANG ])
162
-
163
- atom_names = list (element_dict .keys ())
213
+ #print(atom_types_idx_list)
214
+ #atom_names=list(element_dict.keys())
215
+ atom_names = self .atomic_kinds
164
216
atom_numbs = []
165
- for ii in atom_names :
217
+
218
+ for ii in element_dict .keys ():
166
219
atom_numbs .append (element_dict [ii ][1 ])
220
+ #print(atom_numbs)
167
221
info_dict ['atom_names' ] = atom_names
168
222
info_dict ['atom_numbs' ] = atom_numbs
169
- info_dict ['atom_types' ] = np .asarray (atom_types_list )
223
+ info_dict ['atom_types' ] = np .asarray (atom_types_idx_list )
224
+ info_dict ['print_level' ] = self .print_level
170
225
info_dict ['cells' ] = np .asarray ([self .cell ]).astype ('float32' )
171
226
info_dict ['energies' ] = np .asarray ([energy ]).astype ('float32' )
172
227
info_dict ['forces' ] = np .asarray ([forces_list ]).astype ('float32' )
@@ -208,9 +263,9 @@ def handle_single_xyz_frame(self, lines):
208
263
atom_numbs = []
209
264
for ii in atom_names :
210
265
atom_numbs .append (element_dict [ii ][1 ])
211
- info_dict ['atom_names' ] = atom_names
212
- info_dict ['atom_numbs' ] = atom_numbs
213
- info_dict ['atom_types' ] = np .asarray (atom_types_list )
266
+ # info_dict['atom_names'] = atom_names
267
+ # info_dict['atom_numbs'] = atom_numbs
268
+ # info_dict['atom_types'] = np.asarray(atom_types_list)
214
269
info_dict ['coords' ] = np .asarray ([coords_list ]).astype ('float32' )
215
270
info_dict ['energies' ] = np .array ([energy ]).astype ('float32' )
216
271
info_dict ['orig' ]= [0 ,0 ,0 ]
0 commit comments