-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update rotation computation #319
base: update-0.3.3
Are you sure you want to change the base?
Conversation
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.
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## update-0.3.2 #319 +/- ##
=============================================
Coverage 87.78% 87.78%
=============================================
Files 43 43
Lines 2938 2940 +2
Branches 341 341
=============================================
+ Hits 2579 2581 +2
Misses 335 335
Partials 24 24 ☔ View full report in Codecov by Sentry. |
Co-authored-by: Seung Hyun Kim <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor change.
Co-authored-by: Songyuan Cui <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern is that it will now pass all the "actual" instability cases. Wouldn't it be better to make this threshold to be adjustable? I think clipping is a little too extreme, and actually a wrong implementation. It can cause more ghost issues without showing user where the problem is coming from.
What do you mean by instability? This is clipping the trace of the directors product. If the directors are sufficiently orthonormal, then this is just a floating point issue for vector representation of small angles. This issue will only occur when neighboring directors are very close to each other (Tr(R) ~= 3 means no rotation), so I don't see it as stability issue per se. |
I mean instable as when position and directors start to explode and become not orthogonal and misaligned. Getting an error from this function was one of the early indicator, but now it will just pass with clipped trace. What we wanted is something like if trace < 3.0 + threshold:
theta = arccos(0.5 * trace - 0.5)
else:
raise Error which became the current form of implementation How about either
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the poetry.lock needs to be changed here, right @armantekinalp?
A few thoughts.
|
I agree with most of your point. 3), I checked some time ago that the threshold amount of error we are adding here is negligible compare to other numerical issues upstream (especially multiplying moduli to shear), but I think it is case dependent. I think we are somewhat on the same page: in most of the case when simulation is running properly, this corner case shouldn't be an issue. I am just having a perspective that the behavior of a code block, especially if it is a free-function, should only depend on its functionality. Basically, I think producing NaN is more expected behavior, especially when the input directors are some instable directors. In this regard, I agree with your changes, just we should clearly state and document this behavior inside the function, and maybe add warning message if the trace is significantly deviated outside (-1,3) bound (I think this will benefit many other cases to find numerical issues). If you agree, I can make a change and commit. I can also write some test for the cases, and address the threshold inside the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides resolving the major point raised by @skim0119, minor changes.
theta = arccos(0.5 * trace - 0.5 - 1e-10) | ||
# Clip the trace to between -1 and 3. | ||
# Any deviation beyond this is numerical error | ||
trace = min(trace, 3.0) | ||
trace = max(trace, -1.0) | ||
theta = arccos(0.5 * trace - 0.5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have some quantification for our reference regarding the performance penalty this change creates?
poetry.lock
Outdated
@@ -1,10 +1,9 @@ | |||
# This file is automatically @generated by Poetry and should not be changed by hand. | |||
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there shouldn't be any change in this file right as @Ali-7800 pointed out
@skim0119 and @armantekinalp what should be the follow up on this one? |
I was working on some simulations using relatively stiff rods and was having stability issues. I tracked it down to how the rotation is computed. Numerical error can cause the trace of the directors to be slightly larger than the max value of 3, which then yields a NaN when arccos is called.
The previous solution was to subtract 1e-10 to account for this occasional numerical error, but for a stiff rod, this adjustment was not always enough. Clipping the trace to -3 and 3, which corresponds to the allowable range of arccos yielded large improvements in stability with minimal performance penalty.