-
Notifications
You must be signed in to change notification settings - Fork 55
Add RPyC server/client API #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import multiprocessing | ||
| import threading | ||
| import time | ||
| import rpyc | ||
| from SOFAService import SOFAService, SOFAClient | ||
| import Sofa | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
|
|
||
| SC = SOFAClient() | ||
| SC.start_server(port=18818) | ||
| SC.connect_client(port=18818) | ||
|
|
||
| SC.load_scene("../liver.py") | ||
|
|
||
| SC.setup_shared_memory_for_data(["Liver/dofs.position","Liver/Surf/spheres.position"]) | ||
|
|
||
|
|
||
|
|
||
| asynch_step = None | ||
| currentTime = 0.0 | ||
| while currentTime<0.2: | ||
| if(asynch_step is None or asynch_step.ready): | ||
| #Time to get data from object | ||
| currentTime = SC.sofa_root.getTime() | ||
| print(currentTime) | ||
|
|
||
| print(f"This comes with the socket : {SC.sofa_root.Liver.cg_odesolver.name.value}") | ||
| print(f"This comes with shared memory : {SC.sofa_root.Liver.Surf.spheres.position.value}") | ||
| print(f"This getValue comes with the socket : {SC.sofa_root.Liver.cg_odesolver.name.getValue()}") | ||
| print(f"This getValue comes with shared memory : {SC.sofa_root.Liver.Surf.spheres.position.getValue()}") | ||
| SC.sofa_root.Liver.cg_odesolver.printLog.setValue(True) | ||
| SC.sofa_root.Liver.Surf.spheres.position.setValue([[0,0,0]]) | ||
|
|
||
| #Launch next step | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| asynch_step = SC.asynch_step() | ||
| else: | ||
| print("waiting 0.1 sec") | ||
| time.sleep(0.1) | ||
|
|
||
|
|
||
| # SC.pause_simulation() | ||
| SC.stop_server() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import multiprocessing | ||
| import threading | ||
| import time | ||
| import rpyc | ||
| from SOFAService import SOFAService, SOFAClient | ||
| import Sofa | ||
|
|
||
|
|
||
|
|
||
| def createScene(root): | ||
| #Need to use getValue and setValue instead of the syntax `root.dt = 0.02` or `root.dt.value` to get the actual value | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| root.gravity.setValue([0, -9.81, 0]) | ||
| root.dt.setValue(0.02) | ||
|
|
||
| #RPyC deal in a strange way with list of strings, which leads to error if you use the syntax where you pass a list of plugin name to required plugin. You need to add one required plugin per plugin for it to work, such as done in xml. | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Collision.Detection.Algorithm') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Collision.Detection.Intersection') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Collision.Geometry') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Collision.Response.Contact') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Constraint.Projective') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.IO.Mesh') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.LinearSolver.Iterative') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Mapping.Linear') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Mass') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.ODESolver.Backward') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.SolidMechanics.FEM.Elastic') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.StateContainer') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Topology.Container.Dynamic') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.Component.Visual') | ||
| root.addObject("RequiredPlugin", pluginName='Sofa.GL.Component.Rendering3D') | ||
|
|
||
| root.addObject('DefaultAnimationLoop') | ||
|
|
||
| root.addObject('VisualStyle', displayFlags="showCollisionModels") | ||
| root.addObject('CollisionPipeline', name="CollisionPipeline") | ||
| root.addObject('BruteForceBroadPhase', name="BroadPhase") | ||
| root.addObject('BVHNarrowPhase', name="NarrowPhase") | ||
| root.addObject('CollisionResponse', name="CollisionResponse", response="PenalityContactForceField") | ||
| root.addObject('DiscreteIntersection') | ||
|
|
||
| #Don't forget that this will be launched in the remote, all files should be on its disk or else, use lambda function to capture data and access theme in this funciton | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| root.addObject('MeshOBJLoader', name="LiverSurface", filename="mesh/liver-smooth.obj") | ||
|
|
||
| liver = root.addChild('Liver') | ||
| liver.addObject('EulerImplicitSolver', name="cg_odesolver", rayleighStiffness="0.1", rayleighMass="0.1") | ||
| liver.addObject('CGLinearSolver', name="linear_solver", iterations="25", tolerance="1e-09", threshold="1e-09") | ||
| liver.addObject('MeshGmshLoader', name="meshLoader", filename="mesh/liver.msh") | ||
| liver.addObject('TetrahedronSetTopologyContainer', name="topo", src="@meshLoader") | ||
| liver.addObject('MechanicalObject', name="dofs", src="@meshLoader") | ||
| liver.addObject('TetrahedronSetGeometryAlgorithms', template="Vec3d", name="GeomAlgo") | ||
| liver.addObject('DiagonalMass', name="Mass", massDensity="1.0") | ||
| liver.addObject('TetrahedralCorotationalFEMForceField', template="Vec3d", name="FEM", method="large", poissonRatio="0.3", youngModulus="3000", computeGlobalMatrix="0") | ||
| liver.addObject('FixedProjectiveConstraint', name="FixedConstraint", indices="3 39 64") | ||
|
|
||
| visu = liver.addChild('Visu') | ||
| visu.addObject('OglModel', name="VisualModel", src="@../../LiverSurface") | ||
| visu.addObject('BarycentricMapping', name="VisualMapping", input="@../dofs", output="@VisualModel") | ||
|
|
||
| surf = liver.addChild('Surf') | ||
| surf.addObject('SphereLoader', name="sphereLoader", filename="mesh/liver.sph") | ||
| surf.addObject('MechanicalObject', name="spheres", position="@sphereLoader.position") | ||
| surf.addObject('SphereCollisionModel', name="CollisionModel", listRadius="@sphereLoader.listRadius") | ||
| surf.addObject('BarycentricMapping', name="CollisionMapping", input="@../dofs", output="@spheres") | ||
|
|
||
| return root | ||
|
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
|
|
||
| SC = SOFAClient() | ||
| SC.start_server(port=18818) | ||
| SC.connect_client(port=18818) | ||
|
|
||
| SC.load_scene(createScene) | ||
|
|
||
| #This works only for server and client on the same machine. This tell the server that the passed data path should be copied in shared memory when accessed through the client instead of RPyC. | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| SC.setup_shared_memory_for_data(["Liver/dofs.position","Liver/Surf/spheres.position"]) | ||
|
|
||
|
|
||
|
|
||
| asynch_step = None | ||
| currentTime = 0.0 | ||
| while currentTime<0.2: | ||
| if(asynch_step is None or asynch_step.ready): | ||
| #Time to get data from object | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| currentTime = SC.sofa_root.getTime() | ||
| print(currentTime) | ||
|
|
||
| print(f"This comes with the socket : {SC.sofa_root.Liver.cg_odesolver.name.value}") | ||
| print(f"This comes with shared memory : {SC.sofa_root.Liver.Surf.spheres.position.value}") | ||
| print(f"This getValue comes with the socket : {SC.sofa_root.Liver.cg_odesolver.name.getValue()}") | ||
| print(f"This getValue comes with shared memory : {SC.sofa_root.Liver.Surf.spheres.position.getValue()}") | ||
| SC.sofa_root.Liver.cg_odesolver.printLog.setValue(True) | ||
| SC.sofa_root.Liver.Surf.spheres.position.setValue([[0,0,0]]) | ||
|
|
||
| #Launch next step | ||
bakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| asynch_step = SC.asynch_step() | ||
| else: | ||
| print("waiting 0.1 sec") | ||
| time.sleep(0.1) | ||
|
|
||
| SC.stop_server() | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.