Skip to content
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

[MechanicalLoad] Factorize damping forcefields #4920

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sofa/Component/MechanicalLoad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,13 @@
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/component/mechanicalload/config.h>

#include <sofa/core/behavior/ForceField.h>
#include <sofa/component/mechanicalload/NodalLinearDampingForceField.h>

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 DataTypes>
class DiagonalVelocityDampingForceField : public core::behavior::ForceField<DataTypes>
namespace sofa::component::mechanicalload
{
public:
SOFA_CLASS(SOFA_TEMPLATE(DiagonalVelocityDampingForceField, DataTypes), SOFA_TEMPLATE(core::behavior::ForceField, DataTypes));

typedef core::behavior::ForceField<DataTypes> 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<unsigned int> VecIndex;
typedef core::objectmodel::Data<VecCoord> DataVecCoord;
typedef core::objectmodel::Data<VecDeriv> 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<defaulttype::Vec3Types>;
extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<defaulttype::Vec2Types>;
extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<defaulttype::Vec1Types>;
extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<defaulttype::Vec6Types>;
extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<defaulttype::Rigid3Types>;
extern template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<defaulttype::Rigid2Types>;

#endif

} // namespace sofa::component::mechanicalload
template<class T>
using DiagonalVelocityDampingForceField SOFA_ATTRIBUTE_DEPRECATED("v24.12 ", "v25.12", "DiagonalVelocityDampingForceField has been renamed to NodalLinearDampingForceField") = NodalLinearDampingForceField<T>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,147 +21,6 @@
******************************************************************************/
#pragma once

#include <sofa/component/mechanicalload/DiagonalVelocityDampingForceField.h>
#include <sofa/core/MechanicalParams.h>
#include <sofa/core/behavior/BaseLocalForceFieldMatrix.h>
#include <sofa/component/mechanicalload/NodalLinearDampingForceField.inl>


namespace sofa::component::mechanicalload
{

template<class DataTypes>
DiagonalVelocityDampingForceField<DataTypes>::DiagonalVelocityDampingForceField()
: d_dampingCoefficients(initData(&d_dampingCoefficients, "dampingCoefficient", "velocity damping coefficients (by cinematic dof)"))
{
}



template<class DataTypes>
void DiagonalVelocityDampingForceField<DataTypes>::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<DataVecDeriv> 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<class DataTypes>
void DiagonalVelocityDampingForceField<DataTypes>::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<DataVecDeriv> 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 <class DataTypes>
void DiagonalVelocityDampingForceField<DataTypes>::buildStiffnessMatrix(core::behavior::StiffnessMatrix*)
{
// DiagonalVelocityDampingForceField is a pure damping component: stiffness is not computed
}

template<class DataTypes>
void DiagonalVelocityDampingForceField<DataTypes>::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 <class DataTypes>
void DiagonalVelocityDampingForceField<DataTypes>::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 <class DataTypes>
SReal DiagonalVelocityDampingForceField<DataTypes>::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")
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* *
* Contact information: [email protected] *
******************************************************************************/
#define SOFA_COMPONENT_FORCEFIELD_DIAGONALVELOCITYDAMPINGFORCEFIELD_CPP
#define SOFA_COMPONENT_FORCEFIELD_NODALLINEARDAMPINGFORCEFIELD_CPP

#include <sofa/component/mechanicalload/DiagonalVelocityDampingForceField.inl>
#include <sofa/component/mechanicalload/NodalLinearDampingForceField.inl>
#include <sofa/core/ObjectFactory.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/defaulttype/RigidTypes.h>
Expand All @@ -31,21 +31,21 @@ namespace sofa::component::mechanicalload

using namespace sofa::defaulttype;

int DiagonalVelocityDampingForceFieldClass = core::RegisterObject("Diagonal velocity damping")
.add< DiagonalVelocityDampingForceField<Vec3Types> >()
.add< DiagonalVelocityDampingForceField<Vec2Types> >()
.add< DiagonalVelocityDampingForceField<Vec1Types> >()
.add< DiagonalVelocityDampingForceField<Vec6Types> >()
.add< DiagonalVelocityDampingForceField<Rigid3Types> >()
.add< DiagonalVelocityDampingForceField<Rigid2Types> >()
int NodalLinearDampingForceFieldClass = core::RegisterObject("Linear damping force applied on the degrees of freedom")
.add< NodalLinearDampingForceField<Vec3Types> >()
.add< NodalLinearDampingForceField<Vec2Types> >()
.add< NodalLinearDampingForceField<Vec1Types> >()
.add< NodalLinearDampingForceField<Vec6Types> >()
.add< NodalLinearDampingForceField<Rigid3Types> >()
.add< NodalLinearDampingForceField<Rigid2Types> >()

;

template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<Vec3Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<Vec2Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<Vec1Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<Vec6Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<Rigid3Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API DiagonalVelocityDampingForceField<Rigid2Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField<Vec3Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField<Vec2Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField<Vec1Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField<Vec6Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField<Rigid3Types>;
template class SOFA_COMPONENT_MECHANICALLOAD_API NodalLinearDampingForceField<Rigid2Types>;

} // namespace sofa::component::mechanicalload
Loading
Loading