Skip to content

Commit

Permalink
[Mass,MechanicalLoad] Use enumeration to clarify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hugtalbot committed Aug 13, 2024
1 parent e4fc67f commit 3b1cc06
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
11 changes: 7 additions & 4 deletions Sofa/Component/Mass/src/sofa/component/mass/UniformMass.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ class UniformMass : public core::behavior::Mass<DataTypes>
Data<SReal> d_totalMass; ///< if >0 : total mass of this body
sofa::core::objectmodel::DataFileName d_filenameMass; ///< a .rigid file to automatically load the inertia matrix and other parameters

/// Visualization-related data
Data<bool> d_showCenterOfGravity; ///< display the center of gravity of the system
Data<float> d_showAxisSize; ///< factor length of the axis displayed (only used for rigids)

Data<bool> d_computeMappingInertia; ///< to be used if the mass is placed under a mapping
Data<bool> d_showInitialCenterOfGravity; ///< display the initial center of gravity of the system

Data<bool> d_showX0; ///< display the rest positions

/// optional range of local DOF indices. Any computation involving only
Expand All @@ -78,8 +77,12 @@ class UniformMass : public core::behavior::Mass<DataTypes>
DataSetIndex d_indices; ///< optional local DOF indices. Any computation involving only indices outside of this list are discarded
Data<bool> d_preserveTotalMass; ///< Prevent totalMass from decreasing when removing particles.

bool m_isTotalMassUsed; ///< Boolean specifying whether the data totalMass has been initially given (else vertexMass vector is being used)

/// Enumeration specifying which data was used for initialization
enum InitMethod {
totalMass,
vertexMass
};
InitMethod m_initMethod = totalMass;

////////////////////////// Inherited attributes ////////////////////////////
/// https://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
Expand Down
24 changes: 11 additions & 13 deletions Sofa/Component/Mass/src/sofa/component/mass/UniformMass.inl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ UniformMass<DataTypes>::UniformMass()

sofa::core::objectmodel::Base::addUpdateCallback("updateFromTotalMass", {&d_totalMass}, [this](const core::DataTracker& )
{
if(m_isTotalMassUsed)
if(m_initMethod == totalMass)
{
msg_info() << "dataInternalUpdate: data totalMass has changed";
return updateFromTotalMass();
}
else
else if(m_initMethod == vertexMass)
{
msg_info() << "vertexMass data is initially used, the callback associated with the totalMass is skipped";
return updateFromVertexMass();
Expand All @@ -97,12 +97,12 @@ UniformMass<DataTypes>::UniformMass()

sofa::core::objectmodel::Base::addUpdateCallback("updateFromVertexMass", {&d_vertexMass}, [this](const core::DataTracker& )
{
if(!m_isTotalMassUsed)
if(m_initMethod == vertexMass)
{
msg_info() << "dataInternalUpdate: data vertexMass has changed";
return updateFromVertexMass();
}
else
else if(m_initMethod == totalMass)
{
msg_info() << "totalMass data is initially used, the callback associated with the vertexMass is skipped";
return updateFromTotalMass();
Expand Down Expand Up @@ -162,14 +162,10 @@ void UniformMass<DataTypes>::initDefaultImpl()
{
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);

Mass<DataTypes>::init();

/// SingleStateAccessor checks the mstate pointer to a MechanicalObject
if(!this->isComponentStateValid())
return;

Mass<DataTypes>::init();

WriteAccessor<Data<SetIndexArray > > indices = d_indices;

/// Check filename
if ( d_filenameMass.isSet() && d_filenameMass.getValue() != "unused" )
Expand All @@ -179,6 +175,7 @@ void UniformMass<DataTypes>::initDefaultImpl()


/// Check indices
WriteAccessor<Data<SetIndexArray > > indices = d_indices;
//If d_localRange is set, update indices
if (d_localRange.getValue()[0] >= 0
&& d_localRange.getValue()[1] > 0
Expand Down Expand Up @@ -233,7 +230,7 @@ void UniformMass<DataTypes>::initDefaultImpl()
}


/// Check on data isSet()
/// Check which input data isSet() to define mass initialization
if (d_vertexMass.isSet())
{
if(d_totalMass.isSet())
Expand All @@ -242,20 +239,20 @@ void UniformMass<DataTypes>::initDefaultImpl()
"vertexMass = totalMass / nb_dofs. \n"
"To remove this warning you need to set either totalMass or vertexMass data field, but not both.";

m_isTotalMassUsed = true;
m_initMethod = totalMass;
d_vertexMass.setReadOnly(true);
}
else
{
m_isTotalMassUsed = false;
m_initMethod = vertexMass;
d_totalMass.setReadOnly(true);

msg_info() << "Input vertexMass is used for initialization";
}
}
else if (d_totalMass.isSet())
{
m_isTotalMassUsed = true;
m_initMethod = totalMass;
d_vertexMass.setReadOnly(true);

msg_info() << "Input totalForce is used for initialization";
Expand All @@ -277,6 +274,7 @@ void UniformMass<DataTypes>::initDefaultImpl()
if(!this->isComponentStateValid())
msg_error() << "Initialization process is invalid";


/// Info post-init
msg_info() << "totalMass = " << d_totalMass.getValue() << " | "
"vertexMass = " << d_vertexMass.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ class ConstantForceField : public core::behavior::ForceField<DataTypes>
/// Save system size for update of indices (doUpdateInternal)
size_t m_systemSize;

/// Boolean specifying whether the data totalForce has been initially given
/// (else forces vector is being used)
bool m_isTotalForceUsed;
/// Enumeration specifying which data was used for initialization
enum InitMethod {
totalForce,
forcesVector
};
InitMethod m_initMethod = totalForce;
};

template <>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ ConstantForceField<DataTypes>::ConstantForceField()

sofa::core::objectmodel::Base::addUpdateCallback("updateFromForcesVector", {&d_forces, &d_indices}, [this](const core::DataTracker& )
{
if(!m_isTotalForceUsed)
if(m_initMethod == forcesVector)
{
msg_info() << "dataInternalUpdate: data forces has changed";
return updateFromForcesVector();
}
else
else if(m_initMethod == totalForce)
{
msg_info() << "totalForce data is initially used, the callback associated with the forces vector is skipped";
return updateFromTotalForce();
Expand All @@ -72,12 +72,12 @@ ConstantForceField<DataTypes>::ConstantForceField()

sofa::core::objectmodel::Base::addUpdateCallback("updateFromTotalForce", {&d_totalForce, &d_indices}, [this](const core::DataTracker& )
{
if(m_isTotalForceUsed)
if(m_initMethod == totalForce)
{
msg_info() << "dataInternalUpdate: data totalForce has changed";
return updateFromTotalForce();
}
else
else if(m_initMethod == forcesVector)
{
msg_info() << "forces data is initially used, the callback associated with the totalForce is skipped";
return updateFromForcesVector();
Expand All @@ -89,7 +89,8 @@ ConstantForceField<DataTypes>::ConstantForceField()
template<class DataTypes>
void ConstantForceField<DataTypes>::init()
{
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);


/// Check link to topology
if (l_topology.empty())
Expand All @@ -115,22 +116,23 @@ void ConstantForceField<DataTypes>::init()
}


/// Check on data isSet()
/// Check which input data isSet() to define force initialization
if (d_forces.isSet())
{
if(d_totalForce.isSet())
{
msg_warning() <<"Both data \'forces\' and \'totalForce\' cannot be used simultaneously, please set only one of them to remove this warning";
msg_warning() <<"Both data \'forces\' and \'totalForce\' cannot be used simultaneously.\n"
<< "Vector \'forces\' is used. Please set only one force input to remove this warning";
}

m_isTotalForceUsed = false;
m_initMethod = forcesVector;
d_totalForce.setReadOnly(true);

msg_info() << "Input vector forces is used for initialization";
}
else if (d_totalForce.isSet())
{
m_isTotalForceUsed = true;
m_initMethod = totalForce;
d_forces.setReadOnly(true);

msg_info() << "Input totalForce is used for initialization";
Expand All @@ -151,11 +153,14 @@ void ConstantForceField<DataTypes>::init()
std::iota (std::begin(indicesEdit), std::end(indicesEdit), 0);
}

// init from ForceField

/// init from ForceField
Inherit::init();

// if all init passes, component is valid
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);

/// Trigger callbacks to update data (see constructor)
if(!this->isComponentStateValid())
msg_error() << "Initialization process is invalid";
}


Expand Down Expand Up @@ -369,6 +374,9 @@ void ConstantForceField<DataTypes>::addForce(const core::MechanicalParams* param
template <class DataTypes>
SReal ConstantForceField<DataTypes>::getPotentialEnergy(const core::MechanicalParams* params, const DataVecCoord& x) const
{
if(!this->isComponentStateValid())
return 0_sreal;

SOFA_UNUSED(params);
const VecIndex& indices = d_indices.getValue();
const VecDeriv& f = d_forces.getValue();
Expand Down Expand Up @@ -398,11 +406,14 @@ SReal ConstantForceField<DataTypes>::getPotentialEnergy(const core::MechanicalPa
template <class DataTypes>
void ConstantForceField<DataTypes>::setForce(unsigned i, const Deriv& force)
{
if(m_isTotalForceUsed)
if(!this->isComponentStateValid())
return;

if(m_initMethod == totalForce)
{
msg_warning() << "\'forces\' vector is modified using setForce() while totalMass is initially used. "
<< "Now the 'forces\' vector is used.";
m_isTotalForceUsed = false;
m_initMethod = forcesVector;
}

auto indices = sofa::helper::getWriteAccessor(d_indices);
Expand Down

0 comments on commit 3b1cc06

Please sign in to comment.