Repository navigation
Speed up SMARTS atom typing (~2x) - #674
Merged
Merged
Conversation
SMARTS check (node_match) runs before networkx's graph structure checks. Most candidate atom pairs fail on element or neighbor count, so this skips the more expensive structural work for them.
…he many == comparisons with strs. This lets the checks bypass Lark's implementation of __eq__ and use Python's str __eq__ method.
…the only rule that can change after an initial pass
…ard node_match - Test that _iterate_rules runs non-%label rules only on the first pass, still converges when %label rules depend on each other across passes, and still warns when max_iter is reached. - Comment on why only %label rules are re-run, and what would break it. - Skip the early node_match check in SMARTSMatcher if node_match is None. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #674 +/- ##
==========================================
+ Coverage 76.47% 76.64% +0.16%
==========================================
Files 15 15
Lines 1726 1734 +8
==========================================
+ Hits 1320 1329 +9
+ Misses 406 405 -1 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three changes that speed up atom typing (
find_atomtypes) by about 2x with OPLS-AA. The atom types produced are unchanged.smarts_graph.py):SMARTSMatcheroverridessyntactic_feasibilityso foyer's cheap atom-level SMARTS check (node_match) runs before networkx's induced-subgraph structure checks. Most candidate pairs fail on element orXcount, so they skip the structural work. Pairs that pass go through the unchanged networkx check viasuper(). ~1.17x.stronce (smarts_graph.py):SMARTSGraph.__init__converts eachTree.datafrom a larkTokentostrafter_add_edges, which still needs.data.value._atom_expr_matchesand_atom_id_matchescompare these tags to string literals on every atom check.Token.__eq__is a Python-level call, whilestr.__eq__runs in C. This was about 15% of run time. ~1.8x on top of (1).%labelrules after the first pass (atomtyper.py): rules without%labeldepend only on element, neighbor count, rings and bonds, none of which change during typing, so their matches are final after the first pass. Passes 2+ re-run only the 133 of 228 OPLS-AA rules that use%label. Convergence,max_iterand the warning behave as before. ~1.57x on top of (1) and (2).Combined (
mainvs. this branch, OPLS-AA)Branched polyalkane chain (propyl branch on every other backbone carbon):
mainAtom types were identical to
mainat every size tested.Notes for reviewers
syntactic_feasibilityis a networkx method, not a documented hook. Its signature and its call order (just beforesemantic_feasibility) have been the same since well before ournetworkx>=2.5floor. The override can only reject pairs thatsemantic_feasibilitywould also reject, so results can't change. Pairs that pass the atom check runnode_matchtwice; they are a small fraction of all candidate pairs.SMARTSGraph.astin place, soTree.datais nowstrrather thanToken. Inside foyer,.data.valueis only read during graph construction. gmso, mbuild, forcefield-utilities and antefoyer don't useSMARTSGraph.ast.%labelbeing the only SMARTS primitive that reads typing state. A comment in_iterate_rulessays so and names recursive SMARTS ($(...), not implemented yet) as the case to revisit.Tests
TestIterateRulesintest_atomtyping.py:%labelrule runs only once;max_iterwarning still fires.test_forcefield_parameters.pyare skipped without antefoyer. With antefoyer installed, their fixture fails onforcefields.load_GAFF()because Add foyer logger and update pytest.warns testing with caplog #631 removed the plugin attachment infoyer/forcefields/__init__.py. That breakage is unrelated to this PR. I ran them with a local shim that re-attaches the entry points, and they pass. That should be fixed separately.🤖 Generated with Claude Code