Open
Conversation
Validate externally supplied constraints in count_degrees_of_freedom, centralize per-system DOF math, and add focused regression tests for malformed/out-of-bounds constraint inputs.
3 tasks
thomasloux
reviewed
Mar 2, 2026
|
|
||
|
|
||
| return torch.clamp(total_dof, min=0) | ||
| def _validate_constraints_for_dof(state: SimState, constraints: list[Constraint]) -> None: |
Collaborator
There was a problem hiding this comment.
I think that validate_constraints is enough. The other if-statements should be tests perform by pytest.
| """ | ||
| # Start with unconstrained DOF per system | ||
| total_dof = 3 * state.n_atoms_per_system | ||
| constraints_to_count = state.constraints if constraints is None else constraints |
Collaborator
There was a problem hiding this comment.
Why not but to me it is simpler to only consider the input constraints. Otherwise the user should use state.get_degrees_of_freedom()
Comment on lines
+631
to
+645
| if constraints is not None: | ||
| _validate_constraints_for_dof(state, constraints_to_count) | ||
| return torch.clamp(_dof_per_system(state, constraints_to_count), min=0) | ||
|
|
||
|
|
||
| # Subtract DOF removed by constraints | ||
| def _dof_per_system( | ||
| state: SimState, constraints: list[Constraint] | None = None | ||
| ) -> torch.Tensor: | ||
| """Compute unconstrained-minus-removed DOF per system.""" | ||
| dof_per_system = 3 * state.n_atoms_per_system | ||
| if constraints is not None: | ||
| for constraint in constraints: | ||
| total_dof -= constraint.get_removed_dof(state) | ||
| dof_per_system -= constraint.get_removed_dof(state) | ||
| return dof_per_system | ||
|
|
Collaborator
There was a problem hiding this comment.
I'm not convinced by the value of separating these two helper functions
Collaborator
There was a problem hiding this comment.
Ok I saw that you used it with state.get_degrees_of_freedom. Then I'm fine with the 2 functions
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
count_degrees_of_freedomas a public helper, but validate explicitly supplied constraints against the providedstatebefore counting_dof_per_systemand reuse it from bothcount_degrees_of_freedomandSimState.get_number_of_degrees_of_freedomget_removed_dofoutputs (wrong shape, negative, non-tensor, non-finite), plus helper/method parity and strict-vs-clamped behaviorContext
This follows the API discussion in #479 and the end-of-thread discussion in #477: keep the helper for external use, but make it safe and explicit about compatibility checks instead of allowing arbitrary mismatched state/constraint pairs.
Refs:
Test plan
pytest tests/test_constraints.py -q