Skip to content

Commit caedda6

Browse files
Merge pull request #3995 from hexaeder/hw/addtest
add test for passing params as structural params
2 parents 1980ec8 + dc1adfc commit caedda6

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

test/model_parsing.jl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,3 +1089,89 @@ end
10891089
@test ModelingToolkit.getmetadata(test_model, MyBool, nothing) === false
10901090
@test ModelingToolkit.getmetadata(test_model, NewInt, nothing) === 1
10911091
end
1092+
1093+
@testset "Pass parameters of higher level models as structural parameters" begin
1094+
let D=ModelingToolkit.D_nounits, t=ModelingToolkit.t_nounits
1095+
"""
1096+
╭─────────╮
1097+
in │ K │ out
1098+
╶─>─┤ ------- ├──>─╴
1099+
│ 1 + s T │
1100+
╰─────────╯
1101+
"""
1102+
@mtkmodel SimpleLag begin
1103+
@structural_parameters begin
1104+
K # Gain
1105+
T # Time constant
1106+
end
1107+
@variables begin
1108+
in(t), [description="Input signal", input=true]
1109+
out(t), [description="Output signal", output=true]
1110+
end
1111+
@equations begin
1112+
T * D(out) ~ K*in - out
1113+
end
1114+
end
1115+
1116+
"""
1117+
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓
1118+
┃ DoubleLag ┃
1119+
┃ ╭─────────╮ ╭─────────╮ ┃
1120+
in ┃ │ K1 │ │ K2 │ ┃ out
1121+
─>──╂─┤ ------- ├──┤ ------- ├─╂──>──╴
1122+
┃ │ 1 + sT1 │ │ 1 + sT2 │ ┃
1123+
┃ ╰─────────╯ ╰─────────╯ ┃
1124+
┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1125+
"""
1126+
@mtkmodel DoubleLag begin
1127+
@parameters begin
1128+
K1, [description="Proportional gain 1"]
1129+
T1, [description="Time constant 1"]
1130+
K2, [description="Proportional gain 2"]
1131+
T2, [description="Time constant 2"]
1132+
end
1133+
@components begin
1134+
lag1 = SimpleLag(K = K1, T = T1)
1135+
lag2 = SimpleLag(K = K2, T = T2)
1136+
end
1137+
@variables begin
1138+
in(t), [description="Input signal", input=true]
1139+
out(t), [description="Output signal", output=true]
1140+
end
1141+
@equations begin
1142+
in ~ lag1.in
1143+
lag1.out ~ lag2.in
1144+
out ~ lag2.out
1145+
end
1146+
end
1147+
1148+
@mtkmodel ClosedSystem begin
1149+
@components begin
1150+
double_lag = DoubleLag(; K1 = 1, K2 = 2, T1 = 0.1, T2 = 0.2)
1151+
end
1152+
@equations begin
1153+
double_lag.in ~ 1.0
1154+
end
1155+
end
1156+
1157+
@mtkbuild sys = ClosedSystem()
1158+
@test length(parameters(sys)) == 4
1159+
@test length(unknowns(sys)) == 2
1160+
1161+
p = MTKParameters(sys, defaults(sys))
1162+
u = [0.5 for i in 1:2]
1163+
du = zeros(2)
1164+
# update du for given u and p
1165+
ODEFunction(sys).f.f_iip(du, u, p, 0.0)
1166+
1167+
# find indices of lag1 and lag2 states (might be reordered due to simplification details)
1168+
symnames = string.(ModelingToolkit.getname.(variable_symbols(sys)))
1169+
lag1idx = findall(contains("1"), symnames) |> only
1170+
lag2idx = findall(contains("2"), symnames) |> only
1171+
1172+
# check du values
1173+
K1, K2, T1, T2 = 1, 2, 0.1, 0.2
1174+
@test du[lag1idx] (K1*1.0 - u[lag1idx]) / T1
1175+
@test du[lag2idx] (K2*u[lag1idx] - u[lag2idx]) / T2
1176+
end
1177+
end

0 commit comments

Comments
 (0)