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

31 statements  

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

1import sys 

2from pathlib import Path 

3 

4import pytest 

5 

6from gpaw import GPAW, PW 

7 

8 

9@pytest.mark.old_gpaw_only 

10@pytest.mark.ci 

11def test_new_calculator(in_tmp_dir): 

12 """Test the GPAW.new() method.""" 

13 

14 params = dict( 

15 mode=PW(200), 

16 xc='LDA', 

17 nbands=8, 

18 kpts={'size': (4, 4, 4), 'gamma': True}) 

19 

20 modification_m = [ 

21 dict(mode='fd'), 

22 dict(xc='PBE'), 

23 dict(nbands=10), 

24 dict(kpts={'size': (4, 4, 4)}), 

25 dict(kpts={'size': (3, 3, 3)}, xc='PBE')] 

26 

27 calc0 = GPAW(**params, txt='calc0.txt') 

28 

29 for m, modification in enumerate(modification_m): 

30 if m == 0: 

31 # Don't give a new txt file 

32 calc = calc0.new(**modification) 

33 check_file_handles(calc0, calc) 

34 else: 

35 txt = f'calc{m}.txt' 

36 calc = calc0.new(**modification, txt=txt) 

37 check_file_handles(calc0, calc, txt=txt) 

38 

39 check_calc(calc, params, modification, world=calc.world) 

40 

41 

42def check_file_handles(calc0, calc, txt=None): 

43 assert calc.log.world.rank == calc0.log.world.rank 

44 

45 if calc.log.world.rank == 0: 

46 # We never want to reuse the output file 

47 assert calc.log._fd is not calc0.log._fd 

48 

49 if txt is None: 

50 # When no txt is specified, the new calculator should log its 

51 # output in stdout 

52 assert calc.log._fd is sys.stdout 

53 else: 

54 # Check that the new calculator log file handle was updated 

55 # appropriately 

56 assert Path(calc.log._fd.name).name == txt 

57 

58 

59def check_calc(calc, params, modification, *, world): 

60 desired_params = params.copy() 

61 desired_params.update(modification) 

62 

63 for param, value in desired_params.items(): 

64 assert calc.parameters[param] == value 

65 

66 # Check that the communicator is reused 

67 assert calc.world is world