[FEATURE] Add public API to RigidEntity for kinematic and potential energy.#2613
[FEATURE] Add public API to RigidEntity for kinematic and potential energy.#2613Lidang-Jiang wants to merge 5 commits intoGenesis-Embodied-AI:mainfrom
Conversation
Add get_kinetic_energy(), get_potential_energy(), and get_total_energy() methods to RigidEntity for computing mechanical energy in simulation. - Kinetic energy uses generalized-coordinate mass matrix (handles all inertial coupling terms for articulated bodies) - Potential energy sums m_i * g^T * p_i over all links - All methods support envs_idx for parallel environments Closes Genesis-Embodied-AI#2309 Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
duburcqa
left a comment
There was a problem hiding this comment.
This is invalid. It does not take into account motor armature.
- Add RigidSolver.recompute_mass_matrix() to get clean mass matrix (structural inertia + armature, no implicit damping) - get_kinetic_energy() now recomputes mass matrix before use - Delete test_energy.py; add 2 function-based tests to test_rigid_physics.py - Tests: no class, no precision requirement, marked @pytest.mark.required Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
|
Thanks for the review! Addressed all feedback. Motor armature fix: The root issue was that Fix: Added Test changes:
|
- Add unit [J] to all energy method titles - Remove redundant return descriptions - Remove invalid Note about mid-step calls - Add Note about mass matrix recomputation performance - Use 120-char linewidth for docstrings - Remove implementation detail about implicit damping Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
- Merge test_energy_conservation_free_fall and test_energy_kinetic_potential_relation into one test - Add 2 spheres: undamped (dampratio=0) and damped (default) - Verify analytical KE/PE during free fall (semi-implicit Euler formulas) - Verify energy conservation for undamped sphere, strict decrease for damped - Track first ground impact timestep Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
tests/test_rigid_physics.py
Outdated
| @pytest.mark.required | ||
| def test_energy_analytical_and_conservation(show_viewer, tol): | ||
| g = 9.81 | ||
| dt = 0.01 | ||
| h0 = 1.0 | ||
| radius = 0.1 | ||
| n_steps = 100 | ||
| undamped_sol_params = [0.0, 0.0, 0.9, 0.95, 0.001, 0.5, 2.0] | ||
|
|
||
| scene = gs.Scene( | ||
| sim_options=gs.options.SimOptions(dt=dt, gravity=(0, 0, -g)), |
There was a problem hiding this comment.
Parameterize this test with both Euler and approximate_implicitfast integrators.
There was a problem hiding this comment.
Done in 47129f5. Parameterized with both Euler and approximate_implicitfast. Also changed timeconst from 0 to 0.02 in undamped sol_params — the previous timeconst=0, dampratio=0 combination caused NaN constraint forces on the field backend CI.
There was a problem hiding this comment.
Add a FIXME comment in the code to document this issue.
| ke_a.append(sphere_a.get_kinetic_energy().item()) | ||
| pe_a.append(sphere_a.get_potential_energy().item()) | ||
| ke_b.append(sphere_b.get_kinetic_energy().item()) | ||
| pe_b.append(sphere_b.get_potential_energy().item()) | ||
| z_a.append(sphere_a.get_pos()[..., 2].item()) | ||
| z_b.append(sphere_b.get_pos()[..., 2].item()) |
There was a problem hiding this comment.
Remove .item(), it is useless.
| # First impact timestep (sphere center reaches ~radius above ground) | ||
| impact_step = next((i for i, z in enumerate(z_a) if z <= radius + dt), n_steps) |
There was a problem hiding this comment.
Use real collision detection for this, ie
impact_step = -1
for i in range(n_steps):
[...]
if impact_step < 0 and scene.rigid_solver.collider._collider_state.n_contacts.to_numpy().any():
impact_step = i
assert impact_step > 0
It is relying on private API but it is fine in this context.
- Wrap docstrings to 120 chars linewidth - Skip mass matrix recomputation for non-approximate_implicitfast integrators - Remove recompute_mass_matrix() from RigidSolver, call kernel directly in RigidEntity - Parameterize test with both Euler and approximate_implicitfast integrators - One line per option in test entity creation - Use non-zero timeconst in undamped sol_params to avoid NaN on field backend Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
Summary
get_kinetic_energy(),get_potential_energy(), andget_total_energy()methods toRigidEntityKE = 0.5 * dq^T * M(q) * dq) for accurate computation on articulated bodiesm_i * g^T * p_iover all links, supporting arbitrary gravity directionsenvs_idxfor parallel environmentsCloses #2309
API Demo Output
Full output
Unit Test Results
pytest output (13 tests, all passed)
Test Plan