Home › Forum › SOFA › Using SOFA › [SOLVED] Export Object velocity and position values in each time step
Tagged: Sofa programming, SOFA_2106, Windows_10
- This topic has 4 replies, 2 voices, and was last updated 3 years, 2 months ago by Thresholddd.
-
AuthorPosts
-
12 October 2021 at 08:14 #20591ThresholdddBlocked
Hi, everyone,
I am new to Sofa. I currently use Sofa v21.06 binaries and sofaPython3 in windows 10. Now I want to export nodes’ positions and velocity in each timestep. Do you know how to do that?Thanks very much!
Regards,
Yuan12 October 2021 at 19:28 #20597HugoKeymasterHi @qdyuan4619
welcome to the SOFA forum then!
You can either use one of the Exporter in SOFA like the MeshVTKExporter (see example here).Or you could use python to access data field (“positions” and “velocity”) of the MechanicalObject and write then into any format/file you’d like.
Best wishes,
Hugo
13 October 2021 at 14:41 #20603ThresholdddBlockedHi, @Hugo,
Thanks you for your advice, I read the examples about writing data and successfully record states by using monitor or VtkExamples, but I have one question left that I do not know how to access data field in python. I did not find related examples in sofaPython3 and sofa. Thanks very much for your help!Yuan
14 October 2021 at 09:52 #20607HugoKeymasterHi @qdyuan4619
You can take a look at this example I wrote. A python controller is added in the scene and it accesses data in the scene like the
totalForce
data of a ConstantForceField as shown in theonKeypressedEvent()
function of the controller.import sys import Sofa class ManageParticles(Sofa.Core.Controller): def __init__(self, *args, **kwargs): # These are needed (and the normal way to override from a python class) Sofa.Core.Controller.__init__(self, *args, **kwargs) self.CFF = kwargs.get("ForceField") self.rootNode = kwargs.get("rootNode") self.iteration = 1 def onKeypressedEvent(self, event): key = event['key'] if key=="+" : with self.CFF.totalForce.writeableArray() as wa: wa[0] += 0.01 if key=="-" : with self.CFF.totalForce.writeableArray() as wa: wa[0] -= 0.01 return 0; def createScene(node): node.addObject('RequiredPlugin', name="SIOS-load", pluginName="SofaImplicitOdeSolver") node.addObject('RequiredPlugin', name="SBC-load", pluginName="SofaBoundaryCondition") node.dt = 0.01 node.gravity = [0,0,0] Particle = node.addChild('Particle-0') Particle.addObject('EulerImplicitSolver') Particle.addObject('CGLinearSolver', threshold='1e-09', tolerance='1e-09', iterations='200') Particle.addObject('MechanicalObject', showObject='1', position='0 0 0 0 0 0 1', name='Particle-0', template='Rigid3d') Particle.addObject('UniformMass', totalMass='1') Particle.addObject('ConstantForceField', name="CFF", totalForce="0 0 0 0 0 0" ) node.addObject( ManageParticles(name="MyParticlesController", ForceField=Particle.CFF, rootNode=node) ) return 0; def main(): import SofaRuntime import Sofa.Gui # Make sure to load all SOFA libraries SofaRuntime.importPlugin("SofaBaseMechanics") SofaRuntime.importPlugin("SofaOpenglVisual") #Create the root node root = Sofa.Core.Node("root") # Call the below 'createScene' function to create the scene graph createScene(root) Sofa.Simulation.init(root) # Launch the GUI (qt or qglviewer) Sofa.Gui.GUIManager.Init("myscene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root, __file__) Sofa.Gui.GUIManager.SetDimension(1080, 1080) # Initialization of the scene will be done here Sofa.Gui.GUIManager.MainLoop(root) Sofa.Gui.GUIManager.closeGUI() print("Simulation is done.") # Function used only if this script is called from a python environment if __name__ == '__main__': main()
Best wishes,
Hugo
19 October 2021 at 05:35 #20654ThresholdddBlockedHi, @Hugo,
Thank you for your example! I will try it and mark this problem as solved! Thank you very much again!
Best wishes,
YUAN -
AuthorPosts
- You must be logged in to reply to this topic.