|
| 1 | +/****************************************************************************** |
| 2 | +* SofaPython3 plugin * |
| 3 | +* (c) 2021 CNRS, University of Lille, INRIA * |
| 4 | +* * |
| 5 | +* This program is free software; you can redistribute it and/or modify it * |
| 6 | +* under the terms of the GNU Lesser General Public License as published by * |
| 7 | +* the Free Software Foundation; either version 2.1 of the License, or (at * |
| 8 | +* your option) any later version. * |
| 9 | +* * |
| 10 | +* This program is distributed in the hope that it will be useful, but WITHOUT * |
| 11 | +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * |
| 12 | +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * |
| 13 | +* for more details. * |
| 14 | +* * |
| 15 | +* You should have received a copy of the GNU Lesser General Public License * |
| 16 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | +******************************************************************************* |
| 18 | +* Contact information: contact@sofa-framework.org * |
| 19 | +******************************************************************************/ |
| 20 | + |
| 21 | +#include <pybind11/pybind11.h> |
| 22 | +#include <SofaPython3/Sofa/Core/Binding_Base.h> |
| 23 | +#include <SofaPython3/Sofa/Core/Binding_Mass.h> |
| 24 | +#include <SofaPython3/Sofa/Core/Binding_Mass_doc.h> |
| 25 | +#include <SofaPython3/PythonFactory.h> |
| 26 | + |
| 27 | +#include <sofa/core/behavior/MechanicalState.h> |
| 28 | +#include <sofa/core/behavior/Mass.h> |
| 29 | +#include <sofa/core/MechanicalParams.h> |
| 30 | +#include <sofa/core/behavior/DefaultMultiMatrixAccessor.h> |
| 31 | +#include <sofa/linearalgebra/CompressedRowSparseMatrix.h> |
| 32 | + |
| 33 | +#include <pybind11/eigen.h> |
| 34 | + |
| 35 | +/// Makes an alias for the pybind11 namespace to increase readability. |
| 36 | +namespace py { using namespace pybind11; } |
| 37 | + |
| 38 | +namespace sofapython3 |
| 39 | +{ |
| 40 | + |
| 41 | +using sofa::core::behavior::Mass; |
| 42 | + |
| 43 | +template<class TDOFType> |
| 44 | +void declare_mass(py::module &m) { |
| 45 | + const std::string pyclass_name = std::string("Mass") + TDOFType::Name(); |
| 46 | + py::class_<Mass<TDOFType>, sofa::core::behavior::ForceField<TDOFType>, py_shared_ptr<Mass<TDOFType>>> f(m, pyclass_name.c_str(), sofapython3::doc::mass::massClass); |
| 47 | + |
| 48 | + using Real = typename TDOFType::Real; |
| 49 | + using EigenSparseMatrix = Eigen::SparseMatrix<typename TDOFType::Real, Eigen::RowMajor>; |
| 50 | + using EigenMatrixMap = Eigen::Map<EigenSparseMatrix>; |
| 51 | + |
| 52 | + f.def("assembleMMatrix", [](Mass<TDOFType>& self) -> EigenSparseMatrix |
| 53 | + { |
| 54 | + sofa::linearalgebra::CompressedRowSparseMatrix<Real> matrix; |
| 55 | + |
| 56 | + if (const auto* mstate = self.getMState()) |
| 57 | + { |
| 58 | + const auto matrixSize = static_cast<sofa::linearalgebra::BaseMatrix::Index>(mstate->getMatrixSize()); |
| 59 | + matrix.resize(matrixSize, matrixSize); |
| 60 | + |
| 61 | + sofa::core::behavior::DefaultMultiMatrixAccessor accessor; |
| 62 | + accessor.addMechanicalState(mstate); |
| 63 | + accessor.setGlobalMatrix(&matrix); |
| 64 | + |
| 65 | + auto mparams = *sofa::core::MechanicalParams::defaultInstance(); |
| 66 | + mparams.setKFactor(0.).setMFactor(1.).setBFactor(0.); |
| 67 | + |
| 68 | + self.addMToMatrix(&mparams, &accessor); |
| 69 | + } |
| 70 | + matrix.compress(); |
| 71 | + |
| 72 | + if (matrix.getColsValue().empty() || matrix.rowBegin.empty() || matrix.colsIndex.empty()) |
| 73 | + return {}; |
| 74 | + |
| 75 | + return EigenMatrixMap(matrix.rows(), matrix.cols(), matrix.getColsValue().size(), |
| 76 | + (typename EigenMatrixMap::StorageIndex*)matrix.rowBegin.data(), |
| 77 | + (typename EigenMatrixMap::StorageIndex*)matrix.colsIndex.data(), |
| 78 | + matrix.colsValue.data()); |
| 79 | + }, sofapython3::doc::mass::assembleMMatrix); |
| 80 | + |
| 81 | + PythonFactory::registerType<Mass<TDOFType>>([](sofa::core::objectmodel::Base* object) |
| 82 | + { |
| 83 | + return py::cast(dynamic_cast<Mass<TDOFType>*>(object)); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +void moduleAddMass(py::module &m) { |
| 89 | + declare_mass<sofa::defaulttype::Vec3dTypes>(m); |
| 90 | + declare_mass<sofa::defaulttype::Vec2dTypes>(m); |
| 91 | + declare_mass<sofa::defaulttype::Vec1dTypes>(m); |
| 92 | + declare_mass<sofa::defaulttype::Vec6dTypes>(m); |
| 93 | + declare_mass<sofa::defaulttype::Rigid3dTypes>(m); |
| 94 | + declare_mass<sofa::defaulttype::Rigid2dTypes>(m); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace sofapython3 |
0 commit comments