Repository navigation
Fix: Incorrect capped_distance in some triclinic box situations - #5481
Conversation
capped_distance in some triclinic box situationscapped_distance in some triclinic box situations
Documentation build overview
17 files changed ·
|
|
Seems one of the old snapshots is failing but I'm pretty sure they are just incorrectly snapshotted values, looking into it |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #5481 +/- ##
========================================
Coverage 93.91% 93.91%
========================================
Files 184 184
Lines 22644 22644
Branches 3219 3219
========================================
+ Hits 21266 21267 +1
Misses 917 917
+ Partials 461 460 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This looks great, unfortunately I won't have time to review. I'm going to un-request my review here and pick on someone else ;) |
|
@IAlibay can we get this fix into the next release? It looks important enough as it can affect scientific correctness across many different quantities. |
|
(Yes, I get that it needs to be reviewed...) |
Yes, as long as it can get merged by Sunday. |
|
At first glance this looks very good, but I do need a bit more time to dig into it. |
If "by Sunday" isn't possible - could you propose an alternative deadline? I do agree that this one probably falls under "critical fixes", so we could delay the release a tiny bit. |
|
I'll try to keep the "by Sunday" deadline for this one. Primarily I have to convince myself that what the LLMs are telling me makes sense to me, too. It's been a while since I dug into PBC handling. I even looked up the original algorithm in Frenkel & Smit again – the classics, in FORTRAN! |
|
I've just made some tweaks, there was a 2x slowdown in FastNS construction from the additional numpy calls, but that's now been reduced to the same speeds. Tweaked the new tests a bit to clean them up. |
…nston/mdanalysis into fix-4906-triclinic-pbc
…nston/mdanalysis into fix-4906-triclinic-pbc
- extensive CHANGELOG entry - versionchanged (because the fix is important) - code docs/comment updates
8a50ba1 to
a7b88e1
Compare
orbeckst
left a comment
There was a problem hiding this comment.
It took me a while to understand the fixes. Both make sense to me.
I added more docs (and additional test).
Overall I am happy with it and think it definitely needs to go into 2.11.0.
| * 2.11.0 | ||
|
|
||
| Fixes | ||
| * Fixed PBC handling in triclinic boxes in `_triclinic_pbc()` (used by |
There was a problem hiding this comment.
I added an extensive entry because the impact of the fix is potentially big as it can directly affect computed properties. I know that we normally keep this short but in this case I'd like to be pretty clear.
| - `capped_distance()` and `self_capped_distance()` with the `nsgrid` or | ||
| `pkdtree` methods (and hence bond guessing, distance-based selections) | ||
| could miss pairs within the cutoff; `nsgrid` was affected in triclinic | ||
| boxes with two or more non-90º angles (e.g. rhombic dodecahedron), |
There was a problem hiding this comment.
Note that the patch contains multiple fixes and in fact affected more box shapes than originally noted.
| if (!msr) { | ||
| // translate remainder of crd to central cell along a-axis | ||
| lbound = crd[1] * a_ax_yfactor + crd[2] * a_ax_zfactor; | ||
| lbound = -(crd[1] * bi3 + crd[2] * bi6) * box[0]; |
There was a problem hiding this comment.
yes, this fix makes sense — I looked at it for a long time with diagrams, algebra, and LLM help, and I concur that this is necessary
| # Cells must be at least `cutoff` thick perpendicular to each pair of | ||
| # box faces, so size the grid by the box's perpendicular widths. | ||
| # The box matrix is lower triangular, so width = volume / face area | ||
| # has a closed form. | ||
| ax = self.triclinic_dimensions[XX] | ||
| bx = self.triclinic_dimensions[XY] | ||
| by = self.triclinic_dimensions[YY] | ||
| cx = self.triclinic_dimensions[XZ] | ||
| cy = self.triclinic_dimensions[YZ] | ||
| cz = self.triclinic_dimensions[ZZ] | ||
| volume = ax * by * cz | ||
| widths[0] = widths[1] = widths[2] = 0.0 | ||
| # invalid boxes give an all-zero matrix, leave widths at zero | ||
| if volume > 0: | ||
| widths[0] = volume / math.sqrt((by * cz) ** 2 + (bx * cz) ** 2 | ||
| + (bx * cy - by * cx) ** 2) | ||
| widths[1] = by * cz / math.sqrt(cy * cy + cz * cz) | ||
| widths[2] = cz |
There was a problem hiding this comment.
This fixes issues with boxes that have more than one non-90º angle
| xyz : numpy.ndarray int of shape (3,) | ||
| Cell coordinate for the given coordinate | ||
|
|
||
| Background |
There was a problem hiding this comment.
I think it's useful to write out how the calculation is done, given that the second key bug fix is the missing s_b b_x term
|
|
||
| eps = DielectricConstant(ag, make_whole=False).run() | ||
| assert_allclose(eps.results["eps_mean"], 721.711, rtol=1e-03) | ||
| # reference values depend on correct PBC handling |
There was a problem hiding this comment.
I wanted to make clear that this test is actually sensitive to PBC (which is good); the previous comment in this PR only said that the ref value changed but this historical info gets confusing over time without context
|
I quickly ran the new tests against the develop branch (without the fix).
|
|
Thanks for the thorough review and for updating the docs @orbeckt . I've just updated that test to definitely fail on Should be all good to go. |
…nston/mdanalysis into fix-4906-triclinic-pbc
|
You beat me to it, I was just about to push a similarly updated test. I have one that is guaranteed to fail with specially engineered points so I'll add it. |
|
Always good to have more tests! |
|
🎉 @IAlibay I hope this is still acceptable. |
Fixes #4906
In triclinic boxes where both
b_xandc_yare non-zero (e.g. truncated octahedra), thensgridandpkdtreemethods ofcapped_distance/self_capped_distancemiss pairs within the cutoff. This affects bond guessing, selections, etc. Reproduced with the issue'sstruc.txtand the truncated-octahedron script; both now matchbruteforce.Changes made in this Pull Request:
_triclinic_pbc: fix the a-axis wrapping bound, which could leave coordinates just outside the primary image (affectsapply_PBC,pkdtreeandnsgrid)FastNS.coord2cellxyz: fix the same missing term in the x cell index (as suspected here)FastNS._prepare_box: size grid cells from the box's perpendicular widths, so cells are never thinner than the cutoffapply_PBC,capped_distance,self_capped_distanceandFastNSin skewed boxesLLM / AI generated code disclosure
LLMs or other AI-powered tools (beyond simple IDE use cases) were used in this contribution: no
I did use Claude to scan through and track down the issue, but I wrote the code and the tests.
PR Checklist
package/CHANGELOGfile updated?package/AUTHORS? (If it is not, add it!)Developers Certificate of Origin
I certify that I can submit this code contribution as described in the Developer Certificate of Origin, under the MDAnalysis LICENSE.