@@ -2,34 +2,29 @@ using ModelingToolkit, Test
22using ModelingToolkitStandardLibrary. Blocks
33using OrdinaryDiffEq
44
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 )]
76
87y = ModelingToolkit. promote_to_concrete (x)
98@test eltype (y) == Union{Float64, Parameter{Float64}, Vector{Int64}}
109
11- y = ModelingToolkit. promote_to_concrete (x; tofloat= false )
10+ y = ModelingToolkit. promote_to_concrete (x; tofloat = false )
1211@test eltype (y) == Union{Bool, Float64, Int64, Parameter{Float64}, Vector{Int64}}
1312
14-
15- x = [1 , 2.0 , false , [1 ,2 ,3 ]]
13+ x = [1 , 2.0 , false , [1 , 2 , 3 ]]
1614y = ModelingToolkit. promote_to_concrete (x)
1715@test eltype (y) == Union{Float64, Vector{Int64}}
1816
1917x = Any[1 , 2.0 , false ]
20- y = ModelingToolkit. promote_to_concrete (x; tofloat= false )
18+ y = ModelingToolkit. promote_to_concrete (x; tofloat = false )
2119@test eltype (y) == Union{Bool, Float64, Int64}
2220
23- y = ModelingToolkit. promote_to_concrete (x; use_union= false )
21+ y = ModelingToolkit. promote_to_concrete (x; use_union = false )
2422@test eltype (y) == Float64
2523
26- x = Float16[1. , 2. , 3. ]
24+ x = Float16[1.0 , 2.0 , 3.0 ]
2725y = ModelingToolkit. promote_to_concrete (x)
2826@test eltype (y) == Float16
2927
30-
31-
32-
3328# ------------------------ Mixed Single Values and Vector
3429
3530dt = 4e-4
@@ -74,7 +69,7 @@ eqs = [y ~ src.output.u
7469@named sys = ODESystem (eqs, t, vars, []; systems = [int, src])
7570s = complete (sys)
7671sys = 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 )
7873@test prob. p isa Tuple{Vector{Float64}, Vector{Int}, Vector{Vector{Float64}}}
7974sol = solve (prob, ImplicitEuler ());
8075@test sol. retcode == ReturnCode. Success
@@ -83,18 +78,15 @@ sol = solve(prob, ImplicitEuler());
8378# TODO : remake becomes more complicated now, how to improve?
8479defs = ModelingToolkit. defaults (sys)
8580defs[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 )
8782p′, = 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′)
8984sol = solve (prob′, ImplicitEuler ());
9085@test sol. retcode == ReturnCode. Success
9186@test sol[y][end ] == 2 x[end ]
9287
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
9890
9991# ------------------------ Mixed Type Converted to float (default behavior)
10092
@@ -122,11 +114,6 @@ prob = ODEProblem(sys, [], tspan, []; tofloat = false)
122114sol = solve (prob, ImplicitEuler ());
123115@test sol. retcode == ReturnCode. Success
124116
125-
126-
127-
128-
129-
130117# ------------------------- Bug
131118using ModelingToolkit, LinearAlgebra
132119using ModelingToolkitStandardLibrary. Mechanical. Rotational
@@ -136,51 +123,48 @@ using ModelingToolkit: connect
136123
137124" A wrapper function to make symbolic indexing easier"
138125function 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 )
140127end
141- indexof (sym,syms) = findfirst (isequal (sym),syms)
128+ indexof (sym, syms) = findfirst (isequal (sym), syms)
142129
143130# 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
148135
149136@named inertia1 = Inertia (; J = m1)
150137@named inertia2 = Inertia (; J = m2)
151138@named spring = Spring (; c = k)
152139@named damper = Damper (; d = c)
153- @named torque = Torque (use_support= false )
140+ @named torque = Torque (use_support = false )
154141
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)
158144 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)]
161146 if u != = nothing
162147 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])
164151 end
165152 ODESystem (eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name)
166153end
167154
168-
169155model = 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
171157model_outputs = [model. inertia1. w, model. inertia2. w, model. inertia1. phi, model. inertia2. phi] # This is the state realization we want to control
172158inputs = [model. torque. tau. u]
173159matrices, ssys = ModelingToolkit. linearize (wr (model), inputs, model_outputs)
174160
175161# Design state-feedback gain using LQR
176162# 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
184168
185169# This old definition of MatrixGain will work because the parameter space does not include K (an Array term)
186170# @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
191175# compose(ODESystem(eqs, t, [], []; name = name), [input, output])
192176# end
193177
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
196180
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 ]
199182 connect (d. output, :d , add. input1)
200183 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])
204186S = get_sensitivity (closed_loop, :u )
205-
206-
0 commit comments