1#!/usr/local/bin/python3.8
2"""
3GW corrections
4==============
5
6This example shows how to interpolate the GW corrections and use the interpolated
7values to correct the KS band structure computed on a high symmetry k-path and
8the KS energies of a k-mesh. Finally, the KS and the GW results are plotted with matplotlib.
9"""
10import abipy.data as abidata
11from abipy.abilab import abiopen, ElectronBandsPlotter
12
13# Get quasiparticle results from the SIGRES.nc database.
14sigres = abiopen(abidata.ref_file("si_g0w0ppm_nband30_SIGRES.nc"))
15
16# Read the KS band energies computed on the k-path
17with abiopen(abidata.ref_file("si_nscf_GSR.nc")) as gsr_nscf:
18    ks_ebands_kpath = gsr_nscf.ebands
19
20# Read the KS band energies computed on the Monkhorst-Pack (MP) mesh
21# and compute the DOS with the Gaussian method
22with abiopen(abidata.ref_file("si_scf_GSR.nc")) as gsr_scf:
23    ks_ebands_kmesh = gsr_scf.ebands
24
25ks_edos = ks_ebands_kmesh.get_edos()
26
27# Interpolate the QP corrections and use the interpolated values to correct
28# the KS energies stored in `ks_ebands_kpath` and `ks_ebands_kmesh`.
29#
30# The QP energies are returned in r.qp_ebands_kpath and r.qp_ebands_kmesh.
31# Note that the KS energies are optional but this is the recommended approach
32# because the code will interpolate the corrections instead of the QP energies.
33
34r = sigres.interpolate(lpratio=5,
35                       ks_ebands_kpath=ks_ebands_kpath,
36                       ks_ebands_kmesh=ks_ebands_kmesh
37                       )
38qp_edos = r.qp_ebands_kmesh.get_edos()
39
40# Get points with ab-initio QP energies from the SIGRES so that
41# we can plot the interpolate interpolated QP band structure with the first principles results.
42# This part is optional
43points = sigres.get_points_from_ebands(r.qp_ebands_kpath, size=24)
44r.qp_ebands_kpath.plot(points=points, with_gaps=True)
45#raise ValueError()
46
47# Shortcut: pass the name of the GSR files directly.
48#r = sigres.interpolate(ks_ebands_kpath=abidata.ref_file("si_nscf_GSR.nc"),
49#                       ks_ebands_kmesh=abidata.ref_file("si_scf_GSR.nc"))
50#ks_edos = r.ks_ebands_kmesh.get_edos()
51#qp_edos = r.qp_ebands_kmesh.get_edos()
52
53# Use ElectronBandsPlotter to plot the KS and the QP band structure with matplotlib.
54plotter = ElectronBandsPlotter()
55plotter.add_ebands("LDA", ks_ebands_kpath, edos=ks_edos)
56plotter.add_ebands("GW (interpolated)", r.qp_ebands_kpath, edos=qp_edos)
57
58# Get pandas dataframe with band structure parameters.
59#df = plotter.get_ebands_frame()
60#print(df)
61
62# By default, the two band energies are shifted wrt to *their* fermi level.
63# Use e=0 if you don't want to shift the eigenvalus
64# so that it's possible to visualize the QP corrections.
65plotter.combiplot(title="Combiplot")
66plotter.boxplot(swarm=True, title="Boxplot")
67plotter.combiboxplot(swarm=True, title="Combiboxplot")
68# sphinx_gallery_thumbnail_number = 4
69plotter.gridplot(title="Gridplot", with_gaps=True)
70
71sigres.close()
72