Skip to content

Commit 41c57c9

Browse files
authored
Merge pull request #291 from marco-2023/update_scipy
Fix spherical harmonics for new SciPy sph_harm_y API
2 parents 081c106 + dec6512 commit 41c57c9

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

.github/workflows/pytest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
matrix:
1919
# os: ["ubuntu-latest", "windows-latest", "macos-latest"]
2020
os: ["ubuntu-latest", "windows-latest"]
21-
py: ["3.9", "3.10", "3.11", "3.12", "3.13"]
21+
py: ["3.10", "3.11", "3.12", "3.13"]
2222

2323
steps:
2424
- uses: "actions/checkout@v6"

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ classifiers = [
2121
'Intended Audience :: Science/Research',
2222
"Intended Audience :: Education",
2323
"Natural Language :: English",
24-
"Programming Language :: Python :: 3.9",
2524
"Programming Language :: Python :: 3.10",
2625
"Programming Language :: Python :: 3.11",
2726
"Programming Language :: Python :: 3.12",
2827
"Programming Language :: Python :: 3.13",
2928
]
3029
dependencies = [
31-
"numpy>=1.16",
30+
"numpy>=1.23.5",
3231
"pytest>=8.0.0",
33-
"scipy>=1.4",
32+
"scipy>=1.15.0",
3433
"importlib_resources",
3534
"sympy",
3635
]

src/grid/tests/test_atomgrid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
assert_raises,
3030
)
3131
from scipy.spatial.transform import Rotation as R
32+
from scipy.integrate import trapezoid
3233

3334
from grid.angular import LEBEDEV_DEGREES, AngularGrid
3435
from grid.atomgrid import AtomGrid, _get_rgrid_size
@@ -688,7 +689,7 @@ def func(sph_points):
688689

689690
# Test the integral of spherical average is the integral of Gaussian e^(-x^2)e^(-y^2)...
690691
# from -infinity to infinity which is equal to pi^(3/2)
691-
integral = 4.0 * np.pi * np.trapz(y=spherical_avg(oned_grid) * oned_grid**2.0, x=oned_grid)
692+
integral = 4.0 * np.pi * trapezoid(y=spherical_avg(oned_grid) * oned_grid**2.0, x=oned_grid)
692693
actual_integral = np.sqrt(np.pi) ** 3.0
693694
assert_allclose(actual_integral, integral)
694695

src/grid/utils.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"""Utility Module."""
2121

2222
import numpy as np
23-
from scipy.special import sph_harm
23+
from scipy.special import sph_harm_y
2424

2525
_bragg = np.array(
2626
[
@@ -560,21 +560,23 @@ def generate_real_spherical_harmonics_scipy(l_max: int, theta: np.ndarray, phi:
560560
l_list = np.arange(l_max + 1)
561561
for l_val in l_list:
562562
# generate m=0 real spherical harmonic
563-
zero_real_sph = sph_harm(0, l_val, theta, phi).real
563+
zero_real_sph = sph_harm_y(l_val, 0, phi, theta).real
564564

565-
# generate order m=positive real spherical harmonic
566-
m_list_p = np.arange(1, l_val + 1, dtype=float)
565+
# generate order m=positive real spherical harmonic: sqrt(2) * (-1)^m * Re(Y_l^m)
566+
m_list_p = np.arange(1, l_val + 1, dtype=int)
567567
pos_real_sph = (
568-
sph_harm(m_list_p[:, None], l_val, theta, phi).real
569-
* np.sqrt(2)
570-
* (-1) ** m_list_p[:, None] # Remove Conway phase from SciPy
568+
np.sqrt(2)
569+
* (-1) ** m_list_p[:, None]
570+
* sph_harm_y(l_val, m_list_p[:, None], phi, theta).real
571+
# Remove Conway phase from SciPy
571572
)
572-
# generate order m=negative real spherical harmonic
573-
m_list_n = np.arange(-1, -l_val - 1, -1, dtype=float)
573+
# generate order m=negative real spherical harmonic: sqrt(2) * (-1)^m * Im(Y_l^m)
574574
neg_real_sph = (
575-
sph_harm(m_list_p[:, None], l_val, theta, phi).imag
576-
* np.sqrt(2)
577-
* (-1) ** m_list_n[:, None] # Remove Conway phase from SciPy
575+
np.sqrt(2)
576+
* (-1) ** m_list_p[:, None]
577+
* sph_harm_y(
578+
l_val, m_list_p[:, None], phi, theta
579+
).imag # Remove Conway phase from SciPy
578580
)
579581

580582
# Convert to horton 2 order
@@ -753,7 +755,8 @@ def generate_derivative_real_spherical_harmonics(l_max: int, theta: np.ndarray,
753755
sph_harm_vals = generate_real_spherical_harmonics(l_max, theta, phi)
754756
i_output = 0
755757
for l_val in l_list:
756-
for m in [0, *sum([[x, -x] for x in range(1, l_val + 1)], [])]:
758+
m_values = [0] + [m for x in range(1, l_val + 1) for m in (x, -x)]
759+
for m in m_values:
757760
# Take all spherical harmonics at degree l_val
758761
sph_harm_degree = sph_harm_vals[(l_val) ** 2 : (l_val + 1) ** 2, :]
759762

@@ -777,7 +780,7 @@ def index_m(m):
777780
# Compute it using SciPy, removing conway phase (-1)^m and multiply by 2^0.5.
778781
sph_harm_m = (
779782
fac
780-
* sph_harm(np.abs(float(m)) + 1, l_val, theta, phi)
783+
* sph_harm_y(l_val, np.abs(int(m)) + 1, phi, theta)
781784
* np.sqrt(2)
782785
* (-1.0) ** float(m)
783786
)

0 commit comments

Comments
 (0)