Skip to content

Commit f9529d8

Browse files
committed
[Bindings] Add MechanicalParams bindings
Signed-off-by: Jean-Nicolas Brunet <[email protected]>
1 parent 7174ab6 commit f9529d8

File tree

7 files changed

+178
-0
lines changed

7 files changed

+178
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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: [email protected] *
19+
******************************************************************************/
20+
21+
#include <SofaPython3/Sofa/Core/Binding_MechanicalParams.h>
22+
#include <SofaPython3/Sofa/Core/Binding_MechanicalParams_doc.h>
23+
#include <sofa/core/MechanicalParams.h>
24+
25+
namespace py { using namespace pybind11; }
26+
27+
namespace sofapython3 {
28+
29+
void moduleAddMechanicalParams(py::module& m) {
30+
using namespace sofa::core;
31+
py::class_<MechanicalParams> c (m, "MechanicalParams", doc::mechanicalparams::MechanicalParamsClass);
32+
c.def(py::init());
33+
34+
// Dt
35+
c.def_property("dt", &MechanicalParams::dt, &MechanicalParams::setDt);
36+
c.def("setDt", &MechanicalParams::setDt, py::arg("dt"));
37+
38+
// Implicit
39+
c.def_property("implicit", &MechanicalParams::implicit, &MechanicalParams::setImplicit, "Specify if the time integration scheme is implicit.");
40+
c.def("setImplicit", &MechanicalParams::setImplicit, py::arg("v"), "Specify if the time integration scheme is implicit.");
41+
42+
// Mass factor
43+
c.def_property("mFactor", &MechanicalParams::mFactor, &MechanicalParams::setMFactor, "Set Mass matrix contributions factor (for implicit schemes).");
44+
c.def("setMFactor", &MechanicalParams::setMFactor, py::arg("m"), "Set Mass matrix contributions factor (for implicit schemes).");
45+
46+
// Damping factor
47+
c.def_property("bFactor", &MechanicalParams::bFactor, &MechanicalParams::setBFactor, "Set Damping matrix contributions factor (for implicit schemes).");
48+
c.def("setBFactor", &MechanicalParams::setBFactor, py::arg("b"), "Set Damping matrix contributions factor (for implicit schemes).");
49+
50+
// Stiffness factor
51+
c.def_property("kFactor", &MechanicalParams::kFactor, &MechanicalParams::setKFactor, "Set Stiffness matrix contributions factor (for implicit schemes).");
52+
c.def("setKFactor", &MechanicalParams::setKFactor, py::arg("k"), "Set Stiffness matrix contributions factor (for implicit schemes).");
53+
54+
// Symmetric
55+
c.def_property("symmetricMatrix", &MechanicalParams::symmetricMatrix, &MechanicalParams::setSymmetricMatrix, "Set the symmetric matrix flag (for implicit schemes), for solvers specialized on symmetric matrices");
56+
c.def("setSymmetricMatrix", &MechanicalParams::setSymmetricMatrix, "Set the symmetric matrix flag (for implicit schemes), for solvers specialized on symmetric matrices");
57+
58+
// Energy
59+
c.def_property("energy", &MechanicalParams::energy, &MechanicalParams::setEnergy, "Specify if the potential and kinematic energies should be computed.");
60+
c.def("setEnergy", &MechanicalParams::setEnergy, "Specify if the potential and kinematic energies should be computed.");
61+
}
62+
63+
} // namespace sofapython3
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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: [email protected] *
19+
******************************************************************************/
20+
21+
#pragma once
22+
#include <pybind11/pybind11.h>
23+
24+
namespace sofapython3 {
25+
26+
void moduleAddMechanicalParams(pybind11::module &m);
27+
28+
} /// namespace sofapython3
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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: [email protected] *
19+
******************************************************************************/
20+
21+
#pragma once
22+
23+
namespace sofapython3::doc::mechanicalparams
24+
{
25+
26+
static auto MechanicalParamsClass =
27+
R"(
28+
Class gathering parameters use by mechanical components methods, and transmitted by mechanical visitors
29+
)";
30+
31+
32+
}

bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ set(HEADER_FILES
2222
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ForceField_doc.h
2323
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ObjectFactory.h
2424
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ObjectFactory_doc.h
25+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MechanicalParams.h
26+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MechanicalParams_doc.h
2527
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Node.h
2628
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Node_doc.h
2729
${CMAKE_CURRENT_SOURCE_DIR}/Binding_NodeIterator.h
@@ -61,6 +63,7 @@ set(SOURCE_FILES
6163
${CMAKE_CURRENT_SOURCE_DIR}/Data/Binding_DataVectorString.cpp
6264
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ForceField.cpp
6365
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ObjectFactory.cpp
66+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MechanicalParams.cpp
6467
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Node.cpp
6568
${CMAKE_CURRENT_SOURCE_DIR}/Binding_NodeIterator.cpp
6669
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Prefab.cpp

bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using sofa::helper::logging::Message;
3535
#include <SofaPython3/Sofa/Core/Binding_Controller.h>
3636
#include <SofaPython3/Sofa/Core/Binding_DataEngine.h>
3737
#include <SofaPython3/Sofa/Core/Binding_ObjectFactory.h>
38+
#include <SofaPython3/Sofa/Core/Binding_MechanicalParams.h>
3839
#include <SofaPython3/Sofa/Core/Binding_Node.h>
3940
#include <SofaPython3/Sofa/Core/Binding_NodeIterator.h>
4041
#include <SofaPython3/Sofa/Core/Binding_Prefab.h>
@@ -134,6 +135,7 @@ PYBIND11_MODULE(Core, core)
134135
moduleAddDataEngine(core);
135136
moduleAddForceField(core);
136137
moduleAddObjectFactory(core);
138+
moduleAddMechanicalParams(core);
137139
moduleAddNode(core);
138140
moduleAddNodeIterator(core);
139141
moduleAddPrefab(core);

bindings/Sofa/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(PYTHON_FILES
1818
${CMAKE_CURRENT_SOURCE_DIR}/Core/ForceField.py
1919
${CMAKE_CURRENT_SOURCE_DIR}/Core/DataEngine.py
2020
${CMAKE_CURRENT_SOURCE_DIR}/Core/MyRestShapeForceField.py
21+
${CMAKE_CURRENT_SOURCE_DIR}/Core/MechanicalParams.py
2122
${CMAKE_CURRENT_SOURCE_DIR}/Core/PythonRestShapeForceField.py
2223
${CMAKE_CURRENT_SOURCE_DIR}/Core/BaseLink.py
2324
${CMAKE_CURRENT_SOURCE_DIR}/Helper/Message.py
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Sofa
2+
import unittest
3+
4+
5+
class Test(unittest.TestCase):
6+
def test_creation(self):
7+
mparams = Sofa.Core.MechanicalParams()
8+
9+
# dt
10+
mparams.dt = 1.25
11+
self.assertEqual(mparams.dt, 1.25)
12+
mparams.setDt(1.50)
13+
self.assertEqual(mparams.dt, 1.50)
14+
15+
# Implicit
16+
mparams.implicit = True
17+
self.assertTrue(mparams.implicit)
18+
mparams.setImplicit(False)
19+
self.assertFalse(mparams.implicit)
20+
21+
# Mass factor
22+
mparams.mFactor = 1.30
23+
self.assertEqual(mparams.mFactor, 1.30)
24+
mparams.setMFactor(1.35)
25+
self.assertEqual(mparams.mFactor, 1.35)
26+
27+
# Damping factor
28+
mparams.bFactor = 1.35
29+
self.assertEqual(mparams.bFactor, 1.35)
30+
mparams.setBFactor(1.40)
31+
self.assertEqual(mparams.bFactor, 1.40)
32+
33+
# Stiffness factor
34+
mparams.kFactor = 1.45
35+
self.assertEqual(mparams.kFactor, 1.45)
36+
mparams.setKFactor(1.50)
37+
self.assertEqual(mparams.kFactor, 1.50)
38+
39+
# Symmetric
40+
mparams.symmetricMatrix = True
41+
self.assertTrue(mparams.symmetricMatrix)
42+
mparams.setSymmetricMatrix(False)
43+
self.assertFalse(mparams.symmetricMatrix)
44+
45+
# Energy
46+
mparams.energy = True
47+
self.assertTrue(mparams.energy)
48+
mparams.setEnergy(False)
49+
self.assertFalse(mparams.energy)

0 commit comments

Comments
 (0)