From 6559fa71ebc813bb492fad50c824e787cf3c5777 Mon Sep 17 00:00:00 2001 From: Paul Baksic Date: Tue, 3 Feb 2026 17:24:22 +0100 Subject: [PATCH 1/4] Start building matrix --- .../Direct/tests/SparseLDLSolver_test.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp index 3d72df3da06..26f5e0d89a4 100644 --- a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp +++ b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp @@ -28,6 +28,7 @@ #include #include +#include TEST(SparseLDLSolver, EmptySystem) @@ -140,3 +141,58 @@ TEST(SparseLDLSolver, AssociatedLinearSystem) EXPECT_EQ(MatrixSystem::GetCustomTemplateName(), MatrixType::Name()); } + + +TEST(SparseLDLSolver, TestInvertingRandomMatrix) +{ + using MatrixType = sofa::linearalgebra::CompressedRowSparseMatrix; + using Solver = sofa::component::linearsolver::direct::SparseLDLSolver >; + const Solver::SPtr solver = sofa::core::objectmodel::New(); + + solver->init(); + + unsigned nbRows = 300; + unsigned nbCols = 300; + SReal reg = 5; + float sparsity = 0.5; + const auto nbNonZero = static_cast(sparsity * 0.5 * static_cast(nbRows*nbCols)); + + + sofa::linearalgebra::FullMatrix tempMatrix(nbRows,nbCols), finalTempMatrix; + sofa::helper::RandomGenerator randomGenerator; + randomGenerator.initSeed(153); + + + for (sofa::SignedIndex i = 0; i < nbNonZero; ++i) + { + const auto value = static_cast(sofa::helper::drand(1)); + const auto row = randomGenerator.random(0, nbRows); + const auto col = randomGenerator.random(0, nbCols); + tempMatrix.set(row,col,value); + } + + + const double epsilon = std::numeric_limits::epsilon(); + tempMatrix.mulT(finalTempMatrix, tempMatrix); + for (sofa::SignedIndex i = 0; i < nbRows; ++i) + { + finalTempMatrix.set(i,i,finalTempMatrix(i,i) + reg); + } + sofa::linearalgebra::CompressedRowSparseMatrix matrix; + matrix.resize(nbRows, nbCols); + + for (unsigned i=0; i epsilon) + matrix.set(i,j,tempMatrix(i,j)); + } + + } + matrix.compress(); + + + + +} From 01c6edf63e18e6e662333cbe4cab2e70f4914275 Mon Sep 17 00:00:00 2001 From: Paul Baksic Date: Wed, 4 Feb 2026 16:11:05 +0100 Subject: [PATCH 2/4] finish unit test --- .../Direct/tests/SparseLDLSolver_test.cpp | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp index 26f5e0d89a4..b6e26f36656 100644 --- a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp +++ b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp @@ -19,17 +19,17 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ -#include -#include #include +#include #include +#include +#include #include #include -#include - +#include #include -#include +#include "sofa/type/MatSym.h" TEST(SparseLDLSolver, EmptySystem) { @@ -154,44 +154,69 @@ TEST(SparseLDLSolver, TestInvertingRandomMatrix) unsigned nbRows = 300; unsigned nbCols = 300; SReal reg = 5; - float sparsity = 0.5; - const auto nbNonZero = static_cast(sparsity * 0.5 * static_cast(nbRows*nbCols)); + float sparsity = 0.05; + const auto nbNonZero = static_cast(sparsity * static_cast(nbRows*nbCols)); sofa::linearalgebra::FullMatrix tempMatrix(nbRows,nbCols), finalTempMatrix; + tempMatrix.clear(); + sofa::helper::RandomGenerator randomGenerator; - randomGenerator.initSeed(153); + randomGenerator.initSeed(2807); for (sofa::SignedIndex i = 0; i < nbNonZero; ++i) { - const auto value = static_cast(sofa::helper::drand(1)); + const auto value = static_cast(fabs(sofa::helper::drand(2))); const auto row = randomGenerator.random(0, nbRows); const auto col = randomGenerator.random(0, nbCols); tempMatrix.set(row,col,value); } - - const double epsilon = std::numeric_limits::epsilon(); - tempMatrix.mulT(finalTempMatrix, tempMatrix); for (sofa::SignedIndex i = 0; i < nbRows; ++i) { - finalTempMatrix.set(i,i,finalTempMatrix(i,i) + reg); + tempMatrix.set(i,i,tempMatrix(i,i) + reg); } + tempMatrix.mulT(finalTempMatrix, tempMatrix); + sofa::linearalgebra::CompressedRowSparseMatrix matrix; matrix.resize(nbRows, nbCols); + unsigned nbNZ = 0; for (unsigned i=0; i epsilon) - matrix.set(i,j,tempMatrix(i,j)); + if (finalTempMatrix(i,j) > 1e-8) + matrix.set(i,j,finalTempMatrix(i,j)); + else + ++nbNZ; } } + msg_info("TestInvertingRandomMatrix") << "REAL SPARSITY (#zeros/#elements) : " <<(nbNZ)/static_cast(nbRows*nbCols) ; matrix.compress(); + sofa::linearalgebra::FullVector known(nbCols), unknown(nbCols), rhs(nbRows); + for (unsigned i=0; i(sofa::helper::drand(1)); + known.set(i,value); + } + matrix.mul(rhs, known); + + solver->invert(matrix); + solver->solve(matrix, unknown, rhs); + + for (unsigned i=0; i Date: Wed, 4 Feb 2026 16:11:59 +0100 Subject: [PATCH 3/4] clean --- .../LinearSolver/Direct/tests/SparseLDLSolver_test.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp index b6e26f36656..6ebc20bd4c4 100644 --- a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp +++ b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp @@ -213,11 +213,4 @@ TEST(SparseLDLSolver, TestInvertingRandomMatrix) EXPECT_NEAR(unknown[i], known[i], 1e-12); } - - - - - - - } From 0258dc8180b895a71e606b22cf8572298f46ffc6 Mon Sep 17 00:00:00 2001 From: Paul Baksic Date: Wed, 4 Feb 2026 16:13:59 +0100 Subject: [PATCH 4/4] clean unwanted header --- .../Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp index 6ebc20bd4c4..882c0cb34af 100644 --- a/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp +++ b/Sofa/Component/LinearSolver/Direct/tests/SparseLDLSolver_test.cpp @@ -29,7 +29,6 @@ #include #include -#include "sofa/type/MatSym.h" TEST(SparseLDLSolver, EmptySystem) {