From 7aaa7fc8870d3ffeaf0d0ce8d451cf2982c40888 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 22 Jan 2025 06:23:30 +0100 Subject: [PATCH] add more tests --- src/named_systems2.jl | 10 +++++---- test/test_named_systems2.jl | 45 ++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/named_systems2.jl b/src/named_systems2.jl index ed97448..98c3ba6 100644 --- a/src/named_systems2.jl +++ b/src/named_systems2.jl @@ -25,18 +25,20 @@ function check_all_unique(s1, s2; throw=true) end function generate_unique_x_names(systems...) - x_names = [s.x for s in systems] + x_names = reduce(vcat, s.x for s in systems) uniq = check_unique(x_names, "x", throw = false) if uniq return x_names else # For each system, if it has a name, append the system name to the x names. If neither system has a name, append gensym names to both systems' x names. systemnames = [s.name for s in systems] - x_names = if any(isempty(s.name) for s in systems) || length(unique(systemnames)) < length(systemnames) - # Any name is empty or more than one system has the same name + x_names = if count(isempty(s.name) for s in systems) > 1 || length(unique(systemnames)) < length(systemnames) + # More than one system has empty name or more than one system has the same name + # We can handle one of the names being empty, which is a common case when the plant has a name but a controller/filter is auto promoted to a named system. [gensym(string(x)) for x in x_names] else - reduce(vcat, [[Symbol(string(s.name)*string(x)) for x in s.x] for s in systems]) + # reduce(vcat, [[Symbol(string(s.name)*string(x)) for x in s.x] for s in systems]) + [Symbol(string(s.name)*string(x)) for s in systems for x in s.x] end @assert allunique(x_names) x_names diff --git a/test/test_named_systems2.jl b/test/test_named_systems2.jl index 5e025f0..1efa7d8 100644 --- a/test/test_named_systems2.jl +++ b/test/test_named_systems2.jl @@ -1,5 +1,5 @@ using RobustAndOptimalControl, ControlSystemsBase, LinearAlgebra, Test, Plots -using RobustAndOptimalControl: @check_unique, @check_all_unique +using RobustAndOptimalControl: check_unique, check_all_unique @test :x^3 == expand_symbol(:x, 3) == [:x1, :x2, :x3] @@ -150,7 +150,7 @@ G1 = ss(1,1,1,0) G2 = ss(1,1,1,0) s1 = named_ss(G1, x = :x, u = :u1, y=:y1) s2 = named_ss(G2, x = :z, u = :u2, y=:y2) -@test_nowarn @check_all_unique s1 s2 +@test_nowarn check_all_unique(s1, s2) s1e = ExtendedStateSpace(s1, y=s1.y, u=s1.u, z=[], w=[]) @test s1.sys == system_mapping(s1e) @@ -171,7 +171,46 @@ fb = feedback(s1, s2) s1 = named_ss(G1, x = [:x], u = [:u1], y=[:y1]) s2 = named_ss(G2, x = [:z], u = [:u1], y=[:y2]) -@test_throws ArgumentError @check_all_unique s1 s2 +@test_throws ArgumentError check_all_unique(s1, s2) + +# Same x names, test automatic generation of new names +s1 = named_ss(ssrand(1,1,2), x = :x, u = :u1, y=:y1) +s2 = named_ss(ssrand(1,1,2), x = :x, u = :u2, y=:y2) +s12 = [s1; s2] +@test occursin("x1", string(s12.x[1])) +@test occursin("x2", string(s12.x[2])) +@test occursin("x1", string(s12.x[3])) +@test occursin("x2", string(s12.x[4])) + +# When systems have names, use these to create the new x names +s1 = named_ss(ssrand(1,1,2), "P", x = :x, u = :u1, y=:y1) +s2 = named_ss(ssrand(1,1,2), "C", x = :x, u = :u2, y=:y2) +s12 = [s1; s2] +@test s12.x[1] == :Px1 +@test s12.x[2] == :Px2 +@test s12.x[3] == :Cx1 +@test s12.x[4] == :Cx2 + +# When one system is missing a name, we use the existing name only +s1 = named_ss(ssrand(1,1,2), "P", x = :x, u = :u1, y=:y1) +s2 = named_ss(ssrand(1,1,2), x = :x, u = :u2, y=:y2) +s12 = [s1; s2] +@test s12.x[1] == :Px1 +@test s12.x[2] == :Px2 +@test s12.x[3] == :x1 +@test s12.x[4] == :x2 + +# If more than one system is missing a name, we do not use the system names +s1 = named_ss(ssrand(1,1,2), "P", x = :x, u = :u1, y=:y1) +s2 = named_ss(ssrand(1,1,2), x = :x, u = :u2, y=:y2) +s3 = named_ss(ssrand(1,1,2), x = :x, u = :u3, y=:y3) +s12 = [s1; s2; s3] +@test occursin("x1", string(s12.x[1])) +@test occursin("x2", string(s12.x[2])) +@test occursin("x1", string(s12.x[3])) +@test occursin("x2", string(s12.x[4])) +@test occursin("x1", string(s12.x[5])) +@test occursin("x2", string(s12.x[6])) ## Promotion and conversion @testset "Promotion and conversion" begin