Skip to content

Commit 09cf4d9

Browse files
committed
fix grid_lebedev() due to quadpy API changes
1 parent bff5737 commit 09cf4d9

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

micarray/modal/angular.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .. import util
55
from warnings import warn
66
try:
7-
import quadpy # only for grid_lebedev()
7+
import quadpy.u3._lebedev as _lebedev # only for grid_lebedev()
88
except ImportError:
99
pass
1010

@@ -34,6 +34,8 @@ def sht_matrix(N, azi, colat, weights=None):
3434
(Note: :math:`\mathbf{Y}` is interpreted as the inverse transform (or synthesis)
3535
matrix in examples and documentation.)
3636
37+
cf. eq. (1.9), (3.29) :cite:`Rafaely2019_book`
38+
3739
Parameters
3840
----------
3941
N : int
@@ -265,13 +267,30 @@ def available_quadrature(d):
265267
return matches[0]
266268

267269
if n > 65:
270+
print(n)
268271
raise ValueError("Maximum available Lebedev grid order is 65. "
269272
"(requested: {})".format(n))
270273

271274
# this needs https://pypi.python.org/pypi/quadpy
272-
q = quadpy.sphere.Lebedev(degree=available_quadrature(2*n))
275+
# currently validated with 0.16.5
276+
degree = _lebedev.lebedev_003a() # tmp call to mute pep8 warning
277+
degree = available_quadrature(2 * n)
278+
# the nice API handling currently returns only up to lebedev_047:
279+
# q = quadpy.u3.get_good_scheme(degree=degree)
280+
# thus we call the appropriate lebedev function directly in low level,
281+
# not nice, but then we are save until the quadpy API will be consistent:
282+
if degree == 3:
283+
fcn = '_lebedev.lebedev_00' + str(degree) + 'a()'
284+
elif degree < 10:
285+
fcn = '_lebedev.lebedev_00'+str(degree) + '()'
286+
elif degree < 100:
287+
fcn = '_lebedev.lebedev_0' + str(degree) + '()'
288+
else:
289+
fcn = '_lebedev.lebedev_' + str(degree) + '()'
290+
q = eval(fcn)
273291
if np.any(q.weights < 0):
274292
warn("Lebedev grid of order {} has negative weights.".format(n))
275-
azi = q.azimuthal_polar[:, 0]
276-
colat = q.azimuthal_polar[:, 1]
293+
azi, colat, r = util.cart2sph(q.points[0, :],
294+
q.points[1, :],
295+
q.points[2, :])
277296
return azi, colat, 4*np.pi*q.weights

micarray/util.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,79 @@ def db(x, power=False):
107107
"""
108108
with np.errstate(divide='ignore'):
109109
return 10 if power else 20 * np.log10(np.abs(x))
110+
111+
112+
def sph2cart(alpha, beta, r):
113+
r"""Spherical to cartesian coordinate transform.
114+
115+
taken from https://github.com/sfstoolbox/sfs-python commit: efdda38
116+
117+
.. math::
118+
119+
x = r \cos \alpha \sin \beta \\
120+
y = r \sin \alpha \sin \beta \\
121+
z = r \cos \beta
122+
123+
with :math:`\alpha \in [0, 2\pi), \beta \in [0, \pi], r \geq 0`
124+
125+
Parameters
126+
----------
127+
alpha : float or array_like
128+
Azimuth angle in radiants
129+
beta : float or array_like
130+
Colatitude angle in radiants (with 0 denoting North pole)
131+
r : float or array_like
132+
Radius
133+
134+
Returns
135+
-------
136+
x : float or `numpy.ndarray`
137+
x-component of Cartesian coordinates
138+
y : float or `numpy.ndarray`
139+
y-component of Cartesian coordinates
140+
z : float or `numpy.ndarray`
141+
z-component of Cartesian coordinates
142+
143+
"""
144+
x = r * np.cos(alpha) * np.sin(beta)
145+
y = r * np.sin(alpha) * np.sin(beta)
146+
z = r * np.cos(beta)
147+
return x, y, z
148+
149+
150+
def cart2sph(x, y, z):
151+
r"""Cartesian to spherical coordinate transform.
152+
153+
taken from https://github.com/sfstoolbox/sfs-python commit: efdda38
154+
155+
.. math::
156+
157+
\alpha = \arctan \left( \frac{y}{x} \right) \\
158+
\beta = \arccos \left( \frac{z}{r} \right) \\
159+
r = \sqrt{x^2 + y^2 + z^2}
160+
161+
with :math:`\alpha \in [-pi, pi], \beta \in [0, \pi], r \geq 0`
162+
163+
Parameters
164+
----------
165+
x : float or array_like
166+
x-component of Cartesian coordinates
167+
y : float or array_like
168+
y-component of Cartesian coordinates
169+
z : float or array_like
170+
z-component of Cartesian coordinates
171+
172+
Returns
173+
-------
174+
alpha : float or `numpy.ndarray`
175+
Azimuth angle in radiants
176+
beta : float or `numpy.ndarray`
177+
Colatitude angle in radiants (with 0 denoting North pole)
178+
r : float or `numpy.ndarray`
179+
Radius
180+
181+
"""
182+
r = np.sqrt(x**2 + y**2 + z**2)
183+
alpha = np.arctan2(y, x)
184+
beta = np.arccos(z / r)
185+
return alpha, beta, r

0 commit comments

Comments
 (0)