Skip to content

Commit

Permalink
Speedup contact points reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
qbp758 committed Sep 25, 2023
1 parent 89b9936 commit f4eab32
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 5 deletions.
336 changes: 336 additions & 0 deletions python/profile_speedup_contact_points_reduction.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rainbow.util.timer import Timer
import numpy as np
from itertools import combinations
from numba import cuda
from numba import cuda, jit

import rainbow.cuda.collision_detection.compute_contacts as CUDA_COMPUTE_CONTACTS

Expand Down Expand Up @@ -488,6 +488,31 @@ def _contact_determination(overlaps, engine, stats, debug_on):
stats["contact_determination"] = contact_determination_timer.elapsed
return stats

def _cp_unique_id(cp):
# 这是一个简单的标识生成器,它基于bodyA, bodyB和p来生成唯一的标识
return (cp.bodyA, cp.bodyB, tuple(cp.p))

def _optimize_contact_reduction(engine, stats, debug_on):
if debug_on:
reduction_timer = Timer("contact_point_reduction", 8)
reduction_timer.start()

seen_ids = set()
reduced_list = []

for cp in engine.contact_points:
uid = _cp_unique_id(cp)
if uid not in seen_ids:
reduced_list.append(cp)
seen_ids.add(uid)

engine.contact_points = reduced_list

if debug_on:
reduction_timer.end()
stats["contact_point_reduction"] = reduction_timer.elapsed

return stats

def _contact_reduction(engine, stats, debug_on):
"""
Expand All @@ -505,6 +530,10 @@ def _contact_reduction(engine, stats, debug_on):
:param debug_on: Boolean flag for toggling debug (aka profiling) info on and off.
:return: A dictionary with profiling and timing measurements.
"""

if engine.params.speedup:
return _optimize_contact_reduction(engine, stats, debug_on)

reduction_timer = None
if debug_on:
reduction_timer = Timer("contact_point_reduction", 8)
Expand Down
1 change: 1 addition & 0 deletions python/rainbow/simulators/prox_soft_bodies/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def __init__(self):
)
self.resolution = 64 # The number of grid cells along each axis in the signed distance fields.
self.use_gpu = True # Boolean flag that indicates if we should use GPU computing or not.
self.speedup = True


class Engine:
Expand Down
6 changes: 5 additions & 1 deletion python/soft_body_demo/soft_body_beam_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
def create_scene(material=None, model='SVK', T = 10.0, usd_save_path="soft_body_beam_default.usda", time_step=0.0001):
scene = API.create_engine()
scene.params.time_step = time_step

viewer = VIEWER.Viewer()

create_soft_material(scene, material, model)

scene.materials['soft_mat1'].c = 0.2

create_soft_beam(scene, (12, 3, 3, 4.0, 1.0, 1.0))
create_wall(scene)

Expand Down Expand Up @@ -39,7 +43,7 @@ def run():
]

for material in materials:
create_scene(material=material['material'], model=material['model'], T = 2.0, usd_save_path=f"soft_body_beam_1_{material['model']}.usda", time_step=material['time_step'])
create_scene(material=material['material'], model=material['model'], T = 1.0, usd_save_path=f"soft_body_beam_1_{material['model']}.usda", time_step=material['time_step'])

# create_scene(material='rubber', model='SNH', usd_save_path=f"soft_body_beam_snh.usda")
# create_scene(material='rubber', model='COR', usd_save_path=f"soft_body_beam_cor.usda")
Expand Down
3 changes: 2 additions & 1 deletion python/soft_body_demo/soft_body_sim_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def create_soft_material(engine, name=None, model_name='SVK', friction=0.5):
API.create_surfaces_interaction(engine,'soft_mat1','soft_mat1', friction)

# create the soft beam into a scene
def create_soft_beam(engine, geo, gravity=-10, material='soft_mat1', name='beam'):
def create_soft_beam(engine, geo, gravity=-10, material='soft_mat1', name='beam', y_offset=0.0):
V_beam, T_beam = VM.create_beam(*geo) # geometry
V_beam[:,1] += y_offset
API.create_soft_body(engine, name, V_beam, T_beam)
API.set_type(engine, name, "Free")
API.set_gravity(engine, name, (0,gravity,0))
Expand Down
5 changes: 3 additions & 2 deletions python/soft_body_demo/soft_body_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ def create_scene(material=None, model='SVK', T = 10.0, usd_save_path="soft_body_
viewer = VIEWER.Viewer()

create_soft_material(scene, material, model)
scene.materials['soft_mat1'].c = 0.5

create_soft_beam(scene, (2, 2, 2, 0.5, 0.5, 0.5), name="cube")
create_soft_beam(scene, (20, 2, 10, 4, 0.05, 4), name="surface")
create_soft_beam(scene, (4, 4, 4, 0.5, 0.5, 0.5), name="cube", y_offset=0.275)
create_soft_beam(scene, (10, 2, 10, 4, 0.05, 4), name="surface")

API.create_dirichlet_conditions(scene, 'surface', lambda x: x[0] + 1.9)
API.create_dirichlet_conditions(scene, 'surface', lambda x: 1.9 - x[0])
Expand Down

0 comments on commit f4eab32

Please sign in to comment.