Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add conversions for LTI systems in interconnect()
  • Loading branch information
murrayrm committed Nov 12, 2022
commit 45c3abdb534e8421c2582a7a1da8ecd6f10e1cbd
8 changes: 7 additions & 1 deletion control/iosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,12 @@ def __init__(self, syslist, connections=[], inplist=[], outlist=[],

# Go through the system list and keep track of counts, offsets
for sysidx, sys in enumerate(syslist):
# If we were passed a SS or TF system, convert to LinearIOSystem
if isinstance(sys, (StateSpace, TransferFunction)) and \
not isinstance(sys, LinearIOSystem):
sys = LinearIOSystem(sys)
syslist[sysidx] = sys

# Make sure time bases are consistent
dt = common_timebase(dt, sys.dt)

Expand Down Expand Up @@ -1781,7 +1787,7 @@ def input_output_response(

# Update the parameter values
sys._update_params(params)

#
# Define a function to evaluate the input at an arbitrary time
#
Expand Down
42 changes: 39 additions & 3 deletions control/tests/type_conversion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def sys_dict():
rtype_list = ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']
conversion_table = [
# op left ss tf frd lio ios arr flt
('add', 'ss', ['ss', 'ss', 'frd', 'ss', 'ios', 'ss', 'ss' ]),
('add', 'ss', ['ss', 'ss', 'frd', 'lio', 'ios', 'ss', 'ss' ]),
('add', 'tf', ['tf', 'tf', 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('add', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('add', 'lio', ['lio', 'lio', 'xrd', 'lio', 'ios', 'lio', 'lio']),
Expand All @@ -68,7 +68,7 @@ def sys_dict():
('add', 'flt', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']),

# op left ss tf frd lio ios arr flt
('sub', 'ss', ['ss', 'ss', 'frd', 'ss', 'ios', 'ss', 'ss' ]),
('sub', 'ss', ['ss', 'ss', 'frd', 'lio', 'ios', 'ss', 'ss' ]),
('sub', 'tf', ['tf', 'tf', 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('sub', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('sub', 'lio', ['lio', 'lio', 'xrd', 'lio', 'ios', 'lio', 'lio']),
Expand All @@ -77,7 +77,7 @@ def sys_dict():
('sub', 'flt', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']),

# op left ss tf frd lio ios arr flt
('mul', 'ss', ['ss', 'ss', 'frd', 'ss', 'ios', 'ss', 'ss' ]),
('mul', 'ss', ['ss', 'ss', 'frd', 'lio', 'ios', 'ss', 'ss' ]),
('mul', 'tf', ['tf', 'tf', 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('mul', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('mul', 'lio', ['lio', 'lio', 'xrd', 'lio', 'ios', 'lio', 'lio']),
Expand Down Expand Up @@ -191,3 +191,39 @@ def test_binary_op_type_conversions(opname, ltype, rtype, sys_dict):
assert len(result.output_labels) == result.noutputs
if result.nstates is not None:
assert len(result.state_labels) == result.nstates

@pytest.mark.parametrize(
"typelist, connections, inplist, outlist, expected", [
(['lio', 'lio'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'lio'),
(['lio', 'ss'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'lio'),
(['ss', 'lio'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'lio'),
(['ss', 'ss'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'lio'),
(['lio', 'tf'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'lio'),
(['lio', 'frd'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'E'),
(['ios', 'ios'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'ios'),
(['lio', 'ios'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'ios'),
(['ss', 'ios'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'ios'),
(['tf', 'ios'], [[(1, 0), (0, 0)]], [[(0, 0)]], [[(1, 0)]], 'ios'),
(['lio', 'ss', 'tf'],
[[(1, 0), (0, 0)], [(2, 0), (1, 0)]], [[(0, 0)]], [[(2, 0)]], 'lio'),
(['ios', 'ss', 'tf'],
[[(1, 0), (0, 0)], [(2, 0), (1, 0)]], [[(0, 0)]], [[(2, 0)]], 'ios'),
])
def test_interconnect(
typelist, connections, inplist, outlist, expected, sys_dict):
# Create the system list
syslist = [sys_dict[_type] for _type in typelist]

# Make copies of any duplicates
for sysidx, sys in enumerate(syslist):
if sys == syslist[0]:
syslist[sysidx] = sys.copy()

# Make sure we get the right result
if expected == 'E' or expected[0] == 'x':
# Exception expected
with pytest.raises(TypeError):
result = ct.interconnect(syslist, connections, inplist, outlist)
else:
result = ct.interconnect(syslist, connections, inplist, outlist)
assert isinstance(result, type_dict[expected])