diff --git a/Sofa/Component/MechanicalLoad/CMakeLists.txt b/Sofa/Component/MechanicalLoad/CMakeLists.txt index 6ba52e383ec..a80eb0e59fe 100644 --- a/Sofa/Component/MechanicalLoad/CMakeLists.txt +++ b/Sofa/Component/MechanicalLoad/CMakeLists.txt @@ -21,6 +21,8 @@ set(HEADER_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/InteractionEllipsoidForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.h ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.inl + ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/NodalLinearDampingForceField.h + ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/NodalLinearDampingForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/OscillatingTorsionPressureForceField.h ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/OscillatingTorsionPressureForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/PlaneForceField.h @@ -45,12 +47,12 @@ set(SOURCE_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/init.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/ConicalForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/ConstantForceField.cpp - ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/DiagonalVelocityDampingForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EdgePressureForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/Gravity.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/InteractionEllipsoidForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.cpp + ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/NodalLinearDampingForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/OscillatingTorsionPressureForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/PlaneForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/QuadPressureForceField.cpp @@ -59,7 +61,6 @@ set(SOURCE_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/TaitSurfacePressureForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/TorsionForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/TrianglePressureForceField.cpp - ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/UniformVelocityDampingForceField.cpp ) sofa_find_package(Sofa.Simulation.Core REQUIRED) diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.h index c39b41bcb8b..f7768787652 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.h +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.h @@ -20,65 +20,13 @@ * Contact information: contact@sofa-framework.org * ******************************************************************************/ #pragma once -#include -#include +#include -namespace sofa::component::mechanicalload -{ +SOFA_HEADER_DEPRECATED("v24.12", "v25.12", "sofa/component/mechanicalload/NodalLinearDampingForceField.h") -/// Apply damping forces to given degrees of freedom. -template -class DiagonalVelocityDampingForceField : public core::behavior::ForceField +namespace sofa::component::mechanicalload { -public: - SOFA_CLASS(SOFA_TEMPLATE(DiagonalVelocityDampingForceField, DataTypes), SOFA_TEMPLATE(core::behavior::ForceField, DataTypes)); - - typedef core::behavior::ForceField Inherit; - typedef typename DataTypes::VecCoord VecCoord; - typedef typename DataTypes::VecDeriv VecDeriv; - typedef typename DataTypes::Coord Coord; - typedef typename DataTypes::Deriv Deriv; - typedef typename Coord::value_type Real; - typedef type::vector VecIndex; - typedef core::objectmodel::Data DataVecCoord; - typedef core::objectmodel::Data DataVecDeriv; - - /// velocity damping coefficients (by cinematic dof) - Data< VecDeriv > d_dampingCoefficients; - - SOFA_ATTRIBUTE_DISABLED("v23.12", "v24.06", "This Data is now replaced by d_dampingCoefficients") - DeprecatedAndRemoved dampingCoefficients; - -protected: - - DiagonalVelocityDampingForceField(); - -public: - - void addForce (const core::MechanicalParams*, DataVecDeriv&, const DataVecCoord&, const DataVecDeriv&) override; - - void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) override; - - void addKToMatrix(sofa::linearalgebra::BaseMatrix * /*m*/, SReal /*kFactor*/, unsigned int &/*offset*/) override {} - void buildStiffnessMatrix(core::behavior::StiffnessMatrix* /* matrix */) override; - - void addBToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal bFact, unsigned int& offset) override; - void buildDampingMatrix(core::behavior::DampingMatrix* matrix) override; - - SReal getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const override; - -}; - - -#if !defined(SOFA_COMPONENT_FORCEFIELD_DIAGONALVELOCITYDAMPINGFORCEFIELD_CPP) -extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; - -#endif - -} // namespace sofa::component::mechanicalload +template +using DiagonalVelocityDampingForceField SOFA_ATTRIBUTE_DEPRECATED("v24.12 ", "v25.12", "DiagonalVelocityDampingForceField has been renamed to NodalLinearDampingForceField") = NodalLinearDampingForceField; +} diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.inl index 22f0e05d94c..7d6f9ae5fc0 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.inl @@ -21,147 +21,6 @@ ******************************************************************************/ #pragma once -#include -#include -#include +#include - -namespace sofa::component::mechanicalload -{ - -template -DiagonalVelocityDampingForceField::DiagonalVelocityDampingForceField() - : d_dampingCoefficients(initData(&d_dampingCoefficients, "dampingCoefficient", "velocity damping coefficients (by cinematic dof)")) -{ -} - - - -template -void DiagonalVelocityDampingForceField::addForce(const core::MechanicalParams*, DataVecDeriv&_f, const DataVecCoord&, const DataVecDeriv&_v) -{ - const auto coefs = sofa::helper::getReadAccessor(d_dampingCoefficients); - - if( !coefs.empty() ) - { - const std::size_t nbDampingCoeff = coefs.size(); - - sofa::helper::WriteAccessor f(_f); - const VecDeriv& v = _v.getValue(); - - for(std::size_t i = 0; i < v.size(); ++i) - { - const auto coefId = std::min(i, nbDampingCoeff-1); - for(unsigned j = 0; j < Deriv::total_size; ++j) - { - f[i][j] -= v[i][j] * coefs[coefId][j]; - } - } - } -} - -template -void DiagonalVelocityDampingForceField::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df, const DataVecDeriv& d_dx) -{ - const auto& coefs = d_dampingCoefficients.getValue(); - std::size_t nbDampingCoeff = coefs.size(); - const Real bfactor = (Real)mparams->bFactor(); - - if (nbDampingCoeff && bfactor) - { - sofa::helper::WriteAccessor df(d_df); - const VecDeriv& dx = d_dx.getValue(); - - for (std::size_t i = 0; i < dx.size(); i++) - { - for (unsigned j = 0; j < Deriv::total_size; j++) - { - if (i < nbDampingCoeff) - { - df[i][j] -= dx[i][j] * coefs[i][j] * bfactor; - } - else - { - df[i][j] -= dx[i][j] * coefs.back()[j] * bfactor; - } - } - } - } -} - -template -void DiagonalVelocityDampingForceField::buildStiffnessMatrix(core::behavior::StiffnessMatrix*) -{ - // DiagonalVelocityDampingForceField is a pure damping component: stiffness is not computed -} - -template -void DiagonalVelocityDampingForceField::addBToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal bFact, unsigned int& offset) -{ - const auto& coefs = d_dampingCoefficients.getValue(); - const std::size_t nbDampingCoeff = coefs.size(); - - if (!nbDampingCoeff) - { - return; - } - - const unsigned int size = this->mstate->getSize(); - for (std::size_t i = 0; i < size; i++) - { - const unsigned blockrow = offset + i * Deriv::total_size; - for (unsigned j = 0; j < Deriv::total_size; j++) - { - unsigned row = blockrow + j; - if (i < nbDampingCoeff) - { - mat->add(row, row, -coefs[i][j] * bFact); - } - else - { - mat->add(row, row, -coefs.back()[j] * bFact); - } - } - } -} - -template -void DiagonalVelocityDampingForceField::buildDampingMatrix(core::behavior::DampingMatrix* matrix) -{ - const auto& coefs = d_dampingCoefficients.getValue(); - const std::size_t nbDampingCoeff = coefs.size(); - - if (!nbDampingCoeff) - { - return; - } - - auto dfdv = matrix->getForceDerivativeIn(this->mstate) - .withRespectToVelocityIn(this->mstate); - - const unsigned int size = this->mstate->getSize(); - for (std::size_t i = 0; i < size; i++) - { - const unsigned blockrow = i * Deriv::total_size; - for (unsigned j = 0; j < Deriv::total_size; j++) - { - const unsigned row = blockrow + j; - if (i < nbDampingCoeff) - { - dfdv(row, row) += -coefs[i][j]; - } - else - { - dfdv(row, row) += -coefs.back()[j]; - } - } - } -} - -template -SReal DiagonalVelocityDampingForceField::getPotentialEnergy(const core::MechanicalParams*, const DataVecCoord&) const -{ - return 0; -} - -} // namespace sofa::component::mechanicalload +SOFA_HEADER_DEPRECATED("v24.12", "v25.12", "sofa/component/mechanicalload/NodalLinearDampingForceField.inl") diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.cpp b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.cpp similarity index 62% rename from Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.cpp rename to Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.cpp index ff44e31cd8c..91fa99e12cb 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/DiagonalVelocityDampingForceField.cpp +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.cpp @@ -19,9 +19,9 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ -#define SOFA_COMPONENT_FORCEFIELD_DIAGONALVELOCITYDAMPINGFORCEFIELD_CPP +#define SOFA_COMPONENT_FORCEFIELD_NODALLINEARDAMPINGFORCEFIELD_CPP -#include +#include #include #include #include @@ -31,21 +31,21 @@ namespace sofa::component::mechanicalload using namespace sofa::defaulttype; -int DiagonalVelocityDampingForceFieldClass = core::RegisterObject("Diagonal velocity damping") -.add< DiagonalVelocityDampingForceField >() -.add< DiagonalVelocityDampingForceField >() -.add< DiagonalVelocityDampingForceField >() -.add< DiagonalVelocityDampingForceField >() -.add< DiagonalVelocityDampingForceField >() -.add< DiagonalVelocityDampingForceField >() +int NodalLinearDampingForceFieldClass = core::RegisterObject("Linear damping force applied on the degrees of freedom") +.add< NodalLinearDampingForceField >() +.add< NodalLinearDampingForceField >() +.add< NodalLinearDampingForceField >() +.add< NodalLinearDampingForceField >() +.add< NodalLinearDampingForceField >() +.add< NodalLinearDampingForceField >() ; -template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; } // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.h new file mode 100644 index 00000000000..2e3760c4092 --- /dev/null +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.h @@ -0,0 +1,103 @@ +/****************************************************************************** +* 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 . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include +#include + +namespace sofa::component::mechanicalload +{ + +/// Apply damping forces to given degrees of freedom +template +class NodalLinearDampingForceField : public core::behavior::ForceField +{ +public: + SOFA_CLASS(SOFA_TEMPLATE(NodalLinearDampingForceField, DataTypes), SOFA_TEMPLATE(core::behavior::ForceField, DataTypes)); + + typedef core::behavior::ForceField Inherit; + typedef typename DataTypes::VecCoord VecCoord; + typedef typename DataTypes::VecDeriv VecDeriv; + typedef typename DataTypes::Coord Coord; + typedef typename DataTypes::Deriv Deriv; + typedef typename Coord::value_type Real; + typedef type::vector VecIndex; + typedef core::objectmodel::Data DataVecCoord; + typedef core::objectmodel::Data DataVecDeriv; + + /// Velocity damping coefficients (by cinematic dof) + Data< VecDeriv > d_dampingCoefficients; + +protected: + + NodalLinearDampingForceField(); + + sofa::core::objectmodel::ComponentState updateFromDampingCoefficient(); + +public: + + /// Use parse to allow isotropic and anisotropic damping + void parse ( core::objectmodel::BaseObjectDescription* arg ) override + { + Inherit::parse(arg); + + if (arg->getAttribute("dampingCoefficient") != nullptr) + { + Real isotropicDamping = (Real)arg->getAttributeAsFloat("dampingCoefficient", -1.0); + if(isotropicDamping != -1.0) + { + VecDeriv coef; + coef.resize(1); + + const typename DataTypes::DPos& dpos = DataTypes::getDPos(coef[0]); + for(Size j=0 ; j; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField; + +#endif + +} // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.inl new file mode 100644 index 00000000000..6032ef24ece --- /dev/null +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/NodalLinearDampingForceField.inl @@ -0,0 +1,216 @@ +/****************************************************************************** +* 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 . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include + +#include +#include + +namespace sofa::component::mechanicalload +{ + + +template +NodalLinearDampingForceField::NodalLinearDampingForceField() + : d_dampingCoefficients(initData(&d_dampingCoefficients, "dampingCoefficient", "Velocity damping coefficients (by cinematic dof)")) +{ + sofa::core::objectmodel::Base::addUpdateCallback("updateFromDampingCoefficient", {&d_dampingCoefficients}, [this](const core::DataTracker& ) + { + msg_info() << "call back update: from dampingCoefficient"; + return updateFromDampingCoefficient(); + }, {}); +} + + +template +sofa::core::objectmodel::ComponentState NodalLinearDampingForceField::updateFromDampingCoefficient() +{ + const auto coefs = sofa::helper::getReadAccessor(d_dampingCoefficients); + const unsigned int sizeCoefs = coefs.size(); + + + // Check if the dampingCoefficients vector has a null size + if(sizeCoefs == 0) + { + msg_error() << "Size of the \'dampingCoefficients\' vector is null"; + return sofa::core::objectmodel::ComponentState::Invalid; + } + + // Recover mstate size + const unsigned int sizeMState = this->mstate->getSize(); + + + // If the sizes of the dampingCoefficients vector and the mechanical state mismatch + if(sizeCoefs != sizeMState && sizeCoefs != 1) + { + msg_error() << "Size of the \'dampingCoefficients\' vector does not fit the associated MechanicalObject size"; + return sofa::core::objectmodel::ComponentState::Invalid; + } + + + // If size=1, make a constant vector which size fits the MechanicalObject + if(sizeCoefs == 1) + { + sofa::helper::WriteAccessor > accessorCoefficient = this->d_dampingCoefficients; + Deriv constantCoef = accessorCoefficient[0]; + accessorCoefficient.resize(sizeMState); + + for(unsigned j = 0; j < Deriv::total_size; ++j) + { + if(constantCoef[j] < 0.) + { + msg_error() << "Negative \'dampingCoefficients\' given"; + return sofa::core::objectmodel::ComponentState::Invalid; + } + } + + for (unsigned i = 0; i < sizeMState; ++i) + { + accessorCoefficient[i] = constantCoef; + } + } + else + { + for (unsigned i = 0; i < sizeMState; ++i) + { + for(unsigned j = 0; j < Deriv::total_size; ++j) + { + if(coefs[i][j] < 0.) + { + msg_error() << "Negative value at the " << i << "th entry of the \'dampingCoefficients\' vector"; + return sofa::core::objectmodel::ComponentState::Invalid; + } + } + } + } + + msg_info() << "Update from dampingCoefficient successfully done"; + return sofa::core::objectmodel::ComponentState::Valid; +} + + +template +void NodalLinearDampingForceField::addForce(const core::MechanicalParams*, DataVecDeriv&_f, const DataVecCoord& _x, const DataVecDeriv&_v) +{ + SOFA_UNUSED(_x); + const auto coefs = sofa::helper::getReadAccessor(d_dampingCoefficients); + + if( !coefs.empty() ) + { + const std::size_t nbDampingCoeff = coefs.size(); + + sofa::helper::WriteAccessor f(_f); + const VecDeriv& v = _v.getValue(); + + for(std::size_t i = 0; i < v.size(); ++i) + { + const auto coefId = std::min(i, nbDampingCoeff-1); + for(unsigned j = 0; j < Deriv::total_size; ++j) + { + f[i][j] -= v[i][j] * coefs[coefId][j]; + } + } + } +} + +template +void NodalLinearDampingForceField::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df, const DataVecDeriv& d_dx) +{ + const auto& coefs = d_dampingCoefficients.getValue(); + std::size_t nbDampingCoeff = coefs.size(); + const Real bfactor = (Real)mparams->bFactor(); + + if (nbDampingCoeff && bfactor) + { + sofa::helper::WriteAccessor df(d_df); + const VecDeriv& dx = d_dx.getValue(); + + for (std::size_t i = 0; i < dx.size(); i++) + { + for (unsigned j = 0; j < Deriv::total_size; j++) + { + df[i][j] -= dx[i][j] * coefs[i][j] * bfactor; + } + } + } +} + +template +void NodalLinearDampingForceField::buildStiffnessMatrix(core::behavior::StiffnessMatrix*) +{ + // NodalLinearDampingForceField is a pure damping component + // No stiffness is computed +} + +template +void NodalLinearDampingForceField::addBToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal bFact, unsigned int& offset) +{ + const auto& coefs = d_dampingCoefficients.getValue(); + const std::size_t nbDampingCoeff = coefs.size(); + + if (nbDampingCoeff && bFact) + { + const unsigned int size = this->mstate->getSize(); + for (std::size_t i = 0; i < size; i++) + { + const unsigned blockrow = offset + i * Deriv::total_size; + for (unsigned j = 0; j < Deriv::total_size; j++) + { + unsigned row = blockrow + j; + mat->add(row, row, -coefs[i][j] * bFact); + } + } + } +} + +template +void NodalLinearDampingForceField::buildDampingMatrix(core::behavior::DampingMatrix* matrix) +{ + const auto& coefs = d_dampingCoefficients.getValue(); + const std::size_t nbDampingCoeff = coefs.size(); + + if (nbDampingCoeff) + { + auto dfdv = matrix->getForceDerivativeIn(this->mstate) + .withRespectToVelocityIn(this->mstate); + + const unsigned int size = this->mstate->getSize(); + for (std::size_t i = 0; i < size; i++) + { + const unsigned blockrow = i * Deriv::total_size; + for (unsigned j = 0; j < Deriv::total_size; j++) + { + const unsigned row = blockrow + j; + dfdv(row, row) += -coefs[i][j]; + } + } + } +} + +template +SReal NodalLinearDampingForceField::getPotentialEnergy(const core::MechanicalParams*, const DataVecCoord&) const +{ + return 0; +} + +} // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.cpp b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.cpp deleted file mode 100644 index c53d069fe1e..00000000000 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/****************************************************************************** -* 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 . * -******************************************************************************* -* Authors: The SOFA Team and external contributors (see Authors.txt) * -* * -* Contact information: contact@sofa-framework.org * -******************************************************************************/ -#define SOFA_COMPONENT_FORCEFIELD_UNIFORMVELOCITYDAMPINGFORCEFIELD_CPP - -#include -#include -#include -#include - -namespace sofa::component::mechanicalload -{ - -using namespace sofa::defaulttype; - -int UniformVelocityDampingForceFieldClass = core::RegisterObject("Uniform velocity damping") -.add< UniformVelocityDampingForceField >() -.add< UniformVelocityDampingForceField >() -.add< UniformVelocityDampingForceField >() -.add< UniformVelocityDampingForceField >() -.add< UniformVelocityDampingForceField >() -.add< UniformVelocityDampingForceField >() - -; - - - template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - - -} // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.h index b00593672e4..7acf422cd06 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.h +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.h @@ -20,69 +20,13 @@ * Contact information: contact@sofa-framework.org * ******************************************************************************/ #pragma once -#include -#include +#include -namespace sofa::component::mechanicalload -{ +SOFA_HEADER_DEPRECATED("v24.12", "v25.12", "sofa/component/mechanicalload/NodalLinearDampingForceField.h") -/// Apply damping forces to given degrees of freedom. -template -class UniformVelocityDampingForceField : public core::behavior::ForceField +namespace sofa::component::mechanicalload { - -public: - - SOFA_CLASS(SOFA_TEMPLATE(UniformVelocityDampingForceField, DataTypes), SOFA_TEMPLATE(core::behavior::ForceField, DataTypes)); - - typedef core::behavior::ForceField Inherit; - typedef typename DataTypes::VecCoord VecCoord; - typedef typename DataTypes::VecDeriv VecDeriv; - typedef typename DataTypes::Coord Coord; - typedef typename DataTypes::Deriv Deriv; - typedef typename Coord::value_type Real; - typedef type::vector VecIndex; - typedef core::objectmodel::Data DataVecCoord; - typedef core::objectmodel::Data DataVecDeriv; - - SOFA_ATTRIBUTE_DEPRECATED__RENAME_DATA_IN_MECHANICALLOAD() - Data< Real > dampingCoefficient; - - /// air drag coefficient. - Data< Real > d_dampingCoefficient; - Data d_implicit; ///< should it generate damping matrix df/dv? (explicit otherwise, i.e. only generating a force) - -protected: - - UniformVelocityDampingForceField(); - -public: - - void addForce (const core::MechanicalParams*, DataVecDeriv&, const DataVecCoord&, const DataVecDeriv&) override; - - void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) override; - - void addKToMatrix(sofa::linearalgebra::BaseMatrix *, SReal, unsigned int &) override {} - - void addBToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal bFact, unsigned int& offset) override; - void buildDampingMatrix(core::behavior::DampingMatrix* matrix) override; - - SReal getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const override; - - -}; - - - -#if !defined(SOFA_COMPONENT_FORCEFIELD_UNIFORMVELOCITYDAMPINGFORCEFIELD_CPP) -extern template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API UniformVelocityDampingForceField; - -#endif - -} // namespace sofa::component::mechanicalload +template +using UniformVelocityDampingForceField SOFA_ATTRIBUTE_DEPRECATED("v24.12 ", "v25.12", "UniformVelocityDampingForceField has been removed. Use instead NodalLinearDampingForceField") = NodalLinearDampingForceField; +} diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.inl index 1a7faa93898..7d6f9ae5fc0 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/UniformVelocityDampingForceField.inl @@ -21,91 +21,6 @@ ******************************************************************************/ #pragma once -#include -#include -#include -#include +#include -namespace sofa::component::mechanicalload -{ - - -template -UniformVelocityDampingForceField::UniformVelocityDampingForceField() - : d_dampingCoefficient(initData(&d_dampingCoefficient, Real(0.1), "dampingCoefficient", "velocity damping coefficient")) - , d_implicit(initData(&d_implicit, false, "implicit", "should it generate damping matrix df/dv? (explicit otherwise, i.e. only generating a force)")) -{ - core::objectmodel::Base::addAlias(&d_dampingCoefficient, "damping" ); - dampingCoefficient.setParent(&d_dampingCoefficient); -} - -template -void UniformVelocityDampingForceField::addForce (const core::MechanicalParams*, DataVecDeriv&_f, const DataVecCoord&, const DataVecDeriv&_v) -{ - sofa::helper::WriteAccessor f(_f); - const VecDeriv& v = _v.getValue(); - - for(unsigned int i=0; i -void UniformVelocityDampingForceField::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) -{ - (void)mparams->kFactor(); // get rid of warning message - - if( !d_implicit.getValue() ) return; - - Real bfactor = (Real)sofa::core::mechanicalparams::bFactor(mparams); - - if( bfactor ) - { - sofa::helper::WriteAccessor df(d_df); - const VecDeriv& dx = d_dx.getValue(); - - bfactor *= d_dampingCoefficient.getValue(); - - for(unsigned int i=0; i -void UniformVelocityDampingForceField::addBToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal bFact, unsigned int& offset) -{ - if( !d_implicit.getValue() ) return; - - const sofa::Size size = this->mstate->getMatrixSize(); - const auto dampingContribution = -d_dampingCoefficient.getValue() * bFact; - - for( sofa::Size i = 0 ; i < size; ++i ) - { - mat->add( offset + i, offset + i, dampingContribution ); - } -} - -template -void UniformVelocityDampingForceField::buildDampingMatrix(core::behavior::DampingMatrix* matrix) -{ - if( !d_implicit.getValue() ) return; - - auto dfdv = matrix->getForceDerivativeIn(this->mstate) - .withRespectToVelocityIn(this->mstate); - - const sofa::Size size = this->mstate->getMatrixSize(); - const auto damping = sofa::helper::ReadAccessor(d_dampingCoefficient); - for( sofa::Size i = 0; i < size; ++i) - { - dfdv(i, i) += -damping.ref(); - } -} - -template -SReal UniformVelocityDampingForceField::getPotentialEnergy(const core::MechanicalParams*, const DataVecCoord&) const -{ - // TODO - return 0; -} - - -} // namespace sofa::component::mechanicalload +SOFA_HEADER_DEPRECATED("v24.12", "v25.12", "sofa/component/mechanicalload/NodalLinearDampingForceField.inl") diff --git a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp index 6466fcac7df..3c537f666a2 100644 --- a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp @@ -29,7 +29,8 @@ const std::map > deprecatedComponents = { {"RayTraceDetection", Deprecated("v21.06", "v21.12")}, {"BruteForceDetection", Deprecated("v21.06", "v21.12")}, {"DirectSAP", Deprecated("v21.06", "v21.12")}, - {"RigidRigidMapping", Deprecated("v23.06", "v23.12", "You can use the component RigidMapping with template='Rigid3,Rigid3' instead.")}, + {"RigidRigidMapping", Deprecated("v23.06", "v23.12", "You can use the component RigidMapping with template='Rigid3,Rigid3' instead")}, + {"UniformVelocityDampingForceField", Deprecated("v24.12", "v25.06", "You can use the NodalLinearDampingForceField instead")}, }; const std::map > movedComponents = { @@ -759,7 +760,8 @@ const std::map< std::string, Renamed, std::less<> > renamedComponents = { {"UniformConstraint", Renamed("v24.06","v25.06","UniformLagrangianConstraint")}, {"UnilateralInteractionConstraint", Renamed("v24.06","v25.06","UnilateralLagrangianConstraint")}, {"StiffSpringForceField", Renamed("v24.06","v25.06","SpringForceField")}, - {"ParallelStiffSpringForceField", Renamed("v24.06","v25.06","ParallelSpringForceField")} + {"ParallelStiffSpringForceField", Renamed("v24.06","v25.06","ParallelSpringForceField")}, + {"DiagonalVelocityDampingForceField", Renamed("v24.12","v25.12","NodalLinearDampingForceField")} };