-
Notifications
You must be signed in to change notification settings - Fork 70
Mixed Pbc Support #320
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
Mixed Pbc Support #320
Changes from all commits
29779d8
9a95a7f
3f64506
0d1b82f
c3e14fc
12021f7
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 |
|---|---|---|
|
|
@@ -117,7 +117,7 @@ class BaseState: | |
|
|
||
| positions: torch.Tensor | ||
| cell: torch.Tensor | ||
| pbc: bool | ||
| pbc: torch.Tensor | ||
| species: torch.Tensor | ||
|
|
||
|
|
||
|
|
@@ -133,14 +133,18 @@ def __init__( | |
| device: torch.device | None = None, | ||
| dtype: torch.dtype = torch.float32, | ||
| *, # Force keyword-only arguments | ||
| pbc: bool = True, | ||
| pbc: torch.Tensor | bool = True, | ||
| cutoff: float | None = None, | ||
| ) -> None: | ||
| """Initialize a soft sphere model for multi-component systems.""" | ||
| super().__init__() | ||
| self.device = device or torch.device("cpu") | ||
| self.dtype = dtype | ||
| self.pbc = pbc | ||
| self.pbc = ( | ||
| pbc | ||
| if isinstance(pbc, torch.Tensor) | ||
| else torch.tensor([pbc] * 3, dtype=torch.bool) | ||
| ) | ||
|
|
||
| # Store species list and determine number of unique species | ||
| self.species = species | ||
|
|
@@ -369,20 +373,25 @@ def simulation( | |
| ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
| # Create the simulation environment. | ||
| box_size = box_size_at_packing_fraction(diameter, packing_fraction) | ||
| cell = torch.eye(2) * box_size | ||
| cell = torch.eye(3) * box_size | ||
|
Collaborator
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. I'm not against diff_sim being in 3D, but was there any reason to be in 2D in the first place?
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. ¯\(ツ)/¯ |
||
| # Create the energy function. | ||
| sigma = species_sigma(diameter) | ||
| model = SoftSphereMultiModel(sigma_matrix=sigma, species=species) | ||
| model = torch.compile(model) | ||
| # Randomly initialize the system. | ||
| # Fix seed for reproducible random positions | ||
| torch.manual_seed(seed) | ||
| R = torch.rand(N, 2) * box_size | ||
| R = torch.rand(N, 3) * box_size | ||
|
|
||
| # Minimize to the nearest minimum. | ||
| init_fn, apply_fn = gradient_descent(model, lr=0.1) | ||
|
|
||
| custom_state = BaseState(positions=R, cell=cell, species=species, pbc=True) | ||
| custom_state = BaseState( | ||
| positions=R, | ||
| cell=cell, | ||
| species=species, | ||
| pbc=torch.tensor([True] * 3, dtype=torch.bool), | ||
| ) | ||
| state = init_fn(custom_state) | ||
| for _ in range(simulation_steps): | ||
| state = apply_fn(state) | ||
|
|
@@ -415,7 +424,7 @@ def simulation( | |
| seeds = torch.arange(1, 6) | ||
| box_size_tensor = torch.zeros(len(diameters), len(seeds)) | ||
| raft_energy_tensor = torch.zeros(len(diameters), len(seeds)) | ||
| bubble_positions_tensor = torch.zeros(len(diameters), len(seeds), N, 2) | ||
| bubble_positions_tensor = torch.zeros(len(diameters), len(seeds), N, 3) | ||
| for i, d in enumerate(diameters): | ||
| for j, s in enumerate(seeds): | ||
| box_size, raft_energy, bubble_positions = simulation(d, s) | ||
|
|
@@ -468,7 +477,7 @@ def short_simulation( | |
| ) -> tuple[torch.Tensor, torch.Tensor]: | ||
| diameter = diameter.requires_grad_(True) | ||
| box_size = box_size_at_packing_fraction(diameter, packing_fraction) | ||
| cell = torch.eye(2) * box_size | ||
| cell = torch.eye(3) * box_size | ||
| # Create the energy function. | ||
| sigma = species_sigma(diameter) | ||
| model = SoftSphereMultiModel(sigma_matrix=sigma, species=species) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ def random_state() -> MDState: | |
| cell=torch.unsqueeze(torch.eye(3) * 10.0, 0), | ||
| atomic_numbers=torch.ones(10, dtype=torch.int32), | ||
| system_idx=torch.zeros(10, dtype=torch.int32), | ||
| pbc=True, | ||
| pbc=[True, True, False], | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -93,7 +93,7 @@ def test_write_state_single( | |
| assert trajectory.get_array("positions").shape == (1, 10, 3) | ||
| assert trajectory.get_array("atomic_numbers").shape == (1, 10) | ||
| assert trajectory.get_array("cell").shape == (1, 3, 3) | ||
| assert trajectory.get_array("pbc").shape == (1,) | ||
| assert trajectory.get_array("pbc").shape == (3,) | ||
|
|
||
|
|
||
| def test_write_state_multiple( | ||
|
|
@@ -106,7 +106,7 @@ def test_write_state_multiple( | |
| assert trajectory.get_array("positions").shape == (2, 10, 3) | ||
| assert trajectory.get_array("atomic_numbers").shape == (1, 10) | ||
| assert trajectory.get_array("cell").shape == (2, 3, 3) | ||
| assert trajectory.get_array("pbc").shape == (1,) | ||
| assert trajectory.get_array("pbc").shape == (3,) | ||
|
|
||
|
|
||
| def test_optional_arrays(trajectory: TorchSimTrajectory, random_state: MDState) -> None: | ||
|
|
@@ -439,7 +439,7 @@ def test_get_atoms(trajectory: TorchSimTrajectory, random_state: MDState) -> Non | |
| np.testing.assert_allclose( | ||
| atoms.get_atomic_numbers(), random_state.atomic_numbers.numpy() | ||
| ) | ||
| assert atoms.pbc.all() == random_state.pbc | ||
| np.testing.assert_array_equal(atoms.pbc, random_state.pbc.detach().cpu().numpy()) | ||
|
|
||
|
|
||
| def test_get_state(trajectory: TorchSimTrajectory, random_state: MDState) -> None: | ||
|
|
@@ -473,12 +473,13 @@ def test_get_state(trajectory: TorchSimTrajectory, random_state: MDState) -> Non | |
| assert state.positions.dtype == expected_dtype | ||
| assert state.cell.dtype == expected_dtype | ||
| assert state.atomic_numbers.dtype == torch.int # Should always be int | ||
| assert state.pbc.dtype == torch.bool # Should always be bool | ||
|
|
||
| # Test values (convert to CPU for comparison) | ||
| np.testing.assert_allclose(state.positions, random_state.positions) | ||
| np.testing.assert_allclose(state.cell, random_state.cell) | ||
| np.testing.assert_allclose(state.atomic_numbers, random_state.atomic_numbers) | ||
| assert state.pbc == random_state.pbc | ||
| assert torch.equal(state.pbc, random_state.pbc) | ||
|
|
||
|
|
||
| def test_write_ase_trajectory( | ||
|
|
@@ -509,7 +510,7 @@ def test_write_ase_trajectory( | |
| np.testing.assert_allclose( | ||
| atoms.get_atomic_numbers(), random_state.atomic_numbers.numpy() | ||
| ) | ||
| assert atoms.pbc.all() == random_state.pbc | ||
| np.testing.assert_array_equal(atoms.pbc, random_state.pbc.numpy()) | ||
|
Collaborator
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. maybe you should transfer first in cpu() in case we ever run this on gpu?
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. I think this is fine because the random_state is automatically made on cpu by default. Maybe we can do it in a separate PR if people run these tests and see it fail? (trying to keep this one slim) |
||
|
|
||
| # Clean up | ||
| ase_traj.close() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this repeat is needed since torch_nl_linked_cell accepts (num_systems, 3) for the pbc. it doens't just accept a tensor of shape (3,)