Coverage for gpaw/test/directopt/test_mom_directopt_pw_hybrids.py: 77%

44 statements  

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

1import pytest 

2 

3import numpy as np 

4 

5 

6from gpaw import GPAW, PW 

7from gpaw.mom import prepare_mom_calculation 

8 

9 

10@pytest.mark.do 

11def test_mom_directopt_pw_hybrids(in_tmp_dir, gpw_files): 

12 calc = GPAW(gpw_files['h2_mom_do_pwh']) 

13 h2 = calc.atoms 

14 h2.calc = calc 

15 e = h2.get_potential_energy() 

16 

17 # Total and orbital energies calculated using 

18 # RMMDIIS with disabled code below 

19 e_ref = -6.985891 

20 eig_ref = [-11.77015, 1.18932] 

21 f_ref = [[-0.34178, 0.0, 0.0], [0.34178, 0.0, 0.0]] 

22 e_ref_es = 20.697867 

23 eig_ref_es = [-16.46296, -3.35601] 

24 f_ref_es = [[-34.90936, 0.0, 0.0], [34.90936, 0.0, 0.0]] 

25 

26 reference_calc = False 

27 if reference_calc: 

28 calc = GPAW(mode=PW(300), 

29 # h=0.3, 

30 xc={'name': 'HSE06', 'backend': 'pw'}, 

31 symmetry='off', 

32 nbands=2, 

33 convergence={'eigenstates': 4.0e-6, 

34 'bands': 'all'}) 

35 h2.calc = calc 

36 h2.get_potential_energy() 

37 h2.get_forces() 

38 calc.get_eigenvalues() 

39 

40 f_sn = [[0, 1]] 

41 prepare_mom_calculation(calc, h2, f_sn) 

42 

43 h2.get_potential_energy() 

44 h2.get_forces() 

45 calc.get_eigenvalues() 

46 

47 eig = calc.get_eigenvalues() 

48 assert e == pytest.approx(e_ref, abs=1.0e-3) 

49 assert eig == pytest.approx(eig_ref, abs=0.1) 

50 if calc.old: 

51 f = calc.get_forces() 

52 assert f == pytest.approx(np.array(f_ref), abs=1.0e-2) 

53 

54 calc.set(eigensolver={'name': 'etdm-fdpw', 

55 'excited_state': True, 

56 'converge_unocc': True}) 

57 f_sn = [[0, 1]] 

58 prepare_mom_calculation(calc, h2, f_sn) 

59 

60 e_es = h2.get_potential_energy() 

61 eig_es = calc.get_eigenvalues() 

62 assert e_es == pytest.approx(e_ref_es, abs=1.0e-3) 

63 assert eig_es == pytest.approx(eig_ref_es, abs=0.1) 

64 if calc.old: 

65 f_es = calc.get_forces() 

66 assert f_es == pytest.approx(np.array(f_ref_es), abs=1.0e-2)