forked from SciML/ModelingToolkit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis_points.jl
181 lines (155 loc) · 6.41 KB
/
analysis_points.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks
using OrdinaryDiffEq, LinearAlgebra
using Test
using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem
import ModelingToolkit as MTK
using Symbolics: NAMESPACE_SEPARATOR
@testset "AnalysisPoint is lowered to `connect`" begin
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = -1)
ap = AnalysisPoint(:plant_input)
eqs = [connect(P.output, C.input)
connect(C.output, ap, P.input)]
sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej)
sys_ap2 = @test_nowarn expand_connections(sys_ap)
@test all(eq -> !(eq.lhs isa AnalysisPoint), equations(sys_ap2))
eqs = [connect(P.output, C.input)
connect(C.output, P.input)]
sys_normal = ODESystem(eqs, t, systems = [P, C], name = :hej)
sys_normal2 = @test_nowarn expand_connections(sys_normal)
@test isequal(sys_ap2, sys_normal2)
end
@testset "Inverse causality throws a warning" begin
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = -1)
ap = AnalysisPoint(:plant_input)
@test_warn ["1-th argument", "plant_input", "not a output"] connect(
P.input, ap, C.output)
@test_nowarn connect(P.input, ap, C.output; verbose = false)
end
# also tests `connect(input, name::Symbol, outputs...)` syntax
@testset "AnalysisPoint is accessible via `getproperty`" begin
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = -1)
eqs = [connect(P.output, C.input), connect(C.output, :plant_input, P.input)]
sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej)
ap2 = @test_nowarn sys_ap.plant_input
@test nameof(ap2) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR))
@named sys = ODESystem(Equation[], t; systems = [sys_ap])
ap3 = @test_nowarn sys.hej.plant_input
@test nameof(ap3) == Symbol(join(["sys", "hej", "plant_input"], NAMESPACE_SEPARATOR))
sys = complete(sys)
ap4 = sys.hej.plant_input
@test nameof(ap4) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR))
end
### Ported from MTKStdlib
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = -1)
ap = AnalysisPoint(:plant_input)
eqs = [connect(P.output, C.input), connect(C.output, ap, P.input)]
sys = ODESystem(eqs, t, systems = [P, C], name = :hej)
@named nested_sys = ODESystem(Equation[], t; systems = [sys])
@testset "simplifies and solves" begin
ssys = structural_simplify(sys)
prob = ODEProblem(ssys, [P.x => 1], (0, 10))
sol = solve(prob, Rodas5())
@test norm(sol.u[1]) >= 1
@test norm(sol.u[end]) < 1e-6 # This fails without the feedback through C
end
test_cases = [
("inner", sys, sys.plant_input),
("nested", nested_sys, nested_sys.hej.plant_input),
("inner - Symbol", sys, :plant_input),
("nested - Symbol", nested_sys, nameof(sys.plant_input))
]
@testset "get_sensitivity - $name" for (name, sys, ap) in test_cases
matrices, _ = get_sensitivity(sys, ap)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 1
end
@testset "get_comp_sensitivity - $name" for (name, sys, ap) in test_cases
matrices, _ = get_comp_sensitivity(sys, ap)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == 1 # both positive or negative
@test matrices.D[] == 0
end
#=
# Equivalent code using ControlSystems. This can be used to verify the expected results tested for above.
using ControlSystemsBase
P = tf(1.0, [1, 1])
C = 1 # Negative feedback assumed in ControlSystems
S = sensitivity(P, C) # or feedback(1, P*C)
T = comp_sensitivity(P, C) # or feedback(P*C)
=#
@testset "get_looptransfer - $name" for (name, sys, ap) in test_cases
matrices, _ = get_looptransfer(sys, ap)
@test matrices.A[] == -1
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 0
end
#=
# Equivalent code using ControlSystems. This can be used to verify the expected results tested for above.
using ControlSystemsBase
P = tf(1.0, [1, 1])
C = -1
L = P*C
=#
@testset "open_loop - $name" for (name, sys, ap) in test_cases
open_sys, (du, u) = open_loop(sys, ap)
matrices, _ = linearize(open_sys, [du], [u])
@test matrices.A[] == -1
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 0
end
# Multiple analysis points
eqs = [connect(P.output, :plant_output, C.input)
connect(C.output, :plant_input, P.input)]
sys = ODESystem(eqs, t, systems = [P, C], name = :hej)
@named nested_sys = ODESystem(Equation[], t; systems = [sys])
test_cases = [
("inner", sys, sys.plant_input),
("nested", nested_sys, nested_sys.hej.plant_input),
("inner - Symbol", sys, :plant_input),
("nested - Symbol", nested_sys, nameof(sys.plant_input))
]
@testset "get_sensitivity - $name" for (name, sys, ap) in test_cases
matrices, _ = get_sensitivity(sys, ap)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 1
end
@testset "linearize - $name" for (name, sys, inputap, outputap) in [
("inner", sys, sys.plant_input, sys.plant_output),
("nested", nested_sys, nested_sys.hej.plant_input, nested_sys.hej.plant_output)
]
inputname = Symbol(join(
MTK.namespace_hierarchy(nameof(inputap))[2:end], NAMESPACE_SEPARATOR))
outputname = Symbol(join(
MTK.namespace_hierarchy(nameof(outputap))[2:end], NAMESPACE_SEPARATOR))
@testset "input - $(typeof(input)), output - $(typeof(output))" for (input, output) in [
(inputap, outputap),
(inputname, outputap),
(inputap, outputname),
(inputname, outputname),
(inputap, [outputap]),
(inputname, [outputap]),
(inputap, [outputname]),
(inputname, [outputname])
]
matrices, _ = linearize(sys, input, output)
# Result should be the same as feedpack(P, 1), i.e., the closed-loop transfer function from plant input to plant output
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == 1 # both positive
@test matrices.D[] == 0
end
end
@testset "linearize - variable output - $name" for (name, sys, input, output) in [
("inner", sys, sys.plant_input, P.output.u),
("nested", nested_sys, nested_sys.hej.plant_input, sys.P.output.u)
]
matrices, _ = linearize(sys, input, [output])
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == 1 # both positive
@test matrices.D[] == 0
end