From 815f1c50a7205fe70cefa28c025489f5bb17723a Mon Sep 17 00:00:00 2001 From: Michele Zaffalon Date: Fri, 6 Oct 2023 13:20:01 +0200 Subject: [PATCH] Remove stale connect and use named arguments Update tests accordingly --- examples/complicated_feedback.jl | 4 +-- src/named_systems2.jl | 57 +++++++++++++++----------------- test/runtests.jl | 2 +- test/test_extendedstatespace.jl | 2 +- test/test_manual_hinf.jl | 2 +- test/test_named_systems2.jl | 2 +- 6 files changed, 33 insertions(+), 36 deletions(-) diff --git a/examples/complicated_feedback.jl b/examples/complicated_feedback.jl index ed097f68..8984d580 100644 --- a/examples/complicated_feedback.jl +++ b/examples/complicated_feedback.jl @@ -42,7 +42,7 @@ connections = [ ] w1 = [:uF] -G = ROC.connect([F, R, C, P, addP, addC], connections; w1) +G = ROC.connect([F, R, C, P, addP, addC], connections; w1=w1) @test sminreal(G[:yF, :uF].sys) ≈ F.sys @@ -75,7 +75,7 @@ Sum = named_ss(ss([I(2) -I(2)]), u=[:r1, :r2, :y1, :y2], y=:e) systems = [G,C,Sum] u1 = [:r1, :r2] y1 = [:y1, :y2] -T = ROC.connect(systems; u1, y1, w1=u1) +T = ROC.connect(systems, y1 .=> u1; w1=u1) ## manual diff --git a/src/named_systems2.jl b/src/named_systems2.jl index b39dd271..3f22a3ae 100644 --- a/src/named_systems2.jl +++ b/src/named_systems2.jl @@ -380,35 +380,9 @@ end ControlSystemsBase.feedback(s1::NamedStateSpace{T}, s2::AbstractStateSpace{T}; kwargs...) where {T <: CS.TimeEvolution} = feedback(s1, named_ss(s2); kwargs...) -function connect(systems; u1::Vector{Symbol}, y1::Vector{Symbol}, w1, z1 = (:), verbose = true, kwargs...) - full = append(systems...) - @assert length(y1) == length(u1) - @check_unique u1 "Connected inputs not unique. If you want to connect several signals to the same input, use a summation node, e.g., named_ss(ss([1 1]), u=[:u1, :u2], y=:usum)" - @check_unique full.u "system inputs" - @check_unique full.y "system outputs" - - if verbose - leftover_inputs = setdiff(full.u, [u1; w1]) - isempty(leftover_inputs) || @warn("The following inputs were unconnected $leftover_inputs, ignore this warning if you rely on prefix matching") - leftover_outputs = setdiff(full.y, z1 == (:) ? y1 : [y1; z1]) - isempty(leftover_outputs) || @warn("The following outputs were unconnected $leftover_outputs, ignore this warning if you rely on prefix matching") - end - - - z2 = [] - w2 = [] - - # Connections - y2 = (:) - u2 = (:) - - fb = named_ss(ss(I(length(y1)), full.timeevol)) - G = feedback(full, fb; z1, z2, w1, w2, u1, u2, y1, y2, pos_feedback=true, kwargs...) -end - """ - connect(systems, connections; w1, z1 = (:), verbose = true, kwargs...) + connect(systems, connections, w1, z1 = (:); verbose = true, kwargs...) Create block connections using named inputs and outputs. @@ -459,13 +433,36 @@ connections = [ ] w1 = [:uF] # External inputs -G = connect([F, R, C, P, addP, addC], connections; w1) +G = connect([F, R, C, P, addP, addC], connections, w1) ``` If an external input is to be connected to multiple points, use a `splitter` to split up the signal into a set of unique names which are then used in the connections. """ -function connect(systems, pairs::AbstractVector{<:Pair}; kwargs...) - connect(systems; u1 = last.(pairs), y1 = first.(pairs), kwargs...) +function connect(systems, pairs::AbstractVector{<:Pair}; w1, z1 = (:), verbose = true, kwargs...) + full = append(systems...) + u1 = last.(pairs) + y1 = first.(pairs) + @assert length(y1) == length(u1) + @check_unique u1 "Connected inputs not unique. If you want to connect several signals to the same input, use a summation node, e.g., named_ss(ss([1 1]), u=[:u1, :u2], y=:usum)" + @check_unique full.u "system inputs" + @check_unique full.y "system outputs" + + if verbose + leftover_inputs = setdiff(full.u, [u1; w1]) + isempty(leftover_inputs) || @warn("The following inputs were unconnected $leftover_inputs, ignore this warning if you rely on prefix matching") + leftover_outputs = setdiff(full.y, z1 == (:) ? y1 : [y1; z1]) + isempty(leftover_outputs) || @warn("The following outputs were unconnected $leftover_outputs, ignore this warning if you rely on prefix matching") + end + + z2 = [] + w2 = [] + + # Connections + y2 = (:) + u2 = (:) + + fb = named_ss(ss(I(length(y1)), full.timeevol)) + G = feedback(full, fb; z1, z2, w1, w2, u1, u2, y1, y2, pos_feedback=true, kwargs...) end diff --git a/test/runtests.jl b/test/runtests.jl index 2ff24916..1dddb219 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -106,7 +106,7 @@ using Test include("test_canonical_forms.jl") end - @testset "complicated feedback exmaple" begin + @testset "complicated feedback example" begin @info "Testing complicated feedback exmaple" include("../examples/complicated_feedback.jl") end diff --git a/test/test_extendedstatespace.jl b/test/test_extendedstatespace.jl index 95142768..82eb0a0d 100644 --- a/test/test_extendedstatespace.jl +++ b/test/test_extendedstatespace.jl @@ -52,7 +52,7 @@ S = sumblock("Ku = r - Gy", n=3) z1 = [G.y; K.y] w1 = :r^3 connections = [K.y .=> G.u; G.y .=> G.y; K.u .=> K.u] -Gcl2 = connect([G, K, S], connections; z1, w1) +Gcl2 = connect([G, K, S], connections; z1=z1, w1=w1) @test linfnorm(minreal(Gcl1 - Gcl2.sys))[1] < sqrt(eps()) diff --git a/test/test_manual_hinf.jl b/test/test_manual_hinf.jl index ac932c5a..3cff6bb0 100644 --- a/test/test_manual_hinf.jl +++ b/test/test_manual_hinf.jl @@ -26,7 +26,7 @@ z1 = [ # External outputs :e, :uw, :y ]; -G = connect([P,We,Wu,Wd,sumP,split_u], connections; z1, w1) +G = connect([P,We,Wu,Wd,sumP,split_u], connections; z1=z1, w1=w1) Gsyn = partition(G, u = [:u], y = [:y]) # You can provide either u or w, and either y or z K, γ, info = hinfsynthesize(Gsyn, γrel=1.001, interval = (0.1, 20), transform=false) diff --git a/test/test_named_systems2.jl b/test/test_named_systems2.jl index 21240936..e05fe046 100644 --- a/test/test_named_systems2.jl +++ b/test/test_named_systems2.jl @@ -280,7 +280,7 @@ s2 = named_ss(G2, x = [:z], u = [:u], y=[:y2]) connections = [:y1 => :u, :y2 => :u1] w1 = [:u2] -G = connect([s1,s2], connections; w1) +G = connect([s1,s2], connections; w1=w1) @test G.u == w1 @test G.y == :y^2 @test G.sys == ss(ones(2,2), [1,0], I(2), 0)