From fe7aa03ff179def2735e4079240e4570093c104e Mon Sep 17 00:00:00 2001 From: VannesteFelix Date: Mon, 16 Oct 2023 12:17:10 +0200 Subject: [PATCH] refactore: split diamond exemple into 2 scene: - one with prefab, which does not currently work for the reduction - one without that work -> diamondRobot.py --- python3/mor/wrapper/replaceAndSave.py | 2 +- tools/sofa_test_scene/diamondRobot.py | 118 +++++++++++------- .../sofa_test_scene/diamondRobotWithPrefab.py | 91 ++++++++++++++ 3 files changed, 162 insertions(+), 49 deletions(-) create mode 100644 tools/sofa_test_scene/diamondRobotWithPrefab.py diff --git a/python3/mor/wrapper/replaceAndSave.py b/python3/mor/wrapper/replaceAndSave.py index f70f5ed3..4222ede8 100644 --- a/python3/mor/wrapper/replaceAndSave.py +++ b/python3/mor/wrapper/replaceAndSave.py @@ -174,7 +174,7 @@ def MORreplace(node,type,newParam,initialParam): currentPath = node.getPathName() # print('NODE : '+node.name.value) # print('TYPE : '+str(type)) - #print('PARAM :'+str(newParam[0][0]) ) + # print('PARAM :'+str(newParam[0][0]) ) save = False if 'save' in newParam[1]: save = True diff --git a/tools/sofa_test_scene/diamondRobot.py b/tools/sofa_test_scene/diamondRobot.py index 1be53237..18346db1 100644 --- a/tools/sofa_test_scene/diamondRobot.py +++ b/tools/sofa_test_scene/diamondRobot.py @@ -1,13 +1,11 @@ # -*- coding: utf-8 -*- - -import os import sys +import os # STLIB IMPORT -from stlib3.scene import MainHeader -from stlib3.solver import DefaultSolver -from stlib3.physics.deformable import ElasticMaterialObject from stlib3.physics.constraints import FixedBox +from stlib3.visuals import VisualModel +from stlib3.physics.deformable import ElasticMaterialObject # SOFTROBOTS IMPORT from softrobots.actuators import PullingCable @@ -31,66 +29,90 @@ } ] +plugins=["SofaPython3","SoftRobots","ModelOrderReduction","STLIB", + # normally plugin Sofa.Component is enough but still warning + "Sofa.Component.Visual", + "Sofa.Component.AnimationLoop", + "Sofa.GL.Component.Rendering3D", + "Sofa.Component.Constraint.Lagrangian.Solver", + 'Sofa.Component.IO.Mesh', + 'Sofa.Component.Playback', + 'Sofa.Component.Constraint.Lagrangian.Correction', # Needed to use components [GenericConstraintCorrection] + 'Sofa.Component.Engine.Select', # Needed to use components [BoxROI] + 'Sofa.Component.LinearSolver.Direct', # Needed to use components [SparseLDLSolver] + 'Sofa.Component.Mapping.Linear', # Needed to use components [BarycentricMapping] + 'Sofa.Component.Mass', # Needed to use components [UniformMass] + 'Sofa.Component.ODESolver.Backward', # Needed to use components [EulerImplicitSolver] + 'Sofa.Component.SolidMechanics.FEM.Elastic', # Needed to use components [TetrahedronFEMForceField] + 'Sofa.Component.SolidMechanics.Spring', # Needed to use components [RestShapeSpringsForceField] + 'Sofa.Component.StateContainer', # Needed to use components [MechanicalObject] + 'Sofa.Component.Topology.Container.Dynamic'] # Needed to use components [TetrahedronSetTopologyContainer] + meshPath = os.path.dirname(os.path.abspath(__file__))+'/mesh/' def createScene(rootNode): + rootNode.addObject('FreeMotionAnimationLoop') + rootNode.addObject('GenericConstraintSolver', tolerance="1e-6", maxIterations="1000") + rootNode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") rootNode.addObject('VisualStyle', displayFlags='showVisualModels showForceFields') + rootNode.addObject('RequiredPlugin', pluginName=plugins, printLog=False) rootNode.findData('gravity').value=[0.0,0.0,-9810]; rootNode.findData('dt').value=1 - plugins=["SofaPython3","SoftRobots","ModelOrderReduction","STLIB", - "Sofa.Component.Visual", - "Sofa.Component.AnimationLoop", - "Sofa.GL.Component.Rendering3D", - "Sofa.Component.Constraint.Lagrangian.Solver", - 'Sofa.Component.Constraint.Lagrangian.Correction', # Needed to use components [GenericConstraintCorrection] - 'Sofa.Component.Engine.Select', # Needed to use components [BoxROI] - 'Sofa.Component.LinearSolver.Direct', # Needed to use components [SparseLDLSolver] - 'Sofa.Component.Mapping.Linear', # Needed to use components [BarycentricMapping] - 'Sofa.Component.Mass', # Needed to use components [UniformMass] - 'Sofa.Component.ODESolver.Backward', # Needed to use components [EulerImplicitSolver] - 'Sofa.Component.SolidMechanics.FEM.Elastic', # Needed to use components [TetrahedronFEMForceField] - 'Sofa.Component.SolidMechanics.Spring', # Needed to use components [RestShapeSpringsForceField] - 'Sofa.Component.StateContainer', # Needed to use components [MechanicalObject] - 'Sofa.Component.Topology.Container.Dynamic'] # Needed to use components [TetrahedronSetTopologyContainer] - - for name in plugins: - rootNode.addObject('RequiredPlugin', name=name, printLog=False) - - rootNode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight") - - rootNode.addObject('FreeMotionAnimationLoop') - rootNode.addObject('GenericConstraintSolver', tolerance="1e-6", maxIterations="1000") + volumeMeshFileName=meshPath+'siliconeV0.vtu' + rotation=[90, 0.0, 0.0] + translation=[0.0, 0.0, 35] + totalMass=0.5 + surfaceMeshFileName=meshPath+'surface.stl' + surfaceColor=[0.7, 0.7, 0.7, 0.7] + poissonRatio=0.45 + youngModulus=450 + scale=[1., 1., 1.] + + + modelNode = rootNode.addChild('modelNode') + modelNode.addObject('EulerImplicitSolver', name='integration') + modelNode.addObject('SparseLDLSolver', name="solver", template='CompressedRowSparseMatrixd') + loader = modelNode.addObject('MeshVTKLoader', name='loader', filename=volumeMeshFileName, + rotation=list(rotation), translation=list(translation), + scale3d=list(scale)) + + # loader.tetras.getLinkPath() AND loader.position.getLinkPath() ---> DO NOT WORK + # When you change the scene for the HyperReduction, seems to update the link path to correct new path + # BUT does not load the loader data anymore + modelNode.addObject('TetrahedronSetTopologyContainer', position='@loader.position', + tetras='@loader.tetrahedra', name='container') + modelNode.addObject('MechanicalObject', template='Vec3', name='dofs') + modelNode.addObject('UniformMass', totalMass=totalMass, name='mass') + modelNode.addObject('TetrahedronFEMForceField', template='Vec3', + method='large', name='forcefield', + poissonRatio=poissonRatio, youngModulus=youngModulus) + + visualmodel = modelNode.addChild('visualmodel') + visualmodel.addObject('MeshSTLLoader', name='loader', filename=surfaceMeshFileName) + visualmodel.addObject('OglModel', name="OglModel", src="@loader", + rotation=list(rotation), + translation=list(translation), + scale3d=list(scale), + color=list(surfaceColor), updateNormals=False) + visualmodel.addObject('BarycentricMapping', name='mapping') - modelNode = rootNode.addChild(ElasticMaterialObject( - attachedTo=rootNode, - volumeMeshFileName=meshPath+'siliconeV0.vtu', - name='modelNode', - rotation=[90, 0.0, 0.0], - translation=[0.0, 0.0, 35], - totalMass=0.5, - withConstrain=False, - surfaceMeshFileName=meshPath+'surface.stl', - surfaceColor=[0.7, 0.7, 0.7, 0.7], - poissonRatio=0.45, - youngModulus=450)) - modelNode.addObject('GenericConstraintCorrection', linearSolver='@solver') FixedBox( atPositions=[-15, -15, -40, 15, 15, 10], applyTo=modelNode, - doVisualization=True) + doVisualization=True) for i in range(len(actuatorsParam)): - cable = PullingCable( - attachedTo=modelNode, - name=actuatorsParam[i]['withName'], - cableGeometry=actuatorsParam[i]['withCableGeometry'], - pullPointLocation=actuatorsParam[i]['withAPullPointLocation'], - valueType="displacement") + PullingCable( + attachedTo=modelNode, + name=actuatorsParam[i]['withName'], + cableGeometry=actuatorsParam[i]['withCableGeometry'], + pullPointLocation=actuatorsParam[i]['withAPullPointLocation'], + valueType="displacement") return rootNode diff --git a/tools/sofa_test_scene/diamondRobotWithPrefab.py b/tools/sofa_test_scene/diamondRobotWithPrefab.py new file mode 100644 index 00000000..289de542 --- /dev/null +++ b/tools/sofa_test_scene/diamondRobotWithPrefab.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +import sys +import os + +# STLIB IMPORT +from stlib3.scene import MainHeader +from stlib3.physics.constraints import FixedBox +from stlib3.physics.deformable import ElasticMaterialObject + +# SOFTROBOTS IMPORT +from softrobots.actuators import PullingCable + +actuatorsParam = [ + {'withName' : 'nord', + 'withCableGeometry' : [[0, 97, 45]], + 'withAPullPointLocation' : [0, 10, 30] + }, + {'withName' : 'ouest', + 'withCableGeometry' : [[-97, 0, 45]], + 'withAPullPointLocation' : [-10, 0, 30] + }, + {'withName' : 'sud', + 'withCableGeometry' : [[0, -97, 45]], + 'withAPullPointLocation' : [0, -10, 30] + }, + {'withName' : 'est', + 'withCableGeometry' : [[97, 0, 45]], + 'withAPullPointLocation' : [10, 0, 30] + } + ] + +plugins=["SofaPython3","SoftRobots","ModelOrderReduction","STLIB", + # normally plugin Sofa.Component is enough but still warning + "Sofa.Component.Visual", + "Sofa.Component.AnimationLoop", + "Sofa.GL.Component.Rendering3D", + "Sofa.Component.Constraint.Lagrangian.Solver", + 'Sofa.Component.IO.Mesh', + 'Sofa.Component.Playback', + 'Sofa.Component.Constraint.Lagrangian.Correction', # Needed to use components [GenericConstraintCorrection] + 'Sofa.Component.Engine.Select', # Needed to use components [BoxROI] + 'Sofa.Component.LinearSolver.Direct', # Needed to use components [SparseLDLSolver] + 'Sofa.Component.Mapping.Linear', # Needed to use components [BarycentricMapping] + 'Sofa.Component.Mass', # Needed to use components [UniformMass] + 'Sofa.Component.ODESolver.Backward', # Needed to use components [EulerImplicitSolver] + 'Sofa.Component.SolidMechanics.FEM.Elastic', # Needed to use components [TetrahedronFEMForceField] + 'Sofa.Component.SolidMechanics.Spring', # Needed to use components [RestShapeSpringsForceField] + 'Sofa.Component.StateContainer', # Needed to use components [MechanicalObject] + 'Sofa.Component.Topology.Container.Dynamic'] # Needed to use components [TetrahedronSetTopologyContainer] + +meshPath = os.path.dirname(os.path.abspath(__file__))+'/mesh/' + +def createScene(rootNode): + + MainHeader(rootNode,plugins=plugins, + dt=1.0, + gravity=[0.0, 0.0, -9810.0]) + + # not in Mainheader anymore, need some update + rootNode.addObject('FreeMotionAnimationLoop') + rootNode.addObject('GenericConstraintSolver', tolerance="1e-6", maxIterations="1000") + rootNode.VisualStyle.displayFlags = 'showVisualModels showForceFields' + # ------------------------------------------- + + modelNode = ElasticMaterialObject( + parent=rootNode, + volumeMeshFileName=meshPath+'siliconeV0.vtu', + name='modelNode', + rotation=[90, 0.0, 0.0], + translation=[0.0, 0.0, 35], + totalMass=0.5, + withConstrain=False, + surfaceMeshFileName=meshPath+'surface.stl', + surfaceColor=[0.7, 0.7, 0.7, 0.7], + poissonRatio=0.45, + youngModulus=450) + + FixedBox( + atPositions=[-15, -15, -40, 15, 15, 10], + applyTo=modelNode, + doVisualization=True) + + for i in range(len(actuatorsParam)): + PullingCable( + attachedTo=modelNode, + name=actuatorsParam[i]['withName'], + cableGeometry=actuatorsParam[i]['withCableGeometry'], + pullPointLocation=actuatorsParam[i]['withAPullPointLocation'], + valueType="displacement") + + return rootNode