Skip to content

Commit

Permalink
Sort phonon parsing pylint to avoid other issues (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 authored Jul 28, 2024
1 parent 8be342c commit 0512ad1
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions castep_outputs/parsers/phonon_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
Parse the following castep outputs:
.phonon
"""
import re
from collections import defaultdict
from typing import Any, Dict, TextIO

from ..utilities import castep_res as REs
from ..utilities.castep_res import get_block, labelled_floats
from ..utilities.utility import fix_data_types, log_factory, stack_dict
from ..utilities.castep_res import get_block
from ..utilities.utility import fix_data_types, log_factory
from .parse_utilities import parse_regular_header


def parse_phonon_file(phonon_file: TextIO) -> Dict[str, Any]:
""" Parse castep .phonon file """

# pylint: disable=too-many-locals
logger = log_factory(phonon_file)
phonon_info = defaultdict(list)
evals = []
Expand All @@ -25,43 +23,46 @@ def parse_phonon_file(phonon_file: TextIO) -> Dict[str, Any]:

data = parse_regular_header(block)
phonon_info.update(data)
phonon_info.pop("",None)
eigenvectors_endblock = (format(phonon_info["branches"], ">4")) + (format(phonon_info["ions"], ">4"))
phonon_info.pop("", None)
eigenvectors_endblock = (format(phonon_info["branches"], ">4") +
format(phonon_info["ions"], ">4"))

elif block := get_block(line, phonon_file, "q-pt", "Phonon Eigenvectors", out_fmt=list):

logger("Found eigenvalue block")
for line in block:
if "q-pt" in line:
_, _, posx,posy,posz, *weight = line.split()
qdata = {'pos':[posx,posy,posz], 'weight':weight}
_, _, posx, posy, posz, *weight = line.split()
qdata = {'pos': [posx, posy, posz], 'weight': weight}
fix_data_types(qdata, {'pos': float,
'weight': float})
phonon_info["qpt_pos"].append(qdata['pos'])

elif "Eigenvectors" not in line:
_, e_val, *_ = line.split()
qdata = {'eval':e_val}
qdata = {'eval': e_val}
fix_data_types(qdata, {'eval': float})
evals.append(qdata['eval'])
if len(evals) == phonon_info["branches"]:
phonon_info["evals"].append(evals)
evals = []

elif block := get_block(line, phonon_file, "Mode Ion", eigenvectors_endblock, out_fmt=list):

logger("Found eigenvector block")
for line in block:
if "Mode" not in line:
_, _, *vectors = line.split()
qdata = {'evec':vectors}

qdata = {'evec': vectors}
fix_data_types(qdata, {'evec': float})
qdata['evec'] = [complex(qdata['evec'][i],qdata['evec'][i+1])for i in range(0,len(vectors),2)]
qdata['evec'] = [complex(qdata['evec'][i],
qdata['evec'][i+1])
for i in range(0, len(vectors), 2)]
evecs.append(qdata['evec'])

if len(evecs) == phonon_info["branches"]*phonon_info["ions"]:
phonon_info["evecs"].append(evecs)
evecs = []
return phonon_info

return phonon_info

0 comments on commit 0512ad1

Please sign in to comment.