Skip to content

Commit

Permalink
Merge pull request #295 from SCM-NV/debug
Browse files Browse the repository at this point in the history
MAINT: Explicitly raise when the line with the number of orbitals doesn't have any actual orbitals
  • Loading branch information
BvB93 committed Apr 26, 2022
2 parents 23589d8 + 4326fd3 commit 1d254eb
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/qmflows/parsers/cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,31 @@ def read_mos_data_input(path_input: PathLike) -> Tuple2:
return added_mos, range_mos


def read_cp2k_number_of_orbitals(file_name: PathLike) -> MO_metadata:
"""Look for the line ' Number of molecular orbitals:'."""
def fun_split(string: Optional_[str]) -> int:
if string is not None:
return int(string.rsplit(maxsplit=1)[-1])
else:
return 0
_NUMBER_PATTERN = re.compile("[0-9]+")


properties = ["Number of occupied orbitals", "Number of molecular orbitals",
"Number of orbital functions"]
def _orbital_number_parser(pattern: str, file_name: PathLike) -> int:
"""Read the number of MOs from ``file_name`` as specified by ``pattern``."""
string = try_search_pattern(pattern, file_name)
if string is None:
return 0

values = [fun_split(try_search_pattern(x, file_name)) for x in properties]
# NOTE: There seems to be a CP2K bug (?) where the actual number of orbitals is
# omitted from the line containing... the number of orbitals
match = _NUMBER_PATTERN.search(string)
if match is None:
raise RuntimeError(f"Failed to extract the number of orbitals from {string!r}")
return int(match[0])


def read_cp2k_number_of_orbitals(file_name: PathLike) -> MO_metadata:
"""Look for the line ' Number of molecular orbitals:'."""
properties = [
"Number of occupied orbitals",
"Number of molecular orbitals",
"Number of orbital functions",
]
values = [_orbital_number_parser(x, file_name) for x in properties]

# Search for the spin states
spin_1 = try_search_pattern("Spin 1", file_name)
Expand Down

0 comments on commit 1d254eb

Please sign in to comment.