Пример #1
0
    def test_phonon_computation(self):
        """Testing if pjdoses compute by anaddb integrate to 3*natom"""
        path = os.path.join(abidata.dirpath, "refs", "mgb2_phonons_nkpt_tsmear", "mgb2_121212k_0.04tsmear_DDB")
        ddb = abilab.abiopen(path)

        for dos_method in ("tetra", "gaussian"):
            # Get phonon bands and Dos with anaddb.
            phbands_file, phdos_file = ddb.anaget_phbst_and_phdos_files(nqsmall=4, ndivsm=2,
                dipdip=0, chneut=0, dos_method=dos_method, lo_to_splitting=False, verbose=1)

            phbands, phdos = phbands_file.phbands, phdos_file.phdos
            natom3 = len(phbands.structure) * 3

            # Test that amu is present with correct values.
            assert phbands.amu is not None
            self.assert_almost_equal(phbands.amu[12.0], 0.24305e+02)
            self.assert_almost_equal(phbands.amu[5.0], 0.10811e+02)
            self.assert_almost_equal(phbands.amu_symbol["Mg"], phbands.amu[12.0])
            self.assert_almost_equal(phbands.amu_symbol["B"],  phbands.amu[5.0])

            # Total PHDOS should integrate to 3 * natom
            # Note that anaddb does not renormalize the DOS so we have to increate the tolerance.
            #E       Arrays are not almost equal to 2 decimals
            #E        ACTUAL: 8.9825274146312282
            #E        DESIRED: 9
            self.assert_almost_equal(phdos.integral_value, natom3, decimal=1)

            # Test convertion to eigenvectors. Verify that they are orthonormal
            cidentity = np.eye(natom3, dtype=np.complex)
            eig = phbands.dyn_mat_eigenvect
            for iq in range(phbands.nqpt):
                #print("About to test iq", iq, np.dot(eig[iq].T.conjugate(), eig[iq]))
                #assert np.allclose(np.dot(eig[iq], eig[iq].T), cidentity , atol=1e-5, rtol=1e-3)
                self.assert_almost_equal(np.dot(eig[iq].conjugate().T, eig[iq]), cidentity) #, decimal=1)

            # Summing projected DOSes over types should give the total DOS.
            pj_sum = sum(pjdos.integral_value for pjdos in phdos_file.pjdos_symbol.values())
            self.assert_almost_equal(phdos.integral_value, pj_sum)

            # Summing projected DOSes over types and directions should give the total DOS.
            # phdos_rc_type[ntypat, 3, nomega]
            values = phdos_file.reader.read_value("pjdos_rc_type").sum(axis=(0, 1))
            tot_dos = abilab.Function1D(phdos.mesh, values)
            self.assert_almost_equal(phdos.integral_value, tot_dos.integral_value)

            phbands_file.close()
            phdos_file.close()

        ddb.close()
Пример #2
0
    def test_from_phdosfile(self):
        """Testing PHDOS from netcdf file."""
        ncfile = PhdosFile(abidata.ref_file("trf2_5.out_PHDOS.nc"))
        repr(ncfile)
        str(ncfile)
        assert ncfile.to_string(verbose=1)
        assert hasattr(ncfile, "structure")
        nw = len(ncfile.wmesh)
        assert nw == 461
        natom3 = len(ncfile.structure) * 3

        # Read PJDOSes
        assert list(ncfile.pjdos_symbol.keys()) == ["Al", "As"]
        od = ncfile.reader.read_pjdos_symbol_xyz_dict()
        assert list(od.keys()) == ["Al", "As"]
        assert all(v.shape == (3, nw) for v in od.values())

        arr = ncfile.reader.read_pjdos_atdir()
        assert arr.shape == (len(ncfile.structure), 3, nw)

        phdos = ncfile.phdos
        # Test integrals of DOS (the tolerance is a bit low likely due to too coarse meshes)
        self.assert_almost_equal(phdos.integral_value, natom3, decimal=1)

        # Summing projected DOSes over types should give the total DOS.
        pj_sum = sum(pjdos.integral_value
                     for pjdos in ncfile.pjdos_symbol.values())
        self.assert_almost_equal(phdos.integral_value, pj_sum)

        # Summing projected DOSes over types and directions should give the total DOS.
        # phdos_rc_type[ntypat, 3, nomega]
        values = ncfile.reader.read_value("pjdos_rc_type").sum(axis=(0, 1))
        tot_dos = abilab.Function1D(phdos.mesh, values)
        self.assert_almost_equal(phdos.integral_value, tot_dos.integral_value)

        assert phdos == PhononDos.as_phdos(
            abidata.ref_file("trf2_5.out_PHDOS.nc"))

        # Test Thermodinamics in the Harmonic approximation
        self.assert_almost_equal(phdos.zero_point_energy.to("Ha"),
                                 0.0030872835637731303)

        u = phdos.get_internal_energy()
        self.assert_almost_equal(u.values[0], 0.084009326574073395)
        self.assert_almost_equal(u.values[-1], 0.17270110252071791)

        s = phdos.get_entropy()
        self.assert_almost_equal(s.values[0], 1.6270193052583423e-08)
        self.assert_almost_equal(s.values[-1], 0.00058238779354717)

        cv = phdos.get_cv()
        self.assert_almost_equal(cv.values[0], 5.3715488328604253e-08)
        self.assert_almost_equal(cv.values[-1], 0.00045871909188672578)

        f = phdos.get_free_energy()
        self.assert_almost_equal(f.values, (u - s.mesh * s.values).values)

        self.assertAlmostEqual(phdos.debye_temp, 469.01524830328606)
        self.assertAlmostEqual(
            phdos.get_acoustic_debye_temp(len(ncfile.structure)),
            372.2576492728813)

        assert ncfile.to_pymatgen()

        if self.has_matplotlib():
            assert ncfile.plot_pjdos_type(show=False)
            assert ncfile.plot_pjdos_type(units="cm-1",
                                          stacked=False,
                                          colormap="viridis",
                                          show=False)
            assert ncfile.plot_pjdos_type(units="eV",
                                          stacked=True,
                                          colormap="jet",
                                          exchange_xy=True,
                                          fontsize=8,
                                          show=False)
            assert ncfile.plot_pjdos_cartdirs_type(units="Thz",
                                                   stacked=True,
                                                   show=False)
            assert ncfile.plot_pjdos_cartdirs_type(units="meV",
                                                   stacked=False,
                                                   alpha=0.5,
                                                   show=False)
            assert ncfile.plot_pjdos_cartdirs_site(units="meV",
                                                   stacked=False,
                                                   alpha=0.5,
                                                   show=False)
            assert ncfile.plot_pjdos_cartdirs_site(units="meV",
                                                   view="all",
                                                   stacked=True,
                                                   alpha=0.5,
                                                   show=False)

            assert phdos.plot(units="cm-1", show=False)
            assert phdos.plot_harmonic_thermo(tstar=20,
                                              tstop=350,
                                              units="eV",
                                              formula_units=1,
                                              show=False)
            assert phdos.plot_harmonic_thermo(tstar=20,
                                              tstop=350,
                                              units="Jmol",
                                              formula_units=2,
                                              show=False)

        # Test notebook
        if self.has_nbformat():
            ncfile.write_notebook(nbpath=self.get_tmpname(text=True))

        ncfile.close()