Skip to content

Commit

Permalink
refactore: split diamond exemple into 2 scene:
Browse files Browse the repository at this point in the history
- one with prefab, which does not currently work for the reduction
- one without that work -> diamondRobot.py
  • Loading branch information
VannesteFelix committed Oct 16, 2023
1 parent 297bf2b commit fe7aa03
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 49 deletions.
2 changes: 1 addition & 1 deletion python3/mor/wrapper/replaceAndSave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
118 changes: 70 additions & 48 deletions tools/sofa_test_scene/diamondRobot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
91 changes: 91 additions & 0 deletions tools/sofa_test_scene/diamondRobotWithPrefab.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fe7aa03

Please sign in to comment.