Skip to content

Commit 456b9e3

Browse files
committed
Prevent divide-by-zero error by raising ZeroDivisionError when moment of inertia is zero instead of proceeding with unweighted torque calculation.
1 parent efc4264 commit 456b9e3

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

CodeEntropy/GeometricFunctions.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,19 +356,26 @@ def get_weighted_torques(data_container, bead, rot_axes, force_partitioning=0.5)
356356
weighted_torque[dimension] = 0
357357
continue
358358

359+
# Check for zero moment of inertia
359360
if np.isclose(moment_of_inertia[dimension, dimension], 0):
360-
weighted_torque[dimension] = torques[dimension]
361-
else:
362-
if moment_of_inertia[dimension, dimension] < 0:
363-
raise ValueError(
364-
f"Negative value encountered for moment of inertia: "
365-
f"{moment_of_inertia[dimension, dimension]} "
366-
f"Cannot compute weighted torque."
367-
)
368-
weighted_torque[dimension] = torques[dimension] / np.sqrt(
369-
moment_of_inertia[dimension, dimension]
361+
raise ZeroDivisionError(
362+
f"Attempted to divide by zero moment of inertia in dimension "
363+
f"{dimension}."
364+
)
365+
366+
# Check for negative moment of inertia
367+
if moment_of_inertia[dimension, dimension] < 0:
368+
raise ValueError(
369+
f"Negative value encountered for moment of inertia: "
370+
f"{moment_of_inertia[dimension, dimension]} "
371+
f"Cannot compute weighted torque."
370372
)
371373

374+
# Compute weighted torque
375+
weighted_torque[dimension] = torques[dimension] / np.sqrt(
376+
moment_of_inertia[dimension, dimension]
377+
)
378+
372379
return weighted_torque
373380

374381

0 commit comments

Comments
 (0)