@@ -42,6 +42,9 @@ def parse_cp2k_output_advanced(fstring): # pylint: disable=too-many-locals, too
4242 bohr2ang = 0.529177208590000
4343
4444 for i_line , line in enumerate (lines ):
45+ if line .startswith (' CP2K| version string:' ):
46+ cp2k_version = float (line .split ()[5 ])
47+ result_dict ['cp2k_version' ] = cp2k_version
4548 if line .startswith (' ENERGY| ' ):
4649 energy = float (line .split ()[8 ])
4750 result_dict ['energy' ] = energy
@@ -58,7 +61,7 @@ def parse_cp2k_output_advanced(fstring): # pylint: disable=too-many-locals, too
5861 if "ABORT" in line :
5962 result_dict ["aborted" ] = True
6063 if "KPOINTS| Band Structure Calculation" in line :
61- kpoints , labels , bands = _parse_bands (lines , i_line )
64+ kpoints , labels , bands = _parse_bands (lines , i_line , cp2k_version )
6265 result_dict ["kpoint_data" ] = {
6366 "kpoints" : kpoints ,
6467 "labels" : labels ,
@@ -282,8 +285,34 @@ def parse_cp2k_output_advanced(fstring): # pylint: disable=too-many-locals, too
282285 return result_dict
283286
284287
285- def _parse_bands (lines , n_start ):
286- """Parse band structure from cp2k output"""
288+ def _parse_kpoint_cp2k_lower_81 (lines , line_n ):
289+ """Parse one k-point in the output of CP2K <8.1"""
290+
291+ splitted = lines [line_n ].split ()
292+ spin = int (splitted [3 ])
293+ kpoint = tuple (float (p ) for p in splitted [- 3 :])
294+ nlines = int (math .ceil (int (lines [line_n + 1 ]) / 4 ))
295+ bands = [float (v ) for v in " " .join (lines [line_n + 2 :line_n + 2 + nlines ]).split ()]
296+ return spin , kpoint , bands
297+
298+
299+ def _parse_bands_cp2k_greater_81 (lines , line_n ):
300+ """Parse one k-point in the output of CP2K >=8.1"""
301+
302+ splitted = lines [line_n ].split ()
303+ spin = int (splitted [4 ][:- 1 ])
304+ kpoint = tuple (float (p ) for p in splitted [- 4 :- 1 ])
305+ bands = []
306+ for line in lines [line_n + 2 :]:
307+ try :
308+ bands .append (float (line .split ()[1 ]))
309+ except ValueError :
310+ break
311+ return spin , kpoint , bands
312+
313+
314+ def _parse_bands (lines , n_start , cp2k_version ):
315+ """Parse band structure from the CP2K output."""
287316
288317 import numpy as np
289318
@@ -292,31 +321,35 @@ def _parse_bands(lines, n_start):
292321 bands_s1 = []
293322 bands_s2 = []
294323 known_kpoints = {}
295- pattern = re .compile (".*?Nr.*?Spin.*?K-Point.*?" , re .DOTALL )
324+
325+ if cp2k_version < 8.1 :
326+ parse_one_kpoint = _parse_kpoint_cp2k_lower_81
327+ pattern = re .compile (".*?Nr.*?Spin.*?K-Point.*?" , re .DOTALL )
328+ unspecified = ["not" , "specified" ]
329+ else :
330+ parse_one_kpoint = _parse_bands_cp2k_greater_81
331+ pattern = re .compile (".*?Point.*?Spin.*?" , re .DOTALL )
332+ unspecified = ["not" , "specifi" ]
296333
297334 selected_lines = lines [n_start :]
298- for current_line , line in enumerate (selected_lines ):
299- splitted = line . split ()
300- if "KPOINTS| Special K-Point" in line :
335+ for line_n , line in enumerate (selected_lines ):
336+ if "KPOINTS| Special" in line :
337+ splitted = line . split ()
301338 kpoint = tuple (float (p ) for p in splitted [- 3 :])
302- if " " . join ( splitted [- 5 :- 3 ]) != "not specified" :
339+ if splitted [- 5 :- 3 ] != unspecified :
303340 label = splitted [- 4 ]
304341 known_kpoints [kpoint ] = label
342+
305343 elif pattern .match (line ):
306- spin = int (splitted [3 ])
307- kpoint = tuple (float (p ) for p in splitted [- 3 :])
308- kpoint_n_lines = int (math .ceil (int (selected_lines [current_line + 1 ]) / 4 ))
309- band = [
310- float (v ) for v in " " .join (selected_lines [current_line + 2 :current_line + 2 + kpoint_n_lines ]).split ()
311- ]
344+ spin , kpoint , bands = parse_one_kpoint (selected_lines , line_n )
312345
313346 if spin == 1 :
314347 if kpoint in known_kpoints :
315348 labels .append ((len (kpoints ), known_kpoints [kpoint ]))
316349 kpoints .append (kpoint )
317- bands_s1 .append (band )
350+ bands_s1 .append (bands )
318351 elif spin == 2 :
319- bands_s2 .append (band )
352+ bands_s2 .append (bands )
320353
321354 if bands_s2 :
322355 bands = [bands_s1 , bands_s2 ]
0 commit comments