Coverage for gpaw/test/test_nosave_projections.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-14 00:18 +0000

1import pytest 

2from ase.build import bulk 

3from gpaw.new.ase_interface import GPAW 

4from gpaw.mpi import world 

5 

6# Prevent grid-dependent crash: 

7parallel = dict(band=1 if world.size < 8 else 4) 

8 

9 

10@pytest.fixture(scope='module', params=['fd', 'lcao', 'pw']) 

11def noprojs_gpw(module_tmp_path, request): 

12 mode = request.param 

13 atoms = bulk('Al') 

14 if mode in {'fd', 'lcao'}: 

15 kwargs = dict(mode=mode, gpts=(8, 8, 8)) 

16 else: 

17 kwargs = dict(mode={'name': 'pw', 'ecut': 200.0}) 

18 atoms.calc = GPAW(kpts=[2, 2, 2], txt=None, parallel=parallel, 

19 convergence={'density': 1e6, 'eigenstates': 1e6}, 

20 **kwargs) 

21 atoms.get_potential_energy() 

22 gpw_path = module_tmp_path / f'gs_noprojs_{mode}.gpw' 

23 atoms.calc.write(gpw_path, include_projections=False) 

24 return gpw_path 

25 

26 

27def test_no_save_projections(noprojs_gpw): 

28 calc = GPAW(noprojs_gpw, parallel=parallel) 

29 ibzwfs = list(calc.dft.ibzwfs) 

30 assert len(ibzwfs) > 0 

31 for wfs in ibzwfs: 

32 assert wfs._P_ani is None 

33 

34 

35def test_nice_error_message(noprojs_gpw): 

36 if 'lcao' in noprojs_gpw.name: 

37 pytest.skip('LCAO is not quite ready for this') 

38 # We want there to be a good error message when we do not have 

39 # projections. This only tests the most obvious case of .P_ani access, 

40 # but there could be code paths that will crash less controllably. 

41 calc = GPAW(noprojs_gpw, parallel=parallel) 

42 

43 wfs = next(iter(calc.dft.ibzwfs)) 

44 with pytest.raises(RuntimeError, match='There are no proj'): 

45 wfs.P_ani 

46 

47 

48def test_fixed_density_bandstructure(noprojs_gpw): 

49 calc = GPAW(noprojs_gpw, parallel=parallel) 

50 

51 fixed_calc = calc.fixed_density( 

52 parallel=parallel, 

53 kpts=[[0., 0., 0.], [0., 0., 0.5]], symmetry='off') 

54 

55 bs = fixed_calc.band_structure() 

56 assert len(bs.path.kpts) == 2 

57 ibzwfs = list(fixed_calc.dft.ibzwfs) 

58 

59 for wfs in ibzwfs: 

60 assert len(wfs.P_ani) == len(calc.get_atoms()) 

61 # Should we test something else here? 

62 # If we calculate a full bandstructure, it looks realistic. 

63 # We could compare to an "ordinary" (with projections) gpw file 

64 # to see that the numbers are in fact unaffected by the distinction.