This file is indexed.

/usr/share/octave/site/m/sundialsTB/idas/examples_ser/midasSlCrank_dns.m is in octave-sundials 2.5.0-3+b1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
function midasSlCrank_dns
%midasSlCrank_dns  Slider-crank example
% The multibody system consists of two bodies (crank and 
% connecting rod) with a translational-spring-damper (TSDA)
% and a constant force acting on the connecting rod. 
%
% The system has a single degree of freedom. It is modeled with 3
% generalized coordinates (crank angle, horizontal position of the
% translational joint, and angle of the connecting rod) and 
% therefore has 2 constraints.
%
% Example 6.1.8, pp. 271 in 
% Ed. Haug - Intermediate Dynamics, Prentiss Hall, 1992
%
% For its solution with IDAS, the resulting index-3 DAE is reformulated
% as a stabilized index-2 DAE (Gear-Gupta-Leimkhuler formulation) by 
% introducing 2 additional Lagrange multipliers and appending the
% velocity constraints.
%
%  |                                  |
%  |       /\                         |       /\
%  |      /  \                        |      /  \
%  | a/2 /    \ 1/2                   |     /    \
%  |    /      \                      |    /      \
%  |   /--TSDA--\                     |   /        \
%  |  /          \                    |  /          \
%  | / a/2        \ 1/2               | /            \---
%  |/              \                  |/  \ q1   q3 / \  \
%  +-----------------------------     +----------------------------
%                    \                |             \  |\
%                     \               |               -|-\
%                      \ 1            |                |  \
%                       \             |<----- q2 ----->|   \
%                        \                                  \
%                         \                                  \
%                          \ --> F                            \
%
% The local reference frame on the crank is positioned at the
% revolute joint on the ground. The crank has length a, mass m1, and
% intertia (with respect to the local frame) J1.
% The local reference frame on the conncting rod is positioned at the
% translational joint. The connecting rod has length 2, mass m2, and
% inertia J2.
% The TSDA has spring constant k, damping constant c, and free length l0.
% A constant horizontal force F acts on the connecting rod.

% Radu Serban <radu@llnl.gov>
% Copyright (c) 2007, The Regents of the University of California.
% $Revision: 1.2 $Date: 2009/04/26 23:27:29 $

% Problem data
data.a = 0.5;
data.J1 = 1.0;
data.m2 = 1.0;
data.J2 = 2.0;
data.k = 1.0;
data.c = 1.0;
data.l0 = 1.0;
data.F = 1.0;

% Integration limits
t0 = 0.0;
tf = 10.0;

% Specify algebraic variables
id = ones(10,1);
id(7:10) = 0.0;

% Integration options
options = IDASetOptions('UserData',data,...
                        'RelTol',1.e-6,...
                        'AbsTol',1.e-6,...
                        'VariableTypes',id,...
                        'suppressAlgVars','on',...
                        'MaxNumSteps', 1000,...
                        'LinearSolver','Dense');
% Set consistent IC
[yy0, yp0] = setIC(data);

% Initialize IDAS
IDAInit(@scRes,t0,yy0,yp0,options);

% Store inital time and IC
it = 1;
time(it) = t0;
pos(it,:) = yy0(1:3)';
vel(it,:) = yy0(4:6)';
acc(it,:) = yp0(4:6)';
lam(it,:) = yy0(7:8)';

% Compute constraint joint forces at initial time
Fc(it,:) = joint_forces(yy0, yp0, data);

% Call solver in ONE_STEP mode
t = t0;
while t < tf
  [status,t,yy] = IDASolve(tf,'OneStep');
  it = it+1;
  time(it) = t;
  pos(it,:) = yy(1:3)';
  vel(it,:) = yy(4:6)';
  lam(it,:) = yy(7:8)';
  yp = IDAGet('DerivSolution',t,1);
  acc(it,:) = yp(4:6)';
  Fc(it,:) = joint_forces(yy, yp, data);
end

fprintf('Solver stats:\n');
disp(IDAGetStats);

% Plot solution
figure;
set(gcf,'position',[475 250 1000 800]);

subplot(2,2,1)
hold on
plot(time,pos(:,1),'b');
plot(time,pos(:,2),'r');
plot(time,pos(:,3),'g');
box on
set(gca,'XLim',[t0 tf])
title('position');
legend('q','x','p');

subplot(2,2,2)
hold on
plot(time,vel(:,1),'b');
plot(time,vel(:,2),'r');
plot(time,vel(:,3),'g');
box on
set(gca,'XLim',[t0 tf])
title('velocity');
legend('q''', 'x''', 'p''');

subplot(2,2,3)
hold on
plot(time,acc(:,1),'b');
plot(time,acc(:,2),'r');
plot(time,acc(:,3),'g');
box on
set(gca,'XLim',[t0 tf])
title('acceleration');
legend('q''''', 'x''''', 'p''''');


subplot(2,2,4)
hold on
plot(time,lam(:,1),'b');
plot(time,lam(:,2),'r');
box on
set(gca,'XLim',[t0 tf])
title('Lagrange multipliers (cnstr. forces)');
legend('\lambda_1', '\lambda_2');


% Plot joint forces
figure;
set(gcf,'position',[275 150 800 800]);

plot(time,Fc);
set(gca,'XLim',[t0 tf])
title('joint forces');
legend('vrt. force in rev. crank-ground',...
       'hrz. force in rev. crank-ground',...
       'vrt. force in rev. crank-rod',...
       'hrz. force in rev. crank-rod',...
       'vrt. force in transl.',...
       'torque in transl.');


IDAFree;

% ====================================================================================
% Consistent IC
% ====================================================================================

function [yy, yp] = setIC(data)

a = data.a;
J1 = data.J1;
m2 = data.m2;
J2 = data.J2;

q = pi/2.0;    
p = -asin(a*sin(q));
x = cos(p) + a*cos(q);

yy = zeros(10,1);
yp = zeros(10,1);

yy(1) = q;  % crank angle 
yy(2) = x;  % slider position
yy(3) = p;  % conecting rod angle

Q = appl_forces(yy, data);
g = gamma(yy,data);
G = jac(yy,data);

% Extended mass matrix in index-1 formulation

MM = zeros(5,5);
MM(1,1) = J1;
MM(2,2) = m2;
MM(3,3) = J2;
MM(4:5,1:3) = G;
MM(1:3,4:5) = G';

% Right-hand side in index-1 formulation

b = [Q;g];

% Solution of MM*x = b

acc = MM^(-1)*b;

yp(4) = acc(1);
yp(5) = acc(2);
yp(6) = acc(3);
%
yy(7) = acc(4);
yy(8) = acc(5);

return

% ====================================================================================
% Constraint Jacobian
% ====================================================================================

function G = jac(yy,data)

a = data.a;

q = yy(1);  % crank angle 
x = yy(2);  % slider position
p = yy(3);  % conecting rod angle

qd = yy(4); % crank angular velocity
xd = yy(5); % slider horizontal velocity
pd = yy(6); % conecting rod angular velocity

s1 = sin(q);
c1 = cos(q);
s2 = sin(p);
c2 = cos(p);

G(1,1) = a*s1;
G(1,2) = 1.0;
G(1,3) = s2;

G(2,1) = -a*c1;
G(2,2) = 0.0;
G(2,3) = -c2;

return

% ====================================================================================
% Right-hand side of acceleration constraint
% ====================================================================================

function g = gamma(yy, data)

a = data.a;

q = yy(1);  % crank angle 
x = yy(2);  % slider position
p = yy(3);  % conecting rod angle

qd = yy(4); % crank angular velocity
xd = yy(5); % slider horizontal velocity
pd = yy(6); % conecting rod angular velocity

s1 = sin(q);
c1 = cos(q);
s2 = sin(p);
c2 = cos(p);

g(1,1) = - (a*qd^2*c1+pd^2*c2);
g(2,1) = - (a*qd^2*s1 + pd^2*s2);

return

% ====================================================================================
% Generalized applied forces calculation
% ====================================================================================

function Q = appl_forces(yy, data)

a = data.a;
k = data.k;
c = data.c;
l0 = data.l0;
F = data.F;

q = yy(1);  % crank angle 
x = yy(2);  % slider position
p = yy(3);  % conecting rod angle

qd = yy(4); % crank angular velocity
xd = yy(5); % slider horizontal velocity
pd = yy(6); % conecting rod angular velocity

s1 = sin(q);
c1 = cos(q);
s2 = sin(p);
c2 = cos(p);
s21 = s2*c1 - c2*s1;
c21 = c2*c1 + s2*s1;

l2 = x^2 - x*(c2+a*c1) + (1.0 + a^2)/4.0 + a*c21/2.0;
l = sqrt(l2);
ld = 2.0*x*xd - xd*(c2+a*c1) + x*(s2*pd+a*s1*qd) - a*s21*(pd-qd)/2.0;
ld = ld / (2.0*l);

f = k*(l-l0) + c*ld;
fl = f/l;

Q(1,1) = - fl * a * (s21/2.0 + x*s1) / 2.0;
Q(2,1) = fl * (c2/2.0 - x + a*c1/2.0) + F;
Q(3,1) = - fl * (x*s2 - a*s21/2.0) / 2.0 - F*s2;

return

% ====================================================================================
% Residual function
% ====================================================================================

function [res, flag, new_data] = scRes(t,yy,yp,data)

a = data.a;
J1 = data.J1;
m2 = data.m2;
J2 = data.J2;

q = yy(1);  % crank angle 
x = yy(2);  % slider position
p = yy(3);  % conecting rod angle

qd = yy(4); % crank angular velocity
xd = yy(5); % slider horizontal velocity
pd = yy(6); % conecting rod angular velocity

lam1 = yy(7); % Lagrange multiplier (cnstr)
lam2 = yy(8); % Lagrange multiplier (cnstr)

mu1 = yy(9);  % Lagrange multiplier (GGL)
mu2 = yy(10); % Lagrange multiplier (GGL)

s1 = sin(q);
c1 = cos(q);
s2 = sin(p);
c2 = cos(p);

% Generalized forces
Q = appl_forces(yy, data);

% Velocities (GGL modified)
res(1) = yp(1) - qd + a*s1*mu1 - a*c1*mu2;
res(2) = yp(2) - xd + mu1;
res(3) = yp(3) - pd + s2*mu1 - c2*mu2; 

% Dynamical equations
res(4) = J1*yp(4) - Q(1) + a*s1*lam1 - a*c1*lam2;
res(5) = m2*yp(5) - Q(2) + lam1;
res(6) = J2*yp(6) - Q(3) + s2*lam1 - c2*lam2; 

% Position constraints
res(7) = x - c2 - a*c1;
res(8) = -s2 - a*s1;

% Velocity constraints
res(9) = a*s1*qd + xd + s2*pd;
res(10) = -a*c1*qd - c2*pd;

flag = 0;
new_data = [];

return

% ====================================================================================
% Joint constraint forces
% ====================================================================================

function Fc = joint_forces(yy, yp, data)
% Compute joint reaction forces for given positins, velocities, and
% accelerations. This is done by including the reaction forces and torques,
% considering the free body diagrams for the crank and connecting rod, and
% writing the dynamical equilibrium equations.
%

a = data.a;
k = data.k;
c = data.c;
l0 = data.l0;
F = data.F;

J1 = data.J1;
m2 = data.m2;
J2 = data.J2;

q = yy(1);  % crank angle 
x = yy(2);  % slider position
p = yy(3);  % conecting rod angle

qd = yy(4); % crank angular velocity
xd = yy(5); % slider horizontal velocity
pd = yy(6); % conecting rod angular velocity

qdd = yp(4); % crank angular acc.
xdd = yp(5); % slider horizontal acc.
pdd = yp(6); % connecting rod angular acc.

s1 = sin(q);
c1 = cos(q);
s2 = sin(p);
c2 = cos(p);
s21 = s2*c1 - c2*s1;
c21 = c2*c1 + s2*s1;

l2 = x^2 - x*(c2+a*c1) + (1.0 + a^2)/4.0 + a*c21/2.0;
l = sqrt(l2);
ld = 2.0*x*xd - xd*(c2+a*c1) + x*(s2*pd+a*s1*qd) - a*s21*(pd-qd)/2.0;
ld = ld / (2.0*l);

f = k*(l-l0) + c*ld;
fl = f/l;

% TSDA forces acting on crank and connecting rod

Q1A(1) = x-0.5*c2-a/2*c1;
Q1A(2) = -0.5*(s2+a*s1);
Q1A(3) = -0.5*(a*x*s1+s21);

Q2A(1) = -Q1A(1);
Q2A(2) = -Q1A(2);
Q2A(3) = -0.5*(x*s2+a*s21);

Q1A = fl*Q1A';
Q2A = fl*Q2A';

QA = [-Q1A;-Q2A];

% Force F acting on connecting rod
FA = [0;0;0;F;0;-F*s2];

% Dynamic forces
MA = [0;0;J1*qdd;m2*xdd;0;J2*pdd];

% Dynamic equilibrium equations:
%  MA = QA+FA + FC
% where the joint constraint forces are:
%   Fc(1) = vertical force in revolute join crank-ground
%   Fc(2) = horizontal force in revolute join crank-ground
%   Fc(3) = vertical force in revolute joint crank-connecting rod
%   Fc(4) = vertical force in revolute joint crank-connecting rod
%   Fc(5) = vertical force in translational joint
%   Fc(6) = reaction torque acting in translational joint
% and therefore
%   FC = A * Fc
% where
%   A = [
%       0  1   0     1    0  0
%       1  0   1     0    0  0
%       0  0  a*c1 -1*s1  0  0
%       0  0   0    -1    0  0
%       0  0  -1     0    1  0
%       0  0  -c2   -s2   0  1
%       ];

b = MA-QA-FA;

Fc(4) = -b(4);
Fc(2) = b(1) - Fc(4);
if abs(c1) > 0.1
  Fc(3) = ( b(3) + a*s1*Fc(4) ) / (a*c1);
end
Fc(1) = b(2) - Fc(3); 
Fc(5) = b(5) + Fc(3);
Fc(6) = b(6) + c2*Fc(3) + s2*Fc(4);

return