Skip to content

[Core] Explicit all input vectors in addDForce - #6281

Open
alxbilger wants to merge 13 commits into
sofa-framework:masterfrom
alxbilger:explicitdxadddforce
Open

[Core] Explicit all input vectors in addDForce#6281
alxbilger wants to merge 13 commits into
sofa-framework:masterfrom
alxbilger:explicitdxadddforce

Conversation

@alxbilger

@alxbilger alxbilger commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

addDForce computes the term $df += k \frac{\partial \mathbf{f}}{\partial x} \cdot dx + b \frac{\partial \mathbf{f}}{\partial v} \cdot dx$. To perform this operation, we need $dx$, $x$ and $v$ (the derivatives can depend on $x$ or $v$).

However, the vectors $dx$, $x$ and $v$ couldn't be defined as a parameter of addDForce.

$dx$ was implicitly set to vec_id::read_access::dx through the call of readDx. Now, the vector dx can be defined to something different from vec_id::read_access::dx. For example, look at the trick used here to apply addDForce to v:

/// Compute the current force delta (given the latest propagated velocity)
void MechanicalOperations::computeDfV(core::MultiVecDerivId df, bool clear, bool accumulate)
{
const core::ConstMultiVecDerivId dx = mparams.dx();
mparams.setDx(mparams.v());
setDf(df);
if (clear)
{
executeVisitor( MechanicalResetForceVisitor(&mparams, df, false) );
//finish();
}
executeVisitor( MechanicalComputeDfVisitor(&mparams, df, accumulate) );
mparams.setDx(dx);
}
(I'm not even sure it works properly).

It's breaking for classes derived from BaseForceField, if this class overrides addDForce. This should not be a significant impact because in general, force fields overrides the addDForce of an intermediate class, such as ForceField or PairInteractionForceField. It's not breaking for the users of BaseForceField, because the dx, x and v parameters can be omitted. Without them, it routes to the previous behavior. However, this legacy method is deprecated.

This PR is an example to be generalised wherever an implicit vector is used.

Note: usually addForce pre-computes derivatives that can be used in addDForce. Adding x and v in addDForce will allow to check that the x and v used in addDForce does correspond to the x and v used in addForce.

[with-all-tests]


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).

@alxbilger alxbilger added pr: status to review To notify reviewers to review this pull-request pr: clean Cleaning the code labels Aug 31, 2026
@alxbilger
alxbilger force-pushed the explicitdxadddforce branch from e1098d9 to b3fa52e Compare August 31, 2026 12:47
@alxbilger alxbilger added the pr: breaking Change possibly inducing a compilation error label Aug 31, 2026
@alxbilger

Copy link
Copy Markdown
Contributor Author

[ci-build][with-all-tests]

@th-skam th-skam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 same for the other 2 PRs for me.

Comment thread Sofa/framework/Simulation/Core/src/sofa/simulation/config.h.in Outdated
@alxbilger

Copy link
Copy Markdown
Contributor Author

to do: add x and v to the new signature

@alxbilger alxbilger changed the title [Core] Explicit dx vector in addDForce [Core] Explicit all input vectors in addDForce Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: breaking Change possibly inducing a compilation error pr: clean Cleaning the code pr: status to review To notify reviewers to review this pull-request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants