exciting_parser_dos.py 8.2 KB
Newer Older
1
2
import xml.sax
import logging
3
import numpy as np
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
4
5
from nomadcore.unit_conversion.unit_conversion import convert_unit_function
from nomadcore.unit_conversion.unit_conversion import convert_unit
6
from nomadcore.unit_conversion import unit_conversion
7
8

class DosHandler(xml.sax.handler.ContentHandler):
9
    def __init__(self, backend, spinTreat, unitCellVol):
10
11
12
        self.backend = backend
        self.dosSectionGIndex = -1
        self.inDos = False
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        self.dosProjSectionGIndex = -1
        self.inDosProj = False
        self.spinTreat = spinTreat
        self.totDos = []
        self.totDosSpin = [[],[]]
        self.dosProj = []
        self.dosProjSpin = []
        self.energy = []
        self.energySpin = []
        self.speciesrn = []       
        self.atom = []           
        self.numDosVal = 0
        self.dosProjDummy = []
        self.dosProjDummy2 = []
27
        self.unitCellVol = float(unitCellVol[0])
28
29

    def endDocument(self):
30
31
32
33
34
35
#        self.inDos = False
        self.inDosProj = False
        self.backend.closeSection("section_dos",self.dosSectionGIndex)
#        self.backend.closeSection("section_atom_projected_dos",self.dosProjSectionGIndex)
        self.dosSectionGIndex = -1
#        self.dosProjSectionGIndex = -1
36
#        print("self.unitCellVol=",self.unitCellVol)
37
38

    def startElement(self, name, attrs):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
39
        ha_per_joule = convert_unit(1, "hartree", "J")
40
41
42
#        bohr_cube_to_m_cube = convert_unit(1, "bohr^3", "m^3")
#        print("bohr_cube_to_m_cube = ", bohr_cube_to_m_cube )
#        print("ha_per_joule= ",ha_per_joule)
43
        fromH = unit_conversion.convert_unit_function("hartree", "J")
44
45
        if name == "totaldos":
            self.dosSectionGIndex = self.backend.openSection("section_dos")
46
            self.inDos = True
47
48
49
50
51
52
53
54
55
56
        elif name == "partialdos":
            self.speciesrn.append(int(attrs.getValue('speciesrn'))) 
            self.atom.append(int(attrs.getValue('atom')))         
            self.dosProjDummy.append([])
            self.dosProjDummy2.append([])
            if self.speciesrn [-1] == 1 and self.atom[-1] == 1:
                self.dosProjSectionGIndex = self.backend.openSection("section_atom_projected_dos")
                self.inDosProj = True
        elif name == "point":
            if self.inDos:
57
                self.totDos.append(ha_per_joule*self.unitCellVol*float(attrs.getValue('dos')))
58
59
#                self.energy.append(float(attrs.getValue('e')))
                self.energy.append(fromH(float(attrs.getValue('e'))))
60
            elif self.inDosProj:
61
                self.dosProj.append(ha_per_joule*self.unitCellVol*float(attrs.getValue('dos')))
62
63
#                self.energy.append(float(attrs.getValue('e')))
                self.energy.append(fromH(float(attrs.getValue('e'))))
64
65
66
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
        elif name == "diagram": 
            if not self.speciesrn: pass
            elif self.speciesrn [-1] == 1 and self.atom[-1] == 1:
                if not self.spinTreat:
                    self.dosProjSpin.append([])
                else:
                    nspin = int(attrs.getValue("nspin"))
                    if nspin == 1:
                        self.dosProjSpin.append([])
        elif name == "limrep":
            for i in range(0,len(self.dosProjSpin)):
                for j in range(0,2):
                    self.dosProjSpin[i].append([])          
                    for k in range(0,len(self.speciesrn)):
                        self.dosProjDummy2[k].append([])
                        self.dosProjSpin[i][j].append([])
#            print("self.dosProjSpinPrima=",self.dosProjSpin)
            for i in range (0,len(self.speciesrn)):
                if not self.spinTreat:
                    self.dosProjDummy[i] = self.dosProj[i*len(self.dosProjSpin)*self.numDosVal:(i+1)*len(self.dosProjSpin)*self.numDosVal]
                else:
                    self.dosProjDummy[i] = self.dosProj[i*int(2*len(self.dosProjSpin))*self.numDosVal:(i+1)*int(2*len(self.dosProjSpin))*self.numDosVal]
            for j in range(0,int(2*len(self.dosProjSpin))):       
                for i in range (0,len(self.speciesrn)):                       
                    self.dosProjDummy2[i][j] = self.dosProjDummy[i][j*self.numDosVal:(j+1)*self.numDosVal] 
            if not self.spinTreat:
                for i in range(0,len(self.dosProjSpin)):
                    for j in range(0,len(self.speciesrn)):
                        self.dosProjSpin[i][0][j] = self.dosProjDummy2[j][i]               
                        self.dosProjSpin[i][1][j] = self.dosProjDummy2[j][i]
#                        print("self.dosProjSpin=",self.dosProjSpin)
            else:
                for j in range(0,len(self.speciesrn)):
                    for i in range(0,int(2*len(self.dosProjSpin))):
                        if i < len(self.dosProjSpin):
#                            print("i=",i)
#                            pass
                            self.dosProjSpin[i][0][j] = self.dosProjDummy2[j][i]
                        else:
                            k = int(i - len(self.dosProjSpin))
#                            print("i=",i,"j=",j,"k=",k)
                            self.dosProjSpin[k][1][j] = self.dosProjDummy2[j][i]
#        print("self.dosProjSpin=",len(self.dosProjSpin))
#        print("self.dosProjSpin=",len(self.dosProjSpin))
#        print("self.dosProjDummy2=",len(self.dosProjDummy2))
109
    def endElement(self, name):
110
        if name == 'totaldos':
111
            self.inDos = False
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
            if not self.spinTreat:
                self.numDosVal = len(self.energy)
                self.totDosSpin[0] = self.totDos[0:self.numDosVal]
                self.totDosSpin[1] = self.totDos[0:self.numDosVal]
                self.energySpin = self.energy[0:self.numDosVal]
                self.backend.addValue("dos_values", self.totDosSpin)
                self.backend.addValue("dos_energies",self.energySpin)
                self.backend.addValue("number_of_dos_values", self.numDosVal)
                self.backend.addValue("dos_kind","electronic")
#                print("self.totDosSpin=",self.totDosSpin)
            else:
                self.numDosVal = int(len(self.energy)/2)
                self.totDosSpin[0] = self.totDos[0:self.numDosVal]
                self.totDosSpin[1] = self.totDos[self.numDosVal:int(2*(self.numDosVal))]
                self.energySpin = self.energy[0:self.numDosVal]
                self.backend.addValue("dos_values", self.totDosSpin)
#            self.backend.addValue("dos_energies",self.energySpin)
#            self.backend.addValue("number_of_dos_values", self.numDosVal)
        elif name == 'partialdos':
            pass
#            self.inDosProj = False
#            print("self.dosProjSpin=",self.dosProjSpin)
#            self.backend.addArrayValues("atom_projected_dos_values_lm",np.asarray(self.dosProjSpin))
#            self.backend.closeSection("section_atom_projected_dos",self.dosProjSectionGIndex)
#            self.dosProjSectionGIndex = -1
        elif name == 'interstitialdos':
#            print("self.energy=",len(self.energy))
#            print("self.dosProjSpinFine=",self.dosProjSpin)
            self.backend.addValue("atom_projected_dos_values_lm",self.dosProjSpin)
            self.backend.addValue("number_of_lm_atom_projected_dos",len(self.dosProjSpin))
            self.backend.addValue("number_of_atom_projected_dos_values",self.numDosVal)
            self.backend.addValue("atom_projected_dos_energies",self.energy[0:self.numDosVal])
            self.backend.addValue("atom_projected_dos_m_kind","spherical")
            self.backend.closeSection("section_atom_projected_dos",self.dosProjSectionGIndex)
            self.dosProjSectionGIndex = -1
            self.inDosProj = False
#        backend.addValue()
149
150
151
152
153
154
155
156
157
158
159
160
    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

161
162
def parseDos(inF, backend, spinTreat, unitCellVol):
    handler = DosHandler(backend, spinTreat, unitCellVol)
163
164
165
    logging.error("will parse")
    xml.sax.parse(inF, handler)
    logging.error("did parse")