11"""Implementation of a RestShapeForceField in python"""
22# coding: utf8
33import Sofa
4+ import SofaRuntime
5+ import Sofa .Gui
46import numpy as np
57
68class RestShapeForceField (Sofa .Core .ForceField ):
79 """Implementation of a RestShapeForceField in python"""
810 def __init__ (self , ks = 1.0 , kd = 1.0 , * args , ** kwargs ):
911 Sofa .Core .ForceField .__init__ (self , * args , ** kwargs )
1012 self .addData ("ks" , type = "float" , value = ks , help = "The stiffness spring" , group = "Spring's Properties" )
11- self .addData ("kd" , type = "float" , value = kd , help = "The stiffness spring" , group = "Spring's Properties" )
13+ self .addData ("kd" , type = "float" , value = kd , help = "The damping spring" , group = "Spring's Properties" )
1214
1315 def init (self ):
14- self .initpos = self .mstate .position .array ().copy ()
16+ mstate = self .getContext ().mechanical
17+ self .initpos = mstate .position .array ().copy ()
1518 self .k = np .zeros ((1 ,1 ))
1619 self .f = []
1720 self .d = 0.5
1821
1922 def addForce (self , m , out_force , pos , vel ):
20- out_force .value += ( (self .initpos - pos .value ) * self .ks .value )
23+ with out_force .writeableArray () as wa :
24+ wa [:] += ( (self .initpos - pos .value ) * self .ks .value )
2125
2226 def addDForce (self , df , dx , params ):
2327 pass
@@ -40,8 +44,28 @@ def createScene(node):
4044 c .showColor = [1.0 ,0.0 ,0.0 ,1.0 ]
4145 c .drawMode = 1
4246
43- o .addObject ("UniformMass" , name = "mass" , vertexMass = 0.1 )
44- o .addObject ( RestShapeForceField (name = "CPPObject" , kd = 2.0 ))
45-
47+ o .addObject ("UniformMass" , name = "mass" , totalMass = [ 0.1 ] )
48+ o .addObject ( RestShapeForceField (name = "CPPObject" , ks = 2.0 , kd = 0.1 ))
49+
4650 return node
4751
52+ def main ():
53+ SofaRuntime .importPlugin ("SofaOpenglVisual" )
54+ SofaRuntime .importPlugin ("SofaBaseMechanics" )
55+ SofaRuntime .importPlugin ("SofaImplicitOdeSolver" )
56+
57+ root = Sofa .Core .Node ("root" )
58+ createScene (root )
59+ Sofa .Simulation .init (root )
60+
61+ Sofa .Gui .GUIManager .Init ("myscene" , "qglviewer" )
62+ Sofa .Gui .GUIManager .createGUI (root , __file__ )
63+ Sofa .Gui .GUIManager .SetDimension (1080 , 1080 )
64+ Sofa .Gui .GUIManager .MainLoop (root )
65+ Sofa .Gui .GUIManager .closeGUI ()
66+
67+ print ("End of simulation." )
68+
69+
70+ if __name__ == '__main__' :
71+ main ()
0 commit comments