Refactor element inference to atomic-number inference with hash-backed lookup tables #8
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.
This PR implements a comprehensive refactor of the element resolution system to make it fast, explicit, and future-proof for the upcoming atom_type refactor, while maintaining full backward compatibility.
Overview
The previous
element_guessnaming was unclear and suggested ad-hoc heuristics. This refactor introduces an explicit atomic number (Z) identity system with a human-readable enum and implements hash-backed lookups for common molecular modeling forcefields and PDB conventions.Key Changes
New Public API (
src/core/md_atomic.h)typedef uint8_t md_atomic_number_twithmd_element_tas legacy aliasMD_Z_H=1, MD_Z_C=6, MD_Z_O=8, ..., MD_Z_OG=118for all 118 elementsHash-Backed Inference System (
src/md_atomic_infer.c)md_hashmap32_twithmd_hash64keys for O(1) performancePerfect Backward Compatibility
All existing
md_util_*functions remain unchanged and fully functional:The old
md_util_element_guessnow delegates to the new hash-backed system, providing better performance and accuracy while maintaining identical behavior.Examples
Context-Aware Disambiguation
Water Recognition
Testing
Added comprehensive test suite (13 new tests) covering:
Performance & Compatibility
This refactor delivers the requested fast, explicit element inference while maintaining 100% backward compatibility for downstream users including viamd.
This pull request was created as a result of the following prompt from Copilot chat.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.