exciting_parser_gw.py 19.4 KB
Newer Older
1
2
from builtins import object
import setup_paths
3
import xml.sax
4
5
6
7
8
9
10
from nomadcore.simple_parser import mainFunction, CachingLevel
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.unit_conversion import unit_conversion
import os, sys, json

################################################################
11
# This is the subparser for the exciting GW output
12
13
14
15
16
17
18
19
20
################################################################


class GWContext(object):
    """context for wien2k In2 parser"""

    def __init__(self):
        self.parser = None
        self.spinTreat = None
21
22
        self.vertexDist = []
        self.vertexLabels = []
23
        self.vertexNum = 0
24
25
26
27
28
29
30
31
32
33
34
35
36
37

    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
        self.initialize_values()

    def onClose_x_exciting_section_GW_method(self, backend, gIndex, section):

        dirPath = os.path.dirname(self.parser.fIn.name)
38
39
40
41
42
43
44
        if os.access(os.path.join(dirPath, "EVALQP.DAT"), os.F_OK):
            eigvalGWFile = os.path.join(dirPath, "EVALQP.DAT")
        elif os.access(os.path.join(dirPath, "EVALQP.TXT"), os.F_OK):
            eigvalGWFile = os.path.join(dirPath, "EVALQP.TXT")
        else:
            pass
#        eigvalGWFile = os.path.join(dirPath, "EVALQP.DAT")
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
45
        dosGWFile = os.path.join(dirPath, "TDOS-QP.OUT")
46
47
48
        bandCarbGWFile = os.path.join(dirPath, "bandstructure-qp.dat")
        bandBorGWFile = os.path.join(dirPath, "BAND-QP.OUT")
#        bandGWFile = os.path.join(dirPath, "bandstructure-qp.dat")
49
50
        vertexGWFile = os.path.join(dirPath, "BANDLINES.OUT")
        vertexLabGWFile = os.path.join(dirPath, "bandstructure.xml")
51
        selfCorGWFile = os.path.join(dirPath, "SELFC.DAT")
52

53
54
55
56
57
58
59
60
61
62
63
64
        if os.path.exists(vertexGWFile):
            with open(vertexGWFile) as g:
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    s = s.split()
                    if len(s) > 0:
                        if not self.vertexDist:
                            self.vertexDist.append(float(s[0]))
                        elif float(s[0]) != self.vertexDist[-1]:
                            self.vertexDist.append(float(s[0]))
65
66
67
68
#                print("vertexDist",self.vertexDist)     
                self.vertexNum = len(self.vertexDist)-1
#                print("vertexNum",self.vertexNum)
     
69
70
71
72
73
74
75
76
77
78
        if os.path.exists(vertexLabGWFile):
            with open(vertexLabGWFile) as g:
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    s = s.split()
                    if s[0] == "<vertex":
                        f = s[4].split("\"")
                        self.vertexLabels.append(f[1])
79
80
81
82
83
84
85
86
87
88
89
90
91

        if os.path.exists(selfCorGWFile):
            selfGWGIndex = backend.openSection("x_exciting_section_GW_self_energy")
            with open(selfCorGWFile) as g:
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()



            backend.closeSection("x_exciting_section_GW_self_energy",selfGWGIndex)

92
93
94
        if os.path.exists(eigvalGWFile):
            eigvalGWGIndex = backend.openSection("x_exciting_section_GW_qp_eigenvalues")
            with open(eigvalGWFile) as g:
95
#                print("ggggggggggg=",g)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                qpGWKpoint=[]
                Sx = [[],[]]
                Sc = [[],[]]
                qpE = [[],[]]
                Znk = [[],[]]
                fromH = unit_conversion.convert_unit_function("hartree", "J")
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    if "k-point" in s.split():
                        qpGWKpoint.append([])
                        for i in range(0,2):
                            Sx[i].append([])
                            Sc[i].append([])
                            qpE[i].append([])
                            Znk[i].append([])
113
114
#                        x,y,z,weight = float(s.split()[3]),float(s.split()[4]),float(s.split()[5]),float(s.split()[6])
                        x,y,z = float(s.split()[3]),float(s.split()[4]),float(s.split()[5])
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                        qpGWKpoint[-1].append(x)
                        qpGWKpoint[-1].append(y)
                        qpGWKpoint[-1].append(z)
                    else:
                        s=s.split()
                        if len(s) == 0:
                            continue
                        else:
                            try: not int(s[0])
                            except ValueError:
                                continue
                            if self.spinTreat:
                                pass
                            else:
                                for i in range(0,2):
130
131
132
                                    Sx[i][-1].append(fromH(float(s[4])))
                                    Sc[i][-1].append(fromH(float(s[5])))
                                    qpE[i][-1].append(fromH(float(s[3])))
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                                    Znk[i][-1].append(float(s[9]))
        backend.addValue("x_exciting_GW_qp_eigenvalues_kpoints", qpGWKpoint)
        backend.addValue("x_exciting_GW_qp_number_of_eigenvalues", len(qpE[0]))
        backend.addValue("x_exciting_GW_qp_number_of_eigenvalues_kpoints", len(qpGWKpoint))
        backend.addValue("x_exciting_GW_qp_eigenvalues_values", qpE)
        backend.addValue("x_exciting_GW_qp_linearization_prefactor", Znk)
        backend.closeSection("x_exciting_section_GW_qp_eigenvalues",eigvalGWGIndex)

        selfGWGIndex = backend.openSection("x_exciting_section_GW_self_energy")
        backend.addValue("x_exciting_GW_self_energy_x", Sx)
        backend.addValue("x_exciting_GW_self_energy_c", Sc)
        backend.closeSection("x_exciting_section_GW_self_energy",selfGWGIndex)

####################DOS######################
147

Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        if os.path.exists(dosGWFile):
            dosGWGIndex = backend.openSection("x_exciting_section_GW_dos")
            with open(dosGWFile) as g:
                dosValues = [[],[]]
                dosEnergies = []
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    s = s.split()
                    ene, value = float(s[0]), float(s[1])
                    dosEnergies.append(ene)
                    if not self.spinTreat:
                        for i in range(0,2):
                            dosValues[i].append(value)
                    else:
                        pass
            backend.addValue("x_exciting_GW_dos_energies", dosEnergies)
            backend.addValue("x_exciting_GW_dos_values", dosValues)
            backend.addValue("x_exciting_GW_number_of_dos_values", len(dosEnergies))
            backend.closeSection("x_exciting_section_GW_dos",dosGWGIndex)        
169

170
171
##################BANDSTRUCTURE#####################

172
        if os.path.exists(bandCarbGWFile):
173
            bandGWGIndex = backend.openSection("x_exciting_section_GW_k_band")
174
#            bandGWSegmGIndex = backend.openSection("x_exciting_section_GW_k_band_segment")
175
176
            fromH = unit_conversion.convert_unit_function("hartree", "J")

177
            with open(bandCarbGWFile) as g:
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
                bandEnergies = [[],[]]
                kpoint = []
                dist = []
                Kindex = [0]
                segmK = []
                segmLength = []
                bandEnergiesSegm = []
                bandGWBE = []
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    s = s.split()                
                    if not self.spinTreat:
                        if len(s) == 0:
                            for i in range(0,2):
                                bandEnergies[i].append([])
                        elif s[0] == "#":
                            for i in range(0,2):
                                bandEnergies[i].append([])
                                numBand = int(s[2])
                                numK = int(s[3])          
                        elif len(s) > 0:
                            for i in range(0,2):
#                                ene = fromH(float(s[6]))
                                bandEnergies[i][-1].append(fromH(float(s[6])))
                            if int(s[0]) == 1:
                                kpoint.append([])
                                dist.append(float(s[5]))
                                kpoint[-1].append([float(s[2]),float(s[3]),float(s[4])])
                    else:
                        pass

                for i in range(0,2):
                    bandEnergies[i].pop()

214
215
216
#                print("numBand=",numBand)
#                print("numK=",numK)

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                for i in range(1,numK):
#                    print("i=",i)
#                    print("dist[i-1]=",dist[i-1])
#                    print("dist[i]=",dist[i])
                    if dist[i] == dist[i-1]:
#                        pass
                        Kindex.append(i)
                Kindex.append(numK)
#                        del dist[i]
                for i in range(0,len(Kindex)-1): 
                    segmK.append(dist[Kindex[i]:Kindex[i+1]])

                for i in range(0,len(segmK)):
#                    print("i=",i)
#                    print("segmK[i]=",segmK[i])
                    segmLength.append(len(segmK[i]))
#                    bandEnergiesSegm.append([])
                for i in range(0,2):
                    bandEnergiesSegm.append([])
                    for j in range(0,numBand):
                         bandEnergiesSegm[i].append([])
                         for k in range (0,len(Kindex)-1):
#                             print("l=",k)
#                             print("len(Kindex)=",len(Kindex))
#                             print("Kindex[l]=",Kindex[k])
#                             print("bandEnergies[Kindex[k]:Kindex[k+1]]=",bandEnergies[Kindex[k]:Kindex[k+1]])
                             bandEnergiesSegm[i][j].append(bandEnergies[i][j][Kindex[k]:Kindex[k+1]])
#                    print("i=",i)
#                    print("Kindex[i]=",Kindex[i])
#                    del dist[Kindex[i]]

#            print("bandEnergies=",bandEnergies)  
#            print("self.vertexDist=",self.vertexDist)
#            print("dist=",dist)
#            print("len(dist)=",len(dist))
#            print("segmK=",segmK)
#            print("segmLength=",segmLength)
#            print("bandEnergiesSegm=",bandEnergiesSegm)
#            print("Kindex=",Kindex)
            for i in range(0,len(Kindex)-1):
                bandGWBE.append([])
                for j in range(0,2):
                    bandGWBE[i].append([])
                    for k in range(0,segmLength[i]):
                        bandGWBE[i][j].append([])
                        for l in range(0,numBand):
                            bandGWBE[i][j][-1].append(bandEnergiesSegm[j][l][i][k])

265
266
#            print("bandGWBE=",bandGWBE)
            for i in range(0,len(Kindex)-1):
267
268
#                print("i=",i)
#                print("len(bandGWBE[i])=",len(bandGWBE[i][0]))
269
                bandGWSegmGIndex = backend.openSection("x_exciting_section_GW_k_band_segment")
270
#                print("bandGWBE[i]=",len(bandGWBE[i][0]))
271
272
273
274
275
276
277
                backend.addValue("x_exciting_GW_band_energies", bandGWBE[i])
                backend.closeSection("x_exciting_section_GW_k_band_segment",bandGWSegmGIndex)

            backend.closeSection("x_exciting_section_GW_k_band",bandGWGIndex)
#            backend.closeSection("x_exciting_section_GW_k_band_segment",bandGWSegmGIndex)

        if os.path.exists(bandBorGWFile) and not os.path.exists(bandCarbGWFile):
278
#            print("QUI???")
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
            bandGWGIndex = backend.openSection("x_exciting_section_GW_k_band")
#            bandGWSegmGIndex = backend.openSection("x_exciting_section_GW_k_band_segment")
            fromH = unit_conversion.convert_unit_function("hartree", "J")
#            fromH = 1.0
            with open(bandBorGWFile) as g:
                bandEnergies = [[[]],[[]]]
                kappa = [[[]],[[]]]
#                kpoint = []
                dist1 = [[]]
                Kindex = [0]
                segmK = []
                segmLength = []
                bandEnergiesSegm = []
                bandGWBE = []
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    s = s.split()
                    if not self.spinTreat:
                        if len(s) == 0:
                            for i in range(0,2):
                                bandEnergies[i].append([])
                                kappa[i].append([])
                            dist1.append([])
304
#                            print("bandEnergies[0]=",bandEnergies[0])
305
306
307
308
309
310
311
312
313
#                            if not dist1:
#                        elif s[0] == "#":
#                            for i in range(0,2):
#                                bandEnergies[i].append([])
#                                numBand = int(s[2])
#                                numK = int(s[3])
                        elif len(s) > 0:
                            for i in range(0,2):
#                                ene = fromH(float(s[6]))
314
                                bandEnergies[i][-1].append(fromH(float(s[1])))
315
316
317
318
319
320
321
322
323
324
                                kappa[i][-1].append(float(s[0]))
#                                bandEnergies[i][-1].append(fromH(float(s[1])))
#                            if int(s[0]) == 1:
#                            if not dist1:
#                                print("s[0]=",s[0])
#                                print("dist1=",dist1)
#                                kpoint.append([])
                            dist1[-1].append(float(s[0]))
#                                kpoint[-1].append([float(s[2]),float(s[3]),float(s[4])])                        
                        numK = len(kappa[0][0])
325
#                        print("numK=",numK)
326
327
328
329
330
331
332
333
334
                        for i in kappa[0][0]:
                            if kappa[0][0].count(i) > 1:
                                kappa[0][0].remove(i)
                    else:
                        pass
                for i in range(0,2):
                    bandEnergies[i].pop()
                numBand = len(bandEnergies[0])
#                numK = len(kappa[0][0])
335
336
#                print("kappa=",numK)
#                print("bandnergies=",numBand)
337
338
#                vertex = 0
                for i in range(1,numK + self.vertexNum-1):
339
#                    print("i=",i)
340
341
#                    print("dist1[i-1]=",dist1[0][i-1])
#                    print("dist1[i]=",dist1[0][i])
342
                    if dist1[0][i] == dist1[0][i-1]:
343
344
#                        vertex +=1
#                        print("vertex=",vertex)
345
346
#                        pass   
                        Kindex.append(i)
347
348
#                print("vertex=",vertex)
                Kindex.append(numK + self.vertexNum-1)
349
350
351
352
353
354
355
#                        del dist1[i]
                for i in range(0,len(Kindex)-1):
                    segmK.append(dist1[0][Kindex[i]:Kindex[i+1]])

                for i in range(0,len(segmK)):
#                    print("i=",i)
#                    print("segmK[i]=",segmK[i])
356
#                    print("len(segmK)=",len(segmK))
357
358
359
360
361
362
363
                    segmLength.append(len(segmK[i]))
#                    bandEnergiesSegm.append([])
                for i in range(0,2):
                    bandEnergiesSegm.append([])
                    for j in range(0,numBand):
                         bandEnergiesSegm[i].append([])
                         for k in range (0,len(Kindex)-1):
364
#                         for k in range (0,len(Kindex)):
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#                             print("l=",k)
#                             print("len(Kindex)=",len(Kindex))
#                             print("Kindex[l]=",Kindex[k])
#                             print("bandEnergies[Kindex[k]:Kindex[k+1]]=",bandEnergies[Kindex[k]:Kindex[k+1]])
                             bandEnergiesSegm[i][j].append(bandEnergies[i][j][Kindex[k]:Kindex[k+1]])
#                    print("i=",i)
#                    print("Kindex[i]=",Kindex[i])
#                    del dist1[Kindex[i]]

#            print("bandEnergies=",bandEnergies)  
#            print("self.vertexDist=",self.vertexDist)
#            print("dist1=",dist1)
#            print("len(dist1)=",len(dist1))
#            print("segmK=",segmK)
#            print("segmLength=",segmLength)
#            print("bandEnergiesSegm=",bandEnergiesSegm)
#            print("Kindex=",Kindex)
            for i in range(0,len(Kindex)-1):
                bandGWBE.append([])
                for j in range(0,2):
                    bandGWBE[i].append([])
                    for k in range(0,segmLength[i]):
                        bandGWBE[i][j].append([])
                        for l in range(0,numBand):
                            bandGWBE[i][j][-1].append(bandEnergiesSegm[j][l][i][k])

391
392
#            print("bandGWBE=",bandGWBE)
            for i in range(0,len(Kindex)-1):
393
                bandGWSegmGIndex = backend.openSection("x_exciting_section_GW_k_band_segment")
394
395
#                print("diomerda")
#                print("bandGWBE[i]=",len(bandGWBE[i][0]))
396
                backend.addValue("x_exciting_GW_band_energies", bandGWBE[i])
397
                backend.closeSection("x_exciting_section_GW_k_band_segment",bandGWSegmGIndex)
398
399
400

            backend.closeSection("x_exciting_section_GW_k_band",bandGWGIndex)
#            backend.closeSection("x_exciting_section_GW_k_band_segment",bandGWSegmGIndex)
401
402
403
404
405
406

def buildGWMatchers():
    return SM(
    name = 'root',
    weak = True,
    startReStr = "\*\s*GW input parameters\s*\*",
407
        sections = ["x_exciting_section_GW", "x_exciting_section_GW_method"],
408
409
410
411
    subMatchers = [
#        SM(name = 'GWinput',
#          startReStr = r"(?P<x_wien2k_system_nameIn>.*)"),
#          startReStr = "\s*GW taskname:\s*"
412
413
        SM(r"\s*(?P<x_exciting_GW_type>[-a-zA-Z0-9]+)\s*-\s*[-a-zA-Z0-9]+\s*run") #,
#        SM(r"\s*(?P<x_exciting_GW_type>[-a-zA-Z0-9]+)\s*-\s*[-a-zA-Z0-9]+\s*run"),
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#        SM(r"\s*(?P<x_wien2k_in2c_switch>[A-Z]+)\s*.*"),
#        SM(r"\s*(?P<x_wien2k_in2c_emin>[-+0-9.]+)\s*(?P<x_wien2k_in2c_ne>[-+0-9.]+)\s*(?P<x_wien2k_in2c_espermin>[-+0-9.]+)\s*(?P<x_wien2k_in2c_esper0>[-+0-9.]+)\s*.*"),
#        SM(r"\s*(?P<x_wien2k_smearing_kind>[A-Z]+)\s*\s*(?P<x_wien2k_smearing_width__rydberg>[-+0-9.]+)\s*.*"),
#        SM(r"\s*(?P<x_wien2k_in2c_gmax>[-+0-9.]+)\s*GMAX")
#        SM(r"\s*GW taskname\:\s*")
     
    ])


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_method': CachingLvl
                              }
    return cachingLevelForMetaName