Skip to content

Commit

Permalink
Merge pull request #263 from zswang666/feature/fix_mpm_particles
Browse files Browse the repository at this point in the history
[Feature] add particle free in MPM
  • Loading branch information
zswang666 authored Dec 27, 2024
2 parents affa7d9 + c054cf3 commit 35a2fe8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions genesis/engine/entities/mpm_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
27 changes: 27 additions & 0 deletions genesis/engine/solvers/mpm_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 35a2fe8

Please sign in to comment.