Coverage for gpaw/test/nlopt/test_shift.py: 100%

19 statements  

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

1import pytest 

2import numpy as np 

3 

4from gpaw.mpi import world 

5from gpaw.nlopt.basic import NLOData 

6from gpaw.nlopt.shift import get_shift 

7 

8 

9def test_shift_spinpol(mme_files, in_tmp_dir): 

10 shift_values = np.array([98.0118706 + 0.j, 

11 98.89282526 + 0.j, 

12 99.78333556 + 0.j, 

13 100.68356521 + 0.j, 

14 101.59368155 + 0.j, 

15 102.51385566 + 0.j, 

16 103.44426247 + 0.j, 

17 104.38508086 + 0.j, 

18 105.33649376 + 0.j, 

19 106.29868833 + 0.j, 

20 107.271856 + 0.j]) 

21 

22 freqs = np.linspace(2.3, 2.4, 11) 

23 shift = {} 

24 for spinpol in ['spinpaired', 'spinpol']: 

25 tag = '_spinpol' if spinpol == 'spinpol' else '' 

26 

27 # Get pre-calculated nlodata from SiC fixtures 

28 nlodata = NLOData.load(mme_files[f'sic_pw{tag}'], comm=world) 

29 

30 # Calculate 'xyz' tensor element of shift spectra 

31 get_shift(nlodata, freqs=freqs, eta=0.025, pol='xyz', 

32 out_name=f'shift_xyz{tag}.npy') 

33 world.barrier() 

34 

35 # Load the calculated SHG spectra (in units of nm/V) 

36 shift[spinpol] = np.load(f'shift_xyz{tag}.npy')[1] * 1e9 

37 assert shift[spinpol] == pytest.approx(shift_values, abs=5e-2) 

38 

39 # Assert that the difference between spectra from spinpaired and 

40 # spinpolarised calculations is small 

41 

42 # Absolute error 

43 assert shift['spinpol'].real == pytest.approx( 

44 shift['spinpaired'].real, abs=2e-2) 

45 

46 # Relative error 

47 assert shift['spinpol'].real == pytest.approx( 

48 shift['spinpaired'].real, rel=1e-3) 

49 

50 # Imaginary value should've been removed 

51 assert shift['spinpol'].imag == pytest.approx( 

52 shift['spinpaired'].imag, abs=1e-10)