@@ -1109,91 +1109,91 @@ end
11091109 @test ModelingToolkit. getmetadata (test_model, NewInt, nothing ) === 1
11101110end
11111111
1112- # @testset "Pass parameters of higher level models as structural parameters" begin
1113- # let D=ModelingToolkit.D_nounits, t=ModelingToolkit.t_nounits
1114- # """
1115- # ╭─────────╮
1116- # in │ K │ out
1117- # ╶─>─┤ ------- ├──>─╴
1118- # │ 1 + s T │
1119- # ╰─────────╯
1120- # """
1121- # @mtkmodel SimpleLag begin
1122- # @structural_parameters begin
1123- # K # Gain
1124- # T # Time constant
1125- # end
1126- # @variables begin
1127- # in(t), [description="Input signal", input=true]
1128- # out(t), [description="Output signal", output=true]
1129- # end
1130- # @equations begin
1131- # T * D(out) ~ K*in - out
1132- # end
1133- # end
1134-
1135- # """
1136- # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓
1137- # ┃ DoubleLag ┃
1138- # ┃ ╭─────────╮ ╭─────────╮ ┃
1139- # in ┃ │ K1 │ │ K2 │ ┃ out
1140- # ─>──╂─┤ ------- ├──┤ ------- ├─╂──>──╴
1141- # ┃ │ 1 + sT1 │ │ 1 + sT2 │ ┃
1142- # ┃ ╰─────────╯ ╰─────────╯ ┃
1143- # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1144- # """
1145- # @mtkmodel DoubleLag begin
1146- # @parameters begin
1147- # K1, [description="Proportional gain 1"]
1148- # T1, [description="Time constant 1"]
1149- # K2, [description="Proportional gain 2"]
1150- # T2, [description="Time constant 2"]
1151- # end
1152- # @components begin
1153- # lag1 = SimpleLag(K = K1, T = T1)
1154- # lag2 = SimpleLag(K = K2, T = T2)
1155- # end
1156- # @variables begin
1157- # in(t), [description="Input signal", input=true]
1158- # out(t), [description="Output signal", output=true]
1159- # end
1160- # @equations begin
1161- # in ~ lag1.in
1162- # lag1.out ~ lag2.in
1163- # out ~ lag2.out
1164- # end
1165- # end
1166-
1167- # @mtkmodel ClosedSystem begin
1168- # @components begin
1169- # double_lag = DoubleLag(; K1 = 1, K2 = 2, T1 = 0.1, T2 = 0.2)
1170- # end
1171- # @equations begin
1172- # double_lag.in ~ 1.0
1173- # end
1174- # end
1175-
1176- # @mtkbuild sys = ClosedSystem()
1177- # @test length(parameters(sys)) == 4
1178- # @test length(unknowns(sys)) == 2
1179-
1180- # p = MTKParameters(sys, defaults(sys))
1181- # u = [0.5 for i in 1:2]
1182- # du = zeros(2)
1183- # # update du for given u and p
1184- # ODEFunction(sys).f.f_iip(du, u, p, 0.0)
1185-
1186- # # find indices of lag1 and lag2 states (might be reordered due to simplification details)
1187- # symnames = string.(ModelingToolkit.getname.(variable_symbols(sys)))
1188- # lag1idx = findall(contains("1"), symnames) |> only
1189- # lag2idx = findall(contains("2"), symnames) |> only
1190-
1191- # # check du values
1192- # K1, K2, T1, T2 = 1, 2, 0.1, 0.2
1193- # @test du[lag1idx] ≈ (K1*1.0 - u[lag1idx]) / T1
1194- # @test du[lag2idx] ≈ (K2*u[lag1idx] - u[lag2idx]) / T2
1195- # end
1196- # end
1112+ @testset " Pass parameters of higher level models as structural parameters" begin
1113+ let D= ModelingToolkit. D_nounits, t= ModelingToolkit. t_nounits
1114+ """
1115+ ╭─────────╮
1116+ in │ K │ out
1117+ ╶─>─┤ ------- ├──>─╴
1118+ │ 1 + s T │
1119+ ╰─────────╯
1120+ """
1121+ @mtkmodel SimpleLag begin
1122+ @structural_parameters begin
1123+ K # Gain
1124+ T # Time constant
1125+ end
1126+ @variables begin
1127+ in (t), [description= " Input signal" , input= true ]
1128+ out (t), [description= " Output signal" , output= true ]
1129+ end
1130+ @equations begin
1131+ T * D (out) ~ K* in - out
1132+ end
1133+ end
1134+
1135+ """
1136+ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓
1137+ ┃ DoubleLag ┃
1138+ ┃ ╭─────────╮ ╭─────────╮ ┃
1139+ in ┃ │ K1 │ │ K2 │ ┃ out
1140+ ─>──╂─┤ ------- ├──┤ ------- ├─╂──>──╴
1141+ ┃ │ 1 + sT1 │ │ 1 + sT2 │ ┃
1142+ ┃ ╰─────────╯ ╰─────────╯ ┃
1143+ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1144+ """
1145+ @mtkmodel DoubleLag begin
1146+ @parameters begin
1147+ K1, [description= " Proportional gain 1" ]
1148+ T1, [description= " Time constant 1" ]
1149+ K2, [description= " Proportional gain 2" ]
1150+ T2, [description= " Time constant 2" ]
1151+ end
1152+ @components begin
1153+ lag1 = SimpleLag (K = K1, T = T1)
1154+ lag2 = SimpleLag (K = K2, T = T2)
1155+ end
1156+ @variables begin
1157+ in (t), [description= " Input signal" , input= true ]
1158+ out (t), [description= " Output signal" , output= true ]
1159+ end
1160+ @equations begin
1161+ in ~ lag1. in
1162+ lag1. out ~ lag2. in
1163+ out ~ lag2. out
1164+ end
1165+ end
1166+
1167+ @mtkmodel ClosedSystem begin
1168+ @components begin
1169+ double_lag = DoubleLag (; K1 = 1 , K2 = 2 , T1 = 0.1 , T2 = 0.2 )
1170+ end
1171+ @equations begin
1172+ double_lag. in ~ 1.0
1173+ end
1174+ end
1175+
1176+ @mtkbuild sys = ClosedSystem ()
1177+ @test length (parameters (sys)) == 4
1178+ @test length (unknowns (sys)) == 2
1179+
1180+ p = MTKParameters (sys, defaults (sys))
1181+ u = [0.5 for i in 1 : 2 ]
1182+ du = zeros (2 )
1183+ # update du for given u and p
1184+ ODEFunction (sys). f. f_iip (du, u, p, 0.0 )
1185+
1186+ # find indices of lag1 and lag2 states (might be reordered due to simplification details)
1187+ symnames = string .(ModelingToolkit. getname .(variable_symbols (sys)))
1188+ lag1idx = findall (contains (" 1" ), symnames) |> only
1189+ lag2idx = findall (contains (" 2" ), symnames) |> only
1190+
1191+ # check du values
1192+ K1, K2, T1, T2 = 1 , 2 , 0.1 , 0.2
1193+ @test du[lag1idx] ≈ (K1* 1.0 - u[lag1idx]) / T1
1194+ @test du[lag2idx] ≈ (K2* u[lag1idx] - u[lag2idx]) / T2
1195+ end
1196+ end
11971197
11981198@testset " __ kwarg handling" begin
11991199 @mtkmodel Inner begin
0 commit comments