Ejemplo n.º 1
0
 def test_eig1(self):
     # Test of eigenvalues
     g = self.g.tile(2, 0).tile(2, 1).tile(2, 2)
     H = Hamiltonian(g)
     H.construct((0.1,1.5), (1.,0.1))
     H.eigh()
     H.eigsh(n=4)
     H.empty()
     del H
Ejemplo n.º 2
0
 def test_berry_phase_method_fail(self):
     g = Geometry([[-.6, 0, 0], [0.6, 0, 0]], Atom(1, 1.001), sc=[2, 10, 10])
     g.set_nsc([3, 1, 1])
     H = Hamiltonian(g)
     H.construct([(0.1, 1.0, 1.5), (0, 1., 0.5)])
     # Contour
     k = np.linspace(0.0, 1.0, 101)
     K = np.zeros([k.size, 3])
     K[:, 0] = k
     bz = BrillouinZone(H, K)
     berry_phase(bz, method='unknown')
Ejemplo n.º 3
0
def test_tbt_delta_fail(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.dH.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    H.construct([sisl_system.R, sisl_system.t])
    H.finalize()

    with deltancSileTBtrans(f, 'w') as sile:
        sile.write_delta(H, k=[0.] * 3)
        for i in range(H.no_s):
            H[0, i] = 1.
        sile.write_delta(H, k=[0.2] * 3)
Ejemplo n.º 4
0
def test_nc_overlap(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.nc', _dir)
    tb = Hamiltonian(sisl_system.gtb)
    tb.construct([sisl_system.R, sisl_system.t])
    tb.write(ncSileSiesta(f, 'w'))

    S = ncSileSiesta(f).read_overlap()

    # Ensure no empty data-points
    S.finalize()
    assert np.allclose(S._csr._D.sum(), tb.no)
Ejemplo n.º 5
0
 def test_eig1(self, setup):
     # Test of eigenvalues
     R, param = [0.1, 1.5], [1., 0.1]
     g = setup.g.tile(2, 0).tile(2, 1).tile(2, 2)
     H = Hamiltonian(g)
     H.construct((R, param), eta=True)
     eig1 = H.eigh(dtype=np.complex64)
     assert np.allclose(eig1, H.eigh(dtype=np.complex128))
     H.eigsh(n=4)
     H.empty()
     del H
Ejemplo n.º 6
0
def test_wavefunction1():
    N = 50
    o1 = SphericalOrbital(0, (np.linspace(0, 2, N), np.exp(-np.linspace(0, 100, N))))
    G = Geometry([[1] * 3, [2] * 3], Atom(6, o1), sc=[4, 4, 4])
    H = Hamiltonian(G)
    R, param = [0.1, 1.5], [1., 0.1]
    H.construct([R, param])
    ES = H.eigenstate(dtype=np.float64)
    # Plot in the full thing
    grid = Grid(0.1, geometry=H.geom)
    grid.fill(0.)
    ES.sub(0).wavefunction(grid)
Ejemplo n.º 7
0
def test_tbt_delta_fail_list_col(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.dH.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    H.construct([sisl_system.R, sisl_system.t])

    with deltancSileTBtrans(f, 'w') as sile:
        sile.write_delta(H, E=-1.)
        edges = H.edges(0)
        i = edges.max() + 1
        del H[0, i - 1]
        H[0, i] = 1.
        sile.write_delta(H, E=1.)
Ejemplo n.º 8
0
 def test_eig1(self):
     # Test of eigenvalues
     R, param = [0.1, 1.5], [1., 0.1]
     g = self.g.tile(2, 0).tile(2, 1).tile(2, 2)
     H = Hamiltonian(g)
     H.construct((R, param), eta=True)
     eig1 = H.eigh()
     for i in range(2):
         assert_true(np.allclose(eig1, H.eigh()))
     H.eigsh(n=4)
     H.empty()
     del H
Ejemplo n.º 9
0
 def test_shift3(self, setup):
     R, param = [0.1, 1.5], [(1., -1., 1.), (0.1, 0.1, 0.1)]
     H = Hamiltonian(setup.g.copy(), spin=Spin('P'), orthogonal=False)
     H.construct([R, param])
     eig0_0 = H.eigh(spin=0)[0]
     eig1_0 = H.eigh(spin=1)[0]
     H.shift(0.2)
     assert H.eigh(spin=0)[0] == pytest.approx(eig0_0 + 0.2)
     assert H.eigh(spin=1)[0] == pytest.approx(eig1_0 + 0.2)
     H.shift([0, -0.2])
     assert H.eigh(spin=0)[0] == pytest.approx(eig0_0 + 0.2)
     assert H.eigh(spin=1)[0] == pytest.approx(eig1_0)
Ejemplo n.º 10
0
    def test_gauge_inv_eff(self, setup):
        R, param = [0.1, 1.5], [1., 0.1]
        g = setup.g.tile(2, 0).tile(2, 1).tile(2, 2)
        H = Hamiltonian(g)
        H.construct((R, param))

        k = [0.1] * 3
        ie1 = H.eigenstate(k, gauge='R').inv_eff_mass_tensor()
        ie2 = H.eigenstate(k, gauge='r').inv_eff_mass_tensor()
        print(ie1)
        print(ie2)
        assert np.allclose(ie1, ie2)
Ejemplo n.º 11
0
def test_tbt_delta_write_read(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.dH.nc', _dir)
    H = Hamiltonian(sisl_system.gtb, dtype=np.complex64)
    H.construct([sisl_system.R, sisl_system.t])
    H.finalize()

    with deltancSileTBtrans(f, 'w') as sile:
        sile.write_delta(H)
    with deltancSileTBtrans(f, 'r') as sile:
        h = sile.read_delta()
    assert h.spsame(H)
    assert h.dkind == H.dkind
Ejemplo n.º 12
0
def test_wavefunction_eta():
    N = 50
    o1 = SphericalOrbital(0, (np.linspace(0, 2, N), np.exp(-np.linspace(0, 100, N))))
    G = Geometry([[1] * 3, [2] * 3], Atom(6, o1), sc=[4, 4, 4])
    H = Hamiltonian(G, spin=Spin('nc'))
    R, param = [0.1, 1.5], [[0., 0., 0.1, -0.1],
                            [1., 1., 0.1, -0.1]]
    H.construct([R, param])
    ES = H.eigenstate()
    # Plot in the full thing
    grid = Grid(0.1, dtype=np.complex128, sc=SuperCell([2, 2, 2], origo=[-1] * 3))
    grid.fill(0.)
    ES.sub(0).wavefunction(grid, eta=True)
Ejemplo n.º 13
0
def test_wavefunction2():
    N = 50
    o1 = SphericalOrbital(0, (np.linspace(0, 2, N), np.exp(-np.linspace(0, 100, N))))
    G = Geometry([[1] * 3, [2] * 3], Atom(6, o1), sc=[4, 4, 4])
    H = Hamiltonian(G)
    R, param = [0.1, 1.5], [1., 0.1]
    H.construct([R, param])
    ES = H.eigenstate(dtype=np.float64)
    # This is effectively plotting outside where no atoms exists
    # (there could however still be psi weight).
    grid = Grid(0.1, sc=SuperCell([2, 2, 2], origo=[2] * 3))
    grid.fill(0.)
    ES.sub(0).wavefunction(grid)
Ejemplo n.º 14
0
def test_tbt_delta_fail_ncol(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.dH.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    H.construct([sisl_system.R, sisl_system.t])

    with deltancSileTBtrans(f, 'w') as sile:
        sile.write_delta(H, E=-1.)
        edges = H.edges(0)
        i = edges.max() + 1
        H[0, i] = 1.
        H.finalize()
        with pytest.raises(ValueError):
            sile.write_delta(H, E=1.)
def test_nc2(sisl_tmp, sisl_system):
    f = sisl_tmp('grS.nc', _dir)
    tb = Hamiltonian(sisl_system.gtb, orthogonal=False)
    tb.construct([sisl_system.R, sisl_system.tS])
    tb.write(ncSileSiesta(f, 'w'))

    ntb = ncSileSiesta(f).read_hamiltonian()

    # Assert they are the same
    assert np.allclose(tb.cell, ntb.cell)
    assert np.allclose(tb.xyz, ntb.xyz)
    assert np.allclose(tb._csr._D, ntb._csr._D)
    assert sisl_system.g.atom.equal(ntb.atom, R=False)
Ejemplo n.º 16
0
    def test_nc3(self):
        f = osp.join(self.d, 'grS.nc')
        tb = Hamiltonian(self.gtb, orthogonal=False)
        tb.construct([self.R, self.tS])
        tb.write(ncSileSiesta(f, 'w'))

        ntb = ncSileSiesta(f).read_hamiltonian()

        # Assert they are the same
        assert_true(np.allclose(tb.cell, ntb.cell))
        assert_true(np.allclose(tb.xyz, ntb.xyz))
        assert_true(np.allclose(tb._csr._D, ntb._csr._D))
        assert_true(self.g.atom.equal(ntb.atom, R=False))
Ejemplo n.º 17
0
 def test_set5(self):
     # Test of HUGE construct
     g = self.g.tile(10, 0).tile(10, 1).tile(10, 2)
     H = Hamiltonian(g)
     H.construct( (0.1, 1.5), (1., 0.1) )
     assert_true(H.H[0,0] == 1.)
     assert_true(H.H[1,1] == 1.)
     assert_true(H.H[1,0] == 0.1)
     assert_true(H.H[0,1] == 0.1)
     # This is graphene
     # on-site == len(H)
     # nn == 3 * len(H)
     assert_true(H.nnz == len(H) * 4)
     del H
Ejemplo n.º 18
0
    def test_nc3(self):
        f = osp.join(self.d, 'grS.nc')
        tb = Hamiltonian(self.gtb, orthogonal=False)
        tb.construct(self.dR, self.tS)
        tb.write(ncSileSiesta(f, 'w'))

        ntb = ncSileSiesta(f).read_es()

        # Assert they are the same
        assert_true(np.allclose(tb.cell, ntb.cell))
        assert_true(np.allclose(tb.xyz, ntb.xyz))
        assert_true(np.allclose(tb._data._D, ntb._data._D))
        for ia in ntb.geom:
            assert_true(self.g.atom[ia] == ntb.atom[ia])
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
 def test_berry_phase_zak(self):
     # SSH model, topological cell
     g = Geometry([[-.6, 0, 0], [0.6, 0, 0]], Atom(1, 1.001), sc=[2, 10, 10])
     g.set_nsc([3, 1, 1])
     H = Hamiltonian(g)
     H.construct([(0.1, 1.0, 1.5), (0, 1., 0.5)])
     # Contour
     k = np.linspace(0.0, 1.0, 101)
     K = np.zeros([k.size, 3])
     K[:, 0] = k
     bz = BrillouinZone(H, K)
     assert np.allclose(np.abs(berry_phase(bz, sub=0, method='zak')), np.pi)
     # Just to do the other branch
     berry_phase(bz, method='zak')
Ejemplo n.º 21
0
def test_nc1(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.nc', _dir)
    tb = Hamiltonian(sisl_system.gtb)
    tb.construct([sisl_system.R, sisl_system.t])
    tb.write(ncSileSiesta(f, 'w'))

    ntb = ncSileSiesta(f).read_hamiltonian()

    # Assert they are the same
    assert np.allclose(tb.cell, ntb.cell)
    assert np.allclose(tb.xyz, ntb.xyz)
    tb.finalize()
    assert np.allclose(tb._csr._D[:, 0], ntb._csr._D[:, 0])
    assert sisl_system.g.atoms.equal(ntb.atoms, R=False)
Ejemplo n.º 22
0
 def test_set5(self, setup):
     # Test of HUGE construct
     g = setup.g.tile(10, 0).tile(10, 1).tile(10, 2)
     H = Hamiltonian(g)
     H.construct([(0.1, 1.5), (1., 0.1)])
     assert H.H[0, 0] == 1.
     assert H.H[1, 1] == 1.
     assert H.H[1, 0] == 0.1
     assert H.H[0, 1] == 0.1
     # This is graphene
     # on-site == len(H)
     # nn == 3 * len(H)
     assert H.nnz == len(H) * 4
     del H
Ejemplo n.º 23
0
    def test_repeat2(self, setup):
        R, param = [0.1, 1.5], [1., 0.1]

        # Create reference
        Hg = Hamiltonian(setup.g.repeat(2, 0).repeat(2, 1).repeat(2, 2))
        Hg.construct([R, param])
        Hg.finalize()
        H = Hamiltonian(setup.g)
        H.construct([R, param])
        H = H.repeat(2, 0).repeat(2, 1).repeat(2, 2)
        assert Hg.spsame(H)
        H.finalize()
        Hg.finalize()
        assert np.allclose(H._csr._D, Hg._csr._D)
Ejemplo n.º 24
0
def test_tbt_delta1(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.dH.nc', _dir)
    H = Hamiltonian(sisl_system.gtb)
    H.construct([sisl_system.R, sisl_system.t])

    # annoyingly this has to be performed like this...
    with deltancSileTBtrans(f, 'w') as sile:
        H.geom.write(sile)
    with deltancSileTBtrans(f, 'a') as sile:

        # Write to level-1
        sile.write_delta(H)
        # Write to level-2
        sile.write_delta(H, k=[0, 0, .5])
        assert sile._get_lvl(2).variables['kpt'].shape == (1, 3)
        # Write to level-3
        sile.write_delta(H, E=0.1)
        assert sile._get_lvl(3).variables['E'].shape == (1, )
        sile.write_delta(H, E=0.2)
        assert sile._get_lvl(3).variables['E'].shape == (2, )
        # Write to level-4
        sile.write_delta(H, E=0.1, k=[0, 0, .5])
        assert sile._get_lvl(4).variables['kpt'].shape == (1, 3)
        assert sile._get_lvl(4).variables['E'].shape == (1, )
        sile.write_delta(H, E=0.2, k=[0, 0, .5])
        assert sile._get_lvl(4).variables['kpt'].shape == (1, 3)
        assert sile._get_lvl(4).variables['E'].shape == (2, )
        sile.write_delta(H, E=0.2, k=[0, 1., .5])
        assert sile._get_lvl(4).variables['kpt'].shape == (2, 3)
        assert sile._get_lvl(4).variables['E'].shape == (2, )

    with deltancSileTBtrans(f, 'r') as sile:

        # Read to level-1
        h = sile.read_delta()
        assert h.spsame(H)
        # Read level-2
        h = sile.read_delta(k=[0, 0, .5])
        assert h.spsame(H)
        # Read level-3
        h = sile.read_delta(E=0.1)
        assert h.spsame(H)
        # Read level-4
        h = sile.read_delta(E=0.1, k=[0, 0, .5])
        assert h.spsame(H)
        h = sile.read_delta(E=0.1, k=[0, 0., .5])
        assert h.spsame(H)
        h = sile.read_delta(E=0.2, k=[0, 1., .5])
        assert h.spsame(H)
Ejemplo n.º 25
0
def test_nc_H_non_colinear(sisl_tmp):
    H1 = Hamiltonian(sisl.geom.graphene(), spin=sisl.Spin('NC'))
    H1.construct(([0.1, 1.44], [0.1, 0.2, 0.3, 0.4]))

    f1 = sisl_tmp('H1.nc', _dir)
    f2 = sisl_tmp('H2.nc', _dir)
    H1.write(f1)
    H1.finalize()
    H2 = sisl.get_sile(f1).read_hamiltonian()
    H2.write(f2)
    H3 = sisl.get_sile(f2).read_hamiltonian()
    assert H1._csr.spsame(H2._csr)
    assert np.allclose(H1._csr._D, H2._csr._D)
    assert H1._csr.spsame(H3._csr)
    assert np.allclose(H1._csr._D, H3._csr._D)
Ejemplo n.º 26
0
def test_nc2(sisl_tmp, sisl_system):
    f = sisl_tmp('grS.nc', _dir)
    tb = Hamiltonian(sisl_system.gtb, orthogonal=False)
    tb.construct([sisl_system.R, sisl_system.tS])
    with ncSileSiesta(f, 'w') as s:
        tb.write(s)

    with ncSileSiesta(f) as f:
        ntb = f.read_hamiltonian()

    # Assert they are the same
    assert np.allclose(tb.cell, ntb.cell)
    assert np.allclose(tb.xyz, ntb.xyz)
    tb.finalize()
    assert np.allclose(tb._csr._D[:, 0], ntb._csr._D[:, 0])
    assert sisl_system.g.atoms.equal(ntb.atoms, R=False)
Ejemplo n.º 27
0
    def test_sub1(self):
        R, param = [0.1, 1.5], [1., 0.1]

        # Create reference
        H = Hamiltonian(self.g)
        H.construct([R, param])
        H.finalize()
        # Tiling in this direction will not introduce
        # any new connections.
        # So tiling and removing is a no-op (but
        # increases vacuum in 3rd lattice vector)
        Hg = Hamiltonian(self.g.tile(2, 2))
        Hg.construct([R, param])
        Hg = Hg.sub(range(len(self.g)))
        Hg.finalize()
        assert_true(Hg.spsame(H))
Ejemplo n.º 28
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)
Ejemplo n.º 29
0
def test_nc_H_spin_orbit_nc2tshs2nc(sisl_tmp):
    H1 = Hamiltonian(sisl.geom.graphene(), spin=sisl.Spin('SO'))
    H1.construct(([0.1, 1.44], [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
                                [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]]))

    f1 = sisl_tmp('H1.nc', _dir)
    f2 = sisl_tmp('H2.TSHS', _dir)
    H1.write(f1)
    H1.finalize()
    H2 = sisl.get_sile(f1).read_hamiltonian()
    H2.write(f2)
    H3 = sisl.get_sile(f2).read_hamiltonian()
    assert H1._csr.spsame(H2._csr)
    assert np.allclose(H1._csr._D, H2._csr._D)
    assert H1._csr.spsame(H3._csr)
    assert np.allclose(H1._csr._D, H3._csr._D)
Ejemplo n.º 30
0
 def test_wrap_oplist(self, setup):
     R, param = [0.1, 1.5], [1, 2.1]
     H = Hamiltonian(setup.g.copy())
     H.construct([R, param])
     bz = MonkhorstPack(H, [10, 10, 1])
     E = np.linspace(-4, 4, 1000)
     dist = get_distribution('gaussian', smearing=0.05)
     def wrap(es, parent, k, weight):
         DOS = es.DOS(E, distribution=dist)
         PDOS = es.PDOS(E, distribution=dist)
         vel = es.velocity() * es.occupation().reshape(-1, 1)
         return oplist([DOS, PDOS, vel])
     bz.asaverage()
     results = bz.eigenstate(wrap=wrap)
     assert np.allclose(bz.DOS(E, distribution=dist), results[0])
     assert np.allclose(bz.PDOS(E, distribution=dist), results[1])
Ejemplo n.º 31
0
    def test_gauge_velocity(self, setup):
        R, param = [0.1, 1.5], [1., 0.1]
        g = setup.g.tile(2, 0).tile(2, 1).tile(2, 2)
        H = Hamiltonian(g)
        H.construct((R, param))

        k = [0.1] * 3
        es1 = H.eigenstate(k, gauge='R')
        es2 = H.eigenstate(k, gauge='r')
        assert np.allclose(es1.velocity(), es2.velocity())

        es2.change_gauge('R')
        assert np.allclose(es1.velocity(), es2.velocity())

        es2.change_gauge('r')
        es1.change_gauge('r')
        assert np.allclose(es1.velocity(), es2.velocity())
Ejemplo n.º 32
0
    def test_gauge_eig(self, setup):
        # Test of eigenvalues
        R, param = [0.1, 1.5], [1., 0.1]
        g = setup.g.tile(2, 0).tile(2, 1).tile(2, 2)
        H = Hamiltonian(g)
        H.construct((R, param))

        k = [0.1] * 3
        es1 = H.eigenstate(k, gauge='R')
        es2 = H.eigenstate(k, gauge='r')
        assert np.allclose(es1.eig, es2.eig)
        assert not np.allclose(es1.state, es2.state)

        es1 = H.eigenstate(k, gauge='R', dtype=np.complex64)
        es2 = H.eigenstate(k, gauge='r', dtype=np.complex64)
        assert np.allclose(es1.eig, es2.eig)
        assert not np.allclose(es1.state, es2.state)
Ejemplo n.º 33
0
def test_nc_overlap(sisl_tmp, sisl_system):
    f = sisl_tmp('gr.nc', _dir)
    tb = Hamiltonian(sisl_system.gtb)
    tb.construct([sisl_system.R, sisl_system.t])
    tb.write(ncSileSiesta(f, 'w'))

    S = ncSileSiesta(f).read_overlap()

    # Ensure no empty data-points
    assert np.allclose(S._csr._D.sum(), tb.no)

    # Write test
    f = sisl_tmp('s.nc', _dir)
    S.write(ncSileSiesta(f, "w"))
    S2 = ncSileSiesta(f).read_overlap()
    assert S._csr.spsame(S2._csr)
    assert np.allclose(S._csr._D, S2._csr._D)
Ejemplo n.º 34
0
    def test_nc2(self):
        f = osp.join(self.d, 'gr.dH.nc')
        H = Hamiltonian(self.gtb)
        H.construct(self.dR, self.t)

        # annoyingly this has to be performed like this...
        sile = dHncSileSiesta(f, 'w')
        H.geom.write(sile)
        sile = dHncSileSiesta(f, 'a')

        # Write to level-1
        H.write(sile)
        # Write to level-2
        H.write(sile, k=[0,0,.5])
        # Write to level-3
        H.write(sile, E=0.1)
        # Write to level-4
        H.write(sile, k=[0,0,.5], E=0.1)
Ejemplo n.º 35
0
class TestHamiltonian(object):
    # Base test class for MaskedArrays.

    def setUp(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])

        C = Atom(Z=6, R=bond * 1.01, orbs=1)
        self.g = Geometry(np.array([[0., 0., 0.],
                                    [1., 0., 0.]], np.float64) * bond,
                          atom=C, sc=self.sc)
        self.H = Hamiltonian(self.g)
        self.HS = Hamiltonian(self.g, orthogonal=False)

        C = Atom(Z=6, R=bond * 1.01, orbs=2)
        self.g2 = Geometry(np.array([[0., 0., 0.],
                                    [1., 0., 0.]], np.float64) * bond,
                          atom=C, sc=self.sc)
        self.H2 = Hamiltonian(self.g2)
        self.HS2 = Hamiltonian(self.g2, orthogonal=False)


    def tearDown(self):
        del self.sc
        del self.g
        del self.H
        del self.HS
        del self.g2
        del self.H2
        del self.HS2

    def test_objects(self):
        print(self.H)
        assert_true(len(self.H.xyz) == 2)
        assert_true(self.g.no == len(self.H))
        assert_true(len(self.HS.xyz) == 2)
        assert_true(self.g.no == len(self.HS))

        assert_true(len(self.H2.xyz) == 2)
        assert_true(self.g2.no == len(self.H2))
        assert_true(len(self.HS2.xyz) == 2)
        assert_true(self.g2.no == len(self.HS2))

    def test_dtype(self):
        assert_true(self.H.dtype == np.float64)
        assert_true(self.HS.dtype == np.float64)
        assert_true(self.H2.dtype == np.float64)
        assert_true(self.HS2.dtype == np.float64)

    def test_ortho(self):
        assert_true(self.H.orthogonal)
        assert_false(self.HS.orthogonal)

    def test_set1(self):
        self.H.H[0,0] = 1.
        assert_true(self.H[0,0] == 1.)
        assert_true(self.H[1,0] == 0.)
        self.H.empty()

        self.HS.H[0,0] = 1.
        assert_true(self.HS.H[0,0] == 1.)
        assert_true(self.HS.H[1,0] == 0.)
        assert_true(self.HS.S[0,0] == 0.)
        assert_true(self.HS.S[1,0] == 0.)
        self.HS.S[0,0] = 1.
        assert_true(self.HS.H[0,0] == 1.)
        assert_true(self.HS.H[1,0] == 0.)
        assert_true(self.HS.S[0,0] == 1.)
        assert_true(self.HS.S[1,0] == 0.)

        # delete before creating the same content
        self.HS.empty()
        self.HS[0,0] = 1., 1.
        assert_true(self.HS.H[0,0] == 1.)
        assert_true(self.HS.S[0,0] == 1.)
        self.HS.empty()

    def test_set2(self):
        self.H.construct((0.1,1.5), (1.,0.1))
        assert_true(self.H[0,0] == 1.)
        assert_true(self.H[1,0] == 0.1)
        assert_true(self.H[0,1] == 0.1)
        self.H.empty()

    def test_set3(self):
        self.HS.construct((0.1, 1.5), ((1., 2.), (0.1, 0.2)))
        assert_true(self.HS.H[0,0] == 1.)
        assert_true(self.HS.S[0,0] == 2.)
        assert_true(self.HS.H[1,1] == 1.)
        assert_true(self.HS.S[1,1] == 2.)
        assert_true(self.HS.H[1,0] == 0.1)
        assert_true(self.HS.H[0,1] == 0.1)
        assert_true(self.HS.S[1,0] == 0.2)
        assert_true(self.HS.S[0,1] == 0.2)
        assert_true(self.HS.nnz == len(self.HS) * 4)
        self.HS.empty()

    def test_set4(self):
        for ia, io in self.H:
            # Find atoms close to 'ia'
            idx = self.H.geom.close(ia, dR=(0.1, 1.5) )
            self.H[io, idx[0]] = 1.
            self.H[io, idx[1]] = 0.1
        assert_true(self.H.H[0,0] == 1.)
        assert_true(self.H.H[1,1] == 1.)
        assert_true(self.H.H[1,0] == 0.1)
        assert_true(self.H.H[0,1] == 0.1)
        assert_true(self.H.nnz == len(self.H) * 4)
        self.H.empty()

    @attr('slow')
    def test_set5(self):
        # Test of HUGE construct
        g = self.g.tile(10, 0).tile(10, 1).tile(10, 2)
        H = Hamiltonian(g)
        H.construct( (0.1, 1.5), (1., 0.1) )
        assert_true(H.H[0,0] == 1.)
        assert_true(H.H[1,1] == 1.)
        assert_true(H.H[1,0] == 0.1)
        assert_true(H.H[0,1] == 0.1)
        # This is graphene
        # on-site == len(H)
        # nn == 3 * len(H)
        assert_true(H.nnz == len(H) * 4)
        del H

    def test_op1(self):
        g = Geometry([[i, 0,0] for i in range(100)], Atom(6, R=1.01), sc=[100])
        H = Hamiltonian(g, dtype=np.int32)
        for i in range(10):
            j = range(i*4, i*4+3)
            H[0, j] = i

            # i+
            H += 1
            for jj in j:
                assert_equal(H[0, jj], i+1)
                assert_equal(H[1, jj], 0)

            # i-
            H -= 1
            for jj in j:
                assert_equal(H[0, jj], i)
                assert_equal(H[1, jj], 0)

            # i*
            H *= 2
            for jj in j:
                assert_equal(H[0, jj], i*2)
                assert_equal(H[1, jj], 0)

            # //
            H //= 2
            for jj in j:
                assert_equal(H[0, jj], i)
                assert_equal(H[1, jj], 0)

            # i**
            H **= 2
            for jj in j:
                assert_equal(H[0, jj], i**2)
                assert_equal(H[1, jj], 0)

    def test_op2(self):
        g = Geometry([[i, 0,0] for i in range(100)], Atom(6, R=1.01), sc=[100])
        H = Hamiltonian(g, dtype=np.int32)
        for i in range(10):
            j = range(i*4, i*4+3)
            H[0, j] = i

            # +
            s = H + 1
            for jj in j:
                assert_equal(s[0, jj], i+1)
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

            # -
            s = H - 1
            for jj in j:
                assert_equal(s[0, jj], i-1)
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

            # -
            s = 1 - H
            for jj in j:
                assert_equal(s[0, jj], 1-i)
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

            # *
            s = H * 2
            for jj in j:
                assert_equal(s[0, jj], i*2)
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

            # //
            s = s // 2
            for jj in j:
                assert_equal(s[0, jj], i)
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

            # **
            s = H ** 2
            for jj in j:
                assert_equal(s[0, jj], i**2)
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

            # ** (r)
            s = 2 ** H
            for jj in j:
                assert_equal(s[0, jj], 2 ** H[0, jj])
                assert_equal(H[0, jj], i)
                assert_equal(s[1, jj], 0)

    def test_op3(self):
        g = Geometry([[i, 0,0] for i in range(100)], Atom(6, R=1.01), sc=[100])
        H = Hamiltonian(g, dtype=np.int32)
        # Create initial stuff
        for i in range(10):
            j = range(i*4, i*4+3)
            H[0, j] = i

        for op in ['add', 'sub', 'mul', 'pow']:
            func = getattr(H, '__{}__'.format(op))
            h = func(1)
            assert_equal(h.dtype, np.int32)
            h = func(1.)
            assert_equal(h.dtype, np.float64)
            if op != 'pow':
                h = func(1.j)
                assert_equal(h.dtype, np.complex128)

        H = H.copy(dtype=np.float64)
        for op in ['add', 'sub', 'mul', 'pow']:
            func = getattr(H, '__{}__'.format(op))
            h = func(1)
            assert_equal(h.dtype, np.float64)
            h = func(1.)
            assert_equal(h.dtype, np.float64)
            if op != 'pow':
                h = func(1.j)
                assert_equal(h.dtype, np.complex128)

        H = H.copy(dtype=np.complex128)
        for op in ['add', 'sub', 'mul', 'pow']:
            func = getattr(H, '__{}__'.format(op))
            h = func(1)
            assert_equal(h.dtype, np.complex128)
            h = func(1.)
            assert_equal(h.dtype, np.complex128)
            if op != 'pow':
                h = func(1.j)
                assert_equal(h.dtype, np.complex128)

    def test_op4(self):
        g = Geometry([[i, 0,0] for i in range(100)], Atom(6, R=1.01), sc=[100])
        H = Hamiltonian(g, dtype=np.int32)
        # Create initial stuff
        for i in range(10):
            j = range(i*4, i*4+3)
            H[0, j] = i

        h = 1 + H
        assert_equal(h.dtype, np.int32)
        h = 1. + H
        assert_equal(h.dtype, np.float64)
        h = 1.j + H
        assert_equal(h.dtype, np.complex128)

        h = 1 - H
        assert_equal(h.dtype, np.int32)
        h = 1. - H
        assert_equal(h.dtype, np.float64)
        h = 1.j - H
        assert_equal(h.dtype, np.complex128)
        
        h = 1 * H
        assert_equal(h.dtype, np.int32)
        h = 1. * H
        assert_equal(h.dtype, np.float64)
        h = 1.j * H
        assert_equal(h.dtype, np.complex128)

        h = 1 ** H
        assert_equal(h.dtype, np.int32)
        h = 1. ** H
        assert_equal(h.dtype, np.float64)
        h = 1.j ** H
        assert_equal(h.dtype, np.complex128)

    def test_eig1(self):
        # Test of eigenvalues
        g = self.g.tile(2, 0).tile(2, 1).tile(2, 2)
        H = Hamiltonian(g)
        H.construct((0.1,1.5), (1.,0.1))
        H.eigh()
        H.eigsh(n=4)
        H.empty()
        del H

    def test_eig2(self):
        # Test of eigenvalues
        self.HS.construct((0.1,1.5), ((1.,1.), (0.1,0.1)))
        self.HS.eigh()
        self.HS.empty()

    def test_spin2(self):
        g = Geometry([[i, 0,0] for i in range(10)], Atom(6, R=1.01), sc=[100])
        H = Hamiltonian(g, dtype=np.int32, spin=2)
        for i in range(10):
            j = range(i*4, i*4+3)
            H[0, j] = (i, i*2)


    def test_finalized(self):
        assert_false(self.H.finalized)
        self.H.H[0,0] = 1.
        self.H.finalize()
        assert_true(self.H.finalized)
        assert_true(self.H.nnz == 1)
        self.H.empty()
        assert_false(self.HS.finalized)
        self.HS[0,0] = 1., 1.
        self.HS.finalize()
        assert_true(self.HS.finalized)
        assert_true(self.HS.nnz == 1)
        self.HS.empty()