forked from python-control/python-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterconnect_test.py
More file actions
174 lines (144 loc) · 6.75 KB
/
Copy pathinterconnect_test.py
File metadata and controls
174 lines (144 loc) · 6.75 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
"""interconnect_test.py - test input/output interconnect function
RMM, 22 Jan 2021
This set of unit tests covers the various operatons of the interconnect()
function, as well as some of the support functions associated with
interconnect().
Note: additional tests are available in iosys_test.py, which focuses on the
raw InterconnectedSystem constructor. This set of unit tests focuses on
functionality implemented in the interconnect() function itself.
"""
import pytest
import numpy as np
import scipy as sp
import control as ct
@pytest.mark.parametrize("inputs, output, dimension, D", [
[1, 1, None, [[1]] ],
['u', 'y', None, [[1]] ],
[['u'], ['y'], None, [[1]] ],
[2, 1, None, [[1, 1]] ],
[['r', '-y'], ['e'], None, [[1, -1]] ],
[5, 1, None, np.ones((1, 5)) ],
['u', 'y', 1, [[1]] ],
['u', 'y', 2, [[1, 0], [0, 1]] ],
[['r', '-y'], ['e'], 2, [[1, 0, -1, 0], [0, 1, 0, -1]] ],
])
def test_summing_junction(inputs, output, dimension, D):
ninputs = 1 if isinstance(inputs, str) else \
inputs if isinstance(inputs, int) else len(inputs)
sum = ct.summing_junction(
inputs=inputs, output=output, dimension=dimension)
dim = 1 if dimension is None else dimension
np.testing.assert_array_equal(sum.A, np.ndarray((0, 0)))
np.testing.assert_array_equal(sum.B, np.ndarray((0, ninputs*dim)))
np.testing.assert_array_equal(sum.C, np.ndarray((dim, 0)))
np.testing.assert_array_equal(sum.D, D)
def test_summation_exceptions():
# Bad input description
with pytest.raises(ValueError, match="could not parse input"):
sumblk = ct.summing_junction(None, 'y')
# Bad output description
with pytest.raises(ValueError, match="could not parse output"):
sumblk = ct.summing_junction('u', None)
# Bad input dimension
with pytest.raises(ValueError, match="unrecognized dimension"):
sumblk = ct.summing_junction('u', 'y', dimension=False)
def test_interconnect_implicit():
"""Test the use of implicit connections in interconnect()"""
import random
# System definition
P = ct.ss2io(
ct.rss(2, 1, 1, strictly_proper=True),
inputs='u', outputs='y', name='P')
kp = ct.tf(random.uniform(1, 10), [1])
ki = ct.tf(random.uniform(1, 10), [1, 0])
C = ct.tf2io(kp + ki, inputs='e', outputs='u', name='C')
# Block diagram computation
Tss = ct.feedback(P * C, 1)
# Construct the interconnection explicitly
Tio_exp = ct.interconnect(
(C, P),
connections = [['P.u', 'C.u'], ['C.e', '-P.y']],
inplist='C.e', outlist='P.y')
# Compare to bdalg computation
np.testing.assert_almost_equal(Tio_exp.A, Tss.A)
np.testing.assert_almost_equal(Tio_exp.B, Tss.B)
np.testing.assert_almost_equal(Tio_exp.C, Tss.C)
np.testing.assert_almost_equal(Tio_exp.D, Tss.D)
# Construct the interconnection via a summing junction
sumblk = ct.summing_junction(inputs=['r', '-y'], output='e', name="sum")
Tio_sum = ct.interconnect(
(C, P, sumblk), inplist=['r'], outlist=['y'])
np.testing.assert_almost_equal(Tio_sum.A, Tss.A)
np.testing.assert_almost_equal(Tio_sum.B, Tss.B)
np.testing.assert_almost_equal(Tio_sum.C, Tss.C)
np.testing.assert_almost_equal(Tio_sum.D, Tss.D)
# Setting connections to False should lead to an empty connection map
empty = ct.interconnect(
(C, P, sumblk), connections=False, inplist=['r'], outlist=['y'])
np.testing.assert_array_equal(empty.connect_map, np.zeros((4, 3)))
# Implicit summation across repeated signals
kp_io = ct.tf2io(kp, inputs='e', outputs='u', name='kp')
ki_io = ct.tf2io(ki, inputs='e', outputs='u', name='ki')
Tio_sum = ct.interconnect(
(kp_io, ki_io, P, sumblk), inplist=['r'], outlist=['y'])
np.testing.assert_almost_equal(Tio_sum.A, Tss.A)
np.testing.assert_almost_equal(Tio_sum.B, Tss.B)
np.testing.assert_almost_equal(Tio_sum.C, Tss.C)
np.testing.assert_almost_equal(Tio_sum.D, Tss.D)
# TODO: interconnect a MIMO system using implicit connections
# P = control.ss2io(
# control.rss(2, 2, 2, strictly_proper=True),
# input_prefix='u', output_prefix='y', name='P')
# C = control.ss2io(
# control.rss(2, 2, 2),
# input_prefix='e', output_prefix='u', name='C')
# sumblk = control.summing_junction(
# inputs=['r', '-y'], output='e', dimension=2)
# S = control.interconnect([P, C, sumblk], inplist='r', outlist='y')
# Make sure that repeated inplist/outlist names generate an error
# Input not unique
Cbad = ct.tf2io(ct.tf(10, [1, 1]), inputs='r', outputs='x', name='C')
with pytest.raises(ValueError, match="not unique"):
Tio_sum = ct.interconnect(
(Cbad, P, sumblk), inplist=['r'], outlist=['y'])
# Output not unique
Cbad = ct.tf2io(ct.tf(10, [1, 1]), inputs='e', outputs='y', name='C')
with pytest.raises(ValueError, match="not unique"):
Tio_sum = ct.interconnect(
(Cbad, P, sumblk), inplist=['r'], outlist=['y'])
# Signal not found
with pytest.raises(ValueError, match="could not find"):
Tio_sum = ct.interconnect(
(C, P, sumblk), inplist=['x'], outlist=['y'])
with pytest.raises(ValueError, match="could not find"):
Tio_sum = ct.interconnect(
(C, P, sumblk), inplist=['r'], outlist=['x'])
def test_interconnect_docstring():
"""Test the examples from the interconnect() docstring"""
# MIMO interconnection (note: use [C, P] instead of [P, C] for state order)
P = ct.LinearIOSystem(
ct.rss(2, 2, 2, strictly_proper=True), name='P')
C = ct.LinearIOSystem(ct.rss(2, 2, 2), name='C')
T = ct.interconnect(
[C, P],
connections = [
['P.u[0]', 'C.y[0]'], ['P.u[1]', 'C.y[1]'],
['C.u[0]', '-P.y[0]'], ['C.u[1]', '-P.y[1]']],
inplist = ['C.u[0]', 'C.u[1]'],
outlist = ['P.y[0]', 'P.y[1]'],
)
T_ss = ct.feedback(P * C, ct.ss([], [], [], np.eye(2)))
np.testing.assert_almost_equal(T.A, T_ss.A)
np.testing.assert_almost_equal(T.B, T_ss.B)
np.testing.assert_almost_equal(T.C, T_ss.C)
np.testing.assert_almost_equal(T.D, T_ss.D)
# Implicit interconnection (note: use [C, P, sumblk] for proper state order)
P = ct.tf2io(ct.tf(1, [1, 0]), inputs='u', outputs='y')
C = ct.tf2io(ct.tf(10, [1, 1]), inputs='e', outputs='u')
sumblk = ct.summing_junction(inputs=['r', '-y'], output='e')
T = ct.interconnect([C, P, sumblk], inplist='r', outlist='y')
T_ss = ct.feedback(P * C, 1)
np.testing.assert_almost_equal(T.A, T_ss.A)
np.testing.assert_almost_equal(T.B, T_ss.B)
np.testing.assert_almost_equal(T.C, T_ss.C)
np.testing.assert_almost_equal(T.D, T_ss.D)