-
Notifications
You must be signed in to change notification settings - Fork 311
/
MuscleFixedWidthPennationModel.cpp
356 lines (321 loc) · 15.4 KB
/
MuscleFixedWidthPennationModel.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -------------------------------------------------------------------------- *
* OpenSim: MuscleFixedWidthPennationModel.cpp *
* -------------------------------------------------------------------------- *
* The OpenSim API is a toolkit for musculoskeletal modeling and simulation. *
* See http://opensim.stanford.edu and the NOTICE file for more information. *
* OpenSim is developed at Stanford University and supported by the US *
* National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA *
* through the Warrior Web program. *
* *
* Copyright (c) 2005-2017 Stanford University and the Authors *
* Author(s): Matthew Millard *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* -------------------------------------------------------------------------- */
#include "MuscleFixedWidthPennationModel.h"
using namespace OpenSim;
using namespace SimTK;
using namespace std;
void MuscleFixedWidthPennationModel::setNull()
{
setAuthors("Matthew Millard");
m_parallelogramHeight = SimTK::NaN;
m_maximumSinPennation = SimTK::NaN;
m_minimumFiberLength = SimTK::NaN;
m_minimumFiberLengthAlongTendon = SimTK::NaN;
}
void MuscleFixedWidthPennationModel::constructProperties()
{
constructProperty_optimal_fiber_length(1.0);
constructProperty_pennation_angle_at_optimal(0.0);
constructProperty_maximum_pennation_angle(acos(0.1));
}
MuscleFixedWidthPennationModel::MuscleFixedWidthPennationModel()
{
setNull();
constructProperties();
}
MuscleFixedWidthPennationModel::
MuscleFixedWidthPennationModel(double optimalFiberLength,
double optimalPennationAngle,
double maximumPennationAngle)
{
setNull();
constructProperties();
set_optimal_fiber_length(optimalFiberLength);
set_pennation_angle_at_optimal(optimalPennationAngle);
set_maximum_pennation_angle(maximumPennationAngle);
}
double MuscleFixedWidthPennationModel::getParallelogramHeight() const
{ return m_parallelogramHeight; }
double MuscleFixedWidthPennationModel::getMinimumFiberLength() const
{ return m_minimumFiberLength; }
double MuscleFixedWidthPennationModel::getMinimumFiberLengthAlongTendon() const
{ return m_minimumFiberLengthAlongTendon; }
double MuscleFixedWidthPennationModel::
clampFiberLength(double fiberLength) const
{
return max(m_minimumFiberLength, fiberLength);
}
//==============================================================================
// COMPONENT INTERFACE
//==============================================================================
void MuscleFixedWidthPennationModel::extendFinalizeFromProperties()
{
Super::extendFinalizeFromProperties();
std::string errorLocation = getName() +
" MuscleFixedWidthPennationModel::extendFinalizeFromProperties";
// Ensure property values are within appropriate ranges.
OPENSIM_THROW_IF_FRMOBJ(
get_optimal_fiber_length() <= 0,
InvalidPropertyValue,
getProperty_optimal_fiber_length().getName(),
"Optimal fiber length must be greater than zero");
OPENSIM_THROW_IF_FRMOBJ(
get_pennation_angle_at_optimal() < 0 ||
get_pennation_angle_at_optimal() > SimTK::Pi/2.0-SimTK::SignificantReal,
InvalidPropertyValue,
getProperty_pennation_angle_at_optimal().getName(),
"Pennation angle at optimal fiber length must be in the range [0, Pi/2)");
OPENSIM_THROW_IF_FRMOBJ(
get_maximum_pennation_angle() < 0 ||
get_maximum_pennation_angle() > SimTK::Pi/2.0,
InvalidPropertyValue,
getProperty_maximum_pennation_angle().getName(),
"Maximum pennation angle must be in the range [0, Pi/2]");
// Compute quantities that are used often.
m_parallelogramHeight = get_optimal_fiber_length()
* sin(get_pennation_angle_at_optimal());
m_maximumSinPennation = sin(get_maximum_pennation_angle());
// Compute the minimum fiber length:
// - pennated: the length of the fiber when at its maximum pennation angle
// - straight: 1% of the optimal fiber length
if(get_maximum_pennation_angle() > SimTK::SignificantReal) {
m_minimumFiberLength = m_parallelogramHeight / m_maximumSinPennation;
} else {
m_minimumFiberLength = get_optimal_fiber_length()*0.01;
}
m_minimumFiberLengthAlongTendon = m_minimumFiberLength
* cos(get_maximum_pennation_angle());
}
//==============================================================================
// Position-level kinematics
//==============================================================================
double MuscleFixedWidthPennationModel::
calcPennationAngle(double fiberLength) const
{
double phi = 0;
double optimalPennationAngle = get_pennation_angle_at_optimal();
// This computation is only worth performing on pennated muscles.
if(optimalPennationAngle > SimTK::Eps) {
if(fiberLength > m_minimumFiberLength) {
double sin_phi = m_parallelogramHeight/fiberLength;
phi = (sin_phi < m_maximumSinPennation) ?
asin(sin_phi) : get_maximum_pennation_angle();
} else {
phi = get_maximum_pennation_angle();
}
}
return phi;
}
double MuscleFixedWidthPennationModel::calcTendonLength(
double cosPennationAngle, double fiberLength, double muscleLength) const
{
return muscleLength - fiberLength*cosPennationAngle;
}
double MuscleFixedWidthPennationModel::calcFiberLengthAlongTendon(
double fiberLength, double cosPennationAngle) const
{
return fiberLength*cosPennationAngle;
}
//==============================================================================
// Velocity-level kinematics
//==============================================================================
double MuscleFixedWidthPennationModel::calcPennationAngularVelocity(
double tanPennationAngle, double fiberLength, double fiberVelocity) const
{
double dphi = 0;
double optimalPennationAngle = get_pennation_angle_at_optimal();
if(optimalPennationAngle > SimTK::Eps) {
SimTK_ERRCHK_ALWAYS(fiberLength > 0,
"MuscleFixedWidthPennationModel::calcPennationAngularVelocity",
"Fiber length cannot be zero.");
dphi = -(fiberVelocity/fiberLength) * tanPennationAngle;
}
return dphi;
}
double MuscleFixedWidthPennationModel::
calcTendonVelocity(double cosPennationAngle,
double sinPennationAngle,
double pennationAngularVelocity,
double fiberLength,
double fiberVelocity,
double muscleVelocity) const
{
return muscleVelocity - fiberVelocity*cosPennationAngle
+ fiberLength*sinPennationAngle*pennationAngularVelocity;
}
double MuscleFixedWidthPennationModel::
calcFiberVelocityAlongTendon(double fiberLength,
double fiberVelocity,
double sinPennationAngle,
double cosPennationAngle,
double pennationAngularVelocity) const
{
return fiberVelocity*cosPennationAngle
- fiberLength*sinPennationAngle*pennationAngularVelocity;
}
//==============================================================================
// Acceleration-level kinematics
//==============================================================================
double MuscleFixedWidthPennationModel::
calcPennationAngularAcceleration(double fiberLength,
double fiberVelocity,
double fiberAcceleration,
double sinPennationAngle,
double cosPennationAngle,
double pennationAngularVelocity) const
{
SimTK_ERRCHK_ALWAYS(fiberLength > 0,
"MuscleFixedWidthPennationModel::calcPennationAngularAcceleration",
"Fiber length cannot be zero.");
SimTK_ERRCHK_ALWAYS(cosPennationAngle > 0,
"MuscleFixedWidthPennationModel::calcPennationAngularAcceleration",
"cosPennationAngle cannot be zero.");
double numer = sinPennationAngle*cosPennationAngle*
(fiberVelocity*fiberVelocity - fiberLength*fiberAcceleration)
- fiberLength*fiberVelocity*pennationAngularVelocity;
return numer / (fiberLength*fiberLength
* cosPennationAngle*cosPennationAngle);
}
double MuscleFixedWidthPennationModel::
calcFiberAccelerationAlongTendon(double fiberLength,
double fiberVelocity,
double fiberAcceleration,
double sinPennationAngle,
double cosPennationAngle,
double pennationAngularVelocity,
double pennationAngularAcceleration) const
{
return fiberAcceleration*cosPennationAngle
- fiberLength*sinPennationAngle*pennationAngularAcceleration
- pennationAngularVelocity*
(2.0*fiberVelocity*sinPennationAngle
+ fiberLength*cosPennationAngle*pennationAngularVelocity);
}
//==============================================================================
// Partial derivatives with respect to fiber length
//==============================================================================
double MuscleFixedWidthPennationModel::
calc_DFiberLengthAlongTendon_DfiberLength(double fiberLength,
double sinPennationAngle,
double cosPennationAngle,
double DpennationAngle_DfiberLength)
const
{
// d/dlce (lce*cos(phi)) = cos(phi) - lce*sin(phi)*dphi_dlce
return cosPennationAngle
- fiberLength*sinPennationAngle*DpennationAngle_DfiberLength;
}
double MuscleFixedWidthPennationModel::
calc_DPennationAngularVelocity_DfiberLength(double fiberLength,
double fiberVelocity,
double sinPennationAngle,
double cosPennationAngle,
double pennationAngularVelocity,
double DpennationAngle_DfiberLength)
const
{
SimTK_ERRCHK_ALWAYS(cosPennationAngle > SimTK::Eps,
"MuscleFixedWidthPennationModel::"
"calc_DFiberVelocityAlongTendon_DfiberLength",
"Pennation angle must be less than 90 degrees");
SimTK_ERRCHK_ALWAYS(fiberLength > SimTK::Eps,
"MuscleFixedWidthPennationModel::"
"calc_DFiberVelocityAlongTendon_DfiberLength",
"Fiber length is close to 0.");
// dphidt = -(fiberVelocity/fiberLength)*tanPennationAngle
double tanPhi = sinPennationAngle/cosPennationAngle;
double DtanPhi_Dlce = (1+tanPhi*tanPhi)*DpennationAngle_DfiberLength;
return (fiberVelocity/(fiberLength*fiberLength))*tanPhi
- (fiberVelocity/fiberLength)*DtanPhi_Dlce;
}
double MuscleFixedWidthPennationModel::
calc_DFiberVelocityAlongTendon_DfiberLength(
double fiberLength,
double fiberVelocity,
double sinPennationAngle,
double cosPennationAngle,
double pennationAngularVelocity,
double DpennationAngle_DfiberLength,
double DpennationAngularVelocity_DfiberLength)
const
{
// dlceAT = dlce*cos(phi) - lce*sin(phi)*dphidt
// Now take the partial derivative of dlceAT with respect to lce
return fiberVelocity*(-sinPennationAngle*DpennationAngle_DfiberLength)
- sinPennationAngle*pennationAngularVelocity
- fiberLength*cosPennationAngle
*DpennationAngle_DfiberLength*pennationAngularVelocity
- fiberLength*sinPennationAngle
*DpennationAngularVelocity_DfiberLength;
}
double MuscleFixedWidthPennationModel::
calc_DPennationAngle_DfiberLength(double fiberLength) const
{
SimTK_ERRCHK_ALWAYS(fiberLength > m_parallelogramHeight,
"MuscleFixedWidthPennationModel::calc_DPennationAngle_DfiberLength",
"Fiber length is below the lower bound for this muscle.");
// phi = asin( parallelogramHeight/lce)
// d_phi/d_lce = d/dlce (asin(parallelogramHeight/lce))
double h_over_l = m_parallelogramHeight / fiberLength;
return (-h_over_l/fiberLength) / sqrt(1.0 - h_over_l*h_over_l);
}
double MuscleFixedWidthPennationModel::
calc_DTendonLength_DfiberLength(double fiberLength,
double sinPennationAngle,
double cosPennationAngle,
double DpennationAngle_DfiberLength) const
{
SimTK_ERRCHK_ALWAYS(fiberLength > m_parallelogramHeight,
"MuscleFixedWidthPennationModel::calc_DTendonLength_DfiberLength",
"Fiber length is below the lower bound for this muscle.");
// dtl_dlce = DmuscleLength_DfiberLength - cosPennationAngle
// + fiberLength*sinPennationAngle*DpennationAngle_DfiberLength
return fiberLength*sinPennationAngle*DpennationAngle_DfiberLength
- cosPennationAngle;
}
//==============================================================================
// Kinematic fiber pose equations (useful during initialization)
//==============================================================================
double MuscleFixedWidthPennationModel::calcFiberLength(double muscleLength,
double tendonLength) const
{
double fiberLengthAT = muscleLength-tendonLength;
double fiberLength = 0.0;
if(fiberLengthAT >= getMinimumFiberLengthAlongTendon()) {
fiberLength = sqrt(m_parallelogramHeight*m_parallelogramHeight
+ fiberLengthAT*fiberLengthAT);
} else {
fiberLength = getMinimumFiberLength();
}
return fiberLength;
}
double MuscleFixedWidthPennationModel::
calcFiberVelocity(double cosPennationAngle,
double muscleVelocity,
double tendonVelocity) const
{
// Differentiate l^M cos(phi) + l^T = l^MT ....(1)
// Differentiate h = l^M sin(phi) .............(2)
// Solve (2) for phidot, substitute into (1), and solve for v^M.
return (muscleVelocity-tendonVelocity)*cosPennationAngle;
}