test_sph_harm.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import numpy as np
  2. from numpy.testing import assert_allclose
  3. import scipy.special as sc
  4. def test_first_harmonics():
  5. # Test against explicit representations of the first four
  6. # spherical harmonics which use `theta` as the azimuthal angle,
  7. # `phi` as the polar angle, and include the Condon-Shortley
  8. # phase.
  9. # Notation is Ymn
  10. def Y00(theta, phi):
  11. return 0.5*np.sqrt(1/np.pi)
  12. def Yn11(theta, phi):
  13. return 0.5*np.sqrt(3/(2*np.pi))*np.exp(-1j*theta)*np.sin(phi)
  14. def Y01(theta, phi):
  15. return 0.5*np.sqrt(3/np.pi)*np.cos(phi)
  16. def Y11(theta, phi):
  17. return -0.5*np.sqrt(3/(2*np.pi))*np.exp(1j*theta)*np.sin(phi)
  18. harms = [Y00, Yn11, Y01, Y11]
  19. m = [0, -1, 0, 1]
  20. n = [0, 1, 1, 1]
  21. theta = np.linspace(0, 2*np.pi)
  22. phi = np.linspace(0, np.pi)
  23. theta, phi = np.meshgrid(theta, phi)
  24. for harm, m, n in zip(harms, m, n):
  25. assert_allclose(sc.sph_harm(m, n, theta, phi),
  26. harm(theta, phi),
  27. rtol=1e-15, atol=1e-15,
  28. err_msg="Y^{}_{} incorrect".format(m, n))