Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Modern visitor creation #5126

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sofa/simulation/MechanicalOperations.h>
#include <sofa/simulation/VectorOperations.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/behavior/BaseMass.h>
#include <sofa/core/behavior/LinearSolver.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/ScopedAdvancedTimer.h>
Expand Down Expand Up @@ -85,10 +86,18 @@ void EulerExplicitSolver::solve(const core::ExecParams* params,
computeForce(&mop, f);

sofa::Size nbNonDiagonalMasses = 0;
MechanicalGetNonDiagonalMassesCountVisitor(&mop.mparams, &nbNonDiagonalMasses).execute(this->getContext());
this->getContext()->accept(
sofa::core::objectmodel::topDownVisitor
| [&nbNonDiagonalMasses](sofa::core::behavior::BaseMass* mass)
{
if (mass && !mass->isDiagonal())
{
nbNonDiagonalMasses++;
}
});

// Mass matrix is diagonal, solution can thus be found by computing acc = f/m
if(nbNonDiagonalMasses == 0.)
if(nbNonDiagonalMasses == 0)
{
// acc = M^-1 * f
computeAcceleration(&mop, acc, f);
Expand Down
1 change: 1 addition & 0 deletions Sofa/framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ set(HEADER_FILES
${SRC_ROOT}/objectmodel/TagSet.h
${SRC_ROOT}/objectmodel/vectorData.h
${SRC_ROOT}/objectmodel/vectorLinks.h
${SRC_ROOT}/objectmodel/VisitorDirection.h
${SRC_ROOT}/topology/BaseMeshTopology.h
${SRC_ROOT}/topology/BaseTopology.h
${SRC_ROOT}/topology/BaseTopologyData.h
Expand Down
5 changes: 5 additions & 0 deletions Sofa/framework/Core/src/sofa/core/objectmodel/BaseContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sofa/core/objectmodel/ClassInfo.h>
#include <sofa/core/objectmodel/TypeOfInsertion.h>
#include <sofa/core/ComponentNameHelper.h>
#include <sofa/core/objectmodel/VisitorDirection.h>

namespace sofa::simulation
{
Expand Down Expand Up @@ -387,6 +388,10 @@ class SOFA_CORE_API BaseContext : public virtual Base
/// Propagate an event
virtual void propagateEvent( const core::ExecParams* params, Event* );

virtual void accept(const TopDownVisitor&) {}
virtual void accept(const BottomUpVisitor&) {}
virtual void accept(const TopDownVisitor&, const BottomUpVisitor&) {}

/// @}


Expand Down
104 changes: 104 additions & 0 deletions Sofa/framework/Core/src/sofa/core/objectmodel/VisitorDirection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/******************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/core/config.h>

namespace sofa::core::objectmodel
{

struct DirectionalVisitor
{
virtual ~DirectionalVisitor() = default;
DirectionalVisitor() = default;
virtual void operator()(sofa::core::behavior::OdeSolver*) const {}
virtual void operator()(sofa::core::behavior::ConstraintSolver*) const {}
virtual void operator()(sofa::core::BaseMapping*) const {}
virtual void operator()(sofa::core::behavior::BaseMechanicalState*) const {}
virtual void operator()(sofa::core::behavior::BaseMass*) const {}
virtual void operator()(sofa::core::behavior::BaseForceField*) const {}
virtual void operator()(sofa::core::behavior::BaseInteractionForceField* force) const
{
this->operator()((behavior::BaseForceField*)force);
}
virtual void operator()(sofa::core::behavior::BaseProjectiveConstraintSet*) const {}
virtual void operator()(sofa::core::behavior::BaseConstraintSet*) const {}
virtual void operator()(sofa::core::behavior::BaseInteractionProjectiveConstraintSet* constraint) const
{
this->operator()((behavior::BaseProjectiveConstraintSet*)constraint);
}
virtual void operator()(sofa::core::behavior::BaseInteractionConstraint* constraint) const
{
this->operator()((behavior::BaseConstraintSet*)constraint);
}
};

struct SOFA_CORE_API TopDownVisitor : DirectionalVisitor
{
TopDownVisitor() = default;
};
struct SOFA_CORE_API BottomUpVisitor : DirectionalVisitor
{
BottomUpVisitor() = default;
};

inline TopDownVisitor topDownVisitor {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the same name with only no capital letter at the beginning is misleading. Given the fact that this is kind of equivalent to a static object, why not add a suffix ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no guidelines in SOFA relative to the naming of global variables (I already saw a convention with a g_ prefix). But I agree with you the names of the type and the variable are too similar. The STL has the same design for execution policy, and the variable names are truncated to differentiate with the type names. But here, I don't like it so much. Let's discuss that in the dev meeting.
Keep in mind that this is a detail of one of the implementation of the visitors. We still need to decide which one of both implementations we keep. That's the main question of this PR.

inline BottomUpVisitor bottomUpVisitor {};


template<class VisitorDirection, class Func, class VisitedObject,
typename = std::enable_if_t<std::is_invocable_v<Func, VisitedObject*>>>
struct SpecializedVisitor : public virtual VisitorDirection
{
Func m_specializedFunction;

SpecializedVisitor(const VisitorDirection& visitor, const Func& specializedFunction)
: VisitorDirection(visitor), m_specializedFunction(specializedFunction) {}

void operator()(VisitedObject* object) const override
{
m_specializedFunction(object);
}
};

#define DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(ObjectType) \
template<class VisitorDirection, class Func, \
typename = std::enable_if_t<std::is_invocable_v<Func, ObjectType*>>> \
SpecializedVisitor<VisitorDirection, Func, ObjectType> operator|( \
const VisitorDirection& input, \
const Func& f) \
{ \
return {input, f}; \
}

DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::OdeSolver)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::ConstraintSolver)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::BaseMapping)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseMechanicalState)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseMass)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseForceField)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseInteractionForceField)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseProjectiveConstraintSet)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseConstraintSet)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseInteractionProjectiveConstraintSet)
DEFINE_PIPE_OPERATOR_FOR_OBJECT_TYPE(sofa::core::behavior::BaseInteractionConstraint)

}
1 change: 1 addition & 0 deletions Sofa/framework/Simulation/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(HEADER_FILES
${SRC_ROOT}/MechanicalOperations.h
${SRC_ROOT}/MechanicalVPrintVisitor.h
${SRC_ROOT}/MechanicalVisitor.h
${SRC_ROOT}/MechanicalVisitorCreator.h
${SRC_ROOT}/MutationListener.h
${SRC_ROOT}/Node.h
${SRC_ROOT}/Node.inl
Expand Down
Loading
Loading