Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Jan 22, 2025
1 parent 5366a2d commit 7aaa7fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/named_systems2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 42 additions & 3 deletions test/test_named_systems2.jl
Original file line number Diff line number Diff line change
@@ -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]

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 7aaa7fc

Please sign in to comment.