Commit Graph

1019 Commits

Author SHA1 Message Date
Kyle Bayes 6cb6e5a93f Enable multiccd by default.
PiperOrigin-RevId: 902752921
Change-Id: I8e2085ff17db0ac0db1641b8837415c458e5eca4
2026-04-20 12:00:10 -07:00
Alessio Quaglino 508e581ba9 Optimize flex by pinning nodes in empty cells.
This change introduces an optimization for flexcomp objects defined by a mesh. It identifies grid cells that do not contain any mesh vertices and marks them as empty. Nodes that are exclusively part of empty cells are pinned, preventing them from moving. Stiffness computations are skipped for empty cells, reducing computational cost. The total mass is now distributed only among the non-pinned nodes.

PiperOrigin-RevId: 902565735
Change-Id: Id0a9a685536d5e18a3e42124a25ab08ff3a918f2
2026-04-20 04:41:17 -07:00
Yuval Tassa e6d77650f7 Refactor mju_combineSparse to eliminate temporary buffers.
Combine sparse vectors in-place by first counting total `nnz` and then working backwards from the end. This removes the need for temporary buffers in `mju_combineSparse` and its callers and speeds up the function by ~10%.

PiperOrigin-RevId: 902530210
Change-Id: I4f48c327103552ab968d3915399c6067367bec9f
2026-04-20 03:10:18 -07:00
Alessio Quaglino 3230cf99f9 Change flex constraints to eigenmodes of the stiffness matrix.
This provides a reduction from 26 to 18 constraints for trilinear and from 162 to 75 for quadratic. The assembly of the constraints becomes trivial. In total the speedup for a trilinear 3x3x3 grid is about 3x.

PiperOrigin-RevId: 902502398
Change-Id: I764772c7adef78da5a644f64701f842d36e4b543
2026-04-20 02:02:44 -07:00
Yuval Tassa a04c2b1b4a Consolidate stack allocation calls in primal solvers down from 30 to 2 (dense) or 6 (sparse). Preparation for atomic allocation calls in threaded mode.
PiperOrigin-RevId: 902496039
Change-Id: I39df011951713505c4743c7475d74856ddbdf4f9
2026-04-20 01:47:30 -07:00
Alessio Quaglino 3d45a33190 Refactor flex strain constraints to be per-cell.
Each mjEQ_FLEXSTRAIN equality now represents a single cell within a flex. This allows for more efficient sparse Jacobian computation by only considering the degrees of freedom of the nodes within each specific cell. This change gives a speedup of about 10x on a 3x3x3 model.

PiperOrigin-RevId: 902164069
Change-Id: I78eedf1d5cf39b8989fe9863c22d164922fc0efb
2026-04-19 07:21:18 -07:00
Alessio Quaglino b16383dfaf Use banded solver for implicit flex integration.
The flex interpolation stiffness matrix within the implicit/implicitfast solvers is now built and factorized in a banded format instead of a dense one. This involves:
-   Calculating the bandwidth based on the sparsity of the mass/damping matrix and the connectivity within flex cells.
-   Allocating and populating a banded matrix `H`.
-   Using `mju_cholFactorBand` and `mju_cholSolveBand` for factorization and solving.
This change improves performance for flexes with many DOFs but local coupling.

PiperOrigin-RevId: 901297952
Change-Id: I3efe06353d1903ea65ab30dc49685cede228bb68
2026-04-17 08:00:07 -07:00
Alessio Quaglino 6c7ed66781 Implement multi-cell finite element method for interpolated flexes.
This change introduces a `flex_cellcount` field to `mjModel` to specify the number of cells in each dimension for interpolated flexes. The stiffness computation, passive force calculation, and Jacobian derivatives are updated to operate on a per-cell basis, significantly improving performance by localizing computations to the nodes within each cell.

PiperOrigin-RevId: 901216393
Change-Id: Ic23132e609de11e71bb7fef8d1f139daad2ec264
2026-04-17 04:13:19 -07:00
Kyle Bayes 56e98cc16c Improve AABB bounds in makeAAMM.
MODEL           BEFORE(ns)   AFTER(ns)
boxmesh.xml     874645       744822
box.xml         808226       786226
ellipsoid.xml   1477609      1436058
mixed.xml       1492063      1404848

PiperOrigin-RevId: 900626282
Change-Id: I3df5a6cd8a4cb2e3b9ded1b40a5310a4c14ec624
2026-04-16 03:25:18 -07:00
Kyle Bayes d9b3faf8f4 Remove thread_local EPA data in favor of using mjData stack.
PiperOrigin-RevId: 900201369
Change-Id: I82fbe9bf0ef9ea117c2124353d876dd42a57fb3a
2026-04-15 09:19:57 -07:00
Yuval Tassa a2d0e33c0f 2-3x speedup of sparse matrix squaring.
Split symbolic and numeric phases for sparse `M'*diag*M` computation. Microseconds per call for the monolithic vs the split approach for the 100_humanoids and 2humanoid100 models:

```
+-------+------+----------+------------+---------+
| Model | Arch | Col (µs) | Split (µs) | Speedup |
+-------+------+----------+------------+---------+
| 2H100 | x86  | 238.3    | 74.5       | 3.2x    |
+-------+------+----------+------------+---------+
|       | ARM  | 111.6    | 53.2       | 2.1x    |
+-------+------+----------+------------+---------+
| 100H  | x86  | 1325.3   | 656.2      | 2.0x    |
+-------+------+----------+------------+---------+
|       | ARM  | 594.8    | 306.6      | 1.9x    |
+-------+------+----------+------------+---------+
```

PiperOrigin-RevId: 900154308
Change-Id: Ia6e9b8e196e2ed37b723a0faf60e9731303a9619
2026-04-15 07:19:41 -07:00
Yuval Tassa 8f3ed662eb Hoist solver stack allocations out of iteration loop
PiperOrigin-RevId: 900085248
Change-Id: I2bde8fafc8a4801a92c39aa9a13af0ea7475843d
2026-04-15 04:23:08 -07:00
Michael Moss 4a98cc8bff Update MuJoCo version to 3.7.1 following the 3.7.0 release
PiperOrigin-RevId: 899835826
Change-Id: Ibe247b921e7dad7184acef69ee3199fec79c6e24
2026-04-14 16:49:03 -07:00
Alessio Quaglino 5724158593 Refactor flex stiffness storage to support variable sizes.
This change introduces `nflexstiffness` and `flex_stiffnessadr` to allow flex stiffness matrices to have sizes other than the fixed 21 per element. Higher-order flexes can now store larger stiffness matrices based on their number of nodes. The `flex_stiffnessadr` array provides the starting index for each flex's stiffness data within the `flex_stiffness` array.

PiperOrigin-RevId: 899632263
Change-Id: Ie49182c46c3777acf0c6b492345edfcf62fb5e44
2026-04-14 09:39:00 -07:00
Yuval Tassa 412cee2059 Add Jdot correction for connect and weld constaints
Measured reduction in constraint violations before/after this change:

| Model | Correction ON (Avg Viol) | Correction OFF (Avg Viol) | Reduction |
| :--- | :--- | :--- | :--- |
| `jdotv_connect_2d.xml` | 3.959e-4 | 1.699e-3 | **76.7%** |
| `jdotv_connect_3d.xml` | 1.399e-3 | 5.493e-3 | **74.5%** |
| `jdotv_weld_3d.xml` | 9.472e-3 | 1.148e-2 | **17.5%** |

PiperOrigin-RevId: 899137525
Change-Id: Ic3e33764ebd64239bab916289c23d80c3da0b51b
2026-04-13 12:48:03 -07:00
Yuval Tassa 0c337799bd Implement midpoint integrator for free bodies.
PiperOrigin-RevId: 899043541
Change-Id: I0bb38f6ad94e189b45ab16777a04ad6fefc6adf7
2026-04-13 10:58:49 -07:00
Yuval Tassa c004d144d1 Correct DC motor derivative calculation and enforce actearly.
PiperOrigin-RevId: 898983657
Change-Id: I008529eacf3e400696d26bd3a52a4cf4c1c12225
2026-04-13 10:56:58 -07:00
Yuval Tassa f114ea8038 Optimize mj_tendonBias by computing Jdot * qvel directly
PiperOrigin-RevId: 897628283
Change-Id: Iee26a95d6aaa379730b89be14746308742111e2d
2026-04-10 04:57:55 -07:00
Yuval Tassa 025ba59fab Implement sparse Jacobian time derivative
PiperOrigin-RevId: 897609541
Change-Id: Ic86b6026cfc369c584d3edfe12a9a2fd14cc4357
2026-04-10 04:08:06 -07:00
Yuval Tassa 33fd2fe40e Improve mju_addToSclSparseInc with two-pointer loop.
Profiling `leaves.xml` with contacts disabled, this function takes up most of the time (in the implicit integrator). Total `testspeed` run time:

BEFORE: 18.5s
AFTER: 16.5
PiperOrigin-RevId: 896525902
Change-Id: I57f4a984d178956a2902212deb857bf53f53f01b
2026-04-08 08:49:15 -07:00
Yuval Tassa 0e04436d51 Remove `mjWARN_VGEOMFULL, handle visual geom buffer full warnings in mjvScene`.
PiperOrigin-RevId: 896483023
Change-Id: I6dde2c20d8e8e229cf95e1f002dd10525d3376e6
2026-04-08 07:17:53 -07:00
Yuval Tassa 70a7647ad9 Add <dcmotor> actuator and related docs and tests.
PiperOrigin-RevId: 892927987
Change-Id: I38ed6412801341ba03ddf5fe7b93a6081df24d37
2026-04-01 07:50:23 -07:00
Sam Haves f5d3ce3451 Introduce mjpEncoder plugin architecture
Add a new mjpEncoder plugin type mirroring the existing mjpDecoder pattern.
Encoders serialize an mjSpec + mjModel to an mjResource for a given format.

New API functions:
- mjp_registerEncoder: globally register an encoder
- mjp_defaultEncoder: zero-initialize an encoder struct
- mjp_findEncoder: look up an encoder by filename extension or content type

The mjfEncode callback takes (mjSpec*, mjModel*, mjVFS*, mjResource*) and
returns 0 on success. Writing to mjResource keeps symmetry with the decoder
reading from mjResource and leaves the door open for writable resource providers.

PiperOrigin-RevId: 889187898
Change-Id: I180771b2255b91dea188ac5e2cdc3a8f0fb85364
2026-03-25 05:35:39 -07:00
Alessio Quaglino 2d33b50243 Parallelize tactile sensor computation.
The computation of tactile sensor values for each taxel is now parallelized using the MuJoCo threadpool when the number of taxels exceeds a threshold. Each thread processes a batch of taxels, accumulating forces from all colliding geoms.

PiperOrigin-RevId: 888606457
Change-Id: I98abf3e98a7318fc14080054bea7093ab46e8b8b
2026-03-24 05:21:08 -07:00
Kyle Bayes 5c0de1f84d Remove model and data from ccd object. Each geom gets a copy of pos and mat, which is editable without modifying mjData.
PiperOrigin-RevId: 888028695
Change-Id: Ic9bbea4bcd7c2dbe62326798eab73d14da0aaa13
2026-03-23 04:51:53 -07:00
Yuval Tassa 510d75f4cf Allow actuators to add damping and armature to joint and tendon transmissions.
PiperOrigin-RevId: 886899849
Change-Id: I02200ee0d4f7c96096d8188b95f823b566562a30
2026-03-20 11:41:15 -07:00
Yuval Tassa c348bc9f4c Disable clang-format for MuJoCo public headers and engine source.
PiperOrigin-RevId: 886720463
Change-Id: I7c89fa99bee0598dcf48807bfbd61ee8bf8fa9b8
2026-03-20 05:06:15 -07:00
Kyle Bayes eec6e43a73 Bit compress face vertices in EPA to reduce memory needed. The parameter ccd_iteration is only supported to 1000, so we only need 10 bits for vertex index.
PiperOrigin-RevId: 885690198
Change-Id: I4c6af170ddc86316d3e9cfaf7ca7e0e4fac8a416
2026-03-18 11:10:14 -07:00
Yuval Tassa 9d3ee5a948 Improve ASAN poisoning logic in mj_freeStack.
When freeing a stack frame, only poison the newly-freed memory region
`[old_top, new_top)` instead of the entire region `[limit, top)`. The old
code redundantly re-poisoned already-poisoned memory on every
`mj_freeStack` call.

Benchmarking `engine_forward_test` under ASAN:
- Before: 99.3s
- After:  75.6s  (~24% faster)

PiperOrigin-RevId: 885035750
Change-Id: Ib195661ca337d13c5ff6094bcc107c4c0a617b9b
2026-03-17 08:26:39 -07:00
Yuval Tassa efae9157a7 Polynomial stiffness and damping https://youtu.be/aKa3ZlEF9_Y
PiperOrigin-RevId: 884607673
Change-Id: If8088dbf37fed1055304778a7eb84dec52cba920
2026-03-16 13:25:25 -07:00
Alessio Quaglino aec1b45dce Remove unnecessary const from grad parameter in strain Jacobian functions.
The `grad` parameter in `volumetric_dSdx` and `invariant_dSdx` is not modified, but the `const` qualifier was inconsistent with how the array was being passed, requiring an unnecessary cast. Removing the `const` simplifies the function signatures and calls.

PiperOrigin-RevId: 884598322
Change-Id: Icac8071aa8e838057dd39c979c23a4f47e1dc22d
2026-03-16 13:04:00 -07:00
Alessio Quaglino ac2730ab80 Change flex strain constraints for trilinear to a more stable formulation.
This change improves trilinear flex elements by using reduced integration for volumetric quantities (strain trace and volume ratio) at the element center, while adding full integration for shear components at 8 Gauss points and removing the second strain invariant from the constraints, which is negligible for small strains. This reduced integration "B-bar" technique is standard in finite element analysis and prevents artificial stiffness that can occur when low-order elements are nearly incompressible. The constraint count per trilinear element changes from 24 to 26 compared to using invariants. For quadratic elements, the full 27 quadrature point are used resulting in 162 constraints.

PiperOrigin-RevId: 884570752
Change-Id: Ib74ece8f4712c2c81fbfd784524fcac52a2c80e5
2026-03-16 12:05:16 -07:00
Yuval Tassa 1f2587fe3e Make preprocessor checks for ADDRESS_SANITIZER compatible with GCC
PiperOrigin-RevId: 884468086
Change-Id: I5742f023689fd8cc1747df1af5f0a9687a0e77ac
2026-03-16 09:03:52 -07:00
Taylor Howell bb38a34869 Skip shared dofs for contact constraints
PiperOrigin-RevId: 884455239
Change-Id: I7ee36a13c28be88e12380b4790f150ad62f268c1
2026-03-16 08:36:58 -07:00
Yuval Tassa eca5758bf9 Define ADDRESS_SANITIZER portably using standard compiler macros
Use `__SANITIZE_ADDRESS__` (GCC) and `__has_feature(address_sanitizer)` (Clang)
to define ADDRESS_SANITIZER when the toolchain doesn't provide it natively.

Fixes #3160

PiperOrigin-RevId: 884447485
Change-Id: I48ef51bca8f5c5f15f62089e73cd8fa649723636
2026-03-16 08:18:11 -07:00
Yuval Tassa c651a0daeb Merge friction and limit constraint counting into instantiation functions.
PiperOrigin-RevId: 884379952
Change-Id: I23605f3e5ada0ed0fbdcf3e1903f54788700e2d3
2026-03-16 05:23:20 -07:00
Yuval Tassa a47a18dd28 Rollback of 1cda1e7a8c, fixes #3163
Note this change re-opens issue #2472

PiperOrigin-RevId: 884378131
Change-Id: Ifefc345f1a296e5245cbb10047bb678ffea3598f
2026-03-16 05:17:43 -07:00
Yuval Tassa 55f5ac0a1d Remove mj_jacDifPairCount by incorporating the counting into mj_jacDifPair
PiperOrigin-RevId: 884366070
Change-Id: I7490ca205a315bc661414e32c91ad1ec753a8fe7
2026-03-16 04:44:59 -07:00
Kyle Bayes 88ba8ffdbc Remove const qualifier on mjData in mjfCollision type as mjData is non-const in mjc_SDF via mjpPlugin.
PiperOrigin-RevId: 883225786
Change-Id: I5fecaa1d575b340a6510212db7b3e7db48f361cc
2026-03-13 10:21:53 -07:00
Michael Moss 6d70e66810 Update MuJoCo version from 3.6.0 to 3.7.0 following the 3.6.0 release
PiperOrigin-RevId: 881995135
Change-Id: I0c1a3b4a22c4c7cfada580b49a2cacde64cc84f3
2026-03-11 07:09:24 -07:00
Kyle Bayes acf7f030a6 Remove const qualifier from mjData in mj_geomDistance. The function mj_geomDistance will require data stack space as nativeccd memory is determined from opt.ccd_iterations.
PiperOrigin-RevId: 881946592
Change-Id: I524b9fa41018a581e58647d44de6ca3db4c6608b
2026-03-11 04:55:44 -07:00
Alessio Quaglino f1b305632c Allow mesh with octrees in mjc_distance.
PiperOrigin-RevId: 881355677
Change-Id: I8da9f7c92d6e76f16a5d82e7d16488211a8a65b7
2026-03-10 04:41:34 -07:00
Michael Moss 955d6c0b02 Update MuJoCo version from 3.5.1 to 3.6.0 prior to next release.
The next release will include breaking changes, so this bumps the major
version to reflect that.

PiperOrigin-RevId: 881351265
Change-Id: I970c68a0211651c07fcb8109ebd0b138196cf50a
2026-03-10 04:30:07 -07:00
Alessio Quaglino 713b5524bc Introduces a new "strain" equality mode (flex_edgeequality = 3) for flexcomps with trilinear interpolation. This mode enforces constraints on the three invariants of the Green-Lagrange strain tensor at 8 Gauss quadrature points within each flex element, resulting in 24 equality constraints per flexcomp.
Also fixes a bug when attaching a flex with constraints.

PiperOrigin-RevId: 881349765
Change-Id: I8e6df8239488bfdc87d7e5b473568975e38c737e
2026-03-10 04:26:07 -07:00
Copybara-Service 3a99f6110e Merge pull request #3084 from Ashutosh0x:fix/mingw-stat-localtime
PiperOrigin-RevId: 880906196
Change-Id: I98027e80e9882620cb184766c14110478178ce53
2026-03-09 09:54:10 -07:00
Yuval Tassa 1e1eb48261 No-op refactor of flex interpolation factorization and solve in mj_implicitSkip.
PiperOrigin-RevId: 880867833
Change-Id: I2ab8ce5a027742af07ef0256bc4cdee62289282e
2026-03-09 08:20:24 -07:00
Kyle Bayes 330c36c99b Fix issue in GJK where the tolerance for x_norm was set too low.
PiperOrigin-RevId: 880735671
Change-Id: I324e37e8677d3c7feeb4b8bcf065a2a0122326b6
2026-03-09 02:14:52 -07:00
Alessio Quaglino 8dc5cc4c70 Skip tangential components in tactile sensor if mesh has only normals.
PiperOrigin-RevId: 879036130
Change-Id: Ic4184dd535700c5099768dabf5009ba96b218542
2026-03-05 06:35:41 -08:00
Copybara-Service 870f9fea24 Merge pull request #3150 from kevinzakka:fix/principal-point-frustum-sign
PiperOrigin-RevId: 878610460
Change-Id: I82e1c30c9c263a9981c619d6a401b417f95c39a2
2026-03-04 12:22:39 -08:00
Kevin Zakka 77ce80bc63 Fix swapped vertical frustum bounds for cameras with principal point offset.
The frustum_top and frustum_bottom assignments in mjv_updateCamera used
the wrong elements of the zver array, causing the principal point cy
offset to be applied with an inverted sign. This shifted the rendered
image vertically by 2*cy pixels.
2026-03-04 01:03:55 -08:00