From 02a2ff85d8c21ea58ed0a08bc81c43d7cc279566 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 13 May 2022 22:15:06 +0200 Subject: [PATCH 01/41] First commit --- .../src/sofa/component/mass/DiagonalMass.h | 8 +- .../src/sofa/component/mass/DiagonalMass.inl | 46 +------ .../src/sofa/component/mass/MeshMatrixMass.h | 4 +- .../sofa/component/mass/MeshMatrixMass.inl | 38 +----- .../src/sofa/component/mass/UniformMass.h | 4 +- .../src/sofa/component/mass/UniformMass.inl | 45 +------ Sofa/Component/MechanicalLoad/CMakeLists.txt | 3 + .../mechanicalload/GravityForceField.cpp} | 41 ++++-- .../mechanicalload/GravityForceField.h | 88 +++++++++++++ .../mechanicalload/GravityForceField.inl | 124 ++++++++++++++++++ .../odesolver/forward/EulerSolver.cpp | 10 -- .../component/odesolver/forward/EulerSolver.h | 4 - .../elastic/HexahedralFEMForceFieldAndMass.h | 9 +- .../HexahedralFEMForceFieldAndMass.inl | 22 ---- .../elastic/HexahedronFEMForceFieldAndMass.h | 15 +-- .../HexahedronFEMForceFieldAndMass.inl | 21 --- ...NonUniformHexahedronFEMForceFieldAndMass.h | 1 - ...nUniformHexahedronFEMForceFieldAndMass.inl | 24 ---- .../Core/src/sofa/core/behavior/BaseMass.cpp | 3 +- .../Core/src/sofa/core/behavior/BaseMass.h | 17 +-- .../Core/src/sofa/core/behavior/Mass.h | 39 +++--- .../Core/src/sofa/core/behavior/Mass.inl | 75 ++++------- Sofa/framework/Simulation/Core/CMakeLists.txt | 2 - .../MechanicalAddSeparateGravityVisitor.h | 59 --------- 24 files changed, 319 insertions(+), 383 deletions(-) rename Sofa/{framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.cpp => Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp} (57%) create mode 100644 Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h create mode 100644 Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl delete mode 100644 Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.h diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h index e8b901d3bba..e3c72f97a04 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h @@ -303,16 +303,14 @@ class DiagonalMass : public core::behavior::Mass void accFromF(const core::MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f) override; - void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; - SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& v) const override; ///< vMv/2 using dof->getV() override + SReal getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const override; ///< Mgx potential in a uniform gravity field, null at origin - SReal getPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x) const override; ///< Mgx potential in a uniform gravity field, null at origin + SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& v) const override; ///< vMv/2 using dof->getV() override type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override - void addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) override; - /// Add Mass contribution to global Matrix assembling void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl index 31e270e7aac..0c02fd57105 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl @@ -589,19 +589,16 @@ SReal DiagonalMass::getKineticEnergy( const core::M } template -SReal DiagonalMass::getPotentialEnergy( const core::MechanicalParams* /*mparams*/, const DataVecCoord& x ) const +SReal DiagonalMass::getGravitationalPotentialEnergy( const core::MechanicalParams* /*mparams*/, const DataVecCoord& x, const Deriv& gravity ) const { const MassVector &masses= d_vertexMass.getValue(); helper::ReadAccessor< DataVecCoord > _x = x; SReal e = 0; - // gravity - type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); + for (unsigned int i=0; i::doUpdateInternal() printMass(); } - -template -void DiagonalMass::addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) -{ - if(mparams) - { - VecDeriv& v = *d_v.beginEdit(); - // gravity - sofa::type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); - Deriv hg = theGravity * typename DataTypes::Real(sofa::core::mechanicalparams::dt(mparams)); - - for (unsigned int i=0; i -void DiagonalMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& f, const DataVecCoord& , const DataVecDeriv& ) +void DiagonalMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { - //if gravity was added separately (in solver's "solve" method), then nothing to do here - if(this->m_separateGravity.getValue()) - return; + SOFA_UNUSED(x); + SOFA_UNUSED(v); const MassVector &masses= d_vertexMass.getValue(); helper::WriteOnlyAccessor< DataVecDeriv > _f = f; - // gravity - sofa::type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); - - // add weight and inertia force for (unsigned int i=0; i SReal getKineticEnergy(const core::MechanicalParams*, const DataVecDeriv& v) const override; ///< vMv/2 using dof->getV() override - SReal getPotentialEnergy(const core::MechanicalParams*, const DataVecCoord& x) const override; ///< Mgx potential in a uniform gravity field, null at origin + SReal getGravitationalPotentialEnergy(const core::MechanicalParams*, const DataVecCoord& x, const Deriv& gravity) const override; ///< Mgx potential in a uniform gravity field, null at origin type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)) override - void addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) override; - bool isDiagonal() const override { return isLumped(); } diff --git a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl index f2678649300..7692593d1a1 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl @@ -2062,11 +2062,6 @@ void MeshMatrixMass::accFromF(const core::Mechanica template void MeshMatrixMass::addForce(const core::MechanicalParams*, DataVecDeriv& vf, const DataVecCoord& , const DataVecDeriv& ) { - - //if gravity was added separately (in solver's "solve" method), then nothing to do here - if(this->m_separateGravity.getValue()) - return ; - helper::WriteAccessor< DataVecDeriv > f = vf; const auto &vertexMass= d_vertexMass.getValue(); @@ -2114,20 +2109,15 @@ SReal MeshMatrixMass::getKineticEnergy( const core: template -SReal MeshMatrixMass::getPotentialEnergy( const core::MechanicalParams*, const DataVecCoord& vx) const +SReal MeshMatrixMass::getPotentialEnergy( const core::MechanicalParams*, const DataVecCoord& vx, const Deriv& gravity) const { const auto &vertexMass= d_vertexMass.getValue(); helper::ReadAccessor< DataVecCoord > x = vx; - SReal e = 0; - // gravity - type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); for (unsigned int i=0; i::getMomentum ( const c } - -template -void MeshMatrixMass::addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) -{ - if(this->mstate && mparams) - { - VecDeriv& v = *d_v.beginEdit(); - - // gravity - type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); - Deriv hg = theGravity * (typename DataTypes::Real(sofa::core::mechanicalparams::dt(mparams))); - - for (unsigned int i=0; i void MeshMatrixMass::addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) { diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index 55e4a6582bf..c33b2be4f83 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -136,13 +136,11 @@ class UniformMass : public core::behavior::Mass void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& d_v) const override; ///< vMv/2 using dof->getV() override - SReal getPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x) const override; ///< Mgx potential in a uniform gravity field, null at origin + SReal getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const override; ///< Mgx potential in a uniform gravity field, null at origin type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset); - void addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) override; - void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; /// Add Mass contribution to global Matrix assembling SReal getElementMass(sofa::Index index) const override; diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl index 321cb32ebc2..883216e68d9 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl @@ -424,38 +424,9 @@ void UniformMass::addMDxToVector ( BaseVector * resVect, SOFA_UNUSED(offset); } - -template -void UniformMass::addGravityToV(const MechanicalParams* mparams, - DataVecDeriv& d_v) -{ - if (mparams) - { - VecDeriv& v = *d_v.beginEdit(); - - const SReal* g = getContext()->getGravity().ptr(); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2] ); - Deriv hg = theGravity * Real(sofa::core::mechanicalparams::dt(mparams)); - - dmsg_info()<< " addGravityToV hg = "< void UniformMass::addForce ( const core::MechanicalParams*, DataVecDeriv& vf, const DataVecCoord& /*x*/, const DataVecDeriv& /*v*/ ) { - //if gravity was added separately (in solver's "solve" method), then nothing to do here - if ( this->m_separateGravity.getValue() ) - return; - helper::WriteAccessor f = vf; // weight @@ -471,12 +442,7 @@ void UniformMass::addForce ( const core::MechanicalParams*, DataVecDe ReadAccessor > indices = d_indices; - - // add weight and inertia force - if (this->m_separateGravity.getValue()) for ( unsigned int i=0; i::getKineticEnergy ( const MechanicalParams* params, } template -SReal UniformMass::getPotentialEnergy ( const MechanicalParams* params, - const DataVecCoord& d_x ) const +SReal UniformMass::getGravitationalPotentialEnergy ( const MechanicalParams* params, + const DataVecCoord& d_x, + const Deriv& gravity) const { SOFA_UNUSED(params); ReadAccessor x = d_x; @@ -511,10 +478,6 @@ SReal UniformMass::getPotentialEnergy ( const MechanicalParams* param SReal e = 0; const MassType& m = d_vertexMass.getValue(); - Vec3d g( getContext()->getGravity()); - Deriv gravity; - DataTypes::set(gravity, g[0], g[1], g[2]); - Deriv mg = gravity * m; for ( unsigned int i=0; i +#include +#include -#include +#include +#include -namespace sofa::simulation::mechanicalvisitor +namespace sofa::component::mechanicalload { -Visitor::Result MechanicalAddSeparateGravityVisitor::fwdMass(simulation::Node* /*node*/, core::behavior::BaseMass* mass) -{ - if( mass->m_separateGravity.getValue() ) - { - // - mass->addGravityToV(this->mparams, res); - } - return RESULT_CONTINUE; -} - -} \ No newline at end of file +using namespace sofa::defaulttype; + + +int GravityForceFieldClass = core::RegisterObject("Gravity as an external force in the world coordinate system") + .add< GravityForceField >() + .add< GravityForceField >() + .add< GravityForceField >() + .add< GravityForceField >() + .add< GravityForceField >() + .add< GravityForceField >() + + ; +template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; + + +} // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h new file mode 100644 index 00000000000..bb97cd9a8e7 --- /dev/null +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h @@ -0,0 +1,88 @@ +/****************************************************************************** +* 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 +#include + +namespace sofa::component::mechanicalload +{ + +/// ForceField applying the external force due to the gravitational acceleration. +/// This class requires a link towards a Mass to compute the space integration with the mass density. +template +class GravityForceField : public core::behavior::ForceField +{ +public: + SOFA_CLASS(SOFA_TEMPLATE(GravityForceField, 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 core::objectmodel::Data DataVecCoord; + typedef core::objectmodel::Data DataVecDeriv; + + Data< Deriv > d_gravitationalAcceleration; ///< Value corresponding to the gravitational acceleration + SingleLink, sofa::core::behavior::Mass, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_mass; ///< Link to be set to the mass in the component graph + + /// Init function + void init() override; + + /// Add the external forces due to the gravitational acceleration. + void addForce (const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; + + /// Gravity force has null variation + void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) override; + + /// Gravity force has null variation + void addKToMatrix(sofa::linearalgebra::BaseMatrix *mat, SReal k, unsigned int &offset) override; + + /// Constant force has null variation + using Inherit::addKToMatrix; + virtual void addKToMatrix(const sofa::core::behavior::MultiMatrixAccessor* matrix, SReal kFact) ; + + SReal getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const override; + + /// Set the gravitational acceleration + void setGravitationalAcceleration(const Real grav); + +protected: + GravityForceField(); +}; + + +#if !defined(SOFA_COMPONENT_FORCEFIELD_GRAVITYFORCEFIELD_CPP) +extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +#endif + +} // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl new file mode 100644 index 00000000000..89eb237e743 --- /dev/null +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl @@ -0,0 +1,124 @@ +/****************************************************************************** +* 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 +{ + +using namespace sofa::type; + +template +GravityForceField::GravityForceField() + : d_gravitationalAcceleration(initData(&d_gravitationalAcceleration, "gravitationalAcceleration", "Value corresponding to the gravitational acceleration")) + , l_mass(initLink("mass", "link to the mass")) +{ +} + + +template +void GravityForceField::init() +{ + this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); + + if (l_mass.empty()) + { + msg_info() << "link to the mass should be set to ensure right behavior. First mass found in current context will be used."; + l_mass.set(this->getContext()->getMass()); + } + + // temprory pointer to the mass + if (sofa::core::behavior::Mass* _mass = l_mass.get()) + { + msg_info() << "Mass path used: '" << l_mass.getLinkedPath() << "'"; + } + else + { + msg_error() << "No mass component found at path: " << l_mass.getLinkedPath() << ", nor in current context: " << this->getContext()->name; + return; + } + + // init from ForceField + Inherit::init(); + + // if all init passes, component is valid + this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); +} + + +template +void GravityForceField::setGravitationalAcceleration(const Real grav) +{ + d_gravitationalAcceleration.setValue(grav); +} + + +template +void GravityForceField::addForce(const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x1, const DataVecDeriv& v1) +{ + sofa::core::behavior::Mass* _mass = l_mass.get(); + _mass->addGravitationalForce(params,f,x1,v1); +} + + +template +SReal GravityForceField::getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const +{ + sofa::core::behavior::Mass* _mass = l_mass.get(); + return _mass->getGravitationalPotentialEnergy(params, x, d_gravitationalAcceleration.getValue()); +} + + +template +void GravityForceField::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) +{ + // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce + SOFA_UNUSED(mparams); + SOFA_UNUSED(d_df); + SOFA_UNUSED(d_dx); + mparams->setKFactorUsed(true); +} + + +template +void GravityForceField::addKToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal k, unsigned int & offset) +{ + // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce + SOFA_UNUSED(mat); + SOFA_UNUSED(k); + SOFA_UNUSED(offset); +} + + +template +void GravityForceField::addKToMatrix(const sofa::core::behavior::MultiMatrixAccessor* matrix, SReal kFact) +{ + // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce + SOFA_UNUSED(matrix); + SOFA_UNUSED(kFact); +} + + +} // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp index c3206a5cef1..0fcffe9b282 100644 --- a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp +++ b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp @@ -260,16 +260,6 @@ void EulerExplicitSolver::parse(sofa::core::objectmodel::BaseObjectDescription* } } -void EulerExplicitSolver::addSeparateGravity(sofa::simulation::common::MechanicalOperations* mop, SReal dt, core::MultiVecDerivId v) -{ - sofa::helper::ScopedAdvancedTimer timer("addSeparateGravity"); - - /// Calls the "addGravityToV" method of every BaseMass objects found in the current - /// context tree, if the BaseMass object has the m_separateGravity flag set to true. - /// The method "addGravityToV" usually performs v += dt * g - mop->addSeparateGravity(dt, v); -} - void EulerExplicitSolver::computeForce(sofa::simulation::common::MechanicalOperations* mop, core::MultiVecDerivId f) { sofa::helper::ScopedAdvancedTimer timer("ComputeForce"); diff --git a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.h b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.h index 9278d96f286..51eee0bf824 100644 --- a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.h +++ b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.h @@ -107,10 +107,6 @@ class SOFA_COMPONENT_ODESOLVER_FORWARD_API EulerExplicitSolver : public sofa::co const sofa::core::behavior::MultiVecDeriv& acc, SReal dt) const; - /// Gravity times time step size is added to the velocity for some masses - /// v += g * dt - static void addSeparateGravity(sofa::simulation::common::MechanicalOperations* mop, SReal dt, core::MultiVecDerivId v); - /// Assemble the force vector (right-hand side of the equation) static void computeForce(sofa::simulation::common::MechanicalOperations* mop, core::MultiVecDerivId f); diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h index 8536471db67..232b648134f 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h @@ -86,9 +86,12 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; - SReal getPotentialEnergy(const core::MechanicalParams* /*mparams*/, const DataVecCoord& /* x */) const override + SReal getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const override { - msg_warning() << "Method getPotentialEnergy not implemented yet."; + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(gravity); + msg_warning() << "Method getGravitationalPotentialEnergy not implemented yet."; return 0.0; } @@ -100,8 +103,6 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx) override; - void addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) override; - void draw(const core::visual::VisualParams* vparams) override; SReal getElementMass(sofa::Index index) const override; diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl index d9c92d7771d..786700f79dd 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl @@ -401,33 +401,11 @@ void HexahedralFEMForceFieldAndMass::accFromF(const core::MechanicalP } -template -void HexahedralFEMForceFieldAndMass::addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) -{ - if(mparams) - { - VecDeriv& v = *d_v.beginEdit(); - - SReal _dt = sofa::core::mechanicalparams::dt(mparams); - - for (unsigned int i=0; i<_particleMasses.getValue().size(); i++) - { - v[i] +=this->getContext()->getGravity()*_dt; - } - d_v.beginEdit(); - } -} - - template void HexahedralFEMForceFieldAndMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) { HexahedralFEMForceFieldT::addForce(mparams, f,x,v); - //if gravity was added separately (in solver's "solve" method), then nothing to do here - if (this->m_separateGravity.getValue()) - return; - helper::WriteAccessor< DataVecDeriv > _f = f; for (unsigned int i=0; i<_particleMasses.getValue().size(); i++) { diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h index f5418ba1512..f5071138ddb 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h @@ -94,22 +94,17 @@ class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass::accFromF(const core::MechanicalP // need to built the big global mass matrix and to inverse it... } - -template -void HexahedronFEMForceFieldAndMass::addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) -{ - if(mparams) - { - VecDeriv& v = *d_v.beginEdit(); - SReal dt = sofa::core::mechanicalparams::dt(mparams); - for (unsigned int i=0; i<_particleMasses.size(); i++) - { - v[i] +=this->getContext()->getGravity()*dt; - } - d_v.beginEdit(); - } -} - - template void HexahedronFEMForceFieldAndMass::addForce (const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) { HexahedronFEMForceFieldT::addForce(mparams, f,x,v); - //if gravity was added separately (in solver's "solve" method), then nothing to do here - if (this->m_separateGravity.getValue()) - return; - helper::WriteAccessor< DataVecDeriv > _f = f; for (unsigned int i=0; i<_particleMasses.size(); i++) { diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h index 7a15ee4bc63..01a887c83e5 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h @@ -90,7 +90,6 @@ class NonUniformHexahedronFEMForceFieldAndMass : virtual public component::solid void reinit() override { msg_warning() << "Non-uniform mechanical properties can't be updated, changes on mechanical properties (young, poisson, density) are not taken into account."; } void addMDx(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor) override; - void addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) override; void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl index fb61a176739..e3d67d1b120 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl @@ -448,30 +448,6 @@ void NonUniformHexahedronFEMForceFieldAndMass::addMDx(const core::MechanicalP } } -template -void NonUniformHexahedronFEMForceFieldAndMass::addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) -{ - if(d_useMass.getValue()) - HexahedronFEMForceFieldAndMassT::addGravityToV(mparams, d_v); - else - { - if(mparams) - { - VecDeriv& v = *d_v.beginEdit(); - - const SReal* g = this->getContext()->getGravity().ptr(); - Deriv theGravity; - T::set( theGravity, (Real)g[0], (Real)g[1], (Real)g[2]); - Deriv hg = theGravity * (sofa::core::mechanicalparams::dt(mparams)); - for (unsigned int i=0; i void NonUniformHexahedronFEMForceFieldAndMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) { diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp index 57b50b9e1be..c2590dd8f10 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp @@ -26,8 +26,7 @@ namespace sofa::core::behavior { BaseMass::BaseMass() - : m_separateGravity (initData(&m_separateGravity , false, "separateGravity", "add separately gravity to velocity computation")) - , rayleighMass (initData(&rayleighMass , SReal(0), "rayleighMass", "Rayleigh damping - mass matrix coefficient")) + : rayleighMass (initData(&rayleighMass , SReal(0), "rayleighMass", "Rayleigh damping - mass matrix coefficient")) { } diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h index d9b7e0b29cc..4cdf9f0813b 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h @@ -67,21 +67,14 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor /// dx = M^-1 f virtual void accFromF(const MechanicalParams* mparams, MultiVecDerivId aid) = 0; - /// \brief Perform v += dt*g operation. Used if mass wants to added G separately from the other forces to v. - /// - /// \param mparams \a sofa::core::mechanicalparams::dt(mparams) is the time step of for temporal discretization. - virtual void addGravityToV(const MechanicalParams* mparams, MultiVecDerivId vid) = 0; - /// vMv/2 virtual SReal getKineticEnergy(const MechanicalParams* mparams = mechanicalparams::defaultInstance()) const = 0; - /// Mgx - virtual SReal getPotentialEnergy(const MechanicalParams* mparams = mechanicalparams::defaultInstance()) const = 0; /// (Mv,xMv+Iw) (linear and angular momenta against world origin) virtual type::Vector6 getMomentum(const MechanicalParams* mparams = mechanicalparams::defaultInstance()) const = 0; - /// @} + /// @name Matrix operations /// @{ @@ -104,15 +97,9 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor virtual SReal getElementMass(sofa::Index index) const =0; /// Get the matrix relative to the DOF at \a index. virtual void getElementMass(sofa::Index index, linearalgebra::BaseMatrix *m) const = 0; - + /// Return whether the mass matrix is diagonal or not virtual bool isDiagonal() const = 0; - /// Member specifying if the gravity is added separately to the DOFs velocities (in solve method), - /// or if is added with the other forces(addForceMethod) - Data m_separateGravity; - - - /** @name Rayleigh Damping (mass contribution) */ /// @{ diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 895db28c4fb..3ed9f077d41 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -77,13 +77,20 @@ class Mass : virtual public ForceField, public BaseMass virtual void accFromF(const MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f); - /// Mass forces (gravity) often have null derivative - void addDForce(const MechanicalParams* /*mparams*/, DataVecDeriv & /*df*/, const DataVecDeriv & /*dx*/ ) override; + /// Mass does not generate any force + void addForce(const MechanicalParams* /*mparams*/, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; + + /// Mass does not generate any force derivative + void addDForce(const MechanicalParams* /*mparams*/, DataVecDeriv & df, const DataVecDeriv & dx ) override; + + /// Appart from the potential energy due to the gravitational acceleration, no energy comes from the mass itself + SReal getPotentialEnergy( const MechanicalParams* mparams, const DataVecCoord& x ) const override; + /// Accumulate the contribution of M, B, and/or K matrices multiplied /// by the dx vector with the given coefficients. /// - /// This method computes + /// This method from the ForceField API computes /// $ df += mFactor M dx + bFactor B dx + kFactor K dx $ /// For masses, it calls both addMdx and addDForce (which is often empty). /// @@ -92,20 +99,25 @@ class Mass : virtual public ForceField, public BaseMass /// \param kFact coefficient for stiffness contributions (i.e. DOFs term in the ODE) void addMBKdx(const MechanicalParams* mparams, MultiVecDerivId dfId) override; - /// $ e = 1/2 v^t M v $ + + /// $ f = M g $ /// - /// This method retrieves the velocity vector and call the internal - /// getKineticEnergy(const MechanicalParams*, const DataVecDeriv&) method implemented by the component. - SReal getKineticEnergy( const MechanicalParams* mparams) const override; - virtual SReal getKineticEnergy( const MechanicalParams* mparams, const DataVecDeriv& v) const; + /// This method computes the external force due to the gravitational acceleration + /// addGravitationalForce(const MechanicalParams*, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) method implemented by the component. + virtual void addGravitationalForce( const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) const = 0; /// $ e = M g x $ /// /// This method retrieves the positions vector and call the internal - /// getPotentialEnergy(const MechanicalParams*, const VecCoord&) method implemented by the component. - SReal getPotentialEnergy( const MechanicalParams* mparams) const override; - SReal getPotentialEnergy( const MechanicalParams* mparams, const DataVecCoord& x ) const override; + /// getGravitationalPotentialEnergy(const MechanicalParams*, const VecCoord&) method implemented by the component. + virtual SReal getGravitationalPotentialEnergy( const MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const = 0; + /// $ e = 1/2 v^t M v $ + /// + /// This method retrieves the velocity vector and call the internal + /// getKineticEnergy(const MechanicalParams*, const DataVecDeriv&) method implemented by the component. + SReal getKineticEnergy( const MechanicalParams* mparams) const override; + virtual SReal getKineticEnergy( const MechanicalParams* mparams, const DataVecDeriv& v) const; /// $ m = ( Mv, cross(x,Mv)+Iw ) $ /// linearMomentum = Mv, angularMomentum_particle = cross(x,linearMomentum), angularMomentum_body = cross(x,linearMomentum)+Iw @@ -144,11 +156,6 @@ class Mass : virtual public ForceField, public BaseMass /// export kinetic and potential energy state at "time" to a gnuplot file void exportGnuplot(const MechanicalParams* mparams, SReal time) override; - /// perform v += dt*g operation. Used if mass wants to added G separately from the other forces to v. - void addGravityToV(const MechanicalParams* mparams, MultiVecDerivId /*vid*/) override; - virtual void addGravityToV(const MechanicalParams* /* mparams */, DataVecDeriv& /* d_v */); - - /// recover the mass of an element SReal getElementMass(sofa::Index) const override; void getElementMass(sofa::Index index, linearalgebra::BaseMatrix *m) const override; diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl index 9f8f80743c1..c379b942ca3 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl @@ -75,20 +75,29 @@ void Mass::accFromF(const MechanicalParams* /*mparams*/, DataVecDeriv msg_warning() << "Method accFromF(const MechanicalParams* , DataVecDeriv& , const DataVecDeriv& ) not implemented."; } +template +void Mass::addDForce(const MechanicalParams* mparams, DataVecDeriv & df, const DataVecDeriv & dx) +{ + SOFA_UNUSED(mparams); + SOFA_UNUSED(df); + SOFA_UNUSED(dx); +} template -void Mass::addDForce(const MechanicalParams* - #ifndef NDEBUG - mparams - #endif - , - DataVecDeriv & /*df*/, const DataVecDeriv & /*dx*/) +void Mass::addForce(const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) { -#ifndef NDEBUG - // @TODO Remove - // Hack to disable warning message - sofa::core::mechanicalparams::kFactorIncludingRayleighDamping(mparams, this->rayleighStiffness.getValue()); -#endif + SOFA_UNUSED(mparams); + SOFA_UNUSED(f); + SOFA_UNUSED(x); + SOFA_UNUSED(v); +} + +template +SReal Mass::getPotentialEnergy(const MechanicalParams* mparams, const DataVecCoord& x) const +{ + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + return 0.0; } template @@ -116,23 +125,6 @@ SReal Mass::getKineticEnergy(const MechanicalParams* /*mparams*/, con return 0.0; } - -template -SReal Mass::getPotentialEnergy(const MechanicalParams* mparams) const -{ - if (this->mstate) - return getPotentialEnergy(mparams /* PARAMS FIRST */, *mparams->readX(this->mstate)); - return 0.0; -} - -template -SReal Mass::getPotentialEnergy(const MechanicalParams* /*mparams*/, const DataVecCoord& /*x*/) const -{ - msg_warning() << "Method getPotentialEnergy( const MechanicalParams*, const DataVecCoord& ) not implemented."; - return 0.0; -} - - template type::Vector6 Mass::getMomentum( const MechanicalParams* mparams ) const { @@ -176,27 +168,6 @@ void Mass::addMBKToMatrix(const MechanicalParams* mparams, const sofa addMToMatrix(mparams, matrix); } -template -void Mass::addGravityToV(const MechanicalParams* mparams, MultiVecDerivId vid) -{ - if(this->mstate) - { - DataVecDeriv& v = *vid[this->mstate.get()].write(); - addGravityToV(mparams, v); - } -} - -template -void Mass::addGravityToV(const MechanicalParams* /* mparams */, DataVecDeriv& /* d_v */) -{ - static int i=0; - if (i < 10) { - msg_warning() << "Method addGravityToV with Scalar not implemented"; - i++; - } -} - - template void Mass::initGnuplot(const std::string path) { @@ -215,9 +186,9 @@ void Mass::exportGnuplot(const MechanicalParams* mparams, SReal time) if (m_gnuplotFileEnergy!=nullptr) { (*m_gnuplotFileEnergy) << time <<"\t"<< this->getKineticEnergy(mparams) - <<"\t"<< this->getPotentialEnergy(mparams) - <<"\t"<< this->getPotentialEnergy(mparams) - +this->getKineticEnergy(mparams)<< std::endl; +// <<"\t"<< this->getGravitationalPotentialEnergy(mparams) +// <<"\t"<< this->getGravitationalPotentialEnergy(mparams) +// +this->getKineticEnergy(mparams)<< std::endl; } } diff --git a/Sofa/framework/Simulation/Core/CMakeLists.txt b/Sofa/framework/Simulation/Core/CMakeLists.txt index 2b62d1c3335..a543ca47896 100644 --- a/Sofa/framework/Simulation/Core/CMakeLists.txt +++ b/Sofa/framework/Simulation/Core/CMakeLists.txt @@ -83,7 +83,6 @@ set(HEADER_FILES ${SRC_ROOT}/mechanicalvisitor/MechanicalAddMBKdxVisitor.h ${SRC_ROOT}/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.h ${SRC_ROOT}/mechanicalvisitor/MechanicalAddMDxVisitor.h - ${SRC_ROOT}/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.h ${SRC_ROOT}/mechanicalvisitor/MechanicalApplyConstraintsVisitor.h ${SRC_ROOT}/mechanicalvisitor/MechanicalApplyProjectiveConstraint_ToMatrixVisitor.h ${SRC_ROOT}/mechanicalvisitor/MechanicalBeginIntegrationVisitor.h @@ -206,7 +205,6 @@ set(SOURCE_FILES ${SRC_ROOT}/mechanicalvisitor/MechanicalAddMBKdxVisitor.cpp ${SRC_ROOT}/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.cpp ${SRC_ROOT}/mechanicalvisitor/MechanicalAddMDxVisitor.cpp - ${SRC_ROOT}/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.cpp ${SRC_ROOT}/mechanicalvisitor/MechanicalApplyConstraintsVisitor.cpp ${SRC_ROOT}/mechanicalvisitor/MechanicalApplyProjectiveConstraint_ToMatrixVisitor.cpp ${SRC_ROOT}/mechanicalvisitor/MechanicalBeginIntegrationVisitor.cpp diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.h deleted file mode 100644 index eab1dc99ecc..00000000000 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddSeparateGravityVisitor.h +++ /dev/null @@ -1,59 +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 * -******************************************************************************/ -#pragma once - -#include - -namespace sofa::simulation::mechanicalvisitor -{ - -/** Add dt*mass*Gravity to the velocity -This is called if the mass wants to be added separately to the mm from the other forces -*/ -class SOFA_SIMULATION_CORE_API MechanicalAddSeparateGravityVisitor : public MechanicalVisitor -{ -public: - sofa::core::MultiVecDerivId res; - MechanicalAddSeparateGravityVisitor(const sofa::core::MechanicalParams* m_mparams, - sofa::core::MultiVecDerivId res ) - : MechanicalVisitor(m_mparams) , res(res) - { -#ifdef SOFA_DUMP_VISITOR_INFO - setReadWriteVectors(); -#endif - } - - /// Process the BaseMass - Result fwdMass(simulation::Node* /*node*/,sofa::core::behavior::BaseMass* mass) override; - - /// Return a class name for this visitor - /// Only used for debugging / profiling purposes - const char* getClassName() const override { return "MechanicalAddSeparateGravityVisitor"; } - virtual std::string getInfos() const override { std::string name= "["+res.getName()+"]"; return name; } -#ifdef SOFA_DUMP_VISITOR_INFO - void setReadWriteVectors() override - { - addReadWriteVector(res); - } -#endif -}; -} \ No newline at end of file From df4aeae156b49120f68772af96607dd2cf026587 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sat, 14 May 2022 15:04:56 +0200 Subject: [PATCH 02/41] Create a separate gravity FF --- .../src/sofa/component/mass/DiagonalMass.cpp | 22 ++++------ .../src/sofa/component/mass/DiagonalMass.h | 10 ++--- .../src/sofa/component/mass/DiagonalMass.inl | 6 ++- .../src/sofa/component/mass/MeshMatrixMass.h | 6 +-- .../sofa/component/mass/MeshMatrixMass.inl | 18 ++++---- .../src/sofa/component/mass/UniformMass.cpp | 19 ++++---- .../src/sofa/component/mass/UniformMass.h | 13 +++--- .../src/sofa/component/mass/UniformMass.inl | 18 +++----- .../mechanicalload/GravityForceField.h | 2 +- .../mechanicalload/GravityForceField.inl | 8 ++-- .../backward/EulerImplicitSolver.cpp | 2 - .../backward/NewmarkImplicitSolver.cpp | 1 - .../forward/CentralDifferenceSolver.cpp | 2 - .../odesolver/forward/EulerSolver.cpp | 1 - .../odesolver/forward/RungeKutta2Solver.cpp | 2 - .../odesolver/forward/RungeKutta4Solver.cpp | 2 - .../elastic/HexahedralFEMForceFieldAndMass.h | 5 ++- .../HexahedralFEMForceFieldAndMass.inl | 20 ++++++--- .../elastic/HexahedronFEMForceFieldAndMass.h | 6 ++- .../HexahedronFEMForceFieldAndMass.inl | 20 +++++++-- ...NonUniformHexahedronFEMForceFieldAndMass.h | 1 + ...nUniformHexahedronFEMForceFieldAndMass.inl | 25 ++++++----- .../Core/src/sofa/core/behavior/BaseMass.h | 6 +++ .../Core/src/sofa/core/behavior/Mass.h | 6 +-- .../Core/src/sofa/core/behavior/Mass.inl | 5 +-- .../src/sofa/core/topology/TopologyData.inl | 2 + .../core/topology/TopologyDataHandler.inl | 2 +- .../sofa/simulation/MechanicalOperations.cpp | 11 ----- .../sofa/simulation/MechanicalOperations.h | 6 ++- .../PreassembledMass/PreassembledMass.h | 5 +-- .../PreassembledMass/PreassembledMass.inl | 43 ++++--------------- 31 files changed, 139 insertions(+), 156 deletions(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp index 6892fa1ca6f..b18d71eec01 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp @@ -39,21 +39,17 @@ using namespace sofa::defaulttype; template template -SReal DiagonalMass::getPotentialEnergyRigidImpl( const MechanicalParams* mparams, - const DataVecCoord& x) const +SReal DiagonalMass::getGravitationalPotentialEnergyRigidImpl( const MechanicalParams* mparams, + const DataVecCoord& x, const Deriv& g) const { SOFA_UNUSED(mparams) ; SReal e = 0; const MassVector &masses= d_vertexMass.getValue(); const VecCoord& _x = x.getValue(); - // gravity - Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - RigidTypes::set( theGravity, g[0], g[1], g[2]); for (unsigned int i=0; i<_x.size(); i++) { - e -= getVCenter(theGravity) * masses[i].mass * _x[i].getCenter(); + e -= getVCenter(g) * masses[i].mass * _x[i].getCenter(); } return e; } @@ -266,17 +262,17 @@ type::Vector6 DiagonalMass::getMomentumVec3Impl( co template <> -SReal DiagonalMass::getPotentialEnergy( const MechanicalParams* mparams, - const DataVecCoord& x) const +SReal DiagonalMass::getGravitationalPotentialEnergy( const MechanicalParams* mparams, + const DataVecCoord& x, const Deriv& g) const { - return getPotentialEnergyRigidImpl(mparams, x) ; + return getGravitationalPotentialEnergyRigidImpl(mparams, x, g) ; } template <> -SReal DiagonalMass::getPotentialEnergy( const MechanicalParams* mparams, - const DataVecCoord& x) const +SReal DiagonalMass::getGravitationalPotentialEnergy( const MechanicalParams* mparams, + const DataVecCoord& x, const Deriv& g) const { - return getPotentialEnergyRigidImpl(mparams, x) ; + return getGravitationalPotentialEnergyRigidImpl(mparams, x, g) ; } template <> diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h index e3c72f97a04..fc164c8269c 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.h @@ -303,7 +303,7 @@ class DiagonalMass : public core::behavior::Mass void accFromF(const core::MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f) override; - void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; ///< Mg force in a lumped gravity field SReal getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const override; ///< Mgx potential in a uniform gravity field, null at origin @@ -349,8 +349,8 @@ class DiagonalMass : public core::behavior::Mass private: template - SReal getPotentialEnergyRigidImpl( const core::MechanicalParams* mparams, - const DataVecCoord& x) const ; + SReal getGravitationalPotentialEnergyRigidImpl( const core::MechanicalParams* mparams, + const DataVecCoord& x, const Deriv& g) const ; template void drawRigid3dImpl(const core::visual::VisualParams* vparams) ; @@ -375,9 +375,9 @@ class DiagonalMass : public core::behavior::Mass // Specialization for rigids template <> -SReal DiagonalMass::getPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x) const; +SReal DiagonalMass::getGravitationalPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& g) const; template <> -SReal DiagonalMass::getPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x) const; +SReal DiagonalMass::getGravitationalPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& g) const; template <> void DiagonalMass::draw(const core::visual::VisualParams* vparams); template <> diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl index 0c02fd57105..9741d0880f8 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.inl @@ -589,8 +589,9 @@ SReal DiagonalMass::getKineticEnergy( const core::M } template -SReal DiagonalMass::getGravitationalPotentialEnergy( const core::MechanicalParams* /*mparams*/, const DataVecCoord& x, const Deriv& gravity ) const +SReal DiagonalMass::getGravitationalPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity ) const { + SOFA_UNUSED(mparams); const MassVector &masses= d_vertexMass.getValue(); helper::ReadAccessor< DataVecCoord > _x = x; @@ -1367,8 +1368,9 @@ void DiagonalMass::doUpdateInternal() } template -void DiagonalMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) +void DiagonalMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { + SOFA_UNUSED(mparams); SOFA_UNUSED(x); SOFA_UNUSED(v); diff --git a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.h b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.h index 0ac3b767a7f..8b95af4f70c 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.h @@ -203,12 +203,12 @@ class MeshMatrixMass : public core::behavior::Mass void accFromF(const core::MechanicalParams*, DataVecDeriv& a, const DataVecDeriv& f) override; // This function can't be used as it use M^-1 - void addForce(const core::MechanicalParams*, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; - - SReal getKineticEnergy(const core::MechanicalParams*, const DataVecDeriv& v) const override; ///< vMv/2 using dof->getV() override + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; ///< Mg force in a homogeneous gravity field SReal getGravitationalPotentialEnergy(const core::MechanicalParams*, const DataVecCoord& x, const Deriv& gravity) const override; ///< Mgx potential in a uniform gravity field, null at origin + SReal getKineticEnergy(const core::MechanicalParams*, const DataVecDeriv& v) const override; ///< vMv/2 using dof->getV() override + type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)) override bool isDiagonal() const override { return isLumped(); } diff --git a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl index 7692593d1a1..cf7dc680524 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.inl @@ -2060,20 +2060,18 @@ void MeshMatrixMass::accFromF(const core::Mechanica template -void MeshMatrixMass::addForce(const core::MechanicalParams*, DataVecDeriv& vf, const DataVecCoord& , const DataVecDeriv& ) +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& vf, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity ) { - helper::WriteAccessor< DataVecDeriv > f = vf; + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(v); + helper::WriteAccessor< DataVecDeriv > f = vf; const auto &vertexMass= d_vertexMass.getValue(); - // gravity - type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); - // add weight and inertia force for (unsigned int i=0; i::getKineticEnergy( const core: template -SReal MeshMatrixMass::getPotentialEnergy( const core::MechanicalParams*, const DataVecCoord& vx, const Deriv& gravity) const +SReal MeshMatrixMass::getGravitationalPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& vx, const Deriv& gravity) const { + SOFA_UNUSED(mparams); + const auto &vertexMass= d_vertexMass.getValue(); helper::ReadAccessor< DataVecCoord > x = vx; diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp index 8a62363f1ce..59ea39806d3 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp @@ -375,17 +375,16 @@ Vector6 UniformMass::getMomentumVec3DImpl ( const MechanicalParams*, template template -SReal UniformMass::getPotentialEnergyRigidImpl(const core::MechanicalParams* mparams, - const DataVecCoord& p_x) const +SReal UniformMass::getGravitationalPotentialEnergyRigidImpl(const core::MechanicalParams* mparams, + const DataVecCoord& p_x, const Deriv& g) const { SOFA_UNUSED(mparams) ; SReal e = 0; ReadAccessor< DataVecCoord > x = p_x; ReadAccessor > indices = d_indices; - typename Coord::Pos g ( getContext()->getGravity() ); for (unsigned int i=0; i::draw(const VisualParams* vparams) } template <> SOFA_COMPONENT_MASS_API -SReal UniformMass::getPotentialEnergy( const MechanicalParams* params, - const DataVecCoord& d_x ) const +SReal UniformMass::getGravitationalPotentialEnergy( const MechanicalParams* params, + const DataVecCoord& d_x, const Deriv& g ) const { - return getPotentialEnergyRigidImpl(params, d_x) ; + return getGravitationalPotentialEnergyRigidImpl(params, d_x, g) ; } template <> SOFA_COMPONENT_MASS_API -SReal UniformMass::getPotentialEnergy( const MechanicalParams* params, - const DataVecCoord& vx ) const +SReal UniformMass::getGravitationalPotentialEnergy( const MechanicalParams* params, + const DataVecCoord& vx, const Deriv& g ) const { - return getPotentialEnergyRigidImpl(params, vx) ; + return getGravitationalPotentialEnergyRigidImpl(params, vx, g) ; } template <> SOFA_COMPONENT_MASS_API diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index c33b2be4f83..d748611c54d 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -133,10 +133,11 @@ class UniformMass : public core::behavior::Mass void addMDx(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor) override; void accFromF(const core::MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f) override; - void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; - SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& d_v) const override; ///< vMv/2 using dof->getV() override + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; ///< Mg force in a uniform gravity field SReal getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const override; ///< Mgx potential in a uniform gravity field, null at origin + + SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& d_v) const override; ///< vMv/2 using dof->getV() override type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset); @@ -179,8 +180,8 @@ class UniformMass : public core::behavior::Mass void drawVec6Impl(const core::visual::VisualParams* vparams) ; template - SReal getPotentialEnergyRigidImpl(const core::MechanicalParams* mparams, - const DataVecCoord& x) const; ///< Mgx potential in a uniform gravity field, null at origin + SReal getGravitationalPotentialEnergyRigidImpl(const core::MechanicalParams* mparams, + const DataVecCoord& x, const Deriv& g) const; ///< Mgx potential in a uniform gravity field, null at origin @@ -214,9 +215,9 @@ void UniformMass::draw(const core::visual::VisualParam template <> void UniformMass::draw(const core::visual::VisualParams* vparams); template <> -SReal UniformMass::getPotentialEnergy ( const core::MechanicalParams*, const DataVecCoord& x ) const; +SReal UniformMass::getGravitationalPotentialEnergy ( const core::MechanicalParams*, const DataVecCoord& x, const Deriv& g ) const; template <> -SReal UniformMass::getPotentialEnergy ( const core::MechanicalParams*, const DataVecCoord& x ) const; +SReal UniformMass::getGravitationalPotentialEnergy ( const core::MechanicalParams*, const DataVecCoord& x, const Deriv& g ) const; template <> void UniformMass::draw(const core::visual::VisualParams* vparams); diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl index 883216e68d9..9dc5b7ee2e4 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl @@ -425,22 +425,18 @@ void UniformMass::addMDxToVector ( BaseVector * resVect, } template -void UniformMass::addForce ( const core::MechanicalParams*, DataVecDeriv& vf, const DataVecCoord& /*x*/, const DataVecDeriv& /*v*/ ) +void UniformMass::addGravitationalForce ( const core::MechanicalParams* mparams, DataVecDeriv& vf, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { - helper::WriteAccessor f = vf; + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(v); // weight - const SReal* g = getContext()->getGravity().ptr(); - Deriv theGravity; - DataTypes::set - ( theGravity, g[0], g[1], g[2] ); const MassType& m = d_vertexMass.getValue(); - Deriv mg = theGravity * m; - - dmsg_info() <<" addForce, mg = "<getContext()->getMass()); + sofa::core::behavior::Mass* p_mass; + this->getContext()->get(p_mass); + l_mass.set(p_mass); } // temprory pointer to the mass @@ -69,7 +71,7 @@ void GravityForceField::init() template -void GravityForceField::setGravitationalAcceleration(const Real grav) +void GravityForceField::setGravitationalAcceleration(const Deriv grav) { d_gravitationalAcceleration.setValue(grav); } @@ -79,7 +81,7 @@ template void GravityForceField::addForce(const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x1, const DataVecDeriv& v1) { sofa::core::behavior::Mass* _mass = l_mass.get(); - _mass->addGravitationalForce(params,f,x1,v1); + _mass->addGravitationalForce(params,f,x1,v1,d_gravitationalAcceleration.getValue()); } diff --git a/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/EulerImplicitSolver.cpp b/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/EulerImplicitSolver.cpp index 99075b6274a..99f577cf04d 100644 --- a/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/EulerImplicitSolver.cpp +++ b/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/EulerImplicitSolver.cpp @@ -272,8 +272,6 @@ void EulerImplicitSolver::solve(const core::ExecParams* params, SReal dt, sofa:: } #endif - mop.addSeparateGravity(dt, newVel); // v += dt*g . Used if mass wants to add G separately from the other forces to v - if (f_velocityDamping.getValue()!=0.0) newVel *= exp(-h*f_velocityDamping.getValue()); diff --git a/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/NewmarkImplicitSolver.cpp b/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/NewmarkImplicitSolver.cpp index 0d4b9818b8f..9bb1e3f763e 100644 --- a/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/NewmarkImplicitSolver.cpp +++ b/Sofa/Component/ODESolver/Backward/src/sofa/component/odesolver/backward/NewmarkImplicitSolver.cpp @@ -165,7 +165,6 @@ void NewmarkImplicitSolver::solve(const core::ExecParams* params, SReal dt, sofa #endif - mop.addSeparateGravity(dt, newVel); // v += dt*g . Used if mass wants to add G separately from the other forces to v. if (d_velocityDamping.getValue()!=0.0) newVel *= exp(-h*d_velocityDamping.getValue()); diff --git a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/CentralDifferenceSolver.cpp b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/CentralDifferenceSolver.cpp index bc1c3fd6542..80a6844c8c9 100644 --- a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/CentralDifferenceSolver.cpp +++ b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/CentralDifferenceSolver.cpp @@ -87,8 +87,6 @@ void CentralDifferenceSolver::solve(const core::ExecParams* params, SReal dt, so const SReal r = f_rayleighMass.getValue(); - mop.addSeparateGravity(dt); // v += dt*g . Used if mass wants to added G separately from the other forces to v. - //projectVelocity(vel); // initial velocities are projected to the constrained space // compute the current force diff --git a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp index 0fcffe9b282..f3ea5015338 100644 --- a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp +++ b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/EulerSolver.cpp @@ -73,7 +73,6 @@ void EulerExplicitSolver::solve(const core::ExecParams* params, acc.realloc(&vop, !d_threadSafeVisitor.getValue(), true); - addSeparateGravity(&mop, dt, vResult); computeForce(&mop, f); SReal nbNonDiagonalMasses = 0; diff --git a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta2Solver.cpp b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta2Solver.cpp index 630e39b069f..7633f15b905 100644 --- a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta2Solver.cpp +++ b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta2Solver.cpp @@ -57,8 +57,6 @@ void RungeKutta2Solver::solve(const core::ExecParams* params, SReal dt, sofa::co SReal startTime = this->getTime(); - mop.addSeparateGravity(dt); // v += dt*g . Used if mass wants to added G separately from the other forces to v. - // Compute state derivative. vel is the derivative of pos mop.computeAcc (startTime, acc, pos, vel); // acc is the derivative of vel diff --git a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta4Solver.cpp b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta4Solver.cpp index 327b305d767..0e45c468216 100644 --- a/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta4Solver.cpp +++ b/Sofa/Component/ODESolver/Forward/src/sofa/component/odesolver/forward/RungeKutta4Solver.cpp @@ -69,8 +69,6 @@ void RungeKutta4Solver::solve(const core::ExecParams* params, SReal dt, sofa::co SReal startTime = this->getTime(); - mop.addSeparateGravity(dt); // v += dt*g . Used if mass wants to added G separately from the other forces to v. - //First step dmsg_info() << "RK4 Step 1"; diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h index 232b648134f..1f5b8dca1ea 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h @@ -84,7 +84,7 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass void accFromF(const core::MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f) override; - void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; SReal getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const override { @@ -101,6 +101,9 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass return 0; } + SReal getPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x ) const override; + + void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f,const DataVecCoord& x, const DataVecDeriv& v) override; void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx) override; void draw(const core::visual::VisualParams* vparams) override; diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl index 786700f79dd..d148ccfddb7 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl @@ -402,18 +402,23 @@ void HexahedralFEMForceFieldAndMass::accFromF(const core::MechanicalP template -void HexahedralFEMForceFieldAndMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) +void HexahedralFEMForceFieldAndMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { - HexahedralFEMForceFieldT::addForce(mparams, f,x,v); - + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(v); helper::WriteAccessor< DataVecDeriv > _f = f; for (unsigned int i=0; i<_particleMasses.getValue().size(); i++) { - _f[i] += this->getContext()->getGravity()*_particleMasses.getValue()[i]; + _f[i] += gravity * _particleMasses.getValue()[i]; } } - +template +void HexahedralFEMForceFieldAndMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& f,const DataVecCoord& x, const DataVecDeriv& v) +{ + HexahedralFEMForceFieldT::addForce(mparams, f, x, v); +} template void HexahedralFEMForceFieldAndMass::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx) @@ -421,6 +426,11 @@ void HexahedralFEMForceFieldAndMass::addDForce(const core::Mechanical HexahedralFEMForceFieldT::addDForce(mparams, df, dx); } +template +SReal HexahedralFEMForceFieldAndMass::getPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x) const +{ + HexahedralFEMForceFieldT::getPotentialEnergy(mparams, x); +} template SReal HexahedralFEMForceFieldAndMass::getElementMass(sofa::Index /*index*/) const diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h index f5071138ddb..707a919f787 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h @@ -86,7 +86,7 @@ class HexahedronFEMForceFieldAndMass : virtual public core::behavior::MassgetV() override { @@ -94,6 +94,8 @@ class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass _lumpedMasses; ///< masses per particle computed by lumping mass matrices - }; #if !defined(SOFA_COMPONENT_FORCEFIELD_HEXAHEDRONFEMFORCEFIELDANDMASS_CPP) diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl index 5b6d30d917b..b8b1b018a0b 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl @@ -259,24 +259,38 @@ void HexahedronFEMForceFieldAndMass::accFromF(const core::MechanicalP } template -void HexahedronFEMForceFieldAndMass::addForce (const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) +void HexahedronFEMForceFieldAndMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { - HexahedronFEMForceFieldT::addForce(mparams, f,x,v); + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(v); helper::WriteAccessor< DataVecDeriv > _f = f; for (unsigned int i=0; i<_particleMasses.size(); i++) { - _f[i] += this->getContext()->getGravity()*_particleMasses[i]; + _f[i] += gravity * _particleMasses[i]; } } + +template +void HexahedronFEMForceFieldAndMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& f,const DataVecCoord& x, const DataVecDeriv& v) +{ + HexahedronFEMForceFieldT::addForce(mparams, f, x, v); +} + template void HexahedronFEMForceFieldAndMass::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx) { HexahedronFEMForceFieldT::addDForce(mparams, df, dx); } +template +SReal HexahedronFEMForceFieldAndMass::getPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x ) const +{ + HexahedronFEMForceFieldT::getPotentialEnergy(mparams, x); +} template SReal HexahedronFEMForceFieldAndMass::getElementMass(sofa::Index /*index*/) const diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h index 01a887c83e5..982fbde3242 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.h @@ -91,6 +91,7 @@ class NonUniformHexahedronFEMForceFieldAndMass : virtual public component::solid void addMDx(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor) override; void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) override; protected: diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl index e3d67d1b120..a2ea1c1f240 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl @@ -451,22 +451,21 @@ void NonUniformHexahedronFEMForceFieldAndMass::addMDx(const core::MechanicalP template void NonUniformHexahedronFEMForceFieldAndMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) { - if(d_useMass.getValue()) - HexahedronFEMForceFieldAndMassT::addForce(mparams, f,x,v); - else - { - HexahedronFEMForceFieldT::addForce(mparams, f,x,v); + HexahedronFEMForceFieldAndMassT::addForce(mparams, f,x,v); +} - helper::WriteAccessor _f = f; +template +void NonUniformHexahedronFEMForceFieldAndMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) +{ + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(v); - const SReal* g = this->getContext()->getGravity().ptr(); - Deriv theGravity; - T::set( theGravity, g[0], g[1], g[2]); + helper::WriteAccessor _f = f; - for (unsigned int i=0; i<_f.size(); i++) - { - _f[i] += theGravity * this->_particleMasses[i]; - } + for (unsigned int i=0; i<_f.size(); i++) + { + _f[i] += gravity * this->_particleMasses[i]; } } diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h index 4cdf9f0813b..98691403ca6 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h @@ -75,6 +75,12 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor /// @} + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the separate gravity API.") + virtual void addGravityToV(const MechanicalParams* mparams, MultiVecDerivId vid) = delete; + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the separate gravity API.") + DeprecatedAndRemoved m_separateGravity; + + /// @name Matrix operations /// @{ diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 3ed9f077d41..b911d82a58a 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -78,10 +78,10 @@ class Mass : virtual public ForceField, public BaseMass /// Mass does not generate any force - void addForce(const MechanicalParams* /*mparams*/, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; + void addForce(const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; /// Mass does not generate any force derivative - void addDForce(const MechanicalParams* /*mparams*/, DataVecDeriv & df, const DataVecDeriv & dx ) override; + void addDForce(const MechanicalParams* mparams, DataVecDeriv & df, const DataVecDeriv & dx ) override; /// Appart from the potential energy due to the gravitational acceleration, no energy comes from the mass itself SReal getPotentialEnergy( const MechanicalParams* mparams, const DataVecCoord& x ) const override; @@ -104,7 +104,7 @@ class Mass : virtual public ForceField, public BaseMass /// /// This method computes the external force due to the gravitational acceleration /// addGravitationalForce(const MechanicalParams*, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) method implemented by the component. - virtual void addGravitationalForce( const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) const = 0; + virtual void addGravitationalForce( const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) = 0; /// $ e = M g x $ /// diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl index c379b942ca3..81d0c6fd48f 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl @@ -185,10 +185,7 @@ void Mass::exportGnuplot(const MechanicalParams* mparams, SReal time) { if (m_gnuplotFileEnergy!=nullptr) { - (*m_gnuplotFileEnergy) << time <<"\t"<< this->getKineticEnergy(mparams) -// <<"\t"<< this->getGravitationalPotentialEnergy(mparams) -// <<"\t"<< this->getGravitationalPotentialEnergy(mparams) -// +this->getKineticEnergy(mparams)<< std::endl; + (*m_gnuplotFileEnergy) << time <<"\t"<< this->getKineticEnergy(mparams) << "\t" << 0.0 << "\t" << this->getKineticEnergy(mparams)<< std::endl; } } diff --git a/Sofa/framework/Core/src/sofa/core/topology/TopologyData.inl b/Sofa/framework/Core/src/sofa/core/topology/TopologyData.inl index 75f7979b8fe..cf2d2f2b746 100644 --- a/Sofa/framework/Core/src/sofa/core/topology/TopologyData.inl +++ b/Sofa/framework/Core/src/sofa/core/topology/TopologyData.inl @@ -234,6 +234,8 @@ void TopologyData ::add(const sofa::type::vector >& coefs, const sofa::type::vector< AncestorElem >& ancestorElems) { + SOFA_UNUSED(ancestorElems); + std::size_t nbElements = index.size(); if (nbElements == 0) return; // Using default values diff --git a/Sofa/framework/Core/src/sofa/core/topology/TopologyDataHandler.inl b/Sofa/framework/Core/src/sofa/core/topology/TopologyDataHandler.inl index 183ebd446cd..9c35733ff2b 100644 --- a/Sofa/framework/Core/src/sofa/core/topology/TopologyDataHandler.inl +++ b/Sofa/framework/Core/src/sofa/core/topology/TopologyDataHandler.inl @@ -46,7 +46,7 @@ TopologyDataHandler< TopologyElementType, VecT>::TopologyDataHandler(t_topologic , m_pointsLinked(false), m_edgesLinked(false), m_trianglesLinked(false) , m_quadsLinked(false), m_tetrahedraLinked(false), m_hexahedraLinked(false) { - + SOFA_UNUSED(defaultValue); } diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.cpp index f75aa8f2785..7d063b33730 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -321,16 +320,6 @@ void MechanicalOperations::addMBKv(core::MultiVecDerivId df, SReal m, SReal b, S mparams.setDx(dx); } - - -/// Add dt*Gravity to the velocity -void MechanicalOperations::addSeparateGravity(SReal dt, core::MultiVecDerivId result) -{ - mparams.setDt(dt); - setV(result); - executeVisitor( MechanicalAddSeparateGravityVisitor(&mparams, result) ); -} - void MechanicalOperations::computeContactForce(core::MultiVecDerivId result) { setF(result); diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h index 9a8d996a4a9..67effc0cc12 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h @@ -84,8 +84,10 @@ class SOFA_SIMULATION_CORE_API MechanicalOperations void addMBKdx(core::MultiVecDerivId df, SReal m, SReal b, SReal k, bool clear = true, bool accumulate = true); /// accumulate $ df += (m M + b B + k K) velocity $ void addMBKv(core::MultiVecDerivId df, SReal m, SReal b, SReal k, bool clear = true, bool accumulate = true); - /// Add dt*Gravity to the velocity - void addSeparateGravity(SReal dt, core::MultiVecDerivId result = core::VecDerivId::velocity() ); + + + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the separate gravity API.") + void addSeparateGravity(SReal dt, core::MultiVecDerivId result = core::VecDerivId::velocity() ) = delete; void computeContactForce(core::MultiVecDerivId result); void computeContactDf(core::MultiVecDerivId df); diff --git a/applications/plugins/PreassembledMass/PreassembledMass.h b/applications/plugins/PreassembledMass/PreassembledMass.h index 8decc9ca4da..c8a6f58db2e 100644 --- a/applications/plugins/PreassembledMass/PreassembledMass.h +++ b/applications/plugins/PreassembledMass/PreassembledMass.h @@ -96,10 +96,9 @@ class PreassembledMass : public core::behavior::Mass< DataTypes >, public BasePr // -- Mass interface (employed only if d_massOnIndependents==true) void addMDx(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecDeriv& dx, double factor); void accFromF(const core::MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f); - void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v); + void addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity); double getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& v) const; - double getPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x) const; - void addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v); + double getGravitationalPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity) const; void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix); diff --git a/applications/plugins/PreassembledMass/PreassembledMass.inl b/applications/plugins/PreassembledMass/PreassembledMass.inl index aa24e319fc2..2ca871ebfa5 100644 --- a/applications/plugins/PreassembledMass/PreassembledMass.inl +++ b/applications/plugins/PreassembledMass/PreassembledMass.inl @@ -146,10 +146,10 @@ double PreassembledMass< DataTypes >::getKineticEnergy( const core::MechanicalPa } template < class DataTypes > -double PreassembledMass< DataTypes >::getPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x ) const +double PreassembledMass< DataTypes >::getGravitationalPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x, const Deriv& gravity ) const { serr<::getPotentialEnergy( mparams, x ); + return core::behavior::Mass< DataTypes >::getGravitationalPotentialEnergy( mparams, x, gravity ); // const VecCoord& _x = x.getValue(); @@ -170,48 +170,21 @@ double PreassembledMass< DataTypes >::getPotentialEnergy( const core::Mechanical -template < class DataTypes > -void PreassembledMass< DataTypes >::addGravityToV(const core::MechanicalParams* mparams, DataVecDeriv& d_v) -{ - if(mparams) - { - VecDeriv& v = *d_v.beginEdit(); - - // gravity - Vec3 g ( this->getContext()->getGravity() * (sofa::core::mechanicalparams::dt(mparams)) ); - Deriv theGravity; - DataTypes::set ( theGravity, g[0], g[1], g[2]); - Deriv hg = theGravity * (sofa::core::mechanicalparams::dt(mparams)); - - // add weight force - for (unsigned int i=0; i -void PreassembledMass< DataTypes >::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& f, const DataVecCoord& /*x*/, const DataVecDeriv& /*v*/) +void PreassembledMass< DataTypes >::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { - //if gravity was added separately (in solver's "solve" method), then nothing to do here - if(this->m_separateGravity.getValue()) return; + SOFA_UNUSED(mparams); + SOFA_UNUSED(x); + SOFA_UNUSED(v); VecDeriv& _f = *f.beginEdit(); - // gravity - Vec3 g ( this->getContext()->getGravity() ); -// Deriv theGravity; -// DataTypes::set( theGravity, g[0], g[1], g[2] ); - // add weight -// d_massMatrix.template addMul_by_line( _f, theGravity ); - //TODO optimize this!!! VecDeriv gravities(_f.size()); for(size_t i=0 ; i<_f.size() ; ++i ) - DataTypes::set( gravities[i], g[0], g[1], g[2] ); + gravities[i] = gravity; + d_massMatrix.getValue().addMult( _f, gravities ); f.endEdit(); From 86e8a5e8b200148c0d94c431f89a25547874c3a9 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 15 May 2022 20:42:49 +0200 Subject: [PATCH 03/41] Make gravity as DPos to keep only axis dofs --- .../sofa/component/mechanicalload/GravityForceField.h | 5 +++-- .../component/mechanicalload/GravityForceField.inl | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h index 17083aec855..78f31705aff 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h @@ -43,11 +43,12 @@ class GravityForceField : public core::behavior::ForceField typedef typename DataTypes::VecDeriv VecDeriv; typedef typename DataTypes::Coord Coord; typedef typename DataTypes::Deriv Deriv; + typedef typename DataTypes::DPos DPos; typedef typename Coord::value_type Real; typedef core::objectmodel::Data DataVecCoord; typedef core::objectmodel::Data DataVecDeriv; - Data< Deriv > d_gravitationalAcceleration; ///< Value corresponding to the gravitational acceleration + Data< DPos > d_gravitationalAcceleration; ///< Value corresponding to the gravitational acceleration SingleLink, sofa::core::behavior::Mass, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_mass; ///< Link to be set to the mass in the component graph /// Init function @@ -69,7 +70,7 @@ class GravityForceField : public core::behavior::ForceField SReal getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const override; /// Set the gravitational acceleration - void setGravitationalAcceleration(const Deriv grav); + void setGravitationalAcceleration(const DPos grav); protected: GravityForceField(); diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl index f9e7ed0bdf0..6cf9529bf04 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl @@ -71,7 +71,7 @@ void GravityForceField::init() template -void GravityForceField::setGravitationalAcceleration(const Deriv grav) +void GravityForceField::setGravitationalAcceleration(const DPos grav) { d_gravitationalAcceleration.setValue(grav); } @@ -81,7 +81,9 @@ template void GravityForceField::addForce(const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x1, const DataVecDeriv& v1) { sofa::core::behavior::Mass* _mass = l_mass.get(); - _mass->addGravitationalForce(params,f,x1,v1,d_gravitationalAcceleration.getValue()); + Deriv gravity; + DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); + _mass->addGravitationalForce(params,f,x1,v1,gravity); } @@ -89,7 +91,9 @@ template SReal GravityForceField::getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const { sofa::core::behavior::Mass* _mass = l_mass.get(); - return _mass->getGravitationalPotentialEnergy(params, x, d_gravitationalAcceleration.getValue()); + Deriv gravity; + DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); + return _mass->getGravitationalPotentialEnergy(params, x, gravity); } From b3fb5ff76a797412e82dd44efc77f681494d15ac Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Wed, 18 May 2022 17:29:18 +0200 Subject: [PATCH 04/41] deprecated and remove Gravity --- .../Helper/src/sofa/helper/ComponentChange.cpp | 5 +++++ modules/SofaGraphComponent/CMakeLists.txt | 1 - .../src/SofaGraphComponent/Gravity.h | 10 +++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp index 4f03a08ef4f..d4fa641e865 100644 --- a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp @@ -542,6 +542,11 @@ const std::map > uncreatableComponents /***********************/ // REMOVED SINCE v21.12 + { "Gravity", Removed("v22.06", "v22.06") }, + + /***********************/ + // REMOVED SINCE v21.12 + { "LMDNewProximityIntersection", Removed("v21.12", "v21.12") }, { "LocalMinDistanceFilter", Removed("v21.12", "v21.12") }, { "LineLocalMinDistanceFilter", Removed("v21.12", "v21.12") }, diff --git a/modules/SofaGraphComponent/CMakeLists.txt b/modules/SofaGraphComponent/CMakeLists.txt index 448ef506a22..05a0b8ea1c5 100644 --- a/modules/SofaGraphComponent/CMakeLists.txt +++ b/modules/SofaGraphComponent/CMakeLists.txt @@ -36,7 +36,6 @@ list(APPEND SOURCE_FILES ${SOFAGRAPHCOMPONENT_SRC}/AddRecordedCameraButtonSetting.cpp ${SOFAGRAPHCOMPONENT_SRC}/AttachBodyButtonSetting.cpp ${SOFAGRAPHCOMPONENT_SRC}/FixPickedParticleButtonSetting.cpp - ${SOFAGRAPHCOMPONENT_SRC}/Gravity.cpp ${SOFAGRAPHCOMPONENT_SRC}/SceneCheckDeprecatedComponents.cpp ${SOFAGRAPHCOMPONENT_SRC}/SceneCheckDuplicatedName.cpp ${SOFAGRAPHCOMPONENT_SRC}/SceneCheckMissingRequiredPlugin.cpp diff --git a/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h b/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h index b5d5e1f55f6..d9d11d1c85a 100644 --- a/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h +++ b/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h @@ -35,17 +35,17 @@ namespace sofa::component::contextobject { /** Override the default gravity */ -class SOFA_SOFAGRAPHCOMPONENT_API Gravity : public core::objectmodel::ContextObject +class SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Gravity class has been removed") Gravity : public core::objectmodel::ContextObject { public: SOFA_CLASS(Gravity, core::objectmodel::ContextObject); protected: - Gravity(); + Gravity() = default; public: - Data f_gravity; ///< Gravity in the world coordinate system + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Gravity class has been removed") + DeprecatedAndRemoved f_gravity; ///< Gravity in the world coordinate system - /// Modify the context of the Node - void apply() override; + void apply() override {}; }; } // namespace sofa::component::contextobject From 62c6b19e137f27a42b62c0eb373b0cfe69985c0f Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Wed, 18 May 2022 17:29:44 +0200 Subject: [PATCH 05/41] remove Gravity.cpp --- .../src/SofaGraphComponent/Gravity.cpp | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.cpp diff --git a/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.cpp b/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.cpp deleted file mode 100644 index 022db23ce49..00000000000 --- a/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.cpp +++ /dev/null @@ -1,49 +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 * -******************************************************************************/ -#include -#include -#include -#include -#include - - -namespace sofa::component::contextobject -{ - -using namespace sofa::type; -using namespace core::behavior; - -Gravity::Gravity() - : f_gravity( initData(&f_gravity,Vector3(0,0,0),"gravity","Gravity in the world coordinate system") ) -{ -} - -void Gravity::apply() -{ - getContext()->setGravity( f_gravity.getValue() ); -} - -int GravityClass = core::RegisterObject("Gravity in world coordinates") - .add< Gravity >() - ; - -} // namespace sofa::component::contextobject From c4f9c34d8e23e95163cb335c3b8cd7d69bf91145 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Wed, 18 May 2022 17:32:14 +0200 Subject: [PATCH 06/41] Rename and update scene for GravityForceField --- .../{misc/Gravity.scn => forcefield/GravityForceField.scn} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename examples/Components/{misc/Gravity.scn => forcefield/GravityForceField.scn} (97%) diff --git a/examples/Components/misc/Gravity.scn b/examples/Components/forcefield/GravityForceField.scn similarity index 97% rename from examples/Components/misc/Gravity.scn rename to examples/Components/forcefield/GravityForceField.scn index cdb5f1220e9..05ccddf677c 100644 --- a/examples/Components/misc/Gravity.scn +++ b/examples/Components/forcefield/GravityForceField.scn @@ -19,10 +19,10 @@ - + @@ -42,10 +42,10 @@ - + From 6ce62fad1ac092cc42ca21c36b12d40fbd9bdce2 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Wed, 18 May 2022 17:32:47 +0200 Subject: [PATCH 07/41] estetic change --- .../src/sofa/component/mechanicalload/GravityForceField.inl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl index 6cf9529bf04..cbaf974320a 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl @@ -28,8 +28,10 @@ namespace sofa::component::mechanicalload { + using namespace sofa::type; + template GravityForceField::GravityForceField() : d_gravitationalAcceleration(initData(&d_gravitationalAcceleration, "gravitationalAcceleration", "Value corresponding to the gravitational acceleration")) From e9038b19b323f7e43de810c1a295db028a86d1e7 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Thu, 19 May 2022 09:23:37 +0200 Subject: [PATCH 08/41] Temporarily moving GravityForceField into Core to avoid CMake dirty dependency --- Sofa/Component/MechanicalLoad/CMakeLists.txt | 3 - .../mechanicalload/GravityForceField.inl | 132 ------------------ Sofa/framework/Core/CMakeLists.txt | 3 + .../sofa/core/behavior}/GravityForceField.cpp | 12 +- .../sofa/core/behavior}/GravityForceField.h | 14 +- .../Core/src/sofa/core/behavior/OdeSolver.h | 24 ++++ 6 files changed, 40 insertions(+), 148 deletions(-) delete mode 100644 Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl rename Sofa/{Component/MechanicalLoad/src/sofa/component/mechanicalload => framework/Core/src/sofa/core/behavior}/GravityForceField.cpp (83%) rename Sofa/{Component/MechanicalLoad/src/sofa/component/mechanicalload => framework/Core/src/sofa/core/behavior}/GravityForceField.h (85%) diff --git a/Sofa/Component/MechanicalLoad/CMakeLists.txt b/Sofa/Component/MechanicalLoad/CMakeLists.txt index 357d33025ed..c4877de9fb2 100644 --- a/Sofa/Component/MechanicalLoad/CMakeLists.txt +++ b/Sofa/Component/MechanicalLoad/CMakeLists.txt @@ -14,8 +14,6 @@ set(HEADER_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/DiagonalVelocityDampingForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EdgePressureForceField.h ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EdgePressureForceField.inl - ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/GravityForceField.h - ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/GravityForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.h ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.h @@ -47,7 +45,6 @@ set(SOURCE_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/DiagonalVelocityDampingForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EdgePressureForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.cpp - ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/GravityForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/OscillatingTorsionPressureForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/PlaneForceField.cpp diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl deleted file mode 100644 index cbaf974320a..00000000000 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl +++ /dev/null @@ -1,132 +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 * -******************************************************************************/ -#pragma once - -#include -#include - - -namespace sofa::component::mechanicalload -{ - - -using namespace sofa::type; - - -template -GravityForceField::GravityForceField() - : d_gravitationalAcceleration(initData(&d_gravitationalAcceleration, "gravitationalAcceleration", "Value corresponding to the gravitational acceleration")) - , l_mass(initLink("mass", "link to the mass")) -{ -} - - -template -void GravityForceField::init() -{ - this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); - - if (l_mass.empty()) - { - msg_info() << "link to the mass should be set to ensure right behavior. First mass found in current context will be used."; - sofa::core::behavior::Mass* p_mass; - this->getContext()->get(p_mass); - l_mass.set(p_mass); - } - - // temprory pointer to the mass - if (sofa::core::behavior::Mass* _mass = l_mass.get()) - { - msg_info() << "Mass path used: '" << l_mass.getLinkedPath() << "'"; - } - else - { - msg_error() << "No mass component found at path: " << l_mass.getLinkedPath() << ", nor in current context: " << this->getContext()->name; - return; - } - - // init from ForceField - Inherit::init(); - - // if all init passes, component is valid - this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); -} - - -template -void GravityForceField::setGravitationalAcceleration(const DPos grav) -{ - d_gravitationalAcceleration.setValue(grav); -} - - -template -void GravityForceField::addForce(const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x1, const DataVecDeriv& v1) -{ - sofa::core::behavior::Mass* _mass = l_mass.get(); - Deriv gravity; - DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); - _mass->addGravitationalForce(params,f,x1,v1,gravity); -} - - -template -SReal GravityForceField::getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const -{ - sofa::core::behavior::Mass* _mass = l_mass.get(); - Deriv gravity; - DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); - return _mass->getGravitationalPotentialEnergy(params, x, gravity); -} - - -template -void GravityForceField::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) -{ - // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce - SOFA_UNUSED(mparams); - SOFA_UNUSED(d_df); - SOFA_UNUSED(d_dx); - mparams->setKFactorUsed(true); -} - - -template -void GravityForceField::addKToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal k, unsigned int & offset) -{ - // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce - SOFA_UNUSED(mat); - SOFA_UNUSED(k); - SOFA_UNUSED(offset); -} - - -template -void GravityForceField::addKToMatrix(const sofa::core::behavior::MultiMatrixAccessor* matrix, SReal kFact) -{ - // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce - SOFA_UNUSED(matrix); - SOFA_UNUSED(kFact); -} - - -} // namespace sofa::component::mechanicalload diff --git a/Sofa/framework/Core/CMakeLists.txt b/Sofa/framework/Core/CMakeLists.txt index d5fa1d26216..0ab2a632c4f 100644 --- a/Sofa/framework/Core/CMakeLists.txt +++ b/Sofa/framework/Core/CMakeLists.txt @@ -62,6 +62,8 @@ set(HEADER_FILES ${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.h ${SRC_ROOT}/behavior/ForceField.h ${SRC_ROOT}/behavior/ForceField.inl + ${SRC_ROOT}/behavior/GravityForceField.h + ${SRC_ROOT}/behavior/GravityForceField.inl ${SRC_ROOT}/behavior/LinearSolver.h ${SRC_ROOT}/behavior/Mass.h ${SRC_ROOT}/behavior/Mass.inl @@ -209,6 +211,7 @@ set(SOURCE_FILES ${SRC_ROOT}/behavior/ConstraintSolver.cpp ${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.cpp ${SRC_ROOT}/behavior/ForceField.cpp + ${SRC_ROOT}/behavior/GravityForceField.h ${SRC_ROOT}/behavior/LinearSolver.cpp ${SRC_ROOT}/behavior/Mass.cpp ${SRC_ROOT}/behavior/MechanicalMatrix.cpp diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp similarity index 83% rename from Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp rename to Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp index 96a33cd7a70..974c31c99d8 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp @@ -42,12 +42,12 @@ int GravityForceFieldClass = core::RegisterObject("Gravity as an external force .add< GravityForceField >() ; -template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +template class SOFA_CORE_API GravityForceField; +template class SOFA_CORE_API GravityForceField; +template class SOFA_CORE_API GravityForceField; +template class SOFA_CORE_API GravityForceField; +template class SOFA_CORE_API GravityForceField; +template class SOFA_CORE_API GravityForceField; } // namespace sofa::component::mechanicalload diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h similarity index 85% rename from Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h rename to Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h index 78f31705aff..bfafdbe81ac 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h @@ -21,7 +21,7 @@ ******************************************************************************/ #pragma once -#include +//#include #include #include @@ -78,12 +78,12 @@ class GravityForceField : public core::behavior::ForceField #if !defined(SOFA_COMPONENT_FORCEFIELD_GRAVITYFORCEFIELD_CPP) -extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; -extern template class SOFA_COMPONENT_MECHANICALLOAD_API GravityForceField; +extern template class SOFA_CORE_API GravityForceField; +extern template class SOFA_CORE_API GravityForceField; +extern template class SOFA_CORE_API GravityForceField; +extern template class SOFA_CORE_API GravityForceField; +extern template class SOFA_CORE_API GravityForceField; +extern template class SOFA_CORE_API GravityForceField; #endif } // namespace sofa::component::mechanicalload diff --git a/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h b/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h index 98c67f64fd3..b42dda301ad 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h @@ -23,6 +23,9 @@ #include +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +#include "GravityForceField.h" + namespace sofa::core::behavior { @@ -124,6 +127,27 @@ class SOFA_CORE_API OdeSolver : public virtual objectmodel::BaseObject bool insertInNode( objectmodel::BaseNode* node ) override; bool removeInNode( objectmodel::BaseNode* node ) override; + + // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") + template + static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) + { + typename T::SPtr obj = sofa::core::objectmodel::New(); + if (context) context->addObject(obj); + if (arg) obj->parse(arg); + + const sofa::core::objectmodel::BaseContext::Vec3& gravity = context->getGravity(); + SReal gravityNorm = gravity.norm(); + if(gravityNorm!=0.0) + { + sofa::Size dim = context->getMechanicalState()->getDerivDimension(); +// GravityForceField::SPtr gravityFF = sofa::core::objectmodel::New>(root); +// gravityFF->setName(root->getNameHelper().resolveName(gravityFF->getClassName(), sofa::core::ComponentNameHelper::Convention::python)); +// context->addObject(gravityFF,sofa::core::objectmodel::TypeOfInsertion::AtEnd); + } + + return obj; + } }; } // namespace sofa::core::behavior From 58b5c5090acf9a43bfd3aab774977f05e9981e16 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Thu, 19 May 2022 09:24:25 +0200 Subject: [PATCH 09/41] add forgotten file in the move --- .../sofa/core/behavior/GravityForceField.inl | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl new file mode 100644 index 00000000000..cbaf974320a --- /dev/null +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl @@ -0,0 +1,132 @@ +/****************************************************************************** +* 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 +{ + + +using namespace sofa::type; + + +template +GravityForceField::GravityForceField() + : d_gravitationalAcceleration(initData(&d_gravitationalAcceleration, "gravitationalAcceleration", "Value corresponding to the gravitational acceleration")) + , l_mass(initLink("mass", "link to the mass")) +{ +} + + +template +void GravityForceField::init() +{ + this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); + + if (l_mass.empty()) + { + msg_info() << "link to the mass should be set to ensure right behavior. First mass found in current context will be used."; + sofa::core::behavior::Mass* p_mass; + this->getContext()->get(p_mass); + l_mass.set(p_mass); + } + + // temprory pointer to the mass + if (sofa::core::behavior::Mass* _mass = l_mass.get()) + { + msg_info() << "Mass path used: '" << l_mass.getLinkedPath() << "'"; + } + else + { + msg_error() << "No mass component found at path: " << l_mass.getLinkedPath() << ", nor in current context: " << this->getContext()->name; + return; + } + + // init from ForceField + Inherit::init(); + + // if all init passes, component is valid + this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); +} + + +template +void GravityForceField::setGravitationalAcceleration(const DPos grav) +{ + d_gravitationalAcceleration.setValue(grav); +} + + +template +void GravityForceField::addForce(const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x1, const DataVecDeriv& v1) +{ + sofa::core::behavior::Mass* _mass = l_mass.get(); + Deriv gravity; + DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); + _mass->addGravitationalForce(params,f,x1,v1,gravity); +} + + +template +SReal GravityForceField::getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const +{ + sofa::core::behavior::Mass* _mass = l_mass.get(); + Deriv gravity; + DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); + return _mass->getGravitationalPotentialEnergy(params, x, gravity); +} + + +template +void GravityForceField::addDForce(const core::MechanicalParams* mparams, DataVecDeriv& d_df , const DataVecDeriv& d_dx) +{ + // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce + SOFA_UNUSED(mparams); + SOFA_UNUSED(d_df); + SOFA_UNUSED(d_dx); + mparams->setKFactorUsed(true); +} + + +template +void GravityForceField::addKToMatrix(sofa::linearalgebra::BaseMatrix * mat, SReal k, unsigned int & offset) +{ + // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce + SOFA_UNUSED(mat); + SOFA_UNUSED(k); + SOFA_UNUSED(offset); +} + + +template +void GravityForceField::addKToMatrix(const sofa::core::behavior::MultiMatrixAccessor* matrix, SReal kFact) +{ + // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce + SOFA_UNUSED(matrix); + SOFA_UNUSED(kFact); +} + + +} // namespace sofa::component::mechanicalload From d2e0797cd841bfa501d7dfcc5507baf7a699d26f Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Tue, 24 May 2022 16:49:02 +0200 Subject: [PATCH 10/41] Removing the addMDxToVector function related to the gravity --- .../src/sofa/component/mass/UniformMass.cpp | 33 ------------------- .../src/sofa/component/mass/UniformMass.h | 7 +--- .../src/sofa/component/mass/UniformMass.inl | 13 -------- 3 files changed, 1 insertion(+), 52 deletions(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp index 59ea39806d3..54fff4576dc 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp @@ -389,30 +389,6 @@ SReal UniformMass::getGravitationalPotentialEnergyRigidImpl(const core return e; } -template -template -void UniformMass::addMDxToVectorVecImpl(linearalgebra::BaseVector *resVect, - const VecDeriv* dx, - SReal mFact, - unsigned int& offset) -{ - unsigned int derivDim = (unsigned)Deriv::size(); - double m = d_vertexMass.getValue(); - - ReadAccessor > indices = d_indices; - - const SReal* g = getContext()->getGravity().ptr(); - - for (unsigned int i=0; iadd(offset + indices[i] * derivDim + j, mFact * m * g[j] * (*dx)[indices[i]][0]); - else - resVect->add(offset + indices[i] * derivDim + j, mFact * m * g[j]); - } -} - template<> SOFA_COMPONENT_MASS_API void UniformMass::constructor_message() @@ -473,15 +449,6 @@ void UniformMass::draw(const core::visual::VisualParams* vparams) drawVec6Impl(vparams) ; } -template <> SOFA_COMPONENT_MASS_API -void UniformMass::addMDxToVector(linearalgebra::BaseVector *resVect, - const VecDeriv* dx, - SReal mFact, - unsigned int& offset) -{ - addMDxToVectorVecImpl(resVect, dx,mFact,offset) ; -} - template <> SOFA_COMPONENT_MASS_API Vector6 UniformMass::getMomentum ( const MechanicalParams* params, const DataVecCoord& d_x, diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index d748611c54d..ef0eac81f66 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -140,6 +140,7 @@ class UniformMass : public core::behavior::Mass SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& d_v) const override; ///< vMv/2 using dof->getV() override type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the addMDxToVector function related to the gravity") void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset); void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; /// Add Mass contribution to global Matrix assembling @@ -197,12 +198,6 @@ class UniformMass : public core::behavior::Mass template void loadFromFileRigidImpl(const std::string& filename) ; - template - void addMDxToVectorVecImpl(linearalgebra::BaseVector *resVect, - const VecDeriv* dx, - SReal mFact, - unsigned int& offset); - }; // Specialization for rigids diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl index 9dc5b7ee2e4..d6317d14d19 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl @@ -411,19 +411,6 @@ void UniformMass::accFromF ( const core::MechanicalParams*, a[indices[i]] = f[indices[i]] / m; } - -template -void UniformMass::addMDxToVector ( BaseVector * resVect, - const VecDeriv* dx, - SReal mFact, - unsigned int& offset ) -{ - SOFA_UNUSED(resVect); - SOFA_UNUSED(dx); - SOFA_UNUSED(mFact); - SOFA_UNUSED(offset); -} - template void UniformMass::addGravitationalForce ( const core::MechanicalParams* mparams, DataVecDeriv& vf, const DataVecCoord& x, const DataVecDeriv& v, const Deriv& gravity) { From 9c90034c4b52f199d8c0439c053160c4472ff95c Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 13:10:58 +0200 Subject: [PATCH 11/41] fix typo missing .cpp in CMakeLists --- Sofa/framework/Core/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/framework/Core/CMakeLists.txt b/Sofa/framework/Core/CMakeLists.txt index 0ab2a632c4f..6cf878f2f75 100644 --- a/Sofa/framework/Core/CMakeLists.txt +++ b/Sofa/framework/Core/CMakeLists.txt @@ -211,7 +211,7 @@ set(SOURCE_FILES ${SRC_ROOT}/behavior/ConstraintSolver.cpp ${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.cpp ${SRC_ROOT}/behavior/ForceField.cpp - ${SRC_ROOT}/behavior/GravityForceField.h + ${SRC_ROOT}/behavior/GravityForceField.cpp ${SRC_ROOT}/behavior/LinearSolver.cpp ${SRC_ROOT}/behavior/Mass.cpp ${SRC_ROOT}/behavior/MechanicalMatrix.cpp From 190ab9c8a3d2390152d2a36f87e4a90ed9e3d927 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 13:11:32 +0200 Subject: [PATCH 12/41] Clean in OdeSolver.cpp --- Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.cpp b/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.cpp index 27b57c6cf7c..490615376c5 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.cpp @@ -40,12 +40,6 @@ OdeSolver::~OdeSolver() {} -//const OdeSolver::MechanicalMatrix OdeSolver::M(1,0,0); -//const OdeSolver::MechanicalMatrix OdeSolver::B(0,1,0); -//const OdeSolver::MechanicalMatrix OdeSolver::K(0,0,1); - - - bool OdeSolver::insertInNode( objectmodel::BaseNode* node ) { node->addOdeSolver(this); From e6a910c987441489084c64ba5116d35b69cce709 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 13:12:52 +0200 Subject: [PATCH 13/41] Improve transition comment messages for GravityForceField --- .../Core/src/sofa/core/behavior/GravityForceField.cpp | 7 +++++-- .../Core/src/sofa/core/behavior/GravityForceField.h | 4 ++-- .../Core/src/sofa/core/behavior/GravityForceField.inl | 5 ++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp index 974c31c99d8..a0cd8fddd9b 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp @@ -21,7 +21,10 @@ ******************************************************************************/ #define SOFA_COMPONENT_FORCEFIELD_GRAVITYFORCEFIELD_CPP -#include +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +//#include // TO UNCOMMENT +#include "GravityForceField.inl" // TO REMOVE + #include #include @@ -42,6 +45,7 @@ int GravityForceFieldClass = core::RegisterObject("Gravity as an external force .add< GravityForceField >() ; + template class SOFA_CORE_API GravityForceField; template class SOFA_CORE_API GravityForceField; template class SOFA_CORE_API GravityForceField; @@ -49,5 +53,4 @@ template class SOFA_CORE_API GravityForceField; template class SOFA_CORE_API GravityForceField; template class SOFA_CORE_API GravityForceField; - } // namespace sofa::component::mechanicalload diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h index bfafdbe81ac..945dfbacf69 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h @@ -21,8 +21,8 @@ ******************************************************************************/ #pragma once -//#include - +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +//#include // TO UNCOMMENT #include #include #include diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl index cbaf974320a..595f8fd852f 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl @@ -21,7 +21,10 @@ ******************************************************************************/ #pragma once -#include +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +//#include // TO UNCOMMENT +#include "GravityForceField.h" // TO REMOVE + #include From 0a5ce8818a971cc4a487d412dd73f37abc7bee20 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 13:13:53 +0200 Subject: [PATCH 14/41] Create automatic creation of GravityForceField from create function of Mass, clean of OdeSolver --- .../Core/src/sofa/core/behavior/Mass.h | 53 +++++++++++++++++++ .../Core/src/sofa/core/behavior/OdeSolver.h | 24 --------- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index b911d82a58a..4253c639079 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -25,6 +25,9 @@ #include #include +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +#include // TO REMOVE + namespace sofa::core::behavior { @@ -168,6 +171,56 @@ class Mass : virtual public ForceField, public BaseMass bool insertInNode( objectmodel::BaseNode* node ) override { BaseMass::insertInNode(node); BaseForceField::insertInNode(node); return true; } bool removeInNode( objectmodel::BaseNode* node ) override { BaseMass::removeInNode(node); BaseForceField::removeInNode(node); return true; } + + // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") + template + static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) + { + typename T::SPtr obj = sofa::core::objectmodel::New(); + if (context) context->addObject(obj); + if (arg) obj->parse(arg); + + const sofa::core::objectmodel::BaseContext::Vec3& gravity = context->getRootContext()->getGravity(); + SReal gravityNorm = gravity.norm(); + if(gravityNorm!=0.0) + { + bool savePLog = context->f_printLog.getValue(); + context->f_printLog.setValue(true); + msg_info(context) << "A gravity seem to apply in the node \"" << context->getName() << "\" using the deprecated gravity mechanism (read more in PR#XXXX)." << msgendl + << "A GravityForceField is automatically added in the node \"" << context->getName() << "\"."; + context->f_printLog.setValue(savePLog); + + const sofa::Size dim = context->getMechanicalState()->getDerivDimension(); + const std::string templated = context->getMechanicalState()->getTemplateName(); + std::string gravity_string; + + if(dim == 1) + gravity_string = std::to_string(gravity[0]); + else if(dim == 2) + gravity_string = std::to_string(gravity[0])+" "+std::to_string(gravity[1]); + else + gravity_string = std::to_string(gravity[0])+" "+std::to_string(gravity[1])+" "+std::to_string(gravity[2]); + + + sofa::core::objectmodel::BaseObjectDescription desc("GravityForceField","GravityForceField"); + desc.setAttribute("template", templated); + desc.setAttribute("gravitationalAcceleration", gravity_string); + + /// Create the object. + BaseObject::SPtr obj = sofa::core::ObjectFactory::getInstance()->createObject(context, &desc); + if (obj==nullptr) + { + std::stringstream msg; + msg << "Component '" << desc.getName() << "' of type '" << desc.getAttribute("type","") << "' failed:" << msgendl ; + for (std::vector< std::string >::const_iterator it = desc.getErrors().begin(); it != desc.getErrors().end(); ++it) + msg << " " << *it << msgendl ; + msg_error(context) << msg.str() ; + return nullptr; + } + } + + return obj; + } }; diff --git a/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h b/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h index b42dda301ad..98c67f64fd3 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/OdeSolver.h @@ -23,9 +23,6 @@ #include -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") -#include "GravityForceField.h" - namespace sofa::core::behavior { @@ -127,27 +124,6 @@ class SOFA_CORE_API OdeSolver : public virtual objectmodel::BaseObject bool insertInNode( objectmodel::BaseNode* node ) override; bool removeInNode( objectmodel::BaseNode* node ) override; - - // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") - template - static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) - { - typename T::SPtr obj = sofa::core::objectmodel::New(); - if (context) context->addObject(obj); - if (arg) obj->parse(arg); - - const sofa::core::objectmodel::BaseContext::Vec3& gravity = context->getGravity(); - SReal gravityNorm = gravity.norm(); - if(gravityNorm!=0.0) - { - sofa::Size dim = context->getMechanicalState()->getDerivDimension(); -// GravityForceField::SPtr gravityFF = sofa::core::objectmodel::New>(root); -// gravityFF->setName(root->getNameHelper().resolveName(gravityFF->getClassName(), sofa::core::ComponentNameHelper::Convention::python)); -// context->addObject(gravityFF,sofa::core::objectmodel::TypeOfInsertion::AtEnd); - } - - return obj; - } }; } // namespace sofa::core::behavior From e4cca6c5ab12994ca9d6698c4d6401c785800ce1 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 13:14:25 +0200 Subject: [PATCH 15/41] Add warning in Node to present using the data gravity and refer PR --- .../Simulation/Core/src/sofa/simulation/Node.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index 0ba0d61ff0f..9b51600fb3e 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -169,6 +169,17 @@ void Node::parse( sofa::core::objectmodel::BaseObjectDescription* arg ) objDesc.setAttribute("displayFlags", oldFlags); sofa::core::objectmodel::BaseObject::SPtr obj = sofa::core::ObjectFactory::CreateObject(this, &objDesc); } + + // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") + const char* gravityStr = arg->getAttribute("gravity", nullptr); + if (gravityStr != nullptr) + { + msg_warning() << "A \"gravity\" data has been detected in the context of your node \"" << name << "\": this data is DEPRECATED since PR#XXXX." << msgendl + "Note that the gravity should now be applied using a GravityForceField for each object." << msgendl + "To remove this warning:" << msgendl + " - remove the \"gravity\" data from all nodes" << msgendl + " - add a GravityForceField with the data \"gravitationalAcceleration\" for each object undergoing the gravity"; + } } /// Initialize the components of this node and all the nodes which depend on it. From 4f09c0539a7f0426506b8265ae6fe6a280635579 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 13:48:15 +0200 Subject: [PATCH 16/41] minor typo of version in ComponentChange --- Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp index d4fa641e865..16ff70b7381 100644 --- a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp @@ -540,7 +540,7 @@ const std::map > uncreatableComponents //{ "UniformMass", Pluginized("v20.12", "SofaBaseMechanics") }, /***********************/ - // REMOVED SINCE v21.12 + // REMOVED SINCE v22.06 { "Gravity", Removed("v22.06", "v22.06") }, From 28261e9592dd6fc00ba33043fa61be5ad14cb962 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 29 May 2022 14:09:46 +0200 Subject: [PATCH 17/41] Update PR number in the code deprecation --- Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h | 2 +- Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h | 4 ++-- .../Core/src/sofa/core/behavior/GravityForceField.cpp | 2 +- .../framework/Core/src/sofa/core/behavior/GravityForceField.h | 2 +- .../Core/src/sofa/core/behavior/GravityForceField.inl | 2 +- Sofa/framework/Core/src/sofa/core/behavior/Mass.h | 4 ++-- .../Core/src/sofa/simulation/MechanicalOperations.h | 2 +- Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp | 4 ++-- modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h | 4 ++-- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index ef0eac81f66..31d773e50d3 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -140,7 +140,7 @@ class UniformMass : public core::behavior::Mass SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& d_v) const override; ///< vMv/2 using dof->getV() override type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the addMDxToVector function related to the gravity") + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the addMDxToVector function related to the gravity") void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset); void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; /// Add Mass contribution to global Matrix assembling diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h index 98691403ca6..7d880fade08 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h @@ -75,9 +75,9 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor /// @} - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the separate gravity API.") + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the separate gravity API.") virtual void addGravityToV(const MechanicalParams* mparams, MultiVecDerivId vid) = delete; - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the separate gravity API.") + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the separate gravity API.") DeprecatedAndRemoved m_separateGravity; diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp index a0cd8fddd9b..c443be52ef5 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp @@ -21,7 +21,7 @@ ******************************************************************************/ #define SOFA_COMPONENT_FORCEFIELD_GRAVITYFORCEFIELD_CPP -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") //#include // TO UNCOMMENT #include "GravityForceField.inl" // TO REMOVE diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h index 945dfbacf69..ccff777f055 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h @@ -21,7 +21,7 @@ ******************************************************************************/ #pragma once -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") //#include // TO UNCOMMENT #include #include diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl index 595f8fd852f..4eef336a013 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl @@ -21,7 +21,7 @@ ******************************************************************************/ #pragma once -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") //#include // TO UNCOMMENT #include "GravityForceField.h" // TO REMOVE diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 4253c639079..3031397bd6a 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -25,7 +25,7 @@ #include #include -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") +// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") #include // TO REMOVE namespace sofa::core::behavior @@ -172,7 +172,7 @@ class Mass : virtual public ForceField, public BaseMass bool removeInNode( objectmodel::BaseNode* node ) override { BaseMass::removeInNode(node); BaseForceField::removeInNode(node); return true; } - // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") + // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") template static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) { diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h index 67effc0cc12..d7c50da0300 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h @@ -86,7 +86,7 @@ class SOFA_SIMULATION_CORE_API MechanicalOperations void addMBKv(core::MultiVecDerivId df, SReal m, SReal b, SReal k, bool clear = true, bool accumulate = true); - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#29XX)", "v22.12", "Removing the separate gravity API.") + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the separate gravity API.") void addSeparateGravity(SReal dt, core::MultiVecDerivId result = core::VecDerivId::velocity() ) = delete; void computeContactForce(core::MultiVecDerivId result); diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index 9b51600fb3e..2944bee6ddf 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -170,11 +170,11 @@ void Node::parse( sofa::core::objectmodel::BaseObjectDescription* arg ) sofa::core::objectmodel::BaseObject::SPtr obj = sofa::core::ObjectFactory::CreateObject(this, &objDesc); } - // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Transition removing gravity and introducing GravityForceField") + // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") const char* gravityStr = arg->getAttribute("gravity", nullptr); if (gravityStr != nullptr) { - msg_warning() << "A \"gravity\" data has been detected in the context of your node \"" << name << "\": this data is DEPRECATED since PR#XXXX." << msgendl + msg_warning() << "A \"gravity\" data has been detected in the context of your node \"" << name << "\": this data is DEPRECATED since PR#2988." << msgendl "Note that the gravity should now be applied using a GravityForceField for each object." << msgendl "To remove this warning:" << msgendl " - remove the \"gravity\" data from all nodes" << msgendl diff --git a/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h b/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h index d9d11d1c85a..86421ca8902 100644 --- a/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h +++ b/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h @@ -35,14 +35,14 @@ namespace sofa::component::contextobject { /** Override the default gravity */ -class SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Gravity class has been removed") Gravity : public core::objectmodel::ContextObject +class SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Gravity class has been removed") Gravity : public core::objectmodel::ContextObject { public: SOFA_CLASS(Gravity, core::objectmodel::ContextObject); protected: Gravity() = default; public: - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#XXXX)", "v22.12", "Gravity class has been removed") + SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Gravity class has been removed") DeprecatedAndRemoved f_gravity; ///< Gravity in the world coordinate system void apply() override {}; From 421b68f18d9677eea81fe872be17e26e6d86ebbb Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Wed, 8 Jun 2022 11:17:33 +0200 Subject: [PATCH 18/41] Add PR correct number. --- Sofa/framework/Core/src/sofa/core/behavior/Mass.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 3031397bd6a..08015cbd30f 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -186,7 +186,7 @@ class Mass : virtual public ForceField, public BaseMass { bool savePLog = context->f_printLog.getValue(); context->f_printLog.setValue(true); - msg_info(context) << "A gravity seem to apply in the node \"" << context->getName() << "\" using the deprecated gravity mechanism (read more in PR#XXXX)." << msgendl + msg_info(context) << "A gravity seem to apply in the node \"" << context->getName() << "\" using the deprecated gravity mechanism (read more in PR#2988)." << msgendl << "A GravityForceField is automatically added in the node \"" << context->getName() << "\"."; context->f_printLog.setValue(savePLog); From 50b92075a70daecb4cfb7726480969c5c86a51bb Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Tue, 12 Jul 2022 17:50:44 +0200 Subject: [PATCH 19/41] fix header wrong msg --- .../Core/src/sofa/core/behavior/GravityForceField.inl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl index 4eef336a013..2ad0eee143a 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl @@ -21,13 +21,9 @@ ******************************************************************************/ #pragma once -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") -//#include // TO UNCOMMENT -#include "GravityForceField.h" // TO REMOVE - +#include "GravityForceField.h" #include - namespace sofa::component::mechanicalload { From 92695952ac64bc53aa8ef7234929bf42f0236226 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Tue, 12 Jul 2022 17:52:57 +0200 Subject: [PATCH 20/41] check non null pointer in getPotentialEnergy --- .../Core/src/sofa/core/behavior/GravityForceField.inl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl index 2ad0eee143a..7843db76a91 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl @@ -52,7 +52,7 @@ void GravityForceField::init() l_mass.set(p_mass); } - // temprory pointer to the mass + // temporary pointer to the mass if (sofa::core::behavior::Mass* _mass = l_mass.get()) { msg_info() << "Mass path used: '" << l_mass.getLinkedPath() << "'"; @@ -94,7 +94,10 @@ SReal GravityForceField::getPotentialEnergy(const core::MechanicalPar sofa::core::behavior::Mass* _mass = l_mass.get(); Deriv gravity; DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); - return _mass->getGravitationalPotentialEnergy(params, x, gravity); + if(_mass) + return _mass->getGravitationalPotentialEnergy(params, x, gravity); + else + return 0.0; } From 333d66002bbfe54ea82e5e8d3d271d07dd94bb06 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Tue, 12 Jul 2022 20:41:41 +0200 Subject: [PATCH 21/41] Separate Mass from ForceField API - experimental --- .../src/sofa/component/mass/DiagonalMass.cpp | 4 +- .../src/sofa/component/mass/DiagonalMass.h | 1 - .../sofa/component/mass/MeshMatrixMass.cpp | 12 ++--- .../src/sofa/component/mass/UniformMass.cpp | 12 ++--- .../src/sofa/component/mass/UniformMass.h | 1 - .../src/sofa/component/mass/UniformMass.inl | 12 ++--- .../elastic/HexahedralFEMForceFieldAndMass.h | 5 +- .../HexahedralFEMForceFieldAndMass.inl | 10 ++-- .../elastic/HexahedronFEMForceFieldAndMass.h | 4 +- .../HexahedronFEMForceFieldAndMass.inl | 4 +- ...exahedronCompositeFEMForceFieldAndMass.inl | 4 +- ...nUniformHexahedralFEMForceFieldAndMass.inl | 10 ++-- ...nUniformHexahedronFEMForceFieldAndMass.inl | 5 +- .../Core/src/sofa/core/behavior/BaseMass.cpp | 16 +++++++ .../Core/src/sofa/core/behavior/BaseMass.h | 36 ++++++++++++++ .../Core/src/sofa/core/behavior/Mass.h | 44 ++--------------- .../Core/src/sofa/core/behavior/Mass.inl | 48 ++----------------- .../MechanicalAddMBK_ToMatrixVisitor.cpp | 13 ++++- .../MechanicalAddMBK_ToMatrixVisitor.h | 4 +- .../MechanicalAddMBKdxVisitor.cpp | 7 +++ .../MechanicalAddMBKdxVisitor.h | 1 + 21 files changed, 127 insertions(+), 126 deletions(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp index b18d71eec01..1ee2654e0f6 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp @@ -61,7 +61,7 @@ void DiagonalMass::drawRigid3dImpl(const VisualPar { const MassVector &masses= d_vertexMass.getValue(); if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x =mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x =this->mstate->read(core::ConstVecCoordId::position())->getValue(); if(masses.size() != x.size()) return; @@ -109,7 +109,7 @@ void DiagonalMass::drawRigid2dImpl(const VisualPar { const MassVector &masses= d_vertexMass.getValue(); if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x =mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x =this->mstate->read(core::ConstVecCoordId::position())->getValue(); for (unsigned int i=0; i /// Bring inherited attributes and function in the current lookup context. /// otherwise any access to the base::attribute would require /// the "this->" approach. - using core::behavior::ForceField::mstate ; using core::objectmodel::BaseObject::getContext; //////////////////////////////////////////////////////////////////////////// diff --git a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp index 642d7df81ee..a3ab5b79586 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp @@ -43,9 +43,9 @@ Vector6 MeshMatrixMass::getMomentum ( const core::MechanicalParams*, for( unsigned int i=0 ; igetEdges(); @@ -58,14 +58,14 @@ Vector6 MeshMatrixMass::getMomentum ( const core::MechanicalParams*, const MassType m = edgeMass[i] * static_cast(0.5); Deriv linearMomentum = v[v0] * m; - for( int j=0 ; j::loadFromFileRigidImpl(const string& filename) } setMass(m); } - else if (d_totalMass.getValue()>0 && mstate!=nullptr) d_vertexMass.setValue((Real)d_totalMass.getValue() / mstate->getSize()); + else if (d_totalMass.getValue()>0 && this->mstate!=nullptr) d_vertexMass.setValue((Real)d_totalMass.getValue() / this->mstate->getSize()); } @@ -208,7 +208,7 @@ void UniformMass::drawRigid2DImpl(const VisualParams* vparams) if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x =mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x =this->mstate->read(core::ConstVecCoordId::position())->getValue(); ReadAccessor > indices = d_indices; type::Vec3d len; @@ -231,7 +231,7 @@ void UniformMass::drawRigid3DImpl(const VisualParams* vparams) if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x =mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x =this->mstate->read(core::ConstVecCoordId::position())->getValue(); ReadAccessor > indices = d_indices; typename RigidTypes::Vec3 gravityCenter; type::Vec3d len; @@ -261,7 +261,7 @@ void UniformMass::drawRigid3DImpl(const VisualParams* vparams) if (d_showInitialCenterOfGravity.getValue()) { - const VecCoord& x0 = mstate->read(core::ConstVecCoordId::restPosition())->getValue(); + const VecCoord& x0 = this->mstate->read(core::ConstVecCoordId::restPosition())->getValue(); for (unsigned int i=0; idrawTool()->drawFrame(x0[indices[i]].getCenter(), x0[indices[i]].getOrientation(), len*d_showAxisSize.getValue()); @@ -281,8 +281,8 @@ void UniformMass::drawVec6Impl(const core::visual::VisualParams* vpar { if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x =mstate->read(core::ConstVecCoordId::position())->getValue(); - const VecCoord& x0 = mstate->read(core::ConstVecCoordId::restPosition())->getValue(); + const VecCoord& x =this->mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x0 = this->mstate->read(core::ConstVecCoordId::restPosition())->getValue(); ReadAccessor > indices = d_indices; Mat3x3d R; R.identity(); diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index 31d773e50d3..63389becb4d 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -84,7 +84,6 @@ class UniformMass : public core::behavior::Mass /// Bring inherited attributes and function in the current lookup context. /// otherwise any access to the base::attribute would require /// the "this->" approach. - using core::behavior::ForceField::mstate ; using core::objectmodel::BaseObject::getContext; //////////////////////////////////////////////////////////////////////////// diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl index c789be74888..b5633704e79 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl @@ -135,7 +135,7 @@ void UniformMass::initDefaultImpl() WriteAccessor > indices = d_indices; - if(mstate==nullptr) + if(this->mstate==nullptr) { msg_warning(this) << "Missing mechanical state. \n" "UniformMass need to be used with an object also having a MechanicalState. \n" @@ -152,7 +152,7 @@ void UniformMass::initDefaultImpl() //If localRange is set, update indices if (d_localRange.getValue()[0] >= 0 && d_localRange.getValue()[1] > 0 - && d_localRange.getValue()[1] + 1 < int(mstate->getSize())) + && d_localRange.getValue()[1] + 1 < int(this->mstate->getSize())) { indices.clear(); for(int i=d_localRange.getValue()[0]; i<=d_localRange.getValue()[1]; i++) @@ -163,7 +163,7 @@ void UniformMass::initDefaultImpl() if(indices.size()==0) { indices.clear(); - for(int i=0; igetSize()); i++) + for(int i=0; imstate->getSize()); i++) indices.push_back(i); } @@ -514,7 +514,7 @@ void UniformMass::addMToMatrix (const MechanicalParams *mparams, static constexpr auto N = Deriv::total_size; AddMToMatrixFunctor calc; - MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(mstate); + MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(this->mstate); const Real mFactor = Real(sofa::core::mechanicalparams::mFactorIncludingRayleighDamping(mparams, this->rayleighMass.getValue())); @@ -554,13 +554,13 @@ void UniformMass::draw(const VisualParams* vparams) if ( !vparams->displayFlags().getShowBehaviorModels() ) return; - if (!mstate.get()) + if (!this->mstate.get()) return; if (!d_showCenterOfGravity.getValue()) return; - ReadAccessor x = mstate->read(ConstVecCoordId::position())->getValue(); + ReadAccessor x = this->mstate->read(ConstVecCoordId::position())->getValue(); ReadAccessor > indices = d_indices; Coord gravityCenter; diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h index 1f5b8dca1ea..89272f564b8 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h @@ -75,7 +75,7 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass bool isDiagonal() const override { return _useLumpedMass.getValue(); } using HexahedralFEMForceFieldT::addKToMatrix; - using MassT::addKToMatrix; + ///// WARNING this method only add diagonal elements in the given matrix ! void addKToMatrix(const core::MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; @@ -125,6 +125,9 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass void computeLumpedMasses(); + bool insertInNode( core::objectmodel::BaseNode* node ) override { return HexahedralFEMForceFieldT::insertInNode(node); } + bool removeInNode( core::objectmodel::BaseNode* node ) override { return HexahedralFEMForceFieldT::removeInNode(node); } + protected: Data _density; ///< density == volumetric mass in english (kg.m-3) Data _useLumpedMass; ///< Does it use lumped masses? diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl index d148ccfddb7..2f5ce51d0c5 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl @@ -131,7 +131,7 @@ void HexahedralFEMForceFieldAndMass::computeLumpedMasses( ) template void HexahedralFEMForceFieldAndMass::computeElementMasses( ) { - const VecCoord& initialPoints = this->mstate->read(core::ConstVecCoordId::restPosition())->getValue(); + const VecCoord& initialPoints = HexahedralFEMForceFieldT::mstate->read(core::ConstVecCoordId::restPosition())->getValue(); const VecElement& hexahedra = this->_topology->getHexahedra(); @@ -261,7 +261,7 @@ void HexahedralFEMForceFieldAndMass::addMToMatrix(const core::Mechani const VecElement& hexahedra = this->_topology->getHexahedra(); - sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(this->mstate); + sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(HexahedralFEMForceFieldT::mstate); for(unsigned int e=0; e::addKToMatrix(const core::Mechani Index node1, node2; const VecElement& hexahedra = this->_topology->getHexahedra(); - sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(this->mstate); + sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(HexahedralFEMForceFieldT::mstate); for(it = this->hexahedronInfo.getValue().begin(), e=0 ; it != this->hexahedronInfo.getValue().end() ; ++it,++e) { @@ -350,7 +350,7 @@ void HexahedralFEMForceFieldAndMass::addMBKToMatrix (const core::Mech typename type::vector::const_iterator it; - sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(this->mstate); + sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(HexahedralFEMForceFieldT::mstate); unsigned int e = 0; for ( it = this->hexahedronInfo.getValue().begin() ; it != this->hexahedronInfo.getValue().end() ; ++it, ++e ) @@ -447,7 +447,7 @@ void HexahedralFEMForceFieldAndMass::draw(const core::visual::VisualP if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x = HexahedralFEMForceFieldT::mstate->read(core::ConstVecCoordId::position())->getValue(); // since drawTool requires a std::vector we have to convert x in an ugly way std::vector pos; pos.resize(x.size()); diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h index 707a919f787..ca9c4c265a9 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h @@ -78,7 +78,7 @@ class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass::addKToMatrix; + void addKToMatrix(const core::MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override { HexahedronFEMForceFieldT::addKToMatrix(mparams, matrix); @@ -123,6 +123,8 @@ class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass::addMToMatrix(const core::Mechani int node1, node2; - sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(this->mstate); + sofa::core::behavior::MultiMatrixAccessor::MatrixRef r = matrix->getMatrix(HexahedronFEMForceFieldT::mstate); for(it = this->getIndexedElements()->begin(), e=0 ; it != this->getIndexedElements()->end() ; ++it,++e) { @@ -305,7 +305,7 @@ void HexahedronFEMForceFieldAndMass::draw(const core::visual::VisualP if (!vparams->displayFlags().getShowBehaviorModels()) return; - const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x = HexahedronFEMForceFieldT::mstate->read(core::ConstVecCoordId::position())->getValue(); // since drawTool requires a std::vector we have to convert x in an ugly way std::vector pos; pos.resize(x.size()); diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/HexahedronCompositeFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/HexahedronCompositeFEMForceFieldAndMass.inl index 968125699ed..1c161de9529 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/HexahedronCompositeFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/HexahedronCompositeFEMForceFieldAndMass.inl @@ -1547,7 +1547,7 @@ template void HexahedronCompositeFEMForceFieldAndMass::draw(const core::visual::VisualParams* vparams) { if (!vparams->displayFlags().getShowForceFields()) return; - if (!this->mstate) return; + if (!HexahedronFEMForceFieldT::mstate) return; if (vparams->displayFlags().getShowWireFrame()) return; @@ -1557,7 +1557,7 @@ void HexahedronCompositeFEMForceFieldAndMass::draw(const core::visual::Visual vparams->drawTool()->saveLastState(); - const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& x = HexahedronFEMForceFieldT::mstate->read(core::ConstVecCoordId::position())->getValue(); sofa::type::RGBAColor colour; diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.inl index 310945ec412..44e5648f02b 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.inl @@ -91,7 +91,7 @@ void NonUniformHexahedralFEMForceFieldAndMass::reinit() return; } - const VecCoord& X0=this->mstate->read(core::ConstVecCoordId::restPosition())->getValue(); + const VecCoord& X0=HexahedralFEMForceFieldT::mstate->read(core::ConstVecCoordId::restPosition())->getValue(); type::Vec<8,Coord> nodesCoarse; for(int w=0; w<8; ++w) nodesCoarse[w] = (X0)[this->_topology->getHexahedron(0)[w]]; @@ -428,7 +428,7 @@ template void NonUniformHexahedralFEMForceFieldAndMass::initLarge( const int i) { - const VecCoord& X0=this->mstate->read(core::ConstVecCoordId::restPosition())->getValue(); + const VecCoord& X0=HexahedralFEMForceFieldT::mstate->read(core::ConstVecCoordId::restPosition())->getValue(); type::Vec<8,Coord> nodes; for(int w=0; w<8; ++w) @@ -463,7 +463,7 @@ template void NonUniformHexahedralFEMForceFieldAndMass::initPolar( const int i) { - const VecCoord& X0=this->mstate->read(core::ConstVecCoordId::restPosition())->getValue(); + const VecCoord& X0=HexahedralFEMForceFieldT::mstate->read(core::ConstVecCoordId::restPosition())->getValue(); type::Vec<8,Coord> nodes; for(int j=0; j<8; ++j) @@ -934,8 +934,8 @@ void NonUniformHexahedralFEMForceFieldAndMass::addMBKdx(const core::M { Real mFactor=(Real)sofa::core::mechanicalparams::mFactorIncludingRayleighDamping(mparams, this->rayleighMass.getValue()); Real kFactor=(Real)sofa::core::mechanicalparams::kFactorIncludingRayleighDamping(mparams, this->rayleighStiffness.getValue()); - helper::ReadAccessor < DataVecDeriv > dx = *mparams->readDx(this->mstate); - helper::WriteAccessor< DataVecDeriv > df = *dfId[this->mstate.get()].write(); + helper::ReadAccessor < DataVecDeriv > dx = *mparams->readDx(HexahedralFEMForceFieldT::mstate); + helper::WriteAccessor< DataVecDeriv > df = *dfId[HexahedralFEMForceFieldT::mstate.get()].write(); const VecElement& hexahedra = this->_topology->getHexahedra(); const auto& hexahedronInf = this->hexahedronInfo.getValue(); diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl index 19fd2ae1b36..142d875e89e 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedronFEMForceFieldAndMass.inl @@ -44,6 +44,7 @@ void NonUniformHexahedronFEMForceFieldAndMass::init() else this->_alreadyInit=true; + this->core::behavior::Mass::init(); this->core::behavior::ForceField::init(); if (l_topology.empty()) @@ -79,7 +80,7 @@ void NonUniformHexahedronFEMForceFieldAndMass::init() if (this->_initialPoints.getValue().size() == 0) { - const VecCoord& p = this->mstate->read(core::ConstVecCoordId::position())->getValue(); + const VecCoord& p = HexahedronFEMForceFieldT::mstate->read(core::ConstVecCoordId::position())->getValue(); this->_initialPoints.setValue(p); } @@ -278,7 +279,7 @@ void NonUniformHexahedronFEMForceFieldAndMass::computeClassicalMechanicalMatr type::fixed_array nodes; // for (unsigned int k=0;k<8;++k) nodes[k] = this->_sparseGrid->_virtualFinerLevels[level]->getPointPos(points[k]); - for (unsigned int k=0; k<8; ++k) nodes[k] = this->_sparseGrid->_virtualFinerLevels[level]->getPointPos(points[k]).linearProduct(this->mstate->getScale()); + for (unsigned int k=0; k<8; ++k) nodes[k] = this->_sparseGrid->_virtualFinerLevels[level]->getPointPos(points[k]).linearProduct(HexahedronFEMForceFieldT::mstate->getScale()); // //given an elementIndice, find the 8 others from the sparse grid // //compute MaterialStiffness diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp index c2590dd8f10..c511b959ecb 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.cpp @@ -30,6 +30,22 @@ BaseMass::BaseMass() { } +void BaseMass::addMBKdx(const MechanicalParams* mparams, MultiVecDerivId dfId) +{ + if (sofa::core::mechanicalparams::mFactorIncludingRayleighDamping(mparams,rayleighMass.getValue()) != 0.0) + { + addMDx(mparams, dfId, sofa::core::mechanicalparams::mFactorIncludingRayleighDamping(mparams,rayleighMass.getValue())); + } +} + +void BaseMass::addMBKToMatrix(const MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) +{ + if (sofa::core::mechanicalparams::mFactorIncludingRayleighDamping(mparams,rayleighMass.getValue()) != 0.0 ) + { + addMToMatrix(mparams, matrix); + } +} + bool BaseMass::insertInNode( objectmodel::BaseNode* node ) { node->addMass(this); diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h index 7d880fade08..ddf8a6e73c8 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h @@ -64,6 +64,32 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor /// f += factor M dx virtual void addMDx(const MechanicalParams* mparams, MultiVecDerivId fid, SReal factor) =0; + + /// \brief Accumulate the contribution of M, B, and/or K matrices multiplied + /// by the dx vector with the given coefficients. + /// + /// This method computes + /// \f[ + /// df += mFactor M dx + bFactor B dx + kFactor K dx + /// \f] + /// + /// where M is the mass matrix (associated with inertial forces), + /// K is the stiffness matrix (associated with forces which derive from a potential), + /// and B is the damping matrix (associated with viscous forces). + /// + /// Very often, at least one of these matrices is null. + /// In most cases only one of these matrices will be non-null for a given + /// component. For forcefields without mass it simply calls addDForce. + /// + /// \param mparams + /// - \a mparams->readDx() is the input vector + /// - \a mparams->mFactor() is the coefficient for mass contributions (i.e. second-order derivatives term in the ODE) + /// - \a sofa::core::mechanicalparams::bFactor(mparams) is the coefficient for damping contributions (i.e. first derivatives term in the ODE) + /// - \a mparams->kFactor() is the coefficient for stiffness contributions (i.e. DOFs term in the ODE) + /// \param dfId the output vector + virtual void addMBKdx(const MechanicalParams* mparams, MultiVecDerivId dfId); + + /// dx = M^-1 f virtual void accFromF(const MechanicalParams* mparams, MultiVecDerivId aid) = 0; @@ -91,6 +117,16 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor /// \param mparams \a mparams->mFactor() is the coefficient for mass contributions (i.e. second-order derivatives term in the ODE) virtual void addMToMatrix(const MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) = 0; + + /// \brief Compute the system matrix corresponding to \f$ m M + b B + k K \f$ + /// + /// \param mparams + /// - \a mparams->mFactor() is the coefficient for mass contributions (i.e. second-order derivatives term in the ODE) + /// - \a sofa::core::mechanicalparams::bFactor(mparams) is the coefficient for damping contributions (i.e. first derivatives term in the ODE) + /// - \a mparams->kFactor() is the coefficient for stiffness contributions (i.e. DOFs term in the ODE) + /// \param matrix the matrix to add the result to + virtual void addMBKToMatrix(const MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix ); + /// @} /// initialization to export kinetic and potential energy to gnuplot files format diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 08015cbd30f..d3fb3a84f7a 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -24,6 +24,7 @@ #include #include #include +#include // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") #include // TO REMOVE @@ -41,10 +42,10 @@ namespace sofa::core::behavior * It is also a ForceField, computing gravity-related forces. */ template -class Mass : virtual public ForceField, public BaseMass +class Mass : public BaseMass, public SingleStateAccessor { public: - SOFA_CLASS2(SOFA_TEMPLATE(Mass,DataTypes), SOFA_TEMPLATE(ForceField,DataTypes), BaseMass); + SOFA_CLASS(SOFA_TEMPLATE(Mass,DataTypes), BaseMass); typedef typename DataTypes::VecCoord VecCoord; typedef typename DataTypes::VecDeriv VecDeriv; @@ -79,30 +80,6 @@ class Mass : virtual public ForceField, public BaseMass virtual void accFromF(const MechanicalParams* mparams, DataVecDeriv& a, const DataVecDeriv& f); - - /// Mass does not generate any force - void addForce(const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override; - - /// Mass does not generate any force derivative - void addDForce(const MechanicalParams* mparams, DataVecDeriv & df, const DataVecDeriv & dx ) override; - - /// Appart from the potential energy due to the gravitational acceleration, no energy comes from the mass itself - SReal getPotentialEnergy( const MechanicalParams* mparams, const DataVecCoord& x ) const override; - - - /// Accumulate the contribution of M, B, and/or K matrices multiplied - /// by the dx vector with the given coefficients. - /// - /// This method from the ForceField API computes - /// $ df += mFactor M dx + bFactor B dx + kFactor K dx $ - /// For masses, it calls both addMdx and addDForce (which is often empty). - /// - /// \param mFact coefficient for mass contributions (i.e. second-order derivatives term in the ODE) - /// \param bFact coefficient for damping contributions (i.e. first derivatives term in the ODE) - /// \param kFact coefficient for stiffness contributions (i.e. DOFs term in the ODE) - void addMBKdx(const MechanicalParams* mparams, MultiVecDerivId dfId) override; - - /// $ f = M g $ /// /// This method computes the external force due to the gravitational acceleration @@ -137,20 +114,9 @@ class Mass : virtual public ForceField, public BaseMass /// @name Matrix operations /// @{ - void addKToMatrix(sofa::linearalgebra::BaseMatrix * /*matrix*/, SReal /*kFact*/, unsigned int &/*offset*/) override {} - void addBToMatrix(sofa::linearalgebra::BaseMatrix * /*matrix*/, SReal /*bFact*/, unsigned int &/*offset*/) override {} - void addMToMatrix(const MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; virtual void addMToMatrix(sofa::linearalgebra::BaseMatrix * matrix, SReal mFact, unsigned int &offset); - /// Compute the system matrix corresponding to m M + b B + k K - /// - /// \param matrix matrix to add the result to - /// \param mFact coefficient for mass contributions (i.e. second-order derivatives term in the ODE) - /// \param bFact coefficient for damping contributions (i.e. first derivatives term in the ODE) - /// \param kFact coefficient for stiffness contributions (i.e. DOFs term in the ODE) - void addMBKToMatrix(const MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; - /// @} /// initialization to export kinetic and potential energy to gnuplot files format @@ -168,8 +134,8 @@ class Mass : virtual public ForceField, public BaseMass std::ofstream* m_gnuplotFileEnergy; public: - bool insertInNode( objectmodel::BaseNode* node ) override { BaseMass::insertInNode(node); BaseForceField::insertInNode(node); return true; } - bool removeInNode( objectmodel::BaseNode* node ) override { BaseMass::removeInNode(node); BaseForceField::removeInNode(node); return true; } + bool insertInNode( objectmodel::BaseNode* node ) override { BaseMass::insertInNode(node); return true; } + bool removeInNode( objectmodel::BaseNode* node ) override { BaseMass::removeInNode(node); return true; } // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl index 81d0c6fd48f..8f022b5362c 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.inl @@ -33,7 +33,7 @@ namespace sofa::core::behavior template Mass::Mass(MechanicalState *mm) - : ForceField(mm) + : BaseMass(), SingleStateAccessor(mm) , m_gnuplotFileEnergy(nullptr) { } @@ -48,7 +48,7 @@ void Mass::addMDx(const MechanicalParams* mparams, MultiVecDerivId fi { if (mparams) { - addMDx(mparams, *fid[this->mstate.get()].write(), *mparams->readDx(this->mstate), factor); + addMDx(mparams, *fid[this->mstate.get()].write(), *mparams->readDx(this->mstate), factor); } } @@ -64,7 +64,7 @@ void Mass::accFromF(const MechanicalParams* mparams, MultiVecDerivId { if(mparams) { - accFromF(mparams, *aid[this->mstate.get()].write(), *mparams->readF(this->mstate)); + accFromF(mparams, *aid[this->mstate.get()].write(), *mparams->readF(this->mstate)); } else msg_error() <<"Mass::accFromF(const MechanicalParams* mparams, MultiVecDerivId aid) receives no mparam"; } @@ -75,41 +75,6 @@ void Mass::accFromF(const MechanicalParams* /*mparams*/, DataVecDeriv msg_warning() << "Method accFromF(const MechanicalParams* , DataVecDeriv& , const DataVecDeriv& ) not implemented."; } -template -void Mass::addDForce(const MechanicalParams* mparams, DataVecDeriv & df, const DataVecDeriv & dx) -{ - SOFA_UNUSED(mparams); - SOFA_UNUSED(df); - SOFA_UNUSED(dx); -} - -template -void Mass::addForce(const MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) -{ - SOFA_UNUSED(mparams); - SOFA_UNUSED(f); - SOFA_UNUSED(x); - SOFA_UNUSED(v); -} - -template -SReal Mass::getPotentialEnergy(const MechanicalParams* mparams, const DataVecCoord& x) const -{ - SOFA_UNUSED(mparams); - SOFA_UNUSED(x); - return 0.0; -} - -template -void Mass::addMBKdx(const MechanicalParams* mparams, MultiVecDerivId dfId) -{ - this->ForceField::addMBKdx(mparams, dfId); - if (mparams->mFactorIncludingRayleighDamping(rayleighMass.getValue()) != 0.0) - { - addMDx(mparams, *dfId[this->mstate.get()].write(), *mparams->readDx(this->mstate), mparams->mFactorIncludingRayleighDamping(rayleighMass.getValue())); - } -} - template SReal Mass::getKineticEnergy(const MechanicalParams* mparams) const { @@ -160,13 +125,6 @@ void Mass::addMToMatrix(sofa::linearalgebra::BaseMatrix * /*mat*/, SR } } -template -void Mass::addMBKToMatrix(const MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) -{ - this->ForceField::addMBKToMatrix(mparams, matrix); - if (mparams->mFactorIncludingRayleighDamping(rayleighMass.getValue()) != 0.0) - addMToMatrix(mparams, matrix); -} template void Mass::initGnuplot(const std::string path) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.cpp index 3b38035c14e..36948603383 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.cpp @@ -23,6 +23,7 @@ #include #include +#include namespace sofa::simulation::mechanicalvisitor { @@ -40,6 +41,16 @@ MechanicalAddMBK_ToMatrixVisitor::fwdMechanicalState(simulation::Node *, core::b return RESULT_CONTINUE; } +Visitor::Result MechanicalAddMBK_ToMatrixVisitor::fwdMass(simulation::Node *, core::behavior::BaseMass *mass) +{ + if (matrix != nullptr) + { + mass->addMBKToMatrix(this->mparams, matrix); + } + + return RESULT_CONTINUE; +} + Visitor::Result MechanicalAddMBK_ToMatrixVisitor::fwdForceField(simulation::Node *, core::behavior::BaseForceField *ff) { if (matrix != nullptr) @@ -56,4 +67,4 @@ bool MechanicalAddMBK_ToMatrixVisitor::stopAtMechanicalMapping(simulation::Node SOFA_UNUSED(node); return !map->areMatricesMapped(); } -} \ No newline at end of file +} diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.h index d74571c2fa5..c2d14ec90fe 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBK_ToMatrixVisitor.h @@ -40,8 +40,10 @@ class SOFA_SIMULATION_CORE_API MechanicalAddMBK_ToMatrixVisitor : public Mechani Result fwdMechanicalState(simulation::Node* /*node*/, core::behavior::BaseMechanicalState* /*ms*/) override; + Result fwdMass(simulation::Node* /*node*/, core::behavior::BaseMass* mass) override; + Result fwdForceField(simulation::Node* /*node*/, core::behavior::BaseForceField* ff) override; bool stopAtMechanicalMapping(simulation::Node* node, core::BaseMapping* map) override; }; -} \ No newline at end of file +} diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.cpp index 91c77e95ecf..70eb2df4f62 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.cpp @@ -23,6 +23,7 @@ #include #include +#include namespace sofa::simulation::mechanicalvisitor { @@ -38,6 +39,12 @@ Visitor::Result MechanicalAddMBKdxVisitor::fwdMappedMechanicalState(simulation:: return RESULT_CONTINUE; } +Visitor::Result MechanicalAddMBKdxVisitor::fwdMass(simulation::Node* /*node*/, core::behavior::BaseMass* mass) +{ + mass->addMBKdx( this->mparams, res); + return RESULT_CONTINUE; +} + Visitor::Result MechanicalAddMBKdxVisitor::fwdForceField(simulation::Node* /*node*/, core::behavior::BaseForceField* ff) { if( !ff->isCompliance.getValue() ) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.h index 32473a6418d..dc74d498a44 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/mechanicalvisitor/MechanicalAddMBKdxVisitor.h @@ -50,6 +50,7 @@ class SOFA_SIMULATION_CORE_API MechanicalAddMBKdxVisitor : public MechanicalVisi } Result fwdMechanicalState(simulation::Node* /*node*/,sofa::core::behavior::BaseMechanicalState* mm) override; Result fwdMappedMechanicalState(simulation::Node* /*node*/,sofa::core::behavior::BaseMechanicalState* mm) override; + Result fwdMass(simulation::Node* /*node*/,sofa::core::behavior::BaseMass* mass) override; Result fwdForceField(simulation::Node* /*node*/,sofa::core::behavior::BaseForceField* ff) override; void bwdMechanicalMapping(simulation::Node* /*node*/, sofa::core::BaseMapping* map) override; void bwdMechanicalState(simulation::Node* /*node*/,sofa::core::behavior::BaseMechanicalState* mm) override; From a74c586f4dbf77776d590bd7614eb60d046b7767 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Mon, 5 Sep 2022 21:17:35 +0200 Subject: [PATCH 22/41] Move GravityForceField into MechanicalLoad --- Sofa/Component/MechanicalLoad/CMakeLists.txt | 3 +++ .../src/sofa/component/mechanicalload}/GravityForceField.cpp | 0 .../src/sofa/component/mechanicalload}/GravityForceField.h | 0 .../src/sofa/component/mechanicalload}/GravityForceField.inl | 0 Sofa/framework/Core/CMakeLists.txt | 3 --- 5 files changed, 3 insertions(+), 3 deletions(-) rename Sofa/{framework/Core/src/sofa/core/behavior => Component/MechanicalLoad/src/sofa/component/mechanicalload}/GravityForceField.cpp (100%) rename Sofa/{framework/Core/src/sofa/core/behavior => Component/MechanicalLoad/src/sofa/component/mechanicalload}/GravityForceField.h (100%) rename Sofa/{framework/Core/src/sofa/core/behavior => Component/MechanicalLoad/src/sofa/component/mechanicalload}/GravityForceField.inl (100%) diff --git a/Sofa/Component/MechanicalLoad/CMakeLists.txt b/Sofa/Component/MechanicalLoad/CMakeLists.txt index a414cf8700c..2f222e5d172 100644 --- a/Sofa/Component/MechanicalLoad/CMakeLists.txt +++ b/Sofa/Component/MechanicalLoad/CMakeLists.txt @@ -16,6 +16,8 @@ set(HEADER_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EdgePressureForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.h ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.inl + ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/GravityForceField.h + ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/GravityForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/InteractionEllipsoidForceField.h ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/InteractionEllipsoidForceField.inl ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.h @@ -47,6 +49,7 @@ set(SOURCE_FILES ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/DiagonalVelocityDampingForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EdgePressureForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/EllipsoidForceField.cpp + ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/GravityForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/InteractionEllipsoidForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/LinearForceField.cpp ${SOFACOMPONENTMECHANICALLOAD_SOURCE_DIR}/OscillatingTorsionPressureForceField.cpp diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp similarity index 100% rename from Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.cpp rename to Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h similarity index 100% rename from Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.h rename to Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h diff --git a/Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl similarity index 100% rename from Sofa/framework/Core/src/sofa/core/behavior/GravityForceField.inl rename to Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl diff --git a/Sofa/framework/Core/CMakeLists.txt b/Sofa/framework/Core/CMakeLists.txt index f8c3e73bb10..eb80dcc2efd 100644 --- a/Sofa/framework/Core/CMakeLists.txt +++ b/Sofa/framework/Core/CMakeLists.txt @@ -63,8 +63,6 @@ set(HEADER_FILES ${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.h ${SRC_ROOT}/behavior/ForceField.h ${SRC_ROOT}/behavior/ForceField.inl - ${SRC_ROOT}/behavior/GravityForceField.h - ${SRC_ROOT}/behavior/GravityForceField.inl ${SRC_ROOT}/behavior/LinearSolver.h ${SRC_ROOT}/behavior/Mass.h ${SRC_ROOT}/behavior/Mass.inl @@ -214,7 +212,6 @@ set(SOURCE_FILES ${SRC_ROOT}/behavior/ConstraintSolver.cpp ${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.cpp ${SRC_ROOT}/behavior/ForceField.cpp - ${SRC_ROOT}/behavior/GravityForceField.cpp ${SRC_ROOT}/behavior/LinearSolver.cpp ${SRC_ROOT}/behavior/Mass.cpp ${SRC_ROOT}/behavior/MechanicalMatrix.cpp From 52eaca1b73fc4ebcf660026ca5bed7f25a447c73 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Thu, 8 Sep 2022 15:01:35 +0200 Subject: [PATCH 23/41] mark addMDxToVector in UniformMass as delete --- Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index 63389becb4d..48bb1a59ac9 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -140,7 +140,7 @@ class UniformMass : public core::behavior::Mass type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the addMDxToVector function related to the gravity") - void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset); + void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset) = delete; void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; /// Add Mass contribution to global Matrix assembling From 94bdb1e6888bb4d9a8318abb673005c108c83e53 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Thu, 8 Sep 2022 15:02:35 +0200 Subject: [PATCH 24/41] remove useless SOFA_ATTRIBUTE and remove unneeded addKToMatrix overload --- .../sofa/component/mechanicalload/GravityForceField.cpp | 5 +---- .../src/sofa/component/mechanicalload/GravityForceField.h | 6 ------ .../sofa/component/mechanicalload/GravityForceField.inl | 8 -------- 3 files changed, 1 insertion(+), 18 deletions(-) diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp index c443be52ef5..b718056edce 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.cpp @@ -21,10 +21,7 @@ ******************************************************************************/ #define SOFA_COMPONENT_FORCEFIELD_GRAVITYFORCEFIELD_CPP -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") -//#include // TO UNCOMMENT -#include "GravityForceField.inl" // TO REMOVE - +#include "GravityForceField.inl" #include #include diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h index ccff777f055..2bf1dd1062f 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h @@ -21,8 +21,6 @@ ******************************************************************************/ #pragma once -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") -//#include // TO UNCOMMENT #include #include #include @@ -63,10 +61,6 @@ class GravityForceField : public core::behavior::ForceField /// Gravity force has null variation void addKToMatrix(sofa::linearalgebra::BaseMatrix *mat, SReal k, unsigned int &offset) override; - /// Constant force has null variation - using Inherit::addKToMatrix; - virtual void addKToMatrix(const sofa::core::behavior::MultiMatrixAccessor* matrix, SReal kFact) ; - SReal getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const override; /// Set the gravitational acceleration diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl index 7843db76a91..dee32b51483 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl @@ -122,13 +122,5 @@ void GravityForceField::addKToMatrix(sofa::linearalgebra::BaseMatrix } -template -void GravityForceField::addKToMatrix(const sofa::core::behavior::MultiMatrixAccessor* matrix, SReal kFact) -{ - // Derivative of a constant gravity field is null, no need to compute addKToMatrix nor addDForce - SOFA_UNUSED(matrix); - SOFA_UNUSED(kFact); -} - } // namespace sofa::component::mechanicalload From c91395a375cdec9af8848d5667c0d3452e7051b2 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Thu, 8 Sep 2022 15:03:01 +0200 Subject: [PATCH 25/41] fix code and warning --- .../fem/elastic/HexahedronFEMForceFieldAndMass.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl index a8b8ff0a4d1..6de75a900f0 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.inl @@ -288,7 +288,7 @@ void HexahedronFEMForceFieldAndMass::addDForce(const core::Mechanical template SReal HexahedronFEMForceFieldAndMass::getPotentialEnergy( const core::MechanicalParams* mparams, const DataVecCoord& x ) const { - HexahedronFEMForceFieldT::getPotentialEnergy(mparams, x); + return HexahedronFEMForceFieldT::getPotentialEnergy(mparams, x); } template From 5d357cf9faf2165b2f60e65b10024a49e48a1e4b Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Thu, 8 Sep 2022 15:04:02 +0200 Subject: [PATCH 26/41] clean Mass.h witout SOFA_ATTRIBUTE and unncessary insert/removeInNode function --- Sofa/framework/Core/src/sofa/core/behavior/Mass.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index d3fb3a84f7a..49597848fe6 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -25,9 +25,7 @@ #include #include #include - -// SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") -#include // TO REMOVE +#include namespace sofa::core::behavior { @@ -134,11 +132,6 @@ class Mass : public BaseMass, public SingleStateAccessor std::ofstream* m_gnuplotFileEnergy; public: - bool insertInNode( objectmodel::BaseNode* node ) override { BaseMass::insertInNode(node); return true; } - bool removeInNode( objectmodel::BaseNode* node ) override { BaseMass::removeInNode(node); return true; } - - - // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") template static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) { From a771e1b5eb788fb21c7b1bc78e7af777ea3ff323 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 09:55:28 +0200 Subject: [PATCH 27/41] remove useless protected --- .../fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.h index 4531e76ecb3..2114e0b7512 100644 --- a/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/NonUniform/src/sofa/component/solidmechanics/fem/nonuniform/NonUniformHexahedralFEMForceFieldAndMass.h @@ -191,7 +191,6 @@ class NonUniformHexahedralFEMForceFieldAndMass : virtual public component::solid bool matrixIsDirty; ///< Matrix \f$ \alpha M + \beta B + \gamma C \f$ needs to be recomputed type::vector< ElementMass > mbkMatrix; ///< Matrix \f$ \alpha M + \beta B + \gamma C \f$ -protected: virtual void computeCorrection( ElementMass& ) {} ///< Limit the conditioning number of each mbkMatrix as defined by maxConditioning (in derived classes). }; From 43797c1e3f3fb42b69e971f3d74548e4407c0117 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 09:56:49 +0200 Subject: [PATCH 28/41] update dates in disabled message --- Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h | 2 +- Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h | 4 ++-- .../Core/src/sofa/simulation/MechanicalOperations.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h index 48bb1a59ac9..8680d165947 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h @@ -139,7 +139,7 @@ class UniformMass : public core::behavior::Mass SReal getKineticEnergy(const core::MechanicalParams* mparams, const DataVecDeriv& d_v) const override; ///< vMv/2 using dof->getV() override type::Vector6 getMomentum(const core::MechanicalParams* mparams, const DataVecCoord& x, const DataVecDeriv& v) const override; ///< (Mv,cross(x,Mv)+Iw) override - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the addMDxToVector function related to the gravity") + SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Removing the addMDxToVector function related to the gravity") void addMDxToVector(linearalgebra::BaseVector *resVect, const VecDeriv *dx, SReal mFact, unsigned int& offset) = delete; void addMToMatrix(const core::MechanicalParams *mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix) override; /// Add Mass contribution to global Matrix assembling diff --git a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h index ddf8a6e73c8..b86753d7659 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/BaseMass.h @@ -101,9 +101,9 @@ class SOFA_CORE_API BaseMass : public virtual StateAccessor /// @} - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the separate gravity API.") + SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Removing the separate gravity API.") virtual void addGravityToV(const MechanicalParams* mparams, MultiVecDerivId vid) = delete; - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the separate gravity API.") + SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Removing the separate gravity API.") DeprecatedAndRemoved m_separateGravity; diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h index d7c50da0300..941cf3e6eeb 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/MechanicalOperations.h @@ -86,7 +86,7 @@ class SOFA_SIMULATION_CORE_API MechanicalOperations void addMBKv(core::MultiVecDerivId df, SReal m, SReal b, SReal k, bool clear = true, bool accumulate = true); - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Removing the separate gravity API.") + SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Removing the separate gravity API.") void addSeparateGravity(SReal dt, core::MultiVecDerivId result = core::VecDerivId::velocity() ) = delete; void computeContactForce(core::MultiVecDerivId result); From aaf79899fd4832d48b8a6953795e6f73c85bfac5 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 09:57:13 +0200 Subject: [PATCH 29/41] reformat warning emssage from UniformMass --- Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl index b5633704e79..7b5d68697e0 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl @@ -137,10 +137,8 @@ void UniformMass::initDefaultImpl() if(this->mstate==nullptr) { - msg_warning(this) << "Missing mechanical state. \n" - "UniformMass need to be used with an object also having a MechanicalState. \n" - "To remove this warning: add a to the parent node of the one \n" - " containing this "; + msg_warning(this) << "Missing mechanical state. UniformMass need to be used with an object also having a MechanicalState." << msgendl + << "To remove this warning: add a MechanicalObject to the parent node of the one containing this UniformMass"; return; } From 1bac38b17b15a3f06d2a714b8cb8af5923287e01 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 09:58:18 +0200 Subject: [PATCH 30/41] fix warning due to missing return in HexahedralFEMForceFieldAndMass --- .../fem/elastic/HexahedralFEMForceFieldAndMass.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl index c2088dd6a0f..64d9ba80e76 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.inl @@ -429,7 +429,7 @@ void HexahedralFEMForceFieldAndMass::addDForce(const core::Mechanical template SReal HexahedralFEMForceFieldAndMass::getPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x) const { - HexahedralFEMForceFieldT::getPotentialEnergy(mparams, x); + return HexahedralFEMForceFieldT::getPotentialEnergy(mparams, x); } template From 6a1b993db807709aba7968ab5524a4175657fe03 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 09:59:49 +0200 Subject: [PATCH 31/41] rename dataname for user into worldGravity in Node/Context and add compatibility layer --- .../Core/src/sofa/core/objectmodel/Context.cpp | 2 +- .../Simulation/Core/src/sofa/simulation/Node.cpp | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp index 8c577e2af7c..563404048fb 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp @@ -32,7 +32,7 @@ namespace objectmodel Context::Context() : is_activated(initData(&is_activated, true, "activated", "To Activate a node")) - , worldGravity_(initData(&worldGravity_, Vec3(SReal(0),SReal(-9.81),SReal(0)),"gravity","Gravity in the world coordinate system")) + , worldGravity_(initData(&worldGravity_, Vec3(SReal(0.),SReal(0.),SReal(0.)),"worldGravity","Gravity in the world coordinate system")) , dt_(initData(&dt_,SReal(0.01),"dt","Time step")) , time_(initData(&time_,SReal(0.),"time","Current time")) , animate_(initData(&animate_,false,"animate","Animate the Simulation(applied at initialization only)")) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index fac4269ce47..3741d0e3ac3 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -170,15 +170,17 @@ void Node::parse( sofa::core::objectmodel::BaseObjectDescription* arg ) sofa::core::objectmodel::BaseObject::SPtr obj = sofa::core::ObjectFactory::CreateObject(this, &objDesc); } - // SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Transition removing gravity and introducing GravityForceField") + // SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Transition removing gravity and introducing GravityForceField") const char* gravityStr = arg->getAttribute("gravity", nullptr); if (gravityStr != nullptr) { - msg_warning() << "A \"gravity\" data has been detected in the context of your node \"" << name << "\": this data is DEPRECATED since PR#2988." << msgendl - "Note that the gravity should now be applied using a GravityForceField for each object." << msgendl - "To remove this warning:" << msgendl - " - remove the \"gravity\" data from all nodes" << msgendl - " - add a GravityForceField with the data \"gravitationalAcceleration\" for each object undergoing the gravity"; + msg_deprecated() << "The \"gravity\" data has been renamed \"worldGravity\" since PR#2988 and its default value is (0,0,0)." << msgendl + "This change was made to explicit the fact that the gravity will affect all objects in the scene." << msgendl + "To define a per-node gravity, please do not use the \"worldGravity\" but a GravityForceField for each object instead."; + Vec3 g; + std::istringstream ss( gravityStr ); + ss >> g ; + this->setGravity(g); } } From 327be9ad83fb86e1ac8f32c99f0921e30a8d394b Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 10:00:35 +0200 Subject: [PATCH 32/41] Gravity(deprecated) : update dates in disabled message --- .../SofaGraphComponent/src/SofaGraphComponent/Gravity.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/applications/collections/deprecated/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h b/applications/collections/deprecated/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h index 86421ca8902..38471760df8 100644 --- a/applications/collections/deprecated/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h +++ b/applications/collections/deprecated/modules/SofaGraphComponent/src/SofaGraphComponent/Gravity.h @@ -35,14 +35,14 @@ namespace sofa::component::contextobject { /** Override the default gravity */ -class SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Gravity class has been removed") Gravity : public core::objectmodel::ContextObject +class SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Gravity class has been removed") Gravity : public core::objectmodel::ContextObject { public: SOFA_CLASS(Gravity, core::objectmodel::ContextObject); protected: Gravity() = default; public: - SOFA_ATTRIBUTE_DISABLED("v22.06 (PR#2988)", "v23.06", "Gravity class has been removed") + SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Gravity class has been removed") DeprecatedAndRemoved f_gravity; ///< Gravity in the world coordinate system void apply() override {}; From 234de4db585327957b2d143fe2190dde2ea2af59 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 10:01:26 +0200 Subject: [PATCH 33/41] Introduce new gravity mechanism --- .../mechanicalload/GravityForceField.h | 8 +++ .../mechanicalload/GravityForceField.inl | 70 +++++++++++++++++-- .../Core/src/sofa/core/behavior/Mass.h | 55 +++++++++++---- 3 files changed, 114 insertions(+), 19 deletions(-) diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h index 2bf1dd1062f..9ab883e1d0b 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h @@ -47,6 +47,7 @@ class GravityForceField : public core::behavior::ForceField typedef core::objectmodel::Data DataVecDeriv; Data< DPos > d_gravitationalAcceleration; ///< Value corresponding to the gravitational acceleration + Data< type::Vec3 > d_worldGravity; ///< Vector3 which can be linked to the worldGravity data of Node SingleLink, sofa::core::behavior::Mass, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_mass; ///< Link to be set to the mass in the component graph /// Init function @@ -66,8 +67,15 @@ class GravityForceField : public core::behavior::ForceField /// Set the gravitational acceleration void setGravitationalAcceleration(const DPos grav); + /// Check the norm of the gravity + void checkGravityNorm(); + + /// Update gravitational acceleration from the gravity defined in root node + void setGravityFromRootNode(); + protected: GravityForceField(); + bool m_isNormNull = false; }; diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl index dee32b51483..ced1ae290ee 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl @@ -34,8 +34,21 @@ using namespace sofa::type; template GravityForceField::GravityForceField() : d_gravitationalAcceleration(initData(&d_gravitationalAcceleration, "gravitationalAcceleration", "Value corresponding to the gravitational acceleration")) + , d_worldGravity(initData(&d_worldGravity, "worldGravity", "Vector3 which can be linked to the worldGravity data of Node")) , l_mass(initLink("mass", "link to the mass")) { + // To avoid confusion, the data "d_worldGravity" used for the automatic creation of GravityForceField when using the Node gravity is hidden to the user + d_worldGravity.setDisplayed(false); + + this->addUpdateCallback("connnectToWorldGravity", { &d_worldGravity}, [this](const core::DataTracker& t) + { + SOFA_UNUSED(t); + + setGravityFromRootNode(); + checkGravityNorm(); + + return sofa::core::objectmodel::ComponentState::Valid; + }, {}); } @@ -43,7 +56,7 @@ template void GravityForceField::init() { this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); - + if (l_mass.empty()) { msg_info() << "link to the mass should be set to ensure right behavior. First mass found in current context will be used."; @@ -52,17 +65,28 @@ void GravityForceField::init() l_mass.set(p_mass); } - // temporary pointer to the mass + // Check if several GravityForceField in the current node + std::vector*> gravityFFVector; + this->getContext()->template get(&gravityFFVector, core::objectmodel::BaseContext::Local); + if(gravityFFVector.size()>1) + { + msg_warning() << "Several gravities seem to be defined in node " << this->getContext()->getName(); + } + + // Link to the mass component if (sofa::core::behavior::Mass* _mass = l_mass.get()) { msg_info() << "Mass path used: '" << l_mass.getLinkedPath() << "'"; } else { - msg_error() << "No mass component found at path: " << l_mass.getLinkedPath() << ", nor in current context: " << this->getContext()->name; + msg_error() << "No Mass component with template "<< this->getTemplateName() <<" found in current context: " << this->getContext()->name << ", nor any valid link to a Mass was given. No gravity will be applied."; return; } + // Check if norm is null + checkGravityNorm(); + // init from ForceField Inherit::init(); @@ -71,6 +95,18 @@ void GravityForceField::init() } +template +void GravityForceField::setGravityFromRootNode() +{ + const Vec3& gravityRootNode = d_worldGravity.getValue(); + auto gravity = sofa::helper::getWriteAccessor(d_gravitationalAcceleration); + for(Size i=0; ( i void GravityForceField::setGravitationalAcceleration(const DPos grav) { @@ -78,12 +114,37 @@ void GravityForceField::setGravitationalAcceleration(const DPos grav) } +template +void GravityForceField::checkGravityNorm() +{ + const DPos & gravity = d_gravitationalAcceleration.getValue(); + const Real norm = gravity.norm(); + + if(norm == 0.0) + { + m_isNormNull = true; + msg_info() << "Gravitational acceleration is null"; + } + else + { + m_isNormNull = false; + } +} + + template void GravityForceField::addForce(const core::MechanicalParams* params, DataVecDeriv& f, const DataVecCoord& x1, const DataVecDeriv& v1) { + if(this->d_componentState.getValue() == core::objectmodel::ComponentState::Invalid) + return; + sofa::core::behavior::Mass* _mass = l_mass.get(); Deriv gravity; DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); + + if(m_isNormNull) + return; + _mass->addGravitationalForce(params,f,x1,v1,gravity); } @@ -94,7 +155,8 @@ SReal GravityForceField::getPotentialEnergy(const core::MechanicalPar sofa::core::behavior::Mass* _mass = l_mass.get(); Deriv gravity; DataTypes::setDPos(gravity, d_gravitationalAcceleration.getValue()); - if(_mass) + + if(_mass && !m_isNormNull) return _mass->getGravitationalPotentialEnergy(params, x, gravity); else return 0.0; diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 49597848fe6..6a15d071752 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -132,6 +132,36 @@ class Mass : public BaseMass, public SingleStateAccessor std::ofstream* m_gnuplotFileEnergy; public: + /// Pre-construction check method called by ObjectFactory. + /// Check that DataTypes matches the MechanicalState. + template + static bool canCreate(T*& obj, objectmodel::BaseContext* context, objectmodel::BaseObjectDescription* arg) + { + const std::string attributeName {"mstate"}; + std::string mstateLink = arg->getAttribute(attributeName,""); + if (mstateLink.empty()) + { + if (dynamic_cast*>(context->getMechanicalState()) == nullptr) + { + arg->logError("Since the attribute '" + attributeName + "' has not been specified, a mechanical state " + "with the datatype '" + DataTypes::Name() + "' has been searched in the current context, but not found."); + return false; + } + } + else + { + MechanicalState* mstate = nullptr; + context->findLinkDest(mstate, mstateLink, nullptr); + if (!mstate) + { + arg->logError("Data attribute '" + attributeName + "' does not point to a valid mechanical state of datatype '" + std::string(DataTypes::Name()) + "'."); + return false; + } + } + return BaseObject::canCreate(obj, context, arg); + } + + /// Automatic creation of a local GravityForceField if the worldGravity in the root node is defined template static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) { @@ -143,31 +173,26 @@ class Mass : public BaseMass, public SingleStateAccessor SReal gravityNorm = gravity.norm(); if(gravityNorm!=0.0) { + // SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Transition removing gravity and introducing GravityForceField") + // to remove after v23.06 ... bool savePLog = context->f_printLog.getValue(); context->f_printLog.setValue(true); - msg_info(context) << "A gravity seem to apply in the node \"" << context->getName() << "\" using the deprecated gravity mechanism (read more in PR#2988)." << msgendl + msg_info(context) << "A gravity seem to apply using the worldGravity in the root node (PR#2988)." << msgendl << "A GravityForceField is automatically added in the node \"" << context->getName() << "\"."; context->f_printLog.setValue(savePLog); + // until here - const sofa::Size dim = context->getMechanicalState()->getDerivDimension(); - const std::string templated = context->getMechanicalState()->getTemplateName(); - std::string gravity_string; - - if(dim == 1) - gravity_string = std::to_string(gravity[0]); - else if(dim == 2) - gravity_string = std::to_string(gravity[0])+" "+std::to_string(gravity[1]); - else - gravity_string = std::to_string(gravity[0])+" "+std::to_string(gravity[1])+" "+std::to_string(gravity[2]); + const std::string templated = context->getMechanicalState()->getTemplateName(); + std::string gravity_string = "@"+ context->getRootContext()->getPathName()+".worldGravity"; sofa::core::objectmodel::BaseObjectDescription desc("GravityForceField","GravityForceField"); desc.setAttribute("template", templated); - desc.setAttribute("gravitationalAcceleration", gravity_string); + desc.setAttribute("worldGravity", gravity_string); - /// Create the object. - BaseObject::SPtr obj = sofa::core::ObjectFactory::getInstance()->createObject(context, &desc); - if (obj==nullptr) + /// Create the object + BaseObject::SPtr objGravityFF = sofa::core::ObjectFactory::getInstance()->createObject(context, &desc); + if (objGravityFF==nullptr) { std::stringstream msg; msg << "Component '" << desc.getName() << "' of type '" << desc.getAttribute("type","") << "' failed:" << msgendl ; From a5c90acf0ac66e5b3c7b4997d1fb950525f16918 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 10:01:55 +0200 Subject: [PATCH 34/41] add using to use the proper canCreate --- .../fem/elastic/HexahedralFEMForceFieldAndMass.h | 3 +-- .../fem/elastic/HexahedronFEMForceFieldAndMass.h | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h index 869d98056c9..11ab2d3a4d7 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h @@ -113,8 +113,7 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass void setDensity(Real d) {_density.setValue( d );} Real getDensity() {return _density.getValue();} - - + using HexahedralFEMForceFieldT::canCreate; protected: virtual void computeElementMasses( ); ///< compute the mass matrices diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h index ca9c4c265a9..1a20b30434d 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h @@ -126,6 +126,8 @@ class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass d_elementMasses; ///< mass matrices per element From e264f1169ee7145f3bc060911a69fffeb7a34ad0 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Fri, 9 Sep 2022 17:47:13 +0200 Subject: [PATCH 35/41] Add DataTypes available for SP3 --- Sofa/framework/Core/src/sofa/core/behavior/Mass.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h index 6a15d071752..be223269256 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Mass.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Mass.h @@ -39,12 +39,13 @@ namespace sofa::core::behavior * * It is also a ForceField, computing gravity-related forces. */ -template -class Mass : public BaseMass, public SingleStateAccessor +template +class Mass : public BaseMass, public SingleStateAccessor { public: - SOFA_CLASS(SOFA_TEMPLATE(Mass,DataTypes), BaseMass); + SOFA_CLASS(SOFA_TEMPLATE(Mass,TDataTypes), BaseMass); + typedef TDataTypes DataTypes; typedef typename DataTypes::VecCoord VecCoord; typedef typename DataTypes::VecDeriv VecDeriv; typedef typename DataTypes::Real Real; From 6d4f2eff542df428b53c96b0c43ace53986c0f19 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Wed, 21 Sep 2022 11:32:47 +0200 Subject: [PATCH 36/41] Keep gravity as default name in node and update c++ data name --- .../component/mechanicalload/GravityForceField.h | 2 +- .../component/mechanicalload/GravityForceField.inl | 10 +++++----- Sofa/framework/Core/src/sofa/core/behavior/Mass.h | 8 ++++---- .../Core/src/sofa/core/objectmodel/Context.cpp | 8 ++++---- .../Core/src/sofa/core/objectmodel/Context.h | 4 ++-- .../Simulation/Core/src/sofa/simulation/Node.cpp | 13 ------------- 6 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h index 9ab883e1d0b..a33d3a4cc38 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.h @@ -47,7 +47,7 @@ class GravityForceField : public core::behavior::ForceField typedef core::objectmodel::Data DataVecDeriv; Data< DPos > d_gravitationalAcceleration; ///< Value corresponding to the gravitational acceleration - Data< type::Vec3 > d_worldGravity; ///< Vector3 which can be linked to the worldGravity data of Node + Data< type::Vec3 > d_gravity; ///< Vector3 which can be linked to the gravity data of Node SingleLink, sofa::core::behavior::Mass, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_mass; ///< Link to be set to the mass in the component graph /// Init function diff --git a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl index ced1ae290ee..35b7a19520a 100644 --- a/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl +++ b/Sofa/Component/MechanicalLoad/src/sofa/component/mechanicalload/GravityForceField.inl @@ -34,13 +34,13 @@ using namespace sofa::type; template GravityForceField::GravityForceField() : d_gravitationalAcceleration(initData(&d_gravitationalAcceleration, "gravitationalAcceleration", "Value corresponding to the gravitational acceleration")) - , d_worldGravity(initData(&d_worldGravity, "worldGravity", "Vector3 which can be linked to the worldGravity data of Node")) + , d_gravity(initData(&d_gravity, "gravity", "Vector3 which can be linked to the gravity data of Node")) , l_mass(initLink("mass", "link to the mass")) { - // To avoid confusion, the data "d_worldGravity" used for the automatic creation of GravityForceField when using the Node gravity is hidden to the user - d_worldGravity.setDisplayed(false); + // To avoid confusion, the data "d_gravity" used for the automatic creation of GravityForceField when using the Node gravity is hidden to the user + d_gravity.setDisplayed(false); - this->addUpdateCallback("connnectToWorldGravity", { &d_worldGravity}, [this](const core::DataTracker& t) + this->addUpdateCallback("connnectToGravity", { &d_gravity}, [this](const core::DataTracker& t) { SOFA_UNUSED(t); @@ -98,7 +98,7 @@ void GravityForceField::init() template void GravityForceField::setGravityFromRootNode() { - const Vec3& gravityRootNode = d_worldGravity.getValue(); + const Vec3& gravityRootNode = d_gravity.getValue(); auto gravity = sofa::helper::getWriteAccessor(d_gravitationalAcceleration); for(Size i=0; ( i return BaseObject::canCreate(obj, context, arg); } - /// Automatic creation of a local GravityForceField if the worldGravity in the root node is defined + /// Automatic creation of a local GravityForceField if the gravity in the root node is defined template static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg) { @@ -178,18 +178,18 @@ class Mass : public BaseMass, public SingleStateAccessor // to remove after v23.06 ... bool savePLog = context->f_printLog.getValue(); context->f_printLog.setValue(true); - msg_info(context) << "A gravity seem to apply using the worldGravity in the root node (PR#2988)." << msgendl + msg_info(context) << "A gravity seem to apply using the gravity in the root node (PR#2988)." << msgendl << "A GravityForceField is automatically added in the node \"" << context->getName() << "\"."; context->f_printLog.setValue(savePLog); // until here const std::string templated = context->getMechanicalState()->getTemplateName(); - std::string gravity_string = "@"+ context->getRootContext()->getPathName()+".worldGravity"; + std::string gravity_string = "@"+ context->getRootContext()->getPathName()+".gravity"; sofa::core::objectmodel::BaseObjectDescription desc("GravityForceField","GravityForceField"); desc.setAttribute("template", templated); - desc.setAttribute("worldGravity", gravity_string); + desc.setAttribute("gravity", gravity_string); /// Create the object BaseObject::SPtr objGravityFF = sofa::core::ObjectFactory::getInstance()->createObject(context, &desc); diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp index 563404048fb..d63188e34b8 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Context.cpp @@ -32,7 +32,7 @@ namespace objectmodel Context::Context() : is_activated(initData(&is_activated, true, "activated", "To Activate a node")) - , worldGravity_(initData(&worldGravity_, Vec3(SReal(0.),SReal(0.),SReal(0.)),"worldGravity","Gravity in the world coordinate system")) + , d_gravity(initData(&d_gravity, Vec3(SReal(0.),SReal(0.),SReal(0.)),"gravity","Gravity in the world coordinate system")) , dt_(initData(&dt_,SReal(0.01),"dt","Time step")) , time_(initData(&time_,SReal(0.),"time","Current time")) , animate_(initData(&animate_,false,"animate","Animate the Simulation(applied at initialization only)")) @@ -94,7 +94,7 @@ SReal Context::getTime() const /// Gravity vector in world coordinates const Context::Vec3& Context::getGravity() const { - return worldGravity_.getValue(); + return d_gravity.getValue(); } /// Animation flag @@ -126,7 +126,7 @@ void Context::setTime(SReal val) /// Gravity vector void Context::setGravity(const Vec3& g) { - worldGravity_ .setValue(g); + d_gravity.setValue(g); } /// Animation flag @@ -148,7 +148,7 @@ void Context::copyContext(const Context& c) void Context::copySimulationContext(const Context& c) { - worldGravity_.setValue(c.getGravity()); ///< Gravity IN THE WORLD COORDINATE SYSTEM. + d_gravity.setValue(c.getGravity()); ///< Gravity IN THE WORLD COORDINATE SYSTEM. setDt(c.getDt()); setTime(c.getTime()); setAnimate(c.getAnimate()); diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Context.h b/Sofa/framework/Core/src/sofa/core/objectmodel/Context.h index 73b94db99d8..2aaec99e421 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Context.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Context.h @@ -46,7 +46,7 @@ class SOFA_CORE_API Context : public BaseContext Data is_activated; ///< To Activate a node - Data worldGravity_; ///< Gravity IN THE WORLD COORDINATE SYSTEM. + Data d_gravity; ///< Gravity IN THE WORLD COORDINATE SYSTEM. Data dt_; ///< Time step Data time_; ///< Current time Data animate_; ///< Animate the Simulation(applied at initialization only) @@ -109,7 +109,7 @@ class SOFA_CORE_API Context : public BaseContext void setChangeSleepingState(bool val) override; /// Display flags: Gravity - virtual void setDisplayWorldGravity(bool val) { worldGravity_.setDisplayed(val); } + virtual void setDisplayWorldGravity(bool val) { d_gravity.setDisplayed(val); } /// @} diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index 3741d0e3ac3..2ae910650ba 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -169,19 +169,6 @@ void Node::parse( sofa::core::objectmodel::BaseObjectDescription* arg ) objDesc.setAttribute("displayFlags", oldFlags); sofa::core::objectmodel::BaseObject::SPtr obj = sofa::core::ObjectFactory::CreateObject(this, &objDesc); } - - // SOFA_ATTRIBUTE_DISABLED("v22.12 (PR#2988)", "v23.12", "Transition removing gravity and introducing GravityForceField") - const char* gravityStr = arg->getAttribute("gravity", nullptr); - if (gravityStr != nullptr) - { - msg_deprecated() << "The \"gravity\" data has been renamed \"worldGravity\" since PR#2988 and its default value is (0,0,0)." << msgendl - "This change was made to explicit the fact that the gravity will affect all objects in the scene." << msgendl - "To define a per-node gravity, please do not use the \"worldGravity\" but a GravityForceField for each object instead."; - Vec3 g; - std::istringstream ss( gravityStr ); - ss >> g ; - this->setGravity(g); - } } /// Initialize the components of this node and all the nodes which depend on it. From 6c5eddeaee4997bf25b36eab11e214d749385fb1 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Sun, 26 Feb 2023 14:01:57 +0100 Subject: [PATCH 37/41] Remove option possibly activating ExternalBehaviorModel when legacy headers were used --- applications/plugins/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/plugins/CMakeLists.txt b/applications/plugins/CMakeLists.txt index f5a94f9287a..5a71ef85227 100644 --- a/applications/plugins/CMakeLists.txt +++ b/applications/plugins/CMakeLists.txt @@ -30,7 +30,7 @@ sofa_add_subdirectory(plugin Flexible Flexible EXTERNAL) # Depends on im sofa_add_subdirectory(plugin Registration Registration EXTERNAL) # Depends on image, SofaPython, SofaGui and SofaDistanceGrid sofa_add_subdirectory(plugin BulletCollisionDetection BulletCollisionDetection) # Depends on Compliant and LMConstraint sofa_add_subdirectory(plugin PreassembledMass PreassembledMass) # Depends on Flexible and Compliant -sofa_add_subdirectory(plugin ExternalBehaviorModel ExternalBehaviorModel OFF WHEN_TO_SHOW "SOFA_ENABLE_LEGACY_HEADERS" VALUE_IF_HIDDEN OFF) +sofa_add_subdirectory(plugin ExternalBehaviorModel ExternalBehaviorModel OFF) sofa_add_subdirectory(plugin InvertibleFVM InvertibleFVM EXTERNAL) sofa_add_subdirectory(plugin MeshSTEPLoader MeshSTEPLoader) sofa_add_subdirectory(plugin PluginExample PluginExample EXTERNAL) From 7dca22ee7b36a6162c62d44eae5d0dd7181789b4 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Mon, 27 Feb 2023 21:27:54 +0100 Subject: [PATCH 38/41] Fix Windows compilation due to ambiguous types --- .../fem/elastic/HexahedralFEMForceFieldAndMass.h | 5 ++++- .../fem/elastic/HexahedronFEMForceFieldAndMass.h | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h index 11ab2d3a4d7..5804347df67 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h @@ -37,10 +37,13 @@ template class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass, virtual public HexahedralFEMForceField { public: - SOFA_CLASS2(SOFA_TEMPLATE(HexahedralFEMForceFieldAndMass,DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,DataTypes), SOFA_TEMPLATE(HexahedralFEMForceField,DataTypes)); typedef HexahedralFEMForceField HexahedralFEMForceFieldT; typedef sofa::core::behavior::Mass MassT; + typedef typename HexahedralFEMForceFieldT::DataTypes HexaFEMDataTypes; + typedef typename MassT::DataTypes MassDataTypes; + + SOFA_CLASS2(SOFA_TEMPLATE(HexahedralFEMForceFieldAndMass,DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,MassDataTypes), SOFA_TEMPLATE(HexahedralFEMForceField,HexaFEMDataTypes)); typedef typename DataTypes::VecCoord VecCoord; typedef typename DataTypes::VecDeriv VecDeriv; diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h index 1a20b30434d..36b3bb49223 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h @@ -35,10 +35,12 @@ template class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass, virtual public HexahedronFEMForceField { public: - SOFA_CLASS2(SOFA_TEMPLATE(HexahedronFEMForceFieldAndMass,DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,DataTypes), SOFA_TEMPLATE(HexahedronFEMForceField,DataTypes)); - typedef HexahedronFEMForceField HexahedronFEMForceFieldT; typedef sofa::core::behavior::Mass MassT; + typedef typename HexahedronFEMForceFieldT::DataTypes HexaFEMDataTypes; + typedef typename MassT::DataTypes MassDataTypes; + + SOFA_CLASS2(SOFA_TEMPLATE(HexahedronFEMForceFieldAndMass,DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,MassDataTypes), SOFA_TEMPLATE(HexahedronFEMForceField,HexaFEMDataTypes)); typedef typename DataTypes::Real Real ; typedef typename DataTypes::Coord Coord ; From e369b6ad793343182a1017c39809cf60cbae86b3 Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Tue, 28 Feb 2023 10:17:10 +0100 Subject: [PATCH 39/41] Fix SofaCUDA plugin compilation --- .../component/mass/CudaDiagonalMass.cu | 14 ++-- .../component/mass/CudaDiagonalMass.h | 4 +- .../component/mass/CudaDiagonalMass.inl | 27 ++------ .../component/mass/CudaMeshMatrixMass.cu | 26 +++---- .../component/mass/CudaMeshMatrixMass.h | 6 +- .../component/mass/CudaMeshMatrixMass.inl | 23 +++---- .../component/mass/CudaUniformMass.cu | 20 +++--- .../component/mass/CudaUniformMass.inl | 69 +++++++------------ 8 files changed, 74 insertions(+), 115 deletions(-) diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.cu b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.cu index dfe3741c21d..3659b428677 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.cu +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.cu @@ -40,8 +40,8 @@ extern "C" void DiagonalMassCuda_accFromFf(unsigned int size, const void * mass, const void* f, void* a); void DiagonalMassCuda_accFromFd(unsigned int size, const void * mass, const void* f, void* a); - void DiagonalMassCuda_addForcef(unsigned int size, const void * mass,const double * g, const void* f); - void DiagonalMassCuda_addForced(unsigned int size, const void * mass,const double * g, const void* f); + void DiagonalMassCuda_addGravitationalForcef(unsigned int size, const void * mass,const double * g, const void* f); + void DiagonalMassCuda_addGravitationalForced(unsigned int size, const void * mass,const double * g, const void* f); } @@ -72,7 +72,7 @@ __global__ void DiagonalMassCuda_accFromF_kernel(int size, const real * inv_mass } template -__global__ void DiagonalMassCuda_addForce_kernel(int size, const real * mass, real g_x, real g_y, real g_z, real* f) +__global__ void DiagonalMassCuda_addGravitationalForce_kernel(int size, const real * mass, real g_x, real g_y, real g_z, real* f) { int index = blockIdx.x * BSIZE+threadIdx.x; int index3 = index * 3; @@ -102,11 +102,11 @@ void DiagonalMassCuda_accFromFf(unsigned int size, const void * mass, const void {DiagonalMassCuda_accFromF_kernel<<< grid, threads >>>(size, (const float *) mass, (const float*)f, (float*)a); mycudaDebugError("DiagonalMassCuda_accFromF_kernel");} } -void DiagonalMassCuda_addForcef(unsigned int size, const void * mass,const double * g, const void* f) +void DiagonalMassCuda_addGravitationalForcef(unsigned int size, const void * mass,const double * g, const void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); - {DiagonalMassCuda_addForce_kernel<<< grid, threads >>>(size,(const float *) mass, g[0],g[1],g[2], (float*)f); mycudaDebugError("DiagonalMassCuda_addForce_kernel");} + {DiagonalMassCuda_addGravitationalForce_kernel<<< grid, threads >>>(size,(const float *) mass, g[0],g[1],g[2], (float*)f); mycudaDebugError("DiagonalMassCuda_addGravitationalForce_kernel");} } @@ -126,11 +126,11 @@ void DiagonalMassCuda_accFromFd(unsigned int size, const void * mass, const void {DiagonalMassCuda_accFromF_kernel<<< grid, threads >>>(size, (const double *) mass, (const double*)f, (double*)a); mycudaDebugError("DiagonalMassCuda_accFromF_kernel");} } -void DiagonalMassCuda_addForced(unsigned int size, const void * mass,const double * g, const void* f) +void DiagonalMassCuda_addGravitationalForced(unsigned int size, const void * mass,const double * g, const void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); - {DiagonalMassCuda_addForce_kernel<<< grid, threads >>>(size,(const double *) mass, g[0],g[1],g[2], (double*)f); mycudaDebugError("DiagonalMassCuda_addForce_kernel");} + {DiagonalMassCuda_addGravitationalForce_kernel<<< grid, threads >>>(size,(const double *) mass, g[0],g[1],g[2], (double*)f); mycudaDebugError("DiagonalMassCuda_addGravitationalForce_kernel");} } #endif diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.h b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.h index 62fa38325fc..165a03072b5 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.h +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.h @@ -62,7 +62,7 @@ template <> void DiagonalMass::accFromF(const core::MechanicalParams* mparams, DataVecDeriv& d_a, const DataVecDeriv& d_f); template<> -void DiagonalMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v); +void DiagonalMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v, const Deriv& gravity); #ifdef SOFA_GPU_CUDA_DOUBLE @@ -75,7 +75,7 @@ template <> void DiagonalMass::accFromF(const core::MechanicalParams* mparams, DataVecDeriv& d_a, const DataVecDeriv& d_f); template<> -void DiagonalMass::addForce(const core::MechanicalParams* mparams, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v); +void DiagonalMass::addGravitationalForce(const core::MechanicalParams* mparams, DataVecDeriv& d_f, const DataVecCoord& d_x, const DataVecDeriv& d_v, const Deriv& gravity); // template<> // bool DiagonalMass::addBBox(double* minBBox, double* maxBBox); diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.inl b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.inl index 00a12755061..83c31cbcaaa 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.inl +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaDiagonalMass.inl @@ -42,8 +42,8 @@ extern "C" void DiagonalMassCuda_accFromFf(unsigned int size, const void * mass, const void* f, void* a); void DiagonalMassCuda_accFromFd(unsigned int size, const void * mass, const void* f, void* a); - void DiagonalMassCuda_addForcef(unsigned int size, const void * mass,const void * g, const void* f); - void DiagonalMassCuda_addForced(unsigned int size, const void * mass,const void * g, const void* f); + void DiagonalMassCuda_addGravitationalForcef(unsigned int size, const void * mass,const void * g, const void* f); + void DiagonalMassCuda_addGravitationalForced(unsigned int size, const void * mass,const void * g, const void* f); } @@ -88,24 +88,12 @@ void DiagonalMass::accFromF(const core::MechanicalParams* /*mpar } template <> -void DiagonalMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/) +void DiagonalMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); - //const VecCoord& x = d_x.getValue(); - //const VecDeriv& v = d_v.getValue(); - type::Vec3d g ( this->getContext()->getGravity() ); const MassVector &masses= d_vertexMass.getValue(); - DiagonalMassCuda_addForcef(masses.size(),masses.deviceRead(),g.ptr(), f.deviceWrite()); - -// // gravity -// Vec3d g ( this->getContext()->getGravity() ); -// Deriv theGravity; -// DataTypes::set ( theGravity, g[0], g[1], g[2]); -// -// for (unsigned int i=0;i::accFromF(const core::MechanicalParams* /*mpar } template<> -void DiagonalMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/) +void DiagonalMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); - //const VecCoord& x = d_x.getValue(); - //const VecDeriv& v = d_v.getValue(); - Vec3d g ( this->getContext()->getGravity() ); const MassVector &masses= d_vertexMass.getValue(); - DiagonalMassCuda_addForced(masses.size(),masses.deviceRead(),g.ptr(), f.deviceWrite()); + DiagonalMassCuda_addGravitationalForced(masses.size(),masses.deviceRead(),gravity.ptr(), f.deviceWrite()); d_f.endEdit(); } diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.cu b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.cu index 4ad614a9b80..5e94c4f47e9 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.cu +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.cu @@ -36,15 +36,15 @@ namespace cuda extern "C" { void MeshMatrixMassCuda_addMDx3f(unsigned int size, float factor, float massLumpingCoeff, const void * vertexMass, const void* dx, void* res); - void MeshMatrixMassCuda_addForce3f(int dim, void * f, const void * vertexMass, const double * gravity, float massLumpingCoeff); + void MeshMatrixMassCuda_addGravitationalForce3f(int dim, void * f, const void * vertexMass, const float * gravity, float massLumpingCoeff); void MeshMatrixMassCuda_accFromF3f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff); void MeshMatrixMassCuda_addMDx2f(unsigned int size, float factor, float massLumpingCoeff, const void * vertexMass, const void* dx, void* res); - void MeshMatrixMassCuda_addForce2f(int dim, void * f, const void * vertexMass, const double * gravity, float massLumpingCoeff); + void MeshMatrixMassCuda_addGravitationalForce2f(int dim, void * f, const void * vertexMass, const float * gravity, float massLumpingCoeff); void MeshMatrixMassCuda_accFromF2f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff); void MeshMatrixMassCuda_addMDx1f(unsigned int size, float factor, float massLumpingCoeff, const void * vertexMass, const void* dx, void* res); - void MeshMatrixMassCuda_addForce1f(int dim, void * f, const void * vertexMass, const double * gravity, float massLumpingCoeff); + void MeshMatrixMassCuda_addGravitationalForce1f(int dim, void * f, const void * vertexMass, const float * gravity, float massLumpingCoeff); void MeshMatrixMassCuda_accFromF1f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff); } @@ -85,7 +85,7 @@ __global__ void MeshMatrixMassCuda_addMDx3f_kernel(real factor, real massLumping } template -__global__ void MeshMatrixMassCuda_addForce3f_kernel(int dim, real * f, const real * vertexMass, real g_x, real g_y, real massLumpingCoeff) +__global__ void MeshMatrixMassCuda_addGravitationalForce3f_kernel(int dim, real * f, const real * vertexMass, real g_x, real g_y, real g_z, real massLumpingCoeff) { int index = blockIdx.x * BSIZE+threadIdx.x; int index2 = index * 3; @@ -93,7 +93,7 @@ __global__ void MeshMatrixMassCuda_addForce3f_kernel(int dim, real * f, const r { f[index2+0] += vertexMass[index] * massLumpingCoeff * g_x; f[index2+1] += vertexMass[index] * massLumpingCoeff * g_y; - f[index2+2] += vertexMass[index] * massLumpingCoeff * g_y; + f[index2+2] += vertexMass[index] * massLumpingCoeff * g_z; } } @@ -145,11 +145,11 @@ void MeshMatrixMassCuda_addMDx3f(unsigned int size, float factor, float massLump {MeshMatrixMassCuda_addMDx3f_kernel<<< grid, threads >>>(factor, massLumpingCoeff, (const float *) vertexMass, (const float *) dx, (float*) res); mycudaDebugError("MeshMatrixMassCuda_addMDx2f_kernel");} } -void MeshMatrixMassCuda_addForce3f(int dim, void * f, const void * vertexMass, const double * g, float massLumpingCoeff) +void MeshMatrixMassCuda_addGravitationalForce3f(int dim, void * f, const void * vertexMass, const float * g, float massLumpingCoeff) { dim3 threads(BSIZE,1); dim3 grid((dim+BSIZE-1)/BSIZE,1); - {MeshMatrixMassCuda_addForce3f_kernel<<< grid, threads >>>(dim, (float *) f, (const float *) vertexMass, g[0], g[1], massLumpingCoeff); mycudaDebugError("MeshMatrixMassCuda_addForce2f_kernel");} + {MeshMatrixMassCuda_addGravitationalForce3f_kernel<<< grid, threads >>>(dim, (float *) f, (const float *) vertexMass, g[0], g[1], g[2], massLumpingCoeff); mycudaDebugError("MeshMatrixMassCuda_addGravitationalForce2f_kernel");} } void MeshMatrixMassCuda_accFromF3f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff) @@ -201,7 +201,7 @@ __global__ void MeshMatrixMassCuda_addMDx2f_kernel(real factor, real massLumping } template -__global__ void MeshMatrixMassCuda_addForce2f_kernel(int dim, real * f, const real * vertexMass, real g_x, real g_y, real massLumpingCoeff) +__global__ void MeshMatrixMassCuda_addGravitationalForce2f_kernel(int dim, real * f, const real * vertexMass, real g_x, real g_y, real massLumpingCoeff) { int index = blockIdx.x * BSIZE+threadIdx.x; int index2 = index * 2; @@ -260,11 +260,11 @@ void MeshMatrixMassCuda_addMDx2f(unsigned int size, float factor, float massLump {MeshMatrixMassCuda_addMDx2f_kernel<<< grid, threads >>>(factor, massLumpingCoeff, (const float *) vertexMass, (const float *) dx, (float*) res); mycudaDebugError("MeshMatrixMassCuda_addMDx2f_kernel");} } -void MeshMatrixMassCuda_addForce2f(int dim, void * f, const void * vertexMass, const double * g, float massLumpingCoeff) +void MeshMatrixMassCuda_addGravitationalForce2f(int dim, void * f, const void * vertexMass, const float * g, float massLumpingCoeff) { dim3 threads(BSIZE,1); dim3 grid((dim+BSIZE-1)/BSIZE,1); - {MeshMatrixMassCuda_addForce2f_kernel<<< grid, threads >>>(dim, (float *) f, (const float *) vertexMass, g[0], g[1], massLumpingCoeff); mycudaDebugError("MeshMatrixMassCuda_addForce2f_kernel");} + {MeshMatrixMassCuda_addGravitationalForce2f_kernel<<< grid, threads >>>(dim, (float *) f, (const float *) vertexMass, g[0], g[1], massLumpingCoeff); mycudaDebugError("MeshMatrixMassCuda_addGravitationalForce2f_kernel");} } void MeshMatrixMassCuda_accFromF2f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff) @@ -312,7 +312,7 @@ __global__ void MeshMatrixMassCuda_addMDx1f_kernel(real factor, real massLumping } template -__global__ void MeshMatrixMassCuda_addForce1f_kernel(int dim, real * f, const real * vertexMass, real g_x, real g_y, real massLumpingCoeff) +__global__ void MeshMatrixMassCuda_addGravitationalForce1f_kernel(int dim, real * f, const real * vertexMass, real g_x, real massLumpingCoeff) { int index = blockIdx.x * BSIZE+threadIdx.x; if (index < dim) @@ -365,11 +365,11 @@ void MeshMatrixMassCuda_addMDx1f(unsigned int size, float factor, float massLump {MeshMatrixMassCuda_addMDx1f_kernel<<< grid, threads >>>(factor, massLumpingCoeff, (const float *) vertexMass, (const float *) dx, (float*) res); mycudaDebugError("MeshMatrixMassCuda_addMDx2f_kernel");} } -void MeshMatrixMassCuda_addForce1f(int dim, void * f, const void * vertexMass, const double * g, float massLumpingCoeff) +void MeshMatrixMassCuda_addGravitationalForce1f(int dim, void * f, const void * vertexMass, const float * g, float massLumpingCoeff) { dim3 threads(BSIZE,1); dim3 grid((dim+BSIZE-1)/BSIZE,1); - {MeshMatrixMassCuda_addForce1f_kernel<<< grid, threads >>>(dim, (float *) f, (const float *) vertexMass, g[0], g[1], massLumpingCoeff); mycudaDebugError("MeshMatrixMassCuda_addForce2f_kernel");} + {MeshMatrixMassCuda_addGravitationalForce1f_kernel<<< grid, threads >>>(dim, (float *) f, (const float *) vertexMass, g[0], massLumpingCoeff); mycudaDebugError("MeshMatrixMassCuda_addGravitationalForce2f_kernel");} } void MeshMatrixMassCuda_accFromF1f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff) diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.h b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.h index 98c7c3e92c4..b7b311da924 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.h +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.h @@ -50,7 +50,7 @@ template<> void MeshMatrixMass::addMDx(const core::MechanicalParams*, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor); template<> -void MeshMatrixMass::addForce(const core::MechanicalParams*, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */); +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams*, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */, const Deriv& /* */); template<> void MeshMatrixMass::accFromF(const core::MechanicalParams*, DataVecDeriv& a, const DataVecDeriv& f); @@ -73,7 +73,7 @@ template<> void MeshMatrixMass::addMDx(const core::MechanicalParams*, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor); template<> -void MeshMatrixMass::addForce(const core::MechanicalParams*, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */); +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams*, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */, const Deriv& /* */); template<> void MeshMatrixMass::accFromF(const core::MechanicalParams*, DataVecDeriv& a, const DataVecDeriv& f); @@ -96,7 +96,7 @@ template<> void MeshMatrixMass::addMDx(const core::MechanicalParams*, DataVecDeriv& f, const DataVecDeriv& dx, SReal factor); template<> -void MeshMatrixMass::addForce(const core::MechanicalParams*, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */); +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams*, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */, const Deriv& /* */); template<> void MeshMatrixMass::accFromF(const core::MechanicalParams*, DataVecDeriv& a, const DataVecDeriv& f); diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.inl b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.inl index b806eaa6fd3..66495d2c3f4 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.inl +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaMeshMatrixMass.inl @@ -38,15 +38,15 @@ using namespace sofa::gpu::cuda; extern "C" { void MeshMatrixMassCuda_addMDx3f(unsigned int size, float factor, float massLumpingCoeff,const void * vertexMass, const void* dx, void* res); - void MeshMatrixMassCuda_addForce3f(int dim, void * f, const void * vertexMass, const double * gravity, float massLumpingCoeff); + void MeshMatrixMassCuda_addGravitationalForce3f(int dim, void * f, const void * vertexMass, const float * gravity, float massLumpingCoeff); void MeshMatrixMassCuda_accFromF3f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff); void MeshMatrixMassCuda_addMDx2f(unsigned int size, float factor, float massLumpingCoeff,const void * vertexMass, const void* dx, void* res); - void MeshMatrixMassCuda_addForce2f(int dim, void * f, const void * vertexMass, const double * gravity, float massLumpingCoeff); + void MeshMatrixMassCuda_addGravitationalForce2f(int dim, void * f, const void * vertexMass, const float * gravity, float massLumpingCoeff); void MeshMatrixMassCuda_accFromF2f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff); - void MeshMatrixMassCuda_addMDx1f(unsigned int size, float factor, float massLumpingCoeff,const void * vertexMass, const void* dx, void* res); - void MeshMatrixMassCuda_addForce1f(int dim, void * f, const void * vertexMass, const double * gravity, float massLumpingCoeff); + void MeshMatrixMassCuda_addMDx1fc(unsigned int size, float factor, float massLumpingCoeff,const void * vertexMass, const void* dx, void* res); + void MeshMatrixMassCuda_addGravitationalForce1f(int dim, void * f, const void * vertexMass, const float * gravity, float massLumpingCoeff); void MeshMatrixMassCuda_accFromF1f(int dim, void * acc, const void * f, const void * vertexMass, float massLumpingCoeff); } }// cuda @@ -85,13 +85,12 @@ void MeshMatrixMass::addMDx(const core::MechanicalParams* /*mpar } template<> -void MeshMatrixMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /* */, const DataVecDeriv& /* */) +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams* d_f, DataVecDeriv& /*vf*/, const DataVecCoord& /* */, const DataVecDeriv& /* */, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); const CudaVector& vertexMass = data.vMass; - type::Vec3d g ( this->getContext()->getGravity() ); - MeshMatrixMassCuda_addForce3f( vertexMass.size(), f.deviceWrite(), vertexMass.deviceRead(), g.ptr(), (float) m_massLumpingCoeff); + MeshMatrixMassCuda_addGravitationalForce3f( vertexMass.size(), f.deviceWrite(), vertexMass.deviceRead(), gravity.ptr(), (float) m_massLumpingCoeff); d_f.endEdit(); } @@ -134,13 +133,12 @@ void MeshMatrixMass::addMDx(const core::MechanicalParams* /*mpar } template<> -void MeshMatrixMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /* */, const DataVecDeriv& /* */) +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams*, DataVecDeriv& d_f, const DataVecCoord& /* */, const DataVecDeriv& /* */, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); const CudaVector& vertexMass = data.vMass; - type::Vec3d g ( this->getContext()->getGravity() ); - MeshMatrixMassCuda_addForce2f( vertexMass.size(), f.deviceWrite(), vertexMass.deviceRead(), g.ptr(), (float) m_massLumpingCoeff); + MeshMatrixMassCuda_addGravitationalForce2f( vertexMass.size(), f.deviceWrite(), vertexMass.deviceRead(), gravity.ptr(), (float) m_massLumpingCoeff); d_f.endEdit(); } @@ -183,13 +181,12 @@ void MeshMatrixMass::addMDx(const core::MechanicalParams* /*mpar } template<> -void MeshMatrixMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /* */, const DataVecDeriv& /* */) +void MeshMatrixMass::addGravitationalForce(const core::MechanicalParams*, DataVecDeriv& d_f, const DataVecCoord& /* */, const DataVecDeriv& /* */, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); const CudaVector& vertexMass = data.vMass; - type::Vec3d g ( this->getContext()->getGravity() ); - MeshMatrixMassCuda_addForce1f( vertexMass.size(), f.deviceWrite(), vertexMass.deviceRead(), g.ptr(), (float) m_massLumpingCoeff); + MeshMatrixMassCuda_addGravitationalForce1f( vertexMass.size(), f.deviceWrite(), vertexMass.deviceRead(), gravity.ptr(), (float) m_massLumpingCoeff); d_f.endEdit(); } diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.cu b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.cu index dbd2ea4347b..815010bfd08 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.cu +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.cu @@ -39,25 +39,25 @@ extern "C" { void UniformMassCuda3f_addMDx(unsigned int size, float mass, void* res, const void* dx); void UniformMassCuda3f_accFromF(unsigned int size, float mass, void* a, const void* f); - void UniformMassCuda3f_addForce(unsigned int size, const float *mg, void* f); + void UniformMassCuda3f_addGravitationalForce(unsigned int size, const float *mg, void* f); void UniformMassCuda3f1_addMDx(unsigned int size, float mass, void* res, const void* dx); void UniformMassCuda3f1_accFromF(unsigned int size, float mass, void* a, const void* f); - void UniformMassCuda3f1_addForce(unsigned int size, const float *mg, void* f); + void UniformMassCuda3f1_addGravitationalForce(unsigned int size, const float *mg, void* f); void UniformMassCudaRigid3f_addMDx(unsigned int size, float mass, void* res, const void* dx); void UniformMassCudaRigid3f_accFromF(unsigned int size, float mass, void* a, const void* dx); - void UniformMassCudaRigid3f_addForce(unsigned int size, const float* mg, void* f); + void UniformMassCudaRigid3f_addGravitationalForce(unsigned int size, const float* mg, void* f); #ifdef SOFA_GPU_CUDA_DOUBLE void UniformMassCuda3d_addMDx(unsigned int size, double mass, void* res, const void* dx); void UniformMassCuda3d_accFromF(unsigned int size, double mass, void* a, const void* f); - void UniformMassCuda3d_addForce(unsigned int size, const double *mg, void* f); + void UniformMassCuda3d_addGravitationalForce(unsigned int size, const double *mg, void* f); void UniformMassCuda3d1_addMDx(unsigned int size, double mass, void* res, const void* dx); void UniformMassCuda3d1_accFromF(unsigned int size, double mass, void* a, const void* f); - void UniformMassCuda3d1_addForce(unsigned int size, const double *mg, void* f); + void UniformMassCuda3d1_addGravitationalForce(unsigned int size, const double *mg, void* f); #endif // SOFA_GPU_CUDA_DOUBLE @@ -402,21 +402,21 @@ void UniformMassCudaRigid3f_accFromF(unsigned int size, float mass, void* a, con mycudaDebugError("UniformMassCudaRigid3f_accFromF"); } -void UniformMassCuda3f_addForce(unsigned int size, const float *mg, void* f) +void UniformMassCuda3f_addGravitationalForce(unsigned int size, const float *mg, void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); {UniformMassCuda3t_addForce_kernel<<< grid, threads >>>(size, mg[0], mg[1], mg[2], (float*)f); mycudaDebugError("UniformMassCuda3t_addForce_kernel");} } -void UniformMassCuda3f1_addForce(unsigned int size, const float *mg, void* f) +void UniformMassCuda3f1_addGravitationalForce(unsigned int size, const float *mg, void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); {UniformMassCuda3t1_addForce_kernel<<< grid, threads >>>(size, mg[0], mg[1], mg[2], (CudaVec4*)f); mycudaDebugError("UniformMassCuda3t1_addForce_kernel");} } -void UniformMassCudaRigid3f_addForce(unsigned int size, const float* mg, void* f) +void UniformMassCudaRigid3f_addGravitationalForce(unsigned int size, const float* mg, void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); @@ -464,14 +464,14 @@ void UniformMassCuda3d1_accFromF(unsigned int size, double mass, void* a, const //UniformMassCuda1t_accFromF_kernel<<< grid, threads >>>(4*size, 1.0f/mass, (double*)a, (const double*)f); } -void UniformMassCuda3d_addForce(unsigned int size, const double *mg, void* f) +void UniformMassCuda3d_addGravitationalForce(unsigned int size, const double *mg, void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); {UniformMassCuda3t_addForce_kernel<<< grid, threads >>>(size, mg[0], mg[1], mg[2], (double*)f); mycudaDebugError("UniformMassCuda3t_addForce_kernel");} } -void UniformMassCuda3d1_addForce(unsigned int size, const double *mg, void* f) +void UniformMassCuda3d1_addGravitationalForce(unsigned int size, const double *mg, void* f) { dim3 threads(BSIZE,1); dim3 grid((size+BSIZE-1)/BSIZE,1); diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl index d7ed89a8697..121256a765e 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl @@ -1,4 +1,4 @@ -/****************************************************************************** +/****************************************************************************** * SOFA, Simulation Open-Framework Architecture * * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * * * @@ -39,25 +39,25 @@ extern "C" { void UniformMassCuda3f_addMDx(unsigned int size, float mass, void* res, const void* dx); void UniformMassCuda3f_accFromF(unsigned int size, float mass, void* a, const void* f); - void UniformMassCuda3f_addForce(unsigned int size, const float *mg, void* f); + void UniformMassCuda3f_addGravitationalForce(unsigned int size, const float *mg, void* f); void UniformMassCuda3f1_addMDx(unsigned int size, float mass, void* res, const void* dx); void UniformMassCuda3f1_accFromF(unsigned int size, float mass, void* a, const void* f); - void UniformMassCuda3f1_addForce(unsigned int size, const float *mg, void* f); + void UniformMassCuda3f1_addGravitationalForce(unsigned int size, const float *mg, void* f); - void UniformMassCudaRigid3f_addMDx(unsigned int size, float mass, void* res, const void* dx); - void UniformMassCudaRigid3f_accFromF(unsigned int size, float mass, void* a, const void* dx); - void UniformMassCudaRigid3f_addForce(unsigned int size, const float* mg, void* f); + void UniformMassCudaRigid3f_addMDx(unsigned int size, float mass, void* res, const void* dx); + void UniformMassCudaRigid3f_accFromF(unsigned int size, float mass, void* a, const void* dx); + void UniformMassCudaRigid3f_addGravitationalForce(unsigned int size, const float* mg, void* f); #ifdef SOFA_GPU_CUDA_DOUBLE void UniformMassCuda3d_addMDx(unsigned int size, double mass, void* res, const void* dx); void UniformMassCuda3d_accFromF(unsigned int size, double mass, void* a, const void* f); - void UniformMassCuda3d_addForce(unsigned int size, const double *mg, void* f); + void UniformMassCuda3d_addGravitationalForce(unsigned int size, const double *mg, void* f); void UniformMassCuda3d1_addMDx(unsigned int size, double mass, void* res, const void* dx); void UniformMassCuda3d1_accFromF(unsigned int size, double mass, void* a, const void* f); - void UniformMassCuda3d1_addForce(unsigned int size, const double *mg, void* f); + void UniformMassCuda3d1_addGravitationalForce(unsigned int size, const double *mg, void* f); #endif // SOFA_GPU_CUDA_DOUBLE } @@ -98,18 +98,13 @@ void UniformMass::accFromF(const core::MechanicalParams* /*mpara } template <> -void UniformMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/) +void UniformMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); - //const VecCoord& x = d_x.getValue(); - //const VecDeriv& v = d_v.getValue(); // weight - type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set( theGravity, g[0], g[1], g[2]); - Deriv mg = theGravity * d_vertexMass.getValue(); - UniformMassCuda3f_addForce(f.size(), mg.ptr(), f.deviceWrite()); + Deriv mg = gravity * d_vertexMass.getValue(); + UniformMassCuda3f_addGravitationalForce(f.size(), mg.ptr(), f.deviceWrite()); d_f.endEdit(); } @@ -137,18 +132,13 @@ void UniformMass::accFromF(const core::MechanicalParams* /*mpar } template <> -void UniformMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/) +void UniformMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/, const Deriv& gravity) { VecDeriv& f = *d_f.beginEdit(); - //const VecCoord& x = d_x.getValue(); - //const VecDeriv& v = d_v.getValue(); // weight - type::Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set( theGravity, g[0], g[1], g[2]); - Deriv mg = theGravity * d_vertexMass.getValue(); - UniformMassCuda3f1_addForce(f.size(), mg.ptr(), f.deviceWrite()); + Deriv mg = gravity * d_vertexMass.getValue(); + UniformMassCuda3f1_addGravitationalForce(f.size(), mg.ptr(), f.deviceWrite()); d_f.endEdit(); } @@ -180,18 +170,15 @@ void UniformMass::accFromF(const core::MechanicalPa } template<> -void UniformMass::addForce(const core::MechanicalParams * /*mparams*/, DataVecDeriv &f, const DataVecCoord& /*x*/, const DataVecDeriv& /*v*/) +void UniformMass::addGravitationalForce(const core::MechanicalParams * /*mparams*/, DataVecDeriv &f, const DataVecCoord& /*x*/, const DataVecDeriv& /*v*/, const Deriv& gravity) { - VecDeriv& _f = *f.beginEdit(); - type::Vec3d g(this->getContext()->getGravity()); float m = d_vertexMass.getValue().mass; - const float mg[] = { (float)(m*g(0)), (float)(m*g(1)), (float)(m*g(2)) }; - UniformMassCudaRigid3f_addForce(_f.size(), mg, _f.deviceWrite()); + const float mg[] = { (float)(m*gravity(0)), (float)(m*gravity(1)), (float)(m*gravity(2)) }; + UniformMassCudaRigid3f_addGravitationalForce(_f.size(), mg, _f.deviceWrite()); f.endEdit(); - } @@ -273,18 +260,13 @@ void UniformMass::accFromF(const core::MechanicalParams* /*mpara } template <> -void UniformMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/) +void UniformMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/, const Deriv gravity) { VecDeriv& f = *d_f.beginEdit(); - //const VecCoord& x = d_x.getValue(); - //const VecDeriv& v = d_v.getValue(); // weight - Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set( theGravity, g[0], g[1], g[2]); - Deriv mg = theGravity * d_vertexMass.getValue(); - UniformMassCuda3d_addForce(f.size(), mg.ptr(), f.deviceWrite()); + Deriv mg = gravity * d_vertexMass.getValue(); + UniformMassCuda3d_addGravitationalForce(f.size(), mg.ptr(), f.deviceWrite()); d_f.endEdit(); } @@ -330,18 +312,13 @@ void UniformMass::accFromF(const core::MechanicalParams* /*mpar } template <> -void UniformMass::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/) +void UniformMass::addGravitationalForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/, const Deriv gravity) { VecDeriv& f = *d_f.beginEdit(); - //const VecCoord& x = d_x.getValue(); - //const VecDeriv& v = d_v.getValue(); // weight - Vec3d g ( this->getContext()->getGravity() ); - Deriv theGravity; - DataTypes::set( theGravity, g[0], g[1], g[2]); - Deriv mg = theGravity * d_vertexMass.getValue(); - UniformMassCuda3d1_addForce(f.size(), mg.ptr(), f.deviceWrite()); + Deriv mg = gravity * d_vertexMass.getValue(); + UniformMassCuda3d1_addGravitationalForce(f.size(), mg.ptr(), f.deviceWrite()); d_f.endEdit(); } From e764d5939529e08c43630d14de3d23167c5d77df Mon Sep 17 00:00:00 2001 From: Hugo Talbot Date: Tue, 28 Feb 2023 11:16:19 +0100 Subject: [PATCH 40/41] Fix CudaUniformMass::getGravitationalPotentialEnergy --- .../src/SofaCUDA/component/mass/CudaUniformMass.inl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl index 121256a765e..1963f6e2394 100644 --- a/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl +++ b/applications/plugins/SofaCUDA/src/SofaCUDA/component/mass/CudaUniformMass.inl @@ -175,7 +175,7 @@ void UniformMass::addGravitationalForce(const core: VecDeriv& _f = *f.beginEdit(); float m = d_vertexMass.getValue().mass; - const float mg[] = { (float)(m*gravity(0)), (float)(m*gravity(1)), (float)(m*gravity(2)) }; + const float mg[] = { (float)(m*gravity[0]), (float)(m*gravity[1])), (float)(m*gravity[2]) }; UniformMassCudaRigid3f_addGravitationalForce(_f.size(), mg, _f.deviceWrite()); f.endEdit(); @@ -183,16 +183,15 @@ void UniformMass::addGravitationalForce(const core: template <> -SReal UniformMass::getPotentialEnergy(const core::MechanicalParams* /*mparams*/, const DataVecCoord& d_x) const +SReal UniformMass::getGravitationalPotentialEnergy(const core::MechanicalParams* /*mparams*/, const DataVecCoord& d_x, const Deriv& gravity) const { const VecCoord& x = d_x.getValue(); SReal e = 0; // gravity - type::Vec3d g ( this->getContext()->getGravity() ); for (unsigned int i=0; i Date: Tue, 28 Feb 2023 14:11:35 +0100 Subject: [PATCH 41/41] fix Windows template ambiguity --- .../elastic/HexahedralFEMForceFieldAndMass.h | 24 +++++++-------- .../elastic/HexahedronFEMForceFieldAndMass.h | 30 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h index 5804347df67..fd644ba9917 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedralFEMForceFieldAndMass.h @@ -33,23 +33,23 @@ namespace sofa::component::solidmechanics::fem::elastic /** Compute Finite Element forces based on hexahedral elements including continuum mass matrices */ -template -class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass, virtual public HexahedralFEMForceField +template +class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass, virtual public HexahedralFEMForceField { public: - typedef HexahedralFEMForceField HexahedralFEMForceFieldT; - typedef sofa::core::behavior::Mass MassT; - typedef typename HexahedralFEMForceFieldT::DataTypes HexaFEMDataTypes; - typedef typename MassT::DataTypes MassDataTypes; + typedef HexahedralFEMForceField HexahedralFEMForceFieldT; + typedef sofa::core::behavior::Mass MassT; + typedef typename HexahedralFEMForceFieldT::DataTypes HexaFEMTDataTypes; + typedef typename MassT::DataTypes MassTDataTypes; - SOFA_CLASS2(SOFA_TEMPLATE(HexahedralFEMForceFieldAndMass,DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,MassDataTypes), SOFA_TEMPLATE(HexahedralFEMForceField,HexaFEMDataTypes)); + SOFA_CLASS2(SOFA_TEMPLATE(HexahedralFEMForceFieldAndMass,TDataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,MassTDataTypes), SOFA_TEMPLATE(HexahedralFEMForceField,HexaFEMTDataTypes)); - typedef typename DataTypes::VecCoord VecCoord; - typedef typename DataTypes::VecDeriv VecDeriv; + typedef typename TDataTypes::VecCoord VecCoord; + typedef typename TDataTypes::VecDeriv VecDeriv; typedef VecCoord Vector; - typedef typename DataTypes::Coord Coord; - typedef typename DataTypes::Deriv Deriv; + typedef typename TDataTypes::Coord Coord; + typedef typename TDataTypes::Deriv Deriv; typedef typename Coord::value_type Real; typedef core::objectmodel::Data DataVecDeriv; typedef core::objectmodel::Data DataVecCoord; @@ -100,7 +100,7 @@ class HexahedralFEMForceFieldAndMass : virtual public sofa::core::behavior::Mass SReal getKineticEnergy(const core::MechanicalParams* /* mparams */, const DataVecDeriv& /*v*/) const override ///< vMv/2 using dof->getV() override { - msg_error() << "HexahedralFEMForceFieldAndMass::getKineticEnergy not yet implemented"; + msg_error() << "HexahedralFEMForceFieldAndMass::getKineticEnergy not yet implemented"; return 0; } diff --git a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h index 36b3bb49223..99b1b7e1741 100644 --- a/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h +++ b/Sofa/Component/SolidMechanics/FEM/Elastic/src/sofa/component/solidmechanics/fem/elastic/HexahedronFEMForceFieldAndMass.h @@ -31,23 +31,23 @@ namespace sofa::component::solidmechanics::fem::elastic /** Compute Finite Element forces based on hexahedral elements including continuum mass matrices */ -template -class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass, virtual public HexahedronFEMForceField +template +class HexahedronFEMForceFieldAndMass : virtual public core::behavior::Mass, virtual public HexahedronFEMForceField { public: - typedef HexahedronFEMForceField HexahedronFEMForceFieldT; - typedef sofa::core::behavior::Mass MassT; - typedef typename HexahedronFEMForceFieldT::DataTypes HexaFEMDataTypes; - typedef typename MassT::DataTypes MassDataTypes; - - SOFA_CLASS2(SOFA_TEMPLATE(HexahedronFEMForceFieldAndMass,DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,MassDataTypes), SOFA_TEMPLATE(HexahedronFEMForceField,HexaFEMDataTypes)); - - typedef typename DataTypes::Real Real ; - typedef typename DataTypes::Coord Coord ; - typedef typename DataTypes::Deriv Deriv ; - typedef typename DataTypes::VecCoord VecCoord ; - typedef typename DataTypes::VecDeriv VecDeriv ; - typedef typename DataTypes::VecReal VecReal ; + typedef HexahedronFEMForceField HexahedronFEMForceFieldT; + typedef sofa::core::behavior::Mass MassT; + typedef typename HexahedronFEMForceFieldT::DataTypes HexaFEMTDataTypes; + typedef typename MassT::DataTypes MassTDataTypes; + + SOFA_CLASS2(SOFA_TEMPLATE(HexahedronFEMForceFieldAndMass,TDataTypes), SOFA_TEMPLATE(sofa::core::behavior::Mass,MassTDataTypes), SOFA_TEMPLATE(HexahedronFEMForceField,HexaFEMTDataTypes)); + + typedef typename TDataTypes::Real Real ; + typedef typename TDataTypes::Coord Coord ; + typedef typename TDataTypes::Deriv Deriv ; + typedef typename TDataTypes::VecCoord VecCoord ; + typedef typename TDataTypes::VecDeriv VecDeriv ; + typedef typename TDataTypes::VecReal VecReal ; typedef VecCoord Vector; typedef core::objectmodel::Data DataVecDeriv;