Skip to content

Commit

Permalink
magres old parser style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkshenton authored and oerc0122 committed Feb 12, 2025
1 parent 196a504 commit e0a838f
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions castep_outputs/parsers/magres_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..utilities.datatypes import AtomIndex, ThreeByThreeMatrix, ThreeVector
from ..utilities.filewrapper import Block
from ..utilities.utility import add_aliases, determine_type, to_type
from ..utilities.utility import add_aliases, determine_type, to_type, atreg_to_index

MAGRES_ALIASES = {
"ms": "magnetic_shielding",
Expand Down Expand Up @@ -70,8 +70,8 @@ class UnitsInfo(TypedDict):

class MagresInfo(TypedDict):
"""NMR Magnetic response information."""

atoms: dict[AtomIndex, ThreeVector] #: Atom coordinates.
#: Ion (atom) coordinates.
ions: dict[AtomIndex, ThreeVector]
calc_code: Literal["CASTEP"]
calc_code_hgversion: str
calc_code_platform: str
Expand All @@ -86,27 +86,27 @@ class MagresInfo(TypedDict):
lattice: tuple[float, float, float,
float, float, float,
float, float, float]
# Magnetic shielding tensor.
#: Magnetic shielding tensor.
ms: dict[AtomIndex, list[float]]
# Electric field gradient tensor.
#: Electric field gradient tensor.
efg: dict[AtomIndex, list[float]]
# Local electric field gradient tensor.
#: Local electric field gradient tensor.
efg_local: dict[AtomIndex, list[float]]
# Non-local electric field gradient tensor.
#: Non-local electric field gradient tensor.
efg_nonlocal: dict[AtomIndex, list[float]]
# J-coupling tensor.
#: J-coupling tensor.
isc: dict[tuple[AtomIndex, AtomIndex], list[float]]
# Fermi contact J-coupling tensor.
#: Fermi contact J-coupling tensor.
isc_fc: dict[tuple[AtomIndex, AtomIndex], list[float]]
# Spin dipole J-coupling tensor.
#: Spin dipole J-coupling tensor.
isc_spin: dict[tuple[AtomIndex, AtomIndex], list[float]]
# Orbital paramagnetic J-coupling tensor.
#: Orbital paramagnetic J-coupling tensor.
isc_orbital_p: dict[tuple[AtomIndex, AtomIndex], list[float]]
# Orbital diamagnetic J-coupling tensor.
#: Orbital diamagnetic J-coupling tensor.
isc_orbital_d: dict[tuple[AtomIndex, AtomIndex], list[float]]
# Hyperfine coupling tensor.
#: Hyperfine coupling tensor.
hyperfine: dict[AtomIndex, list[float]]
# Units information.
#: Units information.
units: UnitsInfo


Expand Down Expand Up @@ -262,27 +262,27 @@ def _process_magres_old_block(block: Block) -> dict[str, str | ThreeByThreeMatri
data["atoms"]["units"]["atom"] = "Angstrom"

found_atoms = set()
coords = REs.MAGRES_OLD_RE["coords"].findall(str(block))
for label, i, x, y, z in coords:
index = (label, int(i))
coords_matches = REs.MAGRES_OLD_RE["coords"].finditer(str(block))
for match in coords_matches:
index = atreg_to_index(match)
if index not in found_atoms:
data["atoms"]["coords"][index] = (float(x), float(y), float(z))
data["atoms"]["coords"][index] = to_type(match['val'].split(), float)
found_atoms.add(index)

perturbing_index = None
atoms = REs.MAGRES_OLD_RE["atom"].findall(str(block))
for atom in atoms:
index = atom[1].split(":")[0], int(atom[2])
if atom[0] == " Perturbing Atom":
perturbing_index = index
for match in REs.MAGRES_OLD_RE["atom"].finditer(str(block)):
if match.groups()[0] == " Perturbing Atom":
perturbing_index = atreg_to_index(match)
break

# The magres_old blocks have data in these atom blocks
for atom in atoms:
index = atom[1], int(atom[2])
_process_tensors(atom[3], index, data, "ms" )
_process_tensors(atom[3], index, data, "efg")
_process_tensors(atom[3], index, data, "hf")
_process_jcoupling_tensors(atom[3], index, perturbing_index, data)
for match in REs.MAGRES_OLD_RE["atom"].finditer(str(block)):
index = atreg_to_index(match)
sub_blk = match.groups()[3]
_process_tensors(sub_blk, index, data, "ms" )
_process_tensors(sub_blk, index, data, "efg")
_process_tensors(sub_blk, index, data, "hf")
_process_jcoupling_tensors(sub_blk, index, perturbing_index, data)

return data

Expand Down Expand Up @@ -319,8 +319,7 @@ def _process_jcoupling_tensors(

for tensor in jc_tensors:
tag = _get_jcoupling_tag(tensor[0])
if tag not in data["magres"]:
data["magres"][tag] = {}
data["magres"].setdefault(tag, {})
data["magres"][tag][perturbing_index, index] = _list_to_threebythree(tensor[1:])

# For any isc tensor tags, add the units if not present
Expand Down Expand Up @@ -359,7 +358,7 @@ def _list_to_threebythree(lst: list[float] | list[str]) -> ThreeByThreeMatrix:
g, h, i]
"""
# Convert to floats if necessary
lst = [float(i) for i in lst]
lst = to_type(lst, float)

return (
(lst[0], lst[1], lst[2]),
Expand Down

0 comments on commit e0a838f

Please sign in to comment.