Skip to content

Commit f39682d

Browse files
authored
add binding to convert elastic parameters to Lamé's parameters (#621)
* add binding to convert elastic parameters to Lamé's parameters * add example of toLameParameters3D * cleaning
1 parent 372cdfd commit f39682d

5 files changed

Lines changed: 180 additions & 2 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
23+
#include <SofaPython3/SofaDeformable/Binding_LameParameters.h>
24+
#include <sofa/component/solidmechanics/fem/elastic/impl/LameParameters.h>
25+
26+
namespace sofapython3
27+
{
28+
29+
void moduleAddLameParameters(pybind11::module &m)
30+
{
31+
using namespace sofa::component::solidmechanics::fem::elastic;
32+
33+
m.def(
34+
"toLameParameters2D",
35+
[](SReal youngModulus, SReal poissonRatio) {
36+
LameLambda<SReal> lambda{0};
37+
LameMu<SReal> mu{0};
38+
toLameParameters<2>(YoungModulus<SReal>(youngModulus),
39+
PoissonRatio<SReal>(poissonRatio), lambda, mu);
40+
return std::make_pair(mu.get(), lambda.get());
41+
},
42+
pybind11::arg("youngModulus"), pybind11::arg("poissonRatio"),
43+
"Converts Young's modulus and Poisson's ratio to Lamé parameters (mu, "
44+
"lambda) for 2D.");
45+
46+
m.def(
47+
"toLameParameters3D",
48+
[](SReal youngModulus, SReal poissonRatio) {
49+
LameLambda<SReal> lambda{0};
50+
LameMu<SReal> mu{0};
51+
toLameParameters<3>(YoungModulus<SReal>(youngModulus),
52+
PoissonRatio<SReal>(poissonRatio), lambda, mu);
53+
return std::make_pair(mu.get(), lambda.get());
54+
},
55+
pybind11::arg("youngModulus"), pybind11::arg("poissonRatio"),
56+
"Converts Young's modulus and Poisson's ratio to Lamé parameters (mu, "
57+
"lambda) for 3D.");
58+
}
59+
60+
} // namespace sofapython3
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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+
#pragma once
22+
23+
#include <pybind11/pybind11.h>
24+
25+
namespace sofapython3
26+
{
27+
28+
void moduleAddLameParameters(pybind11::module& m);
29+
30+
} // namespace sofapython3

bindings/Modules/src/SofaPython3/SofaDeformable/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
project(Bindings.Modules.SofaDeformable)
22

33
set(SOURCE_FILES
4-
${CMAKE_CURRENT_SOURCE_DIR}/Module_SofaDeformable.cpp
4+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LameParameters.cpp
55
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring.cpp
66
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SpringForceField.cpp
7+
${CMAKE_CURRENT_SOURCE_DIR}/Module_SofaDeformable.cpp
78
)
89

910
set(HEADER_FILES
11+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LameParameters.h
1012
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring.h
1113
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring_doc.h
1214
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SpringForceField.h
@@ -18,6 +20,7 @@ if (NOT TARGET SofaPython3::Plugin)
1820
endif()
1921

2022
sofa_find_package(Sofa.Component.SolidMechanics.Spring REQUIRED)
23+
sofa_find_package(Sofa.Component.SolidMechanics.FEM.Elastic REQUIRED)
2124

2225
SP3_add_python_module(
2326
TARGET ${PROJECT_NAME}
@@ -26,6 +29,6 @@ SP3_add_python_module(
2629
DESTINATION Sofa
2730
SOURCES ${SOURCE_FILES}
2831
HEADERS ${HEADER_FILES}
29-
DEPENDS Sofa.Component.SolidMechanics.Spring SofaPython3::Plugin SofaPython3::Bindings.Sofa.Core
32+
DEPENDS Sofa.Component.SolidMechanics.Spring Sofa.Component.SolidMechanics.FEM.Elastic SofaPython3::Plugin SofaPython3::Bindings.Sofa.Core
3033

3134
)

bindings/Modules/src/SofaPython3/SofaDeformable/Module_SofaDeformable.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <pybind11/pybind11.h>
2222
#include <SofaPython3/SofaDeformable/Binding_LinearSpring.h>
2323
#include <SofaPython3/SofaDeformable/Binding_SpringForceField.h>
24+
#include <SofaPython3/SofaDeformable/Binding_LameParameters.h>
2425

2526

2627
namespace py { using namespace pybind11; }
@@ -34,6 +35,7 @@ PYBIND11_MODULE(SofaDeformable, m)
3435

3536
moduleAddLinearSpring(m);
3637
moduleAddSpringForceField(m);
38+
moduleAddLameParameters(m);
3739
}
3840

3941
} // namespace sofapython3

examples/stvenant_kirchhoff.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import Sofa
2+
import Sofa.SofaDeformable
3+
4+
from Sofa.Units.Definitions import s, m, cm, N, g, kg, MPa
5+
from Sofa.Units.UnitSystem import MechanicalUnitSystem
6+
7+
def createScene(root_node):
8+
9+
plugins = [
10+
"Sofa.Component.Constraint.Projective",
11+
"Sofa.Component.Engine.Select",
12+
"Sofa.Component.LinearSolver.Direct",
13+
"Sofa.Component.Mapping.Linear",
14+
"Sofa.Component.Mass",
15+
"Sofa.Component.ODESolver.Backward",
16+
"Sofa.Component.SolidMechanics.FEM.HyperElastic",
17+
"Sofa.Component.StateContainer",
18+
"Sofa.Component.Topology.Container.Dynamic",
19+
"Sofa.Component.Topology.Container.Grid",
20+
"Sofa.Component.Topology.Mapping",
21+
"Sofa.GL.Component.Rendering3D",
22+
"Sofa.Component.SolidMechanics.FEM.Elastic"
23+
]
24+
root_node.addObject('RequiredPlugin', pluginName=plugins)
25+
26+
scene_unit = MechanicalUnitSystem(s, cm, g)
27+
28+
root_node.gravity = scene_unit([0, -9.81, 0], N / kg)
29+
root_node.dt = scene_unit(0.01, s)
30+
31+
root_node.addObject('EulerImplicitSolver', name="backwardEuler", rayleighStiffness=0.1, rayleighMass=0.1)
32+
root_node.addObject('SparseLDLSolver', template="CompressedRowSparseMatrixMat3x3d")
33+
root_node.addObject('RegularGridTopology', name="grid", min=[-5, -5, 0], max=[5, 5, 40], n=[5, 5, 20])
34+
root_node.addObject('MechanicalObject', template="Vec3", name="state")
35+
root_node.addObject('NodalMassDensity', property=scene_unit(1150, kg/m**3))
36+
root_node.addObject('FEMMass', template="Vec3,Hexahedron", topology="@grid")
37+
38+
with root_node.addChild('tetra') as tetra:
39+
40+
tetra.addObject('TetrahedronSetTopologyContainer', name="Tetra_topo")
41+
tetra.addObject('TetrahedronSetTopologyModifier', name="Modifier")
42+
tetra.addObject('TetrahedronSetGeometryAlgorithms', template="Vec3", name="GeomAlgo", drawTetrahedra="false")
43+
tetra.addObject('Hexa2TetraTopologicalMapping', input="@grid", output="@Tetra_topo")
44+
45+
young_modulus = scene_unit(0.5, MPa)
46+
poisson_ratio = 0.45
47+
lame_parameters = Sofa.SofaDeformable.toLameParameters3D(young_modulus, poisson_ratio)
48+
tetra.addObject('TetrahedronHyperelasticityFEMForceField', name="FEM", topology="@Tetra_topo",
49+
ParameterSet=lame_parameters, materialName="StVenantKirchhoff")
50+
51+
with tetra.addChild('triangles') as triangles:
52+
53+
triangles.addObject('TriangleSetTopologyContainer', name="Container")
54+
triangles.addObject('TriangleSetTopologyModifier')
55+
triangles.addObject('Tetra2TriangleTopologicalMapping', input="@../Tetra_topo", output="@Container")
56+
57+
with triangles.addChild('Visu') as visu:
58+
59+
visu.addObject('OglModel', name="Visual", color="red")
60+
visu.addObject('IdentityMapping', input="@../../../state", output="@Visual")
61+
62+
root_node.addObject('BoxROI', template="Vec3", name="box_roi", box=[-6, -6, -1, 6, 6, 0.1])
63+
root_node.addObject('FixedProjectiveConstraint', template="Vec3", indices="@box_roi.indices")
64+
65+
66+
def main():
67+
import Sofa
68+
import SofaImGui
69+
70+
root = Sofa.Core.Node("root")
71+
createScene(root)
72+
Sofa.Simulation.initRoot(root)
73+
74+
import Sofa.Gui
75+
Sofa.Gui.GUIManager.Init("myscene", "imgui")
76+
Sofa.Gui.GUIManager.createGUI(root, __file__)
77+
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
78+
Sofa.Gui.GUIManager.MainLoop(root)
79+
Sofa.Gui.GUIManager.closeGUI()
80+
81+
82+
if __name__ == '__main__':
83+
main()

0 commit comments

Comments
 (0)