Пример #1
0
 def test_read_ebands_from_WFK(self):
     """Read ElectronBands from WFK files."""
     for filename in data.WFK_NCFILES:
         ebands = ElectronBands.from_file(filename)
         ebands.to_pymatgen()
         ebands.to_pdframe()
         assert ElectronBands.as_ebands(ebands) is ebands
Пример #2
0
 def test_read_ebands_from_WFK(self):
     """Read ElectronBands from WFK files."""
     for filename in data.WFK_NCFILES:
         ebands = ElectronBands.from_file(filename)
         ebands.to_pymatgen()
         ebands.to_pdframe()
         assert ElectronBands.as_ebands(ebands) is ebands
Пример #3
0
    def __init__(self,
                 ebands_kpath,
                 phbst_file,
                 phdos_file,
                 ebands_kmesh=None):
        self.eb_kpath = ElectronBands.as_ebands(ebands_kpath)
        self.eb_kmesh = ElectronBands.as_ebands(
            ebands_kmesh) if ebands_kmesh is not None else None

        self.phbst_file = phbst_file
        if duck.is_string(self.phbst_file):
            self.phbst_file = PhbstFile(self.phbst_file)
        self.phb_qpath = self.phbst_file.phbands

        self.phdos_file = phdos_file
        if duck.is_string(self.phdos_file):
            self.phdos_file = PhdosFile(phdos_file)
Пример #4
0
    def get_plotter_from_ebands(self, ebands):
        """
        Interpolate energies using the k-points given in input |ElectronBands| ebands.

        Return: |ElectronBandsPlotter| object.
        """
        ebands = ElectronBands.as_ebands(ebands)
        wan_ebands = self.interpolate_ebands(kpoints=ebands.kpoints)

        return ElectronBandsPlotter(key_ebands=[("Ab-initio", ebands), ("Interpolated", wan_ebands)])
Пример #5
0
 def test_frame_from_ebands(self):
     """Testing dataframe_from_ebands."""
     gsr_kmesh = abidata.ref_file("si_scf_GSR.nc")
     si_ebands_kmesh = ElectronBands.as_ebands(gsr_kmesh)
     gsr_nscf_path = abidata.ref_file("si_nscf_GSR.nc")
     index = ["foo", "bar", "hello"]
     df = dataframe_from_ebands([gsr_kmesh, si_ebands_kmesh, gsr_nscf_path], index=index, with_spglib=True)
     #str(df)
     assert all(f == "Si2" for f in df["formula"])
     assert all(num == 227 for num in df["abispg_num"])
     assert all(df["spglib_num"] == df["abispg_num"])
Пример #6
0
 def test_frame_from_ebands(self):
     """Testing dataframe_from_ebands."""
     gsr_kmesh = abidata.ref_file("si_scf_GSR.nc")
     si_ebands_kmesh = ElectronBands.as_ebands(gsr_kmesh)
     gsr_nscf_path = abidata.ref_file("si_nscf_GSR.nc")
     index = ["foo", "bar", "hello"]
     df = dataframe_from_ebands([gsr_kmesh, si_ebands_kmesh, gsr_nscf_path], index=index, with_spglib=True)
     #str(df)
     assert all(f == "Si2" for f in df["formula"])
     assert all(num == 227 for num in df["abispg_num"])
     assert all(df["spglib_num"] == df["abispg_num"])
Пример #7
0
    def test_read_ebands_from_WFK(self):
        """Read ElectronBands from WFK files."""
        for ii, filename in enumerate(abidata.WFK_NCFILES):
            ebands = ElectronBands.from_file(filename)
            ebands.to_pymatgen()
            ebands.to_pdframe()
            assert ElectronBands.as_ebands(ebands) is ebands

            self.serialize_with_pickle(ebands, test_eq=False)
            ElectronBands.from_dict(ebands.as_dict())
            self.assertMSONable(ebands, test_if_subclass=False)

            if ii == 0:
                if self.has_matplotlib(): ebands.plot(show=False)
                ebands.to_xmgrace(self.get_tmpname(text=True))
Пример #8
0
    def test_nickel_ebands_spin(self):
        """Testing Nickel electron bands with nsppol == 2"""
        ref_nelect = 18
        ni_ebands_kmesh = ElectronBands.from_file(abidata.ref_file("ni_666k_GSR.nc"))
        assert ElectronBands.as_ebands(ni_ebands_kmesh) is ni_ebands_kmesh
        with self.assertRaises(TypeError):
            ElectronBands.as_ebands(1)

        repr(ni_ebands_kmesh); str(ni_ebands_kmesh)
        assert ni_ebands_kmesh.nsppol == 2 and ni_ebands_kmesh.nspinor == 1 and ni_ebands_kmesh.nspden == 2
        assert ni_ebands_kmesh.nelect == ref_nelect
        assert ni_ebands_kmesh.kpoints.is_ibz and ni_ebands_kmesh.has_bzmesh and not ni_ebands_kmesh.has_bzpath
        assert ni_ebands_kmesh.has_timrev
        assert ni_ebands_kmesh.has_metallic_scheme
        smearing = ni_ebands_kmesh.smearing
        assert smearing.has_metallic_scheme
        assert smearing.occopt == 7
        self.assert_almost_equal(smearing.tsmear_ev.to("Ha"), 0.0075)
        assert smearing.scheme == "gaussian"
        assert not ni_ebands_kmesh.get_gaps_string()

        #ni_ebands_kmesh.copy()
        #ni_ebands_kmesh.deepcopy()

        ni_edos = ni_ebands_kmesh.get_edos()
        repr(ni_edos); str(ni_edos)
        assert ni_edos.to_string(verbose=2)

        # Get ElectronDosPlotter with nsppol == 2 and test matplotlib methods.
        edos_plotter = ni_ebands_kmesh.compare_gauss_edos(widths=[0.2, 0.4], step=0.2)
        assert len(edos_plotter) == 2
        if self.has_matplotlib():
            # Combiplot.
            assert edos_plotter.combiplot(title="default values", show=False)
            assert edos_plotter.combiplot(what_list=("dos", "idos"), spin_mode="resolved", show=False)
            assert edos_plotter.combiplot(e0=0, what_list="dos", spin_mode="automatic", fontsize=12, show=False)
            # Gridplot
            assert edos_plotter.gridplot(title="default values", show=False)
            assert edos_plotter.gridplot(what="idos", spin_mode="resolved", xlims=(-10, 10), show=False)
            assert edos_plotter.gridplot(e0=0, what="dos", spin_mode="resolved", fontsize=12, show=False)

        ni_ebands_kpath = ElectronBands.from_file(abidata.ref_file("ni_kpath_GSR.nc"))
        assert not ni_ebands_kpath.has_linewidths
        ni_ebands_kpath.linewidths = np.ones(ni_ebands_kpath.shape)
        assert ni_ebands_kpath.has_linewidths

        repr(ni_ebands_kpath); str(ni_ebands_kpath)
        assert ni_ebands_kpath.nsppol == 2 and ni_ebands_kpath.nspinor == 1 and ni_ebands_kpath.nspden == 2
        assert ni_ebands_kpath.nelect == ref_nelect
        assert ni_ebands_kpath.kpoints.is_path and not ni_ebands_kpath.has_bzmesh and ni_ebands_kpath.has_bzpath
        assert ni_ebands_kpath.has_timrev
        assert ni_ebands_kpath.fermie == ni_ebands_kmesh.fermie

        # Serialization
        self.serialize_with_pickle(ni_ebands_kpath, test_eq=False)
        self.assertMSONable(ni_ebands_kpath, test_if_subclass=False)
        assert len(ni_ebands_kpath.to_json())

        od = ni_ebands_kmesh.get_dict4pandas(with_spglib=False)
        assert od["nsppol"] == 2 and od["nspinor"] == 1 and od["nspden"] == 2

        df = ni_ebands_kpath.get_dataframe()
        ni_ebands_kpath.to_xmgrace(self.get_tmpname(text=True))
        ni_ebands_kpath.to_xmgrace(sys.stdout)

        # BXSF cannot be produced because.
        #ngkpt    6 6 6
        #nshiftk  4
        #shiftk   1/2 1/2 1/2 1/2 0.0 0.0 0.0 1/2 0.0 0.0 0.0 1/2
        with self.assertRaises(ValueError):
            ni_ebands_kmesh.to_bxsf(self.get_tmpname(text=True))

        # Interpolation
        r = ni_ebands_kmesh.interpolate(lpratio=10, kmesh=[8, 8, 8], verbose=1)

        assert r.ebands_kpath is not None
        assert r.ebands_kpath.kpoints.is_path
        assert not r.ebands_kpath.kpoints.is_ibz
        mpdivs, shifts = r.ebands_kpath.kpoints.mpdivs_shifts
        assert mpdivs is None and shifts is None

        assert r.ebands_kmesh is not None
        assert r.ebands_kmesh.kpoints.is_ibz
        assert not r.ebands_kmesh.kpoints.is_path
        assert r.ebands_kmesh.kpoints.ksampling is not None
        assert r.ebands_kmesh.kpoints.is_mpmesh
        mpdivs, shifts = r.ebands_kmesh.kpoints.mpdivs_shifts
        self.assert_equal(mpdivs, [8, 8, 8])
        self.assert_equal(shifts.flatten(), [0, 0, 0])

        # Test __add__ and __radd__ (should return ElectronBandsPlotter)
        p = ni_ebands_kmesh + r.ebands_kmesh + r.ebands_kpath
        assert hasattr(p, "combiplot")

        # Test plot methods
        if self.has_matplotlib():
            elims = [-10, 2]
            assert ni_ebands_kmesh.plot(show=False)
            assert ni_ebands_kmesh.plot_bz(show=False)
            assert ni_ebands_kpath.plot(ylims=elims, with_gaps=True, show=False)
            assert ni_ebands_kpath.plot_with_edos(ni_edos, ylims=elims, show=False)
            assert ni_ebands_kpath.plot_bz(show=False)
            assert ni_ebands_kpath.plot_transitions(4.4, qpt=(0, 0, 0), atol_ev=0.1, atol_kdiff=1e-4, show=False)
            assert ni_ebands_kpath.plot_transitions(4.4, qpt=(0.03, 0, 0), atol_ev=0.5, atol_kdiff=0.2, show=False)
            assert ni_ebands_kpath.plot_scatter3d(spin=0, band=8, show=False)
            assert ni_edos.plot(xlims=elims, show=False)
            assert ni_edos.plot_dos_idos(xlims=elims, show=False)
            assert ni_edos.plot_up_minus_down(xlims=elims, show=False)

            # Test linewidths
            assert ni_ebands_kmesh.plot_lws_vs_e0(show=False) is None
            assert ni_ebands_kpath.plot_lws_vs_e0(show=False)

            # TODO Generaliza jdos to metals.
            #vrange, crange = range(0, 4), range(4, 5)
            #assert ni_ebands_kmesh.plot_ejdosvc(vrange, crange, cumulative=False, show=False)
            #assert ni_ebands_kmesh.plot_ejdosvc(vrange, crange, cumulative=True, show=False)

            if self.has_seaborn():
                assert ni_ebands_kmesh.boxplot(brange=[5, 10], show=False,
                    title="Boxplot for up and down spin and 10 > band >= 5")

        # Test Abipy --> Pymatgen converter.
        pmg_bands_kpath = ni_ebands_kpath.to_pymatgen()
        assert hasattr(pmg_bands_kpath, "get_branch")   # Should be BandStructureSymmLine
        assert pmg_bands_kpath.efermi == ni_ebands_kpath.fermie
        assert pmg_bands_kpath.is_spin_polarized
        assert pmg_bands_kpath.is_metal()

        # Test Pymatgen --> Abipy converter.
        same_ekpath = ElectronBands.from_pymatgen(pmg_bands_kpath, ni_ebands_kpath.nelect)
        repr(same_ekpath); str(same_ekpath)
        self.assert_equal(same_ekpath.eigens, ni_ebands_kpath.eigens)
        assert same_ekpath.fermie == ni_ebands_kpath.fermie

        pmg_bands_kmesh = ni_ebands_kmesh.to_pymatgen()
        #assert hasattr(pmg_bands_kmesh, "get_branch")   # Should be BandStructure
        assert pmg_bands_kmesh.efermi == ni_ebands_kmesh.fermie
        assert pmg_bands_kmesh.is_spin_polarized
        assert pmg_bands_kmesh.is_metal()

        # Test Pymatgen --> Abipy converter.
        same_ekmesh = ElectronBands.from_pymatgen(pmg_bands_kmesh, ni_ebands_kmesh.nelect)
        self.assert_equal(same_ekmesh.eigens, ni_ebands_kmesh.eigens)
        assert same_ekmesh.fermie == ni_ebands_kmesh.fermie
Пример #9
0
    def test_nickel_ebands_spin(self):
        """Testing Nickel electron bands with nsppol == 2"""
        ref_nelect = 18
        ni_ebands_kmesh = ElectronBands.from_file(
            abidata.ref_file("ni_666k_GSR.nc"))
        assert ElectronBands.as_ebands(ni_ebands_kmesh) is ni_ebands_kmesh
        with self.assertRaises(TypeError):
            ElectronBands.as_ebands(1)

        repr(ni_ebands_kmesh)
        str(ni_ebands_kmesh)
        assert ni_ebands_kmesh.nsppol == 2 and ni_ebands_kmesh.nspinor == 1 and ni_ebands_kmesh.nspden == 2
        assert ni_ebands_kmesh.nelect == ref_nelect
        assert ni_ebands_kmesh.kpoints.is_ibz and ni_ebands_kmesh.has_bzmesh and not ni_ebands_kmesh.has_bzpath
        assert ni_ebands_kmesh.has_timrev
        assert ni_ebands_kmesh.has_metallic_scheme
        smearing = ni_ebands_kmesh.smearing
        assert smearing.has_metallic_scheme
        assert smearing.occopt == 7
        self.assert_almost_equal(smearing.tsmear_ev.to("Ha"), 0.0075)
        assert smearing.scheme == "gaussian"

        ni_ebands_kmesh.copy()
        ni_ebands_kmesh.deepcopy()

        ni_edos = ni_ebands_kmesh.get_edos()
        repr(ni_edos)
        str(ni_edos)
        assert ni_edos.to_string(verbose=2)

        # Get ElectronDosPlotter with nsppol == 2 and test matplotlib methods.
        edos_plotter = ni_ebands_kmesh.compare_gauss_edos(widths=[0.2, 0.4],
                                                          step=0.2)
        assert len(edos_plotter) == 2
        if self.has_matplotlib():
            # Combiplot.
            assert edos_plotter.combiplot(title="default values", show=False)
            assert edos_plotter.combiplot(what_list=("dos", "idos"),
                                          spin_mode="resolved",
                                          show=False)
            assert edos_plotter.combiplot(e0=0,
                                          what_list="dos",
                                          spin_mode="resolved",
                                          fontsize=12,
                                          show=False)
            # Gridplot
            assert edos_plotter.gridplot(title="default values", show=False)
            assert edos_plotter.gridplot(what="idos",
                                         spin_mode="resolved",
                                         xlims=(-10, 10),
                                         show=False)
            assert edos_plotter.gridplot(e0=0,
                                         what="dos",
                                         spin_mode="resolved",
                                         fontsize=12,
                                         show=False)

        ni_ebands_kpath = ElectronBands.from_file(
            abidata.ref_file("ni_kpath_GSR.nc"))
        assert not ni_ebands_kpath.has_linewidths
        ni_ebands_kpath.linewidths = np.ones(ni_ebands_kpath.shape)
        assert ni_ebands_kpath.has_linewidths

        repr(ni_ebands_kpath)
        str(ni_ebands_kpath)
        assert ni_ebands_kpath.nsppol == 2 and ni_ebands_kpath.nspinor == 1 and ni_ebands_kpath.nspden == 2
        assert ni_ebands_kpath.nelect == ref_nelect
        assert ni_ebands_kpath.kpoints.is_path and not ni_ebands_kpath.has_bzmesh and ni_ebands_kpath.has_bzpath
        assert ni_ebands_kpath.has_timrev
        assert ni_ebands_kpath.fermie == ni_ebands_kmesh.fermie

        # Serialization
        self.serialize_with_pickle(ni_ebands_kpath, test_eq=False)
        self.assertMSONable(ni_ebands_kpath, test_if_subclass=False)
        assert len(ni_ebands_kpath.to_json())

        od = ni_ebands_kmesh.get_dict4pandas(with_spglib=False)
        assert od["nsppol"] == 2 and od["nspinor"] == 1 and od["nspden"] == 2

        df = ni_ebands_kpath.get_dataframe()
        ni_ebands_kpath.to_xmgrace(self.get_tmpname(text=True))
        ni_ebands_kpath.to_xmgrace(sys.stdout)

        # BXSF cannot be produced because.
        #ngkpt    6 6 6
        #nshiftk  4
        #shiftk   1/2 1/2 1/2 1/2 0.0 0.0 0.0 1/2 0.0 0.0 0.0 1/2
        with self.assertRaises(ValueError):
            ni_ebands_kmesh.to_bxsf(self.get_tmpname(text=True))

        # Interpolation
        r = ni_ebands_kmesh.interpolate(lpratio=10, kmesh=[8, 8, 8], verbose=1)

        assert r.ebands_kpath is not None
        assert r.ebands_kpath.kpoints.is_path
        assert not r.ebands_kpath.kpoints.is_ibz
        mpdivs, shifts = r.ebands_kpath.kpoints.mpdivs_shifts
        assert mpdivs is None and shifts is None

        assert r.ebands_kmesh is not None
        assert r.ebands_kmesh.kpoints.is_ibz
        assert not r.ebands_kmesh.kpoints.is_path
        assert r.ebands_kmesh.kpoints.ksampling is not None
        assert r.ebands_kmesh.kpoints.is_mpmesh
        mpdivs, shifts = r.ebands_kmesh.kpoints.mpdivs_shifts
        self.assert_equal(mpdivs, [8, 8, 8])
        self.assert_equal(shifts.flatten(), [0, 0, 0])

        # Test __add__ and __radd__ (should return ElectronBandsPlotter)
        p = ni_ebands_kmesh + r.ebands_kmesh + r.ebands_kpath
        assert hasattr(p, "combiplot")

        # Test plot methods
        if self.has_matplotlib():
            elims = [-10, 2]
            assert ni_ebands_kmesh.plot(show=False)
            assert ni_ebands_kmesh.plot_bz(show=False)
            assert ni_ebands_kpath.plot(ylims=elims, show=False)
            assert ni_ebands_kpath.plot_with_edos(ni_edos,
                                                  ylims=elims,
                                                  show=False)
            assert ni_ebands_kpath.plot_bz(show=False)
            assert ni_ebands_kpath.plot_transitions(4.4,
                                                    qpt=(0, 0, 0),
                                                    atol_ev=0.1,
                                                    atol_kdiff=1e-4,
                                                    show=False)
            assert ni_ebands_kpath.plot_transitions(4.4,
                                                    qpt=(0.03, 0, 0),
                                                    atol_ev=0.5,
                                                    atol_kdiff=0.2,
                                                    show=False)
            assert ni_ebands_kpath.plot_scatter3d(spin=0, band=8, show=False)
            assert ni_edos.plot(xlims=elims, show=False)
            assert ni_edos.plot_dos_idos(xlims=elims, show=False)
            assert ni_edos.plot_up_minus_down(xlims=elims, show=False)

            # Test linewidths
            assert ni_ebands_kmesh.plot_lws_vs_e0(show=False) is None
            assert ni_ebands_kpath.plot_lws_vs_e0(show=False)

            # TODO Generaliza jdos to metals.
            #vrange, crange = range(0, 4), range(4, 5)
            #assert ni_ebands_kmesh.plot_ejdosvc(vrange, crange, cumulative=False, show=False)
            #assert ni_ebands_kmesh.plot_ejdosvc(vrange, crange, cumulative=True, show=False)

            if self.has_seaborn():
                assert ni_ebands_kmesh.boxplot(
                    brange=[5, 10],
                    show=False,
                    title="Boxplot for up and down spin and 10 > band >= 5")

        # Test Abipy --> Pymatgen converter.
        pmg_bands_kpath = ni_ebands_kpath.to_pymatgen()
        assert hasattr(pmg_bands_kpath,
                       "get_branch")  # Should be BandStructureSymmLine
        assert pmg_bands_kpath.efermi == ni_ebands_kpath.fermie
        assert pmg_bands_kpath.is_spin_polarized
        assert pmg_bands_kpath.is_metal()

        # Test Pymatgen --> Abipy converter.
        same_ekpath = ElectronBands.from_pymatgen(pmg_bands_kpath,
                                                  ni_ebands_kpath.nelect)
        repr(same_ekpath)
        str(same_ekpath)
        self.assert_equal(same_ekpath.eigens, ni_ebands_kpath.eigens)
        assert same_ekpath.fermie == ni_ebands_kpath.fermie

        pmg_bands_kmesh = ni_ebands_kmesh.to_pymatgen()
        #assert hasattr(pmg_bands_kmesh, "get_branch")   # Should be BandStructure
        assert pmg_bands_kmesh.efermi == ni_ebands_kmesh.fermie
        assert pmg_bands_kmesh.is_spin_polarized
        assert pmg_bands_kmesh.is_metal()

        # Test Pymatgen --> Abipy converter.
        same_ekmesh = ElectronBands.from_pymatgen(pmg_bands_kmesh,
                                                  ni_ebands_kmesh.nelect)
        self.assert_equal(same_ekmesh.eigens, ni_ebands_kmesh.eigens)
        assert same_ekmesh.fermie == ni_ebands_kmesh.fermie