From abc02b736e78cf14d05accb7b332d73e8eb0dcaf Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Mon, 3 Nov 2025 13:21:59 -0800 Subject: [PATCH] Minor fixes to viewer diagnostics - When `mjData.nefc` was 0 (solver not run at all), we were plotting the stale diagnostics from the last run; now fixed. - In the case where islands were used, the semantic of the plotted diagnostic `sqrt_nnz` (square root of Hessian nonzeros) is changed. It used to be the sum of square roots of solver nonzeros per island, it is now the square root of the sum of all nonzeros in all islands. The semantic `sqrt(sum(nnz_i))` was found to be more consistent than `sum(sqrt(nnz_i))`, in the sense that switching islanding on/off leads to smaller changes and is more in line with the interpretation of islanding as [block-diagonalization](https://mujoco.readthedocs.io/en/3.3.7/computation/#constraint-islands). PiperOrigin-RevId: 827624908 Change-Id: I5fc86364ead585a4752a6d44823fb75cf6c7c318 --- simulate/simulate.cc | 5 +++-- src/experimental/studio/app.cc | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/simulate/simulate.cc b/simulate/simulate.cc index d6a5f80e..a266c57a 100644 --- a/simulate/simulate.cc +++ b/simulate/simulate.cc @@ -299,7 +299,7 @@ void UpdateProfiler(mj::Simulate* sim, const mjModel* m, const mjData* d) { memset(sim->figcost.linepnt, 0, mjMAXLINE*sizeof(int)); // number of islands that have diagnostics - int nisland = mjMAX(1, mjMIN(d->nisland, mjNISLAND)); + int nisland = d->nefc ? mjMAX(1, mjMIN(d->nisland, mjNISLAND)) : 0; // iterate over islands for (int k=0; k < nisland; k++) { @@ -403,9 +403,10 @@ void UpdateProfiler(mj::Simulate* sim, const mjModel* m, const mjData* d) { mjtNum sqrt_nnz = 0; int solver_niter = 0; for (int island=0; island < nisland; island++) { - sqrt_nnz += mju_sqrt(d->solver_nnz[island]); + sqrt_nnz += d->solver_nnz[island]; solver_niter += d->solver_niter[island]; } + sqrt_nnz = mju_sqrt(sqrt_nnz); // get sizes: nv, nbody, nefc, sqrt(nnz), ncont, iter float sdata[6] = { diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index a214a092..5b3ff75a 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -634,14 +634,15 @@ void App::UpdateProfilerData() { cpu_other_.erase(cpu_other_.begin()); cpu_other_.push_back(other); - // Dimensions. + // Solver diagnostics. mjtNum sqrt_nnz = 0; int solver_niter = 0; - const int nisland = mjMAX(1, mjMIN(Data()->nisland, mjNISLAND)); - for (int island = 0; island < nisland; island++) { - sqrt_nnz += mju_sqrt(Data()->solver_nnz[island]); + const int nisland = Data()->nefc ? mjMAX(1, mjMIN(Data()->nisland, mjNISLAND)) : 0; + for (int island=0; island < nisland; island++) { + sqrt_nnz += Data()->solver_nnz[island]; solver_niter += Data()->solver_niter[island]; } + sqrt_nnz = mju_sqrt(sqrt_nnz); dim_dof_.erase(dim_dof_.begin()); dim_dof_.push_back(Model()->nv);