Skip to content

Commit

Permalink
198 cp2k fatal error getting atomic numbers (#200)
Browse files Browse the repository at this point in the history
* Update section header capture of `atomic coordinates` to also cover v2023.1 (+ add tests)

* Make element symbol to atomic number more robust.

---------

Co-authored-by: [email protected] <[email protected]>
  • Loading branch information
ndaelman-hu and [email protected] authored Feb 21, 2024
1 parent f74c5d5 commit 887be8d
Show file tree
Hide file tree
Showing 6 changed files with 45,183 additions and 11 deletions.
26 changes: 15 additions & 11 deletions electronicparsers/cp2k/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# limitations under the License.
#
import os
from typing import Optional
import numpy as np
import logging
import re
Expand Down Expand Up @@ -456,11 +457,11 @@ def str_to_program(val_in):

def str_to_atomic_coordinates(val_in):
val = [v.split() for v in val_in.split("\n")]
lengthunit = val[0][0].lower()
length_unit = val[0][0].lower()
val = np.transpose(np.array([v for v in val if len(v) == 9]))
labels = val[2]
positions = np.transpose(np.array(val[4:7], dtype=float)) * resolve_unit(
lengthunit
length_unit
)
atomic_numbers = {
element: int(val[3][n]) for n, element in enumerate(val[2])
Expand Down Expand Up @@ -814,10 +815,10 @@ def str_to_information(val_in):
),
Quantity(
"atomic_coordinates",
r" ATOMIC COORDINATES IN (angstrom[\s\S]+?)\n\n\n",
r"(?i) atomic coordinates(?: in) (angstrom[\s\S]+?)\n\n\n",
convert=False,
str_operation=str_to_atomic_coordinates,
),
), # TODO: if we always capture angstrom, then no need to extract the units...
Quantity(
"scf_parameters",
r" SCF PARAMETERS([\s\S]+?)\*{79}",
Expand Down Expand Up @@ -1147,13 +1148,16 @@ def _normalize_filename(self, filename):
filename = project_name
return filename

def get_atomic_number(self, element):
atomic_numbers = (
self.out_parser.get(self._calculation_type, {})
.get("atomic_coordinates")
.atomic_numbers
)
return atomic_numbers.get(element, 0)
def get_atomic_number(self, element: str) -> Optional[int]:
"""Convert the element symbol to its corresponding atomic number.
Dependencies:
- ase.data.atomic_numbers
""" # TODO: migrate responsilbity to section normalizer

if match := re.match(r"([A-Z][a-z]?)", element):
return ase.data.atomic_numbers.get(match.group(1), None)
return None

def get_ensemble_type(self, frame):
if self.sampling_method != "molecular_dynamics":
Expand Down
Loading

0 comments on commit 887be8d

Please sign in to comment.