Skip to content

[LinearSystem] Update the "M, B, K factors" of the linear system when necessary#5963

Open
leobois67 wants to merge 2 commits intosofa-framework:masterfrom
leobois67:2602-update-linearsystem-factor
Open

[LinearSystem] Update the "M, B, K factors" of the linear system when necessary#5963
leobois67 wants to merge 2 commits intosofa-framework:masterfrom
leobois67:2602-update-linearsystem-factor

Conversation

@leobois67
Copy link

Problem: calling mop.setSystemMBKMatrix(mFact, bFact, kFact, l_linearSolver.get()); actually ignores the factors except for the first time it is called.

Use case: In order to compute the gradient of a loss wrt some parameters, I want to solve linear systems with my own "M, B, K factors".

Solution: check for a change in the factors and update if necessary. This update is done in MatrixLinearSystem<TMatrix, TVector>::associateLocalMatrixTo().

Details: The factors passed to setSystemMBKMatrix() follow the path below:

MechanicalOperations::setSystemMBKMatrix()
> BaseMatrixLinearSystem::buildSystemMatrix()
    > BaseMatrixLinearSystem::preAssembleSystem() [virtual]
    > TypedMatrixLinearSystem<TMatrix, TVector>::preAssembleSystem()
        > TypedMatrixLinearSystem<TMatrix, TVector>::associateLocalMatrixToComponents() [virtual]
        > MatrixLinearSystem<TMatrix, TVector>::associateLocalMatrixToComponents()
            > MatrixLinearSystem<TMatrix, TVector>::associateLocalMatrixTo()

In this final method, the factors are only considered the first time the system is assembled because of this check:

auto it = componentLocalMatrix.find(pairs);
if (it == componentLocalMatrix.end())
{
    // use M, B, K factors
}

Which is why, after this check, we add the following code:

if (std::abs(localMatrix->m_cachedFactor - factor) > std::numeric_limits<SReal>::epsilon())
{
    localMatrix->setFactor(factor);
}

By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

@leobois67
Copy link
Author

Also: now that I understand better how things work, I may actually be able to do what I want without this PR ;)

@alxbilger
Copy link
Contributor

Your analysis makes sense. But the solution is wrong. m_cachedFactor is protected, therefore a direct access leads to a compilation error.

Comment on lines +866 to +869
if (std::abs(localMatrix->m_cachedFactor - factor) > std::numeric_limits<SReal>::epsilon())
{
localMatrix->setFactor(factor);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (std::abs(localMatrix->m_cachedFactor - factor) > std::numeric_limits<SReal>::epsilon())
{
localMatrix->setFactor(factor);
}
localMatrix->setFactor(factor);

I suggest to always set the factor even if it does not change since last time.

@alxbilger alxbilger changed the title Update the "M, B, K factors" of the linear system when necessary [LinearSystem] Update the "M, B, K factors" of the linear system when necessary Feb 24, 2026
@leobois67
Copy link
Author

leobois67 commented Feb 25, 2026

Yep, that is what we did before adding the condition without checking that it compiled... I made the change.

@bakpaul bakpaul added pr: fast merge Minor change that can be merged without waiting for the 7 review days pr: status to review To notify reviewers to review this pull-request labels Feb 26, 2026
@bakpaul bakpaul requested a review from alxbilger February 26, 2026 10:43
@hugtalbot hugtalbot added pr: enhancement About a possible enhancement pr: status ready Approved a pull-request, ready to be squashed and removed pr: status to review To notify reviewers to review this pull-request labels Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: enhancement About a possible enhancement pr: fast merge Minor change that can be merged without waiting for the 7 review days pr: status ready Approved a pull-request, ready to be squashed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants