Coverage for gpaw/test/pw/test_lfc.py: 100%

48 statements  

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

1import numpy as np 

2import pytest 

3 

4import gpaw.mpi as mpi 

5from gpaw.grid_descriptor import GridDescriptor 

6from gpaw.kpt_descriptor import KPointDescriptor 

7from gpaw.lfc import LocalizedFunctionsCollection as LFC 

8from gpaw.pw.descriptor import PWDescriptor 

9from gpaw.pw.lfc import PWLFC 

10from gpaw.spline import Spline 

11 

12 

13@pytest.mark.ci 

14def test_pw_lfc(): 

15 x = 2.0 

16 rc = 3.5 

17 r = np.linspace(0, rc, 100) 

18 

19 n = 40 

20 a = 8.0 

21 gd = GridDescriptor((n, n, n), (a, a, a), comm=mpi.serial_comm) 

22 

23 kpts = np.array([(0.25, 0.25, 0.0)]) 

24 kd = KPointDescriptor(kpts) 

25 spos_ac = np.array([(0.15, 0.5, 0.95)]) 

26 

27 pd = PWDescriptor(45, gd, complex, kd) 

28 

29 eikr = np.ascontiguousarray( 

30 np.exp(2j * np.pi * np.dot(np.indices(gd.N_c).T, 

31 (kpts / gd.N_c).T).T)[0]) 

32 

33 for l in range(3): 

34 print(l) 

35 s = Spline.from_data(l, rc, 2 * x**1.5 / np.pi * np.exp(-x * r**2)) 

36 

37 lfc1 = LFC(gd, [[s]], kd, dtype=complex) 

38 lfc2 = PWLFC([[s]], pd) 

39 

40 c_axi = {0: np.zeros((1, 2 * l + 1), complex)} 

41 c_axi[0][0, 0] = 1.9 - 4.5j 

42 c_axiv = {0: np.zeros((1, 2 * l + 1, 3), complex)} 

43 

44 b1 = gd.zeros(1, dtype=complex) 

45 b2 = pd.zeros(1, dtype=complex) 

46 

47 for lfc, b in [(lfc1, b1), (lfc2, b2)]: 

48 lfc.set_positions(spos_ac) 

49 lfc.add(b, c_axi, 0) 

50 

51 b2 = pd.ifft(b2[0]) * eikr 

52 assert abs(b2 - b1[0]).max() == pytest.approx(0, abs=0.001) 

53 

54 b1 = eikr[None] 

55 b2 = pd.fft(b1[0] * 0 + 1).reshape((1, -1)) 

56 

57 results = [] 

58 results2 = [] 

59 for lfc, b in [(lfc1, b1), (lfc2, b2)]: 

60 lfc.integrate(b, c_axi, 0) 

61 results.append(c_axi[0][0].copy()) 

62 lfc.derivative(b, c_axiv, 0) 

63 results2.append(c_axiv[0][0].copy()) 

64 assert abs(np.ptp(results2, 0)).max() == pytest.approx(0, abs=1e-7) 

65 assert abs(np.ptp(results, 0)).max() == pytest.approx(0, abs=3e-8)