|
| 1 | +using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks |
| 2 | +using ModelingToolkit: t_nounits as t, D_nounits as D |
| 3 | + |
| 4 | +@testset "Error checking" begin |
| 5 | + @variables begin |
| 6 | + x(t) |
| 7 | + y(t), [input = true] |
| 8 | + z(t), [output = true] |
| 9 | + w(t) |
| 10 | + v(t), [input = true] |
| 11 | + u(t), [output = true] |
| 12 | + xarr(t)[1:4], [output = true] |
| 13 | + yarr(t)[1:2, 1:2], [input = true] |
| 14 | + end |
| 15 | + @parameters begin |
| 16 | + p, [input = true] |
| 17 | + q, [output = true] |
| 18 | + end |
| 19 | + |
| 20 | + @test_throws ["p", "kind", "VARIABLE", "PARAMETER"] connect(z, p) |
| 21 | + @test_throws ["q", "kind", "VARIABLE", "PARAMETER"] connect(q, y) |
| 22 | + @test_throws ["p", "kind", "VARIABLE", "PARAMETER"] connect(z, y, p) |
| 23 | + |
| 24 | + @test_throws ["unique"] connect(z, y, y) |
| 25 | + |
| 26 | + @test_throws ["same size"] connect(xarr, yarr) |
| 27 | + |
| 28 | + @test_throws ["Expected", "x", "output = true", "metadata"] connect(x, y) |
| 29 | + @test_throws ["Expected", "y", "output = true", "metadata"] connect(y, v) |
| 30 | + |
| 31 | + @test_throws ["Expected", "x", "input = true", "metadata"] connect(z, x) |
| 32 | + @test_throws ["Expected", "x", "input = true", "metadata"] connect(z, y, x) |
| 33 | + @test_throws ["Expected", "u", "input = true", "metadata"] connect(z, u) |
| 34 | + @test_throws ["Expected", "u", "input = true", "metadata"] connect(z, y, u) |
| 35 | +end |
| 36 | + |
| 37 | +@testset "Connection expansion" begin |
| 38 | + @named P = FirstOrder(k = 1, T = 1) |
| 39 | + @named C = Gain(; k = -1) |
| 40 | + |
| 41 | + eqs = [connect(P.output.u, C.input.u) |
| 42 | + connect(C.output.u, P.input.u)] |
| 43 | + sys1 = ODESystem(eqs, t, systems = [P, C], name = :hej) |
| 44 | + sys = expand_connections(sys1) |
| 45 | + @test any(isequal(P.output.u ~ C.input.u), equations(sys)) |
| 46 | + @test any(isequal(C.output.u ~ P.input.u), equations(sys)) |
| 47 | + |
| 48 | + @named sysouter = ODESystem(Equation[], t; systems = [sys1]) |
| 49 | + sys = expand_connections(sysouter) |
| 50 | + @test any(isequal(sys1.P.output.u ~ sys1.C.input.u), equations(sys)) |
| 51 | + @test any(isequal(sys1.C.output.u ~ sys1.P.input.u), equations(sys)) |
| 52 | +end |
| 53 | + |
| 54 | +@testset "With Analysis Points" begin |
| 55 | + @named P = FirstOrder(k = 1, T = 1) |
| 56 | + @named C = Gain(; k = -1) |
| 57 | + |
| 58 | + ap = AnalysisPoint(:plant_input) |
| 59 | + eqs = [connect(P.output, C.input), connect(C.output.u, ap, P.input.u)] |
| 60 | + sys = ODESystem(eqs, t, systems = [P, C], name = :hej) |
| 61 | + @named nested_sys = ODESystem(Equation[], t; systems = [sys]) |
| 62 | + |
| 63 | + test_cases = [ |
| 64 | + ("inner", sys, sys.plant_input), |
| 65 | + ("nested", nested_sys, nested_sys.hej.plant_input), |
| 66 | + ("inner - Symbol", sys, :plant_input), |
| 67 | + ("nested - Symbol", nested_sys, nameof(sys.plant_input)) |
| 68 | + ] |
| 69 | + |
| 70 | + @testset "get_sensitivity - $name" for (name, sys, ap) in test_cases |
| 71 | + matrices, _ = get_sensitivity(sys, ap) |
| 72 | + @test matrices.A[] == -2 |
| 73 | + @test matrices.B[] * matrices.C[] == -1 # either one negative |
| 74 | + @test matrices.D[] == 1 |
| 75 | + end |
| 76 | + |
| 77 | + @testset "get_comp_sensitivity - $name" for (name, sys, ap) in test_cases |
| 78 | + matrices, _ = get_comp_sensitivity(sys, ap) |
| 79 | + @test matrices.A[] == -2 |
| 80 | + @test matrices.B[] * matrices.C[] == 1 # both positive or negative |
| 81 | + @test matrices.D[] == 0 |
| 82 | + end |
| 83 | + |
| 84 | + @testset "get_looptransfer - $name" for (name, sys, ap) in test_cases |
| 85 | + matrices, _ = get_looptransfer(sys, ap) |
| 86 | + @test matrices.A[] == -1 |
| 87 | + @test matrices.B[] * matrices.C[] == -1 # either one negative |
| 88 | + @test matrices.D[] == 0 |
| 89 | + end |
| 90 | + |
| 91 | + @testset "open_loop - $name" for (name, sys, ap) in test_cases |
| 92 | + open_sys, (du, u) = open_loop(sys, ap) |
| 93 | + matrices, _ = linearize(open_sys, [du], [u]) |
| 94 | + @test matrices.A[] == -1 |
| 95 | + @test matrices.B[] * matrices.C[] == -1 # either one negative |
| 96 | + @test matrices.D[] == 0 |
| 97 | + end |
| 98 | +end |
0 commit comments