Coverage for gpaw/test/test_spectrum.py: 100%

19 statements  

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

1from math import exp, pi, sqrt 

2import numpy as np 

3 

4from gpaw.gauss import Gauss 

5import pytest 

6from gpaw.utilities.folder import Folder, Lorentz, Voigt # noqa 

7 

8# Gauss and Lorentz functions 

9 

10 

11def test_spectrum(): 

12 width = 0.5 

13 x = 1.5 

14 

15 assert Gauss(width).get(x) == pytest.approx( 

16 exp(- x**2 / 2 / width**2) / sqrt(2 * pi) / width, abs=1.e-15) 

17 assert Gauss(width).fwhm == pytest.approx( 

18 width * np.sqrt(8 * np.log(2)), abs=1.e-15) 

19 assert Lorentz(width).get(x) == pytest.approx( 

20 width / (x**2 + width**2) / pi, abs=1.e-15) 

21 assert Lorentz(width).fwhm == pytest.approx(width * 2, abs=1.e-15) 

22 

23 # folder function 

24 

25 for func in [Gauss, Lorentz, Voigt]: 

26 folder = Folder(width, func(width).__class__.__name__) 

27 

28 x = [0, 2] 

29 y = [[2, 0, 1], [1, 1, 1]] 

30 

31 xl, yl = folder.fold(x, y, dx=.7) 

32 

33 # check first value 

34 yy = np.dot(np.array(y)[:, 0], func(width).get(xl[0] - np.array(x))) 

35 assert yl[0, 0] == pytest.approx(yy, abs=1.e-15)