exciting_parser_bandstructure.py 9.94 KB
Newer Older
1
2
import xml.sax
import logging
3
import numpy as np
4
from nomadcore.unit_conversion import unit_conversion
5
6

class BandHandler(xml.sax.handler.ContentHandler):
7
    def __init__(self, backend, spinTreat):
8
9
        self.backend = backend
        self.bandSectionGIndex = -1
10
        self.normBandSectionGIndex = -1
11
        self.inBand = False
12
13
        self.energy=[]
        self.energySpin = [[],[]]
14
        self.bandEnergies = [[],[]]
15
16
17
18
19
        self.distance = []
        self.vertexCoord = []
        self.vertexLabels = []
        self.vertexDist = []
        self.spinTreat = spinTreat
20
21

    def endDocument(self):
22
            self.inBand = False
23
            self.backend.closeSection("section_k_band",self.bandSectionGIndex)
24
#            self.backend.closeSection("section_k_band_segment",self.normBandSectionGIndex)
25
            self.bandSectionGIndex = -1
26
            self.normBandSectionGIndex = -1
27
28
29

    def startElement(self, name, attrs):
        if name == "bandstructure":
30
31
            self.bandSectionGIndex = self.backend.openSection("section_k_band")
            self.backend.addValue("band_structure_kind","electronic")
32
#            self.normBandSectionGIndex = self.backend.openSection("section_k_band_segment")
33
            self.inBand = True
34
35
36
37
        elif name == "band":
            self.energy.append([])
            self.distance.append([])
        elif name == "point" and self.inBand:
38
39
            fromH = unit_conversion.convert_unit_function("hartree", "J")
            self.energy[-1].append(fromH(float(attrs.getValue('eval'))))
40
41
42
            self.distance[-1].append(float(attrs.getValue('distance')))
        elif name == "vertex" and self.inBand:
            self.vertexCoord.append(attrs.getValue("coord"))
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
43
44
45
46
47
            label = attrs.getValue("label")
            if label == "GAMMA":
                self.vertexLabels.append('\u0393')
            else:
                self.vertexLabels.append(label)
48
            self.vertexDist.append(attrs.getValue("distance"))
49
50
51

    def endElement(self, name):
        if name == 'bandstructure':
52
53
            bandEnergiesBE = []
            vertexDist=[]
54
            vertexDummy = []
55
56
57
58
59
60
61
62
            kBandSegm = []
            bandKpoints = []
            bandEnergies = []
            bandOcc = []
            step = []
            numkPointsPerSegmL = []
            numkPointsPerSemIncr = []
            coordinate = []
63
64
65
66
            vertexNum = len(self.vertexLabels)
            kmesh = len(self.energy[-1])
            bands = len(self.energy)
            bands2 = int(bands/2)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
            kpoints=self.distance[-1]
            vertexDist.append(kpoints[0])

            i=0
            while i < vertexNum:
                coordinate.append(self.vertexCoord[i].split())
                i +=1

            for i in kpoints:
                if kpoints.count(i)>1:
                    index=kpoints.index(i)
                    vertexDist.append(kpoints[index])

            vertexDist.append(kpoints[-1])

            for i in vertexDist:
                if vertexDist.count(i)>1:
                    index=vertexDist.index(i)
                    prova = vertexDist.pop(index)

            i = 0
            while i < vertexNum-1:
                bandKpoints.append([])
                bandEnergies.append([])
                bandOcc.append([])
                step.append([])
                initial = kpoints.index(vertexDist[i])
                final = kpoints.index(vertexDist[i+1])
                kBandSegm.append(kpoints[kpoints.index(vertexDist[i]):kpoints.index(vertexDist[i+1])])
                i +=1

            j = 0
            while j < vertexNum -2:
                bodda = kBandSegm[j+1].pop(0) 
                kBandSegm[j].append(bodda)   
                j+=1

            kBandSegm[-1].append(kpoints[-1])
            numkPointsPerSemIncr.append(0)

            for i in range(0,vertexNum-1):
                numkPointsPerSegm = len(kBandSegm[i])
                numkPointsPerSegmL.append(numkPointsPerSegm)
                numkPointsPerSemIncr.append(numkPointsPerSemIncr[-1]+numkPointsPerSegmL[-1])
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
111
112
113
114
                step[i].append((float(coordinate[i+1][0])-float(coordinate[i][0]))/(numkPointsPerSegm-1))
                step[i].append((float(coordinate[i+1][1])-float(coordinate[i][1]))/(numkPointsPerSegm-1))
                step[i].append((float(coordinate[i+1][2])-float(coordinate[i][2]))/(numkPointsPerSegm-1))
                for j in range(0,numkPointsPerSegm):
115
116
117
118
119
120
121
122
123
124
125
126
127
                    bandKpoints[i].append([])
                    for k in range(0,3):
                        bandKpoints[i][j].append(float(coordinate[i][k])+j*step[i][k])

            i=0
            while i < vertexNum-1:
                bandEnergiesBE.append([])
                for j in range(0,2):
                    bandEnergiesBE[i].append([])
                    for k in range(0,numkPointsPerSegmL[i]):
                        bandEnergiesBE[i][j].append([])
                i+=1

128
129
130
131
            for i in range(0,vertexNum):
                self.vertexCoord[i]=self.vertexCoord[i].split() 
                for j in range(0,3):
                    self.vertexCoord[i][j] = float(self.vertexCoord[i][j])
132
133


134
135
136
137
138
#            for i in range(0,vertexNum-1):
#                self.backend.addValue("band_k_points",bandKpoints[i])
#                self.backend.addValue("band_segm_start_end",self.vertexCoord[i:i+2])
#                self.backend.addValue("number_of_k_points_per_segment",numkPointsPerSegmL[i])
#                self.backend.addValue("band_segm_labels",self.vertexLabels[i:i+2])
139
140
141
142
            self.eigenSectionGIndex = self.backend.openSection("section_eigenvalues")
            self.backend.addValue("number_of_band_segment_eigenvalues",bands)
            self.backend.closeSection("section_eigenvalues",self.eigenSectionGIndex)

143
            if not self.spinTreat:
144
145
                self.energySpin[0] = self.energy[0:bands]
                self.energySpin[1] = self.energy[0:bands]
146
147
148
149
150
151
152
                for i in range (0,bands):
                    self.bandEnergies[0].append([])
                    self.bandEnergies[1].append([])
                    for j in range(0,vertexNum-1):
                        self.bandEnergies[0][i].append(self.energySpin[0][i][numkPointsPerSemIncr[j]:numkPointsPerSemIncr[j+1]])
                        self.bandEnergies[1][i].append(self.energySpin[1][i][numkPointsPerSemIncr[j]:numkPointsPerSemIncr[j+1]])
                for i in range (0,vertexNum-1):
153
                   self.normBandSectionGIndex = self.backend.openSection("section_k_band_segment")
154
155
                   for j in range(0,bands):
                       for k in range(0,numkPointsPerSegmL[i]):
156
157
158
159
160
161
162
163
                            bandEnergiesBE[i][0][k].append(self.bandEnergies[0][j][i][k])
                            bandEnergiesBE[i][1][k].append(self.bandEnergies[1][j][i][k])
#                            bandEnergiesBE[i][0][k].append(self.bandEnergies[0][i][j][k])
#                            bandEnergiesBE[i][1][k].append(self.bandEnergies[1][i][j][k])
                   self.backend.addValue("band_k_points",bandKpoints[i])
                   self.backend.addValue("band_segm_start_end",self.vertexCoord[i:i+2])
                   self.backend.addValue("number_of_k_points_per_segment",numkPointsPerSegmL[i])
                   self.backend.addValue("band_segm_labels",self.vertexLabels[i:i+2])
164
#                   print("bandEnergiesBE[i]=",len(bandEnergiesBE[i][0]))
165
                   self.backend.addValue("band_energies",bandEnergiesBE[i])
166
                   self.backend.closeSection("section_k_band_segment",self.normBandSectionGIndex)
167
            else: #### check for spin polarized!!!!
168
169
                self.energySpin[0] = self.energy[0:bands2]
                self.energySpin[1] = self.energy[bands2:bands]
170
171
172
173
174
175
176
                for i in range (0,bands2):
                    self.bandEnergies[0].append([])
                    self.bandEnergies[1].append([])
                    for j in range(0,vertexNum-1):
                        self.bandEnergies[0][i].append(self.energySpin[0][i][numkPointsPerSemIncr[j]:numkPointsPerSemIncr[j+1]])
                        self.bandEnergies[1][i].append(self.energySpin[1][i][numkPointsPerSemIncr[j]:numkPointsPerSemIncr[j+1]])
                for i in range (0,vertexNum-1):
177
                   self.normBandSectionGIndex = self.backend.openSection("section_k_band_segment")
178
179
                   for j in range(0,bands):
                       for k in range(0,numkPointsPerSegmL[i]):
180
181
182
183
                            bandEnergiesBE[i][0][k].append(self.bandEnergies[0][j][i][k])
                            bandEnergiesBE[i][1][k].append(self.bandEnergies[1][j][i][k])
#                            bandEnergiesBE[i][0][k].append(self.bandEnergies[0][i][j][k])
#                            bandEnergiesBE[i][1][k].append(self.bandEnergies[1][i][j][k])
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
184
185
186
187
#                   print("i=",i)
#                   print("bandKpoints[i]=",bandKpoints[i])
#                   print("numkPointsPerSegmL[i]=",numkPointsPerSegmL[i])
#                   print("len(bandEnergiesBE[i])=",len(bandEnergiesBE[i]))
188
189
190
191
                   self.backend.addValue("band_k_points",bandKpoints[i])
                   self.backend.addValue("band_segm_start_end",self.vertexCoord[i:i+2])
                   self.backend.addValue("number_of_k_points_per_segment",numkPointsPerSegmL[i])
                   self.backend.addValue("band_segm_labels",self.vertexLabels[i:i+2])
192
                   self.backend.addValue("band_energies",bandEnergiesBE[i])
193
                   self.backend.closeSection("section_k_band_segment",self.normBandSectionGIndex)
194

195
196
197
198
199
200
201
202
203
204
205
206
    def startElementNS(self, name, qname, attrs):
        attrDict={}
        for name in attrs.getNames():
            attrDict[name] = attrs.getValue(name)
        logging.error("start element %s ns %s attr %s", name, qname, attrDict)

    def endElementNS(self, name, qname):
        logging.error("end element %s ns %s", name, qname)

    def characters(self, content):
        pass

207
208
def parseBand(inF, backend, spinTreat):
    handler = BandHandler(backend, spinTreat)
209
210
211
    logging.error("will parse")
    xml.sax.parse(inF, handler)
    logging.error("did parse")