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_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])
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_wavefunction3(): 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)
def test_fermi_level(self, setup): R, param = [0.1, 1.5], [(1., 1.), (2.1, 0.1)] H = Hamiltonian(setup.g.copy(), orthogonal=False) H.construct([R, param]) bz = MonkhorstPack(H, [10, 10, 1]) Ef = H.fermi_level(bz, q=1) H.shift(-Ef) assert H.fermi_level(bz, q=1) == pytest.approx(0., abs=1e-6)
def test_op4(self, setup): 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 h.dtype == np.int32 h = 1. + H assert h.dtype == np.float64 h = 1.j + H assert h.dtype == np.complex128 h = 1 - H assert h.dtype == np.int32 h = 1. - H assert h.dtype == np.float64 h = 1.j - H assert h.dtype == np.complex128 h = 1 * H assert h.dtype == np.int32 h = 1. * H assert h.dtype == np.float64 h = 1.j * H assert h.dtype == np.complex128 h = 1 ** H assert h.dtype == np.int32 h = 1. ** H assert h.dtype == np.float64 h = 1.j ** H assert h.dtype == np.complex128
def test_non_collinear1(self): g = Geometry([[i, 0, 0] for i in range(10)], Atom(6, R=1.01), sc=[100]) H = Hamiltonian(g, dtype=np.float64, spin=4) for i in range(10): j = range(i * 4, i * 4 + 3) H[i, i, 0] = 0. H[i, i, 1] = 0. H[i, i, 2] = 0.1 H[i, i, 3] = 0.1 if i > 0: H[i, i - 1, 0] = 1. H[i, i - 1, 1] = 1. if i < 9: H[i, i + 1, 0] = 1. H[i, i + 1, 1] = 1. assert_true(len(H.eigh()) == len(H) * 2)
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')
def sisl_system(): """ A preset list of geometries/Hamiltonians. """ class System: pass d = System() alat = 1.42 sq3h = 3.**.5 * 0.5 C = Atom(Z=6, R=1.42) sc = SuperCell(np.array([[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) * alat, nsc=[3, 3, 1]) d.g = Geometry(np.array([[0., 0., 0.], [1., 0., 0.]], np.float64) * alat, atom=C, sc=sc) d.R = np.array([0.1, 1.5]) d.t = np.array([0., 2.7]) d.tS = np.array([(0., 1.0), (2.7, 0.)]) d.C = Atom(Z=6, R=max(d.R)) d.sc = SuperCell(np.array([[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) * alat, nsc=[3, 3, 1]) d.gtb = Geometry(np.array([[0., 0., 0.], [1., 0., 0.]], np.float64) * alat, atom=C, sc=sc) d.ham = Hamiltonian(d.gtb) d.ham.construct([(0.1, 1.5), (0.1, 2.7)]) return d
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])
def test_berry_phase_fail_loop(self, setup): g = setup.g.tile(2, 0).tile(2, 1).tile(2, 2) H = Hamiltonian(g) bz = BandStructure.param_circle(H, 20, 0.01, [0, 0, 1], [1 / 3] * 3, loop=True) elec.berry_phase(bz)
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]) C = Atom(Z=6, R=[bond * 1.01]) self.g = Geometry(np.array([[0., 0., 0.], [1., 0., 0.]], np.float64) * bond, atom=C, sc=self.sc) self.H = Hamiltonian(self.g) func = self.H.create_construct([0.1, bond+0.1], [0., -2.7]) self.H.construct(func) self.HS = Hamiltonian(self.g, orthogonal=False) func = self.HS.create_construct([0.1, bond+0.1], [(0., 1.), (-2.7, 0.)]) self.HS.construct(func)
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())
def test_eigh_vs_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), eta=True) eig1 = H.eigh(dtype=np.complex64) eig2 = np.sort(H.eig(dtype=np.complex64).real) eig3 = np.sort(H.eig(eigvals_only=False, dtype=np.complex64)[0].real) assert np.allclose(eig1, eig2, atol=1e-5) assert np.allclose(eig1, eig3, atol=1e-5) eig1 = H.eigh([0.01] * 3, dtype=np.complex64) eig2 = np.sort(H.eig([0.01] * 3, dtype=np.complex64).real) eig3 = np.sort(H.eig([0.01] * 3, eigvals_only=False, dtype=np.complex64)[0].real) assert np.allclose(eig1, eig2, atol=1e-5) assert np.allclose(eig1, eig3, atol=1e-5)
def test_fermi_level_spin_separate(self, setup): R, param = [0.1, 1.5], [(1., 1.), (2.1, 0.1)] H = Hamiltonian(setup.g.copy(), spin=Spin('P')) H.construct([R, param]) bz = MonkhorstPack(H, [10, 10, 1]) q = [0.5, 0.3] Ef = H.fermi_level(bz, q=q) assert len(Ef) == 2 H.shift(-Ef) assert np.allclose(H.fermi_level(bz, q=q), 0.)
def test_fermi_level_spin(self, setup): R, param = [0.1, 1.5], [(1., 1.), (2.1, 0.1)] H = Hamiltonian(setup.g.copy(), spin=Spin('P')) H.construct([R, param]) bz = MonkhorstPack(H, [10, 10, 1]) q = 1.1 Ef = H.fermi_level(bz, q=q) assert np.asarray(Ef).ndim == 0 H.shift(-Ef) assert H.fermi_level(bz, q=q) == pytest.approx(0., abs=1e-6)
def test_repeat4(self, setup): def func(self, ia, idxs, idxs_xyz=None): idx = self.geom.close(ia, R=[0.1, 1.43], idx=idxs) io = self.geom.a2o(ia) # Set on-site on first and second orbital odx = self.geom.a2o(idx[0]) self[io, odx] = -1. self[io + 1, odx + 1] = 1. # Set connecting odx = self.geom.a2o(idx[1]) self[io, odx] = 0.2 self[io, odx + 1] = 0.01 self[io + 1, odx] = 0.01 self[io + 1, odx + 1] = 0.3 setup.H2.construct(func) Hbig = setup.H2.repeat(3, 0).repeat(3, 1) gbig = setup.H2.geom.repeat(3, 0).repeat(3, 1) H = Hamiltonian(gbig) H.construct(func) assert H.spsame(Hbig) H.finalize() Hbig.finalize() assert np.allclose(H._csr._D, Hbig._csr._D) setup.H2.empty()
def test_read_write_hamiltonian(self): G = self.g.rotatec(-30) H = Hamiltonian(G) H.construct([[0.1, 1.45], [0.1, -2.7]]) print(H) f = mkstemp(dir=self.d)[1] read_hamiltonian = get_siles(['read_hamiltonian']) for sile in get_siles(['write_hamiltonian']): if not sile in read_hamiltonian: continue # Write sile(f, mode='w').write_hamiltonian(H) # Read 1 try: h = sile(f, mode='r').read_hamiltonian() assert_true(H.spsame(h)) except UnicodeDecodeError as e: pass # Read 2 try: h = Hamiltonian.read(sile(f, mode='r')) assert_true(H.spsame(h)) except UnicodeDecodeError as e: pass # Clean-up file os.remove(f)
def test_spin_squared(self, setup, k): g = Geometry([[i, 0, 0] for i in range(10)], Atom(6, R=1.01), sc=SuperCell(1, nsc=[3, 1, 1])) H = Hamiltonian(g, spin=Spin.POLARIZED) H.construct(([0.1, 1.1], [[0, 0.1], [1, 1.1]])) H[0, 0] = (0.1, 0.) H[0, 1] = (0.5, 0.4) es_alpha = H.eigenstate(k, spin=0) es_beta = H.eigenstate(k, spin=1) sup, sdn = spin_squared(es_alpha.state, es_beta.state) sup1, sdn1 = H.spin_squared(k) assert sup.sum() == pytest.approx(sdn.sum()) assert np.all(sup1 == sup) assert np.all(sdn1 == sdn) assert len(sup) == es_alpha.shape[0] assert len(sdn) == es_beta.shape[0] sup, sdn = spin_squared(es_alpha.sub(range(2)).state, es_beta.state) assert sup.sum() == pytest.approx(sdn.sum()) assert len(sup) == 2 assert len(sdn) == es_beta.shape[0] sup, sdn = spin_squared( es_alpha.sub(range(3)).state, es_beta.sub(range(2)).state) sup1, sdn1 = H.spin_squared(k, 3, 2) assert sup.sum() == pytest.approx(sdn.sum()) assert np.all(sup1 == sup) assert np.all(sdn1 == sdn) assert len(sup) == 3 assert len(sdn) == 2 sup, sdn = spin_squared( es_alpha.sub(0).state.ravel(), es_beta.sub(range(2)).state) assert sup.sum() == pytest.approx(sdn.sum()) assert sup.ndim == 1 assert len(sup) == 1 assert len(sdn) == 2 sup, sdn = spin_squared( es_alpha.sub(0).state.ravel(), es_beta.sub(0).state.ravel()) assert sup.sum() == pytest.approx(sdn.sum()) assert sup.ndim == 0 assert sdn.ndim == 0 sup, sdn = spin_squared( es_alpha.sub(range(2)).state, es_beta.sub(0).state.ravel()) assert sup.sum() == pytest.approx(sdn.sum()) assert len(sup) == 2 assert len(sdn) == 1
def test_read_write_hamiltonian_overlap(self, sisl_tmp, sisl_system, sile): if issubclass(sile, _gfSileSiesta): return G = sisl_system.g.rotate(-30, sisl_system.g.cell[2, :]) H = Hamiltonian(G, orthogonal=False) H.construct([[0.1, 1.45], [(0.1, 1), (-2.7, 0.1)]]) f = sisl_tmp("test_read_write_hamiltonian_overlap.win", _dir) # Write with sile(f, mode='w') as s: s.write_hamiltonian(H) # Read 1 try: with sile(f, mode='r') as s: h = s.read_hamiltonian() assert H.spsame(h) except UnicodeDecodeError as e: pass # Read 2 try: with sile(f, mode='r') as s: h = Hamiltonian.read(s) assert H.spsame(h) except UnicodeDecodeError as e: pass
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_op2(self, setup): 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 s[0, jj] == i + 1 assert H[0, jj] == i assert s[1, jj] == 0 # - s = H - 1 for jj in j: assert s[0, jj] == i - 1 assert H[0, jj] == i assert s[1, jj] == 0 # - s = 1 - H for jj in j: assert s[0, jj] == 1 - i assert H[0, jj] == i assert s[1, jj] == 0 # * s = H * 2 for jj in j: assert s[0, jj] == i * 2 assert H[0, jj] == i assert s[1, jj] == 0 # // s = s // 2 for jj in j: assert s[0, jj] == i assert H[0, jj] == i assert s[1, jj] == 0 # ** s = H**2 for jj in j: assert s[0, jj] == i**2 assert H[0, jj] == i assert s[1, jj] == 0 # ** (r) s = 2**H for jj in j: assert s[0, jj], 2**H[0 == jj] assert H[0, jj] == i assert s[1, jj] == 0
def test_as_simple(self): from sisl import geom, Hamiltonian g = geom.graphene() H = Hamiltonian(g) H.construct([[0.1, 1.44], [0, -2.7]]) bz = MonkhorstPack(H, [2, 2, 2], trs=False) assert len(bz) == 2 ** 3 # Assert that as* all does the same asarray = bz.asarray().eigh() aslist = np.array(bz.aslist().eigh()) asyield = np.array([a for a in bz.asyield().eigh()]) asaverage = bz.asaverage().eigh() assert np.allclose(asarray, aslist) assert np.allclose(asarray, asyield) # Average needs to be performed assert np.allclose((asarray / len(bz)).sum(0), asaverage)
def test_as_single(self): from sisl import geom, Hamiltonian g = geom.graphene() H = Hamiltonian(g) H.construct([[0.1, 1.44], [0, -2.7]]) def wrap(eig): return eig[0] bz = MonkhorstPack(H, [2, 2, 2], trs=False) assert len(bz) == 2**3 # Assert that as* all does the same asarray = bz.apply.array.eigh(wrap=wrap) aslist = np.array(bz.apply.list.eigh(wrap=wrap)) asyield = np.array([a for a in bz.apply.iter.eigh(wrap=wrap)]) assert np.allclose(asarray, aslist) assert np.allclose(asarray, asyield)
def test_berry_phase_loop(self, setup): g = setup.g.tile(2, 0).tile(2, 1).tile(2, 2) H = Hamiltonian(g) bz1 = BandStructure.param_circle(H, 20, 0.01, [0, 0, 1], [1 / 3] * 3) bz2 = BandStructure.param_circle(H, 20, 0.01, [0, 0, 1], [1 / 3] * 3, loop=True) assert np.allclose(berry_phase(bz1), berry_phase(bz2))
def test_as_wrap_default_oplist(self): from sisl import geom, Hamiltonian g = geom.graphene() H = Hamiltonian(g) H.construct([[0.1, 1.44], [0, -2.7]]) bz = MonkhorstPack(H, [2, 2, 2], trs=False).asaverage() assert len(bz) == 2 ** 3 # Check with a wrap function E = np.linspace(-2, 2, 100) def wrap_sum(es, weight): PDOS = es.PDOS(E) * weight return PDOS.sum(0), PDOS DOS, PDOS = bz.assum().eigenstate(wrap=wrap_sum) assert np.allclose(bz.DOS(E), DOS) assert np.allclose(bz.PDOS(E), PDOS)
def test_op3(self, setup): g = Geometry([[i, 0, 0] for i in range(100)], Atom(6, R=1.01), sc=[100]) H = Hamiltonian(g, dtype=np.int32) Hc = H.copy() del Hc # 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 h.dtype == np.int32 h = func(1.) assert h.dtype == np.float64 if op != 'pow': h = func(1.j) assert 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 h.dtype == np.float64 h = func(1.) assert h.dtype == np.float64 if op != 'pow': h = func(1.j) assert 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 h.dtype == np.complex128 h = func(1.) assert h.dtype == np.complex128 if op != 'pow': h = func(1.j) assert h.dtype == np.complex128
def test_real_space_HS_SE_unfold_with_k(): # check that calculating the real-space Green function is equivalent for two equivalent systems sq = Geometry([0] * 3, Atom(1, 1.01), [1]) sq.set_nsc([3] * 3) H = Hamiltonian(sq) H.construct([(0.1, 1.1), (4, -1)]) RSE = RealSpaceSE(H, 0, 1, (3, 4, 1), dk=100, trs=False) k1 = [0, 0, 0.2] k2 = [0, 0, 0.3] for E in [0.1, 1.5]: G1 = RSE.green(E, k1) G2 = RSE.green(E, k2) assert not np.allclose(G1, G2) SE1 = RSE.self_energy(E, k1) SE2 = RSE.self_energy(E, k2) assert not np.allclose(SE1, SE2)
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 __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]) C = Atom(Z=6, R=[bond * 1.01]) 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] * 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 test_as_dataarray(self): pytest.importorskip("xarray", reason="xarray not available") from sisl import geom, Hamiltonian g = geom.graphene() H = Hamiltonian(g) H.construct([[0.1, 1.44], [0, -2.7]]) bz = MonkhorstPack(H, [2, 2, 2], trs=False) # Assert that as* all does the same asarray = bz.apply.array.eigh() bz_da = bz.apply.dataarray asdarray = bz_da.eigh() assert np.allclose(asarray, asdarray.values) assert isinstance(asdarray.bz, MonkhorstPack) assert isinstance(asdarray.parent, Hamiltonian) assert asdarray.dims == ('k', 'v1') asdarray = bz_da.eigh(coords=['orb']) assert asdarray.dims == ('k', 'orb')
def test_wrap_kwargs(arg): from sisl import geom, Hamiltonian g = geom.graphene() H = Hamiltonian(g) H.construct([[0.1, 1.44], [0, -2.7]]) bz = MonkhorstPack(H, [2, 2, 2], trs=False) assert len(bz) == 2**3 def wrap_none(arg): return arg def wrap_kwargs(arg, parent, k, weight): return arg * weight E = np.linspace(-2, 2, 20) bz_array = bz.apply.array asarray1 = (bz_array.DOS(E, wrap=wrap_none) * bz.weight.reshape(-1, 1)).sum(0) asarray2 = bz_array.DOS(E, wrap=wrap_kwargs).sum(0) bz_list = bz.apply.list aslist1 = (np.array(bz_list.DOS(E, wrap=wrap_none)) * bz.weight.reshape(-1, 1)).sum(0) aslist2 = np.array(bz_list.DOS(E, wrap=wrap_kwargs)).sum(0) bz_yield = bz.apply.iter asyield1 = (np.array([a for a in bz_yield.DOS(E, wrap=wrap_none)]) * bz.weight.reshape(-1, 1)).sum(0) asyield2 = np.array([a for a in bz_yield.DOS(E, wrap=wrap_kwargs) ]).sum(0) asaverage = bz.apply.average.DOS(E, wrap=wrap_none) assum = bz.apply.sum.DOS(E, wrap=wrap_kwargs) assert np.allclose(asarray1, asaverage) assert np.allclose(asarray2, asaverage) assert np.allclose(aslist1, asaverage) assert np.allclose(aslist2, asaverage) assert np.allclose(asyield1, asaverage) assert np.allclose(asyield2, asaverage) assert np.allclose(assum, asaverage)
def test_as_dataarray_unzip(self): pytest.importorskip("xarray", reason="xarray not available") from sisl import geom, Hamiltonian g = geom.graphene() H = Hamiltonian(g) H.construct([[0.1, 1.44], [0, -2.7]]) E = np.linspace(-2, 2, 20) bz = MonkhorstPack(H, [2, 2, 1], trs=False) def wrap(es): return es.eig, es.DOS(E), es.PDOS(E) with bz.apply.renew(unzip=True) as unzip: eig, DOS, PDOS = unzip.ndarray.eigenstate(wrap=wrap) ds0 = unzip.dataarray.eigenstate(wrap=wrap, name=["eig", "DOS", "PDOS"]) # explicitly create dimensions ds1 = unzip.dataarray.eigenstate(wrap=wrap, coords=[ { "orb": np.arange(len(H)) }, { "E": E }, { "orb": np.arange(len(H)), "E": E }, ], dims=(['orb'], ['E'], ['orb', 'E']), name=["eig", "DOS", "PDOS"]) for var, data in zip(["eig", "DOS", "PDOS"], [eig, DOS, PDOS]): assert np.allclose(ds0.data_vars[var].values, data) assert np.allclose(ds1.data_vars[var].values, data) assert len(ds1.coords) < len(ds0.coords)
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) tb.finalize() assert np.allclose(tb._csr._D[:, 0], ntb._csr._D[:, 0]) assert sisl_system.g.atoms.equal(ntb.atoms, R=False)
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)
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()
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)