Skip to content

Commit

Permalink
Merge branch 'model-defaults-gh-429' into 'main'
Browse files Browse the repository at this point in the history
Fix overriding Model defaults (GH-429)

See merge request omniverse/warp!977
  • Loading branch information
shi-eric committed Jan 14, 2025
2 parents 0b832e4 + 8bff67d commit af3b467
Showing 1 changed file with 107 additions and 51 deletions.
158 changes: 107 additions & 51 deletions warp/sim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,8 @@ def allocate_rigid_contacts(self, target=None, count=None, limited_contact_count
@property
def soft_contact_max(self):
"""Maximum number of soft contacts that can be registered"""
if self.soft_contact_particle is None:
return 0
return len(self.soft_contact_particle)


Expand Down Expand Up @@ -1747,8 +1749,8 @@ def add_joint_revolute(
mode: int = JOINT_MODE_FORCE,
limit_lower: float = -2 * math.pi,
limit_upper: float = 2 * math.pi,
limit_ke: float = default_joint_limit_ke,
limit_kd: float = default_joint_limit_kd,
limit_ke: float = None,
limit_kd: float = None,
linear_compliance: float = 0.0,
angular_compliance: float = 0.0,
armature: float = 1e-2,
Expand All @@ -1769,8 +1771,8 @@ def add_joint_revolute(
target_kd: The damping of the joint target
limit_lower: The lower limit of the joint
limit_upper: The upper limit of the joint
limit_ke: The stiffness of the joint limit
limit_kd: The damping of the joint limit
limit_ke: The stiffness of the joint limit (None to use the default value :attr:`default_joint_limit_ke`)
limit_kd: The damping of the joint limit (None to use the default value :attr:`default_joint_limit_kd`)
linear_compliance: The linear compliance of the joint
angular_compliance: The angular compliance of the joint
armature: Artificial inertia added around the joint axis
Expand All @@ -1788,6 +1790,9 @@ def add_joint_revolute(
if child_xform is None:
child_xform = wp.transform()

limit_ke = limit_ke if limit_ke is not None else self.default_joint_limit_ke
limit_kd = limit_kd if limit_kd is not None else self.default_joint_limit_kd

action = 0.0
if target is None and mode == JOINT_MODE_TARGET_POSITION:
action = 0.5 * (limit_lower + limit_upper)
Expand Down Expand Up @@ -1834,8 +1839,8 @@ def add_joint_prismatic(
mode: int = JOINT_MODE_FORCE,
limit_lower: float = -1e4,
limit_upper: float = 1e4,
limit_ke: float = default_joint_limit_ke,
limit_kd: float = default_joint_limit_kd,
limit_ke: float = None,
limit_kd: float = None,
linear_compliance: float = 0.0,
angular_compliance: float = 0.0,
armature: float = 1e-2,
Expand All @@ -1856,8 +1861,8 @@ def add_joint_prismatic(
target_kd: The damping of the joint target
limit_lower: The lower limit of the joint
limit_upper: The upper limit of the joint
limit_ke: The stiffness of the joint limit
limit_kd: The damping of the joint limit
limit_ke: The stiffness of the joint limit (None to use the default value :attr:`default_joint_limit_ke`)
limit_kd: The damping of the joint limit (None to use the default value :attr:`default_joint_limit_ke`)
linear_compliance: The linear compliance of the joint
angular_compliance: The angular compliance of the joint
armature: Artificial inertia added around the joint axis
Expand All @@ -1875,6 +1880,9 @@ def add_joint_prismatic(
if child_xform is None:
child_xform = wp.transform()

limit_ke = limit_ke if limit_ke is not None else self.default_joint_limit_ke
limit_kd = limit_kd if limit_kd is not None else self.default_joint_limit_kd

action = 0.0
if target is None and mode == JOINT_MODE_TARGET_POSITION:
action = 0.5 * (limit_lower + limit_upper)
Expand Down Expand Up @@ -3501,11 +3509,11 @@ def add_triangle(
i: int,
j: int,
k: int,
tri_ke: float = default_tri_ke,
tri_ka: float = default_tri_ka,
tri_kd: float = default_tri_kd,
tri_drag: float = default_tri_drag,
tri_lift: float = default_tri_lift,
tri_ke: float = None,
tri_ka: float = None,
tri_kd: float = None,
tri_drag: float = None,
tri_lift: float = None,
) -> float:
"""Adds a triangular FEM element between three particles in the system.
Expand All @@ -3525,6 +3533,11 @@ def add_triangle(
between the particles in their initial configuration.
"""
# TODO: Expose elastic parameters on a per-element basis
tri_ke = tri_ke if tri_ke is not None else self.default_tri_ke
tri_ka = tri_ka if tri_ka is not None else self.default_tri_ka
tri_kd = tri_kd if tri_kd is not None else self.default_tri_kd
tri_drag = tri_drag if tri_drag is not None else self.default_tri_drag
tri_lift = tri_lift if tri_lift is not None else self.default_tri_lift

# compute basis for 2D rest pose
p = self.particle_q[i]
Expand Down Expand Up @@ -3705,8 +3718,8 @@ def add_edge(
k: int,
l: int,
rest: float = None,
edge_ke: float = default_edge_ke,
edge_kd: float = default_edge_kd,
edge_ke: float = None,
edge_kd: float = None,
):
"""Adds a bending edge element between four particles in the system.
Expand All @@ -3727,6 +3740,9 @@ def add_edge(
winding: (i, k, l), (j, l, k).
"""
edge_ke = edge_ke if edge_ke is not None else self.default_edge_ke
edge_kd = edge_kd if edge_kd is not None else self.default_edge_kd

# compute rest angle
if rest is None:
x1 = self.particle_q[i]
Expand Down Expand Up @@ -3834,17 +3850,17 @@ def add_cloth_grid(
fix_right: bool = False,
fix_top: bool = False,
fix_bottom: bool = False,
tri_ke: float = default_tri_ke,
tri_ka: float = default_tri_ka,
tri_kd: float = default_tri_kd,
tri_drag: float = default_tri_drag,
tri_lift: float = default_tri_lift,
edge_ke: float = default_edge_ke,
edge_kd: float = default_edge_kd,
tri_ke: float = None,
tri_ka: float = None,
tri_kd: float = None,
tri_drag: float = None,
tri_lift: float = None,
edge_ke: float = None,
edge_kd: float = None,
add_springs: bool = False,
spring_ke: float = default_spring_ke,
spring_kd: float = default_spring_kd,
particle_radius: float = default_particle_radius,
spring_ke: float = None,
spring_kd: float = None,
particle_radius: float = None,
):
"""Helper to create a regular planar cloth grid
Expand All @@ -3866,6 +3882,16 @@ def add_cloth_grid(
fix_top: Make the top-most edge of particles kinematic
fix_bottom: Make the bottom-most edge of particles kinematic
"""
tri_ke = tri_ke if tri_ke is not None else self.default_tri_ke
tri_ka = tri_ka if tri_ka is not None else self.default_tri_ka
tri_kd = tri_kd if tri_kd is not None else self.default_tri_kd
tri_drag = tri_drag if tri_drag is not None else self.default_tri_drag
tri_lift = tri_lift if tri_lift is not None else self.default_tri_lift
edge_ke = edge_ke if edge_ke is not None else self.default_edge_ke
edge_kd = edge_kd if edge_kd is not None else self.default_edge_kd
spring_ke = spring_ke if spring_ke is not None else self.default_spring_ke
spring_kd = spring_kd if spring_kd is not None else self.default_spring_kd
particle_radius = particle_radius if particle_radius is not None else self.default_particle_radius

def grid_index(x, y, dim_x):
return y * dim_x + x
Expand Down Expand Up @@ -3968,17 +3994,17 @@ def add_cloth_mesh(
density: float,
edge_callback=None,
face_callback=None,
tri_ke: float = default_tri_ke,
tri_ka: float = default_tri_ka,
tri_kd: float = default_tri_kd,
tri_drag: float = default_tri_drag,
tri_lift: float = default_tri_lift,
edge_ke: float = default_edge_ke,
edge_kd: float = default_edge_kd,
tri_ke: float = None,
tri_ka: float = None,
tri_kd: float = None,
tri_drag: float = None,
tri_lift: float = None,
edge_ke: float = None,
edge_kd: float = None,
add_springs: bool = False,
spring_ke: float = default_spring_ke,
spring_kd: float = default_spring_kd,
particle_radius: float = default_particle_radius,
spring_ke: float = None,
spring_kd: float = None,
particle_radius: float = None,
):
"""Helper to create a cloth model from a regular triangle mesh
Expand All @@ -3999,6 +4025,17 @@ def add_cloth_mesh(
The mesh should be two manifold.
"""
tri_ke = tri_ke if tri_ke is not None else self.default_tri_ke
tri_ka = tri_ka if tri_ka is not None else self.default_tri_ka
tri_kd = tri_kd if tri_kd is not None else self.default_tri_kd
tri_drag = tri_drag if tri_drag is not None else self.default_tri_drag
tri_lift = tri_lift if tri_lift is not None else self.default_tri_lift
edge_ke = edge_ke if edge_ke is not None else self.default_edge_ke
edge_kd = edge_kd if edge_kd is not None else self.default_edge_kd
spring_ke = spring_ke if spring_ke is not None else self.default_spring_ke
spring_kd = spring_kd if spring_kd is not None else self.default_spring_kd
particle_radius = particle_radius if particle_radius is not None else self.default_particle_radius

num_tris = int(len(indices) / 3)

start_vertex = len(self.particle_q)
Expand Down Expand Up @@ -4076,9 +4113,11 @@ def add_particle_grid(
cell_z: float,
mass: float,
jitter: float,
radius_mean: float = default_particle_radius,
radius_mean: float = None,
radius_std: float = 0.0,
):
radius_mean = radius_mean if radius_mean is not None else self.default_particle_radius

rng = np.random.default_rng(42)
for z in range(dim_z):
for y in range(dim_y):
Expand Down Expand Up @@ -4113,11 +4152,11 @@ def add_soft_grid(
fix_right: bool = False,
fix_top: bool = False,
fix_bottom: bool = False,
tri_ke: float = default_tri_ke,
tri_ka: float = default_tri_ka,
tri_kd: float = default_tri_kd,
tri_drag: float = default_tri_drag,
tri_lift: float = default_tri_lift,
tri_ke: float = None,
tri_ka: float = None,
tri_kd: float = None,
tri_drag: float = None,
tri_lift: float = None,
):
"""Helper to create a rectangular tetrahedral FEM grid
Expand All @@ -4144,6 +4183,11 @@ def add_soft_grid(
fix_top: Make the top-most edge of particles kinematic
fix_bottom: Make the bottom-most edge of particles kinematic
"""
tri_ke = tri_ke if tri_ke is not None else self.default_tri_ke
tri_ka = tri_ka if tri_ka is not None else self.default_tri_ka
tri_kd = tri_kd if tri_kd is not None else self.default_tri_kd
tri_drag = tri_drag if tri_drag is not None else self.default_tri_drag
tri_lift = tri_lift if tri_lift is not None else self.default_tri_lift

start_vertex = len(self.particle_q)

Expand Down Expand Up @@ -4235,11 +4279,11 @@ def add_soft_mesh(
k_mu: float,
k_lambda: float,
k_damp: float,
tri_ke: float = default_tri_ke,
tri_ka: float = default_tri_ka,
tri_kd: float = default_tri_kd,
tri_drag: float = default_tri_drag,
tri_lift: float = default_tri_lift,
tri_ke: float = None,
tri_ka: float = None,
tri_kd: float = None,
tri_drag: float = None,
tri_lift: float = None,
):
"""Helper to create a tetrahedral model from an input tetrahedral mesh
Expand All @@ -4254,6 +4298,12 @@ def add_soft_mesh(
k_lambda: The second elastic Lame parameter
k_damp: The damping stiffness
"""
tri_ke = tri_ke if tri_ke is not None else self.default_tri_ke
tri_ka = tri_ka if tri_ka is not None else self.default_tri_ka
tri_kd = tri_kd if tri_kd is not None else self.default_tri_kd
tri_drag = tri_drag if tri_drag is not None else self.default_tri_drag
tri_lift = tri_lift if tri_lift is not None else self.default_tri_lift

num_tets = int(len(indices) / 4)

start_vertex = len(self.particle_q)
Expand Down Expand Up @@ -4344,16 +4394,22 @@ def set_ground_plane(
self,
normal=None,
offset=0.0,
ke: float = default_shape_ke,
kd: float = default_shape_kd,
kf: float = default_shape_kf,
mu: float = default_shape_mu,
restitution: float = default_shape_restitution,
ke: float = None,
kd: float = None,
kf: float = None,
mu: float = None,
restitution: float = None,
):
"""
Creates a ground plane for the world. If the normal is not specified,
the up_vector of the ModelBuilder is used.
"""
ke = ke if ke is not None else self.default_shape_ke
kd = kd if kd is not None else self.default_shape_kd
kf = kf if kf is not None else self.default_shape_kf
mu = mu if mu is not None else self.default_shape_mu
restitution = restitution if restitution is not None else self.default_shape_restitution

if normal is None:
normal = self.up_vector
self._ground_params = {
Expand Down

0 comments on commit af3b467

Please sign in to comment.