forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-forcefield.py
More file actions
78 lines (58 loc) · 2.8 KB
/
example-forcefield.py
File metadata and controls
78 lines (58 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Implementation of a RestShapeForceField in python"""
# coding: utf8
import Sofa
import numpy as np
class RestShapeForceField(Sofa.Core.ForceFieldVec3d):
"""Implementation of a RestShapeForceField in python"""
def __init__(self, ks=1.0, kd=1.0, *args, **kwargs):
Sofa.Core.ForceFieldVec3d.__init__(self, *args, **kwargs)
self.addData("ks", type="float", value=ks, help="The stiffness spring", group="Spring's Properties")
self.addData("kd", type="float", value=kd, help="The damping spring", group="Spring's Properties")
def init(self):
mstate = self.getContext().mechanical
self.initpos = mstate.position.array().copy()
self.k = np.zeros((1,1))
self.f = []
self.d = 0.5
def addForce(self, m, out_force, pos, vel):
with out_force.writeableArray() as wa:
wa[:] += ( (self.initpos-pos.value) * self.ks.value )
def addDForce(self, df, dx, params):
pass
#def addKToMatrix(self, a, b):
# print(" Python::addKToMatrix: ", a, " ", b)
def createScene(root):
root.addObject("RequiredPlugin", pluginName=["Sofa.GL.Component",
"Sofa.Component.ODESolver.Backward",
"Sofa.Component.LinearSolver.Direct",
"Sofa.Component.LinearSolver.Iterative",
"Sofa.Component.Mass",
"Sofa.Component.StateContainer",
"Sofa.Component.Visual"
])
root.addObject("LineAxis")
root.addObject("DefaultAnimationLoop", name="loop")
root.addObject("EulerImplicitSolver")
root.addObject("CGLinearSolver", tolerance=1e-12, threshold=1e-12, iterations=25)
o = root.addChild("Object")
c = o.addObject("MechanicalObject", name="mechanical", position=[0.0,0.0,0.0, 1.0,0.0,0.0])
c.showObject = True
c.showColor = [1.0,0.0,0.0,1.0]
c.drawMode = 1
o.addObject("UniformMass", name="mass", totalMass=[0.1])
o.addObject( RestShapeForceField(name="CPPObject", ks=2.0, kd=0.1))
return root
def main():
import SofaImGui
import Sofa.Gui
root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()
print("End of simulation.")
if __name__ == '__main__':
main()