Coverage for tests/integration/test_dos.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-08-01 16:57 +0000

1from __future__ import annotations 

2 

3import pytest 

4import numpy as np 

5 

6from gpaw.mpi import world 

7from rhodent.dos import DOSCalculator 

8 

9 

10@pytest.fixture 

11def dos_calc(gpw_fname, mock_voronoi, zerofermi, atom_projections): 

12 if atom_projections is None: 

13 voronoi = None 

14 else: 

15 voronoi = mock_voronoi(atom_projections=atom_projections) 

16 

17 energies = np.arange(-5, 5, 0.01) 

18 sigma = 0.7 

19 

20 calc = DOSCalculator.from_gpw(gpw_fname, voronoi, energies, sigma, zerofermi=zerofermi) 

21 return calc 

22 

23 

24@pytest.mark.bigdata 

25@pytest.mark.parametrize('test_system', ['Ag8']) 

26@pytest.mark.parametrize('zerofermi', [True, False]) 

27@pytest.mark.parametrize('atom_projections', [[[0, 1, 2], [3]]]) 

28def test_dos_pdos(tmp_path, dos_calc, zerofermi): 

29 """ Test that we can compute the DOS and PDOS 

30 """ 

31 

32 # Write npz and dat files, DOS and PDOS 

33 dos_calc.calculate_dos_and_write(tmp_path / 'dos.npz') 

34 dos_calc.calculate_dos_and_write(tmp_path / 'dos.dat') 

35 dos_calc.calculate_pdos_and_write(tmp_path / 'pdos.dat') 

36 dos_calc.calculate_pdos_and_write(tmp_path / 'pdos.npz') 

37 

38 if world.rank == 0: 

39 dos_archive = np.load(tmp_path / 'dos.npz') 

40 dos_data = np.loadtxt(tmp_path / 'dos.dat') 

41 text = (tmp_path / 'dos.dat').read_text() 

42 if zerofermi: 

43 assert 'relative to Fermi level' in text 

44 else: 

45 assert 'relative to vacuum level' in text 

46 assert 'Gaussian folding' in text 

47 

48 # Compare dat to npz 

49 np.testing.assert_allclose(dos_archive['energy_e'], dos_data[:, 0], rtol=0, atol=1e-12) 

50 np.testing.assert_allclose(dos_archive['dos_e'], dos_data[:, 1]) 

51 assert np.min(dos_archive['dos_e']) >= 0 

52 

53 pdos_archive = np.load(tmp_path / 'pdos.npz') 

54 pdos_data = np.loadtxt(tmp_path / 'pdos.dat') 

55 text = (tmp_path / 'pdos.dat').read_text() 

56 if zerofermi: 

57 assert 'relative to Fermi level' in text 

58 else: 

59 assert 'relative to vacuum level' in text 

60 assert 'Gaussian folding' in text 

61 

62 np.testing.assert_allclose(pdos_archive['energy_e'], pdos_data[:, 0], rtol=0, atol=1e-12) 

63 np.testing.assert_allclose(pdos_archive['pdos_ei'], pdos_data[:, 1:]) 

64 

65 

66@pytest.mark.bigdata 

67@pytest.mark.parametrize('test_system', ['Ag8']) 

68@pytest.mark.parametrize('zerofermi', [True]) 

69@pytest.mark.parametrize('atom_projections', [None]) 

70def test_no_pdos(tmp_path, dos_calc, zerofermi): 

71 """ We cannot compute PDOS if there is no Voronoi 

72 """ 

73 

74 with pytest.raises(ValueError): 

75 dos_calc.calculate_pdos_and_write(tmp_path / 'pdos.npz')