exciting_parser_gw.py 18.4 KB
Newer Older
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
1
# Copyright 2017-2018 Lorenzo Pardini
2
3
4
5
6
7
8
9
10
11
12
13
#
# 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.
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
14

15
16
from builtins import object
import setup_paths
17
import xml.sax
18
19
20
21
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
22
import os, sys, json, logging, exciting_parser_input
23
24

################################################################
25
# This is the subparser for the exciting GW output
26
27
28
################################################################


Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
29
class GWParser(object):
30
31
32
33
    """context for wien2k In2 parser"""

    def __init__(self):
        self.spinTreat = None
34
35
        self.vertexDist = []
        self.vertexLabels = []
36
        self.vertexNum = 0
37
38
39
40
41
42
43
44
        self.parser = None
        self.secSingleConfIndex = None

    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()
45

Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
46
    def parseGW(self, gwFile, backend,  dftMethodSectionGindex, dftSingleConfigurationGindex, xcName, unitCellVol,gmaxvr):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
47
#        logging.error("GW onClose_section_single_configuration_calculation")
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
48
        self.gmaxvr = float(gmaxvr[0])
49
        self.unitCellVol = float(unitCellVol[0])
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
50
51
52
53
54
55
        backend.openNonOverlappingSection("section_single_configuration_calculation")
        if dftSingleConfigurationGindex is not None:
            backend.openNonOverlappingSection("section_calculation_to_calculation_refs")
            backend.addValue("calculation_to_calculation_ref", dftSingleConfigurationGindex)
            backend.addValue("calculation_to_calculation_kind", "starting_point")
            backend.closeNonOverlappingSection("section_calculation_to_calculation_refs")
56

Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
57
        dirPath = os.path.dirname(gwFile)
58
59
60
61
62
63
        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
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
64
        dosGWFile = os.path.join(dirPath, "TDOS-QP.OUT")
65
66
        bandCarbGWFile = os.path.join(dirPath, "bandstructure-qp.dat")
        bandBorGWFile = os.path.join(dirPath, "BAND-QP.OUT")
67
68
        vertexGWFile = os.path.join(dirPath, "BANDLINES.OUT")
        vertexLabGWFile = os.path.join(dirPath, "bandstructure.xml")
69
        selfCorGWFile = os.path.join(dirPath, "SELFC.DAT")
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
70
        inputFile = os.path.join(dirPath, "input.xml")
71
72
        inputgw1File = os.path.join(dirPath, "input-gw.xml")
        inputgw2File = os.path.join(dirPath, "input_gw.xml")
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
73
74

        if os.path.exists(inputFile):
75
76
77
78
79
80
81
82
83
84
85
86
            if os.path.exists(inputgw1File):
                inputgwFile = inputgw1File
            elif os.path.exists(inputgw2File):
                inputgwFile = inputgw2File
            else:
                inputgwFile = inputFile
        elif os.path.exists(inputgw1File):
            inputgwFile = inputgw1File
        elif os.path.exists(inputgw2File):
            inputgwFile = inputgw2File

        if os.path.exists(inputgwFile):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
87
88
            selfGWSetGIndex = backend.openSection("section_method")
            backend.addValue('electronic_structure_method', "G0W0")
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
89
            backend.addValue('gw_starting_point', xcName)
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
90
91
92
93
94
            if dftMethodSectionGindex is not None:
                m2mGindex = backend.openNonOverlappingSection("section_method_to_method_refs")
                backend.addValue("method_to_method_ref", dftMethodSectionGindex)
                backend.addValue("method_to_method_kind", "starting_point")
                backend.closeNonOverlappingSection("section_method_to_method_refs")
95
96
                with open(inputgwFile) as f:
                    exciting_parser_input.parseInput(f, backend, self.gmaxvr)
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
97
            backend.closeSection("section_method",selfGWSetGIndex)
98

99
100
101
102
103
104
105
106
107
108
109
110
        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]))
111
                self.vertexNum = len(self.vertexDist)-1
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
112

113
114
115
116
117
118
119
120
121
122
        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])
123

124
        if os.path.exists(eigvalGWFile):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
125
            eigvalGWGIndex = backend.openSection("section_eigenvalues")
126
127
            with open(eigvalGWFile) as g:
                qpGWKpoint=[]
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
128
                Vxc = [[],[]]
129
130
131
132
133
134
135
136
137
138
139
140
                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):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
141
                            Vxc[i].append([])
142
143
144
145
                            Sx[i].append([])
                            Sc[i].append([])
                            qpE[i].append([])
                            Znk[i].append([])
146
                        x,y,z = float(s.split()[3]),float(s.split()[4]),float(s.split()[5])
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
                        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):
162
163
164
                                    Sx[i][-1].append(fromH(float(s[4])))
                                    Sc[i][-1].append(fromH(float(s[5])))
                                    qpE[i][-1].append(fromH(float(s[3])))
165
                                    Znk[i][-1].append(float(s[9]))
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
166
                                    Vxc[i][-1].append(fromH(float(s[6])))
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
167
168
169
170
        backend.addValue("eigenvalues_kpoints", qpGWKpoint)
        backend.addValue("number_of_eigenvalues", len(qpE[0]))
        backend.addValue("number_of_eigenvalues_kpoints", len(qpGWKpoint))
        backend.addValue("eigenvalues_values", qpE)
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
171
        backend.addValue("gw_qp_linearization_prefactor", Znk)
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
172
        backend.closeSection("section_eigenvalues",eigvalGWGIndex)
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
173
174
175
        backend.addValue("gw_self_energy_x", Sx)
        backend.addValue("gw_self_energy_c", Sc)
        backend.addValue("gw_xc_potential", Vxc)
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
176
177

        ####################DOS######################
178

Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
179
        if os.path.exists(dosGWFile):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
180
181
            dosGWGIndex = backend.openSection("section_dos")
            ha_per_joule = unit_conversion.convert_unit(1, "hartree", "J")
182
            fromH = unit_conversion.convert_unit_function("hartree", "J")
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
183
184
185
186
187
188
189
190
            with open(dosGWFile) as g:
                dosValues = [[],[]]
                dosEnergies = []
                while 1:
                    s = g.readline()
                    if not s: break
                    s = s.strip()
                    s = s.split()
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
191
                    ene, value = fromH(float(s[0])), float(s[1])*ha_per_joule
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
192
193
194
195
196
197
                    dosEnergies.append(ene)
                    if not self.spinTreat:
                        for i in range(0,2):
                            dosValues[i].append(value)
                    else:
                        pass
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
198
199
200
201
202
203
            backend.addValue("dos_energies", dosEnergies)
            backend.addValue("dos_values", dosValues)
            backend.addValue("number_of_dos_values", len(dosEnergies))
            backend.closeSection("section_dos",dosGWGIndex)        

        ##################BANDSTRUCTURE#####################
204

205
        if os.path.exists(bandCarbGWFile):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
206
            bandGWGIndex = backend.openSection("section_k_band")
207
208
            fromH = unit_conversion.convert_unit_function("hartree", "J")

209
            with open(bandCarbGWFile) as g:
210
211
212
213
214
215
216
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
265
266
267
268
                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):
                                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()

                for i in range(1,numK):
                    if dist[i] == dist[i-1]:
                        Kindex.append(i)
                Kindex.append(numK)
                for i in range(0,len(Kindex)-1): 
                    segmK.append(dist[Kindex[i]:Kindex[i+1]])

                for i in range(0,len(segmK)):
                    segmLength.append(len(segmK[i]))
                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):
                             bandEnergiesSegm[i][j].append(bandEnergies[i][j][Kindex[k]:Kindex[k+1]])
            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])

269
            for i in range(0,len(Kindex)-1):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
270
271
272
                bandGWSegmGIndex = backend.openSection("section_k_band_segment")
                backend.addValue("band_energies", bandGWBE[i])
                backend.closeSection("section_k_band_segment",bandGWSegmGIndex)
273

Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
274
            backend.closeSection("section_k_band",bandGWGIndex)
275
276

        if os.path.exists(bandBorGWFile) and not os.path.exists(bandCarbGWFile):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
277
            bandGWGIndex = backend.openSection("section_k_band")
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
            fromH = unit_conversion.convert_unit_function("hartree", "J")
            with open(bandBorGWFile) as g:
                bandEnergies = [[[]],[[]]]
                kappa = [[[]],[[]]]
                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([])
                        elif len(s) > 0:
                            for i in range(0,2):
301
                                bandEnergies[i][-1].append(fromH(float(s[1])))
302
303
304
305
306
307
308
309
310
311
312
                                kappa[i][-1].append(float(s[0]))
                            dist1[-1].append(float(s[0]))
                        numK = len(kappa[0][0])
                        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])
313
                for i in range(1,numK + self.vertexNum-1):
314
315
                    if dist1[0][i] == dist1[0][i-1]:
                        Kindex.append(i)
316
                Kindex.append(numK + self.vertexNum-1)
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
                for i in range(0,len(Kindex)-1):
                    segmK.append(dist1[0][Kindex[i]:Kindex[i+1]])

                for i in range(0,len(segmK)):
                    segmLength.append(len(segmK[i]))
                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):
                             bandEnergiesSegm[i][j].append(bandEnergies[i][j][Kindex[k]:Kindex[k+1]])
            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])

337
            for i in range(0,len(Kindex)-1):
Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
338
339
340
                bandGWSegmGIndex = backend.openSection("section_k_band_segment")
                backend.addValue("band_energies", bandGWBE[i])
                backend.closeSection("section_k_band_segment",bandGWSegmGIndex)
341

Pardini, Lorenzo (lopa)'s avatar
Pardini, Lorenzo (lopa) committed
342
343
344
            backend.closeSection("section_k_band",bandGWGIndex)
        backend.closeNonOverlappingSection("section_single_configuration_calculation")
#        logging.error("done GW onClose_section_single_configuration_calculation")
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#    def onOpen_section_method(self, backend, gIndex, section):
#        fava = section["gw_frequency_number"]
#        print("fava=",fava)

#    def onClose_section_single_configuration_calculation(self, backend, gIndex, section):
#        if dftSingleConfigurationGindex is not None:
##        if self.secSingleConfIndex is None:
##            self.secSingleConfIndex = gIndex
##            singleGIndex = backend.openSection("section_single_configuration_calculation")
#            fermi = section["gw_fermi_energy"]
#            fundamental = section["gw_fundamental_gap"]
#            optical = section["gw_optical_gap"]
#            backend.addValue("gw_fermi_energy", fermi)
#            backend.addValue("gw_fundamental_gap", fundamental)
#            backend.addValue("gw_optical_gap", optical)
##            backend.closeSection("section_single_configuration_calculation",singleGIndex)
#        else:
#            singleGIndex = backend.openSection("section_single_configuration_calculation")
#            fermi = section["gw_fermi_energy"]
#            fundamental = section["gw_fundamental_gap"]
#            optical = section["gw_optical_gap"]
#            backend.addValue("gw_fermi_energy", fermi)
#            backend.addValue("gw_fundamental_gap", fundamental)
 #           backend.addValue("gw_optical_gap", optical)
 #           backend.closeSection("section_single_configuration_calculation",singleGIndex)

def buildGWMatchers():     
    return SM(     
    name = 'root',     
    weak = True,    
    startReStr = "\=\s*Main GW output file\s*\=",
#    sections = ["section_run"], 
    subMatchers = [
    SM(
      startReStr = "\-\s*frequency grid\s*\-",
      endReStr = "\-\s*Peak memory estimate \(Mb, per process\)\:\s*\-",
      sections = ["section_method"],
      subMatchers = [
#        SM(r"\s*Type\:\s*\<\s*(?P<gw_dummy>[-a-zA-Z]+)\s*\>"),
        SM(r"\s*(?P<gw_frequency_number>[0-9]+)\s*(?P<gw_frequency_values__hartree>[0-9]\.[0-9]*([E]?[-]?[-0-9]+))\s*(?P<gw_frequency_weights>[0-9]\.[0-9]*([E]?[-]?[-0-9]+))", repeats = True)
    ]),
    SM(
      startReStr = "\-\s*G0W0\s*\-",
      endReStr = "\=\s*GW timing info \(seconds\)\s*\=",        
      sections = ["section_single_configuration_calculation"],     
      subMatchers = [
        SM(r"\s*Fermi energy\:\s*(?P<gw_fermi_energy__hartree>[-0-9.]+)"),
        SM(r"\s* Fundamental BandGap \(eV\)\:\s*(?P<gw_fundamental_gap__eV>[0-9.]+)"),         
        SM(r"\s*Direct BandGap \(eV\)\:\s*(?P<gw_fundamental_gap__eV>[0-9.]+)"),
        SM(r"\s* Optical BandGap \(eV\)\:\s*(?P<gw_optical_gap__eV>[0-9.]+)")
    ])
    ])
def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl):
    cachingLevelForMetaName = {}
#                                'section_single_configuration_calculation': CachingLvl
#                               }
#    cachingLevelForMetaName["gw_fundamental_gap"] = CachingLevel.Cache
#    cachingLevelForMetaName["gw_optical_gap"] = CachingLevel.Cache
#    cachingLevelForMetaName["gw_fermi_energy"] = CachingLevel.Cache
    return cachingLevelForMetaName