-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathiosys_test.py
More file actions
2421 lines (2067 loc) · 98.1 KB
/
Copy pathiosys_test.py
File metadata and controls
2421 lines (2067 loc) · 98.1 KB
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
"""iosys_test.py - test input/output system operations
RMM, 17 Apr 2019
This test suite checks to make sure that basic input/output class
operations are working. It doesn't do exhaustive testing of
operations on input/output systems. Separate unit tests should be
created for that purpose.
"""
import re
import warnings
from math import sqrt
import numpy as np
import pytest
import scipy
import control as ct
import control.flatsys as fs
class TestIOSys:
@pytest.fixture
def tsys(self):
class TSys:
pass
T = TSys()
"""Return some test systems"""
# Create a single input/single output linear system
T.siso_linsys = ct.StateSpace(
[[-1, 1], [0, -2]], [[0], [1]], [[1, 0]], [[0]])
# Create a multi input/multi output linear system
T.mimo_linsys1 = ct.StateSpace(
[[-1, 1], [0, -2]], [[1, 0], [0, 1]],
[[1, 0], [0, 1]], np.zeros((2, 2)))
# Create a multi input/multi output linear system
T.mimo_linsys2 = ct.StateSpace(
[[-1, 1], [0, -2]], [[0, 1], [1, 0]],
[[1, 0], [0, 1]], np.zeros((2, 2)))
# Create a static gain linear system
T.staticgain = ct.StateSpace([], [], [], 1)
# Create simulation parameters
T.T = np.linspace(0, 10, 100)
T.U = np.sin(T.T)
T.X0 = [0, 0]
return T
def test_linear_iosys(self, tsys):
# Create an input/output system from the linear system
linsys = tsys.siso_linsys
iosys = ct.StateSpace(linsys).copy()
# Make sure that the right hand side matches linear system
for x, u in (([0, 0], 0), ([1, 0], 0), ([0, 1], 0), ([0, 0], 1)):
np.testing.assert_array_almost_equal(
iosys._rhs(0, x, u),
linsys.A @ np.array(x) + linsys.B @ np.array(u, ndmin=1))
# Make sure that simulations also line up
T, U, X0 = tsys.T, tsys.U, tsys.X0
lti_t, lti_y = ct.forced_response(linsys, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys, T, U, X0)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y, atol=0.002, rtol=0.)
# Make sure that a static linear system has dt=None
# and otherwise dt is as specified
assert ct.StateSpace(tsys.staticgain).dt is None
assert ct.StateSpace(tsys.staticgain, dt=.1).dt == .1
def test_tf2io(self, tsys):
# Create a transfer function from the state space system
linsys = tsys.siso_linsys
tfsys = ct.ss2tf(linsys)
with pytest.warns(FutureWarning, match="use tf2ss"):
iosys = ct.tf2io(tfsys)
# Verify correctness via simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
lti_t, lti_y = ct.forced_response(linsys, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys, T, U, X0)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y, atol=0.002, rtol=0.)
# Make sure that non-proper transfer functions generate an error
tfsys = ct.tf('s')
with pytest.raises(ValueError):
with pytest.warns(FutureWarning, match="use tf2ss"):
iosys=ct.tf2io(tfsys)
def test_ss2io(self, tsys):
# Create an input/output system from the linear system
linsys = tsys.siso_linsys
with pytest.warns(FutureWarning, match="use ss"):
iosys = ct.ss2io(linsys)
np.testing.assert_allclose(linsys.A, iosys.A)
np.testing.assert_allclose(linsys.B, iosys.B)
np.testing.assert_allclose(linsys.C, iosys.C)
np.testing.assert_allclose(linsys.D, iosys.D)
# Try adding names to things
with pytest.warns(FutureWarning, match="use ss"):
iosys_named = ct.ss2io(linsys, inputs='u', outputs='y',
states=['x1', 'x2'], name='iosys_named')
assert iosys_named.find_input('u') == 0
assert iosys_named.find_input('x') is None
assert iosys_named.find_output('y') == 0
assert iosys_named.find_output('u') is None
assert iosys_named.find_state('x0') is None
assert iosys_named.find_state('x1') == 0
assert iosys_named.find_state('x2') == 1
np.testing.assert_allclose(linsys.A, iosys_named.A)
np.testing.assert_allclose(linsys.B, iosys_named.B)
np.testing.assert_allclose(linsys.C, iosys_named.C)
np.testing.assert_allclose(linsys.D, iosys_named.D)
def test_sstf_rename(self):
# Create a state space system
sys = ct.rss(4, 1, 1)
sys_ss = ct.ss(sys, inputs=['u1'], outputs=['y1'])
assert sys_ss.input_labels == ['u1']
assert sys_ss.output_labels == ['y1']
assert sys_ss.name == sys.name
# Convert to transfer function with renaming
sys_tf = ct.tf(sys, inputs=['a'], outputs=['c'])
assert sys_tf.input_labels == ['a']
assert sys_tf.output_labels == ['c']
assert sys_tf.name != sys_ss.name
def test_iosys_unspecified(self, tsys):
"""System with unspecified inputs and outputs"""
sys = ct.NonlinearIOSystem(secord_update, secord_output)
np.testing.assert_raises(TypeError, sys.__mul__, sys)
def test_iosys_print(self, tsys, capsys):
"""Make sure we can print various types of I/O systems"""
# Send the output to /dev/null
# Simple I/O system
iosys = ct.ss(tsys.siso_linsys)
print(iosys)
# I/O system without ninputs, noutputs
ios_unspecified = ct.NonlinearIOSystem(secord_update, secord_output)
print(ios_unspecified)
# I/O system with derived inputs and outputs
ios_linearized = ct.linearize(ios_unspecified, [0, 0], [0])
print(ios_linearized)
@pytest.mark.parametrize("ss", [ct.NonlinearIOSystem, ct.ss])
def test_nonlinear_iosys(self, tsys, ss):
# Create a simple nonlinear I/O system
nlsys = ct.NonlinearIOSystem(predprey)
T = tsys.T
# Start by simulating from an equilibrium point
X0 = [0, 0]
ios_t, ios_y = ct.input_output_response(nlsys, T, 0, X0)
np.testing.assert_array_almost_equal(ios_y, np.zeros(np.shape(ios_y)))
# Now simulate from a nonzero point
X0 = [0.5, 0.5]
ios_t, ios_y = ct.input_output_response(nlsys, T, 0, X0)
#
# Simulate a linear function as a nonlinear function and compare
#
# Create a single input/single output linear system
linsys = tsys.siso_linsys
# Create a nonlinear system with the same dynamics
nlupd = lambda t, x, u, params: \
np.reshape(linsys.A @ np.reshape(x, (-1, 1))
+ linsys.B @ np.reshape(u, (-1, 1)),
(-1,))
nlout = lambda t, x, u, params: \
np.reshape(linsys.C @ np.reshape(x, (-1, 1))
+ linsys.D @ np.reshape(u, (-1, 1)),
(-1,))
nlsys = ct.NonlinearIOSystem(nlupd, nlout, inputs=1, outputs=1)
# Make sure that simulations also line up
T, U, X0 = tsys.T, tsys.U, tsys.X0
lti_t, lti_y = ct.forced_response(linsys, T, U, X0)
ios_t, ios_y = ct.input_output_response(nlsys, T, U, X0)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y,atol=0.002,rtol=0.)
@pytest.fixture
def kincar(self):
# Create a simple nonlinear system to check (kinematic car)
def kincar_update(t, x, u, params):
return np.array([np.cos(x[2]) * u[0], np.sin(x[2]) * u[0], u[1]])
def kincar_output(t, x, u, params):
return np.array([x[0], x[1]])
return ct.NonlinearIOSystem(
kincar_update, kincar_output,
inputs = ['v', 'phi'],
outputs = ['x', 'y'],
states = ['x', 'y', 'theta'])
def test_linearize(self, tsys, kincar):
# Create a single input/single output linear system
linsys = tsys.siso_linsys
iosys = ct.StateSpace(linsys)
# Linearize it and make sure we get back what we started with
linearized = iosys.linearize([0, 0], 0)
np.testing.assert_array_almost_equal(linsys.A, linearized.A)
np.testing.assert_array_almost_equal(linsys.B, linearized.B)
np.testing.assert_array_almost_equal(linsys.C, linearized.C)
np.testing.assert_array_almost_equal(linsys.D, linearized.D)
# Create a simple nonlinear system to check (kinematic car)
iosys = kincar
linearized = iosys.linearize([0, 0, 0], [0, 0])
np.testing.assert_array_almost_equal(linearized.A, np.zeros((3,3)))
np.testing.assert_array_almost_equal(
linearized.B, [[1, 0], [0, 0], [0, 1]])
np.testing.assert_array_almost_equal(
linearized.C, [[1, 0, 0], [0, 1, 0]])
np.testing.assert_array_almost_equal(linearized.D, np.zeros((2,2)))
# Pass fewer than the required elements
padded = iosys.linearize([0, 0], np.array([0]))
assert padded.nstates == linearized.nstates
assert padded.ninputs == linearized.ninputs
# Check for warning if last element before padding is nonzero
with pytest.warns(UserWarning, match="x0 too short; padding"):
padded = iosys.linearize([0, 1], np.array([0]))
@pytest.mark.usefixtures("editsdefaults")
def test_linearize_named_signals(self, kincar):
# Full form of the call
linearized = kincar.linearize(
[0, 0, 0], [0, 0], copy_names=True, name='linearized')
assert linearized.name == 'linearized'
assert linearized.find_input('v') == 0
assert linearized.find_input('phi') == 1
assert linearized.find_output('x') == 0
assert linearized.find_output('y') == 1
assert linearized.find_state('x') == 0
assert linearized.find_state('y') == 1
assert linearized.find_state('theta') == 2
# If we copy signal names w/out a system name, append '$linearized'
linearized = kincar.linearize([0, 0, 0], [0, 0], copy_names=True)
assert linearized.name == kincar.name + '$linearized'
# If copy is False, signal names should not be copied
lin_nocopy = kincar.linearize(0, 0, copy_names=False)
assert lin_nocopy.find_input('v') is None
assert lin_nocopy.find_output('x') is None
assert lin_nocopy.find_state('x') is None
# if signal names are provided, they should override those of kincar
linearized_newnames = kincar.linearize(
[0, 0, 0], [0, 0], name='linearized',
copy_names=True, inputs=['v2', 'phi2'], outputs=['x2','y2'])
assert linearized_newnames.name == 'linearized'
assert linearized_newnames.find_input('v2') == 0
assert linearized_newnames.find_input('phi2') == 1
assert linearized_newnames.find_input('v') is None
assert linearized_newnames.find_input('phi') is None
assert linearized_newnames.find_output('x2') == 0
assert linearized_newnames.find_output('y2') == 1
assert linearized_newnames.find_output('x') is None
assert linearized_newnames.find_output('y') is None
# if system name is provided but copy_names is false, override name
linearized_newsysname = kincar.linearize(
[0, 0, 0], [0, 0], name='newname', copy_names=False)
assert linearized_newsysname.name == 'newname'
# Test legacy version as well
with pytest.warns(UserWarning, match="NumPy matrix class no longer"):
ct.use_legacy_defaults('0.8.4')
linearized = kincar.linearize([0, 0, 0], [0, 0], copy_names=True)
assert linearized.name == kincar.name + '_linearized'
def test_connect(self, tsys):
# Define a couple of (linear) systems to interconnection
linsys1 = tsys.siso_linsys
iosys1 = ct.StateSpace(linsys1, name='iosys1')
linsys2 = tsys.siso_linsys
iosys2 = ct.StateSpace(linsys2, name='iosys2')
# Connect systems in different ways and compare to StateSpace
linsys_series = linsys2 * linsys1
iosys_series = ct.InterconnectedSystem(
[iosys1, iosys2], # systems
[[1, 0]], # interconnection (series)
0, # input = first system
1 # output = second system
)
# Run a simulation and compare to linear response
T, U = tsys.T, tsys.U
X0 = np.concatenate((tsys.X0, tsys.X0))
ios_t, ios_y, ios_x = ct.input_output_response(
iosys_series, T, U, X0, return_x=True)
lti_t, lti_y = ct.forced_response(linsys_series, T, U, X0)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y,atol=0.002,rtol=0.)
# Connect systems with different timebases
linsys2c = tsys.siso_linsys
linsys2c.dt = 0 # Reset the timebase
iosys2c = ct.StateSpace(linsys2c)
iosys_series = ct.InterconnectedSystem(
[iosys1, iosys2c], # systems
[[1, 0]], # interconnection (series)
0, # input = first system
1 # output = second system
)
assert ct.isctime(iosys_series, strict=True)
ios_t, ios_y, ios_x = ct.input_output_response(
iosys_series, T, U, X0, return_x=True)
lti_t, lti_y = ct.forced_response(linsys_series, T, U, X0)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y,atol=0.002,rtol=0.)
# Feedback interconnection
linsys_feedback = ct.feedback(linsys1, linsys2)
iosys_feedback = ct.InterconnectedSystem(
[iosys1, iosys2], # systems
[[1, 0], # input of sys2 = output of sys1
[0, (1, 0, -1)]], # input of sys1 = -output of sys2
0, # input = first system
0 # output = first system
)
ios_t, ios_y, ios_x = ct.input_output_response(
iosys_feedback, T, U, X0, return_x=True)
lti_t, lti_y = ct.forced_response(linsys_feedback, T, U, X0)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y,atol=0.002,rtol=0.)
@pytest.mark.parametrize(
"connections, inplist, outlist",
[pytest.param([[(1, 0), (0, 0, 1)]], [[(0, 0, 1)]], [[(1, 0, 1)]],
id="full, raw tuple"),
pytest.param([[(1, 0), (0, 0, -1)]], [[(0, 0)]], [[(1, 0, -1)]],
id="full, raw tuple, canceling gains"),
pytest.param([[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]],
id="full, raw tuple, no gain"),
pytest.param([[(1, 0), (0, 0)]], [(0, 0)], [(1, 0)],
id="full, raw tuple, no gain, no outer list"),
pytest.param([['sys2.u[0]', 'sys1.y[0]']], ['sys1.u[0]'],
['sys2.y[0]'], id="named, full"),
pytest.param([['sys2.u[0]', '-sys1.y[0]']], ['sys1.u[0]'],
['-sys2.y[0]'], id="named, full, caneling gains"),
pytest.param([['sys2.u[0]', 'sys1.y[0]']], 'sys1.u[0]', 'sys2.y[0]',
id="named, full, no list"),
pytest.param([['sys2.u[0]', ('sys1', 'y[0]')]], [(0, 0)], [(1,)],
id="mixed"),
pytest.param([[1, 0]], 0, 1, id="minimal")])
def test_connect_spec_variants(self, tsys, connections, inplist, outlist):
# Define a couple of (linear) systems to interconnection
linsys1 = tsys.siso_linsys
iosys1 = ct.StateSpace(linsys1, name="sys1")
linsys2 = tsys.siso_linsys
iosys2 = ct.StateSpace(linsys2, name="sys2")
# Simple series connection
linsys_series = linsys2 * linsys1
# Create a simulation run to compare against
T, U = tsys.T, tsys.U
X0 = np.concatenate((tsys.X0, tsys.X0))
lti_t, lti_y, lti_x = ct.forced_response(
linsys_series, T, U, X0, return_x=True)
# Create the input/output system with different parameter variations
iosys_series = ct.InterconnectedSystem(
[iosys1, iosys2], connections, inplist, outlist)
ios_t, ios_y, ios_x = ct.input_output_response(
iosys_series, T, U, X0, return_x=True)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y, atol=0.002, rtol=0.)
@pytest.mark.parametrize(
"connections, inplist, outlist",
[pytest.param([['sys2.u[0]', 'sys1.y[0]']],
[[('sys1', 'u[0]'), ('sys1', 'u[0]')]],
[('sys2', 'y[0]', 0.5)], id="duplicated input"),
pytest.param([['sys2.u[0]', ('sys1', 'y[0]', 0.5)],
['sys2.u[0]', ('sys1', 'y[0]', 0.5)]],
'sys1.u[0]', 'sys2.y[0]', id="duplicated connection"),
pytest.param([['sys2.u[0]', 'sys1.y[0]']], 'sys1.u[0]',
[[('sys2', 'y[0]', 0.5), ('sys2', 'y[0]', 0.5)]],
id="duplicated output")])
def test_connect_spec_warnings(self, tsys, connections, inplist, outlist):
# Define a couple of (linear) systems to interconnection
linsys1 = tsys.siso_linsys
iosys1 = ct.StateSpace(linsys1, name="sys1")
linsys2 = tsys.siso_linsys
iosys2 = ct.StateSpace(linsys2, name="sys2")
# Simple series connection
linsys_series = linsys2 * linsys1
# Create a simulation run to compare against
T, U = tsys.T, tsys.U
X0 = np.concatenate((tsys.X0, tsys.X0))
lti_t, lti_y, lti_x = ct.forced_response(
linsys_series, T, U, X0, return_x=True)
# Set up multiple gainst and make sure a warning is generated
with pytest.warns(UserWarning, match="multiple.*combining"):
iosys_series = ct.InterconnectedSystem(
[iosys1, iosys2], connections, inplist, outlist)
ios_t, ios_y, ios_x = ct.input_output_response(
iosys_series, T, U, X0, return_x=True)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y, atol=0.002, rtol=0.)
def test_static_nonlinearity(self, tsys):
# Linear dynamical system
linsys = tsys.siso_linsys
ioslin = ct.StateSpace(linsys)
# Nonlinear saturation
sat = lambda u: u if abs(u) < 1 else np.sign(u)
sat_output = lambda t, x, u, params: sat(u)
nlsat = ct.NonlinearIOSystem(None, sat_output, inputs=1, outputs=1)
# Set up parameters for simulation
T, U, X0 = tsys.T, 2 * tsys.U, tsys.X0
Usat = np.vectorize(sat)(U)
# Make sure saturation works properly by comparing linear system with
# saturated input to nonlinear system with saturation composition
lti_t, lti_y, lti_x = ct.forced_response(
linsys, T, Usat, X0, return_x=True)
ios_t, ios_y, ios_x = ct.input_output_response(
ioslin * nlsat, T, U, X0, return_x=True)
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_array_almost_equal(lti_y, ios_y, decimal=2)
@pytest.mark.filterwarnings("ignore:Duplicate name::control.iosys")
def test_algebraic_loop(self, tsys):
# Create some linear and nonlinear systems to play with
linsys = tsys.siso_linsys
lnios = ct.StateSpace(linsys)
nlios = ct.NonlinearIOSystem(None, \
lambda t, x, u, params: u*u, inputs=1, outputs=1)
nlios1 = nlios.copy(name='nlios1')
nlios2 = nlios.copy(name='nlios2')
# Set up parameters for simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
# Single nonlinear system - no states
ios_t, ios_y = ct.input_output_response(nlios, T, U)
np.testing.assert_array_almost_equal(ios_y, U*U, decimal=3)
# Composed nonlinear system (series)
ios_t, ios_y = ct.input_output_response(nlios1 * nlios2, T, U)
np.testing.assert_array_almost_equal(ios_y, U**4, decimal=3)
# Composed nonlinear system (parallel)
ios_t, ios_y = ct.input_output_response(nlios1 + nlios2, T, U)
np.testing.assert_array_almost_equal(ios_y, 2*U**2, decimal=3)
# Nonlinear system composed with LTI system (series) -- with states
ios_t, ios_y = ct.input_output_response(
nlios * lnios * nlios, T, U, X0)
lti_t, lti_y = ct.forced_response(linsys, T, U*U, X0)
np.testing.assert_array_almost_equal(ios_y, lti_y*lti_y, decimal=3)
# Nonlinear system in feeback loop with LTI system
iosys = ct.InterconnectedSystem(
[lnios, nlios], # linear system w/ nonlinear feedback
[[1], # feedback interconnection (sig to 0)
[0, (1, 0, -1)]],
0, # input to linear system
0 # output from linear system
)
ios_t, ios_y = ct.input_output_response(iosys, T, U, X0)
# No easy way to test the result
# Algebraic loop from static nonlinear system in feedback
# (error will be due to no states)
iosys = ct.InterconnectedSystem(
[nlios1, nlios2], # two copies of a static nonlinear system
[[0, 1], # feedback interconnection
[1, (0, 0, -1)]],
0, 0
)
args = (iosys, T, U)
with pytest.raises(RuntimeError):
ct.input_output_response(*args)
# Algebraic loop due to feedthrough term
linsys = ct.StateSpace(
[[-1, 1], [0, -2]], [[0], [1]], [[1, 0]], [[1]])
lnios = ct.StateSpace(linsys)
iosys = ct.InterconnectedSystem(
[nlios, lnios], # linear system w/ nonlinear feedback
[[0, 1], # feedback interconnection
[1, (0, 0, -1)]],
0, 0
)
args = (iosys, T, U, X0)
# ios_t, ios_y = ct.input_output_response(iosys, T, U, X0)
with pytest.raises(RuntimeError):
ct.input_output_response(*args)
def test_summer(self, tsys):
# Construct a MIMO system for testing
linsys = tsys.mimo_linsys1
linio1 = ct.StateSpace(linsys, name='linio1')
linio2 = ct.StateSpace(linsys, name='linio2')
linsys_parallel = linsys + linsys
iosys_parallel = linio1 + linio2
# Set up parameters for simulation
T = tsys.T
U = [np.sin(T), np.cos(T)]
X0 = 0
lin_t, lin_y = ct.forced_response(linsys_parallel, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_parallel, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
def test_rmul(self, tsys):
# Test right multiplication
# Note: this is also tested in types_conversion_test.py
# Set up parameters for simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
# Linear system with input and output nonlinearities
# Also creates a nested interconnected system
ioslin = ct.StateSpace(tsys.siso_linsys)
nlios = ct.NonlinearIOSystem(None, \
lambda t, x, u, params: u*u, inputs=1, outputs=1)
sys1 = nlios * ioslin
sys2 = sys1 * nlios
# Make sure we got the right thing (via simulation comparison)
ios_t, ios_y = ct.input_output_response(sys2, T, U, X0)
lti_t, lti_y = ct.forced_response(ioslin, T, U*U, X0)
np.testing.assert_array_almost_equal(ios_y, lti_y*lti_y, decimal=3)
def test_neg(self, tsys):
"""Test negation of a system"""
# Set up parameters for simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
# Static nonlinear system
nlios = ct.NonlinearIOSystem(None, \
lambda t, x, u, params: u*u, inputs=1, outputs=1)
ios_t, ios_y = ct.input_output_response(-nlios, T, U)
np.testing.assert_array_almost_equal(ios_y, -U*U, decimal=3)
# Linear system with input nonlinearity
# Also creates a nested interconnected system
ioslin = ct.StateSpace(tsys.siso_linsys)
sys = (ioslin) * (-nlios)
# Make sure we got the right thing (via simulation comparison)
ios_t, ios_y = ct.input_output_response(sys, T, U, X0)
lti_t, lti_y = ct.forced_response(ioslin, T, U*U, X0)
np.testing.assert_array_almost_equal(ios_y, -lti_y, decimal=3)
def test_feedback(self, tsys):
# Set up parameters for simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
# Linear system with constant feedback (via "nonlinear" mapping)
ioslin = ct.StateSpace(tsys.siso_linsys)
nlios = ct.NonlinearIOSystem(None, \
lambda t, x, u, params: u, inputs=1, outputs=1)
iosys = ct.feedback(ioslin, nlios)
linsys = ct.feedback(tsys.siso_linsys, 1)
ios_t, ios_y = ct.input_output_response(iosys, T, U, X0)
lti_t, lti_y = ct.forced_response(linsys, T, U, X0)
np.testing.assert_allclose(ios_y, lti_y,atol=0.002,rtol=0.)
def test_bdalg_functions(self, tsys):
"""Test block diagram functions algebra on I/O systems"""
# Set up parameters for simulation
T = tsys.T
U = [np.sin(T), np.cos(T)]
X0 = 0
# Set up systems to be composed
linsys1 = tsys.mimo_linsys1
linio1 = ct.StateSpace(linsys1)
linsys2 = tsys.mimo_linsys2
linio2 = ct.StateSpace(linsys2)
# Series interconnection
linsys_series = ct.series(linsys1, linsys2)
iosys_series = ct.series(linio1, linio2)
lin_t, lin_y = ct.forced_response(linsys_series, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_series, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Make sure that systems don't commute
linsys_series = ct.series(linsys2, linsys1)
lin_t, lin_y = ct.forced_response(linsys_series, T, U, X0)
assert not (np.abs(lin_y - ios_y) < 1e-3).all()
# Parallel interconnection
linsys_parallel = ct.parallel(linsys1, linsys2)
iosys_parallel = ct.parallel(linio1, linio2)
lin_t, lin_y = ct.forced_response(linsys_parallel, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_parallel, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Negation
linsys_negate = ct.negate(linsys1)
iosys_negate = ct.negate(linio1)
lin_t, lin_y = ct.forced_response(linsys_negate, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_negate, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Feedback interconnection
linsys_feedback = ct.feedback(linsys1, linsys2)
iosys_feedback = ct.feedback(linio1, linio2)
lin_t, lin_y = ct.forced_response(linsys_feedback, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_feedback, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
def test_algebraic_functions(self, tsys):
"""Test algebraic operations on I/O systems"""
# Set up parameters for simulation
T = tsys.T
U = [np.sin(T), np.cos(T)]
X0 = 0
# Set up systems to be composed
linsys1 = tsys.mimo_linsys1
linio1 = ct.StateSpace(linsys1)
linsys2 = tsys.mimo_linsys2
linio2 = ct.StateSpace(linsys2)
# Multiplication
linsys_mul = linsys2 * linsys1
iosys_mul = linio2 * linio1
lin_t, lin_y = ct.forced_response(linsys_mul, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_mul, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Make sure that systems don't commute
linsys_mul = linsys1 * linsys2
lin_t, lin_y = ct.forced_response(linsys_mul, T, U, X0)
assert not (np.abs(lin_y - ios_y) < 1e-3).all()
# Addition
linsys_add = linsys1 + linsys2
iosys_add = linio1 + linio2
lin_t, lin_y = ct.forced_response(linsys_add, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_add, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Subtraction
linsys_sub = linsys1 - linsys2
iosys_sub = linio1 - linio2
lin_t, lin_y = ct.forced_response(linsys_sub, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_sub, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Make sure that systems don't commute
linsys_sub = linsys2 - linsys1
lin_t, lin_y = ct.forced_response(linsys_sub, T, U, X0)
assert not (np.abs(lin_y - ios_y) < 1e-3).all()
# Negation
linsys_negate = -linsys1
iosys_negate = -linio1
lin_t, lin_y = ct.forced_response(linsys_negate, T, U, X0)
ios_t, ios_y = ct.input_output_response(iosys_negate, T, U, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
def test_nonsquare_bdalg(self, tsys):
# Set up parameters for simulation
T = tsys.T
U2 = [np.sin(T), np.cos(T)]
U3 = [np.sin(T), np.cos(T), T]
X0 = 0
# Set up systems to be composed
linsys_2i3o = ct.StateSpace(
[[-1, 1, 0], [0, -2, 0], [0, 0, -3]], [[1, 0], [0, 1], [1, 1]],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]], np.zeros((3, 2)))
iosys_2i3o = ct.StateSpace(linsys_2i3o)
linsys_3i2o = ct.StateSpace(
[[-1, 1, 0], [0, -2, 0], [0, 0, -3]],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[[1, 0, 1], [0, 1, -1]], np.zeros((2, 3)))
iosys_3i2o = ct.StateSpace(linsys_3i2o)
# Multiplication
linsys_multiply = linsys_3i2o * linsys_2i3o
iosys_multiply = iosys_3i2o * iosys_2i3o
lin_t, lin_y = ct.forced_response(linsys_multiply, T, U2, X0)
ios_t, ios_y = ct.input_output_response(iosys_multiply, T, U2, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
linsys_multiply = linsys_2i3o * linsys_3i2o
iosys_multiply = iosys_2i3o * iosys_3i2o
lin_t, lin_y = ct.forced_response(linsys_multiply, T, U3, X0)
ios_t, ios_y = ct.input_output_response(iosys_multiply, T, U3, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Right multiplication
iosys_multiply = iosys_2i3o * iosys_3i2o
ios_t, ios_y = ct.input_output_response(iosys_multiply, T, U3, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Feedback
linsys_multiply = ct.feedback(linsys_3i2o, linsys_2i3o)
iosys_multiply = iosys_3i2o.feedback(iosys_2i3o)
lin_t, lin_y = ct.forced_response(linsys_multiply, T, U3, X0)
ios_t, ios_y = ct.input_output_response(iosys_multiply, T, U3, X0)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Mismatch should generate exception
args = (iosys_3i2o, iosys_3i2o)
with pytest.raises(ValueError):
ct.series(*args)
def test_discrete(self, tsys):
"""Test discrete-time functionality"""
# Create some linear and nonlinear systems to play with
linsys = ct.StateSpace(
[[-1, 1], [0, -2]], [[0], [1]], [[1, 0]], [[0]], True)
lnios = ct.StateSpace(linsys)
# Set up parameters for simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
# Simulate and compare to LTI output
ios_t, ios_y = ct.input_output_response(lnios, T, U, X0)
lin_t, lin_y = ct.forced_response(linsys, T, U, X0)
np.testing.assert_allclose(ios_t, lin_t,atol=0.002,rtol=0.)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
# Test MIMO system, converted to discrete time
linsys = ct.StateSpace(tsys.mimo_linsys1)
linsys.dt = tsys.T[1] - tsys.T[0]
lnios = ct.StateSpace(linsys)
# Set up parameters for simulation
T = tsys.T
U = [np.sin(T), np.cos(T)]
X0 = 0
# Simulate and compare to LTI output
ios_t, ios_y = ct.input_output_response(lnios, T, U, X0)
lin_t, lin_y = ct.forced_response(linsys, T, U, X0)
np.testing.assert_allclose(ios_t, lin_t,atol=0.002,rtol=0.)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
def test_discrete_iosys(self, tsys):
"""Create a discrete-time system from scratch"""
linsys = ct.StateSpace(
[[-1, 1], [0, -2]], [[0], [1]], [[1, 0]], [[0]], True)
# Create nonlinear version of the same system
def nlsys_update(t, x, u, params):
A, B = params['A'], params['B']
return A @ x + B @ u
def nlsys_output(t, x, u, params):
C = params['C']
return C @ x
nlsys = ct.NonlinearIOSystem(
nlsys_update, nlsys_output, inputs=1, outputs=1, states=2, dt=True)
# Set up parameters for simulation
T, U, X0 = tsys.T, tsys.U, tsys.X0
# Simulate and compare to LTI output
ios_t, ios_y = ct.input_output_response(
nlsys, T, U, X0,
params={'A': linsys.A, 'B': linsys.B, 'C': linsys.C})
lin_t, lin_y = ct.forced_response(linsys, T, U, X0)
np.testing.assert_allclose(ios_t, lin_t,atol=0.002,rtol=0.)
np.testing.assert_allclose(ios_y, lin_y,atol=0.002,rtol=0.)
def test_find_eqpts_dfan(self, tsys):
"""Test find_eqpt function on dfan example"""
# Simple equilibrium point with no inputs
nlsys = ct.NonlinearIOSystem(predprey)
xeq, ueq, result = ct.find_eqpt(
nlsys, [1.6, 1.2], None, return_result=True)
assert result.success
np.testing.assert_array_almost_equal(xeq, [1.64705879, 1.17923874])
np.testing.assert_array_almost_equal(
nlsys._rhs(0, xeq, ueq), np.zeros((2,)))
# Ducted fan dynamics with output = velocity
nlsys = ct.NonlinearIOSystem(pvtol, lambda t, x, u, params: x[0:2])
# Make sure the origin is a fixed point
xeq, ueq, result = ct.find_eqpt(
nlsys, [0, 0, 0, 0], [0, 4*9.8], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys._rhs(0, xeq, ueq), np.zeros((4,)))
np.testing.assert_array_almost_equal(xeq, [0, 0, 0, 0])
# Use a small lateral force to cause motion
xeq, ueq, result = ct.find_eqpt(
nlsys, [0, 0, 0, 0], [0.01, 4*9.8], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys._rhs(0, xeq, ueq), np.zeros((4,)), decimal=5)
# Equilibrium point with fixed output
xeq, ueq, result = ct.find_eqpt(
nlsys, [0, 0, 0, 0], [0.01, 4*9.8],
y0=[0.1, 0.1], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys._out(0, xeq, ueq), [0.1, 0.1], decimal=5)
np.testing.assert_array_almost_equal(
nlsys._rhs(0, xeq, ueq), np.zeros((4,)), decimal=5)
# Specify outputs to constrain (replicate previous)
xeq, ueq, result = ct.find_eqpt(
nlsys, [0, 0, 0, 0], [0.01, 4*9.8], y0=[0.1, 0.1],
iy = [0, 1], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys._out(0, xeq, ueq), [0.1, 0.1], decimal=5)
np.testing.assert_array_almost_equal(
nlsys._rhs(0, xeq, ueq), np.zeros((4,)), decimal=5)
# Specify inputs to constrain (replicate previous), w/ no result
xeq, ueq = ct.find_eqpt(
nlsys, [0, 0, 0, 0], [0.01, 4*9.8], y0=[0.1, 0.1], iu = [])
np.testing.assert_array_almost_equal(
nlsys._out(0, xeq, ueq), [0.1, 0.1], decimal=5)
np.testing.assert_array_almost_equal(
nlsys._rhs(0, xeq, ueq), np.zeros((4,)), decimal=5)
# Now solve the problem with the original PVTOL variables
# Constrain the output angle and x velocity
nlsys_full = ct.NonlinearIOSystem(pvtol_full, None)
xeq, ueq, result = ct.find_eqpt(
nlsys_full, [0, 0, 0, 0, 0, 0], [0.01, 4*9.8],
y0=[0, 0, 0.1, 0.1, 0, 0], iy = [2, 3],
idx=[2, 3, 4, 5], ix=[0, 1], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys_full._out(0, xeq, ueq)[[2, 3]], [0.1, 0.1], decimal=5)
np.testing.assert_array_almost_equal(
nlsys_full._rhs(0, xeq, ueq)[-4:], np.zeros((4,)), decimal=5)
# Same test as before, but now all constraints are in the state vector
nlsys_full = ct.NonlinearIOSystem(pvtol_full, None)
xeq, ueq, result = ct.find_eqpt(
nlsys_full, [0, 0, 0.1, 0.1, 0, 0], [0.01, 4*9.8],
idx=[2, 3, 4, 5], ix=[0, 1, 2, 3], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys_full._out(0, xeq, ueq)[[2, 3]], [0.1, 0.1], decimal=5)
np.testing.assert_array_almost_equal(
nlsys_full._rhs(0, xeq, ueq)[-4:], np.zeros((4,)), decimal=5)
# Fix one input and vary the other
nlsys_full = ct.NonlinearIOSystem(pvtol_full, None)
xeq, ueq, result = ct.find_eqpt(
nlsys_full, [0, 0, 0, 0, 0, 0], [0.01, 4*9.8],
y0=[0, 0, 0.1, 0.1, 0, 0], iy=[3], iu=[1],
idx=[2, 3, 4, 5], ix=[0, 1], return_result=True)
assert result.success
np.testing.assert_almost_equal(ueq[1], 4*9.8, decimal=5)
np.testing.assert_array_almost_equal(
nlsys_full._out(0, xeq, ueq)[[3]], [0.1], decimal=5)
np.testing.assert_array_almost_equal(
nlsys_full._rhs(0, xeq, ueq)[-4:], np.zeros((4,)), decimal=5)
# PVTOL with output = y velocity
xeq, ueq, result = ct.find_eqpt(
nlsys_full, [0, 0, 0, 0.1, 0, 0], [0.01, 4*9.8],
y0=[0, 0, 0, 0.1, 0, 0], iy=[3],
dx0=[0.1, 0, 0, 0, 0, 0], idx=[1, 2, 3, 4, 5],
ix=[0, 1], return_result=True)
assert result.success
np.testing.assert_array_almost_equal(
nlsys_full._out(0, xeq, ueq)[-3:], [0.1, 0, 0], decimal=5)
np.testing.assert_array_almost_equal(
nlsys_full._rhs(0, xeq, ueq)[-5:], np.zeros((5,)), decimal=5)
# Unobservable system
linsys = ct.StateSpace(
[[-1, 1], [0, -2]], [[0], [1]], [[0, 0]], [[0]])
lnios = ct.StateSpace(linsys)
# If result is returned, user has to check
xeq, ueq, result = ct.find_eqpt(
lnios, [0, 0], [0], y0=[1], return_result=True)
assert not result.success
# If result is not returned, find_eqpt should return None
xeq, ueq = ct.find_eqpt(lnios, [0, 0], [0], y0=[1])
assert xeq is None
assert ueq is None
def test_params(self, tsys):
# Start with the default set of parameters
ios_secord_default = ct.NonlinearIOSystem(
secord_update, secord_output, inputs=1, outputs=1, states=2)
lin_secord_default = ct.linearize(ios_secord_default, [0, 0], [0])
w_default, v_default = np.linalg.eig(lin_secord_default.A)
# New copy, with modified parameters
ios_secord_update = ct.NonlinearIOSystem(
secord_update, secord_output, inputs=1, outputs=1, states=2,
params={'omega0':2, 'zeta':0})
lin_secord_update = ct.linearize(ios_secord_update, [0, 0], [0])
w_update, v_update = np.linalg.eig(lin_secord_update.A)
# Make sure the default parameters haven't changed
lin_secord_check = ct.linearize(ios_secord_default, [0, 0], [0])
w, v = np.linalg.eig(lin_secord_check.A)
np.testing.assert_array_almost_equal(np.sort(w), np.sort(w_default))
# Make sure updated system parameters got set correctly
lin_secord_update = ct.linearize(ios_secord_update, [0, 0], [0])
w, v = np.linalg.eig(lin_secord_update.A)
np.testing.assert_array_almost_equal(np.sort(w), np.sort([2j, -2j]))
# Change the parameters of the default sys just for the linearization
lin_secord_local = ct.linearize(ios_secord_default, [0, 0], [0],
params={'zeta':0})
w, v = np.linalg.eig(lin_secord_local.A)
np.testing.assert_array_almost_equal(np.sort(w), np.sort([1j, -1j]))
# Change the parameters of the updated sys just for the linearization
lin_secord_local = ct.linearize(ios_secord_update, [0, 0], [0],
params={'zeta':0, 'omega0':3})
w, v = np.linalg.eig(lin_secord_local.A)
np.testing.assert_array_almost_equal(np.sort(w), np.sort([3j, -3j]))
# Make sure that changes propagate through interconnections
ios_series_default_local = ios_secord_default * ios_secord_update
lin_series_default_local = ct.linearize(
ios_series_default_local, [0, 0, 0, 0], [0])
w, v = np.linalg.eig(lin_series_default_local.A)
np.testing.assert_array_almost_equal(
w, np.concatenate([w_update, w_update]))
# Show that we can change the parameters at linearization
lin_series_override = ct.linearize(
ios_series_default_local, [0, 0, 0, 0], [0],
params={'zeta':0, 'omega0':4})
w, v = np.linalg.eig(lin_series_override.A)
np.testing.assert_array_almost_equal(w, [4j, -4j, 4j, -4j])
# Check for warning if we try to set params for StateSpace
linsys = tsys.siso_linsys
iosys = ct.StateSpace(linsys)
T, U, X0 = tsys.T, tsys.U, tsys.X0
lti_t, lti_y = ct.forced_response(linsys, T, U, X0)
# TODO: add back something along these lines
# with pytest.warns(UserWarning, match="StateSpace.*ignored"):
ios_t, ios_y = ct.input_output_response(
iosys, T, U, X0, params={'something':0})
# Check to make sure results are OK
np.testing.assert_array_almost_equal(lti_t, ios_t)
np.testing.assert_allclose(lti_y, ios_y,atol=0.002,rtol=0.)
def test_named_signals(self, tsys):
sys1 = ct.NonlinearIOSystem(
updfcn = lambda t, x, u, params: np.array(
tsys.mimo_linsys1.A @ np.reshape(x, (-1, 1)) \
+ tsys.mimo_linsys1.B @ np.reshape(u, (-1, 1))
).reshape(-1,),
outfcn = lambda t, x, u, params: np.array(
tsys.mimo_linsys1.C @ np.reshape(x, (-1, 1)) \
+ tsys.mimo_linsys1.D @ np.reshape(u, (-1, 1))
).reshape(-1,),
inputs = ['u[0]', 'u[1]'],
outputs = ['y[0]', 'y[1]'],
states = tsys.mimo_linsys1.nstates,
name = 'sys1')