Skip to content

[Simulation.Core] ADD support for several mechanical states in a Node - #6472

Open
alxbilger wants to merge 2 commits into
sofa-framework:masterfrom
alxbilger:multiple_mechanical_states
Open

alxbilger wants to merge 2 commits into
sofa-framework:masterfrom
alxbilger:multiple_mechanical_states

Conversation

@alxbilger

Copy link
Copy Markdown
Contributor

A Node used to hold at most one BaseMechanicalState and one BaseState. Adding a second one replaced the first with a warning saying the behavior is undefined. This PR stores them as sequences, so several states can live in the same node.

Changes

  • Node: state and mechanicalState are now NodeSequence instead of NodeSingle. Their links (state, mechanicalState) become multi-links.
  • New plural accessors: getStates() and getMechanicalStates(), in BaseContext and overridden in Node. They return the local states, or, if there are none, those of the first ancestor that has some.
  • The singular accessors getState() and getMechanicalState() behave as before, except when the node holds several states: they return the first one and warn once per node that the lookup is ambiguous and an explicit link is preferred.
  • Visitors loop over all states: BaseMechanicalVisitor (fwd/bwd), StateChangeVisitor, WriteStateVisitor, VelocityThresholdVisitor, ExportGnuplotVisitor, ExportDotVisitor, MechanicalVPrintVisitor, and the test and SceneCreator helpers.
  • SceneCheckMapping is adapted to state sequences.
  • Playback:
    • WriteStateCreator, ReadStateCreator and CompareStateCreator create one component per mechanical state, with an explicit l_state link.
    • An existing component is reused only if it is linked to that state. An unlinked one is reused only when the node has a single state.
    • The activators, ReadStateModifier and CompareStateResult handle all the components of a node.
    • l_state is now public.

Behavior changes

  • A node with two mechanical states no longer warns when the second one is added, and visitors now process both states. Before, only the last one added was kept.
  • getMechanicalState() in such a node now returns the first state added, not the last.
  • Python: findLink("mechanicalState").isMultiLink() is now True.

Limitations / follow-ups

  • A node is still considered mapped or not as a whole (node->mechanicalMapping), not per state. Mixing mapped and independent states in one node is not supported yet (TODO in BaseMechanicalVisitor).
  • mechanicalMapping and mass are still single.
  • Components that resolve their state implicitly through getMechanicalState() still get the first one; a typed resolution helper is planned.

Related PRs

  • SofaPython3: fix the getMechanicalState binding, add Node.getMechanicalStates(), update the BaseLink test.

Tests

  • Node_test.twoMechanicalStatesInTheSameNode is updated: both states are stored and visited, the plural accessors return both (including from a child node), and the singular one returns the first with a warning.
  • New test Node_test.removeOneOfTwoMechanicalStates.
  • New StateCreators_test: one component per state for each creator, reuse when rerun, reuse of an explicitly linked component, and no reuse of an unlinked one when the node has several states.
  • These suites pass in Debug: Sofa.Simulation.Graph_test, Sofa.Simulation.Core_test, Sofa.Simulation.Core_simutest, Sofa.Simulation.Common_test, Sofa.Core_test, Sofa.Core_simutest, SceneChecking_test, SceneCreator_test, Sofa.Component.Playback_test, Sofa.Component.SceneUtility_test, and the IntegrationScheme Backward and Forward tests.

[ci-depends-on https://github.com/sofa-framework/SofaPython3/pull/658]


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: enhancement About a possible enhancement pr: status to review To notify reviewers to review this pull-request pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI labels Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI pr: enhancement About a possible enhancement 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.

1 participant