Skip to content

Commit fa74211

Browse files
Merge pull request #3403 from AayushSabharwal/as/def-complete
fix: fix `DEF` parameters for `split = true, flatten = false` systems
2 parents d56ca27 + 9d35146 commit fa74211

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

src/systems/abstractsystem.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,9 @@ function complete(sys::AbstractSystem; split = true, flatten = true)
769769
end
770770
if split && has_index_cache(sys)
771771
@set! sys.index_cache = IndexCache(sys)
772-
all_ps = get_ps(sys)
772+
# Ideally we'd do `get_ps` but if `flatten = false`
773+
# we don't get all of them. So we call `parameters`.
774+
all_ps = parameters(sys; initial_parameters = true)
773775
if !isempty(all_ps)
774776
# reorder parameters by portions
775777
ps_split = reorder_parameters(sys, all_ps)

src/systems/systems.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function structural_simplify(
5050
newsys = pass(newsys)
5151
end
5252
if newsys isa ODESystem || has_parent(newsys)
53-
@set! newsys.parent = complete(sys; split, flatten = false)
53+
@set! newsys.parent = complete(sys; split = false, flatten = false)
5454
end
5555
newsys = complete(newsys; split)
5656
if newsys′ isa Tuple

test/fmi/fmi.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus")
3232
@testset "v2, CS" begin
3333
fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :CS)
3434
@named inner = MTK.FMIComponent(
35-
Val(2); fmu, communication_step_size = 0.001, type = :CS)
35+
Val(2); fmu, communication_step_size = 1e-5, type = :CS)
3636
@variables x(t) = 1.0
3737
@mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner])
3838
test_no_inputs_outputs(sys)
@@ -66,7 +66,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus")
6666
@testset "v3, CS" begin
6767
fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :CS)
6868
@named inner = MTK.FMIComponent(
69-
Val(3); fmu, communication_step_size = 0.001, type = :CS)
69+
Val(3); fmu, communication_step_size = 1e-5, type = :CS)
7070
@variables x(t) = 1.0
7171
@mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner])
7272
test_no_inputs_outputs(sys)
@@ -210,7 +210,7 @@ end
210210
@testset "v3, CS" begin
211211
fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS)
212212
@named sspace = MTK.FMIComponent(
213-
Val(3); fmu, communication_step_size = 1e-4, type = :CS,
213+
Val(3); fmu, communication_step_size = 1e-6, type = :CS,
214214
reinitializealg = BrownFullBasicInit())
215215
@test MTK.isinput(sspace.u)
216216
@test MTK.isoutput(sspace.y)
@@ -259,9 +259,9 @@ end
259259
@testset "v2, CS" begin
260260
fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS)
261261
@named adder1 = MTK.FMIComponent(
262-
Val(2); fmu, type = :CS, communication_step_size = 1e-3)
262+
Val(2); fmu, type = :CS, communication_step_size = 1e-5)
263263
@named adder2 = MTK.FMIComponent(
264-
Val(2); fmu, type = :CS, communication_step_size = 1e-3)
264+
Val(2); fmu, type = :CS, communication_step_size = 1e-5)
265265
sys, prob = build_looped_adders(adder1, adder2)
266266
sol = solve(prob,
267267
Tsit5();
@@ -300,9 +300,9 @@ end
300300
@testset "v3, CS" begin
301301
fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS)
302302
@named sspace1 = MTK.FMIComponent(
303-
Val(3); fmu, type = :CS, communication_step_size = 1e-4)
303+
Val(3); fmu, type = :CS, communication_step_size = 1e-5)
304304
@named sspace2 = MTK.FMIComponent(
305-
Val(3); fmu, type = :CS, communication_step_size = 1e-4)
305+
Val(3); fmu, type = :CS, communication_step_size = 1e-5)
306306
sys, prob = build_looped_sspace(sspace1, sspace2)
307307
sol = solve(prob, Rodas5P(autodiff = false); reltol = 1e-8)
308308
@test SciMLBase.successful_retcode(sol)

test/split_parameters.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,40 @@ end
275275
@test_nowarn sol = solve(prob)
276276
end
277277
end
278+
279+
@testset "" begin
280+
@mtkmodel SubSystem begin
281+
@parameters begin
282+
c = 1
283+
end
284+
@variables begin
285+
x(t)
286+
end
287+
@equations begin
288+
D(x) ~ c * x
289+
end
290+
end
291+
292+
@mtkmodel System begin
293+
@components begin
294+
subsys = SubSystem()
295+
end
296+
@parameters begin
297+
k = 1
298+
end
299+
@variables begin
300+
y(t)
301+
end
302+
@equations begin
303+
D(y) ~ k * y + subsys.x
304+
end
305+
end
306+
307+
@named sys = System()
308+
sysref = complete(sys)
309+
sys2 = complete(sys; split = true, flatten = false)
310+
ps = Set(full_parameters(sys2))
311+
@test sysref.k in ps
312+
@test sysref.subsys.c in ps
313+
@test length(ps) == 2
314+
end

0 commit comments

Comments
 (0)