1import numpy as np
2import pytest
3
4from ase.build import bulk
5from ase.calculators.test import FreeElectrons
6from ase.dft.kpoints import special_paths
7from ase.spectrum.band_structure import BandStructure
8
9
10def test_bandstructure(testdir, plt):
11    atoms = bulk('Cu')
12    path = special_paths['fcc']
13    atoms.calc = FreeElectrons(nvalence=1,
14                               kpts={'path': path, 'npoints': 200})
15    atoms.get_potential_energy()
16    bs = atoms.calc.band_structure()
17    coords, labelcoords, labels = bs.get_labels()
18    print(labels)
19    bs.write('hmm.json')
20    bs = BandStructure.read('hmm.json')
21    coords, labelcoords, labels = bs.get_labels()
22    print(labels)
23    assert ''.join(labels) == 'GXWKGLUWLKUX'
24    bs.plot(emax=10, filename='bs.png')
25
26
27@pytest.fixture
28def bs():
29    from ase.lattice import RHL
30    rhl = RHL(4.0, 65.0)
31    path = rhl.bandpath()
32    return path.free_electron_band_structure()
33
34
35def test_print_bs(bs):
36    print(bs)
37
38
39def test_subtract_ref(bs):
40    avg = np.mean(bs.energies)
41    bs._reference = 5
42    bs2 = bs.subtract_reference()
43    avg2 = np.mean(bs2.energies)
44    assert avg - 5 == pytest.approx(avg2)
45