Extend test comparing dense and sparse pipelines to all solvers.

PiperOrigin-RevId: 712862658
Change-Id: Ieb8515d32a225a098ab96055598a5d56bad7e476
This commit is contained in:
Yuval Tassa
2025-01-07 04:36:13 -08:00
committed by Copybara-Service
parent 674d227050
commit 1c69e64e6e
+22 -16
View File
@@ -31,32 +31,38 @@ static const char* const kDefaultModel = "testdata/model.xml";
using ::testing::Pointwise;
using ::testing::DoubleNear;
using ::testing::NotNull;
using PipelineTest = MujocoTest;
// Joint and actuator damping should integrate identically under implicit
// sparse and dense pipelines should produce the same results, for all solvers
TEST_F(PipelineTest, SparseDenseEquivalent) {
const std::string xml_path = GetTestDataFilePath(kDefaultModel);
char error[1024];
mjModel* model = mj_loadXML(xml_path.c_str(), nullptr, error, sizeof(error));
ASSERT_NE(model, nullptr) << error;
ASSERT_THAT(model, NotNull()) << error;
mjData* data = mj_makeData(model);
// set dense jacobian, call mj_forward, save accelerations
model->opt.jacobian = mjJAC_DENSE;
mj_forward(model, data);
std::vector<mjtNum> qacc_dense = AsVector(data->qacc, model->nv);
// set sparse jacobian, call mj_forward, save accelerations
model->opt.jacobian = mjJAC_SPARSE;
mj_forward(model, data);
std::vector<mjtNum> qacc_sparse = AsVector(data->qacc, model->nv);
// expect accelerations to be insignificantly different
mjtNum tol = 1e-11;
EXPECT_THAT(qacc_dense, Pointwise(DoubleNear(tol), qacc_sparse));
// TODO: is 1e-12 larger than we expect?
// investigate sources of discrepancy, eliminate if possible
for (mjtSolver solver : {mjSOL_NEWTON, mjSOL_PGS, mjSOL_CG}) {
model->opt.solver = solver;
// set dense jacobian, call mj_forward, save accelerations
model->opt.jacobian = mjJAC_DENSE;
mj_resetDataKeyframe(model, data, 0);
mj_forward(model, data);
std::vector<mjtNum> qacc_dense = AsVector(data->qacc, model->nv);
// set sparse jacobian, call mj_forward, save accelerations
model->opt.jacobian = mjJAC_SPARSE;
mj_resetDataKeyframe(model, data, 0);
mj_forward(model, data);
std::vector<mjtNum> qacc_sparse = AsVector(data->qacc, model->nv);
// expect accelerations to be insignificantly different
EXPECT_THAT(qacc_dense, Pointwise(DoubleNear(tol), qacc_sparse));
}
mj_deleteData(data);
mj_deleteModel(model);