diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.cpp index 3c2216f1165..243e0402735 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.cpp @@ -89,7 +89,7 @@ void ConstraintSolverImpl::cleanup() ConstraintSolver::cleanup(); } -void ConstraintSolverImpl::removeConstraintCorrection(core::behavior::BaseConstraintCorrection* s) +void ConstraintSolverImpl::doRemoveConstraintCorrection(core::behavior::BaseConstraintCorrection* s) { l_constraintCorrections.remove(s); } diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.h b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.h index be3333ca787..8b606bf1fe6 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.h +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.h @@ -92,7 +92,7 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API ConstraintSolverImpl : pub /// This is used to prevent concurrent access to the LCP when using a LCPForceFeedback through an haptic thread. virtual void lockConstraintProblem(sofa::core::objectmodel::BaseComponent* from, ConstraintProblem* p1, ConstraintProblem* p2=nullptr) = 0; - void removeConstraintCorrection(core::behavior::BaseConstraintCorrection *s) override; + void doRemoveConstraintCorrection(core::behavior::BaseConstraintCorrection *s) override; MultiLink< ConstraintSolverImpl, core::behavior::BaseConstraintCorrection, diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.cpp index 44d9bee1c7a..aeface15d25 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.cpp @@ -147,7 +147,7 @@ void GenericConstraintSolver::cleanup() sofa::component::constraint::lagrangian::solver::ConstraintSolverImpl::cleanup(); } -bool GenericConstraintSolver::prepareStates(const core::ConstraintParams *cParams, MultiVecId /*res1*/, MultiVecId /*res2*/) +bool GenericConstraintSolver::doPrepareStates(const core::ConstraintParams *cParams, MultiVecId /*res1*/, MultiVecId /*res2*/) { last_cp = current_cp; @@ -175,7 +175,7 @@ bool GenericConstraintSolver::prepareStates(const core::ConstraintParams *cParam return true; } -bool GenericConstraintSolver::buildSystem(const core::ConstraintParams *cParams, MultiVecId res1, MultiVecId res2) +bool GenericConstraintSolver::doBuildSystem(const core::ConstraintParams *cParams, MultiVecId res1, MultiVecId res2) { SOFA_UNUSED(res1); SOFA_UNUSED(res2); @@ -206,7 +206,7 @@ bool GenericConstraintSolver::buildSystem(const core::ConstraintParams *cParams, } -void GenericConstraintSolver::rebuildSystem(const SReal massFactor, const SReal forceFactor) +void GenericConstraintSolver::doRebuildSystem(const SReal massFactor, const SReal forceFactor) { for (const auto& cc : l_constraintCorrections) { @@ -253,7 +253,7 @@ void printLCP(std::ostream& file, SReal *q, SReal **M, SReal *f, int dim, bool p file << " ];" << msgendl << msgendl; } -bool GenericConstraintSolver::solveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) +bool GenericConstraintSolver::doSolveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) { current_cp->tolerance = d_tolerance.getValue(); current_cp->maxIterations = d_maxIt.getValue(); @@ -299,7 +299,7 @@ bool GenericConstraintSolver::solveSystem(const core::ConstraintParams * /*cPara return true; } -void GenericConstraintSolver::computeResidual(const core::ExecParams* eparam) +void GenericConstraintSolver::doComputeResidual(const core::ExecParams* eparam) { for (const auto& cc : l_constraintCorrections) { @@ -378,7 +378,7 @@ void GenericConstraintSolver::storeConstraintLambdas(const core::ConstraintParam } -bool GenericConstraintSolver::applyCorrection(const core::ConstraintParams *cParams, MultiVecId res1, MultiVecId res2) +bool GenericConstraintSolver::doApplyCorrection(const core::ConstraintParams *cParams, MultiVecId res1, MultiVecId res2) { computeAndApplyMotionCorrection(cParams, res1, res2); storeConstraintLambdas(cParams); diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h index 9f3ea631fbb..bfb9004b005 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h @@ -50,12 +50,13 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API GenericConstraintSolver : void cleanup() override; - bool prepareStates(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - bool buildSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - void rebuildSystem(const SReal massFactor, const SReal forceFactor) override; - bool solveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - bool applyCorrection(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - void computeResidual(const core::ExecParams* /*params*/) override; + bool doPrepareStates(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + bool doBuildSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + void doRebuildSystem(SReal massFactor, SReal forceFactor) override; + bool doSolveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + bool doApplyCorrection(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + void doComputeResidual(const core::ExecParams* /*params*/) override; + ConstraintProblem* getConstraintProblem() override; void lockConstraintProblem(sofa::core::objectmodel::BaseComponent* from, ConstraintProblem* p1, ConstraintProblem* p2 = nullptr) override; diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp index 8086d196218..1289557e968 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp @@ -85,7 +85,7 @@ void LCPConstraintProblem::solveTimed(SReal tolerance, int maxIt, SReal timeout) helper::nlcp_gaussseidelTimed(dimension, getDfree(), getW(), getF(), mu, tolerance, maxIt, true, timeout); } -bool LCPConstraintSolver::prepareStates(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) +bool LCPConstraintSolver::doPrepareStates(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) { last_cp = current_cp; MechanicalVOpVisitor(core::execparams::defaultInstance(), (core::VecId)core::vec_id::write_access::dx).setMapped(true).execute( getContext()); //dX=0 @@ -115,7 +115,7 @@ void LCPConstraintSolver::addRegularization(linearalgebra::BaseMatrix& W) } } -bool LCPConstraintSolver::buildSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2) +bool LCPConstraintSolver::doBuildSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2) { SOFA_UNUSED(res1); SOFA_UNUSED(res2); @@ -157,7 +157,7 @@ void LCPConstraintSolver::buildSystem() } -bool LCPConstraintSolver::solveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) +bool LCPConstraintSolver::doSolveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) { const auto _mu = d_mu.getValue(); @@ -243,7 +243,7 @@ bool LCPConstraintSolver::solveSystem(const core::ConstraintParams * /*cParams*/ return true; } -bool LCPConstraintSolver::applyCorrection(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) +bool LCPConstraintSolver::doApplyCorrection(const core::ConstraintParams * /*cParams*/, MultiVecId /*res1*/, MultiVecId /*res2*/) { if (d_initial_guess.getValue()) { diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h index 0e4b2f06903..244dfe53a73 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.h @@ -65,10 +65,10 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API LCPConstraintSolver : publ ~LCPConstraintSolver() override; public: - bool prepareStates(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - bool buildSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - bool solveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; - bool applyCorrection(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + bool doPrepareStates(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + bool doBuildSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + bool doSolveSystem(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; + bool doApplyCorrection(const core::ConstraintParams * /*cParams*/, MultiVecId res1, MultiVecId res2=MultiVecId::null()) override; diff --git a/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.cpp b/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.cpp index f20198c898f..920998be51b 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.cpp @@ -31,7 +31,7 @@ namespace sofa::core::behavior ConstraintSolver::ConstraintSolver() = default; ConstraintSolver::~ConstraintSolver() = default; -void ConstraintSolver::solveConstraint(const ConstraintParams * cParams, MultiVecId res1, MultiVecId res2) +void ConstraintSolver::doSolveConstraint(const ConstraintParams * cParams, MultiVecId res1, MultiVecId res2) { SCOPED_TIMER("SolveConstraint"); prepareStatesTask(cParams, res1, res2) && diff --git a/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.h b/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.h index 3cfffbe8437..570d697fcc4 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/ConstraintSolver.h @@ -48,7 +48,17 @@ class SOFA_CORE_API ConstraintSolver : public virtual objectmodel::BaseComponent ConstraintSolver(); ~ConstraintSolver() override; - + virtual void doSolveConstraint(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()); + virtual bool doPrepareStates(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) = 0; + virtual bool doBuildSystem(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) = 0; + virtual void doRebuildSystem(SReal /*massFactor*/, SReal /*forceFactor*/) {}; + virtual bool doSolveSystem(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) = 0; + virtual bool doApplyCorrection(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) = 0; + virtual void doComputeResidual(const core::ExecParams* /*cParams*/) + { + dmsg_error() << "ComputeResidual is not implemented in " << this->getName() ; + } + virtual void doRemoveConstraintCorrection(BaseConstraintCorrection *s) = 0; private: ConstraintSolver(const ConstraintSolver& n) = delete; ConstraintSolver& operator=(const ConstraintSolver& n) = delete; @@ -58,42 +68,124 @@ class SOFA_CORE_API ConstraintSolver : public virtual objectmodel::BaseComponent /** * Launch the sequence of operations in order to solve the constraints */ - virtual void solveConstraint(const ConstraintParams *, MultiVecId res1, MultiVecId res2=MultiVecId::null()); + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doSolveConstraint" internally, + * which is the method to override from now on. + * + **/ + virtual void solveConstraint(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) final + { + //TODO (SPRINT SED 2025): Component state mechanism + doSolveConstraint(cParams, res1, res2); + } /** * Do the precomputation: compute free state, or propagate the states to the mapped mechanical states, where the constraint can be expressed */ - virtual bool prepareStates(const ConstraintParams *, MultiVecId res1, MultiVecId res2=MultiVecId::null())=0; + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doPrepareStates" internally, + * which is the method to override from now on. + * + **/ + virtual bool prepareStates(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) final + { + //TODO (SPRINT SED 2025): Component state mechanism + return doPrepareStates(cParams,res1,res2); + } /** * Create the system corresponding to the constraints */ - virtual bool buildSystem(const ConstraintParams *, MultiVecId res1, MultiVecId res2=MultiVecId::null())=0; + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doBuildSystem" internally, + * which is the method to override from now on. + * + **/ + virtual bool buildSystem(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) final + { + //TODO (SPRINT SED 2025): Component state mechanism + return doBuildSystem(cParams, res1, res2); + } /** * Rebuild the system using a mass and force factor. * Experimental API used to investigate convergence issues. */ - SOFA_ATTRIBUTE_DEPRECATED__REBUILDSYSTEM() virtual void rebuildSystem(SReal /*massfactor*/, SReal /*forceFactor*/){} + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doRebuildSystem" internally, + * which is the method to override from now on. + * + **/ + SOFA_ATTRIBUTE_DEPRECATED__REBUILDSYSTEM() virtual void rebuildSystem(SReal massFactor, SReal forceFactor) final + { + //TODO (SPRINT SED 2025): Component state mechanism + doRebuildSystem(massFactor, forceFactor); + } + /** * Use the system previously built and solve it with the appropriate algorithm */ - virtual bool solveSystem(const ConstraintParams *, MultiVecId res1, MultiVecId res2=MultiVecId::null())=0; + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doSolveSystem" internally, + * which is the method to override from now on. + * + **/ + virtual bool solveSystem(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) final + { + //TODO (SPRINT SED 2025): Component state mechanism + return doSolveSystem(cParams, res1, res2); + } /** * Correct the Mechanical State with the solution found */ - virtual bool applyCorrection(const ConstraintParams *, MultiVecId res1, MultiVecId res2=MultiVecId::null())=0; + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doApplyCorrection" internally, + * which is the method to override from now on. + * + **/ + virtual bool applyCorrection(const ConstraintParams *cParams, MultiVecId res1, MultiVecId res2=MultiVecId::null()) final + { + //TODO (SPRINT SED 2025): Component state mechanism + return doApplyCorrection(cParams, res1, res2); + } /// Compute the residual in the newton iterations due to the constraints forces /// i.e. compute Vecid::force() += J^t lambda /// the result is accumulated in Vecid::force() + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doCompureResidual" internally, + * which is the method to override from now on. + * + **/ SOFA_ATTRIBUTE_DEPRECATED__COMPUTERESIDUAL() - virtual void computeResidual(const core::ExecParams* /*params*/) + virtual void computeResidual(const core::ExecParams* cParams) final { - dmsg_error() << "ComputeResidual is not implemented in " << this->getName() ; + //TODO (SPRINT SED 2025): Component state mechanism + doComputeResidual(cParams); } /// @name Resolution DOFs vectors API @@ -112,7 +204,19 @@ class SOFA_CORE_API ConstraintSolver : public virtual objectmodel::BaseComponent /// Remove reference to ConstraintCorrection /// /// @param c is the ConstraintCorrection - virtual void removeConstraintCorrection(BaseConstraintCorrection *s) = 0; + /** + * !!! WARNING since v25.12 !!! + * + * The template method pattern has been applied to this part of the API. + * This method calls the newly introduced method "doRemoveConstraintCorrection" internally, + * which is the method to override from now on. + * + **/ + virtual void removeConstraintCorrection(BaseConstraintCorrection *s) final + { + //TODO (SPRINT SED 2025): Component state mechanism + doRemoveConstraintCorrection(s); + } bool insertInNode( objectmodel::BaseNode* node ) override; bool removeInNode( objectmodel::BaseNode* node ) override;