Coverage for gpaw/test/fileio/test_restart.py: 100%

41 statements  

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

1import pytest 

2import numpy as np 

3 

4from gpaw import GPAW, restart 

5 

6 

7def get_restart_test_values(calc): 

8 atoms = calc.get_atoms() 

9 # XXX the forces must be evaluated first because a force evaluation 

10 # somehow affects (changes) both the energy and the wf when restarting 

11 # from a gpw file. An issue was created (#1051) documenting this unexpected 

12 # and rather bizarre behavior. 

13 f = atoms.get_forces() 

14 e = atoms.get_potential_energy() 

15 m = atoms.get_magnetic_moments() 

16 wf = calc.get_pseudo_wave_function(band=1) 

17 eig0 = calc.get_eigenvalues(spin=0) 

18 eig1 = calc.get_eigenvalues(spin=1) 

19 

20 return wf, e, f, m, eig0, eig1 

21 

22 

23@pytest.fixture(params=['na3_pw_restart', 'na3_fd_restart']) 

24def gpwfile(request, gpw_files): 

25 return gpw_files[request.param] 

26 

27 

28@pytest.mark.old_gpaw_only 

29def test_fileio_restart(in_tmp_dir, gpwfile): 

30 # gpw restart file is written in fixture 

31 calc = GPAW(gpwfile) 

32 

33 wf0, e0, f0, m0, eig00, eig01 = get_restart_test_values(calc=calc) 

34 

35 # Write the restart file(s): 

36 calc.write('tmp1.gpw') 

37 calc.write('tmp.gpw', 'all') 

38 

39 # Try restarting: 

40 _, calc = restart('tmp.gpw', txt=None) 

41 wf1, e1, f1, m1, eig10, eig11 = get_restart_test_values(calc=calc) 

42 

43 # compare that the values are absolutely equal 

44 print(e0, e1) 

45 assert e0 == pytest.approx(e1, abs=1e-10) 

46 print(f0, f1) 

47 for ff0, ff1 in zip(f0, f1): 

48 err = np.linalg.norm(ff0 - ff1) 

49 assert err <= 1e-10 

50 print(m0, m1) 

51 for mm0, mm1 in zip(m0, m1): 

52 assert mm0 == pytest.approx(mm1, abs=1e-10) 

53 print("A", eig00, eig10) 

54 for eig0, eig1 in zip(eig00, eig10): 

55 assert eig0 == pytest.approx(eig1, abs=1e-10) 

56 print("B", eig01, eig11) 

57 for eig0, eig1 in zip(eig01, eig11): 

58 assert eig0 == pytest.approx(eig1, abs=1e-10) 

59 assert abs(wf1 - wf0).max() == pytest.approx(0, abs=1e-14) 

60 

61 # Check that after restart, everything is writable 

62 calc.write("tmp3.gpw") 

63 calc.write("tmp4.gpw", "all")