@@ -2,34 +2,29 @@ using ModelingToolkit, Test
2
2
using ModelingToolkitStandardLibrary. Blocks
3
3
using OrdinaryDiffEq
4
4
5
-
6
- x = [1 , 2.0 , false , [1 ,2 ,3 ], Parameter (1.0 )]
5
+ x = [1 , 2.0 , false , [1 , 2 , 3 ], Parameter (1.0 )]
7
6
8
7
y = ModelingToolkit. promote_to_concrete (x)
9
8
@test eltype (y) == Union{Float64, Parameter{Float64}, Vector{Int64}}
10
9
11
- y = ModelingToolkit. promote_to_concrete (x; tofloat= false )
10
+ y = ModelingToolkit. promote_to_concrete (x; tofloat = false )
12
11
@test eltype (y) == Union{Bool, Float64, Int64, Parameter{Float64}, Vector{Int64}}
13
12
14
-
15
- x = [1 , 2.0 , false , [1 ,2 ,3 ]]
13
+ x = [1 , 2.0 , false , [1 , 2 , 3 ]]
16
14
y = ModelingToolkit. promote_to_concrete (x)
17
15
@test eltype (y) == Union{Float64, Vector{Int64}}
18
16
19
17
x = Any[1 , 2.0 , false ]
20
- y = ModelingToolkit. promote_to_concrete (x; tofloat= false )
18
+ y = ModelingToolkit. promote_to_concrete (x; tofloat = false )
21
19
@test eltype (y) == Union{Bool, Float64, Int64}
22
20
23
- y = ModelingToolkit. promote_to_concrete (x; use_union= false )
21
+ y = ModelingToolkit. promote_to_concrete (x; use_union = false )
24
22
@test eltype (y) == Float64
25
23
26
- x = Float16[1. , 2. , 3. ]
24
+ x = Float16[1.0 , 2.0 , 3.0 ]
27
25
y = ModelingToolkit. promote_to_concrete (x)
28
26
@test eltype (y) == Float16
29
27
30
-
31
-
32
-
33
28
# ------------------------ Mixed Single Values and Vector
34
29
35
30
dt = 4e-4
@@ -74,7 +69,7 @@ eqs = [y ~ src.output.u
74
69
@named sys = ODESystem (eqs, t, vars, []; systems = [int, src])
75
70
s = complete (sys)
76
71
sys = structural_simplify (sys)
77
- prob = ODEProblem (sys, [], (0.0 , t_end), [s. src. data => x]; tofloat= false )
72
+ prob = ODEProblem (sys, [], (0.0 , t_end), [s. src. data => x]; tofloat = false )
78
73
@test prob. p isa Tuple{Vector{Float64}, Vector{Int}, Vector{Vector{Float64}}}
79
74
sol = solve (prob, ImplicitEuler ());
80
75
@test sol. retcode == ReturnCode. Success
@@ -83,18 +78,15 @@ sol = solve(prob, ImplicitEuler());
83
78
# TODO : remake becomes more complicated now, how to improve?
84
79
defs = ModelingToolkit. defaults (sys)
85
80
defs[s. src. data] = 2 x
86
- p′ = ModelingToolkit. varmap_to_vars (defs, parameters (sys); tofloat= false )
81
+ p′ = ModelingToolkit. varmap_to_vars (defs, parameters (sys); tofloat = false )
87
82
p′, = ModelingToolkit. split_parameters_by_type (p′) # NOTE: we need to ensure this is called now before calling remake()
88
- prob′ = remake (prob; p= p′)
83
+ prob′ = remake (prob; p = p′)
89
84
sol = solve (prob′, ImplicitEuler ());
90
85
@test sol. retcode == ReturnCode. Success
91
86
@test sol[y][end ] == 2 x[end ]
92
87
93
- prob′′ = remake (prob; p= [s. src. data => x])
94
- @test prob′′. p isa Tuple
95
-
96
-
97
-
88
+ prob′′ = remake (prob; p = [s. src. data => x])
89
+ @test_broken prob′′. p isa Tuple
98
90
99
91
# ------------------------ Mixed Type Converted to float (default behavior)
100
92
@@ -122,11 +114,6 @@ prob = ODEProblem(sys, [], tspan, []; tofloat = false)
122
114
sol = solve (prob, ImplicitEuler ());
123
115
@test sol. retcode == ReturnCode. Success
124
116
125
-
126
-
127
-
128
-
129
-
130
117
# ------------------------- Bug
131
118
using ModelingToolkit, LinearAlgebra
132
119
using ModelingToolkitStandardLibrary. Mechanical. Rotational
@@ -136,51 +123,48 @@ using ModelingToolkit: connect
136
123
137
124
" A wrapper function to make symbolic indexing easier"
138
125
function wr (sys)
139
- ODESystem (Equation[], ModelingToolkit. get_iv (sys), systems= [sys], name= :a_wrapper )
126
+ ODESystem (Equation[], ModelingToolkit. get_iv (sys), systems = [sys], name = :a_wrapper )
140
127
end
141
- indexof (sym,syms) = findfirst (isequal (sym),syms)
128
+ indexof (sym, syms) = findfirst (isequal (sym), syms)
142
129
143
130
# Parameters
144
- m1 = 1.
145
- m2 = 1.
146
- k = 10. # Spring stiffness
147
- c = 3. # Damping coefficient
131
+ m1 = 1.0
132
+ m2 = 1.0
133
+ k = 10.0 # Spring stiffness
134
+ c = 3.0 # Damping coefficient
148
135
149
136
@named inertia1 = Inertia (; J = m1)
150
137
@named inertia2 = Inertia (; J = m2)
151
138
@named spring = Spring (; c = k)
152
139
@named damper = Damper (; d = c)
153
- @named torque = Torque (use_support= false )
140
+ @named torque = Torque (use_support = false )
154
141
155
- function SystemModel (u= nothing ; name= :model )
156
- eqs = [
157
- connect (torque. flange, inertia1. flange_a)
142
+ function SystemModel (u = nothing ; name = :model )
143
+ eqs = [connect (torque. flange, inertia1. flange_a)
158
144
connect (inertia1. flange_b, spring. flange_a, damper. flange_a)
159
- connect (inertia2. flange_a, spring. flange_b, damper. flange_b)
160
- ]
145
+ connect (inertia2. flange_a, spring. flange_b, damper. flange_b)]
161
146
if u != = nothing
162
147
push! (eqs, connect (torque. tau, u. output))
163
- return @named model = ODESystem (eqs, t; systems = [torque, inertia1, inertia2, spring, damper, u])
148
+ return @named model = ODESystem (eqs,
149
+ t;
150
+ systems = [torque, inertia1, inertia2, spring, damper, u])
164
151
end
165
152
ODESystem (eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name)
166
153
end
167
154
168
-
169
155
model = SystemModel () # Model with load disturbance
170
- @named d = Step (start_time= 1. , duration= 10. , offset= 0. , height= 1. ) # Disturbance
156
+ @named d = Step (start_time = 1.0 , duration = 10.0 , offset = 0.0 , height = 1.0 ) # Disturbance
171
157
model_outputs = [model. inertia1. w, model. inertia2. w, model. inertia1. phi, model. inertia2. phi] # This is the state realization we want to control
172
158
inputs = [model. torque. tau. u]
173
159
matrices, ssys = ModelingToolkit. linearize (wr (model), inputs, model_outputs)
174
160
175
161
# Design state-feedback gain using LQR
176
162
# Define cost matrices
177
- x_costs = [
178
- model. inertia1. w => 1.
179
- model. inertia2. w => 1.
180
- model. inertia1. phi => 1.
181
- model. inertia2. phi => 1.
182
- ]
183
- L = randn (1 ,4 ) # Post-multiply by `C` to get the correct input to the controller
163
+ x_costs = [model. inertia1. w => 1.0
164
+ model. inertia2. w => 1.0
165
+ model. inertia1. phi => 1.0
166
+ model. inertia2. phi => 1.0 ]
167
+ L = randn (1 , 4 ) # Post-multiply by `C` to get the correct input to the controller
184
168
185
169
# This old definition of MatrixGain will work because the parameter space does not include K (an Array term)
186
170
# @component function MatrixGainAlt(K::AbstractArray; name)
@@ -191,16 +175,12 @@ L = randn(1,4) # Post-multiply by `C` to get the correct input to the controller
191
175
# compose(ODESystem(eqs, t, [], []; name = name), [input, output])
192
176
# end
193
177
194
- @named state_feedback = MatrixGain (K= - L) # Build negative feedback into the feedback matrix
195
- @named add = Add (;k1 = 1. , k2= 1. ) # To add the control signal and the disturbance
178
+ @named state_feedback = MatrixGain (K = - L) # Build negative feedback into the feedback matrix
179
+ @named add = Add (; k1 = 1.0 , k2 = 1.0 ) # To add the control signal and the disturbance
196
180
197
- connections = [
198
- [state_feedback. input. u[i] ~ model_outputs[i] for i in 1 : 4 ]
181
+ connections = [[state_feedback. input. u[i] ~ model_outputs[i] for i in 1 : 4 ]
199
182
connect (d. output, :d , add. input1)
200
183
connect (add. input2, state_feedback. output)
201
- connect (add. output, :u , model. torque. tau)
202
- ]
203
- closed_loop = ODESystem (connections, t, systems= [model, state_feedback, add, d], name= :closed_loop )
184
+ connect (add. output, :u , model. torque. tau)]
185
+ @named closed_loop = ODESystem (connections, t, systems = [model, state_feedback, add, d])
204
186
S = get_sensitivity (closed_loop, :u )
205
-
206
-
0 commit comments