Coverage for gpaw/test/test_spherical_harmonics.py: 100%
49 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
1from math import pi
3import numpy as np
4import pytest
5from gpaw.spherical_harmonics import (YL, Y, Yl, gam, print_YL_table_code,
6 write_c_code)
9def yLL(L1, L2):
10 s = 0.0
11 for c1, n1 in YL[L1]:
12 for c2, n2 in YL[L2]:
13 s += c1 * c2 * gam(n1[0] + n2[0], n1[1] + n2[1], n1[2] + n2[2])
14 return s
17def test_yy():
18 Lmax = len(YL)
19 for L1 in range(Lmax):
20 for L2 in range(Lmax):
21 r = 0.0
22 if L1 == L2:
23 r = 1.0
24 assert yLL(L1, L2) == pytest.approx(r, abs=1e-14)
27def test_y_c_code():
28 R = np.zeros(3)
29 Y = np.zeros(1)
30 Yl(0, R, Y)
31 assert Y[0] == pytest.approx((4 * pi)**-0.5)
32 Y = np.zeros(2 * 8 + 1)
33 with pytest.raises(RuntimeError):
34 Yl(8, R, Y)
37def test_y_c_code2():
38 R_c = np.array([0.1, -0.2, 0.3])
39 for l in range(8):
40 print('l', l)
42 # Using C implementation
43 rlY1_m = np.zeros(2 * l + 1) + np.nan
44 Yl(l, R_c, rlY1_m)
45 print(rlY1_m)
47 # Using Python implementation
48 rlY2_m = []
49 for m in range(-l, l + 1):
50 L = l**2 + l + m
51 rlY2_m.append(Y(L, *R_c))
52 rlY2_m = np.array(rlY2_m)
53 print(rlY2_m)
55 assert np.allclose(rlY2_m, rlY1_m)
58def test_write_c_code(capsys):
59 write_c_code(1)
60 s = capsys.readouterr().out
61 s = s.replace(' ', '')
62 s = s.replace('\n', '')
63 assert s == (
64 'elseif(l==1)' +
65 '{Y_m[0]=0.4886025119029199*y;' +
66 'Y_m[1]=0.4886025119029199*z;' +
67 'Y_m[2]=0.4886025119029199*x;}')
70def test_print_YL_table_code():
71 pytest.importorskip('sympy')
72 print_YL_table_code()