Skip to content

Commit 5a6c82f

Browse files
committed
[Bindings] Add OdeSolver bindings
Signed-off-by: Jean-Nicolas Brunet <[email protected]>
1 parent 43f59cb commit 5a6c82f

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_OdeSolver.h>
22+
#include <SofaPython3/Sofa/Core/Binding_OdeSolver_doc.h>
23+
#include <SofaPython3/Sofa/Core/Binding_Base.h>
24+
#include <sofa/core/behavior/OdeSolver.h>
25+
#include <sofa/core/ExecParams.h>
26+
27+
#include <SofaPython3/PythonFactory.h>
28+
#include <SofaPython3/PythonEnvironment.h>
29+
30+
namespace py { using namespace pybind11; }
31+
32+
namespace sofapython3 {
33+
using namespace sofa::core::behavior;
34+
35+
class PyOdeSolver : public OdeSolver {
36+
public:
37+
SOFA_CLASS(PyOdeSolver, OdeSolver);
38+
void solve(const sofa::core::ExecParams* params, SReal dt, sofa::core::MultiVecCoordId xResult, sofa::core::MultiVecDerivId vResult) final {
39+
PythonEnvironment::gil acquire;
40+
PYBIND11_OVERLOAD_PURE(void, OdeSolver, solve, params, dt, xResult, vResult);
41+
}
42+
};
43+
44+
void moduleAddOdeSolver(py::module& m) {
45+
py::class_<OdeSolver, sofa::core::objectmodel::BaseObject, PyOdeSolver, py_shared_ptr<OdeSolver>> (m, "OdeSolver", doc::odesolver::OdeSolverClass)
46+
.def(py::init([](py::args & /*args*/, py::kwargs & kwargs) {
47+
auto o = sofa::core::sptr<PyOdeSolver> (new PyOdeSolver());
48+
for (auto kv : kwargs) {
49+
auto key = py::cast<std::string>(kv.first);
50+
auto value = py::reinterpret_borrow<py::object>(kv.second);
51+
BindingBase::SetAttr(*o.get(), key, value);
52+
}
53+
return o;
54+
}))
55+
;
56+
57+
/// register the BaseState binding in the downcasting subsystem
58+
PythonFactory::registerType<OdeSolver>([](sofa::core::objectmodel::Base* object)
59+
{
60+
return py::cast(dynamic_cast<OdeSolver*>(object));
61+
});
62+
}
63+
64+
} // 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 moduleAddOdeSolver(pybind11::module &m);
27+
28+
} /// namespace sofapython3
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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::odesolver
24+
{
25+
26+
static auto OdeSolverClass =
27+
R"(
28+
Component responsible for timestep integration, i.e. advancing the state from time t to t+dt.
29+
30+
This class currently control both the integration scheme (explicit,
31+
implicit, static, etc), and the linear system resolution algorithm
32+
(conjugate gradient, matrix direct inversion, etc). Those two aspect will
33+
propably be separated in a future version.
34+
35+
While all computations required to do the integration step are handled by
36+
this object, they should not be implemented directly in it, but instead
37+
the solver propagates orders (or Visitor) to the other components in the
38+
scenegraph that will locally execute them. This allow for greater
39+
flexibility (the solver can just ask for the forces to be computed without
40+
knowing what type of forces are present), as well as performances
41+
(some computations can be executed in parallel).
42+
)";
43+
44+
45+
}

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.h
2323
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ForceField_doc.h
2424
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ObjectFactory.h
25+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_OdeSolver.h
26+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_OdeSolver_doc.h
2527
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ObjectFactory_doc.h
2628
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MechanicalParams.h
2729
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MechanicalParams_doc.h
@@ -67,6 +69,7 @@ set(SOURCE_FILES
6769
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ExecParams.cpp
6870
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ForceField.cpp
6971
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ObjectFactory.cpp
72+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_OdeSolver.cpp
7073
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MechanicalParams.cpp
7174
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MultiVecId.cpp
7275
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Node.cpp

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ using sofa::helper::logging::Message;
3636
#include <SofaPython3/Sofa/Core/Binding_DataEngine.h>
3737
#include <SofaPython3/Sofa/Core/Binding_ExecParams.h>
3838
#include <SofaPython3/Sofa/Core/Binding_ObjectFactory.h>
39+
#include <SofaPython3/Sofa/Core/Binding_OdeSolver.h>
3940
#include <SofaPython3/Sofa/Core/Binding_MechanicalParams.h>
4041
#include <SofaPython3/Sofa/Core/Binding_MultiVecId.h>
4142
#include <SofaPython3/Sofa/Core/Binding_Node.h>
@@ -139,6 +140,7 @@ PYBIND11_MODULE(Core, core)
139140
moduleAddExecParams(core);
140141
moduleAddForceField(core);
141142
moduleAddObjectFactory(core);
143+
moduleAddOdeSolver(core);
142144
moduleAddMechanicalParams(core);
143145
moduleAddMultiVecId(core);
144146
moduleAddNode(core);

0 commit comments

Comments
 (0)