From d7d59d8fb9a4e4b1986410a114474beb9f35f15e Mon Sep 17 00:00:00 2001 From: paneendrakumar0 Date: Sun, 18 Oct 2026 23:18:18 +0530 Subject: [PATCH] Fix false positive algebraic loop in interconnect for MIMO feedthrough (#1257) In InterconnectedSystem._compute_static_io, the iteration bound cycle_count was previously capped at len(self.syslist) + 1. For MIMO systems with direct feedthrough, an acyclic feedthrough path can traverse multiple channels of the same subsystem block more than once, requiring more propagation passes than the number of subsystems. Update cycle_count bound to max(len(self.syslist), ninputs) + 1, where ninputs is the total number of internal subsystem inputs. Add unit regression tests covering MIMO feedthrough without algebraic loops and true algebraic loop detection. Fixes #1257. Signed-off-by: paneendrakumar0 --- control/nlsys.py | 10 ++++-- control/tests/interconnect_test.py | 49 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/control/nlsys.py b/control/nlsys.py index 04fda2e40..ebc5d1be6 100644 --- a/control/nlsys.py +++ b/control/nlsys.py @@ -951,10 +951,14 @@ def _compute_static_io(self, t, x, u): ylist = np.zeros((noutputs + ninputs,)) # To allow for feedthrough terms, iterate multiple times to allow - # feedthrough elements to propagate. For n systems, we could need to - # cycle through n+1 times before reaching steady state + # feedthrough elements to propagate. For an interconnected system, + # an acyclic feedthrough path can traverse through multiple channels + # of the same subsystem in MIMO blocks. The maximum number of feedthrough + # propagation steps before reaching steady state (or confirming convergence) + # is therefore bounded by the number of internal inputs ninputs (or + # the number of subsystems). # TODO (later): see if there is a more efficient way to compute - cycle_count = len(self.syslist) + 1 + cycle_count = max(len(self.syslist), ninputs) + 1 while cycle_count > 0: state_index, input_index, output_index = 0, 0, 0 for sys in self.syslist: diff --git a/control/tests/interconnect_test.py b/control/tests/interconnect_test.py index e725b29f6..e9a1bba84 100644 --- a/control/tests/interconnect_test.py +++ b/control/tests/interconnect_test.py @@ -746,3 +746,52 @@ def test_parallel_interconnect(): np.testing.assert_allclose(sys_bd.B, sys_ic.B) np.testing.assert_allclose(sys_bd.C, sys_ic.C) np.testing.assert_allclose(sys_bd.D, sys_ic.D) + + +# Bug identified in issue #1257: MIMO feedthrough path visiting a subsystem multiple times +def test_interconnect_mimo_feedthrough_no_algebraic_loop(): + # 2-input, 2-output plant with direct feedthrough only in g11 + A = np.array([[-2.0, 0.0], [0.0, -3.0]]) + B = np.array([[1.0, 0.0], [0.0, 1.0]]) + C = np.array([[1.0, 0.0], [0.0, 1.0]]) + D = np.array([[1.0, 0.0], [0.0, 0.0]]) + plant = ct.ss( + A, B, C, D, inputs=['u[0]', 'u[1]'], outputs=['y[0]', 'y[1]'], + name='plant') + + # PI controllers with D = 1 + A_pi = np.array([[0.0]]) + B_pi = np.array([[1.0]]) + C_pi = np.array([[1.0]]) + D_pi = np.array([[1.0]]) + pi1 = ct.ss(A_pi, B_pi, C_pi, D_pi, inputs='e[0]', outputs='u[1]', name='pi1') + pi2 = ct.ss(A_pi, B_pi, C_pi, D_pi, inputs='e[1]', outputs='u[0]', name='pi2') + + err = ct.summing_junction( + inputs=['r', '-y'], outputs='e', dimension=2, name='err') + + # Without the fix, this raises RuntimeError: algebraic loop detected + sys = ct.interconnect( + [err, pi1, pi2, plant], inplist=['r'], outlist=['y']) + + # Compare closed loop poles against ct.feedback + K_ss = ct.ss( + np.zeros((2, 2)), + np.eye(2), + np.array([[0.0, 1.0], [1.0, 0.0]]), + np.array([[0.0, 1.0], [1.0, 0.0]]) + ) + expected_poles = np.sort_complex(ct.feedback(plant, K_ss).poles()) + np.testing.assert_allclose( + np.sort_complex(sys.poles()), expected_poles, rtol=1e-5) + + +def test_interconnect_algebraic_loop_detection(): + # True algebraic loop: feedback through static feedthrough blocks + P = ct.ss([], [], [], [[1.0]], inputs='u', outputs='y', name='P') + sumj = ct.summing_junction(inputs=['r', 'y'], outputs='u', name='sumj') + with pytest.raises(RuntimeError, match='algebraic loop detected'): + ct.interconnect( + [sumj, P], connections=[['P.u', 'sumj.u'], ['sumj.y', 'P.y']], + inplist=['r'], outlist=['P.y']) +