-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[FEATURE] Adds support for attaching MPM particles to rigid links. #2205
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
Changes from all commits
05892a5
4b4fd25
fd747d7
b81d3b5
ec34bae
230a167
ea8886f
9fb3070
858d511
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """ | ||
| MPM to Rigid Link Attachment | ||
|
|
||
| Demonstrates attaching MPM particles to rigid links using soft constraints. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
|
|
||
| import torch | ||
|
|
||
| import genesis as gs | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-v", "--vis", action="store_true", default=False) | ||
| parser.add_argument("-c", "--cpu", action="store_true", default=False) | ||
| args = parser.parse_args() | ||
|
|
||
| gs.init(backend=gs.cpu if args.cpu else gs.gpu) | ||
|
|
||
| scene = gs.Scene( | ||
| sim_options=gs.options.SimOptions(dt=2e-3, substeps=20), | ||
| mpm_options=gs.options.MPMOptions( | ||
| lower_bound=(-1.0, -1.0, 0.0), | ||
| upper_bound=(1.0, 1.0, 1.5), | ||
| grid_density=64, | ||
| ), | ||
| viewer_options=gs.options.ViewerOptions( | ||
| camera_pos=(1.5, 0.0, 0.8), | ||
| camera_lookat=(0.0, 0.0, 0.4), | ||
| ), | ||
| show_viewer=args.vis, | ||
| ) | ||
|
|
||
| scene.add_entity(gs.morphs.Plane()) | ||
|
|
||
| rigid_box = scene.add_entity( | ||
| gs.morphs.Box(pos=(0.0, 0.0, 0.55), size=(0.12, 0.12, 0.05), fixed=False), | ||
| ) | ||
|
|
||
| mpm_cube = scene.add_entity( | ||
| material=gs.materials.MPM.Elastic(E=5e4, nu=0.3, rho=1000), | ||
| morph=gs.morphs.Box(pos=(0.0, 0.0, 0.35), size=(0.15, 0.15, 0.15)), | ||
| ) | ||
|
|
||
| scene.build() | ||
|
|
||
| # Attach top particles of MPM cube to the rigid box | ||
| mask = mpm_cube.get_particles_in_bbox((-0.08, -0.08, 0.41), (0.08, 0.08, 0.44)) | ||
| mpm_cube.set_particle_constraints(mask, rigid_box.links[0].idx, stiffness=1e5) | ||
|
|
||
| n_steps = 500 if "PYTEST_VERSION" not in os.environ else 1 | ||
| initial_z = 0.55 | ||
|
|
||
| for i in range(n_steps): | ||
| z_offset = 0.15 * (1 - abs((i % 200) - 100) / 100.0) | ||
| target_qpos = torch.tensor([0.0, 0.0, initial_z + z_offset, 1.0, 0.0, 0.0, 0.0], device=gs.device) | ||
| rigid_box.set_qpos(target_qpos) | ||
| scene.step() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,6 +221,64 @@ def test_deformable_parallel(show_viewer): | |
| assert_allclose(water.get_particles_vel(), 0.0, atol=5e-2) | ||
|
|
||
|
|
||
| @pytest.mark.required | ||
| def test_mpm_particle_constraints(show_viewer): | ||
|
Comment on lines
+224
to
+225
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. You should flag this test as slow if it is (>200s). Beware that it means that it will only run on production CI, so only do this if necessary.
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. it's not slow on my machine, but I'm happy to add the tag to make our generic CI not too slow
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.
The speed must be benched on the generic CI runners. Not your machine. So you need to run the entire CI pipeline without "slow" mark at least once, otherwise you will never know since it will be skipped. |
||
| """Test MPM particle constraints: bbox selection, attachment, and following.""" | ||
| scene = gs.Scene( | ||
| sim_options=gs.options.SimOptions( | ||
| dt=2e-3, | ||
| substeps=20, | ||
| ), | ||
| mpm_options=gs.options.MPMOptions( | ||
| lower_bound=(-1.0, -1.0, 0.0), | ||
| upper_bound=(1.0, 1.0, 1.0), | ||
| grid_density=64, | ||
| ), | ||
| show_viewer=show_viewer, | ||
| show_FPS=False, | ||
| ) | ||
| scene.add_entity(gs.morphs.Plane()) | ||
| rigid_box = scene.add_entity( | ||
| gs.morphs.Box( | ||
| pos=(0, 0, 0.55), | ||
| size=(0.12, 0.12, 0.05), | ||
| fixed=True, | ||
| ), | ||
| ) | ||
| mpm_cube = scene.add_entity( | ||
| material=gs.materials.MPM.Elastic( | ||
| E=5e4, | ||
| nu=0.3, | ||
| rho=1000, | ||
| ), | ||
| morph=gs.morphs.Box( | ||
| pos=(0, 0, 0.35), | ||
| size=(0.15, 0.15, 0.15), | ||
| ), | ||
| ) | ||
| scene.build(n_envs=2) | ||
|
|
||
| # Test get_particles_in_bbox - returns (n_envs, n_particles) mask | ||
| mask = mpm_cube.get_particles_in_bbox((-0.08, -0.08, 0.41), (0.08, 0.08, 0.44)) | ||
| assert mask.shape == (2, mpm_cube.n_particles), "mask should be (n_envs, n_particles)" | ||
| assert mask.any(), "bbox should select some particles" | ||
| assert not mask.all(), "bbox should not select all particles" | ||
|
|
||
| # Attach and test following | ||
| link_idx = rigid_box.links[0].idx | ||
| mpm_cube.set_particle_constraints(mask, link_idx, stiffness=1e5) | ||
| initial_rigid_pos = rigid_box.get_pos().clone() | ||
| initial_mpm_x = mpm_cube.get_particles_pos()[:, mask[0], 0].mean() | ||
|
|
||
| pos_diff = torch.tensor([0.2, 0, 0], device=gs.device) | ||
| rigid_box.set_pos(initial_rigid_pos + pos_diff, zero_velocity=False) | ||
| for _ in range(30): | ||
| scene.step() | ||
|
|
||
| mpm_diff = mpm_cube.get_particles_pos()[:, mask[0], 0].mean() - initial_mpm_x | ||
| assert mpm_diff > pos_diff[0] * 0.3, f"MPM should follow rigid link. Got {mpm_diff:.3f}" | ||
|
|
||
|
|
||
| def test_sf_solver(show_viewer): | ||
| import gstaichi as ti | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.