Skip to content

Commit 46b33ca

Browse files
nmnaughtonskim0119sy-cui
authored
Update rotation computation (#319)
* Update _rotations.py Make sure that arccos is never given a value outside of its allowable range. The previous version would occasionally yield a nan for stiff rods. Co-authored-by: Seung Hyun Kim <skim0119@gmail.com> Co-authored-by: Songyuan Cui <47127977+sy-cui@users.noreply.github.com>
1 parent 2ce3bde commit 46b33ca

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

elastica/_rotations.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,13 @@ def _inv_rotate(director_collection: NDArray[np.float64]) -> NDArray[np.float64]
153153
)
154154
)
155155

156-
# TODO HARDCODED bugfix has to be changed. Remove 1e-14 tolerance
157-
theta = arccos(0.5 * trace - 0.5 - 1e-10)
156+
# Clip the trace to between -1 and 3.
157+
# Any deviation beyond this is numerical error
158+
trace = min(trace, 3.0)
159+
trace = max(trace, -1.0)
160+
theta = arccos(0.5 * trace - 0.5)
158161

162+
# TODO HARDCODED bugfix has to be changed. Remove 1e-14 tolerance
159163
vector_collection[0, k] *= -0.5 * theta / sin(theta + 1e-14)
160164
vector_collection[1, k] *= -0.5 * theta / sin(theta + 1e-14)
161165
vector_collection[2, k] *= -0.5 * theta / sin(theta + 1e-14)

0 commit comments

Comments
 (0)