From a843ad0054f08a9a6c3ed069e8924d10edacd41d Mon Sep 17 00:00:00 2001 From: Johnson Wang Date: Mon, 23 Dec 2024 02:12:05 -0500 Subject: [PATCH 1/2] add particle free (different from active) --- genesis/engine/entities/mpm_entity.py | 21 +++++++++++++++++++++ genesis/engine/solvers/mpm_solver.py | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/genesis/engine/entities/mpm_entity.py b/genesis/engine/entities/mpm_entity.py index 104b4957..d9a8a4ce 100644 --- a/genesis/engine/entities/mpm_entity.py +++ b/genesis/engine/entities/mpm_entity.py @@ -185,6 +185,27 @@ def set_muscle(self, muscle_group=None, muscle_direction=None): self.set_muscle_direction(muscle_direction) + def set_free(self, free): + self._assert_active() + + self.solver._kernel_set_free( + self._particle_start, + self._n_particles, + free, + ) + + def get_free(self): + self._assert_active() + + free = gs.zeros((self._n_particles,), dtype=int, requires_grad=False, scene=self._scene) + self.solver._kernel_get_free( + self._particle_start, + self._n_particles, + free, + ) + + return free + @ti.kernel def clear_grad(self, f: ti.i32): for i in range(self.n_particles): diff --git a/genesis/engine/solvers/mpm_solver.py b/genesis/engine/solvers/mpm_solver.py index 6356a76b..f7c9028c 100644 --- a/genesis/engine/solvers/mpm_solver.py +++ b/genesis/engine/solvers/mpm_solver.py @@ -93,6 +93,7 @@ def init_particle_fields(self): mat_idx=gs.ti_int, mass=gs.ti_float, default_Jp=gs.ti_float, + free=gs.ti_int, # for muscle muscle_group=gs.ti_int, muscle_direction=gs.ti_vec3, @@ -384,6 +385,9 @@ def p2g(self, f: ti.i32): ) self.grid[f, base - self._grid_offset + offset].mass += weight * self.particles_info[i].mass + if self.particles_info[i].free == 0: # non-free particles behave as boundary conditions + self.grid[f, base - self._grid_offset + offset].vel_in = ti.Vector.zero(gs.ti_float, 3) + @ti.kernel def g2p(self, f: ti.i32): for i in range(self._n_particles): @@ -658,6 +662,7 @@ def _kernel_add_particles( self.particles_info[i_global].mat_idx = mat_idx self.particles_info[i_global].default_Jp = mat_default_Jp self.particles_info[i_global].mass = self._p_vol * mat_rho + self.particles_info[i_global].free = 1 self.particles_info[i_global].muscle_group = 0 self.particles_info[i_global].muscle_direction = ti.Vector([0.0, 0.0, 1.0], dt=gs.ti_float) @@ -816,6 +821,28 @@ def _kernel_set_muscle_direction( for j in ti.static(range(3)): self.particles_info[i_global].muscle_direction[j] = muscle_direction[i, j] + @ti.kernel + def _kernel_set_free( + self, + particle_start: ti.i32, + n_particles: ti.i32, + free: ti.types.ndarray(), + ): + for i in range(n_particles): + i_global = i + particle_start + self.particles_info[i_global].free = free[i] + + @ti.kernel + def _kernel_get_free( + self, + particle_start: ti.i32, + n_particles: ti.i32, + free: ti.types.ndarray(), + ): + for i in range(n_particles): + i_global = i + particle_start + free[i] = self.particles_info[i_global].free + @ti.kernel def _kernel_get_state( self, From c054cf31ca20e1d9236b60628b0fb2e74ba6dce4 Mon Sep 17 00:00:00 2001 From: Johnson Wang Date: Mon, 23 Dec 2024 02:14:51 -0500 Subject: [PATCH 2/2] fix formatting --- genesis/engine/solvers/mpm_solver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/genesis/engine/solvers/mpm_solver.py b/genesis/engine/solvers/mpm_solver.py index f7c9028c..8d490022 100644 --- a/genesis/engine/solvers/mpm_solver.py +++ b/genesis/engine/solvers/mpm_solver.py @@ -385,7 +385,7 @@ def p2g(self, f: ti.i32): ) self.grid[f, base - self._grid_offset + offset].mass += weight * self.particles_info[i].mass - if self.particles_info[i].free == 0: # non-free particles behave as boundary conditions + if self.particles_info[i].free == 0: # non-free particles behave as boundary conditions self.grid[f, base - self._grid_offset + offset].vel_in = ti.Vector.zero(gs.ti_float, 3) @ti.kernel