Fix: particle indexes passed to addParticle #17
Merged
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.
Without this fix, it appears that the particle indexes passed to force.addParticle() in restraint_force() were incorrect when selectors are used.
I've been using this code to learn how to write my own steered MD protocol, and I ran into this part while going through it. The first argument to addParticle() is a particle index; it should always be equal to the index used to subscript positions[index]. When calling enumerate in this way, in general, i is not the same as index.
The particle index is also expected to be an int; MDTrajTopology select() returns an ndarray of int64, which needs an explicit cast as done here. Omitting the cast would case
NotImplementedError: Wrong number or type of arguments for overloaded function 'CustomExternalForce_addParticle'.
.The original code appears to works correctly if no selectors are used, since in that case i and index would be equal. But if any selectors (eg "backbone") are used, then it would add forces to the wrong particles.
I would be grateful if you can double-check this and let me know if my understanding of what is going on is correct.