Skip to content

Commit 1016b94

Browse files
authored
Update advanced_timer.py with comments and fix deprecated data (#562)
1 parent 29c4e87 commit 1016b94

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

examples/advanced_timer.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ def __init__(self, *args, **kwargs):
1313
self.use_sofa_profiler_timer = False
1414

1515
def onAnimateBeginEvent(self, event):
16-
if len(Timer.getRecords('Animate')):
16+
if Timer.isEnabled('Animate'):
17+
# The 'Animate' timer that SOFA is supposed to start is already running, we can use it directly
1718
self.use_sofa_profiler_timer = True
1819
else:
20+
# SOFA did not start the 'Animate' timer (e.g., when batch UI and no computation time sampling):
21+
# we need to start another one manually
1922
Timer.setEnabled("cg_timer", True)
2023
Timer.begin("cg_timer")
2124

2225
def onAnimateEndEvent(self, event):
23-
if self.use_sofa_profiler_timer:
24-
records = Timer.getRecords("Animate")
25-
else:
26-
records = Timer.getRecords("cg_timer")
26+
records = Timer.getRecords("Animate" if self.use_sofa_profiler_timer else "cg_timer")
2727

2828
step_time = records['solve']['Mechanical (meca)']['total_time']
2929
print(f"Step took {step_time:.2f} ms")
@@ -43,46 +43,49 @@ def createScene(root):
4343
root.dt = 0.01
4444

4545
# List of required plugins
46-
root.addObject("RequiredPlugin", pluginName=['Sofa.Component.Constraint.Projective',
47-
'Sofa.Component.Engine.Select',
48-
'Sofa.Component.LinearSolver.Iterative',
49-
'Sofa.Component.MechanicalLoad',
50-
'Sofa.Component.ODESolver.Backward',
51-
'Sofa.Component.SolidMechanics.FEM.Elastic',
52-
'Sofa.Component.StateContainer',
53-
'Sofa.Component.Topology.Container.Dynamic',
54-
'Sofa.Component.Topology.Container.Grid',
55-
'Sofa.Component.Visual'
46+
root.addObject("RequiredPlugin", pluginName=[
47+
'Sofa.Component.Constraint.Projective',
48+
'Sofa.Component.Engine.Select',
49+
'Sofa.Component.LinearSolver.Iterative',
50+
'Sofa.Component.MechanicalLoad',
51+
'Sofa.Component.ODESolver.Backward',
52+
'Sofa.Component.SolidMechanics.FEM.Elastic',
53+
'Sofa.Component.StateContainer',
54+
'Sofa.Component.Topology.Container.Dynamic',
55+
'Sofa.Component.Topology.Container.Grid',
56+
'Sofa.Component.Visual'
5657
])
5758

58-
5959
# AnimationLoop
6060
root.addObject('DefaultAnimationLoop')
6161

6262
# Visual style
6363
root.addObject('VisualStyle', displayFlags='showBehaviorModels showForceFields')
6464

6565
# Add the python controller in the scene
66-
root.addObject( TimerController() )
66+
root.addObject(TimerController())
6767

6868
# Create a grid topology of 10x10x60 centered on (0,0,0)
6969
root.addObject('RegularGridTopology', name='grid', min=[-5, -5, -30], max=[5, 5, 30], n=[6, 6, 16])
7070

7171
# Create our mechanical node
72-
root.addChild("meca")
73-
root.meca.addObject("StaticSolver", newton_iterations=5, printLog=False)
74-
root.meca.addObject("CGLinearSolver", iterations=25, tolerance=1e-5, threshold=1e-5)
75-
76-
root.meca.addObject('MechanicalObject', name='mo', position='@../grid.position')
77-
root.meca.addObject('HexahedronSetTopologyContainer', name='mechanical_topology', src='@../grid')
78-
root.meca.addObject('HexahedronFEMForceField', youngModulus=3000, poissonRatio=0)
79-
80-
root.meca.addObject('BoxROI', name='base_roi', box=[-5.01, -5.01, -30.01, 30.01, 30.01, -29.99])
81-
root.meca.addObject('BoxROI', name='top_roi', box=[-5.01, -5.01, +29.99, 5.01, 5.01, +30.01], quad='@mechanical_topology.quads')
82-
83-
root.meca.addObject('FixedProjectiveConstraint', indices='@base_roi.indices')
84-
root.meca.addObject('QuadSetGeometryAlgorithms')
85-
root.meca.addObject('QuadPressureForceField', pressure=[0, -30, 0], quadList='@top_roi.quadInROI', showForces=False)
72+
with root.addChild("meca") as meca:
73+
meca.addObject("NewtonRaphsonSolver", name="newtonSolver_springs", maxNbIterationsNewton=5,
74+
maxNbIterationsLineSearch=1, warnWhenLineSearchFails=False, printLog=False)
75+
meca.addObject("StaticSolver", newtonSolver="@newtonSolver_springs")
76+
meca.addObject("CGLinearSolver", iterations=25, tolerance=1e-5, threshold=1e-5)
77+
78+
meca.addObject('MechanicalObject', name='mo', position='@../grid.position')
79+
meca.addObject('HexahedronSetTopologyContainer', name='mechanical_topology', src='@../grid')
80+
meca.addObject('HexahedronFEMForceField', youngModulus=3000, poissonRatio=0)
81+
82+
meca.addObject('BoxROI', name='base_roi', box=[-5.01, -5.01, -30.01, 30.01, 30.01, -29.99])
83+
meca.addObject('BoxROI', name='top_roi', box=[-5.01, -5.01, +29.99, 5.01, 5.01, +30.01],
84+
quad='@mechanical_topology.quads')
85+
86+
meca.addObject('FixedProjectiveConstraint', indices='@base_roi.indices')
87+
meca.addObject('QuadSetGeometryAlgorithms')
88+
meca.addObject('QuadPressureForceField', pressure=[0, -30, 0], quadList='@top_roi.quadInROI', showForces=False)
8689

8790

8891
# When not using runSofa, this main function will be called python

0 commit comments

Comments
 (0)