Example #1
0
        def __init__(self):
            bond = 1.42
            sq3h = 3.**.5 * 0.5
            self.sc = SuperCell(
                np.array([[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]],
                         np.float64) * bond,
                nsc=[3, 3, 1])

            n = 60
            rf = np.linspace(0, bond * 1.01, n)
            rf = (rf, rf)
            orb = SphericalOrbital(1, rf, 2.)
            C = Atom(6, orb.toAtomicOrbital())
            self.g = Geometry(
                np.array([[0., 0., 0.], [1., 0., 0.]], np.float64) * bond,
                atom=C,
                sc=self.sc)
            self.D = DensityMatrix(self.g)
            self.DS = DensityMatrix(self.g, orthogonal=False)

            def func(D, ia, idxs, idxs_xyz):
                idx = D.geom.close(ia,
                                   R=(0.1, 1.44),
                                   idx=idxs,
                                   idx_xyz=idxs_xyz)
                ia = ia * 3

                i0 = idx[0] * 3
                i1 = idx[1] * 3
                # on-site
                p = 1.
                D.D[ia, i0] = p
                D.D[ia + 1, i0 + 1] = p
                D.D[ia + 2, i0 + 2] = p

                # nn
                p = 0.1

                # on-site directions
                D.D[ia, ia + 1] = p
                D.D[ia, ia + 2] = p
                D.D[ia + 1, ia] = p
                D.D[ia + 1, ia + 2] = p
                D.D[ia + 2, ia] = p
                D.D[ia + 2, ia + 1] = p

                D.D[ia, i1 + 1] = p
                D.D[ia, i1 + 2] = p

                D.D[ia + 1, i1] = p
                D.D[ia + 1, i1 + 2] = p

                D.D[ia + 2, i1] = p
                D.D[ia + 2, i1 + 1] = p

            self.func = func
Example #2
0
    def test_transform_nonortho(self, setup):
        D = DensityMatrix(setup.g, spin='polarized', orthogonal=False)
        a = np.arange(3)
        a[-1] = 1.
        for ia in setup.g:
            D[ia, ia] = a

        Dt = D.transform(spin='unpolarized', dtype=np.float32)
        assert np.abs(0.5 * D.tocsr(0) + 0.5 * D.tocsr(1) - Dt.tocsr(0)).sum() == 0
        assert np.abs(D.tocsr(-1) - Dt.tocsr(-1)).sum() == 0
        Dt = D.transform(spin='polarized')
        assert np.abs(D.tocsr(0) - Dt.tocsr(0)).sum() == 0
        assert np.abs(D.tocsr(1) - Dt.tocsr(1)).sum() == 0
        Dt = D.transform(spin='polarized', orthogonal=True)
        assert np.abs(D.tocsr(0) - Dt.tocsr(0)).sum() == 0
        assert np.abs(D.tocsr(1) - Dt.tocsr(1)).sum() == 0
        Dt = D.transform(spin='non-colinear', orthogonal=False)
        assert np.abs(D.tocsr(0) - Dt.tocsr(0)).sum() == 0
        assert np.abs(D.tocsr(1) - Dt.tocsr(1)).sum() == 0
        assert np.abs(Dt.tocsr(2)).sum() == 0
        assert np.abs(Dt.tocsr(3)).sum() == 0
        assert np.abs(D.tocsr(-1) - Dt.tocsr(-1)).sum() == 0
        Dt = D.transform(spin='so', orthogonal=True)
        assert np.abs(D.tocsr(0) - Dt.tocsr(0)).sum() == 0
        assert np.abs(D.tocsr(1) - Dt.tocsr(1)).sum() == 0
        assert np.abs(Dt.tocsr(-1)).sum() == 0
Example #3
0
    def test_rho2(self, setup):
        bond = 1.42
        sq3h = 3.**.5 * 0.5
        sc = SuperCell(np.array(
            [[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) *
                       bond,
                       nsc=[3, 3, 1])

        n = 60
        rf = np.linspace(0, bond * 1.01, n)
        rf = (rf, rf)
        orb = SphericalOrbital(1, rf, 2.)
        C = Atom(6, orb)
        g = Geometry(np.array([[0., 0., 0.], [1., 0., 0.]], np.float64) * bond,
                     atom=C,
                     sc=sc)
        D = DensityMatrix(g)
        D.construct([[0.1, bond + 0.01], [1., 0.1]])
        grid = Grid(0.2, geometry=D.geom)
        D.density(grid)

        D = DensityMatrix(g, spin=Spin('P'))
        D.construct([[0.1, bond + 0.01], [(1., 0.5), (0.1, 0.1)]])
        grid = Grid(0.2, geometry=D.geom)
        D.density(grid)
        D.density(grid, [1., -1])
        D.density(grid, 0)
        D.density(grid, 1)

        D = DensityMatrix(g, spin=Spin('NC'))
        D.construct([[0.1, bond + 0.01],
                     [(1., 0.5, 0.01, 0.01), (0.1, 0.1, 0.1, 0.1)]])
        grid = Grid(0.2, geometry=D.geom)
        D.density(grid)
        D.density(grid, [[1., 0.], [0., -1]])

        D = DensityMatrix(g, spin=Spin('SO'))
        D.construct([[0.1, bond + 0.01],
                     [(1., 0.5, 0.01, 0.01, 0.01, 0.01, 0., 0.),
                      (0.1, 0.1, 0.1, 0.1, 0., 0., 0., 0.)]])
        grid = Grid(0.2, geometry=D.geom)
        D.density(grid)
        D.density(grid, [[1., 0.], [0., -1]])
        D.density(grid, Spin.X)
        D.density(grid, Spin.Y)
        D.density(grid, Spin.Z)
Example #4
0
def test_nc_multiple_fail(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    DM = DensityMatrix(sisl_system.gtb)

    sile = ncSileSiesta(f, 'w')
    H.construct([sisl_system.R, sisl_system.t])
    H.write(sile)

    DM[0, 0] = 1.
    DM.write(sile)
Example #5
0
def test_nc_multiple_fail(sisl_tmp, sisl_system):
    # writing two different sparse matrices to the same
    # file will fail
    f = sisl_tmp('gr.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    DM = DensityMatrix(sisl_system.gtb)

    with ncSileSiesta(f, 'w') as sile:
        H.construct([sisl_system.R, sisl_system.t])
        H.write(sile)

        DM[0, 0] = 1.
        with pytest.raises(ValueError):
            DM.write(sile)
Example #6
0
def test_nc_density_matrix(sisl_tmp, sisl_system):
    f = sisl_tmp('grDM.nc', _dir)
    dm = DensityMatrix(sisl_system.gtb)
    for _, ix in dm.iter_orbitals():
        dm[ix, ix] = ix / 2.
    dm.write(ncSileSiesta(f, 'w'))

    ndm = ncSileSiesta(f).read_density_matrix()

    # Assert they are the same
    assert np.allclose(dm.cell, ndm.cell)
    assert np.allclose(dm.xyz, ndm.xyz)
    dm.finalize()
    assert np.allclose(dm._csr._D[:, 0], ndm._csr._D[:, 0])
    assert sisl_system.g.atoms.equal(ndm.atoms, R=False)
Example #7
0
def test_nc_DM_non_colinear(sisl_tmp):
    DM1 = DensityMatrix(sisl.geom.graphene(), spin=sisl.Spin('NC'))
    DM1.construct(([0.1, 1.44], [[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5]]))

    f1 = sisl_tmp('DM1.nc', _dir)
    f2 = sisl_tmp('DM2.nc', _dir)
    DM1.write(f1)
    DM1.finalize()
    DM2 = sisl.get_sile(f1).read_density_matrix()
    DM2.write(f2)
    DM3 = sisl.get_sile(f2).read_density_matrix()
    assert DM1._csr.spsame(DM2._csr)
    assert np.allclose(DM1._csr._D, DM2._csr._D)
    assert DM1._csr.spsame(DM3._csr)
    assert np.allclose(DM1._csr._D, DM3._csr._D)
Example #8
0
    def test_orbital_momentum(self, setup):
        bond = 1.42
        sq3h = 3.**.5 * 0.5
        sc = SuperCell(np.array([[1.5, sq3h, 0.],
                                      [1.5, -sq3h, 0.],
                                      [0., 0., 10.]], np.float64) * bond, nsc=[3, 3, 1])

        orb = AtomicOrbital('px', R=bond * 1.001)
        C = Atom(6, orb)
        g = Geometry(np.array([[0., 0., 0.],
                                    [1., 0., 0.]], np.float64) * bond,
                        atoms=C, sc=sc)
        D = DensityMatrix(g, spin=Spin('SO'))
        D.construct([[0.1, bond + 0.01], [(1., 0.5, 0.01, 0.01, 0.01, 0.01, 0., 0.), (0.1, 0.1, 0.1, 0.1, 0., 0., 0., 0.)]])
        D.orbital_momentum("atom")
        D.orbital_momentum("orbital")
Example #9
0
def test_nc_multiple_checks(sisl_tmp, sisl_system, sort):
    f = sisl_tmp('gr.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    DM = DensityMatrix(sisl_system.gtb)

    sile = ncSileSiesta(f, 'w')
    H.construct([sisl_system.R, sisl_system.t])
    H.write(sile, sort=sort)

    shuffle = np.random.shuffle
    for io in range(len(H)):
        edges = H.edges(io)  # get all edges
        shuffle(edges)
        DM[io, edges] = 2.

    DM.write(sile, sort=sort)
Example #10
0
def test_nc_DM_spin_orbit_nc2dm2nc(sisl_tmp):
    DM1 = DensityMatrix(sisl.geom.graphene(), orthogonal=False, spin=sisl.Spin('SO'))
    DM1.construct(([0.1, 1.44],
                   [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.],
                    [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.]]))

    f1 = sisl_tmp('DM1.nc', _dir)
    f2 = sisl_tmp('DM2.DM', _dir)
    DM1.finalize()
    DM1.write(f1)
    DM2 = sisl.get_sile(f1).read_density_matrix()
    DM2.write(f2)
    DM3 = sisl.get_sile(f2).read_density_matrix()
    assert DM1._csr.spsame(DM2._csr)
    assert np.allclose(DM1._csr._D, DM2._csr._D)
    assert DM1._csr.spsame(DM3._csr)
    assert np.allclose(DM1._csr._D, DM3._csr._D)
Example #11
0
 def test_read_write_density_matrix(self, sisl_tmp, sisl_system, sile):
     G = sisl_system.g.rotate(-30, sisl_system.g.cell[2, :])
     DM = DensityMatrix(G, orthogonal=True)
     DM.construct([[0.1, 1.45], [0.1, -2.7]])
     f = sisl_tmp('test_read_write_density_matrix.win', _dir)
     # Write
     sile(f, mode='w').write_density_matrix(DM)
     # Read 1
     try:
         dm = sile(f, mode='r').read_density_matrix(geometry=DM.geom)
         assert DM.spsame(dm)
     except UnicodeDecodeError as e:
         pass
     # Read 2
     try:
         dm = DensityMatrix.read(sile(f, mode='r'), geometry=DM.geom)
         assert DM.spsame(dm)
     except UnicodeDecodeError as e:
         pass
Example #12
0
    def test_spin_rotate_so(self, setup):
        bond = 1.42
        sq3h = 3.**.5 * 0.5
        sc = SuperCell(np.array([[1.5, sq3h, 0.],
                                      [1.5, -sq3h, 0.],
                                      [0., 0., 10.]], np.float64) * bond, nsc=[3, 3, 1])

        orb = AtomicOrbital('px', R=bond * 1.001)
        C = Atom(6, orb)
        g = Geometry(np.array([[0., 0., 0.],
                                    [1., 0., 0.]], np.float64) * bond,
                        atoms=C, sc=sc)
        D = DensityMatrix(g, spin=Spin('SO'))
        D.construct([[0.1, bond + 0.01], [(1., 0.5, 0.01, 0.01, 0.01, 0.01, 0.2, 0.2), (0.1, 0.2, 0.1, 0.1, 0., 0.1, 0.2, 0.3)]])
        D_mull = D.mulliken()
        d = D.spin_rotate([45, 60, 90], rad=False)
        d_mull = d.mulliken()
        assert not np.allclose(D_mull, d_mull)
        assert np.allclose(D_mull[:, 0], d_mull[:, 0])
Example #13
0
    def test_rho_fail_nc(self, setup):
        bond = 1.42
        sq3h = 3.**.5 * 0.5
        sc = SuperCell(np.array([[1.5, sq3h, 0.],
                                      [1.5, -sq3h, 0.],
                                      [0., 0., 10.]], np.float64) * bond, nsc=[3, 3, 1])

        n = 60
        rf = np.linspace(0, bond * 1.01, n)
        rf = (rf, rf)
        orb = SphericalOrbital(1, rf, 2.)
        C = Atom(6, orb)
        g = Geometry(np.array([[0., 0., 0.],
                                    [1., 0., 0.]], np.float64) * bond,
                        atoms=C, sc=sc)

        D = DensityMatrix(g, spin=Spin('NC'))
        D.construct([[0.1, bond + 0.01], [(1., 0.5, 0.01, 0.01), (0.1, 0.1, 0.1, 0.1)]])
        grid = Grid(0.2, geometry=D.geometry)
        with pytest.raises(ValueError):
            D.density(grid, [1., 0.])
Example #14
0
    def test_transform(self, setup):
        D = DensityMatrix(setup.g, spin='so')
        a = np.arange(8)
        for ia in setup.g:
            D[ia, ia] = a
        Dcsr = [D.tocsr(i) for i in range(D.shape[2])]

        Dt = D.transform(spin='unpolarized', dtype=np.float32)
        assert np.abs(0.5 * Dcsr[0] + 0.5 * Dcsr[1] - Dt.tocsr(0)).sum() == 0

        Dt = D.transform(spin='polarized', orthogonal=False)
        assert np.abs(Dcsr[0] - Dt.tocsr(0)).sum() == 0
        assert np.abs(Dcsr[1] - Dt.tocsr(1)).sum() == 0
        assert np.abs(Dt.tocsr(2)).sum() != 0

        Dt = D.transform(spin='non-colinear', orthogonal=False)
        assert np.abs(Dcsr[0] - Dt.tocsr(0)).sum() == 0
        assert np.abs(Dcsr[1] - Dt.tocsr(1)).sum() == 0
        assert np.abs(Dcsr[2] - Dt.tocsr(2)).sum() == 0
        assert np.abs(Dcsr[3] - Dt.tocsr(3)).sum() == 0
        assert np.abs(Dt.tocsr(-1)).sum() != 0
Example #15
0
def test_nc_multiple_checks(sisl_tmp, sisl_system, sort):
    f = sisl_tmp('gr.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    DM = DensityMatrix(sisl_system.gtb)

    with ncSileSiesta(f, 'w') as sile:
        H.construct([sisl_system.R, sisl_system.t])
        H.write(sile, sort=sort)

        # fix seed
        np.random.seed(42)
        shuffle = np.random.shuffle
        for io in range(len(H)):
            edges = H.edges(io)  # get all edges
            shuffle(edges)
            DM[io, edges] = 2.

        if not sort:
            with pytest.raises(ValueError):
                DM.write(sile, sort=sort)
        else:
            DM.write(sile, sort=sort)
Example #16
0
    def test_spin_align_pol(self, setup):
        bond = 1.42
        sq3h = 3.**.5 * 0.5
        sc = SuperCell(np.array([[1.5, sq3h, 0.],
                                      [1.5, -sq3h, 0.],
                                      [0., 0., 10.]], np.float64) * bond, nsc=[3, 3, 1])

        orb = AtomicOrbital('px', R=bond * 1.001)
        C = Atom(6, orb)
        g = Geometry(np.array([[0., 0., 0.],
                                    [1., 0., 0.]], np.float64) * bond,
                        atoms=C, sc=sc)
        D = DensityMatrix(g, spin=Spin('p'))
        D.construct([[0.1, bond + 0.01], [(1., 0.5), (0.1, 0.2)]])
        D_mull = D.mulliken()
        v = np.array([1, 2, 3])
        d = D.spin_align(v)
        d_mull = d.mulliken()
        assert D_mull.shape == (len(D), 2)
        assert d_mull.shape == (len(D), 4)
        assert not np.allclose(-np.diff(D_mull, axis=1), d_mull[:, 3])
        assert np.allclose(D_mull.sum(1), d_mull[:, 0])
Example #17
0
    def test_spin_align_nc(self, setup):
        bond = 1.42
        sq3h = 3.**.5 * 0.5
        sc = SuperCell(np.array(
            [[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) *
                       bond,
                       nsc=[3, 3, 1])

        orb = AtomicOrbital('px', R=bond * 1.001)
        C = Atom(6, orb)
        g = Geometry(np.array([[0., 0., 0.], [1., 0., 0.]], np.float64) * bond,
                     atom=C,
                     sc=sc)
        D = DensityMatrix(g, spin=Spin('nc'))
        D.construct([[0.1, bond + 0.01],
                     [(1., 0.5, 0.01, 0.01), (0.1, 0.2, 0.1, 0.1)]])
        D_mull = D.mulliken()
        v = np.array([1, 2, 3])
        d = D.spin_align(v)
        d_mull = d.mulliken()
        assert not np.allclose(D_mull, d_mull)
        assert np.allclose(D_mull[:, 0], d_mull[:, 0])