Skip to content

Commit 8cabef5

Browse files
committed
minor update of epsilon on radii calculations
1 parent 1a5a6f8 commit 8cabef5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/sisl/_core/geometry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,12 @@ def angle(
17761776
xi -= _a.asarrayd(ref)[None, :]
17771777
nx = sqrt(square(xi).sum(1))
17781778
ang = np.zeros_like(nx)
1779-
idx = (nx > 1e-6).nonzero()[0]
1779+
# Typically the angles then becomes a precision of the length
1780+
# precision.
1781+
# So calculating angles, and re-calculating coordinates results
1782+
# in differences on the scale of this `eps` number.
1783+
# Hence, we just choose it around numerical accuracy.
1784+
idx = (nx > 1e-16).nonzero()[0]
17801785
ang[idx] = np.arccos(xi[idx] @ dir / nx[idx])
17811786
if rad:
17821787
return ang

src/sisl/utils/mathematics.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
199199
If `cos_phi` is ``True`` this is :math:`\cos(\phi)`, otherwise
200200
:math:`\phi` is returned (the polar angle from the :math:`z` axis)
201201
"""
202+
eps = 1e-16
202203
r = _a.asarray(r)
203204
if maxR is None:
204205
rr = sqrt(square(r).sum(-1))
@@ -207,7 +208,12 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
207208
else:
208209
theta = None
209210
phi = zeros_like(rr)
210-
idx = rr != 0.0
211+
# Typically the angles then becomes a precision of the length
212+
# precision.
213+
# So calculating angles, and re-calculating coordinates results
214+
# in differences on the scale of this `eps` number.
215+
# Hence, we just choose it around numerical accuracy.
216+
idx = rr > eps
211217
divide(r[..., 2], rr, out=phi, where=idx)
212218
if not cos_phi:
213219
arccos(phi, out=phi, where=idx)
@@ -228,7 +234,7 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
228234
theta = None
229235

230236
phi = zeros_like(rr)
231-
idx0 = rr != 0.0
237+
idx0 = rr > eps
232238
divide(r[..., 2], rr, out=phi, where=idx0)
233239
if not cos_phi:
234240
arccos(phi, out=phi, where=idx0)

0 commit comments

Comments
 (0)