-
Notifications
You must be signed in to change notification settings - Fork 74
Features/constraints #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Features/constraints #294
Changes from 1 commit
4157a20
646ddf5
69ee796
38c6138
6eb3d78
c630f39
bfdf6de
f5459b9
6b2710e
c955273
7d63069
e1388fd
ad4fa0a
8beb9d9
c577e1d
9cfe52b
be30d45
33d6025
399fbfd
b31ba80
1483977
3c267eb
06400e1
4081973
35749c3
8c067fb
0688bfe
be161e3
6afab52
4aa1447
e61e452
8144ed6
940827b
1cbd0b0
6e09895
7d8890f
be55c9b
33c6e92
d99a1a7
50d566f
eb26975
c15a012
87644fa
95857b0
7022df2
07624f0
0940919
b49e309
65fd0cf
fee207f
99e8ad3
3b8af40
a226b2d
d42fcf7
153f957
a954832
f07c930
9b1a88a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -400,21 +400,55 @@ def count_degrees_of_freedom( | |
| return max(0, total_dof) # Ensure non-negative | ||
|
|
||
|
|
||
| def warn_if_overlapping_constraints(constraints: list[Constraint]) -> None: | ||
| """Issue warnings if constraints might overlap in problematic ways. | ||
| def validate_constraints( # noqa: C901 | ||
| constraints: list[Constraint], state: SimState | None = None | ||
| ) -> None: | ||
| """Validate constraints for potential issues and incompatibilities. | ||
|
|
||
| This function checks for potential issues like multiple constraints | ||
| acting on the same atoms, which could lead to unexpected behavior. | ||
| This function checks for: | ||
| 1. Overlapping atom indices across multiple constraints | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For 2. AtomIndexedConstraints spanning multiple systems: for 1. it's not so clear that it's a problem to have the same atoms affected by multiple constraints. Another precise example of that, take a water molecule, you often want to constraints H-bonds. Then the oxygen atom will be concerned by 2 constraints. |
||
| 2. AtomIndexedConstraints spanning multiple systems (requires state) | ||
| 3. Mixing FixCom with other constraints (warning only) | ||
|
|
||
| Args: | ||
| constraints: List of constraints to check | ||
| constraints: List of constraints to validate | ||
| state: Optional SimState for validating atom indices belong to same system | ||
|
|
||
| Raises: | ||
| ValueError: If constraints are invalid or span multiple systems | ||
|
|
||
| Warns: | ||
| UserWarning: If constraints may lead to unexpected behavior | ||
| """ | ||
| if not constraints: | ||
| return | ||
|
|
||
| indexed_constraints = [] | ||
| has_com_constraint = False | ||
|
|
||
| for constraint in constraints: | ||
| if isinstance(constraint, AtomIndexedConstraint): | ||
| indexed_constraints.append(constraint) | ||
|
|
||
| # Validate that atom indices exist in state if provided | ||
| if state is not None and len(constraint.indices) > 0: | ||
| if constraint.indices.max() >= state.n_atoms: | ||
| raise ValueError( | ||
| f"Constraint {type(constraint).__name__} has indices up to " | ||
| f"{constraint.indices.max()}, but state only has {state.n_atoms} " | ||
| "atoms" | ||
| ) | ||
|
|
||
| # Check that all constrained atoms belong to same system | ||
| constrained_system_indices = state.system_idx[constraint.indices] | ||
| unique_systems = torch.unique(constrained_system_indices) | ||
| if len(unique_systems) > 1: | ||
| raise ValueError( | ||
| f"Constraint {type(constraint).__name__} acts on atoms from " | ||
| f"multiple systems {unique_systems.tolist()}. Each constraint " | ||
| f"must operate within a single system." | ||
| ) | ||
|
|
||
| elif isinstance(constraint, FixCom): | ||
| has_com_constraint = True | ||
|
|
||
|
|
@@ -427,7 +461,7 @@ def warn_if_overlapping_constraints(constraints: list[Constraint]) -> None: | |
| "Multiple constraints are acting on the same atoms. " | ||
| "This may lead to unexpected behavior.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| stacklevel=3, | ||
| ) | ||
|
|
||
| # Warn about COM constraint with fixed atoms | ||
|
|
@@ -437,5 +471,5 @@ def warn_if_overlapping_constraints(constraints: list[Constraint]) -> None: | |
| "unexpected behavior. The center of mass constraint is applied " | ||
| "to all atoms, including those that may be constrained by other means.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| stacklevel=3, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.