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

20 statements  

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

1import numpy as np 

2import pytest 

3 

4from gpaw.mpi import world 

5from gpaw.nlopt.basic import NLOData 

6from gpaw.nlopt.shg import get_shg 

7 

8 

9def test_shg_spinpol(mme_files, in_tmp_dir): 

10 shg_values = np.array([-0.77053399 - 0.37041593j, 

11 -0.87903174 - 0.4177294j, 

12 -1.00791251 - 0.51051291j, 

13 -1.15465962 - 0.66642326j, 

14 -1.30812094 - 0.92114822j, 

15 -1.42138133 - 1.33424513j, 

16 -1.34649601 - 1.96084827j, 

17 -0.78891819 - 2.66240386j, 

18 0.27801137 - 2.8572836j, 

19 1.12315952 - 2.30446868j, 

20 1.38569995 - 1.59698796j]) 

21 

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

23 shg = {} 

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 SHG spectra 

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

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

33 world.barrier() 

34 

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

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

37 assert shg[spinpol] == pytest.approx(shg_values, abs=1e-3), \ 

38 np.max(np.abs(shg[spinpol] - shg_values)) 

39 

40 # Assert that the difference between spectra from spinpaired and 

41 # spinpolarised calculations is small 

42 

43 # Absolute error 

44 assert shg['spinpol'].real == pytest.approx( 

45 shg['spinpaired'].real, abs=5e-4) 

46 assert shg['spinpol'].imag == pytest.approx( 

47 shg['spinpaired'].imag, abs=5e-4) 

48 

49 # Relative error 

50 assert shg['spinpol'].real == pytest.approx( 

51 shg['spinpaired'].real, rel=2e-3) 

52 assert shg['spinpol'].imag == pytest.approx( 

53 shg['spinpaired'].imag, rel=2e-3)