Skip to content

Commit 4ec0437

Browse files
authored
fix: optimize atom type mapping (#5043)
Bug fix when type num in training system is less than type map. Fix issue #5042 @ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Optimized internal atom-type mapping to make data import and processing more efficient. * Maintains identical behavior and output formats; no changes to public interfaces or exported declarations. * Improves performance and observability during data handling without altering results or user-facing functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 7f25e16 commit 4ec0437

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

deepmd/utils/data.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,10 @@ def __init__(
103103
f"Elements {missing_elements} are not present in the provided `type_map`."
104104
)
105105
if not self.mixed_type:
106-
# Use vectorized operation for better performance with large atom counts
107-
# Create a mapping array where old_type_idx -> new_type_idx
108-
max_old_type = max(self.atom_type) + 1
109-
type_mapping = np.zeros(max_old_type, dtype=np.int32)
110-
for old_idx in range(len(self.type_map)):
111-
type_mapping[old_idx] = type_map.index(self.type_map[old_idx])
112-
self.atom_type = type_mapping[self.atom_type].astype(np.int32)
106+
old_to_new_type_idx = np.array(
107+
[type_map.index(name) for name in self.type_map], dtype=np.int32
108+
)
109+
self.atom_type = old_to_new_type_idx[self.atom_type].astype(np.int32)
113110
else:
114111
self.enforce_type_map = True
115112
sorter = np.argsort(type_map)

0 commit comments

Comments
 (0)