-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathtest_access_subcomponents.py
101 lines (83 loc) · 3.67 KB
/
test_access_subcomponents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Test that sockets, inputs, and outputs are functional in python.
"""
import os, unittest
import opensim as osim
test_dir = os.path.join(os.path.dirname(os.path.abspath(osim.__file__)),
'tests')
# Silence warning messages if mesh (.vtp) files cannot be found.
osim.Model.setDebugLevel(0)
class TestAccessSubcomponents(unittest.TestCase):
def test_individual_components(self):
model = osim.Model(os.path.join(test_dir, "arm26.osim"))
muscle = model.getComponent('BICshort')
assert muscle.getName() == 'BICshort'
# No downcasting necessary!
muscle.get_max_isometric_force() # Method on Muscle.
muscle = model.updComponent('BICshort')
muscle.set_max_isometric_force(100)
def test_ComponentPath(self):
# Ensure that Component functions support both strings and
# ComponentPath objects.
model = osim.Model(os.path.join(test_dir, "arm26.osim"))
muscle = model.getComponent(osim.ComponentPath('BICshort'))
assert muscle.getName() == 'BICshort'
assert (muscle.getAbsolutePathName() ==
osim.ComponentPath('/arm26/BICshort'))
assert muscle.getAbsolutePathName().__str__() == '/arm26/BICshort'
def test_component_list(self):
model = osim.Model(os.path.join(test_dir, "arm26.osim"))
num_components = 0
for comp in model.getComponentsList():
num_components += 1
assert num_components > 0
num_bodies = 0
for body in model.getBodyList():
num_bodies += 1
body.getMass()
assert num_bodies == 2
num_joints = 0
for joint in model.getJointList():
num_joints += 1
joint.get_reverse()
assert num_joints == 2
# Custom filtering.
num_bodies = 0
for frame in model.getFrameList():
body = osim.Body.safeDownCast(frame)
if body != None:
num_bodies += 1
print(body.getName())
body.getInertia()
assert num_bodies == 2
model = osim.Model()
thelenMuscle = osim.Thelen2003Muscle("Darryl", 1, 0.5, 0.5, 0)
millardMuscle = osim.Millard2012EquilibriumMuscle("Matt", 1, 0.5,
0.5, 0)
model.addComponent(thelenMuscle)
model.addComponent(millardMuscle)
# Total number of muscles is 2.
assert len(set(model.getMuscleList())) == 2
for muscle in model.getMuscleList():
assert (isinstance(muscle, osim.Thelen2003Muscle) or
isinstance(muscle, osim.Millard2012EquilibriumMuscle))
# There is exactly 1 Thelen2003Muscle.
assert len(set(model.getThelen2003MuscleList())) == 1
for muscle in model.getThelen2003MuscleList():
assert isinstance(muscle, osim.Thelen2003Muscle)
# There is exactly 1 Millard2012EquilibriumMuscle.
assert len(set(model.getMillard2012EquilibriumMuscleList())) == 1
for muscle in model.getMillard2012EquilibriumMuscleList():
assert isinstance(muscle, osim.Millard2012EquilibriumMuscle)
def test_component_filter(self):
model = osim.Model(os.path.join(test_dir, "arm26.osim"))
comps = model.getMuscleList()
comps.setFilter(osim.ComponentFilterAbsolutePathNameContainsString('BIC'))
count = 0
BICnames = ['BIClong', 'BICshort']
for comp in comps:
assert comp.getName() == BICnames[count]
# The ComponentList iterator does the downcasting for us!
assert type(comp) == osim.Thelen2003Muscle
count += 1
assert count == 2