diff --git a/agent_context/MAP.yaml b/agent_context/MAP.yaml index d521df9466..6551743cb7 100644 --- a/agent_context/MAP.yaml +++ b/agent_context/MAP.yaml @@ -11,11 +11,11 @@ topics: aliases: [simulation system, simulation manager, Newton physics backend, motion module, sim motion, camera attachment, 运动能力模块, 仿真系统, 仿真管理器] keywords: [SimulationManager, SimulationManagerCfg, RenderCfg, DenoisingCfg, DenoisingMode, DLSSCfg, NRDCfg, DLSS, NRD, frame_time_delta_ms, explicit physics stepping, simulation_time, arena, DexSim, PhysicsBackendCfg, - DefaultPhysicsCfg, NewtonPhysicsCfg, VisualObject, VisualObjectCfg, VisualMaterial, VisualMaterialCfg, VisualMaterialInst, preview-material, 材质预览, UsdSourceCfg, source_prim_path, add_visual_object, 纯渲染对象, AutoSolverCfg, DexUniSolverCfg, DexUni, runtime control, deformable, particle set, + DefaultPhysicsCfg, NewtonPhysicsCfg, VisualObject, VisualObjectCfg, VisualMaterial, VisualMaterialCfg, VisualMaterialInst, preview-material, 材质预览, UsdSourceCfg, source_prim_path, add_visual_object, 纯渲染对象, DexUniSolverCfg, DexUni, runtime control, deformable, particle set, UsdSourceSnapshot, read_usd_source, LinkMesh, PhysicalCollisionGeometry, capture_physical_collision_geometry, ArticulationJointKinematics, get_parent_joint_chain, asset_physics_mode, joint_drive_props, enable_gravity, - fixed-cadence playback, quaternion, xyzw] + fixed-cadence playback, FluidObjectCfg, FluidSpawnCfg, FluidObject, SPHSolverCfg, revolved_volume, fill_level, 粒子采样, fluid, 流体, quaternion, xyzw] paths: [topics/simulation-system/simulation-system.md] source_of_truth: - embodichain/lab/sim/motion/__init__.py @@ -24,6 +24,7 @@ topics: - embodichain/cli/_visualization.py - embodichain/lab/sim/__init__.py - embodichain/lab/sim/cfg/ + - embodichain/compute/geometry/particles.py - embodichain/lab/sim/physics/ - embodichain/lab/sim/spawn/ - embodichain/lab/sim/material.py @@ -39,7 +40,7 @@ topics: - embodichain/lab/sim/objects/gizmo.py - embodichain/lab/gym/envs/base_env.py watch_paths: [embodichain/lab/sim/sim_manager.py, embodichain/lab/sim/cfg/, embodichain/lab/sim/physics/, - embodichain/lab/sim/spawn/, embodichain/lab/sim/material.py, embodichain/lab/sim/_material_presets.py, embodichain/lab/scripts/preview_material.py, tests/lab/scripts/test_preview_material.py, embodichain/lab/sim/objects/, tests/sim/] + embodichain/lab/sim/spawn/, embodichain/lab/sim/material.py, embodichain/lab/sim/_material_presets.py, embodichain/lab/scripts/preview_material.py, tests/lab/scripts/test_preview_material.py, embodichain/lab/sim/objects/, embodichain/compute/geometry/particles.py, tests/compute/test_particles.py, tests/sim/] related_topics: [env-framework, robot-system, sensor-system, sim-visualization, ik-solvers, motion-planning, atomic-actions, rl-learning, configclass-pattern, robot-workspace] status: active diff --git a/agent_context/topics/atomic-actions/atomic-actions.md b/agent_context/topics/atomic-actions/atomic-actions.md index 174afc8523..1916327ae1 100644 --- a/agent_context/topics/atomic-actions/atomic-actions.md +++ b/agent_context/topics/atomic-actions/atomic-actions.md @@ -208,6 +208,38 @@ Author contact dimensions and material settings before `prepare()`, never by rewriting captured solver arrays. The current values live in `examples/sim/atomic_skill/support.py` and the selected physical components. +Scene-specific DexUni contact options are authored through +`create_tutorial_simulation(newton_solver_options=...)` before model construction. +`add_tutorial_robot` owns optional `newton_gripper_friction` and `arm_damping` +overrides; `scenario.add_dual_tutorial_robot` owns dual-arm friction and optional +mimic stabilization. +Scene modules retain calibrated close targets, fixture TCPs and grippers, including +Handover's Robotiq 2F-140 fixture. Read those example owners for current values; +calibration does not establish generic physical success or solver equivalence. +The coordinated-placement host opts into mimic stabilization and two-way DexUni +feedback, checks measured +lift and release placement, and retains commanded jaw preload when reconciling +measured arm/held-object state. A contact-blocked jaw measurement is not its +holding command. Its mesh fixtures use +the authored Z-up frame; shared pan geometry selects the handle and basin along +local Y. The three original-GLB benchmark suites instead declare +`support_mesh_frame: native_glb`, selecting along local Z in the native Y-up +mesh. Propagate this frame through grasp, target and footprint qualification; +retain each suite's matching asset scale and root pose. Keep these asset-specific +conventions in the scene/geometry owners. +The host caches textured OBJ geometry so native renderers and NoRender load the +same baked asset frame. +Release checks reuse complete projected mesh-footprint coverage and sampled +support-surface gaps. The example rejects negative sampled gaps and requires +an interior-floor witness within 8 mm to reject overhang, penetration, hovering +and rim-spanning outcomes while permitting curved undersides; the target-input +audit retains its separate collision-free-proposal semantics. This is finite +geometric evidence, not a proof of continuous 3D collision freedom or contact load. +Keep the base-to-table height invariant when raising the fixture. +Read held poses without resetting them at the replan boundary. +The launcher preserves an omitted device through simulation configuration so the +selected backend owns its default; explicit device selections retain priority. + ## Semantic integration boundary `semantics` contains: diff --git a/agent_context/topics/simulation-system/articulation-adapters.md b/agent_context/topics/simulation-system/articulation-adapters.md index f555cf4bfa..183a13c652 100644 --- a/agent_context/topics/simulation-system/articulation-adapters.md +++ b/agent_context/topics/simulation-system/articulation-adapters.md @@ -13,6 +13,11 @@ resolves source metadata before its first immutable build. Do not implement initial configuration as finalize-then-rebuild or duplicate descriptor writes inside `Articulation._apply_spawn_config()`. +URDF continuous joints retain unbounded source limits during resolution. Native +Skeleton metadata may expose zero-filled limits; those must not become a locked +joint at the Newton boundary. Caller-authored limits and later config overlays +retain precedence over the source default. + `ArticulationRootPropertiesCfg` separates root intent from link physics. Fixed-base/self-collision settings enter common descriptors, independently of the source overlay policy; explicit `None` preserves USD intent and selects @@ -21,12 +26,26 @@ the native articulation before Direct GPU initialization and initial reset. Iteration counts are configured together because the native setter is atomic. Applying them only during facade binding is too late for captured GPU settings. +`root_props.newton_gravity_compensation` is Newton creation intent applied in +both asset physics modes; omission preserves the source/default policy. It +compensates articulation link weight without removing payload gravity or +contact reactions. DexSim checks the resolved solver's MuJoCo support. +URDF mimic joints on Newton keep the configured follower drives and the +native MuJoCo equality by default, matching a plain DexSim scene; reset starts +followers on their mimic relation. `root_props.newton_mimic_stabilizer=True` +opts in to the Default-parity stabilizer (follower drive = 1 % of the leader, +mirrored follower targets, retuned equality `solref`). + `joint_drive_props` is the sole joint-property entry point; sparse fields retain source values. `drive_type` describes the response and `target_mode` the active command components. Lowering masks inactive gains: none/effort clear both, velocity clears position gain. Newton rejects active acceleration drives; non-MuJoCo position mode is gain-based emulation with zero velocity target. Exact field options and defaults remain in `cfg/articulation.py`. +Newton-specific `joint_drive_props.passive_damping` lowers to passive DOF +damping, separately from the drive's velocity gain. It retains the same +exact-name/regex/control-part matching and remains active for passive or effort +target modes. Applied Newton-only settings are rejected on Default. ## State order and batched writes @@ -122,3 +141,5 @@ FREE-joint synchronization, mesh export and no-op root writes; `tests/sim/objects/test_articulation.py` for topology/FK. Source configuration and root timing are covered by `tests/sim/spawn/test_descriptors.py` and `tests/sim/test_sim_manager.py`. +`tests/sim/test_dexuni_two_way.py` compares contact-limited dynamic fingers +against the same one-way commands, including CUDA Graph and reset. diff --git a/agent_context/topics/simulation-system/deformables.md b/agent_context/topics/simulation-system/deformables.md index 54b37d7e97..b6c7fc026a 100644 --- a/agent_context/topics/simulation-system/deformables.md +++ b/agent_context/topics/simulation-system/deformables.md @@ -30,7 +30,12 @@ order; node selection for a runtime control is not a render vertex selection. For surfaces requiring stable authored node indices, array-backed `MeshCfg` preserves the supplied order; native file importers can reorder vertices. Volume voxelization creates tetrahedral particles whose indices do not match -the source mesh vertices. +the source mesh vertices. When stable volume node ids matter (material lines, +benchmarks), set `VolumeDeformableObjectCfg.tetrahedra` with an array-backed +`shape`: its vertices become the particles in order, meshing is skipped, and +`shape.triangles` is the optional boundary (derived from the tets if omitted). +`set_nodal_state()` writes world `[position, velocity]` nodes, the inverse of +`data.nodal_state_w`, without changing the rest shape. A surface's optional `visual_shape` can have independent indexing and UV seams; `visual_binding_mode` controls mapping from particles. Visual material/UVs @@ -49,6 +54,23 @@ Optional `attrs.contact_material` sets DexUni stiffness, damping and dynamic friction per soft or cloth object; it is separate from element elasticity and replaces removed flat solver-level soft-contact coefficients in examples. +Surface cloth can author a constant signed rest dihedral angle through +`attrs.edge_rest_angle`; omitting it preserves the mesh-inferred rest geometry. +The surface descriptor compiler validates and forwards it to native cloth +physics. This is a rest-shape choice separate from bending stiffness and is not +a volume-surface material field. + +The contact material lowers to the native cloth/soft-body physics descriptor. +Native validation and pair-material mixing remain DexSim-owned. An explicit +block requires the native per-object material API. + +Surface `attrs.pneumatic` optionally lowers a mapping to native +`PneumaticPhysicsDesc`. Closed-surface topology, pressure-law validation and +solver compatibility remain DexSim-owned. A closed cloth mesh alone has no +gas pressure; callers must author this block for an inflatable cavity. Omission +keeps ordinary cloth compatible with older wheels; explicit cavities fail +before declaration if the installed descriptor API cannot represent them. + ## Kinematic node controls Clear the Newton `ACTIVE` bit for all driven nodes in `particle_flags` before diff --git a/agent_context/topics/simulation-system/fluids.md b/agent_context/topics/simulation-system/fluids.md new file mode 100644 index 0000000000..bbd5e0f53a --- /dev/null +++ b/agent_context/topics/simulation-system/fluids.md @@ -0,0 +1,86 @@ +# Initial-fill fluids + +`cfg/fluid.py` owns source, object and render configuration. Fluids use DexUni, +which dispatches DFSPH when the scene contains fluid particles. Optional +numerical settings belong to DexSim `SPHSolverCfg`, nested under +`NewtonPhysicsCfg.solver_cfg.sph_cfg`; omitting them uses native DFSPH defaults. +`SimulationManager.add_fluid_object()` declares through SpawnScene and binds +`objects/fluid.py` in place at `prepare()`. + +The fluid declaration owns density and spacing. Default native SPH values for +`rest_density` and `particle_diameter` are placeholders, equally for mappings +and native config objects; non-default solver values must match the fluid. +DexSim derives the effective solver values from the fluid descriptors. + +## Runtime requirements + +Fluid simulation requires Newton CUDA, a DexSim build exposing +`DexUniSolverCfg.sph_cfg`, no gradients and fixed particle topology. +Multiple environments require a DexSim SPH solver that explicitly advertises +both world isolation and selected-world reset; arena spacing alone does not +establish isolation. The repository's pinned `dexsim_engine==0.6.0rc1` build +provides the DexUni/DFSPH composition and both multi-world capabilities. + +The current native DexUni/DFSPH implementation is a prototype: DFSPH follows +the rigid step with one-way rigid-to-fluid interaction. When rigid bodies are +present, it requires the pure MuJoCo rigid path; static collision shapes do not +require a rigid-body solver. It cannot mix fluids with cloth or soft-body particles. +Online robot pouring, Gym declarations and replay belong to the dependent task +layer; two-way fluid feedback is not implied by DexUni's rigid/soft coupling. + +## State and sampling + +Fluid state uses world-frame `(instances, particles, 3|6)` Torch snapshots. +Writes use Scene particle batches, invalidate captured graphs and reset +supported solver history. The legacy single-world solver rebaselines optional +SDF boundary pose history. Reset restores the immutable initial state or +transforms a saved local template without stepping the world. Explicit state +writes do not refresh density diagnostics until the next SPH step. + +A spawn reference resolves a declared rigid body's initial pose once; it is +not a persistent attachment. Sources include explicit points/NPZ, primitives, +capped Z-axis revolved profiles and closed fill-volume meshes. Profile points +are ordered `(local Z, radius)` pairs. `fill_level` clips the full source Z +extent before sampling; clearance does not inset that free surface. Mesh +sources describe closed cavities, rather than container walls. Point imports +reject volume-only profile/fill-level controls. + +Sampling math belongs to `compute/geometry/particles.py`, independent of lab. +The manager samples once at declaration and retains the local template; +binding and reset do not reopen source files. Particle count is fixed after +preparation, and all fluids share density and spacing. Adding, replacing or +removing rigid assets after preparing a particle scene requires a new scene. + +`FluidObject(cfg, device)` creates an unregistered facade without sampling or +state allocation. The manager resolves the source recipe and reference pose, +then privately initializes the facade's local particle template, initial +rotation and write-invalidation hook. Spawn receives the same sampled points +and resolved pose. Derived caches never become additional config fields; +binding and reset reuse them while the input config remains a source recipe. + +## Rendering and validation + +Native fluids require FastRT (`fast-rt`) or OfflineRT (`rt`). Hybrid's Vulkan +primary surface lacks the OptiX liquid geometry, so the manager rejects Hybrid +before sampling or declaration, including an already initialized Hybrid +process. Use `no-render` for physics only; fluid mode `none` requires it. +Display clipping changes only a render copy. Viser consumes detached particle +point clouds. NoRender needs no optical material. Advanced optical parameters +follow the renderer requirements of the [material system](materials.md). + +`scripts/tutorials/sim/create_fluid.py` demonstrates procedural box sampling, +five primitive container walls, prepare, step, state reads and reset. Pass +`--num-envs` to use multiple independent worlds on a capable SPH build. The +fixed container uses static SDF collision shapes, so DFSPH can collide with it +without running a rigid-body solver. It needs no robot, task configuration or downloaded assets: + +```bash +python scripts/tutorials/sim/create_fluid.py --headless --renderer no-render +``` + +Focused checks are `tests/compute/test_particles.py`, +`tests/sim/test_fluid_cfg.py`, `tests/sim/objects/test_fluid_object.py` and +`tests/visualization/test_scene_exporter.py`. Serial CUDA lifecycle coverage is +`tests/sim/objects/test_fluid_runtime.py`; `test_fluid_visibility.py` checks +camera pixels after stepping and direct state writes. Tutorial checks remain +syntax/CLI smoke checks rather than tutorial import tests. diff --git a/agent_context/topics/simulation-system/lifecycle.md b/agent_context/topics/simulation-system/lifecycle.md index e696ada674..021c2c36c9 100644 --- a/agent_context/topics/simulation-system/lifecycle.md +++ b/agent_context/topics/simulation-system/lifecycle.md @@ -57,8 +57,9 @@ rows before episode initialization events run. Manager reset restores all selected Newton articulations before one complete Scene batch clears drive targets, dynamics, warm-start state, kinematics and -external wrenches. MuJoCo-Warp cannot safely clear a selected world's solver -state with only some of its articulations; excluding a subset is rejected. +external wrenches. MuJoCo-Warp and DexUni cannot safely clear a selected world's +solver state with only some of its articulations; excluding a subset is rejected +before any facade state changes. Other resource exclusions and environment selections remain honored. `destroy(exit_process=False)` queues native cleanup. Flush with @@ -86,11 +87,12 @@ must not reach into `_spawn_scene.builder`. | `register_kinematic_joint_trajectory()` | Declare articulation first; positions `[env, frames, dof]` use public qpos order; optional root poses `[env, frames, 4, 4]` | | `register_kinematic_nodal_trajectory()` | Declare deformable with selected nodes inactive; offsets `[env, samples, selected_nodes, 3]` are relative to finalized initial world positions | | `register_contact_material_schedule()` | Declared rigid object/robot/articulation; piecewise-constant shape friction/stiffness/damping tracks | -| `register_particle_contact_material_schedule()` | Scene-wide particle-versus-rigid contact properties | +| `register_particle_contact_material_schedule()` | Targeted DexUni deformable material via `uid`; omitted UID retains legacy scene-wide particle properties | +| `add_runtime_control()` | Any DexSim `RuntimeControl` without a dedicated method (for example `RealtimeJointTargetControl`); pass one scene-wide instance or a `factory(arena_name, env_index)`; targets use `f"{arena_name}/{uid}"`; returns the registered controls | -All registrations reject finalized scenes. Joint and shape-material controls -remain graph-compatible; nodal trajectories and particle-material schedules -perform host writes and disable CUDA Graph replay. Nodal `fps` enables linear +All registrations reject finalized scenes. Joint, shape-material and targeted DexUni particle-material controls remain +graph-compatible; nodal trajectories and legacy scene-wide particle-material +schedules perform host writes and disable CUDA Graph replay. Nodal `fps` enables linear substep interpolation; without it each substep consumes a sample. Particle indexing rules are in [deformables](deformables.md). diff --git a/agent_context/topics/simulation-system/physics-configuration.md b/agent_context/topics/simulation-system/physics-configuration.md index f3408e77a7..9f539aff33 100644 --- a/agent_context/topics/simulation-system/physics-configuration.md +++ b/agent_context/topics/simulation-system/physics-configuration.md @@ -6,7 +6,7 @@ Return to the [simulation overview](simulation-system.md) for lifecycle entry. ## Backend, solver and device resolution `SimulationManagerCfg.physics_cfg` selects the backend by config type. -`cfg/simulation.py` owns `PhysicsBackendCfg`, `DefaultPhysicsCfg` and +`cfg/physics.py` owns `PhysicsBackendCfg`, `DefaultPhysicsCfg` and `NewtonPhysicsCfg`; do not add another backend string to the manager config. Gym's file-owned selector and composition rules belong to [environment configuration](../env-framework/configuration.md). @@ -25,9 +25,15 @@ Newton `solver_cfg=None` preserves DexSim's scene-aware `DexUniSolverCfg`. DexUni resolves its ownership and contact paths from the complete Spawn scene; the configured solver type is exposed through `NewtonPhysicsBackend.solver_type`. Do not guess a solver-specific robot preset while scene ownership is unresolved. -Gradient mode requires an explicit +Explicit selection uses `dexuni`; `contact_mode="auto"` retains the native +contact-path policy. Expert settings use the native option groups +(`vbd_options`, `mujoco_options`, `collision_options`); unknown solver names +are rejected for mappings and native config objects, and flat DexUni options +are rejected by native validation. +Standalone SPH is not a supported solver family; fluid settings belong under +[DexUni's nested SPH configuration](fluids.md). Gradient mode requires an explicit `semi_implicit` solver. Read exact solver options/defaults in -`cfg/simulation.py` and the installed DexSim config types; do not substitute a +`cfg/physics.py` and the installed DexSim config types; do not substitute a hardcoded fallback when an expected DexSim API is absent. `collision_cfg` owns the external Newton contact pipeline. Its @@ -39,6 +45,21 @@ external-pipeline tuning does not configure internal contact generation. The manual differentiable trajectory requires the external pipeline; see [differentiable environment](../differentiable-env/differentiable-env.md). +DexUni preserves its one-way contact policy unless `coupling="two_way"` is +explicit. Two-way feedback requires dynamically integrated MuJoCo joints and +supports full-surface rigid-soft feedback in current DexSim builds. Native +validation owns compatible contact modes and option combinations. Prescribed kinematic trajectories +cannot respond to contact reactions. Forward solver expert options through +`NewtonPhysicsCfg.solver_cfg`; DexSim owns their validation and dispatch. +The current [DFSPH composition](fluids.md) remains one-way even when DexUni's +rigid/soft coupling options are set to two-way. +Articulation gravity compensation and passive damping are separate creation +settings; see [articulation adapters](articulation-adapters.md). +`async_sync`, `rigid_transform_sync_mode`, `capture_acquire_timeout` and +`capture_wait_log_interval` pass straight to DexSim `NewtonCfg`. Solver +diagnostics (graph status, effective VBD options, contact capacity) are read +from `sim.spawn_result.get_solver_diagnostics()` after `prepare()`. + Capability checks remain explicit: Default owns native rigid constraints and Newton deformables are separate. Contact support is rechecked after Newton solver resolution. DexUni requires per-row `ContactBuffer` quantity validity; diff --git a/agent_context/topics/simulation-system/rendering.md b/agent_context/topics/simulation-system/rendering.md index ab210a44c9..7bdb31f025 100644 --- a/agent_context/topics/simulation-system/rendering.md +++ b/agent_context/topics/simulation-system/rendering.md @@ -103,6 +103,10 @@ The Newton backend step hook suppresses automatic DexSim publication during manager-owned updates to avoid publishing twice. The manager honors explicit `sync_to_renderer=True` even without consumers; `None`/`False` publish only for consumers. Raw DexSim `World.update()` retains its configured automatic policy. +When fluids are present, publication orders Newton and Torch work before the +native CUDA staging copy and waits for that copy before a camera reads the frame. +After an explicit fluid-state write, it first drains prior staged updates so a +busy native double buffer cannot silently discard the newer state. Render-thread recording only consumes already-published state and must never call the blocking physics-to-render bridge. Marker publication while paused uses `capture_visualization(force=True)` without advancing physics. @@ -154,6 +158,10 @@ Configuration tests do not prove GPU/NGX/NRD runtime support, which initializes on a rendered frame. Read exact renderer and algorithm defaults in the config source. +Optical settings use `VisualMaterialCfg.parameters` for direct and shape-owned +materials. Resource definitions and their reset baselines are owned by the +[material system](materials.md). + ## Startup summaries `sim/_startup_summary.py` collects read-only rows. Manager engine output follows diff --git a/agent_context/topics/simulation-system/simulation-system.md b/agent_context/topics/simulation-system/simulation-system.md index 57ffcd5f80..92606297e4 100644 --- a/agent_context/topics/simulation-system/simulation-system.md +++ b/agent_context/topics/simulation-system/simulation-system.md @@ -156,6 +156,7 @@ tests need non-symmetric rotations to expose component-order mistakes. | Preparation, topology changes, reset, cleanup, controls | [Lifecycle](lifecycle.md); manager and `spawn/scene.py` | | Backend/device selection, source overlays, collision/mass policy | [Physics configuration](physics-configuration.md); `cfg/`, `spawn/descriptors.py` | | Joint state order, drive lowering, mimic coupling, native adaptation | [Articulation and batch adapters](articulation-adapters.md); `objects/backends/`, `objects/articulation.py` | +| Initial-fill fluid configuration, state and rendering | [Fluids](fluids.md); `cfg/fluid.py`, `objects/fluid.py` | | Volume/surface state, topology and kinematic nodes | [Deformables](deformables.md); `cfg/deformable.py`, `objects/deformable/` | | Camera synchronization, native window/offscreen denoising, startup readiness | [Rendering and diagnostics](rendering.md); manager, `cfg/simulation.py`, `_startup_summary.py` | | Robot definition, control parts, robot presets | [Robot system](../robot-system/robot-system.md) | diff --git a/design/newton-backend-design.md b/design/newton-backend-design.md index 4fe4c84412..86a59c16b6 100644 --- a/design/newton-backend-design.md +++ b/design/newton-backend-design.md @@ -282,8 +282,9 @@ mixed deformable tasks, destroy/recreate, and steady-state control throughput. ### Object Groups and Deformables - Maintain Newton rigid-object-group parity through the Scene rigid-body batch. -- Expand mixed rigid/articulation/deformable qualification across AutoSolver - outcomes, especially DexUni's rigid-contact limitation. +- Expand mixed rigid/articulation/deformable qualification across DexUni's + automatic MuJoCo, VBD, and coupled paths, including per-row contact-force + availability. ### Articulation / Robot @@ -364,7 +365,7 @@ Remaining: 7. Add rigid-only Newton gym smoke tests. 10. Add mixed-scene deformable qualification for the supported volume/surface - object APIs and their AutoSolver outcomes. + object APIs and DexUni's scene-based dispatch. 11. Newton-native per-link contact params for articulations (after dexsim exposes a per-link shape-material setter). 12. Full migration off legacy `PhysicalAttr` to dexsim's spawn descriptors diff --git a/docker/Dockerfile b/docker/Dockerfile index 63329b0539..4de8979b1a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -25,9 +25,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ libdbus-1-3 libnss3 libasound2 fonts-dejavu-core \ && rm -rf /var/lib/apt/lists/* COPY docker/locks/conda-*.txt /opt/embodichain/locks/ -RUN curl -fL --retry 3 \ +RUN (curl -fL --retry 3 \ "https://github.com/conda-forge/miniforge/releases/download/${MINIFORGE_VERSION}/Miniforge3-${MINIFORGE_VERSION}-Linux-x86_64.sh" \ -o /tmp/miniforge.sh \ + || curl -fL --retry 3 \ + "https://github.com/conda-forge/miniforge/releases/download/${MINIFORGE_VERSION}/Miniforge3-Linux-x86_64.sh" \ + -o /tmp/miniforge.sh) \ && echo "${MINIFORGE_SHA256} /tmp/miniforge.sh" | sha256sum -c - \ && bash /tmp/miniforge.sh -b -p /opt/conda && rm /tmp/miniforge.sh \ && /opt/conda/bin/python -m pip install --no-cache-dir uv==0.12.5 \ diff --git a/docker/README.md b/docker/README.md index 206ac48dbd..6444a089fc 100644 --- a/docker/README.md +++ b/docker/README.md @@ -11,6 +11,12 @@ profiles introduced by [PR #911](https://github.com/DexForce/EmbodiChain/pull/91 RigidFormer extra dependencies and model weights are not preinstalled. Models, task assets and remote service credentials remain runtime inputs. +This checkout's profiles select DexSim 0.6.0rc1 and Newton 1.7.0. The published +tags and image digests recorded in [VALIDATION.md](VALIDATION.md) describe the +earlier DexSim 0.5.1rc1 runtime. Build and qualify a source-commit candidate for +the current profiles before promoting a replacement; the historical image +qualification does not validate the new dependency set. + ## Install and run Use a Linux amd64 host with an NVIDIA driver **≥ 580.00**, Docker and NVIDIA @@ -82,6 +88,9 @@ see [VALIDATION.md](VALIDATION.md) for digests and CI evidence. Run on a Linux amd64 Docker host with NVIDIA Container Toolkit, an appropriate NVIDIA driver, and access to PyPI, PyTorch's `cu128` index, conda-forge and the public DexSim wheel server (`pyp.open3dv.site:2345`). BuildKit is required. +The Miniforge installer uses a fixed release and SHA256. If its versioned +download name fails, the build retries the official alias within that same +release and checks the same SHA256 before execution. ```bash bash docker/build.sh --verify @@ -193,7 +202,8 @@ free to install their own Torch/codec profile. ## CI rollout and rollback CI defaults to the published internal `v4-py311-py312` image -and activates `py312`. The image reference and activation command are explicit +and explicitly activates `py311`. The image's interactive default remains +`py312`. The image reference and activation command are explicit in the existing workflows/shared action; the existing job graph, triggers, test selection and installation commands are preserved. The SDK compatibility matrix selects Python 3.11 and 3.12 independently of this default. diff --git a/docker/VALIDATION.md b/docker/VALIDATION.md index e623327e0b..6b2dbfe82b 100644 --- a/docker/VALIDATION.md +++ b/docker/VALIDATION.md @@ -1,6 +1,9 @@ -# Published container images +# Container qualification and published images -> This checkout requires Python 3.11 or newer; the main CI uses Python 3.12. +> Historical qualification below includes Python 3.10 results from an earlier +> dependency profile. This checkout requires Python 3.11 or newer; +> the main CI explicitly activates Python 3.11 in the dual-profile image; +> the public image and the internal image's interactive default use Python 3.12. > The current Docker locks select DexSim 0.6.0rc1; the published image digests > and qualification runs below describe the earlier DexSim 0.5.1rc1 images. @@ -61,8 +64,8 @@ and traced ONNX CUDA checks also passed. Fresh installation exposed incompatible OpenCV Python/native files from the two variants required by DexSim and LeRobot. Both now select 4.12.0.88 on Python -3.11 and 4.13.0.92 on Python 3.12. The normal CPU resolver succeeds for both -supported SDK matrix versions; qualification also checks the imported `cv2` version. +3.10/3.11 and 4.13.0.92 on Python 3.12. The normal CPU resolver succeeds for all +three SDK matrix versions; qualification also checks the imported `cv2` version. DexSim SHA256 pins were checked against the published index and all **1,926 payload files in each wheel** matched the previously qualified installation. @@ -91,8 +94,8 @@ succeeded after both CI workflows passed at implementation commit respectively. Formatting, API coverage, documentation tests, the Sphinx build and the final test gate passed. - [SDK compatibility](https://github.com/DexForce/EmbodiChain/actions/runs/41471747895): - all seven jobs passed, including 141 SDK cases each on Python 3.11 and 3.12, - offline-tool jobs and the standalone exporter. + all seven jobs passed, including 141 SDK cases each on Python 3.10, 3.11 and + 3.12, all three offline-tool jobs and the standalone exporter. Promotion copied the existing manifests without rebuilding the images. Their bundled wheel and source labels remain at `c42e8a0b0857`; the CI runs above @@ -110,6 +113,99 @@ Temporary registry credentials were removed from the publishing runner, the GitHub secret was deleted and its absence verified, and local Docker login credentials were deleted. -The PR's main, dataset and documentation workflows now use the formal internal -tag and retain explicit `py311` activation. The current SDK matrix selects -Python 3.11 and 3.12 independently. +At that historical promotion, the main, dataset and documentation workflows +used the formal internal tag with explicit `py311` activation, and the SDK +matrix selected Python 3.10/3.11/3.12. The current checkout's SDK matrix selects +Python 3.11 and 3.12; its main test job still explicitly activates `py311`. + +## DexSim 0.6.0rc1 build and dependency follow-up — 2026-11-09 + +Runtime and Dockerfile revision: +`c1af874bae7f0a6bc53691a8b2b9d2e063d80e57`. +Build host: Linux amd64, Docker 29.6.0, BuildKit 0.34.2. The Dockerfile retains +the CUDA 12.8 Ubuntu 22.04 base-image digest recorded in source. + +### Python distribution checks + +The current package requires Python `>=3.11`. The installation guide now +lists Python 3.11/3.12, and distinguishes the `py312` interactive image default +from the main workflow's explicit `py311` activation. + +The published DexSim 0.6.0rc1 wheels resolved successfully with isolated pip +dry-runs targeting CPython 3.11 and 3.12 on `manylinux_2_31_x86_64`. +Downloaded hashes matched both the profiles and locks: + +| ABI | SHA256 | +| --- | --- | +| cp311 | `f3cef56bd987b3166b871d7d9f29b2f2d6ceb274cf47ca9a598c51d6a6785718` | +| cp312 | `f84c0f1763689b6d57f63308b417932237325666c20d1e3379bafbb7cdad7076` | + +Both published wheel metadata records require `newton[sim]==1.7.0` and +`warp-lang==1.17.0` on Linux. A newly built EmbodiChain 0.3.2 wheel accepted +the Python 3.11/3.12 targets and rejected Python 3.10 through `Requires-Python`. +The wheel's dependency metadata also passed the PyPI direct-URL check. + +Both profile inputs were re-resolved with uv 0.12.5 against the checked-in +constraints, including the `gensim` and `policy-deploy` extras. The 309 py311 +and 298 py312 requirements matched their lock files without dependency drift. + +### Actual container builds + +Both Dockerfile targets completed, including wheel installation with +`--no-deps` and the subsequent `uv pip check`. Final checks reported: + +| Image/environment | Compatible installed packages | +| --- | ---: | +| public / py312 | 299 | +| public / isolated blender311 | 28 | +| internal / py311, including test tools | 312 | +| internal / py312, including test tools | 301 | + +Local OCI manifest digests: + +| Target | Digest | +| --- | --- | +| public | `sha256:75c8cb268415817394a012e1de2543da6017cec53cba44fafc0d5e2cdb538010` | +| internal | `sha256:0fd4288482984fa5e04a668b24b3296c5d28a2d31f7b4c715734c054e78afd9d` | + +Both exported OCI configurations were checked against their config digests, +the runtime revision label and the `EMBODICHAIN_ENV=py312` default. +BuildKit cache and OCI exports used a dedicated secondary-disk directory; +the existing Docker builder remained selected. + +The first build reached the Miniforge download and failed on an upstream +HTTP 504. The Dockerfile now retries the official alias within the same pinned +26.9.4-1 release. Both paths retain the original installer SHA256 check +(`571b410272cc434f28bc6c5c49ca8ffd4a4d1bcba9f57f1b6baccdb46446cfcc`). +The successful builds used that fallback. An interrupted internal build was +resumed from its completed dependency cache before the final successful export. + +The build procedure used a dedicated `docker-container` Buildx builder: + +```bash +for target in public internal; do + docker buildx build --builder pr829-validation --platform linux/amd64 \ + --network host --allow network.host --progress plain \ + --file docker/Dockerfile --target "$target" \ + --build-arg SOURCE_REVISION=c1af874bae7f0a6bc53691a8b2b9d2e063d80e57 \ + --output "type=oci,dest=$target.oci.tar" . +done +``` + +Release/workflow regressions passed 46 tests. The Sphinx dummy build completed +with existing repository diagnostics; the updated installation page retained +no system messages and included the corrected version matrix and image-scope +note. Black 26.10.1 and API documentation coverage (4440/4440) passed. + +This follow-up qualifies distribution availability, resolution, container +builds and installed dependencies. It does not rerun the full GPU, media and +Blender behavior checks in `docker/validate.sh`. The candidates were exported +locally; published image tags and the historical native qualification above +were not promoted or reused as evidence for DexSim 0.6.0rc1. + +The concurrent fluid update `e14fc4e78d4074ec448b4042fc09ea926bffd179` was +preserved during integration. Its dependency-relevant inputs (`pyproject.toml`, +`VERSION`, `setup.py`, Dockerfile, installer, profiles and locks) are unchanged +from the qualified snapshot. Post-integration checks passed 46 regressions, +Black (2312 files unchanged), context validation and API coverage (4449/4449). +The container results above do not qualify the added fluid runtime behavior. diff --git a/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst b/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst index 6cfdcaa858..5b71f8a0e6 100644 --- a/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst +++ b/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst @@ -164,6 +164,14 @@ For manager-owned Newton steps, ``sync_to_renderer=None`` or ``False`` publishes only for visual consumers; ``True`` additionally requests publication on steps without consumers. +DexUni accepts ``coupling="two_way"`` with ``joint_mode="dynamic"`` to feed +contact reactions back to articulations. Keep ``collision_cfg=None`` when +DexUni owns collision detection. ``coupling_options`` configures the native +feedback policy. Full-surface rigid-soft feedback is supported; native validation +owns the compatible contact modes and option combinations. +Articulation gravity compensation and passive joint damping are configured +separately below. + .. currentmodule:: embodichain.lab.sim.cfg.physics .. autosummary:: @@ -177,6 +185,41 @@ without consumers. physics_backend_from_cfg validate_physics_cfg +Articulation configuration module +--------------------------------- + +Articulation types live in ``embodichain.lab.sim.cfg.articulation`` and remain +available from the ``embodichain.lab.sim.cfg`` facade. Root creation settings +are independent of sparse link and joint physics overlays. +``root_props.newton_gravity_compensation`` compensates articulation link +weight on MuJoCo-backed Newton solvers while retaining payload gravity and +contact reactions. Omission preserves source intent. + +``NewtonJointDrivePropertiesCfg.passive_damping`` selects passive DOF damping +through the same exact-name, regex and control-part rules as the drive gains. +It is independent of the drive velocity gain ``damping`` and stays active for +passive and effort target modes. In dictionaries, select this subtype with +``joint_drive_props.backend="newton"``. Both settings require Newton. + +.. currentmodule:: embodichain.lab.sim.cfg.articulation + +.. autosummary:: + + ArticulationRootPropertiesCfg + LinkPhysicsOverrideCfg + link_attrs_from_dict + JointDrivePropertiesCfg + NewtonJointDrivePropertiesCfg + ArticulationCfg + +.. autoclass:: ArticulationRootPropertiesCfg + :members: + :no-index: + +.. autoclass:: NewtonJointDrivePropertiesCfg + :members: + :no-index: + Compatibility facade -------------------- diff --git a/docs/source/api_reference/embodichain/embodichain.lab.sim.sensors.rst b/docs/source/api_reference/embodichain/embodichain.lab.sim.sensors.rst index 5514d4018b..19325eee14 100644 --- a/docs/source/api_reference/embodichain/embodichain.lab.sim.sensors.rst +++ b/docs/source/api_reference/embodichain/embodichain.lab.sim.sensors.rst @@ -120,6 +120,17 @@ sparse Warp kernels. The ``dropped_contacts`` diagnostic sums query loss and scatter overflow across the current control interval; standalone updates without histories or sampling observers report the most recent sample. +Newton force conversion uses the final internal solver substep's duration, +while history clocks advance by the full World-step duration. Standalone +``ContactHistory.update`` accepts an explicit ``impulse_dt`` for this conversion. +Physics-reset-invalidated reports yield invalid observation rows until a new +solve; a sensor-only reset can still reread a valid current report. + +Contact sampling through ``update`` or ``update_physics_step`` must run eagerly +outside caller-owned CUDA Graph capture. Sample before each replay and establish +stream dependencies before consuming the stable ``get_data()`` buffers in the +graph. The sensor's internal reduction graph remains supported. + ``latest_sample_dropped_contacts`` reports the latest fetch's query and scatter loss even when observers retain interval counters. Evidence sampling rejects either diagnostic and takes their maximum to avoid double counting. Full reset @@ -139,8 +150,8 @@ adds no observer snapshots or callbacks. Begin and selective-reset notifications follow the sensor's own history/counter updates; before/after tokens surround one physics-step sample. A prior raw fetch in that same step is reused; repeated substep calls do not repeat callbacks. Observation callbacks run outside the -internal CUDA capture, and external capture with observers is rejected before -callbacks begin. The observer owns retention limits and diagnostics and must not +internal CUDA capture, and all sampling in external capture is rejected before +native queries or callbacks begin. The observer owns retention limits and diagnostics and must not step physics or issue commands. .. currentmodule:: embodichain.lab.sim.sensors.contact.sensor diff --git a/docs/source/api_reference/public_api.rst b/docs/source/api_reference/public_api.rst index c43ae7358f..c54ffa059a 100644 --- a/docs/source/api_reference/public_api.rst +++ b/docs/source/api_reference/public_api.rst @@ -1,5 +1,6 @@ Public API Supplement -============== +===================== + .. This page is maintained by the update-api-docs agent skill. docs/scripts/check_api_docs.py reads it but never writes it. @@ -32,6 +33,17 @@ Offline job preflight, run, resume and verified inspection commands. main +embodichain.compute.geometry.particles +-------------------------------------- + +Deterministic lattice sampling for primitive, revolved and closed mesh fill volumes. + +.. autosummary:: + + embodichain.compute.geometry.particles.sample_volume_particles + +.. autofunction:: embodichain.compute.geometry.particles.sample_volume_particles + embodichain.compute.geometry.shading ------------------------------------ @@ -1821,6 +1833,23 @@ embodichain.lab.gym.envs.embodied_env EmbodiedEnvCfg EmbodiedEnv +embodichain.lab.sim.cfg.fluid +----------------------------- + +Fixed-count fluid sources, physical properties and native particle or surface display. + +.. autosummary:: + + embodichain.lab.sim.cfg.fluid.FluidSpawnCfg + embodichain.lab.sim.cfg.fluid.FluidRenderCfg + embodichain.lab.sim.cfg.fluid.FluidObjectCfg + +.. autoclass:: embodichain.lab.sim.cfg.fluid.FluidSpawnCfg + +.. autoclass:: embodichain.lab.sim.cfg.fluid.FluidRenderCfg + +.. autoclass:: embodichain.lab.sim.cfg.fluid.FluidObjectCfg + embodichain.lab.sim.motion.expansion.session_coverage ----------------------------------------------------- @@ -1859,6 +1888,20 @@ separate checker does not own those attachments. CuroboQuerySnapshot curobo_motion_generator +embodichain.lab.sim.objects.fluid +--------------------------------- + +Scene-owned fluid state snapshots, explicit writes and initial-state restoration. + +.. autosummary:: + + embodichain.lab.sim.objects.fluid.FluidObject + embodichain.lab.sim.objects.fluid.FluidObjectData + +.. autoclass:: embodichain.lab.sim.objects.fluid.FluidObject + +.. autoclass:: embodichain.lab.sim.objects.fluid.FluidObjectData + embodichain.lab.sim.scene.collision_sources ------------------------------------------- diff --git a/docs/source/guides/configuration.md b/docs/source/guides/configuration.md index dbfe9efae1..fd060cfca2 100644 --- a/docs/source/guides/configuration.md +++ b/docs/source/guides/configuration.md @@ -295,12 +295,14 @@ physics_config: gravity: [0.0, 0.0, -9.81] num_substeps: 10 solver_cfg: - solver_type: mujoco_warp + solver_type: dexuni + contact_mode: auto use_cuda_graph: true ``` -Omit `solver_cfg` to let Newton's AutoSolver select from the complete scene at -`prepare()`. For componentized tasks, place these backend declarations in +Omit `solver_cfg` to retain the same DexUni defaults: scene-based ownership +selection at `prepare()` and `contact_mode="auto"`. For componentized tasks, +place these backend declarations in `envs/default.yaml` and `envs/newton.yaml`, then point the deployment's `environment.default` and `environment.newton` entries at those files. Do not repeat `physics` or `physics_config` in the deployment. Keep the diff --git a/docs/source/overview/sim/sensors/contact_sensor.md b/docs/source/overview/sim/sensors/contact_sensor.md index bfdef8c72b..194ecc82b8 100644 --- a/docs/source/overview/sim/sensors/contact_sensor.md +++ b/docs/source/overview/sim/sensors/contact_sensor.md @@ -139,7 +139,10 @@ may be left over from an earlier update. - **`filter_by_user_ids(item_user_ids, env_ids=None)`**: Filter contact report by contact actor IDs. The method name is retained for compatibility. Optionally filter by specific environment IDs. - **`set_contact_point_visibility(visible, rgba, point_size, env_ids=None)`**: Enable/disable visualization of contact points with customizable color and size. Optionally visualize only specific environments. -Newton MuJoCo-Warp exposes contact forces, so both impulse fields are available. Other supported Newton rigid solvers currently expose contact geometry with zero-valued impulse fields. MuJoCo CPU mode does not expose device contact buffers, and DexUni does not currently publish rigid contacts through `ContactQuery`; those modes are therefore unsupported by this sensor. +Newton MuJoCo-Warp and DexUni's MuJoCo-owned rows expose normal and friction +impulses. DexUni can mix these with VBD geometry-only rows; inspect +`impulse_valid` and `friction_valid` before interpreting forces. MuJoCo CPU mode +does not expose device contact buffers. Default Direct GPU reports static counterparts with actor ID `-1` because its raw contact buffer does not expose their object identity. To monitor a dynamic body or articulation link against arbitrary static geometry, select the dynamic/link object and set `filter_need_both_actor=False`. Default CPU and Newton can identify registered static shapes. @@ -159,6 +162,46 @@ owns its snapshot and query graphs; the sensor consumes the current query buffer and its device-resident count. Results are ordered onto the caller's Torch stream. CPU sampling uses the same reductions without capture. +Newton reports impulses from the final internal solver substep. History force +conversion uses the actual World-step duration divided by the active backend's +`num_substeps`; contact and air-time clocks use the full World-step duration. +Standalone `ContactHistory.update(data, dt, impulse_dt=...)` accepts both durations. + +### Caller-owned CUDA Graphs + +Call `contact_sensor.update()` or `update_physics_step(dt)` outside caller-owned +CUDA Graph capture. These methods reject capture before querying DexSim, whose +native snapshots may wait on events from uncaptured work. The sensor's internal +reduction graph above remains supported. + +Capture computations that consume the preallocated `get_data()` tensors. Before +each replay, advance physics, sample eagerly and order the graph's stream after +the sampling stream. Replay alone neither refreshes contacts nor advances history. + +```python +import torch + +contact_sensor.update() # Eager warmup after a completed physics step. +stream = torch.cuda.Stream(device=contact_sensor.device) +load = torch.empty_like(contact_sensor.get_data()["impulse"]) +stream.wait_stream(torch.cuda.current_stream(contact_sensor.device)) +graph = torch.cuda.CUDAGraph() +with torch.cuda.graph(graph, stream=stream): + data = contact_sensor.get_data() + load.copy_(torch.where(data["is_valid"], data["impulse"], 0.0)) + +sim.update(step=1) +contact_sensor.update() # Refresh the same buffers before every replay. +stream.wait_stream(torch.cuda.current_stream(contact_sensor.device)) +with torch.cuda.stream(stream): + graph.replay() +``` + +After a physics reset invalidates the solved report, observation reads return +invalid rows until a new solve. Resetting only the sensor buffers still permits +a fresh read of an existing valid report. Values outside valid rows remain +unspecified; a reset-time empty observation is not measured absence of contact. + The physics loop, simulation clock and rendering callbacks remain outside the sensor graph. Window, offscreen and browser visualization retain their existing update order. diff --git a/docs/source/overview/sim/sim_manager/physics/newton.md b/docs/source/overview/sim/sim_manager/physics/newton.md index 663c182fc1..03caf44a91 100644 --- a/docs/source/overview/sim/sim_manager/physics/newton.md +++ b/docs/source/overview/sim/sim_manager/physics/newton.md @@ -16,10 +16,10 @@ mixed scene class and is the main Newton path documented by EmbodiChain. ## DexSim Unified Coupled Solver For Embodied AI -DexUni supports scenes that combine articulated robots with cloth or volume -soft bodies. It is selected automatically when a finalized scene contains both -an articulation and a supported deformable, or explicitly with -`solver_type="dexuni"`. +DexUni is the default Newton solver configuration. It supports rigid scenes +and scenes that combine articulated robots with cloth or volume soft bodies. +Leave `solver_cfg` unset to retain its defaults, or select +`solver_type="dexuni"` explicitly. ```python from embodichain.lab.sim import SimulationManagerCfg @@ -32,7 +32,8 @@ sim_config = SimulationManagerCfg( num_substeps=10, solver_cfg={ "solver_type": "dexuni", - "iterations": 8, + "contact_mode": "auto", + "vbd_options": {"iterations": 8}, }, # DexUni owns particle-shape collision generation. collision_cfg=None, @@ -44,24 +45,26 @@ DexUni is a DexSim integration configuration, not another native solver class in Newton's public solver list. Its capabilities and current EmbodiChain limitations are documented in {doc}`newton/dexuni`. -## Automatic solver selection +## DexUni automatic dispatch -Leaving `solver_cfg=None` preserves DexSim's scene-aware `AutoSolverCfg`. -DexSim resolves it when {meth}`SimulationManager.prepare` finalizes the complete -Spawn scene: +Leaving `solver_cfg=None` preserves DexSim's `DexUniSolverCfg`. DexUni resolves +ownership and its internal execution path when {meth}`SimulationManager.prepare` +finalizes the complete Spawn scene. Unset ownership selectors retain scene-based +partitioning, while `contact_mode="auto"` selects the native contact path. -| Finalized EmbodiChain scene | Resolved configuration | Solver type | -| :--- | :--- | :--- | -| Independent rigid bodies only | `XPBDSolverCfg` | `xpbd` | -| One or more articulations, without deformables | `MJWarpSolverCfg` | `mujoco_warp` | -| Cloth or volume soft bodies, without articulations | `VBDSolverCfg` | `vbd` | -| Articulations together with cloth or volume soft bodies | `DexUniSolverCfg` | `dexuni` | +| Finalized scene under default ownership | Internal execution | +| :--- | :--- | +| Independent rigid bodies only | MuJoCo branch | +| Articulations with no VBD-owned dynamic bodies or deformables | MuJoCo branch for dynamic joints | +| Deformables with no MuJoCo-owned joints | VBD branch | +| Articulations together with VBD-owned rigid bodies or deformables | Mixed execution selected from ownership, joint mode and contact policy | Add every initial robot and object before `prepare()` so selection sees the -complete scene. An explicit `{"solver_type": "auto"}` or -`{"class_type": "AutoSolverCfg"}` mapping is equivalent to leaving -`solver_cfg` unset. After preparation, inspect `sim.physics.solver_type`; do -not treat `physics: newton` alone as a solver qualification. +complete scene. An explicit +`{"solver_type": "dexuni", "contact_mode": "auto"}` mapping preserves this +automatic policy. `sim.physics.solver_type` remains `"dexuni"`; inspect +`sim.spawn_result.get_solver_diagnostics()` after preparation for the resolved +path. Explicit ownership selectors and kinematic control can change dispatch. For native solver feature comparisons, joint support, contact-material support, and algorithm details, use the @@ -72,11 +75,11 @@ DexSim's scene routing as exposed by this EmbodiChain integration. ## Explicit solver selection Choose a concrete solver when an algorithm or solver-specific parameter must be -fixed. EmbodiChain mapping configs recognize `auto`, `dexuni`, `mujoco_warp` +fixed. EmbodiChain mapping configs recognize `dexuni`, `mujoco_warp` (or `mjwarp`), `xpbd`, `semi_implicit`, `featherstone`, and `vbd`. A DexSim `NewtonSolverCfg` object can also be assigned directly. -AutoSolver does not select a differentiable solver. `requires_grad=True` +DexUni's default dispatch does not select a differentiable solver. `requires_grad=True` therefore requires an explicit `semi_implicit` configuration. See {doc}`newton/runtime_qualification` before treating a successfully decoded configuration as a supported runtime path. @@ -93,7 +96,7 @@ such as `gpu_memory`, `bounce_threshold`, and `enable_ccd` do not belong here. | `device` | `"cuda:0"` | Compute device; explicit CPU execution remains solver/asset dependent. | | `gravity` | `[0.0, 0.0, -9.81]` | World-frame acceleration in m/s². | | `num_substeps` | `10` | Solver substeps per physics step; interval is `physics_dt / num_substeps`. | -| `solver_cfg` | `None` | Preserve DexSim AutoSolver; a mapping or native config fixes a solver. | +| `solver_cfg` | `None` | Preserve native DexUni defaults; mappings or native configs select a solver family and its options. | | `collision_cfg` | `NewtonCollisionPipelineCfg()` | External Newton collision pipeline. Set `None` when the selected path owns collision generation, including DexUni. | | `requires_grad` | `False` | Enable the current differentiable integration; requires explicit `semi_implicit`. | | `use_cuda_graph` | `True` | Request graph capture when supported; unavailable on CPU and disabled in gradient mode. | diff --git a/docs/source/overview/sim/sim_manager/physics/newton/dexuni.md b/docs/source/overview/sim/sim_manager/physics/newton/dexuni.md index 3607c3bff6..dc53b1b797 100644 --- a/docs/source/overview/sim/sim_manager/physics/newton/dexuni.md +++ b/docs/source/overview/sim/sim_manager/physics/newton/dexuni.md @@ -26,30 +26,30 @@ Newton-native solver details, use the | Cable | Unsupported in the current version | Use DexUni for tasks in which Articulated Objects manipulate cloth, garments, -cushions, or other supported deformable assets. If a scene has only deformables -or only rigid bodies, let AutoSolver select the simpler matching solver unless -the task needs an explicit choice. +cushions, or other supported deformable assets. The same default configuration +also handles rigid-only and deformable-only scenes through its internal +scene-based MuJoCo/VBD dispatch. The current version does not support fluid or cable simulation. ## Minimal configuration -AutoSolver selects DexUni when the complete scene contains both an articulation -and a supported deformable. To keep the choice explicit: +`NewtonPhysicsCfg()` retains the native DexUni defaults: automatic ownership +selection and `contact_mode="auto"`. To keep the policy explicit: ```python from embodichain.lab.sim.cfg import NewtonPhysicsCfg physics_cfg = NewtonPhysicsCfg( device="cuda:0", - solver_cfg={"solver_type": "dexuni"}, + solver_cfg={"solver_type": "dexuni", "contact_mode": "auto"}, collision_cfg=None, ) ``` Add all Articulated Objects and deformables before -{meth}`SimulationManager.prepare`. DexUni requires GPU execution and -`requires_grad=False`. Set `collision_cfg=None` because this solver owns the +{meth}`SimulationManager.prepare`. Deformable scenes require CUDA execution and +`requires_grad=False`. Set `collision_cfg=None` because DexUni owns the robot/deformable contact path it uses. For prescribed motion, register the complete joint trajectory before @@ -68,16 +68,20 @@ public joint order. ## Current limitations ```{warning} -The current DexUni path does not solve or publish rigid-rigid or rigid-ground -contacts. `ContactSensor` is therefore unsupported on this path. +Rigid-contact quantities depend on DexUni's selected path. Pure MuJoCo can +publish measured impulses on CUDA; VBD-owned rows can provide geometry only, +and mixed reports can contain both. Honor `is_valid`, `impulse_valid` and +`friction_valid` per row. CPU MuJoCo force-reporting paths do not support +`ContactSensor`. ``` -Full capability coverage, including fluid, cable, and the rigid-contact -limitations above, will be completed in the next release. +Fluid and cable asset APIs are not exposed by this EmbodiChain integration. Qualify any task that mixes deformables with loose rigid objects against its required contact pairs. See {doc}`runtime_qualification` for the complete -solver/device matrix and validation workflow. +solver/device matrix and validation workflow. The outer solver type remains +`"dexuni"`; use `sim.spawn_result.get_solver_diagnostics()` after preparation +to inspect native dispatch. ## Examples and further reading diff --git a/docs/source/overview/sim/sim_manager/physics/newton/runtime_qualification.md b/docs/source/overview/sim/sim_manager/physics/newton/runtime_qualification.md index 8e86277e8b..825d88548d 100644 --- a/docs/source/overview/sim/sim_manager/physics/newton/runtime_qualification.md +++ b/docs/source/overview/sim/sim_manager/physics/newton/runtime_qualification.md @@ -19,7 +19,9 @@ feature does not imply that every asset or operation exposes it here. | MuJoCo-Warp, CPU | Supported | No | Unsupported | CPU contact buffers are not exposed to the sensor. | | XPBD, CUDA | Supported | Scene-dependent | Geometry-only where available | Validate the exact joints, assets, and task. | | VBD, CUDA | Limited by scene | Volume/surface | Geometry-only where available | Deformable-focused path. | -| DexUni, CUDA | Coupled articulation/deformable stepping | Volume/surface | Unsupported | No rigid-rigid or rigid-ground solve/publication on the current path. | +| DexUni, CUDA, pure MuJoCo path | Supported | No deformables in this branch | Geometry + impulse | Rigid scene routed internally to MuJoCo. | +| DexUni, CUDA, mixed path | Coupled articulation/deformable stepping | Volume/surface | Per-row geometry/impulse availability | Honor native quantity-validity masks; qualify required contact pairs. | +| DexUni, CUDA, VBD-owned contacts | Scene-dependent | Volume/surface | Geometry-only | Geometry does not establish measured force. | | Semi-implicit, CUDA, gradients | Not stepped by `DifferentiableEnv` | Unsupported | Unsupported | Current integration is a kinematics bridge only. | The matrix is an admission guide, not a substitute for a task-level rollout. @@ -28,12 +30,14 @@ controls can narrow support further. ## Resolve before qualifying -AutoSolver is unresolved until the complete Spawn scene is finalized. Query the -concrete value only after {meth}`SimulationManager.prepare`: +DexUni resolves scene ownership and its internal execution path after the +complete Spawn scene is finalized. The outer solver type remains `"dexuni"`; +query native diagnostics after {meth}`SimulationManager.prepare`: ```python sim.prepare() print(sim.physics.solver_type) +print(sim.spawn_result.get_solver_diagnostics()) print(sim.device) ``` @@ -61,7 +65,7 @@ Volume soft bodies and surface cloth use `VolumeDeformableObjectCfg` and `SurfaceDeformableObjectCfg`. The current integration requires: - CUDA execution; -- `auto`, `dexuni`, `xpbd`, `semi_implicit`, or `vbd` as the configured solver; +- `dexuni`, `xpbd`, `semi_implicit`, or `vbd` as the configured solver; - `requires_grad=False`; - every deformable declaration before the first `prepare()`/scene finalization. diff --git a/embodichain/compute/geometry/particles.py b/embodichain/compute/geometry/particles.py new file mode 100644 index 0000000000..efa1ca6c87 --- /dev/null +++ b/embodichain/compute/geometry/particles.py @@ -0,0 +1,157 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Deterministic volume sampling, independent of simulation objects.""" + +from __future__ import annotations + +import math +from typing import Sequence +import numpy as np + +__all__ = ["sample_volume_particles"] + + +def sample_volume_particles( + kind: str, + spacing: float, + *, + size: Sequence[float] = (0.05, 0.05, 0.05), + radius: float = 0.025, + height: float = 0.05, + clearance: float = 0.0, + profile: Sequence[Sequence[float]] | None = None, + fill_level: float = 1.0, + mesh_path: str | None = None, + max_particles: int = 1_000_000, +) -> np.ndarray: + """Sample a local primitive, revolved profile or closed mesh on a cubic lattice. + + Args: + kind: ``box``, ``sphere``, ``cylinder`` (Z axis), + ``revolved_volume`` (Z-axis profile), or ``mesh_volume``. + spacing: Positive lattice spacing, metres. + size: Full box dimensions, metres. + radius: Sphere or cylinder radius, metres. + height: Full cylinder height, metres. + clearance: Inward distance from the volume boundary, metres. + profile: For ``revolved_volume``, at least two ``(z, radius)`` pairs + with strictly increasing Z and nonnegative radii, in local metres. + Linear segments define the side wall; both ends are capped. + fill_level: Fraction of the source's full Z extent to fill, in ``(0, 1]``. + The free-surface cutoff is independent of solid-wall clearance. + mesh_path: Closed fill-volume mesh for ``mesh_volume``. + max_particles: Maximum candidate-grid size before allocation. + + Returns: + Contiguous float32 local positions of shape ``(N, 3)``. + """ + if not math.isfinite(spacing) or spacing <= 0: + raise ValueError("spacing must be finite and positive.") + if not math.isfinite(clearance) or clearance < 0: + raise ValueError("clearance must be finite and nonnegative.") + if not math.isfinite(fill_level) or not 0 < fill_level <= 1: + raise ValueError("fill_level must be finite and in (0, 1].") + if type(max_particles) is not int or max_particles <= 0: + raise ValueError("max_particles must be a positive integer.") + mesh = None + section = None + if kind == "revolved_volume": + section = np.asarray(profile, dtype=float) + if ( + section.ndim != 2 + or section.shape[1] != 2 + or len(section) < 2 + or not np.isfinite(section).all() + or np.any(np.diff(section[:, 0]) <= 0) + or np.any(section[:, 1] < 0) + or not np.any(section[:, 1] > 0) + ): + raise ValueError( + "profile requires finite (z, radius) pairs with increasing Z and nonnegative radii." + ) + elif profile is not None: + raise ValueError("profile is only valid for revolved_volume.") + if kind == "mesh_volume": + import trimesh + + if mesh_path is None: + raise ValueError("mesh_volume requires mesh_path.") + mesh = trimesh.load(mesh_path, force="mesh") + if not mesh.is_watertight or not mesh.is_volume: + raise ValueError("Fluid fill mesh must be a closed, oriented volume.") + lower, upper = mesh.bounds + else: + if mesh_path is not None: + raise ValueError("mesh_path is only valid for mesh_volume.") + if kind == "box": + extent = np.asarray(size, dtype=float) + elif kind == "sphere": + extent = np.array([2 * radius] * 3) + elif kind == "cylinder": + extent = np.array([2 * radius, 2 * radius, height]) + elif section is not None: + max_radius = section[:, 1].max() + extent = np.array([2 * max_radius, 2 * max_radius, np.ptp(section[:, 0])]) + else: + raise ValueError(f"Unsupported fluid volume {kind!r}.") + if extent.shape != (3,) or not np.isfinite(extent).all() or (extent <= 0).any(): + raise ValueError("Volume dimensions must be finite and positive.") + lower, upper = -extent / 2, extent / 2 + if section is not None: + lower[2], upper[2] = section[0, 0], section[-1, 0] + surface_z = lower[2] + fill_level * (upper[2] - lower[2]) + lower, upper = lower + clearance, upper - clearance + upper[2] = min(upper[2], surface_z) + counts = [max(0, math.ceil(float(v) / spacing)) for v in upper - lower] + if not all(counts): + raise ValueError("clearance leaves an empty fill volume.") + if math.prod(counts) > max_particles: + raise ValueError("Fluid candidate grid exceeds max_particles.") + axes = [lo + (np.arange(n) + 0.5) * spacing for lo, n in zip(lower, counts)] + points = np.stack(np.meshgrid(*axes, indexing="ij"), axis=-1).reshape(-1, 3) + keep = np.all(points < upper, axis=1) + if kind == "sphere": + keep &= np.linalg.norm(points, axis=1) < radius - clearance + elif kind == "cylinder": + keep &= np.linalg.norm(points[:, :2], axis=1) < radius - clearance + elif section is not None: + radial = np.linalg.norm(points[:, :2], axis=1) + keep &= radial < np.interp(points[:, 2], section[:, 0], section[:, 1]) + if clearance: + # Rotational symmetry reduces side-wall distance to the meridian + # plane. Include all segments, including steep or concave profiles. + meridian = np.column_stack((points[:, 2], radial)) + distance2 = np.full(len(points), np.inf) + for start, end in zip(section[:-1], section[1:]): + segment = end - start + t = np.clip( + (meridian - start) @ segment / np.dot(segment, segment), 0, 1 + ) + delta = meridian - (start + t[:, None] * segment) + distance2 = np.minimum(distance2, np.einsum("ij,ij->i", delta, delta)) + keep &= distance2 >= clearance**2 + elif mesh is not None: + import trimesh + + keep &= mesh.contains(points) + if clearance: + _, distance, _ = trimesh.proximity.closest_point(mesh, points) + keep &= distance >= clearance + points = points[keep] + if not len(points): + raise ValueError("Fluid volume contains no particles at this spacing.") + return np.ascontiguousarray(points, dtype=np.float32) diff --git a/embodichain/lab/sim/_startup_summary.py b/embodichain/lab/sim/_startup_summary.py index ea5687d563..677230af81 100644 --- a/embodichain/lab/sim/_startup_summary.py +++ b/embodichain/lab/sim/_startup_summary.py @@ -269,6 +269,7 @@ def scene_rows(sim: SimulationManager) -> list[Row]: ), ("Scene / env", "Object groups", str(len(sim._rigid_object_groups))), ("Scene / env", "Deformables", str(len(sim._deformable_objects))), + ("Scene / env", "Fluids", str(len(getattr(sim, "_fluid_objects", {})))), ("Scene / env", "Sensors", str(len(sim._sensors))), ( "Scene", diff --git a/embodichain/lab/sim/cfg/__init__.py b/embodichain/lab/sim/cfg/__init__.py index 51e2b2f0ff..230021653d 100644 --- a/embodichain/lab/sim/cfg/__init__.py +++ b/embodichain/lab/sim/cfg/__init__.py @@ -39,6 +39,7 @@ link_attrs_from_dict, ) from .asset import AssetPhysicsMode, ObjectBaseCfg, _resolve_asset_physics_mode +from .fluid import FluidObjectCfg, FluidSpawnCfg, FluidRenderCfg from .source import UsdSourceCfg from .deformable import ( SurfaceElementPropertiesCfg, @@ -88,6 +89,9 @@ from .robot import RobotCfg, RobotPresetCfg # noqa: E402 __all__ = [ + "FluidObjectCfg", + "FluidSpawnCfg", + "FluidRenderCfg", "DEFAULT_RENDERER", "AssetPhysicsMode", "DenoisingCfg", diff --git a/embodichain/lab/sim/cfg/articulation.py b/embodichain/lab/sim/cfg/articulation.py index aaee49cb9c..19cba5b716 100644 --- a/embodichain/lab/sim/cfg/articulation.py +++ b/embodichain/lab/sim/cfg/articulation.py @@ -35,6 +35,15 @@ _rigid_body_physics_from_dict, ) +__all__ = [ + "ArticulationRootPropertiesCfg", + "LinkPhysicsOverrideCfg", + "link_attrs_from_dict", + "JointDrivePropertiesCfg", + "NewtonJointDrivePropertiesCfg", + "ArticulationCfg", +] + def _normalize_joint_target_mode(value: object) -> int: """Normalize a portable joint target mode to its backend integer value.""" @@ -95,6 +104,30 @@ class ArticulationRootPropertiesCfg: min_velocity_iters: int | None = None """Default-only minimum root velocity-solver iterations (0 to 255).""" + newton_gravity_compensation: float | None = None + """MuJoCo link-weight compensation fraction in [0, 1], on Newton only. + + ``None`` preserves the source/default policy; zero retains gravity and one + compensates every link in this articulation. Payload gravity and contact + reactions remain active. This creation setting applies in both asset + physics modes, independently of DexUni's coupling policy. The finalized + Newton solver must support MuJoCo gravity compensation. + """ + + newton_mimic_stabilizer: bool = False + """Whether URDF mimic joints are tuned toward Default-backend behavior. + + ``False`` (default) keeps the configured follower drives and Newton's + native mimic constraint, as a plain DexSim scene does; reset starts each + follower on its mimic relation so the constraint does not kick on the + first step. On MuJoCo-Warp and + DexUni, ``True`` opts in to a Default-parity stabilizer: each mimic + follower's drive becomes one percent of its leader's gains (replacing the + follower gains in :attr:`ArticulationCfg.joint_drive_props`), follower + targets follow the mimic relation, and the MuJoCo equality ``solref`` is + retuned. + """ + def __post_init__(self) -> None: """Require the two values consumed by the atomic Default setter.""" if (self.min_position_iters is None) != (self.min_velocity_iters is None): @@ -378,12 +411,23 @@ def to_dict(self) -> dict[str, Any]: @configclass class NewtonJointDrivePropertiesCfg(JointDrivePropertiesCfg): - """Compatibility subtype for serialized Newton joint-drive configs. + """Joint-drive configuration with Newton-specific passive dynamics. ``target_mode`` is now portable and lives on :class:`JointDrivePropertiesCfg`. The subtype remains so existing - ``backend="newton"`` dictionaries and round trips retain their type; new - robot definitions should use the common class. + ``backend="newton"`` dictionaries and round trips retain their type. Use + the common class when no Newton-specific dynamics are required. + """ + + passive_damping: dict[str, float] | float | None = None + """Nonnegative passive joint damping, independent of drive damping. + + Uses the same joint-name, regex and robot control-part rules as the drive + gains. Apply it with ``asset_physics_mode='overlay'``; ``None`` preserves + source damping. Unlike :attr:`damping`, this + lowers to Newton's ``JointDofConfig.damping`` and remains active when the + drive target mode is ``none`` or ``effort``. It requires Newton and is + consumed by MuJoCo-backed solvers, including dynamic DexUni. """ diff --git a/embodichain/lab/sim/cfg/deformable.py b/embodichain/lab/sim/cfg/deformable.py index 125bd1a1a6..13af9639e7 100644 --- a/embodichain/lab/sim/cfg/deformable.py +++ b/embodichain/lab/sim/cfg/deformable.py @@ -125,7 +125,13 @@ class VolumeDeformablePhysicsCfg: """Whether Newton creates surface bending-edge constraints.""" contact_material: Mapping[str, float] | None = None - """Optional DexUni contact stiffness, damping, and dynamic friction.""" + """Optional DexUni object contact material, separate from element elasticity. + + Keys are ``stiffness`` [N/m], ``damping`` [N s/m], and + ``dynamic_friction``. Omitted keys use the native material defaults. + Values must be finite, nonnegative float32 values. ``None`` preserves the + backend defaults; explicit materials require a supporting DexSim build. + """ def __post_init__(self) -> None: if isinstance(self.surface_props, Mapping): @@ -152,7 +158,37 @@ class SurfaceDeformablePhysicsCfg: """Spring damping; ``None`` uses the Newton default.""" contact_material: Mapping[str, float] | None = None - """Optional DexUni contact stiffness, damping, and dynamic friction.""" + """Optional DexUni object contact material, separate from element elasticity. + + Keys are ``stiffness`` [N/m], ``damping`` [N s/m], and + ``dynamic_friction``. Omitted keys use the native material defaults. + Values must be finite, nonnegative float32 values. ``None`` preserves the + backend defaults; explicit materials require a supporting DexSim build. + """ + + edge_rest_angle: float | None = None + """Constant signed rest dihedral angle [rad] for every bending edge. + + ``None`` preserves angles inferred from the authored mesh. Set ``0.0`` + for a garment that should relax toward a flat surface. This changes the + rest geometry independently of ``surface_props.edge_ke`` and requires + a DexSim build supporting the cloth ``edge_rest_angle`` parameter. + """ + + pneumatic: Mapping[str, Any] | None = None + """Optional DexUni closed-cavity pressure parameters for this surface. + + Keys follow DexSim ``PneumaticPhysicsDesc``: ``mode`` (``isothermal``, + ``adiabatic``, ``target_volume`` or ``prescribed_gauge_pressure``), + ``reference_absolute_pressure`` and ``ambient_pressure`` [Pa], + ``heat_capacity_ratio``, ``target_volume`` [m³], ``volume_stiffness``, + ``bulk_damping``, ``min_volume_ratio``, ``max_absolute_pressure`` [Pa], + and ``prescribed_gauge_pressure`` [Pa]. Native validation owns their + defaults and pressure-law constraints. The authored surface must be + closed; native model construction validates cavity topology and solver + compatibility. ``None`` preserves ordinary cloth and supports older + wheels; an explicit block requires a supporting DexSim build. + """ def __post_init__(self) -> None: if isinstance(self.surface_props, Mapping): @@ -235,7 +271,20 @@ class VolumeDeformableObjectCfg(DeformableObjectCfg): deformable_type: Literal["volume"] = "volume" meshing: VolumeDeformableMeshingCfg = VolumeDeformableMeshingCfg() - """Tetrahedral simulation-mesh voxelization attributes.""" + """Tetrahedral simulation-mesh voxelization attributes. + + Ignored when :attr:`tetrahedra` supplies an explicit simulation mesh. + """ + + tetrahedra: Sequence[Sequence[int]] | np.ndarray | None = None + """Explicit ``(M, 4)`` tetrahedra indexing ``shape.vertices``. + + When set, :attr:`shape` must be array-backed: its vertices become the + simulation particles in the same order, so particle indices equal vertex + indices, and :attr:`meshing` is not used. ``shape.triangles`` may list the + boundary surface over those vertex indices; leave it ``None`` to derive + the surface from the tetrahedra. ``None`` voxelizes :attr:`shape` instead. + """ attrs: VolumeDeformablePhysicsCfg = VolumeDeformablePhysicsCfg() """Newton volume-deformable physical attributes.""" @@ -246,6 +295,17 @@ def __post_init__(self) -> None: self.attrs = VolumeDeformablePhysicsCfg(**self.attrs) if isinstance(self.meshing, Mapping): self.meshing = VolumeDeformableMeshingCfg(**self.meshing) + if self.tetrahedra is not None: + raw = np.asarray(self.tetrahedra) + if raw.dtype.kind not in "iu": + raise TypeError("tetrahedra must contain integer indices.") + if raw.ndim != 2 or raw.shape[1:] != (4,) or len(raw) == 0: + raise ValueError("tetrahedra must have non-empty shape (M, 4).") + if np.any(raw < 0) or np.any(raw > np.iinfo(np.int32).max): + raise ValueError( + "tetrahedra indices must be nonnegative and fit int32." + ) + self.tetrahedra = raw.astype(np.int32) @configclass diff --git a/embodichain/lab/sim/cfg/fluid.py b/embodichain/lab/sim/cfg/fluid.py new file mode 100644 index 0000000000..aa3a94b2fc --- /dev/null +++ b/embodichain/lab/sim/cfg/fluid.py @@ -0,0 +1,186 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Initial-fill fluid configuration; numerical solvers remain owned by DexSim.""" + +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any, Literal +import math +import numpy as np + +from embodichain.utils import configclass +from ..material import VisualMaterialCfg +from .asset import ObjectBaseCfg + +__all__ = ["FluidSpawnCfg", "FluidRenderCfg", "FluidObjectCfg"] + + +@configclass +class FluidSpawnCfg: + """Deterministic local particle source, with no runtime emission. + + Mesh sources describe a closed *fill volume*, not the solid container wall. + File paths are resolved by the caller; NPZ sources use the ``positions`` key. + ``reference_uid`` resolves a declared rigid body's initial pose once. + """ + + kind: Literal[ + "points", "box", "sphere", "cylinder", "revolved_volume", "mesh_volume" + ] = "points" + positions: Any = None + file_path: str | None = None + size: tuple[float, float, float] = (0.05, 0.05, 0.05) + radius: float = 0.025 + height: float = 0.05 + clearance: float = 0.0 + profile: tuple[tuple[float, float], ...] | None = None + """Piecewise-linear ``(local Z, radius)`` pairs for a capped revolved fill volume.""" + fill_level: float = 1.0 + """Filled fraction of the complete source Z extent; only for volume sources.""" + reference_uid: str | None = None + initial_velocity: tuple[float, float, float] = (0.0, 0.0, 0.0) + max_particles: int = 1_000_000 + """Bound the candidate grid allocation before sampling.""" + + def sample(self, spacing: float) -> np.ndarray: + """Return independent finite local positions of shape ``(N, 3)``. + + Args: + spacing: Positive lattice spacing in metres. + + Returns: + Contiguous float32 positions in deterministic lattice order. + """ + from embodichain.compute.geometry.particles import sample_volume_particles + + if not math.isfinite(spacing) or spacing <= 0: + raise ValueError("spacing must be finite and positive.") + if type(self.max_particles) is not int or self.max_particles <= 0: + raise ValueError("max_particles must be a positive integer.") + if self.kind == "points": + if self.profile is not None or self.fill_level != 1.0: + raise ValueError( + "points sources cannot apply profile or fill_level; use a volume source." + ) + if (self.positions is None) == (self.file_path is None): + raise ValueError( + "points requires exactly one of positions or file_path." + ) + if self.file_path is not None: + with np.load(self.file_path, allow_pickle=False) as source: + points = np.asarray(source["positions"], dtype=np.float32).copy() + else: + points = np.asarray(self.positions, dtype=np.float32).copy() + else: + if self.positions is not None: + raise ValueError("positions is only valid for kind='points'.") + points = sample_volume_particles( + self.kind, + spacing, + size=self.size, + radius=self.radius, + height=self.height, + clearance=self.clearance, + profile=self.profile, + fill_level=self.fill_level, + mesh_path=self.file_path, + max_particles=self.max_particles, + ) + if ( + points.ndim != 2 + or points.shape[1] != 3 + or not len(points) + or not np.isfinite(points).all() + ): + raise ValueError("Fluid positions must be finite, nonempty (N, 3) points.") + if len(points) > self.max_particles: + raise ValueError("Fluid source exceeds max_particles.") + return np.ascontiguousarray(points) + + +@configclass +class FluidRenderCfg: + """Native particle/surface display, independent of physical particle state. + + ``mesh_surface`` forwards DexSim FluidMeshSurfaceRenderDesc fields directly. + Optional world-space clipping affects only a detached render copy. + Native fluid geometry requires ``fast-rt`` or ``rt``; Hybrid's Vulkan + primary surface does not contain the OptiX liquid instances. + ``mode="none"`` currently requires the simulation NoRender backend. + """ + + mode: Literal["none", "particles", "mesh_surface"] = "particles" + render_radius: float | None = None + mesh_surface: dict[str, Any] = {} + material: VisualMaterialCfg | None = None + clip_below_z: float | None = None + + def __post_init__(self) -> None: + if isinstance(self.material, Mapping): + self.material = VisualMaterialCfg.from_dict(dict(self.material)) + if self.mode not in {"none", "particles", "mesh_surface"}: + raise ValueError("Unsupported fluid render mode.") + for name in ("render_radius", "clip_below_z"): + value = getattr(self, name) + if value is not None and not math.isfinite(value): + raise ValueError(f"{name} must be finite.") + if self.render_radius is not None and self.render_radius <= 0: + raise ValueError("render_radius must be positive.") + + +@configclass +class FluidObjectCfg(ObjectBaseCfg): + """A fixed-count DexUni/DFSPH fluid, declared before scene preparation. + + Density and spacing determine mass and must agree across fluid objects. + Solver viscosity, surface tension and iteration controls belong exclusively + to DexSim's SPHSolverCfg supplied through DexUniSolverCfg.sph_cfg. + + This config owns density and spacing. Native SPH defaults for those fields + are placeholders in both mapping and native ``sph_cfg`` values; non-default + solver values must match the fluid declaration. + """ + + density: float = 1000.0 + particle_spacing: float = 0.0015 + particle_radius: float | None = None + spawn: FluidSpawnCfg = FluidSpawnCfg() + render: FluidRenderCfg = FluidRenderCfg() + + def __post_init__(self) -> None: + if isinstance(self.spawn, Mapping): + self.spawn = FluidSpawnCfg(**self.spawn) + if isinstance(self.render, Mapping): + self.render = FluidRenderCfg(**self.render) + for name in ("density", "particle_spacing", "particle_radius"): + value = getattr(self, name) + if value is not None and (not math.isfinite(value) or value <= 0): + raise ValueError(f"{name} must be finite and positive.") + velocity = np.asarray(self.spawn.initial_velocity) + if velocity.shape != (3,) or not np.isfinite(velocity).all(): + raise ValueError("initial_velocity must contain three finite values.") + if ( + not isinstance(self.spawn.max_particles, int) + or self.spawn.max_particles <= 0 + ): + raise ValueError("max_particles must be a positive integer.") + + @classmethod + def from_dict(cls, init_dict: dict[str, Any]) -> FluidObjectCfg: + """Decode the same typed configuration used by Python callers.""" + return cls(**init_dict) diff --git a/embodichain/lab/sim/cfg/physics.py b/embodichain/lab/sim/cfg/physics.py index f7f01b1885..3975f31980 100644 --- a/embodichain/lab/sim/cfg/physics.py +++ b/embodichain/lab/sim/cfg/physics.py @@ -375,6 +375,7 @@ class NewtonPhysicsCfg(PhysicsBackendCfg): ``solver_type`` or ``class_type`` to select the solver, then add any parameters accepted by that DexSim solver config. If omitted, EmbodiChain preserves DexSim's ``DexUniSolverCfg`` default and its scene-based dispatch. + Native solver config objects use the same supported solver families. Unset ownership selectors retain automatic scene partitioning; ``contact_mode='auto'`` independently selects the native contact path. For ``solver_type='dexuni'``, the current DexSim contract keeps expert @@ -558,6 +559,7 @@ def _newton_solver_cfg_to_dexsim( "Newton solver_cfg must be a mapping or a DexSim Newton solver " "config object with a 'solver_type' attribute." ) + _normalize_newton_solver_type(str(solver_cfg.solver_type)) return solver_cfg solver_cfg_data = dict(solver_cfg) diff --git a/embodichain/lab/sim/objects/__init__.py b/embodichain/lab/sim/objects/__init__.py index b948e3591d..81f166ed66 100644 --- a/embodichain/lab/sim/objects/__init__.py +++ b/embodichain/lab/sim/objects/__init__.py @@ -29,6 +29,7 @@ RigidBodyGroupData, RigidObjectGroupCfg, ) +from .fluid import FluidObject, FluidObjectData from .deformable import ( DeformableObject, DeformableObjectData, diff --git a/embodichain/lab/sim/objects/articulation.py b/embodichain/lab/sim/objects/articulation.py index dc735e71d0..3b7b856ffa 100644 --- a/embodichain/lab/sim/objects/articulation.py +++ b/embodichain/lab/sim/objects/articulation.py @@ -937,7 +937,10 @@ def _apply_spawn_config(self) -> None: spawn_result = getattr(self, "_spawn_result", None) self._prepare_spawn_runtime_config(spawn_result) - self._newton_mimic_compliance_configured = _configure_newton_mimic_compliance( + root_props = getattr(self.cfg, "root_props", None) + self._newton_mimic_compliance_configured = getattr( + root_props, "newton_mimic_stabilizer", False + ) and _configure_newton_mimic_compliance( result=spawn_result, entities=self._entities, state_joint_names=self._state_joint_names(), @@ -1051,7 +1054,7 @@ def root_link_name(self) -> str: Returns: str: The name of the root link. """ - return self.entities[0].get_root_link_name() + return self._entities[0].get_root_link_name() @cached_property def joint_names(self) -> List[str]: diff --git a/embodichain/lab/sim/objects/backends/collision.py b/embodichain/lab/sim/objects/backends/collision.py index dce7414258..d1e474717f 100644 --- a/embodichain/lab/sim/objects/backends/collision.py +++ b/embodichain/lab/sim/objects/backends/collision.py @@ -443,9 +443,9 @@ def _from_descriptor(entity: object) -> list[CollisionShapeDesc] | None: dtype=torch.float32, ).reshape(3) shapes: list[CollisionShapeDesc] = [] + # Match native shape queries: disabling contact response keeps geometry + # available to planners, including moving obstacles that exert no impulses. for shape_idx, collision in enumerate(collisions): - if getattr(collision, "enable_collision", True) is False: - continue geometry_type = getattr(collision, "geometry_type", None) geometry_type = getattr(geometry_type, "value", geometry_type) local_pose = torch.as_tensor( diff --git a/embodichain/lab/sim/objects/backends/newton.py b/embodichain/lab/sim/objects/backends/newton.py index 33b1644044..1763ea5287 100644 --- a/embodichain/lab/sim/objects/backends/newton.py +++ b/embodichain/lab/sim/objects/backends/newton.py @@ -204,11 +204,14 @@ def _configure_newton_mimic_compliance( backend = get_newton_backend(result.world) if ( backend is None - or backend.solver_type != "mujoco_warp" + or backend.solver_type not in {"mujoco_warp", "dexuni"} or backend.cfg.requires_grad or (backend.model is not None and backend.model.requires_grad) ): return False + solver = backend.mujoco_solver + if solver is None: + return False relation_names = [ (state_joint_names[child_id], state_joint_names[parent_id]) @@ -284,13 +287,21 @@ def _configure_newton_mimic_compliance( f"Newton model has no mimic constraint for joint pair {error.args[0]}." ) from error - solver = runtime.solver mapping = getattr(solver, "mjc_eq_to_newton_mimic", None) mjw_model = getattr(solver, "mjw_model", None) if mapping is None or mjw_model is None: raise RuntimeError("MuJoCo-Warp did not materialize Newton mimic rows.") mapping_values = np.asarray(mapping.numpy()) + if backend.solver_type == "dexuni": + from newton import Model + + scene_rows = backend.get_mujoco_index_map( + Model.AttributeFrequency.CONSTRAINT_MIMIC + ).numpy() + mapping_values = mapping_values.copy() + valid = mapping_values >= 0 + mapping_values[valid] = scene_rows[mapping_values[valid]] selected = np.isin(mapping_values, constraint_rows) if int(selected.sum()) != len(constraint_rows): raise RuntimeError( diff --git a/embodichain/lab/sim/objects/deformable/base.py b/embodichain/lab/sim/objects/deformable/base.py index 231baf3213..02a86cf07e 100644 --- a/embodichain/lab/sim/objects/deformable/base.py +++ b/embodichain/lab/sim/objects/deformable/base.py @@ -427,6 +427,41 @@ def set_local_pose( local_env_ids, ) + def set_nodal_state( + self, + nodal_state: torch.Tensor, + env_ids: Sequence[int] | None = None, + ) -> None: + """Write world-frame particle positions and velocities. + + This is the inverse of :attr:`DeformableObjectData.nodal_state_w`. + Use it to start from a deformed configuration while keeping the + authored rest shape, for example a pre-compressed block. + + Args: + nodal_state: Tensor of shape ``(len(env_ids), num_nodes, 6)`` + holding ``[position, velocity]`` per node in world frame. + env_ids: Environments to write. ``None`` selects all. + + Raises: + ValueError: If the shape does not match or values are not finite. + """ + local_env_ids = self._resolve_env_ids(env_ids) + self._require_data() + state = torch.as_tensor(nodal_state, dtype=torch.float32, device=self.device) + if state.dim() != 3 or state.shape[-1] != 6: + raise ValueError( + "nodal_state must have shape (len(env_ids), num_nodes, 6); got " + f"{tuple(state.shape)}." + ) + if not torch.isfinite(state).all(): + raise ValueError("nodal_state must contain only finite values.") + self._data._apply_nodal_state( + state[..., :3], + state[..., 3:], + local_env_ids, + ) + def _apply_local_pose( self, pose: torch.Tensor, diff --git a/embodichain/lab/sim/objects/fluid.py b/embodichain/lab/sim/objects/fluid.py new file mode 100644 index 0000000000..e1bdf9e0ad --- /dev/null +++ b/embodichain/lab/sim/objects/fluid.py @@ -0,0 +1,271 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Declared fixed-count fluids with Scene-owned particle state.""" + +from __future__ import annotations + +from copy import deepcopy +from typing import Any, Callable, Sequence +import numpy as np +import torch + +from embodichain.lab.sim.cfg.fluid import FluidObjectCfg +from embodichain.lab.sim.objects.deformable.data import DeformableObjectData +from embodichain.utils.math import matrix_from_quat + +__all__ = ["FluidObject", "FluidObjectData"] + + +class FluidObjectData: + """Detached world-frame state snapshots, shaped ``(E, N, 3|6)``. + + The shared Scene particle batch adapter is composed without inheriting + any deformable connectivity or fixed render topology. + """ + + def __init__( + self, entities: Sequence[Any], scene: Any, device: torch.device + ) -> None: + self._state = DeformableObjectData(entities, scene, device) + + @property + def particle_pos_w(self) -> torch.Tensor: + """Current positions in metres; modifying the result does not write state.""" + return self._state.nodal_pos_w + + @property + def particle_vel_w(self) -> torch.Tensor: + """Current world-frame velocities in metres per second.""" + return self._state.nodal_vel_w + + @property + def particle_state_w(self) -> torch.Tensor: + """Concatenated positions and velocities, with shape ``(E, N, 6)``.""" + return self._state.nodal_state_w + + @property + def default_particle_state_w(self) -> torch.Tensor: + """Independent copy of the finalized initial state.""" + return self._state.default_nodal_state_w + + +class FluidObject: + """One logical fluid declared and bound by SimulationManager. + + Particle count is fixed. There is no physical rigid root pose or persistent + attachment to the initial container. Pose reset transforms the rest template. + """ + + def __init__( + self, + cfg: FluidObjectCfg, + device: torch.device = torch.device("cpu"), + ) -> None: + """Create an unregistered facade without sampling or allocating state. + + Args: + cfg: Particle-source recipe and initial physical/render settings. + device: Device for state and resolved source caches after declaration. + """ + self.cfg = deepcopy(cfg) + self.uid = self.cfg.uid + self.device = torch.device(device) + self._rest_positions_local: torch.Tensor | None = None + self._initial_rotation: torch.Tensor | None = None + self._entities: list[Any] = [] + self._spawn_result = None + self._data: FluidObjectData | None = None + self._declared_num_instances: int | None = None + self._on_write: Callable[[Sequence[int]], None] | None = None + + def _initialize_spawn_source( + self, + rest_positions_local: np.ndarray, + initial_rotation: np.ndarray, + *, + on_write: Callable[[Sequence[int]], None], + ) -> None: + """Cache Manager-resolved source data without changing the input config.""" + if self._rest_positions_local is not None: + raise RuntimeError("Fluid source is already initialized.") + positions = np.asarray(rest_positions_local, dtype=np.float32) + rotation = np.asarray(initial_rotation, dtype=np.float32) + if positions.ndim != 2 or positions.shape[1] != 3 or not len(positions): + raise ValueError("Fluid source requires nonempty (N, 3) positions.") + if rotation.shape != (3, 3) or not np.isfinite(rotation).all(): + raise ValueError("Fluid source requires a finite (3, 3) initial rotation.") + if not callable(on_write): + raise TypeError("Fluid state invalidation callback must be callable.") + self._rest_positions_local = torch.as_tensor( + positions.copy(), device=self.device + ) + self._initial_rotation = torch.as_tensor(rotation.copy(), device=self.device) + self._on_write = on_write + + def _initialize_spawn_declaration(self, num_instances: int) -> None: + if num_instances <= 0: + raise ValueError("FluidObject requires a positive instance count.") + if self._declared_num_instances not in (None, num_instances): + raise RuntimeError("FluidObject instance count is already declared.") + self._declared_num_instances = num_instances + + @property + def is_declared(self) -> bool: + """Whether state is still unavailable pending ``prepare()``.""" + return self._data is None + + @property + def num_instances(self) -> int: + """Number of scene-owned replicas.""" + if self._declared_num_instances is None: + raise RuntimeError("FluidObject must be registered through SpawnScene.") + return self._declared_num_instances + + @property + def num_particles(self) -> int: + """Fixed particle count per replica.""" + if self._rest_positions_local is None: + raise RuntimeError( + "Fluid source requires SimulationManager.add_fluid_object()." + ) + return len(self._rest_positions_local) + + @property + def particle_mass(self) -> float: + """Uniform particle mass in kilograms.""" + return self.cfg.density * self.cfg.particle_spacing**3 + + @property + def nominal_volume(self) -> float: + """Rest volume in cubic metres, independent of render reconstruction.""" + return self.num_particles * self.cfg.particle_spacing**3 + + @property + def data(self) -> FluidObjectData: + """Return prepared particle data, or raise before binding.""" + if self._data is None: + raise RuntimeError("Fluid state requires SimulationManager.prepare().") + return self._data + + def attach_spawn_handles(self, entities: Sequence[Any]) -> None: + """Attach the replicas materialized by Spawn.""" + if len(entities) != self.num_instances: + raise RuntimeError("Fluid replica count does not match the declaration.") + self._entities = list(entities) + + def bind_spawn(self, scene: Any) -> None: + """Bind Scene batch state and capture configured initial velocities.""" + if self._data is not None: + return + if ( + self._rest_positions_local is None + or self._initial_rotation is None + or self._on_write is None + ): + raise RuntimeError("Fluid source must be initialized before binding.") + data = FluidObjectData(self._entities, scene, self.device) + if data._state.n_nodes != self.num_particles: + raise RuntimeError("Spawn changed the fluid particle count.") + state = data.particle_state_w + velocity = torch.as_tensor( + self.cfg.spawn.initial_velocity, device=self.device, dtype=torch.float32 + ) + state[..., 3:] = velocity @ self._initial_rotation.T + data._state._apply_nodal_state( + state[..., :3], state[..., 3:], list(range(self.num_instances)) + ) + data._state._default_nodal_state_w = state.clone() + self._spawn_result = scene + self._data = data + + def _indices(self, env_ids: Sequence[int] | None) -> list[int]: + if env_ids is None: + return list(range(self.num_instances)) + ids = torch.as_tensor(env_ids) + if ids.ndim != 1 or ( + ids.numel() and ids.dtype not in (torch.int32, torch.int64) + ): + raise ValueError("env_ids must be a one-dimensional integer selection.") + result = ids.tolist() + if len(set(result)) != len(result) or any( + i < 0 or i >= self.num_instances for i in result + ): + raise ValueError("env_ids must be unique valid instance indices.") + return result + + def write_particle_state_to_sim( + self, + state: torch.Tensor, + env_ids: Sequence[int] | None = None, + ) -> None: + """Write selected world-frame positions and velocities without stepping. + + Args: + state: Finite floating tensor ``(selected E, N, 6)``. + env_ids: Unique instance indices; None selects all instances. + """ + data = self.data + ids = self._indices(env_ids) + if not isinstance(state, torch.Tensor) or not state.is_floating_point(): + raise TypeError("Fluid state must be a floating Torch tensor.") + if tuple(state.shape) != (len(ids), self.num_particles, 6): + raise ValueError("Fluid state must have shape (selected E, N, 6).") + if not torch.isfinite(state).all(): + raise ValueError("Fluid state must be finite.") + if not ids: + return + assert self._on_write is not None + self._on_write(ids) + data._state._apply_nodal_state(state[..., :3], state[..., 3:], ids) + + def reset(self, env_ids: Sequence[int] | None = None) -> None: + """Restore selected initial particle states without advancing time.""" + data = self.data + ids = self._indices(env_ids) + self.write_particle_state_to_sim(data.default_particle_state_w[ids], ids) + + def reset_to_pose( + self, pose_w: torch.Tensor, env_ids: Sequence[int] | None = None + ) -> None: + """Place the local rest template at world poses ``xyz + xyzw``. + + Args: + pose_w: Finite ``(selected E, 7)`` poses with unit quaternions. + env_ids: Unique instance indices, or None for all. + """ + _ = self.data + ids = self._indices(env_ids) + if tuple(pose_w.shape) != (len(ids), 7) or not torch.isfinite(pose_w).all(): + raise ValueError("pose_w must be finite (selected E, 7).") + pose = pose_w.to(device=self.device, dtype=torch.float32) + if not torch.allclose( + pose[:, 3:].norm(dim=-1), + torch.ones(len(ids), device=self.device), + atol=1e-5, + ): + raise ValueError("pose_w requires unit xyzw quaternions.") + rotation = matrix_from_quat(pose[:, 3:]) + assert self._rest_positions_local is not None + points = ( + torch.einsum("eij,nj->eni", rotation, self._rest_positions_local) + + pose[:, None, :3] + ) + velocity = torch.as_tensor( + self.cfg.spawn.initial_velocity, device=self.device, dtype=torch.float32 + ) + velocities = (rotation @ velocity)[..., None, :].expand_as(points) + self.write_particle_state_to_sim(torch.cat((points, velocities), dim=-1), ids) diff --git a/embodichain/lab/sim/physics/newton.py b/embodichain/lab/sim/physics/newton.py index ac1f8544e7..20f4b933f9 100644 --- a/embodichain/lab/sim/physics/newton.py +++ b/embodichain/lab/sim/physics/newton.py @@ -19,6 +19,7 @@ import importlib from typing import TYPE_CHECKING +from collections.abc import Sequence import weakref import warp as wp @@ -27,8 +28,10 @@ if TYPE_CHECKING: import dexsim + import torch from embodichain.lab.sim.sim_manager import SimulationManagerCfg + from embodichain.lab.sim.objects.fluid import FluidObject __all__ = ["NewtonPhysicsBackend"] @@ -58,6 +61,9 @@ def __init__(self, manager) -> None: self._differentiable_runtime = None self._runtime_device: str | None = None self._configured_solver_type: str | None = None + self._fluid_render_clip_key: tuple[object, ...] | None = None + self._fluid_render_clip_z: torch.Tensor | None = None + self._fluid_render_dirty = False @property def solver_type(self) -> str | None: @@ -111,7 +117,192 @@ def sync_render_state(self, result: "dexsim.scene.Scene") -> None: raise RuntimeError( "Newton backend is unavailable for render-state synchronization." ) - backend.sync_to_dexsim(result.world) + fluids = getattr(self._manager, "_fluid_objects", {}) + clipped = [f for f in fluids.values() if f.cfg.render.clip_below_z is not None] + native_stream = producer_stream = None + if fluids and str(backend.state_0.particle_q.device).startswith("cuda"): + import torch + + # DexSim stages fluid positions asynchronously on CUDA's default + # stream. Order the Newton step and clipping before that copy, and + # complete the copy before a camera consumes the staged geometry. + device = torch.device(str(backend.state_0.particle_q.device)) + producer_stream = torch.cuda.current_stream(device) + warp_stream = wp.stream_to_torch(backend.state_0.particle_q.device) + if producer_stream.cuda_stream != warp_stream.cuda_stream: + producer_stream.wait_stream(warp_stream) + native_stream = torch.cuda.default_stream(device) + if self._fluid_render_dirty: + # A previous staged copy may still occupy the native double + # buffer. Drain it before submitting a newer explicit write; + # otherwise the renderer can silently discard the new state. + flush = getattr(result.world, "_flush_render_updates", None) + if not callable(flush): + raise RuntimeError( + "This DexSim build cannot synchronize fluid render updates." + ) + status = flush() + if status is not None and int(status) < 0: + raise RuntimeError("Could not drain previous fluid render updates.") + if not clipped: + self._fluid_render_clip_key = None + self._fluid_render_clip_z = None + positions = None + else: + ranges = [] + for fluid in clipped: + for entity in fluid._entities: + start = entity._physics_binding.particle_start + ranges.append( + ( + start, + start + entity.particle_count, + fluid.cfg.render.clip_below_z, + ) + ) + positions = self._clip_fluid_render_positions( + wp.to_torch(backend.state_0.particle_q), tuple(ranges) + ) + if native_stream is not None: + if native_stream.cuda_stream != producer_stream.cuda_stream: + native_stream.wait_stream(producer_stream) + try: + if positions is None: + backend.sync_to_dexsim(result.world) + else: + backend.render_sync.sync( + result.world, + backend.state_0.body_q, + wp.from_torch(positions, dtype=wp.vec3), + simulation_time=backend.sim_time, + ) + finally: + if native_stream is not None: + native_stream.synchronize() + self._fluid_render_dirty = False + + def _clip_fluid_render_positions( + self, + positions: torch.Tensor, + ranges: tuple[tuple[int, int, float], ...], + ) -> torch.Tensor: + """Create an owned render copy with one comparison and one selection. + + A full-buffer fluid uses a scalar cutoff. Mixed particle scenes cache + immutable per-particle cutoffs; changing layout, dtype, device or CUDA + stream replaces that cache. Each output owns fresh storage so a later + publication cannot overwrite data still consumed by the render bridge. + """ + import torch + + if len(ranges) == 1 and ranges[0][:2] == (0, len(positions)): + self._fluid_render_clip_key = None + self._fluid_render_clip_z = None + return torch.where( + positions[:, 2:3] < ranges[0][2], float("nan"), positions + ) + + # Cache reads and initialization stay on the same Torch stream. A stream + # change gets a new tensor, leaving the old allocation's lifetime ordered + # on its creation stream without adding a device-wide synchronization. + stream = ( + torch.cuda.current_stream(positions.device).cuda_stream + if positions.is_cuda + else None + ) + key = (positions.shape, positions.dtype, positions.device, stream, ranges) + if key != self._fluid_render_clip_key: + cutoffs = positions.new_full((len(positions), 1), float("-inf")) + for start, end, height in ranges: + cutoffs[start:end].fill_(height) + self._fluid_render_clip_z = cutoffs + self._fluid_render_clip_key = key + assert self._fluid_render_clip_z is not None + return torch.where( + positions[:, 2:3] < self._fluid_render_clip_z, float("nan"), positions + ) + + @property + def supports_dexuni_fluids(self) -> bool: + """Whether DexSim exposes the composed DexUni/DFSPH configuration.""" + if self.solver_type != "dexuni": + return False + from dexsim.engine.newton_physics import DexUniSolverCfg + + return "sph_cfg" in getattr(DexUniSolverCfg, "__dataclass_fields__", {}) + + @property + def supports_fluid_worlds(self) -> bool: + """Whether the loaded SPH artifact supports isolated worlds and reset.""" + if not self.supports_dexuni_fluids: + return False + from dexsim.engine.newton_physics.solver_runtime import ensure_solver_available + + solver = ensure_solver_available("sph") + return bool( + getattr(solver, "supports_world_isolation", False) + and getattr(solver, "supports_world_reset", False) + ) + + def invalidate_fluid_state(self, env_ids: Sequence[int] | None = None) -> None: + """Clear selected SPH history after an explicit state write. + + Args: + env_ids: Local worlds to clear, or all local worlds when omitted. + Shared boundary history remains unchanged. Legacy single-world + artifacts rebaseline their optional SDF history directly. + """ + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + + backend = get_newton_backend(self._manager._world) + if backend is None or backend.solver_type != "dexuni": + raise RuntimeError("Prepared DexUni fluid backend required.") + solver = getattr(backend, "sph_solver", None) + if solver is None: + raise RuntimeError("Prepared DexUni SPH solver required.") + backend.invalidate_cuda_graph("fluid state write") + self._fluid_render_dirty = True + if getattr(solver, "supports_world_reset", False): + mask = [ + env_ids is None or i in env_ids + for i in range(backend.model.world_count) + ] + [False] + solver.reset( + backend._runtime.current_state, + world_mask=wp.array(mask, dtype=wp.bool, device=backend.model.device), + flags=0, + ) + return + boundary = solver.sdf_boundary + if boundary is not None and boundary._body_q_prev is not None: + boundary._body_q_prev.assign(backend.state_0.body_q) + + def fluid_diagnostics(self, fluid: FluidObject) -> dict[str, object]: + """Read detached native SPH diagnostics without publishing or stepping.""" + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + import torch + + backend = get_newton_backend(self._manager._world) + solver = getattr(backend, "sph_solver", None) + if solver is None: + raise RuntimeError("Prepared DexUni SPH solver required.") + density = wp.to_torch(solver.particle_density) + values = [] + for entity in fluid._entities: + start = entity._physics_binding.particle_start + values.append(density[start : start + entity.particle_count].clone()) + return { + "particle_density": torch.stack(values), + "volume_map_enabled": solver.volume_map_boundary is not None, + "sdf_penalty_enabled": solver.sdf_boundary is not None, + "boundary_shape_count": ( + 0 + if solver.volume_map_boundary is None + else solver.volume_map_boundary.shape_count + ), + "rest_density_kg_m3": solver.rest_density, + "viscosity": solver.viscosity, + } def _step(self, physics_dt: float) -> None: """Let the manager batch window and offscreen consumers after stepping.""" @@ -126,6 +317,10 @@ def prepare_for_teardown(self) -> None: if self._runtime_device is not None and self._runtime_device.startswith("cuda"): wp.synchronize_device(self._runtime_device) + self._fluid_render_clip_key = None + self._fluid_render_clip_z = None + self._fluid_render_dirty = False + world = getattr(self._manager, "_world", None) if world is None: return diff --git a/embodichain/lab/sim/sim_manager.py b/embodichain/lab/sim/sim_manager.py index c93c60df50..d5870f433a 100644 --- a/embodichain/lab/sim/sim_manager.py +++ b/embodichain/lab/sim/sim_manager.py @@ -16,6 +16,10 @@ from __future__ import annotations +from collections.abc import Mapping +from embodichain.lab.sim.cfg.fluid import FluidObjectCfg +from embodichain.lab.sim.objects.fluid import FluidObject + import os import gc import sys @@ -613,6 +617,7 @@ def _initialize_simulation( self._gizmos: dict[str, object] = {} self._markers: dict[str, _AxisMarkerGroup] = {} self._constraints: dict[str, RigidConstraint] = {} + self._fluid_objects: dict[str, FluidObject] = {} self._visual_materials: dict[str, VisualMaterial] = {} self._texture_cache: dict[str, torch.Tensor | list[torch.Tensor]] = {} @@ -1002,6 +1007,7 @@ def asset_uids(self) -> List[str]: uid_list.extend(list(self._rigid_objects.keys())) uid_list.extend(list(self._rigid_object_groups.keys())) uid_list.extend(list(self._deformable_objects.keys())) + uid_list.extend(list(self._fluid_objects.keys())) uid_list.extend(list(self._articulations.keys())) return uid_list @@ -1070,6 +1076,7 @@ def start_visualization(self) -> VisualizationRuntime | None: getattr(self, "_rigid_objects", {}), getattr(self, "_rigid_object_groups", {}), getattr(self, "_deformable_objects", {}), + getattr(self, "_fluid_objects", {}), getattr(self, "_articulations", {}), getattr(self, "_robots", {}), getattr(self, "_sensors", {}), @@ -1527,26 +1534,31 @@ def register_contact_material_schedule( def register_particle_contact_material_schedule( self, keyframes: Mapping[str, Sequence[Sequence[float]]], + *, + uid: str | None = None, ) -> None: """Register time-varying Newton particle contact properties. Supported tracks are ``dynamic_friction``, ``stiffness``, and - ``damping``. Values apply scene-wide to particle-versus-rigid contacts - and are sampled piecewise-constantly in simulation time. + ``damping``. Values are sampled piecewise-constantly in simulation + time. Supply ``uid`` for a DexUni cloth/soft-body material; otherwise + register the legacy scene-wide control for other Newton solvers. .. attention:: - Register this control before :meth:`prepare`. This host-side - control requires direct Newton stepping and therefore disables - CUDA Graph replay for the finalized simulation. + Register before :meth:`prepare`. Targeted DexUni controls support + CUDA Graph replay. The legacy scene-wide host control requires + direct Newton stepping and disables graph replay. Args: keyframes: Particle contact-property tracks containing ``(time, value)`` pairs. + uid: Optional declared deformable UID for DexUni object materials. Raises: RuntimeError: If the backend is not Newton or the scene is already finalized. ValueError: If the keyframes are invalid. + KeyError: If ``uid`` is not a declared deformable. """ if not self.is_newton_backend: raise RuntimeError( @@ -1559,6 +1571,31 @@ def register_particle_contact_material_schedule( "SimulationManager.prepare()." ) + if uid is not None: + if uid not in self._deformable_objects: + raise KeyError(f"Deformable object {uid!r} is not declared.") + arena_names = scene.arena_names + if len(arena_names) != self.num_envs: + raise RuntimeError( + "Spawn arena count does not match SimulationManager.num_envs." + ) + from dexsim.engine import newton_physics + + control_type = getattr( + newton_physics, "DexUniParticleContactMaterialSchedule", None + ) + if control_type is None: + raise RuntimeError( + "Targeted particle materials require a supporting DexSim build." + ) + controls = tuple( + control_type(keyframes, target=f"{arena_name}/{uid}") + for arena_name in arena_names + ) + for control in controls: + scene.builder.add_runtime_control(control) + return + from dexsim.engine.newton_physics import ParticleContactMaterialSchedule scene.builder.add_runtime_control(ParticleContactMaterialSchedule(keyframes)) @@ -1724,6 +1761,71 @@ def register_kinematic_nodal_trajectory( for control in controls: scene.builder.add_runtime_control(control) + def add_runtime_control( + self, + control: Any | Callable[[str, int], Any], + ) -> tuple[Any, ...]: + """Register DexSim Newton runtime controls before :meth:`prepare`. + + Use this for controls that have no dedicated ``register_*`` method, + such as ``RealtimeJointTargetControl``, ``PinnedParticleControl`` or + ``IndexedConveyorControl`` from ``dexsim.engine.newton_physics``. + + Pass either one control instance, which is registered once for the + whole scene, or a factory called as ``factory(arena_name, env_index)`` + for every environment. Controls address entities by Spawn path, which + is ``f"{arena_name}/{uid}"`` for an object declared with ``uid``. + + Args: + control: A ``RuntimeControl`` instance, or a per-environment + factory returning one. + + Returns: + The registered controls, in environment order for a factory. + Keep them to drive live controls such as joint targets. + + Raises: + RuntimeError: If the backend is not Newton, the Spawn scene is + already finalized, or its arena count is inconsistent. + TypeError: If a control is not a ``RuntimeControl``. + """ + from dexsim.engine.newton_physics.runtime_control import RuntimeControl + + if not self.is_newton_backend: + raise RuntimeError("Runtime controls require the Newton backend.") + scene = self._spawn_scene + if scene.builder.is_finalized: + raise RuntimeError( + "Runtime controls must be registered before " + "SimulationManager.prepare()." + ) + if isinstance(control, RuntimeControl): + controls: tuple[Any, ...] = (control,) + elif callable(control): + arena_names = scene.arena_names + if len(arena_names) != self.num_envs: + raise RuntimeError( + "Spawn arena count does not match SimulationManager.num_envs: " + f"{len(arena_names)} != {self.num_envs}." + ) + controls = tuple( + control(arena_name, env_index) + for env_index, arena_name in enumerate(arena_names) + ) + else: + raise TypeError( + "control must be a RuntimeControl or a factory returning one." + ) + for item in controls: + if not isinstance(item, RuntimeControl): + raise TypeError( + "Runtime control factories must return RuntimeControl " + f"instances; got {type(item).__name__}." + ) + for item in controls: + scene.builder.add_runtime_control(item) + return controls + def prepare(self) -> None: """Materialize declarations, bind state, and restore camera parents.""" self._ready_spawn_topology_revision = -1 @@ -2380,6 +2482,7 @@ def get_asset( | VisualObject | RigidObjectGroup | DeformableObject + | FluidObject | Articulation | None ): @@ -2408,6 +2511,8 @@ def get_asset( return self._rigid_object_groups[uid] if uid in self._deformable_objects: return self._deformable_objects[uid] + if uid in self._fluid_objects: + return self._fluid_objects[uid] if uid in self._articulations: return self._articulations[uid] @@ -2885,10 +2990,156 @@ def _declare_rigid_objects( self.prepare() return [item[3] for item in declarations] + def add_fluid_object(self, cfg: FluidObjectCfg) -> FluidObject: + """Declare a fixed-count Newton DFSPH fluid. + + Args: + cfg: Fluid source, initial state, physical and render properties. + + Returns: + A facade bound in place by ``prepare()``. + """ + from dexsim.spawn import FluidDesc, FluidRenderMode, FluidMeshSurfaceRenderDesc + from embodichain.lab.sim.spawn.descriptors import _pose_from_cfg + + if self.physics.name != "newton" or self.device.type != "cuda": + raise NotImplementedError("Fluid objects require Newton on CUDA.") + if ( + getattr(self, "_active_newton_solver_type", None) != "dexuni" + or self.sim_config.physics_cfg.requires_grad + ): + raise NotImplementedError( + "Fluid objects require non-differentiable DexUni." + ) + if not getattr(self.physics, "supports_dexuni_fluids", False): + raise NotImplementedError( + "Fluid objects require a DexSim build supporting DexUniSolverCfg.sph_cfg." + ) + if getattr(self, "_deformable_objects", {}): + raise NotImplementedError("DexUni fluids cannot coexist with deformables.") + if self.num_envs != 1 and not getattr( + self.physics, "supports_fluid_worlds", False + ): + raise NotImplementedError( + "Batch fluids require a DexSim SPH build with world isolation and selected-world reset." + ) + if self.spawn_result is not None: + raise NotImplementedError("Declare fluids before prepare().") + if not isinstance(cfg, FluidObjectCfg): + raise TypeError("Expected FluidObjectCfg.") + cfg = cfg.copy() + cfg.validate() + cfg.__post_init__() + if ( + self.sim_config.render_cfg.renderer == "hybrid" + or SimulationManager._get_process_renderer() == "hybrid" + ): + raise NotImplementedError( + "Newton fluid rendering requires renderer='fast-rt' or 'rt'. " + "Hybrid does not publish the OptiX liquid geometry to its Vulkan primary surface. " + "Use renderer='no-render' for physics-only runs. " + "Select FastRT or OfflineRT before creating the first World; " + "start a new process if Hybrid is already initialized." + ) + if cfg.render.mode == "none" and self.has_native_renderer: + raise NotImplementedError( + "Fluid mode='none' currently requires renderer='no-render'." + ) + if not cfg.uid or cfg.uid in self.asset_uids: + raise ValueError( + "Fluid uid must be nonempty and unique across scene assets." + ) + for other in self._fluid_objects.values(): + if (cfg.density, cfg.particle_spacing) != ( + other.cfg.density, + other.cfg.particle_spacing, + ): + raise ValueError("All fluids must share density and particle_spacing.") + configured = self.sim_config.physics_cfg.solver_cfg + configured = ( + configured.get("sph_cfg") + if isinstance(configured, Mapping) + else getattr(configured, "sph_cfg", None) + ) + from dexsim.engine.newton_physics import SPHSolverCfg + + defaults = SPHSolverCfg() + for key, value in ( + ("rest_density", cfg.density), + ("particle_diameter", cfg.particle_spacing), + ): + existing = ( + configured.get(key) + if isinstance(configured, Mapping) + else getattr(configured, key, None) + ) + if ( + existing is not None + and not np.isclose(existing, getattr(defaults, key), rtol=1e-7, atol=0) + and not np.isclose(existing, value, rtol=1e-7, atol=0) + ): + raise ValueError( + f"Fluid {key} conflicts with solver_cfg.sph_cfg.{key}." + ) + points = cfg.spawn.sample(cfg.particle_spacing) + pose = _pose_from_cfg(cfg) + if cfg.spawn.reference_uid is not None: + reference = self._rigid_objects.get(cfg.spawn.reference_uid) + if reference is None: + raise ValueError( + "Fluid reference_uid must name an already declared rigid object." + ) + pose = _pose_from_cfg(reference.cfg) @ pose + mesh_surface = FluidMeshSurfaceRenderDesc(**cfg.render.mesh_surface) + material = None + if self.has_native_renderer and cfg.render.material is not None: + material_cfg = cfg.render.material.copy() + material_cfg.uid = f"{cfg.uid}_fluid_material" + material = self.create_visual_material(material_cfg).inst.mat + descriptor = FluidDesc( + name=cfg.uid, + positions=points, + pose=pose, + density=cfg.density, + particle_spacing=cfg.particle_spacing, + particle_radius=cfg.particle_radius, + render_radius=cfg.render.render_radius, + render_mode=FluidRenderMode( + "particles" if cfg.render.mode == "none" else cfg.render.mode + ), + mesh_surface=mesh_surface, + render_material=material, + per_env=True, + ) + fluid = FluidObject(cfg, self.device) + fluid._initialize_spawn_source( + points, pose[:3, :3], on_write=self._invalidate_fluid_state + ) + self._spawn_scene.declare("fluid_object", cfg.uid, descriptor, facade=fluid) + self._fluid_objects[cfg.uid] = fluid + self.notify_visualization_topology_changed() + return fluid + + def _invalidate_fluid_state(self, env_ids: Sequence[int] | None = None) -> None: + self.physics.invalidate_fluid_state(env_ids=env_ids) + if self._render_state_published is not None: + self._render_state_published = False + + def get_fluid_object(self, uid: str) -> FluidObject | None: + """Return the fluid registered under ``uid``, or None.""" + return self._fluid_objects.get(uid) + + def get_fluid_diagnostics(self, uid: str) -> dict[str, object]: + """Return detached diagnostics for a prepared fluid without stepping.""" + fluid = self._fluid_objects[uid] + if fluid.is_declared: + raise RuntimeError("Fluid diagnostics require prepare().") + return self.physics.fluid_diagnostics(fluid) + def add_deformable_object(self, cfg: DeformableObjectCfg) -> DeformableObject: """Declare a volume or surface deformable in the scene. - Deformables are DexSim 0.5 typed particle sets owned by the Newton + Deformables are DexSim typed particle sets owned by the Newton Spawn scene. The Default backend is intentionally unsupported. Args: @@ -2922,6 +3173,8 @@ def add_deformable_object(self, cfg: DeformableObjectCfg) -> DeformableObject: "Newton deformable particle sets currently require a CUDA device." ) solver_type = self._active_newton_solver_type + if solver_type == "dexuni" and getattr(self, "_fluid_objects", {}): + raise NotImplementedError("DexUni fluids cannot coexist with deformables.") supported_solvers = {"dexuni", "xpbd", "semi_implicit", "vbd"} if solver_type not in supported_solvers: raise NotImplementedError( @@ -4059,8 +4312,8 @@ def add_sensor(self, sensor_cfg: SensorCfg) -> BaseSensor: sensor_factory, ContactSensor ): self.prepare() - # ``solver_type='auto'`` is resolved while preparing the Spawn - # scene, so the early capability check above is not sufficient. + # DexUni resolves its contact path while preparing the Spawn scene, + # so the early capability check above is not sufficient. if not self.physics.supports_contact_sensor: raise NotImplementedError( f"ContactSensor is not supported by the {self.physics.name} " @@ -5020,6 +5273,9 @@ def reset_objects_state( for uid, rigid_obj_group in self._rigid_object_groups.items(): if uid not in excluded_uids: rigid_obj_group.reset(env_ids) + for uid, fluid_obj in self._fluid_objects.items(): + if uid not in excluded_uids: + fluid_obj.reset(env_ids) for uid, deformable_obj in self._deformable_objects.items(): if uid not in excluded_uids: deformable_obj.reset(env_ids) @@ -5205,6 +5461,7 @@ def _deferred_destroy(self) -> None: "_rigid_objects", "_rigid_object_groups", "_deformable_objects", + "_fluid_objects", "_articulations", "_robots", ): @@ -5267,6 +5524,7 @@ def _sever_wrapper_refs(obj_registry): _sever_wrapper_refs("_constraints") _sever_wrapper_refs("_rigid_object_groups") _sever_wrapper_refs("_deformable_objects") + _sever_wrapper_refs("_fluid_objects") _sever_wrapper_refs("_articulations") _sever_wrapper_refs("_robots") _sever_wrapper_refs("_sensors") diff --git a/embodichain/lab/sim/spawn/descriptors.py b/embodichain/lab/sim/spawn/descriptors.py index 7956b9e774..f6d1a4565f 100644 --- a/embodichain/lab/sim/spawn/descriptors.py +++ b/embodichain/lab/sim/spawn/descriptors.py @@ -37,7 +37,7 @@ import numbers import os import warnings -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from embodichain.lab.sim.cfg.source import _asset_file_path @@ -55,7 +55,6 @@ MaterialDesc, NewtonCollisionDesc, NewtonJointDesc, - NewtonParticleContactMaterialDesc, ObjectDesc, RenderDesc, RigidBodyPhysicsDesc, @@ -75,6 +74,7 @@ DefaultRigidBodyPropertiesCfg, MassPropertiesCfg, NewtonCollisionPropertiesCfg, + NewtonJointDrivePropertiesCfg, NewtonRigidBodyMaterialCfg, RigidBodyMaterialCfg, RigidBodyPhysicsCfg, @@ -503,15 +503,95 @@ def _mesh_geometry_from_cfg(shape: MeshCfg, *, segment_name: str) -> GeometryDes ) -def _particle_contact_material( +def _particle_contact_material_options( value: Mapping[str, float] | None, -) -> NewtonParticleContactMaterialDesc | None: - """Validate and lower a deformable's DexUni contact material.""" +) -> dict[str, Any]: + """Lower optional object materials without requiring the new API on old wheels.""" if value is None: - return None + return {} if not isinstance(value, Mapping): raise TypeError("Deformable contact_material must be a mapping.") - return NewtonParticleContactMaterialDesc(**dict(value)) + import dexsim.spawn as spawn + + material_type = getattr(spawn, "NewtonParticleContactMaterialDesc", None) + if material_type is None: + raise RuntimeError( + "Deformable contact_material requires DexSim per-object particle materials." + ) + return {"contact_material": material_type(**dict(value))} + + +def _explicit_tet_mesh_from_cfg( + cfg: VolumeDeformableObjectCfg, uid: str +) -> dict[str, Any]: + """Validate an explicit tetrahedral mesh and build its render surface. + + Mirrors DexSim ``SceneBuilder.create_soft_mesh``: particles keep the + order of ``shape.vertices`` and the render mesh holds only surface nodes. + """ + from dexsim.spawn.soft_body_meshing import ( + SoftBodyMeshingStats, + extract_tet_surface_triangles, + ) + + shape = cfg.shape + if shape.vertices is None or ( + not _is_missing(shape.fpath) + and shape.fpath is not None + and bool(str(shape.fpath).strip()) + ): + raise ValueError( + "Volume deformable tetrahedra require an array-backed shape with " + "vertices and no fpath." + ) + positions = np.asarray(shape.vertices, dtype=np.float32) + if positions.ndim != 2 or positions.shape[1:] != (3,) or len(positions) == 0: + raise ValueError("Volume deformable vertices must have shape (N, 3).") + if not np.isfinite(positions).all(): + raise ValueError("Volume deformable vertices must be finite.") + tetrahedra = np.asarray(cfg.tetrahedra) + if tetrahedra.dtype.kind not in "iu": + raise TypeError("Volume deformable tetrahedra must be integer indices.") + tetrahedra = tetrahedra.reshape(-1, 4) + if tetrahedra.min() < 0 or tetrahedra.max() >= len(positions): + raise ValueError("Volume deformable tetrahedra index outside the vertices.") + tetrahedra = tetrahedra.astype(np.int32) + if shape.triangles is None: + surface_triangles = extract_tet_surface_triangles(tetrahedra) + else: + surface_triangles = np.asarray(shape.triangles) + if surface_triangles.dtype.kind not in "iu": + raise TypeError("Volume deformable triangles must be integer indices.") + surface_triangles = surface_triangles.reshape(-1, 3) + if surface_triangles.min() < 0 or surface_triangles.max() >= len(positions): + raise ValueError("Volume deformable triangles index outside the vertices.") + surface_triangles = surface_triangles.astype(np.int32) + if shape.normals is not None or shape.uv_coords is not None: + raise ValueError( + "Volume deformable tetrahedra do not support shape normals or uv_coords." + ) + surface_ids = np.unique(surface_triangles) + render_geometry = GeometryDesc.mesh( + vertices=positions[surface_ids], + triangles=np.searchsorted(surface_ids, surface_triangles).astype(np.int32), + segment_name=uid, + ) + return { + "positions": positions, + "tetrahedra": tetrahedra, + "surface_triangles": surface_triangles, + "render_geometry": render_geometry, + "meshing_stats": SoftBodyMeshingStats( + source_kind="tet_mesh", + source_vertex_count=len(positions), + source_triangle_count=len(surface_triangles), + proxy_vertex_count=0, + proxy_triangle_count=0, + particle_count=len(positions), + tetrahedron_count=len(tetrahedra), + surface_triangle_count=len(surface_triangles), + ), + } def volume_deformable_desc_from_cfg( @@ -521,7 +601,14 @@ def volume_deformable_desc_from_cfg( ) -> tuple[SoftBodyDesc, dict[str, MaterialDesc]]: """Translate a volume deformable into a Newton particle-set descriptor.""" uid = _required_uid(cfg.uid, "Volume deformable") - geometry = _mesh_geometry_from_cfg(cfg.shape, segment_name=uid) + explicit_tets = ( + None if cfg.tetrahedra is None else _explicit_tet_mesh_from_cfg(cfg, uid) + ) + geometry = ( + _mesh_geometry_from_cfg(cfg.shape, segment_name=uid) + if explicit_tets is None + else explicit_tets["render_geometry"] + ) material_ref, material_entry = _compile_visual_material( uid, cfg.shape.visual_material ) @@ -556,7 +643,7 @@ def volume_deformable_desc_from_cfg( ), physics=SoftBodyPhysicsDesc( volume_density=density, - contact_material=_particle_contact_material(attrs.contact_material), + **_particle_contact_material_options(attrs.contact_material), k_mu=youngs / (2.0 * (1.0 + poissons)), k_lambda=(youngs * poissons / ((1.0 + poissons) * (1.0 - 2.0 * poissons))), k_damp=float(attrs.elasticity_damping), @@ -570,14 +657,28 @@ def volume_deformable_desc_from_cfg( for item in fields(surface_props) }, ), - meshing=SoftBodyMeshingDesc( - proxy_simplify_target=cfg.meshing.triangle_simplify_target, - proxy_remesh_resolution=cfg.meshing.triangle_remesh_resolution, - voxel_resolution=cfg.meshing.simulation_mesh_resolution, - voxel_num_relaxation_iters=cfg.meshing.voxel_num_relaxation_iters, - voxel_rel_min_tet_volume=cfg.meshing.voxel_rel_min_tet_volume, - voxel_surface_dist_ratio=cfg.meshing.voxel_surface_dist_ratio, - embedding_impl=cfg.meshing.embedding_impl, + meshing=( + None + if explicit_tets is not None + else SoftBodyMeshingDesc( + proxy_simplify_target=cfg.meshing.triangle_simplify_target, + proxy_remesh_resolution=cfg.meshing.triangle_remesh_resolution, + voxel_resolution=cfg.meshing.simulation_mesh_resolution, + voxel_num_relaxation_iters=cfg.meshing.voxel_num_relaxation_iters, + voxel_rel_min_tet_volume=cfg.meshing.voxel_rel_min_tet_volume, + voxel_surface_dist_ratio=cfg.meshing.voxel_surface_dist_ratio, + embedding_impl=cfg.meshing.embedding_impl, + ) + ), + **( + {} + if explicit_tets is None + else { + "positions": explicit_tets["positions"], + "tetrahedra": explicit_tets["tetrahedra"], + "surface_triangles": explicit_tets["surface_triangles"], + "meshing_stats": explicit_tets["meshing_stats"], + } ), particle_flags=_particle_flags_from_cfg(cfg.particle_flags), particle_radius=particle_radius, @@ -588,6 +689,27 @@ def volume_deformable_desc_from_cfg( return descriptor, materials +def _pneumatic_cloth_options( + values: Mapping[str, Any] | None, +) -> dict[str, Any]: + """Lower an optional cavity without importing unsupported wheel symbols.""" + if values is None: + return {} + if not isinstance(values, Mapping): + raise TypeError("Cloth pneumatic must be a mapping when configured.") + from dexsim import spawn + + native_type = getattr(spawn, "PneumaticPhysicsDesc", None) + if native_type is None or "pneumatic" not in { + item.name for item in fields(ClothPhysicsDesc) + }: + raise NotImplementedError( + "Cloth pneumatic requires a DexSim build with PneumaticPhysicsDesc " + "and ClothPhysicsDesc.pneumatic support." + ) + return {"pneumatic": native_type(**dict(values))} + + def surface_deformable_desc_from_cfg( cfg: SurfaceDeformableObjectCfg, *, @@ -616,6 +738,9 @@ def surface_deformable_desc_from_cfg( attrs = cfg.attrs surface_props = attrs.surface_props density = float(attrs.density) + rest_angle = None if attrs.edge_rest_angle is None else float(attrs.edge_rest_angle) + if rest_angle is not None and not math.isfinite(rest_angle): + raise ValueError("Cloth edge_rest_angle must be finite when set.") particle_radius = ( None if cfg.particle_radius is None else float(cfg.particle_radius) ) @@ -649,7 +774,9 @@ def surface_deformable_desc_from_cfg( visual_binding_mode=cfg.visual_binding_mode, physics=ClothPhysicsDesc( surface_density=density, - contact_material=_particle_contact_material(attrs.contact_material), + **({"edge_rest_angle": rest_angle} if rest_angle is not None else {}), + **_pneumatic_cloth_options(attrs.pneumatic), + **_particle_contact_material_options(attrs.contact_material), add_springs=bool(attrs.add_springs), spring_ke=attrs.spring_ke, spring_kd=attrs.spring_kd, @@ -690,6 +817,10 @@ def articulation_desc_from_cfg( newton_solver_type=newton_solver_type, ) fixed_base, self_collision_enabled = _articulation_root_values(cfg) + compensation = _articulation_gravity_compensation(cfg, newton_solver_type) + gravity_options = ( + {} if compensation is None else {"newton_gravity_compensation": compensation} + ) return ArticulationDesc( name=_articulation_uid(cfg.uid, str(path)), pose=_pose_from_cfg(cfg), @@ -704,9 +835,32 @@ def articulation_desc_from_cfg( urdf_read_inertia=True, per_env=per_env, body_scale=_vector3(cfg.body_scale, field_name="body_scale"), + **gravity_options, ) +def _articulation_gravity_compensation( + cfg: ArticulationCfg, newton_solver_type: str | None +) -> float | None: + """Validate explicit Newton articulation creation intent.""" + value = cfg.root_props.newton_gravity_compensation + if value is None: + return None + if ( + not isinstance(value, numbers.Real) + or not np.isfinite(value) + or not 0 <= value <= 1 + ): + raise ValueError("newton_gravity_compensation must be finite and in [0, 1].") + if newton_solver_type is None: + raise ValueError("newton_gravity_compensation requires the Newton backend.") + if not hasattr(ArticulationDesc, "newton_gravity_compensation"): + raise RuntimeError( + "newton_gravity_compensation requires DexSim dev f32785317 or newer." + ) + return float(value) + + def _validate_articulation_rigid_physics( cfg: ArticulationCfg, *, @@ -803,6 +957,9 @@ def configure_articulation_desc( f"Articulation source {desc.name!r} must be resolved before " "configuration." ) + compensation = _articulation_gravity_compensation(cfg, newton_solver_type) + if compensation is not None: + desc.newton_gravity_compensation = compensation preserve_source_physics = cfg.resolve_asset_physics_mode() == "preserve" setattr(desc, "_embodichain_preserve_source_physics", preserve_source_physics) for link in desc.links: @@ -964,6 +1121,14 @@ def configure_articulation_desc( newton=newton_desc, newton_target_mode=joint_target_modes.get(joint_name), ) + if ( + newton_desc.damping is not None + and desc.get_joint_desc(joint_name).newton.damping != newton_desc.damping + ): + raise RuntimeError( + "DexSim did not retain passive_damping; install DexSim dev " + "f32785317 or newer." + ) _apply_articulation_newton_gap_default(desc, newton_solver_type) return desc @@ -1220,6 +1385,27 @@ def _compile_joint_properties( setattr(default_desc, default_field, scalar) setattr(newton_desc, newton_field, scalar) + if ( + isinstance(cfg.joint_drive_props, NewtonJointDrivePropertiesCfg) + and cfg.joint_drive_props.passive_damping is not None + ): + if newton_solver_type is None: + raise ValueError("passive_damping requires the Newton backend.") + matches = _joint_property_matches( + cfg.joint_drive_props.passive_damping, + joint_names, + property_name="passive_damping", + control_parts=control_parts, + ) + for joint_name, value in matches: + if ( + not isinstance(value, numbers.Real) + or not np.isfinite(value) + or value < 0 + ): + raise ValueError("passive_damping must be finite and nonnegative.") + joint_properties[joint_name][1].damping = float(value) + # Solvers that ignore Newton's target-mode enum still consume drive gains. # Masking inactive components makes NONE, EFFORT, and VELOCITY deterministic # across the currently supported solver set. @@ -1239,9 +1425,12 @@ def _compile_joint_properties( if newton_solver_type is None else newton_solver_type.replace("-", "_").lower() ) - if normalized_solver not in {None, "dexuni", "mujoco_warp", "mjwarp"} and any( - mode == 1 for mode in joint_target_modes.values() - ): + if normalized_solver not in { + None, + "mujoco_warp", + "mjwarp", + "dexuni", + } and any(mode == 1 for mode in joint_target_modes.values()): warnings.warn( f"Newton solver {newton_solver_type!r} does not consume " "joint_target_mode. POSITION is emulated with its configured " diff --git a/embodichain/lab/sim/spawn/scene.py b/embodichain/lab/sim/spawn/scene.py index bbb97ec79a..8b9904a92c 100644 --- a/embodichain/lab/sim/spawn/scene.py +++ b/embodichain/lab/sim/spawn/scene.py @@ -39,6 +39,7 @@ "articulation", "soft_object", "cloth_object", + "fluid_object", ] @@ -195,7 +196,22 @@ def declare( self._assets[uid] = declaration return - if kind == "rigid_object_group": + if kind == "fluid_object": + with self._native_material_scope(): + declaration.descriptor = self.builder.add_fluid( + descriptor.name, + descriptor.positions, + pose=descriptor.pose, + density=descriptor.density, + particle_spacing=descriptor.particle_spacing, + particle_radius=descriptor.particle_radius, + render_radius=descriptor.render_radius, + render_mode=descriptor.render_mode, + mesh_surface=descriptor.mesh_surface, + material=descriptor.render_material, + per_env=descriptor.per_env, + ) + elif kind == "rigid_object_group": with self._native_material_scope(): declaration.descriptor = tuple( self.builder.add_object(member) for member in descriptor @@ -283,7 +299,7 @@ def remove(self, uid: str) -> None: if getattr(declaration, "declaration_only", False): del self._assets[uid] return - if declaration.kind in {"soft_object", "cloth_object"}: + if declaration.kind in {"soft_object", "cloth_object", "fluid_object"}: raise NotImplementedError( "DexSim Spawn does not yet expose pending removal for " f"{declaration.kind.replace('_', ' ')}." diff --git a/embodichain/lab/sim/spawn/usd.py b/embodichain/lab/sim/spawn/usd.py index 5b0699fd90..3c1980aa07 100644 --- a/embodichain/lab/sim/spawn/usd.py +++ b/embodichain/lab/sim/spawn/usd.py @@ -40,6 +40,7 @@ _compile_rigid_physics, _compile_visual_material, _articulation_root_values, + _articulation_gravity_compensation, _pose_from_cfg, _required_uid, _resolve_rigid_physics, @@ -219,6 +220,7 @@ def articulation_desc_from_usd( newton_solver_type: str | None = None, ) -> tuple[ArticulationDesc, dict[str, MaterialDesc]]: """Select an articulation by exact prim path, or require a singleton stage.""" + compensation = _articulation_gravity_compensation(cfg, newton_solver_type) preserve_asset_physics = cfg.resolve_asset_physics_mode() == "preserve" if not preserve_asset_physics: _validate_articulation_rigid_physics( @@ -250,6 +252,8 @@ def articulation_desc_from_usd( fixed_base_default=bool(desc.fixed_base), self_collision_default=desc.enable_self_collision, ) + if compensation is not None: + desc.newton_gravity_compensation = compensation return desc, materials diff --git a/embodichain/lab/visualization/scene_exporter.py b/embodichain/lab/visualization/scene_exporter.py index f3e08f2e8e..1eb8ddaca8 100644 --- a/embodichain/lab/visualization/scene_exporter.py +++ b/embodichain/lab/visualization/scene_exporter.py @@ -772,10 +772,29 @@ def _prepare_overlays(self, overlays: SceneOverlays | None) -> SceneOverlays: else None ) marker_frames = self._capture_axis_marker_overlays(reserved_frame_ids) - if overlays is None: - return SceneOverlays(frames=marker_frames) + overlays = overlays or SceneOverlays() + fluid_points = [] + for uid, fluid in getattr(self._sim, "_fluid_objects", {}).items(): + if fluid.is_declared or fluid.cfg.render.mode == "none": + continue + state = fluid.data.particle_pos_w + for env_id in self._env_ids: + points = state[env_id].detach().cpu().numpy().copy() + if fluid.cfg.render.clip_below_z is not None: + points = points[points[:, 2] >= fluid.cfg.render.clip_below_z] + fluid_points.append( + PointCloudOverlay( + overlay_id=f"fluid/{uid}/{env_id}", + points=points, + point_size=2 + * ( + fluid.cfg.render.render_radius + or fluid.cfg.particle_spacing / 2 + ), + ) + ) point_clouds: list[PointCloudOverlay] = [] - for point_cloud in overlays.point_clouds: + for point_cloud in overlays.point_clouds + tuple(fluid_points): point_count = point_cloud.points.shape[0] if point_count <= self.cfg.point_cloud_max_points: point_clouds.append(point_cloud) diff --git a/examples/sim/atomic_skill/__main__.py b/examples/sim/atomic_skill/__main__.py index 82deb2ecfe..68bf90d11f 100644 --- a/examples/sim/atomic_skill/__main__.py +++ b/examples/sim/atomic_skill/__main__.py @@ -155,8 +155,8 @@ def _resolve_sample(args: argparse.Namespace) -> AtomicSkillRunCfg: not math.isfinite(cfg.arena_space) or cfg.arena_space <= 0 ): raise ValueError("--arena-space must be finite and positive") - if not cfg.device: - raise ValueError("--device must be a nonempty device string") + if cfg.device is not None and not cfg.device: + raise ValueError("--device must be a nonempty device string when specified") if not 0 <= cfg.seed < 2**32: raise ValueError("--seed must be in [0, 2**32)") if args.output_dir is not None: diff --git a/examples/sim/atomic_skill/axis_align.py b/examples/sim/atomic_skill/axis_align.py index d58d404c7f..4d490c6b69 100644 --- a/examples/sim/atomic_skill/axis_align.py +++ b/examples/sim/atomic_skill/axis_align.py @@ -36,6 +36,8 @@ from embodichain.lab.sim.shapes import CubeCfg from embodichain.utils import logger from examples.sim.atomic_skill.support import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, add_ur5_gripper_robot, clone_local_pose_from_first_env, create_affordance_sampling_context, @@ -150,7 +152,14 @@ def run(args: argparse.Namespace) -> None: sim = create_tutorial_simulation(args) robot = add_ur5_gripper_robot(sim, tcp_point="grasp") obj = create_align_object(sim) - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS + if args.robot == "ur5" and sim.physics.solver_type == "dexuni" + else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) initialize_pre_pick_robot_pose(robot, obj, hand_open) motion_gen = create_toppra_motion_generator( robot, diff --git a/examples/sim/atomic_skill/coordinated_pickment.py b/examples/sim/atomic_skill/coordinated_pickment.py index d67b32d904..618825a364 100644 --- a/examples/sim/atomic_skill/coordinated_pickment.py +++ b/examples/sim/atomic_skill/coordinated_pickment.py @@ -161,6 +161,8 @@ class PickmentObjectPreset: PICKMENT_HAND_INTERP_STEPS = 10 PICKMENT_HOLD_STEPS = 4 ROBOTIQ_2F_140_CLOSE_QPOS = 0.7 +# Calibrated for the plastic tray and UR5/DH PGI one-way contact. +DEXUNI_TRAY_CLOSE_QPOS = 0.023 TRAJECTORY_SIM_STEPS = 4 @@ -189,6 +191,8 @@ def parse_arguments() -> argparse.Namespace: def create_dual_robot( sim: SimulationManager, robot_type: TutorialRobot, + *, + newton_gripper_friction: float | None = None, ) -> Robot: """Create the selected dual-arm robot with its matching grippers.""" return add_dual_tutorial_robot( @@ -198,6 +202,7 @@ def create_dual_robot( urdf_name=f"dual_{robot_type}_coordinated_pickment", tcp_z=GRIPPER_TCP_Z, solver="pytorch", + newton_gripper_friction=newton_gripper_friction, ) @@ -395,6 +400,12 @@ def run_coordinated_pickment_demo( hand_close_qpos = ( ROBOTIQ_2F_140_CLOSE_QPOS if args.robot == "ur10" else preset.hand_close_qpos ) + if ( + args.robot == "ur5" + and args.object == "plastic_tray" + and sim.physics.solver_type == "dexuni" + ): + hand_close_qpos = DEXUNI_TRAY_CLOSE_QPOS left_open, left_close = get_hand_open_close_qpos( robot, hand_control_part="left_eef", @@ -529,8 +540,27 @@ def main() -> None: sim = create_tutorial_simulation( args, arena_space=3.0, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" + and args.robot == "ur5" + and args.object == "plastic_tray" + else None + ), + ) + use_vbd_grasp = ( + args.robot == "ur5" + and args.object == "plastic_tray" + and sim.physics.solver_type == "dexuni" + ) + robot = create_dual_robot( + sim, + args.robot, + newton_gripper_friction=4.0 if use_vbd_grasp else None, ) - robot = create_dual_robot(sim, args.robot) run_coordinated_pickment_demo(args, sim, robot) diff --git a/examples/sim/atomic_skill/coordinated_placement.py b/examples/sim/atomic_skill/coordinated_placement.py index 0ee6661b72..b6fb406353 100644 --- a/examples/sim/atomic_skill/coordinated_placement.py +++ b/examples/sim/atomic_skill/coordinated_placement.py @@ -24,7 +24,9 @@ from __future__ import annotations import argparse +import hashlib import sys +import tempfile import time from pathlib import Path @@ -34,8 +36,10 @@ import numpy as np import torch +import trimesh from scipy.spatial.transform import Rotation as SciRotation +from embodichain.data.constants import EMBODICHAIN_DEFAULT_DATA_ROOT from embodichain.lab.sim import SimulationManager from embodichain.lab.sim.atomic_skills import ( ControlPartCommandProfile, @@ -61,6 +65,9 @@ build_support_object_target_pose, build_placing_object_target_pose, ) +from embodichain_benchmarks.motion_generation.scenarios._placement_qualification import ( + _placement_input_geometry, +) from examples.sim.atomic_skill.scenario import ( add_dual_tutorial_robot, compute_local_bounds, @@ -87,10 +94,10 @@ run_tutorial, ) -DEFAULT_MESH_FRAME_CORRECTION_EULER_DEG = (-90.0, 0.0, 0.0) -# DexSim imports this pan GLB with gym raw +Z mapped to local -Y. The +90deg X -# correction therefore makes the pan opening point upward in world Z. -PAN_MESH_FRAME_CORRECTION_EULER_DEG = (90.0, 0.0, 0.0) +# Bake the GLB geometry into the authored Z-up frame before native loading. +# Preserve the gym table/bread orientation; turn the pan's opening upward. +DEFAULT_MESH_FRAME_CORRECTION_EULER_DEG = (0.0, 0.0, 0.0) +PAN_MESH_FRAME_CORRECTION_EULER_DEG = (180.0, 0.0, 0.0) PAN_WORLD_YAW_CORRECTION_DEG = 270.0 @@ -121,12 +128,19 @@ def transform_baseline_pose( PAN_MESH_PATH = f"{PLACEMENT_ASSET_ROOT}/pan.glb" BREAD_LABEL = "bread" PAN_LABEL = "pan" +# Leave opening clearance inside the PGI fingers' roughly 73 mm contact span. +BREAD_MESH_SCALE = 1.4 GRIPPER_TCP_Z = 0.121 PICK_SAMPLE_INTERVAL = 100 COORDINATED_SAMPLE_INTERVAL = 120 -ROBOT_INIT_POS = (1.85, 0.0, 0.1) +BASELINE_ROBOT_INIT_POS = (1.85, 0.0, 0.1) BASELINE_TABLE_TOP_Z = 0.3621708124799265 SCENE_Z_OFFSET = TABLE_TOP_Z - BASELINE_TABLE_TOP_Z +ROBOT_INIT_POS = ( + BASELINE_ROBOT_INIT_POS[0], + BASELINE_ROBOT_INIT_POS[1], + BASELINE_ROBOT_INIT_POS[2] + SCENE_Z_OFFSET, +) BASELINE_TABLE_INIT_POS = ( 0.00014585733079742588, 0.00023304896730074557, @@ -174,16 +188,22 @@ def transform_baseline_pose( mesh_frame_correction_euler_deg=PAN_MESH_FRAME_CORRECTION_EULER_DEG, world_yaw_correction_deg=PAN_WORLD_YAW_CORRECTION_DEG, ) -PAN_INIT_POS = (PAN_INIT_POS[0], PAN_INIT_POS[1], TABLE_TOP_Z + 0.001) -BREAD_GRASP_Z_CLEARANCE = 0.018 +# The bread is about 37 mm thick at the selected mesh scale. +# Seat both objects clear of the surface before Newton builds its contacts. +BREAD_INIT_POS = (BREAD_INIT_POS[0], BREAD_INIT_POS[1], TABLE_TOP_Z + 0.045) +PAN_INIT_POS = (PAN_INIT_POS[0], PAN_INIT_POS[1], TABLE_TOP_Z + 0.008) +BREAD_GRASP_Z_CLEARANCE = 0.015 PAN_GRASP_Z_CLEARANCE = 0.0 +# Use the straight outer handle rather than the rounded basin neck. +PAN_GRASP_HANDLE_TIP_SHIFT = 0.055 PAN_HANDLE_CLOSE_QPOS = 0.045 +DEXUNI_PAN_HANDLE_CLOSE_QPOS = 0.033 PAN_PICK_SAMPLE_INTERVAL = 130 PAN_PICK_HAND_INTERP_STEPS = 32 -BREAD_TARGET_HEIGHT_OFFSET = 0.1 +BREAD_TARGET_HEIGHT_OFFSET = 0.0 SUPPORT_TARGET_HEIGHT_OFFSET = 0.0 PICK_APPROACH_DISTANCE = 0.12 -PLACE_LIFT_HEIGHT = 0.10 +PLACE_LIFT_HEIGHT = 0.05 TRAJECTORY_SIM_STEPS = 8 @@ -215,22 +235,51 @@ def create_dual_robot( urdf_name=f"dual_{robot_type}_coordinated_placement", tcp_z=GRIPPER_TCP_Z, init_pos=ROBOT_INIT_POS, + newton_gripper_friction=4.0 if sim.is_newton_backend else None, + newton_mimic_stabilizer=True if sim.is_newton_backend else None, ) +def _placement_mesh_path(data_path: str) -> str: + """Cache a textured OBJ with the same authored Z-up frame in every renderer.""" + source = Path(resolve_cached_data_path(data_path)) + digest = hashlib.sha256(source.read_bytes()).hexdigest()[:20] + cache_dir = ( + Path(EMBODICHAIN_DEFAULT_DATA_ROOT) + / "derived" + / "coordinated_placement_z_up_v1" + / digest + ) + target = cache_dir / "mesh.obj" + if not target.is_file(): + cache_dir.parent.mkdir(parents=True, exist_ok=True) + # Publish the OBJ, MTL and textures together so concurrent examples + # cannot observe a partially exported asset. + with tempfile.TemporaryDirectory(dir=cache_dir.parent) as staging: + mesh = trimesh.load(str(source), force="mesh", process=False) + mesh.export(Path(staging) / target.name) + try: + Path(staging).rename(cache_dir) + except OSError: + if not target.is_file(): + raise + return str(target) + + def create_table(sim: SimulationManager) -> RigidObject: """Create the table mesh from the bread-pan gym project.""" return sim.add_rigid_object( cfg=RigidObjectCfg( uid="table", - shape=MeshCfg(fpath=resolve_cached_data_path(TABLE_MESH_PATH)), + shape=MeshCfg(fpath=_placement_mesh_path(TABLE_MESH_PATH)), attrs=create_tutorial_rigid_body_physics( mass=10.0, dynamic_friction=0.9, static_friction=0.95, restitution=0.01, ), - body_type="kinematic", + # This fixture never moves; static shapes also enter DexUni's MuJoCo view. + body_type="static", init_pos=list(TABLE_INIT_POS), init_rot=list(TABLE_INIT_ROT), ) @@ -243,7 +292,7 @@ def create_bread(sim: SimulationManager) -> RigidObject: cfg=RigidObjectCfg( uid="bread", shape=MeshCfg( - fpath=resolve_cached_data_path(BREAD_MESH_PATH), + fpath=_placement_mesh_path(BREAD_MESH_PATH), compute_uv=False, collision=MeshCollisionCfg( approximation="convex_decomposition", @@ -252,6 +301,10 @@ def create_bread(sim: SimulationManager) -> RigidObject: ), attrs=create_tutorial_rigid_body_physics( mass=0.01, + dynamic_friction=0.97, + static_friction=0.99, + angular_damping=2.0, + linear_damping=1.0, contact_offset=0.003, rest_offset=0.001, restitution=0.01, @@ -260,7 +313,7 @@ def create_bread(sim: SimulationManager) -> RigidObject: max_depenetration_velocity=10.0, newton_contact=sim.is_newton_backend, ), - body_scale=(1.75, 1.75, 1.75), + body_scale=(BREAD_MESH_SCALE,) * 3, init_pos=list(BREAD_INIT_POS), init_rot=list(BREAD_INIT_ROT), ) @@ -273,7 +326,7 @@ def create_pan(sim: SimulationManager) -> RigidObject: cfg=RigidObjectCfg( uid="pan", shape=MeshCfg( - fpath=resolve_cached_data_path(PAN_MESH_PATH), + fpath=_placement_mesh_path(PAN_MESH_PATH), compute_uv=False, collision=MeshCollisionCfg( approximation="convex_decomposition", @@ -401,8 +454,11 @@ def run_coordinated_placement_demo( sim.prepare() settle_object(sim, bread, step=0) settle_object(sim, pan, step=0) + sim.update(step=10) bread_pose_batch = clone_local_pose_from_first_env(bread) pan_pose_batch = clone_local_pose_from_first_env(pan) + initial_bread_z = bread_pose_batch[:, 2, 3].clone() + initial_pan_z = pan_pose_batch[:, 2, 3].clone() bread.clear_dynamics() pan.clear_dynamics() bread_pose = bread_pose_batch[0].to(device=sim.device, dtype=torch.float32) @@ -422,7 +478,11 @@ def run_coordinated_placement_demo( right_open, right_close = get_hand_open_close_qpos( robot, hand_control_part="right_eef", - close_qpos=PAN_HANDLE_CLOSE_QPOS, + close_qpos=( + DEXUNI_PAN_HANDLE_CLOSE_QPOS + if sim.physics.solver_type == "dexuni" + else PAN_HANDLE_CLOSE_QPOS + ), ) left_open, left_close = get_hand_open_close_qpos( robot, @@ -483,6 +543,7 @@ def run_coordinated_placement_demo( sim.device, z_clearance=PAN_GRASP_Z_CLEARANCE, ) + pan_grasp_pose[:3, 3] += PAN_GRASP_HANDLE_TIP_SHIFT * pan_grasp_pose[:3, 1] pick_invocations = ( engine.make_invocation( "pick_up", @@ -517,15 +578,12 @@ def run_coordinated_placement_demo( f"Compile both pick-ups cost time: {time.time() - start_time:.2f} seconds" ) if len(pick_compiled.skill_plans) != len(pick_invocations): - logger.log_warning("Failed to compile both pick-up trajectories.") - return + raise RuntimeError("Failed to compile both pick-up trajectories.") left_pick_result, right_pick_result = pick_compiled.skill_plans if not left_pick_result.plan_success.all(): - logger.log_warning("Failed to plan left bread pick-up trajectory.") - return + raise RuntimeError("Failed to plan left bread pick-up trajectory.") if not pick_compiled.plan_success.all(): - logger.log_warning("Failed to plan right pan pick-up trajectory.") - return + raise RuntimeError("Failed to plan right pan pick-up trajectory.") left_pick_trajectory = left_pick_result.joint_trajectory right_pick_trajectory = right_pick_result.joint_trajectory if left_pick_trajectory is None or right_pick_trajectory is None: @@ -566,6 +624,11 @@ def log_trajectory_execution(step_idx: int, total_steps: int) -> None: on_trajectory_step=log_trajectory_execution, record=False, ) + bread_lift = bread.get_local_pose(to_matrix=True)[:, 2, 3] - initial_bread_z + if bool((bread_lift < 0.5 * left_pick_options.lift_height).any()): + raise RuntimeError( + f"Bread pickup did not lift the object: bread_lift={bread_lift.tolist()}." + ) bread.clear_dynamics() replay_trajectory( sim, @@ -582,16 +645,29 @@ def log_trajectory_execution(step_idx: int, total_steps: int) -> None: pan.clear_dynamics() # Reconcile the projected task state with measurements before compiling # placement. This keeps the second stage robust to pick-up execution error. - bread_pose_batch = clone_local_pose_from_first_env(bread).to( - device=sim.device, dtype=torch.float32 + bread_pose_batch = ( + bread.get_local_pose(to_matrix=True) + .clone() + .to(device=sim.device, dtype=torch.float32) ) - pan_pose_batch = clone_local_pose_from_first_env(pan).to( - device=sim.device, dtype=torch.float32 + pan_pose_batch = ( + pan.get_local_pose(to_matrix=True) + .clone() + .to(device=sim.device, dtype=torch.float32) ) bread.clear_dynamics() pan.clear_dynamics() bread_pose = bread_pose_batch[0] pan_pose = pan_pose_batch[0] + bread_lift = bread_pose_batch[:, 2, 3] - initial_bread_z + pan_lift = pan_pose_batch[:, 2, 3] - initial_pan_z + if bool((bread_lift < 0.5 * left_pick_options.lift_height).any()) or bool( + (pan_lift < 0.5 * right_pick_options.lift_height).any() + ): + raise RuntimeError( + "Both objects must be lifted before coordinated placement: " + f"bread_lift={bread_lift.tolist()}, pan_lift={pan_lift.tolist()}." + ) bread_held_state = compute_actual_held_state( robot, bread_semantics, @@ -609,8 +685,14 @@ def log_trajectory_execution(step_idx: int, total_steps: int) -> None: held_objects = dict(state.task.held_objects) held_objects["left_arm"] = bread_held_state held_objects["right_arm"] = pan_held_state + # Use measured arm kinematics, but retain the grasp commands. A blocked + # jaw's measured position is more open than its commanded preload; + # replaying that measurement would relax the grasp during placement. + placement_start_qpos = robot.get_qpos().clone() + placement_start_qpos[:, robot.get_joint_ids("left_eef")] = left_close + placement_start_qpos[:, robot.get_joint_ids("right_eef")] = right_close state = state.project( - qpos=robot.get_qpos().clone(), + qpos=placement_start_qpos, task=TaskState( batch_size=state.batch_size, device=state.robot.qpos.device, @@ -677,8 +759,7 @@ def log_trajectory_execution(step_idx: int, total_steps: int) -> None: f"{time.time() - start_time:.2f} seconds" ) if not coordinated_success.all(): - logger.log_warning("Failed to plan coordinated placement trajectory.") - return + raise RuntimeError("Failed to plan coordinated placement trajectory.") log_skill_plan( robot, "coordinated_placement", @@ -719,6 +800,41 @@ def log_trajectory_execution(step_idx: int, total_steps: int) -> None: (0.0, 0.0, 1.0), ), ) + bread_final_pose = bread.get_local_pose(to_matrix=True) + pan_final_pose = pan.get_local_pose(to_matrix=True) + bread_world = ( + bread_vertices.unsqueeze(0) @ bread_final_pose[:, :3, :3].transpose(1, 2) + + bread_final_pose[:, None, :3, 3] + ) + bread_bottom_z = bread_world[:, :, 2].amin(dim=1) + placement = _placement_input_geometry( + bread_vertices, + bread.get_triangles(env_ids=[0])[0], + pan_vertices, + pan.get_triangles(env_ids=[0])[0], + bread_final_pose, + pan_final_pose, + maximum_support_gap_m=0.008, + ) + supported = torch.tensor( + [row["qualified"] for row in placement["rows"]], + device=sim.device, + ) + raised_pan = pan_final_pose[:, 2, 3] > TABLE_TOP_Z + 0.03 + raised_bread = bread_bottom_z > TABLE_TOP_Z + 0.02 + if not bool((supported & raised_pan & raised_bread).all()): + raise RuntimeError( + "Bread must be released into the raised pan: " + f"mesh_evidence={placement['rows']}, " + f"bread_bottom_z={bread_bottom_z.tolist()}, " + f"pan_z={pan_final_pose[:, 2, 3].tolist()}." + ) + logger.log_info( + "Measured placement: " + f"bread_bottom_z={bread_bottom_z.tolist()}, " + f"pan_z={pan_final_pose[:, 2, 3].tolist()}, " + f"mesh_evidence={placement['rows']}" + ) if wait_for_user: input("Press Enter to exit the simulation...") @@ -729,6 +845,22 @@ def main() -> None: sim = create_tutorial_simulation( args, arena_space=3.0, + newton_solver_options=( + { + "coupling": "two_way", + # DexSim's native two-way pick/place example uses soft contacts + # without rigid contact history and increases the proxy mass. + "coupling_options": {"mass_scale": 4.0, "proxy_relaxation": 0.5}, + "vbd_options": { + "iterations": 12, + "friction_epsilon": 1e-4, + "rigid_contact_hard": False, + "rigid_contact_history": False, + }, + } + if args.physics == "newton" + else None + ), ) robot = create_dual_robot(sim, args.robot) run_coordinated_placement_demo(args, sim, robot) diff --git a/examples/sim/atomic_skill/defaults.py b/examples/sim/atomic_skill/defaults.py index e9af1b4f77..07d70034cf 100644 --- a/examples/sim/atomic_skill/defaults.py +++ b/examples/sim/atomic_skill/defaults.py @@ -76,7 +76,7 @@ class AtomicSkillRunCfg: variants: int = 1 expansion: str = "both" seed: int = 0 - device: str = "cpu" + device: str | None = None renderer: str = "auto" headless: bool = False arena_space: float | None = None diff --git a/examples/sim/atomic_skill/move_held_object_cup.py b/examples/sim/atomic_skill/move_held_object_cup.py index 5ce2f3d8fa..5c837e188e 100644 --- a/examples/sim/atomic_skill/move_held_object_cup.py +++ b/examples/sim/atomic_skill/move_held_object_cup.py @@ -27,6 +27,7 @@ sys.path.insert(0, str(_REPO_ROOT)) import torch +import trimesh from embodichain.data import get_data_path from embodichain.lab.sim.atomic_skills import ( @@ -43,6 +44,7 @@ from embodichain.lab.sim.shapes import MeshCfg, MeshCollisionCfg from embodichain.utils import logger from examples.sim.atomic_skill.support import ( + DEFAULT_GRIPPER_CLOSE_QPOS, add_tutorial_robot, broadcast_pose_batch, clone_local_pose_from_first_env, @@ -74,6 +76,8 @@ MOVE_HELD_OBJECT_SAMPLE_INTERVAL = 120 HAND_INTERP_STEPS = 12 POST_TRAJECTORY_STEPS = 240 +# Candidate for this cup and UR5/DH PGI; transport orientation remains unaligned. +DEXUNI_CUP_CLOSE_QPOS = 0.0115 def parse_arguments() -> argparse.Namespace: @@ -87,11 +91,16 @@ def parse_arguments() -> argparse.Namespace: def create_pick_object(sim) -> RigidObject: """Create and settle the paper cup used by the tutorial.""" + mesh = trimesh.load_mesh(get_data_path(OBJECT_MESH_PATH), process=False) + # Author cup proportions in the mesh; physical body scale must be uniform. + mesh.apply_scale((0.75, 0.75, 1.0)) obj = sim.add_rigid_object( cfg=RigidObjectCfg( uid="paper_cup", shape=MeshCfg( - fpath=get_data_path(OBJECT_MESH_PATH), + vertices=mesh.vertices, + triangles=mesh.faces, + normals=mesh.vertex_normals, collision=MeshCollisionCfg( approximation="convex_decomposition", max_hulls=16, @@ -104,7 +113,6 @@ def create_pick_object(sim) -> RigidObject: newton_contact=sim.is_newton_backend, ), init_pos=[*OBJECT_XY, OBJECT_INITIAL_Z], - body_scale=(0.75, 0.75, 1.0), ) ) sim.prepare() @@ -129,15 +137,35 @@ def make_object_target_pose(device: torch.device) -> torch.Tensor: def main() -> None: """Plan MoveEndEffector -> PickUp -> MoveHeldObject.""" args = parse_arguments() - sim = create_tutorial_simulation(args) - robot = add_tutorial_robot(sim, args.robot) + sim = create_tutorial_simulation( + args, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" and args.robot == "ur5" + else None + ), + ) + use_vbd_grasp = args.robot == "ur5" and sim.physics.solver_type == "dexuni" + robot = add_tutorial_robot( + sim, + args.robot, + newton_gripper_friction=4.0 if use_vbd_grasp else None, + ) obj = create_pick_object(sim) sim.prepare() motion_gen = create_curobo_motion_generator( robot, planner=getattr(args, "planner", "trapezoidal"), ) - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUP_CLOSE_QPOS if use_vbd_grasp else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) engine = create_simulation_atomic_skill_engine( motion_generator=motion_gen, diff --git a/examples/sim/atomic_skill/pickup.py b/examples/sim/atomic_skill/pickup.py index 36d9e981c1..d0fd9e36eb 100644 --- a/examples/sim/atomic_skill/pickup.py +++ b/examples/sim/atomic_skill/pickup.py @@ -34,6 +34,8 @@ from embodichain.lab.sim.shapes import CubeCfg from embodichain.utils import logger from examples.sim.atomic_skill.support import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, add_tutorial_robot, clone_local_pose_from_first_env, create_affordance_sampling_context, @@ -136,7 +138,14 @@ def run(args: argparse.Namespace) -> None: robot = add_tutorial_robot(sim, args.robot, tcp_point="grasp") obj = create_pick_object(sim) sim.prepare() - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS + if args.robot == "ur5" and sim.physics.solver_type == "dexuni" + else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) initialize_pre_pick_robot_pose(robot, obj, hand_open) motion_gen = create_curobo_motion_generator( robot, diff --git a/examples/sim/atomic_skill/pickup_rubiks_cube.py b/examples/sim/atomic_skill/pickup_rubiks_cube.py index 5154ab2d38..907a942e7d 100644 --- a/examples/sim/atomic_skill/pickup_rubiks_cube.py +++ b/examples/sim/atomic_skill/pickup_rubiks_cube.py @@ -181,7 +181,13 @@ def create_pick_object(sim, asset_path: str) -> Articulation: sim.prepare() lock_turn_joint(cube) sim.update(step=10) - cube.clear_dynamics() + if sim.is_newton_backend: + # DexUni resets coupled solver history for the complete world, including + # the robot and free-root cube. Restore the cube's runtime lock afterwards. + sim.reset_objects_state() + lock_turn_joint(cube) + else: + cube.clear_dynamics() return cube @@ -211,7 +217,9 @@ def main() -> None: cube = create_pick_object(sim, args.asset_path) sim.prepare() hand_open, hand_close = get_hand_open_close_qpos(robot) - initialize_pre_pick_robot_pose(robot, cube, hand_open) + initialize_pre_pick_robot_pose( + robot, cube, hand_open, clear_dynamics=not sim.is_newton_backend + ) motion_gen = create_curobo_motion_generator( robot, use_cuda_graph=args.physics != "newton", diff --git a/examples/sim/atomic_skill/place.py b/examples/sim/atomic_skill/place.py index 7da359707d..0e33114316 100644 --- a/examples/sim/atomic_skill/place.py +++ b/examples/sim/atomic_skill/place.py @@ -38,6 +38,8 @@ from embodichain.utils import logger from embodichain.utils.math import matrix_from_euler from examples.sim.atomic_skill.support import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, create_affordance_sampling_context, add_tutorial_robot, broadcast_pose_batch, @@ -113,6 +115,7 @@ def parse_arguments() -> argparse.Namespace: def create_pick_object(sim) -> RigidObject: """Create a settled cube for the PickUp and Place sequence.""" + ground_clearance = 0.001 if sim.physics.solver_type == "dexuni" else 0.0 obj = sim.add_rigid_object( cfg=RigidObjectCfg( uid="cube", @@ -125,7 +128,7 @@ def create_pick_object(sim) -> RigidObject: angular_damping=0.2, newton_contact=sim.is_newton_backend, ), - init_pos=[*OBJECT_XY, 0.5 * OBJECT_SIZE[2]], + init_pos=[*OBJECT_XY, 0.5 * OBJECT_SIZE[2] + ground_clearance], ) ) sim.prepare() @@ -162,8 +165,28 @@ def run(args: argparse.Namespace) -> None: Args: args: Resolved standalone or configured host arguments. """ - sim = create_tutorial_simulation(args) - robot = add_tutorial_robot(sim, args.robot, tcp_point="grasp") + sim = create_tutorial_simulation( + args, + newton_solver_options=( + { + "vbd_options": { + "rigid_contact_history": True, + "rigid_avbd_contact_alpha": 0.5, + }, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" and args.robot == "ur5" + else None + ), + ) + use_vbd_grasp = args.robot == "ur5" and sim.physics.solver_type == "dexuni" + robot = add_tutorial_robot( + sim, + args.robot, + tcp_point="grasp", + newton_gripper_friction=4.0 if use_vbd_grasp else None, + arm_damping=1000.0 if use_vbd_grasp else None, + ) obj = create_pick_object(sim) sim.prepare() trajectory_strategy = getattr(args, "trajectory_strategy", "motion_gen") @@ -186,7 +209,12 @@ def run(args: argparse.Namespace) -> None: robot, planner=getattr(args, "planner", "trapezoidal"), ) - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS if use_vbd_grasp else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) initialize_pre_pick_robot_pose(robot, obj, hand_open) engine = create_simulation_atomic_skill_engine( diff --git a/examples/sim/atomic_skill/pour.py b/examples/sim/atomic_skill/pour.py index f3d4d3dc03..183e0fe86f 100644 --- a/examples/sim/atomic_skill/pour.py +++ b/examples/sim/atomic_skill/pour.py @@ -33,18 +33,20 @@ PourOptions, ) from embodichain.lab.sim.motion.planners import ( - TrapezoidalPlanOptions, ToppraPlanOptions, TrajectorySampleMethod, ) from embodichain.utils import logger from examples.sim.atomic_skill.axis_align import ( + OBJECT_SIZE, create_align_object, create_axis_align_semantics, ) from examples.sim.atomic_skill.support import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, create_affordance_sampling_context, - add_ur5_gripper_robot, + add_tutorial_robot, create_parallel_jaw_grasp_pose_generator, create_toppra_motion_generator, create_tutorial_argument_parser, @@ -67,7 +69,7 @@ HAND_INTERP_STEPS = 12 POST_TRAJECTORY_STEPS = 240 PICK_MOTION_SAMPLE_COUNT = PICK_SAMPLE_INTERVAL - HAND_INTERP_STEPS -OBJ_POSITION = (-0.5, 0.0, 0.0) +OBJ_POSITION = (-0.5, 0.0, 0.5 * OBJECT_SIZE[2]) RECORD_LOOK_AT = ( (-1.5, 0.2, 1.2), (-0.4, 0.0, 0.4), @@ -105,14 +107,8 @@ def _create_pick_motion_policy( sample_method=TrajectorySampleMethod.QUANTITY, sample_interval=PICK_MOTION_SAMPLE_COUNT, ) - elif planner == "trapezoidal": - plan_opts = TrapezoidalPlanOptions( - sample_method=TrajectorySampleMethod.QUANTITY, - sample_interval=PICK_MOTION_SAMPLE_COUNT, - ) - elif planner == "curobo": - # cuRobo owns its native sampling and does not use the joint-space - # sample-count options above. + elif planner in ("trapezoidal", "curobo"): + # Retain native waypoints before PickUp resamples its motion segments. plan_opts = None else: raise ValueError(f"Unsupported tutorial planner {planner!r}.") @@ -129,13 +125,34 @@ def run(args: argparse.Namespace) -> None: Args: args: Resolved standalone or configured host arguments. """ - sim = create_tutorial_simulation(args) - robot = add_ur5_gripper_robot(sim, tcp_point="grasp") - obj = create_align_object( + sim = create_tutorial_simulation( + args, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" + else None + ), + ) + use_vbd_grasp = sim.physics.solver_type == "dexuni" + robot = add_tutorial_robot( sim, - obj_position=OBJ_POSITION, + "ur5", + tcp_point="grasp", + newton_gripper_friction=4.0 if use_vbd_grasp else None, + ) + obj_position = list(OBJ_POSITION) + if use_vbd_grasp: + obj_position[2] += 0.001 + obj = create_align_object(sim, obj_position=obj_position) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS if use_vbd_grasp else DEFAULT_GRIPPER_CLOSE_QPOS + ), ) - hand_open, hand_close = get_hand_open_close_qpos(robot) initialize_pre_pick_robot_pose(robot, obj, hand_open) planner = getattr(args, "planner", "trapezoidal") motion_gen = create_toppra_motion_generator(robot, planner=planner) diff --git a/examples/sim/atomic_skill/scenario.py b/examples/sim/atomic_skill/scenario.py index 4de7926e87..c70efd78a5 100644 --- a/examples/sim/atomic_skill/scenario.py +++ b/examples/sim/atomic_skill/scenario.py @@ -267,6 +267,8 @@ def add_dual_tutorial_robot( hand_stiffness: float = 1e3, hand_damping: float = 1e2, hand_max_effort: float = 1e4, + newton_gripper_friction: float | None = None, + newton_mimic_stabilizer: bool | None = None, ) -> Robot: """Add a supported dual-arm tutorial robot to a simulation. @@ -291,6 +293,8 @@ def add_dual_tutorial_robot( hand_stiffness: Hand joint drive stiffness. hand_damping: Hand joint drive damping. hand_max_effort: Hand joint maximum effort. + newton_gripper_friction: Optional sliding friction for gripper links. + newton_mimic_stabilizer: Optional Newton mimic-follower stabilization. Returns: The added dual-arm robot instance. @@ -312,7 +316,11 @@ def add_dual_tutorial_robot( hand_damping=hand_damping, hand_max_effort=hand_max_effort, ) - configure_newton_gripper_contacts(sim, robot_cfg) + configure_newton_gripper_contacts(sim, robot_cfg, friction=newton_gripper_friction) + if newton_mimic_stabilizer is not None: + if not sim.is_newton_backend: + raise ValueError("Newton mimic stabilization requires the Newton backend.") + robot_cfg.root_props.newton_mimic_stabilizer = newton_mimic_stabilizer return sim.add_robot(cfg=robot_cfg) diff --git a/examples/sim/atomic_skill/slide.py b/examples/sim/atomic_skill/slide.py index cd191afef2..f385c7be37 100644 --- a/examples/sim/atomic_skill/slide.py +++ b/examples/sim/atomic_skill/slide.py @@ -93,10 +93,16 @@ def create_drawer( init_pos=DRAWER_POSITION, init_rot=DRAWER_ORIENTATION, init_qpos=(0.0,), - joint_drive_props=JointDrivePropertiesCfg(drive_type="none"), + joint_drive_props=JointDrivePropertiesCfg( + drive_type="force", + target_mode="velocity", + stiffness=0.0, + damping=5.0, + ), attrs=create_tutorial_rigid_body_physics( static_friction=1.0, dynamic_friction=1.0, + linear_damping=0.0, ), ) configure_newton_link_contacts( diff --git a/examples/sim/atomic_skill/support.py b/examples/sim/atomic_skill/support.py index 571945b808..2540ab0d18 100644 --- a/examples/sim/atomic_skill/support.py +++ b/examples/sim/atomic_skill/support.py @@ -123,6 +123,8 @@ (-2.0 * math.pi, 2.0 * math.pi) for _ in range(6) ) DEFAULT_GRIPPER_CLOSE_QPOS = 0.036 +# UR5/DH-PGI calibration for the 5 cm cube with one-way DexUni contacts. +DEXUNI_CUBE_CLOSE_QPOS = 0.013 NEWTON_GRASP_CONTACT_STIFFNESS = 4.0e4 NEWTON_GRASP_CONTACT_DAMPING = 4.0e2 # MuJoCo-Warp's default contact dimension (3) has no torsional friction. A @@ -1409,10 +1411,8 @@ def _tutorial_physics_cfg( ) -> PhysicsBackendCfg: """Build the shared physics configuration for atomic-skill tutorials. - Newton tutorials intentionally use MuJoCo Warp's native collision path. - It generates contacts inside every solver substep, so an external Newton - collision pipeline would be unused and would only allocate unnecessary - contact buffers. + DexUni owns collision detection and retains MuJoCo articulation drives. + Free rigid bodies use VBD with the solver's native one-way coupling. """ physics_cfg = physics_cfg_for_backend(backend) if isinstance(physics_cfg, NewtonPhysicsCfg): @@ -1421,20 +1421,21 @@ def _tutorial_physics_cfg( # contacts and fast-changing manipulator loads. DexSim sizes # contact/constraint buffers from the finalized scene, including the # contact dimensions authored on gripper and object surfaces. - # MultiCCD retains up to four contacts per gripper-mesh/object pair, - # which prevents a marginal two-finger grasp from sliding away. + # Retain the existing MuJoCo articulation tuning. Free-body contacts + # remain VBD-owned and do not inherit these MuJoCo contact options. physics_cfg.num_substeps = 20 physics_cfg.collision_cfg = None physics_cfg.solver_cfg = { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } return physics_cfg @@ -1444,6 +1445,7 @@ def create_tutorial_simulation( *, arena_space: float = 2.5, sun_direction: Sequence[float] = DEFAULT_TUTORIAL_SUN_DIRECTION, + newton_solver_options: Mapping[str, object] | None = None, ) -> SimulationManager: """Create the shared simulation setup used by atomic-skill tutorials. @@ -1453,10 +1455,17 @@ def create_tutorial_simulation( arena_space: Spacing between parallel simulation arenas in meters. sun_direction: Direction of the single global sun light. The vector points from the light toward the scene. + newton_solver_options: Scene-specific Newton solver options, applied + before model construction. Returns: A simulation manager with the tutorial key light configured. """ + physics_cfg = _tutorial_physics_cfg(getattr(args, "physics", "default")) + if newton_solver_options is not None: + if not isinstance(physics_cfg, NewtonPhysicsCfg): + raise ValueError("Newton solver options require the Newton backend.") + physics_cfg.solver_cfg = {**physics_cfg.solver_cfg, **newton_solver_options} if ( getattr(args, "arena_space", None) is not None and getattr(args, "run", None) is not None @@ -1470,7 +1479,7 @@ def create_tutorial_simulation( num_envs=args.num_envs, device=args.device, gpu_id=getattr(args, "gpu_id", 0), - physics_cfg=_tutorial_physics_cfg(getattr(args, "physics", "default")), + physics_cfg=physics_cfg, render_cfg=RenderCfg(renderer=args.renderer), physics_dt=1.0 / 100.0, arena_space=arena_space, @@ -1560,6 +1569,9 @@ def add_tutorial_robot( robot_type: TutorialRobot, init_pos: Sequence[float] = (0.0, 0.0, 0.0), init_qpos: Sequence[float] | None = None, + *, + newton_gripper_friction: float | None = None, + arm_damping: float | None = None, **kwargs: object, ) -> Robot: """Add a selected tutorial robot with the shared PGI gripper. @@ -1569,6 +1581,8 @@ def add_tutorial_robot( robot_type: Tutorial robot family to construct. init_pos: Root position of the robot in its arena. init_qpos: Optional full robot joint configuration. + newton_gripper_friction: Optional sliding friction for gripper links. + arm_damping: Optional arm drive damping applied before spawning. Returns: The added robot instance. @@ -1582,7 +1596,9 @@ def add_tutorial_robot( init_qpos=init_qpos, **kwargs, ) - configure_newton_gripper_contacts(sim, robot_cfg) + if arm_damping is not None: + robot_cfg.joint_drive_props.damping["arm"] = arm_damping + configure_newton_gripper_contacts(sim, robot_cfg, friction=newton_gripper_friction) return sim.add_robot(cfg=robot_cfg) @@ -1771,9 +1787,19 @@ def configure_newton_link_contacts( def configure_newton_gripper_contacts( sim: SimulationManager, robot_cfg: RobotCfg, + *, + friction: float | None = None, ) -> None: - """Configure Newton gripper contacts and recompute their source inertia.""" + """Configure Newton gripper contacts and recompute their source inertia. + + Args: + sim: Simulation manager that owns the robot. + robot_cfg: Robot configuration to update before spawning. + friction: Optional static and dynamic sliding-friction coefficient. + """ if not sim.is_newton_backend: + if friction is not None: + raise ValueError("Newton gripper friction requires the Newton backend.") return configure_newton_link_contacts( @@ -1782,9 +1808,11 @@ def configure_newton_gripper_contacts( group_name="newton_gripper_contacts", link_names_expr=[_GRIPPER_CONTACT_LINK_PATTERN], ) - robot_cfg.link_attrs["newton_gripper_contacts"].attrs.mass_props = ( - MassPropertiesCfg(recompute_inertia=True) - ) + attrs = robot_cfg.link_attrs["newton_gripper_contacts"].attrs + attrs.mass_props = MassPropertiesCfg(recompute_inertia=True) + if friction is not None: + attrs.material_props.static_friction = friction + attrs.material_props.dynamic_friction = friction def create_trapezoidal_motion_generator( @@ -2040,6 +2068,7 @@ def initialize_pre_pick_robot_pose( hand_open: torch.Tensor, *, height: float = 0.36, + clear_dynamics: bool = True, ) -> None: """Set a tutorial robot at a deterministic open-gripper pose above an object. @@ -2048,6 +2077,8 @@ def initialize_pre_pick_robot_pose( obj: Object below the pre-pick TCP pose. hand_open: Open gripper joint positions. height: World-Z coordinate for the pre-pick TCP pose. + clear_dynamics: Clear robot dynamics after initialization. Disable only + after the host has cleared the complete Newton articulation world. Raises: RuntimeError: If the pre-pick pose is unreachable. @@ -2066,7 +2097,8 @@ def initialize_pre_pick_robot_pose( for target in (False, True): robot.set_qpos(arm_qpos, name="arm", target=target) robot.set_qpos(hand_qpos, name="eef", target=target) - robot.clear_dynamics() + if clear_dynamics: + robot.clear_dynamics() def prepare_tutorial_scene( @@ -3029,6 +3061,7 @@ def create_tutorial_robot_cfg( "DEFAULT_AXIS_LEN", "DEFAULT_AXIS_SIZE", "DEFAULT_GRIPPER_CLOSE_QPOS", + "DEXUNI_CUBE_CLOSE_QPOS", "DEFAULT_TUTORIAL_SUN_DIRECTION", "DEFAULT_TUTORIAL_SUN_INTENSITY", "GRIPPER_HAND_JOINT_PATTERN", diff --git a/examples/sim/camera_ground_visibility.py b/examples/sim/camera_ground_visibility.py index 3ae91d2a2e..67effbe920 100644 --- a/examples/sim/camera_ground_visibility.py +++ b/examples/sim/camera_ground_visibility.py @@ -33,6 +33,10 @@ from embodichain.lab.sim.cfg import RenderCfg, RigidObjectCfg, VisualObjectCfg from embodichain.lab.sim.sensors import CameraCfg from embodichain.lab.sim.shapes import CubeCfg +from embodichain.lab.visualization import ( + add_viser_args_to_parser, + visualization_cfg_from_args, +) def main() -> None: @@ -41,6 +45,7 @@ def main() -> None: parser.add_argument( "--output", type=Path, default=Path("outputs/camera_ground_visibility") ) + add_viser_args_to_parser(parser) args = parser.parse_args() args.output.mkdir(parents=True, exist_ok=True) sim = SimulationManager( @@ -49,6 +54,7 @@ def main() -> None: device="cpu", num_envs=4, render_cfg=RenderCfg(renderer=args.renderer), + visualization=visualization_cfg_from_args(args), ) ) records = [] diff --git a/examples/sim/demo/fold_tshirt.py b/examples/sim/demo/fold_tshirt.py index 525f40ffca..d265084184 100644 --- a/examples/sim/demo/fold_tshirt.py +++ b/examples/sim/demo/fold_tshirt.py @@ -560,12 +560,10 @@ def friction_track(values: tuple[float, ...]) -> tuple[tuple[float, float], ...] w1_friction = friction_track((0.25, 1.2, 0.0, 0.25, 1.2, 0.0, 0.25)) table_friction = friction_track((0.5, 0.12, 0.5, 0.5, 0.12, 0.5, 0.5)) - # This global particle control is host-side. Graph mode keeps the initial - # soft_contact_mu while the graph-compatible rigid schedules still run. - if not use_cuda_graph: - sim.register_particle_contact_material_schedule( - {"dynamic_friction": particle_friction} - ) + # The object material track participates in DexUni graph replay. + sim.register_particle_contact_material_schedule( + {"dynamic_friction": particle_friction}, uid="shirt" + ) sim.register_contact_material_schedule( "w1", {"dynamic_friction": w1_friction}, diff --git a/examples/sim/demo/grasp_cup_to_caffe.py b/examples/sim/demo/grasp_cup_to_caffe.py index f4a34cb208..30ecc35614 100644 --- a/examples/sim/demo/grasp_cup_to_caffe.py +++ b/examples/sim/demo/grasp_cup_to_caffe.py @@ -92,13 +92,9 @@ def initialize_simulation(args) -> SimulationManager: """ physics_cfg = physics_cfg_for_backend(args.physics) if args.physics == "newton": - # This contact-heavy URDF scene needs Newton's collision pipeline; - # MuJoCo's native contact path is not reliable for these convex meshes. physics_cfg.solver_cfg = { - "solver_type": "mujoco_warp", - "use_mujoco_contacts": False, - "nconmax": 16384, - "njmax": 65536, + "solver_type": "dexuni", + "mujoco_options": {"nconmax": 16384, "njmax": 65536}, } config = SimulationManagerCfg( @@ -217,7 +213,8 @@ def create_table(sim: SimulationManager) -> RigidObject: attrs=RigidBodyPhysicsCfg( mass_props=MassPropertiesCfg(mass=0.5), ), - body_type="kinematic", + # This fixture never moves; static shapes also enter DexUni's MuJoCo view. + body_type="static", init_pos=[1.1, -0.5, 0.08], init_rot=[0.0, 0.0, 0.0], ) diff --git a/examples/sim/demo/scoop_ice.py b/examples/sim/demo/scoop_ice.py index 260da2d9c0..6208c809b4 100644 --- a/examples/sim/demo/scoop_ice.py +++ b/examples/sim/demo/scoop_ice.py @@ -81,9 +81,12 @@ def initialize_simulation(args): """ physics_cfg = physics_cfg_for_backend(args.physics) if args.physics == "newton": - # Hundreds of free bodies make dense Newton Hessian factorization - # expensive. CG avoids that factorization while retaining contacts. - physics_cfg.solver_cfg = {"solver_type": "mujoco_warp", "solver": "cg"} + physics_cfg.solver_cfg = { + "solver_type": "dexuni", + "mujoco_options": {"solver": "cg"}, + # Dense ice contacts exceed VBD's default per-body capacity of 64. + "vbd_options": {"rigid_body_contact_buffer_size": 256}, + } config = SimulationManagerCfg( headless=True, device=args.device, @@ -354,16 +357,19 @@ def create_ice_cubes(sim: SimulationManager): # Set visual material for ice cubes. # Explicit optical parameters select ice scattering in OfflineRT. - ice_mat = sim.create_visual_material( - cfg=VisualMaterialCfg( - base_color=[1.0, 1.0, 1.0, 1.0], - ior=1.31, - roughness=0.2, - parameters={"transmission": 1.0}, + ice_mat = None + if sim.has_native_renderer: + ice_mat = sim.create_visual_material( + cfg=VisualMaterialCfg( + base_color=[1.0, 1.0, 1.0, 1.0], + ior=1.31, + roughness=0.2, + parameters={"transmission": 1.0}, + ) ) - ) sim.prepare() - ice_cubes.set_visual_material(mat=ice_mat) + if ice_mat is not None: + ice_cubes.set_visual_material(mat=ice_mat) return ice_cubes diff --git a/examples/sim/motion/planners/curobo_planner.py b/examples/sim/motion/planners/curobo_planner.py index a212602169..3850769563 100644 --- a/examples/sim/motion/planners/curobo_planner.py +++ b/examples/sim/motion/planners/curobo_planner.py @@ -504,9 +504,9 @@ def _build_scene( init_rot=(0.0, 0.0, 0.0), ) ) + sim.prepare() finally: demo_block_mesh_path.unlink(missing_ok=True) - sim.prepare() if robot_type == "w1": # Keep the W1-specific IK diagnostic batched so it remains useful when @@ -863,7 +863,10 @@ def main(args: argparse.Namespace | None = None) -> None: binding, motion_policy, ), - ) + ), + context=engine.initial_context( + control_dt=sim.sim_config.physics_dt * args.step_repeat, + ), ) success = compiled.plan_success trajectory = compiled.trajectory.positions @@ -901,7 +904,10 @@ def main(args: argparse.Namespace | None = None) -> None: binding, motion_policy, ), - ) + ), + context=engine.initial_context( + control_dt=sim.sim_config.physics_dt * args.step_repeat, + ), ) success = compiled.plan_success trajectory = compiled.trajectory.positions diff --git a/examples/sim/motion/solvers/fep_solver.py b/examples/sim/motion/solvers/fep_solver.py index 7fc928f046..f838dad28f 100644 --- a/examples/sim/motion/solvers/fep_solver.py +++ b/examples/sim/motion/solvers/fep_solver.py @@ -128,7 +128,7 @@ def main() -> None: actual_color = (255, 140, 30) actual_rgba = np.array((*actual_color, 255), dtype=np.float32) / 255 actual_cloud = None - if not args.viser: + if not args.viser and sim.has_native_renderer: points = circle_world.reshape(-1, 3) sim.visualize_point_cloud( points, diff --git a/examples/sim/motion/trajectory_velocity_tracking.py b/examples/sim/motion/trajectory_velocity_tracking.py index 774ec8c93d..ce31cd170f 100644 --- a/examples/sim/motion/trajectory_velocity_tracking.py +++ b/examples/sim/motion/trajectory_velocity_tracking.py @@ -396,8 +396,7 @@ def main(args: argparse.Namespace | None = None) -> None: robot: Robot = sim.add_robot( FrankaPandaCfg.from_dict({"uid": "tracking_franka"}) ) - if sim.is_use_gpu_physics: - sim.init_gpu_physics() + sim.prepare() if not args.headless: sim.open_window() common_initial_qpos = robot.get_qpos().clone() diff --git a/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_geometry.py b/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_geometry.py index 08e26cd92a..5ed995ee98 100644 --- a/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_geometry.py +++ b/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_geometry.py @@ -42,22 +42,36 @@ PAN_TARGET_CENTER_XY = (-0.06, 0.0) PAN_TARGET_Z_LIFT = 0.06 BREAD_ON_PAN_CLEARANCE = 0.006 -PAN_HANDLE_LOCAL_Z_MIN = 0.04 -PAN_BASIN_LOCAL_Z_MAX = 0.04 +# The authored Z-up pan has its basin at negative Y and handle at positive Y. +PAN_HANDLE_LOCAL_Y_MIN = 0.06 +PAN_BASIN_LOCAL_Y_MAX = 0.04 PAN_HANDLE_ROOT_OFFSET = 0.035 BREAD_TARGET_WORLD_YAW_DEG = 0.0 -def get_pan_basin_vertices(pan_vertices: torch.Tensor) -> torch.Tensor: +def _pan_handle_axis(pan_frame: str) -> int: + if pan_frame == "authored_z_up": + return 1 + if pan_frame == "native_glb": + return 2 + raise ValueError(f"Unsupported pan mesh frame: {pan_frame!r}.") + + +def get_pan_basin_vertices( + pan_vertices: torch.Tensor, *, pan_frame: str = "authored_z_up" +) -> torch.Tensor: """Select basin vertices, falling back to the full mesh for an empty mask. Args: pan_vertices: Local pan mesh vertices with shape ``(N, 3)``. + pan_frame: ``authored_z_up`` for the example's OBJ, or ``native_glb`` + for the benchmark GLB loader's Y-up frame. Returns: - Vertices whose local z is within the configured basin height. + Vertices on the basin side of the handle attachment. """ - basin_vertices = pan_vertices[pan_vertices[:, 2] <= PAN_BASIN_LOCAL_Z_MAX] + axis = _pan_handle_axis(pan_frame) + basin_vertices = pan_vertices[pan_vertices[:, axis] <= PAN_BASIN_LOCAL_Y_MAX] if basin_vertices.numel() == 0: logger.log_warning("Pan basin vertex mask is empty; falling back to full mesh.") return pan_vertices @@ -131,6 +145,7 @@ def build_pan_handle_grasp_pose( device: torch.device, *, z_clearance: float = 0.006, + pan_frame: str = "authored_z_up", ) -> torch.Tensor: """Build a downward-facing grasp displaced toward the pan handle root. @@ -139,11 +154,15 @@ def build_pan_handle_grasp_pose( pan_vertices: Local pan mesh vertices with shape ``(N, 3)``. device: Device for the output transform. z_clearance: TCP clearance above the handle in metres. + pan_frame: Authored OBJ or native GLB mesh frame, as in + :func:`get_pan_basin_vertices`. Returns: A world TCP transform with shape ``(4, 4)``. """ - handle_vertices = pan_vertices[pan_vertices[:, 2] > PAN_HANDLE_LOCAL_Z_MIN] + axis = _pan_handle_axis(pan_frame) + threshold = PAN_HANDLE_LOCAL_Y_MIN if axis == 1 else 0.04 + handle_vertices = pan_vertices[pan_vertices[:, axis] >= threshold] if handle_vertices.numel() == 0: logger.log_warning( "Pan handle vertex mask is empty; falling back to full mesh." @@ -216,6 +235,7 @@ def build_placing_object_target_pose( device: torch.device, *, pan_triangles: torch.Tensor | None = None, + pan_frame: str = "authored_z_up", ) -> torch.Tensor: """Align the placing object over the pan basin with bottom clearance. @@ -228,6 +248,8 @@ def build_placing_object_target_pose( pan_triangles: Optional mesh topology. When provided, intersect the basin-center vertical ray with the actual inner support surface; a bounds-only support uses its upper bound. + pan_frame: Authored OBJ or native GLB mesh frame, as in + :func:`get_pan_basin_vertices`. Returns: A copied placing-object target with the shared basin-clearance policy. @@ -237,7 +259,7 @@ def build_placing_object_target_pose( BREAD_TARGET_WORLD_YAW_DEG, ) pan_basin_world = transform_points( - support_target_pose, get_pan_basin_vertices(pan_vertices) + support_target_pose, get_pan_basin_vertices(pan_vertices, pan_frame=pan_frame) ) basin_center_xy = 0.5 * ( pan_basin_world[:, :2].min(dim=0).values diff --git a/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_qualification.py b/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_qualification.py index d95a15ac3d..d03647fe23 100644 --- a/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_qualification.py +++ b/extensions/embodichain_benchmarks/motion_generation/scenarios/_placement_qualification.py @@ -26,7 +26,7 @@ from shapely.geometry import Polygon from shapely.ops import unary_union -from ._placement_geometry import PAN_BASIN_LOCAL_Z_MAX +from ._placement_geometry import PAN_BASIN_LOCAL_Y_MAX, _pan_handle_axis __all__: list[str] = [] @@ -34,6 +34,7 @@ _BARYCENTRIC_EPS = 1e-9 # One picometre resolves overlay roundoff without adding a physical margin. _PROJECTION_GRID_M = 1e-12 +_FLOOR_PLANE_TOLERANCE_M = 0.002 def _projection(triangles: np.ndarray): @@ -76,6 +77,9 @@ def _placement_input_geometry( support_triangles: torch.Tensor, placing_targets: torch.Tensor, support_targets: torch.Tensor, + *, + pan_frame: str = "authored_z_up", + maximum_support_gap_m: float | None = None, ) -> dict[str, object]: """Audit complete XY containment and sampled vertical gaps; never select rows. @@ -83,7 +87,18 @@ def _placement_input_geometry( frozen target transforms are copied to CPU once. The projected triangle unions retain holes and overhangs; neither object-origin nor AABB inclusion substitutes for footprint coverage. Heights are finite-sample evidence only. + ``pan_frame`` selects the authored OBJ or native GLB handle axis. + ``maximum_support_gap_m`` requires a nearby interior-floor witness when + qualifying settled geometry, rather than a collision-free target proposal. + All sampled gaps must still be nonnegative. Curved object undersides can + have larger gaps away from their supporting point. + Interior-floor samples lie within 2 mm of the pan's center floor plane. """ + axis = _pan_handle_axis(pan_frame) + if maximum_support_gap_m is not None and ( + not np.isfinite(maximum_support_gap_m) or maximum_support_gap_m < 0 + ): + raise ValueError("Maximum support gap must be finite and nonnegative.") meshes = {} arrays = [] for name, vertices, triangles in ( @@ -106,7 +121,7 @@ def _placement_input_geometry( "triangles_int64_le_sha256": hashlib.sha256(f.tobytes()).hexdigest(), } (bread, bread_faces), (pan, pan_faces) = arrays - basin_faces = pan_faces[(pan[pan_faces, 2] <= PAN_BASIN_LOCAL_Z_MAX).all(axis=1)] + basin_faces = pan_faces[(pan[pan_faces, axis] <= PAN_BASIN_LOCAL_Y_MAX).all(axis=1)] if not len(basin_faces): raise ValueError( "Placement input geometry has no triangles within the basin mask." @@ -151,11 +166,36 @@ def _placement_input_geometry( finite = np.isfinite(bottom) & np.isfinite(top) gaps = bottom[finite] - top[finite] minimum = float(gaps.min()) if len(gaps) else None + maximum = float(gaps.max()) if len(gaps) else None + floor_gap = None + if maximum_support_gap_m is not None: + # Locate the interior floor independently of rim contacts. Follow + # the pan's declared up axis so a tilted floor remains one plane. + center = basin.representative_point() + center_xy = np.array([[center.x, center.y]]) + center_z = _ray_heights(center_xy, basin_triangles, upper=True)[0] + normal = pan_pose[:3, 2 if axis == 1 else 1] + if np.isfinite(center_z) and abs(normal[2]) > 1e-6: + offsets = np.column_stack((samples[finite], top[finite])) - np.array( + [center.x, center.y, center_z] + ) + on_floor = np.abs(offsets @ normal) <= _FLOOR_PLANE_TOLERANCE_M + if on_floor.any(): + floor_gap = float(gaps[on_floor].min()) rows.append( { "env_index": row, "qualified": bool( - covered and finite.all() and minimum is not None and minimum >= 0 + covered + and finite.all() + and minimum is not None + and minimum >= 0 + and ( + maximum_support_gap_m is None + or ( + floor_gap is not None and floor_gap <= maximum_support_gap_m + ) + ) ), "footprint_covered": bool(covered), "footprint_area_m2": float(footprint.area), @@ -168,6 +208,8 @@ def _placement_input_geometry( "sample_count": len(samples), "samples_without_surface": int((~finite).sum()), "minimum_sampled_vertical_gap_m": minimum, + "maximum_sampled_vertical_gap_m": maximum, + "minimum_sampled_floor_gap_m": floor_gap, "negative_gap_samples": int((gaps < 0).sum()), "placing_target_pose": bread_pose.tolist(), "support_target_pose": pan_pose.tolist(), @@ -177,13 +219,17 @@ def _placement_input_geometry( "method": "scaled_mesh_projected_triangle_union_and_vertical_rays", "scope": "complete_xy_footprint_and_sampled_height_only_not_continuous_3d_collision_or_physical_success", "frame": "arena", + "pan_mesh_frame": pan_frame, "meshes": meshes, - "basin_local_z_max_m": PAN_BASIN_LOCAL_Z_MAX, + ( + "basin_local_y_max_m" if axis == 1 else "basin_local_z_max_m" + ): PAN_BASIN_LOCAL_Y_MAX, "basin_triangle_count": len(basin_faces), "sample_spacing_m": _SAMPLE_SPACING_M, "coverage_precision_grid_m": _PROJECTION_GRID_M, "samples": "all_mesh_vertices_xy_plus_footprint_boundary_and_interior_grid", "ray_barycentric_epsilon": _BARYCENTRIC_EPS, "minimum_gap_threshold_m": 0.0, + "maximum_gap_threshold_m": maximum_support_gap_m, "rows": rows, } diff --git a/extensions/embodichain_benchmarks/motion_generation/scenarios/atomic_dual_arm_providers.py b/extensions/embodichain_benchmarks/motion_generation/scenarios/atomic_dual_arm_providers.py index 58dfeabda5..16dff0ff94 100644 --- a/extensions/embodichain_benchmarks/motion_generation/scenarios/atomic_dual_arm_providers.py +++ b/extensions/embodichain_benchmarks/motion_generation/scenarios/atomic_dual_arm_providers.py @@ -1487,6 +1487,7 @@ def _placement_pose_row( support_vertices, device, z_clearance=float(config.get("support_grasp_z_clearance_m", 0.0)), + pan_frame=str(config.get("support_mesh_frame", "authored_z_up")), ) if placing_target_frame_pose is None: placing_target_frame_pose = placing_pose @@ -1502,6 +1503,7 @@ def _placement_pose_row( support_target, device, pan_triangles=support_triangles, + pan_frame=str(config.get("support_mesh_frame", "authored_z_up")), ) return { "placing_grasp_pose": placing_grasp, @@ -1728,6 +1730,7 @@ def generate_case( support.entity.get_triangles(env_ids=[0])[0], poses["placing_target_pose"], poses["support_target_pose"], + pan_frame=str(config.get("support_mesh_frame", "authored_z_up")), ) parts = _control_parts(config, ("placing", "support")) parameters: dict[str, object] = { diff --git a/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_franka_pgi_coordinated_placement.yaml b/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_franka_pgi_coordinated_placement.yaml index 5e6d50c365..b1f231159a 100644 --- a/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_franka_pgi_coordinated_placement.yaml +++ b/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_franka_pgi_coordinated_placement.yaml @@ -234,6 +234,7 @@ tracks: settle_steps: 100 skills: - id: coordinated_placement + support_mesh_frame: native_glb sample_count: 120 placing_height_offset_m: 0.1 support_height_offset_m: 0.0 diff --git a/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement.yaml b/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement.yaml index 122366a36f..67e381d5ae 100644 --- a/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement.yaml +++ b/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement.yaml @@ -110,6 +110,7 @@ tracks: settle_steps: 100 skills: - id: coordinated_placement + support_mesh_frame: native_glb sample_count: 120 placing_height_offset_m: 0.1 support_height_offset_m: 0.0 diff --git a/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement_spatial_generalization.yaml b/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement_spatial_generalization.yaml index 09f81d102a..f84be3b733 100644 --- a/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement_spatial_generalization.yaml +++ b/extensions/embodichain_benchmarks/motion_generation/suites/atomic_dual_ur5_pgi_coordinated_placement_spatial_generalization.yaml @@ -380,6 +380,7 @@ tracks: settle_steps: 100 skills: - id: coordinated_placement + support_mesh_frame: native_glb sample_count: 120 placing_height_offset_m: 0.1 support_height_offset_m: 0.0 diff --git a/extensions/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml b/extensions/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml index 00f75fe539..f906406fd0 100644 --- a/extensions/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml +++ b/extensions/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml @@ -13,15 +13,15 @@ physics_config: num_substeps: 20 collision_cfg: null solver_cfg: - solver_type: mujoco_warp - solver: newton - integrator: implicitfast - iterations: 20 - ls_iterations: 100 - cone: elliptic - impratio: 1000.0 - use_mujoco_contacts: true - enable_multiccd: true + solver_type: dexuni + mujoco_options: + solver: newton + integrator: implicitfast + iterations: 20 + ls_iterations: 100 + cone: elliptic + impratio: 1000.0 + enable_multiccd: true simulation: light: diff --git a/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml b/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml index 86e885ac9e..1533382eff 100644 --- a/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml +++ b/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml @@ -15,15 +15,15 @@ physics_config: num_substeps: 20 collision_cfg: null solver_cfg: - solver_type: mujoco_warp - solver: newton - integrator: implicitfast - iterations: 20 - ls_iterations: 100 - cone: elliptic - impratio: 1000.0 - use_mujoco_contacts: true - enable_multiccd: true + solver_type: dexuni + mujoco_options: + solver: newton + integrator: implicitfast + iterations: 20 + ls_iterations: 100 + cone: elliptic + impratio: 1000.0 + enable_multiccd: true simulation: light: diff --git a/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml b/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml index fdd5fab264..28354f9824 100644 --- a/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml +++ b/extensions/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml @@ -9,7 +9,13 @@ objective: task_program: program: task_program/program.yaml integration: task_program/integration.yaml - execution_policy: ../../../components/execution_policies/trajectory_open_loop.yaml + execution_policy: + component: ../../../components/execution_policies/trajectory_open_loop.yaml + overrides: + motion: + sample_count: 160 + recovery: + goal_rotation_threshold: 3.141592653589793 embodiment: component: ../../../components/embodiments/ur5_dh_pgi_140_80.yaml diff --git a/extensions/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml b/extensions/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml index baa1a285a2..b740e49238 100644 --- a/extensions/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml +++ b/extensions/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml @@ -6,7 +6,13 @@ environment: task_program: program: task_program/program.yaml integration: task_program/integration.yaml - execution_policy: ../../../components/execution_policies/trajectory_open_loop.yaml + execution_policy: + component: ../../../components/execution_policies/trajectory_open_loop.yaml + overrides: + motion: + sample_count: 160 + recovery: + goal_rotation_threshold: 3.141592653589793 embodiment: component: ../../../components/embodiments/ur5_dh_pgi_140_80.yaml diff --git a/scripts/benchmark/scene/fluid_render_sync.py b/scripts/benchmark/scene/fluid_render_sync.py new file mode 100644 index 0000000000..0e8a0b569c --- /dev/null +++ b/scripts/benchmark/scene/fluid_render_sync.py @@ -0,0 +1,489 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Compare fluid render clipping and optional native surface publication. + +Run: python -m scripts.benchmark.scene.fluid_render_sync --device cuda:0 --native +Inputs are device resident. Physics stepping is excluded. The optional native +cases measure bridge publication and a complete camera frame separately. +""" + +from __future__ import annotations + +import argparse +from collections.abc import Callable +from datetime import datetime +from importlib.metadata import PackageNotFoundError, version +from pathlib import Path +import platform +import statistics +import time +from types import SimpleNamespace + +import numpy as np +import psutil +import torch +import warp as wp + +from embodichain.lab.sim.physics.newton import NewtonPhysicsBackend + + +def _legacy_clip( + positions: torch.Tensor, ranges: tuple[tuple[int, int, float], ...] +) -> torch.Tensor: + """Reproduce the original clone and per-fluid boolean assignment.""" + output = positions.clone() + for start, end, height in ranges: + selected = output[start:end] + selected[selected[:, 2] < height] = float("nan") + return output + + +def _synchronize(device: torch.device) -> None: + """Complete all participating CUDA streams before recording wall time.""" + if device.type == "cuda": + torch.cuda.synchronize(device) + + +def _measure( + operation: Callable[[], object], device: torch.device, args: argparse.Namespace +) -> dict[str, object]: + """Measure steady-state completed calls and a separate retained output.""" + cuda = device.type == "cuda" + _synchronize(device) + initial = torch.cuda.memory_allocated(device) if cuda else 0 + start = time.perf_counter() + result = operation() + _synchronize(device) + first_call_ms = (time.perf_counter() - start) * 1000 + del result + for _ in range(args.warmup): + result = operation() + _synchronize(device) + del result + persistent = torch.cuda.memory_allocated(device) - initial if cuda else None + samples = [] + for _ in range(args.repeats): + _synchronize(device) + start = time.perf_counter() + result = operation() + _synchronize(device) + samples.append((time.perf_counter() - start) * 1000) + del result + + process = psutil.Process() + cpu_before = process.memory_info().rss + gpu_before = torch.cuda.memory_allocated(device) if cuda else 0 + if cuda: + torch.cuda.reset_peak_memory_stats(device) + result = operation() + _synchronize(device) + mib = 1024**2 + memory = { + "cpu_delta_mb": (process.memory_info().rss - cpu_before) / mib, + "gpu_delta_mb": ( + (torch.cuda.memory_allocated(device) - gpu_before) / mib if cuda else None + ), + "peak_gpu_mb": torch.cuda.max_memory_allocated(device) / mib if cuda else None, + "extra_peak_gpu_mb": ( + (torch.cuda.max_memory_allocated(device) - gpu_before) / mib + if cuda + else None + ), + "warmup_gpu_delta_mb": persistent / mib if persistent is not None else None, + } + del result + return { + "cost_time_ms": statistics.median(samples), + "p95_ms": float(np.percentile(samples, 95)), + "first_call_ms": first_call_ms, + **memory, + } + + +def _paired_latencies( + operations: dict[str, Callable[[], object]], + device: torch.device, + args: argparse.Namespace, +) -> dict[str, dict[str, float]]: + """Alternate equivalent calls to reduce GPU clock and thermal-order bias.""" + samples: dict[str, list[float]] = {name: [] for name in operations} + order = list(operations) + for index in range(args.warmup + args.repeats): + for name in order if index % 2 == 0 else reversed(order): + _synchronize(device) + start = time.perf_counter() + result = operations[name]() + _synchronize(device) + if index >= args.warmup: + samples[name].append((time.perf_counter() - start) * 1000) + del result + return { + name: { + "cost_time_ms": statistics.median(values), + "p95_ms": float(np.percentile(values, 95)), + } + for name, values in samples.items() + } + + +def _benchmark_clipping( + count: int, fluids: int, device: torch.device, args: argparse.Namespace +) -> tuple[list[dict[str, object]], list[dict[str, object]]]: + """Compare equivalent clipping masks, including partial particle ranges.""" + generator = torch.Generator(device=device).manual_seed(42) + positions = torch.randn((count, 3), device=device, generator=generator) + before = positions.clone() + # Multiple ranges leave gaps to exercise particles outside the clipped fluids. + ranges = tuple( + ( + count * i // fluids, + count * (i + 1) // fluids - (1 if fluids > 1 else 0), + 0.1 * i, + ) + for i in range(fluids) + ) + backend = NewtonPhysicsBackend(SimpleNamespace()) + operations = { + "legacy": lambda: _legacy_clip(positions, ranges), + "vectorized": lambda: backend._clip_fluid_render_positions(positions, ranges), + } + expected = positions.clone() + heights = torch.full((count,), float("-inf"), device=device) + for start, end, height in ranges: + heights[start:end] = height + expected[positions[:, 2] < heights] = float("nan") + perf_rows, metric_rows = [], [] + for algorithm, operation in operations.items(): + # Warmup is part of _measure; correctness checks must not prefill its cache. + perf = _measure(operation, device, args) + cutoff = backend._fluid_render_clip_z if algorithm == "vectorized" else None + perf["cutoff_cache_mb"] = ( + cutoff.numel() * cutoff.element_size() / 1024**2 + if cutoff is not None + else 0.0 + ) + actual = operation() + torch.testing.assert_close(actual, expected, equal_nan=True, rtol=0, atol=0) + torch.testing.assert_close(positions, before, rtol=0, atol=0) + identity = { + "scope": "clipping", + "particles": count, + "fluids": fluids, + "algorithm": algorithm, + } + perf_rows.append({**identity, **perf}) + metric_rows.append({**identity, "success_rate": 1.0, "source_unchanged": True}) + del actual + paired = _paired_latencies(operations, device, args) + for row in perf_rows: + row.update(paired[str(row["algorithm"])]) + return perf_rows, metric_rows + + +def _benchmark_native( + count: int, device: torch.device, args: argparse.Namespace +) -> tuple[list[dict[str, object]], list[dict[str, object]]]: + """Measure full native surface publication with identical quality settings.""" + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + from embodichain.lab.sim import SimulationManager, SimulationManagerCfg + from embodichain.lab.sim.cfg import ( + FluidObjectCfg, + FluidRenderCfg, + FluidSpawnCfg, + NewtonPhysicsCfg, + RenderCfg, + ) + from embodichain.lab.sim.sensors import CameraCfg + + side = int(np.ceil(count ** (1 / 3))) + points = np.column_stack(np.unravel_index(np.arange(count), (side,) * 3)) + points = np.asarray((points - side / 2) * 0.003, dtype=np.float32) + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device=str(device), + num_envs=1, + render_cfg=RenderCfg(renderer="fast-rt"), + physics_cfg=NewtonPhysicsCfg( + num_substeps=1, + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "sph_cfg": { + "sph_method": "dfsph", + "volume_map_boundary_enabled": False, + }, + }, + ), + ) + ) + fluid = camera = None + try: + fluid = sim.add_fluid_object( + FluidObjectCfg( + uid="water", + particle_spacing=0.003, + spawn=FluidSpawnCfg(positions=points), + render=FluidRenderCfg( + mode="mesh_surface", + clip_below_z=0.0, + mesh_surface={"gpu_enabled": True, "max_res": 128}, + ), + ) + ) + camera = sim.add_sensor( + CameraCfg( + uid="benchmark_camera", + width=320, + height=240, + intrinsics=(350, 350, 160, 120), + extrinsics=CameraCfg.ExtrinsicsCfg( + eye=(0.18, -0.24, 0.28), target=(0, 0, 0), up=(0, 0, 1) + ), + ) + ) + sim.prepare() + native = get_newton_backend(sim.spawn_result.world) + positions = wp.to_torch(native.state_0.particle_q) + before = positions.clone() + + def legacy() -> None: + # Include the manager/backend lookup and range traversal performed + # by the previous public sync_render_state implementation. + result = sim.spawn_result + current = get_newton_backend(result.world) + clipped_fluids = [ + item + for item in sim._fluid_objects.values() + if item.cfg.render.clip_below_z is not None + ] + producer = torch.cuda.current_stream(device) + warp_stream = wp.stream_to_torch(current.state_0.particle_q.device) + if producer.cuda_stream != warp_stream.cuda_stream: + producer.wait_stream(warp_stream) + clipped = wp.to_torch(current.state_0.particle_q).clone() + for item in clipped_fluids: + for entity in item._entities: + start = entity._physics_binding.particle_start + selected = clipped[start : start + entity.particle_count] + selected[selected[:, 2] < item.cfg.render.clip_below_z] = float( + "nan" + ) + staging = torch.cuda.default_stream(device) + if staging.cuda_stream != producer.cuda_stream: + staging.wait_stream(producer) + try: + current.render_sync.sync( + result.world, + current.state_0.body_q, + wp.from_torch(clipped, dtype=wp.vec3), + simulation_time=current.sim_time, + ) + finally: + staging.synchronize() + if sim._render_state_published is not None: + sim._render_state_published = True + + perf_rows, metric_rows = [], [] + for scope in ("native bridge publication", "native camera frame"): + operations = {} + for algorithm, publish in { + "legacy": legacy, + "vectorized": sim.sync_render_state, + }.items(): + + def operation(publish: Callable[[], None] = publish) -> object: + publish() + if scope == "native camera frame": + sim._world.render_camera_group([camera.group_id]) + camera.update(fetch_only=True) + return camera.get_data()["color"] + return None + + operations[algorithm] = operation + perf = _measure(operation, device, args) + cutoff = ( + sim.physics._fluid_render_clip_z + if algorithm == "vectorized" + else None + ) + perf["cutoff_cache_mb"] = ( + cutoff.numel() * cutoff.element_size() / 1024**2 + if cutoff is not None + else 0.0 + ) + torch.testing.assert_close(positions, before, rtol=0, atol=0) + identity = { + "scope": scope, + "particles": count, + "fluids": 1, + "algorithm": algorithm, + } + perf_rows.append({**identity, **perf}) + metric_rows.append( + {**identity, "success_rate": 1.0, "source_unchanged": True} + ) + paired = _paired_latencies(operations, device, args) + for row in perf_rows: + if row["scope"] == scope: + row.update(paired[str(row["algorithm"])]) + return perf_rows, metric_rows + finally: + fluid = camera = None + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + + +def _write_report( + path: Path, + perf_rows: list[dict[str, object]], + metric_rows: list[dict[str, object]], + notes: list[str], +) -> None: + """Write exactly three tables, including every evaluated algorithm.""" + leaderboard = [] + for algorithm in sorted({str(row["algorithm"]) for row in metric_rows}): + results = [row for row in metric_rows if row["algorithm"] == algorithm] + leaderboard.append( + { + "algorithm": algorithm, + "overall_success_rate": statistics.mean( + float(row["success_rate"]) for row in results + ), + "cases": len(results), + } + ) + leaderboard.sort(key=lambda row: -float(row["overall_success_rate"])) + leaderboard = [{"rank": index, **row} for index, row in enumerate(leaderboard, 1)] + lines = ["# Fluid render synchronization benchmark", "", *notes, ""] + for title, rows in ( + ("Time & Memory", perf_rows), + ("Success & Other Metrics", metric_rows), + ("Leaderboard", leaderboard), + ): + lines.extend([f"## {title}", ""]) + if rows: + headers = list(rows[0]) + lines.extend( + [ + "| " + " | ".join(headers) + " |", + "| " + " | ".join(["---"] * len(headers)) + " |", + ] + ) + for row in rows: + values = [ + f"{row[key]:.6f}" if isinstance(row[key], float) else str(row[key]) + for key in headers + ] + lines.append("| " + " | ".join(values) + " |") + else: + lines.extend(["| status |", "| --- |", "| skipped |"]) + lines.append("") + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("\n".join(lines), encoding="utf-8") + + +def run_all_benchmarks(args: argparse.Namespace) -> None: + """Run clipping/native scopes and save one reproducible Markdown report.""" + device = torch.device(args.device) + notes = [ + f"- Generated: {datetime.now().isoformat(timespec='seconds')}", + f"- CPU: {platform.processor()}; Torch {torch.__version__}; Warp {wp.__version__}", + f"- Device: {device}; dtype=float32; seed=42; CPU threads={torch.get_num_threads()}", + f"- Warmup={args.warmup}; repeats={args.repeats}; median/p95 completed-call wall time.", + "- Latency uses paired alternating implementation order. Memory/cache statistics use a separate warmed pass.", + "- First-call timing precedes warmup and includes cutoff-cache setup; process/World startup and input generation are excluded.", + "- Device-resident inputs; device-wide CUDA synchronization; no physics steps. Native camera scope includes 320x240 FastRT rendering and image fetch.", + "- CPU memory is retained RSS delta. GPU memory covers the Torch allocator only, excluding native/Warp allocations.", + "- Incremental peak is measured separately after warmup. Cutoff cache reports logical buffer bytes; warmup GPU delta may include unrelated lazy allocation/release.", + "- Clipping quality checks exact positions/NaN masks and source preservation. Native success checks completed publication and unchanged physics; camera pixels are covered by test_fluid_visibility.py.", + "- Native surface settings: FastRT, spp=1, mesh_surface GPU enabled, max_res=128; other RenderCfg/FluidMeshSurfaceRenderDesc defaults.", + ] + try: + notes.append(f"- DexSim distribution: {version('dexsim_engine')}") + except PackageNotFoundError: + notes.append("- DexSim distribution metadata unavailable.") + notes.extend(f"- {note}" for note in args.note) + perf_rows, metric_rows = [], [] + if device.type == "cuda" and not torch.cuda.is_available(): + notes.append("- Skipped: CUDA is unavailable.") + else: + if device.type == "cuda": + notes.append(f"- GPU: {torch.cuda.get_device_name(device)}") + with torch.no_grad(): + for count in args.sizes: + for fluids in args.fluid_counts: + perf, metrics = _benchmark_clipping(count, fluids, device, args) + perf_rows.extend(perf) + metric_rows.extend(metrics) + if args.native and device.type == "cuda": + try: + from dexsim.engine.newton_physics import DexUniSolverCfg + except ImportError: + DexUniSolverCfg = None + if "sph_cfg" not in getattr( + DexUniSolverCfg, "__dataclass_fields__", {} + ): + notes.append( + "- Native publication skipped: DexUni/DFSPH is unavailable." + ) + else: + for count in args.native_sizes: + perf, metrics = _benchmark_native(count, device, args) + perf_rows.extend(perf) + metric_rows.extend(metrics) + elif args.native: + notes.append("- Native publication skipped: CUDA is required.") + _write_report(args.output, perf_rows, metric_rows, notes) + for row in perf_rows: + print( + f"{row['scope']}: n={row['particles']} fluids={row['fluids']} " + f"{row['algorithm']} {row['cost_time_ms']:.6f} ms" + ) + print(f"Markdown report saved: {args.output}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--device", default="cuda:0") + parser.add_argument( + "--sizes", nargs="+", type=int, default=[1728, 76607, 1_000_000] + ) + parser.add_argument("--fluid-counts", nargs="+", type=int, default=[1, 8]) + parser.add_argument("--native", action="store_true") + parser.add_argument("--native-sizes", nargs="+", type=int, default=[1728, 76607]) + parser.add_argument("--warmup", type=int, default=10) + parser.add_argument("--repeats", type=int, default=60) + parser.add_argument( + "--note", action="append", default=[], help="Additional measurement conditions" + ) + parser.add_argument( + "--output", + type=Path, + default=Path("outputs/benchmarks") + / f"fluid_render_sync_{datetime.now():%Y%m%d_%H%M%S}.md", + ) + args = parser.parse_args() + if ( + args.warmup < 1 + or args.repeats < 1 + or min(args.sizes + args.native_sizes + args.fluid_counts) < 1 + ): + parser.error("sizes, fluid counts, warmup and repeats must be positive") + if min(args.sizes) < max(args.fluid_counts): + parser.error("particle counts must be at least the number of fluid ranges") + run_all_benchmarks(args) diff --git a/scripts/ci/run_test_plan.py b/scripts/ci/run_test_plan.py index a64dc329e7..79fa13a279 100644 --- a/scripts/ci/run_test_plan.py +++ b/scripts/ci/run_test_plan.py @@ -295,6 +295,18 @@ def _run_resource_lane( "nonempty resource collection returned pytest exit code 5" ) narrowed = _resource_command_for_nodes(command, nodes) + # Native failures can leave the device unusable for subsequent scenes. + # Report the first failure before another test can hang, and expose the + # active node and Python stack if a native call stops making progress. + narrowed.extend( + ( + "--exitfirst", + "--verbose", + "--tb=short", + "-o", + "faulthandler_timeout=120", + ) + ) narrowed.append(f"--ci-expected-collection={manifest}") if no_finalize_resource_lanes: narrowed[2] = "scripts.ci.pytest_no_finalize" diff --git a/scripts/tutorials/gym/modular_env.py b/scripts/tutorials/gym/modular_env.py index 9a9f1232a8..a73c6ce36e 100644 --- a/scripts/tutorials/gym/modular_env.py +++ b/scripts/tutorials/gym/modular_env.py @@ -211,7 +211,7 @@ class ExampleCfg(EmbodiedEnvCfg): shape=MeshCfg( fpath=get_data_path("TableWare/tableware/fork/standard_fork_scale.ply"), ), - body_scale=(0.75, 0.75, 1.0), + body_scale=(0.75, 0.75, 0.75), init_pos=(0.8, 0, 1.0), ), ] diff --git a/scripts/tutorials/sim/create_fluid.py b/scripts/tutorials/sim/create_fluid.py new file mode 100644 index 0000000000..9e837b379e --- /dev/null +++ b/scripts/tutorials/sim/create_fluid.py @@ -0,0 +1,160 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Drop a procedural water block into a box using DexUni and DFSPH. + +Run ``python scripts/tutorials/sim/create_fluid.py`` for a reconstructed mesh surface, or +add ``--headless --renderer no-render`` for physics only. The scene uses five +primitive walls and needs no task assets, robot, Gym or Task Program. +""" + +from __future__ import annotations + +import argparse + +from embodichain.cli.sim import add_sim_args_to_parser + +__all__ = ["build_parser", "main"] + + +def build_parser() -> argparse.ArgumentParser: + """Return simulation options without loading or initializing the engine.""" + parser = argparse.ArgumentParser(description=__doc__) + add_sim_args_to_parser(parser) + parser.set_defaults(device="cuda", physics="newton", renderer="fast-rt") + parser.add_argument("--steps", type=int, default=480) + return parser + + +def main(args: argparse.Namespace | None = None) -> None: + """Create, step, inspect and reset one fluid per environment. + + Args: + args: Parsed options; None reads the command line. + """ + parser = build_parser() + if args is None: + args = parser.parse_args() + if args.physics != "newton" or not str(args.device).startswith("cuda"): + parser.error("Fluids require --physics newton and a CUDA device.") + if args.num_envs < 1: + parser.error("--num-envs must be positive.") + if args.renderer not in {"fast-rt", "rt", "no-render"}: + parser.error("Choose --renderer fast-rt, rt or no-render for fluids.") + if args.steps <= 0: + parser.error("--steps must be positive.") + + import torch + import trimesh + + from embodichain.lab.sim import SimulationManager, SimulationManagerCfg + from embodichain.lab.sim.cfg import ( + FluidObjectCfg, + FluidRenderCfg, + FluidSpawnCfg, + NewtonPhysicsCfg, + RenderCfg, + RigidObjectCfg, + ) + from embodichain.lab.sim.shapes import MeshCfg, MeshCollisionCfg + from embodichain.lab.visualization import visualization_cfg_from_args + + spacing = 0.01 + sim = SimulationManager( + SimulationManagerCfg( + device=args.device, + num_envs=args.num_envs, + headless=args.headless, + gpu_id=args.gpu_id, + physics_dt=1 / 240, + render_cfg=RenderCfg(renderer=args.renderer), + physics_cfg=NewtonPhysicsCfg( + num_substeps=4, + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "sph_cfg": { + "sph_method": "dfsph", + "particle_diameter": spacing, + "particle_support_radius": 2.25 * spacing, + }, + }, + ), + visualization=visualization_cfg_from_args(args), + ) + ) + fluid = None + try: + # SPH queries texture SDFs; render boxes alone are not fluid boundaries. + for uid, position, size in ( + ("floor", (0, 0, 0), (0.32, 0.32, 0.02)), + ("left", (-0.16, 0, 0.15), (0.02, 0.32, 0.3)), + ("right", (0.16, 0, 0.15), (0.02, 0.32, 0.3)), + ("front", (0, -0.16, 0.15), (0.32, 0.02, 0.3)), + ("back", (0, 0.16, 0.15), (0.32, 0.02, 0.3)), + ): + mesh = trimesh.creation.box(extents=size) + sim.add_rigid_object( + RigidObjectCfg( + uid=uid, + body_type="static", + init_pos=position, + shape=MeshCfg( + vertices=mesh.vertices, + triangles=mesh.faces, + collision=MeshCollisionCfg(approximation="sdf"), + ), + ) + ) + fluid = sim.add_fluid_object( + FluidObjectCfg( + uid="water", + particle_spacing=spacing, + init_pos=(0, 0, 0.16), + spawn=FluidSpawnCfg(kind="box", size=(0.12, 0.12, 0.12)), + render=FluidRenderCfg( + mode="none" if args.renderer == "no-render" else "mesh_surface" + ), + ) + ) + sim.prepare() + initial = fluid.data.default_particle_state_w + print( + f"Created {fluid.num_particles} fluid particles per environment " + f"in {fluid.num_instances} environments." + ) + if not args.headless: + sim.open_window() + for _ in range(args.steps): + sim.update(step=1) + state = fluid.data.particle_state_w + if not bool(torch.isfinite(state).all()): + raise RuntimeError("Fluid state is not finite.") + print( + f"Mean particle height per environment after stepping: " + f"{state[..., 2].mean(dim=1).tolist()} m" + ) + fluid.reset() + torch.testing.assert_close(fluid.data.particle_state_w, initial) + print("Restored the initial fluid state without advancing physics.") + finally: + fluid = None + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + + +if __name__ == "__main__": + main() diff --git a/scripts/tutorials/sim/open_drawer.py b/scripts/tutorials/sim/open_drawer.py index c74ec051d1..ae57563cdb 100644 --- a/scripts/tutorials/sim/open_drawer.py +++ b/scripts/tutorials/sim/open_drawer.py @@ -531,22 +531,21 @@ def _tutorial_physics_cfg( """Build the physics configuration used by this tutorial.""" physics_cfg = physics_cfg_for_backend(backend) if isinstance(physics_cfg, NewtonPhysicsCfg): - # Keep the same MuJoCo-Warp solver/contact profile as the atomic-skill - # tutorial and packaged Newton task. The finer drawer substep cadence - # is intentional; per-world capacities remain auto-sized by DexSim - # from the finalized scene. + # Share the DexUni articulation profile with the atomic-skill tutorials. + # Keep the finer drawer substep cadence and native contact ownership. physics_cfg.num_substeps = 20 physics_cfg.collision_cfg = None physics_cfg.solver_cfg = { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } return physics_cfg diff --git a/tests/benchmark/motion_generation/test_placement_geometry.py b/tests/benchmark/motion_generation/test_placement_geometry.py index d09a9ebd63..bf21a936fc 100644 --- a/tests/benchmark/motion_generation/test_placement_geometry.py +++ b/tests/benchmark/motion_generation/test_placement_geometry.py @@ -62,36 +62,184 @@ def test_flat_grasp_uses_transformed_center_and_world_top() -> None: def test_handle_grasp_points_down_and_moves_toward_handle_root() -> None: pan = torch.tensor( - [[-0.1, -0.1, 0.0], [0.1, 0.1, 0.02], [0.0, 0.3, 0.06], [0.0, 0.5, 0.08]] + [[-0.1, -0.1, 0.0], [0.1, 0.03, 0.048], [-0.01, 0.1, 0.04], [0.01, 0.2, 0.05]] ) grasp = build_pan_handle_grasp_pose(torch.eye(4), pan, _CPU) - torch.testing.assert_close(grasp[:3, 3], torch.tensor([0.0, 0.365, 0.086])) + torch.testing.assert_close(grasp[:3, 3], torch.tensor([0.0, 0.115, 0.056])) torch.testing.assert_close(grasp[:3, 2], torch.tensor([0.0, 0.0, -1.0])) torch.testing.assert_close(grasp[:3, :3].T @ grasp[:3, :3], torch.eye(3)) assert torch.linalg.det(grasp[:3, :3]).item() == pytest.approx(1.0) -def test_placing_target_clears_basin_and_ignores_tall_handle() -> None: +def test_placing_target_clears_basin_and_ignores_handle() -> None: bread = torch.tensor([[-0.1, -0.05, 0.0], [0.1, 0.05, 0.02]]) - pan = torch.tensor([[-0.1, -0.1, 0.0], [0.1, 0.1, 0.02], [0.0, 0.5, 0.08]]) + pan = torch.tensor( + [[-0.1, -0.1, 0.0], [0.1, 0.03, 0.048], [0.0, 0.5, 0.08], [0.0, 0.55, 0.01]] + ) initial = torch.eye(4) support = build_support_object_target_pose(initial, _CPU) placing = build_placing_object_target_pose(initial, bread, pan, support, _CPU) - # The basin top is 0.731 m; the bread bottom retains 6 mm clearance. + # The basin top is 0.759 m; the bread bottom retains 6 mm clearance. torch.testing.assert_close(support[:3, 3], torch.tensor([-0.06, 0.0, 0.711])) - torch.testing.assert_close(placing[:3, 3], torch.tensor([-0.06, 0.0, 0.737])) + torch.testing.assert_close(placing[:3, 3], torch.tensor([-0.06, -0.035, 0.765])) torch.testing.assert_close(initial, torch.eye(4)) def test_empty_basin_selection_preserves_full_mesh() -> None: - pan = torch.tensor([[0.0, 0.0, 0.1], [0.0, 0.2, 0.2]]) + pan = torch.tensor([[0.0, 0.1, 0.1], [0.0, 0.2, 0.2]]) torch.testing.assert_close(get_pan_basin_vertices(pan), pan) +def test_explicit_glb_frame_preserves_the_authored_handle_and_basin_geometry(): + pan = torch.tensor( + [ + [-0.1, -0.1, 0.0], + [0.1, 0.03, 0.048], + [-0.01, 0.1, 0.04], + [0.01, 0.2, 0.05], + ] + ) + frame = torch.tensor([[1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]]) + native = pan @ frame.T + pose = torch.eye(4) + pose[:3, :3] = frame.T + torch.testing.assert_close( + get_pan_basin_vertices(native, pan_frame="native_glb") @ frame, + get_pan_basin_vertices(pan), + ) + torch.testing.assert_close( + build_pan_handle_grasp_pose(pose, native, _CPU, pan_frame="native_glb"), + build_pan_handle_grasp_pose(torch.eye(4), pan, _CPU), + ) + + +@pytest.mark.parametrize( + "condition", ["resting", "curved", "floating", "rim", "overhang", "penetrating"] +) +def test_settled_placement_requires_full_footprint_and_bounded_surface_gaps(condition): + floor = torch.tensor( + [[-0.1, -0.03, 0.0], [0.1, -0.03, 0.0], [0.1, 0.03, 0.0], [-0.1, 0.03, 0.0]] + ) + faces = torch.tensor([[0, 1, 2], [0, 2, 3]]) + bread = floor.clone() * 0.8 + pan, pan_faces = floor, faces + placing = torch.eye(4)[None] + placing[:, 2, 3] = 0.006 + if condition == "curved": + bread[:, 2] = 0.02 + bread = torch.cat((bread, torch.zeros(1, 3))) + faces = torch.tensor([[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]) + elif condition == "floating": + placing[:, 2, 3] = 0.03 + elif condition == "penetrating": + placing[:, 2, 3] = -0.001 + elif condition == "overhang": + placing[:, 0, 3] = 0.025 + elif condition == "rim": + # A loaf spanning the rim has contact at its edges but floats over + # the interior floor. A minimum-gap-only check would accept it. + bread = floor.clone() + rim = torch.cat((floor, floor * 0.9)) + torch.tensor([0.0, 0.0, 0.04]) + pan = torch.cat((floor, rim)) + rim_faces = ( + torch.tensor( + [[i, (i + 1) % 4, 4 + (i + 1) % 4] for i in range(4)] + + [[i, 4 + (i + 1) % 4, 4 + i] for i in range(4)] + ) + + 4 + ) + pan_faces = torch.cat((faces, rim_faces)) + placing[:, 2, 3] = 0.04 + result = _placement_input_geometry( + bread, + faces, + pan, + pan_faces, + placing, + torch.eye(4)[None], + maximum_support_gap_m=0.008, + ) + row = result["rows"][0] + assert row["qualified"] is (condition in {"resting", "curved"}) + if condition in {"floating", "rim"}: + assert row["maximum_sampled_vertical_gap_m"] > 0.008 + + +@pytest.mark.requires_sim +@pytest.mark.gpu +@pytest.mark.parametrize("backend", ["default", "newton"]) +@pytest.mark.parametrize( + "suite_name", + [ + "atomic_dual_franka_pgi_coordinated_placement", + "atomic_dual_ur5_pgi_coordinated_placement", + "atomic_dual_ur5_pgi_coordinated_placement_spatial_generalization", + ], +) +def test_suite_pan_frame_matches_actual_native_glb_loading(suite_name, backend): + from embodichain_benchmarks.motion_generation.config import ( + BENCHMARK_ROOT, + load_suite, + ) + from embodichain_benchmarks.motion_generation.scenarios.atomic_objects import ( + create_atomic_object, + ) + from embodichain_benchmarks.motion_generation._atomic_fixtures import ( + get_local_vertices, + ) + from embodichain.lab.sim import SimulationManager, SimulationManagerCfg + from embodichain.lab.sim.cfg import DefaultPhysicsCfg, NewtonPhysicsCfg + + suite = load_suite(BENCHMARK_ROOT / "suites" / f"{suite_name}.yaml") + config = suite.tracks[0].config + pan_cfg = next(obj for obj in config["objects"] if obj["id"] == "pan") + skill = next( + skill for skill in config["skills"] if skill["id"] == "coordinated_placement" + ) + assert skill["support_mesh_frame"] == "native_glb" + physics = ( + DefaultPhysicsCfg() + if backend == "default" + else NewtonPhysicsCfg(num_substeps=1, use_cuda_graph=False) + ) + sim = SimulationManager(SimulationManagerCfg(headless=True, physics_cfg=physics)) + try: + pan = create_atomic_object(sim, pan_cfg, initialize=False).entity + sim.prepare() + vertices = get_local_vertices(pan) + # Exercise the suite's unchanged GLB, scale and native loader. Its + # handle extends along local Z, while Y contains only thickness. + handle = vertices[vertices[:, 2] >= 0.04] + basin = get_pan_basin_vertices(vertices, pan_frame=skill["support_mesh_frame"]) + assert 0 < len(handle) < len(vertices) + assert 0 < len(basin) < len(vertices) + assert vertices[:, 1].max() <= 1e-6 + pose = pan.get_local_pose(to_matrix=True)[0] + grasp = build_pan_handle_grasp_pose( + pose, vertices, sim.device, pan_frame=skill["support_mesh_frame"] + ) + assert torch.isfinite(grasp).all() + grasp_in_pan = pose[:3, :3].T @ (grasp[:3, 3] - pose[:3, 3]) + assert grasp_in_pan[2] >= 0.04 + torch.testing.assert_close( + grasp[:3, :3].T @ grasp[:3, :3], + torch.eye(3, device=sim.device), + atol=1e-5, + rtol=0, + ) + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + if backend == "newton": + from dexsim.engine.newton_physics import teardown_newton_physics + + teardown_newton_physics() + + def test_placing_target_centers_offset_mesh_footprints_on_the_basin() -> None: # The bread's asset origin is displaced 0.3m along X from its mesh center. bread = torch.tensor([[0.2, -0.04, 0.0], [0.4, 0.04, 0.02]]) - pan = torch.tensor([[-0.12, -0.12, 0.0], [0.12, 0.12, 0.02]]) + pan = torch.tensor([[-0.12, -0.03, 0.0], [0.12, 0.03, 0.02]]) initial = torch.eye(4) initial[:3, 3] = torch.tensor([4.0, 3.0, 2.0]) support = torch.eye(4) @@ -107,13 +255,13 @@ def test_placing_target_uses_inner_floor_instead_of_rim_height() -> None: # Two floor layers plus a raised rim vertex: the ray must choose the upper # floor surface, not the lower exterior or the basin's maximum height. floor = torch.tensor( - [[-0.1, -0.1, 0.0], [0.1, -0.1, 0.0], [0.1, 0.1, 0.0], [-0.1, 0.1, 0.0]] + [[-0.1, -0.03, 0.0], [0.1, -0.03, 0.0], [0.1, 0.03, 0.0], [-0.1, 0.03, 0.0]] ) pan = torch.cat( ( floor, floor + torch.tensor([0.0, 0.0, 0.005]), - torch.tensor([[0.1, 0.1, 0.04]]), + torch.tensor([[0.1, 0.03, 0.04]]), ) ) faces = torch.tensor([[0, 1, 2], [0, 2, 3], [4, 5, 6], [4, 6, 7]]) @@ -128,7 +276,7 @@ def test_placing_target_uses_inner_floor_instead_of_rim_height() -> None: def test_placing_target_rejects_missing_center_support_surface() -> None: bread = torch.tensor([[-0.02, -0.02, 0.0], [0.02, 0.02, 0.02]]) pan = torch.tensor( - [[-0.1, -0.1, 0.0], [0.1, -0.1, 0.0], [0.1, -0.09, 0.0], [-0.1, 0.1, 0.0]] + [[-0.1, -0.03, 0.0], [0.1, -0.03, 0.0], [0.1, -0.02, 0.0], [-0.1, 0.03, 0.0]] ) with pytest.raises(ValueError, match="no supporting mesh surface"): build_placing_object_target_pose( @@ -160,6 +308,7 @@ def test_frozen_placement_mesh_audit_checks_full_footprint_and_sampled_gaps(cond ) faces = torch.tensor([[0, 1, 2], [0, 2, 3]]) pan = bread * 5 + pan[:, 1] *= 0.3 # Keep the basin below the authored positive-Y handle. pan_faces = faces.clone() placing, support = torch.eye(4)[None], torch.eye(4)[None] placing[:, 2, 3] = 0.006 @@ -186,10 +335,11 @@ def test_frozen_placement_mesh_audit_checks_full_footprint_and_sampled_gaps(cond ] ) elif condition == "handle": - pan = torch.cat((pan, bread + torch.tensor([0.2, 0.0, 0.1]))) + # A low handle must be excluded by local Y, even below the basin Z limit. + pan = torch.cat((pan, bread + torch.tensor([0.0, 0.2, 0.01]))) pan_faces = torch.cat((faces, faces + 4)) - placing[:, 0, 3] = 0.2 - placing[:, 2, 3] = 0.106 + placing[:, 1, 3] = 0.2 + placing[:, 2, 3] = 0.016 original = placing.clone() result = _placement_input_geometry(bread, faces, pan, pan_faces, placing, support) row = result["rows"][0] @@ -220,7 +370,9 @@ def test_placement_mesh_audit_retains_each_transformed_target_row(): support[1, :2, :2] = torch.tensor([[0.0, -1.0], [1.0, 0.0]]) placing = support.clone() placing[:, 2, 3] += 0.006 - result = _placement_input_geometry(bread, faces, bread * 5, faces, placing, support) + pan = bread * 5 + pan[:, 1] *= 0.3 + result = _placement_input_geometry(bread, faces, pan, faces, placing, support) assert [row["env_index"] for row in result["rows"]] == [0, 1] assert all(row["qualified"] for row in result["rows"]) assert [ @@ -245,6 +397,7 @@ def test_placement_overlay_covers_an_explicitly_included_support_triangle() -> N ], dtype=torch.float64, ) + support[:, 1] -= 0.05 # Keep every support triangle on the basin side. placing_pose = torch.eye(4, dtype=torch.float64)[None] placing_pose[:, 2, 3] = 0.006 result = _placement_input_geometry( diff --git a/tests/ci/test_select_tests.py b/tests/ci/test_select_tests.py index a8e9ebc9e6..6013edee06 100644 --- a/tests/ci/test_select_tests.py +++ b/tests/ci/test_select_tests.py @@ -1159,7 +1159,19 @@ def run(command: list[str], **kwargs: object) -> SimpleNamespace: assert execution[4:marker_index] == [ f"{file}::TestContract::test_selected" for file in files ] - assert execution[marker_index:-1] == original[original.index("-m", 3) :] + original_options = original[original.index("-m", 3) :] + assert execution[marker_index : marker_index + len(original_options)] == ( + original_options + ) + diagnostics = [ + "--exitfirst", + "--verbose", + "--tb=short", + "-o", + "faulthandler_timeout=120", + ] + assert execution[marker_index + len(original_options) : -1] == diagnostics + assert "--exitfirst" not in discovery and "--verbose" not in discovery assert all( kwargs["env"] is None and kwargs["cwd"] == tmp_path for _, kwargs in calls ) diff --git a/tests/compute/test_particles.py b/tests/compute/test_particles.py new file mode 100644 index 0000000000..04855da013 --- /dev/null +++ b/tests/compute/test_particles.py @@ -0,0 +1,133 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Numerical volume sampling for file-free particle initialization.""" + +from __future__ import annotations + +import numpy as np +import pytest +from embodichain.compute.geometry.particles import sample_volume_particles + +pytestmark = pytest.mark.no_sim + + +def test_constant_revolved_profile_matches_cylinder(): + actual = sample_volume_particles( + "revolved_volume", + 0.004, + profile=[[-0.02, 0.015], [0.02, 0.015]], + clearance=0.001, + ) + expected = sample_volume_particles( + "cylinder", 0.004, radius=0.015, height=0.04, clearance=0.001 + ) + np.testing.assert_array_equal(actual, expected) + + +def test_tapered_fill_volume_and_mass_converge_with_resolution(): + # A linear wall allows an independent integral of its inward offset. + profile = [[-0.04, 0.02], [0.04, 0.03]] + clearance = 0.002 + fill_level = 0.8 + slope = (0.03 - 0.02) / 0.08 + lower_z = -0.04 + clearance + surface_z = -0.04 + fill_level * 0.08 + lower_radius = 0.02 + slope * clearance - clearance * np.sqrt(1 + slope**2) + depth = surface_z - lower_z + expected_volume = np.pi * ( + lower_radius**2 * depth + + lower_radius * slope * depth**2 + + slope**2 * depth**3 / 3 + ) + counts = [] + for spacing in (0.002, 0.001): + points = sample_volume_particles( + "revolved_volume", + spacing, + profile=profile, + clearance=clearance, + fill_level=fill_level, + ) + radius = np.linalg.norm(points[:, :2], axis=1) + wall_distance = (0.02 + slope * (points[:, 2] + 0.04) - radius) / np.sqrt( + 1 + slope**2 + ) + assert np.all(wall_distance >= clearance - 1e-8) + assert np.all(points[:, 2] > lower_z) + assert np.all(points[:, 2] < surface_z) + assert len(points) * spacing**3 == pytest.approx(expected_volume, rel=0.025) + counts.append(len(points)) + assert counts[1] / counts[0] == pytest.approx(8, rel=0.025) + + +def test_lower_fill_level_keeps_lattice_origin_and_has_no_solid_free_surface_margin(): + kwargs = dict(size=(0.08, 0.08, 0.1), clearance=0.02) + full = sample_volume_particles("box", 0.01, **kwargs) + half = sample_volume_particles("box", 0.01, fill_level=0.5, **kwargs) + np.testing.assert_array_equal(half, full[full[:, 2] < 0]) + assert half[:, 2].max() == pytest.approx(-0.005) + + +def test_multi_segment_profile_is_deterministic_and_can_narrow_again(): + profile = [[-0.03, 0.02], [0.0, 0.01], [0.03, 0.025]] + kwargs = dict(profile=profile, clearance=0.001) + points = sample_volume_particles("revolved_volume", 0.004, **kwargs) + np.testing.assert_array_equal( + points, sample_volume_particles("revolved_volume", 0.004, **kwargs) + ) + assert np.all( + np.linalg.norm(points[:, :2], axis=1) + < np.interp(points[:, 2], np.array(profile)[:, 0], np.array(profile)[:, 1]) + ) + assert points.flags.c_contiguous + + +@pytest.mark.parametrize( + "profile", + [ + None, + [], + [[0, 1]], + [[0, 1], [0, 2]], + [[1, 1], [0, 2]], + [[0, -1], [1, 1]], + [[0, 0], [1, 0]], + [[0, 1], [float("nan"), 1]], + [[0, 1, 2], [1, 1, 2]], + ], +) +def test_invalid_profiles_fail_before_grid_allocation(profile): + with pytest.raises(ValueError, match="profile"): + sample_volume_particles("revolved_volume", 0.001, profile=profile) + + +@pytest.mark.parametrize("fill_level", [0, -0.1, 1.01, float("inf"), float("nan")]) +def test_invalid_fill_levels_are_rejected(fill_level): + with pytest.raises(ValueError, match="fill_level"): + sample_volume_particles("box", 0.01, fill_level=fill_level) + + +def test_profile_source_obeys_candidate_limit_before_allocation(): + with pytest.raises(ValueError, match="max_particles"): + sample_volume_particles( + "revolved_volume", 1e-9, profile=[[0, 1], [1, 1]], max_particles=100 + ) + + +def test_profile_on_another_volume_is_not_ignored(): + with pytest.raises(ValueError, match="profile"): + sample_volume_particles("box", 0.01, profile=[[0, 1], [1, 1]]) diff --git a/tests/conftest.py b/tests/conftest.py index 004e168079..7481eadbb6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -29,6 +29,33 @@ os.environ.setdefault("EMBODICHAIN_SIM_EXIT_PROCESS", "0") +@pytest.fixture +def requires_dexuni_contact_query() -> None: + """Require per-row contact quantities while retaining released MJWarp tests.""" + from dexsim.scene import ContactBuffer + + if not hasattr(ContactBuffer, "impulse_valid"): + pytest.skip("DexUni contacts require per-row quantity validity.") + + +@pytest.fixture +def requires_particle_contact_material() -> None: + """Require the native per-object material descriptor.""" + import dexsim.spawn + + if not hasattr(dexsim.spawn, "NewtonParticleContactMaterialDesc"): + pytest.skip("Per-object particle materials require a supporting DexSim build.") + + +@pytest.fixture +def requires_dexuni_two_way() -> None: + """Qualify dev-only contracts without excluding released-engine coverage.""" + from dexsim.engine.newton_physics import DexUniSolverCfg + + if "coupling" not in inspect.signature(DexUniSolverCfg).parameters: + pytest.skip("DexUni two-way coupling requires DexSim dev f32785317 or newer.") + + @pytest.fixture(scope="session") def _discover_task_packages(): """Discover all installed task packages once per test session. diff --git a/tests/examples/atomic_skill/test_run.py b/tests/examples/atomic_skill/test_run.py index b467a4cf0d..586cac30f1 100644 --- a/tests/examples/atomic_skill/test_run.py +++ b/tests/examples/atomic_skill/test_run.py @@ -522,6 +522,41 @@ def test_every_sample_defaults_to_one_reference_environment(skill: str) -> None: assert cfg.variants == 1 assert not cfg.affordance_enabled and not cfg.trajectory_enabled assert _scene_args(cfg).num_envs == 1 + assert cfg.device is None + assert _scene_args(cfg).device is None + + +@pytest.mark.parametrize( + "physics, expected_device", [("default", "cpu"), ("newton", "cuda:0")] +) +def test_sample_omitted_device_preserves_backend_default( + monkeypatch: pytest.MonkeyPatch, physics: str, expected_device: str +) -> None: + from examples.sim.atomic_skill import support + + captured = {} + + def create_sim(cfg): + captured["cfg"] = cfg + return SimpleNamespace(add_light=lambda cfg: None) + + monkeypatch.setattr(support, "SimulationManager", create_sim) + args = launcher._create_parser().parse_args( + ["--skill", "pickup", "--physics", physics] + ) + cfg = launcher._resolve_sample(args) + support.create_tutorial_simulation(_scene_args(cfg)) + assert captured["cfg"].device == expected_device + + +@pytest.mark.parametrize("device", ["cpu", "cuda:2"]) +def test_sample_explicit_device_is_preserved(device: str) -> None: + args = launcher._create_parser().parse_args( + ["--skill", "pickup", "--physics", "newton", "--device", device] + ) + cfg = launcher._resolve_sample(args) + assert cfg.device == device + assert _scene_args(cfg).device == device @pytest.mark.parametrize("count", (2, 8)) @@ -551,6 +586,7 @@ def test_variant_count_is_the_parallel_environment_count(count: int, mode: str) (["--gpu-id", "-1"], "non-negative"), (["--seed", "-1"], "seed must"), (["--arena-space", "nan"], "finite"), + (["--device", ""], "device must"), (["--save-video", "--renderer", "no-render"], "requires a renderer"), ), ) @@ -621,3 +657,102 @@ def fail(cfg, args, run): manifest = json.loads((tmp_path / "expansion_manifest.json").read_text()) assert manifest["status"] == "failed" assert type(error).__name__ in manifest["error"] + + +@pytest.mark.no_sim +def test_sample_solver_overrides_preserve_configured_host_fields(monkeypatch) -> None: + from examples.sim.atomic_skill import support + + captured = {} + + def create_sim(cfg): + captured["cfg"] = cfg + return SimpleNamespace(add_light=lambda cfg: None) + + monkeypatch.setattr(support, "SimulationManager", create_sim) + args = SimpleNamespace( + physics="newton", + num_envs=2, + device="cpu", + renderer="no-render", + gpu_id=3, + arena_space=4.0, + run=SimpleNamespace(), + ) + support.create_tutorial_simulation( + args, newton_solver_options={"vbd_options": {"rigid_contact_history": True}} + ) + cfg = captured["cfg"] + assert cfg.arena_space == 4.0 + assert cfg.gpu_id == 3 + assert cfg.physics_cfg.collision_cfg is None + assert cfg.physics_cfg.solver_cfg["solver_type"] == "dexuni" + assert cfg.physics_cfg.solver_cfg["mujoco_options"]["enable_multiccd"] + assert cfg.physics_cfg.solver_cfg["vbd_options"]["rigid_contact_history"] + + +@pytest.mark.no_sim +def test_sample_newton_options_reject_other_backends_before_creation( + monkeypatch, +) -> None: + from examples.sim.atomic_skill import support + + def unexpected(*args, **kwargs): + pytest.fail("Simulation should not be created for incompatible options") + + monkeypatch.setattr(support, "SimulationManager", unexpected) + with pytest.raises(ValueError, match="Newton backend"): + support.create_tutorial_simulation( + SimpleNamespace(physics="default"), + newton_solver_options={"coupling": "two_way"}, + ) + + +@pytest.mark.no_sim +def test_sample_friction_and_arm_damping_are_applied_before_spawn(monkeypatch) -> None: + from examples.sim.atomic_skill import support + from embodichain.lab.sim.cfg import JointDrivePropertiesCfg + + cfg = RobotCfg(joint_drive_props=JointDrivePropertiesCfg(damping={"arm": 2000.0})) + captured = {} + + def create_cfg(*args, **kwargs): + captured.update(kwargs) + return cfg + + monkeypatch.setattr(support, "create_tutorial_robot_cfg", create_cfg) + sim = SimpleNamespace(is_newton_backend=True, add_robot=lambda cfg: cfg) + result = support.add_tutorial_robot( + sim, "ur5", tcp_point="grasp", newton_gripper_friction=4.0, arm_damping=1000.0 + ) + assert captured["tcp_point"] == "grasp" + assert "newton_gripper_friction" not in captured + assert "arm_damping" not in captured + assert result.joint_drive_props.damping["arm"] == 1000.0 + material = result.link_attrs["newton_gripper_contacts"].attrs.material_props + assert material.static_friction == material.dynamic_friction == 4.0 + + +@pytest.mark.no_sim +def test_dual_sample_gripper_friction_retains_fixture_selection(monkeypatch) -> None: + cfg = RobotCfg() + captured = {} + + def create_cfg(**kwargs): + captured.update(kwargs) + return cfg + + monkeypatch.setattr(scenario, "create_dual_tutorial_robot_cfg", create_cfg) + sim = SimpleNamespace(is_newton_backend=True, add_robot=lambda cfg: cfg) + result = scenario.add_dual_tutorial_robot( + sim, + robot_type="ur5", + uid="robot", + urdf_name="fixture", + gripper="robotiq_2f_140", + newton_gripper_friction=4.0, + ) + assert captured["gripper"] == "robotiq_2f_140" + assert "newton_gripper_friction" not in captured + material = result.link_attrs["newton_gripper_contacts"].attrs.material_props + assert material.static_friction == material.dynamic_friction == 4.0 diff --git a/tests/examples/atomic_skill/test_support.py b/tests/examples/atomic_skill/test_support.py index b76c8c2590..8174bd0046 100644 --- a/tests/examples/atomic_skill/test_support.py +++ b/tests/examples/atomic_skill/test_support.py @@ -24,6 +24,7 @@ import re import xml.etree.ElementTree as ET from argparse import Namespace +from pathlib import Path from types import SimpleNamespace from unittest.mock import MagicMock, call, patch @@ -52,6 +53,7 @@ compute_left_to_right_arm_direction, ) from examples.sim.atomic_skill.scenario import ( + add_dual_tutorial_robot, create_dual_tutorial_robot_cfg, ) from examples.sim.atomic_skill.support import ( @@ -267,7 +269,7 @@ def _run_obstacle_animation(*, pace_wall_time: bool) -> tuple[MagicMock, MagicMo return obstacle, adapter -def test_atomic_skill_tutorial_uses_native_mujoco_contact_settings() -> None: +def test_atomic_skill_tutorial_uses_native_dexuni_settings() -> None: module = importlib.import_module("examples.sim.atomic_skill.support") default_cfg = module._tutorial_physics_cfg("default") @@ -278,29 +280,24 @@ def test_atomic_skill_tutorial_uses_native_mujoco_contact_settings() -> None: assert newton_cfg.num_substeps == 20 assert newton_cfg.collision_cfg is None assert newton_cfg.solver_cfg == { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } dexsim_cfg = newton_cfg.to_dexsim_cfg(gpu_id=0) - assert dexsim_cfg.solver_cfg.solver_type == "mujoco_warp" - assert dexsim_cfg.solver_cfg.solver == "newton" - assert dexsim_cfg.solver_cfg.integrator == "implicitfast" - assert dexsim_cfg.solver_cfg.iterations == 20 - assert dexsim_cfg.solver_cfg.ls_iterations == 100 - assert dexsim_cfg.solver_cfg.nconmax is None - assert dexsim_cfg.solver_cfg.njmax is None - assert dexsim_cfg.solver_cfg.cone == "elliptic" - assert dexsim_cfg.solver_cfg.impratio == pytest.approx(1_000.0) - assert dexsim_cfg.solver_cfg.use_mujoco_contacts is True - assert dexsim_cfg.solver_cfg.enable_multiccd is True + assert dexsim_cfg.solver_cfg.solver_type == "dexuni" + assert ( + dexsim_cfg.solver_cfg.mujoco_options == newton_cfg.solver_cfg["mujoco_options"] + ) + assert dexsim_cfg.solver_cfg.vbd_options == {} assert dexsim_cfg.collision_pipeline_cfg is None @@ -541,6 +538,101 @@ def test_franka_tutorial_config_uses_ur5_gripper_component() -> None: assert "fr3_finger_joint[1-2]" not in franka_values +def test_place_waypoints_are_rigid_transforms() -> None: + from examples.sim.atomic_skill.place import make_place_eef_poses + + poses = make_place_eef_poses(torch.device("cpu")) + rotation = poses[:, :3, :3] + torch.testing.assert_close( + rotation.transpose(-1, -2) @ rotation, + torch.eye(3).expand(len(poses), 3, 3), + rtol=0.0, + atol=1e-6, + ) + torch.testing.assert_close( + torch.linalg.det(rotation), torch.ones(len(poses)), rtol=0.0, atol=1e-6 + ) + + +def test_coordinated_placement_preserves_baseline_scene_frames() -> None: + from examples.sim.atomic_skill import coordinated_placement as module + from embodichain.utils.math import matrix_from_euler + + for angles in (module.TABLE_INIT_ROT, module.PAN_INIT_ROT): + rotation = matrix_from_euler(torch.tensor(angles).deg2rad(), "XYZ") + assert float(rotation[2, 2]) > 0.999 + assert module.TABLE_TOP_Z - module.ROBOT_INIT_POS[2] == pytest.approx( + module.BASELINE_TABLE_TOP_Z - module.BASELINE_ROBOT_INIT_POS[2] + ) + + +def test_placement_mesh_cache_preserves_authored_axes_and_texture( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + import trimesh + from PIL import Image + from examples.sim.atomic_skill import coordinated_placement as module + + mesh = trimesh.creation.box(extents=(0.1, 0.2, 0.3)) + mesh.visual = trimesh.visual.TextureVisuals( + uv=torch.zeros((len(mesh.vertices), 2)).numpy(), + image=Image.new("RGB", (2, 2), (210, 70, 15)), + ) + source = tmp_path / "asset.glb" + mesh.export(source) + monkeypatch.setattr(module, "resolve_cached_data_path", lambda _: str(source)) + monkeypatch.setattr( + module, "EMBODICHAIN_DEFAULT_DATA_ROOT", str(tmp_path / "cache") + ) + + path = Path(module._placement_mesh_path("asset.glb")) + loaded = trimesh.load(path, force="mesh", process=False) + torch.testing.assert_close( + torch.as_tensor(loaded.bounds.copy()), + torch.tensor([[-0.05, -0.1, -0.15], [0.05, 0.1, 0.15]], dtype=torch.float64), + atol=1e-7, + rtol=0, + ) + assert loaded.visual.material.image.getpixel((0, 0))[:3] == (210, 70, 15) + loader = MagicMock(side_effect=AssertionError("Cached mesh must not be reloaded")) + monkeypatch.setattr(module.trimesh, "load", loader) + assert module._placement_mesh_path("asset.glb") == str(path) + loader.assert_not_called() + + +@pytest.mark.parametrize("is_newton_backend", [False, True]) +def test_rubiks_cube_initialization_clears_supported_scope_and_keeps_lock( + monkeypatch: pytest.MonkeyPatch, is_newton_backend: bool +) -> None: + from examples.sim.atomic_skill import pickup_rubiks_cube as module + + cleared = [] + cube = SimpleNamespace(locked=False, clear_dynamics=lambda: cleared.append("cube")) + + def reset_world(): + cleared.append("world") + cube.locked = False + + def update(*, step): + assert cube.locked + + sim = SimpleNamespace( + is_newton_backend=is_newton_backend, + add_articulation=lambda **kwargs: cube, + prepare=lambda: None, + update=update, + reset_objects_state=reset_world, + ) + monkeypatch.setattr(module.Path, "is_file", lambda _: True) + monkeypatch.setattr( + module, "lock_turn_joint", lambda obj: setattr(obj, "locked", True) + ) + + assert module.create_pick_object(sim, "/tmp/cube.usdc") is cube + assert cube.locked + assert cleared == (["world"] if is_newton_backend else ["cube"]) + + def test_ur5_and_franka_configs_share_place_binding_contract() -> None: configs = ( create_ur5_gripper_robot_cfg(), @@ -674,6 +766,19 @@ def test_dual_tutorial_configs_share_hand_binding_contract( assert dual_cfg.init_qpos[-12:] == [0.0] * 12 +@pytest.mark.parametrize("stabilizer", [None, True]) +def test_dual_scene_mimic_stabilization_is_explicit(stabilizer: bool | None) -> None: + sim = SimpleNamespace(is_newton_backend=True, add_robot=lambda *, cfg: cfg) + cfg = add_dual_tutorial_robot( + sim, + robot_type="ur5", + uid="test_dual_mimic", + urdf_name="test_dual_mimic", + newton_mimic_stabilizer=stabilizer, + ) + assert cfg.root_props.newton_mimic_stabilizer is (stabilizer is True) + + @pytest.mark.parametrize("robot_type", ("ur5", "ur10")) def test_dual_pytorch_ur_tutorial_solvers_preserve_runtime_joint_limits( robot_type: str, @@ -1021,6 +1126,7 @@ def test_place_tutorial_registers_pick_object_with_simulation_engine_factory() - n_sample=1, no_vis_eef_axis=True, robot="ur5", + physics="default", ) sim = MagicMock() sim.device = torch.device("cpu") @@ -1303,9 +1409,13 @@ def test_pour_tutorial_uses_configured_pickup_and_local_rotation_axis() -> None: assert module.POUR_INTERNAL_AXIS == (1.0, 0.0, 0.0) pick_policy = module._create_pick_motion_policy() assert pick_policy.sample_count == module.PICK_SAMPLE_INTERVAL - assert isinstance(pick_policy.plan_opts, module.TrapezoidalPlanOptions) - assert pick_policy.plan_opts.sample_method is module.TrajectorySampleMethod.QUANTITY - assert pick_policy.plan_opts.sample_interval == module.PICK_MOTION_SAMPLE_COUNT + assert pick_policy.plan_opts is None + toppra_policy = module._create_pick_motion_policy("toppra") + assert isinstance(toppra_policy.plan_opts, module.ToppraPlanOptions) + assert ( + toppra_policy.plan_opts.sample_method is module.TrajectorySampleMethod.QUANTITY + ) + assert toppra_policy.plan_opts.sample_interval == module.PICK_MOTION_SAMPLE_COUNT def test_replay_timed_trajectory_uses_arrival_intervals() -> None: diff --git a/tests/gen_sim/task_engine/test_bundle.py b/tests/gen_sim/task_engine/test_bundle.py index 5b4e77a075..12b45624cb 100644 --- a/tests/gen_sim/task_engine/test_bundle.py +++ b/tests/gen_sim/task_engine/test_bundle.py @@ -169,6 +169,9 @@ def _configured_files(): from embodichain.gen_sim.task_engine.program_declarations import ( build_program_declaration, ) + from embodichain.lab.task_program.integrations._configured_composition import ( + _resolve_execution_policy, + ) root = ( Path(__file__).parents[3] @@ -182,8 +185,8 @@ def _configured_files(): integration = yaml.safe_load( (root / original["task_program"]["integration"]).read_text() ) - policy = yaml.safe_load( - (root / original["task_program"]["execution_policy"]).read_text() + _, policy = _resolve_execution_policy( + original["task_program"]["execution_policy"], base_dir=root ) call = {"kind": "pick", "object": "rubiks_cube"} graph = dict( diff --git a/tests/gym/envs/task_program/test_task_vertical_slices.py b/tests/gym/envs/task_program/test_task_vertical_slices.py index cfb932a540..480af10428 100644 --- a/tests/gym/envs/task_program/test_task_vertical_slices.py +++ b/tests/gym/envs/task_program/test_task_vertical_slices.py @@ -280,15 +280,16 @@ def test_newton_contact_overlays_are_scoped_in_packaged_manipulation_configs() - assert cube_physics.num_substeps == 20 assert cube_physics.collision_cfg is None assert cube_physics.solver_cfg == { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1000.0, + "enable_multiccd": True, + }, } cube_collision = cube_cfg.rigid_object[0].attrs.collision_props assert isinstance(cube_collision, NewtonCollisionPropertiesCfg) diff --git a/tests/gym/envs/test_env_seed.py b/tests/gym/envs/test_env_seed.py index 869f38c3be..ed05be96ee 100644 --- a/tests/gym/envs/test_env_seed.py +++ b/tests/gym/envs/test_env_seed.py @@ -184,6 +184,7 @@ def test_reset_keeps_sensor_uid_exclusions_out_of_physical_registries( "_articulations", "_rigid_objects", "_rigid_object_groups", + "_fluid_objects", "_deformable_objects", "_lights", ): diff --git a/tests/sim/atomic_skills/test_newton_collision_pipeline_schedule.py b/tests/sim/atomic_skills/test_newton_collision_pipeline_schedule.py index 2b4f335e9d..293f297753 100644 --- a/tests/sim/atomic_skills/test_newton_collision_pipeline_schedule.py +++ b/tests/sim/atomic_skills/test_newton_collision_pipeline_schedule.py @@ -34,4 +34,14 @@ def test_atomic_skill_tutorial_disables_external_collision_pipeline() -> None: assert physics_cfg.collision_cfg is None dexsim_cfg = physics_cfg.to_dexsim_cfg(gpu_id=0) assert dexsim_cfg.collision_pipeline_cfg is None - assert dexsim_cfg.solver_cfg.use_mujoco_contacts is True + assert dexsim_cfg.solver_cfg.solver_type == "dexuni" + assert dexsim_cfg.solver_cfg.mujoco_options == { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + } + assert dexsim_cfg.solver_cfg.vbd_options == {} diff --git a/tests/sim/objects/test_backend_physics_helpers.py b/tests/sim/objects/test_backend_physics_helpers.py index b844547ad3..b5e28e2df8 100644 --- a/tests/sim/objects/test_backend_physics_helpers.py +++ b/tests/sim/objects/test_backend_physics_helpers.py @@ -490,3 +490,82 @@ def reset() -> None: assert resets == [0.0] assert state.elapsed == 0.0 assert state.other_position == 1.0 + + +def test_dexuni_mimic_compliance_maps_scene_constraint_rows(monkeypatch) -> None: + from newton import Model + from embodichain.lab.sim.objects.backends.newton import ( + _configure_newton_mimic_compliance, + ) + + class Array: + def __init__(self, value): + self.value = np.asarray(value) + + def numpy(self): + return self.value + + def assign(self, value): + self.value[...] = value + + model = SimpleNamespace( + requires_grad=False, + joint_target_ke=Array([100.0, 0.0]), + joint_target_kd=Array([20.0, 0.0]), + joint_target_mode=Array([3, 0]), + constraint_mimic_joint0=Array([0, 0, 0, 5, 0, 0, 0, 12]), + constraint_mimic_joint1=Array([0, 0, 0, 4, 0, 0, 0, 11]), + ) + runtime = SimpleNamespace(model=model) + solref = Array(np.full((1, 3, 2), 9.0, dtype=np.float32)) + solver = SimpleNamespace( + mjc_eq_to_newton_mimic=Array([[0, 1, -1]]), + mjw_model=SimpleNamespace(eq_solref=solref), + mj_model=None, + ) + + def index_map(frequency): + assert frequency == Model.AttributeFrequency.CONSTRAINT_MIMIC + return Array([3, 7]) # Nonidentity mapping into the complete Scene model. + + backend = SimpleNamespace( + solver_type="dexuni", + cfg=SimpleNamespace(requires_grad=False, dt=0.01, num_substeps=10), + model=model, + mujoco_solver=solver, + get_mujoco_index_map=index_map, + ) + calls = [] + + def set_drive(**kwargs): + calls.append(kwargs) + return 1 + + entity = SimpleNamespace( + _physics_binding=SimpleNamespace( + _runtime=runtime, + joints=[ + SimpleNamespace(name="leader", joint_id=11, qd_size=1, qd_start=0), + SimpleNamespace(name="follower", joint_id=12, qd_size=1, qd_start=1), + ], + ), + set_newton_drive=set_drive, + ) + world = object() + monkeypatch.setattr( + "dexsim.engine.newton_physics.backend_registry.get_newton_backend", + lambda value: backend if value is world else None, + ) + assert _configure_newton_mimic_compliance( + result=SimpleNamespace(backend="newton", world=world), + entities=[entity], + state_joint_names=["leader", "follower"], + mimic_ids=[1], + mimic_parents=[0], + ) + np.testing.assert_allclose(solref.numpy()[0, 1], [0.002, 10.0]) + np.testing.assert_array_equal(solref.numpy()[0, [0, 2]], 9.0) + assert len(calls) == 1 + np.testing.assert_array_equal(calls[0]["joint_ids"], [1]) + np.testing.assert_allclose(calls[0]["target_ke"], [1.0]) + np.testing.assert_allclose(calls[0]["target_kd"], [0.2]) diff --git a/tests/sim/objects/test_collision_shapes.py b/tests/sim/objects/test_collision_shapes.py index df6bf92130..35550d1a72 100644 --- a/tests/sim/objects/test_collision_shapes.py +++ b/tests/sim/objects/test_collision_shapes.py @@ -127,6 +127,18 @@ def test_get_collision_shapes_reads_new_spawned_body_descriptor(): assert shapes[0].half_extents.tolist() == pytest.approx([0.1, 0.2, 0.3]) +def test_disabled_contact_response_keeps_planner_geometry() -> None: + collision = CollisionDesc.from_geometry(GeometryDesc.cube((0.2, 0.4, 0.6))) + collision.enable_collision = False + rigid_object = RigidObject.__new__(RigidObject) + rigid_object.uid = "disabled_contact" + rigid_object._entities = [_DescriptorEntity(collision)] + shapes = rigid_object.get_collision_shapes() + assert len(shapes) == 1 + assert shapes[0].shape_type == RigidBodyShape.BOX + assert shapes[0].half_extents.tolist() == pytest.approx([0.1, 0.2, 0.3]) + + def test_articulation_link_collision_reads_native_physical_shape_not_render_mesh(): from embodichain.lab.sim.objects.backends import ( collision_shapes_from_articulation_link, diff --git a/tests/sim/objects/test_fluid_object.py b/tests/sim/objects/test_fluid_object.py new file mode 100644 index 0000000000..b02033f7fb --- /dev/null +++ b/tests/sim/objects/test_fluid_object.py @@ -0,0 +1,218 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Fluid state contracts exercised without native simulation.""" + +from __future__ import annotations + +from types import SimpleNamespace +import numpy as np +import pytest +import torch +from embodichain.lab.sim.cfg import FluidObjectCfg, FluidSpawnCfg +from embodichain.lab.sim.objects.fluid import FluidObject + +pytestmark = pytest.mark.no_sim + + +class ParticleBatch: + def __init__(self, scene, entities): + self.scene = scene + self.ids = [e.index for e in entities] + + def fetch_particle_positions(self, out): + out.copy_(self.scene.state[self.ids, :, :3].reshape(-1, 3)) + + def fetch_particle_velocities(self, out): + out.copy_(self.scene.state[self.ids, :, 3:].reshape(-1, 3)) + + def apply_particle_positions(self, value): + self.scene.state[self.ids, :, :3] = value.reshape(len(self.ids), -1, 3) + + def apply_particle_velocities(self, value): + self.scene.state[self.ids, :, 3:] = value.reshape(len(self.ids), -1, 3) + + +@pytest.fixture +def fluid(): + points = np.array([[0.01, 0, 0], [0, 0.01, 0]], dtype=np.float32) + scene = SimpleNamespace(state=torch.zeros(2, 2, 6)) + scene.state[:, :, :3] = torch.from_numpy(points) + scene.create_particle_set_batch = lambda entities: ParticleBatch(scene, entities) + result = FluidObject( + FluidObjectCfg(uid="water", spawn=FluidSpawnCfg(positions=points)), + torch.device("cpu"), + ) + result._initialize_spawn_source( + points, np.eye(3, dtype=np.float32), on_write=lambda ids: None + ) + result._initialize_spawn_declaration(2) + result.attach_spawn_handles( + [SimpleNamespace(index=i, particle_count=2) for i in range(2)] + ) + result.bind_spawn(scene) + return result + + +def test_reads_do_not_alias_live_or_default_state(fluid): + before = fluid.data.particle_state_w + detached = fluid.data.particle_pos_w + detached.fill_(12) + torch.testing.assert_close(fluid.data.particle_state_w, before) + default = fluid.data.default_particle_state_w + default.fill_(13) + torch.testing.assert_close(fluid.data.default_particle_state_w, before) + + +def test_selected_write_and_reset_preserve_other_instance(fluid): + original = fluid.data.particle_state_w + changed = original[[1]].clone() + 4 + fluid.write_particle_state_to_sim(changed, [1]) + torch.testing.assert_close(fluid.data.particle_state_w[0], original[0]) + torch.testing.assert_close(fluid.data.particle_state_w[1], changed[0]) + fluid.reset([1]) + torch.testing.assert_close(fluid.data.particle_state_w, original) + + +def test_reset_to_pose_rotates_template_without_changing_default(fluid): + original = fluid.data.default_particle_state_w + q = 2**-0.5 + fluid.reset_to_pose(torch.tensor([[1, 2, 3, 0, 0, q, q]]), [1]) + expected = torch.tensor([[1, 2.01, 3], [0.99, 2, 3]]) + torch.testing.assert_close(fluid.data.particle_pos_w[1], expected) + torch.testing.assert_close(fluid.data.default_particle_state_w, original) + + +@pytest.mark.parametrize("ids", [[0, 0], [-1], [2], [0.5]]) +def test_invalid_selection_does_not_mutate(fluid, ids): + before = fluid.data.particle_state_w + with pytest.raises(ValueError): + fluid.write_particle_state_to_sim(torch.zeros(len(ids), 2, 6), ids) + torch.testing.assert_close(fluid.data.particle_state_w, before) + + +def test_empty_selection_and_invalid_state(fluid): + fluid.write_particle_state_to_sim(torch.empty(0, 2, 6), []) + with pytest.raises(ValueError, match="finite"): + fluid.write_particle_state_to_sim(torch.full((1, 2, 6), float("nan")), [0]) + with pytest.raises(ValueError, match="shape"): + fluid.write_particle_state_to_sim(torch.zeros(1, 3, 6), [0]) + + +def test_integer_initial_velocity_is_normalized(): + points = np.array([[0, 0, 0]], dtype=np.float32) + scene = SimpleNamespace(state=torch.zeros(1, 1, 6)) + scene.create_particle_set_batch = lambda entities: ParticleBatch(scene, entities) + fluid = FluidObject( + FluidObjectCfg( + uid="water", + spawn=FluidSpawnCfg(positions=points, initial_velocity=(1, 0, 0)), + ), + torch.device("cpu"), + ) + fluid._initialize_spawn_source( + points, np.eye(3, dtype=np.float32), on_write=lambda ids: None + ) + fluid._initialize_spawn_declaration(1) + fluid.attach_spawn_handles([SimpleNamespace(index=0, particle_count=1)]) + fluid.bind_spawn(scene) + torch.testing.assert_close(fluid.data.particle_vel_w, torch.tensor([[[1.0, 0, 0]]])) + fluid.reset_to_pose(torch.tensor([[0.0, 0, 0, 0, 0, 0, 1]])) + torch.testing.assert_close(fluid.data.particle_vel_w, torch.tensor([[[1.0, 0, 0]]])) + + +@pytest.mark.parametrize("inside_frame", [False, True]) +def test_fluid_write_respects_render_frame_cache_scope(inside_frame): + from embodichain.lab.sim import SimulationManager + + calls = [] + sim = SimpleNamespace( + physics=SimpleNamespace(invalidate_fluid_state=lambda **kwargs: None), + _render_state_published=True if inside_frame else None, + sync_render_state=lambda: calls.append("publish"), + ) + SimulationManager._invalidate_fluid_state(sim) + SimulationManager._ensure_render_state(sim) + SimulationManager._ensure_render_state(sim) + assert len(calls) == (1 if inside_frame else 2) + assert sim._render_state_published is (True if inside_frame else None) + + +def test_constructor_only_copies_config_without_sampling(monkeypatch): + from unittest.mock import Mock + + sample = Mock(side_effect=AssertionError("Construction must not sample.")) + monkeypatch.setattr(FluidSpawnCfg, "sample", sample) + cfg = FluidObjectCfg(uid="water", spawn=FluidSpawnCfg(kind="box")) + + fluid = FluidObject(cfg) + cfg.spawn.size = (9.0, 9.0, 9.0) + + assert fluid.device == torch.device("cpu") + assert fluid.is_declared + assert fluid.cfg.spawn.size == (0.05, 0.05, 0.05) + sample.assert_not_called() + with pytest.raises(RuntimeError, match="source"): + _ = fluid.num_particles + with pytest.raises(RuntimeError, match="registered"): + _ = fluid.num_instances + + +def test_binding_requires_a_resolved_source(): + from unittest.mock import Mock + + fluid = FluidObject(FluidObjectCfg(uid="water")) + fluid._initialize_spawn_declaration(1) + fluid.attach_spawn_handles([SimpleNamespace(index=0, particle_count=1)]) + scene = SimpleNamespace(create_particle_set_batch=Mock()) + + with pytest.raises(RuntimeError, match="source"): + fluid.bind_spawn(scene) + + scene.create_particle_set_batch.assert_not_called() + + +def test_source_cache_is_independent_and_rotates_initial_velocity(): + points = np.array([[0.01, 0.0, 0.0]], dtype=np.float32) + rotation = np.array([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) + fluid = FluidObject( + FluidObjectCfg( + uid="water", + init_rot=(0.0, 0.0, 90.0), + spawn=FluidSpawnCfg(positions=points, initial_velocity=(1, 0, 0)), + ) + ) + writes = [] + fluid._initialize_spawn_source(points, rotation, on_write=writes.append) + fluid._initialize_spawn_declaration(1) + scene = SimpleNamespace(state=torch.tensor([[[0.0, 0.01, 0.0, 0.0, 0.0, 0.0]]])) + scene.create_particle_set_batch = lambda entities: ParticleBatch(scene, entities) + fluid.attach_spawn_handles([SimpleNamespace(index=0, particle_count=1)]) + points.fill(99) + rotation.fill(0) + + fluid.bind_spawn(scene) + + torch.testing.assert_close( + fluid.data.particle_vel_w, torch.tensor([[[0.0, 1.0, 0.0]]]) + ) + fluid.reset_to_pose(torch.tensor([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]])) + torch.testing.assert_close( + fluid.data.particle_pos_w, torch.tensor([[[0.01, 0.0, 0.0]]]) + ) + assert writes == [[0]] + with pytest.raises(RuntimeError, match="already"): + fluid._initialize_spawn_source(points, rotation, on_write=writes.append) diff --git a/tests/sim/objects/test_fluid_runtime.py b/tests/sim/objects/test_fluid_runtime.py new file mode 100644 index 0000000000..20e9822529 --- /dev/null +++ b/tests/sim/objects/test_fluid_runtime.py @@ -0,0 +1,214 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Small CUDA integration test of the public fluid lifecycle.""" + +from __future__ import annotations + +import json +from pathlib import Path +import subprocess +import sys + +import pytest +import torch +from embodichain.lab.sim import SimulationManager, SimulationManagerCfg +from embodichain.lab.sim.cfg import ( + FluidObjectCfg, + FluidSpawnCfg, + FluidRenderCfg, + NewtonPhysicsCfg, + RenderCfg, +) + +pytestmark = [pytest.mark.gpu, pytest.mark.subprocess_sim] + + +class TestFluidRuntime: + @pytest.mark.parametrize( + "num_envs,automatic", + [(1, False), (2, False), (1, True)], + ids=["single-explicit", "two-explicit", "single-default"], + ) + def test_prepare_write_reset_and_repeat( + self, tmp_path: Path, num_envs: int, automatic: bool + ) -> None: + from dexsim.engine.newton_physics import DexUniSolverCfg + + if "sph_cfg" not in getattr(DexUniSolverCfg, "__dataclass_fields__", {}): + pytest.skip("This DexSim build does not compose SPH through DexUni.") + if num_envs > 1: + from dexsim.engine.newton_physics.solver_runtime import ( + ensure_solver_available, + ) + + solver = ensure_solver_available("sph") + if not ( + getattr(solver, "supports_world_isolation", False) + and getattr(solver, "supports_world_reset", False) + ): + pytest.skip("This DexSim SPH build supports only a single world.") + + def run_worlds(count: int, use_default: bool) -> dict: + result_path = tmp_path / f"lifecycle-{count}-{use_default}.json" + result = subprocess.run( + [ + sys.executable, + str(Path(__file__).resolve()), + str(result_path), + str(count), + str(int(use_default)), + ], + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr + state = json.loads(result_path.read_text()) + assert state["num_particles"] == 64 + assert state["num_envs"] == count + assert state["automatic"] is use_default + return state + + result = run_worlds(num_envs, automatic) + if num_envs > 1: + reference = run_worlds(1, automatic) + torch.testing.assert_close( + torch.tensor(result["first_velocity"]), + torch.tensor(reference["first_velocity"]), + rtol=1e-4, + atol=1e-5, + ) + + +def _run_fluid_lifecycle(num_envs: int, automatic: bool) -> dict[str, object]: + """Run the complete NoRender lifecycle in a fresh native process.""" + from dexsim.engine.newton_physics import DexUniSolverCfg, SPHSolverCfg + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + from embodichain.lab.sim.cfg import RigidObjectCfg + from embodichain.lab.sim.shapes import CubeCfg + + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device="cuda", + num_envs=num_envs, + # The two fluid volumes overlap in world space. SPH worlds must + # remain isolated even when their particles are nearby. + arena_space=0.02, + physics_dt=0.001, + render_cfg=RenderCfg(renderer="no-render"), + physics_cfg=NewtonPhysicsCfg( + num_substeps=2, + collision_cfg=None, + solver_cfg=( + None + if automatic + else DexUniSolverCfg( + sph_cfg=SPHSolverCfg( + sph_method="dfsph", + particle_support_radius=0.0225, + volume_map_boundary_enabled=False, + ) + ) + ), + ), + ) + ) + fluid = rigid = moving = None + try: + rigid = sim.add_rigid_object( + RigidObjectCfg( + uid="boundary", body_type="kinematic", shape=CubeCfg(size=[0.1] * 3) + ) + ) + moving = sim.add_rigid_object( + RigidObjectCfg( + uid="moving", + body_type="dynamic", + init_pos=(0.2, 0.0, 1.0), + shape=CubeCfg(size=[0.04] * 3), + ) + ) + fluid = sim.add_fluid_object( + FluidObjectCfg( + uid="water", + particle_spacing=0.01, + init_pos=(0.0, 0.0, 1.0), + spawn=FluidSpawnCfg(kind="box", size=(0.04, 0.04, 0.04)), + render=FluidRenderCfg(mode="none"), + ) + ) + with pytest.raises(RuntimeError, match="prepare"): + _ = fluid.data + sim.prepare() + backend = get_newton_backend(sim.spawn_result.world) + assert backend.solver_type == "dexuni" + assert backend.sph_solver is not None + assert backend.cfg.solver_cfg.sph_cfg.sph_method == "dfsph" + assert sim.get_fluid_object("water") is fluid + assert sim.get_asset("water") is fluid + assert fluid.num_particles == 64 + original = fluid.data.default_particle_state_w + assert original.shape == (num_envs, 64, 6) + if num_envs > 1: + assert torch.cdist(original[0, :, :3], original[1, :, :3]).min() < 0.0225 + sim.update(step=3, render_final_step=False) + first = fluid.data.particle_state_w + assert torch.isfinite(first).all() + assert torch.all(first[..., 2].mean(dim=1) < original[..., 2].mean(dim=1)) + diagnostics = sim.get_fluid_diagnostics("water") + assert diagnostics["particle_density"].shape == (num_envs, 64) + assert torch.isfinite(diagnostics["particle_density"]).all() + if num_envs > 1: + torch.testing.assert_close( + first[0, :, 3:], first[1, :, 3:], rtol=1e-4, atol=1e-5 + ) + body_before = moving.body_state.clone() + fluid.reset(env_ids=[0]) + torch.testing.assert_close(moving.body_state, body_before, rtol=0, atol=0) + selected = fluid.data.particle_state_w + torch.testing.assert_close(selected[0], original[0], rtol=0, atol=0) + torch.testing.assert_close(selected[1:], first[1:], rtol=0, atol=0) + sim.update(step=3, render_final_step=False) + continued = fluid.data.particle_state_w + torch.testing.assert_close(continued[0], first[0], rtol=1e-5, atol=1e-6) + assert continued[1, :, 2].mean() < first[1, :, 2].mean() + sim.reset_objects_state() + torch.testing.assert_close(fluid.data.particle_state_w, original) + sim.update(step=3, render_final_step=False) + torch.testing.assert_close( + fluid.data.particle_state_w, first, rtol=1e-5, atol=1e-6 + ) + with pytest.raises(NotImplementedError, match="before prepare"): + sim.add_fluid_object(FluidObjectCfg(uid="late")) + return { + "num_particles": fluid.num_particles, + "num_envs": num_envs, + "automatic": automatic, + "first_velocity": first[0, :, 3:].cpu().tolist(), + } + finally: + fluid = rigid = moving = None + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + + +if __name__ == "__main__": + Path(sys.argv[1]).write_text( + json.dumps(_run_fluid_lifecycle(int(sys.argv[2]), bool(int(sys.argv[3])))) + + "\n" + ) diff --git a/tests/sim/objects/test_fluid_visibility.py b/tests/sim/objects/test_fluid_visibility.py new file mode 100644 index 0000000000..acfc10ab94 --- /dev/null +++ b/tests/sim/objects/test_fluid_visibility.py @@ -0,0 +1,222 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""FastRT fluid primary visibility, independent of physical particle counts.""" + +from __future__ import annotations + +import json +from pathlib import Path +import subprocess +import sys + +import pytest + +pytestmark = [pytest.mark.gpu, pytest.mark.subprocess_sim] + + +@pytest.mark.parametrize("clipping", [False, True]) +def test_fluid_surface_changes_record_camera_pixels_in_every_environment( + tmp_path, clipping +): + from dexsim.engine.newton_physics import DexUniSolverCfg + + if "sph_cfg" not in getattr(DexUniSolverCfg, "__dataclass_fields__", {}): + pytest.skip("This DexSim build does not compose SPH through DexUni.") + result = subprocess.run( + [ + sys.executable, + str(Path(__file__).resolve()), + str(tmp_path), + str(int(clipping)), + ], + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr + results = json.loads((tmp_path / "visibility.json").read_text()) + assert results + for row in results: + assert row["visible_blue_pixels"] > 100 + for hidden, restored in zip( + row["hidden_blue_pixels"], row["restored_blue_pixels"] + ): + assert row["visible_blue_pixels"] > 4 * hidden + assert restored > 100 and restored > 4 * hidden + + +def _run_visibility_worker(output: Path, clipping: bool) -> None: + import numpy as np + import torch + from PIL import Image + from dexsim.engine.newton_physics.solver_runtime import ensure_solver_available + from embodichain.lab.sim import SimulationManager, SimulationManagerCfg + from embodichain.lab.sim.cfg import ( + FluidObjectCfg, + FluidSpawnCfg, + FluidRenderCfg, + NewtonPhysicsCfg, + RenderCfg, + ) + from embodichain.lab.sim.material import VisualMaterialCfg + from embodichain.lab.sim.sensors import CameraCfg + + solver = ensure_solver_available("sph") + num_envs = ( + 2 + if getattr(solver, "supports_world_isolation", False) + and getattr(solver, "supports_world_reset", False) + else 1 + ) + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + physics_dt=1 / 1440, + device="cuda", + num_envs=num_envs, + arena_space=1.0, + render_cfg=RenderCfg(renderer="fast-rt", spp=8), + physics_cfg=NewtonPhysicsCfg( + num_substeps=1, + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "sph_cfg": { + "sph_method": "dfsph", + "volume_map_boundary_enabled": False, + "particle_support_radius": 0.00675, + "pressure_relaxation": 0.2, + "max_velocity": 2.0, + }, + }, + ), + ) + ) + fluid = camera = None + try: + fluid = sim.add_fluid_object( + FluidObjectCfg( + uid="visible_water", + particle_spacing=0.003, + init_pos=(0, 0, 0.15), + spawn=FluidSpawnCfg(kind="box", size=(0.06,) * 3), + render=FluidRenderCfg( + mode="mesh_surface", + render_radius=0.0009, + clip_below_z=0.05 if clipping else None, + material=VisualMaterialCfg( + base_color=[0.05, 0.35, 0.9, 1], + roughness=0.1, + ), + mesh_surface={ + "gpu_enabled": True, + "cell_size": 0.00162, + "max_res": 128, + "kernel_radius": 0.0045, + "iso_density": 0.57, + "density_smooth_iters": 2, + "min_component_tris": 32, + }, + ), + ) + ) + camera = sim.add_sensor( + CameraCfg( + uid="record", + width=320, + height=240, + intrinsics=(350, 350, 160, 120), + visualization_role="record", + extrinsics=CameraCfg.ExtrinsicsCfg( + eye=(0.18, -0.24, 0.28), target=(0, 0, 0.15), up=(0, 0, 1) + ), + ) + ) + sim.prepare() + sim.update(step=2) + + def capture(): + sim.render_camera_group([camera.group_id]) + camera.update(fetch_only=True) + return camera.get_data()["color"][..., :3].cpu().numpy().copy() + + visible = capture() + before = fluid.data.particle_state_w + hidden_state = before.clone() + hidden_state[..., 0] += 10 + hidden_frames = [] + restored_frames = [] + # Several publications may occur before the next camera consumes one. + for state in (before, hidden_state): + fluid.write_particle_state_to_sim(state) + sim.sync_render_state() + hidden_frames.append(capture()) + fluid.write_particle_state_to_sim(before) + restored_frames.append(capture()) + for _ in range(3): + fluid.write_particle_state_to_sim(hidden_state) + hidden_frames.append(capture()) + # Visibility captures do not advance physics. + fluid.write_particle_state_to_sim(before) + restored_frames.append(capture()) + stream = torch.cuda.Stream(device=fluid.device) + stream.wait_stream(torch.cuda.current_stream(fluid.device)) + with torch.cuda.stream(stream): + fluid.write_particle_state_to_sim(hidden_state) + # Force the renderer's staging copy to wait for producer work. + torch.cuda._sleep(20_000_000) + hidden_frames.append(capture()) + fluid.write_particle_state_to_sim(before) + restored_frames.append(capture()) + torch.testing.assert_close(fluid.data.particle_state_w, before, rtol=0, atol=0) + + def blue_pixels(frame): + frame = frame.astype(np.int16) + return int( + ( + (frame[..., 2] > frame[..., 0] + 40) + & (frame[..., 2] > frame[..., 1] + 20) + ).sum() + ) + + results = [] + for env_id in range(num_envs): + Image.fromarray(visible[env_id]).save(output / f"visible_env{env_id}.png") + Image.fromarray(hidden_frames[-1][env_id]).save( + output / f"hidden_env{env_id}.png" + ) + results.append( + { + "env_id": env_id, + "visible_blue_pixels": blue_pixels(visible[env_id]), + "hidden_blue_pixels": [ + blue_pixels(frame[env_id]) for frame in hidden_frames + ], + "restored_blue_pixels": [ + blue_pixels(frame[env_id]) for frame in restored_frames + ], + } + ) + (output / "visibility.json").write_text(json.dumps(results, indent=2) + "\n") + finally: + fluid = camera = None + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + + +if __name__ == "__main__": + _run_visibility_worker(Path(sys.argv[1]), bool(int(sys.argv[2]))) diff --git a/tests/sim/objects/test_rigid_constraint.py b/tests/sim/objects/test_rigid_constraint.py index bfb7cb9db3..1be6779b53 100644 --- a/tests/sim/objects/test_rigid_constraint.py +++ b/tests/sim/objects/test_rigid_constraint.py @@ -264,6 +264,7 @@ def __init__(self, num_envs=4, arenas=None): self._rigid_objects = {} self._rigid_object_groups = {} self._deformable_objects = {} + self._fluid_objects = {} self._articulations = {} self._constraints = {} self.device = torch.device("cpu") diff --git a/tests/sim/objects/test_robot.py b/tests/sim/objects/test_robot.py index 792a140359..a9d35f1c53 100644 --- a/tests/sim/objects/test_robot.py +++ b/tests/sim/objects/test_robot.py @@ -747,6 +747,8 @@ class TestRobotNewton: default/CUDA classes already cover the shared control-part/FK/IK logic. """ + MIMIC_STABILIZER = False + def setup_method(self): physics_cfg = physics_cfg_for_backend("newton") physics_cfg.solver_cfg = { @@ -760,6 +762,7 @@ def setup_method(self): self.sim = SimulationManager(config) cfg = DexforceW1Cfg.from_dict({"uid": "dexforce_w1", "version": "v021"}) cfg.init_qpos = [0.0001 * (index + 1) for index in range(W1_ACTIVE_DOF)] + cfg.root_props.newton_mimic_stabilizer = self.MIMIC_STABILIZER self.robot: Robot = self.sim.add_robot(cfg=cfg) self.sim.prepare() @@ -799,9 +802,17 @@ def test_newton_robot_spawn_and_control(self): and "_HAND_" in state_joint_names[parent_id] for mimic_id, parent_id, _, _ in mimic_relations ) + if not self.MIMIC_STABILIZER: + # Without the stabilizer, reset starts each follower on its + # native mimic relation to avoid a corrective impulse. + for mimic_id, parent_id, multiplier, offset in mimic_relations: + initial_qpos_by_name[state_joint_names[mimic_id]] = ( + initial_qpos_by_name[state_joint_names[parent_id]] * multiplier + + offset + ) initial_qpos = self.robot.body_data.qpos[0].detach().cpu().tolist() assert dict(zip(state_joint_names, initial_qpos, strict=True)) == pytest.approx( - initial_qpos_by_name + initial_qpos_by_name, abs=1e-6 ) binding = self.robot._entities[0]._physics_binding @@ -828,25 +839,37 @@ def test_newton_robot_spawn_and_control(self): selected_eq = np.isin(mapping, np.asarray(constraint_rows, dtype=np.int32)) assert int(selected_eq.sum()) == len(mimic_relations) eq_solref = np.asarray(solver.mjw_model.eq_solref.numpy()) - np.testing.assert_allclose( - eq_solref[selected_eq], - np.broadcast_to([2.0e-3, 1.0e1], (len(mimic_relations), 2)), - ) target_ke = np.asarray(model.joint_target_ke.numpy()) target_kd = np.asarray(model.joint_target_kd.numpy()) target_mode = np.asarray(model.joint_target_mode.numpy()) eq_active = np.asarray(solver.mjw_data.eq_active.numpy()) assert np.all(eq_active[selected_eq]) - for mimic_id, parent_id, _, _ in mimic_relations: - child = runtime_joints[state_joint_names[mimic_id]] - parent = runtime_joints[state_joint_names[parent_id]] - assert target_ke[child.qd_start] == pytest.approx( - target_ke[parent.qd_start] * 1.0e-2 + if self.MIMIC_STABILIZER: + np.testing.assert_allclose( + eq_solref[selected_eq], + np.broadcast_to([2.0e-3, 1.0e1], (len(mimic_relations), 2)), ) - assert target_kd[child.qd_start] == pytest.approx( - target_kd[parent.qd_start] * 1.0e-2 + for mimic_id, parent_id, _, _ in mimic_relations: + child = runtime_joints[state_joint_names[mimic_id]] + parent = runtime_joints[state_joint_names[parent_id]] + assert target_ke[child.qd_start] == pytest.approx( + target_ke[parent.qd_start] * 1.0e-2 + ) + assert target_kd[child.qd_start] == pytest.approx( + target_kd[parent.qd_start] * 1.0e-2 + ) + assert target_mode[child.qd_start] == target_mode[parent.qd_start] + else: + # Native DexSim behavior: MuJoCo's default equality solref and + # the follower drives configured by the W1 preset (none). + np.testing.assert_allclose( + eq_solref[selected_eq], + np.broadcast_to([2.0e-2, 1.0], (len(mimic_relations), 2)), ) - assert target_mode[child.qd_start] == target_mode[parent.qd_start] + for mimic_id, _, _, _ in mimic_relations: + child = runtime_joints[state_joint_names[mimic_id]] + assert target_ke[child.qd_start] == 0.0 + assert target_kd[child.qd_start] == 0.0 # This physical check covers both hands and keeps the native coupled # constraints bounded under the W1's self-contacts. Default can also @@ -897,7 +920,11 @@ def test_newton_robot_spawn_and_control(self): self.robot.set_qpos(hand_target, name="right_eef") target_qpos = self.robot.body_data.target_qpos torch.testing.assert_close(target_qpos[:, right_eef_ids], hand_target) - for mimic_id, parent_id, multiplier, offset in mimic_relations: + # Follower targets are mirrored only by the opt-in stabilizer; native + # followers keep their own (here unused, zero-gain) targets. + for mimic_id, parent_id, multiplier, offset in ( + mimic_relations if self.MIMIC_STABILIZER else [] + ): torch.testing.assert_close( target_qpos[:, mimic_id], target_qpos[:, parent_id] * multiplier + offset, @@ -910,7 +937,9 @@ def test_newton_robot_spawn_and_control(self): self.robot.set_qvel(hand_velocity_target, name="right_eef") target_qvel = self.robot.body_data.target_qvel torch.testing.assert_close(target_qvel[:, right_eef_ids], hand_velocity_target) - for mimic_id, parent_id, multiplier, _ in mimic_relations: + for mimic_id, parent_id, multiplier, _ in ( + mimic_relations if self.MIMIC_STABILIZER else [] + ): torch.testing.assert_close( target_qvel[:, mimic_id], target_qvel[:, parent_id] * multiplier, @@ -930,3 +959,9 @@ def test_newton_robot_spawn_and_control(self): test_cpu = TestRobotCUDA() test_cpu.setup_method() test_cpu.test_compute_jacobian() + + +class TestRobotNewtonMimicStabilizer(TestRobotNewton): + """The same W1 coverage with the opt-in Default-parity mimic stabilizer.""" + + MIMIC_STABILIZER = True diff --git a/tests/sim/objects/test_soft_object_tet_mesh.py b/tests/sim/objects/test_soft_object_tet_mesh.py new file mode 100644 index 0000000000..610799a75f --- /dev/null +++ b/tests/sim/objects/test_soft_object_tet_mesh.py @@ -0,0 +1,115 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Explicit tetrahedral soft bodies and direct nodal-state writes.""" + +from __future__ import annotations + +import numpy as np +import pytest +import torch + +from embodichain.lab.sim import SimulationManager, SimulationManagerCfg +from embodichain.lab.sim.cfg import NewtonPhysicsCfg, VolumeDeformablePhysicsCfg +from embodichain.lab.sim.objects import ( + VolumeDeformableObject, + VolumeDeformableObjectCfg, +) + + +def _cube_tet_mesh(cells: int, size: float) -> tuple[np.ndarray, np.ndarray]: + """Structured cube with six tetrahedra per cell, vertices in x-fastest order.""" + n = cells + 1 + axis = np.linspace(-0.5 * size, 0.5 * size, n, dtype=np.float32) + z, y, x = np.meshgrid(axis, axis, axis, indexing="ij") + vertices = np.stack([x.ravel(), y.ravel(), z.ravel()], axis=1) + + def vid(i: int, j: int, k: int) -> int: + return i + n * (j + n * k) + + tets = [] + for k in range(cells): + for j in range(cells): + for i in range(cells): + c = [ + vid(i + dx, j + dy, k + dz) + for dz in (0, 1) + for dy in (0, 1) + for dx in (0, 1) + ] + for a, b in ((1, 3), (3, 2), (2, 6), (6, 4), (4, 5), (5, 1)): + tets.append([c[0], c[a], c[b], c[7]]) + return vertices, np.asarray(tets, dtype=np.int32) + + +class TestExplicitTetSoftBodyCUDA: + def setup_method(self) -> None: + self.vertices, self.tets = _cube_tet_mesh(cells=3, size=0.06) + self.init_pos = np.array([0.0, 0.0, 0.2], dtype=np.float32) + self.sim = SimulationManager( + SimulationManagerCfg( + headless=True, + physics_dt=1.0 / 60.0, + device="cuda", + num_envs=1, + physics_cfg=NewtonPhysicsCfg(solver_cfg={"solver_type": "vbd"}), + ) + ) + self.block: VolumeDeformableObject = self.sim.add_deformable_object( + cfg=VolumeDeformableObjectCfg( + uid="block", + shape={"vertices": self.vertices, "triangles": None}, + tetrahedra=self.tets, + init_pos=self.init_pos.tolist(), + attrs=VolumeDeformablePhysicsCfg( + youngs=3e4, poissons=0.35, density=250.0 + ), + ) + ) + self.sim.prepare() + + def test_particles_follow_authored_vertex_order(self) -> None: + positions = self.block.data.nodal_pos_w[0].cpu().numpy() + assert positions.shape == self.vertices.shape + np.testing.assert_allclose(positions, self.vertices + self.init_pos, atol=1e-6) + + def test_set_nodal_state_writes_positions_and_velocities(self) -> None: + rest = self.block.data.nodal_pos_w + squeezed = rest.clone() + center_z = float(self.init_pos[2]) + squeezed[..., 2] = center_z + 0.9 * (rest[..., 2] - center_z) + velocity = torch.zeros_like(rest) + velocity[..., 0] = 0.01 + + self.block.set_nodal_state(torch.cat((squeezed, velocity), dim=-1)) + + torch.testing.assert_close(self.block.data.nodal_pos_w, squeezed) + torch.testing.assert_close(self.block.data.nodal_vel_w, velocity) + for _ in range(10): + self.sim.update(step=1) + assert torch.isfinite(self.block.data.nodal_pos_w).all() + + def test_set_nodal_state_rejects_bad_shape(self) -> None: + with pytest.raises(ValueError, match="num_nodes, 6"): + self.block.set_nodal_state(self.block.data.nodal_pos_w) + + def teardown_method(self) -> None: + self.sim.destroy() + SimulationManager.flush_cleanup_queue() + self.__dict__.clear() + import gc + + gc.collect() diff --git a/tests/sim/objects/test_visual_object.py b/tests/sim/objects/test_visual_object.py index f2f95854fb..8cdbd9a2ef 100644 --- a/tests/sim/objects/test_visual_object.py +++ b/tests/sim/objects/test_visual_object.py @@ -280,6 +280,7 @@ def test_add_usd_classifies_visuals_without_promoting_collision_only_objects(tmp "_rigid_objects", "_rigid_object_groups", "_deformable_objects", + "_fluid_objects", "_articulations", "_robots", "_lights", @@ -492,3 +493,38 @@ def test_visual_material_read_respects_selected_environments(): visual, _ = _visual() assert visual.get_visual_material_inst([1]) == [None] assert visual.get_visual_material_inst([]) == [] + + +def test_visual_binding_preserves_resource_materials_on_reset(): + from embodichain.lab.sim.material import VisualMaterialCfg + + material_cfg = VisualMaterialCfg( + parameters={ + "transmission": 0.85, + "volume_thickness": 0.08, + "volume_attenuation_distance": 0.5, + "volume_attenuation_color": (0.2, 0.6, 0.8), + }, + ) + visual = VisualObject( + VisualObjectCfg( + uid="glass", + shape=MeshCfg(fpath="/assets/glass.obj", visual_material=material_cfg), + ) + ) + visual._initialize_spawn_declaration(1) + handle = _Handle(0.0) + originals = [MagicMock(name="first"), MagicMock(name="second")] + for i, material in enumerate(originals): + material.get_name.return_value = f"material_{i}" + material.get_template.return_value.get_name.return_value = f"template_{i}" + handle.render.materials = originals.copy() + visual.attach_spawn_handles([handle]) + + visual.bind_spawn(SimpleNamespace(topology_revision=1, world=_World([handle]))) + handle.render.materials = [None, None] + visual.reset() + + assert handle.render.materials == originals + for material in originals: + material.set_pbr_param.assert_not_called() diff --git a/tests/sim/sensors/test_camera.py b/tests/sim/sensors/test_camera.py index f0a925f140..c2530e500c 100644 --- a/tests/sim/sensors/test_camera.py +++ b/tests/sim/sensors/test_camera.py @@ -17,6 +17,9 @@ from __future__ import annotations import os +from pathlib import Path +import subprocess +import sys from types import SimpleNamespace from unittest.mock import MagicMock @@ -36,7 +39,7 @@ ) from embodichain.lab.sim.sensors.stereo import PairCameraView from embodichain.lab.sim.objects import Articulation -from embodichain.lab.sim.cfg import ArticulationCfg, RenderCfg +from embodichain.lab.sim.cfg import ArticulationCfg, DenoisingCfg, RenderCfg from embodichain.data import get_data_path from scripts.tutorials.sim.create_sensor import create_sensor as create_tutorial_sensor @@ -446,11 +449,31 @@ def test_ground_visibility_missing_sdk_fails_before_allocating_camera( @pytest.mark.gpu +@pytest.mark.subprocess_sim @pytest.mark.parametrize("renderer", ["fast-rt", "hybrid"]) def test_parallel_camera_ground_visibility_rgb_depth_reset_and_rebuild( - renderer, + renderer: str, ) -> None: + """Qualify each native renderer in its own process, independent of DLSS.""" + result = subprocess.run( + [ + sys.executable, + "-c", + "import runpy, sys; " + "runpy.run_path(sys.argv[1])['_run_camera_ground_visibility_case'](sys.argv[2])", + str(Path(__file__).resolve()), + renderer, + ], + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr + + +def _run_camera_ground_visibility_case(renderer: str) -> None: """Mixed policies within a group and between cameras preserve one physical floor.""" + from dexsim.types import Renderer from embodichain.lab.sim.cfg import VisualObjectCfg from embodichain.lab.sim.shapes import CubeCfg @@ -459,10 +482,22 @@ def test_parallel_camera_ground_visibility_rgb_depth_reset_and_rebuild( headless=True, device="cpu", num_envs=4, - render_cfg=RenderCfg(renderer=renderer), + # Ground filtering compares raw RGB/depth; NGX availability and + # reconstruction at this tiny resolution are separate contracts. + render_cfg=RenderCfg( + renderer=renderer, + denoising=DenoisingCfg(window="off", offscreen="off"), + ), ) ) try: + assert ( + sim.get_world().get_renderer_type() + == { + "fast-rt": Renderer.FASTRT, + "hybrid": Renderer.HYBRID, + }[renderer] + ) sim.add_visual_object( VisualObjectCfg( uid="under_floor", @@ -537,7 +572,7 @@ def capture(): assert cameras[1].get_capture_metadata()["ground_plane_visible"] == (False,) * 4 assert sim._default_plane.object_desc.physics is not None finally: - sim.destroy() + sim.destroy(exit_process=False) SimulationManager.flush_cleanup_queue() diff --git a/tests/sim/sensors/test_camera_reset.py b/tests/sim/sensors/test_camera_reset.py index 203320ee83..13cb5c8c1e 100644 --- a/tests/sim/sensors/test_camera_reset.py +++ b/tests/sim/sensors/test_camera_reset.py @@ -74,6 +74,8 @@ def test_reset_preserves_batch_dimension_and_unselected_views( camera.device = torch.device("cpu") camera._entities = [_View() for _ in range(3)] camera._num_instances = len(camera._entities) + camera._is_destroyed = False + camera._ground_plane_visibility = [True] * camera.num_instances camera.reset(env_ids) expected = camera.cfg.extrinsics.transformation.clone() diff --git a/tests/sim/sensors/test_contact_newton_e2e.py b/tests/sim/sensors/test_contact_newton_e2e.py index 94b1816d33..203295ebed 100644 --- a/tests/sim/sensors/test_contact_newton_e2e.py +++ b/tests/sim/sensors/test_contact_newton_e2e.py @@ -17,23 +17,44 @@ from __future__ import annotations +import math +from contextlib import nullcontext +from types import SimpleNamespace + import pytest +import torch import warp as wp from embodichain.lab.sim.cfg import ( + ArticulationCfg, MassPropertiesCfg, NewtonPhysicsCfg, RigidBodyPhysicsCfg, RigidObjectCfg, ) -from embodichain.lab.sim.sensors import ContactSensorCfg +from embodichain.lab.gym.envs.base_env import BaseEnv +from embodichain.lab.sim.sensors import ( + ArticulationContactFilterCfg, + ContactSensorCfg, +) from embodichain.lab.sim.shapes import CubeCfg from embodichain.lab.sim.sim_manager import SimulationManager, SimulationManagerCfg +from embodichain_robots import URRobotCfg pytestmark = [pytest.mark.requires_sim, pytest.mark.gpu] -def test_newton_contact_sensor_reports_each_arena() -> None: +@pytest.mark.parametrize( + "solver_type", ["mujoco_warp", pytest.param(None, id="default_dexuni")] +) +@pytest.mark.parametrize("num_substeps", [1, 10]) +def test_newton_contact_sensor_reports_each_arena( + solver_type: str | None, num_substeps: int +) -> None: + from dexsim.scene import ContactBuffer + + if solver_type is None and not hasattr(ContactBuffer, "impulse_valid"): + pytest.skip("DexUni contacts require DexSim's current dev API.") wp.init() if not wp.is_cuda_available(): pytest.skip("CUDA is required for the Newton contact E2E contract.") @@ -45,14 +66,15 @@ def test_newton_contact_sensor_reports_each_arena() -> None: arena_space=2.0, physics_cfg=NewtonPhysicsCfg( device="cuda:0", - num_substeps=1, + num_substeps=num_substeps, use_cuda_graph=False, # Exercise a multi-point manifold so query-side reductions # cannot satisfy this regression contract accidentally. - solver_cfg={ - "solver_type": "mujoco_warp", - "enable_multiccd": True, - }, + solver_cfg=( + {"solver_type": solver_type, "enable_multiccd": True} + if solver_type is not None + else None + ), ), ) ) @@ -86,17 +108,398 @@ def test_newton_contact_sensor_reports_each_arena() -> None: sensor.update() assert sensor.contact_capabilities.geometry + # The default DexUni path assigns this all-rigid scene to MuJoCo. assert sensor.contact_capabilities.impulse + assert sensor.contact_capabilities.friction counts = sensor._num_contacts_per_env.cpu().tolist() - assert counts == [4, 4] data = sensor.get_data() assert data["is_valid"].sum(dim=1).cpu().tolist() == counts - assert (data["impulse"][data["is_valid"]] > 1.0e-7).all() + if solver_type == "mujoco_warp": + assert counts == [4, 4] + else: + assert all(count > 0 for count in counts) + assert data["impulse_valid"][data["is_valid"]].all() + assert data["friction_valid"][data["is_valid"]].all() + measured = data["is_valid"] & data["impulse_valid"] + assert (data["impulse"][measured] > 1.0e-7).all() actor_ids = data["user_ids"][data["is_valid"]] assert all( sensor.get_actor_info(actor_id).path.endswith(("/ground", "/cube")) for actor_id in set(actor_ids.flatten().cpu().tolist()) ) + history = sensor.create_history( + "support", + sensor.get_actor_ids("cube"), + counterpart_ids=sensor.get_actor_ids("ground"), + force_threshold=1.0, + ) + # Keep the physical load invariant while varying both the solver's + # internal substeps and the actual World.update interval. + for dt in (0.01, 0.02): + sensor.begin_control_step() + sim.update(dt, step=3, after_substep=sensor.update_physics_step) + torch.testing.assert_close( + history.force[..., 2], + torch.full_like(history.contact, 9.81, dtype=torch.float32), + rtol=0.01, + atol=0.05, + ) + assert history.found.all() + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + from dexsim.engine.newton_physics import teardown_newton_physics + + teardown_newton_physics() + + +@pytest.fixture +def dexuni_contact_scene(tmp_path): + """A native contact report with articulation dynamics invalidated by reset.""" + urdf = tmp_path / "slider.urdf" + urdf.write_text(""" + + + + + + + + + + + """) + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device="cuda:0", + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + physics_dt=0.01, + num_substeps=10, + use_cuda_graph=False, + ), + ) + ) + try: + sim.add_articulation( + ArticulationCfg(uid="slider", fpath=str(urdf), build_pk_chain=False) + ) + sim.add_rigid_object( + RigidObjectCfg( + uid="ground", + shape=CubeCfg(size=[1.0, 1.0, 0.1]), + attrs=RigidBodyPhysicsCfg(), + body_type="static", + init_pos=(0.0, 0.0, 0.1), + ) + ) + sim.add_rigid_object( + RigidObjectCfg( + uid="cube", + shape=CubeCfg(size=[0.2, 0.2, 0.2]), + attrs=RigidBodyPhysicsCfg(mass_props=MassPropertiesCfg(mass=1.0)), + init_pos=(0.0, 0.0, 0.4), + ) + ) + sensor = sim.add_sensor( + ContactSensorCfg( + uid="contacts", + rigid_uid_list=["ground", "cube"], + max_contacts_per_env=32, + ) + ) + sim.update(step=150) + sensor.update() + assert sensor.get_data()["is_valid"].any() + yield sim, sensor + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + from dexsim.engine.newton_physics import teardown_newton_physics + + teardown_newton_physics() + + +def test_dexuni_physics_reset_keeps_initial_sensor_observations_readable( + dexuni_contact_scene, +): + sim, sensor = dexuni_contact_scene + env = SimpleNamespace( + sim=sim, + sensors={"contacts": sensor}, + device=sim.device, + num_envs=sim.num_envs, + _camera_group_ids=[], + _profiler=SimpleNamespace(section=lambda *_: nullcontext()), + ) + assert not sensor.requires_substep_update + assert BaseEnv._get_sensor_obs(env)["contacts"]["is_valid"].any() + # Sensor-only reset must retain the valid, same-step native report. + sensor.reset([0]) + assert BaseEnv._get_sensor_obs(env)["contacts"]["is_valid"].any() + # Reproduce Gym's physical reset ordering, including articulation clear. + sim.reset_objects_state(env_ids=[0], excluded_sensor_uids=[sensor.uid]) + sensor.reset([0]) + for _ in range(2): + data = BaseEnv._get_sensor_obs(env)["contacts"] + assert not data["is_valid"].any() + assert not data["impulse_valid"].any() + assert not data["friction_valid"].any() + assert sensor.total_current_contacts == 0 + sim.update(step=150) + assert BaseEnv._get_sensor_obs(env)["contacts"]["is_valid"].any() + + +@pytest.mark.parametrize("history_sample", [False, True]) +def test_dexuni_external_graph_consumes_eager_native_samples( + dexuni_contact_scene, history_sample +): + sim, sensor = dexuni_contact_scene + if history_sample: + sensor.create_history( + "support", sensor.get_actor_ids("cube"), force_threshold=1.0 + ) + sim.update(step=3, after_substep=sensor.update_physics_step) + stream = torch.cuda.Stream(device=sim.device) + stream.wait_stream(torch.cuda.current_stream(sim.device)) + with torch.cuda.stream(stream): + sensor.update() + stream.synchronize() + # This is the review's native same-step warmup/capture sequence. The + # boundary error must occur before DexSim can wait on an uncaptured event. + with pytest.raises(RuntimeError, match="outside external CUDA capture"): + with torch.cuda.graph(torch.cuda.CUDAGraph(), stream=stream): + if history_sample: + sensor.update_physics_step(0.01) + else: + sensor.update() + + # Mixed DexUni reports may provide geometry-only rows with zero impulse. + # Consume their validity so replay freshness does not assume force support. + output = torch.empty_like(sensor.get_data()["is_valid"]) + stream.wait_stream(torch.cuda.current_stream(sim.device)) + graph = torch.cuda.CUDAGraph() + with torch.cuda.graph(graph, stream=stream): + data = sensor.get_data() + output.copy_(data["is_valid"]) + + def replay_latest(): + stream.wait_stream(torch.cuda.current_stream(sim.device)) + with torch.cuda.stream(stream): + graph.replay() + stream.synchronize() + data = sensor.get_data() + torch.testing.assert_close(output, data["is_valid"]) + + replay_latest() + assert output.any() + sim.reset_objects_state(env_ids=[0], excluded_sensor_uids=[sensor.uid]) + sensor.reset([0]) + sensor.update() + replay_latest() + assert output.count_nonzero() == 0 + sim.update( + step=150, after_substep=sensor.update_physics_step if history_sample else None + ) + sensor.update() + replay_latest() + assert output.any() + + +@pytest.mark.parametrize("solver_path", ["pure_mujoco", "coupled"]) +@pytest.mark.usefixtures("requires_dexuni_contact_query") +def test_dexuni_pure_mujoco_contact_sensor_reports_impulse(solver_path: str) -> None: + """Pure DexUni/MuJoCo exposes geometry, normal and friction impulses.""" + wp.init() + if not wp.is_cuda_available(): + pytest.skip("CUDA is required for the DexUni contact contract.") + + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device="cuda:0", + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + num_substeps=1, + use_cuda_graph=False, + solver_cfg={ + "solver_type": "dexuni", + "joint_mode": "dynamic", + "contact_mode": "auto", + **({"coupling": "two_way"} if solver_path == "coupled" else {}), + }, + ), + ) + ) + try: + sim.add_rigid_object( + RigidObjectCfg( + uid="ground", + shape=CubeCfg(size=[3.0, 3.0, 0.1]), + attrs=RigidBodyPhysicsCfg(), + body_type="static", + init_pos=(0.0, 0.0, -0.05), + ) + ) + sim.add_robot( + URRobotCfg.from_dict( + { + "robot_type": "ur10", + "uid": "robot", + "init_pos": (0.0, 0.0, 0.0), + } + ) + ) + if solver_path == "coupled": + sim.add_rigid_object( + RigidObjectCfg( + uid="box", + shape=CubeCfg(size=[0.1, 0.1, 0.1]), + attrs=RigidBodyPhysicsCfg(mass_props=MassPropertiesCfg(mass=1.0)), + init_pos=(0.3, 0.0, 0.2), + ) + ) + sensor = sim.add_sensor( + ContactSensorCfg( + uid="contacts", + rigid_uid_list=["ground"], + articulation_cfg_list=[ + ArticulationContactFilterCfg(articulation_uid="robot") + ], + filter_need_both_actor=False, + max_contacts_per_env=64, + ) + ) + + sim.update(step=120 if solver_path == "coupled" else 10) + sensor.update() + + assert sensor.contact_capabilities.geometry + data = sensor.get_data() + valid = data["is_valid"] & data["impulse_valid"] & data["friction_valid"] + if solver_path == "pure_mujoco": + assert sensor.contact_capabilities.impulse + assert sensor.contact_capabilities.friction + assert torch.equal(valid, data["is_valid"]) + else: + assert not sensor.contact_capabilities.impulse + assert (data["is_valid"] & ~data["impulse_valid"]).any() + raw = sensor._query.fetch_solver_report() + for field in ("impulse_valid", "friction_valid"): + assert data[field].sum() == getattr(raw, field)[: raw.count].sum() + assert valid.any() + assert torch.isfinite(data["position"][valid]).all() + assert torch.isfinite(data["normal"][valid]).all() + assert torch.isfinite(data["friction"][valid]).all() + assert torch.isfinite(data["impulse"][valid]).all() + assert torch.isfinite(data["distance"][valid]).all() + normal = data["normal"][valid] + normal_norm = torch.linalg.vector_norm(normal, dim=-1) + assert torch.allclose(normal_norm, torch.ones_like(normal_norm), atol=1.0e-4) + tangent_residual = (data["friction"][valid] * normal).sum(dim=-1).abs() + assert tangent_residual.max() < 1.0e-4 + assert (data["user_ids"][valid] >= 0).all() + assert (data["impulse"][valid] > 1.0e-7).any() + + # Cross-check the adapter against DexUni's native force report. The + # sensor's normal impulse is the normal component of that force over + # the solver substep, so this catches axis/sign/unit regressions that + # finite-value assertions cannot detect. + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + from newton import Contacts + + backend = get_newton_backend(sim._world) + assert backend.solver.features.backend == solver_path + report = Contacts( + backend.mujoco_solver.get_max_contact_count(), + 0, + device=backend.model.device, + requested_attributes={"force"}, + ) + backend.mujoco_solver.update_contacts(report, backend.state_0) + raw_count = int(report.rigid_contact_count.numpy()[0]) + raw_forces = report.force.numpy()[:raw_count, :3] + raw_normals = report.rigid_contact_normal.numpy()[:raw_count] + substep_dt = float(backend.cfg.dt) / int(backend.cfg.num_substeps) + raw_normal_impulse = abs((raw_forces * raw_normals).sum(axis=1)) * substep_dt + raw_normal_impulse = raw_normal_impulse[raw_normal_impulse > 1.0e-7] + sensor_total = float(data["impulse"][valid].sum().item()) + raw_total = float(raw_normal_impulse.sum()) + assert raw_total > 0.0 + assert math.isclose(sensor_total, raw_total, rel_tol=2.0e-3, abs_tol=2.0e-4) + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + from dexsim.engine.newton_physics import teardown_newton_physics + + teardown_newton_physics() + + +@pytest.mark.usefixtures("requires_dexuni_contact_query") +def test_dexuni_vbd_contact_sensor_exposes_geometry_only() -> None: + """VBD-owned rigid contacts keep geometry but disable force fields.""" + wp.init() + if not wp.is_cuda_available(): + pytest.skip("CUDA is required for the DexUni contact contract.") + + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device="cuda:0", + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + num_substeps=1, + use_cuda_graph=False, + # Explicit ownership keeps this a VBD geometry-only contract + # even though all-rigid scenes now default to pure MuJoCo. + solver_cfg={"solver_type": "dexuni", "mujoco_articulations": ()}, + ), + ) + ) + try: + sim.add_rigid_object( + RigidObjectCfg( + uid="ground", + shape=CubeCfg(size=[1.0, 1.0, 0.1]), + attrs=RigidBodyPhysicsCfg(), + body_type="static", + ) + ) + sim.add_rigid_object( + RigidObjectCfg( + uid="cube", + shape=CubeCfg(size=[0.2, 0.2, 0.2]), + attrs=RigidBodyPhysicsCfg(mass_props=MassPropertiesCfg(mass=1.0)), + body_type="dynamic", + init_pos=(0.0, 0.0, 0.4), + ) + ) + sensor = sim.add_sensor( + ContactSensorCfg( + uid="contacts", + rigid_uid_list=["cube"], + filter_need_both_actor=False, + max_contacts_per_env=16, + ) + ) + + sim.update(step=120) + sensor.update() + + assert sensor.contact_capabilities.geometry + assert not sensor.contact_capabilities.impulse + assert not sensor.contact_capabilities.friction + data = sensor.get_data() + valid = data["is_valid"] + assert valid.any() + assert torch.isfinite(data["position"][valid]).all() + assert torch.isfinite(data["normal"][valid]).all() + assert torch.isfinite(data["distance"][valid]).all() + assert not data["impulse_valid"][valid].any() + assert not data["friction_valid"][valid].any() + assert torch.count_nonzero(data["impulse"][valid]) == 0 + assert torch.count_nonzero(data["friction"][valid]) == 0 finally: sim.destroy(exit_process=False) SimulationManager.flush_cleanup_queue() diff --git a/tests/sim/spawn/test_descriptors.py b/tests/sim/spawn/test_descriptors.py index 835e05081e..65118a6ac5 100644 --- a/tests/sim/spawn/test_descriptors.py +++ b/tests/sim/spawn/test_descriptors.py @@ -1177,7 +1177,144 @@ def test_articulation_constructor_defers_newton_properties_until_configure() -> assert contact.ke is None -@pytest.mark.parametrize("solver_type", [None, "auto", "mujoco_warp"]) +@pytest.mark.parametrize("mode", ["preserve", "overlay"]) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_articulation_newton_gravity_compensation_is_creation_intent(mode) -> None: + cfg = ArticulationCfg.from_dict( + { + "uid": "robot", + "fpath": "robot.urdf", + "asset_physics_mode": mode, + "root_props": {"newton_gravity_compensation": 1.0}, + } + ) + descriptor = articulation_desc_from_cfg(cfg, newton_solver_type="dexuni") + assert descriptor.newton_gravity_compensation == 1.0 + + resolved = _resolved_articulation_desc() + configure_articulation_desc(resolved, cfg, newton_solver_type="dexuni") + assert resolved.newton_gravity_compensation == 1.0 + + +def test_articulation_unconfigured_gravity_compensation_preserves_source() -> None: + descriptor = _resolved_articulation_desc() + descriptor.newton_gravity_compensation = 0.5 + configure_articulation_desc( + descriptor, ArticulationCfg(), newton_solver_type="dexuni" + ) + assert descriptor.newton_gravity_compensation == 0.5 + + +@pytest.mark.parametrize("mode", ["preserve", "overlay"]) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_usd_articulation_forwards_gravity_compensation(monkeypatch, mode) -> None: + source = _resolved_articulation_desc() + monkeypatch.setattr( + "embodichain.lab.sim.spawn.usd._parse_singleton", + lambda *_args, **_kwargs: (SimpleNamespace(materials={}), source), + ) + cfg = ArticulationCfg.from_dict( + { + "uid": "robot", + "fpath": "robot.usd", + "asset_physics_mode": mode, + "root_props": {"newton_gravity_compensation": 0.5}, + } + ) + descriptor, _ = articulation_desc_from_usd(cfg, newton_solver_type="dexuni") + assert descriptor.newton_gravity_compensation == 0.5 + + +@pytest.mark.parametrize("value", [-0.1, 1.1, float("nan"), float("inf")]) +def test_articulation_gravity_compensation_rejects_invalid_values(value) -> None: + cfg = ArticulationCfg() + cfg.root_props.newton_gravity_compensation = value + with pytest.raises(ValueError, match="newton_gravity_compensation"): + articulation_desc_from_cfg( + cfg, source_path="robot.urdf", newton_solver_type="dexuni" + ) + + +def test_articulation_gravity_compensation_rejects_default_backend() -> None: + cfg = ArticulationCfg() + cfg.root_props.newton_gravity_compensation = 1.0 + with pytest.raises(ValueError, match="Newton backend"): + articulation_desc_from_cfg(cfg, source_path="robot.urdf") + + +def test_gravity_compensation_requires_native_descriptor_support(monkeypatch) -> None: + monkeypatch.delattr(ArticulationDesc, "newton_gravity_compensation", raising=False) + cfg = ArticulationCfg() + cfg.root_props.newton_gravity_compensation = 1.0 + with pytest.raises(RuntimeError, match="requires DexSim dev"): + articulation_desc_from_cfg( + cfg, source_path="robot.urdf", newton_solver_type="dexuni" + ) + + +@pytest.mark.parametrize("target_mode", ["position_velocity", "none"]) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_newton_passive_damping_is_independent_of_drive_gains(target_mode) -> None: + cfg = ArticulationCfg.from_dict( + { + "asset_physics_mode": "overlay", + "joint_drive_props": { + "backend": "newton", + "target_mode": target_mode, + "stiffness": 12.0, + "damping": 4.0, + "passive_damping": {"arm_.*": 50.0}, + }, + } + ) + descriptor = _resolved_articulation_desc() + configure_articulation_desc(descriptor, cfg, newton_solver_type="dexuni") + joint = descriptor.get_joint_desc("arm_joint") + assert joint.newton.damping == 50.0 + expected_kd = 0.0 if target_mode == "none" else 4.0 + assert joint.newton.target_kd == expected_kd + assert joint.dexsim.damping == expected_kd + round_trip = JointDrivePropertiesCfg.from_dict(cfg.joint_drive_props.to_dict()) + assert round_trip.passive_damping == {"arm_.*": 50.0} + + +@pytest.mark.parametrize("value", [-1.0, float("nan"), float("inf")]) +def test_newton_passive_damping_rejects_invalid_values(value) -> None: + cfg = ArticulationCfg(asset_physics_mode="overlay") + cfg.joint_drive_props = NewtonJointDrivePropertiesCfg() + cfg.joint_drive_props.passive_damping = value + with pytest.raises(ValueError, match="passive_damping"): + configure_articulation_desc( + _resolved_articulation_desc(), cfg, newton_solver_type="dexuni" + ) + + +def test_newton_passive_damping_rejects_default_backend() -> None: + cfg = ArticulationCfg(asset_physics_mode="overlay") + cfg.joint_drive_props = NewtonJointDrivePropertiesCfg() + cfg.joint_drive_props.passive_damping = 50.0 + with pytest.raises(ValueError, match="Newton backend"): + configure_articulation_desc(_resolved_articulation_desc(), cfg) + + +def test_passive_damping_rejects_native_merges_that_discard_it(monkeypatch) -> None: + cfg = ArticulationCfg( + asset_physics_mode="overlay", + joint_drive_props=NewtonJointDrivePropertiesCfg(passive_damping=50.0), + ) + descriptor = _resolved_articulation_desc() + set_properties = descriptor.set_joint_properties + + def discard_damping(*args, **kwargs): + set_properties(*args, **kwargs) + descriptor.get_joint_desc("arm_joint").newton.damping = None + + monkeypatch.setattr(descriptor, "set_joint_properties", discard_damping) + with pytest.raises(RuntimeError, match="did not retain passive_damping"): + configure_articulation_desc(descriptor, cfg, newton_solver_type="dexuni") + + +@pytest.mark.parametrize("solver_type", [None, "dexuni", "mujoco_warp"]) @pytest.mark.parametrize("mode", ["preserve", "overlay"]) @pytest.mark.parametrize("source_gap", [None, 0.0, 0.025]) def test_imported_articulation_gap_default_is_owned_by_embodichain( @@ -1195,7 +1332,7 @@ def test_imported_articulation_gap_default_is_owned_by_embodichain( assert collision.newton.margin == 0.012 -@pytest.mark.parametrize("solver_type", [None, "auto", "mujoco_warp"]) +@pytest.mark.parametrize("solver_type", [None, "dexuni", "mujoco_warp"]) @pytest.mark.parametrize("has_geometry", [True, False, None]) def test_urdf_native_shapes_receive_gap_without_synthetic_geometry( solver_type: str | None, has_geometry: bool | None @@ -1220,7 +1357,7 @@ def test_urdf_native_shapes_receive_gap_without_synthetic_geometry( assert link.collisions[0].newton.ke is None -@pytest.mark.parametrize("solver_type", [None, "auto", "mujoco_warp"]) +@pytest.mark.parametrize("solver_type", [None, "dexuni", "mujoco_warp"]) @pytest.mark.parametrize("mode", ["preserve", "overlay"]) @pytest.mark.parametrize("source_gap", [None, 0.0, 0.025]) def test_imported_rigid_gap_default_is_owned_by_embodichain( @@ -1673,7 +1810,7 @@ def test_articulation_array_qpos_limits_compile_before_backend_build() -> None: assert joint.upper_limit == pytest.approx(0.75) -@pytest.mark.parametrize("solver_type", [None, "auto"]) +@pytest.mark.parametrize("solver_type", [None, "dexuni"]) @pytest.mark.parametrize("authored_velocity", [0.05, None]) def test_robot_default_drive_preserves_authored_velocity_limit( solver_type: str | None, @@ -1691,7 +1828,7 @@ def test_robot_default_drive_preserves_authored_velocity_limit( assert joint.dexsim.max_velocity == authored_velocity -@pytest.mark.parametrize("solver_type", [None, "auto"]) +@pytest.mark.parametrize("solver_type", [None, "dexuni"]) def test_explicit_zero_speed_override_is_preserved(solver_type: str | None) -> None: descriptor = _resolved_articulation_desc() joint = descriptor.get_joint_desc("arm_joint") @@ -2275,6 +2412,32 @@ def test_spawn_post_config_only_applies_render_uv() -> None: render_body.set_projective_uv.assert_called_once_with() +@pytest.mark.parametrize("enabled", [True, False]) +def test_newton_mimic_stabilizer_can_be_disabled(monkeypatch, enabled) -> None: + configure = Mock(return_value=True) + monkeypatch.setattr( + "embodichain.lab.sim.objects.articulation._configure_newton_mimic_compliance", + configure, + ) + articulation = object.__new__(Articulation) + articulation.cfg = SimpleNamespace( + compute_uv=False, + root_props=ArticulationRootPropertiesCfg(newton_mimic_stabilizer=enabled), + ) + articulation._entities = [Mock(joint_dof_layout=[])] + articulation._prepared_default_root_topology_revision = -1 + articulation._mimic_info = SimpleNamespace( + mimic_id=np.array([1], dtype=np.int32), + mimic_parent=np.array([0], dtype=np.int32), + ) + articulation._state_joint_names = Mock(return_value=["leader", "follower"]) + + articulation._apply_spawn_config() + + assert articulation._newton_mimic_compliance_configured is enabled + assert configure.called is enabled + + def test_rigid_object_spawn_applies_projective_uv() -> None: render_body = Mock() entity = Mock() @@ -2413,3 +2576,53 @@ def test_rigid_descriptor_rejects_orientation_without_principal_moments() -> Non ) with pytest.raises(ValueError, match="requires explicit principal inertia"): rigid_desc_from_cfg(cfg) + + +@pytest.mark.parametrize( + "kind", ["rigid", "visual", "volume", "surface", "surface_visual"] +) +def test_shape_optical_transmission_reaches_spawn_material(kind): + import json + from embodichain.lab.sim.cfg import ( + RigidObjectCfg, + VisualObjectCfg, + VolumeDeformableObjectCfg, + SurfaceDeformableObjectCfg, + ) + from embodichain.lab.sim.material import VisualMaterialCfg + from embodichain.lab.sim.spawn.descriptors import ( + visual_desc_from_cfg, + volume_deformable_desc_from_cfg, + surface_deformable_desc_from_cfg, + ) + + cfg_type, compile_descriptor = { + "rigid": (RigidObjectCfg, rigid_desc_from_cfg), + "visual": (VisualObjectCfg, visual_desc_from_cfg), + "volume": (VolumeDeformableObjectCfg, volume_deformable_desc_from_cfg), + "surface": (SurfaceDeformableObjectCfg, surface_deformable_desc_from_cfg), + "surface_visual": ( + SurfaceDeformableObjectCfg, + surface_deformable_desc_from_cfg, + ), + }[kind] + cfg = cfg_type( + uid="glass", + shape=MeshCfg( + fpath="/assets/glass.obj", + visual_material=VisualMaterialCfg(parameters={"transmission": 0.85}), + ), + ) + + if kind == "surface_visual": + cfg.visual_shape = MeshCfg( + fpath="/assets/render.obj", visual_material=cfg.shape.visual_material + ) + cfg.shape.visual_material = VisualMaterialCfg(parameters={"transmission": 0.1}) + + _, materials = compile_descriptor(cfg) + + assert ( + json.loads(next(iter(materials.values())).parameters_json)["transmission"] + == 0.85 + ) diff --git a/tests/sim/spawn/test_scene.py b/tests/sim/spawn/test_scene.py index f8ce89fc7b..e578464378 100644 --- a/tests/sim/spawn/test_scene.py +++ b/tests/sim/spawn/test_scene.py @@ -26,6 +26,7 @@ from embodichain.lab.sim.objects import ( Articulation, DeformableObject, + FluidObject, RigidObject, RigidObjectGroup, Robot, @@ -279,7 +280,7 @@ def test_rigid_object_receives_instance_count_only_when_declared() -> None: @pytest.mark.parametrize( "facade_type", - [RigidObject, Articulation, Robot, RigidObjectGroup, DeformableObject], + [RigidObject, Articulation, Robot, RigidObjectGroup, DeformableObject, FluidObject], ) def test_object_constructors_hide_spawn_lifecycle_parameters( facade_type: type[object], @@ -288,6 +289,9 @@ def test_object_constructors_hide_spawn_lifecycle_parameters( assert "declared_num_instances" not in parameters assert "spawn_result" not in parameters + assert "positions" not in parameters + assert "pose" not in parameters + assert "on_write" not in parameters def test_runtime_config_attaches_articulation_before_preparing_it() -> None: diff --git a/tests/sim/test_backend_parity.py b/tests/sim/test_backend_parity.py index 68a0a9f443..6724bad7df 100644 --- a/tests/sim/test_backend_parity.py +++ b/tests/sim/test_backend_parity.py @@ -74,6 +74,10 @@ } +# This module exercises capability guards using mock managers only. +pytestmark = pytest.mark.no_sim + + def _make_backend(name: str) -> PhysicsBackend: """Construct a backend with a minimal fake owning-manager back-ref.""" return BACKENDS[name](SimpleNamespace()) @@ -132,6 +136,10 @@ def test_newton_contact_sensor_capability_depends_on_runtime( runtime_device: str, expected: bool, ) -> None: + from dexsim.scene import ContactBuffer + + if solver_type == "dexuni" and not hasattr(ContactBuffer, "impulse_valid"): + expected = False # Published wheels predate DexUni ContactQuery. backend = NewtonPhysicsBackend(SimpleNamespace()) backend._configured_solver_type = solver_type backend._runtime_device = runtime_device @@ -139,6 +147,38 @@ def test_newton_contact_sensor_capability_depends_on_runtime( assert backend.supports_contact_sensor is expected +@pytest.mark.parametrize( + ("solver_path", "use_mujoco_cpu", "expected"), + [ + ("pure_mujoco", False, True), + ("pure_mujoco", True, False), + ("pure_vbd", False, True), + ("coupled", False, True), + ("dexuni_kinematic_soft", False, True), + ], +) +@pytest.mark.usefixtures("requires_dexuni_contact_query") +def test_dexuni_contact_sensor_capability_follows_resolved_path( + monkeypatch: pytest.MonkeyPatch, + solver_path: str, + use_mujoco_cpu: bool, + expected: bool, +) -> None: + world = object() + native = SimpleNamespace( + solver_type="dexuni", + solver=SimpleNamespace(features=SimpleNamespace(backend=solver_path)), + mujoco_solver=SimpleNamespace(use_mujoco_cpu=use_mujoco_cpu), + ) + monkeypatch.setattr( + "dexsim.engine.newton_physics.backend_registry.get_newton_backend", + lambda candidate: native if candidate is world else None, + ) + backend = NewtonPhysicsBackend(SimpleNamespace(_world=world)) + + assert backend.supports_contact_sensor is expected + + def test_contact_sensor_capability_is_rechecked_after_prepare( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -259,3 +299,55 @@ def test_matrix_covers_all_backends() -> None: if __name__ == "__main__": pytest.main([__file__, "-v"]) + + +def test_dexuni_contacts_remain_unavailable_without_quantity_validity(monkeypatch): + from dexsim.scene import ContactBuffer + + monkeypatch.delattr(ContactBuffer, "impulse_valid", raising=False) + backend = NewtonPhysicsBackend(SimpleNamespace()) + backend._configured_solver_type = "dexuni" + backend._runtime_device = "cuda:0" + assert not backend.supports_contact_sensor + + +@pytest.mark.parametrize( + "solver_type,solver_backend,two_way,expected", + [ + ("mujoco_warp", "pure_mujoco", False, False), + ("dexuni", "pure_mujoco", False, False), + ("dexuni", "coupled", True, False), + ("dexuni", "coupled", False, True), + ], +) +def test_cpu_mujoco_contact_capability_uses_resolved_solver( + monkeypatch, + solver_type, + solver_backend, + two_way, + expected, +): + from dexsim.scene import ContactBuffer + from dexsim.engine.newton_physics import backend_registry + + if solver_type == "dexuni" and not hasattr(ContactBuffer, "impulse_valid"): + pytest.skip("DexUni contacts require the per-row quantity contract.") + world = object() + native = SimpleNamespace( + solver_type=solver_type, + mujoco_solver=SimpleNamespace(use_mujoco_cpu=True), + solver=SimpleNamespace( + features=SimpleNamespace( + backend=solver_backend, + two_way_coupling_enabled=two_way, + ) + ), + ) + monkeypatch.setattr( + backend_registry, + "get_newton_backend", + lambda value: native if value is world else None, + ) + backend = NewtonPhysicsBackend(SimpleNamespace(_world=world)) + backend._runtime_device = "cuda:0" + assert backend.supports_contact_sensor is expected diff --git a/tests/sim/test_cfg.py b/tests/sim/test_cfg.py index 971838a70f..55c0736185 100644 --- a/tests/sim/test_cfg.py +++ b/tests/sim/test_cfg.py @@ -996,7 +996,11 @@ def test_legacy_mesh_collision_physics_rejects_non_mesh_shape() -> None: def test_backend_joint_and_articulation_configs_round_trip() -> None: drive = NewtonJointDrivePropertiesCfg(target_mode=None) - root = ArticulationRootPropertiesCfg(fixed_base=False) + root = ArticulationRootPropertiesCfg( + fixed_base=False, + newton_gravity_compensation=0.5, + newton_mimic_stabilizer=True, + ) restored_drive = JointDrivePropertiesCfg.from_dict(drive.to_dict()) restored_root = ArticulationRootPropertiesCfg.from_dict(root.to_dict()) @@ -1008,15 +1012,31 @@ def test_backend_joint_and_articulation_configs_round_trip() -> None: "sleep_threshold": None, "min_position_iters": None, "min_velocity_iters": None, + "newton_gravity_compensation": 0.5, + "newton_mimic_stabilizer": True, } assert type(restored_root) is ArticulationRootPropertiesCfg + assert restored_root == root -def test_articulation_root_config_rejects_backend_discriminator() -> None: - with pytest.raises(TypeError, match="backend"): - ArticulationRootPropertiesCfg.from_dict( - {"backend": "newton", "fixed_base": False} - ) +@pytest.mark.parametrize( + ("name", "value"), + [ + ("backend", "newton"), + ("newton_kinematic", True), + ("newton_sleep_policy", "never"), + ("newton_shape_collision", False), + ("newton_shape_collision_links", ["tool"]), + ("newton_particle_collision_links", ["tool"]), + ("newton_mesh_approximation", {"method": "convex_hull"}), + ("newton_force_sdf", True), + ], +) +def test_articulation_root_config_rejects_unsupported_fields(name, value) -> None: + with pytest.raises(TypeError, match=name): + ArticulationRootPropertiesCfg.from_dict({name: value}) + with pytest.raises(TypeError, match=name): + ArticulationCfg.from_dict({"root_props": {name: value}}) def test_articulation_root_config_round_trip() -> None: @@ -1082,6 +1102,7 @@ def test_newton_physics_inherits_common_gravity_and_collision_config( contact_distance: float, ) -> None: cfg = NewtonPhysicsCfg( + solver_cfg=None, gravity=[0.0, 0.0, -1.5], collision_cfg=NewtonCollisionPipelineCfg( broad_phase="sap", @@ -1135,6 +1156,43 @@ def test_newton_physics_can_disable_the_external_collision_pipeline() -> None: assert not hasattr(cfg, "collision_pipeline_update_interval") +def test_newton_physics_forwards_sync_and_capture_options() -> None: + defaults = NewtonPhysicsCfg().to_dexsim_cfg(gpu_id=0) + assert defaults.async_sync is False + assert defaults.rigid_transform_sync_mode == "all" + assert defaults.capture_acquire_timeout is None + assert defaults.capture_wait_log_interval == 10.0 + + dexsim_cfg = NewtonPhysicsCfg( + async_sync=True, + rigid_transform_sync_mode="dynamic", + capture_acquire_timeout=2.5, + capture_wait_log_interval=None, + ).to_dexsim_cfg(gpu_id=0) + + assert dexsim_cfg.async_sync is True + assert dexsim_cfg.rigid_transform_sync_mode == "dynamic" + assert dexsim_cfg.capture_acquire_timeout == 2.5 + assert dexsim_cfg.capture_wait_log_interval is None + + +@pytest.mark.parametrize( + ("field_name", "value", "message"), + [ + ("rigid_transform_sync_mode", "static", "rigid_transform_sync_mode"), + ("capture_acquire_timeout", -1.0, "capture_acquire_timeout"), + ("capture_acquire_timeout", float("inf"), "capture_acquire_timeout"), + ], +) +def test_newton_physics_rejects_invalid_sync_and_capture_options( + field_name: str, + value: str | float, + message: str, +) -> None: + with pytest.raises(ValueError, match=message): + NewtonPhysicsCfg(**{field_name: value}) + + @pytest.mark.parametrize( ("field_name", "value"), [("broad_phase", "sap"), ("visualizer_enabled", True)], @@ -1547,3 +1605,43 @@ def test_nrd_rejects_invalid_settings(field_name: str, invalid_value: object) -> """Malformed NRD values fail before entering the native renderer.""" with pytest.raises(ValueError, match=field_name): sim_cfg.NRDCfg(**{field_name: invalid_value}) + + +@configclass +class _DexUniRobotPresetCfg(RobotPresetCfg): + default: RobotCfg = RobotCfg(uid="portable", control_parts={"arm": ["joint"]}) + newton_dexuni: RobotCfg | None = RobotCfg( + uid="dexuni", control_parts={"arm": ["joint"]} + ) + newton_mujoco_warp: RobotCfg | None = RobotCfg( + uid="mujoco", control_parts={"arm": ["joint"]} + ) + newton_mjwarp: RobotCfg | None = RobotCfg( + uid="mjwarp", control_parts={"arm": ["joint"]} + ) + + +@pytest.mark.parametrize( + "missing,expected", + [ + ((), "dexuni"), + (("newton_dexuni",), "mujoco"), + (("newton_dexuni", "newton_mujoco_warp"), "mjwarp"), + (("newton_dexuni", "newton_mujoco_warp", "newton_mjwarp"), "portable"), + ], +) +@pytest.mark.parametrize( + "solver_mapping", + [None, {}, {"solver_type": "dexuni"}], +) +def test_dexuni_preset_priority_and_isolation( + missing, expected, solver_mapping +) -> None: + preset = _DexUniRobotPresetCfg() + for field in missing: + setattr(preset, field, None) + physics = NewtonPhysicsCfg(solver_cfg=solver_mapping) + resolved = preset.resolve(physics) + assert resolved.uid == expected + resolved.control_parts["probe"] = ["joint"] + assert "probe" not in preset.resolve(physics).control_parts diff --git a/tests/sim/test_deformable_cfg.py b/tests/sim/test_deformable_cfg.py index e68a4d7bba..70bf78ebee 100644 --- a/tests/sim/test_deformable_cfg.py +++ b/tests/sim/test_deformable_cfg.py @@ -32,6 +32,7 @@ surface_deformable_desc_from_cfg, volume_deformable_desc_from_cfg, ) +from embodichain.lab.sim.shapes import MeshCfg pytestmark = pytest.mark.no_sim @@ -89,6 +90,104 @@ def test_unknown_nested_fields_are_rejected(): ) +@pytest.mark.parametrize( + "mode", ["isothermal", "adiabatic", "target_volume", "prescribed_gauge_pressure"] +) +def test_cloth_pneumatic_round_trip_and_descriptor(mode: str) -> None: + """Keep cavity law and physical parameters through config serialization.""" + gas = { + "mode": mode, + "reference_absolute_pressure": 117325.0, + "ambient_pressure": 101325.0, + "target_volume": 2.4e-4, + "volume_stiffness": 2000.0, + "bulk_damping": 3e6, + "max_absolute_pressure": 5e5, + "heat_capacity_ratio": 1.4, + "min_volume_ratio": 0.05, + "prescribed_gauge_pressure": 16000.0, + } + cfg = SurfaceDeformableObjectCfg.from_dict( + {"uid": "pouch", "shape": {"fpath": "pouch.obj"}, "attrs": {"pneumatic": gas}} + ) + restored = SurfaceDeformableObjectCfg.from_dict(cfg.to_dict()) + descriptor, _ = surface_deformable_desc_from_cfg(restored) + assert restored.attrs.pneumatic == gas + for name, value in gas.items(): + assert getattr(descriptor.physics.pneumatic, name) == value + + +def test_ordinary_cloth_does_not_require_pneumatic_support(monkeypatch) -> None: + """Omitting a cavity remains compatible with wheels lacking its descriptor.""" + import dexsim.spawn + + monkeypatch.delattr(dexsim.spawn, "PneumaticPhysicsDesc") + cfg = SurfaceDeformableObjectCfg(uid="cloth", shape=MeshCfg(fpath="cloth.obj")) + descriptor, _ = surface_deformable_desc_from_cfg(cfg) + assert descriptor.physics.pneumatic is None + + +def test_explicit_cavity_rejects_unsupported_wheel(monkeypatch) -> None: + """Never silently discard requested gas physics on an older SDK.""" + import dexsim.spawn + + monkeypatch.delattr(dexsim.spawn, "PneumaticPhysicsDesc") + cfg = SurfaceDeformableObjectCfg.from_dict( + {"uid": "pouch", "shape": {"fpath": "pouch.obj"}, "attrs": {"pneumatic": {}}} + ) + with pytest.raises(NotImplementedError, match="PneumaticPhysicsDesc"): + surface_deformable_desc_from_cfg(cfg) + + +@pytest.mark.parametrize( + "gas, error", + [ + ({"reference_absolute_pressure": float("nan")}, ValueError), + ({"bulk_damping": -1.0}, ValueError), + ({"mode": "unknown"}, ValueError), + ({"reference_pressure_typo": 1.0}, TypeError), + (True, TypeError), + ], +) +def test_cloth_cavity_rejects_invalid_parameters(gas: object, error: type) -> None: + """Validate gas parameters before committing native scene resources.""" + cfg = SurfaceDeformableObjectCfg.from_dict( + {"uid": "pouch", "shape": {"fpath": "pouch.obj"}, "attrs": {"pneumatic": gas}} + ) + with pytest.raises(error): + surface_deformable_desc_from_cfg(cfg) + + +@pytest.mark.parametrize("angle", [None, 0.0, 0.35, -0.4]) +def test_cloth_rest_angle_round_trip_and_descriptor(angle: float | None) -> None: + """Preserve inferred or authored signed rest angles through config lowering.""" + cfg = SurfaceDeformableObjectCfg.from_dict( + { + "uid": "shirt", + "shape": {"fpath": "shirt.obj"}, + "attrs": {"edge_rest_angle": angle}, + } + ) + restored = SurfaceDeformableObjectCfg.from_dict(cfg.to_dict()) + assert restored.attrs.edge_rest_angle == angle + descriptor, _ = surface_deformable_desc_from_cfg(restored) + assert descriptor.physics.edge_rest_angle == angle + + +@pytest.mark.parametrize("angle", [float("nan"), float("inf"), -float("inf")]) +def test_cloth_rest_angle_rejects_nonfinite_values(angle: float) -> None: + """Reject invalid rest geometry before creating native scene resources.""" + cfg = SurfaceDeformableObjectCfg.from_dict( + { + "uid": "shirt", + "shape": {"fpath": "shirt.obj"}, + "attrs": {"edge_rest_angle": angle}, + } + ) + with pytest.raises(ValueError, match="edge_rest_angle.*finite"): + surface_deformable_desc_from_cfg(cfg) + + @pytest.mark.parametrize("topology", ["volume", "surface"]) def test_surface_coefficients_compile_without_changing_values(topology: str) -> None: cls = ( @@ -179,3 +278,174 @@ def test_volume_elasticity_uses_short_names() -> None: def test_only_current_config_keywords_are_accepted(cfg_type, values) -> None: with pytest.raises(TypeError): cfg_type(**values) + + +@pytest.mark.parametrize("topology", ["volume", "surface"]) +@pytest.mark.usefixtures("requires_particle_contact_material") +def test_particle_contact_material_round_trip_and_lowering(topology) -> None: + values = {"stiffness": 5e4, "damping": 0.05, "dynamic_friction": 0.7} + cls = ( + VolumeDeformableObjectCfg + if topology == "volume" + else SurfaceDeformableObjectCfg + ) + cfg = cls.from_dict( + { + "uid": "particles", + "shape": { + "shape_type": "Mesh", + "fpath": "particles.obj", + }, + "attrs": {"contact_material": values}, + } + ) + restored = cls.from_dict(cfg.to_dict()) + assert restored.attrs.contact_material == values + lower = ( + volume_deformable_desc_from_cfg + if topology == "volume" + else surface_deformable_desc_from_cfg + ) + desc, _ = lower(restored) + assert desc.physics.contact_material.stiffness == values["stiffness"] + assert desc.physics.contact_material.damping == values["damping"] + assert desc.physics.contact_material.dynamic_friction == values["dynamic_friction"] + + +@pytest.mark.parametrize("property_name", ["stiffness", "damping", "dynamic_friction"]) +@pytest.mark.parametrize("value", [-1.0, float("nan"), float("inf"), True]) +@pytest.mark.usefixtures("requires_particle_contact_material") +def test_invalid_particle_contact_material_values_fail_at_lowering( + property_name, value +) -> None: + cfg = SurfaceDeformableObjectCfg.from_dict( + { + "uid": "cloth", + "shape": {"shape_type": "Mesh", "fpath": "cloth.obj"}, + "attrs": {"contact_material": {property_name: value}}, + } + ) + with pytest.raises(ValueError, match=property_name): + surface_deformable_desc_from_cfg(cfg) + + +def test_explicit_particle_material_rejects_unsupported_engine(monkeypatch) -> None: + import dexsim.spawn + + monkeypatch.delattr( + dexsim.spawn, "NewtonParticleContactMaterialDesc", raising=False + ) + cfg = SurfaceDeformableObjectCfg.from_dict( + { + "uid": "cloth", + "shape": {"shape_type": "Mesh", "fpath": "cloth.obj"}, + "attrs": {"contact_material": {"stiffness": 1e3}}, + } + ) + with pytest.raises(RuntimeError, match="per-object particle materials"): + surface_deformable_desc_from_cfg(cfg) + + +def _two_tet_mesh(): + import numpy as np + + vertices = np.array( + [ + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, 0.0, 1.0], + [1.0, 1.0, 1.0], + [0.25, 0.25, 0.25], + ], + dtype=np.float32, + ) + tetrahedra = np.array([[0, 1, 2, 5], [1, 2, 3, 4]], dtype=np.int32) + return vertices, tetrahedra + + +def test_volume_explicit_tetrahedra_keep_particle_order() -> None: + import numpy as np + from dexsim.spawn.soft_body_meshing import extract_tet_surface_triangles + + vertices, tetrahedra = _two_tet_mesh() + cfg = VolumeDeformableObjectCfg.from_dict( + { + "uid": "block", + "shape": {"vertices": vertices, "triangles": None}, + "tetrahedra": tetrahedra.tolist(), + } + ) + desc, _ = volume_deformable_desc_from_cfg(cfg) + + np.testing.assert_array_equal(desc.positions, vertices) + np.testing.assert_array_equal(desc.tetrahedra, tetrahedra) + np.testing.assert_array_equal( + desc.surface_triangles, extract_tet_surface_triangles(tetrahedra) + ) + assert desc.meshing is None + assert desc.meshing_stats.source_kind == "tet_mesh" + assert desc.meshing_stats.particle_count == len(vertices) + surface_ids = np.unique(desc.surface_triangles) + np.testing.assert_array_equal(desc.mesh.vertices, vertices[surface_ids]) + + +def test_volume_explicit_tetrahedra_accept_authored_surface() -> None: + import numpy as np + + vertices, tetrahedra = _two_tet_mesh() + surface = np.array([[0, 2, 1], [1, 2, 3]], dtype=np.int32) + cfg = VolumeDeformableObjectCfg( + uid="block", + shape={"vertices": vertices, "triangles": surface}, + tetrahedra=tetrahedra, + ) + desc, _ = volume_deformable_desc_from_cfg(cfg) + np.testing.assert_array_equal(desc.surface_triangles, surface) + + +@pytest.mark.parametrize( + ("overrides", "error", "message"), + [ + ({"shape": {"fpath": "mesh.obj"}}, ValueError, "array-backed"), + ({"tetrahedra": [[0, 1, 2, 9]]}, ValueError, "outside the vertices"), + ({"tetrahedra": [[0.0, 1.0, 2.0, 3.0]]}, TypeError, "integer"), + ({"tetrahedra": [[0, 1, 2]]}, ValueError, r"\(M, 4\)"), + ({"tetrahedra": [[2**32, 1, 2, 3]]}, ValueError, "fit int32"), + ({"tetrahedra": [[-(2**32), 1, 2, 3]]}, ValueError, "fit int32"), + ], +) +def test_volume_explicit_tetrahedra_reject_invalid_input( + overrides, error, message +) -> None: + vertices, tetrahedra = _two_tet_mesh() + data = { + "uid": "block", + "shape": {"vertices": vertices, "triangles": None}, + "tetrahedra": tetrahedra, + **overrides, + } + with pytest.raises(error, match=message): + volume_deformable_desc_from_cfg(VolumeDeformableObjectCfg.from_dict(data)) + + +@pytest.mark.parametrize("index", [2**32, -(2**32)]) +@pytest.mark.parametrize("field", ["tetrahedra", "triangles"]) +def test_explicit_mesh_indices_are_checked_before_native_integer_narrowing( + index, field +): + import numpy as np + + vertices, tetrahedra = _two_tet_mesh() + cfg = VolumeDeformableObjectCfg( + uid="block", + shape={"vertices": vertices, "triangles": None}, + tetrahedra=tetrahedra, + ) + if field == "tetrahedra": + # Mutating a config must not bypass validation at the native boundary. + cfg.tetrahedra = np.array([[index, 1, 2, 3]], dtype=np.int64) + else: + cfg.shape.triangles = np.array([[index, 1, 2]], dtype=np.int64) + with pytest.raises(ValueError, match="outside the vertices"): + volume_deformable_desc_from_cfg(cfg) diff --git a/tests/sim/test_dexuni_two_way.py b/tests/sim/test_dexuni_two_way.py new file mode 100644 index 0000000000..a48ee66f37 --- /dev/null +++ b/tests/sim/test_dexuni_two_way.py @@ -0,0 +1,200 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Physical acceptance of DexUni contact feedback through EmbodiChain.""" + +from __future__ import annotations + +from pathlib import Path +import subprocess +import sys + +import pytest +import torch + +from embodichain.lab.sim.cfg import ( + ArticulationCfg, + ArticulationRootPropertiesCfg, + MassPropertiesCfg, + NewtonCollisionPropertiesCfg, + NewtonJointDrivePropertiesCfg, + NewtonPhysicsCfg, + NewtonRigidBodyMaterialCfg, + RenderCfg, + RigidBodyPhysicsCfg, + RigidObjectCfg, +) +from embodichain.lab.sim.shapes import CubeCfg +from embodichain.lab.sim.sim_manager import SimulationManager, SimulationManagerCfg + +pytestmark = [ + pytest.mark.subprocess_sim, + pytest.mark.gpu, + pytest.mark.usefixtures("requires_dexuni_two_way"), +] + +FINGER_START = 0.08 +FINGER_HALF = 0.01 +BOX_HALF = 0.025 +TARGET = FINGER_START - FINGER_HALF +CONTACT_TRAVEL = FINGER_START - FINGER_HALF - BOX_HALF + + +def _gripper_urdf(path: Path) -> None: + """Write two light, effort-limited fingers without downloadable assets.""" + fingers = [] + for name, sign in (("left", -1.0), ("right", 1.0)): + fingers.append(f""" + + + + + + + + + """) + path.write_text( + '' + "".join(fingers) + "", + encoding="utf-8", + ) + + +def _grip(path: Path, coupling: str, use_cuda_graph: bool) -> torch.Tensor: + material = RigidBodyPhysicsCfg( + collision_props=NewtonCollisionPropertiesCfg(margin=0.0, gap=0.0), + material_props=NewtonRigidBodyMaterialCfg( + ke=2.0e4, kd=100.0, dynamic_friction=1.0 + ), + ) + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + render_cfg=RenderCfg(renderer="no-render"), + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + physics_dt=1.0 / 60.0, + num_substeps=10, + use_cuda_graph=use_cuda_graph, + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "joint_mode": "dynamic", + "coupling": coupling, + "vbd_options": {"iterations": 10}, + "collision_options": {"broad_phase": "nxn"}, + }, + ), + ) + ) + try: + articulation = sim.add_articulation( + ArticulationCfg( + uid="gripper", + fpath=str(path), + asset_physics_mode="overlay", + build_pk_chain=False, + attrs=material, + root_props=ArticulationRootPropertiesCfg( + newton_gravity_compensation=1.0 + ), + joint_drive_props=NewtonJointDrivePropertiesCfg( + target_mode="position_velocity", + stiffness=1000.0, + damping=20.0, + passive_damping=50.0, + armature=0.3, + max_effort=10.0, + ), + ) + ) + sim.add_rigid_object( + RigidObjectCfg( + uid="floor", + shape=CubeCfg(size=(1.0, 1.0, 0.1)), + body_type="static", + init_pos=(0.0, 0.0, -0.05), + attrs=material, + ) + ) + box_material = material.copy() + box_material.mass_props = MassPropertiesCfg(mass=0.0625) + box = sim.add_rigid_object( + RigidObjectCfg( + uid="box", + shape=CubeCfg(size=(2 * BOX_HALF,) * 3), + init_pos=(0.0, 0.0, BOX_HALF), + attrs=box_material, + ) + ) + sim.prepare() + articulation.set_qpos( + torch.full((1, 2), TARGET, device=sim.device), target=True + ) + sim.update(step=120) + qpos = articulation.get_qpos().detach().cpu() + assert torch.isfinite(qpos).all() + assert torch.isfinite(box.get_local_pose()).all() + if use_cuda_graph: + assert sim.physics.cuda_graph_status == "captured" + + sim.reset_objects_state() + torch.testing.assert_close( + articulation.get_qpos(), torch.zeros((1, 2), device=sim.device) + ) + return qpos + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + + +def _assert_two_way_grip(path: Path, use_cuda_graph: bool) -> None: + """Qualify both coupling modes in one process with its own NoRender engine.""" + _gripper_urdf(path) + one_way = _grip(path, "one_way", use_cuda_graph) + two_way = _grip(path, "two_way", use_cuda_graph) + # Feedback must block overlapping targets at the box's physical surface. + assert torch.all(one_way > TARGET - 0.005), one_way + assert torch.all(two_way < CONTACT_TRAVEL + 0.01), two_way + assert torch.all(two_way > CONTACT_TRAVEL - 0.01), two_way + + +@pytest.mark.parametrize("use_cuda_graph", [False, True]) +def test_dexuni_two_way_contact_blocks_dynamic_finger_targets( + tmp_path: Path, use_cuda_graph: bool +) -> None: + if not torch.cuda.is_available(): + pytest.skip("CUDA is required for the DexUni two-way contract.") + script = ( + "import runpy, sys; from pathlib import Path; " + "module = runpy.run_path(sys.argv[1]); " + "module['_assert_two_way_grip'](Path(sys.argv[2]), sys.argv[3] == '1')" + ) + result = subprocess.run( + [ + sys.executable, + "-c", + script, + str(Path(__file__).resolve()), + str(tmp_path / "gripper.urdf"), + "1" if use_cuda_graph else "0", + ], + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr diff --git a/tests/sim/test_fluid_cfg.py b/tests/sim/test_fluid_cfg.py new file mode 100644 index 0000000000..6a290e096d --- /dev/null +++ b/tests/sim/test_fluid_cfg.py @@ -0,0 +1,569 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Fluid declaration and solver configuration contracts.""" + +from __future__ import annotations + +import numpy as np +import pytest +from embodichain.lab.sim.cfg import ( + FluidObjectCfg, + FluidSpawnCfg, + NewtonPhysicsCfg, +) +from embodichain.compute.geometry.particles import sample_volume_particles + +pytestmark = pytest.mark.no_sim + + +def test_fluid_round_trip_keeps_native_render_options(): + cfg = FluidObjectCfg.from_dict( + { + "uid": "water", + "spawn": {"kind": "box", "size": [0.02] * 3}, + "render": {"mode": "mesh_surface", "mesh_surface": {"iso_density": 0.57}}, + } + ) + restored = FluidObjectCfg.from_dict(cfg.to_dict()) + np.testing.assert_array_equal(cfg.spawn.sample(0.005), restored.spawn.sample(0.005)) + assert restored.render.mesh_surface == {"iso_density": 0.57} + restored.render.mesh_surface["iso_density"] = 0.8 + assert cfg.render.mesh_surface["iso_density"] == 0.57 + + +def test_native_sph_and_yaml_mapping_use_same_solver(): + from dexsim.engine.newton_physics import DexUniSolverCfg, SPHSolverCfg + + if "sph_cfg" not in getattr(DexUniSolverCfg, "__dataclass_fields__", {}): + pytest.skip("This DexSim build does not compose SPH through DexUni.") + + source = SPHSolverCfg(sph_method="dfsph", viscosity=0.0006) + native = NewtonPhysicsCfg(solver_cfg=DexUniSolverCfg(sph_cfg=source)).to_dexsim_cfg( + 0 + ) + mapping = NewtonPhysicsCfg( + solver_cfg={ + "solver_type": "dexuni", + "sph_cfg": {"sph_method": "dfsph", "viscosity": 0.0006}, + } + ).to_dexsim_cfg(0) + assert type(native.solver_cfg) is DexUniSolverCfg + assert type(mapping.solver_cfg) is DexUniSolverCfg + assert native.solver_cfg.sph_cfg == source + assert mapping.solver_cfg.sph_cfg == source + + +@pytest.mark.parametrize("as_mapping", [False, True]) +def test_direct_sph_solver_config_is_rejected(as_mapping): + from dexsim.engine.newton_physics import SPHSolverCfg + + solver_cfg = {"solver_type": "sph"} if as_mapping else SPHSolverCfg() + with pytest.raises(RuntimeError, match="Unsupported Newton solver type 'sph'"): + NewtonPhysicsCfg(solver_cfg=solver_cfg).to_dexsim_cfg(0) + + +@pytest.mark.parametrize("kind", ["box", "sphere", "cylinder"]) +def test_lattice_is_deterministic_and_inside_volume(kind): + points = sample_volume_particles( + kind, 0.01, size=(0.04, 0.04, 0.04), radius=0.02, height=0.04 + ) + np.testing.assert_array_equal( + points, + sample_volume_particles( + kind, 0.01, size=(0.04, 0.04, 0.04), radius=0.02, height=0.04 + ), + ) + assert np.all(np.abs(points) < 0.02) + if kind == "sphere": + assert np.all(np.linalg.norm(points, axis=1) < 0.02) + if kind == "cylinder": + assert np.all(np.linalg.norm(points[:, :2], axis=1) < 0.02) + + +def test_grid_limit_is_checked_before_allocation(): + with pytest.raises(ValueError, match="max_particles"): + sample_volume_particles("box", 1e-9) + + +@pytest.mark.parametrize( + "positions", [[], [[float("nan"), 0, 0]], [[0, 1]], [[[0, 0, 0]]]] +) +def test_invalid_particle_sources_rejected(positions): + with pytest.raises(ValueError): + FluidSpawnCfg(positions=positions).sample(0.001) + + +def test_npz_points_are_loaded_without_pickle(tmp_path): + path = tmp_path / "fill.npz" + np.savez(path, positions=np.array([[1, 2, 3]], dtype=np.float32)) + np.testing.assert_array_equal( + FluidSpawnCfg(file_path=str(path)).sample(0.01), [[1, 2, 3]] + ) + + +def test_fill_mesh_must_be_closed(tmp_path): + import trimesh + + path = tmp_path / "open.obj" + trimesh.Trimesh( + vertices=[[0, 0, 0], [1, 0, 0], [0, 1, 0]], faces=[[0, 1, 2]] + ).export(path) + with pytest.raises(ValueError, match="closed"): + sample_volume_particles("mesh_volume", 0.01, mesh_path=str(path)) + + +@pytest.mark.parametrize("backend,num_envs", [("default", 1), ("newton", 2)]) +def test_manager_rejects_unsupported_fluid_worlds_before_declaration(backend, num_envs): + from types import SimpleNamespace + import torch + from embodichain.lab.sim import SimulationManager + + manager = SimpleNamespace( + physics=SimpleNamespace(name=backend), + device=torch.device("cuda"), + num_envs=num_envs, + ) + with pytest.raises(NotImplementedError): + SimulationManager.add_fluid_object(manager, FluidObjectCfg(uid="water")) + + +@pytest.mark.parametrize( + "solver_cfg", + [None, {"solver_type": "dexuni", "sph_cfg": {"sph_method": "dfsph"}}], +) +def test_fluid_declaration_accepts_dexuni_dispatch(solver_cfg, monkeypatch): + from unittest.mock import Mock + from embodichain.lab.sim import SimulationManager + + manager = _fluid_manager_for_renderer("no-render") + manager._active_newton_solver_type = "dexuni" + manager.physics.supports_dexuni_fluids = True + manager.sim_config.physics_cfg.solver_cfg = solver_cfg + monkeypatch.setattr(SimulationManager, "_get_process_renderer", lambda: None) + monkeypatch.setattr("embodichain.lab.sim.sim_manager.FluidObject", Mock()) + cfg = FluidObjectCfg( + uid="water", spawn=FluidSpawnCfg(positions=[[0, 0, 0]]), render={"mode": "none"} + ) + + SimulationManager.add_fluid_object(manager, cfg) + + manager._spawn_scene.declare.assert_called_once() + + +def test_dexuni_nested_sph_density_conflict_is_rejected(monkeypatch): + from embodichain.lab.sim import SimulationManager + + manager = _fluid_manager_for_renderer("no-render") + manager._active_newton_solver_type = "dexuni" + manager.physics.supports_dexuni_fluids = True + manager.sim_config.physics_cfg.solver_cfg = { + "solver_type": "dexuni", + "sph_cfg": {"sph_method": "dfsph", "rest_density": 1200.0}, + } + monkeypatch.setattr(SimulationManager, "_get_process_renderer", lambda: None) + + with pytest.raises(ValueError, match="rest_density conflicts"): + SimulationManager.add_fluid_object(manager, FluidObjectCfg(uid="water")) + manager._spawn_scene.declare.assert_not_called() + + +@pytest.mark.parametrize("solver,capability", [("sph", True), ("dexuni", False)]) +def test_fluid_requires_a_dexuni_build_with_sph_support(solver, capability): + from embodichain.lab.sim import SimulationManager + + manager = _fluid_manager_for_renderer("no-render") + manager._active_newton_solver_type = solver + manager.physics.supports_dexuni_fluids = capability + with pytest.raises(NotImplementedError, match="DexUni"): + SimulationManager.add_fluid_object(manager, FluidObjectCfg(uid="water")) + manager._spawn_scene.declare.assert_not_called() + + +def test_dexuni_fluid_rejects_existing_deformables(): + from embodichain.lab.sim import SimulationManager + + manager = _fluid_manager_for_renderer("no-render") + manager._deformable_objects = {"cloth": object()} + with pytest.raises(NotImplementedError, match="cannot coexist"): + SimulationManager.add_fluid_object(manager, FluidObjectCfg(uid="water")) + manager._spawn_scene.declare.assert_not_called() + + +def test_dexuni_deformable_rejects_existing_fluid(): + from types import SimpleNamespace + import torch + from embodichain.lab.sim import SimulationManager + + manager = SimpleNamespace( + physics=SimpleNamespace(name="newton", supports_surface_deformables=True), + device=torch.device("cuda"), + _active_newton_solver_type="dexuni", + _fluid_objects={"water": object()}, + ) + with pytest.raises(NotImplementedError, match="cannot coexist"): + SimulationManager.add_deformable_object( + manager, SimpleNamespace(deformable_type="surface") + ) + + +@pytest.mark.parametrize("has_sph_cfg", [False, True]) +def test_dexuni_fluid_capability_tracks_native_config(monkeypatch, has_sph_cfg): + from types import SimpleNamespace + import dexsim.engine.newton_physics as native + from embodichain.lab.sim.physics.newton import NewtonPhysicsBackend + + fields = {"sph_cfg": object()} if has_sph_cfg else {} + fake_cfg = type("FakeDexUniCfg", (), {"__dataclass_fields__": fields}) + monkeypatch.setattr(native, "DexUniSolverCfg", fake_cfg) + backend = NewtonPhysicsBackend(SimpleNamespace()) + backend._configured_solver_type = "dexuni" + assert backend.supports_dexuni_fluids is has_sph_cfg + + +def test_fluid_reset_invalidates_sph_without_resetting_dexuni_bodies(monkeypatch): + from types import SimpleNamespace + from unittest.mock import Mock + + import warp as wp + from dexsim.engine.newton_physics import backend_registry + from embodichain.lab.sim.physics.newton import NewtonPhysicsBackend + + wp.init() + sph = SimpleNamespace(supports_world_reset=True, reset=Mock()) + solid = SimpleNamespace(reset=Mock()) + native = SimpleNamespace( + solver_type="dexuni", + sph_solver=sph, + solver=solid, + invalidate_cuda_graph=Mock(), + model=SimpleNamespace(world_count=2, device="cpu"), + _runtime=SimpleNamespace(current_state=object()), + ) + monkeypatch.setattr(backend_registry, "get_newton_backend", lambda _: native) + backend = NewtonPhysicsBackend(SimpleNamespace(_world=object())) + + backend.invalidate_fluid_state([1]) + + native.invalidate_cuda_graph.assert_called_once() + solid.reset.assert_not_called() + assert sph.reset.call_args.args[0] is native._runtime.current_state + assert sph.reset.call_args.kwargs["world_mask"].numpy().tolist() == [ + False, + True, + False, + ] + assert sph.reset.call_args.kwargs["flags"] == 0 + + +def test_points_source_validates_spacing(): + with pytest.raises(ValueError, match="spacing"): + FluidSpawnCfg(positions=[[0, 0, 0]]).sample(0) + + +def test_closed_mesh_sampling_matches_equivalent_box(tmp_path): + import trimesh + + mesh = tmp_path / "fill.obj" + trimesh.creation.box(extents=[0.04] * 3).export(mesh) + actual = sample_volume_particles("mesh_volume", 0.01, mesh_path=str(mesh)) + expected = sample_volume_particles("box", 0.01, size=(0.04,) * 3) + np.testing.assert_allclose(actual, expected) + + +def test_profile_source_round_trip_rebuilds_without_a_particle_file(): + cfg = FluidObjectCfg.from_dict( + { + "uid": "water", + "spawn": { + "kind": "revolved_volume", + "profile": [[-0.02, 0.01], [0.03, 0.02]], + "fill_level": 0.8, + "clearance": 0.001, + "reference_uid": "cup", + }, + } + ) + restored = FluidObjectCfg.from_dict(cfg.to_dict()) + assert restored.spawn.file_path is None + points = restored.spawn.sample(0.004) + np.testing.assert_array_equal(points, cfg.spawn.sample(0.004)) + assert points.dtype == np.float32 + assert points[:, 2].max() < -0.02 + 0.8 * 0.05 + assert len(restored.spawn.sample(0.002)) > len(points) + + +@pytest.mark.parametrize("extra", [{"profile": [[0, 1], [1, 1]]}, {"fill_level": 0.5}]) +def test_points_reject_geometry_filters_instead_of_silently_ignoring_them(extra): + with pytest.raises(ValueError, match="points sources"): + FluidSpawnCfg(positions=[[0, 0, 0]], **extra).sample(0.001) + + +def _fluid_manager_for_renderer(renderer): + from types import SimpleNamespace + from unittest.mock import Mock + import torch + from embodichain.lab.sim import SimulationManagerCfg + from embodichain.lab.sim.cfg import RenderCfg + + return SimpleNamespace( + physics=SimpleNamespace( + name="newton", configure_world=Mock(), supports_dexuni_fluids=True + ), + device=torch.device("cuda"), + num_envs=1, + _active_newton_solver_type="dexuni", + spawn_result=None, + sim_config=SimulationManagerCfg( + headless=True, + physics_cfg=NewtonPhysicsCfg(), + render_cfg=RenderCfg(renderer=renderer), + ), + has_native_renderer=renderer != "no-render", + asset_uids=[], + _fluid_objects={}, + _rigid_objects={}, + _spawn_scene=SimpleNamespace(declare=Mock()), + create_visual_material=Mock(), + _invalidate_fluid_state=Mock(), + notify_visualization_topology_changed=Mock(), + ) + + +@pytest.mark.parametrize("mode", ["particles", "mesh_surface", "none"]) +@pytest.mark.parametrize( + "requested,process_renderer", + [ + ("hybrid", None), + ("fast-rt", "hybrid"), + ("rt", "hybrid"), + ("auto", "hybrid"), + ("no-render", "hybrid"), + ], +) +def test_manager_rejects_hybrid_fluid_rendering_before_sampling( + requested, process_renderer, mode, monkeypatch +): + from unittest.mock import Mock + from embodichain.lab.sim import SimulationManager + + monkeypatch.setattr( + SimulationManager, "_get_process_renderer", lambda: process_renderer + ) + sample = Mock(side_effect=AssertionError("Fluid must be rejected before sampling")) + monkeypatch.setattr(FluidSpawnCfg, "sample", sample) + manager = _fluid_manager_for_renderer(requested) + cfg = FluidObjectCfg(uid="water", render={"mode": mode}) + with pytest.raises(NotImplementedError, match="fast-rt.*Vulkan.*new process"): + SimulationManager.add_fluid_object(manager, cfg) + sample.assert_not_called() + manager.create_visual_material.assert_not_called() + manager._spawn_scene.declare.assert_not_called() + + +@pytest.mark.parametrize("process_renderer", [None, "hybrid"]) +def test_auto_selected_hybrid_rejects_fluid_before_sampling( + process_renderer, monkeypatch, tmp_path +): + from unittest.mock import Mock + from embodichain.lab.sim import SimulationManager, cfg as sim_cfg + + monkeypatch.setattr(sim_cfg, "DEFAULT_RENDERER", "auto") + monkeypatch.setattr( + SimulationManager, "_get_process_renderer", lambda: process_renderer + ) + monkeypatch.setattr( + "embodichain.lab.sim.utility.render_utils.select_default_renderer", + lambda _: "hybrid", + ) + manager = _fluid_manager_for_renderer("auto") + manager._requires_native_renderer = True + manager._material_cache_dir = tmp_path + SimulationManager._convert_sim_config(manager, manager.sim_config) + assert manager.sim_config.render_cfg.renderer == "hybrid" + sample = Mock(side_effect=AssertionError("Fluid must be rejected before sampling")) + monkeypatch.setattr(FluidSpawnCfg, "sample", sample) + with pytest.raises(NotImplementedError, match="fast-rt.*Vulkan"): + SimulationManager.add_fluid_object(manager, FluidObjectCfg(uid="water")) + sample.assert_not_called() + + +@pytest.mark.parametrize( + "requested,process_renderer,mode", + [ + ("fast-rt", None, "particles"), + ("fast-rt", "fast-rt", "mesh_surface"), + ("rt", "rt", "mesh_surface"), + ("no-render", "no-render", "none"), + ("no-render", "no-render", "particles"), + ], +) +def test_fluid_renderer_gate_allows_supported_backends( + requested, process_renderer, mode, monkeypatch +): + from unittest.mock import Mock + from embodichain.lab.sim import SimulationManager + + monkeypatch.setattr( + SimulationManager, "_get_process_renderer", lambda: process_renderer + ) + facade = Mock() + monkeypatch.setattr( + "embodichain.lab.sim.sim_manager.FluidObject", Mock(return_value=facade) + ) + manager = _fluid_manager_for_renderer(requested) + cfg = FluidObjectCfg( + uid="water", spawn=FluidSpawnCfg(kind="box"), render={"mode": mode} + ) + assert SimulationManager.add_fluid_object(manager, cfg) is facade + assert manager._fluid_objects["water"] is facade + manager._spawn_scene.declare.assert_called_once() + + +@pytest.mark.parametrize("representation", ["native", "mapping", "full_mapping"]) +@pytest.mark.parametrize("density,spacing", [(1000.0, 0.0015), (800.0, 0.003)]) +def test_solver_defaults_allow_fluid_owned_density_and_spacing( + representation, density, spacing, monkeypatch +): + from dataclasses import asdict + from unittest.mock import Mock + from dexsim.engine.newton_physics import DexUniSolverCfg, SPHSolverCfg + from embodichain.lab.sim import SimulationManager + + sph = SPHSolverCfg(sph_method="dfsph") + if representation == "native": + if "sph_cfg" not in getattr(DexUniSolverCfg, "__dataclass_fields__", {}): + pytest.skip("This DexSim build does not compose SPH through DexUni.") + solver = DexUniSolverCfg(sph_cfg=sph) + elif representation == "mapping": + solver = {"solver_type": "dexuni", "sph_cfg": {"sph_method": "dfsph"}} + else: + solver = {"solver_type": "dexuni", "sph_cfg": asdict(sph)} + manager = _fluid_manager_for_renderer("no-render") + manager.sim_config.physics_cfg.solver_cfg = solver + monkeypatch.setattr(SimulationManager, "_get_process_renderer", lambda: None) + monkeypatch.setattr("embodichain.lab.sim.sim_manager.FluidObject", Mock()) + cfg = FluidObjectCfg( + uid="water", + density=density, + particle_spacing=spacing, + spawn=FluidSpawnCfg(positions=[[0.0, 0.0, 0.0]]), + render={"mode": "none"}, + ) + + SimulationManager.add_fluid_object(manager, cfg) + + descriptor = manager._spawn_scene.declare.call_args.args[2] + assert descriptor.density == density + assert descriptor.particle_spacing == spacing + if representation != "mapping": + values = asdict(sph) if representation == "native" else solver["sph_cfg"] + assert values["rest_density"] == SPHSolverCfg().rest_density + assert values["particle_diameter"] == SPHSolverCfg().particle_diameter + + +@pytest.mark.parametrize("representation", ["native", "mapping"]) +@pytest.mark.parametrize("conflict", ["rest_density", "particle_diameter", None]) +def test_solver_nondefault_physical_values_must_match_fluid( + representation, conflict, monkeypatch +): + from dataclasses import asdict + from unittest.mock import Mock + from dexsim.engine.newton_physics import DexUniSolverCfg, SPHSolverCfg + from embodichain.lab.sim import SimulationManager + + density, spacing = 800.0, 0.003 + sph = SPHSolverCfg( + sph_method="dfsph", rest_density=density, particle_diameter=spacing + ) + if conflict is not None: + setattr(sph, conflict, 1200.0 if conflict == "rest_density" else 0.02) + if representation == "mapping": + solver = {"solver_type": "dexuni", "sph_cfg": asdict(sph)} + else: + if "sph_cfg" not in getattr(DexUniSolverCfg, "__dataclass_fields__", {}): + pytest.skip("This DexSim build does not compose SPH through DexUni.") + solver = DexUniSolverCfg(sph_cfg=sph) + manager = _fluid_manager_for_renderer("no-render") + manager.sim_config.physics_cfg.solver_cfg = solver + monkeypatch.setattr(SimulationManager, "_get_process_renderer", lambda: None) + monkeypatch.setattr("embodichain.lab.sim.sim_manager.FluidObject", Mock()) + cfg = FluidObjectCfg( + uid="water", + density=density, + particle_spacing=spacing, + spawn=FluidSpawnCfg(positions=[[0.0, 0.0, 0.0]]), + render={"mode": "none"}, + ) + + if conflict is None: + SimulationManager.add_fluid_object(manager, cfg) + manager._spawn_scene.declare.assert_called_once() + else: + with pytest.raises(ValueError, match=f"Fluid {conflict} conflicts"): + SimulationManager.add_fluid_object(manager, cfg) + manager._spawn_scene.declare.assert_not_called() + + +def test_manager_resolves_source_once_without_rewriting_config(monkeypatch): + from unittest.mock import Mock + from types import SimpleNamespace + from embodichain.lab.sim import SimulationManager + from embodichain.lab.sim.cfg import RigidObjectCfg + from embodichain.lab.sim.spawn.descriptors import _pose_from_cfg + + points = np.array([[0.01, 0.0, 0.0]], dtype=np.float32) + sampled = [] + + def sample(source, spacing): + sampled.append(spacing) + return points.copy() + + monkeypatch.setattr(FluidSpawnCfg, "sample", sample) + monkeypatch.setattr(SimulationManager, "_get_process_renderer", lambda: None) + factory = Mock() + monkeypatch.setattr("embodichain.lab.sim.sim_manager.FluidObject", factory) + manager = _fluid_manager_for_renderer("no-render") + reference = RigidObjectCfg( + uid="cup", init_pos=(0.2, 0.3, 0.4), init_rot=(10.0, 20.0, 30.0) + ) + manager._rigid_objects["cup"] = SimpleNamespace(cfg=reference) + cfg = FluidObjectCfg( + uid="water", + init_pos=(0.01, 0.02, 0.03), + init_rot=(15.0, 25.0, 35.0), + spawn=FluidSpawnCfg(kind="box", reference_uid="cup"), + render={"mode": "none"}, + ) + before = cfg.to_dict() + expected_pose = _pose_from_cfg(reference) @ _pose_from_cfg(cfg) + + fluid = SimulationManager.add_fluid_object(manager, cfg) + + assert sampled == [cfg.particle_spacing] + assert cfg.to_dict() == before + assert len(factory.call_args.args) == 2 + assert isinstance(factory.call_args.args[0], FluidObjectCfg) + assert factory.call_args.args[1] == manager.device + assert factory.call_args.kwargs == {} + fluid._initialize_spawn_source.assert_called_once() + args = fluid._initialize_spawn_source.call_args + np.testing.assert_array_equal(args.args[0], points) + np.testing.assert_allclose(args.args[1], expected_pose[:3, :3]) + assert args.kwargs["on_write"] is manager._invalidate_fluid_state + descriptor = manager._spawn_scene.declare.call_args.args[2] + np.testing.assert_array_equal(descriptor.positions, points) + np.testing.assert_allclose(descriptor.pose, expected_pose) diff --git a/tests/sim/test_fluid_render_sync.py b/tests/sim/test_fluid_render_sync.py new file mode 100644 index 0000000000..849b0c4d86 --- /dev/null +++ b/tests/sim/test_fluid_render_sync.py @@ -0,0 +1,189 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Fluid render clipping preserves physics state and publication ownership.""" + +from __future__ import annotations + +from types import SimpleNamespace +from unittest.mock import Mock + +import pytest +import torch +import warp as wp + +from embodichain.lab.sim.physics.newton import NewtonPhysicsBackend + +pytestmark = pytest.mark.no_sim + + +def _backend() -> NewtonPhysicsBackend: + return NewtonPhysicsBackend(SimpleNamespace(has_native_renderer=True)) + + +@pytest.mark.parametrize( + "ranges", + [((0, 5, 0.0),), ((1, 3, 0.0),), ((0, 2, 0.0), (3, 5, 1.0))], +) +def test_clip_only_hides_selected_particles_below_world_height(ranges): + source = torch.tensor( + [[1, 2, -1], [2, 3, 0], [3, 4, -2], [4, 5, 0.5], [5, 6, 1]], + dtype=torch.float32, + ) + before = source.clone() + expected = source.clone() + for start, end, height in ranges: + for index in range(start, end): + if source[index, 2] < height: + expected[index] = float("nan") + + rendered = _backend()._clip_fluid_render_positions(source, ranges) + + torch.testing.assert_close(rendered, expected, equal_nan=True, rtol=0, atol=0) + torch.testing.assert_close(source, before, rtol=0, atol=0) + assert rendered.data_ptr() != source.data_ptr() + + +def test_repeated_publications_do_not_overwrite_previous_render_input(): + backend = _backend() + source = torch.tensor([[1.0, 2, -1], [3, 4, 1], [5, 6, -2]]) + ranges = ((0, 1, 0.0), (2, 3, -3.0)) + first = backend._clip_fluid_render_positions(source, ranges) + saved = first.clone() + source[:, 2] += 5 + + second = backend._clip_fluid_render_positions(source, ranges) + + torch.testing.assert_close(first, saved, equal_nan=True, rtol=0, atol=0) + torch.testing.assert_close(second, source, rtol=0, atol=0) + assert first.data_ptr() != second.data_ptr() + + +def test_clip_layout_cache_tracks_height_ranges_shape_and_dtype(): + backend = _backend() + source = torch.tensor([[1.0, 2, -1], [3, 4, -2], [5, 6, 1]]) + first_ranges = ((0, 1, 0.0), (2, 3, 2.0)) + backend._clip_fluid_render_positions(source, first_ranges) + cached = backend._fluid_render_clip_z + backend._clip_fluid_render_positions(source + 1, first_ranges) + assert backend._fluid_render_clip_z is cached + + changed = backend._clip_fluid_render_positions(source, ((1, 2, -3.0),)) + torch.testing.assert_close(changed, source, rtol=0, atol=0) + assert backend._fluid_render_clip_z is not cached + + for resized in (source[:2], source.to(torch.float64)): + actual = backend._clip_fluid_render_positions(resized, ((0, 1, -3.0),)) + torch.testing.assert_close(actual, resized, rtol=0, atol=0) + assert actual.dtype == resized.dtype + + +def test_single_fluid_needs_no_per_particle_height_cache(): + backend = _backend() + source = torch.ones(3, 3) + backend._clip_fluid_render_positions(source, ((1, 2, 0.0),)) + + result = backend._clip_fluid_render_positions(source, ((0, 3, 0.0),)) + + torch.testing.assert_close(result, source) + assert backend._fluid_render_clip_z is None + assert backend._fluid_render_clip_key is None + + +def test_native_publication_preserves_body_state_time_and_unclipped_ranges(monkeypatch): + from dexsim.engine.newton_physics import backend_registry + + wp.init() + source = torch.tensor([[1.0, 2, -1], [3, 4, -2], [5, 6, -3], [7, 8, 1]]) + before = source.clone() + published = [] + + def publish(world, body_q, particle_q, *, simulation_time): + published.append((world, body_q, wp.to_torch(particle_q), simulation_time)) + + native = SimpleNamespace( + state_0=SimpleNamespace( + particle_q=wp.from_torch(source, dtype=wp.vec3), body_q=object() + ), + sim_time=4.5, + sync_to_dexsim=Mock(), + render_sync=SimpleNamespace(sync=publish), + ) + monkeypatch.setattr(backend_registry, "get_newton_backend", lambda world: native) + backend = _backend() + backend._manager._fluid_objects = { + "clipped": SimpleNamespace( + cfg=SimpleNamespace(render=SimpleNamespace(clip_below_z=0.0)), + _entities=[ + SimpleNamespace( + _physics_binding=SimpleNamespace(particle_start=start), + particle_count=1, + ) + for start in (0, 3) + ], + ), + "unclipped": SimpleNamespace( + cfg=SimpleNamespace(render=SimpleNamespace(clip_below_z=None)) + ), + } + scene = SimpleNamespace(world=object()) + + backend.sync_render_state(scene) + + world, body_q, particles, sim_time = published[0] + assert world is scene.world + assert body_q is native.state_0.body_q + assert sim_time == native.sim_time + assert torch.isnan(particles[0]).all() + torch.testing.assert_close(particles[1:], source[1:], rtol=0, atol=0) + torch.testing.assert_close(source, before, rtol=0, atol=0) + native.sync_to_dexsim.assert_not_called() + + backend._manager._fluid_objects["clipped"].cfg.render.clip_below_z = None + backend.sync_render_state(scene) + native.sync_to_dexsim.assert_called_once_with(scene.world) + assert backend._fluid_render_clip_z is None + + +def test_teardown_releases_clip_metadata_even_without_a_world(): + backend = _backend() + backend._clip_fluid_render_positions(torch.ones(3, 3), ((1, 2, 0.0),)) + + backend.prepare_for_teardown() + + assert backend._fluid_render_clip_z is None + assert backend._fluid_render_clip_key is None + + +@pytest.mark.gpu +def test_clip_cache_is_safe_across_nondefault_cuda_streams(): + backend = _backend() + source = torch.tensor([[1.0, 2, -1], [3, 4, 1], [5, 6, -2]], device="cuda") + ranges = ((0, 1, 0.0), (2, 3, -3.0)) + streams = [torch.cuda.Stream(), torch.cuda.Stream()] + initial = torch.cuda.current_stream() + results = [] + for index in (0, 1, 0): + stream = streams[index] + stream.wait_stream(initial) + with torch.cuda.stream(stream): + results.append(backend._clip_fluid_render_positions(source, ranges)) + torch.cuda.synchronize() + + expected = source.clone() + expected[0] = float("nan") + for result in results: + torch.testing.assert_close(result, expected, equal_nan=True, rtol=0, atol=0) diff --git a/tests/sim/test_open_drawer_tutorial.py b/tests/sim/test_open_drawer_tutorial.py index 23885e338a..ab2f0f8ffb 100644 --- a/tests/sim/test_open_drawer_tutorial.py +++ b/tests/sim/test_open_drawer_tutorial.py @@ -115,7 +115,7 @@ def add_articulation(self, cfg): assert drawer_cfg.link_attrs is None -def test_tutorial_newton_physics_cfg_uses_shared_mujoco_warp_profile() -> None: +def test_tutorial_newton_physics_cfg_uses_shared_dexuni_profile() -> None: tutorial = _load_tutorial_module() cfg = tutorial._tutorial_physics_cfg("newton") @@ -123,15 +123,16 @@ def test_tutorial_newton_physics_cfg_uses_shared_mujoco_warp_profile() -> None: assert cfg.num_substeps == 20 assert cfg.collision_cfg is None assert cfg.solver_cfg == { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } diff --git a/tests/sim/test_scoop_ice_demo.py b/tests/sim/test_scoop_ice_demo.py index 397180715e..7186ea27b2 100644 --- a/tests/sim/test_scoop_ice_demo.py +++ b/tests/sim/test_scoop_ice_demo.py @@ -112,15 +112,68 @@ def compute_ik(pose, **kwargs): torch.testing.assert_close(plan(moved_bin, tilted_grasp), moved_bin @ baseline) -def test_ice_spawn_configs_are_separated_before_prepare(tmp_path, monkeypatch) -> None: +@pytest.mark.gpu +@pytest.mark.requires_sim +@pytest.mark.slow +@pytest.mark.parametrize("seed", [0, 1, 2]) +def test_default_scoops_and_retains_ice(seed: int) -> None: + """Qualify the physical grasp and retained ice after the final settling step.""" + sim = demo.initialize_simulation( + demo.build_parser().parse_args(["--headless", "--physics", "default"]) + ) + try: + robot = demo.create_robot(sim) + demo.create_container(sim) + padding = demo.create_padding_box(sim) + scoop = demo.create_scoop(sim) + heavy = demo.create_heave_ice(sim) + ice = demo.create_ice_cubes(sim) + sim.prepare() + demo.randomize_ice_positions(sim, ice, seed=seed) + demo.scoop_grasp(sim, robot, scoop, heavy, padding) + demo.scoop_ice(sim, robot, scoop) + + assert scoop.get_local_pose()[0, 2] > 0.30 # Above the 22 cm bin rim. + relative = torch.linalg.inv(scoop.get_local_pose(to_matrix=True))[ + :, None + ] @ ice.get_local_pose(to_matrix=True) + centers = relative[0, :, :3, 3] + # Bowl bounds, including one cube radius above the upper surface. + retained = ( + (centers[:, 0].abs() < 0.055) + & (centers[:, 1] > -0.19) + & (centers[:, 1] < -0.04) + & (centers[:, 2] > -0.025) + & (centers[:, 2] < 0.065) + ) + count = int(retained.sum()) + print(f"seed={seed}: retained {count} ice cubes") + assert count > 0 + finally: + sim.destroy(exit_process=False) + demo.SimulationManager.flush_cleanup_queue() + + +@pytest.mark.parametrize("has_native_renderer", [False, True]) +def test_ice_spawn_configs_are_separated_before_prepare( + tmp_path, monkeypatch, has_native_renderer: bool +) -> None: """Newton evaluates initial contacts before runtime randomization can run.""" (tmp_path / "ice.obj").touch() monkeypatch.setattr(demo, "get_data_path", lambda name: str(tmp_path)) declared = [] + material_calls = [] def add_group(*, cfg): declared.extend(cfg.rigid_objects.values()) - return SimpleNamespace(set_visual_material=lambda **kwargs: None) + return SimpleNamespace( + set_visual_material=lambda **kwargs: material_calls.append("assign") + ) + + def create_visual_material(**kwargs): + assert has_native_renderer + material_calls.append("create") + return object() def prepare(): positions = np.array( @@ -138,11 +191,13 @@ def prepare(): assert separations.min() >= 0.030 sim = SimpleNamespace( + has_native_renderer=has_native_renderer, add_rigid_object_group=add_group, - create_visual_material=lambda **kwargs: object(), + create_visual_material=create_visual_material, prepare=prepare, ) demo.create_ice_cubes(sim) + assert material_calls == (["create", "assign"] if has_native_renderer else []) def test_newton_uses_cg_for_the_large_free_body_pile(monkeypatch) -> None: @@ -154,4 +209,11 @@ def manager(cfg): monkeypatch.setattr(demo, "SimulationManager", manager) demo.initialize_simulation(demo.build_parser().parse_args(["--physics", "newton"])) - assert configs[0].physics_cfg.solver_cfg["solver"] == "cg" + assert configs[0].physics_cfg.solver_cfg["solver_type"] == "dexuni" + assert configs[0].physics_cfg.solver_cfg["mujoco_options"]["solver"] == "cg" + assert ( + configs[0].physics_cfg.solver_cfg["vbd_options"][ + "rigid_body_contact_buffer_size" + ] + == 256 + ) diff --git a/tests/sim/test_sim_manager.py b/tests/sim/test_sim_manager.py index 290bf3a262..070e57cd24 100644 --- a/tests/sim/test_sim_manager.py +++ b/tests/sim/test_sim_manager.py @@ -488,6 +488,7 @@ def _make_runtime_control_sim_manager( sim._robots = {"robot": object()} sim._articulations = {} sim._rigid_objects = {"table": object()} + sim._fluid_objects = {} sim._deformable_objects = { "cloth": SimpleNamespace( cfg=SurfaceDeformableObjectCfg(uid="cloth", particle_flags=[0, 1, 0]) @@ -569,6 +570,7 @@ def test_deferred_destroy_prepares_backend_before_releasing_world( sim._rigid_objects = {} sim._constraints = {} sim._rigid_object_groups = {} + sim._fluid_objects = {} sim._deformable_objects = {} sim._articulations = {} sim._robots = {} @@ -1314,6 +1316,7 @@ def capture(self, **_kwargs: object) -> None: ) sim._rigid_objects = {} sim._rigid_object_groups = {} + sim._fluid_objects = {} sim._deformable_objects = {} sim._articulations = {} sim._robots = {} @@ -1896,6 +1899,68 @@ def test_register_kinematic_nodal_trajectory_rejects_finalized_scene() -> None: builder.add_runtime_control.assert_not_called() +def _runtime_control(**attrs: object): + from dexsim.engine.newton_physics.runtime_control import RuntimeControl + + control = type("RecordingControl", (RuntimeControl,), {})() + for name, value in attrs.items(): + setattr(control, name, value) + return control + + +def test_add_runtime_control_registers_one_scene_control() -> None: + sim, builder = _make_runtime_control_sim_manager() + control = _runtime_control() + + assert sim.add_runtime_control(control) == (control,) + + builder.add_runtime_control.assert_called_once_with(control) + + +def test_add_runtime_control_expands_factory_per_arena() -> None: + sim, builder = _make_runtime_control_sim_manager(num_envs=3) + + controls = sim.add_runtime_control( + lambda arena_name, env_index: _runtime_control( + target=f"{arena_name}/robot", env_index=env_index + ) + ) + + assert [control.target for control in controls] == [ + "arena_0/robot", + "arena_1/robot", + "arena_2/robot", + ] + assert [control.env_index for control in controls] == [0, 1, 2] + assert [ + call.args[0] for call in builder.add_runtime_control.call_args_list + ] == list(controls) + + +def test_add_runtime_control_rejects_non_control_factory_results() -> None: + sim, builder = _make_runtime_control_sim_manager() + + with pytest.raises(TypeError, match="RuntimeControl instances"): + sim.add_runtime_control(lambda arena_name, env_index: object()) + with pytest.raises(TypeError, match="RuntimeControl or a factory"): + sim.add_runtime_control(object()) + + builder.add_runtime_control.assert_not_called() + + +def test_add_runtime_control_rejects_finalized_scene_and_default_backend() -> None: + sim, builder = _make_runtime_control_sim_manager() + builder.is_finalized = True + with pytest.raises(RuntimeError, match=r"before SimulationManager\.prepare"): + sim.add_runtime_control(_runtime_control()) + + sim, builder = _make_runtime_control_sim_manager(backend="default") + with pytest.raises(RuntimeError, match="Newton backend"): + sim.add_runtime_control(_runtime_control()) + + builder.add_runtime_control.assert_not_called() + + def test_add_robot_resolves_backend_preset_before_declaration() -> None: @configclass class TestRobotPresetCfg(RobotPresetCfg): @@ -2535,6 +2600,7 @@ def test_remove_asset_marks_visualization_topology_dirty() -> None: sim.prepare = MagicMock() sim._rigid_objects = {"cube": rigid_object} sim._rigid_object_groups = {} + sim._fluid_objects = {} sim._deformable_objects = {} sim._articulations = {} sim._robots = {} @@ -3010,6 +3076,7 @@ def test_reset_objects_state_includes_deformable_assets() -> None: sim._rigid_object_groups = {} sim._lights = {} sim._sensors = {} + sim._fluid_objects = {} sim._visualization_sim_time = 1.25 sim._deformable_objects = { "soft": MagicMock(), @@ -3023,7 +3090,10 @@ def test_reset_objects_state_includes_deformable_assets() -> None: assert sim.simulation_time == 1.25 -def test_newton_reset_clears_all_articulations_in_selected_worlds_together() -> None: +@pytest.mark.parametrize("solver_type", ["mujoco_warp", "dexuni"]) +def test_newton_reset_clears_all_articulations_in_selected_worlds_together( + solver_type, +) -> None: sim = object.__new__(SimulationManager) robot = MagicMock() articulation = MagicMock() @@ -3034,7 +3104,7 @@ def test_newton_reset_clears_all_articulations_in_selected_worlds_together() -> "robot": ("robot_0", "robot_1"), "arm": ("arm_0", "arm_1"), } - sim.physics = SimpleNamespace(name="newton", solver_type="mujoco_warp") + sim.physics = SimpleNamespace(name="newton", solver_type=solver_type) sim.num_envs = 2 sim._spawn_scene = SimpleNamespace( builder=SimpleNamespace(is_finalized=True, result=spawn_result), @@ -3044,6 +3114,7 @@ def test_newton_reset_clears_all_articulations_in_selected_worlds_together() -> sim._articulations = {"arm": articulation} sim._rigid_objects = {} sim._rigid_object_groups = {} + sim._fluid_objects = {} sim._deformable_objects = {} sim._lights = {} sim._sensors = {} @@ -3056,15 +3127,19 @@ def test_newton_reset_clears_all_articulations_in_selected_worlds_together() -> native_batch.clear_dynamics.assert_called_once_with() -def test_newton_reset_rejects_excluding_one_articulation_from_selected_worlds() -> None: +@pytest.mark.parametrize("solver_type", ["mujoco_warp", "dexuni"]) +def test_newton_reset_rejects_excluding_one_articulation_from_selected_worlds( + solver_type, +) -> None: sim = object.__new__(SimulationManager) robot = MagicMock() articulation = MagicMock() - sim.physics = SimpleNamespace(name="newton", solver_type="mujoco_warp") + sim.physics = SimpleNamespace(name="newton", solver_type=solver_type) sim._robots = {"robot": robot} sim._articulations = {"arm": articulation} sim._rigid_objects = {} sim._rigid_object_groups = {} + sim._fluid_objects = {} sim._deformable_objects = {} sim._lights = {} sim._sensors = {} @@ -3093,6 +3168,7 @@ def test_newton_non_mujoco_reset_allows_excluding_an_articulation() -> None: sim._articulations = {"arm": articulation} sim._rigid_objects = {} sim._rigid_object_groups = {} + sim._fluid_objects = {} sim._deformable_objects = {} sim._lights = {} sim._sensors = {} @@ -3328,3 +3404,62 @@ def test_process_no_render_is_detected_after_world_cleanup(monkeypatch) -> None: lambda: SimpleNamespace(renderer=dexsim.types.Renderer.NORENDER), ) assert SimulationManager._get_process_renderer() == "no-render" + + +def test_reset_fluid_selects_rows_and_honors_exclusions(): + sim = object.__new__(SimulationManager) + sim.physics = SimpleNamespace(name="default") + sim.num_envs = 2 + for name in ( + "_robots", + "_articulations", + "_rigid_objects", + "_rigid_object_groups", + "_lights", + "_sensors", + "_deformable_objects", + ): + setattr(sim, name, {}) + fluid, excluded = MagicMock(), MagicMock() + sim._fluid_objects = {"water": fluid, "other": excluded} + sim.reset_objects_state(env_ids=[1], excluded_uids=["other"]) + excluded.reset.assert_not_called() + fluid.reset.assert_called_once_with([1]) + + +@pytest.mark.usefixtures("requires_particle_contact_material") +def test_targeted_particle_material_schedule_expands_arena_paths() -> None: + from dexsim.engine import newton_physics + from dexsim.engine.newton_physics.runtime_control import GraphRuntimeControl + + if not hasattr(newton_physics, "DexUniParticleContactMaterialSchedule"): + pytest.skip("Targeted particle schedules require a supporting DexSim build.") + sim, builder = _make_runtime_control_sim_manager() + sim.register_particle_contact_material_schedule( + {"dynamic_friction": ((0.0, 0.5), (1.0, 1.2))}, uid="cloth" + ) + controls = [call.args[0] for call in builder.add_runtime_control.call_args_list] + assert [control.target for control in controls] == [ + "arena_0/cloth", + "arena_1/cloth", + ] + assert all(isinstance(control, GraphRuntimeControl) for control in controls) + + +def test_targeted_particle_material_schedule_rejects_unknown_uid() -> None: + sim, builder = _make_runtime_control_sim_manager() + with pytest.raises(KeyError, match="missing"): + sim.register_particle_contact_material_schedule( + {"stiffness": ((0.0, 1000.0),)}, uid="missing" + ) + builder.add_runtime_control.assert_not_called() + + +def test_targeted_particle_material_schedule_validates_before_registration() -> None: + sim, builder = _make_runtime_control_sim_manager() + sim._spawn_scene.arena_names = ("arena_0",) + with pytest.raises(RuntimeError, match="arena count"): + sim.register_particle_contact_material_schedule( + {"stiffness": ((0.0, 1000.0),)}, uid="cloth" + ) + builder.add_runtime_control.assert_not_called() diff --git a/tests/sim/test_startup_summary.py b/tests/sim/test_startup_summary.py index 3ef7d592d6..da760f00e6 100644 --- a/tests/sim/test_startup_summary.py +++ b/tests/sim/test_startup_summary.py @@ -41,13 +41,13 @@ def _sim(physics_cfg=None): "newton" if isinstance(cfg.physics_cfg, NewtonPhysicsCfg) else "default" ), solver_type=( - "mujoco_warp" if isinstance(cfg.physics_cfg, NewtonPhysicsCfg) else None + "dexuni" if isinstance(cfg.physics_cfg, NewtonPhysicsCfg) else None ), cuda_graph_status="pending", sync_render_state=lambda result: None, ), _requested_renderer="auto", - _requested_solver="auto", + _requested_solver="dexuni", is_window_opened=False, _robots={}, _articulations={}, @@ -100,18 +100,16 @@ def test_cpu_physics_does_not_imply_rendering_disabled(monkeypatch): assert "\x1b" not in text and "\x00" not in text -def test_newton_reports_pending_then_resolved_solver_and_graph(monkeypatch): +def test_newton_reports_dexuni_and_graph_readiness(monkeypatch): summary = importlib.import_module("embodichain.lab.sim._startup_summary") monkeypatch.setattr(summary.torch.cuda, "is_available", lambda: False) sim = _sim(NewtonPhysicsCfg()) - sim.physics.solver_type = "auto" before = str(summary.simulation_rows(sim)) assert "PENDING" in before sim.spawn_result = SimpleNamespace(topology_revision=1, needs_rebuild=False) sim._ready_spawn_topology_revision = 1 - sim.physics.solver_type = "mujoco_warp" after = str(summary.scene_rows(sim)) - assert "auto -> mujoco_warp" in after + assert "dexuni" in after assert "PENDING" in after and "CAPTURED" not in after sim.physics.cuda_graph_status = "captured" assert "CAPTURED" in str(summary.scene_rows(sim)) diff --git a/tests/visualization/test_scene_exporter.py b/tests/visualization/test_scene_exporter.py index 9e3d09fb75..36513129c8 100644 --- a/tests/visualization/test_scene_exporter.py +++ b/tests/visualization/test_scene_exporter.py @@ -730,6 +730,27 @@ def test_rigid_groups_and_deformable_meshes_are_exported() -> None: ) +def test_fluid_point_overlay_is_detached_and_display_filtered(): + import torch + from types import SimpleNamespace + from embodichain.lab.sim.cfg import FluidObjectCfg, FluidRenderCfg + + sim = _EmptySimulation() + original = torch.tensor([[[0.0, 0, -1], [0.0, 0, 1]]]) + sim._fluid_objects = { + "water": SimpleNamespace( + is_declared=False, + cfg=FluidObjectCfg(render=FluidRenderCfg(clip_below_z=0.0)), + data=SimpleNamespace(particle_pos_w=original), + ) + } + exporter = SceneExporter(sim, VisualizationCfg(backend="viser", env_ids=[0])) + overlays = exporter._prepare_overlays(None) + assert len(overlays.point_clouds) == 1 + np.testing.assert_array_equal(overlays.point_clouds[0].points, [[0, 0, 1]]) + torch.testing.assert_close(original, torch.tensor([[[0.0, 0, -1], [0.0, 0, 1]]])) + + def test_visual_objects_export_pose_and_live_visibility_without_topology_refresh(): """Render-only assets publish their own node kind and visibility state.""" asset = _RigidObject()