diff --git a/wien2kparser/parser_wien2k.py b/wien2kparser/parser_wien2k.py index f496ada3cd1e8b91b9a7eb0ae01ceaccf22be3f5..dea5f492d250ce3902f551c122029d9b173fdf67 100644 --- a/wien2kparser/parser_wien2k.py +++ b/wien2kparser/parser_wien2k.py @@ -1,6 +1,7 @@ from builtins import object # import setup_paths import numpy as np +import ase.io import os import sys @@ -173,7 +174,6 @@ class Wien2kContext(object): if atom_labels is not None: backend.addArrayValues('atom_labels', np.asarray(atom_labels)) - # atom force atom_force = [] for i in ['x', 'y', 'z']: @@ -184,20 +184,30 @@ class Wien2kContext(object): # need to transpose array since its shape is [number_of_atoms,3] in\the metadata backend.addArrayValues('atom_forces', np.transpose(np.asarray(atom_force))) + # Parse the structure file mainFile = self.parser.fIn.fIn.name fName = mainFile[:-4] + ".struct" if os.path.exists(fName): - structSuperContext = wien2k_parser_struct.Wien2kStructContext() - structParser = AncillaryParser( - fileDescription = wien2k_parser_struct.buildStructureMatchers(), - parser = self.parser, - cachingLevelForMetaName = wien2k_parser_struct.get_cachingLevelForMetaName(self.metaInfoEnv, CachingLevel.PreOpenedIgnore), - superContext = structSuperContext) - - with open(fName) as fIn: - structParser.parseFile(fIn) - + # ASE does not support reading file object for WIEN2k structure files. + atoms = ase.io.read(fName, format="struct") + pos = atoms.get_positions() * 1E-10 + symbols = atoms.get_chemical_symbols() + cell = atoms.get_cell() * 1E-10 + pbc = atoms.get_pbc() + backend.addArrayValues('lattice_vectors', cell) + backend.addArrayValues("configuration_periodic_dimensions", pbc) + backend.addValue("atom_labels", symbols) + backend.addArrayValues('atom_positions', pos) + + with open(fName, "r") as fin: + structSuperContext = wien2k_parser_struct.Wien2kStructContext() + structParser = AncillaryParser( + fileDescription = wien2k_parser_struct.buildStructureMatchers(), + parser = self.parser, + cachingLevelForMetaName = wien2k_parser_struct.get_cachingLevelForMetaName(self.metaInfoEnv, CachingLevel.PreOpenedIgnore), + superContext = structSuperContext) + structParser.parseFile(fin) def onClose_section_scf_iteration(self, backend, gIndex, section): #Trigger called when section_scf_iteration is closed. diff --git a/wien2kparser/wien2k_parser_struct.py b/wien2kparser/wien2k_parser_struct.py index c6b6627f5862b93190863b3dd83db5d9407af1ce..c025deb5318987b51ac96372fb9f4f1e4393912d 100644 --- a/wien2kparser/wien2k_parser_struct.py +++ b/wien2kparser/wien2k_parser_struct.py @@ -30,7 +30,8 @@ __maintainer__ = "Daria M. Tomecka" __email__ = "tomeckadm@gmail.com;" __date__ = "15/05/2017" -class Wien2kStructContext(object): + +class Wien2kStructContext(): """context for wien2k struct parser""" def __init__(self): @@ -46,46 +47,6 @@ class Wien2kStructContext(object): # allows to reset values if the same superContext is used to parse different files self.initialize_values() - def onClose_section_system(self, backend, gIndex, section): - # unit_cell - unit_cell_params = [] - for i in ['a', 'b', 'c']: - uci = section['x_wien2k_unit_cell_param_' + i] - #if uci is not None: - unit_cell_params.append(uci[0]) - for i in ['alfa', 'beta', 'gamma']: - uci = section['x_wien2k_angle_between_unit_axis_' + i] - # if uci is not None: - unit_cell_params.append(uci[0]) - - unit_cell = ase.geometry.cellpar_to_cell(unit_cell_params) - backend.addArrayValues('simulation_cell', unit_cell) - backend.addArrayValues("configuration_periodic_dimensions", np.ones(3, dtype=bool)) - - equiv_atoms = section["x_wien2k_section_equiv_atoms"] - #logging.error("section: %s", section) - labels = [] - pos = [] - - for eqAtoms in equiv_atoms: - label = eqAtoms["x_wien2k_atom_name"][0] - x = eqAtoms["x_wien2k_atom_pos_x"] - y = eqAtoms["x_wien2k_atom_pos_y"] - z = eqAtoms["x_wien2k_atom_pos_z"] - #logging.error("equiv_atoms: %s x %s y %s z %s",eqAtoms, x, y, z) - if len(x) != len(y) or len(x) != len(z): - raise Exception("incorrect parsing, different number of x,y,z components") - groupPos = [[x[i],y[i],z[i]] for i in range(len(x))] - nAt = len(groupPos) - labels += [label for i in range(nAt)] - pos += groupPos - backend.addValue("atom_labels", labels) - - #backend.addArrayValues("atom_positions", np.dot(pos,unit_cell)) - #backend.addArrayValues('atom_positions', np.transpose(np.asarray(pos))) - backend.addArrayValues('atom_positions', np.asarray(pos)) - - # description of the input def buildStructureMatchers():