-
Notifications
You must be signed in to change notification settings - Fork 3
/
PH_LinearSystem.m
1570 lines (1395 loc) · 65.7 KB
/
PH_LinearSystem.m
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef PH_LinearSystem < PH_System
%LINEARPHSYSTEM Linear port-Hamiltonian System in input-state-output
%form
% Detailed explanation goes here
properties(SetAccess = protected, GetAccess = public)
J % Structure matrix J
R % Resistance matrix R (dissipation)
Q % Energy matrix Q
G % Input matrix symmetric part G
P % Input matrix skew symmetric part P
M % Feedthrough matrix skew symmetric part M
S % Feedthrough matrix symmetric part S (dissipation)
B % Algebraic constraints on efforts / Lagrange multiplier input mapping
C % Non-collocated output mapping
C_e % Matrix for effort coupling
C_f % Matrix for flow coupling
C_u % Matrix for I/O coupling
C_y % Matrix for I/O coupling
x_p % State variables associated with the generalized momenta
x_q % State variables associated with the generalized displacements
end
methods(Access = public)
% PH_LinearSystem(n, J, Q, G, R, P, S, M, B, x_p, x_q, C)
function obj = PH_LinearSystem(n, J, Q, varargin)
% Sanity checks
obj = obj@PH_System('Linear port-Hamiltonian system');
% Strict type checking
if ~isscalar(n) || n < 0
error('n must be >= 0');
end
obj.n = n;
if ~ismatrix(J) || any(size(J) ~= n) || ~issymmetric(round(J, 12), 'skew')
error('J must be a skew-symmetric nxn matrix');
end
obj.J = J;
if ~ismatrix(Q) || any(size(Q) ~= n)
error('Q must be a nxn matrix');
end
obj.Q = Q;
% Inputs and Outputs
obj.n_u = 0;
obj.G = zeros(obj.n, 0);
if nargin > 3 && ~isempty(varargin{1})
G = varargin{1};
if ~ismatrix(G) || size(G, 1) ~= n
error('G must be a matrix with n rows');
end
obj.n_u = size(G, 2);
obj.G = G;
end
% Dissipation
obj.R = zeros(n);
if nargin > 4
R = varargin{2};
if ~isempty(R)
if ~ismatrix(R) || any(size(R) ~= n) || ~issymmetric(round(R, 12)) || any(round(eig(R),17) < 0)
error('R must be a symmetric positive semidefinite nxn matrix');
end
obj.R = R;
end
end
% System with direct input feedthrough
obj.P = zeros(obj.n, obj.n_u);
obj.M = zeros(obj.n_u);
obj.S = zeros(obj.n_u);
if nargin > 7
P = varargin{3};
S = varargin{4};
M = varargin{5};
if ~isempty(P) && ~isempty(S) && ~isempty(M)
if ~ismatrix(P) || size(P, 1) ~= obj.n || size(P, 2) ~= obj.n_u
error('P must be a n x n_u matrix');
end
if ~ismatrix(S) || size(S, 1) ~= obj.n_u || size(S, 2) ~= obj.n_u || ~issymmetric(S)
error('S must be a symmetric n_u x n_u matrix');
end
if ~ismatrix(M) || size(M, 1) ~= obj.n_u || size(M, 2) ~= obj.n_u || ~issymmetric(M, 'skew')
error('M must be a skew-symmetric n_u x n_u matrix');
end
Z = [obj.R P; P' S];
if any(round(eig(Z),17) < 0)
error('P,S and R must satisfy Z = [R P; P'' S] is positive semidefinite');
end
obj.P = P;
obj.S = S;
obj.M = M;
end
end
% Constraints
obj.C_e = zeros(0, obj.n);
obj.C_f = zeros(0, obj.n);
obj.C_u = zeros(0, obj.n_u);
obj.C_y = zeros(0, obj.n_u);
% Lagrange multiplier input matrix
obj.B = zeros(obj.n, 0);
if nargin > 8
B = varargin{6};
if ~isempty(B)
if size(B, 1) ~= obj.n
error('B must be a n x n_c matrix');
end
obj.B = B;
end
end
obj.n_c = size(obj.B, 2);
% Which states are generalized momenta and which
% generalized displacements
% We assume that the number of generalized momenta equals the
% number of generalized displacements and that the generalized
% momenta are given first
obj.x_p = [1:floor(obj.n/2)]';
obj.x_q = [floor(obj.n/2)+1:obj.n]';
if nargin > 10
x_p = varargin{7};
x_q = varargin{8};
if ~isempty(x_p) && ~isempty(x_q)
if ~isvector(x_p) || ~isvector(x_q) || length(x_p) + length(x_q) ~= n
error('x_p and x_q must have a combined length of n')
end
obj.x_p = x_p;
obj.x_q = x_q;
end
end
obj.C = zeros(0, obj.n);
if nargin > 11
C = varargin{9};
if ~isempty(C)
if size(C, 2) ~= obj.n
error('C must be a n_c x n matrix');
end
obj.C = C;
end
end
obj.n_elements = 1;
obj.elements{1} = PH_Element('Linear PH system', [], []);
end
% Check whether our System is a valid PH system
function b = isValidPHSystem(obj)
b = 1;
if ~issymmetric(round(obj.J, 10), 'skew')
disp('J is not skew-symmetric');
b = 0;
end
E = eig(obj.R);
if ~issymmetric(round(obj.R, 10)) || any(E < -1e-12 * max(abs(E)))
disp('R is not symmetric and positive semidefinite');
b = 0;
end
if ~issymmetric(obj.S)
disp('S is not a symmetric matrix');
b = 0;
end
if ~issymmetric(obj.M, 'skew')
disp('M is not a skew-symmetric matrix');
b = 0;
end
Z = [obj.R obj.P; obj.P' obj.S];
E = eig(Z);
if any(E < -1e-12*max(abs(E)))
disp('Z = [R P; P'' S] is not positive semidefinite');
b = 0;
end
end
% Add another linear PH system
function add(obj, system)
if ~isa(system, 'PH_LinearSystem') || ~system.isValidPHSystem()
error('system must be a valid ''PH_LinearSystem''');
end
states = ones(system.n, 1);
if (sum(states(system.x_p)) + sum(states(system.x_q))) ~= system.n
error('system must be separable into two energy domains');
end
N_p = length(system.x_p);
N = obj.n;
N_q = length(system.x_q);
obj.x_p = [system.x_p; obj.x_p + N_p];
obj.x_q = [obj.x_q + N_p; system.x_q + N];
obj.J = [system.J(system.x_p,system.x_p) zeros(N_p, N) system.J(system.x_p, system.x_q);
zeros(N, N_p) obj.J zeros(N, N_q);
system.J(system.x_q, system.x_p) zeros(N_q, N) system.J(system.x_q, system.x_q)];
obj.Q = [system.Q(system.x_p,system.x_p) zeros(N_p, N) system.Q(system.x_p, system.x_q);
zeros(N, N_p) obj.Q zeros(N, N_q);
system.Q(system.x_q, system.x_p) zeros(N_q, N) system.Q(system.x_q, system.x_q)];
obj.G = [zeros(N_p, obj.n_u), system.G(system.x_p,:);
obj.G, zeros(N, system.n_u);
zeros(N_q, obj.n_u) system.G(system.x_q, :)];
obj.P = [zeros(N_p, obj.n_u), system.P(system.x_p,:);
obj.P, zeros(N, system.n_u);
zeros(N_q, obj.n_u) system.P(system.x_q, :)];
obj.B = [zeros(N_p, size(obj.B,2)), system.B(system.x_p,:);
obj.B, zeros(N, size(system.B, 2));
zeros(N_q, size(obj.B, 2)) system.B(system.x_q, :)];
obj.M = blkdiag(obj.M, system.M);
obj.S = blkdiag(obj.S, system.S);
if ~isempty(system.C)
obj.C = [zeros(size(obj.C, 1), N_p), obj.C, zeros(size(obj.C, 1), N_q);
system.C(:,system.x_p), zeros(size(system.C, 1), obj.n), system.C(:, system.x_q)];
else
obj.C = [zeros(size(obj.C, 1), N_p), obj.C, zeros(size(obj.C, 1), N_q)];
end
if ~isempty(system.R)
obj.R = [system.R(system.x_p,system.x_p) zeros(N_p, N) system.R(system.x_p, system.x_q);
zeros(N, N_p) obj.R zeros(N, N_q);
system.R(system.x_q, system.x_p) zeros(N_q, N) system.R(system.x_q, system.x_q)];
else
obj.R = [zeros(N_p, N_p+N_q+N);
zeros(N, N_p) obj.R zeros(N, N_q);
zeros(N_q, N_p+N_q+N)];
end
obj.C_e = [system.C_e(:,system.x_p) zeros(size(system.C_e, 1), N) system.C_e(:, system.x_q);
zeros(size(obj.C_e, 1), N_p) obj.C_e zeros(size(obj.C_e, 1), N_q)];
obj.C_f = [system.C_f(:,system.x_p) zeros(size(system.C_f, 1), N) system.C_f(:, system.x_q);
zeros(size(obj.C_f, 1), N_p) obj.C_f zeros(size(obj.C_f, 1), N_q)];
obj.C_u = [obj.C_u, zeros(size(obj.C_u, 1), system.n_u); ...
zeros(size(system.C_u, 1), obj.n_u) system.C_u];
obj.C_y = [obj.C_y, zeros(size(obj.C_y, 1), system.n_u); ...
zeros(size(system.C_y, 1), obj.n_u) system.C_y];
obj.n_c = size(obj.C_e, 1) + size(obj.B, 2);
for p = 1:system.n_ports
obj.ports{end+1} = copy(system.ports{p});
obj.ports{end}.nodes = obj.ports{end}.nodes + obj.n_nodes;
if isa(obj.ports{end}, 'PH_Port_external')
obj.ports{end}.IOPair = obj.ports{end}.IOPair + obj.n_u;
end
if isa(obj.ports{end}, 'PH_Port_boundary')
obj.ports{end}.IOPair = obj.ports{end}.IOPair + obj.n_u;
obj.ports{end}.element = obj.ports{end}.element + obj.n_elements;
end
end
for n = 1:system.n_nodes
obj.nodes{end+1} = copy(system.nodes{n});
obj.nodes{end}.ports = obj.nodes{end}.ports + obj.n_ports;
obj.nodes{end}.elements = obj.nodes{end}.elements + obj.n_elements;
end
for e = 1:system.n_elements
obj.elements{end+1} = copy(system.elements{e});
obj.elements{end}.ports = obj.elements{end}.ports + obj.n_ports;
obj.elements{end}.nodes = obj.elements{end}.nodes + obj.n_nodes;
end
obj.n_ports = obj.n_ports + system.n_ports;
obj.n_nodes = obj.n_nodes + system.n_nodes;
obj.n_elements = obj.n_elements + system.n_elements;
obj.n = obj.n + system.n;
obj.n_u = obj.n_u + system.n_u;
% Look for identical nodes and delete duplicates
duplicateNodes = zeros(0,2);
for n = 1:obj.n_nodes
current = obj.nodes{n};
if ~isa(current, 'PH_MechanicalNode') || any(any(duplicateNodes == n))
continue;
else
for i=1:obj.n_nodes
if ~(i == n) && isa(obj.nodes{i}, 'PH_MechanicalNode')
if sum(abs(obj.nodes{i}.location - current.location)) < 1e-14
duplicateNodes(end+1, 1) = n;
duplicateNodes(end, 2) = i;
break;
end
end
end
end
end
if ~isempty(duplicateNodes)
for n = 1:size(duplicateNodes, 1)
current = obj.nodes{duplicateNodes(n, 1)};
current.ports = [current.ports obj.nodes{duplicateNodes(n, 2)}.ports];
current.elements = [current.elements obj.nodes{duplicateNodes(n, 2)}.elements];
current.lockedDOFs = double(current.lockedDOFs | obj.nodes{duplicateNodes(n, 2)}.lockedDOFs);
for p = 1:length(current.ports)
for pn = 1:length(obj.ports{current.ports(p)}.nodes)
if(obj.ports{current.ports(p)}.nodes(pn) == duplicateNodes(n, 2))
obj.ports{current.ports(p)}.nodes(pn) = duplicateNodes(n, 1);
end
end
end
for e = 1:length(current.elements)
for en = 1:length(obj.elements{current.elements(e)}.nodes)
if(obj.elements{current.elements(e)}.nodes(en) == duplicateNodes(n, 2))
obj.elements{current.elements(e)}.nodes(en) = duplicateNodes(n, 1);
end
end
end
obj.nodes{duplicateNodes(n, 2)}.delete();
end
for n = 1:size(duplicateNodes, 1)
for p = 1:obj.n_ports
port = obj.ports{p};
for pn = 1:length(port.nodes)
if port.nodes(pn) >= duplicateNodes(n, 2)
port.nodes(pn) = port.nodes(pn) -1;
end
end
end
for e = 1:obj.n_elements
element = obj.elements{e};
for en = 1:length(element.nodes)
if element.nodes(en) > duplicateNodes(n, 2)
element.nodes(en) = element.nodes(en) -1;
end
end
end
obj.nodes(duplicateNodes(n, 2)) = [];
obj.n_nodes = length(obj.nodes);
for i=n+1:size(duplicateNodes,1)
if duplicateNodes(i, 2) > duplicateNodes(n, 2)
duplicateNodes(i, 2) = duplicateNodes(i, 2) -1;
end
end
end
end
end
% Add Rayleigh damping to the system
% Method can be found in the PhD thesis of Flavio Luiz Cardoso-Ribeiro
% NOTE: works only if there is no direct coupling b/w energy variables
function addRayleighDamping(obj, alpha, beta)
if any(obj.x_p == obj.x_q)
error('not applicable when there is direct coupling between the energy variables');
end
Q_1 = obj.Q(obj.x_p, obj.x_p);
Q_2 = obj.Q(obj.x_q, obj.x_q);
J_12 = obj.J(obj.x_p, obj.x_q);
J_21 = obj.J(obj.x_q, obj.x_p);
D = alpha*(Q_1)^-1 - beta * J_12*Q_2*J_21;
D = D - 0.5*(D - D');
obj.R(obj.x_p, obj.x_p) = D;
end
% Get the system's first n smallest magnitude eigenvalues
% NOTE: works only if there is no direct coupling b/w energy variables
function [lambda, K] = getSmallestMagnitudeEigenvalues(obj, n)
% TODO: check the system matrices instead of the p/q indices...
if any(ismember(obj.x_p, obj.x_q))
error('not applicable when there is direct coupling between the energy variables');
end
Mi = obj.Q(obj.x_p, obj.x_p);
K = obj.Q(obj.x_q, obj.x_q);
if rank(K) < size(K, 1)
error('system is singular - eigenvalues cannot be computed');
end
opts.v0 = ones(size(K, 1),1);
opts.isreal = 1;
[~, lambda] = eigs(K, Mi\eye(size(Mi, 1)), n, 'smallestabs',opts);
lambda = diag(lambda);
end
% Get a subsystem with the given states x_p and x_q.
% Used for decentralized approaches (substructuring)
function sys = getSubsystem(obj, x_p, x_q)
sys = obj.copy();
states = [x_p; x_q];
sys.n = length(states);
sys.J = sys.J(states, states);
sys.R = sys.R(states, states);
sys.Q = sys.Q(states, states);
sys.G = sys.G(states, :);
sys.P = sys.P(states, :);
sys.C = sys.C(:, states);
% Remove inputs that do not influence the subsystem
idx_u0 = find(~any(round(sys.G-sys.P, 12)));
u_dep = sort(idx_u0, 'descend');
ports = sys.getPortsForIOPairs(u_dep);
sys.deletePorts(ports);
% Remove outputs that cannot be obtained in the subsystem
sys.C(~any(sys.C'), :) = [];
[~, idx_a] = unique(sys.G' - sys.P', 'rows', 'last');
idx_u0 = find(~ismember(1:size(sys.G,2), idx_a));
u_dep = sort(idx_u0, 'descend');
ports = sys.getPortsForIOPairs(u_dep);
sys.deletePorts(ports);
sys.B = sys.B(states, :);
sys.C_e = sys.C_e(:, states);
sys.C_f = sys.C_f(:, states);
sys.n_c = size(sys.C_u, 1) + size(sys.B, 2);
sys.x_p = [1:length(x_p)]';
sys.x_q = [length(x_p)+1:length(x_p)+length(x_q)]';
% Delete all nodes and elements that do not belong to the
% subsystem
for n = sys.n_nodes:-1:1
ports = sys.getMechanicalPorts(n);
if isempty(ports)
% Search elements that belong to this node
for e = sys.n_elements:-1:1
element = sys.elements{e};
if any(element.nodes == n)
sys.elements{e}.delete();
sys.elements(e) = [];
sys.n_elements = sys.n_elements-1;
end
end
sys.nodes{n}.delete();
sys.nodes(n) = [];
sys.n_nodes = sys.n_nodes - 1;
end
end
end
% Set all matrix entries of the system matrices to zero whose
% absolute values are below the user-defined treshhold
function cleanSystemMatrices(obj, treshhold)
obj.J(abs(obj.J) < treshhold) = 0;
obj.R(abs(obj.R) < treshhold) = 0;
obj.Q(abs(obj.Q) < treshhold) = 0;
obj.G(abs(obj.G) < treshhold) = 0;
obj.P(abs(obj.P) < treshhold) = 0;
obj.M(abs(obj.M) < treshhold) = 0;
obj.S(abs(obj.S) < treshhold) = 0;
%obj.C(abs(obj.C) < treshhold) = 0;
end
% Coupling constraints for flows, effors, inputs & outputs
function addCouplingConstraints(obj, C_e, C_f, C_u, C_y)
if ~isempty(C_e)
if ~ismatrix(C_e) || size(C_e, 2) ~= obj.n
error(['C_e must be a matrix with ', num2str(obj.n) ' columns']);
end
if ~ismatrix(C_f) || size(C_f, 2) ~= obj.n
error(['C_f must be a matrix with ', num2str(obj.n) ' columns']);
end
if ~(size(C_e, 1) == size(C_f, 1))
error('C_e and C_f must be matrices of equal size!');
end
% remove 'empty' constraints
idx_0 = ~any(C_e') & ~any(C_f');
C_e(idx_0,:) = [];
C_f(idx_0,:) = [];
obj.C_e = [obj.C_e; C_e];
obj.C_f = [obj.C_f; C_f];
obj.C_u = [obj.C_u; zeros(size(obj.C_e, 1), obj.n_u)];
obj.C_y = [obj.C_y; zeros(size(obj.C_e, 1), obj.n_u)];
end
if nargin > 4
if ~ismatrix(C_u) || size(C_u, 2) ~= obj.n_u
error(['C_u must be a matrix with ', num2str(obj.n_u) ' columns']);
end
if ~ismatrix(C_y) || size(C_y, 2) ~= obj.n_u
error(['C_y must be a matrix with ', num2str(obj.n_u) ' columns']);
end
if ~(size(C_u, 1) == size(C_y, 1))
error('C_u and C_y must be matrices of equal size!');
end
% remove 'empty' constraints
idx_0 = ~any(C_u') & ~any(C_y');
C_u(idx_0,:) = [];
C_y(idx_0,:) = [];
obj.C_u = [obj.C_u; C_u];
obj.C_y = [obj.C_y; C_y];
obj.C_e = [obj.C_e; zeros(size(obj.C_u, 1), obj.n)];
obj.C_f = [obj.C_f; zeros(size(obj.C_u, 1), obj.n)];
end
obj.n_c = size(obj.C_e,1) + size(obj.B, 2);
end
% Generate constraints of the form Cy * y + Cu * u = 0.
% Constraints are generated automatically for each node. Only the
% mechanical domain is supported so far. Hydraulic actuators are
% coupled via their mechanical ports.
function generateConstraints(obj)
% Mechanical constraint generation
[Ce_v, Cf_v, Cu_v, Cy_v] = obj.generateVelocityConstraints();
[Ce_f, Cf_f, Cu_f, Cy_f] = obj.generateForceConstraints();
% Add constraint matrices
obj.C_e = [obj.C_e; Ce_v; Ce_f];
obj.C_f = [obj.C_f; Cf_v; Cf_f];
obj.C_u = [obj.C_u; Cu_v; Cu_f];
obj.C_y = [obj.C_y; Cy_v; Cy_f];
% Adjust the number of constraints
obj.n_c = size(obj.C_e, 1) + size(obj.B, 2);
% Constraints were generated successfully. To obtain an ODE system,
% call assemble() followed by eliminateAlgebraicConstraints()
end
% Eliminates the Cu, Cy constraints by solving for u
% In the process, a set of algebraic constraints of the form B'*e = 0
% are usually generated. They can be eliminated by a call to
% eliminateAlgebraicConstraints
function assembleDAESystem(obj)
if obj.n_c == 0
% No constraints --> nothing to do
return
end
if any(any(obj.C_e)) || any(any(obj.C_f))
warning('Constraints on efforts and flows are not handled... setting to zero');
obj.C_e = [];
obj.C_f = [];
end
% Get algebraic constraints
idx_ac = ~any(obj.C_u');
B_y = obj.C_y(idx_ac, :) * (obj.G+obj.P)';
if ~isempty(obj.B)
obj.B = [obj.B, B_y'];
else
obj.B = B_y';
end
obj.B = rref(obj.B')';
obj.B(:, ~any(obj.B)) = [];
obj.C_y(idx_ac, :) = [];
obj.C_u(idx_ac, :) = [];
% Solve remaining equations for u
u_f = ones(1, obj.n_u);
A_uu = eye(obj.n_u);
%A_ue = zeros(obj.n_u, obj.n);
for i = 1:size(obj.C_u, 1)
idx_u = find(obj.C_u(i, :), 1);
if ~isempty(idx_u)
% Constrain this input
u_f(idx_u) = 0;
a_uu = obj.C_u(i, :)*A_uu + obj.C_y(i,:)*(obj.M+obj.S)*A_uu;
b = a_uu(idx_u);
a_uu(idx_u) = 0;
A_uu(idx_u, :) = -b .* a_uu;
%A_ue(idx_u, :) = -b .* obj.C_y(i,:)*(obj.G+obj.P)';
% Update dependent inputs
idx_uc = find(A_uu(:, idx_u));
for k=1:length(idx_uc)
b = A_uu(idx_uc(k), idx_u);
A_uu(idx_uc(k), idx_u) = 0;
A_uu(idx_uc(k), :) = A_uu(idx_uc(k), :) + b*A_uu(idx_u, :);
%A_ue(idx_uc(k), :) = A_ue(idx_uc(k), :) + b*A_ue(idx_u, :);
end
% Update constraints
b = obj.C_u(i, idx_u);
c_uu = -b*obj.C_u(i, :);
c_uu(idx_u) = 0;
c_uy = -b*obj.C_y(i, :);
idx_uc = find(obj.C_u(i+1:end, idx_u)) + i;
for k=1:length(idx_uc)
b = obj.C_u(idx_uc(k), idx_u);
obj.C_u(idx_uc(k), idx_u) = 0;
obj.C_u(idx_uc(k), :) = obj.C_u(idx_uc(k), :) + b * c_uu;
obj.C_y(idx_uc(k), :) = obj.C_y(idx_uc(k), :) + b * c_uy;
end
else
warning(['ignoring constraint ' num2str(i)]);
end
end
A_uu(:, ~any(A_uu)) = [];
u_new = find(u_f);
u_dep = find(~u_f);
J_ = obj.J; %+ (obj.G - obj.P) * A_ue;
R_ = obj.R;
G_ = (obj.G - obj.P) * A_uu;
P_ = zeros(obj.n, size(G_, 2));
D_ = A_uu' * (obj.M + obj.S) * A_uu;
if ~issymmetric(D_, 'skew')
error('Splitting D into S and M needs to be implemented');
else
M_ = D_;
S_ = zeros(size(M_, 1), size(M_, 2));
end
% remove dependent inputs
u_dep = sort(u_dep, 'descend');
ports = obj.getPortsForIOPairs(u_dep);
obj.deletePorts(ports);
u_new = 1:length(u_new);
% Assign new system matrices
obj.J = J_;
obj.R = R_;
obj.G = G_;
obj.P = P_;
obj.M = M_;
obj.S = S_;
% Check for zero colums in G (inputs that are zero) and remove
% them
idx_u0 = ~any(obj.G-obj.P);
u_dep = u_new(idx_u0);
u_dep = sort(u_dep, 'descend');
ports = obj.getPortsForIOPairs(u_dep);
obj.deletePorts(ports);
% System check...
if ~obj.isValidPHSystem()
error('Something is messed up... check your constraints');
end
% All I/O coupling constraints are now eliminated
obj.C_u = zeros(0, obj.n_u);
obj.C_y = zeros(0, obj.n_u);
obj.C_e = zeros(0, obj.n);
obj.C_f = zeros(0, obj.n);
% Some algebraic constraints might remain
% Call eliminateAlgebraicConstraints() to eliminate them
% This also eliminates constraint forces (inputs)
obj.n_c = size(obj.B, 2);
end
% Eliminates all constraints of the form B'*e = 0. Such constraints
% are usually generated by a call to assembleDAESystem()
% Setting useRationalBasis to 1 uses a rational basis for the null
% space of B. This preserves separation of energy domains in some
% cases but is less numerically stable... see for yourself.
function [V_xz] = eliminateAlgebraicConstraints(obj, useRationalBasis)
if size(obj.B, 2) == 0
% No algebraic constraints --> nothing to do!
V_xz = eye(obj.n);
return
end
% Get number of algebraic constraints
n_ac = size(obj.B, 2);
if rank(obj.B) ~= n_ac
obj.B = rref(obj.B')';
obj.B(:,~any(obj.B)) = [];
n_ac = size(obj.B, 2);
end
% Construct a left annihilator such that G_a*B = 0
if nargin > 1 && useRationalBasis
G_a = null(obj.B', 'r')';
else
G_a = null(obj.B')';
end
% Compute transformation matrix
V = [G_a; (obj.B'*obj.B)\obj.B'];
% Transform system matrices
J_ = V*obj.J*V';
R_ = V*obj.R*V';
Q_ = V'\(obj.Q/V);
G_ = V*obj.G;
P_ = V*obj.P;
C_ = obj.C*V';
% Partition Q (Q2x will be eliminated)
Q_11 = Q_(1:obj.n-n_ac, 1:obj.n-n_ac);
Q_12 = Q_(1:obj.n-n_ac, obj.n-n_ac+1:end);
Q_22 = Q_(obj.n-n_ac+1:end, obj.n-n_ac+1:end);
Q_21 = Q_(obj.n-n_ac+1:end, 1:obj.n-n_ac);
x_p_ = zeros(obj.n, 1);
x_p_(obj.x_p) = 1;
x_q_ = zeros(obj.n, 1);
x_q_(obj.x_q) = 1;
% System order is reduced by the number of constraints
obj.n = obj.n - n_ac;
% Map from new to old state variables
V_xz = V\[eye(obj.n); -Q_22\Q_21];
obj.x_p = find(abs(V_xz\x_p_) > 1e-10);
obj.x_q = find(abs(V_xz\x_q_) > 1e-10);
obj.J = J_(1:obj.n, 1:obj.n);
obj.R = R_(1:obj.n, 1:obj.n);
obj.Q = Q_11 - Q_12*(Q_22\Q_21);
obj.G = G_(1:obj.n, :);
obj.P = P_(1:obj.n, :);
obj.C = C_(:, 1:obj.n);
% We know that this method preserves structural properties...
% make sure they are not lost due to the numerics involved
obj.J = obj.J - 0.5*(obj.J+obj.J');
obj.R = obj.R - 0.5*(obj.R-obj.R');
obj.Q = obj.Q - 0.5*(obj.Q-obj.Q');
% Did we eliminate constraint forces?
idx_u0 = find(~any(round(obj.G-obj.P, 10)));
% If yes, remove them
u_dep = sort(idx_u0, 'descend');
ports = obj.getPortsForIOPairs(u_dep);
obj.deletePorts(ports);
% G_c is now empty
obj.B = zeros(obj.n, 0);
% The number of constraints is reduced by a_c
obj.n_c = obj.n_c - n_ac;
% Ensure correct size of constraint matrices
obj.C_u = zeros(0, obj.n_u);
obj.C_y = zeros(0, obj.n_u);
obj.C_e = zeros(0, obj.n);
obj.C_f = zeros(0, obj.n);
% Finally, check the validity of the system
if ~obj.isValidPHSystem()
error('Whoops, something went wrong. Try decreasing arithmetic precision');
end
end
% Check whether there exist linear dependencies between sets of
% effort variables. If yes, derive an orthonormal basis for and
% eliminate them by calling eliminateAlgebraicConstraints
function [V_xz] = eliminateLinearDependencies(obj, useRationalBasis)
V_xz = eye(obj.n);
if size(obj.B, 2) > 0
error('There are still algebraic constraints on the system ... eliminate those first');
end
% Check whether there is a block of linearly dependent flows
row_idx = ~any(round(obj.G'-obj.P', 12),1);
col_idx = any(obj.J(row_idx, :)-obj.R(row_idx,:));
J_sorted = [obj.J(:, col_idx == 1), obj.J(:, col_idx ~=1)];
J_sorted = [J_sorted(row_idx ~= 1, :); J_sorted(row_idx == 1, :)];
R_sorted = [obj.R(:, col_idx == 1), obj.R(:, col_idx ~=1)];
R_sorted = [R_sorted(row_idx ~= 1, :); R_sorted(row_idx == 1, :)];
G_sorted = [obj.G(row_idx ~= 1, :); obj.G(row_idx == 1, :)];
P_sorted = [obj.P(row_idx ~= 1, :); obj.P(row_idx == 1, :)];
row_idx = ~any(round(G_sorted'-P_sorted', 12), 1);
col_idx = any(round(J_sorted(row_idx, :) - R_sorted(row_idx, :), 12));
if sum(row_idx) > sum(col_idx)
% If such a block was found, construct an orthonormal basis
% to eliminate dependent states
A_b = J_sorted(row_idx == 1, :) - R_sorted(row_idx == 1, :);
[U,~] = svd(A_b,'econ');
T_b = U(:, 1:sum(row_idx == 0));
%T_b = orth(A_b);
T = blkdiag(eye(obj.n-size(T_b, 1)), T_b);
% Construct algebraic constraints B_ for linearly dependent
% states
[~,lambda,V] = svd((obj.Q*T)', 0);
lambda = diag(lambda);
lambda = lambda / max(lambda);
if length(lambda) < size(V, 1)
lambda(size(V,1)) = 0;
end
obj.B = V(:, lambda < 1e-18);
obj.n_c = obj.n_c + size(obj.B, 2);
if nargin > 1 && useRationalBasis
V_xz = obj.eliminateAlgebraicConstraints(1);
else
V_xz = obj.eliminateAlgebraicConstraints();
end
end
end
% Perform A modal analysis on the system and truncate high
% frequency eigenmodes
function [T, V] = transformGeneralizedCoordinates(obj, U)
M_ = inv(obj.Q(obj.x_p, obj.x_p));
M_ = M_ - 0.5*(M_-M_');
%obj.Q(obj.x_p, obj.x_q) = 0;
%obj.Q(obj.x_q, obj.x_p) = 0;
T = pinv(blkdiag(M_*U, U));
V = blkdiag(M_*U, U);
obj.Q = pinv(blkdiag(U, M_*U))*obj.Q*V;
obj.Q = obj.Q - 0.5*(obj.Q-obj.Q');
obj.J = T*obj.J*blkdiag(U, M_*U);
obj.J = obj.J - 0.5*(obj.J+obj.J');
obj.R = T*obj.R*blkdiag(U, M_*U);
obj.R = obj.R - 0.5*(obj.R-obj.R');
obj.G = T*obj.G;
obj.P = T*obj.P;
obj.B = T*obj.B;
obj.C = obj.C*blkdiag(U, M_*U);
obj.n = 2*size(U, 2);
obj.x_p = [1:obj.n/2]';
obj.x_q = [obj.n/2+1:obj.n]';
end
% Transform the state space by formulation of algebraic
% constraints. T is the transformation matrix between state spaces.
function V_xz = applyStateTransformation(obj, T)
% Construct algebraic constraints B_ for linearly dependent
% states
[~,lambda,V] = svd(T*obj.Q, 0);
lambda = diag(lambda);
lambda = lambda / max(lambda);
if length(lambda) < size(V, 1)
lambda(size(V,1)) = 0;
end
obj.B = V(:, lambda < 1e-18);
obj.n_c = obj.n_c + size(obj.B, 2);
V_xz = obj.eliminateAlgebraicConstraints();
end
% Designed for mechanical systems where the global dofs are given
% as x = [M dq, q] for a system M ddq + D dq + K q = B u
% NOTE: doens't work for hypostatic systems
function transformToGlobalDOFs(obj, skip2ndStep)
row_idx = any(obj.G'-obj.P', 1);
G_a = null(obj.G(row_idx,:)');
T = blkdiag([(obj.G(row_idx, :)'*obj.G(row_idx,:))\obj.G(row_idx,:)'; G_a'], eye(obj.n-sum(row_idx)));
%T = blkdiag([pinv(obj.G); G_a'], eye(obj.n-sum(row_idx)));
if rank(T) < obj.n
error('system is singular (maybe hypostatic?)... aborting')
end
% Left mulitply the system with T. J and R need to be right
% multiplied with T' to preserve symmetry properties
obj.J = T*obj.J*T';
obj.R = T*obj.R*T';
% This transformation affects e_p only --> turns to M^-1
% Q needs to be left multiplied with the inverse of T' and
% right multiplied with the inverse of T
obj.Q = T'\(obj.Q/T);
obj.G = T*obj.G;
obj.P = T*obj.P;
obj.B = T*obj.B;
obj.C = obj.C*T';
obj.x_p = [1:obj.n/2]';
obj.x_q = [obj.n/2+1:obj.n]';
if nargin > 1 && skip2ndStep
return
end
T = blkdiag(eye(obj.n/2), pinv(obj.J(obj.n/2+1:end, 1:obj.n/2)));
if rank(T) < obj.n
error('system is singular (maybe hypostatic?)... aborting')
end
% Again, J and R needs to be right multiplied by T'
obj.J = T*obj.J*T';
obj.R = T*obj.R*T';
% This transformation affects e_q only --> turns to K
obj.Q = T'\(obj.Q/T);
obj.G = T*obj.G;
obj.P = T*obj.P;
obj.B = T*obj.B;
obj.C = obj.C*T';
% We know that this method preserves structural properties...
% make sure they are not lost due to the numerics involved
obj.J = obj.J - 0.5*(obj.J+obj.J');
obj.R = obj.R - 0.5*(obj.R-obj.R');
obj.Q = obj.Q - 0.5*(obj.Q-obj.Q');
end
% Assuming the system is in some kind of balanced form, use the
% effort-constraint method to eliminate the efforcts e_c
% NOTE: implemented according to Polyuga & v. d. Schaft 2011
% NOTE: MOR is short for model order reduction
function effortConstraintMOR(obj, e_2)
if islogical(e_2)
e_1 = ~e_2;
else
e_1 = 1:obj.n;
e_1(ismember(e_1, e_2)) = [];
end
obj.J = obj.J(e_1, e_1);
obj.R = obj.R(e_1, e_1);
obj.Q = obj.Q(e_1, e_1)-obj.Q(e_1, e_2)*(obj.Q(e_2, e_2)\obj.Q(e_2, e_1));
obj.G = obj.G(e_1, :);
obj.P = obj.P(e_1, :);
obj.M = obj.M(e_1, e_1);
obj.S = obj.S(e_1, e_1);
obj.C = obj.C(:, e_1);
end
% Assuming the system is in some kind of balanced form, use the
% flow-constraint method to elminate the flows f_c
% NOTE: implemented according to Polyuga & v. d. Schaft 2011
% NOTE: MOR is short for model order reduction
function flowConstraintMOR(obj, f_2)
if nnz(obj.P) || nnz(obj.M) || nnz(obj.S)
error('not implemented for systems with input feedthrough');
end
if islogical(f_2)
f_1 = ~f_2;
else
f_1 = 1:obj.n;
f_1(ismember(f_1, f_2)) = [];
end
J_11 = obj.J(f_1, f_1);
J_12 = obj.J(f_1, f_2);
J_21 = obj.J(f_2, f_1);
J_22 = obj.J(f_2, f_2);
G_1 = obj.G(f_1, :);
G_2 = obj.G(f_2, :);
alpha = G_2'*J_22\J_21 - G_1';
beta = J_22\J_21 - eye(size(J_11, 1));
gamma = G_2'*J_22^-1;
delta = J_22^-1;
eta = G_2'*J_22\G_2;
Z = obj.R*(eye(obj.n) - delta*obj.R)^-1;
Z_sym = 0.5*(Z+Z');
Z_sk = 0.5*(Z-Z');
J_s = J_11 - J_12*J_22\J_21;
obj.J = J_s - beta'*Z_sk*beta;
obj.R = beta'*Z_sym*beta;
obj.G = -alpha'+beta'*Z_sk*gamma';
% Flow-constraint method produces feedthrough for G_2 ~= 0
obj.P = -beta'*Z_sym*gamma';
obj.M = -eta+gamma*Z_sk*gamma';
obj.S = gamma*Z_sym*gamma';
% NOTE: no idea what to do about C
end
% Provides the possibility to replace the input matrix G by a
% user-specified one. Deletes all existing inputs.
function setInputMatrix(obj, G_)
if size(G_, 1) ~= obj.n
error('input matrix must have n rows');
end
% Delete all ports associated with current input matrix
obj.deletePorts(obj.getPortsForIOPairs(1:obj.n_u));
obj.G = G_;
obj.n_u = size(obj.G, 2);
obj.P = zeros(size(obj.G));
obj.M = zeros(obj.n_u);
obj.S = zeros(obj.n_u);
end
% Returns the input names of the inputs specified by IOPairs in the
% form of a cell array of strings
function names = getInputNames(obj, IOPairs)
names = cell(1, length(IOPairs));
ports = obj.getPortsForIOPairs(IOPairs);
for p = 1:length(ports)
names{p} = obj.ports{ports(p)}.inputName;
end
end
% Returns the system ODEs as an anonymous function. In the presence
% of algebraic constraints, the lagrange multipliers are calculated
% to obtain an explicit representation.
function [f, dfdx] = getSystemODEfun(obj)
if obj.isConstrained()
lambda = @(x, u) -(obj.B'*obj.Q*obj.B)\obj.B'*obj.Q*((obj.J-obj.R)*obj.Q*x + (obj.G - obj.P)*u);