Skip to content

Commit 0a89f43

Browse files
authored
[MechanicalLoad] Implement buildStiffnessMatrix for TorsionForceField (#4115)
1 parent 2b472b8 commit 0a89f43

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/TorsionForceField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class TorsionForceField : public ForceField<DataTypes>
7777
void addForce(const MechanicalParams *, DataVecDeriv &f, const DataVecCoord &x, const DataVecDeriv &v) override;
7878
void addDForce(const MechanicalParams *mparams, DataVecDeriv &df, const DataVecDeriv &dx) override;
7979
void addKToMatrix(linearalgebra::BaseMatrix *matrix, SReal kFact, unsigned int &offset) override;
80+
void buildStiffnessMatrix(core::behavior::StiffnessMatrix* matrix) override;
8081
void buildDampingMatrix(core::behavior::DampingMatrix* /*matrix*/) final;
8182

8283
SReal getPotentialEnergy(const core::MechanicalParams* /*mparams*/, const DataVecCoord& /* x */) const override;

Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/TorsionForceField.inl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <sofa/linearalgebra/BaseMatrix.h>
2626
#include <sofa/linearalgebra/CompressedRowSparseMatrix.h>
2727
#include <sofa/core/MechanicalParams.h>
28+
#include <sofa/core/behavior/BaseLocalForceFieldMatrix.h>
29+
2830

2931
namespace sofa::component::mechanicalload
3032
{
@@ -118,6 +120,28 @@ void TorsionForceField<DataTypes>::addKToMatrix(linearalgebra::BaseMatrix* matri
118120
}
119121
}
120122

123+
template <typename DataTypes>
124+
void TorsionForceField<DataTypes>::buildStiffnessMatrix(core::behavior::StiffnessMatrix* matrix)
125+
{
126+
auto dfdx = matrix->getForceDerivativeIn(this->mstate)
127+
.withRespectToPositionsIn(this->mstate);
128+
129+
const VecId& indices = m_indices.getValue();
130+
const Real& tau = m_torque.getValue();
131+
132+
sofa::type::MatNoInit<3,3, Real> D;
133+
D(0,0) = 1 - m_u(0)*m_u(0) ; D(0,1) = -m_u(1)*m_u(0) ; D(0,2) = -m_u(2)*m_u(0);
134+
D(1,0) = -m_u(0)*m_u(1) ; D(1,1) = 1 - m_u(1)*m_u(1) ; D(1,2) = -m_u(2)*m_u(1);
135+
D(2,0) = -m_u(0)*m_u(2) ; D(2,1) = -m_u(1)*m_u(2) ; D(2,2) = 1 - m_u(3)*m_u(3);
136+
D *= tau;
137+
138+
for (const auto id : indices)
139+
{
140+
const unsigned int c = Deriv::total_size * id;
141+
dfdx(c, c) += D;
142+
}
143+
}
144+
121145
template <typename DataTypes>
122146
void TorsionForceField<DataTypes>::buildDampingMatrix(core::behavior::DampingMatrix*)
123147
{

0 commit comments

Comments
 (0)