From c4490f8ab596533dab28fe051186a6e28c4e1761 Mon Sep 17 00:00:00 2001 From: Guenther Obermair Date: Fri, 8 Nov 2024 19:01:16 +0100 Subject: [PATCH] Add: catch division by zero --- splinepy/helpme/create.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/splinepy/helpme/create.py b/splinepy/helpme/create.py index d5f531af4..40c9903a7 100644 --- a/splinepy/helpme/create.py +++ b/splinepy/helpme/create.py @@ -442,6 +442,12 @@ def swept( # projecting B_(i) onto the plane normal to e1 B.append(B[i] - _np.dot(B[i], e1) * e1) + if _np.linalg.norm(B[i + 1]) < _settings.TOLERANCE: + _log.warning( + f"Vector B[{i + 1}] is close to zero. Adjusting " + f"to avoid division by zero." + ) + B[i + 1] += _settings.TOLERANCE B[i + 1] /= _np.linalg.norm(B[i + 1]) # defining e2 and e3 vectors @@ -478,6 +484,12 @@ def swept( B_rec[i] - _np.dot(B_rec[i], tang_collection[i]) * tang_collection[i] ) + if _np.linalg.norm(B_rec[i + 1]) < _settings.TOLERANCE: + _log.warning( + f"Vector B_rec[{i + 1}] is close to zero. Adjusting " + f"to avoid division by zero." + ) + B_rec[i + 1] += _settings.TOLERANCE B_rec[i + 1] /= _np.linalg.norm(B_rec[i + 1]) # middle point between B and B_rec B_rec[i + 1] = (B[i + 1] + B_rec[i + 1]) * 0.5