Implicit flex elasticity in the CG constraint solver via an effective metric

This CL replaces the post-hoc implicit flex correction (`flexInterp_cgsolve`) with a **linearly-implicit effective metric** `M̃ = M + (h² + h·damping)·K` carried by the CG constraint solver itself. Contact/friction forces and implicit flex elasticity are now computed against one consistent metric, instead of the solver seeing `M` and a post-solve correction changing `qacc` behind its back.

Gate (unchanged semantics): `solver="CG"` + implicit/implicitfast integrator + pyramidal cones + flex stiffness present. Newton and PGS are untouched. `solver="CG"` remains the user-facing contract — the factorization is an implementation detail of the preconditioner.

### What's in the metric

- **mjData `efm_*`** (arena, efc-like lifetime/skip semantics; built in `mj_fwdPosition`, value-refreshed in `mj_fwdVelocity`): the per-step stiffness CSR `efm_B_*`, its reverse-Cholesky factor `efm_dofid` + `efm_L_*` (nested-dissection ordered, separators-first for the reverse factorization), and the smooth-force shift `efm_c = h·K·qvel`.
- **`mjd_flexStiff_assemble`** now assembles stretch (Gauss–Newton), standard dim-2 bending, and — via the cached corotated stiffness `d->flexelem_krot` — interp stiffness (all node bodies on simple sliders: point Jacobian is I₃, `flex_centered` not required; fixed nodes drop like pins) into one dof-level CSR. `mjd_effMulAdd`/`mjd_effSolve` apply the metric, with matrix-free operator fallbacks where assembly does not apply.
- **mjModel `efm0_*`** (`nefm0dof`/`nefm0L`): the constant part of the metric factor — currently the dim-2 bending factor, computed once in `mj_setConst` — so bending-only models pay zero per-step factorization cost. Naming mirrors mjData's `efm_*` with the standard `0`-suffix (reference/constant) idiom, and is deliberately not bending-specific: future constant contributors extend it without renames.
- The solver consumes the metric through pre-shifted `qfrc_smooth` and the metric products `Ma`/`Mv`/`Mgrad`; `qacc_smooth` becomes the unconstrained minimizer of the implicit dynamics, which makes the no-constraint shortcut and the warmstart choice consistent by construction.
- **`mj_inverse` adds `B·qacc − c`**, making inverse dynamics discrete-consistent with the gated forward dynamics — exact, since the gated path has no qDeriv term (new test `ForwardTest.GatedFlexInverseConsistency`).

### Performance

All numbers: ms/step over the same 2000-step window, models as shipped on each side (old code with the old model settings vs this CL with the new ones).

The new solver path activates on exactly two shipped models — the ponchos, the only flex models that need an implicit integrator (poncho on Euler degenerates to >200 ms/step). For them, this CL trades speed for consistency: the implicit bending solve now runs inside every solver iteration, where the contact solve can see the stiffness, instead of once after the solve. Solver iterations drop because the curvature is visible, but each iteration pays for the implicit solve:

| model | before | after | solver iters/step |
|---|---|---|---|
| poncho | 2.47 | 3.30 (1.33×) | 16.8 → 11.8 |
| poncho_edgeequality | 1.96 | 2.72 (1.39×) | 13.2 → 10.0 |

What that price buys: contact forces consistent with the implicit elasticity (previously the post-hoc correction changed `qacc` after the constraint solve), discrete-consistent inverse dynamics, and the removal of the post-hoc special case from the integration path. Raising poncho's timestep from 2 to 5 ms leaves its per-step cost nearly flat, so the consistency price can be recovered by taking fewer steps where accuracy allows.

Every other flex model was measured stable on Euler at its shipped timestep and switches to it (these models predate the post-hoc integrator; implicit was never load-bearing for them). They end up equal or faster than before: bunny_multicell 0.47 → 0.40, trampoline 0.28 → 0.25, plate 1.02 → 0.99, pancake 0.34 → 0.33.

Finally, the per-step factorization makes configurations practical that the old code could only integrate explicitly: implicit stretch elasticity (`elastic2d="stretch"`/`"both"`, dim-3 solids) and factorized interp stiffness. No before/after exists for these — stock has no implicit treatment of stretch at all.

### Behavior changes

- With the post-hoc correction deleted, interp/bending models running `solver="Newton"` (or elliptic cones, or islands) now integrate flex elasticity **explicitly** (previously: post-hoc implicit). Affects e.g. `gripper_trilinear` (stable, and faster, but different semantics). Follow-up options: Newton-side metric support, or a documented fallback.
- With the gate on, `mj_forward` outputs are timestep-dependent for gated models (they answer the linearly-implicit discrete problem); `qacc_smooth` and `mj_inverse` change accordingly. Non-gated models are bit-identical (full suite green throughout).

### Validation

- 1737/1737 tests, including new: `FlexStretchDerivatives` (FD-validated GN operator), `FlexStiffAssemble`/`FlexStiffAssembleInterp` (CSR ≡ operators), `GatedFlexInverseConsistency` (fails pre-change), equivalence tests vs the old post-hoc treatment (bending matches to 2e-11).
- Fingerprint discipline throughout: bending-only models bit-exact across every refactor; permutation/kernel changes verified iteration-identical.

### Known follow-ups (not in this CL)

3×3-block sparse Cholesky kernel (the numeric factorization is index-bound; projected ~3× on the factor); mjModel persistence of the factor's symbolic pattern (rest-pose ND makes sizes compile-time); the general effective-metric mode (all solvers, all PSD-safe force classes, behind an enable flag).

PiperOrigin-RevId: 948561856
Change-Id: I8b8e32ebd0428042af71647d0470d10773bf6daf
This commit is contained in:
Alessio Quaglino
2026-07-15 14:56:57 -07:00
committed by Copybara-Service
parent f0fa3d8260
commit ea230a950c
39 changed files with 2501 additions and 203 deletions
+22
View File
@@ -4583,12 +4583,24 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
.property("efc_state", &MjData::efc_state)
.property("efc_type", &MjData::efc_type)
.property("efc_vel", &MjData::efc_vel)
.property("efm_K_colind", &MjData::efm_K_colind)
.property("efm_K_rowadr", &MjData::efm_K_rowadr)
.property("efm_K_rownnz", &MjData::efm_K_rownnz)
.property("efm_K_val", &MjData::efm_K_val)
.property("efm_L", &MjData::efm_L)
.property("efm_L_colind", &MjData::efm_L_colind)
.property("efm_L_rowadr", &MjData::efm_L_rowadr)
.property("efm_L_rownnz", &MjData::efm_L_rownnz)
.property("efm_active", &MjData::efm_active, &MjData::set_efm_active, reference())
.property("efm_c", &MjData::efm_c)
.property("efm_dofid", &MjData::efm_dofid)
.property("energy", &MjData::energy)
.property("eq_active", &MjData::eq_active)
.property("flexedge_J", &MjData::flexedge_J)
.property("flexedge_length", &MjData::flexedge_length)
.property("flexedge_velocity", &MjData::flexedge_velocity)
.property("flexelem_aabb", &MjData::flexelem_aabb)
.property("flexelem_krot", &MjData::flexelem_krot)
.property("flexvert_J", &MjData::flexvert_J)
.property("flexvert_length", &MjData::flexvert_length)
.property("flexvert_xpos", &MjData::flexvert_xpos)
@@ -4645,6 +4657,9 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
.property("ncon", &MjData::ncon, &MjData::set_ncon, reference())
.property("ne", &MjData::ne, &MjData::set_ne, reference())
.property("nefc", &MjData::nefc, &MjData::set_nefc, reference())
.property("nefmK", &MjData::nefmK, &MjData::set_nefmK, reference())
.property("nefmL", &MjData::nefmL, &MjData::set_nefmL, reference())
.property("nefmdof", &MjData::nefmdof, &MjData::set_nefmdof, reference())
.property("nf", &MjData::nf, &MjData::set_nf, reference())
.property("nidof", &MjData::nidof, &MjData::set_nidof, reference())
.property("nisland", &MjData::nisland, &MjData::set_nisland, reference())
@@ -4875,6 +4890,11 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
.property("dof_solimp", &MjModel::dof_solimp)
.property("dof_solref", &MjModel::dof_solref)
.property("dof_treeid", &MjModel::dof_treeid)
.property("efm0_L", &MjModel::efm0_L)
.property("efm0_L_colind", &MjModel::efm0_L_colind)
.property("efm0_L_rowadr", &MjModel::efm0_L_rowadr)
.property("efm0_L_rownnz", &MjModel::efm0_L_rownnz)
.property("efm0_dofid", &MjModel::efm0_dofid)
.property("eq_active0", &MjModel::eq_active0)
.property("eq_data", &MjModel::eq_data)
.property("eq_obj1id", &MjModel::eq_obj1id)
@@ -5129,6 +5149,8 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
.property("nbvhstatic", &MjModel::nbvhstatic, &MjModel::set_nbvhstatic, reference())
.property("ncam", &MjModel::ncam, &MjModel::set_ncam, reference())
.property("nconmax", &MjModel::nconmax, &MjModel::set_nconmax, reference())
.property("nefm0L", &MjModel::nefm0L, &MjModel::set_nefm0L, reference())
.property("nefm0dof", &MjModel::nefm0dof, &MjModel::set_nefm0dof, reference())
.property("nemax", &MjModel::nemax, &MjModel::set_nemax, reference())
.property("neq", &MjModel::neq, &MjModel::set_neq, reference())
.property("nexclude", &MjModel::nexclude, &MjModel::set_nexclude, reference())
+84
View File
@@ -3879,6 +3879,18 @@ struct MjModel {
void set_nflexbending(int value) {
ptr_->nflexbending = static_cast<mjtSize>(value);
}
int nefm0dof() const {
return static_cast<int>(ptr_->nefm0dof);
}
void set_nefm0dof(int value) {
ptr_->nefm0dof = static_cast<mjtSize>(value);
}
int nefm0L() const {
return static_cast<int>(ptr_->nefm0L);
}
void set_nefm0L(int value) {
ptr_->nefm0L = static_cast<mjtSize>(value);
}
int nflexelemedge() const {
return static_cast<int>(ptr_->nflexelemedge);
}
@@ -4914,6 +4926,21 @@ struct MjModel {
emscripten::val flex_bending() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nflexbending, ptr_->flex_bending));
}
emscripten::val efm0_dofid() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefm0dof, ptr_->efm0_dofid));
}
emscripten::val efm0_L_rownnz() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefm0dof, ptr_->efm0_L_rownnz));
}
emscripten::val efm0_L_rowadr() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefm0dof, ptr_->efm0_L_rowadr));
}
emscripten::val efm0_L_colind() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefm0L, ptr_->efm0_L_colind));
}
emscripten::val efm0_L() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefm0L, ptr_->efm0_L));
}
emscripten::val flex_damping() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nflex, ptr_->flex_damping));
}
@@ -6656,6 +6683,30 @@ struct MjData {
void set_nJ(int value) {
ptr_->nJ = value;
}
int efm_active() const {
return ptr_->efm_active;
}
void set_efm_active(int value) {
ptr_->efm_active = value;
}
int nefmK() const {
return ptr_->nefmK;
}
void set_nefmK(int value) {
ptr_->nefmK = value;
}
int nefmdof() const {
return ptr_->nefmdof;
}
void set_nefmdof(int value) {
ptr_->nefmdof = value;
}
int nefmL() const {
return ptr_->nefmL;
}
void set_nefmL(int value) {
ptr_->nefmL = value;
}
int nY() const {
return ptr_->nY;
}
@@ -6860,6 +6911,9 @@ struct MjData {
emscripten::val flexelem_aabb() const {
return emscripten::val(emscripten::typed_memory_view(model->nflexelem * 6, ptr_->flexelem_aabb));
}
emscripten::val flexelem_krot() const {
return emscripten::val(emscripten::typed_memory_view(model->nflexstiffness, ptr_->flexelem_krot));
}
emscripten::val flexedge_J() const {
return emscripten::val(emscripten::typed_memory_view(model->nJfe, ptr_->flexedge_J));
}
@@ -7167,6 +7221,36 @@ struct MjData {
emscripten::val efc_aref() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefc, ptr_->efc_aref));
}
emscripten::val efm_c() const {
return emscripten::val(emscripten::typed_memory_view(model->nv, ptr_->efm_c));
}
emscripten::val efm_K_rownnz() const {
return emscripten::val(emscripten::typed_memory_view(model->nv, ptr_->efm_K_rownnz));
}
emscripten::val efm_K_rowadr() const {
return emscripten::val(emscripten::typed_memory_view(model->nv, ptr_->efm_K_rowadr));
}
emscripten::val efm_K_colind() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmK, ptr_->efm_K_colind));
}
emscripten::val efm_K_val() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmK, ptr_->efm_K_val));
}
emscripten::val efm_dofid() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmdof, ptr_->efm_dofid));
}
emscripten::val efm_L_rownnz() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmdof, ptr_->efm_L_rownnz));
}
emscripten::val efm_L_rowadr() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmdof, ptr_->efm_L_rowadr));
}
emscripten::val efm_L_colind() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmL, ptr_->efm_L_colind));
}
emscripten::val efm_L() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefmL, ptr_->efm_L));
}
emscripten::val efc_b() const {
return emscripten::val(emscripten::typed_memory_view(ptr_->nefc, ptr_->efc_b));
}
+7
View File
@@ -339,6 +339,13 @@ MJDATA_SIZES: tuple[str, ...] = (
"efc_state",
"efc_type",
"efc_vel",
"efm_K_colind",
"efm_K_val",
"efm_dofid",
"efm_L",
"efm_L_colind",
"efm_L_rowadr",
"efm_L_rownnz",
"iLDiagInv",
"iM_rowadr",
"iM_rownnz",