-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFO2_MBDModel.py
282 lines (282 loc) · 19.5 KB
/
AFO2_MBDModel.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import numpy as np
import math
import opensim
import AFO9_MeshMechanics
import AFO10_OpenSimAPI
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Drop landing model - New AFO design with strap orientations in the musculoskeletal model for drop landing
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def MBDmodel_Droplanding_AFO (file_MBD, Platform_inclination, AFO_representation, AFO_material, DesignVariables):
# AFO_representation=[AFO_top_local, AFO_bottom_local, AFO_strap_length]
AFO_top_tibial=AFO_representation[0]
AFO_bottom_calcn=AFO_representation[1]
AFO_strap_length=AFO_representation[2]
# AFO_material=[AFO_Fmagnitude, AFO_FLrelationship]
AFO_Fmagnitude=AFO_material[0]
AFO_F_L=AFO_material[1]
[AFO_bottom_location, AFO_top_location, theta_0_values, n_elements]=DesignVariables # Design variables
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Read the MBD osim file and add the coordinate value of the AFO strip end points in the the model
Platform_inclination1=[math.radians(Platform_inclination[0]), math.radians(Platform_inclination[1]), math.radians(Platform_inclination[2])]
with open (file_MBD,"r",encoding="utf-8") as f:
lines=f.readlines()
with open(file_MBD,"w",encoding="utf-8") as f_w:
index=0
index_t=0
for line in lines:
index +=1
if line.strip()=='<Coordinate name="platform_rx">':
f_w.write(line)
lines[index+1]=' <default_value>%f</default_value>' % (Platform_inclination1[0]) + '\n'
elif line.strip()=='<Coordinate name="platform_ry">':
f_w.write(line)
lines[index+1]=' <default_value>%f</default_value>' % (Platform_inclination1[1]) + '\n'
elif line.strip()=='<Coordinate name="platform_rz">':
f_w.write(line)
lines[index+1]=' <default_value>%f</default_value>' % (Platform_inclination1[2]) + '\n'
elif line.strip()=='</CoordinateLimitForce>' and not lines[index].strip().startswith('<CoordinateLimitForce'):
index_t=index
f_w.write(line)
elif index_t !=0 and line.strip()!='<groups>':
pass
elif index_t!=0 and line.strip()=='<groups>':
index_t=0
for k1 in range (len(AFO_top_tibial)):
AFO_F_L_T=AFO_F_L[k1]
# AFO_F_L_T=AFO_F_L_T.reshape(2,-1)
f_w.writelines([' <Ligament name="orthosis_',str(k1+1),'">',"\n"])
f_w.writelines([''' <!--Flag indicating whether the force is applied or not. If true the forceis applied to the MultibodySystem otherwise the force is not applied.NOTE: Prior to OpenSim 4.0, this behavior was controlled by the 'isDisabled' property, where 'true' meant that force was not being applied. Thus, if 'isDisabled' is true, then 'appliesForce` is false.-->
<appliesForce>true</appliesForce>
<!--the set of points defining the path of the ligament-->
<GeometryPath name="geometrypath">
<!--The set of points defining the path-->
<PathPointSet>
<objects>\n'''])
f_w.writelines([' <PathPoint name="orthosis_',str(k1+1),'-P1">',"\n"])
f_w.writelines([''' <!--Path to a Component that satisfies the Socket 'parent_frame' of type PhysicalFrame (description: The frame in which this path point is defined.).-->
<socket_parent_frame>/bodyset/tibia_r</socket_parent_frame>
<!--The fixed location of the path point expressed in its parent frame.-->\n'''])
AFO_top_withoutbracket='%.8f %.8f %.8f' %(AFO_top_tibial[k1,0],AFO_top_tibial[k1,1],AFO_top_tibial[k1,2])
f_w.writelines([" <location>",AFO_top_withoutbracket,"</location>\n"," </PathPoint>\n"])
f_w.writelines([' <PathPoint name="orthosis_',str(k1+1),'-P2">',"\n"])
f_w.writelines([''' <!--Path to a Component that satisfies the Socket 'parent_frame' of type PhysicalFrame (description: The frame in which this path point is defined.).-->
<socket_parent_frame>/bodyset/calcn_r</socket_parent_frame>
<!--The fixed location of the path point expressed in its parent frame.-->\n'''])
AFO_bottom_withoutbracket='%.8f %.8f %.8f' %(AFO_bottom_calcn[k1,0],AFO_bottom_calcn[k1,1],AFO_bottom_calcn[k1,2])
f_w.writelines([" <location>",AFO_bottom_withoutbracket,"</location>\n"])
f_w.writelines([''' </PathPoint>
</objects>
<groups />
</PathPointSet>
<!--The wrap objects that are associated with this path-->
<PathWrapSet>
<objects>
<PathWrap name="pathwrap">
<!--A WrapObject that this PathWrap interacts with.-->
<wrap_object>foot_r_tibia</wrap_object>
<!--The wrapping method used to solve the path around the wrap object.-->
<method>hybrid</method>
<!--The range of indices to use to compute the path over the wrap object.-->
<range>-1 -1</range>
</PathWrap>
</objects>
<groups />
</PathWrapSet>
<!--Default appearance attributes for this GeometryPath-->
<Appearance>
<!--Flag indicating whether the associated Geometry is visible or hidden.-->
<visible>true</visible>
<!--The color, (red, green, blue), [0, 1], used to display the geometry. -->
<color>0 1 0</color>
</Appearance>
</GeometryPath>
<!--resting length of the ligament-->\n'''])
AFO_strap_length_withoutbracket='%.8f' %(AFO_strap_length[k1])
f_w.writelines([" <resting_length>",AFO_strap_length_withoutbracket,"</resting_length>\n"," <!--force magnitude that scales the force-length curve-->\n"])
f_w.writelines([" <pcsa_force>",str(AFO_Fmagnitude),"</pcsa_force>\n"])
f_w.writelines([''' <!--Function representing the force-length behavior of the ligament-->
<SimmSpline name="force_length_curve">\n'''])
f_w.writelines([''' <x>'''])
for j in range (len(AFO_F_L_T[0])):
f_w.write(str(AFO_F_L_T[0][j]))
f_w.write(' ')
f_w.writelines(['''</x>
<y>'''])
for m in range (len(AFO_F_L_T[1])):
f_w.write(str(AFO_F_L_T[1][m]))
f_w.write(' ')
f_w.writelines(['''</y>
</SimmSpline>
</Ligament>\n'''])
f_w.writelines([''' </objects>\n'''])
f_w.write(line)
else:
f_w.write(line)
# Set the resting lengths of the straps in the MSK model
AFO10_OpenSimAPI.LigSetRestingLength(file_MBD)
# Set the Force-length FL relationship in the MSK model
AFO_FLrelationship=AFO9_MeshMechanics.MeshMechanics(file_MBD, theta_0_values, n_elements) # The force length relationship calculated from AFO9_MeshMechanics
# Change the force-length relationship in the MSK model to the one obtained from the AFO9_MeshMechanics
with open (file_MBD, "r+", encoding="utf-8") as f:
lines=f.read()
f.seek(0)
for i in range (len(AFO_FLrelationship)):
FL_x_str=' '.join(str(i) for i in AFO_FLrelationship[i][0])
# Change the x and y values of force-length relationship from <x> 1 <x> to real number
lines=lines.replace('<x> '+ str(i+1)+' </x>', '<x> '+str(FL_x_str)+' </x>') # No space between <x> and str(i+1)
lines=lines.replace('<x>'+ str(i+1)+' </x>', '<x>'+str(FL_x_str)+' </x>') # No space between <x> and str(i+1) in left
lines=lines.replace('<x> '+ str(i+1)+'</x>', '<x> '+str(FL_x_str)+'</x>') # No space between <x> and str(i+1) in right
lines=lines.replace('<x>'+ str(i+1)+'</x>', '<x>'+str(FL_x_str)+'</x>') # No space between <x> and str(i+1) in both sides
FL_y_str=' '.join(str(i) for i in AFO_FLrelationship[i][1])
lines=lines.replace('<y> '+ str(i+1)+' </y>', '<y> '+str(FL_y_str)+' </y>')
lines=lines.replace('<y>'+ str(i+1)+' </y>', '<y>'+str(FL_y_str)+' </y>')
lines=lines.replace('<y> '+ str(i+1)+'</y>', '<y> '+str(FL_y_str)+'</y>')
lines=lines.replace('<y>'+ str(i+1)+'</y>', '<y>'+str(FL_y_str)+'</y>')
f.write(lines)
#
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def MBDmodel_Gait_AFO (MBD_model, MBD_model_AFO, AFO_representation, AFO_material, DesignVariables): # MBD model file with AFO with default position: angular postion=[0]
# AFO_representation=[AFO_top_local, AFO_bottom_local, AFO_length]
AFO_top_tibial=AFO_representation[0]
AFO_bottom_calcn=AFO_representation[1]
AFO_strap_length=AFO_representation[2]
# AFO_material=[AFO_Fmagnitude, AFO_FLrelationship]
AFO_Fmagnitude=AFO_material[0]
AFO_F_L=AFO_material[1]
[AFO_bottom_location, AFO_top_location, theta_0_values, n_elements]=DesignVariables # Design variables
# Read the MBD osim file and add the coordinate value of the AFO strip end points in the the model
with open (MBD_model,"r",encoding="utf-8") as f:
lines=f.readlines()
with open(MBD_model_AFO,"w",encoding="utf-8") as f_w:
index=0
index_t=0
for line in lines:
index +=1
if lines[index-7].strip().startswith('<mesh_file>r_fibula.vtp</mesh_file>') and line.strip()!='<WrapCylinder name="foot_r_tibia">':
f_w.writelines([''' <WrapCylinder name="foot_r_tibia">
<!--Whether or not the WrapObject is considered active in computing paths-->
<active>true</active>
<!--Body-fixed Euler angle sequence for the orientation of the WrapObject-->
<xyz_body_rotation>1.5707963 0 0</xyz_body_rotation>
<!--Translation of the WrapObject.-->
<translation>-0.00949 -0.38 0.006</translation>
<!--The name of quadrant over which the wrap object is active. For example, '+x' or '-y' to set the sidedness of the wrapping.-->
<quadrant>all</quadrant>
<!--Default appearance for this Geometry-->
<Appearance>
<!--Flag indicating whether the associated Geometry is visible or hidden.-->
<visible>true</visible>
<!--The opacity used to display the geometry between 0:transparent, 1:opaque.-->
<opacity>1</opacity>
<!--The color, (red, green, blue), [0, 1], used to display the geometry. -->
<color>0 1 1</color>
<!--Visuals applied to surfaces associated with this Appearance.-->
<SurfaceProperties>
<!--The representation (1:Points, 2:Wire, 3:Shaded) used to display the object.-->
<representation>3</representation>
</SurfaceProperties>
</Appearance>
<!--The radius of the cylinder.-->
<radius>0.0365</radius>
<!--The length of the cylinder.-->
<length>0.18</length>
</WrapCylinder>\n'''])
if line.strip()=='</CoordinateActuator>' and not lines[index].strip().startswith('<CoordinateActuator') and not lines[index].strip().startswith('<PointActuator'):
index_t=index
f_w.write(line)
elif index_t !=0 and line.strip()!='<groups>':
pass
elif index_t!=0 and line.strip()=='<groups>':
index_t=0
for k1 in range (len(AFO_top_tibial)):
AFO_F_L_T=AFO_F_L[k1]
#AFO_F_L_T=AFO_F_L_T.reshape(2,-1)
f_w.writelines([' <Ligament name="orthosis_',str(k1+1),'">',"\n"])
f_w.writelines([''' <!--Flag indicating whether the force is applied or not. If true the forceis applied to the MultibodySystem otherwise the force is not applied.NOTE: Prior to OpenSim 4.0, this behavior was controlled by the 'isDisabled' property, where 'true' meant that force was not being applied. Thus, if 'isDisabled' is true, then 'appliesForce` is false.-->
<appliesForce>true</appliesForce>
<!--the set of points defining the path of the ligament-->
<GeometryPath name="geometrypath">
<!--The set of points defining the path-->
<PathPointSet>
<objects>\n'''])
f_w.writelines([' <PathPoint name="orthosis_',str(k1+1),'-P1">',"\n"])
f_w.writelines([''' <!--Path to a Component that satisfies the Socket 'parent_frame' of type PhysicalFrame (description: The frame in which this path point is defined.).-->
<socket_parent_frame>/bodyset/tibia_r</socket_parent_frame>
<!--The fixed location of the path point expressed in its parent frame.-->\n'''])
AFO_top_withoutbracket='%.8f %.8f %.8f' %(AFO_top_tibial[k1,0],AFO_top_tibial[k1,1],AFO_top_tibial[k1,2])
f_w.writelines([" <location>",AFO_top_withoutbracket,"</location>\n"," </PathPoint>\n"])
f_w.writelines([' <PathPoint name="orthosis_',str(k1+1),'-P2">',"\n"])
f_w.writelines([''' <!--Path to a Component that satisfies the Socket 'parent_frame' of type PhysicalFrame (description: The frame in which this path point is defined.).-->
<socket_parent_frame>/bodyset/calcn_r</socket_parent_frame>
<!--The fixed location of the path point expressed in its parent frame.-->\n'''])
AFO_bottom_withoutbracket='%.8f %.8f %.8f' %(AFO_bottom_calcn[k1,0],AFO_bottom_calcn[k1,1],AFO_bottom_calcn[k1,2])
f_w.writelines([" <location>",AFO_bottom_withoutbracket,"</location>\n"])
f_w.writelines([''' </PathPoint>
</objects>
<groups />
</PathPointSet>
<!--The wrap objects that are associated with this path-->
<PathWrapSet>
<objects>
<PathWrap name="pathwrap">
<!--A WrapObject that this PathWrap interacts with.-->
<wrap_object>foot_r_tibia</wrap_object>
<!--The wrapping method used to solve the path around the wrap object.-->
<method>hybrid</method>
<!--The range of indices to use to compute the path over the wrap object.-->
<range>-1 -1</range>
</PathWrap>
</objects>
<groups />
</PathWrapSet>
<!--Default appearance attributes for this GeometryPath-->
<Appearance>
<!--Flag indicating whether the associated Geometry is visible or hidden.-->
<visible>true</visible>
<!--The color, (red, green, blue), [0, 1], used to display the geometry. -->
<color>0 1 0</color>
</Appearance>
</GeometryPath>
<!--resting length of the ligament-->\n'''])
AFO_length_withoutbracket='%.8f' %(AFO_strap_length[k1])
f_w.writelines([" <resting_length>",AFO_length_withoutbracket,"</resting_length>\n"," <!--force magnitude that scales the force-length curve-->\n"])
f_w.writelines([" <pcsa_force>",str(AFO_Fmagnitude),"</pcsa_force>\n"])
f_w.writelines([''' <!--Function representing the force-length behavior of the ligament-->
<SimmSpline name="force_length_curve">\n'''])
f_w.writelines([''' <x>'''])
for j in range (len(AFO_F_L_T[0])):
f_w.write(str(AFO_F_L_T[0][j]))
f_w.write(' ')
f_w.writelines(['''</x>
<y>'''])
for m in range (len(AFO_F_L_T[1])):
f_w.write(str(AFO_F_L_T[1][m]))
f_w.write(' ')
f_w.writelines(['''</y>
</SimmSpline>
</Ligament>\n'''])
f_w.writelines([''' </objects>\n'''])
f_w.write(line)
else:
f_w.write(line)
# Set the resting lengths of the straps in the MSK model
AFO10_OpenSimAPI.LigSetRestingLength(MBD_model_AFO)
# Set the Force-length FL relationship in the MSK model
AFO_FLrelationship=AFO9_MeshMechanics.MeshMechanics(MBD_model_AFO, theta_0_values, n_elements) # The force length relationship calculated from AFO9_MeshMechanics
# Change the force-length relationship in the MSK model to the one obtained from the AFO9_MeshMechanics
with open (MBD_model_AFO, "r+", encoding="utf-8") as f:
lines=f.read()
f.seek(0)
for i in range (len(AFO_FLrelationship)):
FL_x_str=' '.join(str(i) for i in AFO_FLrelationship[i][0])
lines=lines.replace('<x> '+ str(i+1)+' </x>', '<x> '+str(FL_x_str)+' </x>') # No space between <x> and str(i+1)
lines=lines.replace('<x>'+ str(i+1)+' </x>', '<x>'+str(FL_x_str)+' </x>') # No space between <x> and str(i+1) in left
lines=lines.replace('<x> '+ str(i+1)+'</x>', '<x> '+str(FL_x_str)+'</x>') # No space between <x> and str(i+1) in right
lines=lines.replace('<x>'+ str(i+1)+'</x>', '<x>'+str(FL_x_str)+'</x>') # No space between <x> and str(i+1) in both sides
FL_y_str=' '.join(str(i) for i in AFO_FLrelationship[i][1])
lines=lines.replace('<y> '+ str(i+1)+' </y>', '<y> '+str(FL_y_str)+' </y>')
lines=lines.replace('<y>'+ str(i+1)+' </y>', '<y>'+str(FL_y_str)+' </y>')
lines=lines.replace('<y> '+ str(i+1)+'</y>', '<y> '+str(FL_y_str)+'</y>')
lines=lines.replace('<y>'+ str(i+1)+'</y>', '<y>'+str(FL_y_str)+'</y>')
f.write(lines)
#