Skip to content

Commit

Permalink
Merge pull request #263 from SCM-NV/MOLog
Browse files Browse the repository at this point in the history
MAINT: Account for the different .MOLog structure of CP2K >= 8.2
  • Loading branch information
BvB93 committed Oct 27, 2021
2 parents f8b07d5 + 9c37ecc commit d27f1f8
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/qmflows/parsers/cp2KParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mmap
import os
import subprocess
import warnings
from itertools import islice, chain
from pathlib import Path
from typing import Any, Dict, FrozenSet, Generator, Iterable, List, IO
Expand Down Expand Up @@ -198,16 +199,10 @@ def read_coefficients(
12 1 C 4d+1 -0.0000000210955114 0.0147105486663415
13 1 C 4d+2 0.0534202997324328 0.0000000021056315
"""
with open(path, 'r') as f:
xss = f.readlines()

# remove empty lines and comments
if cp2k_version >= (8, 2):
# Strip the `MO|` prefix added in CP2K 8.2
rs = list(filter(None, map(lambda x: x.split()[1:], xss)))
rs = _get_mos_ge_82(path)
else:
rs = list(filter(None, map(lambda x: x.split(), xss)))
rs = remove_trailing(rs[1:]) # remove header and trail comments
rs = _get_mos(path)

# Split the list in chunks containing the orbitals info
# in block cotaining a maximum of two columns of MOs
Expand Down Expand Up @@ -235,6 +230,31 @@ def read_coefficients(
return InfoMO(energies, coefficients)


def _get_mos(path: PathLike) -> List[List[str]]:
"""Parse CP2k <8.2 MOs."""
with open(path, 'r') as f:
rs = list(filter(None, (x.rstrip('\n').split() for x in f)))
return remove_trailing(rs[1:])


def _get_mos_ge_82(path: PathLike) -> List[List[str]]:
"""Parse CP2k >=8.2 MOs."""
# Find the begining and end of the MO-range of interest
lineno_range = []
with open(path, 'r') as f:
for i, item in enumerate(f, start=1):
if item == " MO| EIGENVALUES, OCCUPATION NUMBERS, AND SPHERICAL EIGENVECTORS\n":
lineno_range.append(i)
if not lineno_range:
raise ValueError("Failed to identify the start of the MO range")
lineno_range.append(i - 4) # Strip the band gap and Fermi info

# Only read the relevant MOs
with open(path, 'r') as f:
iterator = (x.split()[1:] for x in islice(f, *lineno_range[-2:]))
return [i for i in iterator if i]


def remove_trailing(xs: List[List[str]]) -> List[List[str]]:
"""Remove the last lines of the MOs output."""
words = {'Fermi', 'HOMO-LUMO'}
Expand Down Expand Up @@ -711,4 +731,5 @@ def get_cp2k_version(out_file: PathLike) -> CP2KVersion:
return CP2KVersion._make(int(i) for i in version_str.split("."))
except ValueError:
pass
return CP2KVersion(0, 0)
warnings.warn("Failed to identify the CP2K version", QMFlows_Warning, stacklevel=2)
return CP2KVersion(0, 0)

0 comments on commit d27f1f8

Please sign in to comment.