-
Notifications
You must be signed in to change notification settings - Fork 361
[Simulation.Core] Differential operations #6300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alxbilger
wants to merge
5
commits into
sofa-framework:master
Choose a base branch
from
alxbilger:differentialoperations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Sofa/framework/Simulation/Core/src/sofa/simulation/DifferentialOperations.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: contact@sofa-framework.org * | ||
| ******************************************************************************/ | ||
| #include <sofa/simulation/DifferentialOperations.h> | ||
| #include <sofa/core/MechanicalParams.h> | ||
|
|
||
| namespace sofa::simulation::common | ||
| { | ||
|
|
||
| void DifferentialOperations::pushforwardCoord( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecCoordId coordVectorId) | ||
| { | ||
| mappingGraph.algorithms.traverseTopDown_([&](core::BaseMapping& mapping) | ||
| { | ||
| mapping.apply(&mparams, coordVectorId, coordVectorId); | ||
| }); | ||
| } | ||
|
|
||
| void DifferentialOperations::pushforwardTangent( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecDerivId tangentVectorId) | ||
| { | ||
| mappingGraph.algorithms.traverseTopDown_([&](core::BaseMapping& mapping) | ||
| { | ||
| mapping.applyJ(&mparams, tangentVectorId, tangentVectorId); | ||
| }); | ||
| } | ||
|
|
||
| void DifferentialOperations::pullbackCotangent( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecDerivId cotangentVectorId, bool ignoreMappingFlag) | ||
| { | ||
| mappingGraph.algorithms.traverseBottomUp_([&](core::BaseMapping& mapping) | ||
| { | ||
| if (mapping.areForcesMapped() || ignoreMappingFlag) | ||
| { | ||
| mapping.applyJT(&mparams, cotangentVectorId, cotangentVectorId); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void DifferentialOperations::pullbackCotangentTangent( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecDerivId cotangentTangentVectorId) | ||
| { | ||
| mappingGraph.algorithms.traverseBottomUp_( | ||
| [&](core::BaseMapping& mapping) | ||
| { | ||
| mapping.applyJT(&mparams, cotangentTangentVectorId, cotangentTangentVectorId); | ||
| if (mparams.kFactor() != 0) | ||
| { | ||
| // ideally, the child cotangent vector must be provided here, instead of implicitly getting it in the mapping | ||
| mapping.applyDJT(&mparams, cotangentTangentVectorId, cotangentTangentVectorId); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| } // namespace sofa::simulation::common | ||
121 changes: 121 additions & 0 deletions
121
Sofa/framework/Simulation/Core/src/sofa/simulation/DifferentialOperations.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: contact@sofa-framework.org * | ||
| ******************************************************************************/ | ||
| #pragma once | ||
|
|
||
| #include <sofa/simulation/config.h> | ||
|
|
||
| #include <sofa/core/MultiVecId.h> | ||
| #include <sofa/simulation/MappingGraph.h> | ||
|
|
||
| namespace sofa::simulation::common | ||
| { | ||
|
|
||
| /** | ||
| * @brief Differential and geometric operations over a MappingGraph. | ||
| * | ||
| * Provides static operations for pushing forward and pulling back multi-vectors | ||
| * through the hierarchy of mappings defined within a MappingGraph. | ||
| * | ||
| * In physical simulations, primal quantities (coordinates, tangent vectors such as velocities | ||
| * or displacements) are propagated top-to-bottom through pushforward operations (applying | ||
| * the mapping function or its Jacobian $\mathbf{J}$). Dual quantities (cotangent vectors such | ||
| * as forces) are transformed bottom-to-top through pullback operations (using the transpose | ||
| * Jacobian $\mathbf{J}^T$). | ||
| */ | ||
| class SOFA_SIMULATION_CORE_API DifferentialOperations | ||
| { | ||
| public: | ||
|
|
||
| /** | ||
| * @brief Push forward primal coordinates top-down through the mapping graph. | ||
| * | ||
| * Traverses the mapping graph from top to bottom and applies each mapping function | ||
| * to transform primal configuration coordinates (e.g. positions) across coordinate frames. | ||
| * | ||
| * @param mappingGraph The mapping graph containing the topology and mappings to traverse. | ||
| * @param mparams Mechanical parameters associated with the current operation. | ||
| * @param coordVectorId Identifier of the coordinate multi-vector to push forward. | ||
| */ | ||
| static void pushforwardCoord( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecCoordId coordVectorId); | ||
|
|
||
| /** | ||
| * @brief Push forward tangent vectors top-down via the mapping Jacobian. | ||
| * | ||
| * Traverses the mapping graph from top to bottom and applies the Jacobian matrix $\mathbf{J}$ | ||
| * of each mapping to propagate tangent quantities (e.g. velocities, displacements $dx$) | ||
| * via Jacobian-vector products ($\mathbf{J}v$). | ||
| * | ||
| * @param mappingGraph The mapping graph containing the topology and mappings to traverse. | ||
| * @param mparams Mechanical parameters associated with the current operation. | ||
| * @param tangentVectorId Identifier of the derivative multi-vector representing tangent quantities to push forward. | ||
| */ | ||
| static void pushforwardTangent( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecDerivId tangentVectorId); | ||
|
|
||
| /** | ||
| * @brief Pull back cotangent vectors bottom-up via the transpose of the mapping Jacobian. | ||
| * | ||
| * Traverses the mapping graph from bottom to top and applies the transposed Jacobian $\mathbf{J}^T$ | ||
| * of each mapping to pull back dual quantities (e.g. forces, momentum) via | ||
| * vector-Jacobian products ($v^T\mathbf{J}$). | ||
| * | ||
| * @param mappingGraph The mapping graph containing the topology and mappings to traverse. | ||
| * @param mparams Mechanical parameters associated with the current operation. | ||
| * @param cotangentVectorId Identifier of the derivative multi-vector representing cotangent quantities to pull back. | ||
| * @param ignoreMappingFlag If true, forces are pulled back through all mappings regardless of | ||
| * whether their internal force-mapping flag (`areForcesMapped()`) is enabled. | ||
| */ | ||
| static void pullbackCotangent( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecDerivId cotangentVectorId, | ||
| bool ignoreMappingFlag = true); | ||
|
|
||
| /** | ||
| * @brief Pull back the differential (tangent variation) of a cotangent vector bottom-up. | ||
| * | ||
| * Computes the total differential of a vector-Jacobian product (VJP), corresponding to | ||
| * the variation of a pulled-back cotangent quantity (such as force variation @f$ df_x = d(\mathbf{J}^T f_y) @f$): | ||
| * @f[ | ||
| * df_x = \mathbf{J}^T df_y + d(\mathbf{J}^T) f_y | ||
| * @f] | ||
| * | ||
| * Traverses the mapping graph from bottom to top and accumulates: | ||
| * 1. The first-order pullback of the cotangent differential via the transpose Jacobian: | ||
| * @f$ \mathbf{J}^T df_y @f$ (`applyJT()`). | ||
| * 2. When the stiffness factor @c kFactor is non-zero, the geometric stiffness contribution | ||
| * arising from the non-linear variation of the mapping's Jacobian (a Hessian-vector contraction): | ||
| * @f$ d(\mathbf{J}^T) f_y @f$ (`applyDJT()`). | ||
| * | ||
| * @param mappingGraph The mapping graph containing the topology and mappings to traverse. | ||
| * @param mparams Mechanical parameters associated with the current operation (including @c kFactor). | ||
| * @param cotangentTangentVectorId Identifier of the derivative multi-vector storing the cotangent | ||
| * tangent variations (e.g. @f$ df @f$) to pull back. | ||
| */ | ||
| static void pullbackCotangentTangent( | ||
| const MappingGraph& mappingGraph, const core::MechanicalParams& mparams, | ||
| core::MultiVecDerivId cotangentTangentVectorId); | ||
| }; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the areForcesMapped() methode renamed it seems a rather mechanical term.
I would also invert the ignoreMappingFlag and mapping.areForcesMapped() because the cost for quering mapping.areForceMapped() is much more costly as it is virtual function, that is accessing a Data, so calling a bunch of other virtual function trigger and update the DDG).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes of course