wien2k_parser_struct.py 6.52 KB
Newer Older
Daria Tomecka's avatar
Daria Tomecka committed
1
2
3
4
5
from builtins import object
import setup_paths
from nomadcore.simple_parser import mainFunction, CachingLevel
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
6
7
import os, sys, json, logging
import numpy as np
8
import ase.geometry
Daria Tomecka's avatar
Daria Tomecka committed
9

Daria Tomecka's avatar
Daria Tomecka committed
10
11
12
13
14

################################################################
# This is the subparser for the main WIEN2k input file (.struct)
################################################################

Daria M. Tomecka's avatar
Daria M. Tomecka committed
15
# Copyright 2016-2018 Daria M. Tomecka, Fawzi Mohamed
Daria M. Tomecka's avatar
Daria M. Tomecka committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = "Daria M. Tomecka"
__maintainer__ = "Daria M. Tomecka"
__email__ = "tomeckadm@gmail.com;"
__date__ = "15/05/2017"
Daria Tomecka's avatar
Daria Tomecka committed
33

Daria Tomecka's avatar
Daria Tomecka committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Wien2kStructContext(object):
    """context for wien2k struct parser"""

    def __init__(self):
        self.parser = None

    def initialize_values(self):
        """allows to reset values if the same superContext is used to parse different files"""
        pass

    def startedParsing(self, path, parser):
        """called when parsing starts"""
        self.parser = parser
        # allows to reset values if the same superContext is used to parse different files
        self.initialize_values()

50
    def onClose_section_system(self, backend, gIndex, section):
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        #   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))

66
67
68
69
        equiv_atoms = section["x_wien2k_section_equiv_atoms"]
        #logging.error("section: %s", section)
        labels = []
        pos = []
70

71
        for eqAtoms in equiv_atoms:
72
            label = eqAtoms["x_wien2k_atom_name"][0]
73
74
75
            x = eqAtoms["x_wien2k_atom_pos_x"]
            y = eqAtoms["x_wien2k_atom_pos_y"]
            z = eqAtoms["x_wien2k_atom_pos_z"]
Daria Tomecka's avatar
Daria Tomecka committed
76
            #logging.error("equiv_atoms: %s x %s y %s z %s",eqAtoms, x, y, z)
77
78
79
80
81
82
83
            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)
Daria Tomecka's avatar
Daria Tomecka committed
84
85
86

        #backend.addArrayValues("atom_positions", np.dot(pos,unit_cell))
        #backend.addArrayValues('atom_positions', np.transpose(np.asarray(pos)))
87
88
89
        backend.addArrayValues('atom_positions', np.asarray(pos))


90

Daria Tomecka's avatar
Daria Tomecka committed
91
92
93
94
95
96
97
98
99
# description of the input
def buildStructureMatchers():
    return SM(
    name = 'root',
    weak = True,
    startReStr = "",
    sections = ["section_run", "section_system"],
    subMatchers = [
        SM(name = 'systemName',
100
           startReStr = r"(?P<x_wien2k_system_namein>.*)"),
101
102
        SM(r"\w+\s*LATTICE,NONEQUIV\.ATOMS.\s*(?P<x_wien2k_nonequiv_atoms>[0-9]+)"),
        SM(r"(?P<x_wien2k_calc_mode>.*)"),
103
       # SM(r"\s*(?P<x_wien2k_unit_cell_param_a>[-+0-9.eEdD]+)\s*(?P<x_wien2k_unit_cell_param_b>[-+0-9.eEdD]+)\s*(?P<x_wien2k_unit_cell_param_c>[-+0-9.eEdD]+)\s*(?P<x_wien2k_angle_between_unit_axis_alfa>[-+0-9.eEdD]{9})\s*(?P<x_wien2k_angle_between_unit_axis_beta>[-+0-9.eEdD]{9})\s*(?P<x_wien2k_angle_between_unit_axis_gamma>[-+0-9.eEdD]+)"),
104
105
        SM(r"\s*(?P<x_wien2k_unit_cell_param_a__angstrom>[-+0-9]*\.\d{0,6}){0,10}\s*(?P<x_wien2k_unit_cell_param_b__angstrom>[-+0-9]*\.\d{0,6}){0,10}\s*(?P<x_wien2k_unit_cell_param_c__angstrom>[-+0-9]*\.\d{0,6}){0,10}\s*(?P<x_wien2k_angle_between_unit_axis_alfa>[-+]?[0-9]*\.\d{0,6}){0,10}\s*(?P<x_wien2k_angle_between_unit_axis_beta>[-+]?[0-9]*\.\d{0,6}){0,10}\s*(?P<x_wien2k_angle_between_unit_axis_gamma>[-+]?[0-9]*\.\d*)"),
        SM(r"\s*ATOM\s*[-0-9]+:\s*X=(?P<x_wien2k_atom_pos_x__angstrom>[-+0-9.eEdD]+)\s*Y=(?P<x_wien2k_atom_pos_y__angstrom>[-+0-9.eEdD]+)\s*Z=(?P<x_wien2k_atom_pos_z__angstrom>[-+0-9.eEdD]+)",
106
107
108
           repeats=True,
           sections=["x_wien2k_section_equiv_atoms"],
           subMatchers=[
109
               SM(r"\s*[-0-9]+:\s*X=(?P<x_wien2k_atom_pos_x__angstrom>[-+0-9.eEdD]+)\s*Y=(?P<x_wien2k_atom_pos_y__angstrom>[-+0-9.eEdD]+)\s*Z=(?P<x_wien2k_atom_pos_z__angstrom>[-+0-9.eEdD]+)",
110
                  repeats=True
111
112
              ),
    #           SM(r"\s*(?P<atom>.{10})\s*NPT=\s*(?P<NPT>[0-9]+)\s*R0=(?P<r0>[0-9.]+)\s*RMT=\s*(?P<rmt>[0-9.]+)\s*Z:\s*(?P<z>[0-9.]+)",)
113
               SM(r"\s*(?P<x_wien2k_atom_name>^.+)\s*NPT=\s*(?P<x_wien2k_npt>[0-9]+)\s*R0=(?P<x_wien2k_r0>[0-9.]+)\s*RMT=\s*(?P<x_wien2k_rmt>[0-9.]+)\s*Z:\s*(?P<x_wien2k_atomic_number_z>[0-9.]+)",)
114
115
           ]
       )
Daria Tomecka's avatar
Daria Tomecka committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    ])

def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl):
    """Sets the caching level for the metadata.

    Args:
        metaInfoEnv: metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py.
        CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
            This allows to run the parser without opening new sections.

    Returns:
        Dictionary with metaname as key and caching level as value.
    """
    # manually adjust caching of metadata
    cachingLevelForMetaName = {
                               'section_run': CachingLvl,
                               'section_system': CachingLvl
                              }
134
    cachingLevelForMetaName["x_wien2k_system_namein"] = CachingLevel.ForwardAndCache
135
    cachingLevelForMetaName["x_wien2k_section_equiv_atoms"] = CachingLevel.ForwardAndCache
136
    cachingLevelForMetaName["atom_labels"] = CachingLevel.ForwardAndCache
Daria Tomecka's avatar
Daria Tomecka committed
137
138
139
    return cachingLevelForMetaName

# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/fhi_aims.nomadmetainfo.json