Skip to content

fix: fix DEF parameters for split = true, flatten = false systems #3403

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@
"""
Initial(x)

The `Initial` operator. Used by initializaton to store constant constraints on variables

Check warning on line 625 in src/systems/abstractsystem.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"initializaton" should be "initialization".
of a system. See the documentation section on initialization for more information.
"""
struct Initial <: Symbolics.Operator end
Expand Down Expand Up @@ -769,7 +769,9 @@
end
if split && has_index_cache(sys)
@set! sys.index_cache = IndexCache(sys)
all_ps = get_ps(sys)
# Ideally we'd do `get_ps` but if `flatten = false`
# we don't get all of them. So we call `parameters`.
all_ps = parameters(sys; initial_parameters = true)
if !isempty(all_ps)
# reorder parameters by portions
ps_split = reorder_parameters(sys, all_ps)
Expand Down
2 changes: 1 addition & 1 deletion src/systems/systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function structural_simplify(
newsys = pass(newsys)
end
if newsys isa ODESystem || has_parent(newsys)
@set! newsys.parent = complete(sys; split, flatten = false)
@set! newsys.parent = complete(sys; split = false, flatten = false)
end
newsys = complete(newsys; split)
if newsys′ isa Tuple
Expand Down
14 changes: 7 additions & 7 deletions test/fmi/fmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus")
@testset "v2, CS" begin
fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :CS)
@named inner = MTK.FMIComponent(
Val(2); fmu, communication_step_size = 0.001, type = :CS)
Val(2); fmu, communication_step_size = 1e-5, type = :CS)
@variables x(t) = 1.0
@mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner])
test_no_inputs_outputs(sys)
Expand Down Expand Up @@ -66,7 +66,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus")
@testset "v3, CS" begin
fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :CS)
@named inner = MTK.FMIComponent(
Val(3); fmu, communication_step_size = 0.001, type = :CS)
Val(3); fmu, communication_step_size = 1e-5, type = :CS)
@variables x(t) = 1.0
@mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner])
test_no_inputs_outputs(sys)
Expand Down Expand Up @@ -210,7 +210,7 @@ end
@testset "v3, CS" begin
fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS)
@named sspace = MTK.FMIComponent(
Val(3); fmu, communication_step_size = 1e-4, type = :CS,
Val(3); fmu, communication_step_size = 1e-6, type = :CS,
reinitializealg = BrownFullBasicInit())
@test MTK.isinput(sspace.u)
@test MTK.isoutput(sspace.y)
Expand Down Expand Up @@ -259,9 +259,9 @@ end
@testset "v2, CS" begin
fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS)
@named adder1 = MTK.FMIComponent(
Val(2); fmu, type = :CS, communication_step_size = 1e-3)
Val(2); fmu, type = :CS, communication_step_size = 1e-5)
@named adder2 = MTK.FMIComponent(
Val(2); fmu, type = :CS, communication_step_size = 1e-3)
Val(2); fmu, type = :CS, communication_step_size = 1e-5)
sys, prob = build_looped_adders(adder1, adder2)
sol = solve(prob,
Tsit5();
Expand Down Expand Up @@ -300,9 +300,9 @@ end
@testset "v3, CS" begin
fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS)
@named sspace1 = MTK.FMIComponent(
Val(3); fmu, type = :CS, communication_step_size = 1e-4)
Val(3); fmu, type = :CS, communication_step_size = 1e-5)
@named sspace2 = MTK.FMIComponent(
Val(3); fmu, type = :CS, communication_step_size = 1e-4)
Val(3); fmu, type = :CS, communication_step_size = 1e-5)
sys, prob = build_looped_sspace(sspace1, sspace2)
sol = solve(prob, Rodas5P(autodiff = false); reltol = 1e-8)
@test SciMLBase.successful_retcode(sol)
Expand Down
37 changes: 37 additions & 0 deletions test/split_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,40 @@ end
@test_nowarn sol = solve(prob)
end
end

@testset "" begin
@mtkmodel SubSystem begin
@parameters begin
c = 1
end
@variables begin
x(t)
end
@equations begin
D(x) ~ c * x
end
end

@mtkmodel System begin
@components begin
subsys = SubSystem()
end
@parameters begin
k = 1
end
@variables begin
y(t)
end
@equations begin
D(y) ~ k * y + subsys.x
end
end

@named sys = System()
sysref = complete(sys)
sys2 = complete(sys; split = true, flatten = false)
ps = Set(full_parameters(sys2))
@test sysref.k in ps
@test sysref.subsys.c in ps
@test length(ps) == 2
end
Loading