def test_interpolator(self): """Test QP interpolation.""" from abipy.abilab import abiopen, ElectronBandsPlotter # Get quasiparticle results from the SIGRES.nc database. sigres = abiopen(abidata.ref_file("si_g0w0ppm_nband30_SIGRES.nc")) # Interpolate QP corrections and apply them on top of the KS band structures. # QP band energies are returned in r.qp_ebands_kpath and r.qp_ebands_kmesh. r = sigres.interpolate( lpratio=5, ks_ebands_kpath=abidata.ref_file("si_nscf_GSR.nc"), ks_ebands_kmesh=abidata.ref_file("si_scf_GSR.nc"), verbose=0, filter_params=[1.0, 1.0], line_density=10) assert r.qp_ebands_kpath is not None assert r.qp_ebands_kpath.kpoints.is_path #print(r.qp_ebands_kpath.kpoints.ksampling, r.qp_ebands_kpath.kpoints.mpdivs_shifts) assert r.qp_ebands_kpath.kpoints.mpdivs_shifts == (None, None) assert r.qp_ebands_kmesh is not None assert r.qp_ebands_kmesh.kpoints.is_ibz assert r.qp_ebands_kmesh.kpoints.ksampling is not None assert r.qp_ebands_kmesh.kpoints.is_mpmesh qp_mpdivs, qp_shifts = r.qp_ebands_kmesh.kpoints.mpdivs_shifts assert not ((qp_mpdivs, qp_shifts) == (None, None)) ks_mpdivs, ks_shifts = r.ks_ebands_kmesh.kpoints.mpdivs_shifts self.assert_equal(qp_mpdivs, ks_mpdivs) self.assert_equal(qp_shifts, ks_shifts) # Get DOS from interpolated energies. ks_edos = r.ks_ebands_kmesh.get_edos() qp_edos = r.qp_ebands_kmesh.get_edos() r.qp_ebands_kmesh.to_bxsf(self.get_tmpname(text=True)) # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() plotter.add_ebands("LDA", r.ks_ebands_kpath, dos=ks_edos) plotter.add_ebands("GW (interpolated)", r.qp_ebands_kpath, dos=qp_edos) if self.has_matplotlib(): plotter.combiplot(title="Silicon band structure", show=False) plotter.gridplot(title="Silicon band structure", show=False) sigres.close()
def plot_qpbands(self, bands_filepath, bands_label=None, dos_filepath=None, dos_args=None, **kwargs): """ Correct the energies found in the netcdf file bands_filepath and plot the band energies (both the initial and the corrected ones) with matplotlib. The plot contains the KS and the QP DOS if dos_filepath is not None. Args: bands_filepath: Path to the netcdf file containing the initial KS energies to be corrected. bands_label String used to label the KS bands in the plot. dos_filepath: Optional path to a netcdf file with the initial KS energies on a homogeneous k-mesh (used to compute the KS and the QP dos) dos_args: Dictionary with the arguments passed to get_dos to compute the DOS Used if dos_filepath is not None. kwargs: Options passed to the plotter. Returns: matplotlib figure """ from abipy.abilab import abiopen, ElectronBandsPlotter # Read the KS band energies from bands_filepath and apply the scissors operator. with abiopen(bands_filepath) as ncfile: ks_bands = ncfile.ebands qp_bands = ks_bands.apply_scissors(self._scissors_spin) # Read the band energies computed on the Monkhorst-Pack (MP) mesh and compute the DOS. ks_dos, qp_dos = None, None if dos_filepath is not None: with abiopen(dos_filepath) as ncfile: ks_mpbands = ncfile.ebands dos_args = {} if not dos_args else dos_args ks_dos = ks_mpbands.get_edos(**dos_args) # Compute the DOS with the modified QPState energies. qp_mpbands = ks_mpbands.apply_scissors(self._scissors_spin) qp_dos = qp_mpbands.get_edos(**dos_args) # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() bands_label = bands_label if bands_label is not None else os.path.basename(bands_filepath) plotter.add_ebands(bands_label, ks_bands, dos=ks_dos) plotter.add_ebands(bands_label + " + scissors", qp_bands, dos=qp_dos) return plotter.plot(**kwargs)
def test_scissors_polyfit(self): """Testing scissors from SIGRES file.""" # Get the quasiparticle results from the SIGRES.nc database. sigma_file = abiopen(abidata.ref_file("si_g0w0ppm_nband30_SIGRES.nc")) qplist_spin = sigma_file.qplist_spin sigma_file.close() # Construct the scissors operator domains = [[-10, 6.1], [6.1, 18]] scissors = qplist_spin[0].build_scissors(domains, bounds=None) # Read the KS band energies computed on the k-path with abiopen(abidata.ref_file("si_nscf_GSR.nc")) as nc: ks_bands = nc.ebands # Read the KS band energies computed on the Monkhorst-Pack (MP) mesh # and compute the DOS with the Gaussian method with abiopen(abidata.ref_file("si_scf_GSR.nc")) as nc: ks_mpbands = nc.ebands ks_dos = ks_mpbands.get_edos() # Apply the scissors operator first on the KS band structure # along the k-path then on the energies computed with the MP mesh. qp_bands = ks_bands.apply_scissors(scissors) qp_mpbands = ks_mpbands.apply_scissors(scissors) # Compute the DOS with the modified QPState energies. qp_dos = qp_mpbands.get_edos() # Plot the LDA and the QPState band structure with matplotlib. if self.has_matplotlib(): plotter = ElectronBandsPlotter() plotter.add_ebands("LDA", ks_bands, dos=ks_dos) plotter.add_ebands("LDA+scissors(e)", qp_bands, dos=qp_dos) # By default, the two band energies are shifted wrt to *their* fermi level. # Use e=0 if you don't want to shift the eigenvalus # so that it's possible to visualize the QP corrections. plotter.combiplot(title="Silicon band structure") plotter.gridplot(title="Silicon band structure")
#!/usr/bin/env python """ This example shows how to plot several band structures on a grid We use two GSR files: si_scf_GSR.n: energies on a homogeneous sampling of the BZ (can be used to compute DOS) si_nscf_GSR.nc: energies on a k-path in the BZ (used to plot the band dispersion) """ from abipy.abilab import ElectronBandsPlotter from abipy.data import ref_file # To plot a grid with two band structures: plotter = ElectronBandsPlotter() plotter.add_ebands("BZ sampling", ref_file("si_scf_GSR.nc")) plotter.add_ebands("k-path", ref_file("si_nscf_GSR.nc")) frame = plotter.get_ebands_frame() print(frame) plotter.gridplot(e0=None) #plotter.animate() # To plot a grid with band structures + DOS, use the optional argument `edos_objects` # The first subplot will get the band dispersion from eb_objects[0] and the dos from edos_objects[0] # edos_kwargs is an optional dictionary with the arguments that will be passed to `get_dos` to compute the DOS. #eb_objects = 2 * [ref_file("si_nscf_GSR.nc")] #edos_objects = 2 * [ref_file("si_scf_GSR.nc")] #plotter = ElectronBandsPlotter() #plotter.add_ebands("Si", ref_file("si_nscf_GSR.nc"), dos=ref_file("si_scf_GSR.nc")) #plotter.add_ebands("Same data", ref_file("si_nscf_GSR.nc"), dos=ref_file("si_scf_GSR.nc")) #plotter.gridplot()
# Read the KS band energies computed on the Monkhorst-Pack (MP) mesh # and compute the DOS with the Gaussian method ks_mpbands = abiopen(abidata.ref_file("si_scf_GSR.nc")).ebands ks_dos = ks_mpbands.get_edos() # Apply the scissors operator first on the KS band structure # along the k-path then on the energies computed with the MP mesh. qp_bands = ks_bands.apply_scissors(scissors) qp_mpbands = ks_mpbands.apply_scissors(scissors) # Compute the DOS with the modified QPState energies. qp_dos = qp_mpbands.get_edos() # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() plotter.add_ebands("LDA", ks_bands, dos=ks_dos) plotter.add_ebands("LDA+scissors(e)", qp_bands, dos=qp_dos) # Define the mapping reduced_coordinates -> name of the k-point. klabels = { (0.5, 0.0, 0.0): "L", (0.0, 0.0, 0.0): "$\Gamma$", (0.0, 0.5, 0.5): "X", } plotter.plot(title="Silicon band structure", klabels=klabels)
ks_mpbands = abiopen(abidata.ref_file("si_scf_GSR.nc")).ebands ks_dos = ks_mpbands.get_edos() # Apply the scissors operator first on the KS band structure # along the k-path then on the energies computed with the MP mesh. qp_bands = ks_bands.apply_scissors(scissors) qp_mpbands = ks_mpbands.apply_scissors(scissors) # Compute the DOS with the modified QPState energies. qp_dos = qp_mpbands.get_edos() # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() plotter.add_ebands("LDA", ks_bands, dos=ks_dos) plotter.add_ebands("LDA+scissors(e)", qp_bands, dos=qp_dos) # Define the mapping reduced_coordinates -> name of the k-point. klabels = { (0.5, 0.0, 0.0): "L", (0.0, 0.0, 0.0): "$\Gamma$", (0.0, 0.5, 0.5): "X", } plotter.plot(title="Silicon band structure", klabels=klabels) except (IOError, OSError): print('no abinit output found')
def plot_qpbands(self, bands_filepath, bands_label=None, dos_filepath=None, dos_args=None, **kwargs): """ Correct the energies found in the netcdf file bands_filepath and plot the band energies (both the initial and the corrected ones) with matplotlib. The plot contains the KS and the QP DOS if dos_filepath is not None. Args: bands_filepath: Path to the netcdf file containing the initial KS energies to be corrected. bands_label String used to label the KS bands in the plot. dos_filepath: Optional path to a netcdf file with the initial KS energies on a homogeneous k-mesh (used to compute the KS and the QP dos) dos_args: Dictionary with the arguments passed to get_dos to compute the DOS Used if dos_filepath is not None. kwargs: Options passed to the plotter. Return: |matplotlib-Figure| """ from abipy.abilab import abiopen, ElectronBandsPlotter # Read the KS band energies from bands_filepath and apply the scissors operator. with abiopen(bands_filepath) as ncfile: ks_bands = ncfile.ebands #structure = ncfile.structure qp_bands = ks_bands.apply_scissors(self._scissors_spin) # Read the band energies computed on the Monkhorst-Pack (MP) mesh and compute the DOS. ks_dos, qp_dos = None, None if dos_filepath is not None: with abiopen(dos_filepath) as ncfile: ks_mpbands = ncfile.ebands dos_args = {} if not dos_args else dos_args ks_dos = ks_mpbands.get_edos(**dos_args) # Compute the DOS with the modified QPState energies. qp_mpbands = ks_mpbands.apply_scissors(self._scissors_spin) qp_dos = qp_mpbands.get_edos(**dos_args) # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() bands_label = bands_label if bands_label is not None else os.path.basename( bands_filepath) plotter.add_ebands(bands_label, ks_bands, edos=ks_dos) plotter.add_ebands(bands_label + " + scissors", qp_bands, edos=qp_dos) #qp_marker: if int > 0, markers for the ab-initio QP energies are displayed. e.g qp_marker=50 #qp_marker = 50 #if qp_marker is not None: # # Compute correspondence between the k-points in qp_list and the k-path in qp_bands. # # TODO # # WARNING: strictly speaking one should check if qp_kpoint is in the star of k-point. # # but compute_star is too slow if written in pure python. # x, y, s = [], [], [] # for ik_path, kpoint in enumerate(qp_bands.kpoints): # #kstar = kpoint.compute_star(structure.fm_symmops) # for spin in range(self.nsppol): # for ik_qp, qp in enumerate(self._qps_spin[spin]): # #if qp.kpoint in kstar: # if qp.kpoint == kpoint: # x.append(ik_path) # y.append(np.real(qp.qpe)) # s.append(qp_marker) # plotter.set_marker("ab-initio QP", [x, y, s]) return plotter.combiplot(**kwargs)
# because the code will interpolate the corrections instead of the QP energies. r = sigres.interpolate(lpratio=5, ks_ebands_kpath=ks_ebands_kpath, ks_ebands_kmesh=ks_ebands_kmesh) qp_edos = r.qp_ebands_kmesh.get_edos() # Shortcut: pass the name of the GSR files directly. #r = sigres.interpolate(ks_ebands_kpath=abidata.ref_file("si_nscf_GSR.nc"), # ks_ebands_kmesh=abidata.ref_file("si_scf_GSR.nc")) #ks_edos = r.ks_ebands_kmesh.get_edos() #qp_edos = r.qp_ebands_kmesh.get_edos() # Use ElectronBandsPlotter to plot the KS and the QP band structure with matplotlib. plotter = ElectronBandsPlotter() plotter.add_ebands("LDA", ks_ebands_kpath, dos=ks_edos) plotter.add_ebands("GW (interpolated)", r.qp_ebands_kpath, dos=qp_edos) # Get pandas dataframe with band structure parameters. #df = plotter.get_ebands_frame() #print(df) # By default, the two band energies are shifted wrt to *their* fermi level. # Use e=0 if you don't want to shift the eigenvalus # so that it's possible to visualize the QP corrections. plotter.combiplot(title="Combiplot") plotter.boxplot(swarm=True, title="Boxplot") plotter.combiboxplot(swarm=True, title="Combiboxplot") # sphinx_gallery_thumbnail_number = 4 plotter.gridplot(title="Gridplot")
#!/usr/bin/env python """ This example shows how to plot several band structures on a grid We use two GSR files: si_scf_GSR.n: energies on a homogeneous sampling of the BZ (can be used to compute DOS) si_nscf_GSR.nc: energies on a k-path in the BZ (used to plot the band dispersion) """ from abipy.abilab import ElectronBandsPlotter from abipy.data import ref_file # To plot a grid with two band structures: plotter = ElectronBandsPlotter() plotter.add_ebands("BZ sampling", ref_file("si_scf_GSR.nc")) plotter.add_ebands("k-path", ref_file("si_nscf_GSR.nc")) # Get pandas dataframe frame = plotter.get_ebands_frame() print(frame) plotter.gridplot() #plotter.animate() # To plot a grid with band structures + DOS, use the optional argument `edos_objects` # The first subplot will get the band dispersion from eb_objects[0] and the DOS from edos_objects[0] # edos_kwargs is an optional dictionary with the arguments that will be passed to `get_dos` to compute the DOS. eb_objects = 2 * [ref_file("si_nscf_GSR.nc")] edos_objects = 2 * [ref_file("si_scf_GSR.nc")] plotter = ElectronBandsPlotter() plotter.add_ebands("Si", ref_file("si_nscf_GSR.nc"), dos=ref_file("si_scf_GSR.nc"))
# Get points with ab-initio QP energies from the SIGRES so that # we can plot the interpolate interpolated QP band structure with the first principles results. # This part is optional points = sigres.get_points_from_ebands(r.qp_ebands_kpath, size=24) r.qp_ebands_kpath.plot(points=points, with_gaps=True) #raise ValueError() # Shortcut: pass the name of the GSR files directly. #r = sigres.interpolate(ks_ebands_kpath=abidata.ref_file("si_nscf_GSR.nc"), # ks_ebands_kmesh=abidata.ref_file("si_scf_GSR.nc")) #ks_edos = r.ks_ebands_kmesh.get_edos() #qp_edos = r.qp_ebands_kmesh.get_edos() # Use ElectronBandsPlotter to plot the KS and the QP band structure with matplotlib. plotter = ElectronBandsPlotter() plotter.add_ebands("LDA", ks_ebands_kpath, edos=ks_edos) plotter.add_ebands("GW (interpolated)", r.qp_ebands_kpath, edos=qp_edos) # Get pandas dataframe with band structure parameters. #df = plotter.get_ebands_frame() #print(df) # By default, the two band energies are shifted wrt to *their* fermi level. # Use e=0 if you don't want to shift the eigenvalus # so that it's possible to visualize the QP corrections. plotter.combiplot(title="Combiplot") plotter.boxplot(swarm=True, title="Boxplot") plotter.combiboxplot(swarm=True, title="Combiboxplot") # sphinx_gallery_thumbnail_number = 4 plotter.gridplot(title="Gridplot", with_gaps=True)
def band_plots(self, query=None, sort='system'): """ Plot the scissored bands for all the systems in the DB :param query: :param sort: :return: """ if query is None: query = {} for item in self.col.find(query).sort(sort): exclude = ['ZnO_mp-2133', 'K3Sb_mp-14017', 'NiP2_mp-486', 'OsS2_mp-20905', 'PbO_mp-1336', 'Rb3Sb_mp-16319', 'As2Os_mp-2455'] if item['system'] in exclude: continue if False: print('System : ', item['system'].split('_mp-')[0]) print('Ps : ', item['ps']) print('extra : ', item['extra_vars']) print('gwresults : ', item['gw_results']) try: data = self.gfs.get(item['results_file']).read() if len(data) > 1000: srf = MySigResFile(data) title = "QPState corrections of " + str(item['system']) + "\nusing " \ + str(item['ps'].split('/')[-2]) if item['extra_vars'] is not None: title += ' and ' + str(self.fix_extra(item['extra_vars'])) srf.plot_scissor(title=title) srf.print_gap_info() sc = srf.get_scissor() with self.gfs.get(item['ksbands_file']) as f: ksb = MyBandsFile(f.read()).ebands qpb = ksb.apply_scissors(sc) # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() plotter.add_ebands("KS", ksb) plotter.add_ebands("KS+scissors(e)", qpb) fig = plotter.plot(align='cbm', ylim=(-5, 10), title="%s Bandstructure" % item['system'].split('_mp-')[0]) try: if not item['tgwgap']: bands_data = self.gfs.get(item['ksbands_file']) ks_bands = MyBandsFile(bands_data) item['tkshomo'] = ks_bands.h**o item['tkslumo'] = ks_bands.lumo item['tksgap'] = ks_bands.lumo - ks_bands.h**o item['tgwhomo'] = ks_bands.h**o + sc.apply(ks_bands.h**o) item['tgwlumo'] = ks_bands.lumo + sc.apply(ks_bands.lumo) item['tgwgap'] = item['tgwlumo'] - item['tgwhomo'] self.col.save(item) except KeyError: pass except (KeyError, IOError, NoFile): print('No Sigres file in DataBase') try: return fig except UnboundLocalError: return None
def plot_qpbands(self, bands_filepath, bands_label=None, dos_filepath=None, dos_args=None, qp_marker=None, **kwargs): """ Correct the energies found in the netcdf file bands_filepath and plot the band energies (both the initial and the corrected ones) with matplotlib. The plot contains the KS and the QP DOS if dos_filepath is not None. Args: bands_filepath: Path to the netcdf file containing the initial KS energies to be corrected. bands_label String used to label the KS bands in the plot. dos_filepath: Optional path to a netcdf file with the initial KS energies on a homogeneous k-mesh (used to compute the KS and the QP dos) dos_args: Dictionary with the arguments passed to get_dos to compute the DOS Used if dos_filepath is not None. qp_marker: if int > 0, markers for the ab-initio QP energies are displayed. e.g qp_marker=50 kwargs: Options passed to the plotter. Returns: matplotlib figure """ from abipy.abilab import abiopen, ElectronBandsPlotter # Read the KS band energies from bands_filepath and apply the scissors operator. with abiopen(bands_filepath) as ncfile: ks_bands = ncfile.ebands #structure = ncfile.structure qp_bands = ks_bands.apply_scissors(self._scissors_spin) # Read the band energies computed on the Monkhorst-Pack (MP) mesh and compute the DOS. ks_dos, qp_dos = None, None if dos_filepath is not None: with abiopen(dos_filepath) as ncfile: ks_mpbands = ncfile.ebands dos_args = {} if not dos_args else dos_args ks_dos = ks_mpbands.get_edos(**dos_args) # Compute the DOS with the modified QPState energies. qp_mpbands = ks_mpbands.apply_scissors(self._scissors_spin) qp_dos = qp_mpbands.get_edos(**dos_args) # Plot the LDA and the QPState band structure with matplotlib. plotter = ElectronBandsPlotter() bands_label = bands_label if bands_label is not None else os.path.basename(bands_filepath) plotter.add_ebands(bands_label, ks_bands, dos=ks_dos) plotter.add_ebands(bands_label + " + scissors", qp_bands, dos=qp_dos) #qp_marker = 50 if qp_marker is not None: # Compute correspondence between the k-points in qp_list and the k-path in qp_bands. # TODO # WARNING: strictly speaking one should check if qp_kpoint is in the star of k-point. # but compute_star is too slow if written in pure python. x, y, s = [], [], [] for ik_path, kpoint in enumerate(qp_bands.kpoints): #kstar = kpoint.compute_star(structure.fm_symmops) for spin in range(self.nsppol): for ik_qp, qp in enumerate(self._qps_spin[spin]): if qp.kpoint == kpoint: #if qp.kpoint in kstar: x.append(ik_path) y.append(np.real(qp.qpe)) s.append(qp_marker) plotter.set_marker("ab-initio QP", [x, y, s]) return plotter.plot(**kwargs)
ElectronBandsPlotter ==================== This example shows how to plot several band structures on a grid We use two GSR files: si_scf_GSR.n: energies on a homogeneous sampling of the BZ (can be used to compute DOS) si_nscf_GSR.nc: energies on a k-path in the BZ (used to plot the band dispersion) """ from abipy.abilab import ElectronBandsPlotter from abipy.data import ref_file # To plot a grid with two band structures: plotter = ElectronBandsPlotter() plotter.add_ebands("BZ sampling", ref_file("si_scf_GSR.nc")) plotter.add_ebands("k-path", ref_file("si_nscf_GSR.nc")) # Get pandas dataframe frame = plotter.get_ebands_frame() print(frame) plotter.gridplot() #plotter.animate() # To plot a grid with band structures + DOS, use the optional argument `edos_objects` # The first subplot gets the band dispersion from eb_objects[0] and the DOS from edos_objects[0] # edos_kwargs is an optional dictionary passed to `get_dos` to compute the DOS. eb_objects = 2 * [ref_file("si_nscf_GSR.nc")] edos_objects = 2 * [ref_file("si_scf_GSR.nc")]