-
Notifications
You must be signed in to change notification settings - Fork 46
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
Access to the constraint matrix #459
Open
alxbilger
wants to merge
1
commit into
sofa-framework:master
Choose a base branch
from
alxbilger:constraintmatrix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
bindings/Sofa/src/SofaPython3/Sofa/Types/Binding_CompressedRowSparseMatrix.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/****************************************************************************** | ||
* SOFA, Simulation Open-Framework Architecture * | ||
* (c) 2021 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/>. * | ||
******************************************************************************* | ||
* Contact information: [email protected] * | ||
******************************************************************************/ | ||
#include <SofaPython3/Sofa/Types/Binding_CompressedRowSparseMatrix.h> | ||
|
||
#include <sofa/core/objectmodel/BaseData.h> | ||
#include <sofa/core/objectmodel/Data.h> | ||
#include <sofa/defaulttype/RigidTypes.h> | ||
#include <sofa/linearalgebra/CompressedRowSparseMatrixConstraint.h> | ||
#include <pybind11/eigen.h> | ||
#include <sofa/linearalgebra/CompressedRowSparseMatrixConstraintEigenUtils.h> | ||
#include <sofa/type/trait/is_specialization_of.h> | ||
#include <SofaPython3/PythonFactory.h> | ||
|
||
|
||
namespace py { using namespace pybind11; } | ||
|
||
namespace sofapython3 | ||
{ | ||
|
||
template<class TBlock> | ||
typename sofa::linearalgebra::CompressedRowSparseMatrixToEigenSparseVec<TBlock>::EigenSparseMatrix | ||
toEigen(const sofa::linearalgebra::CompressedRowSparseMatrixConstraint<TBlock>& matrix) | ||
{ | ||
static sofa::linearalgebra::CompressedRowSparseMatrixToEigenSparseVec<TBlock> convert; | ||
return convert(matrix); | ||
} | ||
|
||
template<class MatrixDeriv> | ||
void bindCompressedRowSparseMatrixConstraint(pybind11::module& m) | ||
{ | ||
static_assert(sofa::type::trait::is_specialization_of_v<MatrixDeriv, sofa::linearalgebra::CompressedRowSparseMatrixConstraint>, | ||
"Template argument must be a specialization of CompressedRowSparseMatrixConstraint"); | ||
|
||
py::class_<sofa::Data<MatrixDeriv>, sofa::core::objectmodel::BaseData, | ||
std::unique_ptr<sofa::Data<MatrixDeriv>, pybind11::nodelete> > crsmc(m, MatrixDeriv::Name() ); | ||
|
||
crsmc.def_property_readonly("value", [](sofa::Data<MatrixDeriv>& self) | ||
{ | ||
sofa::helper::ReadAccessor accessor(self); | ||
return toEigen(accessor.ref()); | ||
}); | ||
|
||
PythonFactory::registerType(MatrixDeriv::Name(), [](sofa::core::BaseData* data) -> py::object { | ||
auto matrix = reinterpret_cast<sofa::Data<MatrixDeriv>*>(data); | ||
return py::cast(matrix); | ||
}); | ||
} | ||
|
||
/** | ||
* Given a template parameter pack, create the bindings of all the templates in | ||
* the pack, making sure that there are no duplicate types. | ||
*/ | ||
template <typename T, typename... Args> | ||
void bindAllCompressedRowSparseMatrixConstraintTypes(pybind11::module& m) | ||
{ | ||
static_assert(!(std::is_same_v<T, Args> || ...), "Error: Duplicate types found!"); | ||
|
||
bindCompressedRowSparseMatrixConstraint<T>(m); | ||
if constexpr (sizeof...(Args) > 0) | ||
{ | ||
bindAllCompressedRowSparseMatrixConstraintTypes<Args...>(m); | ||
} | ||
} | ||
|
||
void moduleAddCompressedRowSparseMatrix(pybind11::module& m) | ||
{ | ||
using namespace sofa::defaulttype; | ||
|
||
bindAllCompressedRowSparseMatrixConstraintTypes< | ||
Vec1Types::MatrixDeriv, | ||
Vec2Types::MatrixDeriv, | ||
Vec3Types::MatrixDeriv, | ||
Vec6Types::MatrixDeriv, | ||
Rigid2Types::MatrixDeriv, | ||
Rigid3Types::MatrixDeriv | ||
>(m); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
bindings/Sofa/src/SofaPython3/Sofa/Types/Binding_CompressedRowSparseMatrix.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/****************************************************************************** | ||
* SOFA, Simulation Open-Framework Architecture * | ||
* (c) 2021 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/>. * | ||
******************************************************************************* | ||
* Contact information: [email protected] * | ||
******************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace sofapython3 { | ||
|
||
void moduleAddCompressedRowSparseMatrix(pybind11::module& m); | ||
|
||
} // namespace sofapython3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Required import for python | ||
import Sofa | ||
import SofaRuntime | ||
import numpy as np | ||
|
||
|
||
# Function called when the scene graph is being created | ||
def createScene(root): | ||
root.addObject("RequiredPlugin", pluginName=["Sofa.Component.AnimationLoop", | ||
"Sofa.Component.Constraint.Lagrangian.Correction", | ||
"Sofa.Component.Constraint.Lagrangian.Model", | ||
"Sofa.Component.Constraint.Lagrangian.Solver", | ||
"Sofa.Component.Constraint.Projective", | ||
"Sofa.Component.IO.Mesh", | ||
"Sofa.Component.LinearSolver.Direct", | ||
"Sofa.Component.Mapping.MappedMatrix", | ||
"Sofa.Component.Mass", | ||
"Sofa.Component.ODESolver.Backward", | ||
"Sofa.Component.Topology.Container.Dynamic", | ||
"Sofa.Component.Mapping.NonLinear", | ||
"Sofa.Component.StateContainer" | ||
]) | ||
|
||
root.addObject("FreeMotionAnimationLoop", solveVelocityConstraintFirst=True) | ||
root.addObject("GenericConstraintSolver", tolerance=1e-9, maxIterations=1000) | ||
root.addObject("StringMeshCreator", name="loader", resolution="20") | ||
|
||
root.addObject("EulerImplicitSolver") | ||
root.addObject("EigenSimplicialLLT", template='CompressedRowSparseMatrixMat3x3d') | ||
root.addObject("GenericConstraintCorrection") | ||
|
||
root.addObject("EdgeSetTopologyContainer", name="topo", position="@loader.position", edges="@loader.edges") | ||
mechanical_object = root.addObject("MechanicalObject", name="defoDOF", template="Vec3d") | ||
root.addObject("EdgeSetGeometryAlgorithms", drawEdges=True) | ||
root.addObject("FixedProjectiveConstraint", indices=[0]) | ||
root.addObject("DiagonalMass", name="mass", totalMass="1e-3") | ||
|
||
ext = root.addChild("extensionsNode") | ||
ext.addObject("MechanicalObject", template="Vec1d", name="extensionsDOF") | ||
ext.addObject("DistanceMapping", name="distanceMapping", topology="@topo") | ||
ext.addObject("UniformLagrangianConstraint", template="Vec1d", iterative=True) | ||
|
||
root.addObject(MatrixAccessController('MatrixAccessor', name='matrixAccessor', mechanical_object=mechanical_object)) | ||
|
||
|
||
class MatrixAccessController(Sofa.Core.Controller): | ||
|
||
def __init__(self, *args, **kwargs): | ||
Sofa.Core.Controller.__init__(self, *args, **kwargs) | ||
self.mechanical_object = kwargs.get("mechanical_object") | ||
|
||
def onBuildConstraintSystemEndEvent(self, event): | ||
constraint_matrix = self.mechanical_object.constraint.value | ||
|
||
print(self.mechanical_object.constraint.linkpath) | ||
|
||
print('====================================') | ||
print('Constraint matrix in constraint space') | ||
print('====================================') | ||
print('dtype: ' + str(constraint_matrix.dtype)) | ||
print('shape: ' + str(constraint_matrix.shape)) | ||
print('ndim: ' + str(constraint_matrix.ndim)) | ||
|
||
print(constraint_matrix) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.