From deffd785791a77565356c3a46ad5bbaae7622f60 Mon Sep 17 00:00:00 2001 From: Themis Skamagkis Date: Thu, 13 Aug 2026 11:08:11 +0200 Subject: [PATCH 1/2] Add binding for getPotentialEnergy --- .../Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp index de486854..326b44dc 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp @@ -246,6 +246,11 @@ namespace sofapython3 matrix.colsValue.data()); }, sofapython3::doc::forceField::assembleKMatrix); + f.def("getPotentialEnergy", [](ForceField& self) -> SReal + { + return self.getPotentialEnergy(MechanicalParams::defaultInstance()); + }, sofapython3::doc::forceField::getPotentialEnergy); + PythonFactory::registerType>([](sofa::core::objectmodel::Base* object) { return py::cast(dynamic_cast*>(object)); From 7e198600240bc3a70de9901ecfec1040446c4365 Mon Sep 17 00:00:00 2001 From: Themis Skamagkis Date: Thu, 13 Aug 2026 11:36:17 +0200 Subject: [PATCH 2/2] Add the header --- .../src/SofaPython3/Sofa/Core/Binding_ForceField_doc.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField_doc.h b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField_doc.h index 123f781e..d4f79872 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField_doc.h +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField_doc.h @@ -41,4 +41,11 @@ stiffness_matrix = self.force_field.assembleKMatrix() Returns: A scipy.sparse.csr_matrix object representing the stiffness matrix of the force field )"; + +static constexpr const char* getPotentialEnergy = R"( +Potential energy of the force field, evaluated at its current positions. + +Returns: + The potential energy of the force field, as a float +)"; }