-
Notifications
You must be signed in to change notification settings - Fork 340
[Lagrangian] Add corrected Jacobi constraint solving method #5680
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
bakpaul
wants to merge
19
commits into
sofa-framework:master
Choose a base branch
from
bakpaul:25_08_add_corrected_jacobi_constraint_solving_method
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 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
98c593d
First working version of exploded constraint problrms. Still lots of …
bakpaul e2aa322
Changed all problems into solvers. It makes more sense to use an inhe…
bakpaul fd67c12
Apply changes to scenes, tests and tutorials
bakpaul 200e4d8
Add stopping ocndition based on constraint force evolutionr ate as pr…
bakpaul 6e4796d
Merge branch 'master' into 25_08_add_ending_condition_in_NNCG
bakpaul a07de45
Add first version of improved Jacobi
bakpaul 0a2a097
Finish improved jacobi
bakpaul b0837e7
Finalize the solver in monothread
bakpaul b079b15
Remove unwanted file
bakpaul 66b5c49
Add reference
bakpaul 4d7f646
Fix condition to exit loop
bakpaul 6132cf0
Include review comment + fix algorithm from a Gauss-seidel to a real …
bakpaul 60a94f3
Add parameters
bakpaul 3e8b76a
fix compilation
bakpaul 08df692
Fix compilation
bakpaul a1d0bc7
Merge branch 'master' into 25_08_add_corrected_jacobi_constraint_solv…
bakpaul 35638f3
Clean and fix
bakpaul a3a3afe
Apply suggestions from code review
bakpaul 75f2791
Merge branch 'master' into 25_08_add_corrected_jacobi_constraint_solv…
fredroy 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
198 changes: 198 additions & 0 deletions
198
...Solver/src/sofa/component/constraint/lagrangian/solver/ImprovedJacobiConstraintSolver.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,198 @@ | ||
| /****************************************************************************** | ||
| * 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/component/constraint/lagrangian/solver/ImprovedJacobiConstraintSolver.h> | ||
| #include <sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h> | ||
| #include <sofa/helper/AdvancedTimer.h> | ||
| #include <sofa/helper/ScopedAdvancedTimer.h> | ||
| #include <sofa/core/ObjectFactory.h> | ||
| #include <Eigen/Eigenvalues> | ||
|
|
||
| namespace sofa::component::constraint::lagrangian::solver | ||
| { | ||
|
|
||
| ImprovedJacobiConstraintSolver::ImprovedJacobiConstraintSolver() | ||
| : BuiltConstraintSolver() | ||
| , d_useSpectralCorrection(initData(&d_useSpectralCorrection,false,"useSpectralCorrection","If set to true, the solution found after each iteration will be multiplied by spectralCorrectionFactor*2/spr(W), with spr() denoting the spectral radius.")) | ||
| , d_spectralCorrectionFactor(initData(&d_spectralCorrectionFactor,1.0,"spectralCorrectionFactor","Factor used to modulate the spectral correction")) | ||
| , d_useConjugateResidue(initData(&d_useConjugateResidue,false,"useConjugateResidue","If set to true, the solution found after each iteration will be corrected along the solution direction using `\\lambda^{i+1} -= beta^{i} * (\\lambda^{i} - \\lambda^{i-1})` with beta following the formula beta^{i} = min(1, (i/maxIterations)^{conjugateResidueSpeedFactor}) ")) | ||
| , d_conjugateResidueSpeedFactor(initData(&d_conjugateResidueSpeedFactor,10.0,"conjugateResidueSpeedFactor","FActor used to modulate the speed in which beta used in the conjugate residue part reaches 1.0. The higher the value, the slower the reach. ")) | ||
| { | ||
|
|
||
| } | ||
|
|
||
|
|
||
| void ImprovedJacobiConstraintSolver::doSolve(GenericConstraintProblem * problem , SReal timeout) | ||
| { | ||
| SCOPED_TIMER_VARNAME(gaussSeidelTimer, "ImprovedJacobiConstraintSolver"); | ||
|
|
||
|
|
||
| const int dimension = problem->getDimension(); | ||
|
|
||
| if(!dimension) | ||
| { | ||
| problem->currentError = 0.0; | ||
| problem->currentIterations = 0; | ||
| return; | ||
| } | ||
|
|
||
| SReal *dfree = problem->getDfree(); | ||
| SReal *force = problem->getF(); | ||
| SReal **w = problem->getW(); | ||
| SReal tol = problem->tolerance; | ||
| SReal *d = problem->_d.ptr(); | ||
|
|
||
| std::copy_n(dfree, dimension, d); | ||
|
|
||
| for(unsigned i=0; i< dimension; ++i) | ||
| { | ||
| force[i] = 0; | ||
| } | ||
|
|
||
| std::vector<SReal> lastF; | ||
| lastF.resize(problem->getDimension(), 0.0); | ||
|
|
||
| std::vector<SReal> deltaF; | ||
| deltaF.resize(problem->getDimension(), 0.0); | ||
|
|
||
| std::vector<SReal> correctedD; | ||
| correctedD.resize(problem->getDimension(), 0.0); | ||
|
|
||
| // std::cout<<"Initialized vectors"<<std::endl; | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| SReal error=0.0; | ||
| bool convergence = false; | ||
| if(problem->scaleTolerance && !problem->allVerified) | ||
| { | ||
| tol *= dimension; | ||
| } | ||
|
|
||
| for(int i=0; i<dimension; ) | ||
| { | ||
| if(!problem->constraintsResolutions[i]) | ||
| { | ||
| msg_error()<< "Bad size of constraintsResolutions in GenericConstraintSolver" ; | ||
| break; | ||
| } | ||
| problem->constraintsResolutions[i]->init(i, w, force); | ||
| i += problem->constraintsResolutions[i]->getNbLines(); | ||
| } | ||
|
|
||
| sofa::type::vector<SReal> tabErrors(dimension); | ||
|
|
||
| int iterCount = 0; | ||
|
|
||
| SReal rho = 1.0; | ||
|
|
||
| if (d_useSpectralCorrection.getValue()) | ||
| { | ||
| Eigen::Map<Eigen::MatrixX<SReal>> EigenW(w[0],dimension, dimension) ; | ||
| SReal eigenRadius = 0; | ||
| for(auto s : EigenW.eigenvalues()) | ||
| { | ||
| eigenRadius=std::max(eigenRadius,norm(s)); | ||
| } | ||
| rho = d_spectralCorrectionFactor.getValue()*std::min(1.0, 0.9 * 2/eigenRadius); | ||
| } | ||
|
|
||
| for(int i=0; i<problem->maxIterations; i++) | ||
| { | ||
| iterCount ++; | ||
| bool constraintsAreVerified = true; | ||
|
|
||
| error=0.0; | ||
| SReal beta = d_useConjugateResidue.getValue() * std::min(1.0, pow( ((float)i)/problem->maxIterations,d_conjugateResidueSpeedFactor.getValue())); | ||
|
|
||
| for(int j=0; j<dimension; ) // increment of j realized at the end of the loop | ||
| { | ||
| // 1. nbLines provide the dimension of the constraint | ||
| const unsigned int nb = problem->constraintsResolutions[j]->getNbLines(); | ||
|
|
||
| for(unsigned l=j; l<j+nb; ++l ) | ||
| { | ||
| for(unsigned k=0; k<dimension; ++k) | ||
| { | ||
| d[l] += w[l][k] * deltaF[k]; | ||
| } | ||
| correctedD[l] = rho * d[l] ; | ||
| } | ||
| j += nb; | ||
| } | ||
|
|
||
| for(int j=0; j<dimension; ) // increment of j realized at the end of the loop | ||
| { | ||
| // 1. nbLines provide the dimension of the constraint | ||
| const unsigned int nb = problem->constraintsResolutions[j]->getNbLines(); | ||
|
|
||
| problem->constraintsResolutions[j]->resolution(j,w,correctedD.data(), force, dfree); | ||
| for(unsigned l=j; l<j+nb; ++l ) | ||
| { | ||
| force[l] += beta * deltaF[l] ; | ||
| deltaF[l] = force[l] - lastF[l]; | ||
| lastF[l] = force[l]; | ||
| } | ||
|
|
||
| SReal cstError = 0.0; | ||
| for(unsigned l=j; l<j+nb; ++l ) | ||
| { | ||
| for(unsigned k=0; k<dimension; ++k) | ||
| { | ||
| cstError += pow(w[l][k] * deltaF[k],2); | ||
| } | ||
| constraintsAreVerified = constraintsAreVerified && cstError < pow(tol,2); | ||
| } | ||
| error += sqrt(cstError); | ||
| j+= nb; | ||
|
|
||
| } | ||
|
|
||
| if (problem->allVerified) | ||
| { | ||
| if (constraintsAreVerified) | ||
| { | ||
| convergence = true; | ||
| break; | ||
| } | ||
| } | ||
| else if(error < tol && i > 0) // do not stop at the first iteration (that is used for initial guess computation) | ||
| { | ||
| convergence = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| sofa::helper::AdvancedTimer::valSet("GS iterations", problem->currentIterations); | ||
|
|
||
| problem->result_output(this, force, error, iterCount, convergence); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| void registerImprovedJacobiConstraintSolver(sofa::core::ObjectFactory* factory) | ||
| { | ||
| factory->registerObjects(core::ObjectRegistrationData("A Constraint Solver using the Linear Complementarity Problem formulation to solve Constraint based components using a Projected Jacobi iterative method") | ||
| .add< ImprovedJacobiConstraintSolver >()); | ||
| } | ||
|
|
||
|
|
||
| } | ||
50 changes: 50 additions & 0 deletions
50
...n/Solver/src/sofa/component/constraint/lagrangian/solver/ImprovedJacobiConstraintSolver.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,50 @@ | ||
| /****************************************************************************** | ||
| * 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/component/constraint/lagrangian/solver/BuiltConstraintSolver.h> | ||
| #include <sofa/core/behavior/ConstraintResolution.h> | ||
|
|
||
| namespace sofa::component::constraint::lagrangian::solver | ||
| { | ||
| class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API ImprovedJacobiConstraintSolver : public BuiltConstraintSolver | ||
| { | ||
| public: | ||
| SOFA_CLASS(ImprovedJacobiConstraintSolver, BuiltConstraintSolver); | ||
|
|
||
| Data<bool> d_useSpectralCorrection; | ||
| Data<SReal> d_spectralCorrectionFactor; | ||
| Data<bool> d_useConjugateResidue; | ||
| Data<SReal> d_conjugateResidueSpeedFactor; | ||
|
|
||
| ImprovedJacobiConstraintSolver(); | ||
|
|
||
| protected: | ||
| /** | ||
| * Based on paper | ||
| * Francu, Mihai & Moldoveanu, Florica. An Improved Jacobi Solver for Particle Simulation. | ||
| * VRPHYS 2014 | ||
| **/ | ||
| virtual void doSolve(GenericConstraintProblem * problem , SReal timeout = 0.0) override; | ||
|
|
||
| }; | ||
| } |
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
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.