Coverage for gpaw/test/mom/test_mom_fd_energy.py: 100%

52 statements  

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

1import numpy as np 

2import pytest 

3from ase.build import molecule 

4from gpaw import GPAW, restart 

5from gpaw.mom import prepare_mom_calculation 

6 

7 

8@pytest.mark.mom 

9def test_mom_fd_spinpol(in_tmp_dir): 

10 dE_ref = [7.8009908153, 7.5234341583] 

11 

12 atoms = molecule('HCl') 

13 atoms.center(vacuum=2) 

14 

15 calc = GPAW(mode='fd', 

16 nbands=6, 

17 h=0.24, 

18 xc='PBE', 

19 spinpol=True, 

20 convergence={'energy': 100, 

21 'density': 1e-4, 

22 'eigenstates': 100, 

23 'bands': -1}) 

24 

25 atoms.calc = calc 

26 # Ground-state calculation 

27 E_gs = atoms.get_potential_energy() 

28 

29 calc.write('hcl_fd_gs.gpw', 'all') 

30 

31 # Test match orbitals directly 

32 for mom in [False, True]: 

33 # Test spin polarized excited-state calculations 

34 for s in [0, 1]: 

35 atoms, calc = restart('hcl_fd_gs.gpw', txt='-') 

36 

37 f_sn = [] 

38 for spin in range(calc.get_number_of_spins()): 

39 f_n = calc.get_occupation_numbers(spin=spin) 

40 f_sn.append(f_n) 

41 f_sn[0][3] -= 1. 

42 f_sn[s][4] += 1. 

43 

44 occ = prepare_mom_calculation(calc, atoms, f_sn, 

45 use_projections=mom) 

46 

47 E_es = atoms.get_potential_energy() 

48 

49 # Test overlaps 

50 occ.initialize_reference_orbitals() 

51 for kpt in calc.wfs.kpt_u: 

52 f_sn = calc.get_occupation_numbers(spin=kpt.s) 

53 P = occ.calculate_weights(kpt, 1.0) 

54 assert (np.allclose(P, f_sn)) 

55 

56 dE = E_es - E_gs 

57 print(s, dE) 

58 assert dE == pytest.approx(dE_ref[s], abs=0.015) 

59 

60 

61@pytest.mark.mom 

62def test_mom_fd_spinpair(in_tmp_dir): 

63 dE_ref = 8.4695551944 

64 

65 atoms = molecule('HCl') 

66 atoms.center(vacuum=2) 

67 

68 calc = GPAW(mode='fd', 

69 nbands=6, 

70 h=0.24, 

71 xc='PBE', 

72 convergence={'energy': 100, 

73 'density': 1e-3, 

74 'eigenstates': 100, 

75 'bands': -1}) 

76 

77 atoms.calc = calc 

78 # Ground-state calculation 

79 E_gs = atoms.get_potential_energy() 

80 calc.write('hcl_fd_gs_spin-pair.gpw', 'all') 

81 

82 # Test match orbitals directly 

83 for mom in [False, True]: 

84 atoms, calc = restart('hcl_fd_gs_spin-pair.gpw', txt='-') 

85 

86 # Test spin paired excited-state calculation 

87 f_n = [calc.get_occupation_numbers(spin=0) / 2.] 

88 f_n[0][3] -= 0.5 

89 f_n[0][4] += 0.5 

90 

91 prepare_mom_calculation(calc, atoms, f_n, 

92 use_projections=mom) 

93 E_es = atoms.get_potential_energy() 

94 

95 dE = E_es - E_gs 

96 print(dE) 

97 assert dE == pytest.approx(dE_ref, abs=0.01)