From b5df2c10bff3c1b62d4f5eee15477793f7f756aa Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Mon, 6 Jan 2025 06:57:19 -0800 Subject: [PATCH] Remove `mjData.C_diag`, no longer required as C is now lower triangular. PiperOrigin-RevId: 712514204 Change-Id: I8293f65ebc09c6a8fed76b0e59d2e2a667e01a43 --- doc/includes/references.h | 1 - include/mujoco/mjdata.h | 1 - include/mujoco/mjxmacro.h | 1 - introspect/structs.py | 8 -------- mjx/mujoco/mjx/_src/io.py | 1 - mjx/mujoco/mjx/_src/types.py | 2 -- src/engine/engine_io.c | 22 ++++++++++++---------- src/engine/engine_print.c | 2 +- unity/Runtime/Bindings/MjBindings.cs | 1 - 9 files changed, 13 insertions(+), 26 deletions(-) diff --git a/doc/includes/references.h b/doc/includes/references.h index 685d8e3d..8cd73e5a 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -313,7 +313,6 @@ struct mjData_ { int* B_colind; // body-dof: column indices of non-zeros (nB x 1) int* C_rownnz; // reduced dof-dof: non-zeros in each row (nv x 1) int* C_rowadr; // reduced dof-dof: address of each row in C_colind (nv x 1) - int* C_diag; // reduced dof-dof: index of diagonal element (nv x 1) int* C_colind; // reduced dof-dof: column indices of non-zeros (nC x 1) int* mapM2C; // index mapping from M to C (nC x 1) int* D_rownnz; // dof-dof: non-zeros in each row (nv x 1) diff --git a/include/mujoco/mjdata.h b/include/mujoco/mjdata.h index c29af364..b51b19ae 100644 --- a/include/mujoco/mjdata.h +++ b/include/mujoco/mjdata.h @@ -341,7 +341,6 @@ struct mjData_ { int* B_colind; // body-dof: column indices of non-zeros (nB x 1) int* C_rownnz; // reduced dof-dof: non-zeros in each row (nv x 1) int* C_rowadr; // reduced dof-dof: address of each row in C_colind (nv x 1) - int* C_diag; // reduced dof-dof: index of diagonal element (nv x 1) int* C_colind; // reduced dof-dof: column indices of non-zeros (nC x 1) int* mapM2C; // index mapping from M to C (nC x 1) int* D_rownnz; // dof-dof: non-zeros in each row (nv x 1) diff --git a/include/mujoco/mjxmacro.h b/include/mujoco/mjxmacro.h index 9edd1fed..2d78fc02 100644 --- a/include/mujoco/mjxmacro.h +++ b/include/mujoco/mjxmacro.h @@ -654,7 +654,6 @@ X ( int, B_colind, nB, 1 ) \ X ( int, C_rownnz, nv, 1 ) \ X ( int, C_rowadr, nv, 1 ) \ - X ( int, C_diag, nv, 1 ) \ X ( int, C_colind, nC, 1 ) \ X ( int, mapM2C, nC, 1 ) \ X ( int, D_rownnz, nv, 1 ) \ diff --git a/introspect/structs.py b/introspect/structs.py index f02158d9..be60293c 100644 --- a/introspect/structs.py +++ b/introspect/structs.py @@ -5388,14 +5388,6 @@ STRUCTS: Mapping[str, StructDecl] = dict([ ), doc='reduced dof-dof: address of each row in C_colind (nv x 1)', # pylint: disable=line-too-long ), - StructFieldDecl( - name='C_diag', - type=PointerType( - inner_type=ValueType(name='int'), - ), - doc='reduced dof-dof: index of diagonal element', - array_extent=('nv',), - ), StructFieldDecl( name='C_colind', type=PointerType( diff --git a/mjx/mujoco/mjx/_src/io.py b/mjx/mujoco/mjx/_src/io.py index ec00ee87..55b6c08c 100644 --- a/mjx/mujoco/mjx/_src/io.py +++ b/mjx/mujoco/mjx/_src/io.py @@ -332,7 +332,6 @@ def make_data( 'B_colind': (m.nB, jp.int32), 'C_rownnz': (m.nv, jp.int32), 'C_rowadr': (m.nv, jp.int32), - 'C_diag': (m.nv, jp.int32), 'C_colind': (m.nC, jp.int32), 'mapM2C': (m.nC, jp.int32), 'D_rownnz': (m.nv, jp.int32), diff --git a/mjx/mujoco/mjx/_src/types.py b/mjx/mujoco/mjx/_src/types.py index 8d6a62d9..16fa3a81 100644 --- a/mjx/mujoco/mjx/_src/types.py +++ b/mjx/mujoco/mjx/_src/types.py @@ -1296,7 +1296,6 @@ class Data(PyTreeNode): B_colind: body-dof: column indices of non-zeros (nB,) C_rownnz: reduced dof-dof: non-zeros in each row (nv,) C_rowadr: reduced dof-dof: address of each row in C_colind (nv,) - C_diag: reduced dof-dof: index of diagonal element (nv,) C_colind: reduced dof-dof: column indices of non-zeros (nC,) mapM2C: index mapping from M to C (nC,) D_rownnz: dof-dof: non-zeros in each row (nv,) @@ -1426,7 +1425,6 @@ class Data(PyTreeNode): B_colind: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name C_rownnz: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name C_rowadr: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name - C_diag: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name C_colind: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name mapM2C: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name D_rownnz: jax.Array = _restricted_to('mujoco') # pylint:disable=invalid-name diff --git a/src/engine/engine_io.c b/src/engine/engine_io.c index 1cbfa804..e32a9add 100644 --- a/src/engine/engine_io.c +++ b/src/engine/engine_io.c @@ -994,16 +994,18 @@ static void makeDofDofSparse(const mjModel* m, mjData* d, } // find diagonal indices - for (int i = 0; i < nv; i++) { - int adr = rowadr[i]; - int j = 0; - while (colind[adr + j] < i && j < rownnz[i]) { - j++; + if (diag) { + for (int i = 0; i < nv; i++) { + int adr = rowadr[i]; + int j = 0; + while (colind[adr + j] < i && j < rownnz[i]) { + j++; + } + if (colind[adr + j] != i) { + mjERROR("diagonal index not found"); + } + diag[i] = j; } - if (colind[adr + j] != i) { - mjERROR("diagonal index not found"); - } - diag[i] = j; } mj_freeStack(d); @@ -1963,7 +1965,7 @@ static void _resetData(const mjModel* m, mjData* d, unsigned char debug_value) { checkDBSparse(m, d); // make C - makeDofDofSparse(m, d, d->C_rownnz, d->C_rowadr, d->C_diag, d->C_colind, /*reduced=*/1); + makeDofDofSparse(m, d, d->C_rownnz, d->C_rowadr, NULL, d->C_colind, /*reduced=*/1); makeDofDofmap(m, d); } diff --git a/src/engine/engine_print.c b/src/engine/engine_print.c index 6cba6352..9f6b6e33 100644 --- a/src/engine/engine_print.c +++ b/src/engine/engine_print.c @@ -1135,7 +1135,7 @@ void mj_printFormattedData(const mjModel* m, mjData* d, const char* filename, fprintf(fp, "\n\n"); // C sparse structure - mj_printSparsity("C: reduced dof-dof matrix", m->nv, m->nv, d->C_rowadr, d->C_diag, d->C_rownnz, + mj_printSparsity("C: reduced dof-dof matrix", m->nv, m->nv, d->C_rowadr, NULL, d->C_rownnz, NULL, d->C_colind, fp); fprintf(fp, NAME_FORMAT, "C_rownnz"); diff --git a/unity/Runtime/Bindings/MjBindings.cs b/unity/Runtime/Bindings/MjBindings.cs index 92fbd9e9..7ad3d1b6 100644 --- a/unity/Runtime/Bindings/MjBindings.cs +++ b/unity/Runtime/Bindings/MjBindings.cs @@ -4939,7 +4939,6 @@ public unsafe struct mjData_ { public int* B_colind; public int* C_rownnz; public int* C_rowadr; - public int* C_diag; public int* C_colind; public int* mapM2C; public int* D_rownnz;